1 // mips.cc -- mips target support for gold.
3 // Copyright (C) 2011-2016 Free Software Foundation, Inc.
4 // Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
5 // and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
6 // This file contains borrowed and adapted code from bfd/elfxx-mips.c.
8 // This file is part of gold.
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
33 #include "parameters.h"
40 #include "copy-relocs.h"
42 #include "target-reloc.h"
43 #include "target-select.h"
47 #include "attributes.h"
54 template<int size
, bool big_endian
>
55 class Mips_output_data_plt
;
57 template<int size
, bool big_endian
>
58 class Mips_output_data_got
;
60 template<int size
, bool big_endian
>
63 template<int size
, bool big_endian
>
64 class Mips_output_section_reginfo
;
66 template<int size
, bool big_endian
>
67 class Mips_output_data_la25_stub
;
69 template<int size
, bool big_endian
>
70 class Mips_output_data_mips_stubs
;
75 template<int size
, bool big_endian
>
78 template<int size
, bool big_endian
>
81 class Mips16_stub_section_base
;
83 template<int size
, bool big_endian
>
84 class Mips16_stub_section
;
86 // The ABI says that every symbol used by dynamic relocations must have
87 // a global GOT entry. Among other things, this provides the dynamic
88 // linker with a free, directly-indexed cache. The GOT can therefore
89 // contain symbols that are not referenced by GOT relocations themselves
90 // (in other words, it may have symbols that are not referenced by things
91 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
93 // GOT relocations are less likely to overflow if we put the associated
94 // GOT entries towards the beginning. We therefore divide the global
95 // GOT entries into two areas: "normal" and "reloc-only". Entries in
96 // the first area can be used for both dynamic relocations and GP-relative
97 // accesses, while those in the "reloc-only" area are for dynamic
100 // These GGA_* ("Global GOT Area") values are organised so that lower
101 // values are more general than higher values. Also, non-GGA_NONE
102 // values are ordered by the position of the area in the GOT.
111 // The types of GOT entries needed for this platform.
112 // These values are exposed to the ABI in an incremental link.
113 // Do not renumber existing values without changing the version
114 // number of the .gnu_incremental_inputs section.
117 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
118 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
119 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
121 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
122 GOT_TYPE_STANDARD_MULTIGOT
= 3,
123 GOT_TYPE_TLS_OFFSET_MULTIGOT
= GOT_TYPE_STANDARD_MULTIGOT
+ 1024,
124 GOT_TYPE_TLS_PAIR_MULTIGOT
= GOT_TYPE_TLS_OFFSET_MULTIGOT
+ 1024
127 // TLS type of GOT entry.
136 // Values found in the r_ssym field of a relocation entry.
137 enum Special_relocation_symbol
139 RSS_UNDEF
= 0, // None - value is zero.
140 RSS_GP
= 1, // Value of GP.
141 RSS_GP0
= 2, // Value of GP in object being relocated.
142 RSS_LOC
= 3 // Address of location being relocated.
145 // Whether the section is readonly.
147 is_readonly_section(Output_section
* output_section
)
149 elfcpp::Elf_Xword section_flags
= output_section
->flags();
150 elfcpp::Elf_Word section_type
= output_section
->type();
152 if (section_type
== elfcpp::SHT_NOBITS
)
155 if (section_flags
& elfcpp::SHF_WRITE
)
161 // Return TRUE if a relocation of type R_TYPE from OBJECT might
162 // require an la25 stub. See also local_pic_function, which determines
163 // whether the destination function ever requires a stub.
164 template<int size
, bool big_endian
>
166 relocation_needs_la25_stub(Mips_relobj
<size
, big_endian
>* object
,
167 unsigned int r_type
, bool target_is_16_bit_code
)
169 // We specifically ignore branches and jumps from EF_PIC objects,
170 // where the onus is on the compiler or programmer to perform any
171 // necessary initialization of $25. Sometimes such initialization
172 // is unnecessary; for example, -mno-shared functions do not use
173 // the incoming value of $25, and may therefore be called directly.
174 if (object
->is_pic())
179 case elfcpp::R_MIPS_26
:
180 case elfcpp::R_MIPS_PC16
:
181 case elfcpp::R_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
:
190 case elfcpp::R_MIPS16_26
:
191 return !target_is_16_bit_code
;
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
>
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())))
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
);
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
);
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
);
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
);
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
);
265 eh_reloc(unsigned int r_type
)
267 return (r_type
== elfcpp::R_MIPS_EH
);
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
);
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
);
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
);
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
);
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
);
309 mips16_call_reloc(unsigned int r_type
)
311 return (r_type
== elfcpp::R_MIPS16_26
312 || r_type
== elfcpp::R_MIPS16_CALL16
);
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
);
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.
334 mips16_reloc(unsigned int 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
:
358 // Check if R_TYPE is a microMIPS reloc.
360 micromips_reloc(unsigned int 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
:
404 is_matching_lo16_reloc(unsigned int high_reloc
, unsigned int lo16_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
;
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
>
437 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Mips_address
;
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.
454 is_for_local_symbol() const
455 { return this->symndx_
!= -1U; }
457 // Return whether this entry is for a global symbol.
459 is_for_global_symbol() const
460 { return this->symndx_
== -1U; }
462 // Return the hash of this entry.
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.
479 equals(Mips_got_entry
<size
, big_endian
>* other
) const
481 if (this->tls_type_
== GOT_TLS_LDM
)
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
>*
496 gold_assert(this->symndx_
!= -1U);
497 return this->d
.object
;
500 // Return local symbol index for local GOT entries.
504 gold_assert(this->symndx_
!= -1U);
505 return this->symndx_
;
508 // Return the relocation addend for local GOT entries.
511 { return this->addend_
; }
513 // Return global symbol for global GOT entries.
517 gold_assert(this->symndx_
== -1U);
521 // Return whether this is a TLS GOT entry.
524 { return this->tls_type_
!= GOT_TLS_NONE
; }
526 // Return TLS type of this GOT entry.
529 { return this->tls_type_
; }
531 // Return section index of the local symbol for local GOT entries.
534 { return this->shndx_
; }
536 // Return whether this is a STT_SECTION symbol.
538 is_section_symbol() const
539 { return this->is_section_symbol_
; }
543 Mips_address addend_
;
545 // The index of the symbol if we have a local symbol; -1 otherwise.
546 unsigned int symndx_
;
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
;
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.
569 // Hash for Mips_got_entry.
571 template<int size
, bool big_endian
>
572 class Mips_got_entry_hash
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
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.
595 class Mips_symbol_hash
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
610 : next(NULL
), min_addend(0), max_addend(0)
613 Got_page_range
* next
;
617 // Return the maximum number of GOT page entries required.
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
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.
638 // The index of the symbol, as stored in the relocation r_info.
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
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
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
>
671 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Mips_address
;
672 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>
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
;
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),
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.
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
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.
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.
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.
728 add_local_entries(Target_mips
<size
, big_endian
>* target
, Layout
* layout
);
730 // Create GOT page entries.
732 add_page_entries(Target_mips
<size
, big_endian
>* target
, Layout
* layout
);
734 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
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.
741 add_reloc_only_entries(Mips_output_data_got
<size
, big_endian
>* got
);
743 // Create TLS GOT entries.
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).
752 count_got_symbols(Symbol_table
* symtab
);
754 // Return the offset of GOT page entry for VALUE.
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.
763 // Count the number of GOT entries required by ENTRY. Accumulate the result.
765 count_got_entry(Mips_got_entry
<size
, big_endian
>* entry
);
767 // Add FROM's GOT entries.
769 add_got_entries(Mips_got_info
<size
, big_endian
>* from
);
771 // Add FROM's GOT page entries.
773 add_got_page_entries(Mips_got_info
<size
, big_endian
>* from
);
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.
785 { return this->local_gotno_
; }
787 // Return the maximum number of page GOT entries needed.
790 { return this->page_gotno_
; }
792 // Return the number of global GOT entries.
795 { return this->global_gotno_
; }
797 // Set the number of global GOT entries.
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.
804 reloc_only_gotno() const
805 { return this->reloc_only_gotno_
; }
807 // Return the number of TLS GOT entries.
810 { return this->tls_gotno_
; }
812 // Return the GOT type for this GOT. Used for multi-GOT links only.
814 multigot_got_type(unsigned int got_type
) const
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_
;
829 // Remove lazy-binding stubs for global symbols in this GOT.
831 remove_lazy_stubs(Target_mips
<size
, big_endian
>* target
);
833 // Return offset of this GOT from the start of .got section.
836 { return this->offset_
; }
838 // Set offset of this GOT from the start of .got section.
840 set_offset(unsigned int offset
)
841 { this->offset_
= offset
; }
843 // Set index of this GOT in multi-GOT links.
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
>*
851 { return this->next_
; }
853 // Set next GOT in multi-GOT links.
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.
860 tls_ldm_offset() const
861 { return this->tls_ldm_offset_
; }
863 // Set the offset of TLS LDM entry for this GOT.
865 set_tls_ldm_offset(unsigned int tls_ldm_offset
)
866 { this->tls_ldm_offset_
= tls_ldm_offset
; }
868 Global_got_entry_set
&
870 { return this->global_got_symbols_
; }
872 // Return the GOT_TLS_* type required by relocation type R_TYPE.
874 mips_elf_reloc_tls_type(unsigned int r_type
)
876 if (tls_gd_reloc(r_type
))
879 if (tls_ldm_reloc(r_type
))
882 if (tls_gottprel_reloc(r_type
))
888 // Return the number of GOT slots needed for GOT TLS type TYPE.
890 mips_tls_got_entries(unsigned int type
)
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.
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
>
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
),
957 const Sized_relobj_file
<size
, big_endian
>* object
;
964 // .MIPS.abiflags section content
966 template<bool big_endian
>
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
;
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.
980 // The level of the ISA: 1-5, 32, 64.
982 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
984 // The size of general purpose registers.
986 // The size of co-processor 1 registers.
988 // The size of co-processor 2 registers.
990 // The floating-point ABI.
992 // Processor-specific extension.
994 // Mask of ASEs used.
996 // Mask of general flags.
1001 // Mips_symbol class. Holds additional symbol information needed for Mips.
1004 class Mips_symbol
: public Sized_symbol
<size
>
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.
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.
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.
1037 need_fn_stub() const
1038 { return this->need_fn_stub_
; }
1040 // Set that the symbol needs MIPS16 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.
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.
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.
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.
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.
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).
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
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.
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.
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.
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.
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.
1114 pointer_equality_needed() const
1115 { return this->pointer_equality_needed_
; }
1117 // Set that there are relocations for this symbol where pointer equality
1120 set_pointer_equality_needed()
1121 { this->pointer_equality_needed_
= true; }
1123 // Return global GOT area where this symbol in located.
1125 global_got_area() const
1126 { return this->global_got_area_
; }
1128 // Set global GOT area where this symbol in located.
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.
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.
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.
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.
1158 set_got_not_only_for_calls()
1159 { this->got_only_for_calls_
= false; }
1161 // Return whether this is a PIC symbol.
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.
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));
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.
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));
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.
1215 has_lazy_stub() const
1216 { return this->has_lazy_stub_
; }
1218 // Set whether the symbol has lazy-binding stub.
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.
1225 needs_mips_plt() const
1226 { return this->needs_mips_plt_
; }
1228 // Set whether the symbol needs a standard PLT entry.
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
1236 needs_comp_plt() const
1237 { return this->needs_comp_plt_
; }
1239 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
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.
1246 mips_plt_offset() const
1247 { return this->mips_plt_offset_
; }
1249 // Set standard PLT entry offset.
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.
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.
1261 comp_plt_offset() const
1262 { return this->comp_plt_offset_
; }
1264 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
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.
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.
1284 set_mips16_fn_stub(Mips16_stub_section_base
* stub
)
1285 { this->mips16_fn_stub_
= stub
; }
1287 // Return whether symbol has MIPS16 fn stub.
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
>*>(
1301 // Set MIPS16 call stub for a symbol.
1303 set_mips16_call_stub(Mips16_stub_section_base
* stub
)
1304 { this->mips16_call_stub_
= stub
; }
1306 // Return whether symbol has MIPS16 call stub.
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.
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.
1327 has_mips16_call_fp_stub() const
1328 { return this->mips16_call_fp_stub_
!= NULL
; }
1331 get_applied_secondary_got_fixup() const
1332 { return applied_secondary_got_fixup_
; }
1335 set_applied_secondary_got_fixup()
1336 { this->applied_secondary_got_fixup_
= true; }
1338 // Return the hash of this symbol.
1342 return gold::string_hash
<char>(this->name());
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.
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.
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
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
;
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
>*
1466 { return this->object_
; }
1468 // Return the size of a section.
1470 section_size() const
1471 { return this->object_
->section_size(this->shndx_
); }
1473 // Return section index of this stub section.
1476 { return this->shndx_
; }
1478 // Return symbol index, if stub is for a local function.
1481 { return this->r_sym_
; }
1483 // Return symbol, if stub is for a global function.
1486 { return this->gsym_
; }
1488 // Return whether stub is for a local function.
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.
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
1501 if (this->found_r_mips_none_
)
1503 if (r_type
== elfcpp::R_MIPS_NONE
)
1505 this->r_sym_
= r_sym
;
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.
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
1521 if (this->found_r_mips_none_
)
1523 if (r_type
== elfcpp::R_MIPS_NONE
)
1527 this->found_r_mips_none_
= true;
1529 else if (!is_target_found())
1533 // Return whether we found the stub target.
1535 is_target_found() const
1536 { return this->r_sym_
!= 0 || this->gsym_
!= NULL
; }
1538 // Return whether this is a fn stub.
1541 { return this->object_
->is_mips16_fn_stub_section(this->shndx_
); }
1543 // Return whether this is a call stub.
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.
1550 is_call_fp_stub() const
1551 { return this->object_
->is_mips16_call_fp_stub_section(this->shndx_
); }
1553 // Return the output address.
1555 output_address() const
1557 return (this->object_
->output_section(this->shndx_
)->address()
1558 + this->object_
->output_section_offset(this->shndx_
));
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
;
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());
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
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.
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.
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
;
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.
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
>*>(
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
;
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.
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
>*>(
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
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.
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.
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.
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.
1740 { return this->gp_
; }
1742 // Return whether the object is a PIC object.
1745 { return this->is_pic_
; }
1747 // Return whether the object uses N32 ABI.
1750 { return this->is_n32_
; }
1752 // Return whether the object uses N64 ABI.
1755 { return size
== 64; }
1757 // Return whether the object uses NewABI conventions.
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.
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.
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.
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.
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.
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.
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.
1829 discard_mips16_stub_sections(Symbol_table
* symtab
);
1831 // Return whether there is a .reginfo section.
1833 has_reginfo_section() const
1834 { return this->has_reginfo_section_
; }
1836 // Return gprmask from the .reginfo section of this object.
1839 { return this->gprmask_
; }
1841 // Return cprmask1 from the .reginfo section of this object.
1844 { return this->cprmask1_
; }
1846 // Return cprmask2 from the .reginfo section of this object.
1849 { return this->cprmask2_
; }
1851 // Return cprmask3 from the .reginfo section of this object.
1854 { return this->cprmask3_
; }
1856 // Return cprmask4 from the .reginfo section of this object.
1859 { return this->cprmask4_
; }
1861 // This is the contents of the .MIPS.abiflags section if there is one.
1862 Mips_abiflags
<big_endian
>*
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_
; }
1872 // Count the local symbols.
1874 do_count_local_symbols(Stringpool_template
<char>*,
1875 Stringpool_template
<char>*);
1877 // Read the symbol information.
1879 do_read_symbols(Read_symbols_data
* sd
);
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.
1920 // Whether the object is a PIC object.
1922 // Whether the object uses N32 ABI.
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.
1952 // cprmask1 from the .reginfo section of this object.
1954 // cprmask2 from the .reginfo section of this object.
1956 // cprmask3 from the .reginfo section of this object.
1958 // cprmask4 from the .reginfo section of this object.
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
>
1970 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Valtype
;
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.
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
,
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
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.
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.
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.
2029 add_static_reloc(unsigned int got_offset
, unsigned int r_type
,
2030 Sized_relobj_file
<size
, big_endian
>* relobj
,
2033 this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, relobj
,
2037 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2038 // secondary GOT at OFFSET.
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
,
2047 // Update GOT entry at OFFSET with VALUE.
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.
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());
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
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.
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.
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.
2087 lay_out_multi_got(Layout
* layout
, const Input_objects
* input_objects
);
2089 // Attempt to merge GOTs of different input objects.
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.
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.
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
);
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
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.
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.
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.
2162 { return this->primary_got_
!= NULL
; }
2164 // Return the offset of OBJECT's GOT from the start of .got section.
2166 get_got_offset(const Mips_relobj
<size
, big_endian
>* object
)
2168 if (!this->multi_got())
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.
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.
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
));
2206 // Write out the GOT table.
2208 do_write(Output_file
*);
2212 // This class represent dynamic relocations that need to be applied by
2213 // gold because we are using TLS relocations in a static link.
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.
2233 { return this->got_offset_
; }
2238 { return this->r_type_
; }
2240 // Whether the symbol is global or not.
2242 symbol_is_global() const
2243 { return this->symbol_is_global_
; }
2245 // For a relocation against a global symbol, the global symbol.
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
>*
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.
2265 gold_assert(!this->symbol_is_global_
);
2266 return this->u_
.local
.index
;
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.
2281 // For a global symbol, the symbol itself.
2282 Mips_symbol
<size
>* symbol
;
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.
2295 Target_mips
<size
, big_endian
>* target_
;
2296 // The symbol table.
2297 Symbol_table
* symbol_table_
;
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)
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
;
2338 Mips_output_data_la25_stub()
2339 : Output_section_data(size
== 32 ? 4 : 8), symbols_()
2342 // Create LA25 stub for a symbol.
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.
2349 stub_address(const Mips_symbol
<size
>* sym
) const
2351 gold_assert(sym
->has_la25_stub());
2352 return this->address() + sym
->la25_stub_offset();
2357 do_adjust_output_section(Output_section
* os
)
2358 { os
->set_entsize(0); }
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.
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.
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.
2379 do_print_to_mapfile(Mapfile
* mapfile
) const
2380 { mapfile
->print_output_data(this, _(".LA25.stubs")); }
2382 // Write out the LA25 stub section.
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
;
2402 write(typename
Relocs::const_iterator p
, unsigned char* 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
;
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
);
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
,
2433 Mips_output_data_reloc(bool sort_relocs
)
2434 : Output_data_reloc
<sh_type
, dynamic
, size
, big_endian
>(sort_relocs
)
2438 // Write out the data.
2440 do_write(Output_file
* of
)
2442 typedef Mips_output_reloc_writer
<sh_type
, dynamic
, size
,
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
;
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),
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.
2475 add_entry(Mips_symbol
<size
>* gsym
, unsigned int r_type
);
2477 // Return the .rel.plt section data.
2478 const Reloc_section
*
2480 { return this->rel_
; }
2482 // Return the number of PLT entries.
2485 { return this->symbols_
.size(); }
2487 // Return the offset of the first non-reserved PLT entry.
2489 first_plt_entry_offset() const
2490 { return sizeof(plt0_entry_o32
); }
2492 // Return the size of a PLT entry.
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.
2502 // Return the offset of the first standard PLT entry.
2504 first_mips_plt_offset() const
2505 { return this->plt_header_size_
; }
2507 // Return the offset of the first compressed PLT entry.
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.
2514 has_standard_entries() const
2515 { return this->plt_mips_offset_
> 0; }
2517 // Return the output address of standard PLT entry.
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.
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());
2537 do_adjust_output_section(Output_section
* os
)
2538 { os
->set_entsize(0); }
2540 // Write to a map file.
2542 do_print_to_mapfile(Mapfile
* mapfile
) const
2543 { mapfile
->print_output_data(this, _(".plt")); }
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.
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.
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.
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.
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]));
2598 return (2 * sizeof(plt0_entry_micromips_o32
)
2599 / sizeof(plt0_entry_micromips_o32
[0]));
2602 // Return the PLT header entry.
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
;
2615 return plt0_entry_micromips_o32
;
2618 // Return the size of the standard PLT entry.
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.
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]));
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_
;
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
;
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.
2675 make_entry(Mips_symbol
<size
>*);
2677 // Remove entry for a symbol.
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.
2684 set_lazy_stub_offsets();
2687 set_needs_dynsym_value();
2689 // Set the number of entries in dynamic symbol table.
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.
2702 stub_max_size() const
2704 if (!this->target_
->is_output_micromips()
2705 || this->target_
->use_32bit_micromips_instructions())
2711 // Return the size of the stub. This method expects that the final dynsym
2716 gold_assert(this->dynsym_count_
!= -1U);
2717 if (this->dynsym_count_
> 0x10000)
2718 return this->stub_max_size();
2720 return this->stub_max_size() - 4;
2723 // Return output address of a stub.
2725 stub_address(const Mips_symbol
<size
>* sym
) const
2727 gold_assert(sym
->has_lazy_stub());
2728 return this->address() + sym
->lazy_stub_offset();
2733 do_adjust_output_section(Output_section
* os
)
2734 { os
->set_entsize(0); }
2736 // Write to a map file.
2738 do_print_to_mapfile(Mapfile
* mapfile
) const
2739 { mapfile
->print_output_data(this, _(".MIPS.stubs")); }
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.
2765 set_final_data_size()
2766 { this->set_data_size(this->symbols_
.size() * this->stub_max_size()); }
2768 // Write out the .MIPS.stubs data.
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_
;
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
;
2790 Mips_output_section_reginfo(Target_mips
<size
, big_endian
>* target
,
2791 Valtype gprmask
, Valtype cprmask1
,
2792 Valtype cprmask2
, Valtype cprmask3
,
2794 : Output_section_data(24, 4, true), target_(target
),
2795 gprmask_(gprmask
), cprmask1_(cprmask1
), cprmask2_(cprmask2
),
2796 cprmask3_(cprmask3
), cprmask4_(cprmask4
)
2800 // Write to a map file.
2802 do_print_to_mapfile(Mapfile
* mapfile
) const
2803 { mapfile
->print_output_data(this, _(".reginfo")); }
2805 // Write out reginfo section.
2807 do_write(Output_file
* of
);
2810 Target_mips
<size
, big_endian
>* target_
;
2812 // gprmask of the output .reginfo section.
2814 // cprmask1 of the output .reginfo section.
2816 // cprmask2 of the output .reginfo section.
2818 // cprmask3 of the output .reginfo section.
2820 // cprmask4 of the output .reginfo section.
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
2830 Mips_output_section_abiflags(const Mips_abiflags
<big_endian
>& abiflags
)
2831 : Output_section_data(24, 8, true), abiflags_(abiflags
)
2835 // Write to a map file.
2837 do_print_to_mapfile(Mapfile
* mapfile
) const
2838 { mapfile
->print_output_data(this, _(".MIPS.abiflags")); }
2841 do_write(Output_file
* of
);
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
>
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
;
2866 case elfcpp::R_MIPS_26
:
2867 return Relocatable_relocs::RELOC_SPECIAL
;
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
>
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.
2893 emit_mips(Output_data_reloc
<sh_type
, true, size
, big_endian
>*,
2894 Symbol_table
*, Layout
*, Target_mips
<size
, big_endian
>*);
2897 typedef typename Copy_relocs
<sh_type
, size
, big_endian
>::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_.
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.
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.
2922 // STV_HIDDEN or STV_INTERNAL ones must be local.
2923 if (sym
->visibility() == elfcpp::STV_HIDDEN
2924 || sym
->visibility() == elfcpp::STV_INTERNAL
)
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())
2933 // Forced local symbols resolve locally.
2934 if (sym
->is_forced_local())
2937 // As do non-dynamic symbols.
2938 if (!has_dynsym_entry
)
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())
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
)
2953 // STV_PROTECTED non-function symbols are local.
2954 if (sym
->type() != elfcpp::STT_FUNC
)
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
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.
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
>
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.
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
3005 template<int size
, bool big_endian
>
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();
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
);
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
3033 Symbol_visitor_check_symbols(Target_mips
<size
, big_endian
>* target
,
3034 Layout
* layout
, Symbol_table
* symtab
)
3035 : target_(target
), layout_(layout
), symtab_(symtab
)
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
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
);
3063 Target_mips
<size
, big_endian
>* target_
;
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
*)
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(); }
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
*)
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(); }
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.
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
>
3154 typedef typename Mips_reloc_types
<sh_type_
, 32, big_endian
>::Reloc
3156 typedef typename Mips_reloc_types
<sh_type_
, 32, big_endian
>::Reloc_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
*)
3173 static inline unsigned int
3174 get_r_type3(const Reltype
*)
3177 static inline unsigned int
3178 get_r_ssym(const Reltype
*)
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
)
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.
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.
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).
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
>
3216 typedef typename Mips_reloc_types
<sh_type_
, 64, big_endian
>::Reloc
3218 typedef typename Mips_reloc_types
<sh_type_
, 64, big_endian
>::Reloc_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
)
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.
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.
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).
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
>
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
3289 typedef typename Mips_reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
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
), copy_relocs_(), dyn_relocs_(),
3296 la25_stub_(NULL
), mips_mach_extensions_(), mips_stubs_(NULL
),
3297 attributes_section_data_(NULL
), abiflags_(NULL
), mach_(0), layout_(NULL
),
3298 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.
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.
3319 gc_process_relocs(Symbol_table
* symtab
,
3321 Sized_relobj_file
<size
, big_endian
>* object
,
3322 unsigned int data_shndx
,
3323 unsigned int sh_type
,
3324 const unsigned char* prelocs
,
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.
3333 scan_relocs(Symbol_table
* symtab
,
3335 Sized_relobj_file
<size
, big_endian
>* object
,
3336 unsigned int data_shndx
,
3337 unsigned int sh_type
,
3338 const unsigned char* prelocs
,
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.
3347 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
3349 // Relocate a section.
3351 relocate_section(const Relocate_info
<size
, big_endian
>*,
3352 unsigned int sh_type
,
3353 const unsigned char* prelocs
,
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.
3364 scan_relocatable_relocs(Symbol_table
* symtab
,
3366 Sized_relobj_file
<size
, big_endian
>* object
,
3367 unsigned int data_shndx
,
3368 unsigned int sh_type
,
3369 const unsigned char* prelocs
,
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.
3379 emit_relocs_scan(Symbol_table
* symtab
,
3381 Sized_relobj_file
<size
, big_endian
>* object
,
3382 unsigned int data_shndx
,
3383 unsigned int sh_type
,
3384 const unsigned char* prelocs
,
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.
3394 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
3395 unsigned int sh_type
,
3396 const unsigned char* prelocs
,
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.
3410 relocate_special_relocatable(const Relocate_info
<size
, big_endian
>* relinfo
,
3411 unsigned int sh_type
,
3412 const unsigned char* preloc_in
,
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.
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.
3433 got_entry_count() const
3435 if (!this->has_got_section())
3437 return this->got_size() / (size
/8);
3440 // Return the number of entries in the PLT.
3442 plt_entry_count() const
3444 if (this->plt_
== NULL
)
3446 return this->plt_
->entry_count();
3449 // Return the offset of the first non-reserved PLT entry.
3451 first_plt_entry_offset() const
3452 { return this->plt_
->first_plt_entry_offset(); }
3454 // Return the size of each PLT entry.
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
>*
3467 gold_assert(this->got_
!= NULL
);
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
>*
3491 gold_assert(this->la25_stub_
!= NULL
);
3492 return this->la25_stub_
;
3495 // Get gp value. It has the value of .got + 0x7FF0.
3499 if (this->gp_
!= NULL
)
3500 return this->gp_
->value();
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.
3507 adjusted_gp_value(const Mips_relobj
<size
, big_endian
>* object
)
3509 if (this->gp_
== NULL
)
3512 bool multi_got
= false;
3513 if (this->has_got_section())
3514 multi_got
= this->got_section()->multi_got();
3516 return this->gp_
->value();
3518 return this->gp_
->value() + this->got_section()->get_got_offset(object
);
3521 // Get the dynamic reloc section, creating it if necessary.
3523 rel_dyn_section(Layout
*);
3526 do_has_custom_set_dynsym_indexes() const
3529 // Don't emit input .reginfo/.MIPS.abiflags sections to
3530 // output .reginfo/.MIPS.abiflags.
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.
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.
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
3559 mips_elf_gnu_got1_mask()
3561 if (this->is_output_n64())
3562 return (uint64_t)1 << 63;
3567 // Whether the output has microMIPS code. This is valid only after
3568 // merge_obj_e_flags() is called.
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.
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.
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.
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.
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.
3607 use_32bit_micromips_instructions() const
3608 { return this->insn32_
; }
3610 // Return the r_sym field from a relocation.
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
>::
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.
3627 do_dynsym_value(const Symbol
* gsym
) const;
3629 // Make an ELF object.
3631 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
3632 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
);
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.
3641 do_adjust_elf_header(unsigned char* view
, int len
);
3643 // Get the custom dynamic tag value.
3645 do_dynamic_tag_custom_value(elfcpp::DT
) const;
3647 // Adjust the value written to the dynamic symbol table.
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())
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
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
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
3688 || (this->is_output_micromips() && mips_sym
->has_lazy_stub()))
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)));
3698 // The class which scans relocations.
3706 get_reference_flags(unsigned int r_type
);
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
,
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
,
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
,
3733 unsigned int rel_type
,
3734 unsigned int r_type
,
3735 const elfcpp::Sym
<size
, big_endian
>& lsym
,
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
,
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
,
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
,
3761 unsigned int rel_type
,
3762 unsigned int r_type
,
3766 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
*,
3768 Sized_relobj_file
<size
, big_endian
>*,
3773 const elfcpp::Sym
<size
, big_endian
>&)
3777 global_reloc_may_be_function_pointer(Symbol_table
*, Layout
*,
3779 Sized_relobj_file
<size
, big_endian
>*,
3783 unsigned int, Symbol
*)
3787 local_reloc_may_be_function_pointer(Symbol_table
*, Layout
*,
3789 Sized_relobj_file
<size
, big_endian
>*,
3794 const elfcpp::Sym
<size
, big_endian
>&)
3798 global_reloc_may_be_function_pointer(Symbol_table
*, Layout
*,
3800 Sized_relobj_file
<size
, big_endian
>*,
3804 unsigned int, Symbol
*)
3808 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
3809 unsigned int r_type
);
3812 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
3813 unsigned int r_type
, Symbol
*);
3816 // The class which implements relocation.
3826 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
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.
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
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
),
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.
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_
);
3874 rel_dyn
->add_symbolless_global_addend(this->sym_
, this->r_type_
,
3875 this->output_section_
, this->relobj_
,
3876 this->shndx_
, this->r_offset_
);
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.
3896 has_got_section() const
3897 { return this->got_
!= NULL
; }
3899 // Check whether the given ELF header flags describe a 32-bit binary.
3901 mips_32bit_flags(elfcpp::Elf_Word
);
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,
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.
3953 elf_mips_mach(elfcpp::Elf_Word
);
3955 // Return the MACH for each .MIPS.abiflags ISA Extension.
3957 mips_isa_ext_mach(unsigned int);
3959 // Return the .MIPS.abiflags value representing each ISA Extension.
3961 mips_isa_ext(unsigned int);
3963 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
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.
3970 infer_abiflags(Mips_relobj
<size
, big_endian
>*, Mips_abiflags
<big_endian
>*);
3972 // Create abiflags from elf header or from .MIPS.abiflags section.
3974 create_abiflags(Mips_relobj
<size
, big_endian
>*, Mips_abiflags
<big_endian
>*);
3976 // Return the meaning of fp_abi, or "unknown" if not known.
3982 select_fp_abi(const std::string
&, int, int);
3984 // Merge attributes from input object.
3986 merge_obj_attributes(const std::string
&, const Attributes_section_data
*);
3988 // Merge abiflags from input object.
3990 merge_obj_abiflags(const std::string
&, Mips_abiflags
<big_endian
>*);
3992 // Check whether machine EXTENSION is an extension of machine BASE.
3994 mips_mach_extends(unsigned int, unsigned int);
3996 // Merge file header flags from input object.
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.
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
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
4024 // Return the size of the GOT section.
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.
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
>*
4041 gold_assert(this->plt_
!= NULL
);
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.
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
));
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.
4079 set_gp(Layout
*, Symbol_table
*);
4082 elf_mips_abi_name(elfcpp::Elf_Word e_flags
);
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.
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.
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
;
4175 Mips_output_data_got
<size
, big_endian
>* got_
;
4176 // gp symbol. It has the value of .got + 0x7FF0.
4177 Sized_symbol
<size
>* gp_
;
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 // Relocs saved to avoid a COPY reloc.
4185 Mips_copy_relocs
<elfcpp::SHT_REL
, size
, big_endian
> copy_relocs_
;
4187 // A list of dyn relocs to be saved.
4188 std::vector
<Dyn_reloc
> dyn_relocs_
;
4190 // The LA25 stub section.
4191 Mips_output_data_la25_stub
<size
, big_endian
>* la25_stub_
;
4192 // Architecture extensions.
4193 std::vector
<std::pair
<unsigned int, unsigned int> > mips_mach_extensions_
;
4195 Mips_output_data_mips_stubs
<size
, big_endian
>* mips_stubs_
;
4197 // Attributes section data in output.
4198 Attributes_section_data
* attributes_section_data_
;
4199 // .MIPS.abiflags section data in output.
4200 Mips_abiflags
<big_endian
>* abiflags_
;
4205 typename
std::list
<got16_addend
<size
, big_endian
> > got16_addends_
;
4207 // Whether there is an input .MIPS.abiflags section.
4208 bool has_abiflags_section_
;
4210 // Whether the entry symbol is mips16 or micromips.
4211 bool entry_symbol_is_compressed_
;
4213 // Whether we can use only 32-bit microMIPS instructions.
4214 // TODO(sasa): This should be a linker option.
4218 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4219 // It records high part of the relocation pair.
4221 template<int size
, bool big_endian
>
4224 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Mips_address
;
4226 reloc_high(unsigned char* _view
, const Mips_relobj
<size
, big_endian
>* _object
,
4227 const Symbol_value
<size
>* _psymval
, Mips_address _addend
,
4228 unsigned int _r_type
, unsigned int _r_sym
, bool _extract_addend
,
4229 Mips_address _address
= 0, bool _gp_disp
= false)
4230 : view(_view
), object(_object
), psymval(_psymval
), addend(_addend
),
4231 r_type(_r_type
), r_sym(_r_sym
), extract_addend(_extract_addend
),
4232 address(_address
), gp_disp(_gp_disp
)
4235 unsigned char* view
;
4236 const Mips_relobj
<size
, big_endian
>* object
;
4237 const Symbol_value
<size
>* psymval
;
4238 Mips_address addend
;
4239 unsigned int r_type
;
4241 bool extract_addend
;
4242 Mips_address address
;
4246 template<int size
, bool big_endian
>
4247 class Mips_relocate_functions
: public Relocate_functions
<size
, big_endian
>
4249 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Mips_address
;
4250 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Valtype
;
4251 typedef typename
elfcpp::Swap
<16, big_endian
>::Valtype Valtype16
;
4252 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype32
;
4253 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype64
;
4258 STATUS_OKAY
, // No error during relocation.
4259 STATUS_OVERFLOW
, // Relocation overflow.
4260 STATUS_BAD_RELOC
, // Relocation cannot be applied.
4261 STATUS_PCREL_UNALIGNED
// Unaligned PC-relative relocation.
4265 typedef Relocate_functions
<size
, big_endian
> Base
;
4266 typedef Mips_relocate_functions
<size
, big_endian
> This
;
4268 static typename
std::list
<reloc_high
<size
, big_endian
> > hi16_relocs
;
4269 static typename
std::list
<reloc_high
<size
, big_endian
> > got16_relocs
;
4270 static typename
std::list
<reloc_high
<size
, big_endian
> > pchi16_relocs
;
4272 template<int valsize
>
4273 static inline typename
This::Status
4274 check_overflow(Valtype value
)
4277 return (Bits
<valsize
>::has_overflow32(value
)
4278 ? This::STATUS_OVERFLOW
4279 : This::STATUS_OKAY
);
4281 return (Bits
<valsize
>::has_overflow(value
)
4282 ? This::STATUS_OVERFLOW
4283 : This::STATUS_OKAY
);
4287 should_shuffle_micromips_reloc(unsigned int r_type
)
4289 return (micromips_reloc(r_type
)
4290 && r_type
!= elfcpp::R_MICROMIPS_PC7_S1
4291 && r_type
!= elfcpp::R_MICROMIPS_PC10_S1
);
4295 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4296 // Most mips16 instructions are 16 bits, but these instructions
4299 // The format of these instructions is:
4301 // +--------------+--------------------------------+
4302 // | JALX | X| Imm 20:16 | Imm 25:21 |
4303 // +--------------+--------------------------------+
4304 // | Immediate 15:0 |
4305 // +-----------------------------------------------+
4307 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4308 // Note that the immediate value in the first word is swapped.
4310 // When producing a relocatable object file, R_MIPS16_26 is
4311 // handled mostly like R_MIPS_26. In particular, the addend is
4312 // stored as a straight 26-bit value in a 32-bit instruction.
4313 // (gas makes life simpler for itself by never adjusting a
4314 // R_MIPS16_26 reloc to be against a section, so the addend is
4315 // always zero). However, the 32 bit instruction is stored as 2
4316 // 16-bit values, rather than a single 32-bit value. In a
4317 // big-endian file, the result is the same; in a little-endian
4318 // file, the two 16-bit halves of the 32 bit value are swapped.
4319 // This is so that a disassembler can recognize the jal
4322 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4323 // instruction stored as two 16-bit values. The addend A is the
4324 // contents of the targ26 field. The calculation is the same as
4325 // R_MIPS_26. When storing the calculated value, reorder the
4326 // immediate value as shown above, and don't forget to store the
4327 // value as two 16-bit values.
4329 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4333 // +--------+----------------------+
4337 // +--------+----------------------+
4340 // +----------+------+-------------+
4342 // | sub1 | | sub2 |
4343 // |0 9|10 15|16 31|
4344 // +----------+--------------------+
4345 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4346 // ((sub1 << 16) | sub2)).
4348 // When producing a relocatable object file, the calculation is
4349 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4350 // When producing a fully linked file, the calculation is
4351 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4352 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4354 // The table below lists the other MIPS16 instruction relocations.
4355 // Each one is calculated in the same way as the non-MIPS16 relocation
4356 // given on the right, but using the extended MIPS16 layout of 16-bit
4357 // immediate fields:
4359 // R_MIPS16_GPREL R_MIPS_GPREL16
4360 // R_MIPS16_GOT16 R_MIPS_GOT16
4361 // R_MIPS16_CALL16 R_MIPS_CALL16
4362 // R_MIPS16_HI16 R_MIPS_HI16
4363 // R_MIPS16_LO16 R_MIPS_LO16
4365 // A typical instruction will have a format like this:
4367 // +--------------+--------------------------------+
4368 // | EXTEND | Imm 10:5 | Imm 15:11 |
4369 // +--------------+--------------------------------+
4370 // | Major | rx | ry | Imm 4:0 |
4371 // +--------------+--------------------------------+
4373 // EXTEND is the five bit value 11110. Major is the instruction
4376 // All we need to do here is shuffle the bits appropriately.
4377 // As above, the two 16-bit halves must be swapped on a
4378 // little-endian system.
4380 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4381 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4382 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4385 mips_reloc_unshuffle(unsigned char* view
, unsigned int r_type
,
4388 if (!mips16_reloc(r_type
)
4389 && !should_shuffle_micromips_reloc(r_type
))
4392 // Pick up the first and second halfwords of the instruction.
4393 Valtype16 first
= elfcpp::Swap
<16, big_endian
>::readval(view
);
4394 Valtype16 second
= elfcpp::Swap
<16, big_endian
>::readval(view
+ 2);
4397 if (micromips_reloc(r_type
)
4398 || (r_type
== elfcpp::R_MIPS16_26
&& !jal_shuffle
))
4399 val
= first
<< 16 | second
;
4400 else if (r_type
!= elfcpp::R_MIPS16_26
)
4401 val
= (((first
& 0xf800) << 16) | ((second
& 0xffe0) << 11)
4402 | ((first
& 0x1f) << 11) | (first
& 0x7e0) | (second
& 0x1f));
4404 val
= (((first
& 0xfc00) << 16) | ((first
& 0x3e0) << 11)
4405 | ((first
& 0x1f) << 21) | second
);
4407 elfcpp::Swap
<32, big_endian
>::writeval(view
, val
);
4411 mips_reloc_shuffle(unsigned char* view
, unsigned int r_type
, bool jal_shuffle
)
4413 if (!mips16_reloc(r_type
)
4414 && !should_shuffle_micromips_reloc(r_type
))
4417 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(view
);
4418 Valtype16 first
, second
;
4420 if (micromips_reloc(r_type
)
4421 || (r_type
== elfcpp::R_MIPS16_26
&& !jal_shuffle
))
4423 second
= val
& 0xffff;
4426 else if (r_type
!= elfcpp::R_MIPS16_26
)
4428 second
= ((val
>> 11) & 0xffe0) | (val
& 0x1f);
4429 first
= ((val
>> 16) & 0xf800) | ((val
>> 11) & 0x1f) | (val
& 0x7e0);
4433 second
= val
& 0xffff;
4434 first
= ((val
>> 16) & 0xfc00) | ((val
>> 11) & 0x3e0)
4435 | ((val
>> 21) & 0x1f);
4438 elfcpp::Swap
<16, big_endian
>::writeval(view
+ 2, second
);
4439 elfcpp::Swap
<16, big_endian
>::writeval(view
, first
);
4442 // R_MIPS_16: S + sign-extend(A)
4443 static inline typename
This::Status
4444 rel16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4445 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
4446 bool extract_addend
, bool calculate_only
, Valtype
* calculated_value
)
4448 Valtype16
* wv
= reinterpret_cast<Valtype16
*>(view
);
4449 Valtype16 val
= elfcpp::Swap
<16, big_endian
>::readval(wv
);
4451 Valtype addend
= (extract_addend
? Bits
<16>::sign_extend32(val
)
4454 Valtype x
= psymval
->value(object
, addend
);
4455 val
= Bits
<16>::bit_select32(val
, x
, 0xffffU
);
4459 *calculated_value
= x
;
4460 return This::STATUS_OKAY
;
4463 elfcpp::Swap
<16, big_endian
>::writeval(wv
, val
);
4465 return check_overflow
<16>(x
);
4469 static inline typename
This::Status
4470 rel32(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4471 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
4472 bool extract_addend
, bool calculate_only
, Valtype
* calculated_value
)
4474 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4475 Valtype addend
= (extract_addend
4476 ? elfcpp::Swap
<32, big_endian
>::readval(wv
)
4478 Valtype x
= psymval
->value(object
, addend
);
4481 *calculated_value
= x
;
4483 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
4485 return This::STATUS_OKAY
;
4488 // R_MIPS_JALR, R_MICROMIPS_JALR
4489 static inline typename
This::Status
4490 reljalr(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4491 const Symbol_value
<size
>* psymval
, Mips_address address
,
4492 Mips_address addend_a
, bool extract_addend
, bool cross_mode_jump
,
4493 unsigned int r_type
, bool jalr_to_bal
, bool jr_to_b
,
4494 bool calculate_only
, Valtype
* calculated_value
)
4496 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4497 Valtype addend
= extract_addend
? 0 : addend_a
;
4498 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4500 // Try converting J(AL)R to B(AL), if the target is in range.
4501 if (!parameters
->options().relocatable()
4502 && r_type
== elfcpp::R_MIPS_JALR
4504 && ((jalr_to_bal
&& val
== 0x0320f809) // jalr t9
4505 || (jr_to_b
&& val
== 0x03200008))) // jr t9
4507 int offset
= psymval
->value(object
, addend
) - (address
+ 4);
4508 if (!Bits
<18>::has_overflow32(offset
))
4510 if (val
== 0x03200008) // jr t9
4511 val
= 0x10000000 | (((Valtype32
)offset
>> 2) & 0xffff); // b addr
4513 val
= 0x04110000 | (((Valtype32
)offset
>> 2) & 0xffff); //bal addr
4518 *calculated_value
= val
;
4520 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4522 return This::STATUS_OKAY
;
4525 // R_MIPS_PC32: S + A - P
4526 static inline typename
This::Status
4527 relpc32(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4528 const Symbol_value
<size
>* psymval
, Mips_address address
,
4529 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
4530 Valtype
* calculated_value
)
4532 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4533 Valtype addend
= (extract_addend
4534 ? elfcpp::Swap
<32, big_endian
>::readval(wv
)
4536 Valtype x
= psymval
->value(object
, addend
) - address
;
4539 *calculated_value
= x
;
4541 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
4543 return This::STATUS_OKAY
;
4546 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4547 static inline typename
This::Status
4548 rel26(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4549 const Symbol_value
<size
>* psymval
, Mips_address address
,
4550 bool local
, Mips_address addend_a
, bool extract_addend
,
4551 const Symbol
* gsym
, bool cross_mode_jump
, unsigned int r_type
,
4552 bool jal_to_bal
, bool calculate_only
, Valtype
* calculated_value
)
4554 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4555 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4560 if (r_type
== elfcpp::R_MICROMIPS_26_S1
)
4561 addend
= (val
& 0x03ffffff) << 1;
4563 addend
= (val
& 0x03ffffff) << 2;
4568 // Make sure the target of JALX is word-aligned. Bit 0 must be
4569 // the correct ISA mode selector and bit 1 must be 0.
4570 if (!calculate_only
&& cross_mode_jump
4571 && (psymval
->value(object
, 0) & 3) != (r_type
== elfcpp::R_MIPS_26
))
4573 gold_warning(_("JALX to a non-word-aligned address"));
4574 return This::STATUS_BAD_RELOC
;
4577 // Shift is 2, unusually, for microMIPS JALX.
4578 unsigned int shift
=
4579 (!cross_mode_jump
&& r_type
== elfcpp::R_MICROMIPS_26_S1
) ? 1 : 2;
4583 x
= addend
| ((address
+ 4) & (0xfc000000 << shift
));
4587 x
= Bits
<27>::sign_extend32(addend
);
4589 x
= Bits
<28>::sign_extend32(addend
);
4591 x
= psymval
->value(object
, x
) >> shift
;
4593 if (!calculate_only
&& !local
&& !gsym
->is_weak_undefined())
4595 if ((x
>> 26) != ((address
+ 4) >> (26 + shift
)))
4597 gold_error(_("relocation truncated to fit: %u against '%s'"),
4598 r_type
, gsym
->name());
4599 return This::STATUS_OVERFLOW
;
4603 val
= Bits
<32>::bit_select32(val
, x
, 0x03ffffff);
4605 // If required, turn JAL into JALX.
4606 if (cross_mode_jump
)
4609 Valtype32 opcode
= val
>> 26;
4610 Valtype32 jalx_opcode
;
4612 // Check to see if the opcode is already JAL or JALX.
4613 if (r_type
== elfcpp::R_MIPS16_26
)
4615 ok
= (opcode
== 0x6) || (opcode
== 0x7);
4618 else if (r_type
== elfcpp::R_MICROMIPS_26_S1
)
4620 ok
= (opcode
== 0x3d) || (opcode
== 0x3c);
4625 ok
= (opcode
== 0x3) || (opcode
== 0x1d);
4629 // If the opcode is not JAL or JALX, there's a problem. We cannot
4630 // convert J or JALS to JALX.
4631 if (!calculate_only
&& !ok
)
4633 gold_error(_("Unsupported jump between ISA modes; consider "
4634 "recompiling with interlinking enabled."));
4635 return This::STATUS_BAD_RELOC
;
4638 // Make this the JALX opcode.
4639 val
= (val
& ~(0x3f << 26)) | (jalx_opcode
<< 26);
4642 // Try converting JAL to BAL, if the target is in range.
4643 if (!parameters
->options().relocatable()
4646 && r_type
== elfcpp::R_MIPS_26
4647 && (val
>> 26) == 0x3))) // jal addr
4649 Valtype32 dest
= (x
<< 2) | (((address
+ 4) >> 28) << 28);
4650 int offset
= dest
- (address
+ 4);
4651 if (!Bits
<18>::has_overflow32(offset
))
4653 if (val
== 0x03200008) // jr t9
4654 val
= 0x10000000 | (((Valtype32
)offset
>> 2) & 0xffff); // b addr
4656 val
= 0x04110000 | (((Valtype32
)offset
>> 2) & 0xffff); //bal addr
4661 *calculated_value
= val
;
4663 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4665 return This::STATUS_OKAY
;
4669 static inline typename
This::Status
4670 relpc16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4671 const Symbol_value
<size
>* psymval
, Mips_address address
,
4672 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
4673 Valtype
* calculated_value
)
4675 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4676 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4678 Valtype addend
= (extract_addend
4679 ? Bits
<18>::sign_extend32((val
& 0xffff) << 2)
4682 Valtype x
= psymval
->value(object
, addend
) - address
;
4683 val
= Bits
<16>::bit_select32(val
, x
>> 2, 0xffff);
4687 *calculated_value
= x
>> 2;
4688 return This::STATUS_OKAY
;
4691 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4693 return check_overflow
<18>(x
);
4697 static inline typename
This::Status
4698 relpc21(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4699 const Symbol_value
<size
>* psymval
, Mips_address address
,
4700 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
4701 Valtype
* calculated_value
)
4703 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4704 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4706 Valtype addend
= (extract_addend
4707 ? Bits
<23>::sign_extend32((val
& 0x1fffff) << 2)
4710 Valtype x
= psymval
->value(object
, addend
) - address
;
4711 val
= Bits
<21>::bit_select32(val
, x
>> 2, 0x1fffff);
4715 *calculated_value
= x
>> 2;
4716 return This::STATUS_OKAY
;
4719 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4721 if (psymval
->value(object
, addend
) & 3)
4722 return This::STATUS_PCREL_UNALIGNED
;
4724 return check_overflow
<23>(x
);
4728 static inline typename
This::Status
4729 relpc26(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4730 const Symbol_value
<size
>* psymval
, Mips_address address
,
4731 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
4732 Valtype
* calculated_value
)
4734 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4735 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4737 Valtype addend
= (extract_addend
4738 ? Bits
<28>::sign_extend32((val
& 0x3ffffff) << 2)
4741 Valtype x
= psymval
->value(object
, addend
) - address
;
4742 val
= Bits
<26>::bit_select32(val
, x
>> 2, 0x3ffffff);
4746 *calculated_value
= x
>> 2;
4747 return This::STATUS_OKAY
;
4750 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4752 if (psymval
->value(object
, addend
) & 3)
4753 return This::STATUS_PCREL_UNALIGNED
;
4755 return check_overflow
<28>(x
);
4759 static inline typename
This::Status
4760 relpc18(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4761 const Symbol_value
<size
>* psymval
, Mips_address address
,
4762 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
4763 Valtype
* calculated_value
)
4765 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4766 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4768 Valtype addend
= (extract_addend
4769 ? Bits
<21>::sign_extend32((val
& 0x3ffff) << 3)
4772 Valtype x
= psymval
->value(object
, addend
) - ((address
| 7) ^ 7);
4773 val
= Bits
<18>::bit_select32(val
, x
>> 3, 0x3ffff);
4777 *calculated_value
= x
>> 3;
4778 return This::STATUS_OKAY
;
4781 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4783 if (psymval
->value(object
, addend
) & 7)
4784 return This::STATUS_PCREL_UNALIGNED
;
4786 return check_overflow
<21>(x
);
4790 static inline typename
This::Status
4791 relpc19(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4792 const Symbol_value
<size
>* psymval
, Mips_address address
,
4793 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
4794 Valtype
* calculated_value
)
4796 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4797 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4799 Valtype addend
= (extract_addend
4800 ? Bits
<21>::sign_extend32((val
& 0x7ffff) << 2)
4803 Valtype x
= psymval
->value(object
, addend
) - address
;
4804 val
= Bits
<19>::bit_select32(val
, x
>> 2, 0x7ffff);
4808 *calculated_value
= x
>> 2;
4809 return This::STATUS_OKAY
;
4812 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4814 if (psymval
->value(object
, addend
) & 3)
4815 return This::STATUS_PCREL_UNALIGNED
;
4817 return check_overflow
<21>(x
);
4821 static inline typename
This::Status
4822 relpchi16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4823 const Symbol_value
<size
>* psymval
, Mips_address addend
,
4824 Mips_address address
, unsigned int r_sym
, bool extract_addend
)
4826 // Record the relocation. It will be resolved when we find pclo16 part.
4827 pchi16_relocs
.push_back(reloc_high
<size
, big_endian
>(view
, object
, psymval
,
4828 addend
, 0, r_sym
, extract_addend
, address
));
4829 return This::STATUS_OKAY
;
4833 static inline typename
This::Status
4834 do_relpchi16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4835 const Symbol_value
<size
>* psymval
, Mips_address addend_hi
,
4836 Mips_address address
, bool extract_addend
, Valtype32 addend_lo
,
4837 bool calculate_only
, Valtype
* calculated_value
)
4839 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4840 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4842 Valtype addend
= (extract_addend
? ((val
& 0xffff) << 16) + addend_lo
4845 Valtype value
= psymval
->value(object
, addend
) - address
;
4846 Valtype x
= ((value
+ 0x8000) >> 16) & 0xffff;
4847 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
4850 *calculated_value
= x
;
4852 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4854 return This::STATUS_OKAY
;
4858 static inline typename
This::Status
4859 relpclo16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4860 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
4861 bool extract_addend
, Mips_address address
, unsigned int r_sym
,
4862 unsigned int rel_type
, bool calculate_only
,
4863 Valtype
* calculated_value
)
4865 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4866 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4868 Valtype addend
= (extract_addend
? Bits
<16>::sign_extend32(val
& 0xffff)
4871 if (rel_type
== elfcpp::SHT_REL
)
4873 // Resolve pending R_MIPS_PCHI16 relocations.
4874 typename
std::list
<reloc_high
<size
, big_endian
> >::iterator it
=
4875 pchi16_relocs
.begin();
4876 while (it
!= pchi16_relocs
.end())
4878 reloc_high
<size
, big_endian
> pchi16
= *it
;
4879 if (pchi16
.r_sym
== r_sym
)
4881 do_relpchi16(pchi16
.view
, pchi16
.object
, pchi16
.psymval
,
4882 pchi16
.addend
, pchi16
.address
,
4883 pchi16
.extract_addend
, addend
, calculate_only
,
4885 it
= pchi16_relocs
.erase(it
);
4892 // Resolve R_MIPS_PCLO16 relocation.
4893 Valtype x
= psymval
->value(object
, addend
) - address
;
4894 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
4897 *calculated_value
= x
;
4899 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4901 return This::STATUS_OKAY
;
4904 // R_MICROMIPS_PC7_S1
4905 static inline typename
This::Status
4906 relmicromips_pc7_s1(unsigned char* view
,
4907 const Mips_relobj
<size
, big_endian
>* object
,
4908 const Symbol_value
<size
>* psymval
, Mips_address address
,
4909 Mips_address addend_a
, bool extract_addend
,
4910 bool calculate_only
, Valtype
* calculated_value
)
4912 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4913 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4915 Valtype addend
= extract_addend
? Bits
<8>::sign_extend32((val
& 0x7f) << 1)
4918 Valtype x
= psymval
->value(object
, addend
) - address
;
4919 val
= Bits
<16>::bit_select32(val
, x
>> 1, 0x7f);
4923 *calculated_value
= x
>> 1;
4924 return This::STATUS_OKAY
;
4927 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4929 return check_overflow
<8>(x
);
4932 // R_MICROMIPS_PC10_S1
4933 static inline typename
This::Status
4934 relmicromips_pc10_s1(unsigned char* view
,
4935 const Mips_relobj
<size
, big_endian
>* object
,
4936 const Symbol_value
<size
>* psymval
, Mips_address address
,
4937 Mips_address addend_a
, bool extract_addend
,
4938 bool calculate_only
, Valtype
* calculated_value
)
4940 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4941 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4943 Valtype addend
= (extract_addend
4944 ? Bits
<11>::sign_extend32((val
& 0x3ff) << 1)
4947 Valtype x
= psymval
->value(object
, addend
) - address
;
4948 val
= Bits
<16>::bit_select32(val
, x
>> 1, 0x3ff);
4952 *calculated_value
= x
>> 1;
4953 return This::STATUS_OKAY
;
4956 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4958 return check_overflow
<11>(x
);
4961 // R_MICROMIPS_PC16_S1
4962 static inline typename
This::Status
4963 relmicromips_pc16_s1(unsigned char* view
,
4964 const Mips_relobj
<size
, big_endian
>* object
,
4965 const Symbol_value
<size
>* psymval
, Mips_address address
,
4966 Mips_address addend_a
, bool extract_addend
,
4967 bool calculate_only
, Valtype
* calculated_value
)
4969 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
4970 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4972 Valtype addend
= (extract_addend
4973 ? Bits
<17>::sign_extend32((val
& 0xffff) << 1)
4976 Valtype x
= psymval
->value(object
, addend
) - address
;
4977 val
= Bits
<16>::bit_select32(val
, x
>> 1, 0xffff);
4981 *calculated_value
= x
>> 1;
4982 return This::STATUS_OKAY
;
4985 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
4987 return check_overflow
<17>(x
);
4990 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4991 static inline typename
This::Status
4992 relhi16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
4993 const Symbol_value
<size
>* psymval
, Mips_address addend
,
4994 Mips_address address
, bool gp_disp
, unsigned int r_type
,
4995 unsigned int r_sym
, bool extract_addend
)
4997 // Record the relocation. It will be resolved when we find lo16 part.
4998 hi16_relocs
.push_back(reloc_high
<size
, big_endian
>(view
, object
, psymval
,
4999 addend
, r_type
, r_sym
, extract_addend
, address
,
5001 return This::STATUS_OKAY
;
5004 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5005 static inline typename
This::Status
5006 do_relhi16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5007 const Symbol_value
<size
>* psymval
, Mips_address addend_hi
,
5008 Mips_address address
, bool is_gp_disp
, unsigned int r_type
,
5009 bool extract_addend
, Valtype32 addend_lo
,
5010 Target_mips
<size
, big_endian
>* target
, bool calculate_only
,
5011 Valtype
* calculated_value
)
5013 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5014 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5016 Valtype addend
= (extract_addend
? ((val
& 0xffff) << 16) + addend_lo
5021 value
= psymval
->value(object
, addend
);
5024 // For MIPS16 ABI code we generate this sequence
5025 // 0: li $v0,%hi(_gp_disp)
5026 // 4: addiupc $v1,%lo(_gp_disp)
5030 // So the offsets of hi and lo relocs are the same, but the
5031 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5032 // ADDIUPC clears the low two bits of the instruction address,
5033 // so the base is ($t9 + 4) & ~3.
5035 if (r_type
== elfcpp::R_MIPS16_HI16
)
5036 gp_disp
= (target
->adjusted_gp_value(object
)
5037 - ((address
+ 4) & ~0x3));
5038 // The microMIPS .cpload sequence uses the same assembly
5039 // instructions as the traditional psABI version, but the
5040 // incoming $t9 has the low bit set.
5041 else if (r_type
== elfcpp::R_MICROMIPS_HI16
)
5042 gp_disp
= target
->adjusted_gp_value(object
) - address
- 1;
5044 gp_disp
= target
->adjusted_gp_value(object
) - address
;
5045 value
= gp_disp
+ addend
;
5047 Valtype x
= ((value
+ 0x8000) >> 16) & 0xffff;
5048 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5052 *calculated_value
= x
;
5053 return This::STATUS_OKAY
;
5056 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5058 return (is_gp_disp
? check_overflow
<16>(x
)
5059 : This::STATUS_OKAY
);
5062 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5063 static inline typename
This::Status
5064 relgot16_local(unsigned char* view
,
5065 const Mips_relobj
<size
, big_endian
>* object
,
5066 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
5067 bool extract_addend
, unsigned int r_type
, unsigned int r_sym
)
5069 // Record the relocation. It will be resolved when we find lo16 part.
5070 got16_relocs
.push_back(reloc_high
<size
, big_endian
>(view
, object
, psymval
,
5071 addend_a
, r_type
, r_sym
, extract_addend
));
5072 return This::STATUS_OKAY
;
5075 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5076 static inline typename
This::Status
5077 do_relgot16_local(unsigned char* view
,
5078 const Mips_relobj
<size
, big_endian
>* object
,
5079 const Symbol_value
<size
>* psymval
, Mips_address addend_hi
,
5080 bool extract_addend
, Valtype32 addend_lo
,
5081 Target_mips
<size
, big_endian
>* target
, bool calculate_only
,
5082 Valtype
* calculated_value
)
5084 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5085 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5087 Valtype addend
= (extract_addend
? ((val
& 0xffff) << 16) + addend_lo
5090 // Find GOT page entry.
5091 Mips_address value
= ((psymval
->value(object
, addend
) + 0x8000) >> 16)
5094 unsigned int got_offset
=
5095 target
->got_section()->get_got_page_offset(value
, object
);
5097 // Resolve the relocation.
5098 Valtype x
= target
->got_section()->gp_offset(got_offset
, object
);
5099 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5103 *calculated_value
= x
;
5104 return This::STATUS_OKAY
;
5107 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5109 return check_overflow
<16>(x
);
5112 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5113 static inline typename
This::Status
5114 rello16(Target_mips
<size
, big_endian
>* target
, unsigned char* view
,
5115 const Mips_relobj
<size
, big_endian
>* object
,
5116 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
5117 bool extract_addend
, Mips_address address
, bool is_gp_disp
,
5118 unsigned int r_type
, unsigned int r_sym
, unsigned int rel_type
,
5119 bool calculate_only
, Valtype
* calculated_value
)
5121 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5122 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5124 Valtype addend
= (extract_addend
? Bits
<16>::sign_extend32(val
& 0xffff)
5127 if (rel_type
== elfcpp::SHT_REL
)
5129 typename
This::Status reloc_status
= This::STATUS_OKAY
;
5130 // Resolve pending R_MIPS_HI16 relocations.
5131 typename
std::list
<reloc_high
<size
, big_endian
> >::iterator it
=
5132 hi16_relocs
.begin();
5133 while (it
!= hi16_relocs
.end())
5135 reloc_high
<size
, big_endian
> hi16
= *it
;
5136 if (hi16
.r_sym
== r_sym
5137 && is_matching_lo16_reloc(hi16
.r_type
, r_type
))
5139 mips_reloc_unshuffle(hi16
.view
, hi16
.r_type
, false);
5140 reloc_status
= do_relhi16(hi16
.view
, hi16
.object
, hi16
.psymval
,
5141 hi16
.addend
, hi16
.address
, hi16
.gp_disp
,
5142 hi16
.r_type
, hi16
.extract_addend
, addend
,
5143 target
, calculate_only
, calculated_value
);
5144 mips_reloc_shuffle(hi16
.view
, hi16
.r_type
, false);
5145 if (reloc_status
== This::STATUS_OVERFLOW
)
5146 return This::STATUS_OVERFLOW
;
5147 it
= hi16_relocs
.erase(it
);
5153 // Resolve pending local R_MIPS_GOT16 relocations.
5154 typename
std::list
<reloc_high
<size
, big_endian
> >::iterator it2
=
5155 got16_relocs
.begin();
5156 while (it2
!= got16_relocs
.end())
5158 reloc_high
<size
, big_endian
> got16
= *it2
;
5159 if (got16
.r_sym
== r_sym
5160 && is_matching_lo16_reloc(got16
.r_type
, r_type
))
5162 mips_reloc_unshuffle(got16
.view
, got16
.r_type
, false);
5164 reloc_status
= do_relgot16_local(got16
.view
, got16
.object
,
5165 got16
.psymval
, got16
.addend
,
5166 got16
.extract_addend
, addend
, target
,
5167 calculate_only
, calculated_value
);
5169 mips_reloc_shuffle(got16
.view
, got16
.r_type
, false);
5170 if (reloc_status
== This::STATUS_OVERFLOW
)
5171 return This::STATUS_OVERFLOW
;
5172 it2
= got16_relocs
.erase(it2
);
5179 // Resolve R_MIPS_LO16 relocation.
5182 x
= psymval
->value(object
, addend
);
5185 // See the comment for R_MIPS16_HI16 above for the reason
5186 // for this conditional.
5188 if (r_type
== elfcpp::R_MIPS16_LO16
)
5189 gp_disp
= target
->adjusted_gp_value(object
) - (address
& ~0x3);
5190 else if (r_type
== elfcpp::R_MICROMIPS_LO16
5191 || r_type
== elfcpp::R_MICROMIPS_HI0_LO16
)
5192 gp_disp
= target
->adjusted_gp_value(object
) - address
+ 3;
5194 gp_disp
= target
->adjusted_gp_value(object
) - address
+ 4;
5195 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5196 // for overflow. Relocations against _gp_disp are normally
5197 // generated from the .cpload pseudo-op. It generates code
5198 // that normally looks like this:
5200 // lui $gp,%hi(_gp_disp)
5201 // addiu $gp,$gp,%lo(_gp_disp)
5204 // Here $t9 holds the address of the function being called,
5205 // as required by the MIPS ELF ABI. The R_MIPS_LO16
5206 // relocation can easily overflow in this situation, but the
5207 // R_MIPS_HI16 relocation will handle the overflow.
5208 // Therefore, we consider this a bug in the MIPS ABI, and do
5209 // not check for overflow here.
5210 x
= gp_disp
+ addend
;
5212 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5215 *calculated_value
= x
;
5217 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5219 return This::STATUS_OKAY
;
5222 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5223 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5224 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5225 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5226 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5227 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5228 static inline typename
This::Status
5229 relgot(unsigned char* view
, int gp_offset
, bool calculate_only
,
5230 Valtype
* calculated_value
)
5232 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5233 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5234 Valtype x
= gp_offset
;
5235 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5239 *calculated_value
= x
;
5240 return This::STATUS_OKAY
;
5243 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5245 return check_overflow
<16>(x
);
5249 static inline typename
This::Status
5250 releh(unsigned char* view
, int gp_offset
, bool calculate_only
,
5251 Valtype
* calculated_value
)
5253 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5254 Valtype x
= gp_offset
;
5258 *calculated_value
= x
;
5259 return This::STATUS_OKAY
;
5262 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
5264 return check_overflow
<32>(x
);
5267 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5268 static inline typename
This::Status
5269 relgotpage(Target_mips
<size
, big_endian
>* target
, unsigned char* view
,
5270 const Mips_relobj
<size
, big_endian
>* object
,
5271 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
5272 bool extract_addend
, bool calculate_only
,
5273 Valtype
* calculated_value
)
5275 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5276 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(view
);
5277 Valtype addend
= extract_addend
? val
& 0xffff : addend_a
;
5279 // Find a GOT page entry that points to within 32KB of symbol + addend.
5280 Mips_address value
= (psymval
->value(object
, addend
) + 0x8000) & ~0xffff;
5281 unsigned int got_offset
=
5282 target
->got_section()->get_got_page_offset(value
, object
);
5284 Valtype x
= target
->got_section()->gp_offset(got_offset
, object
);
5285 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5289 *calculated_value
= x
;
5290 return This::STATUS_OKAY
;
5293 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5295 return check_overflow
<16>(x
);
5298 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5299 static inline typename
This::Status
5300 relgotofst(Target_mips
<size
, big_endian
>* target
, unsigned char* view
,
5301 const Mips_relobj
<size
, big_endian
>* object
,
5302 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
5303 bool extract_addend
, bool local
, bool calculate_only
,
5304 Valtype
* calculated_value
)
5306 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5307 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(view
);
5308 Valtype addend
= extract_addend
? val
& 0xffff : addend_a
;
5310 // For a local symbol, find a GOT page entry that points to within 32KB of
5311 // symbol + addend. Relocation value is the offset of the GOT page entry's
5312 // value from symbol + addend.
5313 // For a global symbol, relocation value is addend.
5317 // Find GOT page entry.
5318 Mips_address value
= ((psymval
->value(object
, addend
) + 0x8000)
5320 target
->got_section()->get_got_page_offset(value
, object
);
5322 x
= psymval
->value(object
, addend
) - value
;
5326 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5330 *calculated_value
= x
;
5331 return This::STATUS_OKAY
;
5334 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5336 return check_overflow
<16>(x
);
5339 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5340 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5341 static inline typename
This::Status
5342 relgot_hi16(unsigned char* view
, int gp_offset
, bool calculate_only
,
5343 Valtype
* calculated_value
)
5345 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5346 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5347 Valtype x
= gp_offset
;
5348 x
= ((x
+ 0x8000) >> 16) & 0xffff;
5349 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5352 *calculated_value
= x
;
5354 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5356 return This::STATUS_OKAY
;
5359 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5360 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5361 static inline typename
This::Status
5362 relgot_lo16(unsigned char* view
, int gp_offset
, bool calculate_only
,
5363 Valtype
* calculated_value
)
5365 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5366 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5367 Valtype x
= gp_offset
;
5368 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5371 *calculated_value
= x
;
5373 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5375 return This::STATUS_OKAY
;
5378 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5379 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5380 static inline typename
This::Status
5381 relgprel(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5382 const Symbol_value
<size
>* psymval
, Mips_address gp
,
5383 Mips_address addend_a
, bool extract_addend
, bool local
,
5384 unsigned int r_type
, bool calculate_only
,
5385 Valtype
* calculated_value
)
5387 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5388 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5393 if (r_type
== elfcpp::R_MICROMIPS_GPREL7_S2
)
5394 addend
= (val
& 0x7f) << 2;
5396 addend
= val
& 0xffff;
5397 // Only sign-extend the addend if it was extracted from the
5398 // instruction. If the addend was separate, leave it alone,
5399 // otherwise we may lose significant bits.
5400 addend
= Bits
<16>::sign_extend32(addend
);
5405 Valtype x
= psymval
->value(object
, addend
) - gp
;
5407 // If the symbol was local, any earlier relocatable links will
5408 // have adjusted its addend with the gp offset, so compensate
5409 // for that now. Don't do it for symbols forced local in this
5410 // link, though, since they won't have had the gp offset applied
5413 x
+= object
->gp_value();
5415 if (r_type
== elfcpp::R_MICROMIPS_GPREL7_S2
)
5416 val
= Bits
<32>::bit_select32(val
, x
, 0x7f);
5418 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5422 *calculated_value
= x
;
5423 return This::STATUS_OKAY
;
5426 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5428 if (check_overflow
<16>(x
) == This::STATUS_OVERFLOW
)
5430 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5431 "limit (see option -G)"));
5432 return This::STATUS_OVERFLOW
;
5434 return This::STATUS_OKAY
;
5438 static inline typename
This::Status
5439 relgprel32(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5440 const Symbol_value
<size
>* psymval
, Mips_address gp
,
5441 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
5442 Valtype
* calculated_value
)
5444 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5445 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5446 Valtype addend
= extract_addend
? val
: addend_a
;
5448 // R_MIPS_GPREL32 relocations are defined for local symbols only.
5449 Valtype x
= psymval
->value(object
, addend
) + object
->gp_value() - gp
;
5452 *calculated_value
= x
;
5454 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
5456 return This::STATUS_OKAY
;
5459 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5460 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5461 // R_MICROMIPS_TLS_DTPREL_HI16
5462 static inline typename
This::Status
5463 tlsrelhi16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5464 const Symbol_value
<size
>* psymval
, Valtype32 tp_offset
,
5465 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
5466 Valtype
* calculated_value
)
5468 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5469 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5470 Valtype addend
= extract_addend
? val
& 0xffff : addend_a
;
5472 // tls symbol values are relative to tls_segment()->vaddr()
5473 Valtype x
= ((psymval
->value(object
, addend
) - tp_offset
) + 0x8000) >> 16;
5474 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5477 *calculated_value
= x
;
5479 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5481 return This::STATUS_OKAY
;
5484 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5485 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5486 // R_MICROMIPS_TLS_DTPREL_LO16,
5487 static inline typename
This::Status
5488 tlsrello16(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5489 const Symbol_value
<size
>* psymval
, Valtype32 tp_offset
,
5490 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
5491 Valtype
* calculated_value
)
5493 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5494 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5495 Valtype addend
= extract_addend
? val
& 0xffff : addend_a
;
5497 // tls symbol values are relative to tls_segment()->vaddr()
5498 Valtype x
= psymval
->value(object
, addend
) - tp_offset
;
5499 val
= Bits
<32>::bit_select32(val
, x
, 0xffff);
5502 *calculated_value
= x
;
5504 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
5506 return This::STATUS_OKAY
;
5509 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5510 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5511 static inline typename
This::Status
5512 tlsrel32(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5513 const Symbol_value
<size
>* psymval
, Valtype32 tp_offset
,
5514 Mips_address addend_a
, bool extract_addend
, bool calculate_only
,
5515 Valtype
* calculated_value
)
5517 Valtype32
* wv
= reinterpret_cast<Valtype32
*>(view
);
5518 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5519 Valtype addend
= extract_addend
? val
: addend_a
;
5521 // tls symbol values are relative to tls_segment()->vaddr()
5522 Valtype x
= psymval
->value(object
, addend
) - tp_offset
;
5525 *calculated_value
= x
;
5527 elfcpp::Swap
<32, big_endian
>::writeval(wv
, x
);
5529 return This::STATUS_OKAY
;
5532 // R_MIPS_SUB, R_MICROMIPS_SUB
5533 static inline typename
This::Status
5534 relsub(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5535 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
5536 bool extract_addend
, bool calculate_only
, Valtype
* calculated_value
)
5538 Valtype64
* wv
= reinterpret_cast<Valtype64
*>(view
);
5539 Valtype64 addend
= (extract_addend
5540 ? elfcpp::Swap
<64, big_endian
>::readval(wv
)
5543 Valtype64 x
= psymval
->value(object
, -addend
);
5545 *calculated_value
= x
;
5547 elfcpp::Swap
<64, big_endian
>::writeval(wv
, x
);
5549 return This::STATUS_OKAY
;
5553 static inline typename
This::Status
5554 rel64(unsigned char* view
, const Mips_relobj
<size
, big_endian
>* object
,
5555 const Symbol_value
<size
>* psymval
, Mips_address addend_a
,
5556 bool extract_addend
, bool calculate_only
, Valtype
* calculated_value
,
5557 bool apply_addend_only
)
5559 Valtype64
* wv
= reinterpret_cast<Valtype64
*>(view
);
5560 Valtype64 addend
= (extract_addend
5561 ? elfcpp::Swap
<64, big_endian
>::readval(wv
)
5564 Valtype64 x
= psymval
->value(object
, addend
);
5566 *calculated_value
= x
;
5569 if (apply_addend_only
)
5571 elfcpp::Swap
<64, big_endian
>::writeval(wv
, x
);
5574 return This::STATUS_OKAY
;
5579 template<int size
, bool big_endian
>
5580 typename
std::list
<reloc_high
<size
, big_endian
> >
5581 Mips_relocate_functions
<size
, big_endian
>::hi16_relocs
;
5583 template<int size
, bool big_endian
>
5584 typename
std::list
<reloc_high
<size
, big_endian
> >
5585 Mips_relocate_functions
<size
, big_endian
>::got16_relocs
;
5587 template<int size
, bool big_endian
>
5588 typename
std::list
<reloc_high
<size
, big_endian
> >
5589 Mips_relocate_functions
<size
, big_endian
>::pchi16_relocs
;
5591 // Mips_got_info methods.
5593 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5594 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5596 template<int size
, bool big_endian
>
5598 Mips_got_info
<size
, big_endian
>::record_local_got_symbol(
5599 Mips_relobj
<size
, big_endian
>* object
, unsigned int symndx
,
5600 Mips_address addend
, unsigned int r_type
, unsigned int shndx
,
5601 bool is_section_symbol
)
5603 Mips_got_entry
<size
, big_endian
>* entry
=
5604 new Mips_got_entry
<size
, big_endian
>(object
, symndx
, addend
,
5605 mips_elf_reloc_tls_type(r_type
),
5606 shndx
, is_section_symbol
);
5607 this->record_got_entry(entry
, object
);
5610 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5611 // in OBJECT. FOR_CALL is true if the caller is only interested in
5612 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5615 template<int size
, bool big_endian
>
5617 Mips_got_info
<size
, big_endian
>::record_global_got_symbol(
5618 Mips_symbol
<size
>* mips_sym
, Mips_relobj
<size
, big_endian
>* object
,
5619 unsigned int r_type
, bool dyn_reloc
, bool for_call
)
5622 mips_sym
->set_got_not_only_for_calls();
5624 // A global symbol in the GOT must also be in the dynamic symbol table.
5625 if (!mips_sym
->needs_dynsym_entry())
5627 switch (mips_sym
->visibility())
5629 case elfcpp::STV_INTERNAL
:
5630 case elfcpp::STV_HIDDEN
:
5631 mips_sym
->set_is_forced_local();
5634 mips_sym
->set_needs_dynsym_entry();
5639 unsigned char tls_type
= mips_elf_reloc_tls_type(r_type
);
5640 if (tls_type
== GOT_TLS_NONE
)
5641 this->global_got_symbols_
.insert(mips_sym
);
5645 if (mips_sym
->global_got_area() == GGA_NONE
)
5646 mips_sym
->set_global_got_area(GGA_RELOC_ONLY
);
5650 Mips_got_entry
<size
, big_endian
>* entry
=
5651 new Mips_got_entry
<size
, big_endian
>(mips_sym
, tls_type
);
5653 this->record_got_entry(entry
, object
);
5656 // Add ENTRY to master GOT and to OBJECT's GOT.
5658 template<int size
, bool big_endian
>
5660 Mips_got_info
<size
, big_endian
>::record_got_entry(
5661 Mips_got_entry
<size
, big_endian
>* entry
,
5662 Mips_relobj
<size
, big_endian
>* object
)
5664 this->got_entries_
.insert(entry
);
5666 // Create the GOT entry for the OBJECT's GOT.
5667 Mips_got_info
<size
, big_endian
>* g
= object
->get_or_create_got_info();
5668 Mips_got_entry
<size
, big_endian
>* entry2
=
5669 new Mips_got_entry
<size
, big_endian
>(*entry
);
5671 g
->got_entries_
.insert(entry2
);
5674 // Record that OBJECT has a page relocation against symbol SYMNDX and
5675 // that ADDEND is the addend for that relocation.
5676 // This function creates an upper bound on the number of GOT slots
5677 // required; no attempt is made to combine references to non-overridable
5678 // global symbols across multiple input files.
5680 template<int size
, bool big_endian
>
5682 Mips_got_info
<size
, big_endian
>::record_got_page_entry(
5683 Mips_relobj
<size
, big_endian
>* object
, unsigned int symndx
, int addend
)
5685 struct Got_page_range
**range_ptr
, *range
;
5686 int old_pages
, new_pages
;
5688 // Find the Got_page_entry for this symbol.
5689 Got_page_entry
* entry
= new Got_page_entry(object
, symndx
);
5690 typename
Got_page_entry_set::iterator it
=
5691 this->got_page_entries_
.find(entry
);
5692 if (it
!= this->got_page_entries_
.end())
5695 this->got_page_entries_
.insert(entry
);
5697 // Add the same entry to the OBJECT's GOT.
5698 Got_page_entry
* entry2
= NULL
;
5699 Mips_got_info
<size
, big_endian
>* g2
= object
->get_or_create_got_info();
5700 if (g2
->got_page_entries_
.find(entry
) == g2
->got_page_entries_
.end())
5702 entry2
= new Got_page_entry(*entry
);
5703 g2
->got_page_entries_
.insert(entry2
);
5706 // Skip over ranges whose maximum extent cannot share a page entry
5708 range_ptr
= &entry
->ranges
;
5709 while (*range_ptr
&& addend
> (*range_ptr
)->max_addend
+ 0xffff)
5710 range_ptr
= &(*range_ptr
)->next
;
5712 // If we scanned to the end of the list, or found a range whose
5713 // minimum extent cannot share a page entry with ADDEND, create
5714 // a new singleton range.
5716 if (!range
|| addend
< range
->min_addend
- 0xffff)
5718 range
= new Got_page_range();
5719 range
->next
= *range_ptr
;
5720 range
->min_addend
= addend
;
5721 range
->max_addend
= addend
;
5726 ++entry2
->num_pages
;
5727 ++this->page_gotno_
;
5732 // Remember how many pages the old range contributed.
5733 old_pages
= range
->get_max_pages();
5735 // Update the ranges.
5736 if (addend
< range
->min_addend
)
5737 range
->min_addend
= addend
;
5738 else if (addend
> range
->max_addend
)
5740 if (range
->next
&& addend
>= range
->next
->min_addend
- 0xffff)
5742 old_pages
+= range
->next
->get_max_pages();
5743 range
->max_addend
= range
->next
->max_addend
;
5744 range
->next
= range
->next
->next
;
5747 range
->max_addend
= addend
;
5750 // Record any change in the total estimate.
5751 new_pages
= range
->get_max_pages();
5752 if (old_pages
!= new_pages
)
5754 entry
->num_pages
+= new_pages
- old_pages
;
5756 entry2
->num_pages
+= new_pages
- old_pages
;
5757 this->page_gotno_
+= new_pages
- old_pages
;
5758 g2
->page_gotno_
+= new_pages
- old_pages
;
5762 // Create all entries that should be in the local part of the GOT.
5764 template<int size
, bool big_endian
>
5766 Mips_got_info
<size
, big_endian
>::add_local_entries(
5767 Target_mips
<size
, big_endian
>* target
, Layout
* layout
)
5769 Mips_output_data_got
<size
, big_endian
>* got
= target
->got_section();
5770 // First two GOT entries are reserved. The first entry will be filled at
5771 // runtime. The second entry will be used by some runtime loaders.
5772 got
->add_constant(0);
5773 got
->add_constant(target
->mips_elf_gnu_got1_mask());
5775 for (typename
Got_entry_set::iterator
5776 p
= this->got_entries_
.begin();
5777 p
!= this->got_entries_
.end();
5780 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
5781 if (entry
->is_for_local_symbol() && !entry
->is_tls_entry())
5783 got
->add_local(entry
->object(), entry
->symndx(),
5784 GOT_TYPE_STANDARD
, entry
->addend());
5785 unsigned int got_offset
= entry
->object()->local_got_offset(
5786 entry
->symndx(), GOT_TYPE_STANDARD
, entry
->addend());
5787 if (got
->multi_got() && this->index_
> 0
5788 && parameters
->options().output_is_position_independent())
5790 if (!entry
->is_section_symbol())
5791 target
->rel_dyn_section(layout
)->add_local(entry
->object(),
5792 entry
->symndx(), elfcpp::R_MIPS_REL32
, got
, got_offset
);
5794 target
->rel_dyn_section(layout
)->add_symbolless_local_addend(
5795 entry
->object(), entry
->symndx(), elfcpp::R_MIPS_REL32
,
5801 this->add_page_entries(target
, layout
);
5803 // Add global entries that should be in the local area.
5804 for (typename
Got_entry_set::iterator
5805 p
= this->got_entries_
.begin();
5806 p
!= this->got_entries_
.end();
5809 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
5810 if (!entry
->is_for_global_symbol())
5813 Mips_symbol
<size
>* mips_sym
= entry
->sym();
5814 if (mips_sym
->global_got_area() == GGA_NONE
&& !entry
->is_tls_entry())
5816 unsigned int got_type
;
5817 if (!got
->multi_got())
5818 got_type
= GOT_TYPE_STANDARD
;
5820 got_type
= GOT_TYPE_STANDARD_MULTIGOT
+ this->index_
;
5821 if (got
->add_global(mips_sym
, got_type
))
5823 mips_sym
->set_global_gotoffset(mips_sym
->got_offset(got_type
));
5824 if (got
->multi_got() && this->index_
> 0
5825 && parameters
->options().output_is_position_independent())
5826 target
->rel_dyn_section(layout
)->add_symbolless_global_addend(
5827 mips_sym
, elfcpp::R_MIPS_REL32
, got
,
5828 mips_sym
->got_offset(got_type
));
5834 // Create GOT page entries.
5836 template<int size
, bool big_endian
>
5838 Mips_got_info
<size
, big_endian
>::add_page_entries(
5839 Target_mips
<size
, big_endian
>* target
, Layout
* layout
)
5841 if (this->page_gotno_
== 0)
5844 Mips_output_data_got
<size
, big_endian
>* got
= target
->got_section();
5845 this->got_page_offset_start_
= got
->add_constant(0);
5846 if (got
->multi_got() && this->index_
> 0
5847 && parameters
->options().output_is_position_independent())
5848 target
->rel_dyn_section(layout
)->add_absolute(elfcpp::R_MIPS_REL32
, got
,
5849 this->got_page_offset_start_
);
5850 int num_entries
= this->page_gotno_
;
5851 unsigned int prev_offset
= this->got_page_offset_start_
;
5852 while (--num_entries
> 0)
5854 unsigned int next_offset
= got
->add_constant(0);
5855 if (got
->multi_got() && this->index_
> 0
5856 && parameters
->options().output_is_position_independent())
5857 target
->rel_dyn_section(layout
)->add_absolute(elfcpp::R_MIPS_REL32
, got
,
5859 gold_assert(next_offset
== prev_offset
+ size
/8);
5860 prev_offset
= next_offset
;
5862 this->got_page_offset_next_
= this->got_page_offset_start_
;
5865 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5867 template<int size
, bool big_endian
>
5869 Mips_got_info
<size
, big_endian
>::add_global_entries(
5870 Target_mips
<size
, big_endian
>* target
, Layout
* layout
,
5871 unsigned int non_reloc_only_global_gotno
)
5873 Mips_output_data_got
<size
, big_endian
>* got
= target
->got_section();
5874 // Add GGA_NORMAL entries.
5875 unsigned int count
= 0;
5876 for (typename
Got_entry_set::iterator
5877 p
= this->got_entries_
.begin();
5878 p
!= this->got_entries_
.end();
5881 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
5882 if (!entry
->is_for_global_symbol())
5885 Mips_symbol
<size
>* mips_sym
= entry
->sym();
5886 if (mips_sym
->global_got_area() != GGA_NORMAL
)
5889 unsigned int got_type
;
5890 if (!got
->multi_got())
5891 got_type
= GOT_TYPE_STANDARD
;
5893 // In multi-GOT links, global symbol can be in both primary and
5894 // secondary GOT(s). By creating custom GOT type
5895 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5896 // is added to secondary GOT(s).
5897 got_type
= GOT_TYPE_STANDARD_MULTIGOT
+ this->index_
;
5898 if (!got
->add_global(mips_sym
, got_type
))
5901 mips_sym
->set_global_gotoffset(mips_sym
->got_offset(got_type
));
5902 if (got
->multi_got() && this->index_
== 0)
5904 if (got
->multi_got() && this->index_
> 0)
5906 if (parameters
->options().output_is_position_independent()
5907 || (!parameters
->doing_static_link()
5908 && mips_sym
->is_from_dynobj() && !mips_sym
->is_undefined()))
5910 target
->rel_dyn_section(layout
)->add_global(
5911 mips_sym
, elfcpp::R_MIPS_REL32
, got
,
5912 mips_sym
->got_offset(got_type
));
5913 got
->add_secondary_got_reloc(mips_sym
->got_offset(got_type
),
5914 elfcpp::R_MIPS_REL32
, mips_sym
);
5919 if (!got
->multi_got() || this->index_
== 0)
5921 if (got
->multi_got())
5923 // We need to allocate space in the primary GOT for GGA_NORMAL entries
5924 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
5925 // entries correspond to dynamic symbol indexes.
5926 while (count
< non_reloc_only_global_gotno
)
5928 got
->add_constant(0);
5933 // Add GGA_RELOC_ONLY entries.
5934 got
->add_reloc_only_entries();
5938 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
5940 template<int size
, bool big_endian
>
5942 Mips_got_info
<size
, big_endian
>::add_reloc_only_entries(
5943 Mips_output_data_got
<size
, big_endian
>* got
)
5945 for (typename
Global_got_entry_set::iterator
5946 p
= this->global_got_symbols_
.begin();
5947 p
!= this->global_got_symbols_
.end();
5950 Mips_symbol
<size
>* mips_sym
= *p
;
5951 if (mips_sym
->global_got_area() == GGA_RELOC_ONLY
)
5953 unsigned int got_type
;
5954 if (!got
->multi_got())
5955 got_type
= GOT_TYPE_STANDARD
;
5957 got_type
= GOT_TYPE_STANDARD_MULTIGOT
;
5958 if (got
->add_global(mips_sym
, got_type
))
5959 mips_sym
->set_global_gotoffset(mips_sym
->got_offset(got_type
));
5964 // Create TLS GOT entries.
5966 template<int size
, bool big_endian
>
5968 Mips_got_info
<size
, big_endian
>::add_tls_entries(
5969 Target_mips
<size
, big_endian
>* target
, Layout
* layout
)
5971 Mips_output_data_got
<size
, big_endian
>* got
= target
->got_section();
5972 // Add local tls entries.
5973 for (typename
Got_entry_set::iterator
5974 p
= this->got_entries_
.begin();
5975 p
!= this->got_entries_
.end();
5978 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
5979 if (!entry
->is_tls_entry() || !entry
->is_for_local_symbol())
5982 if (entry
->tls_type() == GOT_TLS_GD
)
5984 unsigned int got_type
= GOT_TYPE_TLS_PAIR
;
5985 unsigned int r_type1
= (size
== 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5986 : elfcpp::R_MIPS_TLS_DTPMOD64
);
5987 unsigned int r_type2
= (size
== 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5988 : elfcpp::R_MIPS_TLS_DTPREL64
);
5990 if (!parameters
->doing_static_link())
5992 got
->add_local_pair_with_rel(entry
->object(), entry
->symndx(),
5993 entry
->shndx(), got_type
,
5994 target
->rel_dyn_section(layout
),
5995 r_type1
, entry
->addend());
5996 unsigned int got_offset
=
5997 entry
->object()->local_got_offset(entry
->symndx(), got_type
,
5999 got
->add_static_reloc(got_offset
+ size
/8, r_type2
,
6000 entry
->object(), entry
->symndx());
6004 // We are doing a static link. Mark it as belong to module 1,
6006 unsigned int got_offset
= got
->add_constant(1);
6007 entry
->object()->set_local_got_offset(entry
->symndx(), got_type
,
6010 got
->add_constant(0);
6011 got
->add_static_reloc(got_offset
+ size
/8, r_type2
,
6012 entry
->object(), entry
->symndx());
6015 else if (entry
->tls_type() == GOT_TLS_IE
)
6017 unsigned int got_type
= GOT_TYPE_TLS_OFFSET
;
6018 unsigned int r_type
= (size
== 32 ? elfcpp::R_MIPS_TLS_TPREL32
6019 : elfcpp::R_MIPS_TLS_TPREL64
);
6020 if (!parameters
->doing_static_link())
6021 got
->add_local_with_rel(entry
->object(), entry
->symndx(), got_type
,
6022 target
->rel_dyn_section(layout
), r_type
,
6026 got
->add_local(entry
->object(), entry
->symndx(), got_type
,
6028 unsigned int got_offset
=
6029 entry
->object()->local_got_offset(entry
->symndx(), got_type
,
6031 got
->add_static_reloc(got_offset
, r_type
, entry
->object(),
6035 else if (entry
->tls_type() == GOT_TLS_LDM
)
6037 unsigned int r_type
= (size
== 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6038 : elfcpp::R_MIPS_TLS_DTPMOD64
);
6039 unsigned int got_offset
;
6040 if (!parameters
->doing_static_link())
6042 got_offset
= got
->add_constant(0);
6043 target
->rel_dyn_section(layout
)->add_local(
6044 entry
->object(), 0, r_type
, got
, got_offset
);
6047 // We are doing a static link. Just mark it as belong to module 1,
6049 got_offset
= got
->add_constant(1);
6051 got
->add_constant(0);
6052 got
->set_tls_ldm_offset(got_offset
, entry
->object());
6058 // Add global tls entries.
6059 for (typename
Got_entry_set::iterator
6060 p
= this->got_entries_
.begin();
6061 p
!= this->got_entries_
.end();
6064 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
6065 if (!entry
->is_tls_entry() || !entry
->is_for_global_symbol())
6068 Mips_symbol
<size
>* mips_sym
= entry
->sym();
6069 if (entry
->tls_type() == GOT_TLS_GD
)
6071 unsigned int got_type
;
6072 if (!got
->multi_got())
6073 got_type
= GOT_TYPE_TLS_PAIR
;
6075 got_type
= GOT_TYPE_TLS_PAIR_MULTIGOT
+ this->index_
;
6076 unsigned int r_type1
= (size
== 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6077 : elfcpp::R_MIPS_TLS_DTPMOD64
);
6078 unsigned int r_type2
= (size
== 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6079 : elfcpp::R_MIPS_TLS_DTPREL64
);
6080 if (!parameters
->doing_static_link())
6081 got
->add_global_pair_with_rel(mips_sym
, got_type
,
6082 target
->rel_dyn_section(layout
), r_type1
, r_type2
);
6085 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
6086 // GOT entries. The first one is initialized to be 1, which is the
6087 // module index for the main executable and the second one 0. A
6088 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6089 // the second GOT entry and will be applied by gold.
6090 unsigned int got_offset
= got
->add_constant(1);
6091 mips_sym
->set_got_offset(got_type
, got_offset
);
6092 got
->add_constant(0);
6093 got
->add_static_reloc(got_offset
+ size
/8, r_type2
, mips_sym
);
6096 else if (entry
->tls_type() == GOT_TLS_IE
)
6098 unsigned int got_type
;
6099 if (!got
->multi_got())
6100 got_type
= GOT_TYPE_TLS_OFFSET
;
6102 got_type
= GOT_TYPE_TLS_OFFSET_MULTIGOT
+ this->index_
;
6103 unsigned int r_type
= (size
== 32 ? elfcpp::R_MIPS_TLS_TPREL32
6104 : elfcpp::R_MIPS_TLS_TPREL64
);
6105 if (!parameters
->doing_static_link())
6106 got
->add_global_with_rel(mips_sym
, got_type
,
6107 target
->rel_dyn_section(layout
), r_type
);
6110 got
->add_global(mips_sym
, got_type
);
6111 unsigned int got_offset
= mips_sym
->got_offset(got_type
);
6112 got
->add_static_reloc(got_offset
, r_type
, mips_sym
);
6120 // Decide whether the symbol needs an entry in the global part of the primary
6121 // GOT, setting global_got_area accordingly. Count the number of global
6122 // symbols that are in the primary GOT only because they have dynamic
6123 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
6125 template<int size
, bool big_endian
>
6127 Mips_got_info
<size
, big_endian
>::count_got_symbols(Symbol_table
* symtab
)
6129 for (typename
Global_got_entry_set::iterator
6130 p
= this->global_got_symbols_
.begin();
6131 p
!= this->global_got_symbols_
.end();
6134 Mips_symbol
<size
>* sym
= *p
;
6135 // Make a final decision about whether the symbol belongs in the
6136 // local or global GOT. Symbols that bind locally can (and in the
6137 // case of forced-local symbols, must) live in the local GOT.
6138 // Those that are aren't in the dynamic symbol table must also
6139 // live in the local GOT.
6141 if (!sym
->should_add_dynsym_entry(symtab
)
6142 || (sym
->got_only_for_calls()
6143 ? symbol_calls_local(sym
, sym
->should_add_dynsym_entry(symtab
))
6144 : symbol_references_local(sym
,
6145 sym
->should_add_dynsym_entry(symtab
))))
6146 // The symbol belongs in the local GOT. We no longer need this
6147 // entry if it was only used for relocations; those relocations
6148 // will be against the null or section symbol instead.
6149 sym
->set_global_got_area(GGA_NONE
);
6150 else if (sym
->global_got_area() == GGA_RELOC_ONLY
)
6152 ++this->reloc_only_gotno_
;
6153 ++this->global_gotno_
;
6158 // Return the offset of GOT page entry for VALUE. Initialize the entry with
6159 // VALUE if it is not initialized.
6161 template<int size
, bool big_endian
>
6163 Mips_got_info
<size
, big_endian
>::get_got_page_offset(Mips_address value
,
6164 Mips_output_data_got
<size
, big_endian
>* got
)
6166 typename
Got_page_offsets::iterator it
= this->got_page_offsets_
.find(value
);
6167 if (it
!= this->got_page_offsets_
.end())
6170 gold_assert(this->got_page_offset_next_
< this->got_page_offset_start_
6171 + (size
/8) * this->page_gotno_
);
6173 unsigned int got_offset
= this->got_page_offset_next_
;
6174 this->got_page_offsets_
[value
] = got_offset
;
6175 this->got_page_offset_next_
+= size
/8;
6176 got
->update_got_entry(got_offset
, value
);
6180 // Remove lazy-binding stubs for global symbols in this GOT.
6182 template<int size
, bool big_endian
>
6184 Mips_got_info
<size
, big_endian
>::remove_lazy_stubs(
6185 Target_mips
<size
, big_endian
>* target
)
6187 for (typename
Got_entry_set::iterator
6188 p
= this->got_entries_
.begin();
6189 p
!= this->got_entries_
.end();
6192 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
6193 if (entry
->is_for_global_symbol())
6194 target
->remove_lazy_stub_entry(entry
->sym());
6198 // Count the number of GOT entries required.
6200 template<int size
, bool big_endian
>
6202 Mips_got_info
<size
, big_endian
>::count_got_entries()
6204 for (typename
Got_entry_set::iterator
6205 p
= this->got_entries_
.begin();
6206 p
!= this->got_entries_
.end();
6209 this->count_got_entry(*p
);
6213 // Count the number of GOT entries required by ENTRY. Accumulate the result.
6215 template<int size
, bool big_endian
>
6217 Mips_got_info
<size
, big_endian
>::count_got_entry(
6218 Mips_got_entry
<size
, big_endian
>* entry
)
6220 if (entry
->is_tls_entry())
6221 this->tls_gotno_
+= mips_tls_got_entries(entry
->tls_type());
6222 else if (entry
->is_for_local_symbol()
6223 || entry
->sym()->global_got_area() == GGA_NONE
)
6224 ++this->local_gotno_
;
6226 ++this->global_gotno_
;
6229 // Add FROM's GOT entries.
6231 template<int size
, bool big_endian
>
6233 Mips_got_info
<size
, big_endian
>::add_got_entries(
6234 Mips_got_info
<size
, big_endian
>* from
)
6236 for (typename
Got_entry_set::iterator
6237 p
= from
->got_entries_
.begin();
6238 p
!= from
->got_entries_
.end();
6241 Mips_got_entry
<size
, big_endian
>* entry
= *p
;
6242 if (this->got_entries_
.find(entry
) == this->got_entries_
.end())
6244 Mips_got_entry
<size
, big_endian
>* entry2
=
6245 new Mips_got_entry
<size
, big_endian
>(*entry
);
6246 this->got_entries_
.insert(entry2
);
6247 this->count_got_entry(entry
);
6252 // Add FROM's GOT page entries.
6254 template<int size
, bool big_endian
>
6256 Mips_got_info
<size
, big_endian
>::add_got_page_entries(
6257 Mips_got_info
<size
, big_endian
>* from
)
6259 for (typename
Got_page_entry_set::iterator
6260 p
= from
->got_page_entries_
.begin();
6261 p
!= from
->got_page_entries_
.end();
6264 Got_page_entry
* entry
= *p
;
6265 if (this->got_page_entries_
.find(entry
) == this->got_page_entries_
.end())
6267 Got_page_entry
* entry2
= new Got_page_entry(*entry
);
6268 this->got_page_entries_
.insert(entry2
);
6269 this->page_gotno_
+= entry
->num_pages
;
6274 // Mips_output_data_got methods.
6276 // Lay out the GOT. Add local, global and TLS entries. If GOT is
6277 // larger than 64K, create multi-GOT.
6279 template<int size
, bool big_endian
>
6281 Mips_output_data_got
<size
, big_endian
>::lay_out_got(Layout
* layout
,
6282 Symbol_table
* symtab
, const Input_objects
* input_objects
)
6284 // Decide which symbols need to go in the global part of the GOT and
6285 // count the number of reloc-only GOT symbols.
6286 this->master_got_info_
->count_got_symbols(symtab
);
6288 // Count the number of GOT entries.
6289 this->master_got_info_
->count_got_entries();
6291 unsigned int got_size
= this->master_got_info_
->got_size();
6292 if (got_size
> Target_mips
<size
, big_endian
>::MIPS_GOT_MAX_SIZE
)
6293 this->lay_out_multi_got(layout
, input_objects
);
6296 // Record that all objects use single GOT.
6297 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
6298 p
!= input_objects
->relobj_end();
6301 Mips_relobj
<size
, big_endian
>* object
=
6302 Mips_relobj
<size
, big_endian
>::as_mips_relobj(*p
);
6303 if (object
->get_got_info() != NULL
)
6304 object
->set_got_info(this->master_got_info_
);
6307 this->master_got_info_
->add_local_entries(this->target_
, layout
);
6308 this->master_got_info_
->add_global_entries(this->target_
, layout
,
6310 this->master_got_info_
->add_tls_entries(this->target_
, layout
);
6314 // Create multi-GOT. For every GOT, add local, global and TLS entries.
6316 template<int size
, bool big_endian
>
6318 Mips_output_data_got
<size
, big_endian
>::lay_out_multi_got(Layout
* layout
,
6319 const Input_objects
* input_objects
)
6321 // Try to merge the GOTs of input objects together, as long as they
6322 // don't seem to exceed the maximum GOT size, choosing one of them
6323 // to be the primary GOT.
6324 this->merge_gots(input_objects
);
6326 // Every symbol that is referenced in a dynamic relocation must be
6327 // present in the primary GOT.
6328 this->primary_got_
->set_global_gotno(this->master_got_info_
->global_gotno());
6332 unsigned int offset
= 0;
6333 Mips_got_info
<size
, big_endian
>* g
= this->primary_got_
;
6337 g
->set_offset(offset
);
6339 g
->add_local_entries(this->target_
, layout
);
6341 g
->add_global_entries(this->target_
, layout
,
6342 (this->master_got_info_
->global_gotno()
6343 - this->master_got_info_
->reloc_only_gotno()));
6345 g
->add_global_entries(this->target_
, layout
, /*not used*/-1U);
6346 g
->add_tls_entries(this->target_
, layout
);
6348 // Forbid global symbols in every non-primary GOT from having
6349 // lazy-binding stubs.
6351 g
->remove_lazy_stubs(this->target_
);
6354 offset
+= g
->got_size();
6360 // Attempt to merge GOTs of different input objects. Try to use as much as
6361 // possible of the primary GOT, since it doesn't require explicit dynamic
6362 // relocations, but don't use objects that would reference global symbols
6363 // out of the addressable range. Failing the primary GOT, attempt to merge
6364 // with the current GOT, or finish the current GOT and then make make the new
6367 template<int size
, bool big_endian
>
6369 Mips_output_data_got
<size
, big_endian
>::merge_gots(
6370 const Input_objects
* input_objects
)
6372 gold_assert(this->primary_got_
== NULL
);
6373 Mips_got_info
<size
, big_endian
>* current
= NULL
;
6375 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
6376 p
!= input_objects
->relobj_end();
6379 Mips_relobj
<size
, big_endian
>* object
=
6380 Mips_relobj
<size
, big_endian
>::as_mips_relobj(*p
);
6382 Mips_got_info
<size
, big_endian
>* g
= object
->get_got_info();
6386 g
->count_got_entries();
6388 // Work out the number of page, local and TLS entries.
6389 unsigned int estimate
= this->master_got_info_
->page_gotno();
6390 if (estimate
> g
->page_gotno())
6391 estimate
= g
->page_gotno();
6392 estimate
+= g
->local_gotno() + g
->tls_gotno();
6394 // We place TLS GOT entries after both locals and globals. The globals
6395 // for the primary GOT may overflow the normal GOT size limit, so be
6396 // sure not to merge a GOT which requires TLS with the primary GOT in that
6397 // case. This doesn't affect non-primary GOTs.
6398 estimate
+= (g
->tls_gotno() > 0 ? this->master_got_info_
->global_gotno()
6399 : g
->global_gotno());
6401 unsigned int max_count
=
6402 Target_mips
<size
, big_endian
>::MIPS_GOT_MAX_SIZE
/ (size
/8) - 2;
6403 if (estimate
<= max_count
)
6405 // If we don't have a primary GOT, use it as
6406 // a starting point for the primary GOT.
6407 if (!this->primary_got_
)
6409 this->primary_got_
= g
;
6413 // Try merging with the primary GOT.
6414 if (this->merge_got_with(g
, object
, this->primary_got_
))
6418 // If we can merge with the last-created GOT, do it.
6419 if (current
&& this->merge_got_with(g
, object
, current
))
6422 // Well, we couldn't merge, so create a new GOT. Don't check if it
6423 // fits; if it turns out that it doesn't, we'll get relocation
6424 // overflows anyway.
6425 g
->set_next(current
);
6429 // If we do not find any suitable primary GOT, create an empty one.
6430 if (this->primary_got_
== NULL
)
6431 this->primary_got_
= new Mips_got_info
<size
, big_endian
>();
6433 // Link primary GOT with secondary GOTs.
6434 this->primary_got_
->set_next(current
);
6437 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6438 // this would lead to overflow, true if they were merged successfully.
6440 template<int size
, bool big_endian
>
6442 Mips_output_data_got
<size
, big_endian
>::merge_got_with(
6443 Mips_got_info
<size
, big_endian
>* from
,
6444 Mips_relobj
<size
, big_endian
>* object
,
6445 Mips_got_info
<size
, big_endian
>* to
)
6447 // Work out how many page entries we would need for the combined GOT.
6448 unsigned int estimate
= this->master_got_info_
->page_gotno();
6449 if (estimate
>= from
->page_gotno() + to
->page_gotno())
6450 estimate
= from
->page_gotno() + to
->page_gotno();
6452 // Conservatively estimate how many local and TLS entries would be needed.
6453 estimate
+= from
->local_gotno() + to
->local_gotno();
6454 estimate
+= from
->tls_gotno() + to
->tls_gotno();
6456 // If we're merging with the primary got, any TLS relocations will
6457 // come after the full set of global entries. Otherwise estimate those
6458 // conservatively as well.
6459 if (to
== this->primary_got_
&& (from
->tls_gotno() + to
->tls_gotno()) > 0)
6460 estimate
+= this->master_got_info_
->global_gotno();
6462 estimate
+= from
->global_gotno() + to
->global_gotno();
6464 // Bail out if the combined GOT might be too big.
6465 unsigned int max_count
=
6466 Target_mips
<size
, big_endian
>::MIPS_GOT_MAX_SIZE
/ (size
/8) - 2;
6467 if (estimate
> max_count
)
6470 // Transfer the object's GOT information from FROM to TO.
6471 to
->add_got_entries(from
);
6472 to
->add_got_page_entries(from
);
6474 // Record that OBJECT should use output GOT TO.
6475 object
->set_got_info(to
);
6480 // Write out the GOT.
6482 template<int size
, bool big_endian
>
6484 Mips_output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
6486 typedef Unordered_set
<Mips_symbol
<size
>*, Mips_symbol_hash
<size
> >
6487 Mips_stubs_entry_set
;
6489 // Call parent to write out GOT.
6490 Output_data_got
<size
, big_endian
>::do_write(of
);
6492 const off_t offset
= this->offset();
6493 const section_size_type oview_size
=
6494 convert_to_section_size_type(this->data_size());
6495 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
6497 // Needed for fixing values of .got section.
6498 this->got_view_
= oview
;
6500 // Write lazy stub addresses.
6501 for (typename
Mips_stubs_entry_set::iterator
6502 p
= this->master_got_info_
->global_got_symbols().begin();
6503 p
!= this->master_got_info_
->global_got_symbols().end();
6506 Mips_symbol
<size
>* mips_sym
= *p
;
6507 if (mips_sym
->has_lazy_stub())
6509 Valtype
* wv
= reinterpret_cast<Valtype
*>(
6510 oview
+ this->get_primary_got_offset(mips_sym
));
6512 this->target_
->mips_stubs_section()->stub_address(mips_sym
);
6513 elfcpp::Swap
<size
, big_endian
>::writeval(wv
, value
);
6517 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
6518 for (typename
Mips_stubs_entry_set::iterator
6519 p
= this->master_got_info_
->global_got_symbols().begin();
6520 p
!= this->master_got_info_
->global_got_symbols().end();
6523 Mips_symbol
<size
>* mips_sym
= *p
;
6524 if (!this->multi_got()
6525 && (mips_sym
->is_mips16() || mips_sym
->is_micromips())
6526 && mips_sym
->global_got_area() == GGA_NONE
6527 && mips_sym
->has_got_offset(GOT_TYPE_STANDARD
))
6529 Valtype
* wv
= reinterpret_cast<Valtype
*>(
6530 oview
+ mips_sym
->got_offset(GOT_TYPE_STANDARD
));
6531 Valtype value
= elfcpp::Swap
<size
, big_endian
>::readval(wv
);
6535 elfcpp::Swap
<size
, big_endian
>::writeval(wv
, value
);
6540 if (!this->secondary_got_relocs_
.empty())
6542 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6543 // secondary GOT entries with non-zero initial value copy the value
6544 // to the corresponding primary GOT entry, and set the secondary GOT
6546 // TODO(sasa): This is workaround. It needs to be investigated further.
6548 for (size_t i
= 0; i
< this->secondary_got_relocs_
.size(); ++i
)
6550 Static_reloc
& reloc(this->secondary_got_relocs_
[i
]);
6551 if (reloc
.symbol_is_global())
6553 Mips_symbol
<size
>* gsym
= reloc
.symbol();
6554 gold_assert(gsym
!= NULL
);
6556 unsigned got_offset
= reloc
.got_offset();
6557 gold_assert(got_offset
< oview_size
);
6559 // Find primary GOT entry.
6560 Valtype
* wv_prim
= reinterpret_cast<Valtype
*>(
6561 oview
+ this->get_primary_got_offset(gsym
));
6563 // Find secondary GOT entry.
6564 Valtype
* wv_sec
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
6566 Valtype value
= elfcpp::Swap
<size
, big_endian
>::readval(wv_sec
);
6569 elfcpp::Swap
<size
, big_endian
>::writeval(wv_prim
, value
);
6570 elfcpp::Swap
<size
, big_endian
>::writeval(wv_sec
, 0);
6571 gsym
->set_applied_secondary_got_fixup();
6576 of
->write_output_view(offset
, oview_size
, oview
);
6579 // We are done if there is no fix up.
6580 if (this->static_relocs_
.empty())
6583 Output_segment
* tls_segment
= this->layout_
->tls_segment();
6584 gold_assert(tls_segment
!= NULL
);
6586 for (size_t i
= 0; i
< this->static_relocs_
.size(); ++i
)
6588 Static_reloc
& reloc(this->static_relocs_
[i
]);
6591 if (!reloc
.symbol_is_global())
6593 Sized_relobj_file
<size
, big_endian
>* object
= reloc
.relobj();
6594 const Symbol_value
<size
>* psymval
=
6595 object
->local_symbol(reloc
.index());
6597 // We are doing static linking. Issue an error and skip this
6598 // relocation if the symbol is undefined or in a discarded_section.
6600 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
6601 if ((shndx
== elfcpp::SHN_UNDEF
)
6603 && shndx
!= elfcpp::SHN_UNDEF
6604 && !object
->is_section_included(shndx
)
6605 && !this->symbol_table_
->is_section_folded(object
, shndx
)))
6607 gold_error(_("undefined or discarded local symbol %u from "
6608 " object %s in GOT"),
6609 reloc
.index(), reloc
.relobj()->name().c_str());
6613 value
= psymval
->value(object
, 0);
6617 const Mips_symbol
<size
>* gsym
= reloc
.symbol();
6618 gold_assert(gsym
!= NULL
);
6620 // We are doing static linking. Issue an error and skip this
6621 // relocation if the symbol is undefined or in a discarded_section
6622 // unless it is a weakly_undefined symbol.
6623 if ((gsym
->is_defined_in_discarded_section() || gsym
->is_undefined())
6624 && !gsym
->is_weak_undefined())
6626 gold_error(_("undefined or discarded symbol %s in GOT"),
6631 if (!gsym
->is_weak_undefined())
6632 value
= gsym
->value();
6637 unsigned got_offset
= reloc
.got_offset();
6638 gold_assert(got_offset
< oview_size
);
6640 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
6643 switch (reloc
.r_type())
6645 case elfcpp::R_MIPS_TLS_DTPMOD32
:
6646 case elfcpp::R_MIPS_TLS_DTPMOD64
:
6649 case elfcpp::R_MIPS_TLS_DTPREL32
:
6650 case elfcpp::R_MIPS_TLS_DTPREL64
:
6651 x
= value
- elfcpp::DTP_OFFSET
;
6653 case elfcpp::R_MIPS_TLS_TPREL32
:
6654 case elfcpp::R_MIPS_TLS_TPREL64
:
6655 x
= value
- elfcpp::TP_OFFSET
;
6662 elfcpp::Swap
<size
, big_endian
>::writeval(wv
, x
);
6665 of
->write_output_view(offset
, oview_size
, oview
);
6668 // Mips_relobj methods.
6670 // Count the local symbols. The Mips backend needs to know if a symbol
6671 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6672 // because the Symbol object keeps the ELF symbol type and st_other field.
6673 // For local symbol it is harder because we cannot access this information.
6674 // So we override the do_count_local_symbol in parent and scan local symbols to
6675 // mark MIPS16 and microMIPS functions. This is not the most efficient way but
6676 // I do not want to slow down other ports by calling a per symbol target hook
6677 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6679 template<int size
, bool big_endian
>
6681 Mips_relobj
<size
, big_endian
>::do_count_local_symbols(
6682 Stringpool_template
<char>* pool
,
6683 Stringpool_template
<char>* dynpool
)
6685 // Ask parent to count the local symbols.
6686 Sized_relobj_file
<size
, big_endian
>::do_count_local_symbols(pool
, dynpool
);
6687 const unsigned int loccount
= this->local_symbol_count();
6691 // Initialize the mips16 and micromips function bit-vector.
6692 this->local_symbol_is_mips16_
.resize(loccount
, false);
6693 this->local_symbol_is_micromips_
.resize(loccount
, false);
6695 // Read the symbol table section header.
6696 const unsigned int symtab_shndx
= this->symtab_shndx();
6697 elfcpp::Shdr
<size
, big_endian
>
6698 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
6699 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
6701 // Read the local symbols.
6702 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
6703 gold_assert(loccount
== symtabshdr
.get_sh_info());
6704 off_t locsize
= loccount
* sym_size
;
6705 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
6706 locsize
, true, true);
6708 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6710 // Skip the first dummy symbol.
6712 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
6714 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
6715 unsigned char st_other
= sym
.get_st_other();
6716 this->local_symbol_is_mips16_
[i
] = elfcpp::elf_st_is_mips16(st_other
);
6717 this->local_symbol_is_micromips_
[i
] =
6718 elfcpp::elf_st_is_micromips(st_other
);
6722 // Read the symbol information.
6724 template<int size
, bool big_endian
>
6726 Mips_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
6728 // Call parent class to read symbol information.
6729 this->base_read_symbols(sd
);
6731 // Read processor-specific flags in ELF file header.
6732 const unsigned char* pehdr
= this->get_view(elfcpp::file_header_offset
,
6733 elfcpp::Elf_sizes
<size
>::ehdr_size
,
6735 elfcpp::Ehdr
<size
, big_endian
> ehdr(pehdr
);
6736 this->processor_specific_flags_
= ehdr
.get_e_flags();
6738 // Get the section names.
6739 const unsigned char* pnamesu
= sd
->section_names
->data();
6740 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
6742 // Initialize the mips16 stub section bit-vectors.
6743 this->section_is_mips16_fn_stub_
.resize(this->shnum(), false);
6744 this->section_is_mips16_call_stub_
.resize(this->shnum(), false);
6745 this->section_is_mips16_call_fp_stub_
.resize(this->shnum(), false);
6747 const size_t shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
6748 const unsigned char* pshdrs
= sd
->section_headers
->data();
6749 const unsigned char* ps
= pshdrs
+ shdr_size
;
6750 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
6752 elfcpp::Shdr
<size
, big_endian
> shdr(ps
);
6754 if (shdr
.get_sh_type() == elfcpp::SHT_MIPS_REGINFO
)
6756 this->has_reginfo_section_
= true;
6757 // Read the gp value that was used to create this object. We need the
6758 // gp value while processing relocs. The .reginfo section is not used
6759 // in the 64-bit MIPS ELF ABI.
6760 section_offset_type section_offset
= shdr
.get_sh_offset();
6761 section_size_type section_size
=
6762 convert_to_section_size_type(shdr
.get_sh_size());
6763 const unsigned char* view
=
6764 this->get_view(section_offset
, section_size
, true, false);
6766 this->gp_
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ 20);
6768 // Read the rest of .reginfo.
6769 this->gprmask_
= elfcpp::Swap
<size
, big_endian
>::readval(view
);
6770 this->cprmask1_
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ 4);
6771 this->cprmask2_
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ 8);
6772 this->cprmask3_
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ 12);
6773 this->cprmask4_
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ 16);
6776 if (shdr
.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES
)
6778 gold_assert(this->attributes_section_data_
== NULL
);
6779 section_offset_type section_offset
= shdr
.get_sh_offset();
6780 section_size_type section_size
=
6781 convert_to_section_size_type(shdr
.get_sh_size());
6782 const unsigned char* view
=
6783 this->get_view(section_offset
, section_size
, true, false);
6784 this->attributes_section_data_
=
6785 new Attributes_section_data(view
, section_size
);
6788 if (shdr
.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS
)
6790 gold_assert(this->abiflags_
== NULL
);
6791 section_offset_type section_offset
= shdr
.get_sh_offset();
6792 section_size_type section_size
=
6793 convert_to_section_size_type(shdr
.get_sh_size());
6794 const unsigned char* view
=
6795 this->get_view(section_offset
, section_size
, true, false);
6796 this->abiflags_
= new Mips_abiflags
<big_endian
>();
6798 this->abiflags_
->version
=
6799 elfcpp::Swap
<16, big_endian
>::readval(view
);
6800 if (this->abiflags_
->version
!= 0)
6802 gold_error(_("%s: .MIPS.abiflags section has "
6803 "unsupported version %u"),
6804 this->name().c_str(),
6805 this->abiflags_
->version
);
6808 this->abiflags_
->isa_level
=
6809 elfcpp::Swap
<8, big_endian
>::readval(view
+ 2);
6810 this->abiflags_
->isa_rev
=
6811 elfcpp::Swap
<8, big_endian
>::readval(view
+ 3);
6812 this->abiflags_
->gpr_size
=
6813 elfcpp::Swap
<8, big_endian
>::readval(view
+ 4);
6814 this->abiflags_
->cpr1_size
=
6815 elfcpp::Swap
<8, big_endian
>::readval(view
+ 5);
6816 this->abiflags_
->cpr2_size
=
6817 elfcpp::Swap
<8, big_endian
>::readval(view
+ 6);
6818 this->abiflags_
->fp_abi
=
6819 elfcpp::Swap
<8, big_endian
>::readval(view
+ 7);
6820 this->abiflags_
->isa_ext
=
6821 elfcpp::Swap
<32, big_endian
>::readval(view
+ 8);
6822 this->abiflags_
->ases
=
6823 elfcpp::Swap
<32, big_endian
>::readval(view
+ 12);
6824 this->abiflags_
->flags1
=
6825 elfcpp::Swap
<32, big_endian
>::readval(view
+ 16);
6826 this->abiflags_
->flags2
=
6827 elfcpp::Swap
<32, big_endian
>::readval(view
+ 20);
6830 // In the 64-bit ABI, .MIPS.options section holds register information.
6831 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6832 // starts with this header:
6836 // // Type of option.
6837 // unsigned char kind[1];
6838 // // Size of option descriptor, including header.
6839 // unsigned char size[1];
6840 // // Section index of affected section, or 0 for global option.
6841 // unsigned char section[2];
6842 // // Information specific to this kind of option.
6843 // unsigned char info[4];
6846 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6847 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6848 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
6850 if (shdr
.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS
)
6852 section_offset_type section_offset
= shdr
.get_sh_offset();
6853 section_size_type section_size
=
6854 convert_to_section_size_type(shdr
.get_sh_size());
6855 const unsigned char* view
=
6856 this->get_view(section_offset
, section_size
, true, false);
6857 const unsigned char* end
= view
+ section_size
;
6859 while (view
+ 8 <= end
)
6861 unsigned char kind
= elfcpp::Swap
<8, big_endian
>::readval(view
);
6862 unsigned char sz
= elfcpp::Swap
<8, big_endian
>::readval(view
+ 1);
6865 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6867 this->name().c_str(),
6868 this->mips_elf_options_section_name(), sz
);
6872 if (this->is_n64() && kind
== elfcpp::ODK_REGINFO
)
6874 // In the 64 bit ABI, an ODK_REGINFO option is the following
6875 // structure. The info field of the options header is not
6880 // // Mask of general purpose registers used.
6881 // unsigned char ri_gprmask[4];
6883 // unsigned char ri_pad[4];
6884 // // Mask of co-processor registers used.
6885 // unsigned char ri_cprmask[4][4];
6886 // // GP register value for this object file.
6887 // unsigned char ri_gp_value[8];
6890 this->gp_
= elfcpp::Swap
<size
, big_endian
>::readval(view
6893 else if (kind
== elfcpp::ODK_REGINFO
)
6895 // In the 32 bit ABI, an ODK_REGINFO option is the following
6896 // structure. The info field of the options header is not
6897 // used. The same structure is used in .reginfo section.
6901 // unsigned char ri_gprmask[4];
6902 // unsigned char ri_cprmask[4][4];
6903 // unsigned char ri_gp_value[4];
6906 this->gp_
= elfcpp::Swap
<size
, big_endian
>::readval(view
6913 const char* name
= pnames
+ shdr
.get_sh_name();
6914 this->section_is_mips16_fn_stub_
[i
] = is_prefix_of(".mips16.fn", name
);
6915 this->section_is_mips16_call_stub_
[i
] =
6916 is_prefix_of(".mips16.call.", name
);
6917 this->section_is_mips16_call_fp_stub_
[i
] =
6918 is_prefix_of(".mips16.call.fp.", name
);
6920 if (strcmp(name
, ".pdr") == 0)
6922 gold_assert(this->pdr_shndx_
== -1U);
6923 this->pdr_shndx_
= i
;
6928 // Discard MIPS16 stub secions that are not needed.
6930 template<int size
, bool big_endian
>
6932 Mips_relobj
<size
, big_endian
>::discard_mips16_stub_sections(Symbol_table
* symtab
)
6934 for (typename
Mips16_stubs_int_map::const_iterator
6935 it
= this->mips16_stub_sections_
.begin();
6936 it
!= this->mips16_stub_sections_
.end(); ++it
)
6938 Mips16_stub_section
<size
, big_endian
>* stub_section
= it
->second
;
6939 if (!stub_section
->is_target_found())
6941 gold_error(_("no relocation found in mips16 stub section '%s'"),
6942 stub_section
->object()
6943 ->section_name(stub_section
->shndx()).c_str());
6946 bool discard
= false;
6947 if (stub_section
->is_for_local_function())
6949 if (stub_section
->is_fn_stub())
6951 // This stub is for a local symbol. This stub will only
6952 // be needed if there is some relocation in this object,
6953 // other than a 16 bit function call, which refers to this
6955 if (!this->has_local_non_16bit_call_relocs(stub_section
->r_sym()))
6958 this->add_local_mips16_fn_stub(stub_section
);
6962 // This stub is for a local symbol. This stub will only
6963 // be needed if there is some relocation (R_MIPS16_26) in
6964 // this object that refers to this symbol.
6965 gold_assert(stub_section
->is_call_stub()
6966 || stub_section
->is_call_fp_stub());
6967 if (!this->has_local_16bit_call_relocs(stub_section
->r_sym()))
6970 this->add_local_mips16_call_stub(stub_section
);
6975 Mips_symbol
<size
>* gsym
= stub_section
->gsym();
6976 if (stub_section
->is_fn_stub())
6978 if (gsym
->has_mips16_fn_stub())
6979 // We already have a stub for this function.
6983 gsym
->set_mips16_fn_stub(stub_section
);
6984 if (gsym
->should_add_dynsym_entry(symtab
))
6986 // If we have a MIPS16 function with a stub, the
6987 // dynamic symbol must refer to the stub, since only
6988 // the stub uses the standard calling conventions.
6989 gsym
->set_need_fn_stub();
6990 if (gsym
->is_from_dynobj())
6991 gsym
->set_needs_dynsym_value();
6994 if (!gsym
->need_fn_stub())
6997 else if (stub_section
->is_call_stub())
6999 if (gsym
->is_mips16())
7000 // We don't need the call_stub; this is a 16 bit
7001 // function, so calls from other 16 bit functions are
7004 else if (gsym
->has_mips16_call_stub())
7005 // We already have a stub for this function.
7008 gsym
->set_mips16_call_stub(stub_section
);
7012 gold_assert(stub_section
->is_call_fp_stub());
7013 if (gsym
->is_mips16())
7014 // We don't need the call_stub; this is a 16 bit
7015 // function, so calls from other 16 bit functions are
7018 else if (gsym
->has_mips16_call_fp_stub())
7019 // We already have a stub for this function.
7022 gsym
->set_mips16_call_fp_stub(stub_section
);
7026 this->set_output_section(stub_section
->shndx(), NULL
);
7030 // Mips_output_data_la25_stub methods.
7032 // Template for standard LA25 stub.
7033 template<int size
, bool big_endian
>
7035 Mips_output_data_la25_stub
<size
, big_endian
>::la25_stub_entry
[] =
7037 0x3c190000, // lui $25,%hi(func)
7038 0x08000000, // j func
7039 0x27390000, // add $25,$25,%lo(func)
7043 // Template for microMIPS LA25 stub.
7044 template<int size
, bool big_endian
>
7046 Mips_output_data_la25_stub
<size
, big_endian
>::la25_stub_micromips_entry
[] =
7048 0x41b9, 0x0000, // lui t9,%hi(func)
7049 0xd400, 0x0000, // j func
7050 0x3339, 0x0000, // addiu t9,t9,%lo(func)
7051 0x0000, 0x0000 // nop
7054 // Create la25 stub for a symbol.
7056 template<int size
, bool big_endian
>
7058 Mips_output_data_la25_stub
<size
, big_endian
>::create_la25_stub(
7059 Symbol_table
* symtab
, Target_mips
<size
, big_endian
>* target
,
7060 Mips_symbol
<size
>* gsym
)
7062 if (!gsym
->has_la25_stub())
7064 gsym
->set_la25_stub_offset(this->symbols_
.size() * 16);
7065 this->symbols_
.push_back(gsym
);
7066 this->create_stub_symbol(gsym
, symtab
, target
, 16);
7070 // Create a symbol for SYM stub's value and size, to help make the disassembly
7073 template<int size
, bool big_endian
>
7075 Mips_output_data_la25_stub
<size
, big_endian
>::create_stub_symbol(
7076 Mips_symbol
<size
>* sym
, Symbol_table
* symtab
,
7077 Target_mips
<size
, big_endian
>* target
, uint64_t symsize
)
7079 std::string
name(".pic.");
7080 name
+= sym
->name();
7082 unsigned int offset
= sym
->la25_stub_offset();
7083 if (sym
->is_micromips())
7086 // Make it a local function.
7087 Symbol
* new_sym
= symtab
->define_in_output_data(name
.c_str(), NULL
,
7088 Symbol_table::PREDEFINED
,
7089 target
->la25_stub_section(),
7090 offset
, symsize
, elfcpp::STT_FUNC
,
7092 elfcpp::STV_DEFAULT
, 0,
7094 new_sym
->set_is_forced_local();
7097 // Write out la25 stubs. This uses the hand-coded instructions above,
7098 // and adjusts them as needed.
7100 template<int size
, bool big_endian
>
7102 Mips_output_data_la25_stub
<size
, big_endian
>::do_write(Output_file
* of
)
7104 const off_t offset
= this->offset();
7105 const section_size_type oview_size
=
7106 convert_to_section_size_type(this->data_size());
7107 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
7109 for (typename
std::vector
<Mips_symbol
<size
>*>::iterator
7110 p
= this->symbols_
.begin();
7111 p
!= this->symbols_
.end();
7114 Mips_symbol
<size
>* sym
= *p
;
7115 unsigned char* pov
= oview
+ sym
->la25_stub_offset();
7117 Mips_address target
= sym
->value();
7118 if (!sym
->is_micromips())
7120 elfcpp::Swap
<32, big_endian
>::writeval(pov
,
7121 la25_stub_entry
[0] | (((target
+ 0x8000) >> 16) & 0xffff));
7122 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4,
7123 la25_stub_entry
[1] | ((target
>> 2) & 0x3ffffff));
7124 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8,
7125 la25_stub_entry
[2] | (target
& 0xffff));
7126 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 12, la25_stub_entry
[3]);
7131 // First stub instruction. Paste high 16-bits of the target.
7132 elfcpp::Swap
<16, big_endian
>::writeval(pov
,
7133 la25_stub_micromips_entry
[0]);
7134 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2,
7135 ((target
+ 0x8000) >> 16) & 0xffff);
7136 // Second stub instruction. Paste low 26-bits of the target, shifted
7138 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 4,
7139 la25_stub_micromips_entry
[2] | ((target
>> 17) & 0x3ff));
7140 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 6,
7141 la25_stub_micromips_entry
[3] | ((target
>> 1) & 0xffff));
7142 // Third stub instruction. Paste low 16-bits of the target.
7143 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 8,
7144 la25_stub_micromips_entry
[4]);
7145 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 10, target
& 0xffff);
7146 // Fourth stub instruction.
7147 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 12,
7148 la25_stub_micromips_entry
[6]);
7149 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 14,
7150 la25_stub_micromips_entry
[7]);
7154 of
->write_output_view(offset
, oview_size
, oview
);
7157 // Mips_output_data_plt methods.
7159 // The format of the first PLT entry in an O32 executable.
7160 template<int size
, bool big_endian
>
7161 const uint32_t Mips_output_data_plt
<size
, big_endian
>::plt0_entry_o32
[] =
7163 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
7164 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
7165 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
7166 0x031cc023, // subu $24, $24, $28
7167 0x03e07825, // or $15, $31, zero
7168 0x0018c082, // srl $24, $24, 2
7169 0x0320f809, // jalr $25
7170 0x2718fffe // subu $24, $24, 2
7173 // The format of the first PLT entry in an N32 executable. Different
7174 // because gp ($28) is not available; we use t2 ($14) instead.
7175 template<int size
, bool big_endian
>
7176 const uint32_t Mips_output_data_plt
<size
, big_endian
>::plt0_entry_n32
[] =
7178 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7179 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
7180 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7181 0x030ec023, // subu $24, $24, $14
7182 0x03e07825, // or $15, $31, zero
7183 0x0018c082, // srl $24, $24, 2
7184 0x0320f809, // jalr $25
7185 0x2718fffe // subu $24, $24, 2
7188 // The format of the first PLT entry in an N64 executable. Different
7189 // from N32 because of the increased size of GOT entries.
7190 template<int size
, bool big_endian
>
7191 const uint32_t Mips_output_data_plt
<size
, big_endian
>::plt0_entry_n64
[] =
7193 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7194 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
7195 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7196 0x030ec023, // subu $24, $24, $14
7197 0x03e07825, // or $15, $31, zero
7198 0x0018c0c2, // srl $24, $24, 3
7199 0x0320f809, // jalr $25
7200 0x2718fffe // subu $24, $24, 2
7203 // The format of the microMIPS first PLT entry in an O32 executable.
7204 // We rely on v0 ($2) rather than t8 ($24) to contain the address
7205 // of the GOTPLT entry handled, so this stub may only be used when
7206 // all the subsequent PLT entries are microMIPS code too.
7208 // The trailing NOP is for alignment and correct disassembly only.
7209 template<int size
, bool big_endian
>
7210 const uint32_t Mips_output_data_plt
<size
, big_endian
>::
7211 plt0_entry_micromips_o32
[] =
7213 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
7214 0xff23, 0x0000, // lw $25, 0($3)
7215 0x0535, // subu $2, $2, $3
7216 0x2525, // srl $2, $2, 2
7217 0x3302, 0xfffe, // subu $24, $2, 2
7218 0x0dff, // move $15, $31
7219 0x45f9, // jalrs $25
7220 0x0f83, // move $28, $3
7224 // The format of the microMIPS first PLT entry in an O32 executable
7225 // in the insn32 mode.
7226 template<int size
, bool big_endian
>
7227 const uint32_t Mips_output_data_plt
<size
, big_endian
>::
7228 plt0_entry_micromips32_o32
[] =
7230 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
7231 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
7232 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
7233 0x0398, 0xc1d0, // subu $24, $24, $28
7234 0x001f, 0x7a90, // or $15, $31, zero
7235 0x0318, 0x1040, // srl $24, $24, 2
7236 0x03f9, 0x0f3c, // jalr $25
7237 0x3318, 0xfffe // subu $24, $24, 2
7240 // The format of subsequent standard entries in the PLT.
7241 template<int size
, bool big_endian
>
7242 const uint32_t Mips_output_data_plt
<size
, big_endian
>::plt_entry
[] =
7244 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7245 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7246 0x03200008, // jr $25
7247 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7250 // The format of subsequent R6 PLT entries.
7251 template<int size
, bool big_endian
>
7252 const uint32_t Mips_output_data_plt
<size
, big_endian
>::plt_entry_r6
[] =
7254 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7255 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7256 0x03200009, // jr $25
7257 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7260 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
7261 // temporary because t8 ($24) and t9 ($25) are not directly addressable.
7262 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7263 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7264 // target function address in register v0.
7265 template<int size
, bool big_endian
>
7266 const uint32_t Mips_output_data_plt
<size
, big_endian
>::plt_entry_mips16_o32
[] =
7268 0xb303, // lw $3, 12($pc)
7269 0x651b, // move $24, $3
7270 0x9b60, // lw $3, 0($3)
7272 0x653b, // move $25, $3
7274 0x0000, 0x0000 // .word (.got.plt entry)
7277 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
7278 // as a temporary because t8 ($24) is not addressable with ADDIUPC.
7279 template<int size
, bool big_endian
>
7280 const uint32_t Mips_output_data_plt
<size
, big_endian
>::
7281 plt_entry_micromips_o32
[] =
7283 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7284 0xff22, 0x0000, // lw $25, 0($2)
7286 0x0f02 // move $24, $2
7289 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7290 template<int size
, bool big_endian
>
7291 const uint32_t Mips_output_data_plt
<size
, big_endian
>::
7292 plt_entry_micromips32_o32
[] =
7294 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
7295 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
7296 0x0019, 0x0f3c, // jr $25
7297 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
7300 // Add an entry to the PLT for a symbol referenced by r_type relocation.
7302 template<int size
, bool big_endian
>
7304 Mips_output_data_plt
<size
, big_endian
>::add_entry(Mips_symbol
<size
>* gsym
,
7305 unsigned int r_type
)
7307 gold_assert(!gsym
->has_plt_offset());
7309 // Final PLT offset for a symbol will be set in method set_plt_offsets().
7310 gsym
->set_plt_offset(this->entry_count() * sizeof(plt_entry
)
7311 + sizeof(plt0_entry_o32
));
7312 this->symbols_
.push_back(gsym
);
7314 // Record whether the relocation requires a standard MIPS
7315 // or a compressed code entry.
7316 if (jal_reloc(r_type
))
7318 if (r_type
== elfcpp::R_MIPS_26
)
7319 gsym
->set_needs_mips_plt(true);
7321 gsym
->set_needs_comp_plt(true);
7324 section_offset_type got_offset
= this->got_plt_
->current_data_size();
7326 // Every PLT entry needs a GOT entry which points back to the PLT
7327 // entry (this will be changed by the dynamic linker, normally
7328 // lazily when the function is called).
7329 this->got_plt_
->set_current_data_size(got_offset
+ size
/8);
7331 gsym
->set_needs_dynsym_entry();
7332 this->rel_
->add_global(gsym
, elfcpp::R_MIPS_JUMP_SLOT
, this->got_plt_
,
7336 // Set final PLT offsets. For each symbol, determine whether standard or
7337 // compressed (MIPS16 or microMIPS) PLT entry is used.
7339 template<int size
, bool big_endian
>
7341 Mips_output_data_plt
<size
, big_endian
>::set_plt_offsets()
7343 // The sizes of individual PLT entries.
7344 unsigned int plt_mips_entry_size
= this->standard_plt_entry_size();
7345 unsigned int plt_comp_entry_size
= (!this->target_
->is_output_newabi()
7346 ? this->compressed_plt_entry_size() : 0);
7348 for (typename
std::vector
<Mips_symbol
<size
>*>::const_iterator
7349 p
= this->symbols_
.begin(); p
!= this->symbols_
.end(); ++p
)
7351 Mips_symbol
<size
>* mips_sym
= *p
;
7353 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7354 // so always use a standard entry there.
7356 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7357 // all MIPS16 calls will go via that stub, and there is no benefit
7358 // to having a MIPS16 entry. And in the case of call_stub a
7359 // standard entry actually has to be used as the stub ends with a J
7361 if (this->target_
->is_output_newabi()
7362 || mips_sym
->has_mips16_call_stub()
7363 || mips_sym
->has_mips16_call_fp_stub())
7365 mips_sym
->set_needs_mips_plt(true);
7366 mips_sym
->set_needs_comp_plt(false);
7369 // Otherwise, if there are no direct calls to the function, we
7370 // have a free choice of whether to use standard or compressed
7371 // entries. Prefer microMIPS entries if the object is known to
7372 // contain microMIPS code, so that it becomes possible to create
7373 // pure microMIPS binaries. Prefer standard entries otherwise,
7374 // because MIPS16 ones are no smaller and are usually slower.
7375 if (!mips_sym
->needs_mips_plt() && !mips_sym
->needs_comp_plt())
7377 if (this->target_
->is_output_micromips())
7378 mips_sym
->set_needs_comp_plt(true);
7380 mips_sym
->set_needs_mips_plt(true);
7383 if (mips_sym
->needs_mips_plt())
7385 mips_sym
->set_mips_plt_offset(this->plt_mips_offset_
);
7386 this->plt_mips_offset_
+= plt_mips_entry_size
;
7388 if (mips_sym
->needs_comp_plt())
7390 mips_sym
->set_comp_plt_offset(this->plt_comp_offset_
);
7391 this->plt_comp_offset_
+= plt_comp_entry_size
;
7395 // Figure out the size of the PLT header if we know that we are using it.
7396 if (this->plt_mips_offset_
+ this->plt_comp_offset_
!= 0)
7397 this->plt_header_size_
= this->get_plt_header_size();
7400 // Write out the PLT. This uses the hand-coded instructions above,
7401 // and adjusts them as needed.
7403 template<int size
, bool big_endian
>
7405 Mips_output_data_plt
<size
, big_endian
>::do_write(Output_file
* of
)
7407 const off_t offset
= this->offset();
7408 const section_size_type oview_size
=
7409 convert_to_section_size_type(this->data_size());
7410 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
7412 const off_t gotplt_file_offset
= this->got_plt_
->offset();
7413 const section_size_type gotplt_size
=
7414 convert_to_section_size_type(this->got_plt_
->data_size());
7415 unsigned char* const gotplt_view
= of
->get_output_view(gotplt_file_offset
,
7417 unsigned char* pov
= oview
;
7419 Mips_address plt_address
= this->address();
7421 // Calculate the address of .got.plt.
7422 Mips_address gotplt_addr
= this->got_plt_
->address();
7423 Mips_address gotplt_addr_high
= ((gotplt_addr
+ 0x8000) >> 16) & 0xffff;
7424 Mips_address gotplt_addr_low
= gotplt_addr
& 0xffff;
7426 // The PLT sequence is not safe for N64 if .got.plt's address can
7427 // not be loaded in two instructions.
7428 gold_assert((gotplt_addr
& ~(Mips_address
) 0x7fffffff) == 0
7429 || ~(gotplt_addr
| 0x7fffffff) == 0);
7431 // Write the PLT header.
7432 const uint32_t* plt0_entry
= this->get_plt_header_entry();
7433 if (plt0_entry
== plt0_entry_micromips_o32
)
7435 // Write microMIPS PLT header.
7436 gold_assert(gotplt_addr
% 4 == 0);
7438 Mips_address gotpc_offset
= gotplt_addr
- ((plt_address
| 3) ^ 3);
7440 // ADDIUPC has a span of +/-16MB, check we're in range.
7441 if (gotpc_offset
+ 0x1000000 >= 0x2000000)
7443 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7444 "ADDIUPC"), (long)gotpc_offset
);
7448 elfcpp::Swap
<16, big_endian
>::writeval(pov
,
7449 plt0_entry
[0] | ((gotpc_offset
>> 18) & 0x7f));
7450 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2,
7451 (gotpc_offset
>> 2) & 0xffff);
7453 for (unsigned int i
= 2;
7454 i
< (sizeof(plt0_entry_micromips_o32
)
7455 / sizeof(plt0_entry_micromips_o32
[0]));
7458 elfcpp::Swap
<16, big_endian
>::writeval(pov
, plt0_entry
[i
]);
7462 else if (plt0_entry
== plt0_entry_micromips32_o32
)
7464 // Write microMIPS PLT header in insn32 mode.
7465 elfcpp::Swap
<16, big_endian
>::writeval(pov
, plt0_entry
[0]);
7466 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, gotplt_addr_high
);
7467 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 4, plt0_entry
[2]);
7468 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 6, gotplt_addr_low
);
7469 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 8, plt0_entry
[4]);
7470 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 10, gotplt_addr_low
);
7472 for (unsigned int i
= 6;
7473 i
< (sizeof(plt0_entry_micromips32_o32
)
7474 / sizeof(plt0_entry_micromips32_o32
[0]));
7477 elfcpp::Swap
<16, big_endian
>::writeval(pov
, plt0_entry
[i
]);
7483 // Write standard PLT header.
7484 elfcpp::Swap
<32, big_endian
>::writeval(pov
,
7485 plt0_entry
[0] | gotplt_addr_high
);
7486 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4,
7487 plt0_entry
[1] | gotplt_addr_low
);
7488 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8,
7489 plt0_entry
[2] | gotplt_addr_low
);
7491 for (int i
= 3; i
< 8; i
++)
7493 elfcpp::Swap
<32, big_endian
>::writeval(pov
, plt0_entry
[i
]);
7499 unsigned char* gotplt_pov
= gotplt_view
;
7500 unsigned int got_entry_size
= size
/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7502 // The first two entries in .got.plt are reserved.
7503 elfcpp::Swap
<size
, big_endian
>::writeval(gotplt_pov
, 0);
7504 elfcpp::Swap
<size
, big_endian
>::writeval(gotplt_pov
+ got_entry_size
, 0);
7506 unsigned int gotplt_offset
= 2 * got_entry_size
;
7507 gotplt_pov
+= 2 * got_entry_size
;
7509 // Calculate the address of the PLT header.
7510 Mips_address header_address
= (plt_address
7511 + (this->is_plt_header_compressed() ? 1 : 0));
7513 // Initialize compressed PLT area view.
7514 unsigned char* pov2
= pov
+ this->plt_mips_offset_
;
7516 // Write the PLT entries.
7517 for (typename
std::vector
<Mips_symbol
<size
>*>::const_iterator
7518 p
= this->symbols_
.begin();
7519 p
!= this->symbols_
.end();
7520 ++p
, gotplt_pov
+= got_entry_size
, gotplt_offset
+= got_entry_size
)
7522 Mips_symbol
<size
>* mips_sym
= *p
;
7524 // Calculate the address of the .got.plt entry.
7525 uint32_t gotplt_entry_addr
= (gotplt_addr
+ gotplt_offset
);
7526 uint32_t gotplt_entry_addr_hi
= (((gotplt_entry_addr
+ 0x8000) >> 16)
7528 uint32_t gotplt_entry_addr_lo
= gotplt_entry_addr
& 0xffff;
7530 // Initially point the .got.plt entry at the PLT header.
7531 if (this->target_
->is_output_n64())
7532 elfcpp::Swap
<64, big_endian
>::writeval(gotplt_pov
, header_address
);
7534 elfcpp::Swap
<32, big_endian
>::writeval(gotplt_pov
, header_address
);
7536 // Now handle the PLT itself. First the standard entry.
7537 if (mips_sym
->has_mips_plt_offset())
7539 // Pick the load opcode (LW or LD).
7540 uint64_t load
= this->target_
->is_output_n64() ? 0xdc000000
7543 const uint32_t* entry
= this->target_
->is_output_r6() ? plt_entry_r6
7546 // Fill in the PLT entry itself.
7547 elfcpp::Swap
<32, big_endian
>::writeval(pov
,
7548 entry
[0] | gotplt_entry_addr_hi
);
7549 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4,
7550 entry
[1] | gotplt_entry_addr_lo
| load
);
7551 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 8, entry
[2]);
7552 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 12,
7553 entry
[3] | gotplt_entry_addr_lo
);
7557 // Now the compressed entry. They come after any standard ones.
7558 if (mips_sym
->has_comp_plt_offset())
7560 if (!this->target_
->is_output_micromips())
7562 // Write MIPS16 PLT entry.
7563 const uint32_t* plt_entry
= plt_entry_mips16_o32
;
7565 elfcpp::Swap
<16, big_endian
>::writeval(pov2
, plt_entry
[0]);
7566 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 2, plt_entry
[1]);
7567 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 4, plt_entry
[2]);
7568 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 6, plt_entry
[3]);
7569 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 8, plt_entry
[4]);
7570 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 10, plt_entry
[5]);
7571 elfcpp::Swap
<32, big_endian
>::writeval(pov2
+ 12,
7575 else if (this->target_
->use_32bit_micromips_instructions())
7577 // Write microMIPS PLT entry in insn32 mode.
7578 const uint32_t* plt_entry
= plt_entry_micromips32_o32
;
7580 elfcpp::Swap
<16, big_endian
>::writeval(pov2
, plt_entry
[0]);
7581 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 2,
7582 gotplt_entry_addr_hi
);
7583 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 4, plt_entry
[2]);
7584 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 6,
7585 gotplt_entry_addr_lo
);
7586 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 8, plt_entry
[4]);
7587 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 10, plt_entry
[5]);
7588 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 12, plt_entry
[6]);
7589 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 14,
7590 gotplt_entry_addr_lo
);
7595 // Write microMIPS PLT entry.
7596 const uint32_t* plt_entry
= plt_entry_micromips_o32
;
7598 gold_assert(gotplt_entry_addr
% 4 == 0);
7600 Mips_address loc_address
= plt_address
+ pov2
- oview
;
7601 int gotpc_offset
= gotplt_entry_addr
- ((loc_address
| 3) ^ 3);
7603 // ADDIUPC has a span of +/-16MB, check we're in range.
7604 if (gotpc_offset
+ 0x1000000 >= 0x2000000)
7606 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7607 "range of ADDIUPC"), (long)gotpc_offset
);
7611 elfcpp::Swap
<16, big_endian
>::writeval(pov2
,
7612 plt_entry
[0] | ((gotpc_offset
>> 18) & 0x7f));
7613 elfcpp::Swap
<16, big_endian
>::writeval(
7614 pov2
+ 2, (gotpc_offset
>> 2) & 0xffff);
7615 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 4, plt_entry
[2]);
7616 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 6, plt_entry
[3]);
7617 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 8, plt_entry
[4]);
7618 elfcpp::Swap
<16, big_endian
>::writeval(pov2
+ 10, plt_entry
[5]);
7624 // Check the number of bytes written for standard entries.
7625 gold_assert(static_cast<section_size_type
>(
7626 pov
- oview
- this->plt_header_size_
) == this->plt_mips_offset_
);
7627 // Check the number of bytes written for compressed entries.
7628 gold_assert((static_cast<section_size_type
>(pov2
- pov
)
7629 == this->plt_comp_offset_
));
7630 // Check the total number of bytes written.
7631 gold_assert(static_cast<section_size_type
>(pov2
- oview
) == oview_size
);
7633 gold_assert(static_cast<section_size_type
>(gotplt_pov
- gotplt_view
)
7636 of
->write_output_view(offset
, oview_size
, oview
);
7637 of
->write_output_view(gotplt_file_offset
, gotplt_size
, gotplt_view
);
7640 // Mips_output_data_mips_stubs methods.
7642 // The format of the lazy binding stub when dynamic symbol count is less than
7643 // 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7644 template<int size
, bool big_endian
>
7646 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_normal_1
[4] =
7648 0x8f998010, // lw t9,0x8010(gp)
7649 0x03e07825, // or t7,ra,zero
7650 0x0320f809, // jalr t9,ra
7651 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7654 // The format of the lazy binding stub when dynamic symbol count is less than
7655 // 64K, dynamic symbol index is less than 32K, and ABI is N64.
7656 template<int size
, bool big_endian
>
7658 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_normal_1_n64
[4] =
7660 0xdf998010, // ld t9,0x8010(gp)
7661 0x03e07825, // or t7,ra,zero
7662 0x0320f809, // jalr t9,ra
7663 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7666 // The format of the lazy binding stub when dynamic symbol count is less than
7667 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7668 template<int size
, bool big_endian
>
7670 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_normal_2
[4] =
7672 0x8f998010, // lw t9,0x8010(gp)
7673 0x03e07825, // or t7,ra,zero
7674 0x0320f809, // jalr t9,ra
7675 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7678 // The format of the lazy binding stub when dynamic symbol count is less than
7679 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7680 template<int size
, bool big_endian
>
7682 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_normal_2_n64
[4] =
7684 0xdf998010, // ld t9,0x8010(gp)
7685 0x03e07825, // or t7,ra,zero
7686 0x0320f809, // jalr t9,ra
7687 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7690 // The format of the lazy binding stub when dynamic symbol count is greater than
7691 // 64K, and ABI is not N64.
7692 template<int size
, bool big_endian
>
7693 const uint32_t Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_big
[5] =
7695 0x8f998010, // lw t9,0x8010(gp)
7696 0x03e07825, // or t7,ra,zero
7697 0x3c180000, // lui t8,DYN_INDEX
7698 0x0320f809, // jalr t9,ra
7699 0x37180000 // ori t8,t8,DYN_INDEX
7702 // The format of the lazy binding stub when dynamic symbol count is greater than
7703 // 64K, and ABI is N64.
7704 template<int size
, bool big_endian
>
7706 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_big_n64
[5] =
7708 0xdf998010, // ld t9,0x8010(gp)
7709 0x03e07825, // or t7,ra,zero
7710 0x3c180000, // lui t8,DYN_INDEX
7711 0x0320f809, // jalr t9,ra
7712 0x37180000 // ori t8,t8,DYN_INDEX
7717 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7718 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7719 template<int size
, bool big_endian
>
7721 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_micromips_normal_1
[] =
7723 0xff3c, 0x8010, // lw t9,0x8010(gp)
7724 0x0dff, // move t7,ra
7726 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7729 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7730 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7731 template<int size
, bool big_endian
>
7733 Mips_output_data_mips_stubs
<size
, big_endian
>::
7734 lazy_stub_micromips_normal_1_n64
[] =
7736 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7737 0x0dff, // move t7,ra
7739 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7742 // The format of the microMIPS lazy binding stub when dynamic symbol
7743 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7744 // and ABI is not N64.
7745 template<int size
, bool big_endian
>
7747 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_micromips_normal_2
[] =
7749 0xff3c, 0x8010, // lw t9,0x8010(gp)
7750 0x0dff, // move t7,ra
7752 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7755 // The format of the microMIPS lazy binding stub when dynamic symbol
7756 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7758 template<int size
, bool big_endian
>
7760 Mips_output_data_mips_stubs
<size
, big_endian
>::
7761 lazy_stub_micromips_normal_2_n64
[] =
7763 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7764 0x0dff, // move t7,ra
7766 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7769 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7770 // greater than 64K, and ABI is not N64.
7771 template<int size
, bool big_endian
>
7773 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_micromips_big
[] =
7775 0xff3c, 0x8010, // lw t9,0x8010(gp)
7776 0x0dff, // move t7,ra
7777 0x41b8, 0x0000, // lui t8,DYN_INDEX
7779 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7782 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7783 // greater than 64K, and ABI is N64.
7784 template<int size
, bool big_endian
>
7786 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_micromips_big_n64
[] =
7788 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7789 0x0dff, // move t7,ra
7790 0x41b8, 0x0000, // lui t8,DYN_INDEX
7792 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7795 // 32-bit microMIPS stubs.
7797 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7798 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7799 // can use only 32-bit instructions.
7800 template<int size
, bool big_endian
>
7802 Mips_output_data_mips_stubs
<size
, big_endian
>::
7803 lazy_stub_micromips32_normal_1
[] =
7805 0xff3c, 0x8010, // lw t9,0x8010(gp)
7806 0x001f, 0x7a90, // or t7,ra,zero
7807 0x03f9, 0x0f3c, // jalr ra,t9
7808 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7811 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7812 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7813 // use only 32-bit instructions.
7814 template<int size
, bool big_endian
>
7816 Mips_output_data_mips_stubs
<size
, big_endian
>::
7817 lazy_stub_micromips32_normal_1_n64
[] =
7819 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7820 0x001f, 0x7a90, // or t7,ra,zero
7821 0x03f9, 0x0f3c, // jalr ra,t9
7822 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7825 // The format of the microMIPS lazy binding stub when dynamic symbol
7826 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7827 // ABI is not N64, and we can use only 32-bit instructions.
7828 template<int size
, bool big_endian
>
7830 Mips_output_data_mips_stubs
<size
, big_endian
>::
7831 lazy_stub_micromips32_normal_2
[] =
7833 0xff3c, 0x8010, // lw t9,0x8010(gp)
7834 0x001f, 0x7a90, // or t7,ra,zero
7835 0x03f9, 0x0f3c, // jalr ra,t9
7836 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7839 // The format of the microMIPS lazy binding stub when dynamic symbol
7840 // count is less than 64K, dynamic symbol index is between 32K and 64K,
7841 // ABI is N64, and we can use only 32-bit instructions.
7842 template<int size
, bool big_endian
>
7844 Mips_output_data_mips_stubs
<size
, big_endian
>::
7845 lazy_stub_micromips32_normal_2_n64
[] =
7847 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7848 0x001f, 0x7a90, // or t7,ra,zero
7849 0x03f9, 0x0f3c, // jalr ra,t9
7850 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7853 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7854 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7855 template<int size
, bool big_endian
>
7857 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_micromips32_big
[] =
7859 0xff3c, 0x8010, // lw t9,0x8010(gp)
7860 0x001f, 0x7a90, // or t7,ra,zero
7861 0x41b8, 0x0000, // lui t8,DYN_INDEX
7862 0x03f9, 0x0f3c, // jalr ra,t9
7863 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7866 // The format of the microMIPS lazy binding stub when dynamic symbol count is
7867 // greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7868 template<int size
, bool big_endian
>
7870 Mips_output_data_mips_stubs
<size
, big_endian
>::lazy_stub_micromips32_big_n64
[] =
7872 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7873 0x001f, 0x7a90, // or t7,ra,zero
7874 0x41b8, 0x0000, // lui t8,DYN_INDEX
7875 0x03f9, 0x0f3c, // jalr ra,t9
7876 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7879 // Create entry for a symbol.
7881 template<int size
, bool big_endian
>
7883 Mips_output_data_mips_stubs
<size
, big_endian
>::make_entry(
7884 Mips_symbol
<size
>* gsym
)
7886 if (!gsym
->has_lazy_stub() && !gsym
->has_plt_offset())
7888 this->symbols_
.insert(gsym
);
7889 gsym
->set_has_lazy_stub(true);
7893 // Remove entry for a symbol.
7895 template<int size
, bool big_endian
>
7897 Mips_output_data_mips_stubs
<size
, big_endian
>::remove_entry(
7898 Mips_symbol
<size
>* gsym
)
7900 if (gsym
->has_lazy_stub())
7902 this->symbols_
.erase(gsym
);
7903 gsym
->set_has_lazy_stub(false);
7907 // Set stub offsets for symbols. This method expects that the number of
7908 // entries in dynamic symbol table is set.
7910 template<int size
, bool big_endian
>
7912 Mips_output_data_mips_stubs
<size
, big_endian
>::set_lazy_stub_offsets()
7914 gold_assert(this->dynsym_count_
!= -1U);
7916 if (this->stub_offsets_are_set_
)
7919 unsigned int stub_size
= this->stub_size();
7920 unsigned int offset
= 0;
7921 for (typename
Mips_stubs_entry_set::const_iterator
7922 p
= this->symbols_
.begin();
7923 p
!= this->symbols_
.end();
7924 ++p
, offset
+= stub_size
)
7926 Mips_symbol
<size
>* mips_sym
= *p
;
7927 mips_sym
->set_lazy_stub_offset(offset
);
7929 this->stub_offsets_are_set_
= true;
7932 template<int size
, bool big_endian
>
7934 Mips_output_data_mips_stubs
<size
, big_endian
>::set_needs_dynsym_value()
7936 for (typename
Mips_stubs_entry_set::const_iterator
7937 p
= this->symbols_
.begin(); p
!= this->symbols_
.end(); ++p
)
7939 Mips_symbol
<size
>* sym
= *p
;
7940 if (sym
->is_from_dynobj())
7941 sym
->set_needs_dynsym_value();
7945 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
7946 // adjusts them as needed.
7948 template<int size
, bool big_endian
>
7950 Mips_output_data_mips_stubs
<size
, big_endian
>::do_write(Output_file
* of
)
7952 const off_t offset
= this->offset();
7953 const section_size_type oview_size
=
7954 convert_to_section_size_type(this->data_size());
7955 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
7957 bool big_stub
= this->dynsym_count_
> 0x10000;
7959 unsigned char* pov
= oview
;
7960 for (typename
Mips_stubs_entry_set::const_iterator
7961 p
= this->symbols_
.begin(); p
!= this->symbols_
.end(); ++p
)
7963 Mips_symbol
<size
>* sym
= *p
;
7964 const uint32_t* lazy_stub
;
7965 bool n64
= this->target_
->is_output_n64();
7967 if (!this->target_
->is_output_micromips())
7969 // Write standard (non-microMIPS) stub.
7972 if (sym
->dynsym_index() & ~0x7fff)
7973 // Dynsym index is between 32K and 64K.
7974 lazy_stub
= n64
? lazy_stub_normal_2_n64
: lazy_stub_normal_2
;
7976 // Dynsym index is less than 32K.
7977 lazy_stub
= n64
? lazy_stub_normal_1_n64
: lazy_stub_normal_1
;
7980 lazy_stub
= n64
? lazy_stub_big_n64
: lazy_stub_big
;
7983 elfcpp::Swap
<32, big_endian
>::writeval(pov
, lazy_stub
[i
]);
7984 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, lazy_stub
[i
+ 1]);
7990 // LUI instruction of the big stub. Paste high 16 bits of the
7992 elfcpp::Swap
<32, big_endian
>::writeval(pov
,
7993 lazy_stub
[i
] | ((sym
->dynsym_index() >> 16) & 0x7fff));
7997 elfcpp::Swap
<32, big_endian
>::writeval(pov
, lazy_stub
[i
]);
7998 // Last stub instruction. Paste low 16 bits of the dynsym index.
7999 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4,
8000 lazy_stub
[i
+ 1] | (sym
->dynsym_index() & 0xffff));
8003 else if (this->target_
->use_32bit_micromips_instructions())
8005 // Write microMIPS stub in insn32 mode.
8008 if (sym
->dynsym_index() & ~0x7fff)
8009 // Dynsym index is between 32K and 64K.
8010 lazy_stub
= n64
? lazy_stub_micromips32_normal_2_n64
8011 : lazy_stub_micromips32_normal_2
;
8013 // Dynsym index is less than 32K.
8014 lazy_stub
= n64
? lazy_stub_micromips32_normal_1_n64
8015 : lazy_stub_micromips32_normal_1
;
8018 lazy_stub
= n64
? lazy_stub_micromips32_big_n64
8019 : lazy_stub_micromips32_big
;
8022 // First stub instruction. We emit 32-bit microMIPS instructions by
8023 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8024 // the instruction where the opcode is must always come first, for
8025 // both little and big endian.
8026 elfcpp::Swap
<16, big_endian
>::writeval(pov
, lazy_stub
[i
]);
8027 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lazy_stub
[i
+ 1]);
8028 // Second stub instruction.
8029 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 4, lazy_stub
[i
+ 2]);
8030 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 6, lazy_stub
[i
+ 3]);
8035 // LUI instruction of the big stub. Paste high 16 bits of the
8037 elfcpp::Swap
<16, big_endian
>::writeval(pov
, lazy_stub
[i
]);
8038 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2,
8039 (sym
->dynsym_index() >> 16) & 0x7fff);
8043 elfcpp::Swap
<16, big_endian
>::writeval(pov
, lazy_stub
[i
]);
8044 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lazy_stub
[i
+ 1]);
8045 // Last stub instruction. Paste low 16 bits of the dynsym index.
8046 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 4, lazy_stub
[i
+ 2]);
8047 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 6,
8048 sym
->dynsym_index() & 0xffff);
8053 // Write microMIPS stub.
8056 if (sym
->dynsym_index() & ~0x7fff)
8057 // Dynsym index is between 32K and 64K.
8058 lazy_stub
= n64
? lazy_stub_micromips_normal_2_n64
8059 : lazy_stub_micromips_normal_2
;
8061 // Dynsym index is less than 32K.
8062 lazy_stub
= n64
? lazy_stub_micromips_normal_1_n64
8063 : lazy_stub_micromips_normal_1
;
8066 lazy_stub
= n64
? lazy_stub_micromips_big_n64
8067 : lazy_stub_micromips_big
;
8070 // First stub instruction. We emit 32-bit microMIPS instructions by
8071 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8072 // the instruction where the opcode is must always come first, for
8073 // both little and big endian.
8074 elfcpp::Swap
<16, big_endian
>::writeval(pov
, lazy_stub
[i
]);
8075 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lazy_stub
[i
+ 1]);
8076 // Second stub instruction.
8077 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 4, lazy_stub
[i
+ 2]);
8082 // LUI instruction of the big stub. Paste high 16 bits of the
8084 elfcpp::Swap
<16, big_endian
>::writeval(pov
, lazy_stub
[i
]);
8085 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2,
8086 (sym
->dynsym_index() >> 16) & 0x7fff);
8090 elfcpp::Swap
<16, big_endian
>::writeval(pov
, lazy_stub
[i
]);
8091 // Last stub instruction. Paste low 16 bits of the dynsym index.
8092 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 2, lazy_stub
[i
+ 1]);
8093 elfcpp::Swap
<16, big_endian
>::writeval(pov
+ 4,
8094 sym
->dynsym_index() & 0xffff);
8099 // We always allocate 20 bytes for every stub, because final dynsym count is
8100 // not known in method do_finalize_sections. There are 4 unused bytes per
8101 // stub if final dynsym count is less than 0x10000.
8102 unsigned int used
= pov
- oview
;
8103 unsigned int unused
= big_stub
? 0 : this->symbols_
.size() * 4;
8104 gold_assert(static_cast<section_size_type
>(used
+ unused
) == oview_size
);
8106 // Fill the unused space with zeroes.
8107 // TODO(sasa): Can we strip unused bytes during the relaxation?
8109 memset(pov
, 0, unused
);
8111 of
->write_output_view(offset
, oview_size
, oview
);
8114 // Mips_output_section_reginfo methods.
8116 template<int size
, bool big_endian
>
8118 Mips_output_section_reginfo
<size
, big_endian
>::do_write(Output_file
* of
)
8120 off_t offset
= this->offset();
8121 off_t data_size
= this->data_size();
8123 unsigned char* view
= of
->get_output_view(offset
, data_size
);
8124 elfcpp::Swap
<size
, big_endian
>::writeval(view
, this->gprmask_
);
8125 elfcpp::Swap
<size
, big_endian
>::writeval(view
+ 4, this->cprmask1_
);
8126 elfcpp::Swap
<size
, big_endian
>::writeval(view
+ 8, this->cprmask2_
);
8127 elfcpp::Swap
<size
, big_endian
>::writeval(view
+ 12, this->cprmask3_
);
8128 elfcpp::Swap
<size
, big_endian
>::writeval(view
+ 16, this->cprmask4_
);
8129 // Write the gp value.
8130 elfcpp::Swap
<size
, big_endian
>::writeval(view
+ 20,
8131 this->target_
->gp_value());
8133 of
->write_output_view(offset
, data_size
, view
);
8136 // Mips_output_section_abiflags methods.
8138 template<int size
, bool big_endian
>
8140 Mips_output_section_abiflags
<size
, big_endian
>::do_write(Output_file
* of
)
8142 off_t offset
= this->offset();
8143 off_t data_size
= this->data_size();
8145 unsigned char* view
= of
->get_output_view(offset
, data_size
);
8146 elfcpp::Swap
<16, big_endian
>::writeval(view
, this->abiflags_
.version
);
8147 elfcpp::Swap
<8, big_endian
>::writeval(view
+ 2, this->abiflags_
.isa_level
);
8148 elfcpp::Swap
<8, big_endian
>::writeval(view
+ 3, this->abiflags_
.isa_rev
);
8149 elfcpp::Swap
<8, big_endian
>::writeval(view
+ 4, this->abiflags_
.gpr_size
);
8150 elfcpp::Swap
<8, big_endian
>::writeval(view
+ 5, this->abiflags_
.cpr1_size
);
8151 elfcpp::Swap
<8, big_endian
>::writeval(view
+ 6, this->abiflags_
.cpr2_size
);
8152 elfcpp::Swap
<8, big_endian
>::writeval(view
+ 7, this->abiflags_
.fp_abi
);
8153 elfcpp::Swap
<32, big_endian
>::writeval(view
+ 8, this->abiflags_
.isa_ext
);
8154 elfcpp::Swap
<32, big_endian
>::writeval(view
+ 12, this->abiflags_
.ases
);
8155 elfcpp::Swap
<32, big_endian
>::writeval(view
+ 16, this->abiflags_
.flags1
);
8156 elfcpp::Swap
<32, big_endian
>::writeval(view
+ 20, this->abiflags_
.flags2
);
8158 of
->write_output_view(offset
, data_size
, view
);
8161 // Mips_copy_relocs methods.
8163 // Emit any saved relocs.
8165 template<int sh_type
, int size
, bool big_endian
>
8167 Mips_copy_relocs
<sh_type
, size
, big_endian
>::emit_mips(
8168 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
,
8169 Symbol_table
* symtab
, Layout
* layout
, Target_mips
<size
, big_endian
>* target
)
8171 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
8172 Copy_reloc_entries::iterator p
= this->entries_
.begin();
8173 p
!= this->entries_
.end();
8175 emit_entry(*p
, reloc_section
, symtab
, layout
, target
);
8177 // We no longer need the saved information.
8178 this->entries_
.clear();
8181 // Emit the reloc if appropriate.
8183 template<int sh_type
, int size
, bool big_endian
>
8185 Mips_copy_relocs
<sh_type
, size
, big_endian
>::emit_entry(
8186 Copy_reloc_entry
& entry
,
8187 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
,
8188 Symbol_table
* symtab
, Layout
* layout
, Target_mips
<size
, big_endian
>* target
)
8190 // If the symbol is no longer defined in a dynamic object, then we
8191 // emitted a COPY relocation, and we do not want to emit this
8192 // dynamic relocation.
8193 if (!entry
.sym_
->is_from_dynobj())
8196 bool can_make_dynamic
= (entry
.reloc_type_
== elfcpp::R_MIPS_32
8197 || entry
.reloc_type_
== elfcpp::R_MIPS_REL32
8198 || entry
.reloc_type_
== elfcpp::R_MIPS_64
);
8200 Mips_symbol
<size
>* sym
= Mips_symbol
<size
>::as_mips_sym(entry
.sym_
);
8201 if (can_make_dynamic
&& !sym
->has_static_relocs())
8203 Mips_relobj
<size
, big_endian
>* object
=
8204 Mips_relobj
<size
, big_endian
>::as_mips_relobj(entry
.relobj_
);
8205 target
->got_section(symtab
, layout
)->record_global_got_symbol(
8206 sym
, object
, entry
.reloc_type_
, true, false);
8207 if (!symbol_references_local(sym
, sym
->should_add_dynsym_entry(symtab
)))
8208 target
->rel_dyn_section(layout
)->add_global(sym
, elfcpp::R_MIPS_REL32
,
8209 entry
.output_section_
, entry
.relobj_
, entry
.shndx_
, entry
.address_
);
8211 target
->rel_dyn_section(layout
)->add_symbolless_global_addend(
8212 sym
, elfcpp::R_MIPS_REL32
, entry
.output_section_
, entry
.relobj_
,
8213 entry
.shndx_
, entry
.address_
);
8216 this->make_copy_reloc(symtab
, layout
,
8217 static_cast<Sized_symbol
<size
>*>(entry
.sym_
),
8222 // Target_mips methods.
8224 // Return the value to use for a dynamic symbol which requires special
8225 // treatment. This is how we support equality comparisons of function
8226 // pointers across shared library boundaries, as described in the
8227 // processor specific ABI supplement.
8229 template<int size
, bool big_endian
>
8231 Target_mips
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
8234 const Mips_symbol
<size
>* mips_sym
= Mips_symbol
<size
>::as_mips_sym(gsym
);
8236 if (!mips_sym
->has_lazy_stub())
8238 if (mips_sym
->has_plt_offset())
8240 // We distinguish between PLT entries and lazy-binding stubs by
8241 // giving the former an st_other value of STO_MIPS_PLT. Set the
8242 // value to the stub address if there are any relocations in the
8243 // binary where pointer equality matters.
8244 if (mips_sym
->pointer_equality_needed())
8246 // Prefer a standard MIPS PLT entry.
8247 if (mips_sym
->has_mips_plt_offset())
8248 value
= this->plt_section()->mips_entry_address(mips_sym
);
8250 value
= this->plt_section()->comp_entry_address(mips_sym
) + 1;
8258 // First, set stub offsets for symbols. This method expects that the
8259 // number of entries in dynamic symbol table is set.
8260 this->mips_stubs_section()->set_lazy_stub_offsets();
8262 // The run-time linker uses the st_value field of the symbol
8263 // to reset the global offset table entry for this external
8264 // to its stub address when unlinking a shared object.
8265 value
= this->mips_stubs_section()->stub_address(mips_sym
);
8268 if (mips_sym
->has_mips16_fn_stub())
8270 // If we have a MIPS16 function with a stub, the dynamic symbol must
8271 // refer to the stub, since only the stub uses the standard calling
8273 value
= mips_sym
->template
8274 get_mips16_fn_stub
<big_endian
>()->output_address();
8280 // Get the dynamic reloc section, creating it if necessary. It's always
8281 // .rel.dyn, even for MIPS64.
8283 template<int size
, bool big_endian
>
8284 typename Target_mips
<size
, big_endian
>::Reloc_section
*
8285 Target_mips
<size
, big_endian
>::rel_dyn_section(Layout
* layout
)
8287 if (this->rel_dyn_
== NULL
)
8289 gold_assert(layout
!= NULL
);
8290 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
8291 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
8292 elfcpp::SHF_ALLOC
, this->rel_dyn_
,
8293 ORDER_DYNAMIC_RELOCS
, false);
8295 // First entry in .rel.dyn has to be null.
8296 // This is hack - we define dummy output data and set its address to 0,
8297 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8298 // This ensures that the entry is null.
8299 Output_data
* od
= new Output_data_zero_fill(0, 0);
8301 this->rel_dyn_
->add_absolute(elfcpp::R_MIPS_NONE
, od
, 0);
8303 return this->rel_dyn_
;
8306 // Get the GOT section, creating it if necessary.
8308 template<int size
, bool big_endian
>
8309 Mips_output_data_got
<size
, big_endian
>*
8310 Target_mips
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
8313 if (this->got_
== NULL
)
8315 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
8317 this->got_
= new Mips_output_data_got
<size
, big_endian
>(this, symtab
,
8319 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
8320 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
|
8321 elfcpp::SHF_MIPS_GPREL
),
8322 this->got_
, ORDER_DATA
, false);
8324 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8325 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
8326 Symbol_table::PREDEFINED
,
8328 0, 0, elfcpp::STT_OBJECT
,
8330 elfcpp::STV_DEFAULT
, 0,
8337 // Calculate value of _gp symbol.
8339 template<int size
, bool big_endian
>
8341 Target_mips
<size
, big_endian
>::set_gp(Layout
* layout
, Symbol_table
* symtab
)
8343 if (this->gp_
!= NULL
)
8346 Output_data
* section
= layout
->find_output_section(".got");
8347 if (section
== NULL
)
8349 // If there is no .got section, gp should be based on .sdata.
8350 // TODO(sasa): This is probably not needed. This was needed for older
8351 // MIPS architectures which accessed both GOT and .sdata section using
8352 // gp-relative addressing. Modern Mips Linux ELF architectures don't
8353 // access .sdata using gp-relative addressing.
8354 for (Layout::Section_list::const_iterator
8355 p
= layout
->section_list().begin();
8356 p
!= layout
->section_list().end();
8359 if (strcmp((*p
)->name(), ".sdata") == 0)
8367 Sized_symbol
<size
>* gp
=
8368 static_cast<Sized_symbol
<size
>*>(symtab
->lookup("_gp"));
8371 if (gp
->source() != Symbol::IS_CONSTANT
&& section
!= NULL
)
8372 gp
->init_output_data(gp
->name(), NULL
, section
, MIPS_GP_OFFSET
, 0,
8375 elfcpp::STV_DEFAULT
, 0,
8379 else if (section
!= NULL
)
8381 gp
= static_cast<Sized_symbol
<size
>*>(symtab
->define_in_output_data(
8382 "_gp", NULL
, Symbol_table::PREDEFINED
,
8383 section
, MIPS_GP_OFFSET
, 0,
8386 elfcpp::STV_DEFAULT
,
8392 // Set the dynamic symbol indexes. INDEX is the index of the first
8393 // global dynamic symbol. Pointers to the symbols are stored into the
8394 // vector SYMS. The names are added to DYNPOOL. This returns an
8395 // updated dynamic symbol index.
8397 template<int size
, bool big_endian
>
8399 Target_mips
<size
, big_endian
>::do_set_dynsym_indexes(
8400 std::vector
<Symbol
*>* dyn_symbols
, unsigned int index
,
8401 std::vector
<Symbol
*>* syms
, Stringpool
* dynpool
,
8402 Versions
* versions
, Symbol_table
* symtab
) const
8404 std::vector
<Symbol
*> non_got_symbols
;
8405 std::vector
<Symbol
*> got_symbols
;
8407 reorder_dyn_symbols
<size
, big_endian
>(dyn_symbols
, &non_got_symbols
,
8410 for (std::vector
<Symbol
*>::iterator p
= non_got_symbols
.begin();
8411 p
!= non_got_symbols
.end();
8416 // Note that SYM may already have a dynamic symbol index, since
8417 // some symbols appear more than once in the symbol table, with
8418 // and without a version.
8420 if (!sym
->has_dynsym_index())
8422 sym
->set_dynsym_index(index
);
8424 syms
->push_back(sym
);
8425 dynpool
->add(sym
->name(), false, NULL
);
8427 // Record any version information.
8428 if (sym
->version() != NULL
)
8429 versions
->record_version(symtab
, dynpool
, sym
);
8431 // If the symbol is defined in a dynamic object and is
8432 // referenced in a regular object, then mark the dynamic
8433 // object as needed. This is used to implement --as-needed.
8434 if (sym
->is_from_dynobj() && sym
->in_reg())
8435 sym
->object()->set_is_needed();
8439 for (std::vector
<Symbol
*>::iterator p
= got_symbols
.begin();
8440 p
!= got_symbols
.end();
8444 if (!sym
->has_dynsym_index())
8446 // Record any version information.
8447 if (sym
->version() != NULL
)
8448 versions
->record_version(symtab
, dynpool
, sym
);
8452 index
= versions
->finalize(symtab
, index
, syms
);
8454 int got_sym_count
= 0;
8455 for (std::vector
<Symbol
*>::iterator p
= got_symbols
.begin();
8456 p
!= got_symbols
.end();
8461 if (!sym
->has_dynsym_index())
8464 sym
->set_dynsym_index(index
);
8466 syms
->push_back(sym
);
8467 dynpool
->add(sym
->name(), false, NULL
);
8469 // If the symbol is defined in a dynamic object and is
8470 // referenced in a regular object, then mark the dynamic
8471 // object as needed. This is used to implement --as-needed.
8472 if (sym
->is_from_dynobj() && sym
->in_reg())
8473 sym
->object()->set_is_needed();
8477 // Set index of the first symbol that has .got entry.
8478 this->got_
->set_first_global_got_dynsym_index(
8479 got_sym_count
> 0 ? index
- got_sym_count
: -1U);
8481 if (this->mips_stubs_
!= NULL
)
8482 this->mips_stubs_
->set_dynsym_count(index
);
8487 // Create a PLT entry for a global symbol referenced by r_type relocation.
8489 template<int size
, bool big_endian
>
8491 Target_mips
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
8493 Mips_symbol
<size
>* gsym
,
8494 unsigned int r_type
)
8496 if (gsym
->has_lazy_stub() || gsym
->has_plt_offset())
8499 if (this->plt_
== NULL
)
8501 // Create the GOT section first.
8502 this->got_section(symtab
, layout
);
8504 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
8505 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
8506 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
),
8507 this->got_plt_
, ORDER_DATA
, false);
8509 // The first two entries are reserved.
8510 this->got_plt_
->set_current_data_size(2 * size
/8);
8512 this->plt_
= new Mips_output_data_plt
<size
, big_endian
>(layout
,
8515 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
8517 | elfcpp::SHF_EXECINSTR
),
8518 this->plt_
, ORDER_PLT
, false);
8521 this->plt_
->add_entry(gsym
, r_type
);
8525 // Get the .MIPS.stubs section, creating it if necessary.
8527 template<int size
, bool big_endian
>
8528 Mips_output_data_mips_stubs
<size
, big_endian
>*
8529 Target_mips
<size
, big_endian
>::mips_stubs_section(Layout
* layout
)
8531 if (this->mips_stubs_
== NULL
)
8534 new Mips_output_data_mips_stubs
<size
, big_endian
>(this);
8535 layout
->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS
,
8537 | elfcpp::SHF_EXECINSTR
),
8538 this->mips_stubs_
, ORDER_PLT
, false);
8540 return this->mips_stubs_
;
8543 // Get the LA25 stub section, creating it if necessary.
8545 template<int size
, bool big_endian
>
8546 Mips_output_data_la25_stub
<size
, big_endian
>*
8547 Target_mips
<size
, big_endian
>::la25_stub_section(Layout
* layout
)
8549 if (this->la25_stub_
== NULL
)
8551 this->la25_stub_
= new Mips_output_data_la25_stub
<size
, big_endian
>();
8552 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
8554 | elfcpp::SHF_EXECINSTR
),
8555 this->la25_stub_
, ORDER_TEXT
, false);
8557 return this->la25_stub_
;
8560 // Process the relocations to determine unreferenced sections for
8561 // garbage collection.
8563 template<int size
, bool big_endian
>
8565 Target_mips
<size
, big_endian
>::gc_process_relocs(
8566 Symbol_table
* symtab
,
8568 Sized_relobj_file
<size
, big_endian
>* object
,
8569 unsigned int data_shndx
,
8570 unsigned int sh_type
,
8571 const unsigned char* prelocs
,
8573 Output_section
* output_section
,
8574 bool needs_special_offset_handling
,
8575 size_t local_symbol_count
,
8576 const unsigned char* plocal_symbols
)
8578 typedef Target_mips
<size
, big_endian
> Mips
;
8580 if (sh_type
== elfcpp::SHT_REL
)
8582 typedef Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>
8585 gold::gc_process_relocs
<size
, big_endian
, Mips
, Scan
, Classify_reloc
>(
8594 needs_special_offset_handling
,
8598 else if (sh_type
== elfcpp::SHT_RELA
)
8600 typedef Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8603 gold::gc_process_relocs
<size
, big_endian
, Mips
, Scan
, Classify_reloc
>(
8612 needs_special_offset_handling
,
8620 // Scan relocations for a section.
8622 template<int size
, bool big_endian
>
8624 Target_mips
<size
, big_endian
>::scan_relocs(
8625 Symbol_table
* symtab
,
8627 Sized_relobj_file
<size
, big_endian
>* object
,
8628 unsigned int data_shndx
,
8629 unsigned int sh_type
,
8630 const unsigned char* prelocs
,
8632 Output_section
* output_section
,
8633 bool needs_special_offset_handling
,
8634 size_t local_symbol_count
,
8635 const unsigned char* plocal_symbols
)
8637 typedef Target_mips
<size
, big_endian
> Mips
;
8639 if (sh_type
== elfcpp::SHT_REL
)
8641 typedef Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>
8644 gold::scan_relocs
<size
, big_endian
, Mips
, Scan
, Classify_reloc
>(
8653 needs_special_offset_handling
,
8657 else if (sh_type
== elfcpp::SHT_RELA
)
8659 typedef Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8662 gold::scan_relocs
<size
, big_endian
, Mips
, Scan
, Classify_reloc
>(
8671 needs_special_offset_handling
,
8677 template<int size
, bool big_endian
>
8679 Target_mips
<size
, big_endian
>::mips_32bit_flags(elfcpp::Elf_Word flags
)
8681 return ((flags
& elfcpp::EF_MIPS_32BITMODE
) != 0
8682 || (flags
& elfcpp::EF_MIPS_ABI
) == elfcpp::E_MIPS_ABI_O32
8683 || (flags
& elfcpp::EF_MIPS_ABI
) == elfcpp::E_MIPS_ABI_EABI32
8684 || (flags
& elfcpp::EF_MIPS_ARCH
) == elfcpp::E_MIPS_ARCH_1
8685 || (flags
& elfcpp::EF_MIPS_ARCH
) == elfcpp::E_MIPS_ARCH_2
8686 || (flags
& elfcpp::EF_MIPS_ARCH
) == elfcpp::E_MIPS_ARCH_32
8687 || (flags
& elfcpp::EF_MIPS_ARCH
) == elfcpp::E_MIPS_ARCH_32R2
8688 || (flags
& elfcpp::EF_MIPS_ARCH
) == elfcpp::E_MIPS_ARCH_32R6
);
8691 // Return the MACH for a MIPS e_flags value.
8692 template<int size
, bool big_endian
>
8694 Target_mips
<size
, big_endian
>::elf_mips_mach(elfcpp::Elf_Word flags
)
8696 switch (flags
& elfcpp::EF_MIPS_MACH
)
8698 case elfcpp::E_MIPS_MACH_3900
:
8699 return mach_mips3900
;
8701 case elfcpp::E_MIPS_MACH_4010
:
8702 return mach_mips4010
;
8704 case elfcpp::E_MIPS_MACH_4100
:
8705 return mach_mips4100
;
8707 case elfcpp::E_MIPS_MACH_4111
:
8708 return mach_mips4111
;
8710 case elfcpp::E_MIPS_MACH_4120
:
8711 return mach_mips4120
;
8713 case elfcpp::E_MIPS_MACH_4650
:
8714 return mach_mips4650
;
8716 case elfcpp::E_MIPS_MACH_5400
:
8717 return mach_mips5400
;
8719 case elfcpp::E_MIPS_MACH_5500
:
8720 return mach_mips5500
;
8722 case elfcpp::E_MIPS_MACH_5900
:
8723 return mach_mips5900
;
8725 case elfcpp::E_MIPS_MACH_9000
:
8726 return mach_mips9000
;
8728 case elfcpp::E_MIPS_MACH_SB1
:
8729 return mach_mips_sb1
;
8731 case elfcpp::E_MIPS_MACH_LS2E
:
8732 return mach_mips_loongson_2e
;
8734 case elfcpp::E_MIPS_MACH_LS2F
:
8735 return mach_mips_loongson_2f
;
8737 case elfcpp::E_MIPS_MACH_LS3A
:
8738 return mach_mips_loongson_3a
;
8740 case elfcpp::E_MIPS_MACH_OCTEON3
:
8741 return mach_mips_octeon3
;
8743 case elfcpp::E_MIPS_MACH_OCTEON2
:
8744 return mach_mips_octeon2
;
8746 case elfcpp::E_MIPS_MACH_OCTEON
:
8747 return mach_mips_octeon
;
8749 case elfcpp::E_MIPS_MACH_XLR
:
8750 return mach_mips_xlr
;
8753 switch (flags
& elfcpp::EF_MIPS_ARCH
)
8756 case elfcpp::E_MIPS_ARCH_1
:
8757 return mach_mips3000
;
8759 case elfcpp::E_MIPS_ARCH_2
:
8760 return mach_mips6000
;
8762 case elfcpp::E_MIPS_ARCH_3
:
8763 return mach_mips4000
;
8765 case elfcpp::E_MIPS_ARCH_4
:
8766 return mach_mips8000
;
8768 case elfcpp::E_MIPS_ARCH_5
:
8771 case elfcpp::E_MIPS_ARCH_32
:
8772 return mach_mipsisa32
;
8774 case elfcpp::E_MIPS_ARCH_64
:
8775 return mach_mipsisa64
;
8777 case elfcpp::E_MIPS_ARCH_32R2
:
8778 return mach_mipsisa32r2
;
8780 case elfcpp::E_MIPS_ARCH_32R6
:
8781 return mach_mipsisa32r6
;
8783 case elfcpp::E_MIPS_ARCH_64R2
:
8784 return mach_mipsisa64r2
;
8786 case elfcpp::E_MIPS_ARCH_64R6
:
8787 return mach_mipsisa64r6
;
8794 // Return the MACH for each .MIPS.abiflags ISA Extension.
8796 template<int size
, bool big_endian
>
8798 Target_mips
<size
, big_endian
>::mips_isa_ext_mach(unsigned int isa_ext
)
8802 case elfcpp::AFL_EXT_3900
:
8803 return mach_mips3900
;
8805 case elfcpp::AFL_EXT_4010
:
8806 return mach_mips4010
;
8808 case elfcpp::AFL_EXT_4100
:
8809 return mach_mips4100
;
8811 case elfcpp::AFL_EXT_4111
:
8812 return mach_mips4111
;
8814 case elfcpp::AFL_EXT_4120
:
8815 return mach_mips4120
;
8817 case elfcpp::AFL_EXT_4650
:
8818 return mach_mips4650
;
8820 case elfcpp::AFL_EXT_5400
:
8821 return mach_mips5400
;
8823 case elfcpp::AFL_EXT_5500
:
8824 return mach_mips5500
;
8826 case elfcpp::AFL_EXT_5900
:
8827 return mach_mips5900
;
8829 case elfcpp::AFL_EXT_10000
:
8830 return mach_mips10000
;
8832 case elfcpp::AFL_EXT_LOONGSON_2E
:
8833 return mach_mips_loongson_2e
;
8835 case elfcpp::AFL_EXT_LOONGSON_2F
:
8836 return mach_mips_loongson_2f
;
8838 case elfcpp::AFL_EXT_LOONGSON_3A
:
8839 return mach_mips_loongson_3a
;
8841 case elfcpp::AFL_EXT_SB1
:
8842 return mach_mips_sb1
;
8844 case elfcpp::AFL_EXT_OCTEON
:
8845 return mach_mips_octeon
;
8847 case elfcpp::AFL_EXT_OCTEONP
:
8848 return mach_mips_octeonp
;
8850 case elfcpp::AFL_EXT_OCTEON2
:
8851 return mach_mips_octeon2
;
8853 case elfcpp::AFL_EXT_XLR
:
8854 return mach_mips_xlr
;
8857 return mach_mips3000
;
8861 // Return the .MIPS.abiflags value representing each ISA Extension.
8863 template<int size
, bool big_endian
>
8865 Target_mips
<size
, big_endian
>::mips_isa_ext(unsigned int mips_mach
)
8870 return elfcpp::AFL_EXT_3900
;
8873 return elfcpp::AFL_EXT_4010
;
8876 return elfcpp::AFL_EXT_4100
;
8879 return elfcpp::AFL_EXT_4111
;
8882 return elfcpp::AFL_EXT_4120
;
8885 return elfcpp::AFL_EXT_4650
;
8888 return elfcpp::AFL_EXT_5400
;
8891 return elfcpp::AFL_EXT_5500
;
8894 return elfcpp::AFL_EXT_5900
;
8896 case mach_mips10000
:
8897 return elfcpp::AFL_EXT_10000
;
8899 case mach_mips_loongson_2e
:
8900 return elfcpp::AFL_EXT_LOONGSON_2E
;
8902 case mach_mips_loongson_2f
:
8903 return elfcpp::AFL_EXT_LOONGSON_2F
;
8905 case mach_mips_loongson_3a
:
8906 return elfcpp::AFL_EXT_LOONGSON_3A
;
8909 return elfcpp::AFL_EXT_SB1
;
8911 case mach_mips_octeon
:
8912 return elfcpp::AFL_EXT_OCTEON
;
8914 case mach_mips_octeonp
:
8915 return elfcpp::AFL_EXT_OCTEONP
;
8917 case mach_mips_octeon3
:
8918 return elfcpp::AFL_EXT_OCTEON3
;
8920 case mach_mips_octeon2
:
8921 return elfcpp::AFL_EXT_OCTEON2
;
8924 return elfcpp::AFL_EXT_XLR
;
8931 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
8933 template<int size
, bool big_endian
>
8935 Target_mips
<size
, big_endian
>::update_abiflags_isa(const std::string
& name
,
8936 elfcpp::Elf_Word e_flags
, Mips_abiflags
<big_endian
>* abiflags
)
8939 switch (e_flags
& elfcpp::EF_MIPS_ARCH
)
8941 case elfcpp::E_MIPS_ARCH_1
:
8942 new_isa
= this->level_rev(1, 0);
8944 case elfcpp::E_MIPS_ARCH_2
:
8945 new_isa
= this->level_rev(2, 0);
8947 case elfcpp::E_MIPS_ARCH_3
:
8948 new_isa
= this->level_rev(3, 0);
8950 case elfcpp::E_MIPS_ARCH_4
:
8951 new_isa
= this->level_rev(4, 0);
8953 case elfcpp::E_MIPS_ARCH_5
:
8954 new_isa
= this->level_rev(5, 0);
8956 case elfcpp::E_MIPS_ARCH_32
:
8957 new_isa
= this->level_rev(32, 1);
8959 case elfcpp::E_MIPS_ARCH_32R2
:
8960 new_isa
= this->level_rev(32, 2);
8962 case elfcpp::E_MIPS_ARCH_32R6
:
8963 new_isa
= this->level_rev(32, 6);
8965 case elfcpp::E_MIPS_ARCH_64
:
8966 new_isa
= this->level_rev(64, 1);
8968 case elfcpp::E_MIPS_ARCH_64R2
:
8969 new_isa
= this->level_rev(64, 2);
8971 case elfcpp::E_MIPS_ARCH_64R6
:
8972 new_isa
= this->level_rev(64, 6);
8975 gold_error(_("%s: Unknown architecture %s"), name
.c_str(),
8976 this->elf_mips_mach_name(e_flags
));
8979 if (new_isa
> this->level_rev(abiflags
->isa_level
, abiflags
->isa_rev
))
8981 // Decode a single value into level and revision.
8982 abiflags
->isa_level
= new_isa
>> 3;
8983 abiflags
->isa_rev
= new_isa
& 0x7;
8986 // Update the isa_ext if needed.
8987 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags
->isa_ext
),
8988 this->elf_mips_mach(e_flags
)))
8989 abiflags
->isa_ext
= this->mips_isa_ext(this->elf_mips_mach(e_flags
));
8992 // Infer the content of the ABI flags based on the elf header.
8994 template<int size
, bool big_endian
>
8996 Target_mips
<size
, big_endian
>::infer_abiflags(
8997 Mips_relobj
<size
, big_endian
>* relobj
, Mips_abiflags
<big_endian
>* abiflags
)
8999 const Attributes_section_data
* pasd
= relobj
->attributes_section_data();
9000 int attr_fp_abi
= elfcpp::Val_GNU_MIPS_ABI_FP_ANY
;
9001 elfcpp::Elf_Word e_flags
= relobj
->processor_specific_flags();
9003 this->update_abiflags_isa(relobj
->name(), e_flags
, abiflags
);
9006 // Read fp_abi from the .gnu.attribute section.
9007 const Object_attribute
* attr
=
9008 pasd
->known_attributes(Object_attribute::OBJ_ATTR_GNU
);
9009 attr_fp_abi
= attr
[elfcpp::Tag_GNU_MIPS_ABI_FP
].int_value();
9012 abiflags
->fp_abi
= attr_fp_abi
;
9013 abiflags
->cpr1_size
= elfcpp::AFL_REG_NONE
;
9014 abiflags
->cpr2_size
= elfcpp::AFL_REG_NONE
;
9015 abiflags
->gpr_size
= this->mips_32bit_flags(e_flags
) ? elfcpp::AFL_REG_32
9016 : elfcpp::AFL_REG_64
;
9018 if (abiflags
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9019 || abiflags
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_XX
9020 || (abiflags
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9021 && abiflags
->gpr_size
== elfcpp::AFL_REG_32
))
9022 abiflags
->cpr1_size
= elfcpp::AFL_REG_32
;
9023 else if (abiflags
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9024 || abiflags
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_64
9025 || abiflags
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_64A
)
9026 abiflags
->cpr1_size
= elfcpp::AFL_REG_64
;
9028 if (e_flags
& elfcpp::EF_MIPS_ARCH_ASE_MDMX
)
9029 abiflags
->ases
|= elfcpp::AFL_ASE_MDMX
;
9030 if (e_flags
& elfcpp::EF_MIPS_ARCH_ASE_M16
)
9031 abiflags
->ases
|= elfcpp::AFL_ASE_MIPS16
;
9032 if (e_flags
& elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS
)
9033 abiflags
->ases
|= elfcpp::AFL_ASE_MICROMIPS
;
9035 if (abiflags
->fp_abi
!= elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9036 && abiflags
->fp_abi
!= elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9037 && abiflags
->fp_abi
!= elfcpp::Val_GNU_MIPS_ABI_FP_64A
9038 && abiflags
->isa_level
>= 32
9039 && abiflags
->isa_ext
!= elfcpp::AFL_EXT_LOONGSON_3A
)
9040 abiflags
->flags1
|= elfcpp::AFL_FLAGS1_ODDSPREG
;
9043 // Create abiflags from elf header or from .MIPS.abiflags section.
9045 template<int size
, bool big_endian
>
9047 Target_mips
<size
, big_endian
>::create_abiflags(
9048 Mips_relobj
<size
, big_endian
>* relobj
,
9049 Mips_abiflags
<big_endian
>* abiflags
)
9051 Mips_abiflags
<big_endian
>* sec_abiflags
= relobj
->abiflags();
9052 Mips_abiflags
<big_endian
> header_abiflags
;
9054 this->infer_abiflags(relobj
, &header_abiflags
);
9056 if (sec_abiflags
== NULL
)
9058 // If there is no input .MIPS.abiflags section, use abiflags created
9060 *abiflags
= header_abiflags
;
9064 this->has_abiflags_section_
= true;
9066 // It is not possible to infer the correct ISA revision for R3 or R5
9067 // so drop down to R2 for the checks.
9068 unsigned char isa_rev
= sec_abiflags
->isa_rev
;
9069 if (isa_rev
== 3 || isa_rev
== 5)
9072 // Check compatibility between abiflags created from elf header
9073 // and abiflags from .MIPS.abiflags section in this object file.
9074 if (this->level_rev(sec_abiflags
->isa_level
, isa_rev
)
9075 < this->level_rev(header_abiflags
.isa_level
, header_abiflags
.isa_rev
))
9076 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9077 relobj
->name().c_str());
9078 if (header_abiflags
.fp_abi
!= elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9079 && sec_abiflags
->fp_abi
!= header_abiflags
.fp_abi
)
9080 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9081 ".MIPS.abiflags"), relobj
->name().c_str());
9082 if ((sec_abiflags
->ases
& header_abiflags
.ases
) != header_abiflags
.ases
)
9083 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9084 relobj
->name().c_str());
9085 // The isa_ext is allowed to be an extension of what can be inferred
9087 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags
.isa_ext
),
9088 this->mips_isa_ext_mach(sec_abiflags
->isa_ext
)))
9089 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9090 ".MIPS.abiflags"), relobj
->name().c_str());
9091 if (sec_abiflags
->flags2
!= 0)
9092 gold_warning(_("%s: Unexpected flag in the flags2 field of "
9093 ".MIPS.abiflags (0x%x)"), relobj
->name().c_str(),
9094 sec_abiflags
->flags2
);
9095 // Use abiflags from .MIPS.abiflags section.
9096 *abiflags
= *sec_abiflags
;
9099 // Return the meaning of fp_abi, or "unknown" if not known.
9101 template<int size
, bool big_endian
>
9103 Target_mips
<size
, big_endian
>::fp_abi_string(int fp
)
9107 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
:
9108 return "-mdouble-float";
9109 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
:
9110 return "-msingle-float";
9111 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
:
9112 return "-msoft-float";
9113 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64
:
9114 return _("-mips32r2 -mfp64 (12 callee-saved)");
9115 case elfcpp::Val_GNU_MIPS_ABI_FP_XX
:
9117 case elfcpp::Val_GNU_MIPS_ABI_FP_64
:
9118 return "-mgp32 -mfp64";
9119 case elfcpp::Val_GNU_MIPS_ABI_FP_64A
:
9120 return "-mgp32 -mfp64 -mno-odd-spreg";
9128 template<int size
, bool big_endian
>
9130 Target_mips
<size
, big_endian
>::select_fp_abi(const std::string
& name
, int in_fp
,
9133 if (in_fp
== out_fp
)
9136 if (out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_ANY
)
9138 else if (out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_XX
9139 && (in_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9140 || in_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64
9141 || in_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64A
))
9143 else if (in_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_XX
9144 && (out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9145 || out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64
9146 || out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64A
))
9147 return out_fp
; // Keep the current setting.
9148 else if (out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64A
9149 && in_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64
)
9151 else if (in_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64A
9152 && out_fp
== elfcpp::Val_GNU_MIPS_ABI_FP_64
)
9153 return out_fp
; // Keep the current setting.
9154 else if (in_fp
!= elfcpp::Val_GNU_MIPS_ABI_FP_ANY
)
9155 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name
.c_str(),
9156 fp_abi_string(in_fp
), fp_abi_string(out_fp
));
9160 // Merge attributes from input object.
9162 template<int size
, bool big_endian
>
9164 Target_mips
<size
, big_endian
>::merge_obj_attributes(const std::string
& name
,
9165 const Attributes_section_data
* pasd
)
9167 // Return if there is no attributes section data.
9171 // If output has no object attributes, just copy.
9172 if (this->attributes_section_data_
== NULL
)
9174 this->attributes_section_data_
= new Attributes_section_data(*pasd
);
9178 Object_attribute
* out_attr
= this->attributes_section_data_
->known_attributes(
9179 Object_attribute::OBJ_ATTR_GNU
);
9181 out_attr
[elfcpp::Tag_GNU_MIPS_ABI_FP
].set_type(1);
9182 out_attr
[elfcpp::Tag_GNU_MIPS_ABI_FP
].set_int_value(this->abiflags_
->fp_abi
);
9184 // Merge Tag_compatibility attributes and any common GNU ones.
9185 this->attributes_section_data_
->merge(name
.c_str(), pasd
);
9188 // Merge abiflags from input object.
9190 template<int size
, bool big_endian
>
9192 Target_mips
<size
, big_endian
>::merge_obj_abiflags(const std::string
& name
,
9193 Mips_abiflags
<big_endian
>* in_abiflags
)
9195 // If output has no abiflags, just copy.
9196 if (this->abiflags_
== NULL
)
9198 this->abiflags_
= new Mips_abiflags
<big_endian
>(*in_abiflags
);
9202 this->abiflags_
->fp_abi
= this->select_fp_abi(name
, in_abiflags
->fp_abi
,
9203 this->abiflags_
->fp_abi
);
9206 this->abiflags_
->isa_level
= std::max(this->abiflags_
->isa_level
,
9207 in_abiflags
->isa_level
);
9208 this->abiflags_
->isa_rev
= std::max(this->abiflags_
->isa_rev
,
9209 in_abiflags
->isa_rev
);
9210 this->abiflags_
->gpr_size
= std::max(this->abiflags_
->gpr_size
,
9211 in_abiflags
->gpr_size
);
9212 this->abiflags_
->cpr1_size
= std::max(this->abiflags_
->cpr1_size
,
9213 in_abiflags
->cpr1_size
);
9214 this->abiflags_
->cpr2_size
= std::max(this->abiflags_
->cpr2_size
,
9215 in_abiflags
->cpr2_size
);
9216 this->abiflags_
->ases
|= in_abiflags
->ases
;
9217 this->abiflags_
->flags1
|= in_abiflags
->flags1
;
9220 // Check whether machine EXTENSION is an extension of machine BASE.
9221 template<int size
, bool big_endian
>
9223 Target_mips
<size
, big_endian
>::mips_mach_extends(unsigned int base
,
9224 unsigned int extension
)
9226 if (extension
== base
)
9229 if ((base
== mach_mipsisa32
)
9230 && this->mips_mach_extends(mach_mipsisa64
, extension
))
9233 if ((base
== mach_mipsisa32r2
)
9234 && this->mips_mach_extends(mach_mipsisa64r2
, extension
))
9237 for (unsigned int i
= 0; i
< this->mips_mach_extensions_
.size(); ++i
)
9238 if (extension
== this->mips_mach_extensions_
[i
].first
)
9240 extension
= this->mips_mach_extensions_
[i
].second
;
9241 if (extension
== base
)
9248 // Merge file header flags from input object.
9250 template<int size
, bool big_endian
>
9252 Target_mips
<size
, big_endian
>::merge_obj_e_flags(const std::string
& name
,
9253 elfcpp::Elf_Word in_flags
)
9255 // If flags are not set yet, just copy them.
9256 if (!this->are_processor_specific_flags_set())
9258 this->set_processor_specific_flags(in_flags
);
9259 this->mach_
= this->elf_mips_mach(in_flags
);
9263 elfcpp::Elf_Word new_flags
= in_flags
;
9264 elfcpp::Elf_Word old_flags
= this->processor_specific_flags();
9265 elfcpp::Elf_Word merged_flags
= this->processor_specific_flags();
9266 merged_flags
|= new_flags
& elfcpp::EF_MIPS_NOREORDER
;
9268 // Check flag compatibility.
9269 new_flags
&= ~elfcpp::EF_MIPS_NOREORDER
;
9270 old_flags
&= ~elfcpp::EF_MIPS_NOREORDER
;
9272 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9273 // doesn't seem to matter.
9274 new_flags
&= ~elfcpp::EF_MIPS_XGOT
;
9275 old_flags
&= ~elfcpp::EF_MIPS_XGOT
;
9277 // MIPSpro generates ucode info in n64 objects. Again, we should
9278 // just be able to ignore this.
9279 new_flags
&= ~elfcpp::EF_MIPS_UCODE
;
9280 old_flags
&= ~elfcpp::EF_MIPS_UCODE
;
9282 if (new_flags
== old_flags
)
9284 this->set_processor_specific_flags(merged_flags
);
9288 if (((new_flags
& (elfcpp::EF_MIPS_PIC
| elfcpp::EF_MIPS_CPIC
)) != 0)
9289 != ((old_flags
& (elfcpp::EF_MIPS_PIC
| elfcpp::EF_MIPS_CPIC
)) != 0))
9290 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9293 if (new_flags
& (elfcpp::EF_MIPS_PIC
| elfcpp::EF_MIPS_CPIC
))
9294 merged_flags
|= elfcpp::EF_MIPS_CPIC
;
9295 if (!(new_flags
& elfcpp::EF_MIPS_PIC
))
9296 merged_flags
&= ~elfcpp::EF_MIPS_PIC
;
9298 new_flags
&= ~(elfcpp::EF_MIPS_PIC
| elfcpp::EF_MIPS_CPIC
);
9299 old_flags
&= ~(elfcpp::EF_MIPS_PIC
| elfcpp::EF_MIPS_CPIC
);
9301 // Compare the ISAs.
9302 if (mips_32bit_flags(old_flags
) != mips_32bit_flags(new_flags
))
9303 gold_error(_("%s: linking 32-bit code with 64-bit code"), name
.c_str());
9304 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags
), this->mach_
))
9306 // Output ISA isn't the same as, or an extension of, input ISA.
9307 if (this->mips_mach_extends(this->mach_
, this->elf_mips_mach(in_flags
)))
9309 // Copy the architecture info from input object to output. Also copy
9310 // the 32-bit flag (if set) so that we continue to recognise
9311 // output as a 32-bit binary.
9312 this->mach_
= this->elf_mips_mach(in_flags
);
9313 merged_flags
&= ~(elfcpp::EF_MIPS_ARCH
| elfcpp::EF_MIPS_MACH
);
9314 merged_flags
|= (new_flags
& (elfcpp::EF_MIPS_ARCH
9315 | elfcpp::EF_MIPS_MACH
| elfcpp::EF_MIPS_32BITMODE
));
9317 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9318 this->update_abiflags_isa(name
, merged_flags
, this->abiflags_
);
9320 // Copy across the ABI flags if output doesn't use them
9321 // and if that was what caused us to treat input object as 32-bit.
9322 if ((old_flags
& elfcpp::EF_MIPS_ABI
) == 0
9323 && this->mips_32bit_flags(new_flags
)
9324 && !this->mips_32bit_flags(new_flags
& ~elfcpp::EF_MIPS_ABI
))
9325 merged_flags
|= new_flags
& elfcpp::EF_MIPS_ABI
;
9328 // The ISAs aren't compatible.
9329 gold_error(_("%s: linking %s module with previous %s modules"),
9330 name
.c_str(), this->elf_mips_mach_name(in_flags
),
9331 this->elf_mips_mach_name(merged_flags
));
9334 new_flags
&= (~(elfcpp::EF_MIPS_ARCH
| elfcpp::EF_MIPS_MACH
9335 | elfcpp::EF_MIPS_32BITMODE
));
9336 old_flags
&= (~(elfcpp::EF_MIPS_ARCH
| elfcpp::EF_MIPS_MACH
9337 | elfcpp::EF_MIPS_32BITMODE
));
9340 if ((new_flags
& elfcpp::EF_MIPS_ABI
) != (old_flags
& elfcpp::EF_MIPS_ABI
))
9342 // Only error if both are set (to different values).
9343 if ((new_flags
& elfcpp::EF_MIPS_ABI
)
9344 && (old_flags
& elfcpp::EF_MIPS_ABI
))
9345 gold_error(_("%s: ABI mismatch: linking %s module with "
9346 "previous %s modules"), name
.c_str(),
9347 this->elf_mips_abi_name(in_flags
),
9348 this->elf_mips_abi_name(merged_flags
));
9350 new_flags
&= ~elfcpp::EF_MIPS_ABI
;
9351 old_flags
&= ~elfcpp::EF_MIPS_ABI
;
9354 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9355 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9356 if ((new_flags
& elfcpp::EF_MIPS_ARCH_ASE
)
9357 != (old_flags
& elfcpp::EF_MIPS_ARCH_ASE
))
9359 int old_micro
= old_flags
& elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS
;
9360 int new_micro
= new_flags
& elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS
;
9361 int old_m16
= old_flags
& elfcpp::EF_MIPS_ARCH_ASE_M16
;
9362 int new_m16
= new_flags
& elfcpp::EF_MIPS_ARCH_ASE_M16
;
9363 int micro_mis
= old_m16
&& new_micro
;
9364 int m16_mis
= old_micro
&& new_m16
;
9366 if (m16_mis
|| micro_mis
)
9367 gold_error(_("%s: ASE mismatch: linking %s module with "
9368 "previous %s modules"), name
.c_str(),
9369 m16_mis
? "MIPS16" : "microMIPS",
9370 m16_mis
? "microMIPS" : "MIPS16");
9372 merged_flags
|= new_flags
& elfcpp::EF_MIPS_ARCH_ASE
;
9374 new_flags
&= ~ elfcpp::EF_MIPS_ARCH_ASE
;
9375 old_flags
&= ~ elfcpp::EF_MIPS_ARCH_ASE
;
9378 // Compare NaN encodings.
9379 if ((new_flags
& elfcpp::EF_MIPS_NAN2008
) != (old_flags
& elfcpp::EF_MIPS_NAN2008
))
9381 gold_error(_("%s: linking %s module with previous %s modules"),
9383 (new_flags
& elfcpp::EF_MIPS_NAN2008
9384 ? "-mnan=2008" : "-mnan=legacy"),
9385 (old_flags
& elfcpp::EF_MIPS_NAN2008
9386 ? "-mnan=2008" : "-mnan=legacy"));
9388 new_flags
&= ~elfcpp::EF_MIPS_NAN2008
;
9389 old_flags
&= ~elfcpp::EF_MIPS_NAN2008
;
9392 // Compare FP64 state.
9393 if ((new_flags
& elfcpp::EF_MIPS_FP64
) != (old_flags
& elfcpp::EF_MIPS_FP64
))
9395 gold_error(_("%s: linking %s module with previous %s modules"),
9397 (new_flags
& elfcpp::EF_MIPS_FP64
9398 ? "-mfp64" : "-mfp32"),
9399 (old_flags
& elfcpp::EF_MIPS_FP64
9400 ? "-mfp64" : "-mfp32"));
9402 new_flags
&= ~elfcpp::EF_MIPS_FP64
;
9403 old_flags
&= ~elfcpp::EF_MIPS_FP64
;
9406 // Warn about any other mismatches.
9407 if (new_flags
!= old_flags
)
9408 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9409 "modules (0x%x)"), name
.c_str(), new_flags
, old_flags
);
9411 this->set_processor_specific_flags(merged_flags
);
9414 // Adjust ELF file header.
9416 template<int size
, bool big_endian
>
9418 Target_mips
<size
, big_endian
>::do_adjust_elf_header(
9419 unsigned char* view
,
9422 gold_assert(len
== elfcpp::Elf_sizes
<size
>::ehdr_size
);
9424 elfcpp::Ehdr
<size
, big_endian
> ehdr(view
);
9425 unsigned char e_ident
[elfcpp::EI_NIDENT
];
9426 elfcpp::Elf_Word flags
= this->processor_specific_flags();
9427 memcpy(e_ident
, ehdr
.get_e_ident(), elfcpp::EI_NIDENT
);
9429 unsigned char ei_abiversion
= 0;
9430 elfcpp::Elf_Half type
= ehdr
.get_e_type();
9431 if (type
== elfcpp::ET_EXEC
9432 && parameters
->options().copyreloc()
9433 && (flags
& (elfcpp::EF_MIPS_PIC
| elfcpp::EF_MIPS_CPIC
))
9434 == elfcpp::EF_MIPS_CPIC
)
9437 if (this->abiflags_
!= NULL
9438 && (this->abiflags_
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_64
9439 || this->abiflags_
->fp_abi
== elfcpp::Val_GNU_MIPS_ABI_FP_64A
))
9442 e_ident
[elfcpp::EI_ABIVERSION
] = ei_abiversion
;
9443 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
9444 oehdr
.put_e_ident(e_ident
);
9446 if (this->entry_symbol_is_compressed_
)
9447 oehdr
.put_e_entry(ehdr
.get_e_entry() + 1);
9450 // do_make_elf_object to override the same function in the base class.
9451 // We need to use a target-specific sub-class of
9452 // Sized_relobj_file<size, big_endian> to store Mips specific information.
9453 // Hence we need to have our own ELF object creation.
9455 template<int size
, bool big_endian
>
9457 Target_mips
<size
, big_endian
>::do_make_elf_object(
9458 const std::string
& name
,
9459 Input_file
* input_file
,
9460 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
9462 int et
= ehdr
.get_e_type();
9463 // ET_EXEC files are valid input for --just-symbols/-R,
9464 // and we treat them as relocatable objects.
9465 if (et
== elfcpp::ET_REL
9466 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
9468 Mips_relobj
<size
, big_endian
>* obj
=
9469 new Mips_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
9473 else if (et
== elfcpp::ET_DYN
)
9475 // TODO(sasa): Should we create Mips_dynobj?
9476 return Target::do_make_elf_object(name
, input_file
, offset
, ehdr
);
9480 gold_error(_("%s: unsupported ELF file type %d"),
9486 // Finalize the sections.
9488 template <int size
, bool big_endian
>
9490 Target_mips
<size
, big_endian
>::do_finalize_sections(Layout
* layout
,
9491 const Input_objects
* input_objects
,
9492 Symbol_table
* symtab
)
9494 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9495 // DT_FINI have correct values.
9496 Mips_symbol
<size
>* init
= static_cast<Mips_symbol
<size
>*>(
9497 symtab
->lookup(parameters
->options().init()));
9498 if (init
!= NULL
&& (init
->is_mips16() || init
->is_micromips()))
9499 init
->set_value(init
->value() | 1);
9500 Mips_symbol
<size
>* fini
= static_cast<Mips_symbol
<size
>*>(
9501 symtab
->lookup(parameters
->options().fini()));
9502 if (fini
!= NULL
&& (fini
->is_mips16() || fini
->is_micromips()))
9503 fini
->set_value(fini
->value() | 1);
9505 // Check whether the entry symbol is mips16 or micromips. This is needed to
9506 // adjust entry address in ELF header.
9507 Mips_symbol
<size
>* entry
=
9508 static_cast<Mips_symbol
<size
>*>(symtab
->lookup(this->entry_symbol_name()));
9509 this->entry_symbol_is_compressed_
= (entry
!= NULL
&& (entry
->is_mips16()
9510 || entry
->is_micromips()));
9512 if (!parameters
->doing_static_link()
9513 && (strcmp(parameters
->options().hash_style(), "gnu") == 0
9514 || strcmp(parameters
->options().hash_style(), "both") == 0))
9516 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9517 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9518 // MIPS ABI requires a mapping between the GOT and the symbol table.
9519 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9522 // Check whether the final section that was scanned has HI16 or GOT16
9523 // relocations without the corresponding LO16 part.
9524 if (this->got16_addends_
.size() > 0)
9525 gold_error("Can't find matching LO16 reloc");
9528 this->set_gp(layout
, symtab
);
9530 // Check for any mips16 stub sections that we can discard.
9531 if (!parameters
->options().relocatable())
9533 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
9534 p
!= input_objects
->relobj_end();
9537 Mips_relobj
<size
, big_endian
>* object
=
9538 Mips_relobj
<size
, big_endian
>::as_mips_relobj(*p
);
9539 object
->discard_mips16_stub_sections(symtab
);
9543 Valtype gprmask
= 0;
9544 Valtype cprmask1
= 0;
9545 Valtype cprmask2
= 0;
9546 Valtype cprmask3
= 0;
9547 Valtype cprmask4
= 0;
9548 bool has_reginfo_section
= false;
9550 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
9551 p
!= input_objects
->relobj_end();
9554 Mips_relobj
<size
, big_endian
>* relobj
=
9555 Mips_relobj
<size
, big_endian
>::as_mips_relobj(*p
);
9557 // Merge .reginfo contents of input objects.
9558 if (relobj
->has_reginfo_section())
9560 has_reginfo_section
= true;
9561 gprmask
|= relobj
->gprmask();
9562 cprmask1
|= relobj
->cprmask1();
9563 cprmask2
|= relobj
->cprmask2();
9564 cprmask3
|= relobj
->cprmask3();
9565 cprmask4
|= relobj
->cprmask4();
9568 Input_file::Format format
= relobj
->input_file()->format();
9569 if (format
!= Input_file::FORMAT_ELF
)
9572 // If all input sections will be discarded, don't use this object
9573 // file for merging processor specific flags.
9574 bool should_merge_processor_specific_flags
= false;
9576 for (unsigned int i
= 1; i
< relobj
->shnum(); ++i
)
9577 if (relobj
->output_section(i
) != NULL
)
9579 should_merge_processor_specific_flags
= true;
9583 if (!should_merge_processor_specific_flags
)
9586 // Merge processor specific flags.
9587 Mips_abiflags
<big_endian
> in_abiflags
;
9589 this->create_abiflags(relobj
, &in_abiflags
);
9590 this->merge_obj_e_flags(relobj
->name(),
9591 relobj
->processor_specific_flags());
9592 this->merge_obj_abiflags(relobj
->name(), &in_abiflags
);
9593 this->merge_obj_attributes(relobj
->name(),
9594 relobj
->attributes_section_data());
9597 // Create a .gnu.attributes section if we have merged any attributes
9599 if (this->attributes_section_data_
!= NULL
)
9601 Output_attributes_section_data
* attributes_section
=
9602 new Output_attributes_section_data(*this->attributes_section_data_
);
9603 layout
->add_output_section_data(".gnu.attributes",
9604 elfcpp::SHT_GNU_ATTRIBUTES
, 0,
9605 attributes_section
, ORDER_INVALID
, false);
9608 // Create .MIPS.abiflags output section if there is an input section.
9609 if (this->has_abiflags_section_
)
9611 Mips_output_section_abiflags
<size
, big_endian
>* abiflags_section
=
9612 new Mips_output_section_abiflags
<size
, big_endian
>(*this->abiflags_
);
9614 Output_section
* os
=
9615 layout
->add_output_section_data(".MIPS.abiflags",
9616 elfcpp::SHT_MIPS_ABIFLAGS
,
9618 abiflags_section
, ORDER_INVALID
, false);
9620 if (!parameters
->options().relocatable() && os
!= NULL
)
9622 Output_segment
* abiflags_segment
=
9623 layout
->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS
, elfcpp::PF_R
);
9624 abiflags_segment
->add_output_section_to_nonload(os
, elfcpp::PF_R
);
9628 if (has_reginfo_section
&& !parameters
->options().gc_sections())
9630 // Create .reginfo output section.
9631 Mips_output_section_reginfo
<size
, big_endian
>* reginfo_section
=
9632 new Mips_output_section_reginfo
<size
, big_endian
>(this, gprmask
,
9634 cprmask3
, cprmask4
);
9636 Output_section
* os
=
9637 layout
->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO
,
9638 elfcpp::SHF_ALLOC
, reginfo_section
,
9639 ORDER_INVALID
, false);
9641 if (!parameters
->options().relocatable() && os
!= NULL
)
9643 Output_segment
* reginfo_segment
=
9644 layout
->make_output_segment(elfcpp::PT_MIPS_REGINFO
,
9646 reginfo_segment
->add_output_section_to_nonload(os
, elfcpp::PF_R
);
9650 if (this->plt_
!= NULL
)
9652 // Set final PLT offsets for symbols.
9653 this->plt_section()->set_plt_offsets();
9655 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9656 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9657 // there are no standard PLT entries present.
9658 unsigned char nonvis
= 0;
9659 if (this->is_output_micromips()
9660 && !this->plt_section()->has_standard_entries())
9661 nonvis
= elfcpp::STO_MICROMIPS
>> 2;
9662 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
9663 Symbol_table::PREDEFINED
,
9665 0, 0, elfcpp::STT_FUNC
,
9667 elfcpp::STV_DEFAULT
, nonvis
,
9671 if (this->mips_stubs_
!= NULL
)
9673 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9674 unsigned char nonvis
= 0;
9675 if (this->is_output_micromips())
9676 nonvis
= elfcpp::STO_MICROMIPS
>> 2;
9677 symtab
->define_in_output_data("_MIPS_STUBS_", NULL
,
9678 Symbol_table::PREDEFINED
,
9680 0, 0, elfcpp::STT_FUNC
,
9682 elfcpp::STV_DEFAULT
, nonvis
,
9686 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
9687 // In case there is no .got section, create one.
9688 this->got_section(symtab
, layout
);
9690 // Emit any relocs we saved in an attempt to avoid generating COPY
9692 if (this->copy_relocs_
.any_saved_relocs())
9693 this->copy_relocs_
.emit_mips(this->rel_dyn_section(layout
), symtab
, layout
,
9696 // Emit dynamic relocs.
9697 for (typename
std::vector
<Dyn_reloc
>::iterator p
= this->dyn_relocs_
.begin();
9698 p
!= this->dyn_relocs_
.end();
9700 p
->emit(this->rel_dyn_section(layout
), this->got_section(), symtab
);
9702 if (this->has_got_section())
9703 this->got_section()->lay_out_got(layout
, symtab
, input_objects
);
9705 if (this->mips_stubs_
!= NULL
)
9706 this->mips_stubs_
->set_needs_dynsym_value();
9708 // Check for functions that might need $25 to be valid on entry.
9709 // TODO(sasa): Can we do this without iterating over all symbols?
9710 typedef Symbol_visitor_check_symbols
<size
, big_endian
> Symbol_visitor
;
9711 symtab
->for_all_symbols
<size
, Symbol_visitor
>(Symbol_visitor(this, layout
,
9714 // Add NULL segment.
9715 if (!parameters
->options().relocatable())
9716 layout
->make_output_segment(elfcpp::PT_NULL
, 0);
9718 // Fill in some more dynamic tags.
9719 // TODO(sasa): Add more dynamic tags.
9720 const Reloc_section
* rel_plt
= (this->plt_
== NULL
9721 ? NULL
: this->plt_
->rel_plt());
9722 layout
->add_target_dynamic_tags(true, this->got_
, rel_plt
,
9723 this->rel_dyn_
, true, false);
9725 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
9727 && !parameters
->options().relocatable()
9728 && !parameters
->doing_static_link())
9731 // This element holds a 32-bit version id for the Runtime
9732 // Linker Interface. This will start at integer value 1.
9734 odyn
->add_constant(elfcpp::DT_MIPS_RLD_VERSION
, d_val
);
9737 d_val
= elfcpp::RHF_NOTPOT
;
9738 odyn
->add_constant(elfcpp::DT_MIPS_FLAGS
, d_val
);
9740 // Save layout for using when emiting custom dynamic tags.
9741 this->layout_
= layout
;
9743 // This member holds the base address of the segment.
9744 odyn
->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS
);
9746 // This member holds the number of entries in the .dynsym section.
9747 odyn
->add_custom(elfcpp::DT_MIPS_SYMTABNO
);
9749 // This member holds the index of the first dynamic symbol
9750 // table entry that corresponds to an entry in the global offset table.
9751 odyn
->add_custom(elfcpp::DT_MIPS_GOTSYM
);
9753 // This member holds the number of local GOT entries.
9754 odyn
->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO
,
9755 this->got_
->get_local_gotno());
9757 if (this->plt_
!= NULL
)
9758 // DT_MIPS_PLTGOT dynamic tag
9759 odyn
->add_section_address(elfcpp::DT_MIPS_PLTGOT
, this->got_plt_
);
9763 // Get the custom dynamic tag value.
9764 template<int size
, bool big_endian
>
9766 Target_mips
<size
, big_endian
>::do_dynamic_tag_custom_value(elfcpp::DT tag
) const
9770 case elfcpp::DT_MIPS_BASE_ADDRESS
:
9772 // The base address of the segment.
9773 // At this point, the segment list has been sorted into final order,
9774 // so just return vaddr of the first readable PT_LOAD segment.
9775 Output_segment
* seg
=
9776 this->layout_
->find_output_segment(elfcpp::PT_LOAD
, elfcpp::PF_R
, 0);
9777 gold_assert(seg
!= NULL
);
9778 return seg
->vaddr();
9781 case elfcpp::DT_MIPS_SYMTABNO
:
9782 // The number of entries in the .dynsym section.
9783 return this->get_dt_mips_symtabno();
9785 case elfcpp::DT_MIPS_GOTSYM
:
9787 // The index of the first dynamic symbol table entry that corresponds
9788 // to an entry in the GOT.
9789 if (this->got_
->first_global_got_dynsym_index() != -1U)
9790 return this->got_
->first_global_got_dynsym_index();
9792 // In case if we don't have global GOT symbols we default to setting
9793 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9794 return this->get_dt_mips_symtabno();
9798 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag
);
9801 return (unsigned int)-1;
9804 // Relocate section data.
9806 template<int size
, bool big_endian
>
9808 Target_mips
<size
, big_endian
>::relocate_section(
9809 const Relocate_info
<size
, big_endian
>* relinfo
,
9810 unsigned int sh_type
,
9811 const unsigned char* prelocs
,
9813 Output_section
* output_section
,
9814 bool needs_special_offset_handling
,
9815 unsigned char* view
,
9816 Mips_address address
,
9817 section_size_type view_size
,
9818 const Reloc_symbol_changes
* reloc_symbol_changes
)
9820 typedef Target_mips
<size
, big_endian
> Mips
;
9821 typedef typename Target_mips
<size
, big_endian
>::Relocate Mips_relocate
;
9823 if (sh_type
== elfcpp::SHT_REL
)
9825 typedef Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>
9828 gold::relocate_section
<size
, big_endian
, Mips
, Mips_relocate
,
9829 gold::Default_comdat_behavior
, Classify_reloc
>(
9835 needs_special_offset_handling
,
9839 reloc_symbol_changes
);
9841 else if (sh_type
== elfcpp::SHT_RELA
)
9843 typedef Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9846 gold::relocate_section
<size
, big_endian
, Mips
, Mips_relocate
,
9847 gold::Default_comdat_behavior
, Classify_reloc
>(
9853 needs_special_offset_handling
,
9857 reloc_symbol_changes
);
9861 // Return the size of a relocation while scanning during a relocatable
9865 mips_get_size_for_reloc(unsigned int r_type
, Relobj
* object
)
9869 case elfcpp::R_MIPS_NONE
:
9870 case elfcpp::R_MIPS_TLS_DTPMOD64
:
9871 case elfcpp::R_MIPS_TLS_DTPREL64
:
9872 case elfcpp::R_MIPS_TLS_TPREL64
:
9875 case elfcpp::R_MIPS_32
:
9876 case elfcpp::R_MIPS_TLS_DTPMOD32
:
9877 case elfcpp::R_MIPS_TLS_DTPREL32
:
9878 case elfcpp::R_MIPS_TLS_TPREL32
:
9879 case elfcpp::R_MIPS_REL32
:
9880 case elfcpp::R_MIPS_PC32
:
9881 case elfcpp::R_MIPS_GPREL32
:
9882 case elfcpp::R_MIPS_JALR
:
9883 case elfcpp::R_MIPS_EH
:
9886 case elfcpp::R_MIPS_16
:
9887 case elfcpp::R_MIPS_HI16
:
9888 case elfcpp::R_MIPS_LO16
:
9889 case elfcpp::R_MIPS_GPREL16
:
9890 case elfcpp::R_MIPS16_HI16
:
9891 case elfcpp::R_MIPS16_LO16
:
9892 case elfcpp::R_MIPS_PC16
:
9893 case elfcpp::R_MIPS_PCHI16
:
9894 case elfcpp::R_MIPS_PCLO16
:
9895 case elfcpp::R_MIPS_GOT16
:
9896 case elfcpp::R_MIPS16_GOT16
:
9897 case elfcpp::R_MIPS_CALL16
:
9898 case elfcpp::R_MIPS16_CALL16
:
9899 case elfcpp::R_MIPS_GOT_HI16
:
9900 case elfcpp::R_MIPS_CALL_HI16
:
9901 case elfcpp::R_MIPS_GOT_LO16
:
9902 case elfcpp::R_MIPS_CALL_LO16
:
9903 case elfcpp::R_MIPS_TLS_DTPREL_HI16
:
9904 case elfcpp::R_MIPS_TLS_DTPREL_LO16
:
9905 case elfcpp::R_MIPS_TLS_TPREL_HI16
:
9906 case elfcpp::R_MIPS_TLS_TPREL_LO16
:
9907 case elfcpp::R_MIPS16_GPREL
:
9908 case elfcpp::R_MIPS_GOT_DISP
:
9909 case elfcpp::R_MIPS_LITERAL
:
9910 case elfcpp::R_MIPS_GOT_PAGE
:
9911 case elfcpp::R_MIPS_GOT_OFST
:
9912 case elfcpp::R_MIPS_TLS_GD
:
9913 case elfcpp::R_MIPS_TLS_LDM
:
9914 case elfcpp::R_MIPS_TLS_GOTTPREL
:
9917 // These relocations are not byte sized
9918 case elfcpp::R_MIPS_26
:
9919 case elfcpp::R_MIPS16_26
:
9920 case elfcpp::R_MIPS_PC21_S2
:
9921 case elfcpp::R_MIPS_PC26_S2
:
9922 case elfcpp::R_MIPS_PC18_S3
:
9923 case elfcpp::R_MIPS_PC19_S2
:
9926 case elfcpp::R_MIPS_COPY
:
9927 case elfcpp::R_MIPS_JUMP_SLOT
:
9928 object
->error(_("unexpected reloc %u in object file"), r_type
);
9932 object
->error(_("unsupported reloc %u in object file"), r_type
);
9937 // Scan the relocs during a relocatable link.
9939 template<int size
, bool big_endian
>
9941 Target_mips
<size
, big_endian
>::scan_relocatable_relocs(
9942 Symbol_table
* symtab
,
9944 Sized_relobj_file
<size
, big_endian
>* object
,
9945 unsigned int data_shndx
,
9946 unsigned int sh_type
,
9947 const unsigned char* prelocs
,
9949 Output_section
* output_section
,
9950 bool needs_special_offset_handling
,
9951 size_t local_symbol_count
,
9952 const unsigned char* plocal_symbols
,
9953 Relocatable_relocs
* rr
)
9955 if (sh_type
== elfcpp::SHT_REL
)
9957 typedef Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>
9959 typedef Mips_scan_relocatable_relocs
<big_endian
, Classify_reloc
>
9960 Scan_relocatable_relocs
;
9962 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_relocatable_relocs
>(
9970 needs_special_offset_handling
,
9975 else if (sh_type
== elfcpp::SHT_RELA
)
9977 typedef Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9979 typedef Mips_scan_relocatable_relocs
<big_endian
, Classify_reloc
>
9980 Scan_relocatable_relocs
;
9982 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_relocatable_relocs
>(
9990 needs_special_offset_handling
,
9999 // Scan the relocs for --emit-relocs.
10001 template<int size
, bool big_endian
>
10003 Target_mips
<size
, big_endian
>::emit_relocs_scan(
10004 Symbol_table
* symtab
,
10006 Sized_relobj_file
<size
, big_endian
>* object
,
10007 unsigned int data_shndx
,
10008 unsigned int sh_type
,
10009 const unsigned char* prelocs
,
10010 size_t reloc_count
,
10011 Output_section
* output_section
,
10012 bool needs_special_offset_handling
,
10013 size_t local_symbol_count
,
10014 const unsigned char* plocal_syms
,
10015 Relocatable_relocs
* rr
)
10017 if (sh_type
== elfcpp::SHT_REL
)
10019 typedef Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>
10021 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
10022 Emit_relocs_strategy
;
10024 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
10032 needs_special_offset_handling
,
10033 local_symbol_count
,
10037 else if (sh_type
== elfcpp::SHT_RELA
)
10039 typedef Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
10041 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
10042 Emit_relocs_strategy
;
10044 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
10052 needs_special_offset_handling
,
10053 local_symbol_count
,
10058 gold_unreachable();
10061 // Emit relocations for a section.
10063 template<int size
, bool big_endian
>
10065 Target_mips
<size
, big_endian
>::relocate_relocs(
10066 const Relocate_info
<size
, big_endian
>* relinfo
,
10067 unsigned int sh_type
,
10068 const unsigned char* prelocs
,
10069 size_t reloc_count
,
10070 Output_section
* output_section
,
10071 typename
elfcpp::Elf_types
<size
>::Elf_Off
10072 offset_in_output_section
,
10073 unsigned char* view
,
10074 Mips_address view_address
,
10075 section_size_type view_size
,
10076 unsigned char* reloc_view
,
10077 section_size_type reloc_view_size
)
10079 if (sh_type
== elfcpp::SHT_REL
)
10081 typedef Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>
10084 gold::relocate_relocs
<size
, big_endian
, Classify_reloc
>(
10089 offset_in_output_section
,
10096 else if (sh_type
== elfcpp::SHT_RELA
)
10098 typedef Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
10101 gold::relocate_relocs
<size
, big_endian
, Classify_reloc
>(
10106 offset_in_output_section
,
10114 gold_unreachable();
10117 // Perform target-specific processing in a relocatable link. This is
10118 // only used if we use the relocation strategy RELOC_SPECIAL.
10120 template<int size
, bool big_endian
>
10122 Target_mips
<size
, big_endian
>::relocate_special_relocatable(
10123 const Relocate_info
<size
, big_endian
>* relinfo
,
10124 unsigned int sh_type
,
10125 const unsigned char* preloc_in
,
10127 Output_section
* output_section
,
10128 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
10129 unsigned char* view
,
10130 Mips_address view_address
,
10132 unsigned char* preloc_out
)
10134 // We can only handle REL type relocation sections.
10135 gold_assert(sh_type
== elfcpp::SHT_REL
);
10137 typedef typename Reloc_types
<elfcpp::SHT_REL
, size
, big_endian
>::Reloc
10139 typedef typename Reloc_types
<elfcpp::SHT_REL
, size
, big_endian
>::Reloc_write
10142 typedef Mips_relocate_functions
<size
, big_endian
> Reloc_funcs
;
10144 const Mips_address invalid_address
= static_cast<Mips_address
>(0) - 1;
10146 Mips_relobj
<size
, big_endian
>* object
=
10147 Mips_relobj
<size
, big_endian
>::as_mips_relobj(relinfo
->object
);
10148 const unsigned int local_count
= object
->local_symbol_count();
10150 Reltype
reloc(preloc_in
);
10151 Reltype_write
reloc_write(preloc_out
);
10153 elfcpp::Elf_types
<32>::Elf_WXword r_info
= reloc
.get_r_info();
10154 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
10155 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
10157 // Get the new symbol index.
10158 // We only use RELOC_SPECIAL strategy in local relocations.
10159 gold_assert(r_sym
< local_count
);
10161 // We are adjusting a section symbol. We need to find
10162 // the symbol table index of the section symbol for
10163 // the output section corresponding to input section
10164 // in which this symbol is defined.
10166 unsigned int shndx
= object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
10167 gold_assert(is_ordinary
);
10168 Output_section
* os
= object
->output_section(shndx
);
10169 gold_assert(os
!= NULL
);
10170 gold_assert(os
->needs_symtab_index());
10171 unsigned int new_symndx
= os
->symtab_index();
10173 // Get the new offset--the location in the output section where
10174 // this relocation should be applied.
10176 Mips_address offset
= reloc
.get_r_offset();
10177 Mips_address new_offset
;
10178 if (offset_in_output_section
!= invalid_address
)
10179 new_offset
= offset
+ offset_in_output_section
;
10182 section_offset_type sot_offset
=
10183 convert_types
<section_offset_type
, Mips_address
>(offset
);
10184 section_offset_type new_sot_offset
=
10185 output_section
->output_offset(object
, relinfo
->data_shndx
,
10187 gold_assert(new_sot_offset
!= -1);
10188 new_offset
= new_sot_offset
;
10191 // In an object file, r_offset is an offset within the section.
10192 // In an executable or dynamic object, generated by
10193 // --emit-relocs, r_offset is an absolute address.
10194 if (!parameters
->options().relocatable())
10196 new_offset
+= view_address
;
10197 if (offset_in_output_section
!= invalid_address
)
10198 new_offset
-= offset_in_output_section
;
10201 reloc_write
.put_r_offset(new_offset
);
10202 reloc_write
.put_r_info(elfcpp::elf_r_info
<32>(new_symndx
, r_type
));
10204 // Handle the reloc addend.
10205 // The relocation uses a section symbol in the input file.
10206 // We are adjusting it to use a section symbol in the output
10207 // file. The input section symbol refers to some address in
10208 // the input section. We need the relocation in the output
10209 // file to refer to that same address. This adjustment to
10210 // the addend is the same calculation we use for a simple
10211 // absolute relocation for the input section symbol.
10212 Valtype calculated_value
= 0;
10213 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
10215 unsigned char* paddend
= view
+ offset
;
10216 typename
Reloc_funcs::Status reloc_status
= Reloc_funcs::STATUS_OKAY
;
10219 case elfcpp::R_MIPS_26
:
10220 reloc_status
= Reloc_funcs::rel26(paddend
, object
, psymval
,
10221 offset_in_output_section
, true, 0, sh_type
== elfcpp::SHT_REL
, NULL
,
10222 false /*TODO(sasa): cross mode jump*/, r_type
, this->jal_to_bal(),
10223 false, &calculated_value
);
10227 gold_unreachable();
10230 // Report any errors.
10231 switch (reloc_status
)
10233 case Reloc_funcs::STATUS_OKAY
:
10235 case Reloc_funcs::STATUS_OVERFLOW
:
10236 gold_error_at_location(relinfo
, relnum
, reloc
.get_r_offset(),
10237 _("relocation overflow"));
10239 case Reloc_funcs::STATUS_BAD_RELOC
:
10240 gold_error_at_location(relinfo
, relnum
, reloc
.get_r_offset(),
10241 _("unexpected opcode while processing relocation"));
10244 gold_unreachable();
10248 // Optimize the TLS relocation type based on what we know about the
10249 // symbol. IS_FINAL is true if the final address of this symbol is
10250 // known at link time.
10252 template<int size
, bool big_endian
>
10253 tls::Tls_optimization
10254 Target_mips
<size
, big_endian
>::optimize_tls_reloc(bool, int)
10256 // FIXME: Currently we do not do any TLS optimization.
10257 return tls::TLSOPT_NONE
;
10260 // Scan a relocation for a local symbol.
10262 template<int size
, bool big_endian
>
10264 Target_mips
<size
, big_endian
>::Scan::local(
10265 Symbol_table
* symtab
,
10267 Target_mips
<size
, big_endian
>* target
,
10268 Sized_relobj_file
<size
, big_endian
>* object
,
10269 unsigned int data_shndx
,
10270 Output_section
* output_section
,
10271 const Relatype
* rela
,
10272 const Reltype
* rel
,
10273 unsigned int rel_type
,
10274 unsigned int r_type
,
10275 const elfcpp::Sym
<size
, big_endian
>& lsym
,
10281 Mips_address r_offset
;
10282 unsigned int r_sym
;
10283 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
;
10285 if (rel_type
== elfcpp::SHT_RELA
)
10287 r_offset
= rela
->get_r_offset();
10288 r_sym
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
10290 r_addend
= rela
->get_r_addend();
10294 r_offset
= rel
->get_r_offset();
10295 r_sym
= Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>::
10300 Mips_relobj
<size
, big_endian
>* mips_obj
=
10301 Mips_relobj
<size
, big_endian
>::as_mips_relobj(object
);
10303 if (mips_obj
->is_mips16_stub_section(data_shndx
))
10305 mips_obj
->get_mips16_stub_section(data_shndx
)
10306 ->new_local_reloc_found(r_type
, r_sym
);
10309 if (r_type
== elfcpp::R_MIPS_NONE
)
10310 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10314 if (!mips16_call_reloc(r_type
)
10315 && !mips_obj
->section_allows_mips16_refs(data_shndx
))
10316 // This reloc would need to refer to a MIPS16 hard-float stub, if
10317 // there is one. We ignore MIPS16 stub sections and .pdr section when
10318 // looking for relocs that would need to refer to MIPS16 stubs.
10319 mips_obj
->add_local_non_16bit_call(r_sym
);
10321 if (r_type
== elfcpp::R_MIPS16_26
10322 && !mips_obj
->section_allows_mips16_refs(data_shndx
))
10323 mips_obj
->add_local_16bit_call(r_sym
);
10327 case elfcpp::R_MIPS_GOT16
:
10328 case elfcpp::R_MIPS_CALL16
:
10329 case elfcpp::R_MIPS_CALL_HI16
:
10330 case elfcpp::R_MIPS_CALL_LO16
:
10331 case elfcpp::R_MIPS_GOT_HI16
:
10332 case elfcpp::R_MIPS_GOT_LO16
:
10333 case elfcpp::R_MIPS_GOT_PAGE
:
10334 case elfcpp::R_MIPS_GOT_OFST
:
10335 case elfcpp::R_MIPS_GOT_DISP
:
10336 case elfcpp::R_MIPS_TLS_GOTTPREL
:
10337 case elfcpp::R_MIPS_TLS_GD
:
10338 case elfcpp::R_MIPS_TLS_LDM
:
10339 case elfcpp::R_MIPS16_GOT16
:
10340 case elfcpp::R_MIPS16_CALL16
:
10341 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
10342 case elfcpp::R_MIPS16_TLS_GD
:
10343 case elfcpp::R_MIPS16_TLS_LDM
:
10344 case elfcpp::R_MICROMIPS_GOT16
:
10345 case elfcpp::R_MICROMIPS_CALL16
:
10346 case elfcpp::R_MICROMIPS_CALL_HI16
:
10347 case elfcpp::R_MICROMIPS_CALL_LO16
:
10348 case elfcpp::R_MICROMIPS_GOT_HI16
:
10349 case elfcpp::R_MICROMIPS_GOT_LO16
:
10350 case elfcpp::R_MICROMIPS_GOT_PAGE
:
10351 case elfcpp::R_MICROMIPS_GOT_OFST
:
10352 case elfcpp::R_MICROMIPS_GOT_DISP
:
10353 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
10354 case elfcpp::R_MICROMIPS_TLS_GD
:
10355 case elfcpp::R_MICROMIPS_TLS_LDM
:
10356 case elfcpp::R_MIPS_EH
:
10357 // We need a GOT section.
10358 target
->got_section(symtab
, layout
);
10365 if (call_lo16_reloc(r_type
)
10366 || got_lo16_reloc(r_type
)
10367 || got_disp_reloc(r_type
)
10368 || eh_reloc(r_type
))
10370 // We may need a local GOT entry for this relocation. We
10371 // don't count R_MIPS_GOT_PAGE because we can estimate the
10372 // maximum number of pages needed by looking at the size of
10373 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10374 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10375 // R_MIPS_CALL_HI16 because these are always followed by an
10376 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10377 Mips_output_data_got
<size
, big_endian
>* got
=
10378 target
->got_section(symtab
, layout
);
10379 bool is_section_symbol
= lsym
.get_st_type() == elfcpp::STT_SECTION
;
10380 got
->record_local_got_symbol(mips_obj
, r_sym
, r_addend
, r_type
, -1U,
10381 is_section_symbol
);
10386 case elfcpp::R_MIPS_CALL16
:
10387 case elfcpp::R_MIPS16_CALL16
:
10388 case elfcpp::R_MICROMIPS_CALL16
:
10389 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10390 (unsigned long)r_offset
);
10393 case elfcpp::R_MIPS_GOT_PAGE
:
10394 case elfcpp::R_MICROMIPS_GOT_PAGE
:
10395 case elfcpp::R_MIPS16_GOT16
:
10396 case elfcpp::R_MIPS_GOT16
:
10397 case elfcpp::R_MIPS_GOT_HI16
:
10398 case elfcpp::R_MIPS_GOT_LO16
:
10399 case elfcpp::R_MICROMIPS_GOT16
:
10400 case elfcpp::R_MICROMIPS_GOT_HI16
:
10401 case elfcpp::R_MICROMIPS_GOT_LO16
:
10403 // This relocation needs a page entry in the GOT.
10404 // Get the section contents.
10405 section_size_type view_size
= 0;
10406 const unsigned char* view
= object
->section_contents(data_shndx
,
10407 &view_size
, false);
10410 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(view
);
10411 Valtype32 addend
= (rel_type
== elfcpp::SHT_REL
? val
& 0xffff
10414 if (rel_type
== elfcpp::SHT_REL
&& got16_reloc(r_type
))
10415 target
->got16_addends_
.push_back(got16_addend
<size
, big_endian
>(
10416 object
, data_shndx
, r_type
, r_sym
, addend
));
10418 target
->got_section()->record_got_page_entry(mips_obj
, r_sym
, addend
);
10422 case elfcpp::R_MIPS_HI16
:
10423 case elfcpp::R_MIPS_PCHI16
:
10424 case elfcpp::R_MIPS16_HI16
:
10425 case elfcpp::R_MICROMIPS_HI16
:
10426 // Record the reloc so that we can check whether the corresponding LO16
10428 if (rel_type
== elfcpp::SHT_REL
)
10429 target
->got16_addends_
.push_back(got16_addend
<size
, big_endian
>(
10430 object
, data_shndx
, r_type
, r_sym
, 0));
10433 case elfcpp::R_MIPS_LO16
:
10434 case elfcpp::R_MIPS_PCLO16
:
10435 case elfcpp::R_MIPS16_LO16
:
10436 case elfcpp::R_MICROMIPS_LO16
:
10438 if (rel_type
!= elfcpp::SHT_REL
)
10441 // Find corresponding GOT16/HI16 relocation.
10443 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10444 // be immediately following. However, for the IRIX6 ABI, the next
10445 // relocation may be a composed relocation consisting of several
10446 // relocations for the same address. In that case, the R_MIPS_LO16
10447 // relocation may occur as one of these. We permit a similar
10448 // extension in general, as that is useful for GCC.
10450 // In some cases GCC dead code elimination removes the LO16 but
10451 // keeps the corresponding HI16. This is strictly speaking a
10452 // violation of the ABI but not immediately harmful.
10454 typename
std::list
<got16_addend
<size
, big_endian
> >::iterator it
=
10455 target
->got16_addends_
.begin();
10456 while (it
!= target
->got16_addends_
.end())
10458 got16_addend
<size
, big_endian
> _got16_addend
= *it
;
10460 // TODO(sasa): Split got16_addends_ list into two lists - one for
10461 // GOT16 relocs and the other for HI16 relocs.
10463 // Report an error if we find HI16 or GOT16 reloc from the
10464 // previous section without the matching LO16 part.
10465 if (_got16_addend
.object
!= object
10466 || _got16_addend
.shndx
!= data_shndx
)
10468 gold_error("Can't find matching LO16 reloc");
10472 if (_got16_addend
.r_sym
!= r_sym
10473 || !is_matching_lo16_reloc(_got16_addend
.r_type
, r_type
))
10479 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10480 // For GOT16, we need to calculate combined addend and record GOT page
10482 if (got16_reloc(_got16_addend
.r_type
))
10485 section_size_type view_size
= 0;
10486 const unsigned char* view
= object
->section_contents(data_shndx
,
10491 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(view
);
10492 int32_t addend
= Bits
<16>::sign_extend32(val
& 0xffff);
10494 addend
= (_got16_addend
.addend
<< 16) + addend
;
10495 target
->got_section()->record_got_page_entry(mips_obj
, r_sym
,
10499 it
= target
->got16_addends_
.erase(it
);
10507 case elfcpp::R_MIPS_32
:
10508 case elfcpp::R_MIPS_REL32
:
10509 case elfcpp::R_MIPS_64
:
10511 if (parameters
->options().output_is_position_independent())
10513 // If building a shared library (or a position-independent
10514 // executable), we need to create a dynamic relocation for
10516 if (is_readonly_section(output_section
))
10518 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
10519 rel_dyn
->add_symbolless_local_addend(object
, r_sym
,
10520 elfcpp::R_MIPS_REL32
,
10521 output_section
, data_shndx
,
10527 case elfcpp::R_MIPS_TLS_GOTTPREL
:
10528 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
10529 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
10530 case elfcpp::R_MIPS_TLS_LDM
:
10531 case elfcpp::R_MIPS16_TLS_LDM
:
10532 case elfcpp::R_MICROMIPS_TLS_LDM
:
10533 case elfcpp::R_MIPS_TLS_GD
:
10534 case elfcpp::R_MIPS16_TLS_GD
:
10535 case elfcpp::R_MICROMIPS_TLS_GD
:
10537 bool output_is_shared
= parameters
->options().shared();
10538 const tls::Tls_optimization optimized_type
10539 = Target_mips
<size
, big_endian
>::optimize_tls_reloc(
10540 !output_is_shared
, r_type
);
10543 case elfcpp::R_MIPS_TLS_GD
:
10544 case elfcpp::R_MIPS16_TLS_GD
:
10545 case elfcpp::R_MICROMIPS_TLS_GD
:
10546 if (optimized_type
== tls::TLSOPT_NONE
)
10548 // Create a pair of GOT entries for the module index and
10549 // dtv-relative offset.
10550 Mips_output_data_got
<size
, big_endian
>* got
=
10551 target
->got_section(symtab
, layout
);
10552 unsigned int shndx
= lsym
.get_st_shndx();
10554 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
10557 object
->error(_("local symbol %u has bad shndx %u"),
10561 got
->record_local_got_symbol(mips_obj
, r_sym
, r_addend
, r_type
,
10566 // FIXME: TLS optimization not supported yet.
10567 gold_unreachable();
10571 case elfcpp::R_MIPS_TLS_LDM
:
10572 case elfcpp::R_MIPS16_TLS_LDM
:
10573 case elfcpp::R_MICROMIPS_TLS_LDM
:
10574 if (optimized_type
== tls::TLSOPT_NONE
)
10576 // We always record LDM symbols as local with index 0.
10577 target
->got_section()->record_local_got_symbol(mips_obj
, 0,
10583 // FIXME: TLS optimization not supported yet.
10584 gold_unreachable();
10587 case elfcpp::R_MIPS_TLS_GOTTPREL
:
10588 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
10589 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
10590 layout
->set_has_static_tls();
10591 if (optimized_type
== tls::TLSOPT_NONE
)
10593 // Create a GOT entry for the tp-relative offset.
10594 Mips_output_data_got
<size
, big_endian
>* got
=
10595 target
->got_section(symtab
, layout
);
10596 got
->record_local_got_symbol(mips_obj
, r_sym
, r_addend
, r_type
,
10601 // FIXME: TLS optimization not supported yet.
10602 gold_unreachable();
10607 gold_unreachable();
10616 // Refuse some position-dependent relocations when creating a
10617 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10618 // not PIC, but we can create dynamic relocations and the result
10619 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10620 // combined with R_MIPS_GOT16.
10621 if (parameters
->options().shared())
10625 case elfcpp::R_MIPS16_HI16
:
10626 case elfcpp::R_MIPS_HI16
:
10627 case elfcpp::R_MICROMIPS_HI16
:
10628 // Don't refuse a high part relocation if it's against
10629 // no symbol (e.g. part of a compound relocation).
10635 case elfcpp::R_MIPS16_26
:
10636 case elfcpp::R_MIPS_26
:
10637 case elfcpp::R_MICROMIPS_26_S1
:
10638 gold_error(_("%s: relocation %u against `%s' can not be used when "
10639 "making a shared object; recompile with -fPIC"),
10640 object
->name().c_str(), r_type
, "a local symbol");
10647 template<int size
, bool big_endian
>
10649 Target_mips
<size
, big_endian
>::Scan::local(
10650 Symbol_table
* symtab
,
10652 Target_mips
<size
, big_endian
>* target
,
10653 Sized_relobj_file
<size
, big_endian
>* object
,
10654 unsigned int data_shndx
,
10655 Output_section
* output_section
,
10656 const Reltype
& reloc
,
10657 unsigned int r_type
,
10658 const elfcpp::Sym
<size
, big_endian
>& lsym
,
10671 (const Relatype
*) NULL
,
10675 lsym
, is_discarded
);
10679 template<int size
, bool big_endian
>
10681 Target_mips
<size
, big_endian
>::Scan::local(
10682 Symbol_table
* symtab
,
10684 Target_mips
<size
, big_endian
>* target
,
10685 Sized_relobj_file
<size
, big_endian
>* object
,
10686 unsigned int data_shndx
,
10687 Output_section
* output_section
,
10688 const Relatype
& reloc
,
10689 unsigned int r_type
,
10690 const elfcpp::Sym
<size
, big_endian
>& lsym
,
10704 (const Reltype
*) NULL
,
10707 lsym
, is_discarded
);
10710 // Scan a relocation for a global symbol.
10712 template<int size
, bool big_endian
>
10714 Target_mips
<size
, big_endian
>::Scan::global(
10715 Symbol_table
* symtab
,
10717 Target_mips
<size
, big_endian
>* target
,
10718 Sized_relobj_file
<size
, big_endian
>* object
,
10719 unsigned int data_shndx
,
10720 Output_section
* output_section
,
10721 const Relatype
* rela
,
10722 const Reltype
* rel
,
10723 unsigned int rel_type
,
10724 unsigned int r_type
,
10727 Mips_address r_offset
;
10728 unsigned int r_sym
;
10729 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
;
10731 if (rel_type
== elfcpp::SHT_RELA
)
10733 r_offset
= rela
->get_r_offset();
10734 r_sym
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
10736 r_addend
= rela
->get_r_addend();
10740 r_offset
= rel
->get_r_offset();
10741 r_sym
= Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>::
10746 Mips_relobj
<size
, big_endian
>* mips_obj
=
10747 Mips_relobj
<size
, big_endian
>::as_mips_relobj(object
);
10748 Mips_symbol
<size
>* mips_sym
= Mips_symbol
<size
>::as_mips_sym(gsym
);
10750 if (mips_obj
->is_mips16_stub_section(data_shndx
))
10752 mips_obj
->get_mips16_stub_section(data_shndx
)
10753 ->new_global_reloc_found(r_type
, mips_sym
);
10756 if (r_type
== elfcpp::R_MIPS_NONE
)
10757 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10761 if (!mips16_call_reloc(r_type
)
10762 && !mips_obj
->section_allows_mips16_refs(data_shndx
))
10763 // This reloc would need to refer to a MIPS16 hard-float stub, if
10764 // there is one. We ignore MIPS16 stub sections and .pdr section when
10765 // looking for relocs that would need to refer to MIPS16 stubs.
10766 mips_sym
->set_need_fn_stub();
10768 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
10769 // section. We check here to avoid creating a dynamic reloc against
10770 // _GLOBAL_OFFSET_TABLE_.
10771 if (!target
->has_got_section()
10772 && strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
10773 target
->got_section(symtab
, layout
);
10775 // We need PLT entries if there are static-only relocations against
10776 // an externally-defined function. This can technically occur for
10777 // shared libraries if there are branches to the symbol, although it
10778 // is unlikely that this will be used in practice due to the short
10779 // ranges involved. It can occur for any relative or absolute relocation
10780 // in executables; in that case, the PLT entry becomes the function's
10781 // canonical address.
10782 bool static_reloc
= false;
10784 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10785 // relocation into a dynamic one.
10786 bool can_make_dynamic
= false;
10789 case elfcpp::R_MIPS_GOT16
:
10790 case elfcpp::R_MIPS_CALL16
:
10791 case elfcpp::R_MIPS_CALL_HI16
:
10792 case elfcpp::R_MIPS_CALL_LO16
:
10793 case elfcpp::R_MIPS_GOT_HI16
:
10794 case elfcpp::R_MIPS_GOT_LO16
:
10795 case elfcpp::R_MIPS_GOT_PAGE
:
10796 case elfcpp::R_MIPS_GOT_OFST
:
10797 case elfcpp::R_MIPS_GOT_DISP
:
10798 case elfcpp::R_MIPS_TLS_GOTTPREL
:
10799 case elfcpp::R_MIPS_TLS_GD
:
10800 case elfcpp::R_MIPS_TLS_LDM
:
10801 case elfcpp::R_MIPS16_GOT16
:
10802 case elfcpp::R_MIPS16_CALL16
:
10803 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
10804 case elfcpp::R_MIPS16_TLS_GD
:
10805 case elfcpp::R_MIPS16_TLS_LDM
:
10806 case elfcpp::R_MICROMIPS_GOT16
:
10807 case elfcpp::R_MICROMIPS_CALL16
:
10808 case elfcpp::R_MICROMIPS_CALL_HI16
:
10809 case elfcpp::R_MICROMIPS_CALL_LO16
:
10810 case elfcpp::R_MICROMIPS_GOT_HI16
:
10811 case elfcpp::R_MICROMIPS_GOT_LO16
:
10812 case elfcpp::R_MICROMIPS_GOT_PAGE
:
10813 case elfcpp::R_MICROMIPS_GOT_OFST
:
10814 case elfcpp::R_MICROMIPS_GOT_DISP
:
10815 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
10816 case elfcpp::R_MICROMIPS_TLS_GD
:
10817 case elfcpp::R_MICROMIPS_TLS_LDM
:
10818 case elfcpp::R_MIPS_EH
:
10819 // We need a GOT section.
10820 target
->got_section(symtab
, layout
);
10823 // This is just a hint; it can safely be ignored. Don't set
10824 // has_static_relocs for the corresponding symbol.
10825 case elfcpp::R_MIPS_JALR
:
10826 case elfcpp::R_MICROMIPS_JALR
:
10829 case elfcpp::R_MIPS_GPREL16
:
10830 case elfcpp::R_MIPS_GPREL32
:
10831 case elfcpp::R_MIPS16_GPREL
:
10832 case elfcpp::R_MICROMIPS_GPREL16
:
10834 // GP-relative relocations always resolve to a definition in a
10835 // regular input file, ignoring the one-definition rule. This is
10836 // important for the GP setup sequence in NewABI code, which
10837 // always resolves to a local function even if other relocations
10838 // against the symbol wouldn't.
10839 //constrain_symbol_p = FALSE;
10842 case elfcpp::R_MIPS_32
:
10843 case elfcpp::R_MIPS_REL32
:
10844 case elfcpp::R_MIPS_64
:
10845 if ((parameters
->options().shared()
10846 || (strcmp(gsym
->name(), "__gnu_local_gp") != 0
10847 && (!is_readonly_section(output_section
)
10848 || mips_obj
->is_pic())))
10849 && (output_section
->flags() & elfcpp::SHF_ALLOC
) != 0)
10851 if (r_type
!= elfcpp::R_MIPS_REL32
)
10852 mips_sym
->set_pointer_equality_needed();
10853 can_make_dynamic
= true;
10859 // Most static relocations require pointer equality, except
10861 mips_sym
->set_pointer_equality_needed();
10865 case elfcpp::R_MIPS_26
:
10866 case elfcpp::R_MIPS_PC16
:
10867 case elfcpp::R_MIPS_PC21_S2
:
10868 case elfcpp::R_MIPS_PC26_S2
:
10869 case elfcpp::R_MIPS16_26
:
10870 case elfcpp::R_MICROMIPS_26_S1
:
10871 case elfcpp::R_MICROMIPS_PC7_S1
:
10872 case elfcpp::R_MICROMIPS_PC10_S1
:
10873 case elfcpp::R_MICROMIPS_PC16_S1
:
10874 case elfcpp::R_MICROMIPS_PC23_S2
:
10875 static_reloc
= true;
10876 mips_sym
->set_has_static_relocs();
10880 // If there are call relocations against an externally-defined symbol,
10881 // see whether we can create a MIPS lazy-binding stub for it. We can
10882 // only do this if all references to the function are through call
10883 // relocations, and in that case, the traditional lazy-binding stubs
10884 // are much more efficient than PLT entries.
10887 case elfcpp::R_MIPS16_CALL16
:
10888 case elfcpp::R_MIPS_CALL16
:
10889 case elfcpp::R_MIPS_CALL_HI16
:
10890 case elfcpp::R_MIPS_CALL_LO16
:
10891 case elfcpp::R_MIPS_JALR
:
10892 case elfcpp::R_MICROMIPS_CALL16
:
10893 case elfcpp::R_MICROMIPS_CALL_HI16
:
10894 case elfcpp::R_MICROMIPS_CALL_LO16
:
10895 case elfcpp::R_MICROMIPS_JALR
:
10896 if (!mips_sym
->no_lazy_stub())
10898 if ((mips_sym
->needs_plt_entry() && mips_sym
->is_from_dynobj())
10899 // Calls from shared objects to undefined symbols of type
10900 // STT_NOTYPE need lazy-binding stub.
10901 || (mips_sym
->is_undefined() && parameters
->options().shared()))
10902 target
->mips_stubs_section(layout
)->make_entry(mips_sym
);
10907 // We must not create a stub for a symbol that has relocations
10908 // related to taking the function's address.
10909 mips_sym
->set_no_lazy_stub();
10910 target
->remove_lazy_stub_entry(mips_sym
);
10915 if (relocation_needs_la25_stub
<size
, big_endian
>(mips_obj
, r_type
,
10916 mips_sym
->is_mips16()))
10917 mips_sym
->set_has_nonpic_branches();
10919 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
10920 // and has a special meaning.
10921 bool gp_disp_against_hi16
= (!mips_obj
->is_newabi()
10922 && strcmp(gsym
->name(), "_gp_disp") == 0
10923 && (hi16_reloc(r_type
) || lo16_reloc(r_type
)));
10924 if (static_reloc
&& gsym
->needs_plt_entry())
10926 target
->make_plt_entry(symtab
, layout
, mips_sym
, r_type
);
10928 // Since this is not a PC-relative relocation, we may be
10929 // taking the address of a function. In that case we need to
10930 // set the entry in the dynamic symbol table to the address of
10932 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
10934 gsym
->set_needs_dynsym_value();
10935 // We distinguish between PLT entries and lazy-binding stubs by
10936 // giving the former an st_other value of STO_MIPS_PLT. Set the
10937 // flag if there are any relocations in the binary where pointer
10938 // equality matters.
10939 if (mips_sym
->pointer_equality_needed())
10940 mips_sym
->set_mips_plt();
10943 if ((static_reloc
|| can_make_dynamic
) && !gp_disp_against_hi16
)
10945 // Absolute addressing relocations.
10946 // Make a dynamic relocation if necessary.
10947 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
10949 if (gsym
->may_need_copy_reloc())
10951 target
->copy_reloc(symtab
, layout
, object
, data_shndx
,
10952 output_section
, gsym
, r_type
, r_offset
);
10954 else if (can_make_dynamic
)
10956 // Create .rel.dyn section.
10957 target
->rel_dyn_section(layout
);
10958 target
->dynamic_reloc(mips_sym
, elfcpp::R_MIPS_REL32
, mips_obj
,
10959 data_shndx
, output_section
, r_offset
);
10962 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
10967 bool for_call
= false;
10970 case elfcpp::R_MIPS_CALL16
:
10971 case elfcpp::R_MIPS16_CALL16
:
10972 case elfcpp::R_MICROMIPS_CALL16
:
10973 case elfcpp::R_MIPS_CALL_HI16
:
10974 case elfcpp::R_MIPS_CALL_LO16
:
10975 case elfcpp::R_MICROMIPS_CALL_HI16
:
10976 case elfcpp::R_MICROMIPS_CALL_LO16
:
10980 case elfcpp::R_MIPS16_GOT16
:
10981 case elfcpp::R_MIPS_GOT16
:
10982 case elfcpp::R_MIPS_GOT_HI16
:
10983 case elfcpp::R_MIPS_GOT_LO16
:
10984 case elfcpp::R_MICROMIPS_GOT16
:
10985 case elfcpp::R_MICROMIPS_GOT_HI16
:
10986 case elfcpp::R_MICROMIPS_GOT_LO16
:
10987 case elfcpp::R_MIPS_GOT_DISP
:
10988 case elfcpp::R_MICROMIPS_GOT_DISP
:
10989 case elfcpp::R_MIPS_EH
:
10991 // The symbol requires a GOT entry.
10992 Mips_output_data_got
<size
, big_endian
>* got
=
10993 target
->got_section(symtab
, layout
);
10994 got
->record_global_got_symbol(mips_sym
, mips_obj
, r_type
, false,
10996 mips_sym
->set_global_got_area(GGA_NORMAL
);
11000 case elfcpp::R_MIPS_GOT_PAGE
:
11001 case elfcpp::R_MICROMIPS_GOT_PAGE
:
11003 // This relocation needs a page entry in the GOT.
11004 // Get the section contents.
11005 section_size_type view_size
= 0;
11006 const unsigned char* view
=
11007 object
->section_contents(data_shndx
, &view_size
, false);
11010 Valtype32 val
= elfcpp::Swap
<32, big_endian
>::readval(view
);
11011 Valtype32 addend
= (rel_type
== elfcpp::SHT_REL
? val
& 0xffff
11013 Mips_output_data_got
<size
, big_endian
>* got
=
11014 target
->got_section(symtab
, layout
);
11015 got
->record_got_page_entry(mips_obj
, r_sym
, addend
);
11017 // If this is a global, overridable symbol, GOT_PAGE will
11018 // decay to GOT_DISP, so we'll need a GOT entry for it.
11019 bool def_regular
= (mips_sym
->source() == Symbol::FROM_OBJECT
11020 && !mips_sym
->object()->is_dynamic()
11021 && !mips_sym
->is_undefined());
11023 || (parameters
->options().output_is_position_independent()
11024 && !parameters
->options().Bsymbolic()
11025 && !mips_sym
->is_forced_local()))
11027 got
->record_global_got_symbol(mips_sym
, mips_obj
, r_type
, false,
11029 mips_sym
->set_global_got_area(GGA_NORMAL
);
11034 case elfcpp::R_MIPS_TLS_GOTTPREL
:
11035 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
11036 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
11037 case elfcpp::R_MIPS_TLS_LDM
:
11038 case elfcpp::R_MIPS16_TLS_LDM
:
11039 case elfcpp::R_MICROMIPS_TLS_LDM
:
11040 case elfcpp::R_MIPS_TLS_GD
:
11041 case elfcpp::R_MIPS16_TLS_GD
:
11042 case elfcpp::R_MICROMIPS_TLS_GD
:
11044 const bool is_final
= gsym
->final_value_is_known();
11045 const tls::Tls_optimization optimized_type
=
11046 Target_mips
<size
, big_endian
>::optimize_tls_reloc(is_final
, r_type
);
11050 case elfcpp::R_MIPS_TLS_GD
:
11051 case elfcpp::R_MIPS16_TLS_GD
:
11052 case elfcpp::R_MICROMIPS_TLS_GD
:
11053 if (optimized_type
== tls::TLSOPT_NONE
)
11055 // Create a pair of GOT entries for the module index and
11056 // dtv-relative offset.
11057 Mips_output_data_got
<size
, big_endian
>* got
=
11058 target
->got_section(symtab
, layout
);
11059 got
->record_global_got_symbol(mips_sym
, mips_obj
, r_type
, false,
11064 // FIXME: TLS optimization not supported yet.
11065 gold_unreachable();
11069 case elfcpp::R_MIPS_TLS_LDM
:
11070 case elfcpp::R_MIPS16_TLS_LDM
:
11071 case elfcpp::R_MICROMIPS_TLS_LDM
:
11072 if (optimized_type
== tls::TLSOPT_NONE
)
11074 // We always record LDM symbols as local with index 0.
11075 target
->got_section()->record_local_got_symbol(mips_obj
, 0,
11081 // FIXME: TLS optimization not supported yet.
11082 gold_unreachable();
11085 case elfcpp::R_MIPS_TLS_GOTTPREL
:
11086 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
11087 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
11088 layout
->set_has_static_tls();
11089 if (optimized_type
== tls::TLSOPT_NONE
)
11091 // Create a GOT entry for the tp-relative offset.
11092 Mips_output_data_got
<size
, big_endian
>* got
=
11093 target
->got_section(symtab
, layout
);
11094 got
->record_global_got_symbol(mips_sym
, mips_obj
, r_type
, false,
11099 // FIXME: TLS optimization not supported yet.
11100 gold_unreachable();
11105 gold_unreachable();
11109 case elfcpp::R_MIPS_COPY
:
11110 case elfcpp::R_MIPS_JUMP_SLOT
:
11111 // These are relocations which should only be seen by the
11112 // dynamic linker, and should never be seen here.
11113 gold_error(_("%s: unexpected reloc %u in object file"),
11114 object
->name().c_str(), r_type
);
11121 // Refuse some position-dependent relocations when creating a
11122 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
11123 // not PIC, but we can create dynamic relocations and the result
11124 // will be fine. Also do not refuse R_MIPS_LO16, which can be
11125 // combined with R_MIPS_GOT16.
11126 if (parameters
->options().shared())
11130 case elfcpp::R_MIPS16_HI16
:
11131 case elfcpp::R_MIPS_HI16
:
11132 case elfcpp::R_MICROMIPS_HI16
:
11133 // Don't refuse a high part relocation if it's against
11134 // no symbol (e.g. part of a compound relocation).
11138 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11139 // and has a special meaning.
11140 if (!mips_obj
->is_newabi() && strcmp(gsym
->name(), "_gp_disp") == 0)
11145 case elfcpp::R_MIPS16_26
:
11146 case elfcpp::R_MIPS_26
:
11147 case elfcpp::R_MICROMIPS_26_S1
:
11148 gold_error(_("%s: relocation %u against `%s' can not be used when "
11149 "making a shared object; recompile with -fPIC"),
11150 object
->name().c_str(), r_type
, gsym
->name());
11157 template<int size
, bool big_endian
>
11159 Target_mips
<size
, big_endian
>::Scan::global(
11160 Symbol_table
* symtab
,
11162 Target_mips
<size
, big_endian
>* target
,
11163 Sized_relobj_file
<size
, big_endian
>* object
,
11164 unsigned int data_shndx
,
11165 Output_section
* output_section
,
11166 const Relatype
& reloc
,
11167 unsigned int r_type
,
11178 (const Reltype
*) NULL
,
11184 template<int size
, bool big_endian
>
11186 Target_mips
<size
, big_endian
>::Scan::global(
11187 Symbol_table
* symtab
,
11189 Target_mips
<size
, big_endian
>* target
,
11190 Sized_relobj_file
<size
, big_endian
>* object
,
11191 unsigned int data_shndx
,
11192 Output_section
* output_section
,
11193 const Reltype
& reloc
,
11194 unsigned int r_type
,
11204 (const Relatype
*) NULL
,
11211 // Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11212 // In cases where Scan::local() or Scan::global() has created
11213 // a dynamic relocation, the addend of the relocation is carried
11214 // in the data, and we must not apply the static relocation.
11216 template<int size
, bool big_endian
>
11218 Target_mips
<size
, big_endian
>::Relocate::should_apply_static_reloc(
11219 const Mips_symbol
<size
>* gsym
,
11220 unsigned int r_type
,
11221 Output_section
* output_section
,
11222 Target_mips
* target
)
11224 // If the output section is not allocated, then we didn't call
11225 // scan_relocs, we didn't create a dynamic reloc, and we must apply
11227 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
11234 // For global symbols, we use the same helper routines used in the
11236 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
))
11237 && !gsym
->may_need_copy_reloc())
11239 // We have generated dynamic reloc (R_MIPS_REL32).
11241 bool multi_got
= false;
11242 if (target
->has_got_section())
11243 multi_got
= target
->got_section()->multi_got();
11244 bool has_got_offset
;
11246 has_got_offset
= gsym
->has_got_offset(GOT_TYPE_STANDARD
);
11248 has_got_offset
= gsym
->global_gotoffset() != -1U;
11249 if (!has_got_offset
)
11252 // Apply the relocation only if the symbol is in the local got.
11253 // Do not apply the relocation if the symbol is in the global
11255 return symbol_references_local(gsym
, gsym
->has_dynsym_index());
11258 // We have not generated dynamic reloc.
11263 // Perform a relocation.
11265 template<int size
, bool big_endian
>
11267 Target_mips
<size
, big_endian
>::Relocate::relocate(
11268 const Relocate_info
<size
, big_endian
>* relinfo
,
11269 unsigned int rel_type
,
11270 Target_mips
* target
,
11271 Output_section
* output_section
,
11273 const unsigned char* preloc
,
11274 const Sized_symbol
<size
>* gsym
,
11275 const Symbol_value
<size
>* psymval
,
11276 unsigned char* view
,
11277 Mips_address address
,
11280 Mips_address r_offset
;
11281 unsigned int r_sym
;
11282 unsigned int r_type
;
11283 unsigned int r_type2
;
11284 unsigned int r_type3
;
11285 unsigned char r_ssym
;
11286 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
;
11288 if (rel_type
== elfcpp::SHT_RELA
)
11290 const Relatype
rela(preloc
);
11291 r_offset
= rela
.get_r_offset();
11292 r_sym
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
11294 r_type
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
11296 r_type2
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
11297 get_r_type2(&rela
);
11298 r_type3
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
11299 get_r_type3(&rela
);
11300 r_ssym
= Mips_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>::
11302 r_addend
= rela
.get_r_addend();
11306 const Reltype
rel(preloc
);
11307 r_offset
= rel
.get_r_offset();
11308 r_sym
= Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>::
11310 r_type
= Mips_classify_reloc
<elfcpp::SHT_REL
, size
, big_endian
>::
11318 typedef Mips_relocate_functions
<size
, big_endian
> Reloc_funcs
;
11319 typename
Reloc_funcs::Status reloc_status
= Reloc_funcs::STATUS_OKAY
;
11321 Mips_relobj
<size
, big_endian
>* object
=
11322 Mips_relobj
<size
, big_endian
>::as_mips_relobj(relinfo
->object
);
11324 bool target_is_16_bit_code
= false;
11325 bool target_is_micromips_code
= false;
11326 bool cross_mode_jump
;
11328 Symbol_value
<size
> symval
;
11330 const Mips_symbol
<size
>* mips_sym
= Mips_symbol
<size
>::as_mips_sym(gsym
);
11332 bool changed_symbol_value
= false;
11335 target_is_16_bit_code
= object
->local_symbol_is_mips16(r_sym
);
11336 target_is_micromips_code
= object
->local_symbol_is_micromips(r_sym
);
11337 if (target_is_16_bit_code
|| target_is_micromips_code
)
11339 // MIPS16/microMIPS text labels should be treated as odd.
11340 symval
.set_output_value(psymval
->value(object
, 1));
11342 changed_symbol_value
= true;
11347 target_is_16_bit_code
= mips_sym
->is_mips16();
11348 target_is_micromips_code
= mips_sym
->is_micromips();
11350 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11351 // it odd. This will cause something like .word SYM to come up with
11352 // the right value when it is loaded into the PC.
11354 if ((mips_sym
->is_mips16() || mips_sym
->is_micromips())
11355 && psymval
->value(object
, 0) != 0)
11357 symval
.set_output_value(psymval
->value(object
, 0) | 1);
11359 changed_symbol_value
= true;
11362 // Pick the value to use for symbols defined in shared objects.
11363 if (mips_sym
->use_plt_offset(Scan::get_reference_flags(r_type
))
11364 || mips_sym
->has_lazy_stub())
11366 Mips_address value
;
11367 if (!mips_sym
->has_lazy_stub())
11369 // Prefer a standard MIPS PLT entry.
11370 if (mips_sym
->has_mips_plt_offset())
11372 value
= target
->plt_section()->mips_entry_address(mips_sym
);
11373 target_is_micromips_code
= false;
11374 target_is_16_bit_code
= false;
11378 value
= (target
->plt_section()->comp_entry_address(mips_sym
)
11380 if (target
->is_output_micromips())
11381 target_is_micromips_code
= true;
11383 target_is_16_bit_code
= true;
11387 value
= target
->mips_stubs_section()->stub_address(mips_sym
);
11389 symval
.set_output_value(value
);
11394 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11395 // Note that such a symbol must always be a global symbol.
11396 bool gp_disp
= (gsym
!= NULL
&& (strcmp(gsym
->name(), "_gp_disp") == 0)
11397 && !object
->is_newabi());
11399 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11400 // Note that such a symbol must always be a global symbol.
11401 bool gnu_local_gp
= gsym
&& (strcmp(gsym
->name(), "__gnu_local_gp") == 0);
11406 if (!hi16_reloc(r_type
) && !lo16_reloc(r_type
))
11407 gold_error_at_location(relinfo
, relnum
, r_offset
,
11408 _("relocations against _gp_disp are permitted only"
11409 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11411 else if (gnu_local_gp
)
11413 // __gnu_local_gp is _gp symbol.
11414 symval
.set_output_value(target
->adjusted_gp_value(object
));
11418 // If this is a reference to a 16-bit function with a stub, we need
11419 // to redirect the relocation to the stub unless:
11421 // (a) the relocation is for a MIPS16 JAL;
11423 // (b) the relocation is for a MIPS16 PIC call, and there are no
11424 // non-MIPS16 uses of the GOT slot; or
11426 // (c) the section allows direct references to MIPS16 functions.
11427 if (r_type
!= elfcpp::R_MIPS16_26
11428 && !parameters
->options().relocatable()
11429 && ((mips_sym
!= NULL
11430 && mips_sym
->has_mips16_fn_stub()
11431 && (r_type
!= elfcpp::R_MIPS16_CALL16
|| mips_sym
->need_fn_stub()))
11432 || (mips_sym
== NULL
11433 && object
->get_local_mips16_fn_stub(r_sym
) != NULL
))
11434 && !object
->section_allows_mips16_refs(relinfo
->data_shndx
))
11436 // This is a 32- or 64-bit call to a 16-bit function. We should
11437 // have already noticed that we were going to need the
11439 Mips_address value
;
11440 if (mips_sym
== NULL
)
11441 value
= object
->get_local_mips16_fn_stub(r_sym
)->output_address();
11444 gold_assert(mips_sym
->need_fn_stub());
11445 if (mips_sym
->has_la25_stub())
11446 value
= target
->la25_stub_section()->stub_address(mips_sym
);
11449 value
= mips_sym
->template
11450 get_mips16_fn_stub
<big_endian
>()->output_address();
11453 symval
.set_output_value(value
);
11455 changed_symbol_value
= true;
11457 // The target is 16-bit, but the stub isn't.
11458 target_is_16_bit_code
= false;
11460 // If this is a MIPS16 call with a stub, that is made through the PLT or
11461 // to a standard MIPS function, we need to redirect the call to the stub.
11462 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11463 // indirect calls should use an indirect stub instead.
11464 else if (r_type
== elfcpp::R_MIPS16_26
&& !parameters
->options().relocatable()
11465 && ((mips_sym
!= NULL
11466 && (mips_sym
->has_mips16_call_stub()
11467 || mips_sym
->has_mips16_call_fp_stub()))
11468 || (mips_sym
== NULL
11469 && object
->get_local_mips16_call_stub(r_sym
) != NULL
))
11470 && ((mips_sym
!= NULL
&& mips_sym
->has_plt_offset())
11471 || !target_is_16_bit_code
))
11473 Mips16_stub_section
<size
, big_endian
>* call_stub
;
11474 if (mips_sym
== NULL
)
11475 call_stub
= object
->get_local_mips16_call_stub(r_sym
);
11478 // If both call_stub and call_fp_stub are defined, we can figure
11479 // out which one to use by checking which one appears in the input
11481 if (mips_sym
->has_mips16_call_stub()
11482 && mips_sym
->has_mips16_call_fp_stub())
11485 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
11487 if (object
->is_mips16_call_fp_stub_section(i
))
11489 call_stub
= mips_sym
->template
11490 get_mips16_call_fp_stub
<big_endian
>();
11495 if (call_stub
== NULL
)
11497 mips_sym
->template get_mips16_call_stub
<big_endian
>();
11499 else if (mips_sym
->has_mips16_call_stub())
11500 call_stub
= mips_sym
->template get_mips16_call_stub
<big_endian
>();
11502 call_stub
= mips_sym
->template get_mips16_call_fp_stub
<big_endian
>();
11505 symval
.set_output_value(call_stub
->output_address());
11507 changed_symbol_value
= true;
11509 // If this is a direct call to a PIC function, redirect to the
11511 else if (mips_sym
!= NULL
11512 && mips_sym
->has_la25_stub()
11513 && relocation_needs_la25_stub
<size
, big_endian
>(
11514 object
, r_type
, target_is_16_bit_code
))
11516 Mips_address value
= target
->la25_stub_section()->stub_address(mips_sym
);
11517 if (mips_sym
->is_micromips())
11519 symval
.set_output_value(value
);
11522 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11523 // entry is used if a standard PLT entry has also been made.
11524 else if ((r_type
== elfcpp::R_MIPS16_26
11525 || r_type
== elfcpp::R_MICROMIPS_26_S1
)
11526 && !parameters
->options().relocatable()
11527 && mips_sym
!= NULL
11528 && mips_sym
->has_plt_offset()
11529 && mips_sym
->has_comp_plt_offset()
11530 && mips_sym
->has_mips_plt_offset())
11532 Mips_address value
= (target
->plt_section()->comp_entry_address(mips_sym
)
11534 symval
.set_output_value(value
);
11537 target_is_16_bit_code
= !target
->is_output_micromips();
11538 target_is_micromips_code
= target
->is_output_micromips();
11541 // Make sure MIPS16 and microMIPS are not used together.
11542 if ((r_type
== elfcpp::R_MIPS16_26
&& target_is_micromips_code
)
11543 || (micromips_branch_reloc(r_type
) && target_is_16_bit_code
))
11545 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11548 // Calls from 16-bit code to 32-bit code and vice versa require the
11549 // mode change. However, we can ignore calls to undefined weak symbols,
11550 // which should never be executed at runtime. This exception is important
11551 // because the assembly writer may have "known" that any definition of the
11552 // symbol would be 16-bit code, and that direct jumps were therefore
11555 (!parameters
->options().relocatable()
11556 && !(gsym
!= NULL
&& gsym
->is_weak_undefined())
11557 && ((r_type
== elfcpp::R_MIPS16_26
&& !target_is_16_bit_code
)
11558 || (r_type
== elfcpp::R_MICROMIPS_26_S1
&& !target_is_micromips_code
)
11559 || ((r_type
== elfcpp::R_MIPS_26
|| r_type
== elfcpp::R_MIPS_JALR
)
11560 && (target_is_16_bit_code
|| target_is_micromips_code
))));
11562 bool local
= (mips_sym
== NULL
11563 || (mips_sym
->got_only_for_calls()
11564 ? symbol_calls_local(mips_sym
, mips_sym
->has_dynsym_index())
11565 : symbol_references_local(mips_sym
,
11566 mips_sym
->has_dynsym_index())));
11568 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11569 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11570 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11571 if (got_page_reloc(r_type
) && !local
)
11572 r_type
= (micromips_reloc(r_type
) ? elfcpp::R_MICROMIPS_GOT_DISP
11573 : elfcpp::R_MIPS_GOT_DISP
);
11575 unsigned int got_offset
= 0;
11578 bool calculate_only
= false;
11579 Valtype calculated_value
= 0;
11580 bool extract_addend
= rel_type
== elfcpp::SHT_REL
;
11581 unsigned int r_types
[3] = { r_type
, r_type2
, r_type3
};
11583 Reloc_funcs::mips_reloc_unshuffle(view
, r_type
, false);
11585 // For Mips64 N64 ABI, there may be up to three operations specified per
11586 // record, by the fields r_type, r_type2, and r_type3. The first operation
11587 // takes its addend from the relocation record. Each subsequent operation
11588 // takes as its addend the result of the previous operation.
11589 // The first operation in a record which references a symbol uses the symbol
11590 // implied by r_sym. The next operation in a record which references a symbol
11591 // uses the special symbol value given by the r_ssym field. A third operation
11592 // in a record which references a symbol will assume a NULL symbol,
11593 // i.e. value zero.
11596 // Check if a record references to a symbol.
11597 for (unsigned int i
= 0; i
< 3; ++i
)
11599 if (r_types
[i
] == elfcpp::R_MIPS_NONE
)
11603 // Check if the next relocation is for the same instruction.
11604 calculate_only
= i
== 2 ? false
11605 : r_types
[i
+1] != elfcpp::R_MIPS_NONE
;
11607 if (object
->is_n64())
11611 // Handle special symbol for r_type2 relocation type.
11615 symval
.set_output_value(0);
11618 symval
.set_output_value(target
->gp_value());
11621 symval
.set_output_value(object
->gp_value());
11624 symval
.set_output_value(address
);
11627 gold_unreachable();
11633 // For r_type3 symbol value is 0.
11634 symval
.set_output_value(0);
11638 bool update_got_entry
= false;
11639 switch (r_types
[i
])
11641 case elfcpp::R_MIPS_NONE
:
11643 case elfcpp::R_MIPS_16
:
11644 reloc_status
= Reloc_funcs::rel16(view
, object
, psymval
, r_addend
,
11645 extract_addend
, calculate_only
,
11646 &calculated_value
);
11649 case elfcpp::R_MIPS_32
:
11650 if (should_apply_static_reloc(mips_sym
, r_types
[i
], output_section
,
11652 reloc_status
= Reloc_funcs::rel32(view
, object
, psymval
, r_addend
,
11653 extract_addend
, calculate_only
,
11654 &calculated_value
);
11655 if (mips_sym
!= NULL
11656 && (mips_sym
->is_mips16() || mips_sym
->is_micromips())
11657 && mips_sym
->global_got_area() == GGA_RELOC_ONLY
)
11659 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11660 // already updated by adding +1.
11661 if (mips_sym
->has_mips16_fn_stub())
11663 gold_assert(mips_sym
->need_fn_stub());
11664 Mips16_stub_section
<size
, big_endian
>* fn_stub
=
11665 mips_sym
->template get_mips16_fn_stub
<big_endian
>();
11667 symval
.set_output_value(fn_stub
->output_address());
11670 got_offset
= mips_sym
->global_gotoffset();
11671 update_got_entry
= true;
11675 case elfcpp::R_MIPS_64
:
11676 if (should_apply_static_reloc(mips_sym
, r_types
[i
], output_section
,
11678 reloc_status
= Reloc_funcs::rel64(view
, object
, psymval
, r_addend
,
11679 extract_addend
, calculate_only
,
11680 &calculated_value
, false);
11681 else if (target
->is_output_n64() && r_addend
!= 0)
11682 // Only apply the addend. The static relocation was RELA, but the
11683 // dynamic relocation is REL, so we need to apply the addend.
11684 reloc_status
= Reloc_funcs::rel64(view
, object
, psymval
, r_addend
,
11685 extract_addend
, calculate_only
,
11686 &calculated_value
, true);
11688 case elfcpp::R_MIPS_REL32
:
11689 gold_unreachable();
11691 case elfcpp::R_MIPS_PC32
:
11692 reloc_status
= Reloc_funcs::relpc32(view
, object
, psymval
, address
,
11693 r_addend
, extract_addend
,
11695 &calculated_value
);
11698 case elfcpp::R_MIPS16_26
:
11699 // The calculation for R_MIPS16_26 is just the same as for an
11700 // R_MIPS_26. It's only the storage of the relocated field into
11701 // the output file that's different. So, we just fall through to the
11702 // R_MIPS_26 case here.
11703 case elfcpp::R_MIPS_26
:
11704 case elfcpp::R_MICROMIPS_26_S1
:
11705 reloc_status
= Reloc_funcs::rel26(view
, object
, psymval
, address
,
11706 gsym
== NULL
, r_addend
, extract_addend
, gsym
, cross_mode_jump
,
11707 r_types
[i
], target
->jal_to_bal(), calculate_only
,
11708 &calculated_value
);
11711 case elfcpp::R_MIPS_HI16
:
11712 case elfcpp::R_MIPS16_HI16
:
11713 case elfcpp::R_MICROMIPS_HI16
:
11714 if (rel_type
== elfcpp::SHT_RELA
)
11715 reloc_status
= Reloc_funcs::do_relhi16(view
, object
, psymval
,
11717 gp_disp
, r_types
[i
],
11719 target
, calculate_only
,
11720 &calculated_value
);
11721 else if (rel_type
== elfcpp::SHT_REL
)
11722 reloc_status
= Reloc_funcs::relhi16(view
, object
, psymval
, r_addend
,
11723 address
, gp_disp
, r_types
[i
],
11724 r_sym
, extract_addend
);
11726 gold_unreachable();
11729 case elfcpp::R_MIPS_LO16
:
11730 case elfcpp::R_MIPS16_LO16
:
11731 case elfcpp::R_MICROMIPS_LO16
:
11732 case elfcpp::R_MICROMIPS_HI0_LO16
:
11733 reloc_status
= Reloc_funcs::rello16(target
, view
, object
, psymval
,
11734 r_addend
, extract_addend
, address
,
11735 gp_disp
, r_types
[i
], r_sym
,
11736 rel_type
, calculate_only
,
11737 &calculated_value
);
11740 case elfcpp::R_MIPS_LITERAL
:
11741 case elfcpp::R_MICROMIPS_LITERAL
:
11742 // Because we don't merge literal sections, we can handle this
11743 // just like R_MIPS_GPREL16. In the long run, we should merge
11744 // shared literals, and then we will need to additional work
11749 case elfcpp::R_MIPS_GPREL16
:
11750 case elfcpp::R_MIPS16_GPREL
:
11751 case elfcpp::R_MICROMIPS_GPREL7_S2
:
11752 case elfcpp::R_MICROMIPS_GPREL16
:
11753 reloc_status
= Reloc_funcs::relgprel(view
, object
, psymval
,
11754 target
->adjusted_gp_value(object
),
11755 r_addend
, extract_addend
,
11756 gsym
== NULL
, r_types
[i
],
11757 calculate_only
, &calculated_value
);
11760 case elfcpp::R_MIPS_PC16
:
11761 reloc_status
= Reloc_funcs::relpc16(view
, object
, psymval
, address
,
11762 r_addend
, extract_addend
,
11764 &calculated_value
);
11767 case elfcpp::R_MIPS_PC21_S2
:
11768 reloc_status
= Reloc_funcs::relpc21(view
, object
, psymval
, address
,
11769 r_addend
, extract_addend
,
11771 &calculated_value
);
11774 case elfcpp::R_MIPS_PC26_S2
:
11775 reloc_status
= Reloc_funcs::relpc26(view
, object
, psymval
, address
,
11776 r_addend
, extract_addend
,
11778 &calculated_value
);
11781 case elfcpp::R_MIPS_PC18_S3
:
11782 reloc_status
= Reloc_funcs::relpc18(view
, object
, psymval
, address
,
11783 r_addend
, extract_addend
,
11785 &calculated_value
);
11788 case elfcpp::R_MIPS_PC19_S2
:
11789 reloc_status
= Reloc_funcs::relpc19(view
, object
, psymval
, address
,
11790 r_addend
, extract_addend
,
11792 &calculated_value
);
11795 case elfcpp::R_MIPS_PCHI16
:
11796 if (rel_type
== elfcpp::SHT_RELA
)
11797 reloc_status
= Reloc_funcs::do_relpchi16(view
, object
, psymval
,
11801 &calculated_value
);
11802 else if (rel_type
== elfcpp::SHT_REL
)
11803 reloc_status
= Reloc_funcs::relpchi16(view
, object
, psymval
,
11804 r_addend
, address
, r_sym
,
11807 gold_unreachable();
11810 case elfcpp::R_MIPS_PCLO16
:
11811 reloc_status
= Reloc_funcs::relpclo16(view
, object
, psymval
, r_addend
,
11812 extract_addend
, address
, r_sym
,
11813 rel_type
, calculate_only
,
11814 &calculated_value
);
11816 case elfcpp::R_MICROMIPS_PC7_S1
:
11817 reloc_status
= Reloc_funcs::relmicromips_pc7_s1(view
, object
, psymval
,
11821 &calculated_value
);
11823 case elfcpp::R_MICROMIPS_PC10_S1
:
11824 reloc_status
= Reloc_funcs::relmicromips_pc10_s1(view
, object
,
11826 r_addend
, extract_addend
,
11828 &calculated_value
);
11830 case elfcpp::R_MICROMIPS_PC16_S1
:
11831 reloc_status
= Reloc_funcs::relmicromips_pc16_s1(view
, object
,
11833 r_addend
, extract_addend
,
11835 &calculated_value
);
11837 case elfcpp::R_MIPS_GPREL32
:
11838 reloc_status
= Reloc_funcs::relgprel32(view
, object
, psymval
,
11839 target
->adjusted_gp_value(object
),
11840 r_addend
, extract_addend
,
11842 &calculated_value
);
11844 case elfcpp::R_MIPS_GOT_HI16
:
11845 case elfcpp::R_MIPS_CALL_HI16
:
11846 case elfcpp::R_MICROMIPS_GOT_HI16
:
11847 case elfcpp::R_MICROMIPS_CALL_HI16
:
11849 got_offset
= target
->got_section()->got_offset(gsym
,
11853 got_offset
= target
->got_section()->got_offset(r_sym
,
11856 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11857 reloc_status
= Reloc_funcs::relgot_hi16(view
, gp_offset
,
11859 &calculated_value
);
11860 update_got_entry
= changed_symbol_value
;
11863 case elfcpp::R_MIPS_GOT_LO16
:
11864 case elfcpp::R_MIPS_CALL_LO16
:
11865 case elfcpp::R_MICROMIPS_GOT_LO16
:
11866 case elfcpp::R_MICROMIPS_CALL_LO16
:
11868 got_offset
= target
->got_section()->got_offset(gsym
,
11872 got_offset
= target
->got_section()->got_offset(r_sym
,
11875 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11876 reloc_status
= Reloc_funcs::relgot_lo16(view
, gp_offset
,
11878 &calculated_value
);
11879 update_got_entry
= changed_symbol_value
;
11882 case elfcpp::R_MIPS_GOT_DISP
:
11883 case elfcpp::R_MICROMIPS_GOT_DISP
:
11884 case elfcpp::R_MIPS_EH
:
11886 got_offset
= target
->got_section()->got_offset(gsym
,
11890 got_offset
= target
->got_section()->got_offset(r_sym
,
11893 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11894 if (eh_reloc(r_types
[i
]))
11895 reloc_status
= Reloc_funcs::releh(view
, gp_offset
,
11897 &calculated_value
);
11899 reloc_status
= Reloc_funcs::relgot(view
, gp_offset
,
11901 &calculated_value
);
11903 case elfcpp::R_MIPS_CALL16
:
11904 case elfcpp::R_MIPS16_CALL16
:
11905 case elfcpp::R_MICROMIPS_CALL16
:
11906 gold_assert(gsym
!= NULL
);
11907 got_offset
= target
->got_section()->got_offset(gsym
,
11910 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11911 reloc_status
= Reloc_funcs::relgot(view
, gp_offset
,
11912 calculate_only
, &calculated_value
);
11913 // TODO(sasa): We should also initialize update_got_entry
11914 // in other place swhere relgot is called.
11915 update_got_entry
= changed_symbol_value
;
11918 case elfcpp::R_MIPS_GOT16
:
11919 case elfcpp::R_MIPS16_GOT16
:
11920 case elfcpp::R_MICROMIPS_GOT16
:
11923 got_offset
= target
->got_section()->got_offset(gsym
,
11926 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11927 reloc_status
= Reloc_funcs::relgot(view
, gp_offset
,
11929 &calculated_value
);
11933 if (rel_type
== elfcpp::SHT_RELA
)
11934 reloc_status
= Reloc_funcs::do_relgot16_local(view
, object
,
11939 &calculated_value
);
11940 else if (rel_type
== elfcpp::SHT_REL
)
11941 reloc_status
= Reloc_funcs::relgot16_local(view
, object
,
11944 r_types
[i
], r_sym
);
11946 gold_unreachable();
11948 update_got_entry
= changed_symbol_value
;
11951 case elfcpp::R_MIPS_TLS_GD
:
11952 case elfcpp::R_MIPS16_TLS_GD
:
11953 case elfcpp::R_MICROMIPS_TLS_GD
:
11955 got_offset
= target
->got_section()->got_offset(gsym
,
11959 got_offset
= target
->got_section()->got_offset(r_sym
,
11962 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11963 reloc_status
= Reloc_funcs::relgot(view
, gp_offset
, calculate_only
,
11964 &calculated_value
);
11967 case elfcpp::R_MIPS_TLS_GOTTPREL
:
11968 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
11969 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
11971 got_offset
= target
->got_section()->got_offset(gsym
,
11972 GOT_TYPE_TLS_OFFSET
,
11975 got_offset
= target
->got_section()->got_offset(r_sym
,
11976 GOT_TYPE_TLS_OFFSET
,
11978 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11979 reloc_status
= Reloc_funcs::relgot(view
, gp_offset
, calculate_only
,
11980 &calculated_value
);
11983 case elfcpp::R_MIPS_TLS_LDM
:
11984 case elfcpp::R_MIPS16_TLS_LDM
:
11985 case elfcpp::R_MICROMIPS_TLS_LDM
:
11986 // Relocate the field with the offset of the GOT entry for
11987 // the module index.
11988 got_offset
= target
->got_section()->tls_ldm_offset(object
);
11989 gp_offset
= target
->got_section()->gp_offset(got_offset
, object
);
11990 reloc_status
= Reloc_funcs::relgot(view
, gp_offset
, calculate_only
,
11991 &calculated_value
);
11994 case elfcpp::R_MIPS_GOT_PAGE
:
11995 case elfcpp::R_MICROMIPS_GOT_PAGE
:
11996 reloc_status
= Reloc_funcs::relgotpage(target
, view
, object
, psymval
,
11997 r_addend
, extract_addend
,
11999 &calculated_value
);
12002 case elfcpp::R_MIPS_GOT_OFST
:
12003 case elfcpp::R_MICROMIPS_GOT_OFST
:
12004 reloc_status
= Reloc_funcs::relgotofst(target
, view
, object
, psymval
,
12005 r_addend
, extract_addend
,
12006 local
, calculate_only
,
12007 &calculated_value
);
12010 case elfcpp::R_MIPS_JALR
:
12011 case elfcpp::R_MICROMIPS_JALR
:
12012 // This relocation is only a hint. In some cases, we optimize
12013 // it into a bal instruction. But we don't try to optimize
12014 // when the symbol does not resolve locally.
12016 || symbol_calls_local(gsym
, gsym
->has_dynsym_index()))
12017 reloc_status
= Reloc_funcs::reljalr(view
, object
, psymval
, address
,
12018 r_addend
, extract_addend
,
12019 cross_mode_jump
, r_types
[i
],
12020 target
->jalr_to_bal(),
12023 &calculated_value
);
12026 case elfcpp::R_MIPS_TLS_DTPREL_HI16
:
12027 case elfcpp::R_MIPS16_TLS_DTPREL_HI16
:
12028 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16
:
12029 reloc_status
= Reloc_funcs::tlsrelhi16(view
, object
, psymval
,
12030 elfcpp::DTP_OFFSET
, r_addend
,
12031 extract_addend
, calculate_only
,
12032 &calculated_value
);
12034 case elfcpp::R_MIPS_TLS_DTPREL_LO16
:
12035 case elfcpp::R_MIPS16_TLS_DTPREL_LO16
:
12036 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16
:
12037 reloc_status
= Reloc_funcs::tlsrello16(view
, object
, psymval
,
12038 elfcpp::DTP_OFFSET
, r_addend
,
12039 extract_addend
, calculate_only
,
12040 &calculated_value
);
12042 case elfcpp::R_MIPS_TLS_DTPREL32
:
12043 case elfcpp::R_MIPS_TLS_DTPREL64
:
12044 reloc_status
= Reloc_funcs::tlsrel32(view
, object
, psymval
,
12045 elfcpp::DTP_OFFSET
, r_addend
,
12046 extract_addend
, calculate_only
,
12047 &calculated_value
);
12049 case elfcpp::R_MIPS_TLS_TPREL_HI16
:
12050 case elfcpp::R_MIPS16_TLS_TPREL_HI16
:
12051 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16
:
12052 reloc_status
= Reloc_funcs::tlsrelhi16(view
, object
, psymval
,
12053 elfcpp::TP_OFFSET
, r_addend
,
12054 extract_addend
, calculate_only
,
12055 &calculated_value
);
12057 case elfcpp::R_MIPS_TLS_TPREL_LO16
:
12058 case elfcpp::R_MIPS16_TLS_TPREL_LO16
:
12059 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16
:
12060 reloc_status
= Reloc_funcs::tlsrello16(view
, object
, psymval
,
12061 elfcpp::TP_OFFSET
, r_addend
,
12062 extract_addend
, calculate_only
,
12063 &calculated_value
);
12065 case elfcpp::R_MIPS_TLS_TPREL32
:
12066 case elfcpp::R_MIPS_TLS_TPREL64
:
12067 reloc_status
= Reloc_funcs::tlsrel32(view
, object
, psymval
,
12068 elfcpp::TP_OFFSET
, r_addend
,
12069 extract_addend
, calculate_only
,
12070 &calculated_value
);
12072 case elfcpp::R_MIPS_SUB
:
12073 case elfcpp::R_MICROMIPS_SUB
:
12074 reloc_status
= Reloc_funcs::relsub(view
, object
, psymval
, r_addend
,
12076 calculate_only
, &calculated_value
);
12079 gold_error_at_location(relinfo
, relnum
, r_offset
,
12080 _("unsupported reloc %u"), r_types
[i
]);
12084 if (update_got_entry
)
12086 Mips_output_data_got
<size
, big_endian
>* got
= target
->got_section();
12087 if (mips_sym
!= NULL
&& mips_sym
->get_applied_secondary_got_fixup())
12088 got
->update_got_entry(got
->get_primary_got_offset(mips_sym
),
12089 psymval
->value(object
, 0));
12091 got
->update_got_entry(got_offset
, psymval
->value(object
, 0));
12094 r_addend
= calculated_value
;
12097 bool jal_shuffle
= jal_reloc(r_type
) ? !parameters
->options().relocatable()
12099 Reloc_funcs::mips_reloc_shuffle(view
, r_type
, jal_shuffle
);
12101 // Report any errors.
12102 switch (reloc_status
)
12104 case Reloc_funcs::STATUS_OKAY
:
12106 case Reloc_funcs::STATUS_OVERFLOW
:
12107 gold_error_at_location(relinfo
, relnum
, r_offset
,
12108 _("relocation overflow"));
12110 case Reloc_funcs::STATUS_BAD_RELOC
:
12111 gold_error_at_location(relinfo
, relnum
, r_offset
,
12112 _("unexpected opcode while processing relocation"));
12114 case Reloc_funcs::STATUS_PCREL_UNALIGNED
:
12115 gold_error_at_location(relinfo
, relnum
, r_offset
,
12116 _("unaligned PC-relative relocation"));
12119 gold_unreachable();
12125 // Get the Reference_flags for a particular relocation.
12127 template<int size
, bool big_endian
>
12129 Target_mips
<size
, big_endian
>::Scan::get_reference_flags(
12130 unsigned int r_type
)
12134 case elfcpp::R_MIPS_NONE
:
12135 // No symbol reference.
12138 case elfcpp::R_MIPS_16
:
12139 case elfcpp::R_MIPS_32
:
12140 case elfcpp::R_MIPS_64
:
12141 case elfcpp::R_MIPS_HI16
:
12142 case elfcpp::R_MIPS_LO16
:
12143 case elfcpp::R_MIPS16_HI16
:
12144 case elfcpp::R_MIPS16_LO16
:
12145 case elfcpp::R_MICROMIPS_HI16
:
12146 case elfcpp::R_MICROMIPS_LO16
:
12147 return Symbol::ABSOLUTE_REF
;
12149 case elfcpp::R_MIPS_26
:
12150 case elfcpp::R_MIPS16_26
:
12151 case elfcpp::R_MICROMIPS_26_S1
:
12152 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
12154 case elfcpp::R_MIPS_PC18_S3
:
12155 case elfcpp::R_MIPS_PC19_S2
:
12156 case elfcpp::R_MIPS_PCHI16
:
12157 case elfcpp::R_MIPS_PCLO16
:
12158 case elfcpp::R_MIPS_GPREL32
:
12159 case elfcpp::R_MIPS_GPREL16
:
12160 case elfcpp::R_MIPS_REL32
:
12161 case elfcpp::R_MIPS16_GPREL
:
12162 return Symbol::RELATIVE_REF
;
12164 case elfcpp::R_MIPS_PC16
:
12165 case elfcpp::R_MIPS_PC32
:
12166 case elfcpp::R_MIPS_PC21_S2
:
12167 case elfcpp::R_MIPS_PC26_S2
:
12168 case elfcpp::R_MIPS_JALR
:
12169 case elfcpp::R_MICROMIPS_JALR
:
12170 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
12172 case elfcpp::R_MIPS_GOT16
:
12173 case elfcpp::R_MIPS_CALL16
:
12174 case elfcpp::R_MIPS_GOT_DISP
:
12175 case elfcpp::R_MIPS_GOT_HI16
:
12176 case elfcpp::R_MIPS_GOT_LO16
:
12177 case elfcpp::R_MIPS_CALL_HI16
:
12178 case elfcpp::R_MIPS_CALL_LO16
:
12179 case elfcpp::R_MIPS_LITERAL
:
12180 case elfcpp::R_MIPS_GOT_PAGE
:
12181 case elfcpp::R_MIPS_GOT_OFST
:
12182 case elfcpp::R_MIPS16_GOT16
:
12183 case elfcpp::R_MIPS16_CALL16
:
12184 case elfcpp::R_MICROMIPS_GOT16
:
12185 case elfcpp::R_MICROMIPS_CALL16
:
12186 case elfcpp::R_MICROMIPS_GOT_HI16
:
12187 case elfcpp::R_MICROMIPS_GOT_LO16
:
12188 case elfcpp::R_MICROMIPS_CALL_HI16
:
12189 case elfcpp::R_MICROMIPS_CALL_LO16
:
12190 case elfcpp::R_MIPS_EH
:
12191 // Absolute in GOT.
12192 return Symbol::RELATIVE_REF
;
12194 case elfcpp::R_MIPS_TLS_DTPMOD32
:
12195 case elfcpp::R_MIPS_TLS_DTPREL32
:
12196 case elfcpp::R_MIPS_TLS_DTPMOD64
:
12197 case elfcpp::R_MIPS_TLS_DTPREL64
:
12198 case elfcpp::R_MIPS_TLS_GD
:
12199 case elfcpp::R_MIPS_TLS_LDM
:
12200 case elfcpp::R_MIPS_TLS_DTPREL_HI16
:
12201 case elfcpp::R_MIPS_TLS_DTPREL_LO16
:
12202 case elfcpp::R_MIPS_TLS_GOTTPREL
:
12203 case elfcpp::R_MIPS_TLS_TPREL32
:
12204 case elfcpp::R_MIPS_TLS_TPREL64
:
12205 case elfcpp::R_MIPS_TLS_TPREL_HI16
:
12206 case elfcpp::R_MIPS_TLS_TPREL_LO16
:
12207 case elfcpp::R_MIPS16_TLS_GD
:
12208 case elfcpp::R_MIPS16_TLS_GOTTPREL
:
12209 case elfcpp::R_MICROMIPS_TLS_GD
:
12210 case elfcpp::R_MICROMIPS_TLS_GOTTPREL
:
12211 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16
:
12212 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16
:
12213 return Symbol::TLS_REF
;
12215 case elfcpp::R_MIPS_COPY
:
12216 case elfcpp::R_MIPS_JUMP_SLOT
:
12218 gold_unreachable();
12219 // Not expected. We will give an error later.
12224 // Report an unsupported relocation against a local symbol.
12226 template<int size
, bool big_endian
>
12228 Target_mips
<size
, big_endian
>::Scan::unsupported_reloc_local(
12229 Sized_relobj_file
<size
, big_endian
>* object
,
12230 unsigned int r_type
)
12232 gold_error(_("%s: unsupported reloc %u against local symbol"),
12233 object
->name().c_str(), r_type
);
12236 // Report an unsupported relocation against a global symbol.
12238 template<int size
, bool big_endian
>
12240 Target_mips
<size
, big_endian
>::Scan::unsupported_reloc_global(
12241 Sized_relobj_file
<size
, big_endian
>* object
,
12242 unsigned int r_type
,
12245 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12246 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
12249 // Return printable name for ABI.
12250 template<int size
, bool big_endian
>
12252 Target_mips
<size
, big_endian
>::elf_mips_abi_name(elfcpp::Elf_Word e_flags
)
12254 switch (e_flags
& elfcpp::EF_MIPS_ABI
)
12257 if ((e_flags
& elfcpp::EF_MIPS_ABI2
) != 0)
12259 else if (size
== 64)
12263 case elfcpp::E_MIPS_ABI_O32
:
12265 case elfcpp::E_MIPS_ABI_O64
:
12267 case elfcpp::E_MIPS_ABI_EABI32
:
12269 case elfcpp::E_MIPS_ABI_EABI64
:
12272 return "unknown abi";
12276 template<int size
, bool big_endian
>
12278 Target_mips
<size
, big_endian
>::elf_mips_mach_name(elfcpp::Elf_Word e_flags
)
12280 switch (e_flags
& elfcpp::EF_MIPS_MACH
)
12282 case elfcpp::E_MIPS_MACH_3900
:
12283 return "mips:3900";
12284 case elfcpp::E_MIPS_MACH_4010
:
12285 return "mips:4010";
12286 case elfcpp::E_MIPS_MACH_4100
:
12287 return "mips:4100";
12288 case elfcpp::E_MIPS_MACH_4111
:
12289 return "mips:4111";
12290 case elfcpp::E_MIPS_MACH_4120
:
12291 return "mips:4120";
12292 case elfcpp::E_MIPS_MACH_4650
:
12293 return "mips:4650";
12294 case elfcpp::E_MIPS_MACH_5400
:
12295 return "mips:5400";
12296 case elfcpp::E_MIPS_MACH_5500
:
12297 return "mips:5500";
12298 case elfcpp::E_MIPS_MACH_5900
:
12299 return "mips:5900";
12300 case elfcpp::E_MIPS_MACH_SB1
:
12302 case elfcpp::E_MIPS_MACH_9000
:
12303 return "mips:9000";
12304 case elfcpp::E_MIPS_MACH_LS2E
:
12305 return "mips:loongson_2e";
12306 case elfcpp::E_MIPS_MACH_LS2F
:
12307 return "mips:loongson_2f";
12308 case elfcpp::E_MIPS_MACH_LS3A
:
12309 return "mips:loongson_3a";
12310 case elfcpp::E_MIPS_MACH_OCTEON
:
12311 return "mips:octeon";
12312 case elfcpp::E_MIPS_MACH_OCTEON2
:
12313 return "mips:octeon2";
12314 case elfcpp::E_MIPS_MACH_OCTEON3
:
12315 return "mips:octeon3";
12316 case elfcpp::E_MIPS_MACH_XLR
:
12319 switch (e_flags
& elfcpp::EF_MIPS_ARCH
)
12322 case elfcpp::E_MIPS_ARCH_1
:
12323 return "mips:3000";
12325 case elfcpp::E_MIPS_ARCH_2
:
12326 return "mips:6000";
12328 case elfcpp::E_MIPS_ARCH_3
:
12329 return "mips:4000";
12331 case elfcpp::E_MIPS_ARCH_4
:
12332 return "mips:8000";
12334 case elfcpp::E_MIPS_ARCH_5
:
12335 return "mips:mips5";
12337 case elfcpp::E_MIPS_ARCH_32
:
12338 return "mips:isa32";
12340 case elfcpp::E_MIPS_ARCH_64
:
12341 return "mips:isa64";
12343 case elfcpp::E_MIPS_ARCH_32R2
:
12344 return "mips:isa32r2";
12346 case elfcpp::E_MIPS_ARCH_32R6
:
12347 return "mips:isa32r6";
12349 case elfcpp::E_MIPS_ARCH_64R2
:
12350 return "mips:isa64r2";
12352 case elfcpp::E_MIPS_ARCH_64R6
:
12353 return "mips:isa64r6";
12356 return "unknown CPU";
12359 template<int size
, bool big_endian
>
12360 const Target::Target_info Target_mips
<size
, big_endian
>::mips_info
=
12363 big_endian
, // is_big_endian
12364 elfcpp::EM_MIPS
, // machine_code
12365 true, // has_make_symbol
12366 false, // has_resolve
12367 false, // has_code_fill
12368 true, // is_default_stack_executable
12369 false, // can_icf_inline_merge_sections
12371 size
== 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
12372 0x400000, // default_text_segment_address
12373 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12374 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12375 false, // isolate_execinstr
12376 0, // rosegment_gap
12377 elfcpp::SHN_UNDEF
, // small_common_shndx
12378 elfcpp::SHN_UNDEF
, // large_common_shndx
12379 0, // small_common_section_flags
12380 0, // large_common_section_flags
12381 NULL
, // attributes_section
12382 NULL
, // attributes_vendor
12383 "__start", // entry_symbol_name
12384 32, // hash_entry_size
12387 template<int size
, bool big_endian
>
12388 class Target_mips_nacl
: public Target_mips
<size
, big_endian
>
12392 : Target_mips
<size
, big_endian
>(&mips_nacl_info
)
12396 static const Target::Target_info mips_nacl_info
;
12399 template<int size
, bool big_endian
>
12400 const Target::Target_info Target_mips_nacl
<size
, big_endian
>::mips_nacl_info
=
12403 big_endian
, // is_big_endian
12404 elfcpp::EM_MIPS
, // machine_code
12405 true, // has_make_symbol
12406 false, // has_resolve
12407 false, // has_code_fill
12408 true, // is_default_stack_executable
12409 false, // can_icf_inline_merge_sections
12411 "/lib/ld.so.1", // dynamic_linker
12412 0x20000, // default_text_segment_address
12413 0x10000, // abi_pagesize (overridable by -z max-page-size)
12414 0x10000, // common_pagesize (overridable by -z common-page-size)
12415 true, // isolate_execinstr
12416 0x10000000, // rosegment_gap
12417 elfcpp::SHN_UNDEF
, // small_common_shndx
12418 elfcpp::SHN_UNDEF
, // large_common_shndx
12419 0, // small_common_section_flags
12420 0, // large_common_section_flags
12421 NULL
, // attributes_section
12422 NULL
, // attributes_vendor
12423 "_start", // entry_symbol_name
12424 32, // hash_entry_size
12427 // Target selector for Mips. Note this is never instantiated directly.
12428 // It's only used in Target_selector_mips_nacl, below.
12430 template<int size
, bool big_endian
>
12431 class Target_selector_mips
: public Target_selector
12434 Target_selector_mips()
12435 : Target_selector(elfcpp::EM_MIPS
, size
, big_endian
,
12437 (big_endian
? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12438 (big_endian
? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12440 (big_endian
? "elf64btsmip" : "elf64ltsmip") :
12441 (big_endian
? "elf32btsmip" : "elf32ltsmip")))
12444 Target
* do_instantiate_target()
12445 { return new Target_mips
<size
, big_endian
>(); }
12448 template<int size
, bool big_endian
>
12449 class Target_selector_mips_nacl
12450 : public Target_selector_nacl
<Target_selector_mips
<size
, big_endian
>,
12451 Target_mips_nacl
<size
, big_endian
> >
12454 Target_selector_mips_nacl()
12455 : Target_selector_nacl
<Target_selector_mips
<size
, big_endian
>,
12456 Target_mips_nacl
<size
, big_endian
> >(
12457 // NaCl currently supports only MIPS32 little-endian.
12458 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12462 Target_selector_mips_nacl
<32, true> target_selector_mips32
;
12463 Target_selector_mips_nacl
<32, false> target_selector_mips32el
;
12464 Target_selector_mips_nacl
<64, true> target_selector_mips64
;
12465 Target_selector_mips_nacl
<64, false> target_selector_mips64el
;
12467 } // End anonymous namespace.