Add missing Mips_output_data_la25_stub::do_print_to_mapfile.
[deliverable/binutils-gdb.git] / gold / mips.cc
CommitLineData
9810d34d
SS
1// mips.cc -- mips target support for gold.
2
6f2750fe 3// Copyright (C) 2011-2016 Free Software Foundation, Inc.
9810d34d
SS
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.
7
8// This file is part of gold.
9
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.
14
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.
19
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.
24
25#include "gold.h"
26
27#include <algorithm>
28#include <set>
29#include <sstream>
30#include "demangle.h"
31
32#include "elfcpp.h"
33#include "parameters.h"
34#include "reloc.h"
35#include "mips.h"
36#include "object.h"
37#include "symtab.h"
38#include "layout.h"
39#include "output.h"
40#include "copy-relocs.h"
41#include "target.h"
42#include "target-reloc.h"
43#include "target-select.h"
44#include "tls.h"
45#include "errors.h"
46#include "gc.h"
62661c93 47#include "nacl.h"
9810d34d
SS
48
49namespace
50{
51using namespace gold;
52
53template<int size, bool big_endian>
54class Mips_output_data_plt;
55
56template<int size, bool big_endian>
57class Mips_output_data_got;
58
59template<int size, bool big_endian>
60class Target_mips;
61
62template<int size, bool big_endian>
63class Mips_output_section_reginfo;
64
65template<int size, bool big_endian>
66class Mips_output_data_la25_stub;
67
68template<int size, bool big_endian>
69class Mips_output_data_mips_stubs;
70
71template<int size>
72class Mips_symbol;
73
74template<int size, bool big_endian>
75class Mips_got_info;
76
77template<int size, bool big_endian>
78class Mips_relobj;
79
80class Mips16_stub_section_base;
81
82template<int size, bool big_endian>
83class Mips16_stub_section;
84
85// The ABI says that every symbol used by dynamic relocations must have
86// a global GOT entry. Among other things, this provides the dynamic
87// linker with a free, directly-indexed cache. The GOT can therefore
88// contain symbols that are not referenced by GOT relocations themselves
89// (in other words, it may have symbols that are not referenced by things
90// like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
91
92// GOT relocations are less likely to overflow if we put the associated
93// GOT entries towards the beginning. We therefore divide the global
94// GOT entries into two areas: "normal" and "reloc-only". Entries in
95// the first area can be used for both dynamic relocations and GP-relative
96// accesses, while those in the "reloc-only" area are for dynamic
97// relocations only.
98
99// These GGA_* ("Global GOT Area") values are organised so that lower
100// values are more general than higher values. Also, non-GGA_NONE
101// values are ordered by the position of the area in the GOT.
102
103enum Global_got_area
104{
105 GGA_NORMAL = 0,
106 GGA_RELOC_ONLY = 1,
107 GGA_NONE = 2
108};
109
110// The types of GOT entries needed for this platform.
111// These values are exposed to the ABI in an incremental link.
112// Do not renumber existing values without changing the version
113// number of the .gnu_incremental_inputs section.
114enum Got_type
115{
116 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
117 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
118 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
119
120 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
121 GOT_TYPE_STANDARD_MULTIGOT = 3,
122 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
123 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
124};
125
126// TLS type of GOT entry.
127enum Got_tls_type
128{
129 GOT_TLS_NONE = 0,
130 GOT_TLS_GD = 1,
131 GOT_TLS_LDM = 2,
132 GOT_TLS_IE = 4
133};
134
135// Return TRUE if a relocation of type R_TYPE from OBJECT might
136// require an la25 stub. See also local_pic_function, which determines
137// whether the destination function ever requires a stub.
138template<int size, bool big_endian>
139static inline bool
140relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
141 unsigned int r_type, bool target_is_16_bit_code)
142{
143 // We specifically ignore branches and jumps from EF_PIC objects,
144 // where the onus is on the compiler or programmer to perform any
145 // necessary initialization of $25. Sometimes such initialization
146 // is unnecessary; for example, -mno-shared functions do not use
147 // the incoming value of $25, and may therefore be called directly.
148 if (object->is_pic())
149 return false;
150
151 switch (r_type)
152 {
153 case elfcpp::R_MIPS_26:
154 case elfcpp::R_MIPS_PC16:
155 case elfcpp::R_MICROMIPS_26_S1:
156 case elfcpp::R_MICROMIPS_PC7_S1:
157 case elfcpp::R_MICROMIPS_PC10_S1:
158 case elfcpp::R_MICROMIPS_PC16_S1:
159 case elfcpp::R_MICROMIPS_PC23_S2:
160 return true;
161
162 case elfcpp::R_MIPS16_26:
163 return !target_is_16_bit_code;
164
165 default:
166 return false;
167 }
168}
169
170// Return true if SYM is a locally-defined PIC function, in the sense
171// that it or its fn_stub might need $25 to be valid on entry.
172// Note that MIPS16 functions set up $gp using PC-relative instructions,
173// so they themselves never need $25 to be valid. Only non-MIPS16
174// entry points are of interest here.
175template<int size, bool big_endian>
176static inline bool
177local_pic_function(Mips_symbol<size>* sym)
178{
179 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
180 && !sym->object()->is_dynamic()
181 && !sym->is_undefined());
182
183 if (sym->is_defined() && def_regular)
184 {
185 Mips_relobj<size, big_endian>* object =
186 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
187
188 if ((object->is_pic() || sym->is_pic())
189 && (!sym->is_mips16()
190 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
191 return true;
192 }
193 return false;
194}
195
196static inline bool
197hi16_reloc(int r_type)
198{
199 return (r_type == elfcpp::R_MIPS_HI16
200 || r_type == elfcpp::R_MIPS16_HI16
201 || r_type == elfcpp::R_MICROMIPS_HI16);
202}
203
204static inline bool
205lo16_reloc(int r_type)
206{
207 return (r_type == elfcpp::R_MIPS_LO16
208 || r_type == elfcpp::R_MIPS16_LO16
209 || r_type == elfcpp::R_MICROMIPS_LO16);
210}
211
212static inline bool
213got16_reloc(unsigned int r_type)
214{
215 return (r_type == elfcpp::R_MIPS_GOT16
216 || r_type == elfcpp::R_MIPS16_GOT16
217 || r_type == elfcpp::R_MICROMIPS_GOT16);
218}
219
220static inline bool
221call_lo16_reloc(unsigned int r_type)
222{
223 return (r_type == elfcpp::R_MIPS_CALL_LO16
224 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
225}
226
227static inline bool
228got_lo16_reloc(unsigned int r_type)
229{
230 return (r_type == elfcpp::R_MIPS_GOT_LO16
231 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
232}
233
234static inline bool
235got_disp_reloc(unsigned int r_type)
236{
237 return (r_type == elfcpp::R_MIPS_GOT_DISP
238 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
239}
240
241static inline bool
242got_page_reloc(unsigned int r_type)
243{
244 return (r_type == elfcpp::R_MIPS_GOT_PAGE
245 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
246}
247
248static inline bool
249tls_gd_reloc(unsigned int r_type)
250{
251 return (r_type == elfcpp::R_MIPS_TLS_GD
252 || r_type == elfcpp::R_MIPS16_TLS_GD
253 || r_type == elfcpp::R_MICROMIPS_TLS_GD);
254}
255
256static inline bool
257tls_gottprel_reloc(unsigned int r_type)
258{
259 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
260 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
261 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
262}
263
264static inline bool
265tls_ldm_reloc(unsigned int r_type)
266{
267 return (r_type == elfcpp::R_MIPS_TLS_LDM
268 || r_type == elfcpp::R_MIPS16_TLS_LDM
269 || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
270}
271
272static inline bool
273mips16_call_reloc(unsigned int r_type)
274{
275 return (r_type == elfcpp::R_MIPS16_26
276 || r_type == elfcpp::R_MIPS16_CALL16);
277}
278
279static inline bool
280jal_reloc(unsigned int r_type)
281{
282 return (r_type == elfcpp::R_MIPS_26
283 || r_type == elfcpp::R_MIPS16_26
284 || r_type == elfcpp::R_MICROMIPS_26_S1);
285}
286
287static inline bool
288micromips_branch_reloc(unsigned int r_type)
289{
290 return (r_type == elfcpp::R_MICROMIPS_26_S1
291 || r_type == elfcpp::R_MICROMIPS_PC16_S1
292 || r_type == elfcpp::R_MICROMIPS_PC10_S1
293 || r_type == elfcpp::R_MICROMIPS_PC7_S1);
294}
295
296// Check if R_TYPE is a MIPS16 reloc.
297static inline bool
298mips16_reloc(unsigned int r_type)
299{
300 switch (r_type)
301 {
302 case elfcpp::R_MIPS16_26:
303 case elfcpp::R_MIPS16_GPREL:
304 case elfcpp::R_MIPS16_GOT16:
305 case elfcpp::R_MIPS16_CALL16:
306 case elfcpp::R_MIPS16_HI16:
307 case elfcpp::R_MIPS16_LO16:
308 case elfcpp::R_MIPS16_TLS_GD:
309 case elfcpp::R_MIPS16_TLS_LDM:
310 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
311 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
312 case elfcpp::R_MIPS16_TLS_GOTTPREL:
313 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
314 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
315 return true;
316
317 default:
318 return false;
319 }
320}
321
322// Check if R_TYPE is a microMIPS reloc.
323static inline bool
324micromips_reloc(unsigned int r_type)
325{
326 switch (r_type)
327 {
328 case elfcpp::R_MICROMIPS_26_S1:
329 case elfcpp::R_MICROMIPS_HI16:
330 case elfcpp::R_MICROMIPS_LO16:
331 case elfcpp::R_MICROMIPS_GPREL16:
332 case elfcpp::R_MICROMIPS_LITERAL:
333 case elfcpp::R_MICROMIPS_GOT16:
334 case elfcpp::R_MICROMIPS_PC7_S1:
335 case elfcpp::R_MICROMIPS_PC10_S1:
336 case elfcpp::R_MICROMIPS_PC16_S1:
337 case elfcpp::R_MICROMIPS_CALL16:
338 case elfcpp::R_MICROMIPS_GOT_DISP:
339 case elfcpp::R_MICROMIPS_GOT_PAGE:
340 case elfcpp::R_MICROMIPS_GOT_OFST:
341 case elfcpp::R_MICROMIPS_GOT_HI16:
342 case elfcpp::R_MICROMIPS_GOT_LO16:
343 case elfcpp::R_MICROMIPS_SUB:
344 case elfcpp::R_MICROMIPS_HIGHER:
345 case elfcpp::R_MICROMIPS_HIGHEST:
346 case elfcpp::R_MICROMIPS_CALL_HI16:
347 case elfcpp::R_MICROMIPS_CALL_LO16:
348 case elfcpp::R_MICROMIPS_SCN_DISP:
349 case elfcpp::R_MICROMIPS_JALR:
350 case elfcpp::R_MICROMIPS_HI0_LO16:
351 case elfcpp::R_MICROMIPS_TLS_GD:
352 case elfcpp::R_MICROMIPS_TLS_LDM:
353 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
354 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
355 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
356 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
357 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
358 case elfcpp::R_MICROMIPS_GPREL7_S2:
359 case elfcpp::R_MICROMIPS_PC23_S2:
360 return true;
361
362 default:
363 return false;
364 }
365}
366
367static inline bool
368is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
369{
370 switch (high_reloc)
371 {
372 case elfcpp::R_MIPS_HI16:
373 case elfcpp::R_MIPS_GOT16:
374 return lo16_reloc == elfcpp::R_MIPS_LO16;
375 case elfcpp::R_MIPS16_HI16:
376 case elfcpp::R_MIPS16_GOT16:
377 return lo16_reloc == elfcpp::R_MIPS16_LO16;
378 case elfcpp::R_MICROMIPS_HI16:
379 case elfcpp::R_MICROMIPS_GOT16:
380 return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
381 default:
382 return false;
383 }
384}
385
386// This class is used to hold information about one GOT entry.
387// There are three types of entry:
388//
389// (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
390// (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
391// (2) a SYMBOL address, where SYMBOL is not local to an input object
392// (object != NULL, symndx == -1)
393// (3) a TLS LDM slot
394// (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
395
396template<int size, bool big_endian>
397class Mips_got_entry
398{
399 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
400
401 public:
402 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
403 Mips_address addend, unsigned char tls_type,
404 unsigned int shndx)
405 : object_(object), symndx_(symndx), tls_type_(tls_type), shndx_(shndx)
406 { this->d.addend = addend; }
407
408 Mips_got_entry(Mips_relobj<size, big_endian>* object, Mips_symbol<size>* sym,
409 unsigned char tls_type)
410 : object_(object), symndx_(-1U), tls_type_(tls_type), shndx_(-1U)
411 { this->d.sym = sym; }
412
413 // Return whether this entry is for a local symbol.
414 bool
415 is_for_local_symbol() const
416 { return this->symndx_ != -1U; }
417
418 // Return whether this entry is for a global symbol.
419 bool
420 is_for_global_symbol() const
421 { return this->symndx_ == -1U; }
422
423 // Return the hash of this entry.
424 size_t
425 hash() const
426 {
427 if (this->tls_type_ == GOT_TLS_LDM)
428 return this->symndx_ + (1 << 18);
429 if (this->symndx_ != -1U)
430 {
431 uintptr_t object_id = reinterpret_cast<uintptr_t>(this->object());
432 return this->symndx_ + object_id + this->d.addend;
433 }
434 else
435 {
436 uintptr_t sym_id = reinterpret_cast<uintptr_t>(this->d.sym);
437 return this->symndx_ + sym_id;
438 }
439 }
440
441 // Return whether this entry is equal to OTHER.
442 bool
443 equals(Mips_got_entry<size, big_endian>* other) const
444 {
445 if (this->symndx_ != other->symndx_
446 || this->tls_type_ != other->tls_type_)
447 return false;
448 if (this->tls_type_ == GOT_TLS_LDM)
449 return true;
450 if (this->symndx_ != -1U)
451 return (this->object() == other->object()
452 && this->d.addend == other->d.addend);
453 else
454 return this->d.sym == other->d.sym;
455 }
456
457 // Return input object that needs this GOT entry.
458 Mips_relobj<size, big_endian>*
459 object() const
460 {
461 gold_assert(this->object_ != NULL);
462 return this->object_;
463 }
464
465 // Return local symbol index for local GOT entries.
466 unsigned int
467 symndx() const
468 {
469 gold_assert(this->symndx_ != -1U);
470 return this->symndx_;
471 }
472
473 // Return the relocation addend for local GOT entries.
474 Mips_address
475 addend() const
476 {
477 gold_assert(this->symndx_ != -1U);
478 return this->d.addend;
479 }
480
481 // Return global symbol for global GOT entries.
482 Mips_symbol<size>*
483 sym() const
484 {
485 gold_assert(this->symndx_ == -1U);
486 return this->d.sym;
487 }
488
489 // Return whether this is a TLS GOT entry.
490 bool
491 is_tls_entry() const
492 { return this->tls_type_ != GOT_TLS_NONE; }
493
494 // Return TLS type of this GOT entry.
495 unsigned char
496 tls_type() const
497 { return this->tls_type_; }
498
499 // Return section index of the local symbol for local GOT entries.
500 unsigned int
501 shndx() const
502 { return this->shndx_; }
503
504 private:
505 // The input object that needs the GOT entry.
506 Mips_relobj<size, big_endian>* object_;
507 // The index of the symbol if we have a local symbol; -1 otherwise.
508 unsigned int symndx_;
509
510 union
511 {
512 // If symndx != -1, the addend of the relocation that should be added to the
513 // symbol value.
514 Mips_address addend;
515 // If symndx == -1, the global symbol corresponding to this GOT entry. The
516 // symbol's entry is in the local area if mips_sym->global_got_area is
517 // GGA_NONE, otherwise it is in the global area.
518 Mips_symbol<size>* sym;
519 } d;
520
521 // The TLS type of this GOT entry. An LDM GOT entry will be a local
522 // symbol entry with r_symndx == 0.
523 unsigned char tls_type_;
524
525 // For local GOT entries, section index of the local symbol.
526 unsigned int shndx_;
527};
528
529// Hash for Mips_got_entry.
530
531template<int size, bool big_endian>
532class Mips_got_entry_hash
533{
534 public:
535 size_t
536 operator()(Mips_got_entry<size, big_endian>* entry) const
537 { return entry->hash(); }
538};
539
540// Equality for Mips_got_entry.
541
542template<int size, bool big_endian>
543class Mips_got_entry_eq
544{
545 public:
546 bool
547 operator()(Mips_got_entry<size, big_endian>* e1,
548 Mips_got_entry<size, big_endian>* e2) const
549 { return e1->equals(e2); }
550};
551
552// Got_page_range. This class describes a range of addends: [MIN_ADDEND,
553// MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
554// increasing MIN_ADDEND.
555
556struct Got_page_range
557{
558 Got_page_range()
559 : next(NULL), min_addend(0), max_addend(0)
560 { }
561
562 Got_page_range* next;
563 int min_addend;
564 int max_addend;
565
566 // Return the maximum number of GOT page entries required.
567 int
568 get_max_pages()
569 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
570};
571
572// Got_page_entry. This class describes the range of addends that are applied
573// to page relocations against a given symbol.
574
575struct Got_page_entry
576{
577 Got_page_entry()
578 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
579 { }
580
581 Got_page_entry(Object* object_, unsigned int symndx_)
582 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
583 { }
584
585 // The input object that needs the GOT page entry.
586 Object* object;
587 // The index of the symbol, as stored in the relocation r_info.
588 unsigned int symndx;
589 // The ranges for this page entry.
590 Got_page_range* ranges;
591 // The maximum number of page entries needed for RANGES.
592 unsigned int num_pages;
593};
594
595// Hash for Got_page_entry.
596
597struct Got_page_entry_hash
598{
599 size_t
600 operator()(Got_page_entry* entry) const
601 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
602};
603
604// Equality for Got_page_entry.
605
606struct Got_page_entry_eq
607{
608 bool
609 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
610 {
611 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
612 }
613};
614
615// This class is used to hold .got information when linking.
616
617template<int size, bool big_endian>
618class Mips_got_info
619{
620 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
621 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
622 Reloc_section;
623 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
624
625 // Unordered set of GOT entries.
626 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
627 Mips_got_entry_hash<size, big_endian>,
628 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
629
630 // Unordered set of GOT page entries.
631 typedef Unordered_set<Got_page_entry*,
632 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
633
634 public:
635 Mips_got_info()
636 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
637 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
638 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
639 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
640 offset_(0)
641 { }
642
643 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
644 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
645 void
646 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
647 unsigned int symndx, Mips_address addend,
648 unsigned int r_type, unsigned int shndx);
649
650 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
651 // in OBJECT. FOR_CALL is true if the caller is only interested in
652 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
653 // relocation.
654 void
655 record_global_got_symbol(Mips_symbol<size>* mips_sym,
656 Mips_relobj<size, big_endian>* object,
657 unsigned int r_type, bool dyn_reloc, bool for_call);
658
659 // Add ENTRY to master GOT and to OBJECT's GOT.
660 void
661 record_got_entry(Mips_got_entry<size, big_endian>* entry,
662 Mips_relobj<size, big_endian>* object);
663
664 // Record that OBJECT has a page relocation against symbol SYMNDX and
665 // that ADDEND is the addend for that relocation.
666 void
667 record_got_page_entry(Mips_relobj<size, big_endian>* object,
668 unsigned int symndx, int addend);
669
670 // Create all entries that should be in the local part of the GOT.
671 void
672 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
673
674 // Create GOT page entries.
675 void
676 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
677
678 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
679 void
680 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
681 unsigned int non_reloc_only_global_gotno);
682
683 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
684 void
685 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
686
687 // Create TLS GOT entries.
688 void
689 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
690
691 // Decide whether the symbol needs an entry in the global part of the primary
692 // GOT, setting global_got_area accordingly. Count the number of global
693 // symbols that are in the primary GOT only because they have dynamic
694 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
695 void
696 count_got_symbols(Symbol_table* symtab);
697
698 // Return the offset of GOT page entry for VALUE.
699 unsigned int
700 get_got_page_offset(Mips_address value,
701 Mips_output_data_got<size, big_endian>* got);
702
703 // Count the number of GOT entries required.
704 void
705 count_got_entries();
706
707 // Count the number of GOT entries required by ENTRY. Accumulate the result.
708 void
709 count_got_entry(Mips_got_entry<size, big_endian>* entry);
710
711 // Add FROM's GOT entries.
712 void
713 add_got_entries(Mips_got_info<size, big_endian>* from);
714
715 // Add FROM's GOT page entries.
716 void
717 add_got_page_entries(Mips_got_info<size, big_endian>* from);
718
719 // Return GOT size.
720 unsigned int
721 got_size() const
722 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
723 + this->tls_gotno_) * size/8);
724 }
725
726 // Return the number of local GOT entries.
727 unsigned int
728 local_gotno() const
729 { return this->local_gotno_; }
730
731 // Return the maximum number of page GOT entries needed.
732 unsigned int
733 page_gotno() const
734 { return this->page_gotno_; }
735
736 // Return the number of global GOT entries.
737 unsigned int
738 global_gotno() const
739 { return this->global_gotno_; }
740
741 // Set the number of global GOT entries.
742 void
743 set_global_gotno(unsigned int global_gotno)
744 { this->global_gotno_ = global_gotno; }
745
746 // Return the number of GGA_RELOC_ONLY global GOT entries.
747 unsigned int
748 reloc_only_gotno() const
749 { return this->reloc_only_gotno_; }
750
751 // Return the number of TLS GOT entries.
752 unsigned int
753 tls_gotno() const
754 { return this->tls_gotno_; }
755
756 // Return the GOT type for this GOT. Used for multi-GOT links only.
757 unsigned int
758 multigot_got_type(unsigned int got_type) const
759 {
760 switch (got_type)
761 {
762 case GOT_TYPE_STANDARD:
763 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
764 case GOT_TYPE_TLS_OFFSET:
765 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
766 case GOT_TYPE_TLS_PAIR:
767 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
768 default:
769 gold_unreachable();
770 }
771 }
772
773 // Remove lazy-binding stubs for global symbols in this GOT.
774 void
775 remove_lazy_stubs(Target_mips<size, big_endian>* target);
776
777 // Return offset of this GOT from the start of .got section.
778 unsigned int
779 offset() const
780 { return this->offset_; }
781
782 // Set offset of this GOT from the start of .got section.
783 void
784 set_offset(unsigned int offset)
785 { this->offset_ = offset; }
786
787 // Set index of this GOT in multi-GOT links.
788 void
789 set_index(unsigned int index)
790 { this->index_ = index; }
791
792 // Return next GOT in multi-GOT links.
793 Mips_got_info<size, big_endian>*
794 next() const
795 { return this->next_; }
796
797 // Set next GOT in multi-GOT links.
798 void
799 set_next(Mips_got_info<size, big_endian>* next)
800 { this->next_ = next; }
801
802 // Return the offset of TLS LDM entry for this GOT.
803 unsigned int
804 tls_ldm_offset() const
805 { return this->tls_ldm_offset_; }
806
807 // Set the offset of TLS LDM entry for this GOT.
808 void
809 set_tls_ldm_offset(unsigned int tls_ldm_offset)
810 { this->tls_ldm_offset_ = tls_ldm_offset; }
811
812 Unordered_set<Mips_symbol<size>*>&
813 global_got_symbols()
814 { return this->global_got_symbols_; }
815
816 // Return the GOT_TLS_* type required by relocation type R_TYPE.
817 static int
818 mips_elf_reloc_tls_type(unsigned int r_type)
819 {
820 if (tls_gd_reloc(r_type))
821 return GOT_TLS_GD;
822
823 if (tls_ldm_reloc(r_type))
824 return GOT_TLS_LDM;
825
826 if (tls_gottprel_reloc(r_type))
827 return GOT_TLS_IE;
828
829 return GOT_TLS_NONE;
830 }
831
832 // Return the number of GOT slots needed for GOT TLS type TYPE.
833 static int
834 mips_tls_got_entries(unsigned int type)
835 {
836 switch (type)
837 {
838 case GOT_TLS_GD:
839 case GOT_TLS_LDM:
840 return 2;
841
842 case GOT_TLS_IE:
843 return 1;
844
845 case GOT_TLS_NONE:
846 return 0;
847
848 default:
849 gold_unreachable();
850 }
851 }
852
853 private:
854 // The number of local GOT entries.
855 unsigned int local_gotno_;
856 // The maximum number of page GOT entries needed.
857 unsigned int page_gotno_;
858 // The number of global GOT entries.
859 unsigned int global_gotno_;
860 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
861 unsigned int reloc_only_gotno_;
862 // The number of TLS GOT entries.
863 unsigned int tls_gotno_;
864 // The offset of TLS LDM entry for this GOT.
865 unsigned int tls_ldm_offset_;
866 // All symbols that have global GOT entry.
867 Unordered_set<Mips_symbol<size>*> global_got_symbols_;
868 // A hash table holding GOT entries.
869 Got_entry_set got_entries_;
870 // A hash table of GOT page entries.
871 Got_page_entry_set got_page_entries_;
872 // The offset of first GOT page entry for this GOT.
873 unsigned int got_page_offset_start_;
874 // The offset of next available GOT page entry for this GOT.
875 unsigned int got_page_offset_next_;
876 // A hash table that maps GOT page entry value to the GOT offset where
877 // the entry is located.
878 Got_page_offsets got_page_offsets_;
879 // In multi-GOT links, a pointer to the next GOT.
880 Mips_got_info<size, big_endian>* next_;
881 // Index of this GOT in multi-GOT links.
882 unsigned int index_;
883 // The offset of this GOT in multi-GOT links.
884 unsigned int offset_;
885};
886
887// This is a helper class used during relocation scan. It records GOT16 addend.
888
889template<int size, bool big_endian>
890struct got16_addend
891{
892 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
893
894 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
895 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
896 Mips_address _addend)
897 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
898 addend(_addend)
899 { }
900
901 const Sized_relobj_file<size, big_endian>* object;
902 unsigned int shndx;
903 unsigned int r_type;
904 unsigned int r_sym;
905 Mips_address addend;
906};
907
908// Mips_symbol class. Holds additional symbol information needed for Mips.
909
910template<int size>
911class Mips_symbol : public Sized_symbol<size>
912{
913 public:
914 Mips_symbol()
915 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
916 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
917 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
918 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
919 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
920 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
921 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
922 { }
923
924 // Return whether this is a MIPS16 symbol.
925 bool
926 is_mips16() const
927 {
928 // (st_other & STO_MIPS16) == STO_MIPS16
929 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
930 == elfcpp::STO_MIPS16 >> 2);
931 }
932
933 // Return whether this is a microMIPS symbol.
934 bool
935 is_micromips() const
936 {
937 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
938 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
939 == elfcpp::STO_MICROMIPS >> 2);
940 }
941
942 // Return whether the symbol needs MIPS16 fn_stub.
943 bool
944 need_fn_stub() const
945 { return this->need_fn_stub_; }
946
947 // Set that the symbol needs MIPS16 fn_stub.
948 void
949 set_need_fn_stub()
950 { this->need_fn_stub_ = true; }
951
952 // Return whether this symbol is referenced by branch relocations from
953 // any non-PIC input file.
954 bool
955 has_nonpic_branches() const
956 { return this->has_nonpic_branches_; }
957
958 // Set that this symbol is referenced by branch relocations from
959 // any non-PIC input file.
960 void
961 set_has_nonpic_branches()
962 { this->has_nonpic_branches_ = true; }
963
964 // Return the offset of the la25 stub for this symbol from the start of the
965 // la25 stub section.
966 unsigned int
967 la25_stub_offset() const
968 { return this->la25_stub_offset_; }
969
970 // Set the offset of the la25 stub for this symbol from the start of the
971 // la25 stub section.
972 void
973 set_la25_stub_offset(unsigned int offset)
974 { this->la25_stub_offset_ = offset; }
975
976 // Return whether the symbol has la25 stub. This is true if this symbol is
977 // for a PIC function, and there are non-PIC branches and jumps to it.
978 bool
979 has_la25_stub() const
980 { return this->la25_stub_offset_ != -1U; }
981
982 // Return whether there is a relocation against this symbol that must be
983 // resolved by the static linker (that is, the relocation cannot possibly
984 // be made dynamic).
985 bool
986 has_static_relocs() const
987 { return this->has_static_relocs_; }
988
989 // Set that there is a relocation against this symbol that must be resolved
990 // by the static linker (that is, the relocation cannot possibly be made
991 // dynamic).
992 void
993 set_has_static_relocs()
994 { this->has_static_relocs_ = true; }
995
996 // Return whether we must not create a lazy-binding stub for this symbol.
997 bool
998 no_lazy_stub() const
999 { return this->no_lazy_stub_; }
1000
1001 // Set that we must not create a lazy-binding stub for this symbol.
1002 void
1003 set_no_lazy_stub()
1004 { this->no_lazy_stub_ = true; }
1005
1006 // Return the offset of the lazy-binding stub for this symbol from the start
1007 // of .MIPS.stubs section.
1008 unsigned int
1009 lazy_stub_offset() const
1010 { return this->lazy_stub_offset_; }
1011
1012 // Set the offset of the lazy-binding stub for this symbol from the start
1013 // of .MIPS.stubs section.
1014 void
1015 set_lazy_stub_offset(unsigned int offset)
1016 { this->lazy_stub_offset_ = offset; }
1017
1018 // Return whether there are any relocations for this symbol where
1019 // pointer equality matters.
1020 bool
1021 pointer_equality_needed() const
1022 { return this->pointer_equality_needed_; }
1023
1024 // Set that there are relocations for this symbol where pointer equality
1025 // matters.
1026 void
1027 set_pointer_equality_needed()
1028 { this->pointer_equality_needed_ = true; }
1029
1030 // Return global GOT area where this symbol in located.
1031 Global_got_area
1032 global_got_area() const
1033 { return this->global_got_area_; }
1034
1035 // Set global GOT area where this symbol in located.
1036 void
1037 set_global_got_area(Global_got_area global_got_area)
1038 { this->global_got_area_ = global_got_area; }
1039
1040 // Return the global GOT offset for this symbol. For multi-GOT links, this
1041 // returns the offset from the start of .got section to the first GOT entry
1042 // for the symbol. Note that in multi-GOT links the symbol can have entry
1043 // in more than one GOT.
1044 unsigned int
1045 global_gotoffset() const
1046 { return this->global_gotoffset_; }
1047
1048 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1049 // the symbol can have entry in more than one GOT. This method will set
1050 // the offset only if it is less than current offset.
1051 void
1052 set_global_gotoffset(unsigned int offset)
1053 {
1054 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1055 this->global_gotoffset_ = offset;
1056 }
1057
1058 // Return whether all GOT relocations for this symbol are for calls.
1059 bool
1060 got_only_for_calls() const
1061 { return this->got_only_for_calls_; }
1062
1063 // Set that there is a GOT relocation for this symbol that is not for call.
1064 void
1065 set_got_not_only_for_calls()
1066 { this->got_only_for_calls_ = false; }
1067
1068 // Return whether this is a PIC symbol.
1069 bool
1070 is_pic() const
1071 {
1072 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1073 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1074 == (elfcpp::STO_MIPS_PIC >> 2));
1075 }
1076
1077 // Set the flag in st_other field that marks this symbol as PIC.
1078 void
1079 set_pic()
1080 {
1081 if (this->is_mips16())
1082 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1083 this->set_nonvis((this->nonvis()
1084 & ~((elfcpp::STO_MIPS16 >> 2)
1085 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1086 | (elfcpp::STO_MIPS_PIC >> 2));
1087 else
1088 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1089 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1090 | (elfcpp::STO_MIPS_PIC >> 2));
1091 }
1092
1093 // Set the flag in st_other field that marks this symbol as PLT.
1094 void
1095 set_mips_plt()
1096 {
1097 if (this->is_mips16())
1098 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1099 this->set_nonvis((this->nonvis()
1100 & ((elfcpp::STO_MIPS16 >> 2)
1101 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1102 | (elfcpp::STO_MIPS_PLT >> 2));
1103
1104 else
1105 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1106 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1107 | (elfcpp::STO_MIPS_PLT >> 2));
1108 }
1109
1110 // Downcast a base pointer to a Mips_symbol pointer.
1111 static Mips_symbol<size>*
1112 as_mips_sym(Symbol* sym)
1113 { return static_cast<Mips_symbol<size>*>(sym); }
1114
1115 // Downcast a base pointer to a Mips_symbol pointer.
1116 static const Mips_symbol<size>*
1117 as_mips_sym(const Symbol* sym)
1118 { return static_cast<const Mips_symbol<size>*>(sym); }
1119
1120 // Return whether the symbol has lazy-binding stub.
1121 bool
1122 has_lazy_stub() const
1123 { return this->has_lazy_stub_; }
1124
1125 // Set whether the symbol has lazy-binding stub.
1126 void
1127 set_has_lazy_stub(bool has_lazy_stub)
1128 { this->has_lazy_stub_ = has_lazy_stub; }
1129
1130 // Return whether the symbol needs a standard PLT entry.
1131 bool
1132 needs_mips_plt() const
1133 { return this->needs_mips_plt_; }
1134
1135 // Set whether the symbol needs a standard PLT entry.
1136 void
1137 set_needs_mips_plt(bool needs_mips_plt)
1138 { this->needs_mips_plt_ = needs_mips_plt; }
1139
1140 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1141 // entry.
1142 bool
1143 needs_comp_plt() const
1144 { return this->needs_comp_plt_; }
1145
1146 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1147 void
1148 set_needs_comp_plt(bool needs_comp_plt)
1149 { this->needs_comp_plt_ = needs_comp_plt; }
1150
1151 // Return standard PLT entry offset, or -1 if none.
1152 unsigned int
1153 mips_plt_offset() const
1154 { return this->mips_plt_offset_; }
1155
1156 // Set standard PLT entry offset.
1157 void
1158 set_mips_plt_offset(unsigned int mips_plt_offset)
1159 { this->mips_plt_offset_ = mips_plt_offset; }
1160
1161 // Return whether the symbol has standard PLT entry.
1162 bool
1163 has_mips_plt_offset() const
1164 { return this->mips_plt_offset_ != -1U; }
1165
1166 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1167 unsigned int
1168 comp_plt_offset() const
1169 { return this->comp_plt_offset_; }
1170
1171 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1172 void
1173 set_comp_plt_offset(unsigned int comp_plt_offset)
1174 { this->comp_plt_offset_ = comp_plt_offset; }
1175
1176 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1177 bool
1178 has_comp_plt_offset() const
1179 { return this->comp_plt_offset_ != -1U; }
1180
1181 // Return MIPS16 fn stub for a symbol.
1182 template<bool big_endian>
1183 Mips16_stub_section<size, big_endian>*
1184 get_mips16_fn_stub() const
1185 {
1186 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1187 }
1188
1189 // Set MIPS16 fn stub for a symbol.
1190 void
1191 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1192 { this->mips16_fn_stub_ = stub; }
1193
1194 // Return whether symbol has MIPS16 fn stub.
1195 bool
1196 has_mips16_fn_stub() const
1197 { return this->mips16_fn_stub_ != NULL; }
1198
1199 // Return MIPS16 call stub for a symbol.
1200 template<bool big_endian>
1201 Mips16_stub_section<size, big_endian>*
1202 get_mips16_call_stub() const
1203 {
1204 return static_cast<Mips16_stub_section<size, big_endian>*>(
1205 mips16_call_stub_);
1206 }
1207
1208 // Set MIPS16 call stub for a symbol.
1209 void
1210 set_mips16_call_stub(Mips16_stub_section_base* stub)
1211 { this->mips16_call_stub_ = stub; }
1212
1213 // Return whether symbol has MIPS16 call stub.
1214 bool
1215 has_mips16_call_stub() const
1216 { return this->mips16_call_stub_ != NULL; }
1217
1218 // Return MIPS16 call_fp stub for a symbol.
1219 template<bool big_endian>
1220 Mips16_stub_section<size, big_endian>*
1221 get_mips16_call_fp_stub() const
1222 {
1223 return static_cast<Mips16_stub_section<size, big_endian>*>(
1224 mips16_call_fp_stub_);
1225 }
1226
1227 // Set MIPS16 call_fp stub for a symbol.
1228 void
1229 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1230 { this->mips16_call_fp_stub_ = stub; }
1231
1232 // Return whether symbol has MIPS16 call_fp stub.
1233 bool
1234 has_mips16_call_fp_stub() const
1235 { return this->mips16_call_fp_stub_ != NULL; }
1236
1237 bool
1238 get_applied_secondary_got_fixup() const
1239 { return applied_secondary_got_fixup_; }
1240
1241 void
1242 set_applied_secondary_got_fixup()
1243 { this->applied_secondary_got_fixup_ = true; }
1244
1245 private:
1246 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1247 // appears in any relocs other than a 16 bit call.
1248 bool need_fn_stub_;
1249
1250 // True if this symbol is referenced by branch relocations from
1251 // any non-PIC input file. This is used to determine whether an
1252 // la25 stub is required.
1253 bool has_nonpic_branches_;
1254
1255 // The offset of the la25 stub for this symbol from the start of the
1256 // la25 stub section.
1257 unsigned int la25_stub_offset_;
1258
1259 // True if there is a relocation against this symbol that must be
1260 // resolved by the static linker (that is, the relocation cannot
1261 // possibly be made dynamic).
1262 bool has_static_relocs_;
1263
1264 // Whether we must not create a lazy-binding stub for this symbol.
1265 // This is true if the symbol has relocations related to taking the
1266 // function's address.
1267 bool no_lazy_stub_;
1268
1269 // The offset of the lazy-binding stub for this symbol from the start of
1270 // .MIPS.stubs section.
1271 unsigned int lazy_stub_offset_;
1272
1273 // True if there are any relocations for this symbol where pointer equality
1274 // matters.
1275 bool pointer_equality_needed_;
1276
1277 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1278 // in the global part of the GOT.
1279 Global_got_area global_got_area_;
1280
1281 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1282 // from the start of .got section to the first GOT entry for the symbol.
1283 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1284 unsigned int global_gotoffset_;
1285
1286 // Whether all GOT relocations for this symbol are for calls.
1287 bool got_only_for_calls_;
1288 // Whether the symbol has lazy-binding stub.
1289 bool has_lazy_stub_;
1290 // Whether the symbol needs a standard PLT entry.
1291 bool needs_mips_plt_;
1292 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1293 bool needs_comp_plt_;
1294 // Standard PLT entry offset, or -1 if none.
1295 unsigned int mips_plt_offset_;
1296 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1297 unsigned int comp_plt_offset_;
1298 // MIPS16 fn stub for a symbol.
1299 Mips16_stub_section_base* mips16_fn_stub_;
1300 // MIPS16 call stub for a symbol.
1301 Mips16_stub_section_base* mips16_call_stub_;
1302 // MIPS16 call_fp stub for a symbol.
1303 Mips16_stub_section_base* mips16_call_fp_stub_;
1304
1305 bool applied_secondary_got_fixup_;
1306};
1307
1308// Mips16_stub_section class.
1309
1310// The mips16 compiler uses a couple of special sections to handle
1311// floating point arguments.
1312
1313// Section names that look like .mips16.fn.FNNAME contain stubs that
1314// copy floating point arguments from the fp regs to the gp regs and
1315// then jump to FNNAME. If any 32 bit function calls FNNAME, the
1316// call should be redirected to the stub instead. If no 32 bit
1317// function calls FNNAME, the stub should be discarded. We need to
1318// consider any reference to the function, not just a call, because
1319// if the address of the function is taken we will need the stub,
1320// since the address might be passed to a 32 bit function.
1321
1322// Section names that look like .mips16.call.FNNAME contain stubs
1323// that copy floating point arguments from the gp regs to the fp
1324// regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1325// then any 16 bit function that calls FNNAME should be redirected
1326// to the stub instead. If FNNAME is not a 32 bit function, the
1327// stub should be discarded.
1328
1329// .mips16.call.fp.FNNAME sections are similar, but contain stubs
1330// which call FNNAME and then copy the return value from the fp regs
1331// to the gp regs. These stubs store the return address in $18 while
1332// calling FNNAME; any function which might call one of these stubs
1333// must arrange to save $18 around the call. (This case is not
1334// needed for 32 bit functions that call 16 bit functions, because
1335// 16 bit functions always return floating point values in both
1336// $f0/$f1 and $2/$3.)
1337
1338// Note that in all cases FNNAME might be defined statically.
1339// Therefore, FNNAME is not used literally. Instead, the relocation
1340// information will indicate which symbol the section is for.
1341
1342// We record any stubs that we find in the symbol table.
1343
1344// TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1345
1346class Mips16_stub_section_base { };
1347
1348template<int size, bool big_endian>
1349class Mips16_stub_section : public Mips16_stub_section_base
1350{
1351 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1352
1353 public:
1354 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1355 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1356 found_r_mips_none_(false)
1357 {
1358 gold_assert(object->is_mips16_fn_stub_section(shndx)
1359 || object->is_mips16_call_stub_section(shndx)
1360 || object->is_mips16_call_fp_stub_section(shndx));
1361 }
1362
1363 // Return the object of this stub section.
1364 Mips_relobj<size, big_endian>*
1365 object() const
1366 { return this->object_; }
1367
1368 // Return the size of a section.
1369 uint64_t
1370 section_size() const
1371 { return this->object_->section_size(this->shndx_); }
1372
1373 // Return section index of this stub section.
1374 unsigned int
1375 shndx() const
1376 { return this->shndx_; }
1377
1378 // Return symbol index, if stub is for a local function.
1379 unsigned int
1380 r_sym() const
1381 { return this->r_sym_; }
1382
1383 // Return symbol, if stub is for a global function.
1384 Mips_symbol<size>*
1385 gsym() const
1386 { return this->gsym_; }
1387
1388 // Return whether stub is for a local function.
1389 bool
1390 is_for_local_function() const
1391 { return this->gsym_ == NULL; }
1392
1393 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1394 // is found in the stub section. Try to find stub target.
1395 void
1396 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1397 {
1398 // To find target symbol for this stub, trust the first R_MIPS_NONE
1399 // relocation, if any. Otherwise trust the first relocation, whatever
1400 // its kind.
1401 if (this->found_r_mips_none_)
1402 return;
1403 if (r_type == elfcpp::R_MIPS_NONE)
1404 {
1405 this->r_sym_ = r_sym;
1406 this->gsym_ = NULL;
1407 this->found_r_mips_none_ = true;
1408 }
1409 else if (!is_target_found())
1410 this->r_sym_ = r_sym;
1411 }
1412
1413 // This method is called when a new relocation R_TYPE for global symbol GSYM
1414 // is found in the stub section. Try to find stub target.
1415 void
1416 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1417 {
1418 // To find target symbol for this stub, trust the first R_MIPS_NONE
1419 // relocation, if any. Otherwise trust the first relocation, whatever
1420 // its kind.
1421 if (this->found_r_mips_none_)
1422 return;
1423 if (r_type == elfcpp::R_MIPS_NONE)
1424 {
1425 this->gsym_ = gsym;
1426 this->r_sym_ = 0;
1427 this->found_r_mips_none_ = true;
1428 }
1429 else if (!is_target_found())
1430 this->gsym_ = gsym;
1431 }
1432
1433 // Return whether we found the stub target.
1434 bool
1435 is_target_found() const
1436 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1437
1438 // Return whether this is a fn stub.
1439 bool
1440 is_fn_stub() const
1441 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1442
1443 // Return whether this is a call stub.
1444 bool
1445 is_call_stub() const
1446 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1447
1448 // Return whether this is a call_fp stub.
1449 bool
1450 is_call_fp_stub() const
1451 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1452
1453 // Return the output address.
1454 Mips_address
1455 output_address() const
1456 {
1457 return (this->object_->output_section(this->shndx_)->address()
1458 + this->object_->output_section_offset(this->shndx_));
1459 }
1460
1461 private:
1462 // The object of this stub section.
1463 Mips_relobj<size, big_endian>* object_;
1464 // The section index of this stub section.
1465 unsigned int shndx_;
1466 // The symbol index, if stub is for a local function.
1467 unsigned int r_sym_;
1468 // The symbol, if stub is for a global function.
1469 Mips_symbol<size>* gsym_;
1470 // True if we found R_MIPS_NONE relocation in this stub.
1471 bool found_r_mips_none_;
1472};
1473
1474// Mips_relobj class.
1475
1476template<int size, bool big_endian>
1477class Mips_relobj : public Sized_relobj_file<size, big_endian>
1478{
1479 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1480 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1481 Mips16_stubs_int_map;
1482 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1483
1484 public:
1485 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1486 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1487 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1488 processor_specific_flags_(0), local_symbol_is_mips16_(),
1489 local_symbol_is_micromips_(), mips16_stub_sections_(),
1490 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1491 local_mips16_call_stubs_(), gp_(0), got_info_(NULL),
1492 section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1493 section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U), gprmask_(0),
1494 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1495 {
1496 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1497 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
1498 this->is_n64_ = elfcpp::abi_64(ehdr.get_e_ident()[elfcpp::EI_CLASS]);
1499 }
1500
1501 ~Mips_relobj()
1502 { }
1503
1504 // Downcast a base pointer to a Mips_relobj pointer. This is
1505 // not type-safe but we only use Mips_relobj not the base class.
1506 static Mips_relobj<size, big_endian>*
1507 as_mips_relobj(Relobj* relobj)
1508 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1509
1510 // Downcast a base pointer to a Mips_relobj pointer. This is
1511 // not type-safe but we only use Mips_relobj not the base class.
1512 static const Mips_relobj<size, big_endian>*
1513 as_mips_relobj(const Relobj* relobj)
1514 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1515
1516 // Processor-specific flags in ELF file header. This is valid only after
1517 // reading symbols.
1518 elfcpp::Elf_Word
1519 processor_specific_flags() const
1520 { return this->processor_specific_flags_; }
1521
1522 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1523 // index. This is only valid after do_count_local_symbol is called.
1524 bool
1525 local_symbol_is_mips16(unsigned int r_sym) const
1526 {
1527 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1528 return this->local_symbol_is_mips16_[r_sym];
1529 }
1530
1531 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1532 // index. This is only valid after do_count_local_symbol is called.
1533 bool
1534 local_symbol_is_micromips(unsigned int r_sym) const
1535 {
1536 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1537 return this->local_symbol_is_micromips_[r_sym];
1538 }
1539
1540 // Get or create MIPS16 stub section.
1541 Mips16_stub_section<size, big_endian>*
1542 get_mips16_stub_section(unsigned int shndx)
1543 {
1544 typename Mips16_stubs_int_map::const_iterator it =
1545 this->mips16_stub_sections_.find(shndx);
1546 if (it != this->mips16_stub_sections_.end())
1547 return (*it).second;
1548
1549 Mips16_stub_section<size, big_endian>* stub_section =
1550 new Mips16_stub_section<size, big_endian>(this, shndx);
1551 this->mips16_stub_sections_.insert(
1552 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1553 stub_section->shndx(), stub_section));
1554 return stub_section;
1555 }
1556
1557 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1558 // object doesn't have fn stub for R_SYM.
1559 Mips16_stub_section<size, big_endian>*
1560 get_local_mips16_fn_stub(unsigned int r_sym) const
1561 {
1562 typename Mips16_stubs_int_map::const_iterator it =
1563 this->local_mips16_fn_stubs_.find(r_sym);
1564 if (it != this->local_mips16_fn_stubs_.end())
1565 return (*it).second;
1566 return NULL;
1567 }
1568
1569 // Record that this object has MIPS16 fn stub for local symbol. This method
1570 // is only called if we decided not to discard the stub.
1571 void
1572 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1573 {
1574 gold_assert(stub->is_for_local_function());
1575 unsigned int r_sym = stub->r_sym();
1576 this->local_mips16_fn_stubs_.insert(
1577 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1578 r_sym, stub));
1579 }
1580
1581 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1582 // object doesn't have call stub for R_SYM.
1583 Mips16_stub_section<size, big_endian>*
1584 get_local_mips16_call_stub(unsigned int r_sym) const
1585 {
1586 typename Mips16_stubs_int_map::const_iterator it =
1587 this->local_mips16_call_stubs_.find(r_sym);
1588 if (it != this->local_mips16_call_stubs_.end())
1589 return (*it).second;
1590 return NULL;
1591 }
1592
1593 // Record that this object has MIPS16 call stub for local symbol. This method
1594 // is only called if we decided not to discard the stub.
1595 void
1596 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1597 {
1598 gold_assert(stub->is_for_local_function());
1599 unsigned int r_sym = stub->r_sym();
1600 this->local_mips16_call_stubs_.insert(
1601 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1602 r_sym, stub));
1603 }
1604
1605 // Record that we found "non 16-bit" call relocation against local symbol
1606 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1607 // is one.
1608 void
1609 add_local_non_16bit_call(unsigned int symndx)
1610 { this->local_non_16bit_calls_.insert(symndx); }
1611
1612 // Return true if there is any "non 16-bit" call relocation against local
1613 // symbol SYMNDX in this object.
1614 bool
1615 has_local_non_16bit_call_relocs(unsigned int symndx)
1616 {
1617 return (this->local_non_16bit_calls_.find(symndx)
1618 != this->local_non_16bit_calls_.end());
1619 }
1620
1621 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1622 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1623 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1624 void
1625 add_local_16bit_call(unsigned int symndx)
1626 { this->local_16bit_calls_.insert(symndx); }
1627
1628 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1629 // symbol SYMNDX in this object.
1630 bool
1631 has_local_16bit_call_relocs(unsigned int symndx)
1632 {
1633 return (this->local_16bit_calls_.find(symndx)
1634 != this->local_16bit_calls_.end());
1635 }
1636
1637 // Get gp value that was used to create this object.
1638 Mips_address
1639 gp_value() const
1640 { return this->gp_; }
1641
1642 // Return whether the object is a PIC object.
1643 bool
1644 is_pic() const
1645 { return this->is_pic_; }
1646
1647 // Return whether the object uses N32 ABI.
1648 bool
1649 is_n32() const
1650 { return this->is_n32_; }
1651
1652 // Return whether the object uses N64 ABI.
1653 bool
1654 is_n64() const
1655 { return this->is_n64_; }
1656
1657 // Return whether the object uses NewABI conventions.
1658 bool
1659 is_newabi() const
1660 { return this->is_n32_ || this->is_n64_; }
1661
1662 // Return Mips_got_info for this object.
1663 Mips_got_info<size, big_endian>*
1664 get_got_info() const
1665 { return this->got_info_; }
1666
1667 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1668 Mips_got_info<size, big_endian>*
1669 get_or_create_got_info()
1670 {
1671 if (!this->got_info_)
1672 this->got_info_ = new Mips_got_info<size, big_endian>();
1673 return this->got_info_;
1674 }
1675
1676 // Set Mips_got_info for this object.
1677 void
1678 set_got_info(Mips_got_info<size, big_endian>* got_info)
1679 { this->got_info_ = got_info; }
1680
1681 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1682 // after do_read_symbols is called.
1683 bool
1684 is_mips16_stub_section(unsigned int shndx)
1685 {
1686 return (is_mips16_fn_stub_section(shndx)
1687 || is_mips16_call_stub_section(shndx)
1688 || is_mips16_call_fp_stub_section(shndx));
1689 }
1690
1691 // Return TRUE if relocations in section SHNDX can refer directly to a
1692 // MIPS16 function rather than to a hard-float stub. This is only valid
1693 // after do_read_symbols is called.
1694 bool
1695 section_allows_mips16_refs(unsigned int shndx)
1696 {
1697 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1698 }
1699
1700 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1701 // after do_read_symbols is called.
1702 bool
1703 is_mips16_fn_stub_section(unsigned int shndx)
1704 {
1705 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1706 return this->section_is_mips16_fn_stub_[shndx];
1707 }
1708
1709 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1710 // after do_read_symbols is called.
1711 bool
1712 is_mips16_call_stub_section(unsigned int shndx)
1713 {
1714 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1715 return this->section_is_mips16_call_stub_[shndx];
1716 }
1717
1718 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1719 // valid after do_read_symbols is called.
1720 bool
1721 is_mips16_call_fp_stub_section(unsigned int shndx)
1722 {
1723 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1724 return this->section_is_mips16_call_fp_stub_[shndx];
1725 }
1726
1727 // Discard MIPS16 stub secions that are not needed.
1728 void
1729 discard_mips16_stub_sections(Symbol_table* symtab);
1730
1731 // Return gprmask from the .reginfo section of this object.
1732 Valtype
1733 gprmask() const
1734 { return this->gprmask_; }
1735
1736 // Return cprmask1 from the .reginfo section of this object.
1737 Valtype
1738 cprmask1() const
1739 { return this->cprmask1_; }
1740
1741 // Return cprmask2 from the .reginfo section of this object.
1742 Valtype
1743 cprmask2() const
1744 { return this->cprmask2_; }
1745
1746 // Return cprmask3 from the .reginfo section of this object.
1747 Valtype
1748 cprmask3() const
1749 { return this->cprmask3_; }
1750
1751 // Return cprmask4 from the .reginfo section of this object.
1752 Valtype
1753 cprmask4() const
1754 { return this->cprmask4_; }
1755
1756 protected:
1757 // Count the local symbols.
1758 void
1759 do_count_local_symbols(Stringpool_template<char>*,
1760 Stringpool_template<char>*);
1761
1762 // Read the symbol information.
1763 void
1764 do_read_symbols(Read_symbols_data* sd);
1765
1766 private:
1767 // processor-specific flags in ELF file header.
1768 elfcpp::Elf_Word processor_specific_flags_;
1769
1770 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1771 // This is only valid after do_count_local_symbol is called.
1772 std::vector<bool> local_symbol_is_mips16_;
1773
1774 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1775 // This is only valid after do_count_local_symbol is called.
1776 std::vector<bool> local_symbol_is_micromips_;
1777
1778 // Map from section index to the MIPS16 stub for that section. This contains
1779 // all stubs found in this object.
1780 Mips16_stubs_int_map mips16_stub_sections_;
1781
1782 // Local symbols that have "non 16-bit" call relocation. This relocation
1783 // would need to refer to a MIPS16 fn stub, if there is one.
1784 std::set<unsigned int> local_non_16bit_calls_;
1785
1786 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1787 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1788 // relocation that refers to the stub symbol.
1789 std::set<unsigned int> local_16bit_calls_;
1790
1791 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1792 // This contains only the stubs that we decided not to discard.
1793 Mips16_stubs_int_map local_mips16_fn_stubs_;
1794
1795 // Map from local symbol index to the MIPS16 call stub for that symbol.
1796 // This contains only the stubs that we decided not to discard.
1797 Mips16_stubs_int_map local_mips16_call_stubs_;
1798
1799 // gp value that was used to create this object.
1800 Mips_address gp_;
1801 // Whether the object is a PIC object.
1802 bool is_pic_ : 1;
1803 // Whether the object uses N32 ABI.
1804 bool is_n32_ : 1;
1805 // Whether the object uses N64 ABI.
1806 bool is_n64_ : 1;
1807 // The Mips_got_info for this object.
1808 Mips_got_info<size, big_endian>* got_info_;
1809
1810 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1811 // This is only valid after do_read_symbols is called.
1812 std::vector<bool> section_is_mips16_fn_stub_;
1813
1814 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1815 // This is only valid after do_read_symbols is called.
1816 std::vector<bool> section_is_mips16_call_stub_;
1817
1818 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1819 // This is only valid after do_read_symbols is called.
1820 std::vector<bool> section_is_mips16_call_fp_stub_;
1821
1822 // .pdr section index.
1823 unsigned int pdr_shndx_;
1824
1825 // gprmask from the .reginfo section of this object.
1826 Valtype gprmask_;
1827 // cprmask1 from the .reginfo section of this object.
1828 Valtype cprmask1_;
1829 // cprmask2 from the .reginfo section of this object.
1830 Valtype cprmask2_;
1831 // cprmask3 from the .reginfo section of this object.
1832 Valtype cprmask3_;
1833 // cprmask4 from the .reginfo section of this object.
1834 Valtype cprmask4_;
1835};
1836
1837// Mips_output_data_got class.
1838
1839template<int size, bool big_endian>
1840class Mips_output_data_got : public Output_data_got<size, big_endian>
1841{
1842 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1843 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1844 Reloc_section;
1845 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1846
1847 public:
1848 Mips_output_data_got(Target_mips<size, big_endian>* target,
1849 Symbol_table* symtab, Layout* layout)
1850 : Output_data_got<size, big_endian>(), target_(target),
1851 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1852 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1853 secondary_got_relocs_()
1854 {
1855 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1856 this->set_addralign(16);
1857 }
1858
1859 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1860 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1861 void
1862 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1863 unsigned int symndx, Mips_address addend,
1864 unsigned int r_type, unsigned int shndx)
1865 {
1866 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
1867 r_type, shndx);
1868 }
1869
1870 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
1871 // in OBJECT. FOR_CALL is true if the caller is only interested in
1872 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
1873 // relocation.
1874 void
1875 record_global_got_symbol(Mips_symbol<size>* mips_sym,
1876 Mips_relobj<size, big_endian>* object,
1877 unsigned int r_type, bool dyn_reloc, bool for_call)
1878 {
1879 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
1880 dyn_reloc, for_call);
1881 }
1882
1883 // Record that OBJECT has a page relocation against symbol SYMNDX and
1884 // that ADDEND is the addend for that relocation.
1885 void
1886 record_got_page_entry(Mips_relobj<size, big_endian>* object,
1887 unsigned int symndx, int addend)
1888 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
1889
1890 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1891 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1892 // applied in a static link.
1893 void
1894 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1895 Mips_symbol<size>* gsym)
1896 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1897
1898 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1899 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1900 // relocation that needs to be applied in a static link.
1901 void
1902 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1903 Sized_relobj_file<size, big_endian>* relobj,
1904 unsigned int index)
1905 {
1906 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1907 index));
1908 }
1909
1910 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
1911 // secondary GOT at OFFSET.
1912 void
1913 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
1914 Mips_symbol<size>* gsym)
1915 {
1916 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
1917 r_type, gsym));
1918 }
1919
1920 // Update GOT entry at OFFSET with VALUE.
1921 void
1922 update_got_entry(unsigned int offset, Mips_address value)
1923 {
1924 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
1925 }
1926
1927 // Return the number of entries in local part of the GOT. This includes
1928 // local entries, page entries and 2 reserved entries.
1929 unsigned int
1930 get_local_gotno() const
1931 {
1932 if (!this->multi_got())
1933 {
1934 return (2 + this->master_got_info_->local_gotno()
1935 + this->master_got_info_->page_gotno());
1936 }
1937 else
1938 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
1939 }
1940
1941 // Return dynamic symbol table index of the first symbol with global GOT
1942 // entry.
1943 unsigned int
1944 first_global_got_dynsym_index() const
1945 { return this->first_global_got_dynsym_index_; }
1946
1947 // Set dynamic symbol table index of the first symbol with global GOT entry.
1948 void
1949 set_first_global_got_dynsym_index(unsigned int index)
1950 { this->first_global_got_dynsym_index_ = index; }
1951
1952 // Lay out the GOT. Add local, global and TLS entries. If GOT is
1953 // larger than 64K, create multi-GOT.
1954 void
1955 lay_out_got(Layout* layout, Symbol_table* symtab,
1956 const Input_objects* input_objects);
1957
1958 // Create multi-GOT. For every GOT, add local, global and TLS entries.
1959 void
1960 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
1961
1962 // Attempt to merge GOTs of different input objects.
1963 void
1964 merge_gots(const Input_objects* input_objects);
1965
1966 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
1967 // this would lead to overflow, true if they were merged successfully.
1968 bool
1969 merge_got_with(Mips_got_info<size, big_endian>* from,
1970 Mips_relobj<size, big_endian>* object,
1971 Mips_got_info<size, big_endian>* to);
1972
1973 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
1974 // use OBJECT's GOT.
1975 unsigned int
1976 get_got_page_offset(Mips_address value,
1977 const Mips_relobj<size, big_endian>* object)
1978 {
1979 Mips_got_info<size, big_endian>* g = (!this->multi_got()
1980 ? this->master_got_info_
1981 : object->get_got_info());
1982 gold_assert(g != NULL);
1983 return g->get_got_page_offset(value, this);
1984 }
1985
1986 // Return the GOT offset of type GOT_TYPE of the global symbol
1987 // GSYM. For multi-GOT links, use OBJECT's GOT.
1988 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
1989 Mips_relobj<size, big_endian>* object) const
1990 {
1991 if (!this->multi_got())
1992 return gsym->got_offset(got_type);
1993 else
1994 {
1995 Mips_got_info<size, big_endian>* g = object->get_got_info();
1996 gold_assert(g != NULL);
1997 return gsym->got_offset(g->multigot_got_type(got_type));
1998 }
1999 }
2000
2001 // Return the GOT offset of type GOT_TYPE of the local symbol
2002 // SYMNDX.
2003 unsigned int
2004 got_offset(unsigned int symndx, unsigned int got_type,
2005 Sized_relobj_file<size, big_endian>* object) const
2006 { return object->local_got_offset(symndx, got_type); }
2007
2008 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2009 unsigned int
2010 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2011 {
2012 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2013 ? this->master_got_info_
2014 : object->get_got_info());
2015 gold_assert(g != NULL);
2016 return g->tls_ldm_offset();
2017 }
2018
2019 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2020 void
2021 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2022 Mips_relobj<size, big_endian>* object)
2023 {
2024 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2025 ? this->master_got_info_
2026 : object->get_got_info());
2027 gold_assert(g != NULL);
2028 g->set_tls_ldm_offset(tls_ldm_offset);
2029 }
2030
2031 // Return true for multi-GOT links.
2032 bool
2033 multi_got() const
2034 { return this->primary_got_ != NULL; }
2035
2036 // Return the offset of OBJECT's GOT from the start of .got section.
2037 unsigned int
2038 get_got_offset(const Mips_relobj<size, big_endian>* object)
2039 {
2040 if (!this->multi_got())
2041 return 0;
2042 else
2043 {
2044 Mips_got_info<size, big_endian>* g = object->get_got_info();
2045 return g != NULL ? g->offset() : 0;
2046 }
2047 }
2048
2049 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2050 void
2051 add_reloc_only_entries()
2052 { this->master_got_info_->add_reloc_only_entries(this); }
2053
2054 // Return offset of the primary GOT's entry for global symbol.
2055 unsigned int
2056 get_primary_got_offset(const Mips_symbol<size>* sym) const
2057 {
2058 gold_assert(sym->global_got_area() != GGA_NONE);
2059 return (this->get_local_gotno() + sym->dynsym_index()
2060 - this->first_global_got_dynsym_index()) * size/8;
2061 }
2062
2063 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2064 // Input argument GOT_OFFSET is always global offset from the start of
2065 // .got section, for both single and multi-GOT links.
2066 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2067 // links, the return value is object_got_offset - 0x7FF0, where
2068 // object_got_offset is offset in the OBJECT's GOT.
2069 int
2070 gp_offset(unsigned int got_offset,
2071 const Mips_relobj<size, big_endian>* object) const
2072 {
2073 return (this->address() + got_offset
2074 - this->target_->adjusted_gp_value(object));
2075 }
2076
2077 protected:
2078 // Write out the GOT table.
2079 void
2080 do_write(Output_file*);
2081
2082 private:
2083
2084 // This class represent dynamic relocations that need to be applied by
2085 // gold because we are using TLS relocations in a static link.
2086 class Static_reloc
2087 {
2088 public:
2089 Static_reloc(unsigned int got_offset, unsigned int r_type,
2090 Mips_symbol<size>* gsym)
2091 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2092 { this->u_.global.symbol = gsym; }
2093
2094 Static_reloc(unsigned int got_offset, unsigned int r_type,
2095 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2096 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2097 {
2098 this->u_.local.relobj = relobj;
2099 this->u_.local.index = index;
2100 }
2101
2102 // Return the GOT offset.
2103 unsigned int
2104 got_offset() const
2105 { return this->got_offset_; }
2106
2107 // Relocation type.
2108 unsigned int
2109 r_type() const
2110 { return this->r_type_; }
2111
2112 // Whether the symbol is global or not.
2113 bool
2114 symbol_is_global() const
2115 { return this->symbol_is_global_; }
2116
2117 // For a relocation against a global symbol, the global symbol.
2118 Mips_symbol<size>*
2119 symbol() const
2120 {
2121 gold_assert(this->symbol_is_global_);
2122 return this->u_.global.symbol;
2123 }
2124
2125 // For a relocation against a local symbol, the defining object.
2126 Sized_relobj_file<size, big_endian>*
2127 relobj() const
2128 {
2129 gold_assert(!this->symbol_is_global_);
2130 return this->u_.local.relobj;
2131 }
2132
2133 // For a relocation against a local symbol, the local symbol index.
2134 unsigned int
2135 index() const
2136 {
2137 gold_assert(!this->symbol_is_global_);
2138 return this->u_.local.index;
2139 }
2140
2141 private:
2142 // GOT offset of the entry to which this relocation is applied.
2143 unsigned int got_offset_;
2144 // Type of relocation.
2145 unsigned int r_type_;
2146 // Whether this relocation is against a global symbol.
2147 bool symbol_is_global_;
2148 // A global or local symbol.
2149 union
2150 {
2151 struct
2152 {
2153 // For a global symbol, the symbol itself.
2154 Mips_symbol<size>* symbol;
2155 } global;
2156 struct
2157 {
2158 // For a local symbol, the object defining object.
2159 Sized_relobj_file<size, big_endian>* relobj;
2160 // For a local symbol, the symbol index.
2161 unsigned int index;
2162 } local;
2163 } u_;
2164 };
2165
2166 // The target.
2167 Target_mips<size, big_endian>* target_;
2168 // The symbol table.
2169 Symbol_table* symbol_table_;
2170 // The layout.
2171 Layout* layout_;
2172 // Static relocs to be applied to the GOT.
2173 std::vector<Static_reloc> static_relocs_;
2174 // .got section view.
2175 unsigned char* got_view_;
2176 // The dynamic symbol table index of the first symbol with global GOT entry.
2177 unsigned int first_global_got_dynsym_index_;
2178 // The master GOT information.
2179 Mips_got_info<size, big_endian>* master_got_info_;
2180 // The primary GOT information.
2181 Mips_got_info<size, big_endian>* primary_got_;
2182 // Secondary GOT fixups.
2183 std::vector<Static_reloc> secondary_got_relocs_;
2184};
2185
2186// A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2187// two ways of creating these interfaces. The first is to add:
2188//
2189// lui $25,%hi(func)
2190// j func
2191// addiu $25,$25,%lo(func)
2192//
2193// to a separate trampoline section. The second is to add:
2194//
2195// lui $25,%hi(func)
2196// addiu $25,$25,%lo(func)
2197//
2198// immediately before a PIC function "func", but only if a function is at the
2199// beginning of the section, and the section is not too heavily aligned (i.e we
2200// would need to add no more than 2 nops before the stub.)
2201//
2202// We only create stubs of the first type.
2203
2204template<int size, bool big_endian>
2205class Mips_output_data_la25_stub : public Output_section_data
2206{
2207 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2208
2209 public:
2210 Mips_output_data_la25_stub()
2211 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2212 { }
2213
2214 // Create LA25 stub for a symbol.
2215 void
2216 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2217 Mips_symbol<size>* gsym);
2218
2219 // Return output address of a stub.
2220 Mips_address
2221 stub_address(const Mips_symbol<size>* sym) const
2222 {
2223 gold_assert(sym->has_la25_stub());
2224 return this->address() + sym->la25_stub_offset();
2225 }
2226
2227 protected:
2228 void
2229 do_adjust_output_section(Output_section* os)
2230 { os->set_entsize(0); }
2231
2232 private:
2233 // Template for standard LA25 stub.
2234 static const uint32_t la25_stub_entry[];
2235 // Template for microMIPS LA25 stub.
2236 static const uint32_t la25_stub_micromips_entry[];
2237
2238 // Set the final size.
2239 void
2240 set_final_data_size()
2241 { this->set_data_size(this->symbols_.size() * 16); }
2242
2243 // Create a symbol for SYM stub's value and size, to help make the
2244 // disassembly easier to read.
2245 void
2246 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2247 Target_mips<size, big_endian>* target, uint64_t symsize);
2248
1a08ae21
VR
2249 // Write to a map file.
2250 void
2251 do_print_to_mapfile(Mapfile* mapfile) const
2252 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2253
9810d34d
SS
2254 // Write out the LA25 stub section.
2255 void
2256 do_write(Output_file*);
2257
2258 // Symbols that have LA25 stubs.
2259 Unordered_set<Mips_symbol<size>*> symbols_;
2260};
2261
2262// A class to handle the PLT data.
2263
2264template<int size, bool big_endian>
2265class Mips_output_data_plt : public Output_section_data
2266{
2267 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2268 typedef Output_data_reloc<elfcpp::SHT_REL, true,
2269 size, big_endian> Reloc_section;
2270
2271 public:
2272 // Create the PLT section. The ordinary .got section is an argument,
2273 // since we need to refer to the start.
2274 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2275 Target_mips<size, big_endian>* target)
2276 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2277 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2278 target_(target)
2279 {
2280 this->rel_ = new Reloc_section(false);
2281 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2282 elfcpp::SHF_ALLOC, this->rel_,
2283 ORDER_DYNAMIC_PLT_RELOCS, false);
2284 }
2285
2286 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2287 void
2288 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2289
2290 // Return the .rel.plt section data.
2291 const Reloc_section*
2292 rel_plt() const
2293 { return this->rel_; }
2294
2295 // Return the number of PLT entries.
2296 unsigned int
2297 entry_count() const
2298 { return this->symbols_.size(); }
2299
2300 // Return the offset of the first non-reserved PLT entry.
2301 unsigned int
2302 first_plt_entry_offset() const
2303 { return sizeof(plt0_entry_o32); }
2304
2305 // Return the size of a PLT entry.
2306 unsigned int
2307 plt_entry_size() const
2308 { return sizeof(plt_entry); }
2309
2310 // Set final PLT offsets. For each symbol, determine whether standard or
2311 // compressed (MIPS16 or microMIPS) PLT entry is used.
2312 void
2313 set_plt_offsets();
2314
2315 // Return the offset of the first standard PLT entry.
2316 unsigned int
2317 first_mips_plt_offset() const
2318 { return this->plt_header_size_; }
2319
2320 // Return the offset of the first compressed PLT entry.
2321 unsigned int
2322 first_comp_plt_offset() const
2323 { return this->plt_header_size_ + this->plt_mips_offset_; }
2324
2325 // Return whether there are any standard PLT entries.
2326 bool
2327 has_standard_entries() const
2328 { return this->plt_mips_offset_ > 0; }
2329
2330 // Return the output address of standard PLT entry.
2331 Mips_address
2332 mips_entry_address(const Mips_symbol<size>* sym) const
2333 {
2334 gold_assert (sym->has_mips_plt_offset());
2335 return (this->address() + this->first_mips_plt_offset()
2336 + sym->mips_plt_offset());
2337 }
2338
2339 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2340 Mips_address
2341 comp_entry_address(const Mips_symbol<size>* sym) const
2342 {
2343 gold_assert (sym->has_comp_plt_offset());
2344 return (this->address() + this->first_comp_plt_offset()
2345 + sym->comp_plt_offset());
2346 }
2347
2348 protected:
2349 void
2350 do_adjust_output_section(Output_section* os)
2351 { os->set_entsize(0); }
2352
2353 // Write to a map file.
2354 void
2355 do_print_to_mapfile(Mapfile* mapfile) const
2356 { mapfile->print_output_data(this, _(".plt")); }
2357
2358 private:
2359 // Template for the first PLT entry.
2360 static const uint32_t plt0_entry_o32[];
2361 static const uint32_t plt0_entry_n32[];
2362 static const uint32_t plt0_entry_n64[];
2363 static const uint32_t plt0_entry_micromips_o32[];
2364 static const uint32_t plt0_entry_micromips32_o32[];
2365
2366 // Template for subsequent PLT entries.
2367 static const uint32_t plt_entry[];
2368 static const uint32_t plt_entry_mips16_o32[];
2369 static const uint32_t plt_entry_micromips_o32[];
2370 static const uint32_t plt_entry_micromips32_o32[];
2371
2372 // Set the final size.
2373 void
2374 set_final_data_size()
2375 {
2376 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2377 + this->plt_comp_offset_);
2378 }
2379
2380 // Write out the PLT data.
2381 void
2382 do_write(Output_file*);
2383
2384 // Return whether the plt header contains microMIPS code. For the sake of
2385 // cache alignment always use a standard header whenever any standard entries
2386 // are present even if microMIPS entries are present as well. This also lets
2387 // the microMIPS header rely on the value of $v0 only set by microMIPS
2388 // entries, for a small size reduction.
2389 bool
2390 is_plt_header_compressed() const
2391 {
2392 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2393 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2394 }
2395
2396 // Return the size of the PLT header.
2397 unsigned int
2398 get_plt_header_size() const
2399 {
2400 if (this->target_->is_output_n64())
2401 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2402 else if (this->target_->is_output_n32())
2403 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2404 else if (!this->is_plt_header_compressed())
2405 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2406 else if (this->target_->use_32bit_micromips_instructions())
2407 return (2 * sizeof(plt0_entry_micromips32_o32)
2408 / sizeof(plt0_entry_micromips32_o32[0]));
2409 else
2410 return (2 * sizeof(plt0_entry_micromips_o32)
2411 / sizeof(plt0_entry_micromips_o32[0]));
2412 }
2413
2414 // Return the PLT header entry.
2415 const uint32_t*
2416 get_plt_header_entry() const
2417 {
2418 if (this->target_->is_output_n64())
2419 return plt0_entry_n64;
2420 else if (this->target_->is_output_n32())
2421 return plt0_entry_n32;
2422 else if (!this->is_plt_header_compressed())
2423 return plt0_entry_o32;
2424 else if (this->target_->use_32bit_micromips_instructions())
2425 return plt0_entry_micromips32_o32;
2426 else
2427 return plt0_entry_micromips_o32;
2428 }
2429
2430 // Return the size of the standard PLT entry.
2431 unsigned int
2432 standard_plt_entry_size() const
2433 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2434
2435 // Return the size of the compressed PLT entry.
2436 unsigned int
2437 compressed_plt_entry_size() const
2438 {
2439 gold_assert(!this->target_->is_output_newabi());
2440
2441 if (!this->target_->is_output_micromips())
2442 return (2 * sizeof(plt_entry_mips16_o32)
2443 / sizeof(plt_entry_mips16_o32[0]));
2444 else if (this->target_->use_32bit_micromips_instructions())
2445 return (2 * sizeof(plt_entry_micromips32_o32)
2446 / sizeof(plt_entry_micromips32_o32[0]));
2447 else
2448 return (2 * sizeof(plt_entry_micromips_o32)
2449 / sizeof(plt_entry_micromips_o32[0]));
2450 }
2451
2452 // The reloc section.
2453 Reloc_section* rel_;
2454 // The .got.plt section.
2455 Output_data_space* got_plt_;
2456 // Symbols that have PLT entry.
2457 std::vector<Mips_symbol<size>*> symbols_;
2458 // The offset of the next standard PLT entry to create.
2459 unsigned int plt_mips_offset_;
2460 // The offset of the next compressed PLT entry to create.
2461 unsigned int plt_comp_offset_;
2462 // The size of the PLT header in bytes.
2463 unsigned int plt_header_size_;
2464 // The target.
2465 Target_mips<size, big_endian>* target_;
2466};
2467
2468// A class to handle the .MIPS.stubs data.
2469
2470template<int size, bool big_endian>
2471class Mips_output_data_mips_stubs : public Output_section_data
2472{
2473 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2474
2475 public:
2476 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2477 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2478 stub_offsets_are_set_(false), target_(target)
2479 { }
2480
2481 // Create entry for a symbol.
2482 void
2483 make_entry(Mips_symbol<size>*);
2484
2485 // Remove entry for a symbol.
2486 void
2487 remove_entry(Mips_symbol<size>* gsym);
2488
2489 // Set stub offsets for symbols. This method expects that the number of
2490 // entries in dynamic symbol table is set.
2491 void
2492 set_lazy_stub_offsets();
2493
2494 void
2495 set_needs_dynsym_value();
2496
2497 // Set the number of entries in dynamic symbol table.
2498 void
2499 set_dynsym_count(unsigned int dynsym_count)
2500 { this->dynsym_count_ = dynsym_count; }
2501
2502 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2503 // count is greater than 0x10000. If the dynamic symbol count is less than
2504 // 0x10000, the stub will be 4 bytes smaller.
2505 // There's no disadvantage from using microMIPS code here, so for the sake of
2506 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2507 // output produced at all. This has a benefit of stubs being shorter by
2508 // 4 bytes each too, unless in the insn32 mode.
2509 unsigned int
2510 stub_max_size() const
2511 {
2512 if (!this->target_->is_output_micromips()
2513 || this->target_->use_32bit_micromips_instructions())
2514 return 20;
2515 else
2516 return 16;
2517 }
2518
2519 // Return the size of the stub. This method expects that the final dynsym
2520 // count is set.
2521 unsigned int
2522 stub_size() const
2523 {
2524 gold_assert(this->dynsym_count_ != -1U);
2525 if (this->dynsym_count_ > 0x10000)
2526 return this->stub_max_size();
2527 else
2528 return this->stub_max_size() - 4;
2529 }
2530
2531 // Return output address of a stub.
2532 Mips_address
2533 stub_address(const Mips_symbol<size>* sym) const
2534 {
2535 gold_assert(sym->has_lazy_stub());
2536 return this->address() + sym->lazy_stub_offset();
2537 }
2538
2539 protected:
2540 void
2541 do_adjust_output_section(Output_section* os)
2542 { os->set_entsize(0); }
2543
2544 // Write to a map file.
2545 void
2546 do_print_to_mapfile(Mapfile* mapfile) const
2547 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2548
2549 private:
2550 static const uint32_t lazy_stub_normal_1[];
2551 static const uint32_t lazy_stub_normal_1_n64[];
2552 static const uint32_t lazy_stub_normal_2[];
2553 static const uint32_t lazy_stub_normal_2_n64[];
2554 static const uint32_t lazy_stub_big[];
2555 static const uint32_t lazy_stub_big_n64[];
2556
2557 static const uint32_t lazy_stub_micromips_normal_1[];
2558 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2559 static const uint32_t lazy_stub_micromips_normal_2[];
2560 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2561 static const uint32_t lazy_stub_micromips_big[];
2562 static const uint32_t lazy_stub_micromips_big_n64[];
2563
2564 static const uint32_t lazy_stub_micromips32_normal_1[];
2565 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2566 static const uint32_t lazy_stub_micromips32_normal_2[];
2567 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2568 static const uint32_t lazy_stub_micromips32_big[];
2569 static const uint32_t lazy_stub_micromips32_big_n64[];
2570
2571 // Set the final size.
2572 void
2573 set_final_data_size()
2574 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2575
2576 // Write out the .MIPS.stubs data.
2577 void
2578 do_write(Output_file*);
2579
2580 // .MIPS.stubs symbols
2581 Unordered_set<Mips_symbol<size>*> symbols_;
2582 // Number of entries in dynamic symbol table.
2583 unsigned int dynsym_count_;
2584 // Whether the stub offsets are set.
2585 bool stub_offsets_are_set_;
2586 // The target.
2587 Target_mips<size, big_endian>* target_;
2588};
2589
2590// This class handles Mips .reginfo output section.
2591
2592template<int size, bool big_endian>
2593class Mips_output_section_reginfo : public Output_section
2594{
2595 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2596
2597 public:
2598 Mips_output_section_reginfo(const char* name, elfcpp::Elf_Word type,
2599 elfcpp::Elf_Xword flags,
2600 Target_mips<size, big_endian>* target)
2601 : Output_section(name, type, flags), target_(target), gprmask_(0),
2602 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
2603 { }
2604
2605 // Downcast a base pointer to a Mips_output_section_reginfo pointer.
2606 static Mips_output_section_reginfo<size, big_endian>*
2607 as_mips_output_section_reginfo(Output_section* os)
2608 { return static_cast<Mips_output_section_reginfo<size, big_endian>*>(os); }
2609
2610 // Set masks of the output .reginfo section.
2611 void
2612 set_masks(Valtype gprmask, Valtype cprmask1, Valtype cprmask2,
2613 Valtype cprmask3, Valtype cprmask4)
2614 {
2615 this->gprmask_ = gprmask;
2616 this->cprmask1_ = cprmask1;
2617 this->cprmask2_ = cprmask2;
2618 this->cprmask3_ = cprmask3;
2619 this->cprmask4_ = cprmask4;
2620 }
2621
2622 protected:
2623 // Set the final data size.
2624 void
2625 set_final_data_size()
2626 { this->set_data_size(24); }
2627
2628 // Write out reginfo section.
2629 void
2630 do_write(Output_file* of);
2631
2632 private:
2633 Target_mips<size, big_endian>* target_;
2634
2635 // gprmask of the output .reginfo section.
2636 Valtype gprmask_;
2637 // cprmask1 of the output .reginfo section.
2638 Valtype cprmask1_;
2639 // cprmask2 of the output .reginfo section.
2640 Valtype cprmask2_;
2641 // cprmask3 of the output .reginfo section.
2642 Valtype cprmask3_;
2643 // cprmask4 of the output .reginfo section.
2644 Valtype cprmask4_;
2645};
2646
2647// The MIPS target has relocation types which default handling of relocatable
2648// relocation cannot process. So we have to extend the default code.
2649
4d625b70 2650template<bool big_endian, typename Classify_reloc>
9810d34d 2651class Mips_scan_relocatable_relocs :
4d625b70 2652 public Default_scan_relocatable_relocs<Classify_reloc>
9810d34d
SS
2653{
2654 public:
2655 // Return the strategy to use for a local symbol which is a section
2656 // symbol, given the relocation type.
2657 inline Relocatable_relocs::Reloc_strategy
2658 local_section_strategy(unsigned int r_type, Relobj* object)
2659 {
4d625b70 2660 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
9810d34d
SS
2661 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2662 else
2663 {
2664 switch (r_type)
2665 {
2666 case elfcpp::R_MIPS_26:
2667 return Relocatable_relocs::RELOC_SPECIAL;
2668
2669 default:
4d625b70 2670 return Default_scan_relocatable_relocs<Classify_reloc>::
9810d34d
SS
2671 local_section_strategy(r_type, object);
2672 }
2673 }
2674 }
2675};
2676
2677// Mips_copy_relocs class. The only difference from the base class is the
2678// method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2679// Mips cannot convert all relocation types to dynamic relocs. If a reloc
2680// cannot be made dynamic, a COPY reloc is emitted.
2681
2682template<int sh_type, int size, bool big_endian>
2683class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2684{
2685 public:
2686 Mips_copy_relocs()
2687 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2688 { }
2689
2690 // Emit any saved relocations which turn out to be needed. This is
2691 // called after all the relocs have been scanned.
2692 void
2693 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2694 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2695
2696 private:
2697 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2698 Copy_reloc_entry;
2699
2700 // Emit this reloc if appropriate. This is called after we have
2701 // scanned all the relocations, so we know whether we emitted a
2702 // COPY relocation for SYM_.
2703 void
2704 emit_entry(Copy_reloc_entry& entry,
2705 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2706 Symbol_table* symtab, Layout* layout,
2707 Target_mips<size, big_endian>* target);
2708};
2709
2710
2711// Return true if the symbol SYM should be considered to resolve local
2712// to the current module, and false otherwise. The logic is taken from
2713// GNU ld's method _bfd_elf_symbol_refs_local_p.
2714static bool
2715symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2716 bool local_protected)
2717{
2718 // If it's a local sym, of course we resolve locally.
2719 if (sym == NULL)
2720 return true;
2721
2722 // STV_HIDDEN or STV_INTERNAL ones must be local.
2723 if (sym->visibility() == elfcpp::STV_HIDDEN
2724 || sym->visibility() == elfcpp::STV_INTERNAL)
2725 return true;
2726
2727 // If we don't have a definition in a regular file, then we can't
2728 // resolve locally. The sym is either undefined or dynamic.
2729 if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
2730 || sym->is_undefined())
2731 return false;
2732
2733 // Forced local symbols resolve locally.
2734 if (sym->is_forced_local())
2735 return true;
2736
2737 // As do non-dynamic symbols.
2738 if (!has_dynsym_entry)
2739 return true;
2740
2741 // At this point, we know the symbol is defined and dynamic. In an
2742 // executable it must resolve locally, likewise when building symbolic
2743 // shared libraries.
2744 if (parameters->options().output_is_executable()
2745 || parameters->options().Bsymbolic())
2746 return true;
2747
2748 // Now deal with defined dynamic symbols in shared libraries. Ones
2749 // with default visibility might not resolve locally.
2750 if (sym->visibility() == elfcpp::STV_DEFAULT)
2751 return false;
2752
2753 // STV_PROTECTED non-function symbols are local.
2754 if (sym->type() != elfcpp::STT_FUNC)
2755 return true;
2756
2757 // Function pointer equality tests may require that STV_PROTECTED
2758 // symbols be treated as dynamic symbols. If the address of a
2759 // function not defined in an executable is set to that function's
2760 // plt entry in the executable, then the address of the function in
2761 // a shared library must also be the plt entry in the executable.
2762 return local_protected;
2763}
2764
2765// Return TRUE if references to this symbol always reference the symbol in this
2766// object.
2767static bool
2768symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
2769{
2770 return symbol_refs_local(sym, has_dynsym_entry, false);
2771}
2772
2773// Return TRUE if calls to this symbol always call the version in this object.
2774static bool
2775symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
2776{
2777 return symbol_refs_local(sym, has_dynsym_entry, true);
2778}
2779
2780// Compare GOT offsets of two symbols.
2781
2782template<int size, bool big_endian>
2783static bool
2784got_offset_compare(Symbol* sym1, Symbol* sym2)
2785{
2786 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
2787 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
2788 unsigned int area1 = mips_sym1->global_got_area();
2789 unsigned int area2 = mips_sym2->global_got_area();
2790 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
2791
2792 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
2793 if (area1 != area2)
2794 return area1 < area2;
2795
2796 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
2797}
2798
2799// This method divides dynamic symbols into symbols that have GOT entry, and
2800// symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
2801// Mips ABI requires that symbols with the GOT entry must be at the end of
2802// dynamic symbol table, and the order in dynamic symbol table must match the
2803// order in GOT.
2804
2805template<int size, bool big_endian>
2806static void
2807reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
2808 std::vector<Symbol*>* non_got_symbols,
2809 std::vector<Symbol*>* got_symbols)
2810{
2811 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
2812 p != dyn_symbols->end();
2813 ++p)
2814 {
2815 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
2816 if (mips_sym->global_got_area() == GGA_NORMAL
2817 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
2818 got_symbols->push_back(mips_sym);
2819 else
2820 non_got_symbols->push_back(mips_sym);
2821 }
2822
2823 std::sort(got_symbols->begin(), got_symbols->end(),
2824 got_offset_compare<size, big_endian>);
2825}
2826
2827// Functor class for processing the global symbol table.
2828
2829template<int size, bool big_endian>
2830class Symbol_visitor_check_symbols
2831{
2832 public:
2833 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
2834 Layout* layout, Symbol_table* symtab)
2835 : target_(target), layout_(layout), symtab_(symtab)
2836 { }
2837
2838 void
2839 operator()(Sized_symbol<size>* sym)
2840 {
2841 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
2842 if (local_pic_function<size, big_endian>(mips_sym))
2843 {
2844 // SYM is a function that might need $25 to be valid on entry.
2845 // If we're creating a non-PIC relocatable object, mark SYM as
2846 // being PIC. If we're creating a non-relocatable object with
2847 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
2848 // stub.
2849 if (parameters->options().relocatable())
2850 {
2851 if (!parameters->options().output_is_position_independent())
2852 mips_sym->set_pic();
2853 }
2854 else if (mips_sym->has_nonpic_branches())
2855 {
2856 this->target_->la25_stub_section(layout_)
2857 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
2858 }
2859 }
2860 }
2861
2862 private:
2863 Target_mips<size, big_endian>* target_;
2864 Layout* layout_;
2865 Symbol_table* symtab_;
2866};
2867
4d625b70
CC
2868// Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
2869// and endianness. The relocation format for MIPS-64 is non-standard.
2870
2871template<int sh_type, int size, bool big_endian>
2872struct Mips_reloc_types;
2873
2874template<bool big_endian>
2875struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
2876{
2877 typedef typename elfcpp::Rel<32, big_endian> Reloc;
2878 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
2879
2880 static unsigned typename elfcpp::Elf_types<32>::Elf_Swxword
2881 get_r_addend(const Reloc*)
2882 { return 0; }
2883
2884 static inline void
2885 set_reloc_addend(Reloc_write*,
2886 typename elfcpp::Elf_types<32>::Elf_Swxword)
2887 { gold_unreachable(); }
2888};
2889
2890template<bool big_endian>
2891struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
2892{
2893 typedef typename elfcpp::Rela<32, big_endian> Reloc;
2894 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
2895
2896 static unsigned typename elfcpp::Elf_types<32>::Elf_Swxword
2897 get_r_addend(const Reloc* reloc)
2898 { return reloc->get_r_addend(); }
2899
2900 static inline void
2901 set_reloc_addend(Reloc_write* p,
2902 typename elfcpp::Elf_types<32>::Elf_Swxword val)
2903 { p->put_r_addend(val); }
2904};
2905
2906template<bool big_endian>
2907struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
2908{
2909 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
2910 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
2911
2912 static unsigned typename elfcpp::Elf_types<64>::Elf_Swxword
2913 get_r_addend(const Reloc*)
2914 { return 0; }
2915
2916 static inline void
2917 set_reloc_addend(Reloc_write*,
2918 typename elfcpp::Elf_types<64>::Elf_Swxword)
2919 { gold_unreachable(); }
2920};
2921
2922template<bool big_endian>
2923struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
2924{
2925 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
2926 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
2927
2928 static unsigned typename elfcpp::Elf_types<64>::Elf_Swxword
2929 get_r_addend(const Reloc* reloc)
2930 { return reloc->get_r_addend(); }
2931
2932 static inline void
2933 set_reloc_addend(Reloc_write* p,
2934 typename elfcpp::Elf_types<64>::Elf_Swxword val)
2935 { p->put_r_addend(val); }
2936};
2937
2938// Forward declaration.
2939static unsigned int
2940mips_get_size_for_reloc(unsigned int, Relobj*);
2941
2942// A class for inquiring about properties of a relocation,
2943// used while scanning relocs during a relocatable link and
2944// garbage collection.
2945
2946template<int sh_type_, int size, bool big_endian>
2947class Mips_classify_reloc;
2948
2949template<int sh_type_, bool big_endian>
2950class Mips_classify_reloc<sh_type_, 32, big_endian> :
2951 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
2952{
2953 public:
2954 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
2955 Reltype;
2956 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
2957 Reltype_write;
2958
2959 // Return the symbol referred to by the relocation.
2960 static inline unsigned int
2961 get_r_sym(const Reltype* reloc)
2962 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
2963
2964 // Return the type of the relocation.
2965 static inline unsigned int
2966 get_r_type(const Reltype* reloc)
2967 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
2968
2969 // Return the explicit addend of the relocation (return 0 for SHT_REL).
2970 static inline unsigned int
2971 get_r_addend(const Reltype* reloc)
8a8880cb
CC
2972 {
2973 if (sh_type_ == elfcpp::SHT_REL)
2974 return 0;
2975 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
2976 }
4d625b70
CC
2977
2978 // Write the r_info field to a new reloc, using the r_info field from
2979 // the original reloc, replacing the r_sym field with R_SYM.
2980 static inline void
2981 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
2982 {
2983 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
c2fa9ced 2984 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
4d625b70
CC
2985 }
2986
2987 // Write the r_addend field to a new reloc.
2988 static inline void
2989 put_r_addend(Reltype_write* to,
2990 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
2991 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
2992
2993 // Return the size of the addend of the relocation (only used for SHT_REL).
2994 static unsigned int
2995 get_size_for_reloc(unsigned int r_type, Relobj* obj)
2996 { return mips_get_size_for_reloc(r_type, obj); }
2997};
2998
2999template<int sh_type_, bool big_endian>
3000class Mips_classify_reloc<sh_type_, 64, big_endian> :
3001 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3002{
3003 public:
3004 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3005 Reltype;
3006 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3007 Reltype_write;
3008
3009 // Return the symbol referred to by the relocation.
3010 static inline unsigned int
3011 get_r_sym(const Reltype* reloc)
3012 { return reloc->get_r_sym(); }
3013
3014 // Return the type of the relocation.
3015 static inline unsigned int
3016 get_r_type(const Reltype* reloc)
3017 { return reloc->get_r_type(); }
3018
3019 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3020 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3021 get_r_addend(const Reltype* reloc)
8a8880cb
CC
3022 {
3023 if (sh_type_ == elfcpp::SHT_REL)
3024 return 0;
3025 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3026 }
4d625b70
CC
3027
3028 // Write the r_info field to a new reloc, using the r_info field from
3029 // the original reloc, replacing the r_sym field with R_SYM.
3030 static inline void
3031 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3032 {
3033 new_reloc->put_r_sym(r_sym);
3034 new_reloc->put_r_ssym(reloc->get_r_ssym());
3035 new_reloc->put_r_type3(reloc->get_r_type3());
3036 new_reloc->put_r_type2(reloc->get_r_type2());
3037 new_reloc->put_r_type(reloc->get_r_type());
3038 }
3039
3040 // Write the r_addend field to a new reloc.
3041 static inline void
3042 put_r_addend(Reltype_write* to,
3043 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3044 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3045
3046 // Return the size of the addend of the relocation (only used for SHT_REL).
3047 static unsigned int
3048 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3049 { return mips_get_size_for_reloc(r_type, obj); }
3050};
3051
9810d34d
SS
3052template<int size, bool big_endian>
3053class Target_mips : public Sized_target<size, big_endian>
3054{
3055 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3056 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
3057 Reloc_section;
3058 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3059 Reloca_section;
3060 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3061 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4d625b70
CC
3062 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3063 Reltype;
3064 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3065 Relatype;
9810d34d
SS
3066
3067 public:
3068 Target_mips(const Target::Target_info* info = &mips_info)
3069 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3070 got_plt_(NULL), rel_dyn_(NULL), copy_relocs_(),
3071 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3072 mips_stubs_(NULL), ei_class_(0), mach_(0), layout_(NULL),
3073 got16_addends_(), entry_symbol_is_compressed_(false), insn32_(false)
3074 {
3075 this->add_machine_extensions();
3076 }
3077
3078 // The offset of $gp from the beginning of the .got section.
3079 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3080
3081 // The maximum size of the GOT for it to be addressable using 16-bit
3082 // offsets from $gp.
3083 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3084
3085 // Make a new symbol table entry for the Mips target.
3086 Sized_symbol<size>*
dc1c8a16 3087 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
9810d34d
SS
3088 { return new Mips_symbol<size>(); }
3089
3090 // Process the relocations to determine unreferenced sections for
3091 // garbage collection.
3092 void
3093 gc_process_relocs(Symbol_table* symtab,
3094 Layout* layout,
3095 Sized_relobj_file<size, big_endian>* object,
3096 unsigned int data_shndx,
3097 unsigned int sh_type,
3098 const unsigned char* prelocs,
3099 size_t reloc_count,
3100 Output_section* output_section,
3101 bool needs_special_offset_handling,
3102 size_t local_symbol_count,
3103 const unsigned char* plocal_symbols);
3104
3105 // Scan the relocations to look for symbol adjustments.
3106 void
3107 scan_relocs(Symbol_table* symtab,
3108 Layout* layout,
3109 Sized_relobj_file<size, big_endian>* object,
3110 unsigned int data_shndx,
3111 unsigned int sh_type,
3112 const unsigned char* prelocs,
3113 size_t reloc_count,
3114 Output_section* output_section,
3115 bool needs_special_offset_handling,
3116 size_t local_symbol_count,
3117 const unsigned char* plocal_symbols);
3118
3119 // Finalize the sections.
3120 void
3121 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3122
3123 // Relocate a section.
3124 void
3125 relocate_section(const Relocate_info<size, big_endian>*,
3126 unsigned int sh_type,
3127 const unsigned char* prelocs,
3128 size_t reloc_count,
3129 Output_section* output_section,
3130 bool needs_special_offset_handling,
3131 unsigned char* view,
3132 Mips_address view_address,
3133 section_size_type view_size,
3134 const Reloc_symbol_changes*);
3135
3136 // Scan the relocs during a relocatable link.
3137 void
3138 scan_relocatable_relocs(Symbol_table* symtab,
3139 Layout* layout,
3140 Sized_relobj_file<size, big_endian>* object,
3141 unsigned int data_shndx,
3142 unsigned int sh_type,
3143 const unsigned char* prelocs,
3144 size_t reloc_count,
3145 Output_section* output_section,
3146 bool needs_special_offset_handling,
3147 size_t local_symbol_count,
3148 const unsigned char* plocal_symbols,
3149 Relocatable_relocs*);
3150
4d625b70
CC
3151 // Scan the relocs for --emit-relocs.
3152 void
3153 emit_relocs_scan(Symbol_table* symtab,
3154 Layout* layout,
3155 Sized_relobj_file<size, big_endian>* object,
3156 unsigned int data_shndx,
3157 unsigned int sh_type,
3158 const unsigned char* prelocs,
3159 size_t reloc_count,
3160 Output_section* output_section,
3161 bool needs_special_offset_handling,
3162 size_t local_symbol_count,
3163 const unsigned char* plocal_syms,
3164 Relocatable_relocs* rr);
3165
9810d34d
SS
3166 // Emit relocations for a section.
3167 void
3168 relocate_relocs(const Relocate_info<size, big_endian>*,
3169 unsigned int sh_type,
3170 const unsigned char* prelocs,
3171 size_t reloc_count,
3172 Output_section* output_section,
3173 typename elfcpp::Elf_types<size>::Elf_Off
3174 offset_in_output_section,
9810d34d
SS
3175 unsigned char* view,
3176 Mips_address view_address,
3177 section_size_type view_size,
3178 unsigned char* reloc_view,
3179 section_size_type reloc_view_size);
3180
3181 // Perform target-specific processing in a relocatable link. This is
3182 // only used if we use the relocation strategy RELOC_SPECIAL.
3183 void
3184 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3185 unsigned int sh_type,
3186 const unsigned char* preloc_in,
3187 size_t relnum,
3188 Output_section* output_section,
3189 typename elfcpp::Elf_types<size>::Elf_Off
3190 offset_in_output_section,
3191 unsigned char* view,
3192 Mips_address view_address,
3193 section_size_type view_size,
3194 unsigned char* preloc_out);
3195
3196 // Return whether SYM is defined by the ABI.
3197 bool
3198 do_is_defined_by_abi(const Symbol* sym) const
3199 {
3200 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3201 || (strcmp(sym->name(), "_gp_disp") == 0)
3202 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3203 }
3204
3205 // Return the number of entries in the GOT.
3206 unsigned int
3207 got_entry_count() const
3208 {
3209 if (!this->has_got_section())
3210 return 0;
3211 return this->got_size() / (size/8);
3212 }
3213
3214 // Return the number of entries in the PLT.
3215 unsigned int
3216 plt_entry_count() const
3217 {
3218 if (this->plt_ == NULL)
3219 return 0;
3220 return this->plt_->entry_count();
3221 }
3222
3223 // Return the offset of the first non-reserved PLT entry.
3224 unsigned int
3225 first_plt_entry_offset() const
3226 { return this->plt_->first_plt_entry_offset(); }
3227
3228 // Return the size of each PLT entry.
3229 unsigned int
3230 plt_entry_size() const
3231 { return this->plt_->plt_entry_size(); }
3232
3233 // Get the GOT section, creating it if necessary.
3234 Mips_output_data_got<size, big_endian>*
3235 got_section(Symbol_table*, Layout*);
3236
3237 // Get the GOT section.
3238 Mips_output_data_got<size, big_endian>*
3239 got_section() const
3240 {
3241 gold_assert(this->got_ != NULL);
3242 return this->got_;
3243 }
3244
3245 // Get the .MIPS.stubs section, creating it if necessary.
3246 Mips_output_data_mips_stubs<size, big_endian>*
3247 mips_stubs_section(Layout* layout);
3248
3249 // Get the .MIPS.stubs section.
3250 Mips_output_data_mips_stubs<size, big_endian>*
3251 mips_stubs_section() const
3252 {
3253 gold_assert(this->mips_stubs_ != NULL);
3254 return this->mips_stubs_;
3255 }
3256
3257 // Get the LA25 stub section, creating it if necessary.
3258 Mips_output_data_la25_stub<size, big_endian>*
3259 la25_stub_section(Layout*);
3260
3261 // Get the LA25 stub section.
3262 Mips_output_data_la25_stub<size, big_endian>*
3263 la25_stub_section()
3264 {
3265 gold_assert(this->la25_stub_ != NULL);
3266 return this->la25_stub_;
3267 }
3268
3269 // Get gp value. It has the value of .got + 0x7FF0.
3270 Mips_address
3271 gp_value() const
3272 {
3273 if (this->gp_ != NULL)
3274 return this->gp_->value();
3275 return 0;
3276 }
3277
3278 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3279 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3280 Mips_address
3281 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3282 {
3283 if (this->gp_ == NULL)
3284 return 0;
3285
3286 bool multi_got = false;
3287 if (this->has_got_section())
3288 multi_got = this->got_section()->multi_got();
3289 if (!multi_got)
3290 return this->gp_->value();
3291 else
3292 return this->gp_->value() + this->got_section()->get_got_offset(object);
3293 }
3294
3295 // Get the dynamic reloc section, creating it if necessary.
3296 Reloc_section*
3297 rel_dyn_section(Layout*);
3298
3299 bool
3300 do_has_custom_set_dynsym_indexes() const
3301 { return true; }
3302
3303 // Don't emit input .reginfo sections to output .reginfo.
3304 bool
3305 do_should_include_section(elfcpp::Elf_Word sh_type) const
3306 { return sh_type != elfcpp::SHT_MIPS_REGINFO; }
3307
3308 // Set the dynamic symbol indexes. INDEX is the index of the first
3309 // global dynamic symbol. Pointers to the symbols are stored into the
3310 // vector SYMS. The names are added to DYNPOOL. This returns an
3311 // updated dynamic symbol index.
3312 unsigned int
3313 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3314 std::vector<Symbol*>* syms, Stringpool* dynpool,
3315 Versions* versions, Symbol_table* symtab) const;
3316
3317 // Remove .MIPS.stubs entry for a symbol.
3318 void
3319 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3320 {
3321 if (this->mips_stubs_ != NULL)
3322 this->mips_stubs_->remove_entry(sym);
3323 }
3324
3325 // The value to write into got[1] for SVR4 targets, to identify it is
3326 // a GNU object. The dynamic linker can then use got[1] to store the
3327 // module pointer.
3328 uint64_t
3329 mips_elf_gnu_got1_mask()
3330 {
3331 if (this->is_output_n64())
3332 return (uint64_t)1 << 63;
3333 else
3334 return 1 << 31;
3335 }
3336
3337 // Whether the output has microMIPS code. This is valid only after
3338 // merge_processor_specific_flags() is called.
3339 bool
3340 is_output_micromips() const
3341 {
3342 gold_assert(this->are_processor_specific_flags_set());
3343 return elfcpp::is_micromips(this->processor_specific_flags());
3344 }
3345
3346 // Whether the output uses N32 ABI. This is valid only after
3347 // merge_processor_specific_flags() is called.
3348 bool
3349 is_output_n32() const
3350 {
3351 gold_assert(this->are_processor_specific_flags_set());
3352 return elfcpp::abi_n32(this->processor_specific_flags());
3353 }
3354
3355 // Whether the output uses N64 ABI. This is valid only after
3356 // merge_processor_specific_flags() is called.
3357 bool
3358 is_output_n64() const
3359 {
3360 gold_assert(this->are_processor_specific_flags_set());
3361 return elfcpp::abi_64(this->ei_class_);
3362 }
3363
3364 // Whether the output uses NEWABI. This is valid only after
3365 // merge_processor_specific_flags() is called.
3366 bool
3367 is_output_newabi() const
3368 { return this->is_output_n32() || this->is_output_n64(); }
3369
3370 // Whether we can only use 32-bit microMIPS instructions.
3371 bool
3372 use_32bit_micromips_instructions() const
3373 { return this->insn32_; }
3374
4d625b70
CC
3375 // Return the r_sym field from a relocation.
3376 unsigned int
3377 get_r_sym(const unsigned char* preloc) const
3378 {
3379 // Since REL and RELA relocs share the same structure through
3380 // the r_info field, we can just use REL here.
3381 Reltype rel(preloc);
3382 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3383 get_r_sym(&rel);
3384 }
3385
9810d34d
SS
3386 protected:
3387 // Return the value to use for a dynamic symbol which requires special
3388 // treatment. This is how we support equality comparisons of function
3389 // pointers across shared library boundaries, as described in the
3390 // processor specific ABI supplement.
3391 uint64_t
3392 do_dynsym_value(const Symbol* gsym) const;
3393
3394 // Make an ELF object.
3395 Object*
3396 do_make_elf_object(const std::string&, Input_file*, off_t,
3397 const elfcpp::Ehdr<size, big_endian>& ehdr);
3398
3399 Object*
3400 do_make_elf_object(const std::string&, Input_file*, off_t,
3401 const elfcpp::Ehdr<size, !big_endian>&)
3402 { gold_unreachable(); }
3403
3404 // Make an output section.
3405 Output_section*
3406 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3407 elfcpp::Elf_Xword flags)
3408 {
3409 if (type == elfcpp::SHT_MIPS_REGINFO)
3410 return new Mips_output_section_reginfo<size, big_endian>(name, type,
3411 flags, this);
3412 else
3413 return new Output_section(name, type, flags);
3414 }
3415
3416 // Adjust ELF file header.
3417 void
3418 do_adjust_elf_header(unsigned char* view, int len);
3419
3420 // Get the custom dynamic tag value.
3421 unsigned int
3422 do_dynamic_tag_custom_value(elfcpp::DT) const;
3423
3424 // Adjust the value written to the dynamic symbol table.
3425 virtual void
3426 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3427 {
3428 elfcpp::Sym<size, big_endian> isym(view);
3429 elfcpp::Sym_write<size, big_endian> osym(view);
3430 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3431
3432 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3433 // to treat compressed symbols like any other.
3434 Mips_address value = isym.get_st_value();
3435 if (mips_sym->is_mips16() && value != 0)
3436 {
3437 if (!mips_sym->has_mips16_fn_stub())
3438 value |= 1;
3439 else
3440 {
3441 // If we have a MIPS16 function with a stub, the dynamic symbol
3442 // must refer to the stub, since only the stub uses the standard
3443 // calling conventions. Stub contains MIPS32 code, so don't add +1
3444 // in this case.
3445
3446 // There is a code which does this in the method
3447 // Target_mips::do_dynsym_value, but that code will only be
3448 // executed if the symbol is from dynobj.
3449 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3450 // table.
3451
3452 Mips16_stub_section<size, big_endian>* fn_stub =
3453 mips_sym->template get_mips16_fn_stub<big_endian>();
3454 value = fn_stub->output_address();
3455 osym.put_st_size(fn_stub->section_size());
3456 }
3457
3458 osym.put_st_value(value);
3459 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3460 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3461 }
3462 else if ((mips_sym->is_micromips()
3463 // Stubs are always microMIPS if there is any microMIPS code in
3464 // the output.
3465 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3466 && value != 0)
3467 {
3468 osym.put_st_value(value | 1);
3469 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3470 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3471 }
3472 }
3473
3474 private:
3475 // The class which scans relocations.
3476 class Scan
3477 {
3478 public:
3479 Scan()
3480 { }
3481
3482 static inline int
3483 get_reference_flags(unsigned int r_type);
3484
3485 inline void
3486 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3487 Sized_relobj_file<size, big_endian>* object,
3488 unsigned int data_shndx,
3489 Output_section* output_section,
4d625b70 3490 const Reltype& reloc, unsigned int r_type,
9810d34d
SS
3491 const elfcpp::Sym<size, big_endian>& lsym,
3492 bool is_discarded);
3493
3494 inline void
3495 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3496 Sized_relobj_file<size, big_endian>* object,
3497 unsigned int data_shndx,
3498 Output_section* output_section,
4d625b70 3499 const Relatype& reloc, unsigned int r_type,
9810d34d
SS
3500 const elfcpp::Sym<size, big_endian>& lsym,
3501 bool is_discarded);
3502
3503 inline void
3504 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3505 Sized_relobj_file<size, big_endian>* object,
3506 unsigned int data_shndx,
3507 Output_section* output_section,
4d625b70
CC
3508 const Relatype* rela,
3509 const Reltype* rel,
9810d34d
SS
3510 unsigned int rel_type,
3511 unsigned int r_type,
3512 const elfcpp::Sym<size, big_endian>& lsym,
3513 bool is_discarded);
3514
3515 inline void
3516 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3517 Sized_relobj_file<size, big_endian>* object,
3518 unsigned int data_shndx,
3519 Output_section* output_section,
4d625b70 3520 const Reltype& reloc, unsigned int r_type,
9810d34d
SS
3521 Symbol* gsym);
3522
3523 inline void
3524 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3525 Sized_relobj_file<size, big_endian>* object,
3526 unsigned int data_shndx,
3527 Output_section* output_section,
4d625b70 3528 const Relatype& reloc, unsigned int r_type,
9810d34d
SS
3529 Symbol* gsym);
3530
3531 inline void
3532 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3533 Sized_relobj_file<size, big_endian>* object,
3534 unsigned int data_shndx,
3535 Output_section* output_section,
4d625b70
CC
3536 const Relatype* rela,
3537 const Reltype* rel,
9810d34d
SS
3538 unsigned int rel_type,
3539 unsigned int r_type,
3540 Symbol* gsym);
3541
3542 inline bool
3543 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3544 Target_mips*,
3545 Sized_relobj_file<size, big_endian>*,
3546 unsigned int,
3547 Output_section*,
4d625b70 3548 const Reltype&,
9810d34d
SS
3549 unsigned int,
3550 const elfcpp::Sym<size, big_endian>&)
3551 { return false; }
3552
3553 inline bool
3554 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3555 Target_mips*,
3556 Sized_relobj_file<size, big_endian>*,
3557 unsigned int,
3558 Output_section*,
4d625b70 3559 const Reltype&,
9810d34d
SS
3560 unsigned int, Symbol*)
3561 { return false; }
3562
3563 inline bool
3564 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3565 Target_mips*,
3566 Sized_relobj_file<size, big_endian>*,
3567 unsigned int,
3568 Output_section*,
4d625b70 3569 const Relatype&,
9810d34d
SS
3570 unsigned int,
3571 const elfcpp::Sym<size, big_endian>&)
3572 { return false; }
3573
3574 inline bool
3575 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3576 Target_mips*,
3577 Sized_relobj_file<size, big_endian>*,
3578 unsigned int,
3579 Output_section*,
4d625b70 3580 const Relatype&,
9810d34d
SS
3581 unsigned int, Symbol*)
3582 { return false; }
3583 private:
3584 static void
3585 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3586 unsigned int r_type);
3587
3588 static void
3589 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3590 unsigned int r_type, Symbol*);
3591 };
3592
3593 // The class which implements relocation.
3594 class Relocate
3595 {
3596 public:
3597 Relocate()
3598 { }
3599
3600 ~Relocate()
3601 { }
3602
3603 // Return whether the R_MIPS_32 relocation needs to be applied.
3604 inline bool
3605 should_apply_r_mips_32_reloc(const Mips_symbol<size>* gsym,
3606 unsigned int r_type,
3607 Output_section* output_section,
3608 Target_mips* target);
3609
3610 // Do a relocation. Return false if the caller should not issue
3611 // any warnings about this relocation.
3612 inline bool
91a65d2f
AM
3613 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3614 Target_mips*, Output_section*, size_t, const unsigned char*,
3615 const Sized_symbol<size>*, const Symbol_value<size>*,
3616 unsigned char*, Mips_address, section_size_type);
9810d34d
SS
3617 };
3618
9810d34d
SS
3619 // This POD class holds the dynamic relocations that should be emitted instead
3620 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3621 // relocations if it turns out that the symbol does not have static
3622 // relocations.
3623 class Dyn_reloc
3624 {
3625 public:
3626 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3627 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3628 Output_section* output_section, Mips_address r_offset)
3629 : sym_(sym), r_type_(r_type), relobj_(relobj),
3630 shndx_(shndx), output_section_(output_section),
3631 r_offset_(r_offset)
3632 { }
3633
3634 // Emit this reloc if appropriate. This is called after we have
3635 // scanned all the relocations, so we know whether the symbol has
3636 // static relocations.
3637 void
3638 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3639 Symbol_table* symtab)
3640 {
3641 if (!this->sym_->has_static_relocs())
3642 {
3643 got->record_global_got_symbol(this->sym_, this->relobj_,
3644 this->r_type_, true, false);
3645 if (!symbol_references_local(this->sym_,
3646 this->sym_->should_add_dynsym_entry(symtab)))
3647 rel_dyn->add_global(this->sym_, this->r_type_,
3648 this->output_section_, this->relobj_,
3649 this->shndx_, this->r_offset_);
3650 else
3651 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3652 this->output_section_, this->relobj_,
3653 this->shndx_, this->r_offset_);
3654 }
3655 }
3656
3657 private:
3658 Mips_symbol<size>* sym_;
3659 unsigned int r_type_;
3660 Mips_relobj<size, big_endian>* relobj_;
3661 unsigned int shndx_;
3662 Output_section* output_section_;
3663 Mips_address r_offset_;
3664 };
3665
3666 // Adjust TLS relocation type based on the options and whether this
3667 // is a local symbol.
3668 static tls::Tls_optimization
3669 optimize_tls_reloc(bool is_final, int r_type);
3670
3671 // Return whether there is a GOT section.
3672 bool
3673 has_got_section() const
3674 { return this->got_ != NULL; }
3675
3676 // Check whether the given ELF header flags describe a 32-bit binary.
3677 bool
3678 mips_32bit_flags(elfcpp::Elf_Word);
3679
3680 enum Mips_mach {
3681 mach_mips3000 = 3000,
3682 mach_mips3900 = 3900,
3683 mach_mips4000 = 4000,
3684 mach_mips4010 = 4010,
3685 mach_mips4100 = 4100,
3686 mach_mips4111 = 4111,
3687 mach_mips4120 = 4120,
3688 mach_mips4300 = 4300,
3689 mach_mips4400 = 4400,
3690 mach_mips4600 = 4600,
3691 mach_mips4650 = 4650,
3692 mach_mips5000 = 5000,
3693 mach_mips5400 = 5400,
3694 mach_mips5500 = 5500,
3695 mach_mips6000 = 6000,
3696 mach_mips7000 = 7000,
3697 mach_mips8000 = 8000,
3698 mach_mips9000 = 9000,
3699 mach_mips10000 = 10000,
3700 mach_mips12000 = 12000,
3701 mach_mips14000 = 14000,
3702 mach_mips16000 = 16000,
3703 mach_mips16 = 16,
3704 mach_mips5 = 5,
3705 mach_mips_loongson_2e = 3001,
3706 mach_mips_loongson_2f = 3002,
3707 mach_mips_loongson_3a = 3003,
3708 mach_mips_sb1 = 12310201, // octal 'SB', 01
3709 mach_mips_octeon = 6501,
3710 mach_mips_octeonp = 6601,
3711 mach_mips_octeon2 = 6502,
3712 mach_mips_xlr = 887682, // decimal 'XLR'
3713 mach_mipsisa32 = 32,
3714 mach_mipsisa32r2 = 33,
3715 mach_mipsisa64 = 64,
3716 mach_mipsisa64r2 = 65,
3717 mach_mips_micromips = 96
3718 };
3719
3720 // Return the MACH for a MIPS e_flags value.
3721 unsigned int
3722 elf_mips_mach(elfcpp::Elf_Word);
3723
3724 // Check whether machine EXTENSION is an extension of machine BASE.
3725 bool
3726 mips_mach_extends(unsigned int, unsigned int);
3727
3728 // Merge processor specific flags.
3729 void
3730 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word,
3731 unsigned char, bool);
3732
3733 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
3734 static inline bool
3735 jal_to_bal()
3736 { return false; }
3737
3738 // True if we are linking for CPUs that are faster if JALR is converted to
3739 // BAL. This should be safe for all architectures. We enable this predicate
3740 // for all CPUs.
3741 static inline bool
3742 jalr_to_bal()
3743 { return true; }
3744
3745 // True if we are linking for CPUs that are faster if JR is converted to B.
3746 // This should be safe for all architectures. We enable this predicate for
3747 // all CPUs.
3748 static inline bool
3749 jr_to_b()
3750 { return true; }
3751
3752 // Return the size of the GOT section.
3753 section_size_type
3754 got_size() const
3755 {
3756 gold_assert(this->got_ != NULL);
3757 return this->got_->data_size();
3758 }
3759
3760 // Create a PLT entry for a global symbol referenced by r_type relocation.
3761 void
3762 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
3763 unsigned int r_type);
3764
3765 // Get the PLT section.
3766 Mips_output_data_plt<size, big_endian>*
3767 plt_section() const
3768 {
3769 gold_assert(this->plt_ != NULL);
3770 return this->plt_;
3771 }
3772
3773 // Get the GOT PLT section.
3774 const Mips_output_data_plt<size, big_endian>*
3775 got_plt_section() const
3776 {
3777 gold_assert(this->got_plt_ != NULL);
3778 return this->got_plt_;
3779 }
3780
3781 // Copy a relocation against a global symbol.
3782 void
3783 copy_reloc(Symbol_table* symtab, Layout* layout,
3784 Sized_relobj_file<size, big_endian>* object,
3785 unsigned int shndx, Output_section* output_section,
4d625b70 3786 Symbol* sym, const Reltype& reloc)
9810d34d 3787 {
4d625b70
CC
3788 unsigned int r_type =
3789 Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3790 get_r_type(&reloc);
9810d34d
SS
3791 this->copy_relocs_.copy_reloc(symtab, layout,
3792 symtab->get_sized_symbol<size>(sym),
3793 object, shndx, output_section,
859d7987
CC
3794 r_type, reloc.get_r_offset(), 0,
3795 this->rel_dyn_section(layout));
9810d34d
SS
3796 }
3797
3798 void
3799 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3800 Mips_relobj<size, big_endian>* relobj,
3801 unsigned int shndx, Output_section* output_section,
3802 Mips_address r_offset)
3803 {
3804 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
3805 output_section, r_offset));
3806 }
3807
3808 // Calculate value of _gp symbol.
3809 void
3810 set_gp(Layout*, Symbol_table*);
3811
3812 const char*
3813 elf_mips_abi_name(elfcpp::Elf_Word e_flags, unsigned char ei_class);
3814 const char*
3815 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
3816
3817 // Adds entries that describe how machines relate to one another. The entries
3818 // are ordered topologically with MIPS I extensions listed last. First
3819 // element is extension, second element is base.
3820 void
3821 add_machine_extensions()
3822 {
3823 // MIPS64r2 extensions.
3824 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
3825 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
3826 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
3827
3828 // MIPS64 extensions.
3829 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
3830 this->add_extension(mach_mips_sb1, mach_mipsisa64);
3831 this->add_extension(mach_mips_xlr, mach_mipsisa64);
3832 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64);
3833
3834 // MIPS V extensions.
3835 this->add_extension(mach_mipsisa64, mach_mips5);
3836
3837 // R10000 extensions.
3838 this->add_extension(mach_mips12000, mach_mips10000);
3839 this->add_extension(mach_mips14000, mach_mips10000);
3840 this->add_extension(mach_mips16000, mach_mips10000);
3841
3842 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
3843 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
3844 // better to allow vr5400 and vr5500 code to be merged anyway, since
3845 // many libraries will just use the core ISA. Perhaps we could add
3846 // some sort of ASE flag if this ever proves a problem.
3847 this->add_extension(mach_mips5500, mach_mips5400);
3848 this->add_extension(mach_mips5400, mach_mips5000);
3849
3850 // MIPS IV extensions.
3851 this->add_extension(mach_mips5, mach_mips8000);
3852 this->add_extension(mach_mips10000, mach_mips8000);
3853 this->add_extension(mach_mips5000, mach_mips8000);
3854 this->add_extension(mach_mips7000, mach_mips8000);
3855 this->add_extension(mach_mips9000, mach_mips8000);
3856
3857 // VR4100 extensions.
3858 this->add_extension(mach_mips4120, mach_mips4100);
3859 this->add_extension(mach_mips4111, mach_mips4100);
3860
3861 // MIPS III extensions.
3862 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
3863 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
3864 this->add_extension(mach_mips8000, mach_mips4000);
3865 this->add_extension(mach_mips4650, mach_mips4000);
3866 this->add_extension(mach_mips4600, mach_mips4000);
3867 this->add_extension(mach_mips4400, mach_mips4000);
3868 this->add_extension(mach_mips4300, mach_mips4000);
3869 this->add_extension(mach_mips4100, mach_mips4000);
3870 this->add_extension(mach_mips4010, mach_mips4000);
3871
3872 // MIPS32 extensions.
3873 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
3874
3875 // MIPS II extensions.
3876 this->add_extension(mach_mips4000, mach_mips6000);
3877 this->add_extension(mach_mipsisa32, mach_mips6000);
3878
3879 // MIPS I extensions.
3880 this->add_extension(mach_mips6000, mach_mips3000);
3881 this->add_extension(mach_mips3900, mach_mips3000);
3882 }
3883
3884 // Add value to MIPS extenstions.
3885 void
3886 add_extension(unsigned int base, unsigned int extension)
3887 {
3888 std::pair<unsigned int, unsigned int> ext(base, extension);
3889 this->mips_mach_extensions_.push_back(ext);
3890 }
3891
3892 // Return the number of entries in the .dynsym section.
3893 unsigned int get_dt_mips_symtabno() const
3894 {
3895 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
3896 / elfcpp::Elf_sizes<size>::sym_size));
3897 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
3898 }
3899
3900 // Information about this specific target which we pass to the
3901 // general Target structure.
62661c93 3902 static const Target::Target_info mips_info;
9810d34d
SS
3903 // The GOT section.
3904 Mips_output_data_got<size, big_endian>* got_;
3905 // gp symbol. It has the value of .got + 0x7FF0.
3906 Sized_symbol<size>* gp_;
3907 // The PLT section.
3908 Mips_output_data_plt<size, big_endian>* plt_;
3909 // The GOT PLT section.
3910 Output_data_space* got_plt_;
3911 // The dynamic reloc section.
3912 Reloc_section* rel_dyn_;
3913 // Relocs saved to avoid a COPY reloc.
3914 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
3915
3916 // A list of dyn relocs to be saved.
3917 std::vector<Dyn_reloc> dyn_relocs_;
3918
3919 // The LA25 stub section.
3920 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
3921 // Architecture extensions.
3922 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
3923 // .MIPS.stubs
3924 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
3925
3926 unsigned char ei_class_;
3927 unsigned int mach_;
3928 Layout* layout_;
3929
3930 typename std::list<got16_addend<size, big_endian> > got16_addends_;
3931
3932 // Whether the entry symbol is mips16 or micromips.
3933 bool entry_symbol_is_compressed_;
3934
3935 // Whether we can use only 32-bit microMIPS instructions.
3936 // TODO(sasa): This should be a linker option.
3937 bool insn32_;
3938};
3939
9810d34d
SS
3940// Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
3941// It records high part of the relocation pair.
3942
3943template<int size, bool big_endian>
3944struct reloc_high
3945{
3946 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3947
3948 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
3949 const Symbol_value<size>* _psymval, Mips_address _addend,
3d0064a9 3950 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
9810d34d
SS
3951 Mips_address _address = 0, bool _gp_disp = false)
3952 : view(_view), object(_object), psymval(_psymval), addend(_addend),
3d0064a9
CC
3953 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
3954 address(_address), gp_disp(_gp_disp)
9810d34d
SS
3955 { }
3956
3957 unsigned char* view;
3958 const Mips_relobj<size, big_endian>* object;
3959 const Symbol_value<size>* psymval;
3960 Mips_address addend;
3961 unsigned int r_type;
3d0064a9 3962 unsigned int r_sym;
9810d34d
SS
3963 bool extract_addend;
3964 Mips_address address;
3965 bool gp_disp;
3966};
3967
3968template<int size, bool big_endian>
3969class Mips_relocate_functions : public Relocate_functions<size, big_endian>
3970{
3971 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3972 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
3973 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3974
3975 public:
3976 typedef enum
3977 {
3978 STATUS_OKAY, // No error during relocation.
3979 STATUS_OVERFLOW, // Relocation overflow.
3980 STATUS_BAD_RELOC // Relocation cannot be applied.
3981 } Status;
3982
3983 private:
3984 typedef Relocate_functions<size, big_endian> Base;
3985 typedef Mips_relocate_functions<size, big_endian> This;
3986
3987 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
3988 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
3989
3990 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
3991 // Most mips16 instructions are 16 bits, but these instructions
3992 // are 32 bits.
3993 //
3994 // The format of these instructions is:
3995 //
3996 // +--------------+--------------------------------+
3997 // | JALX | X| Imm 20:16 | Imm 25:21 |
3998 // +--------------+--------------------------------+
3999 // | Immediate 15:0 |
4000 // +-----------------------------------------------+
4001 //
4002 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4003 // Note that the immediate value in the first word is swapped.
4004 //
4005 // When producing a relocatable object file, R_MIPS16_26 is
4006 // handled mostly like R_MIPS_26. In particular, the addend is
4007 // stored as a straight 26-bit value in a 32-bit instruction.
4008 // (gas makes life simpler for itself by never adjusting a
4009 // R_MIPS16_26 reloc to be against a section, so the addend is
4010 // always zero). However, the 32 bit instruction is stored as 2
4011 // 16-bit values, rather than a single 32-bit value. In a
4012 // big-endian file, the result is the same; in a little-endian
4013 // file, the two 16-bit halves of the 32 bit value are swapped.
4014 // This is so that a disassembler can recognize the jal
4015 // instruction.
4016 //
4017 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4018 // instruction stored as two 16-bit values. The addend A is the
4019 // contents of the targ26 field. The calculation is the same as
4020 // R_MIPS_26. When storing the calculated value, reorder the
4021 // immediate value as shown above, and don't forget to store the
4022 // value as two 16-bit values.
4023 //
4024 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4025 // defined as
4026 //
4027 // big-endian:
4028 // +--------+----------------------+
4029 // | | |
4030 // | | targ26-16 |
4031 // |31 26|25 0|
4032 // +--------+----------------------+
4033 //
4034 // little-endian:
4035 // +----------+------+-------------+
4036 // | | | |
4037 // | sub1 | | sub2 |
4038 // |0 9|10 15|16 31|
4039 // +----------+--------------------+
4040 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4041 // ((sub1 << 16) | sub2)).
4042 //
4043 // When producing a relocatable object file, the calculation is
4044 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4045 // When producing a fully linked file, the calculation is
4046 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4047 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4048 //
4049 // The table below lists the other MIPS16 instruction relocations.
4050 // Each one is calculated in the same way as the non-MIPS16 relocation
4051 // given on the right, but using the extended MIPS16 layout of 16-bit
4052 // immediate fields:
4053 //
4054 // R_MIPS16_GPREL R_MIPS_GPREL16
4055 // R_MIPS16_GOT16 R_MIPS_GOT16
4056 // R_MIPS16_CALL16 R_MIPS_CALL16
4057 // R_MIPS16_HI16 R_MIPS_HI16
4058 // R_MIPS16_LO16 R_MIPS_LO16
4059 //
4060 // A typical instruction will have a format like this:
4061 //
4062 // +--------------+--------------------------------+
4063 // | EXTEND | Imm 10:5 | Imm 15:11 |
4064 // +--------------+--------------------------------+
4065 // | Major | rx | ry | Imm 4:0 |
4066 // +--------------+--------------------------------+
4067 //
4068 // EXTEND is the five bit value 11110. Major is the instruction
4069 // opcode.
4070 //
4071 // All we need to do here is shuffle the bits appropriately.
4072 // As above, the two 16-bit halves must be swapped on a
4073 // little-endian system.
4074
4075 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4076 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4077 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4078
4079 static inline bool
4080 should_shuffle_micromips_reloc(unsigned int r_type)
4081 {
4082 return (micromips_reloc(r_type)
4083 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4084 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4085 }
4086
4087 static void
4088 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4089 bool jal_shuffle)
4090 {
4091 if (!mips16_reloc(r_type)
4092 && !should_shuffle_micromips_reloc(r_type))
4093 return;
4094
4095 // Pick up the first and second halfwords of the instruction.
4096 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4097 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4098 Valtype32 val;
4099
4100 if (micromips_reloc(r_type)
4101 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4102 val = first << 16 | second;
4103 else if (r_type != elfcpp::R_MIPS16_26)
4104 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4105 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4106 else
4107 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4108 | ((first & 0x1f) << 21) | second);
4109
4110 elfcpp::Swap<32, big_endian>::writeval(view, val);
4111 }
4112
4113 static void
4114 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4115 {
4116 if (!mips16_reloc(r_type)
4117 && !should_shuffle_micromips_reloc(r_type))
4118 return;
4119
4120 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4121 Valtype16 first, second;
4122
4123 if (micromips_reloc(r_type)
4124 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4125 {
4126 second = val & 0xffff;
4127 first = val >> 16;
4128 }
4129 else if (r_type != elfcpp::R_MIPS16_26)
4130 {
4131 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4132 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4133 }
4134 else
4135 {
4136 second = val & 0xffff;
4137 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4138 | ((val >> 21) & 0x1f);
4139 }
4140
4141 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4142 elfcpp::Swap<16, big_endian>::writeval(view, first);
4143 }
4144
4145 public:
4146 // R_MIPS_16: S + sign-extend(A)
4147 static inline typename This::Status
4148 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4149 const Symbol_value<size>* psymval, Mips_address addend_a,
4150 bool extract_addend, unsigned int r_type)
4151 {
4152 mips_reloc_unshuffle(view, r_type, false);
4153 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4154 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4155
4156 Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val)
4157 : Bits<16>::sign_extend32(addend_a));
4158
4159 Valtype32 x = psymval->value(object, addend);
4160 val = Bits<16>::bit_select32(val, x, 0xffffU);
4161 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4162 mips_reloc_shuffle(view, r_type, false);
4163 return (Bits<16>::has_overflow32(x)
4164 ? This::STATUS_OVERFLOW
4165 : This::STATUS_OKAY);
4166 }
4167
4168 // R_MIPS_32: S + A
4169 static inline typename This::Status
4170 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4171 const Symbol_value<size>* psymval, Mips_address addend_a,
4172 bool extract_addend, unsigned int r_type)
4173 {
4174 mips_reloc_unshuffle(view, r_type, false);
4175 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4176 Valtype32 addend = (extract_addend
4177 ? elfcpp::Swap<32, big_endian>::readval(wv)
4178 : Bits<32>::sign_extend32(addend_a));
4179 Valtype32 x = psymval->value(object, addend);
4180 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4181 mips_reloc_shuffle(view, r_type, false);
4182 return This::STATUS_OKAY;
4183 }
4184
4185 // R_MIPS_JALR, R_MICROMIPS_JALR
4186 static inline typename This::Status
4187 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4188 const Symbol_value<size>* psymval, Mips_address address,
4189 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
4190 unsigned int r_type, bool jalr_to_bal, bool jr_to_b)
4191 {
4192 mips_reloc_unshuffle(view, r_type, false);
4193 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4194 Valtype32 addend = extract_addend ? 0 : addend_a;
4195 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4196
4197 // Try converting J(AL)R to B(AL), if the target is in range.
4198 if (!parameters->options().relocatable()
4199 && r_type == elfcpp::R_MIPS_JALR
4200 && !cross_mode_jump
4201 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4202 || (jr_to_b && val == 0x03200008))) // jr t9
4203 {
4204 int offset = psymval->value(object, addend) - (address + 4);
4205 if (!Bits<18>::has_overflow32(offset))
4206 {
4207 if (val == 0x03200008) // jr t9
4208 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4209 else
4210 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4211 }
4212 }
4213
4214 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4215 mips_reloc_shuffle(view, r_type, false);
4216 return This::STATUS_OKAY;
4217 }
4218
4219 // R_MIPS_PC32: S + A - P
4220 static inline typename This::Status
4221 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4222 const Symbol_value<size>* psymval, Mips_address address,
4223 Mips_address addend_a, bool extract_addend, unsigned int r_type)
4224 {
4225 mips_reloc_unshuffle(view, r_type, false);
4226 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4227 Valtype32 addend = (extract_addend
4228 ? elfcpp::Swap<32, big_endian>::readval(wv)
4229 : Bits<32>::sign_extend32(addend_a));
4230 Valtype32 x = psymval->value(object, addend) - address;
4231 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4232 mips_reloc_shuffle(view, r_type, false);
4233 return This::STATUS_OKAY;
4234 }
4235
4236 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4237 static inline typename This::Status
4238 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4239 const Symbol_value<size>* psymval, Mips_address address,
4240 bool local, Mips_address addend_a, bool extract_addend,
4241 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
4242 bool jal_to_bal)
4243 {
4244 mips_reloc_unshuffle(view, r_type, false);
4245 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4246 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4247
4248 Valtype32 addend;
4249 if (extract_addend)
4250 {
4251 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4252 addend = (val & 0x03ffffff) << 1;
4253 else
4254 addend = (val & 0x03ffffff) << 2;
4255 }
4256 else
4257 addend = addend_a;
4258
4259 // Make sure the target of JALX is word-aligned. Bit 0 must be
4260 // the correct ISA mode selector and bit 1 must be 0.
4261 if (cross_mode_jump
4262 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4263 {
4264 gold_warning(_("JALX to a non-word-aligned address"));
4265 mips_reloc_shuffle(view, r_type, !parameters->options().relocatable());
4266 return This::STATUS_BAD_RELOC;
4267 }
4268
4269 // Shift is 2, unusually, for microMIPS JALX.
4270 unsigned int shift =
4271 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4272
4273 Valtype32 x;
4274 if (local)
4275 x = addend | ((address + 4) & (0xfc000000 << shift));
4276 else
4277 {
4278 if (shift == 1)
4279 x = Bits<27>::sign_extend32(addend);
4280 else
4281 x = Bits<28>::sign_extend32(addend);
4282 }
4283 x = psymval->value(object, x) >> shift;
4284
4285 if (!local && !gsym->is_weak_undefined())
4286 {
4287 if ((x >> 26) != ((address + 4) >> (26 + shift)))
4288 {
4289 gold_error(_("relocation truncated to fit: %u against '%s'"),
4290 r_type, gsym->name());
4291 return This::STATUS_OVERFLOW;
4292 }
4293 }
4294
4295 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4296
4297 // If required, turn JAL into JALX.
4298 if (cross_mode_jump)
4299 {
4300 bool ok;
4301 Valtype32 opcode = val >> 26;
4302 Valtype32 jalx_opcode;
4303
4304 // Check to see if the opcode is already JAL or JALX.
4305 if (r_type == elfcpp::R_MIPS16_26)
4306 {
4307 ok = (opcode == 0x6) || (opcode == 0x7);
4308 jalx_opcode = 0x7;
4309 }
4310 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4311 {
4312 ok = (opcode == 0x3d) || (opcode == 0x3c);
4313 jalx_opcode = 0x3c;
4314 }
4315 else
4316 {
4317 ok = (opcode == 0x3) || (opcode == 0x1d);
4318 jalx_opcode = 0x1d;
4319 }
4320
4321 // If the opcode is not JAL or JALX, there's a problem. We cannot
4322 // convert J or JALS to JALX.
4323 if (!ok)
4324 {
4325 gold_error(_("Unsupported jump between ISA modes; consider "
4326 "recompiling with interlinking enabled."));
4327 return This::STATUS_BAD_RELOC;
4328 }
4329
4330 // Make this the JALX opcode.
4331 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4332 }
4333
4334 // Try converting JAL to BAL, if the target is in range.
4335 if (!parameters->options().relocatable()
4336 && !cross_mode_jump
4337 && ((jal_to_bal
4338 && r_type == elfcpp::R_MIPS_26
4339 && (val >> 26) == 0x3))) // jal addr
4340 {
4341 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4342 int offset = dest - (address + 4);
4343 if (!Bits<18>::has_overflow32(offset))
4344 {
4345 if (val == 0x03200008) // jr t9
4346 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4347 else
4348 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4349 }
4350 }
4351
4352 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4353 mips_reloc_shuffle(view, r_type, !parameters->options().relocatable());
4354 return This::STATUS_OKAY;
4355 }
4356
4357 // R_MIPS_PC16
4358 static inline typename This::Status
4359 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4360 const Symbol_value<size>* psymval, Mips_address address,
4361 Mips_address addend_a, bool extract_addend, unsigned int r_type)
4362 {
4363 mips_reloc_unshuffle(view, r_type, false);
4364 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4365 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4366
4367 Valtype32 addend = extract_addend ? (val & 0xffff) << 2 : addend_a;
4368 addend = Bits<18>::sign_extend32(addend);
4369
4370 Valtype32 x = psymval->value(object, addend) - address;
4371 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
4372 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4373 mips_reloc_shuffle(view, r_type, false);
4374 return (Bits<18>::has_overflow32(x)
4375 ? This::STATUS_OVERFLOW
4376 : This::STATUS_OKAY);
4377 }
4378
4379 // R_MICROMIPS_PC7_S1
4380 static inline typename This::Status
4381 relmicromips_pc7_s1(unsigned char* view,
4382 const Mips_relobj<size, big_endian>* object,
4383 const Symbol_value<size>* psymval, Mips_address address,
4384 Mips_address addend_a, bool extract_addend,
4385 unsigned int r_type)
4386 {
4387 mips_reloc_unshuffle(view, r_type, false);
4388 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4389 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4390
4391 Valtype32 addend = extract_addend ? (val & 0x7f) << 1 : addend_a;
4392 addend = Bits<8>::sign_extend32(addend);
4393
4394 Valtype32 x = psymval->value(object, addend) - address;
4395 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
4396 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4397 mips_reloc_shuffle(view, r_type, false);
4398 return (Bits<8>::has_overflow32(x)
4399 ? This::STATUS_OVERFLOW
4400 : This::STATUS_OKAY);
4401 }
4402
4403 // R_MICROMIPS_PC10_S1
4404 static inline typename This::Status
4405 relmicromips_pc10_s1(unsigned char* view,
4406 const Mips_relobj<size, big_endian>* object,
4407 const Symbol_value<size>* psymval, Mips_address address,
4408 Mips_address addend_a, bool extract_addend,
4409 unsigned int r_type)
4410 {
4411 mips_reloc_unshuffle(view, r_type, false);
4412 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4413 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4414
4415 Valtype32 addend = extract_addend ? (val & 0x3ff) << 1 : addend_a;
4416 addend = Bits<11>::sign_extend32(addend);
4417
4418 Valtype32 x = psymval->value(object, addend) - address;
4419 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
4420 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4421 mips_reloc_shuffle(view, r_type, false);
4422 return (Bits<11>::has_overflow32(x)
4423 ? This::STATUS_OVERFLOW
4424 : This::STATUS_OKAY);
4425 }
4426
4427 // R_MICROMIPS_PC16_S1
4428 static inline typename This::Status
4429 relmicromips_pc16_s1(unsigned char* view,
4430 const Mips_relobj<size, big_endian>* object,
4431 const Symbol_value<size>* psymval, Mips_address address,
4432 Mips_address addend_a, bool extract_addend,
4433 unsigned int r_type)
4434 {
4435 mips_reloc_unshuffle(view, r_type, false);
4436 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4437 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4438
4439 Valtype32 addend = extract_addend ? (val & 0xffff) << 1 : addend_a;
4440 addend = Bits<17>::sign_extend32(addend);
4441
4442 Valtype32 x = psymval->value(object, addend) - address;
4443 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
4444 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4445 mips_reloc_shuffle(view, r_type, false);
4446 return (Bits<17>::has_overflow32(x)
4447 ? This::STATUS_OVERFLOW
4448 : This::STATUS_OKAY);
4449 }
4450
4451 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4452 static inline typename This::Status
4453 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4454 const Symbol_value<size>* psymval, Mips_address addend,
4455 Mips_address address, bool gp_disp, unsigned int r_type,
3d0064a9 4456 unsigned int r_sym, bool extract_addend)
9810d34d
SS
4457 {
4458 // Record the relocation. It will be resolved when we find lo16 part.
4459 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
3d0064a9
CC
4460 addend, r_type, r_sym, extract_addend, address,
4461 gp_disp));
9810d34d
SS
4462 return This::STATUS_OKAY;
4463 }
4464
4465 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4466 static inline typename This::Status
4467 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4468 const Symbol_value<size>* psymval, Mips_address addend_hi,
4469 Mips_address address, bool is_gp_disp, unsigned int r_type,
4470 bool extract_addend, Valtype32 addend_lo,
4471 Target_mips<size, big_endian>* target)
4472 {
4473 mips_reloc_unshuffle(view, r_type, false);
4474 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4475 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4476
4477 Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4478 : addend_hi);
4479
4480 Valtype32 value;
4481 if (!is_gp_disp)
4482 value = psymval->value(object, addend);
4483 else
4484 {
4485 // For MIPS16 ABI code we generate this sequence
4486 // 0: li $v0,%hi(_gp_disp)
4487 // 4: addiupc $v1,%lo(_gp_disp)
4488 // 8: sll $v0,16
4489 // 12: addu $v0,$v1
4490 // 14: move $gp,$v0
4491 // So the offsets of hi and lo relocs are the same, but the
4492 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
4493 // ADDIUPC clears the low two bits of the instruction address,
4494 // so the base is ($t9 + 4) & ~3.
4495 Valtype32 gp_disp;
4496 if (r_type == elfcpp::R_MIPS16_HI16)
4497 gp_disp = (target->adjusted_gp_value(object)
4498 - ((address + 4) & ~0x3));
4499 // The microMIPS .cpload sequence uses the same assembly
4500 // instructions as the traditional psABI version, but the
4501 // incoming $t9 has the low bit set.
4502 else if (r_type == elfcpp::R_MICROMIPS_HI16)
4503 gp_disp = target->adjusted_gp_value(object) - address - 1;
4504 else
4505 gp_disp = target->adjusted_gp_value(object) - address;
4506 value = gp_disp + addend;
4507 }
4508 Valtype32 x = ((value + 0x8000) >> 16) & 0xffff;
4509 val = Bits<32>::bit_select32(val, x, 0xffff);
4510 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4511 mips_reloc_shuffle(view, r_type, false);
4512 return (is_gp_disp && Bits<16>::has_overflow32(x)
4513 ? This::STATUS_OVERFLOW
4514 : This::STATUS_OKAY);
4515 }
4516
4517 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4518 static inline typename This::Status
4519 relgot16_local(unsigned char* view,
4520 const Mips_relobj<size, big_endian>* object,
4521 const Symbol_value<size>* psymval, Mips_address addend_a,
3d0064a9 4522 bool extract_addend, unsigned int r_type, unsigned int r_sym)
9810d34d
SS
4523 {
4524 // Record the relocation. It will be resolved when we find lo16 part.
4525 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
3d0064a9 4526 addend_a, r_type, r_sym, extract_addend));
9810d34d
SS
4527 return This::STATUS_OKAY;
4528 }
4529
4530 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4531 static inline typename This::Status
4532 do_relgot16_local(unsigned char* view,
4533 const Mips_relobj<size, big_endian>* object,
4534 const Symbol_value<size>* psymval, Mips_address addend_hi,
4535 unsigned int r_type, bool extract_addend,
4536 Valtype32 addend_lo, Target_mips<size, big_endian>* target)
4537 {
4538 mips_reloc_unshuffle(view, r_type, false);
4539 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4540 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4541
4542 Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4543 : addend_hi);
4544
4545 // Find GOT page entry.
4546 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
4547 & 0xffff;
4548 value <<= 16;
4549 unsigned int got_offset =
4550 target->got_section()->get_got_page_offset(value, object);
4551
4552 // Resolve the relocation.
4553 Valtype32 x = target->got_section()->gp_offset(got_offset, object);
4554 val = Bits<32>::bit_select32(val, x, 0xffff);
4555 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4556 mips_reloc_shuffle(view, r_type, false);
4557 return (Bits<16>::has_overflow32(x)
4558 ? This::STATUS_OVERFLOW
4559 : This::STATUS_OKAY);
4560 }
4561
4562 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
4563 static inline typename This::Status
4564 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
4565 const Mips_relobj<size, big_endian>* object,
4566 const Symbol_value<size>* psymval, Mips_address addend_a,
4567 bool extract_addend, Mips_address address, bool is_gp_disp,
3d0064a9 4568 unsigned int r_type, unsigned int r_sym)
9810d34d
SS
4569 {
4570 mips_reloc_unshuffle(view, r_type, false);
4571 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4572 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4573
4574 Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4575 : addend_a);
4576
4577 // Resolve pending R_MIPS_HI16 relocations.
4578 typename std::list<reloc_high<size, big_endian> >::iterator it =
4579 hi16_relocs.begin();
4580 while (it != hi16_relocs.end())
4581 {
4582 reloc_high<size, big_endian> hi16 = *it;
3d0064a9
CC
4583 if (hi16.r_sym == r_sym
4584 && is_matching_lo16_reloc(hi16.r_type, r_type))
9810d34d
SS
4585 {
4586 if (do_relhi16(hi16.view, hi16.object, hi16.psymval, hi16.addend,
4587 hi16.address, hi16.gp_disp, hi16.r_type,
4588 hi16.extract_addend, addend, target)
4589 == This::STATUS_OVERFLOW)
4590 return This::STATUS_OVERFLOW;
4591 it = hi16_relocs.erase(it);
4592 }
4593 else
4594 ++it;
4595 }
4596
4597 // Resolve pending local R_MIPS_GOT16 relocations.
4598 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
4599 got16_relocs.begin();
4600 while (it2 != got16_relocs.end())
4601 {
4602 reloc_high<size, big_endian> got16 = *it2;
3d0064a9
CC
4603 if (got16.r_sym == r_sym
4604 && is_matching_lo16_reloc(got16.r_type, r_type))
9810d34d
SS
4605 {
4606 if (do_relgot16_local(got16.view, got16.object, got16.psymval,
4607 got16.addend, got16.r_type,
4608 got16.extract_addend, addend,
4609 target) == This::STATUS_OVERFLOW)
4610 return This::STATUS_OVERFLOW;
4611 it2 = got16_relocs.erase(it2);
4612 }
4613 else
4614 ++it2;
4615 }
4616
4617 // Resolve R_MIPS_LO16 relocation.
4618 Valtype32 x;
4619 if (!is_gp_disp)
4620 x = psymval->value(object, addend);
4621 else
4622 {
4623 // See the comment for R_MIPS16_HI16 above for the reason
4624 // for this conditional.
4625 Valtype32 gp_disp;
4626 if (r_type == elfcpp::R_MIPS16_LO16)
4627 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
4628 else if (r_type == elfcpp::R_MICROMIPS_LO16
4629 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
4630 gp_disp = target->adjusted_gp_value(object) - address + 3;
4631 else
4632 gp_disp = target->adjusted_gp_value(object) - address + 4;
4633 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
4634 // for overflow. Relocations against _gp_disp are normally
4635 // generated from the .cpload pseudo-op. It generates code
4636 // that normally looks like this:
4637
4638 // lui $gp,%hi(_gp_disp)
4639 // addiu $gp,$gp,%lo(_gp_disp)
4640 // addu $gp,$gp,$t9
4641
4642 // Here $t9 holds the address of the function being called,
4643 // as required by the MIPS ELF ABI. The R_MIPS_LO16
4644 // relocation can easily overflow in this situation, but the
4645 // R_MIPS_HI16 relocation will handle the overflow.
4646 // Therefore, we consider this a bug in the MIPS ABI, and do
4647 // not check for overflow here.
4648 x = gp_disp + addend;
4649 }
4650 val = Bits<32>::bit_select32(val, x, 0xffff);
4651 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4652 mips_reloc_shuffle(view, r_type, false);
4653 return This::STATUS_OKAY;
4654 }
4655
4656 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
4657 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4658 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
4659 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
4660 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
4661 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
4662 static inline typename This::Status
4663 relgot(unsigned char* view, int gp_offset, unsigned int r_type)
4664 {
4665 mips_reloc_unshuffle(view, r_type, false);
4666 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4667 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4668 Valtype32 x = gp_offset;
4669 val = Bits<32>::bit_select32(val, x, 0xffff);
4670 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4671 mips_reloc_shuffle(view, r_type, false);
4672 return (Bits<16>::has_overflow32(x)
4673 ? This::STATUS_OVERFLOW
4674 : This::STATUS_OKAY);
4675 }
4676
4677 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
4678 static inline typename This::Status
4679 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
4680 const Mips_relobj<size, big_endian>* object,
4681 const Symbol_value<size>* psymval, Mips_address addend_a,
4682 bool extract_addend, unsigned int r_type)
4683 {
4684 mips_reloc_unshuffle(view, r_type, false);
4685 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4686 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4687 Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
4688
4689 // Find a GOT page entry that points to within 32KB of symbol + addend.
4690 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
4691 unsigned int got_offset =
4692 target->got_section()->get_got_page_offset(value, object);
4693
4694 Valtype32 x = target->got_section()->gp_offset(got_offset, object);
4695 val = Bits<32>::bit_select32(val, x, 0xffff);
4696 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4697 mips_reloc_shuffle(view, r_type, false);
4698 return (Bits<16>::has_overflow32(x)
4699 ? This::STATUS_OVERFLOW
4700 : This::STATUS_OKAY);
4701 }
4702
4703 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
4704 static inline typename This::Status
4705 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
4706 const Mips_relobj<size, big_endian>* object,
4707 const Symbol_value<size>* psymval, Mips_address addend_a,
4708 bool extract_addend, bool local, unsigned int r_type)
4709 {
4710 mips_reloc_unshuffle(view, r_type, false);
4711 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4712 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4713 Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
4714
4715 // For a local symbol, find a GOT page entry that points to within 32KB of
4716 // symbol + addend. Relocation value is the offset of the GOT page entry's
4717 // value from symbol + addend.
4718 // For a global symbol, relocation value is addend.
4719 Valtype32 x;
4720 if (local)
4721 {
4722 // Find GOT page entry.
4723 Mips_address value = ((psymval->value(object, addend) + 0x8000)
4724 & ~0xffff);
4725 target->got_section()->get_got_page_offset(value, object);
4726
4727 x = psymval->value(object, addend) - value;
4728 }
4729 else
4730 x = addend;
4731 val = Bits<32>::bit_select32(val, x, 0xffff);
4732 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4733 mips_reloc_shuffle(view, r_type, false);
4734 return (Bits<16>::has_overflow32(x)
4735 ? This::STATUS_OVERFLOW
4736 : This::STATUS_OKAY);
4737 }
4738
4739 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
4740 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
4741 static inline typename This::Status
4742 relgot_hi16(unsigned char* view, int gp_offset, unsigned int r_type)
4743 {
4744 mips_reloc_unshuffle(view, r_type, false);
4745 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4746 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4747 Valtype32 x = gp_offset;
4748 x = ((x + 0x8000) >> 16) & 0xffff;
4749 val = Bits<32>::bit_select32(val, x, 0xffff);
4750 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4751 mips_reloc_shuffle(view, r_type, false);
4752 return This::STATUS_OKAY;
4753 }
4754
4755 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
4756 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
4757 static inline typename This::Status
4758 relgot_lo16(unsigned char* view, int gp_offset, unsigned int r_type)
4759 {
4760 mips_reloc_unshuffle(view, r_type, false);
4761 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4762 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4763 Valtype32 x = gp_offset;
4764 val = Bits<32>::bit_select32(val, x, 0xffff);
4765 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4766 mips_reloc_shuffle(view, r_type, false);
4767 return This::STATUS_OKAY;
4768 }
4769
4770 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
4771 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
4772 static inline typename This::Status
4773 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4774 const Symbol_value<size>* psymval, Mips_address gp,
4775 Mips_address addend_a, bool extract_addend, bool local,
4776 unsigned int r_type)
4777 {
4778 mips_reloc_unshuffle(view, r_type, false);
4779 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4780 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4781
4782 Valtype32 addend;
4783 if (extract_addend)
4784 {
4785 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
4786 addend = (val & 0x7f) << 2;
4787 else
4788 addend = val & 0xffff;
4789 // Only sign-extend the addend if it was extracted from the
4790 // instruction. If the addend was separate, leave it alone,
4791 // otherwise we may lose significant bits.
4792 addend = Bits<16>::sign_extend32(addend);
4793 }
4794 else
4795 addend = addend_a;
4796
4797 Valtype32 x = psymval->value(object, addend) - gp;
4798
4799 // If the symbol was local, any earlier relocatable links will
4800 // have adjusted its addend with the gp offset, so compensate
4801 // for that now. Don't do it for symbols forced local in this
4802 // link, though, since they won't have had the gp offset applied
4803 // to them before.
4804 if (local)
4805 x += object->gp_value();
4806
4807 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
4808 val = Bits<32>::bit_select32(val, x, 0x7f);
4809 else
4810 val = Bits<32>::bit_select32(val, x, 0xffff);
4811 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4812 mips_reloc_shuffle(view, r_type, false);
4813 if (Bits<16>::has_overflow32(x))
4814 {
4815 gold_error(_("small-data section exceeds 64KB; lower small-data size "
4816 "limit (see option -G)"));
4817 return This::STATUS_OVERFLOW;
4818 }
4819 return This::STATUS_OKAY;
4820 }
4821
4822 // R_MIPS_GPREL32
4823 static inline typename This::Status
4824 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4825 const Symbol_value<size>* psymval, Mips_address gp,
4826 Mips_address addend_a, bool extract_addend, unsigned int r_type)
4827 {
4828 mips_reloc_unshuffle(view, r_type, false);
4829 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4830 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4831 Valtype32 addend = extract_addend ? val : addend_a;
4832
4833 // R_MIPS_GPREL32 relocations are defined for local symbols only.
4834 Valtype32 x = psymval->value(object, addend) + object->gp_value() - gp;
4835 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4836 mips_reloc_shuffle(view, r_type, false);
4837 return This::STATUS_OKAY;
4838 }
4839
4840 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
4841 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
4842 // R_MICROMIPS_TLS_DTPREL_HI16
4843 static inline typename This::Status
4844 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4845 const Symbol_value<size>* psymval, Valtype32 tp_offset,
4846 Mips_address addend_a, bool extract_addend, unsigned int r_type)
4847 {
4848 mips_reloc_unshuffle(view, r_type, false);
4849 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4850 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4851 Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
4852
4853 // tls symbol values are relative to tls_segment()->vaddr()
4854 Valtype32 x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
4855 val = Bits<32>::bit_select32(val, x, 0xffff);
4856 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4857 mips_reloc_shuffle(view, r_type, false);
4858 return This::STATUS_OKAY;
4859 }
4860
4861 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
4862 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
4863 // R_MICROMIPS_TLS_DTPREL_LO16,
4864 static inline typename This::Status
4865 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4866 const Symbol_value<size>* psymval, Valtype32 tp_offset,
4867 Mips_address addend_a, bool extract_addend, unsigned int r_type)
4868 {
4869 mips_reloc_unshuffle(view, r_type, false);
4870 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4871 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4872 Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
4873
4874 // tls symbol values are relative to tls_segment()->vaddr()
4875 Valtype32 x = psymval->value(object, addend) - tp_offset;
4876 val = Bits<32>::bit_select32(val, x, 0xffff);
4877 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4878 mips_reloc_shuffle(view, r_type, false);
4879 return This::STATUS_OKAY;
4880 }
4881
4882 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
4883 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
4884 static inline typename This::Status
4885 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4886 const Symbol_value<size>* psymval, Valtype32 tp_offset,
4887 Mips_address addend_a, bool extract_addend, unsigned int r_type)
4888 {
4889 mips_reloc_unshuffle(view, r_type, false);
4890 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4891 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4892 Valtype32 addend = extract_addend ? val : addend_a;
4893
4894 // tls symbol values are relative to tls_segment()->vaddr()
4895 Valtype32 x = psymval->value(object, addend) - tp_offset;
4896 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4897 mips_reloc_shuffle(view, r_type, false);
4898 return This::STATUS_OKAY;
4899 }
4900
4901 // R_MIPS_SUB, R_MICROMIPS_SUB
4902 static inline typename This::Status
4903 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4904 const Symbol_value<size>* psymval, Mips_address addend_a,
4905 bool extract_addend, unsigned int r_type)
4906 {
4907 mips_reloc_unshuffle(view, r_type, false);
4908 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4909 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4910 Valtype32 addend = extract_addend ? val : addend_a;
4911
4912 Valtype32 x = psymval->value(object, -addend);
4913 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4914 mips_reloc_shuffle(view, r_type, false);
4915 return This::STATUS_OKAY;
4916 }
4917};
4918
4919template<int size, bool big_endian>
4920typename std::list<reloc_high<size, big_endian> >
4921 Mips_relocate_functions<size, big_endian>::hi16_relocs;
4922
4923template<int size, bool big_endian>
4924typename std::list<reloc_high<size, big_endian> >
4925 Mips_relocate_functions<size, big_endian>::got16_relocs;
4926
4927// Mips_got_info methods.
4928
4929// Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
4930// SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
4931
4932template<int size, bool big_endian>
4933void
4934Mips_got_info<size, big_endian>::record_local_got_symbol(
4935 Mips_relobj<size, big_endian>* object, unsigned int symndx,
4936 Mips_address addend, unsigned int r_type, unsigned int shndx)
4937{
4938 Mips_got_entry<size, big_endian>* entry =
4939 new Mips_got_entry<size, big_endian>(object, symndx, addend,
4940 mips_elf_reloc_tls_type(r_type),
4941 shndx);
4942 this->record_got_entry(entry, object);
4943}
4944
4945// Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
4946// in OBJECT. FOR_CALL is true if the caller is only interested in
4947// using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
4948// relocation.
4949
4950template<int size, bool big_endian>
4951void
4952Mips_got_info<size, big_endian>::record_global_got_symbol(
4953 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
4954 unsigned int r_type, bool dyn_reloc, bool for_call)
4955{
4956 if (!for_call)
4957 mips_sym->set_got_not_only_for_calls();
4958
4959 // A global symbol in the GOT must also be in the dynamic symbol table.
4960 if (!mips_sym->needs_dynsym_entry())
4961 {
4962 switch (mips_sym->visibility())
4963 {
4964 case elfcpp::STV_INTERNAL:
4965 case elfcpp::STV_HIDDEN:
4966 mips_sym->set_is_forced_local();
4967 break;
4968 default:
4969 mips_sym->set_needs_dynsym_entry();
4970 break;
4971 }
4972 }
4973
4974 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
4975 if (tls_type == GOT_TLS_NONE)
4976 this->global_got_symbols_.insert(mips_sym);
4977
4978 if (dyn_reloc)
4979 {
4980 if (mips_sym->global_got_area() == GGA_NONE)
4981 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
4982 return;
4983 }
4984
4985 Mips_got_entry<size, big_endian>* entry =
4986 new Mips_got_entry<size, big_endian>(object, mips_sym, tls_type);
4987
4988 this->record_got_entry(entry, object);
4989}
4990
4991// Add ENTRY to master GOT and to OBJECT's GOT.
4992
4993template<int size, bool big_endian>
4994void
4995Mips_got_info<size, big_endian>::record_got_entry(
4996 Mips_got_entry<size, big_endian>* entry,
4997 Mips_relobj<size, big_endian>* object)
4998{
4999 if (this->got_entries_.find(entry) == this->got_entries_.end())
5000 this->got_entries_.insert(entry);
5001
5002 // Create the GOT entry for the OBJECT's GOT.
5003 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5004 Mips_got_entry<size, big_endian>* entry2 =
5005 new Mips_got_entry<size, big_endian>(*entry);
5006
5007 if (g->got_entries_.find(entry2) == g->got_entries_.end())
5008 g->got_entries_.insert(entry2);
5009}
5010
5011// Record that OBJECT has a page relocation against symbol SYMNDX and
5012// that ADDEND is the addend for that relocation.
5013// This function creates an upper bound on the number of GOT slots
5014// required; no attempt is made to combine references to non-overridable
5015// global symbols across multiple input files.
5016
5017template<int size, bool big_endian>
5018void
5019Mips_got_info<size, big_endian>::record_got_page_entry(
5020 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5021{
5022 struct Got_page_range **range_ptr, *range;
5023 int old_pages, new_pages;
5024
5025 // Find the Got_page_entry for this symbol.
5026 Got_page_entry* entry = new Got_page_entry(object, symndx);
5027 typename Got_page_entry_set::iterator it =
5028 this->got_page_entries_.find(entry);
5029 if (it != this->got_page_entries_.end())
5030 entry = *it;
5031 else
5032 this->got_page_entries_.insert(entry);
5033
5034 // Add the same entry to the OBJECT's GOT.
5035 Got_page_entry* entry2 = NULL;
5036 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5037 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5038 {
5039 entry2 = new Got_page_entry(*entry);
5040 g2->got_page_entries_.insert(entry2);
5041 }
5042
5043 // Skip over ranges whose maximum extent cannot share a page entry
5044 // with ADDEND.
5045 range_ptr = &entry->ranges;
5046 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5047 range_ptr = &(*range_ptr)->next;
5048
5049 // If we scanned to the end of the list, or found a range whose
5050 // minimum extent cannot share a page entry with ADDEND, create
5051 // a new singleton range.
5052 range = *range_ptr;
5053 if (!range || addend < range->min_addend - 0xffff)
5054 {
5055 range = new Got_page_range();
5056 range->next = *range_ptr;
5057 range->min_addend = addend;
5058 range->max_addend = addend;
5059
5060 *range_ptr = range;
5061 ++entry->num_pages;
5062 if (entry2 != NULL)
5063 ++entry2->num_pages;
5064 ++this->page_gotno_;
5065 ++g2->page_gotno_;
5066 return;
5067 }
5068
5069 // Remember how many pages the old range contributed.
5070 old_pages = range->get_max_pages();
5071
5072 // Update the ranges.
5073 if (addend < range->min_addend)
5074 range->min_addend = addend;
5075 else if (addend > range->max_addend)
5076 {
5077 if (range->next && addend >= range->next->min_addend - 0xffff)
5078 {
5079 old_pages += range->next->get_max_pages();
5080 range->max_addend = range->next->max_addend;
5081 range->next = range->next->next;
5082 }
5083 else
5084 range->max_addend = addend;
5085 }
5086
5087 // Record any change in the total estimate.
5088 new_pages = range->get_max_pages();
5089 if (old_pages != new_pages)
5090 {
5091 entry->num_pages += new_pages - old_pages;
5092 if (entry2 != NULL)
5093 entry2->num_pages += new_pages - old_pages;
5094 this->page_gotno_ += new_pages - old_pages;
5095 g2->page_gotno_ += new_pages - old_pages;
5096 }
5097}
5098
5099// Create all entries that should be in the local part of the GOT.
5100
5101template<int size, bool big_endian>
5102void
5103Mips_got_info<size, big_endian>::add_local_entries(
5104 Target_mips<size, big_endian>* target, Layout* layout)
5105{
5106 Mips_output_data_got<size, big_endian>* got = target->got_section();
5107 // First two GOT entries are reserved. The first entry will be filled at
5108 // runtime. The second entry will be used by some runtime loaders.
5109 got->add_constant(0);
5110 got->add_constant(target->mips_elf_gnu_got1_mask());
5111
5112 for (typename Got_entry_set::iterator
5113 p = this->got_entries_.begin();
5114 p != this->got_entries_.end();
5115 ++p)
5116 {
5117 Mips_got_entry<size, big_endian>* entry = *p;
5118 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5119 {
5120 got->add_local(entry->object(), entry->symndx(),
5121 GOT_TYPE_STANDARD);
5122 unsigned int got_offset = entry->object()->local_got_offset(
5123 entry->symndx(), GOT_TYPE_STANDARD);
5124 if (got->multi_got() && this->index_ > 0
5125 && parameters->options().output_is_position_independent())
5126 target->rel_dyn_section(layout)->add_local(entry->object(),
5127 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5128 }
5129 }
5130
5131 this->add_page_entries(target, layout);
5132
5133 // Add global entries that should be in the local area.
5134 for (typename Got_entry_set::iterator
5135 p = this->got_entries_.begin();
5136 p != this->got_entries_.end();
5137 ++p)
5138 {
5139 Mips_got_entry<size, big_endian>* entry = *p;
5140 if (!entry->is_for_global_symbol())
5141 continue;
5142
5143 Mips_symbol<size>* mips_sym = entry->sym();
5144 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5145 {
5146 unsigned int got_type;
5147 if (!got->multi_got())
5148 got_type = GOT_TYPE_STANDARD;
5149 else
5150 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5151 if (got->add_global(mips_sym, got_type))
5152 {
5153 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5154 if (got->multi_got() && this->index_ > 0
5155 && parameters->options().output_is_position_independent())
5156 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5157 mips_sym, elfcpp::R_MIPS_REL32, got,
5158 mips_sym->got_offset(got_type));
5159 }
5160 }
5161 }
5162}
5163
5164// Create GOT page entries.
5165
5166template<int size, bool big_endian>
5167void
5168Mips_got_info<size, big_endian>::add_page_entries(
5169 Target_mips<size, big_endian>* target, Layout* layout)
5170{
5171 if (this->page_gotno_ == 0)
5172 return;
5173
5174 Mips_output_data_got<size, big_endian>* got = target->got_section();
5175 this->got_page_offset_start_ = got->add_constant(0);
5176 if (got->multi_got() && this->index_ > 0
5177 && parameters->options().output_is_position_independent())
5178 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5179 this->got_page_offset_start_);
5180 int num_entries = this->page_gotno_;
5181 unsigned int prev_offset = this->got_page_offset_start_;
5182 while (--num_entries > 0)
5183 {
5184 unsigned int next_offset = got->add_constant(0);
5185 if (got->multi_got() && this->index_ > 0
5186 && parameters->options().output_is_position_independent())
5187 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5188 next_offset);
5189 gold_assert(next_offset == prev_offset + size/8);
5190 prev_offset = next_offset;
5191 }
5192 this->got_page_offset_next_ = this->got_page_offset_start_;
5193}
5194
5195// Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5196
5197template<int size, bool big_endian>
5198void
5199Mips_got_info<size, big_endian>::add_global_entries(
5200 Target_mips<size, big_endian>* target, Layout* layout,
5201 unsigned int non_reloc_only_global_gotno)
5202{
5203 Mips_output_data_got<size, big_endian>* got = target->got_section();
5204 // Add GGA_NORMAL entries.
5205 unsigned int count = 0;
5206 for (typename Got_entry_set::iterator
5207 p = this->got_entries_.begin();
5208 p != this->got_entries_.end();
5209 ++p)
5210 {
5211 Mips_got_entry<size, big_endian>* entry = *p;
5212 if (!entry->is_for_global_symbol())
5213 continue;
5214
5215 Mips_symbol<size>* mips_sym = entry->sym();
5216 if (mips_sym->global_got_area() != GGA_NORMAL)
5217 continue;
5218
5219 unsigned int got_type;
5220 if (!got->multi_got())
5221 got_type = GOT_TYPE_STANDARD;
5222 else
5223 // In multi-GOT links, global symbol can be in both primary and
5224 // secondary GOT(s). By creating custom GOT type
5225 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5226 // is added to secondary GOT(s).
5227 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5228 if (!got->add_global(mips_sym, got_type))
5229 continue;
5230
5231 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5232 if (got->multi_got() && this->index_ == 0)
5233 count++;
5234 if (got->multi_got() && this->index_ > 0)
5235 {
5236 if (parameters->options().output_is_position_independent()
5237 || (!parameters->doing_static_link()
5238 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
5239 {
5240 target->rel_dyn_section(layout)->add_global(
5241 mips_sym, elfcpp::R_MIPS_REL32, got,
5242 mips_sym->got_offset(got_type));
5243 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
5244 elfcpp::R_MIPS_REL32, mips_sym);
5245 }
5246 }
5247 }
5248
5249 if (!got->multi_got() || this->index_ == 0)
5250 {
5251 if (got->multi_got())
5252 {
5253 // We need to allocate space in the primary GOT for GGA_NORMAL entries
5254 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
5255 // entries correspond to dynamic symbol indexes.
5256 while (count < non_reloc_only_global_gotno)
5257 {
5258 got->add_constant(0);
5259 ++count;
5260 }
5261 }
5262
5263 // Add GGA_RELOC_ONLY entries.
5264 got->add_reloc_only_entries();
5265 }
5266}
5267
5268// Create global GOT entries that should be in the GGA_RELOC_ONLY area.
5269
5270template<int size, bool big_endian>
5271void
5272Mips_got_info<size, big_endian>::add_reloc_only_entries(
5273 Mips_output_data_got<size, big_endian>* got)
5274{
5275 for (typename Unordered_set<Mips_symbol<size>*>::iterator
5276 p = this->global_got_symbols_.begin();
5277 p != this->global_got_symbols_.end();
5278 ++p)
5279 {
5280 Mips_symbol<size>* mips_sym = *p;
5281 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
5282 {
5283 unsigned int got_type;
5284 if (!got->multi_got())
5285 got_type = GOT_TYPE_STANDARD;
5286 else
5287 got_type = GOT_TYPE_STANDARD_MULTIGOT;
5288 if (got->add_global(mips_sym, got_type))
5289 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5290 }
5291 }
5292}
5293
5294// Create TLS GOT entries.
5295
5296template<int size, bool big_endian>
5297void
5298Mips_got_info<size, big_endian>::add_tls_entries(
5299 Target_mips<size, big_endian>* target, Layout* layout)
5300{
5301 Mips_output_data_got<size, big_endian>* got = target->got_section();
5302 // Add local tls entries.
5303 for (typename Got_entry_set::iterator
5304 p = this->got_entries_.begin();
5305 p != this->got_entries_.end();
5306 ++p)
5307 {
5308 Mips_got_entry<size, big_endian>* entry = *p;
5309 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
5310 continue;
5311
5312 if (entry->tls_type() == GOT_TLS_GD)
5313 {
5314 unsigned int got_type = GOT_TYPE_TLS_PAIR;
5315 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5316 : elfcpp::R_MIPS_TLS_DTPMOD64);
5317 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5318 : elfcpp::R_MIPS_TLS_DTPREL64);
5319
5320 if (!parameters->doing_static_link())
5321 {
5322 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
5323 entry->shndx(), got_type,
5324 target->rel_dyn_section(layout),
5325 r_type1);
5326 unsigned int got_offset =
5327 entry->object()->local_got_offset(entry->symndx(), got_type);
5328 got->add_static_reloc(got_offset + size/8, r_type2,
5329 entry->object(), entry->symndx());
5330 }
5331 else
5332 {
5333 // We are doing a static link. Mark it as belong to module 1,
5334 // the executable.
5335 unsigned int got_offset = got->add_constant(1);
5336 entry->object()->set_local_got_offset(entry->symndx(), got_type,
5337 got_offset);
5338 got->add_constant(0);
5339 got->add_static_reloc(got_offset + size/8, r_type2,
5340 entry->object(), entry->symndx());
5341 }
5342 }
5343 else if (entry->tls_type() == GOT_TLS_IE)
5344 {
5345 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
5346 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5347 : elfcpp::R_MIPS_TLS_TPREL64);
5348 if (!parameters->doing_static_link())
5349 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
5350 target->rel_dyn_section(layout), r_type);
5351 else
5352 {
5353 got->add_local(entry->object(), entry->symndx(), got_type);
5354 unsigned int got_offset =
5355 entry->object()->local_got_offset(entry->symndx(), got_type);
5356 got->add_static_reloc(got_offset, r_type, entry->object(),
5357 entry->symndx());
5358 }
5359 }
5360 else if (entry->tls_type() == GOT_TLS_LDM)
5361 {
5362 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5363 : elfcpp::R_MIPS_TLS_DTPMOD64);
5364 unsigned int got_offset;
5365 if (!parameters->doing_static_link())
5366 {
5367 got_offset = got->add_constant(0);
5368 target->rel_dyn_section(layout)->add_local(
5369 entry->object(), 0, r_type, got, got_offset);
5370 }
5371 else
5372 // We are doing a static link. Just mark it as belong to module 1,
5373 // the executable.
5374 got_offset = got->add_constant(1);
5375
5376 got->add_constant(0);
5377 got->set_tls_ldm_offset(got_offset, entry->object());
5378 }
5379 else
5380 gold_unreachable();
5381 }
5382
5383 // Add global tls entries.
5384 for (typename Got_entry_set::iterator
5385 p = this->got_entries_.begin();
5386 p != this->got_entries_.end();
5387 ++p)
5388 {
5389 Mips_got_entry<size, big_endian>* entry = *p;
5390 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
5391 continue;
5392
5393 Mips_symbol<size>* mips_sym = entry->sym();
5394 if (entry->tls_type() == GOT_TLS_GD)
5395 {
5396 unsigned int got_type;
5397 if (!got->multi_got())
5398 got_type = GOT_TYPE_TLS_PAIR;
5399 else
5400 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
5401 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5402 : elfcpp::R_MIPS_TLS_DTPMOD64);
5403 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5404 : elfcpp::R_MIPS_TLS_DTPREL64);
5405 if (!parameters->doing_static_link())
5406 got->add_global_pair_with_rel(mips_sym, got_type,
5407 target->rel_dyn_section(layout), r_type1, r_type2);
5408 else
5409 {
5410 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
5411 // GOT entries. The first one is initialized to be 1, which is the
5412 // module index for the main executable and the second one 0. A
5413 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
5414 // the second GOT entry and will be applied by gold.
5415 unsigned int got_offset = got->add_constant(1);
5416 mips_sym->set_got_offset(got_type, got_offset);
5417 got->add_constant(0);
5418 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
5419 }
5420 }
5421 else if (entry->tls_type() == GOT_TLS_IE)
5422 {
5423 unsigned int got_type;
5424 if (!got->multi_got())
5425 got_type = GOT_TYPE_TLS_OFFSET;
5426 else
5427 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
5428 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5429 : elfcpp::R_MIPS_TLS_TPREL64);
5430 if (!parameters->doing_static_link())
5431 got->add_global_with_rel(mips_sym, got_type,
5432 target->rel_dyn_section(layout), r_type);
5433 else
5434 {
5435 got->add_global(mips_sym, got_type);
5436 unsigned int got_offset = mips_sym->got_offset(got_type);
5437 got->add_static_reloc(got_offset, r_type, mips_sym);
5438 }
5439 }
5440 else
5441 gold_unreachable();
5442 }
5443}
5444
5445// Decide whether the symbol needs an entry in the global part of the primary
5446// GOT, setting global_got_area accordingly. Count the number of global
5447// symbols that are in the primary GOT only because they have dynamic
5448// relocations R_MIPS_REL32 against them (reloc_only_gotno).
5449
5450template<int size, bool big_endian>
5451void
5452Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
5453{
5454 for (typename Unordered_set<Mips_symbol<size>*>::iterator
5455 p = this->global_got_symbols_.begin();
5456 p != this->global_got_symbols_.end();
5457 ++p)
5458 {
5459 Mips_symbol<size>* sym = *p;
5460 // Make a final decision about whether the symbol belongs in the
5461 // local or global GOT. Symbols that bind locally can (and in the
5462 // case of forced-local symbols, must) live in the local GOT.
5463 // Those that are aren't in the dynamic symbol table must also
5464 // live in the local GOT.
5465
5466 if (!sym->should_add_dynsym_entry(symtab)
5467 || (sym->got_only_for_calls()
5468 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
5469 : symbol_references_local(sym,
5470 sym->should_add_dynsym_entry(symtab))))
5471 // The symbol belongs in the local GOT. We no longer need this
5472 // entry if it was only used for relocations; those relocations
5473 // will be against the null or section symbol instead.
5474 sym->set_global_got_area(GGA_NONE);
5475 else if (sym->global_got_area() == GGA_RELOC_ONLY)
5476 {
5477 ++this->reloc_only_gotno_;
5478 ++this->global_gotno_ ;
5479 }
5480 }
5481}
5482
5483// Return the offset of GOT page entry for VALUE. Initialize the entry with
5484// VALUE if it is not initialized.
5485
5486template<int size, bool big_endian>
5487unsigned int
5488Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
5489 Mips_output_data_got<size, big_endian>* got)
5490{
5491 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
5492 if (it != this->got_page_offsets_.end())
5493 return it->second;
5494
5495 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
5496 + (size/8) * this->page_gotno_);
5497
5498 unsigned int got_offset = this->got_page_offset_next_;
5499 this->got_page_offsets_[value] = got_offset;
5500 this->got_page_offset_next_ += size/8;
5501 got->update_got_entry(got_offset, value);
5502 return got_offset;
5503}
5504
5505// Remove lazy-binding stubs for global symbols in this GOT.
5506
5507template<int size, bool big_endian>
5508void
5509Mips_got_info<size, big_endian>::remove_lazy_stubs(
5510 Target_mips<size, big_endian>* target)
5511{
5512 for (typename Got_entry_set::iterator
5513 p = this->got_entries_.begin();
5514 p != this->got_entries_.end();
5515 ++p)
5516 {
5517 Mips_got_entry<size, big_endian>* entry = *p;
5518 if (entry->is_for_global_symbol())
5519 target->remove_lazy_stub_entry(entry->sym());
5520 }
5521}
5522
5523// Count the number of GOT entries required.
5524
5525template<int size, bool big_endian>
5526void
5527Mips_got_info<size, big_endian>::count_got_entries()
5528{
5529 for (typename Got_entry_set::iterator
5530 p = this->got_entries_.begin();
5531 p != this->got_entries_.end();
5532 ++p)
5533 {
5534 this->count_got_entry(*p);
5535 }
5536}
5537
5538// Count the number of GOT entries required by ENTRY. Accumulate the result.
5539
5540template<int size, bool big_endian>
5541void
5542Mips_got_info<size, big_endian>::count_got_entry(
5543 Mips_got_entry<size, big_endian>* entry)
5544{
5545 if (entry->is_tls_entry())
5546 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
5547 else if (entry->is_for_local_symbol()
5548 || entry->sym()->global_got_area() == GGA_NONE)
5549 ++this->local_gotno_;
5550 else
5551 ++this->global_gotno_;
5552}
5553
5554// Add FROM's GOT entries.
5555
5556template<int size, bool big_endian>
5557void
5558Mips_got_info<size, big_endian>::add_got_entries(
5559 Mips_got_info<size, big_endian>* from)
5560{
5561 for (typename Got_entry_set::iterator
5562 p = from->got_entries_.begin();
5563 p != from->got_entries_.end();
5564 ++p)
5565 {
5566 Mips_got_entry<size, big_endian>* entry = *p;
5567 if (this->got_entries_.find(entry) == this->got_entries_.end())
5568 {
5569 Mips_got_entry<size, big_endian>* entry2 =
5570 new Mips_got_entry<size, big_endian>(*entry);
5571 this->got_entries_.insert(entry2);
5572 this->count_got_entry(entry);
5573 }
5574 }
5575}
5576
5577// Add FROM's GOT page entries.
5578
5579template<int size, bool big_endian>
5580void
5581Mips_got_info<size, big_endian>::add_got_page_entries(
5582 Mips_got_info<size, big_endian>* from)
5583{
5584 for (typename Got_page_entry_set::iterator
5585 p = from->got_page_entries_.begin();
5586 p != from->got_page_entries_.end();
5587 ++p)
5588 {
5589 Got_page_entry* entry = *p;
5590 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
5591 {
5592 Got_page_entry* entry2 = new Got_page_entry(*entry);
5593 this->got_page_entries_.insert(entry2);
5594 this->page_gotno_ += entry->num_pages;
5595 }
5596 }
5597}
5598
5599// Mips_output_data_got methods.
5600
5601// Lay out the GOT. Add local, global and TLS entries. If GOT is
5602// larger than 64K, create multi-GOT.
5603
5604template<int size, bool big_endian>
5605void
5606Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
5607 Symbol_table* symtab, const Input_objects* input_objects)
5608{
5609 // Decide which symbols need to go in the global part of the GOT and
5610 // count the number of reloc-only GOT symbols.
5611 this->master_got_info_->count_got_symbols(symtab);
5612
5613 // Count the number of GOT entries.
5614 this->master_got_info_->count_got_entries();
5615
5616 unsigned int got_size = this->master_got_info_->got_size();
5617 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
5618 this->lay_out_multi_got(layout, input_objects);
5619 else
5620 {
5621 // Record that all objects use single GOT.
5622 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5623 p != input_objects->relobj_end();
5624 ++p)
5625 {
5626 Mips_relobj<size, big_endian>* object =
5627 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
5628 if (object->get_got_info() != NULL)
5629 object->set_got_info(this->master_got_info_);
5630 }
5631
5632 this->master_got_info_->add_local_entries(this->target_, layout);
5633 this->master_got_info_->add_global_entries(this->target_, layout,
5634 /*not used*/-1U);
5635 this->master_got_info_->add_tls_entries(this->target_, layout);
5636 }
5637}
5638
5639// Create multi-GOT. For every GOT, add local, global and TLS entries.
5640
5641template<int size, bool big_endian>
5642void
5643Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
5644 const Input_objects* input_objects)
5645{
5646 // Try to merge the GOTs of input objects together, as long as they
5647 // don't seem to exceed the maximum GOT size, choosing one of them
5648 // to be the primary GOT.
5649 this->merge_gots(input_objects);
5650
5651 // Every symbol that is referenced in a dynamic relocation must be
5652 // present in the primary GOT.
5653 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
5654
5655 // Add GOT entries.
5656 unsigned int i = 0;
5657 unsigned int offset = 0;
5658 Mips_got_info<size, big_endian>* g = this->primary_got_;
5659 do
5660 {
5661 g->set_index(i);
5662 g->set_offset(offset);
5663
5664 g->add_local_entries(this->target_, layout);
5665 if (i == 0)
5666 g->add_global_entries(this->target_, layout,
5667 (this->master_got_info_->global_gotno()
5668 - this->master_got_info_->reloc_only_gotno()));
5669 else
5670 g->add_global_entries(this->target_, layout, /*not used*/-1U);
5671 g->add_tls_entries(this->target_, layout);
5672
5673 // Forbid global symbols in every non-primary GOT from having
5674 // lazy-binding stubs.
5675 if (i > 0)
5676 g->remove_lazy_stubs(this->target_);
5677
5678 ++i;
5679 offset += g->got_size();
5680 g = g->next();
5681 }
5682 while (g);
5683}
5684
5685// Attempt to merge GOTs of different input objects. Try to use as much as
5686// possible of the primary GOT, since it doesn't require explicit dynamic
5687// relocations, but don't use objects that would reference global symbols
5688// out of the addressable range. Failing the primary GOT, attempt to merge
5689// with the current GOT, or finish the current GOT and then make make the new
5690// GOT current.
5691
5692template<int size, bool big_endian>
5693void
5694Mips_output_data_got<size, big_endian>::merge_gots(
5695 const Input_objects* input_objects)
5696{
5697 gold_assert(this->primary_got_ == NULL);
5698 Mips_got_info<size, big_endian>* current = NULL;
5699
5700 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5701 p != input_objects->relobj_end();
5702 ++p)
5703 {
5704 Mips_relobj<size, big_endian>* object =
5705 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
5706
5707 Mips_got_info<size, big_endian>* g = object->get_got_info();
5708 if (g == NULL)
5709 continue;
5710
5711 g->count_got_entries();
5712
5713 // Work out the number of page, local and TLS entries.
5714 unsigned int estimate = this->master_got_info_->page_gotno();
5715 if (estimate > g->page_gotno())
5716 estimate = g->page_gotno();
5717 estimate += g->local_gotno() + g->tls_gotno();
5718
5719 // We place TLS GOT entries after both locals and globals. The globals
5720 // for the primary GOT may overflow the normal GOT size limit, so be
5721 // sure not to merge a GOT which requires TLS with the primary GOT in that
5722 // case. This doesn't affect non-primary GOTs.
5723 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
5724 : g->global_gotno());
5725
5726 unsigned int max_count =
5727 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
5728 if (estimate <= max_count)
5729 {
5730 // If we don't have a primary GOT, use it as
5731 // a starting point for the primary GOT.
5732 if (!this->primary_got_)
5733 {
5734 this->primary_got_ = g;
5735 continue;
5736 }
5737
5738 // Try merging with the primary GOT.
5739 if (this->merge_got_with(g, object, this->primary_got_))
5740 continue;
5741 }
5742
5743 // If we can merge with the last-created GOT, do it.
5744 if (current && this->merge_got_with(g, object, current))
5745 continue;
5746
5747 // Well, we couldn't merge, so create a new GOT. Don't check if it
5748 // fits; if it turns out that it doesn't, we'll get relocation
5749 // overflows anyway.
5750 g->set_next(current);
5751 current = g;
5752 }
5753
5754 // If we do not find any suitable primary GOT, create an empty one.
5755 if (this->primary_got_ == NULL)
5756 this->primary_got_ = new Mips_got_info<size, big_endian>();
5757
5758 // Link primary GOT with secondary GOTs.
5759 this->primary_got_->set_next(current);
5760}
5761
5762// Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
5763// this would lead to overflow, true if they were merged successfully.
5764
5765template<int size, bool big_endian>
5766bool
5767Mips_output_data_got<size, big_endian>::merge_got_with(
5768 Mips_got_info<size, big_endian>* from,
5769 Mips_relobj<size, big_endian>* object,
5770 Mips_got_info<size, big_endian>* to)
5771{
5772 // Work out how many page entries we would need for the combined GOT.
5773 unsigned int estimate = this->master_got_info_->page_gotno();
5774 if (estimate >= from->page_gotno() + to->page_gotno())
5775 estimate = from->page_gotno() + to->page_gotno();
5776
5777 // Conservatively estimate how many local and TLS entries would be needed.
5778 estimate += from->local_gotno() + to->local_gotno();
5779 estimate += from->tls_gotno() + to->tls_gotno();
5780
5781 // If we're merging with the primary got, any TLS relocations will
5782 // come after the full set of global entries. Otherwise estimate those
5783 // conservatively as well.
5784 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
5785 estimate += this->master_got_info_->global_gotno();
5786 else
5787 estimate += from->global_gotno() + to->global_gotno();
5788
5789 // Bail out if the combined GOT might be too big.
5790 unsigned int max_count =
5791 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
5792 if (estimate > max_count)
5793 return false;
5794
5795 // Transfer the object's GOT information from FROM to TO.
5796 to->add_got_entries(from);
5797 to->add_got_page_entries(from);
5798
5799 // Record that OBJECT should use output GOT TO.
5800 object->set_got_info(to);
5801
5802 return true;
5803}
5804
5805// Write out the GOT.
5806
5807template<int size, bool big_endian>
5808void
5809Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
5810{
5811 // Call parent to write out GOT.
5812 Output_data_got<size, big_endian>::do_write(of);
5813
5814 const off_t offset = this->offset();
5815 const section_size_type oview_size =
5816 convert_to_section_size_type(this->data_size());
5817 unsigned char* const oview = of->get_output_view(offset, oview_size);
5818
5819 // Needed for fixing values of .got section.
5820 this->got_view_ = oview;
5821
5822 // Write lazy stub addresses.
5823 for (typename Unordered_set<Mips_symbol<size>*>::iterator
5824 p = this->master_got_info_->global_got_symbols().begin();
5825 p != this->master_got_info_->global_got_symbols().end();
5826 ++p)
5827 {
5828 Mips_symbol<size>* mips_sym = *p;
5829 if (mips_sym->has_lazy_stub())
5830 {
5831 Valtype* wv = reinterpret_cast<Valtype*>(
5832 oview + this->get_primary_got_offset(mips_sym));
5833 Valtype value =
5834 this->target_->mips_stubs_section()->stub_address(mips_sym);
5835 elfcpp::Swap<size, big_endian>::writeval(wv, value);
5836 }
5837 }
5838
5839 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
5840 for (typename Unordered_set<Mips_symbol<size>*>::iterator
5841 p = this->master_got_info_->global_got_symbols().begin();
5842 p != this->master_got_info_->global_got_symbols().end();
5843 ++p)
5844 {
5845 Mips_symbol<size>* mips_sym = *p;
5846 if (!this->multi_got()
5847 && (mips_sym->is_mips16() || mips_sym->is_micromips())
5848 && mips_sym->global_got_area() == GGA_NONE
5849 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
5850 {
5851 Valtype* wv = reinterpret_cast<Valtype*>(
5852 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
5853 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
5854 if (value != 0)
5855 {
5856 value |= 1;
5857 elfcpp::Swap<size, big_endian>::writeval(wv, value);
5858 }
5859 }
5860 }
5861
5862 if (!this->secondary_got_relocs_.empty())
5863 {
5864 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
5865 // secondary GOT entries with non-zero initial value copy the value
5866 // to the corresponding primary GOT entry, and set the secondary GOT
5867 // entry to zero.
5868 // TODO(sasa): This is workaround. It needs to be investigated further.
5869
5870 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
5871 {
5872 Static_reloc& reloc(this->secondary_got_relocs_[i]);
5873 if (reloc.symbol_is_global())
5874 {
5875 Mips_symbol<size>* gsym = reloc.symbol();
5876 gold_assert(gsym != NULL);
5877
5878 unsigned got_offset = reloc.got_offset();
5879 gold_assert(got_offset < oview_size);
5880
5881 // Find primary GOT entry.
5882 Valtype* wv_prim = reinterpret_cast<Valtype*>(
5883 oview + this->get_primary_got_offset(gsym));
5884
5885 // Find secondary GOT entry.
5886 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
5887
5888 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
5889 if (value != 0)
5890 {
5891 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
5892 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
5893 gsym->set_applied_secondary_got_fixup();
5894 }
5895 }
5896 }
5897
5898 of->write_output_view(offset, oview_size, oview);
5899 }
5900
5901 // We are done if there is no fix up.
5902 if (this->static_relocs_.empty())
5903 return;
5904
5905 Output_segment* tls_segment = this->layout_->tls_segment();
5906 gold_assert(tls_segment != NULL);
5907
5908 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
5909 {
5910 Static_reloc& reloc(this->static_relocs_[i]);
5911
5912 Mips_address value;
5913 if (!reloc.symbol_is_global())
5914 {
5915 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
5916 const Symbol_value<size>* psymval =
5917 object->local_symbol(reloc.index());
5918
5919 // We are doing static linking. Issue an error and skip this
5920 // relocation if the symbol is undefined or in a discarded_section.
5921 bool is_ordinary;
5922 unsigned int shndx = psymval->input_shndx(&is_ordinary);
5923 if ((shndx == elfcpp::SHN_UNDEF)
5924 || (is_ordinary
5925 && shndx != elfcpp::SHN_UNDEF
5926 && !object->is_section_included(shndx)
5927 && !this->symbol_table_->is_section_folded(object, shndx)))
5928 {
5929 gold_error(_("undefined or discarded local symbol %u from "
5930 " object %s in GOT"),
5931 reloc.index(), reloc.relobj()->name().c_str());
5932 continue;
5933 }
5934
5935 value = psymval->value(object, 0);
5936 }
5937 else
5938 {
5939 const Mips_symbol<size>* gsym = reloc.symbol();
5940 gold_assert(gsym != NULL);
5941
5942 // We are doing static linking. Issue an error and skip this
5943 // relocation if the symbol is undefined or in a discarded_section
5944 // unless it is a weakly_undefined symbol.
5945 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
5946 && !gsym->is_weak_undefined())
5947 {
5948 gold_error(_("undefined or discarded symbol %s in GOT"),
5949 gsym->name());
5950 continue;
5951 }
5952
5953 if (!gsym->is_weak_undefined())
5954 value = gsym->value();
5955 else
5956 value = 0;
5957 }
5958
5959 unsigned got_offset = reloc.got_offset();
5960 gold_assert(got_offset < oview_size);
5961
5962 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
5963 Valtype x;
5964
5965 switch (reloc.r_type())
5966 {
5967 case elfcpp::R_MIPS_TLS_DTPMOD32:
5968 case elfcpp::R_MIPS_TLS_DTPMOD64:
5969 x = value;
5970 break;
5971 case elfcpp::R_MIPS_TLS_DTPREL32:
5972 case elfcpp::R_MIPS_TLS_DTPREL64:
5973 x = value - elfcpp::DTP_OFFSET;
5974 break;
5975 case elfcpp::R_MIPS_TLS_TPREL32:
5976 case elfcpp::R_MIPS_TLS_TPREL64:
5977 x = value - elfcpp::TP_OFFSET;
5978 break;
5979 default:
5980 gold_unreachable();
5981 break;
5982 }
5983
5984 elfcpp::Swap<size, big_endian>::writeval(wv, x);
5985 }
5986
5987 of->write_output_view(offset, oview_size, oview);
5988}
5989
5990// Mips_relobj methods.
5991
5992// Count the local symbols. The Mips backend needs to know if a symbol
5993// is a MIPS16 or microMIPS function or not. For global symbols, it is easy
5994// because the Symbol object keeps the ELF symbol type and st_other field.
5995// For local symbol it is harder because we cannot access this information.
5996// So we override the do_count_local_symbol in parent and scan local symbols to
5997// mark MIPS16 and microMIPS functions. This is not the most efficient way but
5998// I do not want to slow down other ports by calling a per symbol target hook
5999// inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6000
6001template<int size, bool big_endian>
6002void
6003Mips_relobj<size, big_endian>::do_count_local_symbols(
6004 Stringpool_template<char>* pool,
6005 Stringpool_template<char>* dynpool)
6006{
6007 // Ask parent to count the local symbols.
6008 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6009 const unsigned int loccount = this->local_symbol_count();
6010 if (loccount == 0)
6011 return;
6012
6013 // Initialize the mips16 and micromips function bit-vector.
6014 this->local_symbol_is_mips16_.resize(loccount, false);
6015 this->local_symbol_is_micromips_.resize(loccount, false);
6016
6017 // Read the symbol table section header.
6018 const unsigned int symtab_shndx = this->symtab_shndx();
6019 elfcpp::Shdr<size, big_endian>
6020 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6021 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6022
6023 // Read the local symbols.
6024 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6025 gold_assert(loccount == symtabshdr.get_sh_info());
6026 off_t locsize = loccount * sym_size;
6027 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6028 locsize, true, true);
6029
6030 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6031
6032 // Skip the first dummy symbol.
6033 psyms += sym_size;
6034 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6035 {
6036 elfcpp::Sym<size, big_endian> sym(psyms);
6037 unsigned char st_other = sym.get_st_other();
6038 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6039 this->local_symbol_is_micromips_[i] =
6040 elfcpp::elf_st_is_micromips(st_other);
6041 }
6042}
6043
6044// Read the symbol information.
6045
6046template<int size, bool big_endian>
6047void
6048Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6049{
6050 // Call parent class to read symbol information.
f35c4853 6051 this->base_read_symbols(sd);
9810d34d
SS
6052
6053 // Read processor-specific flags in ELF file header.
6054 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6055 elfcpp::Elf_sizes<size>::ehdr_size,
6056 true, false);
6057 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6058 this->processor_specific_flags_ = ehdr.get_e_flags();
6059
6060 // Get the section names.
6061 const unsigned char* pnamesu = sd->section_names->data();
6062 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6063
6064 // Initialize the mips16 stub section bit-vectors.
6065 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6066 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6067 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6068
6069 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6070 const unsigned char* pshdrs = sd->section_headers->data();
6071 const unsigned char* ps = pshdrs + shdr_size;
6072 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6073 {
6074 elfcpp::Shdr<size, big_endian> shdr(ps);
6075
6076 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6077 {
6078 // Read the gp value that was used to create this object. We need the
6079 // gp value while processing relocs. The .reginfo section is not used
6080 // in the 64-bit MIPS ELF ABI.
6081 section_offset_type section_offset = shdr.get_sh_offset();
6082 section_size_type section_size =
6083 convert_to_section_size_type(shdr.get_sh_size());
6084 const unsigned char* view =
6085 this->get_view(section_offset, section_size, true, false);
6086
6087 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6088
6089 // Read the rest of .reginfo.
6090 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6091 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6092 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6093 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6094 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6095 }
6096
6097 const char* name = pnames + shdr.get_sh_name();
6098 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
6099 this->section_is_mips16_call_stub_[i] =
6100 is_prefix_of(".mips16.call.", name);
6101 this->section_is_mips16_call_fp_stub_[i] =
6102 is_prefix_of(".mips16.call.fp.", name);
6103
6104 if (strcmp(name, ".pdr") == 0)
6105 {
6106 gold_assert(this->pdr_shndx_ == -1U);
6107 this->pdr_shndx_ = i;
6108 }
6109 }
6110}
6111
6112// Discard MIPS16 stub secions that are not needed.
6113
6114template<int size, bool big_endian>
6115void
6116Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
6117{
6118 for (typename Mips16_stubs_int_map::const_iterator
6119 it = this->mips16_stub_sections_.begin();
6120 it != this->mips16_stub_sections_.end(); ++it)
6121 {
6122 Mips16_stub_section<size, big_endian>* stub_section = it->second;
6123 if (!stub_section->is_target_found())
6124 {
6125 gold_error(_("no relocation found in mips16 stub section '%s'"),
6126 stub_section->object()
6127 ->section_name(stub_section->shndx()).c_str());
6128 }
6129
6130 bool discard = false;
6131 if (stub_section->is_for_local_function())
6132 {
6133 if (stub_section->is_fn_stub())
6134 {
6135 // This stub is for a local symbol. This stub will only
6136 // be needed if there is some relocation in this object,
6137 // other than a 16 bit function call, which refers to this
6138 // symbol.
6139 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
6140 discard = true;
6141 else
6142 this->add_local_mips16_fn_stub(stub_section);
6143 }
6144 else
6145 {
6146 // This stub is for a local symbol. This stub will only
6147 // be needed if there is some relocation (R_MIPS16_26) in
6148 // this object that refers to this symbol.
6149 gold_assert(stub_section->is_call_stub()
6150 || stub_section->is_call_fp_stub());
6151 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
6152 discard = true;
6153 else
6154 this->add_local_mips16_call_stub(stub_section);
6155 }
6156 }
6157 else
6158 {
6159 Mips_symbol<size>* gsym = stub_section->gsym();
6160 if (stub_section->is_fn_stub())
6161 {
6162 if (gsym->has_mips16_fn_stub())
6163 // We already have a stub for this function.
6164 discard = true;
6165 else
6166 {
6167 gsym->set_mips16_fn_stub(stub_section);
6168 if (gsym->should_add_dynsym_entry(symtab))
6169 {
6170 // If we have a MIPS16 function with a stub, the
6171 // dynamic symbol must refer to the stub, since only
6172 // the stub uses the standard calling conventions.
6173 gsym->set_need_fn_stub();
6174 if (gsym->is_from_dynobj())
6175 gsym->set_needs_dynsym_value();
6176 }
6177 }
6178 if (!gsym->need_fn_stub())
6179 discard = true;
6180 }
6181 else if (stub_section->is_call_stub())
6182 {
6183 if (gsym->is_mips16())
6184 // We don't need the call_stub; this is a 16 bit
6185 // function, so calls from other 16 bit functions are
6186 // OK.
6187 discard = true;
6188 else if (gsym->has_mips16_call_stub())
6189 // We already have a stub for this function.
6190 discard = true;
6191 else
6192 gsym->set_mips16_call_stub(stub_section);
6193 }
6194 else
6195 {
6196 gold_assert(stub_section->is_call_fp_stub());
6197 if (gsym->is_mips16())
6198 // We don't need the call_stub; this is a 16 bit
6199 // function, so calls from other 16 bit functions are
6200 // OK.
6201 discard = true;
6202 else if (gsym->has_mips16_call_fp_stub())
6203 // We already have a stub for this function.
6204 discard = true;
6205 else
6206 gsym->set_mips16_call_fp_stub(stub_section);
6207 }
6208 }
6209 if (discard)
6210 this->set_output_section(stub_section->shndx(), NULL);
6211 }
6212}
6213
6214// Mips_output_data_la25_stub methods.
6215
6216// Template for standard LA25 stub.
6217template<int size, bool big_endian>
6218const uint32_t
6219Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
6220{
6221 0x3c190000, // lui $25,%hi(func)
6222 0x08000000, // j func
6223 0x27390000, // add $25,$25,%lo(func)
6224 0x00000000 // nop
6225};
6226
6227// Template for microMIPS LA25 stub.
6228template<int size, bool big_endian>
6229const uint32_t
6230Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
6231{
6232 0x41b9, 0x0000, // lui t9,%hi(func)
6233 0xd400, 0x0000, // j func
6234 0x3339, 0x0000, // addiu t9,t9,%lo(func)
6235 0x0000, 0x0000 // nop
6236};
6237
6238// Create la25 stub for a symbol.
6239
6240template<int size, bool big_endian>
6241void
6242Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
6243 Symbol_table* symtab, Target_mips<size, big_endian>* target,
6244 Mips_symbol<size>* gsym)
6245{
6246 if (!gsym->has_la25_stub())
6247 {
6248 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
6249 this->symbols_.insert(gsym);
6250 this->create_stub_symbol(gsym, symtab, target, 16);
6251 }
6252}
6253
6254// Create a symbol for SYM stub's value and size, to help make the disassembly
6255// easier to read.
6256
6257template<int size, bool big_endian>
6258void
6259Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
6260 Mips_symbol<size>* sym, Symbol_table* symtab,
6261 Target_mips<size, big_endian>* target, uint64_t symsize)
6262{
6263 std::string name(".pic.");
6264 name += sym->name();
6265
6266 unsigned int offset = sym->la25_stub_offset();
6267 if (sym->is_micromips())
6268 offset |= 1;
6269
6270 // Make it a local function.
6271 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
6272 Symbol_table::PREDEFINED,
6273 target->la25_stub_section(),
6274 offset, symsize, elfcpp::STT_FUNC,
6275 elfcpp::STB_LOCAL,
6276 elfcpp::STV_DEFAULT, 0,
6277 false, false);
6278 new_sym->set_is_forced_local();
6279}
6280
6281// Write out la25 stubs. This uses the hand-coded instructions above,
6282// and adjusts them as needed.
6283
6284template<int size, bool big_endian>
6285void
6286Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
6287{
6288 const off_t offset = this->offset();
6289 const section_size_type oview_size =
6290 convert_to_section_size_type(this->data_size());
6291 unsigned char* const oview = of->get_output_view(offset, oview_size);
6292
6293 for (typename Unordered_set<Mips_symbol<size>*>::iterator
6294 p = this->symbols_.begin();
6295 p != this->symbols_.end();
6296 ++p)
6297 {
6298 Mips_symbol<size>* sym = *p;
6299 unsigned char* pov = oview + sym->la25_stub_offset();
6300
6301 Mips_address target = sym->value();
6302 if (!sym->is_micromips())
6303 {
6304 elfcpp::Swap<32, big_endian>::writeval(pov,
6305 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
6306 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
6307 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
6308 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
6309 la25_stub_entry[2] | (target & 0xffff));
6310 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
6311 }
6312 else
6313 {
6314 target |= 1;
6315 // First stub instruction. Paste high 16-bits of the target.
6316 elfcpp::Swap<16, big_endian>::writeval(pov,
6317 la25_stub_micromips_entry[0]);
6318 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
6319 ((target + 0x8000) >> 16) & 0xffff);
6320 // Second stub instruction. Paste low 26-bits of the target, shifted
6321 // right by 1.
6322 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
6323 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
6324 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
6325 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
6326 // Third stub instruction. Paste low 16-bits of the target.
6327 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
6328 la25_stub_micromips_entry[4]);
6329 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
6330 // Fourth stub instruction.
6331 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
6332 la25_stub_micromips_entry[6]);
6333 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
6334 la25_stub_micromips_entry[7]);
6335 }
6336 }
6337
6338 of->write_output_view(offset, oview_size, oview);
6339}
6340
6341// Mips_output_data_plt methods.
6342
6343// The format of the first PLT entry in an O32 executable.
6344template<int size, bool big_endian>
6345const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
6346{
6347 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
6348 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
6349 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
6350 0x031cc023, // subu $24, $24, $28
40fc1451 6351 0x03e07825, // or $15, $31, zero
9810d34d
SS
6352 0x0018c082, // srl $24, $24, 2
6353 0x0320f809, // jalr $25
6354 0x2718fffe // subu $24, $24, 2
6355};
6356
6357// The format of the first PLT entry in an N32 executable. Different
6358// because gp ($28) is not available; we use t2 ($14) instead.
6359template<int size, bool big_endian>
6360const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
6361{
6362 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6363 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
6364 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6365 0x030ec023, // subu $24, $24, $14
40fc1451 6366 0x03e07825, // or $15, $31, zero
9810d34d
SS
6367 0x0018c082, // srl $24, $24, 2
6368 0x0320f809, // jalr $25
6369 0x2718fffe // subu $24, $24, 2
6370};
6371
6372// The format of the first PLT entry in an N64 executable. Different
6373// from N32 because of the increased size of GOT entries.
6374template<int size, bool big_endian>
6375const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
6376{
6377 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6378 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
6379 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6380 0x030ec023, // subu $24, $24, $14
40fc1451 6381 0x03e07825, // or $15, $31, zero
9810d34d
SS
6382 0x0018c0c2, // srl $24, $24, 3
6383 0x0320f809, // jalr $25
6384 0x2718fffe // subu $24, $24, 2
6385};
6386
6387// The format of the microMIPS first PLT entry in an O32 executable.
6388// We rely on v0 ($2) rather than t8 ($24) to contain the address
6389// of the GOTPLT entry handled, so this stub may only be used when
6390// all the subsequent PLT entries are microMIPS code too.
6391//
6392// The trailing NOP is for alignment and correct disassembly only.
6393template<int size, bool big_endian>
6394const uint32_t Mips_output_data_plt<size, big_endian>::
6395plt0_entry_micromips_o32[] =
6396{
6397 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
6398 0xff23, 0x0000, // lw $25, 0($3)
6399 0x0535, // subu $2, $2, $3
6400 0x2525, // srl $2, $2, 2
6401 0x3302, 0xfffe, // subu $24, $2, 2
6402 0x0dff, // move $15, $31
6403 0x45f9, // jalrs $25
6404 0x0f83, // move $28, $3
6405 0x0c00 // nop
6406};
6407
6408// The format of the microMIPS first PLT entry in an O32 executable
6409// in the insn32 mode.
6410template<int size, bool big_endian>
6411const uint32_t Mips_output_data_plt<size, big_endian>::
6412plt0_entry_micromips32_o32[] =
6413{
6414 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
6415 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
6416 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
6417 0x0398, 0xc1d0, // subu $24, $24, $28
40fc1451 6418 0x001f, 0x7a90, // or $15, $31, zero
9810d34d
SS
6419 0x0318, 0x1040, // srl $24, $24, 2
6420 0x03f9, 0x0f3c, // jalr $25
6421 0x3318, 0xfffe // subu $24, $24, 2
6422};
6423
6424// The format of subsequent standard entries in the PLT.
6425template<int size, bool big_endian>
6426const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
6427{
6428 0x3c0f0000, // lui $15, %hi(.got.plt entry)
6429 0x8df90000, // l[wd] $25, %lo(.got.plt entry)($15)
6430 0x03200008, // jr $25
6431 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
6432};
6433
6434// The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
6435// temporary because t8 ($24) and t9 ($25) are not directly addressable.
6436// Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
6437// We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
6438// target function address in register v0.
6439template<int size, bool big_endian>
6440const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
6441{
6442 0xb303, // lw $3, 12($pc)
6443 0x651b, // move $24, $3
6444 0x9b60, // lw $3, 0($3)
6445 0xeb00, // jr $3
6446 0x653b, // move $25, $3
6447 0x6500, // nop
6448 0x0000, 0x0000 // .word (.got.plt entry)
6449};
6450
6451// The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
6452// as a temporary because t8 ($24) is not addressable with ADDIUPC.
6453template<int size, bool big_endian>
6454const uint32_t Mips_output_data_plt<size, big_endian>::
6455plt_entry_micromips_o32[] =
6456{
6457 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
6458 0xff22, 0x0000, // lw $25, 0($2)
6459 0x4599, // jr $25
6460 0x0f02 // move $24, $2
6461};
6462
6463// The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
6464template<int size, bool big_endian>
6465const uint32_t Mips_output_data_plt<size, big_endian>::
6466plt_entry_micromips32_o32[] =
6467{
6468 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
6469 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
6470 0x0019, 0x0f3c, // jr $25
6471 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
6472};
6473
6474// Add an entry to the PLT for a symbol referenced by r_type relocation.
6475
6476template<int size, bool big_endian>
6477void
6478Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
6479 unsigned int r_type)
6480{
6481 gold_assert(!gsym->has_plt_offset());
6482
6483 // Final PLT offset for a symbol will be set in method set_plt_offsets().
6484 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
6485 + sizeof(plt0_entry_o32));
6486 this->symbols_.push_back(gsym);
6487
6488 // Record whether the relocation requires a standard MIPS
6489 // or a compressed code entry.
6490 if (jal_reloc(r_type))
6491 {
6492 if (r_type == elfcpp::R_MIPS_26)
6493 gsym->set_needs_mips_plt(true);
6494 else
6495 gsym->set_needs_comp_plt(true);
6496 }
6497
6498 section_offset_type got_offset = this->got_plt_->current_data_size();
6499
6500 // Every PLT entry needs a GOT entry which points back to the PLT
6501 // entry (this will be changed by the dynamic linker, normally
6502 // lazily when the function is called).
6503 this->got_plt_->set_current_data_size(got_offset + size/8);
6504
6505 gsym->set_needs_dynsym_entry();
6506 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
6507 got_offset);
6508}
6509
6510// Set final PLT offsets. For each symbol, determine whether standard or
6511// compressed (MIPS16 or microMIPS) PLT entry is used.
6512
6513template<int size, bool big_endian>
6514void
6515Mips_output_data_plt<size, big_endian>::set_plt_offsets()
6516{
6517 // The sizes of individual PLT entries.
6518 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
6519 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
6520 ? this->compressed_plt_entry_size() : 0);
6521
6522 for (typename std::vector<Mips_symbol<size>*>::const_iterator
6523 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
6524 {
6525 Mips_symbol<size>* mips_sym = *p;
6526
6527 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
6528 // so always use a standard entry there.
6529 //
6530 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
6531 // all MIPS16 calls will go via that stub, and there is no benefit
6532 // to having a MIPS16 entry. And in the case of call_stub a
6533 // standard entry actually has to be used as the stub ends with a J
6534 // instruction.
6535 if (this->target_->is_output_newabi()
6536 || mips_sym->has_mips16_call_stub()
6537 || mips_sym->has_mips16_call_fp_stub())
6538 {
6539 mips_sym->set_needs_mips_plt(true);
6540 mips_sym->set_needs_comp_plt(false);
6541 }
6542
6543 // Otherwise, if there are no direct calls to the function, we
6544 // have a free choice of whether to use standard or compressed
6545 // entries. Prefer microMIPS entries if the object is known to
6546 // contain microMIPS code, so that it becomes possible to create
6547 // pure microMIPS binaries. Prefer standard entries otherwise,
6548 // because MIPS16 ones are no smaller and are usually slower.
6549 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
6550 {
6551 if (this->target_->is_output_micromips())
6552 mips_sym->set_needs_comp_plt(true);
6553 else
6554 mips_sym->set_needs_mips_plt(true);
6555 }
6556
6557 if (mips_sym->needs_mips_plt())
6558 {
6559 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
6560 this->plt_mips_offset_ += plt_mips_entry_size;
6561 }
6562 if (mips_sym->needs_comp_plt())
6563 {
6564 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
6565 this->plt_comp_offset_ += plt_comp_entry_size;
6566 }
6567 }
6568
6569 // Figure out the size of the PLT header if we know that we are using it.
6570 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
6571 this->plt_header_size_ = this->get_plt_header_size();
6572}
6573
6574// Write out the PLT. This uses the hand-coded instructions above,
6575// and adjusts them as needed.
6576
6577template<int size, bool big_endian>
6578void
6579Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
6580{
6581 const off_t offset = this->offset();
6582 const section_size_type oview_size =
6583 convert_to_section_size_type(this->data_size());
6584 unsigned char* const oview = of->get_output_view(offset, oview_size);
6585
6586 const off_t gotplt_file_offset = this->got_plt_->offset();
6587 const section_size_type gotplt_size =
6588 convert_to_section_size_type(this->got_plt_->data_size());
6589 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
6590 gotplt_size);
6591 unsigned char* pov = oview;
6592
6593 Mips_address plt_address = this->address();
6594
6595 // Calculate the address of .got.plt.
6596 Mips_address gotplt_addr = this->got_plt_->address();
6597 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
6598 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
6599
6600 // The PLT sequence is not safe for N64 if .got.plt's address can
6601 // not be loaded in two instructions.
6602 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
6603 || ~(gotplt_addr | 0x7fffffff) == 0);
6604
6605 // Write the PLT header.
6606 const uint32_t* plt0_entry = this->get_plt_header_entry();
6607 if (plt0_entry == plt0_entry_micromips_o32)
6608 {
6609 // Write microMIPS PLT header.
6610 gold_assert(gotplt_addr % 4 == 0);
6611
6612 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
6613
6614 // ADDIUPC has a span of +/-16MB, check we're in range.
6615 if (gotpc_offset + 0x1000000 >= 0x2000000)
6616 {
6617 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
6618 "ADDIUPC"), (long)gotpc_offset);
6619 return;
6620 }
6621
6622 elfcpp::Swap<16, big_endian>::writeval(pov,
6623 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
6624 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
6625 (gotpc_offset >> 2) & 0xffff);
6626 pov += 4;
6627 for (unsigned int i = 2;
6628 i < (sizeof(plt0_entry_micromips_o32)
6629 / sizeof(plt0_entry_micromips_o32[0]));
6630 i++)
6631 {
6632 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
6633 pov += 2;
6634 }
6635 }
6636 else if (plt0_entry == plt0_entry_micromips32_o32)
6637 {
6638 // Write microMIPS PLT header in insn32 mode.
6639 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
6640 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
6641 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
6642 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
6643 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
6644 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
6645 pov += 12;
6646 for (unsigned int i = 6;
6647 i < (sizeof(plt0_entry_micromips32_o32)
6648 / sizeof(plt0_entry_micromips32_o32[0]));
6649 i++)
6650 {
6651 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
6652 pov += 2;
6653 }
6654 }
6655 else
6656 {
6657 // Write standard PLT header.
6658 elfcpp::Swap<32, big_endian>::writeval(pov,
6659 plt0_entry[0] | gotplt_addr_high);
6660 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
6661 plt0_entry[1] | gotplt_addr_low);
6662 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
6663 plt0_entry[2] | gotplt_addr_low);
6664 pov += 12;
6665 for (int i = 3; i < 8; i++)
6666 {
6667 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
6668 pov += 4;
6669 }
6670 }
6671
6672
6673 unsigned char* gotplt_pov = gotplt_view;
6674 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
6675
6676 // The first two entries in .got.plt are reserved.
6677 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
6678 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
6679
6680 unsigned int gotplt_offset = 2 * got_entry_size;
6681 gotplt_pov += 2 * got_entry_size;
6682
6683 // Calculate the address of the PLT header.
6684 Mips_address header_address = (plt_address
6685 + (this->is_plt_header_compressed() ? 1 : 0));
6686
6687 // Initialize compressed PLT area view.
6688 unsigned char* pov2 = pov + this->plt_mips_offset_;
6689
6690 // Write the PLT entries.
6691 for (typename std::vector<Mips_symbol<size>*>::const_iterator
6692 p = this->symbols_.begin();
6693 p != this->symbols_.end();
6694 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
6695 {
6696 Mips_symbol<size>* mips_sym = *p;
6697
6698 // Calculate the address of the .got.plt entry.
6699 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
6700 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
6701 & 0xffff);
6702 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
6703
6704 // Initially point the .got.plt entry at the PLT header.
6705 if (this->target_->is_output_n64())
6706 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
6707 else
6708 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
6709
6710 // Now handle the PLT itself. First the standard entry.
6711 if (mips_sym->has_mips_plt_offset())
6712 {
6713 // Pick the load opcode (LW or LD).
6714 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
6715 : 0x8c000000;
6716
6717 // Fill in the PLT entry itself.
6718 elfcpp::Swap<32, big_endian>::writeval(pov,
6719 plt_entry[0] | gotplt_entry_addr_hi);
6720 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
6721 plt_entry[1] | gotplt_entry_addr_lo | load);
6722 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_entry[2]);
6723 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
6724 plt_entry[3] | gotplt_entry_addr_lo);
6725 pov += 16;
6726 }
6727
6728 // Now the compressed entry. They come after any standard ones.
6729 if (mips_sym->has_comp_plt_offset())
6730 {
6731 if (!this->target_->is_output_micromips())
6732 {
6733 // Write MIPS16 PLT entry.
6734 const uint32_t* plt_entry = plt_entry_mips16_o32;
6735
6736 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
6737 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
6738 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
6739 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
6740 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
6741 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
6742 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
6743 gotplt_entry_addr);
6744 pov2 += 16;
6745 }
6746 else if (this->target_->use_32bit_micromips_instructions())
6747 {
6748 // Write microMIPS PLT entry in insn32 mode.
6749 const uint32_t* plt_entry = plt_entry_micromips32_o32;
6750
6751 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
6752 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
6753 gotplt_entry_addr_hi);
6754 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
6755 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
6756 gotplt_entry_addr_lo);
6757 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
6758 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
6759 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
6760 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
6761 gotplt_entry_addr_lo);
6762 pov2 += 16;
6763 }
6764 else
6765 {
6766 // Write microMIPS PLT entry.
6767 const uint32_t* plt_entry = plt_entry_micromips_o32;
6768
6769 gold_assert(gotplt_entry_addr % 4 == 0);
6770
6771 Mips_address loc_address = plt_address + pov2 - oview;
6772 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
6773
6774 // ADDIUPC has a span of +/-16MB, check we're in range.
6775 if (gotpc_offset + 0x1000000 >= 0x2000000)
6776 {
6777 gold_error(_(".got.plt offset of %ld from .plt beyond the "
6778 "range of ADDIUPC"), (long)gotpc_offset);
6779 return;
6780 }
6781
6782 elfcpp::Swap<16, big_endian>::writeval(pov2,
6783 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
6784 elfcpp::Swap<16, big_endian>::writeval(
6785 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
6786 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
6787 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
6788 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
6789 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
6790 pov2 += 12;
6791 }
6792 }
6793 }
6794
6795 // Check the number of bytes written for standard entries.
6796 gold_assert(static_cast<section_size_type>(
6797 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
6798 // Check the number of bytes written for compressed entries.
6799 gold_assert((static_cast<section_size_type>(pov2 - pov)
6800 == this->plt_comp_offset_));
6801 // Check the total number of bytes written.
6802 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
6803
6804 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
6805 == gotplt_size);
6806
6807 of->write_output_view(offset, oview_size, oview);
6808 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
6809}
6810
6811// Mips_output_data_mips_stubs methods.
6812
6813// The format of the lazy binding stub when dynamic symbol count is less than
6814// 64K, dynamic symbol index is less than 32K, and ABI is not N64.
6815template<int size, bool big_endian>
6816const uint32_t
6817Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
6818{
6819 0x8f998010, // lw t9,0x8010(gp)
40fc1451 6820 0x03e07825, // or t7,ra,zero
9810d34d
SS
6821 0x0320f809, // jalr t9,ra
6822 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
6823};
6824
6825// The format of the lazy binding stub when dynamic symbol count is less than
6826// 64K, dynamic symbol index is less than 32K, and ABI is N64.
6827template<int size, bool big_endian>
6828const uint32_t
6829Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
6830{
6831 0xdf998010, // ld t9,0x8010(gp)
40fc1451 6832 0x03e07825, // or t7,ra,zero
9810d34d
SS
6833 0x0320f809, // jalr t9,ra
6834 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
6835};
6836
6837// The format of the lazy binding stub when dynamic symbol count is less than
6838// 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
6839template<int size, bool big_endian>
6840const uint32_t
6841Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
6842{
6843 0x8f998010, // lw t9,0x8010(gp)
40fc1451 6844 0x03e07825, // or t7,ra,zero
9810d34d
SS
6845 0x0320f809, // jalr t9,ra
6846 0x34180000 // ori t8,zero,DYN_INDEX unsigned
6847};
6848
6849// The format of the lazy binding stub when dynamic symbol count is less than
6850// 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
6851template<int size, bool big_endian>
6852const uint32_t
6853Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
6854{
6855 0xdf998010, // ld t9,0x8010(gp)
40fc1451 6856 0x03e07825, // or t7,ra,zero
9810d34d
SS
6857 0x0320f809, // jalr t9,ra
6858 0x34180000 // ori t8,zero,DYN_INDEX unsigned
6859};
6860
6861// The format of the lazy binding stub when dynamic symbol count is greater than
6862// 64K, and ABI is not N64.
6863template<int size, bool big_endian>
6864const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
6865{
6866 0x8f998010, // lw t9,0x8010(gp)
40fc1451 6867 0x03e07825, // or t7,ra,zero
9810d34d
SS
6868 0x3c180000, // lui t8,DYN_INDEX
6869 0x0320f809, // jalr t9,ra
6870 0x37180000 // ori t8,t8,DYN_INDEX
6871};
6872
6873// The format of the lazy binding stub when dynamic symbol count is greater than
6874// 64K, and ABI is N64.
6875template<int size, bool big_endian>
6876const uint32_t
6877Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
6878{
6879 0xdf998010, // ld t9,0x8010(gp)
40fc1451 6880 0x03e07825, // or t7,ra,zero
9810d34d
SS
6881 0x3c180000, // lui t8,DYN_INDEX
6882 0x0320f809, // jalr t9,ra
6883 0x37180000 // ori t8,t8,DYN_INDEX
6884};
6885
6886// microMIPS stubs.
6887
6888// The format of the microMIPS lazy binding stub when dynamic symbol count is
6889// less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
6890template<int size, bool big_endian>
6891const uint32_t
6892Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
6893{
6894 0xff3c, 0x8010, // lw t9,0x8010(gp)
6895 0x0dff, // move t7,ra
6896 0x45d9, // jalr t9
6897 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
6898};
6899
6900// The format of the microMIPS lazy binding stub when dynamic symbol count is
6901// less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
6902template<int size, bool big_endian>
6903const uint32_t
6904Mips_output_data_mips_stubs<size, big_endian>::
6905lazy_stub_micromips_normal_1_n64[] =
6906{
6907 0xdf3c, 0x8010, // ld t9,0x8010(gp)
6908 0x0dff, // move t7,ra
6909 0x45d9, // jalr t9
6910 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
6911};
6912
6913// The format of the microMIPS lazy binding stub when dynamic symbol
6914// count is less than 64K, dynamic symbol index is between 32K and 64K,
6915// and ABI is not N64.
6916template<int size, bool big_endian>
6917const uint32_t
6918Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
6919{
6920 0xff3c, 0x8010, // lw t9,0x8010(gp)
6921 0x0dff, // move t7,ra
6922 0x45d9, // jalr t9
6923 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
6924};
6925
6926// The format of the microMIPS lazy binding stub when dynamic symbol
6927// count is less than 64K, dynamic symbol index is between 32K and 64K,
6928// and ABI is N64.
6929template<int size, bool big_endian>
6930const uint32_t
6931Mips_output_data_mips_stubs<size, big_endian>::
6932lazy_stub_micromips_normal_2_n64[] =
6933{
6934 0xdf3c, 0x8010, // ld t9,0x8010(gp)
6935 0x0dff, // move t7,ra
6936 0x45d9, // jalr t9
6937 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
6938};
6939
6940// The format of the microMIPS lazy binding stub when dynamic symbol count is
6941// greater than 64K, and ABI is not N64.
6942template<int size, bool big_endian>
6943const uint32_t
6944Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
6945{
6946 0xff3c, 0x8010, // lw t9,0x8010(gp)
6947 0x0dff, // move t7,ra
6948 0x41b8, 0x0000, // lui t8,DYN_INDEX
6949 0x45d9, // jalr t9
6950 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
6951};
6952
6953// The format of the microMIPS lazy binding stub when dynamic symbol count is
6954// greater than 64K, and ABI is N64.
6955template<int size, bool big_endian>
6956const uint32_t
6957Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
6958{
6959 0xdf3c, 0x8010, // ld t9,0x8010(gp)
6960 0x0dff, // move t7,ra
6961 0x41b8, 0x0000, // lui t8,DYN_INDEX
6962 0x45d9, // jalr t9
6963 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
6964};
6965
6966// 32-bit microMIPS stubs.
6967
6968// The format of the microMIPS lazy binding stub when dynamic symbol count is
6969// less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
6970// can use only 32-bit instructions.
6971template<int size, bool big_endian>
6972const uint32_t
6973Mips_output_data_mips_stubs<size, big_endian>::
6974lazy_stub_micromips32_normal_1[] =
6975{
6976 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 6977 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
6978 0x03f9, 0x0f3c, // jalr ra,t9
6979 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
6980};
6981
6982// The format of the microMIPS lazy binding stub when dynamic symbol count is
6983// less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
6984// use only 32-bit instructions.
6985template<int size, bool big_endian>
6986const uint32_t
6987Mips_output_data_mips_stubs<size, big_endian>::
6988lazy_stub_micromips32_normal_1_n64[] =
6989{
6990 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 6991 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
6992 0x03f9, 0x0f3c, // jalr ra,t9
6993 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
6994};
6995
6996// The format of the microMIPS lazy binding stub when dynamic symbol
6997// count is less than 64K, dynamic symbol index is between 32K and 64K,
6998// ABI is not N64, and we can use only 32-bit instructions.
6999template<int size, bool big_endian>
7000const uint32_t
7001Mips_output_data_mips_stubs<size, big_endian>::
7002lazy_stub_micromips32_normal_2[] =
7003{
7004 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7005 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7006 0x03f9, 0x0f3c, // jalr ra,t9
7007 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7008};
7009
7010// The format of the microMIPS lazy binding stub when dynamic symbol
7011// count is less than 64K, dynamic symbol index is between 32K and 64K,
7012// ABI is N64, and we can use only 32-bit instructions.
7013template<int size, bool big_endian>
7014const uint32_t
7015Mips_output_data_mips_stubs<size, big_endian>::
7016lazy_stub_micromips32_normal_2_n64[] =
7017{
7018 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7019 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7020 0x03f9, 0x0f3c, // jalr ra,t9
7021 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7022};
7023
7024// The format of the microMIPS lazy binding stub when dynamic symbol count is
7025// greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7026template<int size, bool big_endian>
7027const uint32_t
7028Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7029{
7030 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7031 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7032 0x41b8, 0x0000, // lui t8,DYN_INDEX
7033 0x03f9, 0x0f3c, // jalr ra,t9
7034 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7035};
7036
7037// The format of the microMIPS lazy binding stub when dynamic symbol count is
7038// greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7039template<int size, bool big_endian>
7040const uint32_t
7041Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7042{
7043 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7044 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7045 0x41b8, 0x0000, // lui t8,DYN_INDEX
7046 0x03f9, 0x0f3c, // jalr ra,t9
7047 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7048};
7049
7050// Create entry for a symbol.
7051
7052template<int size, bool big_endian>
7053void
7054Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7055 Mips_symbol<size>* gsym)
7056{
7057 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7058 {
7059 this->symbols_.insert(gsym);
7060 gsym->set_has_lazy_stub(true);
7061 }
7062}
7063
7064// Remove entry for a symbol.
7065
7066template<int size, bool big_endian>
7067void
7068Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
7069 Mips_symbol<size>* gsym)
7070{
7071 if (gsym->has_lazy_stub())
7072 {
7073 this->symbols_.erase(gsym);
7074 gsym->set_has_lazy_stub(false);
7075 }
7076}
7077
7078// Set stub offsets for symbols. This method expects that the number of
7079// entries in dynamic symbol table is set.
7080
7081template<int size, bool big_endian>
7082void
7083Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
7084{
7085 gold_assert(this->dynsym_count_ != -1U);
7086
7087 if (this->stub_offsets_are_set_)
7088 return;
7089
7090 unsigned int stub_size = this->stub_size();
7091 unsigned int offset = 0;
7092 for (typename Unordered_set<Mips_symbol<size>*>::const_iterator
7093 p = this->symbols_.begin();
7094 p != this->symbols_.end();
7095 ++p, offset += stub_size)
7096 {
7097 Mips_symbol<size>* mips_sym = *p;
7098 mips_sym->set_lazy_stub_offset(offset);
7099 }
7100 this->stub_offsets_are_set_ = true;
7101}
7102
7103template<int size, bool big_endian>
7104void
7105Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
7106{
7107 for (typename Unordered_set<Mips_symbol<size>*>::const_iterator
7108 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7109 {
7110 Mips_symbol<size>* sym = *p;
7111 if (sym->is_from_dynobj())
7112 sym->set_needs_dynsym_value();
7113 }
7114}
7115
7116// Write out the .MIPS.stubs. This uses the hand-coded instructions and
7117// adjusts them as needed.
7118
7119template<int size, bool big_endian>
7120void
7121Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
7122{
7123 const off_t offset = this->offset();
7124 const section_size_type oview_size =
7125 convert_to_section_size_type(this->data_size());
7126 unsigned char* const oview = of->get_output_view(offset, oview_size);
7127
7128 bool big_stub = this->dynsym_count_ > 0x10000;
7129
7130 unsigned char* pov = oview;
7131 for (typename Unordered_set<Mips_symbol<size>*>::const_iterator
7132 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7133 {
7134 Mips_symbol<size>* sym = *p;
7135 const uint32_t* lazy_stub;
7136 bool n64 = this->target_->is_output_n64();
7137
7138 if (!this->target_->is_output_micromips())
7139 {
7140 // Write standard (non-microMIPS) stub.
7141 if (!big_stub)
7142 {
7143 if (sym->dynsym_index() & ~0x7fff)
7144 // Dynsym index is between 32K and 64K.
7145 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
7146 else
7147 // Dynsym index is less than 32K.
7148 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
7149 }
7150 else
7151 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
7152
7153 unsigned int i = 0;
7154 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7155 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
7156 pov += 8;
7157
7158 i += 2;
7159 if (big_stub)
7160 {
7161 // LUI instruction of the big stub. Paste high 16 bits of the
7162 // dynsym index.
7163 elfcpp::Swap<32, big_endian>::writeval(pov,
7164 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
7165 pov += 4;
7166 i += 1;
7167 }
7168 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7169 // Last stub instruction. Paste low 16 bits of the dynsym index.
7170 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7171 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
7172 pov += 8;
7173 }
7174 else if (this->target_->use_32bit_micromips_instructions())
7175 {
7176 // Write microMIPS stub in insn32 mode.
7177 if (!big_stub)
7178 {
7179 if (sym->dynsym_index() & ~0x7fff)
7180 // Dynsym index is between 32K and 64K.
7181 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
7182 : lazy_stub_micromips32_normal_2;
7183 else
7184 // Dynsym index is less than 32K.
7185 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
7186 : lazy_stub_micromips32_normal_1;
7187 }
7188 else
7189 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
7190 : lazy_stub_micromips32_big;
7191
7192 unsigned int i = 0;
7193 // First stub instruction. We emit 32-bit microMIPS instructions by
7194 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7195 // the instruction where the opcode is must always come first, for
7196 // both little and big endian.
7197 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7198 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7199 // Second stub instruction.
7200 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7201 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
7202 pov += 8;
7203 i += 4;
7204 if (big_stub)
7205 {
7206 // LUI instruction of the big stub. Paste high 16 bits of the
7207 // dynsym index.
7208 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7209 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7210 (sym->dynsym_index() >> 16) & 0x7fff);
7211 pov += 4;
7212 i += 2;
7213 }
7214 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7215 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7216 // Last stub instruction. Paste low 16 bits of the dynsym index.
7217 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7218 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7219 sym->dynsym_index() & 0xffff);
7220 pov += 8;
7221 }
7222 else
7223 {
7224 // Write microMIPS stub.
7225 if (!big_stub)
7226 {
7227 if (sym->dynsym_index() & ~0x7fff)
7228 // Dynsym index is between 32K and 64K.
7229 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
7230 : lazy_stub_micromips_normal_2;
7231 else
7232 // Dynsym index is less than 32K.
7233 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
7234 : lazy_stub_micromips_normal_1;
7235 }
7236 else
7237 lazy_stub = n64 ? lazy_stub_micromips_big_n64
7238 : lazy_stub_micromips_big;
7239
7240 unsigned int i = 0;
7241 // First stub instruction. We emit 32-bit microMIPS instructions by
7242 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7243 // the instruction where the opcode is must always come first, for
7244 // both little and big endian.
7245 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7246 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7247 // Second stub instruction.
7248 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7249 pov += 6;
7250 i += 3;
7251 if (big_stub)
7252 {
7253 // LUI instruction of the big stub. Paste high 16 bits of the
7254 // dynsym index.
7255 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7256 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7257 (sym->dynsym_index() >> 16) & 0x7fff);
7258 pov += 4;
7259 i += 2;
7260 }
7261 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7262 // Last stub instruction. Paste low 16 bits of the dynsym index.
7263 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7264 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7265 sym->dynsym_index() & 0xffff);
7266 pov += 6;
7267 }
7268 }
7269
7270 // We always allocate 20 bytes for every stub, because final dynsym count is
7271 // not known in method do_finalize_sections. There are 4 unused bytes per
7272 // stub if final dynsym count is less than 0x10000.
7273 unsigned int used = pov - oview;
7274 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
7275 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
7276
7277 // Fill the unused space with zeroes.
7278 // TODO(sasa): Can we strip unused bytes during the relaxation?
7279 if (unused > 0)
7280 memset(pov, 0, unused);
7281
7282 of->write_output_view(offset, oview_size, oview);
7283}
7284
7285// Mips_output_section_reginfo methods.
7286
7287template<int size, bool big_endian>
7288void
7289Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
7290{
7291 off_t offset = this->offset();
7292 off_t data_size = this->data_size();
7293
7294 unsigned char* view = of->get_output_view(offset, data_size);
7295 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
7296 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
7297 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
7298 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
7299 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
7300 // Write the gp value.
7301 elfcpp::Swap<size, big_endian>::writeval(view + 20,
7302 this->target_->gp_value());
7303
7304 of->write_output_view(offset, data_size, view);
7305}
7306
7307// Mips_copy_relocs methods.
7308
7309// Emit any saved relocs.
7310
7311template<int sh_type, int size, bool big_endian>
7312void
7313Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
7314 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7315 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7316{
7317 for (typename Copy_relocs<sh_type, size, big_endian>::
7318 Copy_reloc_entries::iterator p = this->entries_.begin();
7319 p != this->entries_.end();
7320 ++p)
7321 emit_entry(*p, reloc_section, symtab, layout, target);
7322
7323 // We no longer need the saved information.
7324 this->entries_.clear();
7325}
7326
7327// Emit the reloc if appropriate.
7328
7329template<int sh_type, int size, bool big_endian>
7330void
7331Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
7332 Copy_reloc_entry& entry,
7333 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7334 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7335{
7336 // If the symbol is no longer defined in a dynamic object, then we
7337 // emitted a COPY relocation, and we do not want to emit this
7338 // dynamic relocation.
7339 if (!entry.sym_->is_from_dynobj())
7340 return;
7341
7342 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
7343 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
7344 || entry.reloc_type_ == elfcpp::R_MIPS_64);
7345
7346 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
7347 if (can_make_dynamic && !sym->has_static_relocs())
7348 {
7349 Mips_relobj<size, big_endian>* object =
7350 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
7351 target->got_section(symtab, layout)->record_global_got_symbol(
7352 sym, object, entry.reloc_type_, true, false);
7353 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
7354 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
7355 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
7356 else
7357 target->rel_dyn_section(layout)->add_symbolless_global_addend(
7358 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
7359 entry.shndx_, entry.address_);
7360 }
7361 else
7362 this->make_copy_reloc(symtab, layout,
7363 static_cast<Sized_symbol<size>*>(entry.sym_),
7364 reloc_section);
7365}
7366
7367// Target_mips methods.
7368
7369// Return the value to use for a dynamic symbol which requires special
7370// treatment. This is how we support equality comparisons of function
7371// pointers across shared library boundaries, as described in the
7372// processor specific ABI supplement.
7373
7374template<int size, bool big_endian>
7375uint64_t
7376Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7377{
7378 uint64_t value = 0;
7379 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
7380
7381 if (!mips_sym->has_lazy_stub())
7382 {
7383 if (mips_sym->has_plt_offset())
7384 {
7385 // We distinguish between PLT entries and lazy-binding stubs by
7386 // giving the former an st_other value of STO_MIPS_PLT. Set the
7387 // value to the stub address if there are any relocations in the
7388 // binary where pointer equality matters.
7389 if (mips_sym->pointer_equality_needed())
7390 {
7391 // Prefer a standard MIPS PLT entry.
7392 if (mips_sym->has_mips_plt_offset())
7393 value = this->plt_section()->mips_entry_address(mips_sym);
7394 else
7395 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
7396 }
7397 else
7398 value = 0;
7399 }
7400 }
7401 else
7402 {
7403 // First, set stub offsets for symbols. This method expects that the
7404 // number of entries in dynamic symbol table is set.
7405 this->mips_stubs_section()->set_lazy_stub_offsets();
7406
7407 // The run-time linker uses the st_value field of the symbol
7408 // to reset the global offset table entry for this external
7409 // to its stub address when unlinking a shared object.
7410 value = this->mips_stubs_section()->stub_address(mips_sym);
7411 }
7412
7413 if (mips_sym->has_mips16_fn_stub())
7414 {
7415 // If we have a MIPS16 function with a stub, the dynamic symbol must
7416 // refer to the stub, since only the stub uses the standard calling
7417 // conventions.
7418 value = mips_sym->template
7419 get_mips16_fn_stub<big_endian>()->output_address();
7420 }
7421
7422 return value;
7423}
7424
7425// Get the dynamic reloc section, creating it if necessary. It's always
7426// .rel.dyn, even for MIPS64.
7427
7428template<int size, bool big_endian>
7429typename Target_mips<size, big_endian>::Reloc_section*
7430Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
7431{
7432 if (this->rel_dyn_ == NULL)
7433 {
7434 gold_assert(layout != NULL);
7435 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
7436 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
7437 elfcpp::SHF_ALLOC, this->rel_dyn_,
7438 ORDER_DYNAMIC_RELOCS, false);
7439
7440 // First entry in .rel.dyn has to be null.
7441 // This is hack - we define dummy output data and set its address to 0,
7442 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
7443 // This ensures that the entry is null.
7444 Output_data* od = new Output_data_zero_fill(0, 0);
7445 od->set_address(0);
7446 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
7447 }
7448 return this->rel_dyn_;
7449}
7450
7451// Get the GOT section, creating it if necessary.
7452
7453template<int size, bool big_endian>
7454Mips_output_data_got<size, big_endian>*
7455Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
7456 Layout* layout)
7457{
7458 if (this->got_ == NULL)
7459 {
7460 gold_assert(symtab != NULL && layout != NULL);
7461
7462 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
7463 layout);
7464 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
7465 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
7466 elfcpp::SHF_MIPS_GPREL),
7467 this->got_, ORDER_DATA, false);
7468
7469 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
7470 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
7471 Symbol_table::PREDEFINED,
7472 this->got_,
7473 0, 0, elfcpp::STT_OBJECT,
7474 elfcpp::STB_GLOBAL,
7475 elfcpp::STV_DEFAULT, 0,
7476 false, false);
7477 }
7478
7479 return this->got_;
7480}
7481
7482// Calculate value of _gp symbol.
7483
7484template<int size, bool big_endian>
7485void
7486Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
7487{
7488 if (this->gp_ != NULL)
7489 return;
7490
7491 Output_data* section = layout->find_output_section(".got");
7492 if (section == NULL)
7493 {
7494 // If there is no .got section, gp should be based on .sdata.
7495 // TODO(sasa): This is probably not needed. This was needed for older
7496 // MIPS architectures which accessed both GOT and .sdata section using
7497 // gp-relative addressing. Modern Mips Linux ELF architectures don't
7498 // access .sdata using gp-relative addressing.
7499 for (Layout::Section_list::const_iterator
7500 p = layout->section_list().begin();
7501 p != layout->section_list().end();
7502 ++p)
7503 {
7504 if (strcmp((*p)->name(), ".sdata") == 0)
7505 {
7506 section = *p;
7507 break;
7508 }
7509 }
7510 }
7511
7512 Sized_symbol<size>* gp =
7513 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
7514 if (gp != NULL)
7515 {
7516 if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
7517 gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
7518 elfcpp::STT_OBJECT,
7519 elfcpp::STB_GLOBAL,
7520 elfcpp::STV_DEFAULT, 0,
7521 false, false);
7522 this->gp_ = gp;
7523 }
7524 else if (section != NULL)
7525 {
7526 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
7527 "_gp", NULL, Symbol_table::PREDEFINED,
7528 section, MIPS_GP_OFFSET, 0,
7529 elfcpp::STT_OBJECT,
7530 elfcpp::STB_GLOBAL,
7531 elfcpp::STV_DEFAULT,
7532 0, false, false));
7533 this->gp_ = gp;
7534 }
7535}
7536
7537// Set the dynamic symbol indexes. INDEX is the index of the first
7538// global dynamic symbol. Pointers to the symbols are stored into the
7539// vector SYMS. The names are added to DYNPOOL. This returns an
7540// updated dynamic symbol index.
7541
7542template<int size, bool big_endian>
7543unsigned int
7544Target_mips<size, big_endian>::do_set_dynsym_indexes(
7545 std::vector<Symbol*>* dyn_symbols, unsigned int index,
7546 std::vector<Symbol*>* syms, Stringpool* dynpool,
7547 Versions* versions, Symbol_table* symtab) const
7548{
7549 std::vector<Symbol*> non_got_symbols;
7550 std::vector<Symbol*> got_symbols;
7551
7552 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
7553 &got_symbols);
7554
7555 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
7556 p != non_got_symbols.end();
7557 ++p)
7558 {
7559 Symbol* sym = *p;
7560
7561 // Note that SYM may already have a dynamic symbol index, since
7562 // some symbols appear more than once in the symbol table, with
7563 // and without a version.
7564
7565 if (!sym->has_dynsym_index())
7566 {
7567 sym->set_dynsym_index(index);
7568 ++index;
7569 syms->push_back(sym);
7570 dynpool->add(sym->name(), false, NULL);
7571
7572 // Record any version information.
7573 if (sym->version() != NULL)
7574 versions->record_version(symtab, dynpool, sym);
7575
7576 // If the symbol is defined in a dynamic object and is
7577 // referenced in a regular object, then mark the dynamic
7578 // object as needed. This is used to implement --as-needed.
7579 if (sym->is_from_dynobj() && sym->in_reg())
7580 sym->object()->set_is_needed();
7581 }
7582 }
7583
7584 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
7585 p != got_symbols.end();
7586 ++p)
7587 {
7588 Symbol* sym = *p;
7589 if (!sym->has_dynsym_index())
7590 {
7591 // Record any version information.
7592 if (sym->version() != NULL)
7593 versions->record_version(symtab, dynpool, sym);
7594 }
7595 }
7596
7597 index = versions->finalize(symtab, index, syms);
7598
7599 int got_sym_count = 0;
7600 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
7601 p != got_symbols.end();
7602 ++p)
7603 {
7604 Symbol* sym = *p;
7605
7606 if (!sym->has_dynsym_index())
7607 {
7608 ++got_sym_count;
7609 sym->set_dynsym_index(index);
7610 ++index;
7611 syms->push_back(sym);
7612 dynpool->add(sym->name(), false, NULL);
7613
7614 // If the symbol is defined in a dynamic object and is
7615 // referenced in a regular object, then mark the dynamic
7616 // object as needed. This is used to implement --as-needed.
7617 if (sym->is_from_dynobj() && sym->in_reg())
7618 sym->object()->set_is_needed();
7619 }
7620 }
7621
7622 // Set index of the first symbol that has .got entry.
7623 this->got_->set_first_global_got_dynsym_index(
7624 got_sym_count > 0 ? index - got_sym_count : -1U);
7625
7626 if (this->mips_stubs_ != NULL)
7627 this->mips_stubs_->set_dynsym_count(index);
7628
7629 return index;
7630}
7631
7632// Create a PLT entry for a global symbol referenced by r_type relocation.
7633
7634template<int size, bool big_endian>
7635void
7636Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7637 Layout* layout,
7638 Mips_symbol<size>* gsym,
7639 unsigned int r_type)
7640{
7641 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
7642 return;
7643
7644 if (this->plt_ == NULL)
7645 {
7646 // Create the GOT section first.
7647 this->got_section(symtab, layout);
7648
7649 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
7650 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
7651 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
7652 this->got_plt_, ORDER_DATA, false);
7653
7654 // The first two entries are reserved.
7655 this->got_plt_->set_current_data_size(2 * size/8);
7656
7657 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
7658 this->got_plt_,
7659 this);
7660 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7661 (elfcpp::SHF_ALLOC
7662 | elfcpp::SHF_EXECINSTR),
7663 this->plt_, ORDER_PLT, false);
7664 }
7665
7666 this->plt_->add_entry(gsym, r_type);
7667}
7668
7669
7670// Get the .MIPS.stubs section, creating it if necessary.
7671
7672template<int size, bool big_endian>
7673Mips_output_data_mips_stubs<size, big_endian>*
7674Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
7675{
7676 if (this->mips_stubs_ == NULL)
7677 {
7678 this->mips_stubs_ =
7679 new Mips_output_data_mips_stubs<size, big_endian>(this);
7680 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
7681 (elfcpp::SHF_ALLOC
7682 | elfcpp::SHF_EXECINSTR),
7683 this->mips_stubs_, ORDER_PLT, false);
7684 }
7685 return this->mips_stubs_;
7686}
7687
7688// Get the LA25 stub section, creating it if necessary.
7689
7690template<int size, bool big_endian>
7691Mips_output_data_la25_stub<size, big_endian>*
7692Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
7693{
7694 if (this->la25_stub_ == NULL)
7695 {
7696 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
7697 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7698 (elfcpp::SHF_ALLOC
7699 | elfcpp::SHF_EXECINSTR),
7700 this->la25_stub_, ORDER_TEXT, false);
7701 }
7702 return this->la25_stub_;
7703}
7704
7705// Process the relocations to determine unreferenced sections for
7706// garbage collection.
7707
7708template<int size, bool big_endian>
7709void
7710Target_mips<size, big_endian>::gc_process_relocs(
7711 Symbol_table* symtab,
7712 Layout* layout,
7713 Sized_relobj_file<size, big_endian>* object,
7714 unsigned int data_shndx,
7715 unsigned int,
7716 const unsigned char* prelocs,
7717 size_t reloc_count,
7718 Output_section* output_section,
7719 bool needs_special_offset_handling,
7720 size_t local_symbol_count,
7721 const unsigned char* plocal_symbols)
7722{
7723 typedef Target_mips<size, big_endian> Mips;
4d625b70
CC
7724 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
7725 Classify_reloc;
9810d34d 7726
4d625b70 7727 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
9810d34d
SS
7728 symtab,
7729 layout,
7730 this,
7731 object,
7732 data_shndx,
7733 prelocs,
7734 reloc_count,
7735 output_section,
7736 needs_special_offset_handling,
7737 local_symbol_count,
7738 plocal_symbols);
7739}
7740
7741// Scan relocations for a section.
7742
7743template<int size, bool big_endian>
7744void
7745Target_mips<size, big_endian>::scan_relocs(
7746 Symbol_table* symtab,
7747 Layout* layout,
7748 Sized_relobj_file<size, big_endian>* object,
7749 unsigned int data_shndx,
7750 unsigned int sh_type,
7751 const unsigned char* prelocs,
7752 size_t reloc_count,
7753 Output_section* output_section,
7754 bool needs_special_offset_handling,
7755 size_t local_symbol_count,
7756 const unsigned char* plocal_symbols)
7757{
7758 typedef Target_mips<size, big_endian> Mips;
9810d34d
SS
7759
7760 if (sh_type == elfcpp::SHT_REL)
4d625b70
CC
7761 {
7762 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
7763 Classify_reloc;
7764
7765 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
7766 symtab,
7767 layout,
7768 this,
7769 object,
7770 data_shndx,
7771 prelocs,
7772 reloc_count,
7773 output_section,
7774 needs_special_offset_handling,
7775 local_symbol_count,
7776 plocal_symbols);
7777 }
9810d34d 7778 else if (sh_type == elfcpp::SHT_RELA)
4d625b70
CC
7779 {
7780 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7781 Classify_reloc;
7782
7783 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
7784 symtab,
7785 layout,
7786 this,
7787 object,
7788 data_shndx,
7789 prelocs,
7790 reloc_count,
7791 output_section,
7792 needs_special_offset_handling,
7793 local_symbol_count,
7794 plocal_symbols);
7795 }
9810d34d
SS
7796}
7797
7798template<int size, bool big_endian>
7799bool
7800Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
7801{
7802 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
7803 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
7804 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
7805 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
7806 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
7807 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
7808 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
7809}
7810
7811// Return the MACH for a MIPS e_flags value.
7812template<int size, bool big_endian>
7813unsigned int
7814Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
7815{
7816 switch (flags & elfcpp::EF_MIPS_MACH)
7817 {
7818 case elfcpp::E_MIPS_MACH_3900:
7819 return mach_mips3900;
7820
7821 case elfcpp::E_MIPS_MACH_4010:
7822 return mach_mips4010;
7823
7824 case elfcpp::E_MIPS_MACH_4100:
7825 return mach_mips4100;
7826
7827 case elfcpp::E_MIPS_MACH_4111:
7828 return mach_mips4111;
7829
7830 case elfcpp::E_MIPS_MACH_4120:
7831 return mach_mips4120;
7832
7833 case elfcpp::E_MIPS_MACH_4650:
7834 return mach_mips4650;
7835
7836 case elfcpp::E_MIPS_MACH_5400:
7837 return mach_mips5400;
7838
7839 case elfcpp::E_MIPS_MACH_5500:
7840 return mach_mips5500;
7841
7842 case elfcpp::E_MIPS_MACH_9000:
7843 return mach_mips9000;
7844
7845 case elfcpp::E_MIPS_MACH_SB1:
7846 return mach_mips_sb1;
7847
7848 case elfcpp::E_MIPS_MACH_LS2E:
7849 return mach_mips_loongson_2e;
7850
7851 case elfcpp::E_MIPS_MACH_LS2F:
7852 return mach_mips_loongson_2f;
7853
7854 case elfcpp::E_MIPS_MACH_LS3A:
7855 return mach_mips_loongson_3a;
7856
7857 case elfcpp::E_MIPS_MACH_OCTEON2:
7858 return mach_mips_octeon2;
7859
7860 case elfcpp::E_MIPS_MACH_OCTEON:
7861 return mach_mips_octeon;
7862
7863 case elfcpp::E_MIPS_MACH_XLR:
7864 return mach_mips_xlr;
7865
7866 default:
7867 switch (flags & elfcpp::EF_MIPS_ARCH)
7868 {
7869 default:
7870 case elfcpp::E_MIPS_ARCH_1:
7871 return mach_mips3000;
7872
7873 case elfcpp::E_MIPS_ARCH_2:
7874 return mach_mips6000;
7875
7876 case elfcpp::E_MIPS_ARCH_3:
7877 return mach_mips4000;
7878
7879 case elfcpp::E_MIPS_ARCH_4:
7880 return mach_mips8000;
7881
7882 case elfcpp::E_MIPS_ARCH_5:
7883 return mach_mips5;
7884
7885 case elfcpp::E_MIPS_ARCH_32:
7886 return mach_mipsisa32;
7887
7888 case elfcpp::E_MIPS_ARCH_64:
7889 return mach_mipsisa64;
7890
7891 case elfcpp::E_MIPS_ARCH_32R2:
7892 return mach_mipsisa32r2;
7893
7894 case elfcpp::E_MIPS_ARCH_64R2:
7895 return mach_mipsisa64r2;
7896 }
7897 }
7898
7899 return 0;
7900}
7901
7902// Check whether machine EXTENSION is an extension of machine BASE.
7903template<int size, bool big_endian>
7904bool
7905Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
7906 unsigned int extension)
7907{
7908 if (extension == base)
7909 return true;
7910
7911 if ((base == mach_mipsisa32)
7912 && this->mips_mach_extends(mach_mipsisa64, extension))
7913 return true;
7914
7915 if ((base == mach_mipsisa32r2)
7916 && this->mips_mach_extends(mach_mipsisa64r2, extension))
7917 return true;
7918
7919 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
7920 if (extension == this->mips_mach_extensions_[i].first)
7921 {
7922 extension = this->mips_mach_extensions_[i].second;
7923 if (extension == base)
7924 return true;
7925 }
7926
7927 return false;
7928}
7929
7930template<int size, bool big_endian>
7931void
7932Target_mips<size, big_endian>::merge_processor_specific_flags(
7933 const std::string& name, elfcpp::Elf_Word in_flags,
7934 unsigned char in_ei_class, bool dyn_obj)
7935{
7936 // If flags are not set yet, just copy them.
7937 if (!this->are_processor_specific_flags_set())
7938 {
7939 this->set_processor_specific_flags(in_flags);
7940 this->ei_class_ = in_ei_class;
7941 this->mach_ = this->elf_mips_mach(in_flags);
7942 return;
7943 }
7944
7945 elfcpp::Elf_Word new_flags = in_flags;
7946 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
7947 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
7948 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
7949
7950 // Check flag compatibility.
7951 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
7952 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
7953
7954 // Some IRIX 6 BSD-compatibility objects have this bit set. It
7955 // doesn't seem to matter.
7956 new_flags &= ~elfcpp::EF_MIPS_XGOT;
7957 old_flags &= ~elfcpp::EF_MIPS_XGOT;
7958
7959 // MIPSpro generates ucode info in n64 objects. Again, we should
7960 // just be able to ignore this.
7961 new_flags &= ~elfcpp::EF_MIPS_UCODE;
7962 old_flags &= ~elfcpp::EF_MIPS_UCODE;
7963
7964 // DSOs should only be linked with CPIC code.
7965 if (dyn_obj)
7966 new_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
7967
7968 if (new_flags == old_flags)
7969 {
7970 this->set_processor_specific_flags(merged_flags);
7971 return;
7972 }
7973
7974 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
7975 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
7976 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
7977 name.c_str());
7978
7979 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
7980 merged_flags |= elfcpp::EF_MIPS_CPIC;
7981 if (!(new_flags & elfcpp::EF_MIPS_PIC))
7982 merged_flags &= ~elfcpp::EF_MIPS_PIC;
7983
7984 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
7985 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
7986
7987 // Compare the ISAs.
7988 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
7989 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
7990 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
7991 {
7992 // Output ISA isn't the same as, or an extension of, input ISA.
7993 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
7994 {
7995 // Copy the architecture info from input object to output. Also copy
7996 // the 32-bit flag (if set) so that we continue to recognise
7997 // output as a 32-bit binary.
7998 this->mach_ = this->elf_mips_mach(in_flags);
7999 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
8000 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
8001 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
8002
8003 // Copy across the ABI flags if output doesn't use them
8004 // and if that was what caused us to treat input object as 32-bit.
8005 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
8006 && this->mips_32bit_flags(new_flags)
8007 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
8008 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
8009 }
8010 else
8011 // The ISAs aren't compatible.
8012 gold_error(_("%s: linking %s module with previous %s modules"),
8013 name.c_str(), this->elf_mips_mach_name(in_flags),
8014 this->elf_mips_mach_name(merged_flags));
8015 }
8016
8017 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
8018 | elfcpp::EF_MIPS_32BITMODE));
8019 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
8020 | elfcpp::EF_MIPS_32BITMODE));
8021
8022 // Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it does set
8023 // EI_CLASS differently from any 32-bit ABI.
8024 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI)
8025 || (in_ei_class != this->ei_class_))
8026 {
8027 // Only error if both are set (to different values).
8028 if (((new_flags & elfcpp::EF_MIPS_ABI)
8029 && (old_flags & elfcpp::EF_MIPS_ABI))
8030 || (in_ei_class != this->ei_class_))
8031 gold_error(_("%s: ABI mismatch: linking %s module with "
8032 "previous %s modules"), name.c_str(),
8033 this->elf_mips_abi_name(in_flags, in_ei_class),
8034 this->elf_mips_abi_name(merged_flags, this->ei_class_));
8035
8036 new_flags &= ~elfcpp::EF_MIPS_ABI;
8037 old_flags &= ~elfcpp::EF_MIPS_ABI;
8038 }
8039
8040 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
8041 // and allow arbitrary mixing of the remaining ASEs (retain the union).
8042 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
8043 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
8044 {
8045 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
8046 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
8047 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
8048 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
8049 int micro_mis = old_m16 && new_micro;
8050 int m16_mis = old_micro && new_m16;
8051
8052 if (m16_mis || micro_mis)
8053 gold_error(_("%s: ASE mismatch: linking %s module with "
8054 "previous %s modules"), name.c_str(),
8055 m16_mis ? "MIPS16" : "microMIPS",
8056 m16_mis ? "microMIPS" : "MIPS16");
8057
8058 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
8059
8060 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
8061 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
8062 }
8063
8064 // Warn about any other mismatches.
8065 if (new_flags != old_flags)
8066 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
8067 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
8068
8069 this->set_processor_specific_flags(merged_flags);
8070}
8071
8072// Adjust ELF file header.
8073
8074template<int size, bool big_endian>
8075void
8076Target_mips<size, big_endian>::do_adjust_elf_header(
8077 unsigned char* view,
8078 int len)
8079{
8080 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
8081
8082 elfcpp::Ehdr<size, big_endian> ehdr(view);
8083 unsigned char e_ident[elfcpp::EI_NIDENT];
8084 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
8085
8086 e_ident[elfcpp::EI_CLASS] = this->ei_class_;
8087
8088 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
8089 oehdr.put_e_ident(e_ident);
8090 if (this->entry_symbol_is_compressed_)
8091 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
8092}
8093
8094// do_make_elf_object to override the same function in the base class.
8095// We need to use a target-specific sub-class of
8096// Sized_relobj_file<size, big_endian> to store Mips specific information.
8097// Hence we need to have our own ELF object creation.
8098
8099template<int size, bool big_endian>
8100Object*
8101Target_mips<size, big_endian>::do_make_elf_object(
8102 const std::string& name,
8103 Input_file* input_file,
8104 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
8105{
8106 int et = ehdr.get_e_type();
8107 // ET_EXEC files are valid input for --just-symbols/-R,
8108 // and we treat them as relocatable objects.
8109 if (et == elfcpp::ET_REL
8110 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
8111 {
8112 Mips_relobj<size, big_endian>* obj =
8113 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
8114 obj->setup();
8115 return obj;
8116 }
8117 else if (et == elfcpp::ET_DYN)
8118 {
8119 // TODO(sasa): Should we create Mips_dynobj?
8120 return Target::do_make_elf_object(name, input_file, offset, ehdr);
8121 }
8122 else
8123 {
8124 gold_error(_("%s: unsupported ELF file type %d"),
8125 name.c_str(), et);
8126 return NULL;
8127 }
8128}
8129
8130// Finalize the sections.
8131
8132template <int size, bool big_endian>
8133void
8134Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
8135 const Input_objects* input_objects,
8136 Symbol_table* symtab)
8137{
8138 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
8139 // DT_FINI have correct values.
8140 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
8141 symtab->lookup(parameters->options().init()));
8142 if (init != NULL && (init->is_mips16() || init->is_micromips()))
8143 init->set_value(init->value() | 1);
8144 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
8145 symtab->lookup(parameters->options().fini()));
8146 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
8147 fini->set_value(fini->value() | 1);
8148
8149 // Check whether the entry symbol is mips16 or micromips. This is needed to
8150 // adjust entry address in ELF header.
8151 Mips_symbol<size>* entry =
8152 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
8153 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
8154 || entry->is_micromips()));
8155
8156 if (!parameters->doing_static_link()
8157 && (strcmp(parameters->options().hash_style(), "gnu") == 0
8158 || strcmp(parameters->options().hash_style(), "both") == 0))
8159 {
8160 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
8161 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
8162 // MIPS ABI requires a mapping between the GOT and the symbol table.
8163 gold_error(".gnu.hash is incompatible with the MIPS ABI");
8164 }
8165
8166 // Check whether the final section that was scanned has HI16 or GOT16
8167 // relocations without the corresponding LO16 part.
8168 if (this->got16_addends_.size() > 0)
8169 gold_error("Can't find matching LO16 reloc");
8170
8171 // Set _gp value.
8172 this->set_gp(layout, symtab);
8173
8174 // Check for any mips16 stub sections that we can discard.
8175 if (!parameters->options().relocatable())
8176 {
8177 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8178 p != input_objects->relobj_end();
8179 ++p)
8180 {
8181 Mips_relobj<size, big_endian>* object =
8182 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8183 object->discard_mips16_stub_sections(symtab);
8184 }
8185 }
8186
8187 // Merge processor-specific flags.
8188 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8189 p != input_objects->relobj_end();
8190 ++p)
8191 {
8192 Mips_relobj<size, big_endian>* relobj =
8193 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8194
8195 Input_file::Format format = relobj->input_file()->format();
8196 if (format == Input_file::FORMAT_ELF)
8197 {
8198 // Read processor-specific flags in ELF file header.
8199 const unsigned char* pehdr = relobj->get_view(
8200 elfcpp::file_header_offset,
8201 elfcpp::Elf_sizes<size>::ehdr_size,
8202 true, false);
8203
8204 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
8205 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
8206 unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS];
8207
8208 this->merge_processor_specific_flags(relobj->name(), in_flags,
8209 ei_class, false);
8210 }
8211 }
8212
8213 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8214 p != input_objects->dynobj_end();
8215 ++p)
8216 {
8217 Sized_dynobj<size, big_endian>* dynobj =
8218 static_cast<Sized_dynobj<size, big_endian>*>(*p);
8219
8220 // Read processor-specific flags.
8221 const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
8222 elfcpp::Elf_sizes<size>::ehdr_size,
8223 true, false);
8224
8225 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
8226 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
8227 unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS];
8228
8229 this->merge_processor_specific_flags(dynobj->name(), in_flags, ei_class,
8230 true);
8231 }
8232
8233 // Merge .reginfo contents of input objects.
8234 Valtype gprmask = 0;
8235 Valtype cprmask1 = 0;
8236 Valtype cprmask2 = 0;
8237 Valtype cprmask3 = 0;
8238 Valtype cprmask4 = 0;
8239 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8240 p != input_objects->relobj_end();
8241 ++p)
8242 {
8243 Mips_relobj<size, big_endian>* relobj =
8244 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8245
8246 gprmask |= relobj->gprmask();
8247 cprmask1 |= relobj->cprmask1();
8248 cprmask2 |= relobj->cprmask2();
8249 cprmask3 |= relobj->cprmask3();
8250 cprmask4 |= relobj->cprmask4();
8251 }
8252
8253 if (this->plt_ != NULL)
8254 {
8255 // Set final PLT offsets for symbols.
8256 this->plt_section()->set_plt_offsets();
8257
8258 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
8259 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
8260 // there are no standard PLT entries present.
8261 unsigned char nonvis = 0;
8262 if (this->is_output_micromips()
8263 && !this->plt_section()->has_standard_entries())
8264 nonvis = elfcpp::STO_MICROMIPS >> 2;
8265 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
8266 Symbol_table::PREDEFINED,
8267 this->plt_,
8268 0, 0, elfcpp::STT_FUNC,
8269 elfcpp::STB_LOCAL,
8270 elfcpp::STV_DEFAULT, nonvis,
8271 false, false);
8272 }
8273
8274 if (this->mips_stubs_ != NULL)
8275 {
8276 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
8277 unsigned char nonvis = 0;
8278 if (this->is_output_micromips())
8279 nonvis = elfcpp::STO_MICROMIPS >> 2;
8280 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
8281 Symbol_table::PREDEFINED,
8282 this->mips_stubs_,
8283 0, 0, elfcpp::STT_FUNC,
8284 elfcpp::STB_LOCAL,
8285 elfcpp::STV_DEFAULT, nonvis,
8286 false, false);
8287 }
8288
8289 if (!parameters->options().relocatable() && !parameters->doing_static_link())
8290 // In case there is no .got section, create one.
8291 this->got_section(symtab, layout);
8292
8293 // Emit any relocs we saved in an attempt to avoid generating COPY
8294 // relocs.
8295 if (this->copy_relocs_.any_saved_relocs())
8296 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
8297 this);
8298
8299 // Emit dynamic relocs.
8300 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
8301 p != this->dyn_relocs_.end();
8302 ++p)
8303 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
8304
8305 if (this->has_got_section())
8306 this->got_section()->lay_out_got(layout, symtab, input_objects);
8307
8308 if (this->mips_stubs_ != NULL)
8309 this->mips_stubs_->set_needs_dynsym_value();
8310
8311 // Check for functions that might need $25 to be valid on entry.
8312 // TODO(sasa): Can we do this without iterating over all symbols?
8313 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
8314 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
8315 symtab));
8316
8317 // Add NULL segment.
8318 if (!parameters->options().relocatable())
8319 layout->make_output_segment(elfcpp::PT_NULL, 0);
8320
8321 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
8322 p != layout->section_list().end();
8323 ++p)
8324 {
8325 if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO)
8326 {
8327 Mips_output_section_reginfo<size, big_endian>* reginfo =
8328 Mips_output_section_reginfo<size, big_endian>::
8329 as_mips_output_section_reginfo(*p);
8330
8331 reginfo->set_masks(gprmask, cprmask1, cprmask2, cprmask3, cprmask4);
8332
8333 if (!parameters->options().relocatable())
8334 {
8335 Output_segment* reginfo_segment =
8336 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
8337 elfcpp::PF_R);
8338 reginfo_segment->add_output_section_to_nonload(reginfo,
8339 elfcpp::PF_R);
8340 }
8341 }
8342 }
8343
8344 // Fill in some more dynamic tags.
8345 // TODO(sasa): Add more dynamic tags.
8346 const Reloc_section* rel_plt = (this->plt_ == NULL
8347 ? NULL : this->plt_->rel_plt());
8348 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
8349 this->rel_dyn_, true, false);
8350
8351 Output_data_dynamic* const odyn = layout->dynamic_data();
8352 if (odyn != NULL
8353 && !parameters->options().relocatable()
8354 && !parameters->doing_static_link())
8355 {
8356 unsigned int d_val;
8357 // This element holds a 32-bit version id for the Runtime
8358 // Linker Interface. This will start at integer value 1.
8359 d_val = 0x01;
8360 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
8361
8362 // Dynamic flags
8363 d_val = elfcpp::RHF_NOTPOT;
8364 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
8365
8366 // Save layout for using when emiting custom dynamic tags.
8367 this->layout_ = layout;
8368
8369 // This member holds the base address of the segment.
8370 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
8371
8372 // This member holds the number of entries in the .dynsym section.
8373 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
8374
8375 // This member holds the index of the first dynamic symbol
8376 // table entry that corresponds to an entry in the global offset table.
8377 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
8378
8379 // This member holds the number of local GOT entries.
8380 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
8381 this->got_->get_local_gotno());
8382
8383 if (this->plt_ != NULL)
8384 // DT_MIPS_PLTGOT dynamic tag
8385 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
8386 }
8387 }
8388
8389// Get the custom dynamic tag value.
8390template<int size, bool big_endian>
8391unsigned int
8392Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
8393{
8394 switch (tag)
8395 {
8396 case elfcpp::DT_MIPS_BASE_ADDRESS:
8397 {
8398 // The base address of the segment.
8399 // At this point, the segment list has been sorted into final order,
8400 // so just return vaddr of the first readable PT_LOAD segment.
8401 Output_segment* seg =
8402 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
8403 gold_assert(seg != NULL);
8404 return seg->vaddr();
8405 }
8406
8407 case elfcpp::DT_MIPS_SYMTABNO:
8408 // The number of entries in the .dynsym section.
8409 return this->get_dt_mips_symtabno();
8410
8411 case elfcpp::DT_MIPS_GOTSYM:
8412 {
8413 // The index of the first dynamic symbol table entry that corresponds
8414 // to an entry in the GOT.
8415 if (this->got_->first_global_got_dynsym_index() != -1U)
8416 return this->got_->first_global_got_dynsym_index();
8417 else
8418 // In case if we don't have global GOT symbols we default to setting
8419 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
8420 return this->get_dt_mips_symtabno();
8421 }
8422
8423 default:
8424 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
8425 }
8426
8427 return (unsigned int)-1;
8428}
8429
8430// Relocate section data.
8431
8432template<int size, bool big_endian>
8433void
8434Target_mips<size, big_endian>::relocate_section(
8435 const Relocate_info<size, big_endian>* relinfo,
8436 unsigned int sh_type,
8437 const unsigned char* prelocs,
8438 size_t reloc_count,
8439 Output_section* output_section,
8440 bool needs_special_offset_handling,
8441 unsigned char* view,
8442 Mips_address address,
8443 section_size_type view_size,
8444 const Reloc_symbol_changes* reloc_symbol_changes)
8445{
8446 typedef Target_mips<size, big_endian> Mips;
8447 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
8448
8449 if (sh_type == elfcpp::SHT_REL)
4d625b70
CC
8450 {
8451 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8452 Classify_reloc;
8453
8454 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
8455 gold::Default_comdat_behavior, Classify_reloc>(
8456 relinfo,
8457 this,
8458 prelocs,
8459 reloc_count,
8460 output_section,
8461 needs_special_offset_handling,
8462 view,
8463 address,
8464 view_size,
8465 reloc_symbol_changes);
8466 }
9810d34d 8467 else if (sh_type == elfcpp::SHT_RELA)
4d625b70
CC
8468 {
8469 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8470 Classify_reloc;
8471
8472 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
8473 gold::Default_comdat_behavior, Classify_reloc>(
8474 relinfo,
8475 this,
8476 prelocs,
8477 reloc_count,
8478 output_section,
8479 needs_special_offset_handling,
8480 view,
8481 address,
8482 view_size,
8483 reloc_symbol_changes);
8484 }
9810d34d
SS
8485}
8486
8487// Return the size of a relocation while scanning during a relocatable
8488// link.
8489
9810d34d 8490unsigned int
4d625b70 8491mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
9810d34d
SS
8492{
8493 switch (r_type)
8494 {
8495 case elfcpp::R_MIPS_NONE:
8496 case elfcpp::R_MIPS_TLS_DTPMOD64:
8497 case elfcpp::R_MIPS_TLS_DTPREL64:
8498 case elfcpp::R_MIPS_TLS_TPREL64:
8499 return 0;
8500
8501 case elfcpp::R_MIPS_32:
8502 case elfcpp::R_MIPS_TLS_DTPMOD32:
8503 case elfcpp::R_MIPS_TLS_DTPREL32:
8504 case elfcpp::R_MIPS_TLS_TPREL32:
8505 case elfcpp::R_MIPS_REL32:
8506 case elfcpp::R_MIPS_PC32:
8507 case elfcpp::R_MIPS_GPREL32:
8508 case elfcpp::R_MIPS_JALR:
8509 return 4;
8510
8511 case elfcpp::R_MIPS_16:
8512 case elfcpp::R_MIPS_HI16:
8513 case elfcpp::R_MIPS_LO16:
8514 case elfcpp::R_MIPS_GPREL16:
8515 case elfcpp::R_MIPS16_HI16:
8516 case elfcpp::R_MIPS16_LO16:
8517 case elfcpp::R_MIPS_PC16:
8518 case elfcpp::R_MIPS_GOT16:
8519 case elfcpp::R_MIPS16_GOT16:
8520 case elfcpp::R_MIPS_CALL16:
8521 case elfcpp::R_MIPS16_CALL16:
8522 case elfcpp::R_MIPS_GOT_HI16:
8523 case elfcpp::R_MIPS_CALL_HI16:
8524 case elfcpp::R_MIPS_GOT_LO16:
8525 case elfcpp::R_MIPS_CALL_LO16:
8526 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
8527 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
8528 case elfcpp::R_MIPS_TLS_TPREL_HI16:
8529 case elfcpp::R_MIPS_TLS_TPREL_LO16:
8530 case elfcpp::R_MIPS16_GPREL:
8531 case elfcpp::R_MIPS_GOT_DISP:
8532 case elfcpp::R_MIPS_LITERAL:
8533 case elfcpp::R_MIPS_GOT_PAGE:
8534 case elfcpp::R_MIPS_GOT_OFST:
8535 case elfcpp::R_MIPS_TLS_GD:
8536 case elfcpp::R_MIPS_TLS_LDM:
8537 case elfcpp::R_MIPS_TLS_GOTTPREL:
8538 return 2;
8539
8540 // These relocations are not byte sized
8541 case elfcpp::R_MIPS_26:
8542 case elfcpp::R_MIPS16_26:
8543 return 4;
8544
8545 case elfcpp::R_MIPS_COPY:
8546 case elfcpp::R_MIPS_JUMP_SLOT:
8547 object->error(_("unexpected reloc %u in object file"), r_type);
8548 return 0;
8549
8550 default:
8551 object->error(_("unsupported reloc %u in object file"), r_type);
8552 return 0;
8553 }
8554}
8555
8556// Scan the relocs during a relocatable link.
8557
8558template<int size, bool big_endian>
8559void
8560Target_mips<size, big_endian>::scan_relocatable_relocs(
8561 Symbol_table* symtab,
8562 Layout* layout,
8563 Sized_relobj_file<size, big_endian>* object,
8564 unsigned int data_shndx,
8565 unsigned int sh_type,
8566 const unsigned char* prelocs,
8567 size_t reloc_count,
8568 Output_section* output_section,
8569 bool needs_special_offset_handling,
8570 size_t local_symbol_count,
8571 const unsigned char* plocal_symbols,
8572 Relocatable_relocs* rr)
8573{
4d625b70
CC
8574 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8575 Classify_reloc;
8576 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
8577 Scan_relocatable_relocs;
9810d34d 8578
4d625b70 8579 gold_assert(sh_type == elfcpp::SHT_REL);
9810d34d 8580
4d625b70 8581 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
9810d34d
SS
8582 symtab,
8583 layout,
8584 object,
8585 data_shndx,
8586 prelocs,
8587 reloc_count,
8588 output_section,
8589 needs_special_offset_handling,
8590 local_symbol_count,
8591 plocal_symbols,
8592 rr);
8593}
8594
4d625b70
CC
8595// Scan the relocs for --emit-relocs.
8596
8597template<int size, bool big_endian>
8598void
8599Target_mips<size, big_endian>::emit_relocs_scan(
8600 Symbol_table* symtab,
8601 Layout* layout,
8602 Sized_relobj_file<size, big_endian>* object,
8603 unsigned int data_shndx,
8604 unsigned int sh_type,
8605 const unsigned char* prelocs,
8606 size_t reloc_count,
8607 Output_section* output_section,
8608 bool needs_special_offset_handling,
8609 size_t local_symbol_count,
8610 const unsigned char* plocal_syms,
8611 Relocatable_relocs* rr)
8612{
8613 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8614 Classify_reloc;
8615 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
8616 Emit_relocs_strategy;
8617
8618 gold_assert(sh_type == elfcpp::SHT_REL);
8619
8620 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
8621 symtab,
8622 layout,
8623 object,
8624 data_shndx,
8625 prelocs,
8626 reloc_count,
8627 output_section,
8628 needs_special_offset_handling,
8629 local_symbol_count,
8630 plocal_syms,
8631 rr);
8632}
8633
9810d34d
SS
8634// Emit relocations for a section.
8635
8636template<int size, bool big_endian>
8637void
8638Target_mips<size, big_endian>::relocate_relocs(
8639 const Relocate_info<size, big_endian>* relinfo,
8640 unsigned int sh_type,
8641 const unsigned char* prelocs,
8642 size_t reloc_count,
8643 Output_section* output_section,
8644 typename elfcpp::Elf_types<size>::Elf_Off
8645 offset_in_output_section,
9810d34d
SS
8646 unsigned char* view,
8647 Mips_address view_address,
8648 section_size_type view_size,
8649 unsigned char* reloc_view,
8650 section_size_type reloc_view_size)
8651{
4d625b70
CC
8652 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8653 Classify_reloc;
8654
9810d34d
SS
8655 gold_assert(sh_type == elfcpp::SHT_REL);
8656
4d625b70 8657 gold::relocate_relocs<size, big_endian, Classify_reloc>(
9810d34d
SS
8658 relinfo,
8659 prelocs,
8660 reloc_count,
8661 output_section,
8662 offset_in_output_section,
9810d34d
SS
8663 view,
8664 view_address,
8665 view_size,
8666 reloc_view,
8667 reloc_view_size);
8668}
8669
8670// Perform target-specific processing in a relocatable link. This is
8671// only used if we use the relocation strategy RELOC_SPECIAL.
8672
8673template<int size, bool big_endian>
8674void
8675Target_mips<size, big_endian>::relocate_special_relocatable(
8676 const Relocate_info<size, big_endian>* relinfo,
8677 unsigned int sh_type,
8678 const unsigned char* preloc_in,
8679 size_t relnum,
8680 Output_section* output_section,
8681 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
8682 unsigned char* view,
8683 Mips_address view_address,
8684 section_size_type,
8685 unsigned char* preloc_out)
8686{
8687 // We can only handle REL type relocation sections.
8688 gold_assert(sh_type == elfcpp::SHT_REL);
8689
8690 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
8691 Reltype;
8692 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
8693 Reltype_write;
8694
8695 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
8696
8697 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
8698
8699 Mips_relobj<size, big_endian>* object =
8700 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
8701 const unsigned int local_count = object->local_symbol_count();
8702
8703 Reltype reloc(preloc_in);
8704 Reltype_write reloc_write(preloc_out);
8705
8706 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
8707 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
8708 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
8709
8710 // Get the new symbol index.
8711 // We only use RELOC_SPECIAL strategy in local relocations.
8712 gold_assert(r_sym < local_count);
8713
8714 // We are adjusting a section symbol. We need to find
8715 // the symbol table index of the section symbol for
8716 // the output section corresponding to input section
8717 // in which this symbol is defined.
8718 bool is_ordinary;
8719 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
8720 gold_assert(is_ordinary);
8721 Output_section* os = object->output_section(shndx);
8722 gold_assert(os != NULL);
8723 gold_assert(os->needs_symtab_index());
8724 unsigned int new_symndx = os->symtab_index();
8725
8726 // Get the new offset--the location in the output section where
8727 // this relocation should be applied.
8728
8729 Mips_address offset = reloc.get_r_offset();
8730 Mips_address new_offset;
8731 if (offset_in_output_section != invalid_address)
8732 new_offset = offset + offset_in_output_section;
8733 else
8734 {
8735 section_offset_type sot_offset =
8736 convert_types<section_offset_type, Mips_address>(offset);
8737 section_offset_type new_sot_offset =
8738 output_section->output_offset(object, relinfo->data_shndx,
8739 sot_offset);
8740 gold_assert(new_sot_offset != -1);
8741 new_offset = new_sot_offset;
8742 }
8743
8744 // In an object file, r_offset is an offset within the section.
8745 // In an executable or dynamic object, generated by
8746 // --emit-relocs, r_offset is an absolute address.
8747 if (!parameters->options().relocatable())
8748 {
8749 new_offset += view_address;
8750 if (offset_in_output_section != invalid_address)
8751 new_offset -= offset_in_output_section;
8752 }
8753
8754 reloc_write.put_r_offset(new_offset);
8755 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
8756
8757 // Handle the reloc addend.
8758 // The relocation uses a section symbol in the input file.
8759 // We are adjusting it to use a section symbol in the output
8760 // file. The input section symbol refers to some address in
8761 // the input section. We need the relocation in the output
8762 // file to refer to that same address. This adjustment to
8763 // the addend is the same calculation we use for a simple
8764 // absolute relocation for the input section symbol.
8765
8766 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
8767
8768 unsigned char* paddend = view + offset;
8769 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
8770 switch (r_type)
8771 {
8772 case elfcpp::R_MIPS_26:
8773 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
8774 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
8775 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal());
8776 break;
8777
8778 default:
8779 gold_unreachable();
8780 }
8781
8782 // Report any errors.
8783 switch (reloc_status)
8784 {
8785 case Reloc_funcs::STATUS_OKAY:
8786 break;
8787 case Reloc_funcs::STATUS_OVERFLOW:
8788 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
8789 _("relocation overflow"));
8790 break;
8791 case Reloc_funcs::STATUS_BAD_RELOC:
8792 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
8793 _("unexpected opcode while processing relocation"));
8794 break;
8795 default:
8796 gold_unreachable();
8797 }
8798}
8799
8800// Optimize the TLS relocation type based on what we know about the
8801// symbol. IS_FINAL is true if the final address of this symbol is
8802// known at link time.
8803
8804template<int size, bool big_endian>
8805tls::Tls_optimization
8806Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
8807{
8808 // FIXME: Currently we do not do any TLS optimization.
8809 return tls::TLSOPT_NONE;
8810}
8811
8812// Scan a relocation for a local symbol.
8813
8814template<int size, bool big_endian>
8815inline void
8816Target_mips<size, big_endian>::Scan::local(
8817 Symbol_table* symtab,
8818 Layout* layout,
8819 Target_mips<size, big_endian>* target,
8820 Sized_relobj_file<size, big_endian>* object,
8821 unsigned int data_shndx,
8822 Output_section* output_section,
4d625b70
CC
8823 const Relatype* rela,
8824 const Reltype* rel,
9810d34d
SS
8825 unsigned int rel_type,
8826 unsigned int r_type,
8827 const elfcpp::Sym<size, big_endian>& lsym,
8828 bool is_discarded)
8829{
8830 if (is_discarded)
8831 return;
8832
8833 Mips_address r_offset;
4d625b70 8834 unsigned int r_sym;
9810d34d
SS
8835 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
8836
8837 if (rel_type == elfcpp::SHT_RELA)
8838 {
8839 r_offset = rela->get_r_offset();
4d625b70
CC
8840 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
8841 get_r_sym(rela);
9810d34d
SS
8842 r_addend = rela->get_r_addend();
8843 }
8844 else
8845 {
8846 r_offset = rel->get_r_offset();
4d625b70
CC
8847 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
8848 get_r_sym(rel);
9810d34d
SS
8849 r_addend = 0;
8850 }
8851
9810d34d
SS
8852 Mips_relobj<size, big_endian>* mips_obj =
8853 Mips_relobj<size, big_endian>::as_mips_relobj(object);
8854
8855 if (mips_obj->is_mips16_stub_section(data_shndx))
8856 {
8857 mips_obj->get_mips16_stub_section(data_shndx)
8858 ->new_local_reloc_found(r_type, r_sym);
8859 }
8860
8861 if (r_type == elfcpp::R_MIPS_NONE)
8862 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
8863 // mips16 stub.
8864 return;
8865
8866 if (!mips16_call_reloc(r_type)
8867 && !mips_obj->section_allows_mips16_refs(data_shndx))
8868 // This reloc would need to refer to a MIPS16 hard-float stub, if
8869 // there is one. We ignore MIPS16 stub sections and .pdr section when
8870 // looking for relocs that would need to refer to MIPS16 stubs.
8871 mips_obj->add_local_non_16bit_call(r_sym);
8872
8873 if (r_type == elfcpp::R_MIPS16_26
8874 && !mips_obj->section_allows_mips16_refs(data_shndx))
8875 mips_obj->add_local_16bit_call(r_sym);
8876
8877 switch (r_type)
8878 {
8879 case elfcpp::R_MIPS_GOT16:
8880 case elfcpp::R_MIPS_CALL16:
8881 case elfcpp::R_MIPS_CALL_HI16:
8882 case elfcpp::R_MIPS_CALL_LO16:
8883 case elfcpp::R_MIPS_GOT_HI16:
8884 case elfcpp::R_MIPS_GOT_LO16:
8885 case elfcpp::R_MIPS_GOT_PAGE:
8886 case elfcpp::R_MIPS_GOT_OFST:
8887 case elfcpp::R_MIPS_GOT_DISP:
8888 case elfcpp::R_MIPS_TLS_GOTTPREL:
8889 case elfcpp::R_MIPS_TLS_GD:
8890 case elfcpp::R_MIPS_TLS_LDM:
8891 case elfcpp::R_MIPS16_GOT16:
8892 case elfcpp::R_MIPS16_CALL16:
8893 case elfcpp::R_MIPS16_TLS_GOTTPREL:
8894 case elfcpp::R_MIPS16_TLS_GD:
8895 case elfcpp::R_MIPS16_TLS_LDM:
8896 case elfcpp::R_MICROMIPS_GOT16:
8897 case elfcpp::R_MICROMIPS_CALL16:
8898 case elfcpp::R_MICROMIPS_CALL_HI16:
8899 case elfcpp::R_MICROMIPS_CALL_LO16:
8900 case elfcpp::R_MICROMIPS_GOT_HI16:
8901 case elfcpp::R_MICROMIPS_GOT_LO16:
8902 case elfcpp::R_MICROMIPS_GOT_PAGE:
8903 case elfcpp::R_MICROMIPS_GOT_OFST:
8904 case elfcpp::R_MICROMIPS_GOT_DISP:
8905 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
8906 case elfcpp::R_MICROMIPS_TLS_GD:
8907 case elfcpp::R_MICROMIPS_TLS_LDM:
8908 // We need a GOT section.
8909 target->got_section(symtab, layout);
8910 break;
8911
8912 default:
8913 break;
8914 }
8915
8916 if (call_lo16_reloc(r_type)
8917 || got_lo16_reloc(r_type)
8918 || got_disp_reloc(r_type))
8919 {
8920 // We may need a local GOT entry for this relocation. We
8921 // don't count R_MIPS_GOT_PAGE because we can estimate the
8922 // maximum number of pages needed by looking at the size of
8923 // the segment. Similar comments apply to R_MIPS*_GOT16 and
8924 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
8925 // R_MIPS_CALL_HI16 because these are always followed by an
8926 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
8927 Mips_output_data_got<size, big_endian>* got =
8928 target->got_section(symtab, layout);
9810d34d
SS
8929 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U);
8930 }
8931
8932 switch (r_type)
8933 {
8934 case elfcpp::R_MIPS_CALL16:
8935 case elfcpp::R_MIPS16_CALL16:
8936 case elfcpp::R_MICROMIPS_CALL16:
8937 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
8938 (unsigned long)r_offset);
8939 return;
8940
8941 case elfcpp::R_MIPS_GOT_PAGE:
8942 case elfcpp::R_MICROMIPS_GOT_PAGE:
8943 case elfcpp::R_MIPS16_GOT16:
8944 case elfcpp::R_MIPS_GOT16:
8945 case elfcpp::R_MIPS_GOT_HI16:
8946 case elfcpp::R_MIPS_GOT_LO16:
8947 case elfcpp::R_MICROMIPS_GOT16:
8948 case elfcpp::R_MICROMIPS_GOT_HI16:
8949 case elfcpp::R_MICROMIPS_GOT_LO16:
8950 {
8951 // This relocation needs a page entry in the GOT.
8952 // Get the section contents.
8953 section_size_type view_size = 0;
8954 const unsigned char* view = object->section_contents(data_shndx,
8955 &view_size, false);
8956 view += r_offset;
8957
8958 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
8959 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
8960 : r_addend);
8961
8962 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
8963 target->got16_addends_.push_back(got16_addend<size, big_endian>(
8964 object, data_shndx, r_type, r_sym, addend));
8965 else
8966 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
8967 break;
8968 }
8969
8970 case elfcpp::R_MIPS_HI16:
8971 case elfcpp::R_MIPS16_HI16:
8972 case elfcpp::R_MICROMIPS_HI16:
8973 // Record the reloc so that we can check whether the corresponding LO16
8974 // part exists.
8975 if (rel_type == elfcpp::SHT_REL)
8976 target->got16_addends_.push_back(got16_addend<size, big_endian>(
8977 object, data_shndx, r_type, r_sym, 0));
8978 break;
8979
8980 case elfcpp::R_MIPS_LO16:
8981 case elfcpp::R_MIPS16_LO16:
8982 case elfcpp::R_MICROMIPS_LO16:
8983 {
8984 if (rel_type != elfcpp::SHT_REL)
8985 break;
8986
8987 // Find corresponding GOT16/HI16 relocation.
8988
8989 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8990 // be immediately following. However, for the IRIX6 ABI, the next
8991 // relocation may be a composed relocation consisting of several
8992 // relocations for the same address. In that case, the R_MIPS_LO16
8993 // relocation may occur as one of these. We permit a similar
8994 // extension in general, as that is useful for GCC.
8995
8996 // In some cases GCC dead code elimination removes the LO16 but
8997 // keeps the corresponding HI16. This is strictly speaking a
8998 // violation of the ABI but not immediately harmful.
8999
9000 typename std::list<got16_addend<size, big_endian> >::iterator it =
9001 target->got16_addends_.begin();
9002 while (it != target->got16_addends_.end())
9003 {
9004 got16_addend<size, big_endian> _got16_addend = *it;
9005
9006 // TODO(sasa): Split got16_addends_ list into two lists - one for
9007 // GOT16 relocs and the other for HI16 relocs.
9008
9009 // Report an error if we find HI16 or GOT16 reloc from the
9010 // previous section without the matching LO16 part.
9011 if (_got16_addend.object != object
9012 || _got16_addend.shndx != data_shndx)
9013 {
9014 gold_error("Can't find matching LO16 reloc");
9015 break;
9016 }
9017
9018 if (_got16_addend.r_sym != r_sym
9019 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
9020 {
9021 ++it;
9022 continue;
9023 }
9024
9025 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
9026 // For GOT16, we need to calculate combined addend and record GOT page
9027 // entry.
9028 if (got16_reloc(_got16_addend.r_type))
9029 {
9030
9031 section_size_type view_size = 0;
9032 const unsigned char* view = object->section_contents(data_shndx,
9033 &view_size,
9034 false);
9035 view += r_offset;
9036
9037 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
9038 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
9039
9040 addend = (_got16_addend.addend << 16) + addend;
9041 target->got_section()->record_got_page_entry(mips_obj, r_sym,
9042 addend);
9043 }
9044
9045 it = target->got16_addends_.erase(it);
9046 }
9047 break;
9048 }
9049 }
9050
9051 switch (r_type)
9052 {
9053 case elfcpp::R_MIPS_32:
9054 case elfcpp::R_MIPS_REL32:
9055 case elfcpp::R_MIPS_64:
9056 {
9057 if (parameters->options().output_is_position_independent())
9058 {
9059 // If building a shared library (or a position-independent
9060 // executable), we need to create a dynamic relocation for
9061 // this location.
9062 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9810d34d
SS
9063 rel_dyn->add_symbolless_local_addend(object, r_sym,
9064 elfcpp::R_MIPS_REL32,
9065 output_section, data_shndx,
9066 r_offset);
9067 }
9068 break;
9069 }
9070
9071 case elfcpp::R_MIPS_TLS_GOTTPREL:
9072 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9073 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9074 case elfcpp::R_MIPS_TLS_LDM:
9075 case elfcpp::R_MIPS16_TLS_LDM:
9076 case elfcpp::R_MICROMIPS_TLS_LDM:
9077 case elfcpp::R_MIPS_TLS_GD:
9078 case elfcpp::R_MIPS16_TLS_GD:
9079 case elfcpp::R_MICROMIPS_TLS_GD:
9080 {
9810d34d
SS
9081 bool output_is_shared = parameters->options().shared();
9082 const tls::Tls_optimization optimized_type
9083 = Target_mips<size, big_endian>::optimize_tls_reloc(
9084 !output_is_shared, r_type);
9085 switch (r_type)
9086 {
9087 case elfcpp::R_MIPS_TLS_GD:
9088 case elfcpp::R_MIPS16_TLS_GD:
9089 case elfcpp::R_MICROMIPS_TLS_GD:
9090 if (optimized_type == tls::TLSOPT_NONE)
9091 {
9092 // Create a pair of GOT entries for the module index and
9093 // dtv-relative offset.
9094 Mips_output_data_got<size, big_endian>* got =
9095 target->got_section(symtab, layout);
9096 unsigned int shndx = lsym.get_st_shndx();
9097 bool is_ordinary;
9098 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
9099 if (!is_ordinary)
9100 {
9101 object->error(_("local symbol %u has bad shndx %u"),
9102 r_sym, shndx);
9103 break;
9104 }
9105 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
9106 shndx);
9107 }
9108 else
9109 {
9110 // FIXME: TLS optimization not supported yet.
9111 gold_unreachable();
9112 }
9113 break;
9114
9115 case elfcpp::R_MIPS_TLS_LDM:
9116 case elfcpp::R_MIPS16_TLS_LDM:
9117 case elfcpp::R_MICROMIPS_TLS_LDM:
9118 if (optimized_type == tls::TLSOPT_NONE)
9119 {
9120 // We always record LDM symbols as local with index 0.
9121 target->got_section()->record_local_got_symbol(mips_obj, 0,
9122 r_addend, r_type,
9123 -1U);
9124 }
9125 else
9126 {
9127 // FIXME: TLS optimization not supported yet.
9128 gold_unreachable();
9129 }
9130 break;
9131 case elfcpp::R_MIPS_TLS_GOTTPREL:
9132 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9133 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9134 layout->set_has_static_tls();
9135 if (optimized_type == tls::TLSOPT_NONE)
9136 {
9137 // Create a GOT entry for the tp-relative offset.
9138 Mips_output_data_got<size, big_endian>* got =
9139 target->got_section(symtab, layout);
9140 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
9141 -1U);
9142 }
9143 else
9144 {
9145 // FIXME: TLS optimization not supported yet.
9146 gold_unreachable();
9147 }
9148 break;
9149
9150 default:
9151 gold_unreachable();
9152 }
9153 }
9154 break;
9155
9156 default:
9157 break;
9158 }
9159
9160 // Refuse some position-dependent relocations when creating a
9161 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9162 // not PIC, but we can create dynamic relocations and the result
9163 // will be fine. Also do not refuse R_MIPS_LO16, which can be
9164 // combined with R_MIPS_GOT16.
9165 if (parameters->options().shared())
9166 {
9167 switch (r_type)
9168 {
9169 case elfcpp::R_MIPS16_HI16:
9170 case elfcpp::R_MIPS_HI16:
9171 case elfcpp::R_MICROMIPS_HI16:
9172 // Don't refuse a high part relocation if it's against
9173 // no symbol (e.g. part of a compound relocation).
9174 if (r_sym == 0)
9175 break;
9176
9177 // FALLTHROUGH
9178
9179 case elfcpp::R_MIPS16_26:
9180 case elfcpp::R_MIPS_26:
9181 case elfcpp::R_MICROMIPS_26_S1:
9182 gold_error(_("%s: relocation %u against `%s' can not be used when "
9183 "making a shared object; recompile with -fPIC"),
9184 object->name().c_str(), r_type, "a local symbol");
9185 default:
9186 break;
9187 }
9188 }
9189}
9190
9191template<int size, bool big_endian>
9192inline void
9193Target_mips<size, big_endian>::Scan::local(
9194 Symbol_table* symtab,
9195 Layout* layout,
9196 Target_mips<size, big_endian>* target,
9197 Sized_relobj_file<size, big_endian>* object,
9198 unsigned int data_shndx,
9199 Output_section* output_section,
4d625b70 9200 const Reltype& reloc,
9810d34d
SS
9201 unsigned int r_type,
9202 const elfcpp::Sym<size, big_endian>& lsym,
9203 bool is_discarded)
9204{
9205 if (is_discarded)
9206 return;
9207
9208 local(
9209 symtab,
9210 layout,
9211 target,
9212 object,
9213 data_shndx,
9214 output_section,
4d625b70 9215 (const Relatype*) NULL,
9810d34d
SS
9216 &reloc,
9217 elfcpp::SHT_REL,
9218 r_type,
9219 lsym, is_discarded);
9220}
9221
9222
9223template<int size, bool big_endian>
9224inline void
9225Target_mips<size, big_endian>::Scan::local(
9226 Symbol_table* symtab,
9227 Layout* layout,
9228 Target_mips<size, big_endian>* target,
9229 Sized_relobj_file<size, big_endian>* object,
9230 unsigned int data_shndx,
9231 Output_section* output_section,
4d625b70 9232 const Relatype& reloc,
9810d34d
SS
9233 unsigned int r_type,
9234 const elfcpp::Sym<size, big_endian>& lsym,
9235 bool is_discarded)
9236{
9237 if (is_discarded)
9238 return;
9239
9240 local(
9241 symtab,
9242 layout,
9243 target,
9244 object,
9245 data_shndx,
9246 output_section,
9247 &reloc,
4d625b70 9248 (const Reltype*) NULL,
9810d34d
SS
9249 elfcpp::SHT_RELA,
9250 r_type,
9251 lsym, is_discarded);
9252}
9253
9254// Scan a relocation for a global symbol.
9255
9256template<int size, bool big_endian>
9257inline void
9258Target_mips<size, big_endian>::Scan::global(
9259 Symbol_table* symtab,
9260 Layout* layout,
9261 Target_mips<size, big_endian>* target,
9262 Sized_relobj_file<size, big_endian>* object,
9263 unsigned int data_shndx,
9264 Output_section* output_section,
4d625b70
CC
9265 const Relatype* rela,
9266 const Reltype* rel,
9810d34d
SS
9267 unsigned int rel_type,
9268 unsigned int r_type,
9269 Symbol* gsym)
9270{
9271 Mips_address r_offset;
4d625b70 9272 unsigned int r_sym;
9810d34d
SS
9273 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
9274
9275 if (rel_type == elfcpp::SHT_RELA)
9276 {
9277 r_offset = rela->get_r_offset();
4d625b70
CC
9278 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
9279 get_r_sym(rela);
9810d34d
SS
9280 r_addend = rela->get_r_addend();
9281 }
9282 else
9283 {
9284 r_offset = rel->get_r_offset();
4d625b70
CC
9285 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
9286 get_r_sym(rel);
9810d34d
SS
9287 r_addend = 0;
9288 }
9289
9810d34d
SS
9290 Mips_relobj<size, big_endian>* mips_obj =
9291 Mips_relobj<size, big_endian>::as_mips_relobj(object);
9292 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
9293
9294 if (mips_obj->is_mips16_stub_section(data_shndx))
9295 {
9296 mips_obj->get_mips16_stub_section(data_shndx)
9297 ->new_global_reloc_found(r_type, mips_sym);
9298 }
9299
9300 if (r_type == elfcpp::R_MIPS_NONE)
9301 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
9302 // mips16 stub.
9303 return;
9304
9305 if (!mips16_call_reloc(r_type)
9306 && !mips_obj->section_allows_mips16_refs(data_shndx))
9307 // This reloc would need to refer to a MIPS16 hard-float stub, if
9308 // there is one. We ignore MIPS16 stub sections and .pdr section when
9309 // looking for relocs that would need to refer to MIPS16 stubs.
9310 mips_sym->set_need_fn_stub();
9311
9312 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
9313 // section. We check here to avoid creating a dynamic reloc against
9314 // _GLOBAL_OFFSET_TABLE_.
9315 if (!target->has_got_section()
9316 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9317 target->got_section(symtab, layout);
9318
9319 // We need PLT entries if there are static-only relocations against
9320 // an externally-defined function. This can technically occur for
9321 // shared libraries if there are branches to the symbol, although it
9322 // is unlikely that this will be used in practice due to the short
9323 // ranges involved. It can occur for any relative or absolute relocation
9324 // in executables; in that case, the PLT entry becomes the function's
9325 // canonical address.
9326 bool static_reloc = false;
9327
9328 // Set CAN_MAKE_DYNAMIC to true if we can convert this
9329 // relocation into a dynamic one.
9330 bool can_make_dynamic = false;
9331 switch (r_type)
9332 {
9333 case elfcpp::R_MIPS_GOT16:
9334 case elfcpp::R_MIPS_CALL16:
9335 case elfcpp::R_MIPS_CALL_HI16:
9336 case elfcpp::R_MIPS_CALL_LO16:
9337 case elfcpp::R_MIPS_GOT_HI16:
9338 case elfcpp::R_MIPS_GOT_LO16:
9339 case elfcpp::R_MIPS_GOT_PAGE:
9340 case elfcpp::R_MIPS_GOT_OFST:
9341 case elfcpp::R_MIPS_GOT_DISP:
9342 case elfcpp::R_MIPS_TLS_GOTTPREL:
9343 case elfcpp::R_MIPS_TLS_GD:
9344 case elfcpp::R_MIPS_TLS_LDM:
9345 case elfcpp::R_MIPS16_GOT16:
9346 case elfcpp::R_MIPS16_CALL16:
9347 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9348 case elfcpp::R_MIPS16_TLS_GD:
9349 case elfcpp::R_MIPS16_TLS_LDM:
9350 case elfcpp::R_MICROMIPS_GOT16:
9351 case elfcpp::R_MICROMIPS_CALL16:
9352 case elfcpp::R_MICROMIPS_CALL_HI16:
9353 case elfcpp::R_MICROMIPS_CALL_LO16:
9354 case elfcpp::R_MICROMIPS_GOT_HI16:
9355 case elfcpp::R_MICROMIPS_GOT_LO16:
9356 case elfcpp::R_MICROMIPS_GOT_PAGE:
9357 case elfcpp::R_MICROMIPS_GOT_OFST:
9358 case elfcpp::R_MICROMIPS_GOT_DISP:
9359 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9360 case elfcpp::R_MICROMIPS_TLS_GD:
9361 case elfcpp::R_MICROMIPS_TLS_LDM:
9362 // We need a GOT section.
9363 target->got_section(symtab, layout);
9364 break;
9365
9366 // This is just a hint; it can safely be ignored. Don't set
9367 // has_static_relocs for the corresponding symbol.
9368 case elfcpp::R_MIPS_JALR:
9369 case elfcpp::R_MICROMIPS_JALR:
9370 break;
9371
9372 case elfcpp::R_MIPS_GPREL16:
9373 case elfcpp::R_MIPS_GPREL32:
9374 case elfcpp::R_MIPS16_GPREL:
9375 case elfcpp::R_MICROMIPS_GPREL16:
9376 // TODO(sasa)
9377 // GP-relative relocations always resolve to a definition in a
9378 // regular input file, ignoring the one-definition rule. This is
9379 // important for the GP setup sequence in NewABI code, which
9380 // always resolves to a local function even if other relocations
9381 // against the symbol wouldn't.
9382 //constrain_symbol_p = FALSE;
9383 break;
9384
9385 case elfcpp::R_MIPS_32:
9386 case elfcpp::R_MIPS_REL32:
9387 case elfcpp::R_MIPS_64:
9388 if (parameters->options().shared()
9389 || strcmp(gsym->name(), "__gnu_local_gp") != 0)
9390 {
9391 if (r_type != elfcpp::R_MIPS_REL32)
9392 {
9393 static_reloc = true;
9394 mips_sym->set_pointer_equality_needed();
9395 }
9396 can_make_dynamic = true;
9397 break;
9398 }
9399 // Fall through.
9400
9401 default:
9402 // Most static relocations require pointer equality, except
9403 // for branches.
9404 mips_sym->set_pointer_equality_needed();
9405
9406 // Fall through.
9407
9408 case elfcpp::R_MIPS_26:
9409 case elfcpp::R_MIPS_PC16:
9410 case elfcpp::R_MIPS16_26:
9411 case elfcpp::R_MICROMIPS_26_S1:
9412 case elfcpp::R_MICROMIPS_PC7_S1:
9413 case elfcpp::R_MICROMIPS_PC10_S1:
9414 case elfcpp::R_MICROMIPS_PC16_S1:
9415 case elfcpp::R_MICROMIPS_PC23_S2:
9416 static_reloc = true;
9417 mips_sym->set_has_static_relocs();
9418 break;
9419 }
9420
9421 // If there are call relocations against an externally-defined symbol,
9422 // see whether we can create a MIPS lazy-binding stub for it. We can
9423 // only do this if all references to the function are through call
9424 // relocations, and in that case, the traditional lazy-binding stubs
9425 // are much more efficient than PLT entries.
9426 switch (r_type)
9427 {
9428 case elfcpp::R_MIPS16_CALL16:
9429 case elfcpp::R_MIPS_CALL16:
9430 case elfcpp::R_MIPS_CALL_HI16:
9431 case elfcpp::R_MIPS_CALL_LO16:
9432 case elfcpp::R_MIPS_JALR:
9433 case elfcpp::R_MICROMIPS_CALL16:
9434 case elfcpp::R_MICROMIPS_CALL_HI16:
9435 case elfcpp::R_MICROMIPS_CALL_LO16:
9436 case elfcpp::R_MICROMIPS_JALR:
9437 if (!mips_sym->no_lazy_stub())
9438 {
9439 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
9440 // Calls from shared objects to undefined symbols of type
9441 // STT_NOTYPE need lazy-binding stub.
9442 || (mips_sym->is_undefined() && parameters->options().shared()))
9443 target->mips_stubs_section(layout)->make_entry(mips_sym);
9444 }
9445 break;
9446 default:
9447 {
9448 // We must not create a stub for a symbol that has relocations
9449 // related to taking the function's address.
9450 mips_sym->set_no_lazy_stub();
9451 target->remove_lazy_stub_entry(mips_sym);
9452 break;
9453 }
9454 }
9455
9456 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
9457 mips_sym->is_mips16()))
9458 mips_sym->set_has_nonpic_branches();
9459
9460 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
9461 // and has a special meaning.
9462 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
9463 && strcmp(gsym->name(), "_gp_disp") == 0
9464 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
9465 if (static_reloc && gsym->needs_plt_entry())
9466 {
9467 target->make_plt_entry(symtab, layout, mips_sym, r_type);
9468
9469 // Since this is not a PC-relative relocation, we may be
9470 // taking the address of a function. In that case we need to
9471 // set the entry in the dynamic symbol table to the address of
9472 // the PLT entry.
9473 if (gsym->is_from_dynobj() && !parameters->options().shared())
9474 {
9475 gsym->set_needs_dynsym_value();
9476 // We distinguish between PLT entries and lazy-binding stubs by
9477 // giving the former an st_other value of STO_MIPS_PLT. Set the
9478 // flag if there are any relocations in the binary where pointer
9479 // equality matters.
9480 if (mips_sym->pointer_equality_needed())
9481 mips_sym->set_mips_plt();
9482 }
9483 }
9484 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
9485 {
9486 // Absolute addressing relocations.
9487 // Make a dynamic relocation if necessary.
9488 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
9489 {
9490 if (gsym->may_need_copy_reloc())
9491 {
9492 target->copy_reloc(symtab, layout, object,
9493 data_shndx, output_section, gsym, *rel);
9494 }
9495 else if (can_make_dynamic)
9496 {
9497 // Create .rel.dyn section.
9498 target->rel_dyn_section(layout);
9499 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
9500 data_shndx, output_section, r_offset);
9501 }
9502 else
9503 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
9504 gsym->name());
9505 }
9506 }
9507
9508 bool for_call = false;
9509 switch (r_type)
9510 {
9511 case elfcpp::R_MIPS_CALL16:
9512 case elfcpp::R_MIPS16_CALL16:
9513 case elfcpp::R_MICROMIPS_CALL16:
9514 case elfcpp::R_MIPS_CALL_HI16:
9515 case elfcpp::R_MIPS_CALL_LO16:
9516 case elfcpp::R_MICROMIPS_CALL_HI16:
9517 case elfcpp::R_MICROMIPS_CALL_LO16:
9518 for_call = true;
9519 // Fall through.
9520
9521 case elfcpp::R_MIPS16_GOT16:
9522 case elfcpp::R_MIPS_GOT16:
9523 case elfcpp::R_MIPS_GOT_HI16:
9524 case elfcpp::R_MIPS_GOT_LO16:
9525 case elfcpp::R_MICROMIPS_GOT16:
9526 case elfcpp::R_MICROMIPS_GOT_HI16:
9527 case elfcpp::R_MICROMIPS_GOT_LO16:
9528 case elfcpp::R_MIPS_GOT_DISP:
9529 case elfcpp::R_MICROMIPS_GOT_DISP:
9530 {
9531 // The symbol requires a GOT entry.
9532 Mips_output_data_got<size, big_endian>* got =
9533 target->got_section(symtab, layout);
9534 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
9535 for_call);
9536 mips_sym->set_global_got_area(GGA_NORMAL);
9537 }
9538 break;
9539
9540 case elfcpp::R_MIPS_GOT_PAGE:
9541 case elfcpp::R_MICROMIPS_GOT_PAGE:
9542 {
9543 // This relocation needs a page entry in the GOT.
9544 // Get the section contents.
9545 section_size_type view_size = 0;
9546 const unsigned char* view =
9547 object->section_contents(data_shndx, &view_size, false);
9548 view += r_offset;
9549
9550 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
9551 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
9552 : r_addend);
9553 Mips_output_data_got<size, big_endian>* got =
9554 target->got_section(symtab, layout);
9555 got->record_got_page_entry(mips_obj, r_sym, addend);
9556
9557 // If this is a global, overridable symbol, GOT_PAGE will
9558 // decay to GOT_DISP, so we'll need a GOT entry for it.
9559 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
9560 && !mips_sym->object()->is_dynamic()
9561 && !mips_sym->is_undefined());
9562 if (!def_regular
9563 || (parameters->options().output_is_position_independent()
9564 && !parameters->options().Bsymbolic()
9565 && !mips_sym->is_forced_local()))
9566 {
9567 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
9568 for_call);
9569 mips_sym->set_global_got_area(GGA_NORMAL);
9570 }
9571 }
9572 break;
9573
9574 case elfcpp::R_MIPS_TLS_GOTTPREL:
9575 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9576 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9577 case elfcpp::R_MIPS_TLS_LDM:
9578 case elfcpp::R_MIPS16_TLS_LDM:
9579 case elfcpp::R_MICROMIPS_TLS_LDM:
9580 case elfcpp::R_MIPS_TLS_GD:
9581 case elfcpp::R_MIPS16_TLS_GD:
9582 case elfcpp::R_MICROMIPS_TLS_GD:
9583 {
9584 const bool is_final = gsym->final_value_is_known();
9585 const tls::Tls_optimization optimized_type =
9586 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
9587
9588 switch (r_type)
9589 {
9590 case elfcpp::R_MIPS_TLS_GD:
9591 case elfcpp::R_MIPS16_TLS_GD:
9592 case elfcpp::R_MICROMIPS_TLS_GD:
9593 if (optimized_type == tls::TLSOPT_NONE)
9594 {
9595 // Create a pair of GOT entries for the module index and
9596 // dtv-relative offset.
9597 Mips_output_data_got<size, big_endian>* got =
9598 target->got_section(symtab, layout);
9599 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
9600 false);
9601 }
9602 else
9603 {
9604 // FIXME: TLS optimization not supported yet.
9605 gold_unreachable();
9606 }
9607 break;
9608
9609 case elfcpp::R_MIPS_TLS_LDM:
9610 case elfcpp::R_MIPS16_TLS_LDM:
9611 case elfcpp::R_MICROMIPS_TLS_LDM:
9612 if (optimized_type == tls::TLSOPT_NONE)
9613 {
9614 // We always record LDM symbols as local with index 0.
9615 target->got_section()->record_local_got_symbol(mips_obj, 0,
9616 r_addend, r_type,
9617 -1U);
9618 }
9619 else
9620 {
9621 // FIXME: TLS optimization not supported yet.
9622 gold_unreachable();
9623 }
9624 break;
9625 case elfcpp::R_MIPS_TLS_GOTTPREL:
9626 case elfcpp::R_MIPS16_TLS_GOTTPREL:
9627 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
9628 layout->set_has_static_tls();
9629 if (optimized_type == tls::TLSOPT_NONE)
9630 {
9631 // Create a GOT entry for the tp-relative offset.
9632 Mips_output_data_got<size, big_endian>* got =
9633 target->got_section(symtab, layout);
9634 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
9635 false);
9636 }
9637 else
9638 {
9639 // FIXME: TLS optimization not supported yet.
9640 gold_unreachable();
9641 }
9642 break;
9643
9644 default:
9645 gold_unreachable();
9646 }
9647 }
9648 break;
9649 case elfcpp::R_MIPS_COPY:
9650 case elfcpp::R_MIPS_JUMP_SLOT:
9651 // These are relocations which should only be seen by the
9652 // dynamic linker, and should never be seen here.
9653 gold_error(_("%s: unexpected reloc %u in object file"),
9654 object->name().c_str(), r_type);
9655 break;
9656
9657 default:
9658 break;
9659 }
9660
9661 // Refuse some position-dependent relocations when creating a
9662 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9663 // not PIC, but we can create dynamic relocations and the result
9664 // will be fine. Also do not refuse R_MIPS_LO16, which can be
9665 // combined with R_MIPS_GOT16.
9666 if (parameters->options().shared())
9667 {
9668 switch (r_type)
9669 {
9670 case elfcpp::R_MIPS16_HI16:
9671 case elfcpp::R_MIPS_HI16:
9672 case elfcpp::R_MICROMIPS_HI16:
9673 // Don't refuse a high part relocation if it's against
9674 // no symbol (e.g. part of a compound relocation).
9675 if (r_sym == 0)
9676 break;
9677
9678 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
9679 // and has a special meaning.
9680 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
9681 break;
9682
9683 // FALLTHROUGH
9684
9685 case elfcpp::R_MIPS16_26:
9686 case elfcpp::R_MIPS_26:
9687 case elfcpp::R_MICROMIPS_26_S1:
9688 gold_error(_("%s: relocation %u against `%s' can not be used when "
9689 "making a shared object; recompile with -fPIC"),
9690 object->name().c_str(), r_type, gsym->name());
9691 default:
9692 break;
9693 }
9694 }
9695}
9696
9697template<int size, bool big_endian>
9698inline void
9699Target_mips<size, big_endian>::Scan::global(
9700 Symbol_table* symtab,
9701 Layout* layout,
9702 Target_mips<size, big_endian>* target,
9703 Sized_relobj_file<size, big_endian>* object,
9704 unsigned int data_shndx,
9705 Output_section* output_section,
4d625b70 9706 const Relatype& reloc,
9810d34d
SS
9707 unsigned int r_type,
9708 Symbol* gsym)
9709{
9710 global(
9711 symtab,
9712 layout,
9713 target,
9714 object,
9715 data_shndx,
9716 output_section,
9717 &reloc,
4d625b70 9718 (const Reltype*) NULL,
9810d34d
SS
9719 elfcpp::SHT_RELA,
9720 r_type,
9721 gsym);
9722}
9723
9724template<int size, bool big_endian>
9725inline void
9726Target_mips<size, big_endian>::Scan::global(
9727 Symbol_table* symtab,
9728 Layout* layout,
9729 Target_mips<size, big_endian>* target,
9730 Sized_relobj_file<size, big_endian>* object,
9731 unsigned int data_shndx,
9732 Output_section* output_section,
4d625b70 9733 const Reltype& reloc,
9810d34d
SS
9734 unsigned int r_type,
9735 Symbol* gsym)
9736{
9737 global(
9738 symtab,
9739 layout,
9740 target,
9741 object,
9742 data_shndx,
9743 output_section,
4d625b70 9744 (const Relatype*) NULL,
9810d34d
SS
9745 &reloc,
9746 elfcpp::SHT_REL,
9747 r_type,
9748 gsym);
9749}
9750
9751// Return whether a R_MIPS_32 relocation needs to be applied.
9752
9753template<int size, bool big_endian>
9754inline bool
9755Target_mips<size, big_endian>::Relocate::should_apply_r_mips_32_reloc(
9756 const Mips_symbol<size>* gsym,
9757 unsigned int r_type,
9758 Output_section* output_section,
9759 Target_mips* target)
9760{
9761 // If the output section is not allocated, then we didn't call
9762 // scan_relocs, we didn't create a dynamic reloc, and we must apply
9763 // the reloc here.
9764 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
9765 return true;
9766
9767 if (gsym == NULL)
9768 return true;
9769 else
9770 {
9771 // For global symbols, we use the same helper routines used in the
9772 // scan pass.
9773 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
9774 && !gsym->may_need_copy_reloc())
9775 {
9776 // We have generated dynamic reloc (R_MIPS_REL32).
9777
9778 bool multi_got = false;
9779 if (target->has_got_section())
9780 multi_got = target->got_section()->multi_got();
9781 bool has_got_offset;
9782 if (!multi_got)
9783 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
9784 else
9785 has_got_offset = gsym->global_gotoffset() != -1U;
9786 if (!has_got_offset)
9787 return true;
9788 else
9789 // Apply the relocation only if the symbol is in the local got.
9790 // Do not apply the relocation if the symbol is in the global
9791 // got.
9792 return symbol_references_local(gsym, gsym->has_dynsym_index());
9793 }
9794 else
9795 // We have not generated dynamic reloc.
9796 return true;
9797 }
9798}
9799
9800// Perform a relocation.
9801
9802template<int size, bool big_endian>
9803inline bool
9804Target_mips<size, big_endian>::Relocate::relocate(
9805 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 9806 unsigned int rel_type,
9810d34d
SS
9807 Target_mips* target,
9808 Output_section* output_section,
9809 size_t relnum,
91a65d2f 9810 const unsigned char* preloc,
9810d34d
SS
9811 const Sized_symbol<size>* gsym,
9812 const Symbol_value<size>* psymval,
9813 unsigned char* view,
9814 Mips_address address,
9815 section_size_type)
9816{
9817 Mips_address r_offset;
4d625b70
CC
9818 unsigned int r_sym;
9819 unsigned int r_type;
9810d34d
SS
9820 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
9821
9822 if (rel_type == elfcpp::SHT_RELA)
9823 {
4d625b70 9824 const Relatype rela(preloc);
91a65d2f 9825 r_offset = rela.get_r_offset();
4d625b70
CC
9826 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
9827 get_r_sym(&rela);
9828 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
9829 get_r_type(&rela);
91a65d2f 9830 r_addend = rela.get_r_addend();
9810d34d
SS
9831 }
9832 else
9833 {
4d625b70
CC
9834
9835 const Reltype rel(preloc);
91a65d2f 9836 r_offset = rel.get_r_offset();
4d625b70
CC
9837 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
9838 get_r_sym(&rel);
9839 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
9840 get_r_type(&rel);
9810d34d
SS
9841 r_addend = 0;
9842 }
9843
9844 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
9845 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
9846
9847 Mips_relobj<size, big_endian>* object =
4d625b70 9848 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
9810d34d 9849
9810d34d
SS
9850 bool target_is_16_bit_code = false;
9851 bool target_is_micromips_code = false;
9852 bool cross_mode_jump;
9853
9854 Symbol_value<size> symval;
9855
9856 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
9857
9858 bool changed_symbol_value = false;
9859 if (gsym == NULL)
9860 {
9861 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
9862 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
9863 if (target_is_16_bit_code || target_is_micromips_code)
9864 {
9865 // MIPS16/microMIPS text labels should be treated as odd.
9866 symval.set_output_value(psymval->value(object, 1));
9867 psymval = &symval;
9868 changed_symbol_value = true;
9869 }
9870 }
9871 else
9872 {
9873 target_is_16_bit_code = mips_sym->is_mips16();
9874 target_is_micromips_code = mips_sym->is_micromips();
9875
9876 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
9877 // it odd. This will cause something like .word SYM to come up with
9878 // the right value when it is loaded into the PC.
9879
9880 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
9881 && psymval->value(object, 0) != 0)
9882 {
9883 symval.set_output_value(psymval->value(object, 0) | 1);
9884 psymval = &symval;
9885 changed_symbol_value = true;
9886 }
9887
9888 // Pick the value to use for symbols defined in shared objects.
9889 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
9890 || mips_sym->has_lazy_stub())
9891 {
9892 Mips_address value;
9893 if (!mips_sym->has_lazy_stub())
9894 {
9895 // Prefer a standard MIPS PLT entry.
9896 if (mips_sym->has_mips_plt_offset())
9897 {
9898 value = target->plt_section()->mips_entry_address(mips_sym);
9899 target_is_micromips_code = false;
9900 target_is_16_bit_code = false;
9901 }
9902 else
9903 {
9904 value = (target->plt_section()->comp_entry_address(mips_sym)
9905 + 1);
9906 if (target->is_output_micromips())
9907 target_is_micromips_code = true;
9908 else
9909 target_is_16_bit_code = true;
9910 }
9911 }
9912 else
9913 value = target->mips_stubs_section()->stub_address(mips_sym);
9914
9915 symval.set_output_value(value);
9916 psymval = &symval;
9917 }
9918 }
9919
9920 // TRUE if the symbol referred to by this relocation is "_gp_disp".
9921 // Note that such a symbol must always be a global symbol.
9922 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
9923 && !object->is_newabi());
9924
9925 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
9926 // Note that such a symbol must always be a global symbol.
9927 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
9928
9929
9930 if (gp_disp)
9931 {
9932 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
9933 gold_error_at_location(relinfo, relnum, r_offset,
9934 _("relocations against _gp_disp are permitted only"
9935 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
9936 }
9937 else if (gnu_local_gp)
9938 {
9939 // __gnu_local_gp is _gp symbol.
9940 symval.set_output_value(target->adjusted_gp_value(object));
9941 psymval = &symval;
9942 }
9943
9944 // If this is a reference to a 16-bit function with a stub, we need
9945 // to redirect the relocation to the stub unless:
9946 //
9947 // (a) the relocation is for a MIPS16 JAL;
9948 //
9949 // (b) the relocation is for a MIPS16 PIC call, and there are no
9950 // non-MIPS16 uses of the GOT slot; or
9951 //
9952 // (c) the section allows direct references to MIPS16 functions.
9953 if (r_type != elfcpp::R_MIPS16_26
9954 && !parameters->options().relocatable()
9955 && ((mips_sym != NULL
9956 && mips_sym->has_mips16_fn_stub()
9957 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
9958 || (mips_sym == NULL
9959 && object->get_local_mips16_fn_stub(r_sym) != NULL))
9960 && !object->section_allows_mips16_refs(relinfo->data_shndx))
9961 {
9962 // This is a 32- or 64-bit call to a 16-bit function. We should
9963 // have already noticed that we were going to need the
9964 // stub.
9965 Mips_address value;
9966 if (mips_sym == NULL)
9967 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
9968 else
9969 {
9970 gold_assert(mips_sym->need_fn_stub());
9971 if (mips_sym->has_la25_stub())
9972 value = target->la25_stub_section()->stub_address(mips_sym);
9973 else
9974 {
9975 value = mips_sym->template
9976 get_mips16_fn_stub<big_endian>()->output_address();
9977 }
9978 }
9979 symval.set_output_value(value);
9980 psymval = &symval;
9981 changed_symbol_value = true;
9982
9983 // The target is 16-bit, but the stub isn't.
9984 target_is_16_bit_code = false;
9985 }
9986 // If this is a MIPS16 call with a stub, that is made through the PLT or
9987 // to a standard MIPS function, we need to redirect the call to the stub.
9988 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
9989 // indirect calls should use an indirect stub instead.
9990 else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable()
9991 && ((mips_sym != NULL
9992 && (mips_sym->has_mips16_call_stub()
9993 || mips_sym->has_mips16_call_fp_stub()))
9994 || (mips_sym == NULL
9995 && object->get_local_mips16_call_stub(r_sym) != NULL))
9996 && ((mips_sym != NULL && mips_sym->has_plt_offset())
9997 || !target_is_16_bit_code))
9998 {
9999 Mips16_stub_section<size, big_endian>* call_stub;
10000 if (mips_sym == NULL)
10001 call_stub = object->get_local_mips16_call_stub(r_sym);
10002 else
10003 {
10004 // If both call_stub and call_fp_stub are defined, we can figure
10005 // out which one to use by checking which one appears in the input
10006 // file.
10007 if (mips_sym->has_mips16_call_stub()
10008 && mips_sym->has_mips16_call_fp_stub())
10009 {
10010 call_stub = NULL;
10011 for (unsigned int i = 1; i < object->shnum(); ++i)
10012 {
10013 if (object->is_mips16_call_fp_stub_section(i))
10014 {
10015 call_stub = mips_sym->template
10016 get_mips16_call_fp_stub<big_endian>();
10017 break;
10018 }
10019
10020 }
10021 if (call_stub == NULL)
10022 call_stub =
10023 mips_sym->template get_mips16_call_stub<big_endian>();
10024 }
10025 else if (mips_sym->has_mips16_call_stub())
10026 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
10027 else
10028 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
10029 }
10030
10031 symval.set_output_value(call_stub->output_address());
10032 psymval = &symval;
10033 changed_symbol_value = true;
10034 }
10035 // If this is a direct call to a PIC function, redirect to the
10036 // non-PIC stub.
10037 else if (mips_sym != NULL
10038 && mips_sym->has_la25_stub()
10039 && relocation_needs_la25_stub<size, big_endian>(
10040 object, r_type, target_is_16_bit_code))
10041 {
10042 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
10043 if (mips_sym->is_micromips())
10044 value += 1;
10045 symval.set_output_value(value);
10046 psymval = &symval;
10047 }
10048 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
10049 // entry is used if a standard PLT entry has also been made.
10050 else if ((r_type == elfcpp::R_MIPS16_26
10051 || r_type == elfcpp::R_MICROMIPS_26_S1)
10052 && !parameters->options().relocatable()
10053 && mips_sym != NULL
10054 && mips_sym->has_plt_offset()
10055 && mips_sym->has_comp_plt_offset()
10056 && mips_sym->has_mips_plt_offset())
10057 {
10058 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
10059 + 1);
10060 symval.set_output_value(value);
10061 psymval = &symval;
10062
10063 target_is_16_bit_code = !target->is_output_micromips();
10064 target_is_micromips_code = target->is_output_micromips();
10065 }
10066
10067 // Make sure MIPS16 and microMIPS are not used together.
10068 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
10069 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
10070 {
10071 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
10072 }
10073
10074 // Calls from 16-bit code to 32-bit code and vice versa require the
10075 // mode change. However, we can ignore calls to undefined weak symbols,
10076 // which should never be executed at runtime. This exception is important
10077 // because the assembly writer may have "known" that any definition of the
10078 // symbol would be 16-bit code, and that direct jumps were therefore
10079 // acceptable.
10080 cross_mode_jump =
10081 (!parameters->options().relocatable()
10082 && !(gsym != NULL && gsym->is_weak_undefined())
10083 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
10084 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
10085 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
10086 && (target_is_16_bit_code || target_is_micromips_code))));
10087
10088 bool local = (mips_sym == NULL
10089 || (mips_sym->got_only_for_calls()
10090 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
10091 : symbol_references_local(mips_sym,
10092 mips_sym->has_dynsym_index())));
10093
10094 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
10095 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
10096 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
10097 if (got_page_reloc(r_type) && !local)
10098 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
10099 : elfcpp::R_MIPS_GOT_DISP);
10100
10101 unsigned int got_offset = 0;
10102 int gp_offset = 0;
10103
10104 bool update_got_entry = false;
10105 bool extract_addend = rel_type == elfcpp::SHT_REL;
10106 switch (r_type)
10107 {
10108 case elfcpp::R_MIPS_NONE:
10109 break;
10110 case elfcpp::R_MIPS_16:
10111 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
10112 extract_addend, r_type);
10113 break;
10114
10115 case elfcpp::R_MIPS_32:
10116 if (should_apply_r_mips_32_reloc(mips_sym, r_type, output_section,
10117 target))
10118 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
10119 extract_addend, r_type);
10120 if (mips_sym != NULL
10121 && (mips_sym->is_mips16() || mips_sym->is_micromips())
10122 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
10123 {
10124 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
10125 // already updated by adding +1.
10126 if (mips_sym->has_mips16_fn_stub())
10127 {
10128 gold_assert(mips_sym->need_fn_stub());
10129 Mips16_stub_section<size, big_endian>* fn_stub =
10130 mips_sym->template get_mips16_fn_stub<big_endian>();
10131
10132 symval.set_output_value(fn_stub->output_address());
10133 psymval = &symval;
10134 }
10135 got_offset = mips_sym->global_gotoffset();
10136 update_got_entry = true;
10137 }
10138 break;
10139
10140 case elfcpp::R_MIPS_REL32:
10141 gold_unreachable();
10142
10143 case elfcpp::R_MIPS_PC32:
10144 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
10145 r_addend, extract_addend, r_type);
10146 break;
10147
10148 case elfcpp::R_MIPS16_26:
10149 // The calculation for R_MIPS16_26 is just the same as for an
10150 // R_MIPS_26. It's only the storage of the relocated field into
10151 // the output file that's different. So, we just fall through to the
10152 // R_MIPS_26 case here.
10153 case elfcpp::R_MIPS_26:
10154 case elfcpp::R_MICROMIPS_26_S1:
10155 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
10156 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump, r_type,
10157 target->jal_to_bal());
10158 break;
10159
10160 case elfcpp::R_MIPS_HI16:
10161 case elfcpp::R_MIPS16_HI16:
10162 case elfcpp::R_MICROMIPS_HI16:
10163 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
3d0064a9 10164 address, gp_disp, r_type, r_sym,
9810d34d
SS
10165 extract_addend);
10166 break;
10167
10168 case elfcpp::R_MIPS_LO16:
10169 case elfcpp::R_MIPS16_LO16:
10170 case elfcpp::R_MICROMIPS_LO16:
10171 case elfcpp::R_MICROMIPS_HI0_LO16:
10172 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
10173 r_addend, extract_addend, address,
3d0064a9 10174 gp_disp, r_type, r_sym);
9810d34d
SS
10175 break;
10176
10177 case elfcpp::R_MIPS_LITERAL:
10178 case elfcpp::R_MICROMIPS_LITERAL:
10179 // Because we don't merge literal sections, we can handle this
10180 // just like R_MIPS_GPREL16. In the long run, we should merge
10181 // shared literals, and then we will need to additional work
10182 // here.
10183
10184 // Fall through.
10185
10186 case elfcpp::R_MIPS_GPREL16:
10187 case elfcpp::R_MIPS16_GPREL:
10188 case elfcpp::R_MICROMIPS_GPREL7_S2:
10189 case elfcpp::R_MICROMIPS_GPREL16:
10190 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
10191 target->adjusted_gp_value(object),
10192 r_addend, extract_addend,
10193 gsym == NULL, r_type);
10194 break;
10195
10196 case elfcpp::R_MIPS_PC16:
10197 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
10198 r_addend, extract_addend, r_type);
10199 break;
10200 case elfcpp::R_MICROMIPS_PC7_S1:
10201 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
10202 address, r_addend,
10203 extract_addend, r_type);
10204 break;
10205 case elfcpp::R_MICROMIPS_PC10_S1:
10206 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object, psymval,
10207 address, r_addend,
10208 extract_addend, r_type);
10209 break;
10210 case elfcpp::R_MICROMIPS_PC16_S1:
10211 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object, psymval,
10212 address, r_addend,
10213 extract_addend, r_type);
10214 break;
10215 case elfcpp::R_MIPS_GPREL32:
10216 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
10217 target->adjusted_gp_value(object),
10218 r_addend, extract_addend, r_type);
10219 break;
10220 case elfcpp::R_MIPS_GOT_HI16:
10221 case elfcpp::R_MIPS_CALL_HI16:
10222 case elfcpp::R_MICROMIPS_GOT_HI16:
10223 case elfcpp::R_MICROMIPS_CALL_HI16:
10224 if (gsym != NULL)
10225 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
10226 object);
10227 else
10228 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
10229 object);
10230 gp_offset = target->got_section()->gp_offset(got_offset, object);
10231 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset, r_type);
10232 update_got_entry = changed_symbol_value;
10233 break;
10234
10235 case elfcpp::R_MIPS_GOT_LO16:
10236 case elfcpp::R_MIPS_CALL_LO16:
10237 case elfcpp::R_MICROMIPS_GOT_LO16:
10238 case elfcpp::R_MICROMIPS_CALL_LO16:
10239 if (gsym != NULL)
10240 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
10241 object);
10242 else
10243 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
10244 object);
10245 gp_offset = target->got_section()->gp_offset(got_offset, object);
10246 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset, r_type);
10247 update_got_entry = changed_symbol_value;
10248 break;
10249
10250 case elfcpp::R_MIPS_GOT_DISP:
10251 case elfcpp::R_MICROMIPS_GOT_DISP:
10252 if (gsym != NULL)
10253 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
10254 object);
10255 else
10256 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
10257 object);
10258 gp_offset = target->got_section()->gp_offset(got_offset, object);
10259 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
10260 break;
10261
10262 case elfcpp::R_MIPS_CALL16:
10263 case elfcpp::R_MIPS16_CALL16:
10264 case elfcpp::R_MICROMIPS_CALL16:
10265 gold_assert(gsym != NULL);
10266 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
10267 object);
10268 gp_offset = target->got_section()->gp_offset(got_offset, object);
10269 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
10270 // TODO(sasa): We should also initialize update_got_entry in other places
10271 // where relgot is called.
10272 update_got_entry = changed_symbol_value;
10273 break;
10274
10275 case elfcpp::R_MIPS_GOT16:
10276 case elfcpp::R_MIPS16_GOT16:
10277 case elfcpp::R_MICROMIPS_GOT16:
10278 if (gsym != NULL)
10279 {
10280 got_offset = target->got_section()->got_offset(gsym,
10281 GOT_TYPE_STANDARD,
10282 object);
10283 gp_offset = target->got_section()->gp_offset(got_offset, object);
10284 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
10285 }
10286 else
10287 reloc_status = Reloc_funcs::relgot16_local(view, object, psymval,
10288 r_addend, extract_addend,
3d0064a9 10289 r_type, r_sym);
9810d34d
SS
10290 update_got_entry = changed_symbol_value;
10291 break;
10292
10293 case elfcpp::R_MIPS_TLS_GD:
10294 case elfcpp::R_MIPS16_TLS_GD:
10295 case elfcpp::R_MICROMIPS_TLS_GD:
10296 if (gsym != NULL)
10297 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_TLS_PAIR,
10298 object);
10299 else
10300 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_TLS_PAIR,
10301 object);
10302 gp_offset = target->got_section()->gp_offset(got_offset, object);
10303 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
10304 break;
10305
10306 case elfcpp::R_MIPS_TLS_GOTTPREL:
10307 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10308 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10309 if (gsym != NULL)
10310 got_offset = target->got_section()->got_offset(gsym,
10311 GOT_TYPE_TLS_OFFSET,
10312 object);
10313 else
10314 got_offset = target->got_section()->got_offset(r_sym,
10315 GOT_TYPE_TLS_OFFSET,
10316 object);
10317 gp_offset = target->got_section()->gp_offset(got_offset, object);
10318 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
10319 break;
10320
10321 case elfcpp::R_MIPS_TLS_LDM:
10322 case elfcpp::R_MIPS16_TLS_LDM:
10323 case elfcpp::R_MICROMIPS_TLS_LDM:
10324 // Relocate the field with the offset of the GOT entry for
10325 // the module index.
10326 got_offset = target->got_section()->tls_ldm_offset(object);
10327 gp_offset = target->got_section()->gp_offset(got_offset, object);
10328 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type);
10329 break;
10330
10331 case elfcpp::R_MIPS_GOT_PAGE:
10332 case elfcpp::R_MICROMIPS_GOT_PAGE:
10333 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
10334 r_addend, extract_addend, r_type);
10335 break;
10336
10337 case elfcpp::R_MIPS_GOT_OFST:
10338 case elfcpp::R_MICROMIPS_GOT_OFST:
10339 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
10340 r_addend, extract_addend, local,
10341 r_type);
10342 break;
10343
10344 case elfcpp::R_MIPS_JALR:
10345 case elfcpp::R_MICROMIPS_JALR:
10346 // This relocation is only a hint. In some cases, we optimize
10347 // it into a bal instruction. But we don't try to optimize
10348 // when the symbol does not resolve locally.
10349 if (gsym == NULL || symbol_calls_local(gsym, gsym->has_dynsym_index()))
10350 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
10351 r_addend, extract_addend,
10352 cross_mode_jump, r_type,
10353 target->jalr_to_bal(),
10354 target->jr_to_b());
10355 break;
10356
10357 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10358 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
10359 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
10360 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
10361 elfcpp::DTP_OFFSET, r_addend,
10362 extract_addend, r_type);
10363 break;
10364 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10365 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
10366 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
10367 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
10368 elfcpp::DTP_OFFSET, r_addend,
10369 extract_addend, r_type);
10370 break;
10371 case elfcpp::R_MIPS_TLS_DTPREL32:
10372 case elfcpp::R_MIPS_TLS_DTPREL64:
10373 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
10374 elfcpp::DTP_OFFSET, r_addend,
10375 extract_addend, r_type);
10376 break;
10377 case elfcpp::R_MIPS_TLS_TPREL_HI16:
10378 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
10379 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
10380 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
10381 elfcpp::TP_OFFSET, r_addend,
10382 extract_addend, r_type);
10383 break;
10384 case elfcpp::R_MIPS_TLS_TPREL_LO16:
10385 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
10386 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
10387 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
10388 elfcpp::TP_OFFSET, r_addend,
10389 extract_addend, r_type);
10390 break;
10391 case elfcpp::R_MIPS_TLS_TPREL32:
10392 case elfcpp::R_MIPS_TLS_TPREL64:
10393 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
10394 elfcpp::TP_OFFSET, r_addend,
10395 extract_addend, r_type);
10396 break;
10397 case elfcpp::R_MIPS_SUB:
10398 case elfcpp::R_MICROMIPS_SUB:
10399 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
10400 extract_addend, r_type);
10401 break;
10402 default:
10403 gold_error_at_location(relinfo, relnum, r_offset,
10404 _("unsupported reloc %u"), r_type);
10405 break;
10406 }
10407
10408 if (update_got_entry)
10409 {
10410 Mips_output_data_got<size, big_endian>* got = target->got_section();
10411 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
10412 got->update_got_entry(got->get_primary_got_offset(mips_sym),
10413 psymval->value(object, 0));
10414 else
10415 got->update_got_entry(got_offset, psymval->value(object, 0));
10416 }
10417
10418 // Report any errors.
10419 switch (reloc_status)
10420 {
10421 case Reloc_funcs::STATUS_OKAY:
10422 break;
10423 case Reloc_funcs::STATUS_OVERFLOW:
10424 gold_error_at_location(relinfo, relnum, r_offset,
10425 _("relocation overflow"));
10426 break;
10427 case Reloc_funcs::STATUS_BAD_RELOC:
10428 gold_error_at_location(relinfo, relnum, r_offset,
10429 _("unexpected opcode while processing relocation"));
10430 break;
10431 default:
10432 gold_unreachable();
10433 }
10434
10435 return true;
10436}
10437
9810d34d
SS
10438// Get the Reference_flags for a particular relocation.
10439
10440template<int size, bool big_endian>
10441int
10442Target_mips<size, big_endian>::Scan::get_reference_flags(
10443 unsigned int r_type)
10444{
10445 switch (r_type)
10446 {
10447 case elfcpp::R_MIPS_NONE:
10448 // No symbol reference.
10449 return 0;
10450
10451 case elfcpp::R_MIPS_16:
10452 case elfcpp::R_MIPS_32:
10453 case elfcpp::R_MIPS_64:
10454 case elfcpp::R_MIPS_HI16:
10455 case elfcpp::R_MIPS_LO16:
10456 case elfcpp::R_MIPS16_HI16:
10457 case elfcpp::R_MIPS16_LO16:
10458 case elfcpp::R_MICROMIPS_HI16:
10459 case elfcpp::R_MICROMIPS_LO16:
10460 return Symbol::ABSOLUTE_REF;
10461
10462 case elfcpp::R_MIPS_26:
10463 case elfcpp::R_MIPS16_26:
10464 case elfcpp::R_MICROMIPS_26_S1:
10465 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
10466
10467 case elfcpp::R_MIPS_GPREL32:
10468 case elfcpp::R_MIPS_GPREL16:
10469 case elfcpp::R_MIPS_REL32:
10470 case elfcpp::R_MIPS16_GPREL:
10471 return Symbol::RELATIVE_REF;
10472
10473 case elfcpp::R_MIPS_PC16:
10474 case elfcpp::R_MIPS_PC32:
10475 case elfcpp::R_MIPS_JALR:
10476 case elfcpp::R_MICROMIPS_JALR:
10477 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
10478
10479 case elfcpp::R_MIPS_GOT16:
10480 case elfcpp::R_MIPS_CALL16:
10481 case elfcpp::R_MIPS_GOT_DISP:
10482 case elfcpp::R_MIPS_GOT_HI16:
10483 case elfcpp::R_MIPS_GOT_LO16:
10484 case elfcpp::R_MIPS_CALL_HI16:
10485 case elfcpp::R_MIPS_CALL_LO16:
10486 case elfcpp::R_MIPS_LITERAL:
10487 case elfcpp::R_MIPS_GOT_PAGE:
10488 case elfcpp::R_MIPS_GOT_OFST:
10489 case elfcpp::R_MIPS16_GOT16:
10490 case elfcpp::R_MIPS16_CALL16:
10491 case elfcpp::R_MICROMIPS_GOT16:
10492 case elfcpp::R_MICROMIPS_CALL16:
10493 case elfcpp::R_MICROMIPS_GOT_HI16:
10494 case elfcpp::R_MICROMIPS_GOT_LO16:
10495 case elfcpp::R_MICROMIPS_CALL_HI16:
10496 case elfcpp::R_MICROMIPS_CALL_LO16:
10497 // Absolute in GOT.
10498 return Symbol::RELATIVE_REF;
10499
10500 case elfcpp::R_MIPS_TLS_DTPMOD32:
10501 case elfcpp::R_MIPS_TLS_DTPREL32:
10502 case elfcpp::R_MIPS_TLS_DTPMOD64:
10503 case elfcpp::R_MIPS_TLS_DTPREL64:
10504 case elfcpp::R_MIPS_TLS_GD:
10505 case elfcpp::R_MIPS_TLS_LDM:
10506 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10507 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10508 case elfcpp::R_MIPS_TLS_GOTTPREL:
10509 case elfcpp::R_MIPS_TLS_TPREL32:
10510 case elfcpp::R_MIPS_TLS_TPREL64:
10511 case elfcpp::R_MIPS_TLS_TPREL_HI16:
10512 case elfcpp::R_MIPS_TLS_TPREL_LO16:
10513 case elfcpp::R_MIPS16_TLS_GD:
10514 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10515 case elfcpp::R_MICROMIPS_TLS_GD:
10516 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10517 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
10518 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
10519 return Symbol::TLS_REF;
10520
10521 case elfcpp::R_MIPS_COPY:
10522 case elfcpp::R_MIPS_JUMP_SLOT:
10523 default:
10524 gold_unreachable();
10525 // Not expected. We will give an error later.
10526 return 0;
10527 }
10528}
10529
10530// Report an unsupported relocation against a local symbol.
10531
10532template<int size, bool big_endian>
10533void
10534Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
10535 Sized_relobj_file<size, big_endian>* object,
10536 unsigned int r_type)
10537{
10538 gold_error(_("%s: unsupported reloc %u against local symbol"),
10539 object->name().c_str(), r_type);
10540}
10541
10542// Report an unsupported relocation against a global symbol.
10543
10544template<int size, bool big_endian>
10545void
10546Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
10547 Sized_relobj_file<size, big_endian>* object,
10548 unsigned int r_type,
10549 Symbol* gsym)
10550{
10551 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
10552 object->name().c_str(), r_type, gsym->demangled_name().c_str());
10553}
10554
10555// Return printable name for ABI.
10556template<int size, bool big_endian>
10557const char*
10558Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags,
10559 unsigned char ei_class)
10560{
10561 switch (e_flags & elfcpp::EF_MIPS_ABI)
10562 {
10563 case 0:
10564 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
10565 return "N32";
10566 else if (elfcpp::abi_64(ei_class))
10567 return "64";
10568 else
10569 return "none";
10570 case elfcpp::E_MIPS_ABI_O32:
10571 return "O32";
10572 case elfcpp::E_MIPS_ABI_O64:
10573 return "O64";
10574 case elfcpp::E_MIPS_ABI_EABI32:
10575 return "EABI32";
10576 case elfcpp::E_MIPS_ABI_EABI64:
10577 return "EABI64";
10578 default:
10579 return "unknown abi";
10580 }
10581}
10582
10583template<int size, bool big_endian>
10584const char*
10585Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
10586{
10587 switch (e_flags & elfcpp::EF_MIPS_MACH)
10588 {
10589 case elfcpp::E_MIPS_MACH_3900:
10590 return "mips:3900";
10591 case elfcpp::E_MIPS_MACH_4010:
10592 return "mips:4010";
10593 case elfcpp::E_MIPS_MACH_4100:
10594 return "mips:4100";
10595 case elfcpp::E_MIPS_MACH_4111:
10596 return "mips:4111";
10597 case elfcpp::E_MIPS_MACH_4120:
10598 return "mips:4120";
10599 case elfcpp::E_MIPS_MACH_4650:
10600 return "mips:4650";
10601 case elfcpp::E_MIPS_MACH_5400:
10602 return "mips:5400";
10603 case elfcpp::E_MIPS_MACH_5500:
10604 return "mips:5500";
10605 case elfcpp::E_MIPS_MACH_SB1:
10606 return "mips:sb1";
10607 case elfcpp::E_MIPS_MACH_9000:
10608 return "mips:9000";
10609 case elfcpp::E_MIPS_MACH_LS2E:
10610 return "mips:loongson-2e";
10611 case elfcpp::E_MIPS_MACH_LS2F:
10612 return "mips:loongson-2f";
10613 case elfcpp::E_MIPS_MACH_LS3A:
10614 return "mips:loongson-3a";
10615 case elfcpp::E_MIPS_MACH_OCTEON:
10616 return "mips:octeon";
10617 case elfcpp::E_MIPS_MACH_OCTEON2:
10618 return "mips:octeon2";
10619 case elfcpp::E_MIPS_MACH_XLR:
10620 return "mips:xlr";
10621 default:
10622 switch (e_flags & elfcpp::EF_MIPS_ARCH)
10623 {
10624 default:
10625 case elfcpp::E_MIPS_ARCH_1:
10626 return "mips:3000";
10627
10628 case elfcpp::E_MIPS_ARCH_2:
10629 return "mips:6000";
10630
10631 case elfcpp::E_MIPS_ARCH_3:
10632 return "mips:4000";
10633
10634 case elfcpp::E_MIPS_ARCH_4:
10635 return "mips:8000";
10636
10637 case elfcpp::E_MIPS_ARCH_5:
10638 return "mips:mips5";
10639
10640 case elfcpp::E_MIPS_ARCH_32:
10641 return "mips:isa32";
10642
10643 case elfcpp::E_MIPS_ARCH_64:
10644 return "mips:isa64";
10645
10646 case elfcpp::E_MIPS_ARCH_32R2:
10647 return "mips:isa32r2";
10648
10649 case elfcpp::E_MIPS_ARCH_64R2:
10650 return "mips:isa64r2";
10651 }
10652 }
10653 return "unknown CPU";
10654}
10655
10656template<int size, bool big_endian>
62661c93 10657const Target::Target_info Target_mips<size, big_endian>::mips_info =
9810d34d
SS
10658{
10659 size, // size
10660 big_endian, // is_big_endian
10661 elfcpp::EM_MIPS, // machine_code
10662 true, // has_make_symbol
10663 false, // has_resolve
10664 false, // has_code_fill
10665 true, // is_default_stack_executable
10666 false, // can_icf_inline_merge_sections
10667 '\0', // wrap_char
10668 "/lib/ld.so.1", // dynamic_linker
10669 0x400000, // default_text_segment_address
10670 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
10671 4 * 1024, // common_pagesize (overridable by -z common-page-size)
10672 false, // isolate_execinstr
10673 0, // rosegment_gap
10674 elfcpp::SHN_UNDEF, // small_common_shndx
10675 elfcpp::SHN_UNDEF, // large_common_shndx
10676 0, // small_common_section_flags
10677 0, // large_common_section_flags
10678 NULL, // attributes_section
10679 NULL, // attributes_vendor
8d9743bd
MK
10680 "__start", // entry_symbol_name
10681 32, // hash_entry_size
9810d34d
SS
10682};
10683
62661c93
SS
10684template<int size, bool big_endian>
10685class Target_mips_nacl : public Target_mips<size, big_endian>
10686{
10687 public:
10688 Target_mips_nacl()
10689 : Target_mips<size, big_endian>(&mips_nacl_info)
10690 { }
10691
10692 private:
10693 static const Target::Target_info mips_nacl_info;
10694};
10695
10696template<int size, bool big_endian>
10697const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
10698{
10699 size, // size
10700 big_endian, // is_big_endian
10701 elfcpp::EM_MIPS, // machine_code
10702 true, // has_make_symbol
10703 false, // has_resolve
10704 false, // has_code_fill
10705 true, // is_default_stack_executable
10706 false, // can_icf_inline_merge_sections
10707 '\0', // wrap_char
10708 "/lib/ld.so.1", // dynamic_linker
10709 0x20000, // default_text_segment_address
10710 0x10000, // abi_pagesize (overridable by -z max-page-size)
10711 0x10000, // common_pagesize (overridable by -z common-page-size)
10712 true, // isolate_execinstr
10713 0x10000000, // rosegment_gap
10714 elfcpp::SHN_UNDEF, // small_common_shndx
10715 elfcpp::SHN_UNDEF, // large_common_shndx
10716 0, // small_common_section_flags
10717 0, // large_common_section_flags
10718 NULL, // attributes_section
10719 NULL, // attributes_vendor
8d9743bd
MK
10720 "_start", // entry_symbol_name
10721 32, // hash_entry_size
62661c93
SS
10722};
10723
10724// Target selector for Mips. Note this is never instantiated directly.
10725// It's only used in Target_selector_mips_nacl, below.
9810d34d
SS
10726
10727template<int size, bool big_endian>
10728class Target_selector_mips : public Target_selector
10729{
10730public:
10731 Target_selector_mips()
10732 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
10733 (size == 64 ?
10734 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
10735 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
10736 (size == 64 ?
10737 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
10738 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")))
10739 { }
10740
10741 Target* do_instantiate_target()
10742 { return new Target_mips<size, big_endian>(); }
10743};
10744
62661c93
SS
10745template<int size, bool big_endian>
10746class Target_selector_mips_nacl
10747 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
10748 Target_mips_nacl<size, big_endian> >
10749{
10750 public:
10751 Target_selector_mips_nacl()
10752 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
10753 Target_mips_nacl<size, big_endian> >(
10754 // NaCl currently supports only MIPS32 little-endian.
10755 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
10756 { }
10757};
9810d34d 10758
62661c93
SS
10759Target_selector_mips_nacl<32, true> target_selector_mips32;
10760Target_selector_mips_nacl<32, false> target_selector_mips32el;
10761Target_selector_mips_nacl<64, true> target_selector_mips64;
10762Target_selector_mips_nacl<64, false> target_selector_mips64el;
9810d34d
SS
10763
10764} // End anonymous namespace.
This page took 0.519459 seconds and 4 git commands to generate.