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