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