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