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