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