Constify arch_type and friends
[deliverable/binutils-gdb.git] / gold / mips.cc
CommitLineData
9810d34d
SS
1// mips.cc -- mips target support for gold.
2
6f2750fe 3// Copyright (C) 2011-2016 Free Software Foundation, Inc.
9810d34d
SS
4// Written by Sasa Stankovic <sasa.stankovic@imgtec.com>
5// and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
6// This file contains borrowed and adapted code from bfd/elfxx-mips.c.
7
8// This file is part of gold.
9
10// This program is free software; you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation; either version 3 of the License, or
13// (at your option) any later version.
14
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23// MA 02110-1301, USA.
24
25#include "gold.h"
26
27#include <algorithm>
28#include <set>
29#include <sstream>
30#include "demangle.h"
31
32#include "elfcpp.h"
33#include "parameters.h"
34#include "reloc.h"
35#include "mips.h"
36#include "object.h"
37#include "symtab.h"
38#include "layout.h"
39#include "output.h"
40#include "copy-relocs.h"
41#include "target.h"
42#include "target-reloc.h"
43#include "target-select.h"
44#include "tls.h"
45#include "errors.h"
46#include "gc.h"
62661c93 47#include "nacl.h"
9810d34d
SS
48
49namespace
50{
51using namespace gold;
52
53template<int size, bool big_endian>
54class Mips_output_data_plt;
55
56template<int size, bool big_endian>
57class Mips_output_data_got;
58
59template<int size, bool big_endian>
60class Target_mips;
61
62template<int size, bool big_endian>
63class Mips_output_section_reginfo;
64
65template<int size, bool big_endian>
66class Mips_output_data_la25_stub;
67
68template<int size, bool big_endian>
69class Mips_output_data_mips_stubs;
70
71template<int size>
72class Mips_symbol;
73
74template<int size, bool big_endian>
75class Mips_got_info;
76
77template<int size, bool big_endian>
78class Mips_relobj;
79
80class Mips16_stub_section_base;
81
82template<int size, bool big_endian>
83class Mips16_stub_section;
84
85// The ABI says that every symbol used by dynamic relocations must have
86// a global GOT entry. Among other things, this provides the dynamic
87// linker with a free, directly-indexed cache. The GOT can therefore
88// contain symbols that are not referenced by GOT relocations themselves
89// (in other words, it may have symbols that are not referenced by things
90// like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
91
92// GOT relocations are less likely to overflow if we put the associated
93// GOT entries towards the beginning. We therefore divide the global
94// GOT entries into two areas: "normal" and "reloc-only". Entries in
95// the first area can be used for both dynamic relocations and GP-relative
96// accesses, while those in the "reloc-only" area are for dynamic
97// relocations only.
98
99// These GGA_* ("Global GOT Area") values are organised so that lower
100// values are more general than higher values. Also, non-GGA_NONE
101// values are ordered by the position of the area in the GOT.
102
103enum Global_got_area
104{
105 GGA_NORMAL = 0,
106 GGA_RELOC_ONLY = 1,
107 GGA_NONE = 2
108};
109
110// The types of GOT entries needed for this platform.
111// These values are exposed to the ABI in an incremental link.
112// Do not renumber existing values without changing the version
113// number of the .gnu_incremental_inputs section.
114enum Got_type
115{
116 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
117 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
118 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
119
120 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
121 GOT_TYPE_STANDARD_MULTIGOT = 3,
122 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
123 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
124};
125
126// TLS type of GOT entry.
127enum Got_tls_type
128{
129 GOT_TLS_NONE = 0,
130 GOT_TLS_GD = 1,
131 GOT_TLS_LDM = 2,
132 GOT_TLS_IE = 4
133};
134
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
15eb1beb
VR
423// (sym != NULL, symndx == -1)
424// (3) a TLS LDM slot (there's only one of these per GOT.)
9810d34d
SS
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 435 unsigned int shndx, bool is_section_symbol)
15eb1beb 436 : addend_(addend), symndx_(symndx), tls_type_(tls_type),
47a9f4fc 437 is_section_symbol_(is_section_symbol), shndx_(shndx)
15eb1beb 438 { this->d.object = object; }
9810d34d 439
15eb1beb
VR
440 Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
441 : addend_(0), symndx_(-1U), tls_type_(tls_type),
47a9f4fc 442 is_section_symbol_(false), shndx_(-1U)
9810d34d
SS
443 { this->d.sym = sym; }
444
445 // Return whether this entry is for a local symbol.
446 bool
447 is_for_local_symbol() const
448 { return this->symndx_ != -1U; }
449
450 // Return whether this entry is for a global symbol.
451 bool
452 is_for_global_symbol() const
453 { return this->symndx_ == -1U; }
454
455 // Return the hash of this entry.
456 size_t
457 hash() const
458 {
459 if (this->tls_type_ == GOT_TLS_LDM)
460 return this->symndx_ + (1 << 18);
15eb1beb
VR
461
462 size_t name_hash_value = gold::string_hash<char>(
463 (this->symndx_ != -1U)
464 ? this->d.object->name().c_str()
465 : this->d.sym->name());
466 size_t addend = this->addend_;
467 return name_hash_value ^ this->symndx_ ^ addend;
9810d34d
SS
468 }
469
470 // Return whether this entry is equal to OTHER.
471 bool
472 equals(Mips_got_entry<size, big_endian>* other) const
473 {
9810d34d
SS
474 if (this->tls_type_ == GOT_TLS_LDM)
475 return true;
15eb1beb
VR
476
477 return ((this->tls_type_ == other->tls_type_)
478 && (this->symndx_ == other->symndx_)
479 && ((this->symndx_ != -1U)
480 ? (this->d.object == other->d.object)
481 : (this->d.sym == other->d.sym))
482 && (this->addend_ == other->addend_));
9810d34d
SS
483 }
484
485 // Return input object that needs this GOT entry.
486 Mips_relobj<size, big_endian>*
487 object() const
488 {
15eb1beb
VR
489 gold_assert(this->symndx_ != -1U);
490 return this->d.object;
9810d34d
SS
491 }
492
493 // Return local symbol index for local GOT entries.
494 unsigned int
495 symndx() const
496 {
497 gold_assert(this->symndx_ != -1U);
498 return this->symndx_;
499 }
500
501 // Return the relocation addend for local GOT entries.
502 Mips_address
503 addend() const
15eb1beb 504 { return this->addend_; }
9810d34d
SS
505
506 // Return global symbol for global GOT entries.
507 Mips_symbol<size>*
508 sym() const
509 {
510 gold_assert(this->symndx_ == -1U);
511 return this->d.sym;
512 }
513
514 // Return whether this is a TLS GOT entry.
515 bool
516 is_tls_entry() const
517 { return this->tls_type_ != GOT_TLS_NONE; }
518
519 // Return TLS type of this GOT entry.
520 unsigned char
521 tls_type() const
522 { return this->tls_type_; }
523
524 // Return section index of the local symbol for local GOT entries.
525 unsigned int
526 shndx() const
527 { return this->shndx_; }
528
47a9f4fc
VR
529 // Return whether this is a STT_SECTION symbol.
530 bool
531 is_section_symbol() const
532 { return this->is_section_symbol_; }
533
9810d34d 534 private:
15eb1beb
VR
535 // The addend.
536 Mips_address addend_;
537
9810d34d
SS
538 // The index of the symbol if we have a local symbol; -1 otherwise.
539 unsigned int symndx_;
540
541 union
542 {
15eb1beb
VR
543 // The input object for local symbols that needs the GOT entry.
544 Mips_relobj<size, big_endian>* object;
9810d34d
SS
545 // If symndx == -1, the global symbol corresponding to this GOT entry. The
546 // symbol's entry is in the local area if mips_sym->global_got_area is
547 // GGA_NONE, otherwise it is in the global area.
548 Mips_symbol<size>* sym;
549 } d;
550
551 // The TLS type of this GOT entry. An LDM GOT entry will be a local
552 // symbol entry with r_symndx == 0.
553 unsigned char tls_type_;
554
47a9f4fc
VR
555 // Whether this is a STT_SECTION symbol.
556 bool is_section_symbol_;
557
9810d34d
SS
558 // For local GOT entries, section index of the local symbol.
559 unsigned int shndx_;
560};
561
562// Hash for Mips_got_entry.
563
564template<int size, bool big_endian>
565class Mips_got_entry_hash
566{
567 public:
568 size_t
569 operator()(Mips_got_entry<size, big_endian>* entry) const
570 { return entry->hash(); }
571};
572
573// Equality for Mips_got_entry.
574
575template<int size, bool big_endian>
576class Mips_got_entry_eq
577{
578 public:
579 bool
580 operator()(Mips_got_entry<size, big_endian>* e1,
581 Mips_got_entry<size, big_endian>* e2) const
582 { return e1->equals(e2); }
583};
584
15eb1beb
VR
585// Hash for Mips_symbol.
586
587template<int size>
588class Mips_symbol_hash
589{
590 public:
591 size_t
592 operator()(Mips_symbol<size>* sym) const
593 { return sym->hash(); }
594};
595
9810d34d
SS
596// Got_page_range. This class describes a range of addends: [MIN_ADDEND,
597// MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
598// increasing MIN_ADDEND.
599
600struct Got_page_range
601{
602 Got_page_range()
603 : next(NULL), min_addend(0), max_addend(0)
604 { }
605
606 Got_page_range* next;
607 int min_addend;
608 int max_addend;
609
610 // Return the maximum number of GOT page entries required.
611 int
612 get_max_pages()
613 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
614};
615
616// Got_page_entry. This class describes the range of addends that are applied
617// to page relocations against a given symbol.
618
619struct Got_page_entry
620{
621 Got_page_entry()
622 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
623 { }
624
625 Got_page_entry(Object* object_, unsigned int symndx_)
626 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
627 { }
628
629 // The input object that needs the GOT page entry.
630 Object* object;
631 // The index of the symbol, as stored in the relocation r_info.
632 unsigned int symndx;
633 // The ranges for this page entry.
634 Got_page_range* ranges;
635 // The maximum number of page entries needed for RANGES.
636 unsigned int num_pages;
637};
638
639// Hash for Got_page_entry.
640
641struct Got_page_entry_hash
642{
643 size_t
644 operator()(Got_page_entry* entry) const
645 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
646};
647
648// Equality for Got_page_entry.
649
650struct Got_page_entry_eq
651{
652 bool
653 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
654 {
655 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
656 }
657};
658
659// This class is used to hold .got information when linking.
660
661template<int size, bool big_endian>
662class Mips_got_info
663{
664 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
665 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
666 Reloc_section;
667 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
668
669 // Unordered set of GOT entries.
670 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
671 Mips_got_entry_hash<size, big_endian>,
672 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
673
674 // Unordered set of GOT page entries.
675 typedef Unordered_set<Got_page_entry*,
676 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
677
15eb1beb
VR
678 // Unordered set of global GOT entries.
679 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
680 Global_got_entry_set;
681
9810d34d
SS
682 public:
683 Mips_got_info()
684 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
685 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
686 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
687 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
688 offset_(0)
689 { }
690
691 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
692 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
693 void
694 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
695 unsigned int symndx, Mips_address addend,
47a9f4fc
VR
696 unsigned int r_type, unsigned int shndx,
697 bool is_section_symbol);
9810d34d
SS
698
699 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
700 // in OBJECT. FOR_CALL is true if the caller is only interested in
701 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
702 // relocation.
703 void
704 record_global_got_symbol(Mips_symbol<size>* mips_sym,
705 Mips_relobj<size, big_endian>* object,
706 unsigned int r_type, bool dyn_reloc, bool for_call);
707
708 // Add ENTRY to master GOT and to OBJECT's GOT.
709 void
710 record_got_entry(Mips_got_entry<size, big_endian>* entry,
711 Mips_relobj<size, big_endian>* object);
712
713 // Record that OBJECT has a page relocation against symbol SYMNDX and
714 // that ADDEND is the addend for that relocation.
715 void
716 record_got_page_entry(Mips_relobj<size, big_endian>* object,
717 unsigned int symndx, int addend);
718
719 // Create all entries that should be in the local part of the GOT.
720 void
721 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
722
723 // Create GOT page entries.
724 void
725 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
726
727 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
728 void
729 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
730 unsigned int non_reloc_only_global_gotno);
731
732 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
733 void
734 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
735
736 // Create TLS GOT entries.
737 void
738 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
739
740 // Decide whether the symbol needs an entry in the global part of the primary
741 // GOT, setting global_got_area accordingly. Count the number of global
742 // symbols that are in the primary GOT only because they have dynamic
743 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
744 void
745 count_got_symbols(Symbol_table* symtab);
746
747 // Return the offset of GOT page entry for VALUE.
748 unsigned int
749 get_got_page_offset(Mips_address value,
750 Mips_output_data_got<size, big_endian>* got);
751
752 // Count the number of GOT entries required.
753 void
754 count_got_entries();
755
756 // Count the number of GOT entries required by ENTRY. Accumulate the result.
757 void
758 count_got_entry(Mips_got_entry<size, big_endian>* entry);
759
760 // Add FROM's GOT entries.
761 void
762 add_got_entries(Mips_got_info<size, big_endian>* from);
763
764 // Add FROM's GOT page entries.
765 void
766 add_got_page_entries(Mips_got_info<size, big_endian>* from);
767
768 // Return GOT size.
769 unsigned int
770 got_size() const
771 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
772 + this->tls_gotno_) * size/8);
773 }
774
775 // Return the number of local GOT entries.
776 unsigned int
777 local_gotno() const
778 { return this->local_gotno_; }
779
780 // Return the maximum number of page GOT entries needed.
781 unsigned int
782 page_gotno() const
783 { return this->page_gotno_; }
784
785 // Return the number of global GOT entries.
786 unsigned int
787 global_gotno() const
788 { return this->global_gotno_; }
789
790 // Set the number of global GOT entries.
791 void
792 set_global_gotno(unsigned int global_gotno)
793 { this->global_gotno_ = global_gotno; }
794
795 // Return the number of GGA_RELOC_ONLY global GOT entries.
796 unsigned int
797 reloc_only_gotno() const
798 { return this->reloc_only_gotno_; }
799
800 // Return the number of TLS GOT entries.
801 unsigned int
802 tls_gotno() const
803 { return this->tls_gotno_; }
804
805 // Return the GOT type for this GOT. Used for multi-GOT links only.
806 unsigned int
807 multigot_got_type(unsigned int got_type) const
808 {
809 switch (got_type)
810 {
811 case GOT_TYPE_STANDARD:
812 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
813 case GOT_TYPE_TLS_OFFSET:
814 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
815 case GOT_TYPE_TLS_PAIR:
816 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
817 default:
818 gold_unreachable();
819 }
820 }
821
822 // Remove lazy-binding stubs for global symbols in this GOT.
823 void
824 remove_lazy_stubs(Target_mips<size, big_endian>* target);
825
826 // Return offset of this GOT from the start of .got section.
827 unsigned int
828 offset() const
829 { return this->offset_; }
830
831 // Set offset of this GOT from the start of .got section.
832 void
833 set_offset(unsigned int offset)
834 { this->offset_ = offset; }
835
836 // Set index of this GOT in multi-GOT links.
837 void
838 set_index(unsigned int index)
839 { this->index_ = index; }
840
841 // Return next GOT in multi-GOT links.
842 Mips_got_info<size, big_endian>*
843 next() const
844 { return this->next_; }
845
846 // Set next GOT in multi-GOT links.
847 void
848 set_next(Mips_got_info<size, big_endian>* next)
849 { this->next_ = next; }
850
851 // Return the offset of TLS LDM entry for this GOT.
852 unsigned int
853 tls_ldm_offset() const
854 { return this->tls_ldm_offset_; }
855
856 // Set the offset of TLS LDM entry for this GOT.
857 void
858 set_tls_ldm_offset(unsigned int tls_ldm_offset)
859 { this->tls_ldm_offset_ = tls_ldm_offset; }
860
15eb1beb 861 Global_got_entry_set&
9810d34d
SS
862 global_got_symbols()
863 { return this->global_got_symbols_; }
864
865 // Return the GOT_TLS_* type required by relocation type R_TYPE.
866 static int
867 mips_elf_reloc_tls_type(unsigned int r_type)
868 {
869 if (tls_gd_reloc(r_type))
870 return GOT_TLS_GD;
871
872 if (tls_ldm_reloc(r_type))
873 return GOT_TLS_LDM;
874
875 if (tls_gottprel_reloc(r_type))
876 return GOT_TLS_IE;
877
878 return GOT_TLS_NONE;
879 }
880
881 // Return the number of GOT slots needed for GOT TLS type TYPE.
882 static int
883 mips_tls_got_entries(unsigned int type)
884 {
885 switch (type)
886 {
887 case GOT_TLS_GD:
888 case GOT_TLS_LDM:
889 return 2;
890
891 case GOT_TLS_IE:
892 return 1;
893
894 case GOT_TLS_NONE:
895 return 0;
896
897 default:
898 gold_unreachable();
899 }
900 }
901
902 private:
903 // The number of local GOT entries.
904 unsigned int local_gotno_;
905 // The maximum number of page GOT entries needed.
906 unsigned int page_gotno_;
907 // The number of global GOT entries.
908 unsigned int global_gotno_;
909 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
910 unsigned int reloc_only_gotno_;
911 // The number of TLS GOT entries.
912 unsigned int tls_gotno_;
913 // The offset of TLS LDM entry for this GOT.
914 unsigned int tls_ldm_offset_;
915 // All symbols that have global GOT entry.
15eb1beb 916 Global_got_entry_set global_got_symbols_;
9810d34d
SS
917 // A hash table holding GOT entries.
918 Got_entry_set got_entries_;
919 // A hash table of GOT page entries.
920 Got_page_entry_set got_page_entries_;
921 // The offset of first GOT page entry for this GOT.
922 unsigned int got_page_offset_start_;
923 // The offset of next available GOT page entry for this GOT.
924 unsigned int got_page_offset_next_;
925 // A hash table that maps GOT page entry value to the GOT offset where
926 // the entry is located.
927 Got_page_offsets got_page_offsets_;
928 // In multi-GOT links, a pointer to the next GOT.
929 Mips_got_info<size, big_endian>* next_;
930 // Index of this GOT in multi-GOT links.
931 unsigned int index_;
932 // The offset of this GOT in multi-GOT links.
933 unsigned int offset_;
934};
935
936// This is a helper class used during relocation scan. It records GOT16 addend.
937
938template<int size, bool big_endian>
939struct got16_addend
940{
941 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
942
943 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
944 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
945 Mips_address _addend)
946 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
947 addend(_addend)
948 { }
949
950 const Sized_relobj_file<size, big_endian>* object;
951 unsigned int shndx;
952 unsigned int r_type;
953 unsigned int r_sym;
954 Mips_address addend;
955};
956
957// Mips_symbol class. Holds additional symbol information needed for Mips.
958
959template<int size>
960class Mips_symbol : public Sized_symbol<size>
961{
962 public:
963 Mips_symbol()
964 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
965 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
966 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
967 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
968 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
969 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
970 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
971 { }
972
973 // Return whether this is a MIPS16 symbol.
974 bool
975 is_mips16() const
976 {
977 // (st_other & STO_MIPS16) == STO_MIPS16
978 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
979 == elfcpp::STO_MIPS16 >> 2);
980 }
981
982 // Return whether this is a microMIPS symbol.
983 bool
984 is_micromips() const
985 {
986 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
987 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
988 == elfcpp::STO_MICROMIPS >> 2);
989 }
990
991 // Return whether the symbol needs MIPS16 fn_stub.
992 bool
993 need_fn_stub() const
994 { return this->need_fn_stub_; }
995
996 // Set that the symbol needs MIPS16 fn_stub.
997 void
998 set_need_fn_stub()
999 { this->need_fn_stub_ = true; }
1000
1001 // Return whether this symbol is referenced by branch relocations from
1002 // any non-PIC input file.
1003 bool
1004 has_nonpic_branches() const
1005 { return this->has_nonpic_branches_; }
1006
1007 // Set that this symbol is referenced by branch relocations from
1008 // any non-PIC input file.
1009 void
1010 set_has_nonpic_branches()
1011 { this->has_nonpic_branches_ = true; }
1012
1013 // Return the offset of the la25 stub for this symbol from the start of the
1014 // la25 stub section.
1015 unsigned int
1016 la25_stub_offset() const
1017 { return this->la25_stub_offset_; }
1018
1019 // Set the offset of the la25 stub for this symbol from the start of the
1020 // la25 stub section.
1021 void
1022 set_la25_stub_offset(unsigned int offset)
1023 { this->la25_stub_offset_ = offset; }
1024
1025 // Return whether the symbol has la25 stub. This is true if this symbol is
1026 // for a PIC function, and there are non-PIC branches and jumps to it.
1027 bool
1028 has_la25_stub() const
1029 { return this->la25_stub_offset_ != -1U; }
1030
1031 // Return whether there is a relocation against this symbol that must be
1032 // resolved by the static linker (that is, the relocation cannot possibly
1033 // be made dynamic).
1034 bool
1035 has_static_relocs() const
1036 { return this->has_static_relocs_; }
1037
1038 // Set that there is a relocation against this symbol that must be resolved
1039 // by the static linker (that is, the relocation cannot possibly be made
1040 // dynamic).
1041 void
1042 set_has_static_relocs()
1043 { this->has_static_relocs_ = true; }
1044
1045 // Return whether we must not create a lazy-binding stub for this symbol.
1046 bool
1047 no_lazy_stub() const
1048 { return this->no_lazy_stub_; }
1049
1050 // Set that we must not create a lazy-binding stub for this symbol.
1051 void
1052 set_no_lazy_stub()
1053 { this->no_lazy_stub_ = true; }
1054
1055 // Return the offset of the lazy-binding stub for this symbol from the start
1056 // of .MIPS.stubs section.
1057 unsigned int
1058 lazy_stub_offset() const
1059 { return this->lazy_stub_offset_; }
1060
1061 // Set the offset of the lazy-binding stub for this symbol from the start
1062 // of .MIPS.stubs section.
1063 void
1064 set_lazy_stub_offset(unsigned int offset)
1065 { this->lazy_stub_offset_ = offset; }
1066
1067 // Return whether there are any relocations for this symbol where
1068 // pointer equality matters.
1069 bool
1070 pointer_equality_needed() const
1071 { return this->pointer_equality_needed_; }
1072
1073 // Set that there are relocations for this symbol where pointer equality
1074 // matters.
1075 void
1076 set_pointer_equality_needed()
1077 { this->pointer_equality_needed_ = true; }
1078
1079 // Return global GOT area where this symbol in located.
1080 Global_got_area
1081 global_got_area() const
1082 { return this->global_got_area_; }
1083
1084 // Set global GOT area where this symbol in located.
1085 void
1086 set_global_got_area(Global_got_area global_got_area)
1087 { this->global_got_area_ = global_got_area; }
1088
1089 // Return the global GOT offset for this symbol. For multi-GOT links, this
1090 // returns the offset from the start of .got section to the first GOT entry
1091 // for the symbol. Note that in multi-GOT links the symbol can have entry
1092 // in more than one GOT.
1093 unsigned int
1094 global_gotoffset() const
1095 { return this->global_gotoffset_; }
1096
1097 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1098 // the symbol can have entry in more than one GOT. This method will set
1099 // the offset only if it is less than current offset.
1100 void
1101 set_global_gotoffset(unsigned int offset)
1102 {
1103 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1104 this->global_gotoffset_ = offset;
1105 }
1106
1107 // Return whether all GOT relocations for this symbol are for calls.
1108 bool
1109 got_only_for_calls() const
1110 { return this->got_only_for_calls_; }
1111
1112 // Set that there is a GOT relocation for this symbol that is not for call.
1113 void
1114 set_got_not_only_for_calls()
1115 { this->got_only_for_calls_ = false; }
1116
1117 // Return whether this is a PIC symbol.
1118 bool
1119 is_pic() const
1120 {
1121 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1122 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1123 == (elfcpp::STO_MIPS_PIC >> 2));
1124 }
1125
1126 // Set the flag in st_other field that marks this symbol as PIC.
1127 void
1128 set_pic()
1129 {
1130 if (this->is_mips16())
1131 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1132 this->set_nonvis((this->nonvis()
1133 & ~((elfcpp::STO_MIPS16 >> 2)
1134 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1135 | (elfcpp::STO_MIPS_PIC >> 2));
1136 else
1137 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1138 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1139 | (elfcpp::STO_MIPS_PIC >> 2));
1140 }
1141
1142 // Set the flag in st_other field that marks this symbol as PLT.
1143 void
1144 set_mips_plt()
1145 {
1146 if (this->is_mips16())
1147 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1148 this->set_nonvis((this->nonvis()
1149 & ((elfcpp::STO_MIPS16 >> 2)
1150 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1151 | (elfcpp::STO_MIPS_PLT >> 2));
1152
1153 else
1154 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1155 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1156 | (elfcpp::STO_MIPS_PLT >> 2));
1157 }
1158
1159 // Downcast a base pointer to a Mips_symbol pointer.
1160 static Mips_symbol<size>*
1161 as_mips_sym(Symbol* sym)
1162 { return static_cast<Mips_symbol<size>*>(sym); }
1163
1164 // Downcast a base pointer to a Mips_symbol pointer.
1165 static const Mips_symbol<size>*
1166 as_mips_sym(const Symbol* sym)
1167 { return static_cast<const Mips_symbol<size>*>(sym); }
1168
1169 // Return whether the symbol has lazy-binding stub.
1170 bool
1171 has_lazy_stub() const
1172 { return this->has_lazy_stub_; }
1173
1174 // Set whether the symbol has lazy-binding stub.
1175 void
1176 set_has_lazy_stub(bool has_lazy_stub)
1177 { this->has_lazy_stub_ = has_lazy_stub; }
1178
1179 // Return whether the symbol needs a standard PLT entry.
1180 bool
1181 needs_mips_plt() const
1182 { return this->needs_mips_plt_; }
1183
1184 // Set whether the symbol needs a standard PLT entry.
1185 void
1186 set_needs_mips_plt(bool needs_mips_plt)
1187 { this->needs_mips_plt_ = needs_mips_plt; }
1188
1189 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1190 // entry.
1191 bool
1192 needs_comp_plt() const
1193 { return this->needs_comp_plt_; }
1194
1195 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1196 void
1197 set_needs_comp_plt(bool needs_comp_plt)
1198 { this->needs_comp_plt_ = needs_comp_plt; }
1199
1200 // Return standard PLT entry offset, or -1 if none.
1201 unsigned int
1202 mips_plt_offset() const
1203 { return this->mips_plt_offset_; }
1204
1205 // Set standard PLT entry offset.
1206 void
1207 set_mips_plt_offset(unsigned int mips_plt_offset)
1208 { this->mips_plt_offset_ = mips_plt_offset; }
1209
1210 // Return whether the symbol has standard PLT entry.
1211 bool
1212 has_mips_plt_offset() const
1213 { return this->mips_plt_offset_ != -1U; }
1214
1215 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1216 unsigned int
1217 comp_plt_offset() const
1218 { return this->comp_plt_offset_; }
1219
1220 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1221 void
1222 set_comp_plt_offset(unsigned int comp_plt_offset)
1223 { this->comp_plt_offset_ = comp_plt_offset; }
1224
1225 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1226 bool
1227 has_comp_plt_offset() const
1228 { return this->comp_plt_offset_ != -1U; }
1229
1230 // Return MIPS16 fn stub for a symbol.
1231 template<bool big_endian>
1232 Mips16_stub_section<size, big_endian>*
1233 get_mips16_fn_stub() const
1234 {
1235 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1236 }
1237
1238 // Set MIPS16 fn stub for a symbol.
1239 void
1240 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1241 { this->mips16_fn_stub_ = stub; }
1242
1243 // Return whether symbol has MIPS16 fn stub.
1244 bool
1245 has_mips16_fn_stub() const
1246 { return this->mips16_fn_stub_ != NULL; }
1247
1248 // Return MIPS16 call stub for a symbol.
1249 template<bool big_endian>
1250 Mips16_stub_section<size, big_endian>*
1251 get_mips16_call_stub() const
1252 {
1253 return static_cast<Mips16_stub_section<size, big_endian>*>(
1254 mips16_call_stub_);
1255 }
1256
1257 // Set MIPS16 call stub for a symbol.
1258 void
1259 set_mips16_call_stub(Mips16_stub_section_base* stub)
1260 { this->mips16_call_stub_ = stub; }
1261
1262 // Return whether symbol has MIPS16 call stub.
1263 bool
1264 has_mips16_call_stub() const
1265 { return this->mips16_call_stub_ != NULL; }
1266
1267 // Return MIPS16 call_fp stub for a symbol.
1268 template<bool big_endian>
1269 Mips16_stub_section<size, big_endian>*
1270 get_mips16_call_fp_stub() const
1271 {
1272 return static_cast<Mips16_stub_section<size, big_endian>*>(
1273 mips16_call_fp_stub_);
1274 }
1275
1276 // Set MIPS16 call_fp stub for a symbol.
1277 void
1278 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1279 { this->mips16_call_fp_stub_ = stub; }
1280
1281 // Return whether symbol has MIPS16 call_fp stub.
1282 bool
1283 has_mips16_call_fp_stub() const
1284 { return this->mips16_call_fp_stub_ != NULL; }
1285
1286 bool
1287 get_applied_secondary_got_fixup() const
1288 { return applied_secondary_got_fixup_; }
1289
1290 void
1291 set_applied_secondary_got_fixup()
1292 { this->applied_secondary_got_fixup_ = true; }
1293
15eb1beb
VR
1294 // Return the hash of this symbol.
1295 size_t
1296 hash() const
1297 {
1298 return gold::string_hash<char>(this->name());
1299 }
1300
9810d34d
SS
1301 private:
1302 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1303 // appears in any relocs other than a 16 bit call.
1304 bool need_fn_stub_;
1305
1306 // True if this symbol is referenced by branch relocations from
1307 // any non-PIC input file. This is used to determine whether an
1308 // la25 stub is required.
1309 bool has_nonpic_branches_;
1310
1311 // The offset of the la25 stub for this symbol from the start of the
1312 // la25 stub section.
1313 unsigned int la25_stub_offset_;
1314
1315 // True if there is a relocation against this symbol that must be
1316 // resolved by the static linker (that is, the relocation cannot
1317 // possibly be made dynamic).
1318 bool has_static_relocs_;
1319
1320 // Whether we must not create a lazy-binding stub for this symbol.
1321 // This is true if the symbol has relocations related to taking the
1322 // function's address.
1323 bool no_lazy_stub_;
1324
1325 // The offset of the lazy-binding stub for this symbol from the start of
1326 // .MIPS.stubs section.
1327 unsigned int lazy_stub_offset_;
1328
1329 // True if there are any relocations for this symbol where pointer equality
1330 // matters.
1331 bool pointer_equality_needed_;
1332
1333 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1334 // in the global part of the GOT.
1335 Global_got_area global_got_area_;
1336
1337 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1338 // from the start of .got section to the first GOT entry for the symbol.
1339 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1340 unsigned int global_gotoffset_;
1341
1342 // Whether all GOT relocations for this symbol are for calls.
1343 bool got_only_for_calls_;
1344 // Whether the symbol has lazy-binding stub.
1345 bool has_lazy_stub_;
1346 // Whether the symbol needs a standard PLT entry.
1347 bool needs_mips_plt_;
1348 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1349 bool needs_comp_plt_;
1350 // Standard PLT entry offset, or -1 if none.
1351 unsigned int mips_plt_offset_;
1352 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1353 unsigned int comp_plt_offset_;
1354 // MIPS16 fn stub for a symbol.
1355 Mips16_stub_section_base* mips16_fn_stub_;
1356 // MIPS16 call stub for a symbol.
1357 Mips16_stub_section_base* mips16_call_stub_;
1358 // MIPS16 call_fp stub for a symbol.
1359 Mips16_stub_section_base* mips16_call_fp_stub_;
1360
1361 bool applied_secondary_got_fixup_;
1362};
1363
1364// Mips16_stub_section class.
1365
1366// The mips16 compiler uses a couple of special sections to handle
1367// floating point arguments.
1368
1369// Section names that look like .mips16.fn.FNNAME contain stubs that
1370// copy floating point arguments from the fp regs to the gp regs and
1371// then jump to FNNAME. If any 32 bit function calls FNNAME, the
1372// call should be redirected to the stub instead. If no 32 bit
1373// function calls FNNAME, the stub should be discarded. We need to
1374// consider any reference to the function, not just a call, because
1375// if the address of the function is taken we will need the stub,
1376// since the address might be passed to a 32 bit function.
1377
1378// Section names that look like .mips16.call.FNNAME contain stubs
1379// that copy floating point arguments from the gp regs to the fp
1380// regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1381// then any 16 bit function that calls FNNAME should be redirected
1382// to the stub instead. If FNNAME is not a 32 bit function, the
1383// stub should be discarded.
1384
1385// .mips16.call.fp.FNNAME sections are similar, but contain stubs
1386// which call FNNAME and then copy the return value from the fp regs
1387// to the gp regs. These stubs store the return address in $18 while
1388// calling FNNAME; any function which might call one of these stubs
1389// must arrange to save $18 around the call. (This case is not
1390// needed for 32 bit functions that call 16 bit functions, because
1391// 16 bit functions always return floating point values in both
1392// $f0/$f1 and $2/$3.)
1393
1394// Note that in all cases FNNAME might be defined statically.
1395// Therefore, FNNAME is not used literally. Instead, the relocation
1396// information will indicate which symbol the section is for.
1397
1398// We record any stubs that we find in the symbol table.
1399
1400// TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1401
1402class Mips16_stub_section_base { };
1403
1404template<int size, bool big_endian>
1405class Mips16_stub_section : public Mips16_stub_section_base
1406{
1407 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1408
1409 public:
1410 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1411 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1412 found_r_mips_none_(false)
1413 {
1414 gold_assert(object->is_mips16_fn_stub_section(shndx)
1415 || object->is_mips16_call_stub_section(shndx)
1416 || object->is_mips16_call_fp_stub_section(shndx));
1417 }
1418
1419 // Return the object of this stub section.
1420 Mips_relobj<size, big_endian>*
1421 object() const
1422 { return this->object_; }
1423
1424 // Return the size of a section.
1425 uint64_t
1426 section_size() const
1427 { return this->object_->section_size(this->shndx_); }
1428
1429 // Return section index of this stub section.
1430 unsigned int
1431 shndx() const
1432 { return this->shndx_; }
1433
1434 // Return symbol index, if stub is for a local function.
1435 unsigned int
1436 r_sym() const
1437 { return this->r_sym_; }
1438
1439 // Return symbol, if stub is for a global function.
1440 Mips_symbol<size>*
1441 gsym() const
1442 { return this->gsym_; }
1443
1444 // Return whether stub is for a local function.
1445 bool
1446 is_for_local_function() const
1447 { return this->gsym_ == NULL; }
1448
1449 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1450 // is found in the stub section. Try to find stub target.
1451 void
1452 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1453 {
1454 // To find target symbol for this stub, trust the first R_MIPS_NONE
1455 // relocation, if any. Otherwise trust the first relocation, whatever
1456 // its kind.
1457 if (this->found_r_mips_none_)
1458 return;
1459 if (r_type == elfcpp::R_MIPS_NONE)
1460 {
1461 this->r_sym_ = r_sym;
1462 this->gsym_ = NULL;
1463 this->found_r_mips_none_ = true;
1464 }
1465 else if (!is_target_found())
1466 this->r_sym_ = r_sym;
1467 }
1468
1469 // This method is called when a new relocation R_TYPE for global symbol GSYM
1470 // is found in the stub section. Try to find stub target.
1471 void
1472 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1473 {
1474 // To find target symbol for this stub, trust the first R_MIPS_NONE
1475 // relocation, if any. Otherwise trust the first relocation, whatever
1476 // its kind.
1477 if (this->found_r_mips_none_)
1478 return;
1479 if (r_type == elfcpp::R_MIPS_NONE)
1480 {
1481 this->gsym_ = gsym;
1482 this->r_sym_ = 0;
1483 this->found_r_mips_none_ = true;
1484 }
1485 else if (!is_target_found())
1486 this->gsym_ = gsym;
1487 }
1488
1489 // Return whether we found the stub target.
1490 bool
1491 is_target_found() const
1492 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1493
1494 // Return whether this is a fn stub.
1495 bool
1496 is_fn_stub() const
1497 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1498
1499 // Return whether this is a call stub.
1500 bool
1501 is_call_stub() const
1502 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1503
1504 // Return whether this is a call_fp stub.
1505 bool
1506 is_call_fp_stub() const
1507 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1508
1509 // Return the output address.
1510 Mips_address
1511 output_address() const
1512 {
1513 return (this->object_->output_section(this->shndx_)->address()
1514 + this->object_->output_section_offset(this->shndx_));
1515 }
1516
1517 private:
1518 // The object of this stub section.
1519 Mips_relobj<size, big_endian>* object_;
1520 // The section index of this stub section.
1521 unsigned int shndx_;
1522 // The symbol index, if stub is for a local function.
1523 unsigned int r_sym_;
1524 // The symbol, if stub is for a global function.
1525 Mips_symbol<size>* gsym_;
1526 // True if we found R_MIPS_NONE relocation in this stub.
1527 bool found_r_mips_none_;
1528};
1529
1530// Mips_relobj class.
1531
1532template<int size, bool big_endian>
1533class Mips_relobj : public Sized_relobj_file<size, big_endian>
1534{
1535 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1536 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1537 Mips16_stubs_int_map;
1538 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1539
1540 public:
1541 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1542 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1543 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1544 processor_specific_flags_(0), local_symbol_is_mips16_(),
1545 local_symbol_is_micromips_(), mips16_stub_sections_(),
1546 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
1547 local_mips16_call_stubs_(), gp_(0), got_info_(NULL),
1548 section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1549 section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U), gprmask_(0),
1550 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
1551 {
1552 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1553 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
9810d34d
SS
1554 }
1555
1556 ~Mips_relobj()
1557 { }
1558
1559 // Downcast a base pointer to a Mips_relobj pointer. This is
1560 // not type-safe but we only use Mips_relobj not the base class.
1561 static Mips_relobj<size, big_endian>*
1562 as_mips_relobj(Relobj* relobj)
1563 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1564
1565 // Downcast a base pointer to a Mips_relobj pointer. This is
1566 // not type-safe but we only use Mips_relobj not the base class.
1567 static const Mips_relobj<size, big_endian>*
1568 as_mips_relobj(const Relobj* relobj)
1569 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1570
1571 // Processor-specific flags in ELF file header. This is valid only after
1572 // reading symbols.
1573 elfcpp::Elf_Word
1574 processor_specific_flags() const
1575 { return this->processor_specific_flags_; }
1576
1577 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1578 // index. This is only valid after do_count_local_symbol is called.
1579 bool
1580 local_symbol_is_mips16(unsigned int r_sym) const
1581 {
1582 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1583 return this->local_symbol_is_mips16_[r_sym];
1584 }
1585
1586 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1587 // index. This is only valid after do_count_local_symbol is called.
1588 bool
1589 local_symbol_is_micromips(unsigned int r_sym) const
1590 {
1591 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1592 return this->local_symbol_is_micromips_[r_sym];
1593 }
1594
1595 // Get or create MIPS16 stub section.
1596 Mips16_stub_section<size, big_endian>*
1597 get_mips16_stub_section(unsigned int shndx)
1598 {
1599 typename Mips16_stubs_int_map::const_iterator it =
1600 this->mips16_stub_sections_.find(shndx);
1601 if (it != this->mips16_stub_sections_.end())
1602 return (*it).second;
1603
1604 Mips16_stub_section<size, big_endian>* stub_section =
1605 new Mips16_stub_section<size, big_endian>(this, shndx);
1606 this->mips16_stub_sections_.insert(
1607 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1608 stub_section->shndx(), stub_section));
1609 return stub_section;
1610 }
1611
1612 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1613 // object doesn't have fn stub for R_SYM.
1614 Mips16_stub_section<size, big_endian>*
1615 get_local_mips16_fn_stub(unsigned int r_sym) const
1616 {
1617 typename Mips16_stubs_int_map::const_iterator it =
1618 this->local_mips16_fn_stubs_.find(r_sym);
1619 if (it != this->local_mips16_fn_stubs_.end())
1620 return (*it).second;
1621 return NULL;
1622 }
1623
1624 // Record that this object has MIPS16 fn stub for local symbol. This method
1625 // is only called if we decided not to discard the stub.
1626 void
1627 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1628 {
1629 gold_assert(stub->is_for_local_function());
1630 unsigned int r_sym = stub->r_sym();
1631 this->local_mips16_fn_stubs_.insert(
1632 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1633 r_sym, stub));
1634 }
1635
1636 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1637 // object doesn't have call stub for R_SYM.
1638 Mips16_stub_section<size, big_endian>*
1639 get_local_mips16_call_stub(unsigned int r_sym) const
1640 {
1641 typename Mips16_stubs_int_map::const_iterator it =
1642 this->local_mips16_call_stubs_.find(r_sym);
1643 if (it != this->local_mips16_call_stubs_.end())
1644 return (*it).second;
1645 return NULL;
1646 }
1647
1648 // Record that this object has MIPS16 call stub for local symbol. This method
1649 // is only called if we decided not to discard the stub.
1650 void
1651 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1652 {
1653 gold_assert(stub->is_for_local_function());
1654 unsigned int r_sym = stub->r_sym();
1655 this->local_mips16_call_stubs_.insert(
1656 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1657 r_sym, stub));
1658 }
1659
1660 // Record that we found "non 16-bit" call relocation against local symbol
1661 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1662 // is one.
1663 void
1664 add_local_non_16bit_call(unsigned int symndx)
1665 { this->local_non_16bit_calls_.insert(symndx); }
1666
1667 // Return true if there is any "non 16-bit" call relocation against local
1668 // symbol SYMNDX in this object.
1669 bool
1670 has_local_non_16bit_call_relocs(unsigned int symndx)
1671 {
1672 return (this->local_non_16bit_calls_.find(symndx)
1673 != this->local_non_16bit_calls_.end());
1674 }
1675
1676 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1677 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1678 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1679 void
1680 add_local_16bit_call(unsigned int symndx)
1681 { this->local_16bit_calls_.insert(symndx); }
1682
1683 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1684 // symbol SYMNDX in this object.
1685 bool
1686 has_local_16bit_call_relocs(unsigned int symndx)
1687 {
1688 return (this->local_16bit_calls_.find(symndx)
1689 != this->local_16bit_calls_.end());
1690 }
1691
1692 // Get gp value that was used to create this object.
1693 Mips_address
1694 gp_value() const
1695 { return this->gp_; }
1696
1697 // Return whether the object is a PIC object.
1698 bool
1699 is_pic() const
1700 { return this->is_pic_; }
1701
1702 // Return whether the object uses N32 ABI.
1703 bool
1704 is_n32() const
1705 { return this->is_n32_; }
1706
1707 // Return whether the object uses N64 ABI.
1708 bool
1709 is_n64() const
01b84e25 1710 { return size == 64; }
9810d34d
SS
1711
1712 // Return whether the object uses NewABI conventions.
1713 bool
1714 is_newabi() const
01b84e25 1715 { return this->is_n32() || this->is_n64(); }
9810d34d
SS
1716
1717 // Return Mips_got_info for this object.
1718 Mips_got_info<size, big_endian>*
1719 get_got_info() const
1720 { return this->got_info_; }
1721
1722 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1723 Mips_got_info<size, big_endian>*
1724 get_or_create_got_info()
1725 {
1726 if (!this->got_info_)
1727 this->got_info_ = new Mips_got_info<size, big_endian>();
1728 return this->got_info_;
1729 }
1730
1731 // Set Mips_got_info for this object.
1732 void
1733 set_got_info(Mips_got_info<size, big_endian>* got_info)
1734 { this->got_info_ = got_info; }
1735
1736 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1737 // after do_read_symbols is called.
1738 bool
1739 is_mips16_stub_section(unsigned int shndx)
1740 {
1741 return (is_mips16_fn_stub_section(shndx)
1742 || is_mips16_call_stub_section(shndx)
1743 || is_mips16_call_fp_stub_section(shndx));
1744 }
1745
1746 // Return TRUE if relocations in section SHNDX can refer directly to a
1747 // MIPS16 function rather than to a hard-float stub. This is only valid
1748 // after do_read_symbols is called.
1749 bool
1750 section_allows_mips16_refs(unsigned int shndx)
1751 {
1752 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1753 }
1754
1755 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1756 // after do_read_symbols is called.
1757 bool
1758 is_mips16_fn_stub_section(unsigned int shndx)
1759 {
1760 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1761 return this->section_is_mips16_fn_stub_[shndx];
1762 }
1763
1764 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1765 // after do_read_symbols is called.
1766 bool
1767 is_mips16_call_stub_section(unsigned int shndx)
1768 {
1769 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1770 return this->section_is_mips16_call_stub_[shndx];
1771 }
1772
1773 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1774 // valid after do_read_symbols is called.
1775 bool
1776 is_mips16_call_fp_stub_section(unsigned int shndx)
1777 {
1778 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1779 return this->section_is_mips16_call_fp_stub_[shndx];
1780 }
1781
1782 // Discard MIPS16 stub secions that are not needed.
1783 void
1784 discard_mips16_stub_sections(Symbol_table* symtab);
1785
1786 // Return gprmask from the .reginfo section of this object.
1787 Valtype
1788 gprmask() const
1789 { return this->gprmask_; }
1790
1791 // Return cprmask1 from the .reginfo section of this object.
1792 Valtype
1793 cprmask1() const
1794 { return this->cprmask1_; }
1795
1796 // Return cprmask2 from the .reginfo section of this object.
1797 Valtype
1798 cprmask2() const
1799 { return this->cprmask2_; }
1800
1801 // Return cprmask3 from the .reginfo section of this object.
1802 Valtype
1803 cprmask3() const
1804 { return this->cprmask3_; }
1805
1806 // Return cprmask4 from the .reginfo section of this object.
1807 Valtype
1808 cprmask4() const
1809 { return this->cprmask4_; }
1810
1811 protected:
1812 // Count the local symbols.
1813 void
1814 do_count_local_symbols(Stringpool_template<char>*,
1815 Stringpool_template<char>*);
1816
1817 // Read the symbol information.
1818 void
1819 do_read_symbols(Read_symbols_data* sd);
1820
1821 private:
47a9f4fc
VR
1822 // The name of the options section.
1823 const char* mips_elf_options_section_name()
1824 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1825
9810d34d
SS
1826 // processor-specific flags in ELF file header.
1827 elfcpp::Elf_Word processor_specific_flags_;
1828
1829 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1830 // This is only valid after do_count_local_symbol is called.
1831 std::vector<bool> local_symbol_is_mips16_;
1832
1833 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1834 // This is only valid after do_count_local_symbol is called.
1835 std::vector<bool> local_symbol_is_micromips_;
1836
1837 // Map from section index to the MIPS16 stub for that section. This contains
1838 // all stubs found in this object.
1839 Mips16_stubs_int_map mips16_stub_sections_;
1840
1841 // Local symbols that have "non 16-bit" call relocation. This relocation
1842 // would need to refer to a MIPS16 fn stub, if there is one.
1843 std::set<unsigned int> local_non_16bit_calls_;
1844
1845 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1846 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1847 // relocation that refers to the stub symbol.
1848 std::set<unsigned int> local_16bit_calls_;
1849
1850 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1851 // This contains only the stubs that we decided not to discard.
1852 Mips16_stubs_int_map local_mips16_fn_stubs_;
1853
1854 // Map from local symbol index to the MIPS16 call stub for that symbol.
1855 // This contains only the stubs that we decided not to discard.
1856 Mips16_stubs_int_map local_mips16_call_stubs_;
1857
1858 // gp value that was used to create this object.
1859 Mips_address gp_;
1860 // Whether the object is a PIC object.
1861 bool is_pic_ : 1;
1862 // Whether the object uses N32 ABI.
1863 bool is_n32_ : 1;
9810d34d
SS
1864 // The Mips_got_info for this object.
1865 Mips_got_info<size, big_endian>* got_info_;
1866
1867 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1868 // This is only valid after do_read_symbols is called.
1869 std::vector<bool> section_is_mips16_fn_stub_;
1870
1871 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1872 // This is only valid after do_read_symbols is called.
1873 std::vector<bool> section_is_mips16_call_stub_;
1874
1875 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1876 // This is only valid after do_read_symbols is called.
1877 std::vector<bool> section_is_mips16_call_fp_stub_;
1878
1879 // .pdr section index.
1880 unsigned int pdr_shndx_;
1881
1882 // gprmask from the .reginfo section of this object.
1883 Valtype gprmask_;
1884 // cprmask1 from the .reginfo section of this object.
1885 Valtype cprmask1_;
1886 // cprmask2 from the .reginfo section of this object.
1887 Valtype cprmask2_;
1888 // cprmask3 from the .reginfo section of this object.
1889 Valtype cprmask3_;
1890 // cprmask4 from the .reginfo section of this object.
1891 Valtype cprmask4_;
1892};
1893
1894// Mips_output_data_got class.
1895
1896template<int size, bool big_endian>
1897class Mips_output_data_got : public Output_data_got<size, big_endian>
1898{
1899 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1900 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1901 Reloc_section;
1902 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1903
1904 public:
1905 Mips_output_data_got(Target_mips<size, big_endian>* target,
1906 Symbol_table* symtab, Layout* layout)
1907 : Output_data_got<size, big_endian>(), target_(target),
1908 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1909 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1910 secondary_got_relocs_()
1911 {
1912 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1913 this->set_addralign(16);
1914 }
1915
1916 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1917 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1918 void
1919 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1920 unsigned int symndx, Mips_address addend,
47a9f4fc
VR
1921 unsigned int r_type, unsigned int shndx,
1922 bool is_section_symbol)
9810d34d
SS
1923 {
1924 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
47a9f4fc
VR
1925 r_type, shndx,
1926 is_section_symbol);
9810d34d
SS
1927 }
1928
1929 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
1930 // in OBJECT. FOR_CALL is true if the caller is only interested in
1931 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
1932 // relocation.
1933 void
1934 record_global_got_symbol(Mips_symbol<size>* mips_sym,
1935 Mips_relobj<size, big_endian>* object,
1936 unsigned int r_type, bool dyn_reloc, bool for_call)
1937 {
1938 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
1939 dyn_reloc, for_call);
1940 }
1941
1942 // Record that OBJECT has a page relocation against symbol SYMNDX and
1943 // that ADDEND is the addend for that relocation.
1944 void
1945 record_got_page_entry(Mips_relobj<size, big_endian>* object,
1946 unsigned int symndx, int addend)
1947 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
1948
1949 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
1950 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1951 // applied in a static link.
1952 void
1953 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1954 Mips_symbol<size>* gsym)
1955 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1956
1957 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
1958 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
1959 // relocation that needs to be applied in a static link.
1960 void
1961 add_static_reloc(unsigned int got_offset, unsigned int r_type,
1962 Sized_relobj_file<size, big_endian>* relobj,
1963 unsigned int index)
1964 {
1965 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1966 index));
1967 }
1968
1969 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
1970 // secondary GOT at OFFSET.
1971 void
1972 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
1973 Mips_symbol<size>* gsym)
1974 {
1975 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
1976 r_type, gsym));
1977 }
1978
1979 // Update GOT entry at OFFSET with VALUE.
1980 void
1981 update_got_entry(unsigned int offset, Mips_address value)
1982 {
1983 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
1984 }
1985
1986 // Return the number of entries in local part of the GOT. This includes
1987 // local entries, page entries and 2 reserved entries.
1988 unsigned int
1989 get_local_gotno() const
1990 {
1991 if (!this->multi_got())
1992 {
1993 return (2 + this->master_got_info_->local_gotno()
1994 + this->master_got_info_->page_gotno());
1995 }
1996 else
1997 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
1998 }
1999
2000 // Return dynamic symbol table index of the first symbol with global GOT
2001 // entry.
2002 unsigned int
2003 first_global_got_dynsym_index() const
2004 { return this->first_global_got_dynsym_index_; }
2005
2006 // Set dynamic symbol table index of the first symbol with global GOT entry.
2007 void
2008 set_first_global_got_dynsym_index(unsigned int index)
2009 { this->first_global_got_dynsym_index_ = index; }
2010
2011 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2012 // larger than 64K, create multi-GOT.
2013 void
2014 lay_out_got(Layout* layout, Symbol_table* symtab,
2015 const Input_objects* input_objects);
2016
2017 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2018 void
2019 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2020
2021 // Attempt to merge GOTs of different input objects.
2022 void
2023 merge_gots(const Input_objects* input_objects);
2024
2025 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2026 // this would lead to overflow, true if they were merged successfully.
2027 bool
2028 merge_got_with(Mips_got_info<size, big_endian>* from,
2029 Mips_relobj<size, big_endian>* object,
2030 Mips_got_info<size, big_endian>* to);
2031
2032 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2033 // use OBJECT's GOT.
2034 unsigned int
2035 get_got_page_offset(Mips_address value,
2036 const Mips_relobj<size, big_endian>* object)
2037 {
2038 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2039 ? this->master_got_info_
2040 : object->get_got_info());
2041 gold_assert(g != NULL);
2042 return g->get_got_page_offset(value, this);
2043 }
2044
2045 // Return the GOT offset of type GOT_TYPE of the global symbol
2046 // GSYM. For multi-GOT links, use OBJECT's GOT.
2047 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2048 Mips_relobj<size, big_endian>* object) const
2049 {
2050 if (!this->multi_got())
2051 return gsym->got_offset(got_type);
2052 else
2053 {
2054 Mips_got_info<size, big_endian>* g = object->get_got_info();
2055 gold_assert(g != NULL);
2056 return gsym->got_offset(g->multigot_got_type(got_type));
2057 }
2058 }
2059
2060 // Return the GOT offset of type GOT_TYPE of the local symbol
2061 // SYMNDX.
2062 unsigned int
2063 got_offset(unsigned int symndx, unsigned int got_type,
47a9f4fc
VR
2064 Sized_relobj_file<size, big_endian>* object,
2065 uint64_t addend) const
2066 { return object->local_got_offset(symndx, got_type, addend); }
9810d34d
SS
2067
2068 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2069 unsigned int
2070 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2071 {
2072 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2073 ? this->master_got_info_
2074 : object->get_got_info());
2075 gold_assert(g != NULL);
2076 return g->tls_ldm_offset();
2077 }
2078
2079 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2080 void
2081 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2082 Mips_relobj<size, big_endian>* object)
2083 {
2084 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2085 ? this->master_got_info_
2086 : object->get_got_info());
2087 gold_assert(g != NULL);
2088 g->set_tls_ldm_offset(tls_ldm_offset);
2089 }
2090
2091 // Return true for multi-GOT links.
2092 bool
2093 multi_got() const
2094 { return this->primary_got_ != NULL; }
2095
2096 // Return the offset of OBJECT's GOT from the start of .got section.
2097 unsigned int
2098 get_got_offset(const Mips_relobj<size, big_endian>* object)
2099 {
2100 if (!this->multi_got())
2101 return 0;
2102 else
2103 {
2104 Mips_got_info<size, big_endian>* g = object->get_got_info();
2105 return g != NULL ? g->offset() : 0;
2106 }
2107 }
2108
2109 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2110 void
2111 add_reloc_only_entries()
2112 { this->master_got_info_->add_reloc_only_entries(this); }
2113
2114 // Return offset of the primary GOT's entry for global symbol.
2115 unsigned int
2116 get_primary_got_offset(const Mips_symbol<size>* sym) const
2117 {
2118 gold_assert(sym->global_got_area() != GGA_NONE);
2119 return (this->get_local_gotno() + sym->dynsym_index()
2120 - this->first_global_got_dynsym_index()) * size/8;
2121 }
2122
2123 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2124 // Input argument GOT_OFFSET is always global offset from the start of
2125 // .got section, for both single and multi-GOT links.
2126 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2127 // links, the return value is object_got_offset - 0x7FF0, where
2128 // object_got_offset is offset in the OBJECT's GOT.
2129 int
2130 gp_offset(unsigned int got_offset,
2131 const Mips_relobj<size, big_endian>* object) const
2132 {
2133 return (this->address() + got_offset
2134 - this->target_->adjusted_gp_value(object));
2135 }
2136
2137 protected:
2138 // Write out the GOT table.
2139 void
2140 do_write(Output_file*);
2141
2142 private:
2143
2144 // This class represent dynamic relocations that need to be applied by
2145 // gold because we are using TLS relocations in a static link.
2146 class Static_reloc
2147 {
2148 public:
2149 Static_reloc(unsigned int got_offset, unsigned int r_type,
2150 Mips_symbol<size>* gsym)
2151 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2152 { this->u_.global.symbol = gsym; }
2153
2154 Static_reloc(unsigned int got_offset, unsigned int r_type,
2155 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2156 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2157 {
2158 this->u_.local.relobj = relobj;
2159 this->u_.local.index = index;
2160 }
2161
2162 // Return the GOT offset.
2163 unsigned int
2164 got_offset() const
2165 { return this->got_offset_; }
2166
2167 // Relocation type.
2168 unsigned int
2169 r_type() const
2170 { return this->r_type_; }
2171
2172 // Whether the symbol is global or not.
2173 bool
2174 symbol_is_global() const
2175 { return this->symbol_is_global_; }
2176
2177 // For a relocation against a global symbol, the global symbol.
2178 Mips_symbol<size>*
2179 symbol() const
2180 {
2181 gold_assert(this->symbol_is_global_);
2182 return this->u_.global.symbol;
2183 }
2184
2185 // For a relocation against a local symbol, the defining object.
2186 Sized_relobj_file<size, big_endian>*
2187 relobj() const
2188 {
2189 gold_assert(!this->symbol_is_global_);
2190 return this->u_.local.relobj;
2191 }
2192
2193 // For a relocation against a local symbol, the local symbol index.
2194 unsigned int
2195 index() const
2196 {
2197 gold_assert(!this->symbol_is_global_);
2198 return this->u_.local.index;
2199 }
2200
2201 private:
2202 // GOT offset of the entry to which this relocation is applied.
2203 unsigned int got_offset_;
2204 // Type of relocation.
2205 unsigned int r_type_;
2206 // Whether this relocation is against a global symbol.
2207 bool symbol_is_global_;
2208 // A global or local symbol.
2209 union
2210 {
2211 struct
2212 {
2213 // For a global symbol, the symbol itself.
2214 Mips_symbol<size>* symbol;
2215 } global;
2216 struct
2217 {
2218 // For a local symbol, the object defining object.
2219 Sized_relobj_file<size, big_endian>* relobj;
2220 // For a local symbol, the symbol index.
2221 unsigned int index;
2222 } local;
2223 } u_;
2224 };
2225
2226 // The target.
2227 Target_mips<size, big_endian>* target_;
2228 // The symbol table.
2229 Symbol_table* symbol_table_;
2230 // The layout.
2231 Layout* layout_;
2232 // Static relocs to be applied to the GOT.
2233 std::vector<Static_reloc> static_relocs_;
2234 // .got section view.
2235 unsigned char* got_view_;
2236 // The dynamic symbol table index of the first symbol with global GOT entry.
2237 unsigned int first_global_got_dynsym_index_;
2238 // The master GOT information.
2239 Mips_got_info<size, big_endian>* master_got_info_;
2240 // The primary GOT information.
2241 Mips_got_info<size, big_endian>* primary_got_;
2242 // Secondary GOT fixups.
2243 std::vector<Static_reloc> secondary_got_relocs_;
2244};
2245
2246// A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2247// two ways of creating these interfaces. The first is to add:
2248//
2249// lui $25,%hi(func)
2250// j func
2251// addiu $25,$25,%lo(func)
2252//
2253// to a separate trampoline section. The second is to add:
2254//
2255// lui $25,%hi(func)
2256// addiu $25,$25,%lo(func)
2257//
2258// immediately before a PIC function "func", but only if a function is at the
2259// beginning of the section, and the section is not too heavily aligned (i.e we
2260// would need to add no more than 2 nops before the stub.)
2261//
2262// We only create stubs of the first type.
2263
2264template<int size, bool big_endian>
2265class Mips_output_data_la25_stub : public Output_section_data
2266{
2267 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2268
2269 public:
2270 Mips_output_data_la25_stub()
2271 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2272 { }
2273
2274 // Create LA25 stub for a symbol.
2275 void
2276 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2277 Mips_symbol<size>* gsym);
2278
2279 // Return output address of a stub.
2280 Mips_address
2281 stub_address(const Mips_symbol<size>* sym) const
2282 {
2283 gold_assert(sym->has_la25_stub());
2284 return this->address() + sym->la25_stub_offset();
2285 }
2286
2287 protected:
2288 void
2289 do_adjust_output_section(Output_section* os)
2290 { os->set_entsize(0); }
2291
2292 private:
2293 // Template for standard LA25 stub.
2294 static const uint32_t la25_stub_entry[];
2295 // Template for microMIPS LA25 stub.
2296 static const uint32_t la25_stub_micromips_entry[];
2297
2298 // Set the final size.
2299 void
2300 set_final_data_size()
2301 { this->set_data_size(this->symbols_.size() * 16); }
2302
2303 // Create a symbol for SYM stub's value and size, to help make the
2304 // disassembly easier to read.
2305 void
2306 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2307 Target_mips<size, big_endian>* target, uint64_t symsize);
2308
1a08ae21
VR
2309 // Write to a map file.
2310 void
2311 do_print_to_mapfile(Mapfile* mapfile) const
2312 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2313
9810d34d
SS
2314 // Write out the LA25 stub section.
2315 void
2316 do_write(Output_file*);
2317
2318 // Symbols that have LA25 stubs.
15eb1beb 2319 std::vector<Mips_symbol<size>*> symbols_;
9810d34d
SS
2320};
2321
47a9f4fc
VR
2322// MIPS-specific relocation writer.
2323
2324template<int sh_type, bool dynamic, int size, bool big_endian>
2325struct Mips_output_reloc_writer;
2326
2327template<int sh_type, bool dynamic, bool big_endian>
2328struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2329{
2330 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2331 typedef std::vector<Output_reloc_type> Relocs;
2332
2333 static void
2334 write(typename Relocs::const_iterator p, unsigned char* pov)
2335 { p->write(pov); }
2336};
2337
2338template<int sh_type, bool dynamic, bool big_endian>
2339struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2340{
2341 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2342 typedef std::vector<Output_reloc_type> Relocs;
2343
2344 static void
2345 write(typename Relocs::const_iterator p, unsigned char* pov)
2346 {
2347 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2348 orel.put_r_offset(p->get_address());
2349 orel.put_r_sym(p->get_symbol_index());
2350 orel.put_r_ssym(RSS_UNDEF);
2351 orel.put_r_type(p->type());
2352 if (p->type() == elfcpp::R_MIPS_REL32)
2353 orel.put_r_type2(elfcpp::R_MIPS_64);
2354 else
2355 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2356 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2357 }
2358};
2359
2360template<int sh_type, bool dynamic, int size, bool big_endian>
2361class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2362 size, big_endian>
2363{
2364 public:
2365 Mips_output_data_reloc(bool sort_relocs)
2366 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2367 { }
2368
2369 protected:
2370 // Write out the data.
2371 void
2372 do_write(Output_file* of)
2373 {
2374 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2375 big_endian> Writer;
2376 this->template do_write_generic<Writer>(of);
2377 }
2378};
2379
2380
9810d34d
SS
2381// A class to handle the PLT data.
2382
2383template<int size, bool big_endian>
2384class Mips_output_data_plt : public Output_section_data
2385{
2386 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
47a9f4fc
VR
2387 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2388 size, big_endian> Reloc_section;
9810d34d
SS
2389
2390 public:
2391 // Create the PLT section. The ordinary .got section is an argument,
2392 // since we need to refer to the start.
2393 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2394 Target_mips<size, big_endian>* target)
2395 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2396 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2397 target_(target)
2398 {
2399 this->rel_ = new Reloc_section(false);
2400 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2401 elfcpp::SHF_ALLOC, this->rel_,
2402 ORDER_DYNAMIC_PLT_RELOCS, false);
2403 }
2404
2405 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2406 void
2407 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2408
2409 // Return the .rel.plt section data.
2410 const Reloc_section*
2411 rel_plt() const
2412 { return this->rel_; }
2413
2414 // Return the number of PLT entries.
2415 unsigned int
2416 entry_count() const
2417 { return this->symbols_.size(); }
2418
2419 // Return the offset of the first non-reserved PLT entry.
2420 unsigned int
2421 first_plt_entry_offset() const
2422 { return sizeof(plt0_entry_o32); }
2423
2424 // Return the size of a PLT entry.
2425 unsigned int
2426 plt_entry_size() const
2427 { return sizeof(plt_entry); }
2428
2429 // Set final PLT offsets. For each symbol, determine whether standard or
2430 // compressed (MIPS16 or microMIPS) PLT entry is used.
2431 void
2432 set_plt_offsets();
2433
2434 // Return the offset of the first standard PLT entry.
2435 unsigned int
2436 first_mips_plt_offset() const
2437 { return this->plt_header_size_; }
2438
2439 // Return the offset of the first compressed PLT entry.
2440 unsigned int
2441 first_comp_plt_offset() const
2442 { return this->plt_header_size_ + this->plt_mips_offset_; }
2443
2444 // Return whether there are any standard PLT entries.
2445 bool
2446 has_standard_entries() const
2447 { return this->plt_mips_offset_ > 0; }
2448
2449 // Return the output address of standard PLT entry.
2450 Mips_address
2451 mips_entry_address(const Mips_symbol<size>* sym) const
2452 {
2453 gold_assert (sym->has_mips_plt_offset());
2454 return (this->address() + this->first_mips_plt_offset()
2455 + sym->mips_plt_offset());
2456 }
2457
2458 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2459 Mips_address
2460 comp_entry_address(const Mips_symbol<size>* sym) const
2461 {
2462 gold_assert (sym->has_comp_plt_offset());
2463 return (this->address() + this->first_comp_plt_offset()
2464 + sym->comp_plt_offset());
2465 }
2466
2467 protected:
2468 void
2469 do_adjust_output_section(Output_section* os)
2470 { os->set_entsize(0); }
2471
2472 // Write to a map file.
2473 void
2474 do_print_to_mapfile(Mapfile* mapfile) const
2475 { mapfile->print_output_data(this, _(".plt")); }
2476
2477 private:
2478 // Template for the first PLT entry.
2479 static const uint32_t plt0_entry_o32[];
2480 static const uint32_t plt0_entry_n32[];
2481 static const uint32_t plt0_entry_n64[];
2482 static const uint32_t plt0_entry_micromips_o32[];
2483 static const uint32_t plt0_entry_micromips32_o32[];
2484
2485 // Template for subsequent PLT entries.
2486 static const uint32_t plt_entry[];
2487 static const uint32_t plt_entry_mips16_o32[];
2488 static const uint32_t plt_entry_micromips_o32[];
2489 static const uint32_t plt_entry_micromips32_o32[];
2490
2491 // Set the final size.
2492 void
2493 set_final_data_size()
2494 {
2495 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2496 + this->plt_comp_offset_);
2497 }
2498
2499 // Write out the PLT data.
2500 void
2501 do_write(Output_file*);
2502
2503 // Return whether the plt header contains microMIPS code. For the sake of
2504 // cache alignment always use a standard header whenever any standard entries
2505 // are present even if microMIPS entries are present as well. This also lets
2506 // the microMIPS header rely on the value of $v0 only set by microMIPS
2507 // entries, for a small size reduction.
2508 bool
2509 is_plt_header_compressed() const
2510 {
2511 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2512 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2513 }
2514
2515 // Return the size of the PLT header.
2516 unsigned int
2517 get_plt_header_size() const
2518 {
2519 if (this->target_->is_output_n64())
2520 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2521 else if (this->target_->is_output_n32())
2522 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2523 else if (!this->is_plt_header_compressed())
2524 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2525 else if (this->target_->use_32bit_micromips_instructions())
2526 return (2 * sizeof(plt0_entry_micromips32_o32)
2527 / sizeof(plt0_entry_micromips32_o32[0]));
2528 else
2529 return (2 * sizeof(plt0_entry_micromips_o32)
2530 / sizeof(plt0_entry_micromips_o32[0]));
2531 }
2532
2533 // Return the PLT header entry.
2534 const uint32_t*
2535 get_plt_header_entry() const
2536 {
2537 if (this->target_->is_output_n64())
2538 return plt0_entry_n64;
2539 else if (this->target_->is_output_n32())
2540 return plt0_entry_n32;
2541 else if (!this->is_plt_header_compressed())
2542 return plt0_entry_o32;
2543 else if (this->target_->use_32bit_micromips_instructions())
2544 return plt0_entry_micromips32_o32;
2545 else
2546 return plt0_entry_micromips_o32;
2547 }
2548
2549 // Return the size of the standard PLT entry.
2550 unsigned int
2551 standard_plt_entry_size() const
2552 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2553
2554 // Return the size of the compressed PLT entry.
2555 unsigned int
2556 compressed_plt_entry_size() const
2557 {
2558 gold_assert(!this->target_->is_output_newabi());
2559
2560 if (!this->target_->is_output_micromips())
2561 return (2 * sizeof(plt_entry_mips16_o32)
2562 / sizeof(plt_entry_mips16_o32[0]));
2563 else if (this->target_->use_32bit_micromips_instructions())
2564 return (2 * sizeof(plt_entry_micromips32_o32)
2565 / sizeof(plt_entry_micromips32_o32[0]));
2566 else
2567 return (2 * sizeof(plt_entry_micromips_o32)
2568 / sizeof(plt_entry_micromips_o32[0]));
2569 }
2570
2571 // The reloc section.
2572 Reloc_section* rel_;
2573 // The .got.plt section.
2574 Output_data_space* got_plt_;
2575 // Symbols that have PLT entry.
2576 std::vector<Mips_symbol<size>*> symbols_;
2577 // The offset of the next standard PLT entry to create.
2578 unsigned int plt_mips_offset_;
2579 // The offset of the next compressed PLT entry to create.
2580 unsigned int plt_comp_offset_;
2581 // The size of the PLT header in bytes.
2582 unsigned int plt_header_size_;
2583 // The target.
2584 Target_mips<size, big_endian>* target_;
2585};
2586
2587// A class to handle the .MIPS.stubs data.
2588
2589template<int size, bool big_endian>
2590class Mips_output_data_mips_stubs : public Output_section_data
2591{
2592 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2593
15eb1beb
VR
2594 // Unordered set of .MIPS.stubs entries.
2595 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2596 Mips_stubs_entry_set;
2597
9810d34d
SS
2598 public:
2599 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2600 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2601 stub_offsets_are_set_(false), target_(target)
2602 { }
2603
2604 // Create entry for a symbol.
2605 void
2606 make_entry(Mips_symbol<size>*);
2607
2608 // Remove entry for a symbol.
2609 void
2610 remove_entry(Mips_symbol<size>* gsym);
2611
2612 // Set stub offsets for symbols. This method expects that the number of
2613 // entries in dynamic symbol table is set.
2614 void
2615 set_lazy_stub_offsets();
2616
2617 void
2618 set_needs_dynsym_value();
2619
2620 // Set the number of entries in dynamic symbol table.
2621 void
2622 set_dynsym_count(unsigned int dynsym_count)
2623 { this->dynsym_count_ = dynsym_count; }
2624
2625 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2626 // count is greater than 0x10000. If the dynamic symbol count is less than
2627 // 0x10000, the stub will be 4 bytes smaller.
2628 // There's no disadvantage from using microMIPS code here, so for the sake of
2629 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2630 // output produced at all. This has a benefit of stubs being shorter by
2631 // 4 bytes each too, unless in the insn32 mode.
2632 unsigned int
2633 stub_max_size() const
2634 {
2635 if (!this->target_->is_output_micromips()
2636 || this->target_->use_32bit_micromips_instructions())
2637 return 20;
2638 else
2639 return 16;
2640 }
2641
2642 // Return the size of the stub. This method expects that the final dynsym
2643 // count is set.
2644 unsigned int
2645 stub_size() const
2646 {
2647 gold_assert(this->dynsym_count_ != -1U);
2648 if (this->dynsym_count_ > 0x10000)
2649 return this->stub_max_size();
2650 else
2651 return this->stub_max_size() - 4;
2652 }
2653
2654 // Return output address of a stub.
2655 Mips_address
2656 stub_address(const Mips_symbol<size>* sym) const
2657 {
2658 gold_assert(sym->has_lazy_stub());
2659 return this->address() + sym->lazy_stub_offset();
2660 }
2661
2662 protected:
2663 void
2664 do_adjust_output_section(Output_section* os)
2665 { os->set_entsize(0); }
2666
2667 // Write to a map file.
2668 void
2669 do_print_to_mapfile(Mapfile* mapfile) const
2670 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2671
2672 private:
2673 static const uint32_t lazy_stub_normal_1[];
2674 static const uint32_t lazy_stub_normal_1_n64[];
2675 static const uint32_t lazy_stub_normal_2[];
2676 static const uint32_t lazy_stub_normal_2_n64[];
2677 static const uint32_t lazy_stub_big[];
2678 static const uint32_t lazy_stub_big_n64[];
2679
2680 static const uint32_t lazy_stub_micromips_normal_1[];
2681 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2682 static const uint32_t lazy_stub_micromips_normal_2[];
2683 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2684 static const uint32_t lazy_stub_micromips_big[];
2685 static const uint32_t lazy_stub_micromips_big_n64[];
2686
2687 static const uint32_t lazy_stub_micromips32_normal_1[];
2688 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2689 static const uint32_t lazy_stub_micromips32_normal_2[];
2690 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2691 static const uint32_t lazy_stub_micromips32_big[];
2692 static const uint32_t lazy_stub_micromips32_big_n64[];
2693
2694 // Set the final size.
2695 void
2696 set_final_data_size()
2697 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2698
2699 // Write out the .MIPS.stubs data.
2700 void
2701 do_write(Output_file*);
2702
2703 // .MIPS.stubs symbols
15eb1beb 2704 Mips_stubs_entry_set symbols_;
9810d34d
SS
2705 // Number of entries in dynamic symbol table.
2706 unsigned int dynsym_count_;
2707 // Whether the stub offsets are set.
2708 bool stub_offsets_are_set_;
2709 // The target.
2710 Target_mips<size, big_endian>* target_;
2711};
2712
2713// This class handles Mips .reginfo output section.
2714
2715template<int size, bool big_endian>
2716class Mips_output_section_reginfo : public Output_section
2717{
2718 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2719
2720 public:
2721 Mips_output_section_reginfo(const char* name, elfcpp::Elf_Word type,
2722 elfcpp::Elf_Xword flags,
2723 Target_mips<size, big_endian>* target)
2724 : Output_section(name, type, flags), target_(target), gprmask_(0),
2725 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
2726 { }
2727
2728 // Downcast a base pointer to a Mips_output_section_reginfo pointer.
2729 static Mips_output_section_reginfo<size, big_endian>*
2730 as_mips_output_section_reginfo(Output_section* os)
2731 { return static_cast<Mips_output_section_reginfo<size, big_endian>*>(os); }
2732
2733 // Set masks of the output .reginfo section.
2734 void
2735 set_masks(Valtype gprmask, Valtype cprmask1, Valtype cprmask2,
2736 Valtype cprmask3, Valtype cprmask4)
2737 {
2738 this->gprmask_ = gprmask;
2739 this->cprmask1_ = cprmask1;
2740 this->cprmask2_ = cprmask2;
2741 this->cprmask3_ = cprmask3;
2742 this->cprmask4_ = cprmask4;
2743 }
2744
2745 protected:
2746 // Set the final data size.
2747 void
2748 set_final_data_size()
2749 { this->set_data_size(24); }
2750
2751 // Write out reginfo section.
2752 void
2753 do_write(Output_file* of);
2754
2755 private:
2756 Target_mips<size, big_endian>* target_;
2757
2758 // gprmask of the output .reginfo section.
2759 Valtype gprmask_;
2760 // cprmask1 of the output .reginfo section.
2761 Valtype cprmask1_;
2762 // cprmask2 of the output .reginfo section.
2763 Valtype cprmask2_;
2764 // cprmask3 of the output .reginfo section.
2765 Valtype cprmask3_;
2766 // cprmask4 of the output .reginfo section.
2767 Valtype cprmask4_;
2768};
2769
2770// The MIPS target has relocation types which default handling of relocatable
2771// relocation cannot process. So we have to extend the default code.
2772
4d625b70 2773template<bool big_endian, typename Classify_reloc>
9810d34d 2774class Mips_scan_relocatable_relocs :
4d625b70 2775 public Default_scan_relocatable_relocs<Classify_reloc>
9810d34d
SS
2776{
2777 public:
2778 // Return the strategy to use for a local symbol which is a section
2779 // symbol, given the relocation type.
2780 inline Relocatable_relocs::Reloc_strategy
2781 local_section_strategy(unsigned int r_type, Relobj* object)
2782 {
4d625b70 2783 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
9810d34d
SS
2784 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2785 else
2786 {
2787 switch (r_type)
2788 {
2789 case elfcpp::R_MIPS_26:
2790 return Relocatable_relocs::RELOC_SPECIAL;
2791
2792 default:
4d625b70 2793 return Default_scan_relocatable_relocs<Classify_reloc>::
9810d34d
SS
2794 local_section_strategy(r_type, object);
2795 }
2796 }
2797 }
2798};
2799
2800// Mips_copy_relocs class. The only difference from the base class is the
2801// method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2802// Mips cannot convert all relocation types to dynamic relocs. If a reloc
2803// cannot be made dynamic, a COPY reloc is emitted.
2804
2805template<int sh_type, int size, bool big_endian>
2806class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2807{
2808 public:
2809 Mips_copy_relocs()
2810 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2811 { }
2812
2813 // Emit any saved relocations which turn out to be needed. This is
2814 // called after all the relocs have been scanned.
2815 void
2816 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2817 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2818
2819 private:
2820 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2821 Copy_reloc_entry;
2822
2823 // Emit this reloc if appropriate. This is called after we have
2824 // scanned all the relocations, so we know whether we emitted a
2825 // COPY relocation for SYM_.
2826 void
2827 emit_entry(Copy_reloc_entry& entry,
2828 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2829 Symbol_table* symtab, Layout* layout,
2830 Target_mips<size, big_endian>* target);
2831};
2832
2833
2834// Return true if the symbol SYM should be considered to resolve local
2835// to the current module, and false otherwise. The logic is taken from
2836// GNU ld's method _bfd_elf_symbol_refs_local_p.
2837static bool
2838symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2839 bool local_protected)
2840{
2841 // If it's a local sym, of course we resolve locally.
2842 if (sym == NULL)
2843 return true;
2844
2845 // STV_HIDDEN or STV_INTERNAL ones must be local.
2846 if (sym->visibility() == elfcpp::STV_HIDDEN
2847 || sym->visibility() == elfcpp::STV_INTERNAL)
2848 return true;
2849
2850 // If we don't have a definition in a regular file, then we can't
2851 // resolve locally. The sym is either undefined or dynamic.
2852 if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
2853 || sym->is_undefined())
2854 return false;
2855
2856 // Forced local symbols resolve locally.
2857 if (sym->is_forced_local())
2858 return true;
2859
2860 // As do non-dynamic symbols.
2861 if (!has_dynsym_entry)
2862 return true;
2863
2864 // At this point, we know the symbol is defined and dynamic. In an
2865 // executable it must resolve locally, likewise when building symbolic
2866 // shared libraries.
2867 if (parameters->options().output_is_executable()
2868 || parameters->options().Bsymbolic())
2869 return true;
2870
2871 // Now deal with defined dynamic symbols in shared libraries. Ones
2872 // with default visibility might not resolve locally.
2873 if (sym->visibility() == elfcpp::STV_DEFAULT)
2874 return false;
2875
2876 // STV_PROTECTED non-function symbols are local.
2877 if (sym->type() != elfcpp::STT_FUNC)
2878 return true;
2879
2880 // Function pointer equality tests may require that STV_PROTECTED
2881 // symbols be treated as dynamic symbols. If the address of a
2882 // function not defined in an executable is set to that function's
2883 // plt entry in the executable, then the address of the function in
2884 // a shared library must also be the plt entry in the executable.
2885 return local_protected;
2886}
2887
2888// Return TRUE if references to this symbol always reference the symbol in this
2889// object.
2890static bool
2891symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
2892{
2893 return symbol_refs_local(sym, has_dynsym_entry, false);
2894}
2895
2896// Return TRUE if calls to this symbol always call the version in this object.
2897static bool
2898symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
2899{
2900 return symbol_refs_local(sym, has_dynsym_entry, true);
2901}
2902
2903// Compare GOT offsets of two symbols.
2904
2905template<int size, bool big_endian>
2906static bool
2907got_offset_compare(Symbol* sym1, Symbol* sym2)
2908{
2909 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
2910 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
2911 unsigned int area1 = mips_sym1->global_got_area();
2912 unsigned int area2 = mips_sym2->global_got_area();
2913 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
2914
2915 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
2916 if (area1 != area2)
2917 return area1 < area2;
2918
2919 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
2920}
2921
2922// This method divides dynamic symbols into symbols that have GOT entry, and
2923// symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
2924// Mips ABI requires that symbols with the GOT entry must be at the end of
2925// dynamic symbol table, and the order in dynamic symbol table must match the
2926// order in GOT.
2927
2928template<int size, bool big_endian>
2929static void
2930reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
2931 std::vector<Symbol*>* non_got_symbols,
2932 std::vector<Symbol*>* got_symbols)
2933{
2934 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
2935 p != dyn_symbols->end();
2936 ++p)
2937 {
2938 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
2939 if (mips_sym->global_got_area() == GGA_NORMAL
2940 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
2941 got_symbols->push_back(mips_sym);
2942 else
2943 non_got_symbols->push_back(mips_sym);
2944 }
2945
2946 std::sort(got_symbols->begin(), got_symbols->end(),
2947 got_offset_compare<size, big_endian>);
2948}
2949
2950// Functor class for processing the global symbol table.
2951
2952template<int size, bool big_endian>
2953class Symbol_visitor_check_symbols
2954{
2955 public:
2956 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
2957 Layout* layout, Symbol_table* symtab)
2958 : target_(target), layout_(layout), symtab_(symtab)
2959 { }
2960
2961 void
2962 operator()(Sized_symbol<size>* sym)
2963 {
2964 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
2965 if (local_pic_function<size, big_endian>(mips_sym))
2966 {
2967 // SYM is a function that might need $25 to be valid on entry.
2968 // If we're creating a non-PIC relocatable object, mark SYM as
2969 // being PIC. If we're creating a non-relocatable object with
2970 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
2971 // stub.
2972 if (parameters->options().relocatable())
2973 {
2974 if (!parameters->options().output_is_position_independent())
2975 mips_sym->set_pic();
2976 }
2977 else if (mips_sym->has_nonpic_branches())
2978 {
2979 this->target_->la25_stub_section(layout_)
2980 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
2981 }
2982 }
2983 }
2984
2985 private:
2986 Target_mips<size, big_endian>* target_;
2987 Layout* layout_;
2988 Symbol_table* symtab_;
2989};
2990
4d625b70
CC
2991// Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
2992// and endianness. The relocation format for MIPS-64 is non-standard.
2993
2994template<int sh_type, int size, bool big_endian>
2995struct Mips_reloc_types;
2996
2997template<bool big_endian>
2998struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
2999{
3000 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3001 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3002
47a9f4fc 3003 static typename elfcpp::Elf_types<32>::Elf_Swxword
4d625b70
CC
3004 get_r_addend(const Reloc*)
3005 { return 0; }
3006
3007 static inline void
3008 set_reloc_addend(Reloc_write*,
3009 typename elfcpp::Elf_types<32>::Elf_Swxword)
3010 { gold_unreachable(); }
3011};
3012
3013template<bool big_endian>
3014struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3015{
3016 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3017 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3018
47a9f4fc 3019 static typename elfcpp::Elf_types<32>::Elf_Swxword
4d625b70
CC
3020 get_r_addend(const Reloc* reloc)
3021 { return reloc->get_r_addend(); }
3022
3023 static inline void
3024 set_reloc_addend(Reloc_write* p,
3025 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3026 { p->put_r_addend(val); }
3027};
3028
3029template<bool big_endian>
3030struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3031{
3032 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3033 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3034
47a9f4fc 3035 static typename elfcpp::Elf_types<64>::Elf_Swxword
4d625b70
CC
3036 get_r_addend(const Reloc*)
3037 { return 0; }
3038
3039 static inline void
3040 set_reloc_addend(Reloc_write*,
3041 typename elfcpp::Elf_types<64>::Elf_Swxword)
3042 { gold_unreachable(); }
3043};
3044
3045template<bool big_endian>
3046struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3047{
3048 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3049 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3050
47a9f4fc 3051 static typename elfcpp::Elf_types<64>::Elf_Swxword
4d625b70
CC
3052 get_r_addend(const Reloc* reloc)
3053 { return reloc->get_r_addend(); }
3054
3055 static inline void
3056 set_reloc_addend(Reloc_write* p,
3057 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3058 { p->put_r_addend(val); }
3059};
3060
3061// Forward declaration.
3062static unsigned int
3063mips_get_size_for_reloc(unsigned int, Relobj*);
3064
3065// A class for inquiring about properties of a relocation,
3066// used while scanning relocs during a relocatable link and
3067// garbage collection.
3068
3069template<int sh_type_, int size, bool big_endian>
3070class Mips_classify_reloc;
3071
3072template<int sh_type_, bool big_endian>
3073class Mips_classify_reloc<sh_type_, 32, big_endian> :
3074 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3075{
3076 public:
3077 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3078 Reltype;
3079 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3080 Reltype_write;
3081
3082 // Return the symbol referred to by the relocation.
3083 static inline unsigned int
3084 get_r_sym(const Reltype* reloc)
3085 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3086
3087 // Return the type of the relocation.
3088 static inline unsigned int
3089 get_r_type(const Reltype* reloc)
3090 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3091
47a9f4fc
VR
3092 static inline unsigned int
3093 get_r_type2(const Reltype*)
3094 { return 0; }
3095
3096 static inline unsigned int
3097 get_r_type3(const Reltype*)
3098 { return 0; }
3099
3100 static inline unsigned int
3101 get_r_ssym(const Reltype*)
3102 { return 0; }
3103
4d625b70
CC
3104 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3105 static inline unsigned int
3106 get_r_addend(const Reltype* reloc)
8a8880cb
CC
3107 {
3108 if (sh_type_ == elfcpp::SHT_REL)
3109 return 0;
3110 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3111 }
4d625b70
CC
3112
3113 // Write the r_info field to a new reloc, using the r_info field from
3114 // the original reloc, replacing the r_sym field with R_SYM.
3115 static inline void
3116 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3117 {
3118 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
c2fa9ced 3119 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
4d625b70
CC
3120 }
3121
3122 // Write the r_addend field to a new reloc.
3123 static inline void
3124 put_r_addend(Reltype_write* to,
3125 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3126 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3127
3128 // Return the size of the addend of the relocation (only used for SHT_REL).
3129 static unsigned int
3130 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3131 { return mips_get_size_for_reloc(r_type, obj); }
3132};
3133
3134template<int sh_type_, bool big_endian>
3135class Mips_classify_reloc<sh_type_, 64, big_endian> :
3136 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3137{
3138 public:
3139 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3140 Reltype;
3141 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3142 Reltype_write;
3143
3144 // Return the symbol referred to by the relocation.
3145 static inline unsigned int
3146 get_r_sym(const Reltype* reloc)
3147 { return reloc->get_r_sym(); }
3148
47a9f4fc 3149 // Return the r_type of the relocation.
4d625b70
CC
3150 static inline unsigned int
3151 get_r_type(const Reltype* reloc)
3152 { return reloc->get_r_type(); }
3153
47a9f4fc
VR
3154 // Return the r_type2 of the relocation.
3155 static inline unsigned int
3156 get_r_type2(const Reltype* reloc)
3157 { return reloc->get_r_type2(); }
3158
3159 // Return the r_type3 of the relocation.
3160 static inline unsigned int
3161 get_r_type3(const Reltype* reloc)
3162 { return reloc->get_r_type3(); }
3163
3164 // Return the special symbol of the relocation.
3165 static inline unsigned int
3166 get_r_ssym(const Reltype* reloc)
3167 { return reloc->get_r_ssym(); }
3168
4d625b70
CC
3169 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3170 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3171 get_r_addend(const Reltype* reloc)
8a8880cb
CC
3172 {
3173 if (sh_type_ == elfcpp::SHT_REL)
3174 return 0;
3175 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3176 }
4d625b70
CC
3177
3178 // Write the r_info field to a new reloc, using the r_info field from
3179 // the original reloc, replacing the r_sym field with R_SYM.
3180 static inline void
3181 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3182 {
3183 new_reloc->put_r_sym(r_sym);
3184 new_reloc->put_r_ssym(reloc->get_r_ssym());
3185 new_reloc->put_r_type3(reloc->get_r_type3());
3186 new_reloc->put_r_type2(reloc->get_r_type2());
3187 new_reloc->put_r_type(reloc->get_r_type());
3188 }
3189
3190 // Write the r_addend field to a new reloc.
3191 static inline void
3192 put_r_addend(Reltype_write* to,
3193 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3194 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3195
3196 // Return the size of the addend of the relocation (only used for SHT_REL).
3197 static unsigned int
3198 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3199 { return mips_get_size_for_reloc(r_type, obj); }
3200};
3201
9810d34d
SS
3202template<int size, bool big_endian>
3203class Target_mips : public Sized_target<size, big_endian>
3204{
3205 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
47a9f4fc 3206 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
9810d34d 3207 Reloc_section;
9810d34d
SS
3208 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3209 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4d625b70
CC
3210 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3211 Reltype;
3212 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3213 Relatype;
9810d34d
SS
3214
3215 public:
3216 Target_mips(const Target::Target_info* info = &mips_info)
3217 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
3218 got_plt_(NULL), rel_dyn_(NULL), copy_relocs_(),
3219 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
01b84e25
VR
3220 mips_stubs_(NULL), mach_(0), layout_(NULL), got16_addends_(),
3221 entry_symbol_is_compressed_(false), insn32_(false)
9810d34d
SS
3222 {
3223 this->add_machine_extensions();
3224 }
3225
3226 // The offset of $gp from the beginning of the .got section.
3227 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3228
3229 // The maximum size of the GOT for it to be addressable using 16-bit
3230 // offsets from $gp.
3231 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3232
3233 // Make a new symbol table entry for the Mips target.
3234 Sized_symbol<size>*
dc1c8a16 3235 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
9810d34d
SS
3236 { return new Mips_symbol<size>(); }
3237
3238 // Process the relocations to determine unreferenced sections for
3239 // garbage collection.
3240 void
3241 gc_process_relocs(Symbol_table* symtab,
3242 Layout* layout,
3243 Sized_relobj_file<size, big_endian>* object,
3244 unsigned int data_shndx,
3245 unsigned int sh_type,
3246 const unsigned char* prelocs,
3247 size_t reloc_count,
3248 Output_section* output_section,
3249 bool needs_special_offset_handling,
3250 size_t local_symbol_count,
3251 const unsigned char* plocal_symbols);
3252
3253 // Scan the relocations to look for symbol adjustments.
3254 void
3255 scan_relocs(Symbol_table* symtab,
3256 Layout* layout,
3257 Sized_relobj_file<size, big_endian>* object,
3258 unsigned int data_shndx,
3259 unsigned int sh_type,
3260 const unsigned char* prelocs,
3261 size_t reloc_count,
3262 Output_section* output_section,
3263 bool needs_special_offset_handling,
3264 size_t local_symbol_count,
3265 const unsigned char* plocal_symbols);
3266
3267 // Finalize the sections.
3268 void
3269 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3270
3271 // Relocate a section.
3272 void
3273 relocate_section(const Relocate_info<size, big_endian>*,
3274 unsigned int sh_type,
3275 const unsigned char* prelocs,
3276 size_t reloc_count,
3277 Output_section* output_section,
3278 bool needs_special_offset_handling,
3279 unsigned char* view,
3280 Mips_address view_address,
3281 section_size_type view_size,
3282 const Reloc_symbol_changes*);
3283
3284 // Scan the relocs during a relocatable link.
3285 void
3286 scan_relocatable_relocs(Symbol_table* symtab,
3287 Layout* layout,
3288 Sized_relobj_file<size, big_endian>* object,
3289 unsigned int data_shndx,
3290 unsigned int sh_type,
3291 const unsigned char* prelocs,
3292 size_t reloc_count,
3293 Output_section* output_section,
3294 bool needs_special_offset_handling,
3295 size_t local_symbol_count,
3296 const unsigned char* plocal_symbols,
3297 Relocatable_relocs*);
3298
4d625b70
CC
3299 // Scan the relocs for --emit-relocs.
3300 void
3301 emit_relocs_scan(Symbol_table* symtab,
3302 Layout* layout,
3303 Sized_relobj_file<size, big_endian>* object,
3304 unsigned int data_shndx,
3305 unsigned int sh_type,
3306 const unsigned char* prelocs,
3307 size_t reloc_count,
3308 Output_section* output_section,
3309 bool needs_special_offset_handling,
3310 size_t local_symbol_count,
3311 const unsigned char* plocal_syms,
3312 Relocatable_relocs* rr);
3313
9810d34d
SS
3314 // Emit relocations for a section.
3315 void
3316 relocate_relocs(const Relocate_info<size, big_endian>*,
3317 unsigned int sh_type,
3318 const unsigned char* prelocs,
3319 size_t reloc_count,
3320 Output_section* output_section,
3321 typename elfcpp::Elf_types<size>::Elf_Off
3322 offset_in_output_section,
9810d34d
SS
3323 unsigned char* view,
3324 Mips_address view_address,
3325 section_size_type view_size,
3326 unsigned char* reloc_view,
3327 section_size_type reloc_view_size);
3328
3329 // Perform target-specific processing in a relocatable link. This is
3330 // only used if we use the relocation strategy RELOC_SPECIAL.
3331 void
3332 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3333 unsigned int sh_type,
3334 const unsigned char* preloc_in,
3335 size_t relnum,
3336 Output_section* output_section,
3337 typename elfcpp::Elf_types<size>::Elf_Off
3338 offset_in_output_section,
3339 unsigned char* view,
3340 Mips_address view_address,
3341 section_size_type view_size,
3342 unsigned char* preloc_out);
3343
3344 // Return whether SYM is defined by the ABI.
3345 bool
3346 do_is_defined_by_abi(const Symbol* sym) const
3347 {
3348 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3349 || (strcmp(sym->name(), "_gp_disp") == 0)
3350 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3351 }
3352
3353 // Return the number of entries in the GOT.
3354 unsigned int
3355 got_entry_count() const
3356 {
3357 if (!this->has_got_section())
3358 return 0;
3359 return this->got_size() / (size/8);
3360 }
3361
3362 // Return the number of entries in the PLT.
3363 unsigned int
3364 plt_entry_count() const
3365 {
3366 if (this->plt_ == NULL)
3367 return 0;
3368 return this->plt_->entry_count();
3369 }
3370
3371 // Return the offset of the first non-reserved PLT entry.
3372 unsigned int
3373 first_plt_entry_offset() const
3374 { return this->plt_->first_plt_entry_offset(); }
3375
3376 // Return the size of each PLT entry.
3377 unsigned int
3378 plt_entry_size() const
3379 { return this->plt_->plt_entry_size(); }
3380
3381 // Get the GOT section, creating it if necessary.
3382 Mips_output_data_got<size, big_endian>*
3383 got_section(Symbol_table*, Layout*);
3384
3385 // Get the GOT section.
3386 Mips_output_data_got<size, big_endian>*
3387 got_section() const
3388 {
3389 gold_assert(this->got_ != NULL);
3390 return this->got_;
3391 }
3392
3393 // Get the .MIPS.stubs section, creating it if necessary.
3394 Mips_output_data_mips_stubs<size, big_endian>*
3395 mips_stubs_section(Layout* layout);
3396
3397 // Get the .MIPS.stubs section.
3398 Mips_output_data_mips_stubs<size, big_endian>*
3399 mips_stubs_section() const
3400 {
3401 gold_assert(this->mips_stubs_ != NULL);
3402 return this->mips_stubs_;
3403 }
3404
3405 // Get the LA25 stub section, creating it if necessary.
3406 Mips_output_data_la25_stub<size, big_endian>*
3407 la25_stub_section(Layout*);
3408
3409 // Get the LA25 stub section.
3410 Mips_output_data_la25_stub<size, big_endian>*
3411 la25_stub_section()
3412 {
3413 gold_assert(this->la25_stub_ != NULL);
3414 return this->la25_stub_;
3415 }
3416
3417 // Get gp value. It has the value of .got + 0x7FF0.
3418 Mips_address
3419 gp_value() const
3420 {
3421 if (this->gp_ != NULL)
3422 return this->gp_->value();
3423 return 0;
3424 }
3425
3426 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3427 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3428 Mips_address
3429 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3430 {
3431 if (this->gp_ == NULL)
3432 return 0;
3433
3434 bool multi_got = false;
3435 if (this->has_got_section())
3436 multi_got = this->got_section()->multi_got();
3437 if (!multi_got)
3438 return this->gp_->value();
3439 else
3440 return this->gp_->value() + this->got_section()->get_got_offset(object);
3441 }
3442
3443 // Get the dynamic reloc section, creating it if necessary.
3444 Reloc_section*
3445 rel_dyn_section(Layout*);
3446
3447 bool
3448 do_has_custom_set_dynsym_indexes() const
3449 { return true; }
3450
3451 // Don't emit input .reginfo sections to output .reginfo.
3452 bool
3453 do_should_include_section(elfcpp::Elf_Word sh_type) const
3454 { return sh_type != elfcpp::SHT_MIPS_REGINFO; }
3455
3456 // Set the dynamic symbol indexes. INDEX is the index of the first
3457 // global dynamic symbol. Pointers to the symbols are stored into the
3458 // vector SYMS. The names are added to DYNPOOL. This returns an
3459 // updated dynamic symbol index.
3460 unsigned int
3461 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3462 std::vector<Symbol*>* syms, Stringpool* dynpool,
3463 Versions* versions, Symbol_table* symtab) const;
3464
3465 // Remove .MIPS.stubs entry for a symbol.
3466 void
3467 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3468 {
3469 if (this->mips_stubs_ != NULL)
3470 this->mips_stubs_->remove_entry(sym);
3471 }
3472
3473 // The value to write into got[1] for SVR4 targets, to identify it is
3474 // a GNU object. The dynamic linker can then use got[1] to store the
3475 // module pointer.
3476 uint64_t
3477 mips_elf_gnu_got1_mask()
3478 {
3479 if (this->is_output_n64())
3480 return (uint64_t)1 << 63;
3481 else
3482 return 1 << 31;
3483 }
3484
3485 // Whether the output has microMIPS code. This is valid only after
3486 // merge_processor_specific_flags() is called.
3487 bool
3488 is_output_micromips() const
3489 {
3490 gold_assert(this->are_processor_specific_flags_set());
3491 return elfcpp::is_micromips(this->processor_specific_flags());
3492 }
3493
3494 // Whether the output uses N32 ABI. This is valid only after
3495 // merge_processor_specific_flags() is called.
3496 bool
3497 is_output_n32() const
3498 {
3499 gold_assert(this->are_processor_specific_flags_set());
3500 return elfcpp::abi_n32(this->processor_specific_flags());
3501 }
3502
01b84e25 3503 // Whether the output uses N64 ABI.
9810d34d
SS
3504 bool
3505 is_output_n64() const
01b84e25 3506 { return size == 64; }
9810d34d
SS
3507
3508 // Whether the output uses NEWABI. This is valid only after
3509 // merge_processor_specific_flags() is called.
3510 bool
3511 is_output_newabi() const
3512 { return this->is_output_n32() || this->is_output_n64(); }
3513
3514 // Whether we can only use 32-bit microMIPS instructions.
3515 bool
3516 use_32bit_micromips_instructions() const
3517 { return this->insn32_; }
3518
4d625b70
CC
3519 // Return the r_sym field from a relocation.
3520 unsigned int
3521 get_r_sym(const unsigned char* preloc) const
3522 {
3523 // Since REL and RELA relocs share the same structure through
3524 // the r_info field, we can just use REL here.
3525 Reltype rel(preloc);
3526 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3527 get_r_sym(&rel);
3528 }
3529
9810d34d
SS
3530 protected:
3531 // Return the value to use for a dynamic symbol which requires special
3532 // treatment. This is how we support equality comparisons of function
3533 // pointers across shared library boundaries, as described in the
3534 // processor specific ABI supplement.
3535 uint64_t
3536 do_dynsym_value(const Symbol* gsym) const;
3537
3538 // Make an ELF object.
3539 Object*
3540 do_make_elf_object(const std::string&, Input_file*, off_t,
3541 const elfcpp::Ehdr<size, big_endian>& ehdr);
3542
3543 Object*
3544 do_make_elf_object(const std::string&, Input_file*, off_t,
3545 const elfcpp::Ehdr<size, !big_endian>&)
3546 { gold_unreachable(); }
3547
3548 // Make an output section.
3549 Output_section*
3550 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3551 elfcpp::Elf_Xword flags)
3552 {
3553 if (type == elfcpp::SHT_MIPS_REGINFO)
3554 return new Mips_output_section_reginfo<size, big_endian>(name, type,
3555 flags, this);
3556 else
3557 return new Output_section(name, type, flags);
3558 }
3559
3560 // Adjust ELF file header.
3561 void
3562 do_adjust_elf_header(unsigned char* view, int len);
3563
3564 // Get the custom dynamic tag value.
3565 unsigned int
3566 do_dynamic_tag_custom_value(elfcpp::DT) const;
3567
3568 // Adjust the value written to the dynamic symbol table.
3569 virtual void
3570 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3571 {
3572 elfcpp::Sym<size, big_endian> isym(view);
3573 elfcpp::Sym_write<size, big_endian> osym(view);
3574 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3575
3576 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3577 // to treat compressed symbols like any other.
3578 Mips_address value = isym.get_st_value();
3579 if (mips_sym->is_mips16() && value != 0)
3580 {
3581 if (!mips_sym->has_mips16_fn_stub())
3582 value |= 1;
3583 else
3584 {
3585 // If we have a MIPS16 function with a stub, the dynamic symbol
3586 // must refer to the stub, since only the stub uses the standard
3587 // calling conventions. Stub contains MIPS32 code, so don't add +1
3588 // in this case.
3589
3590 // There is a code which does this in the method
3591 // Target_mips::do_dynsym_value, but that code will only be
3592 // executed if the symbol is from dynobj.
3593 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3594 // table.
3595
3596 Mips16_stub_section<size, big_endian>* fn_stub =
3597 mips_sym->template get_mips16_fn_stub<big_endian>();
3598 value = fn_stub->output_address();
3599 osym.put_st_size(fn_stub->section_size());
3600 }
3601
3602 osym.put_st_value(value);
3603 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3604 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3605 }
3606 else if ((mips_sym->is_micromips()
3607 // Stubs are always microMIPS if there is any microMIPS code in
3608 // the output.
3609 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3610 && value != 0)
3611 {
3612 osym.put_st_value(value | 1);
3613 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3614 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3615 }
3616 }
3617
3618 private:
3619 // The class which scans relocations.
3620 class Scan
3621 {
3622 public:
3623 Scan()
3624 { }
3625
3626 static inline int
3627 get_reference_flags(unsigned int r_type);
3628
3629 inline void
3630 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3631 Sized_relobj_file<size, big_endian>* object,
3632 unsigned int data_shndx,
3633 Output_section* output_section,
4d625b70 3634 const Reltype& reloc, unsigned int r_type,
9810d34d
SS
3635 const elfcpp::Sym<size, big_endian>& lsym,
3636 bool is_discarded);
3637
3638 inline void
3639 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3640 Sized_relobj_file<size, big_endian>* object,
3641 unsigned int data_shndx,
3642 Output_section* output_section,
4d625b70 3643 const Relatype& reloc, unsigned int r_type,
9810d34d
SS
3644 const elfcpp::Sym<size, big_endian>& lsym,
3645 bool is_discarded);
3646
3647 inline void
3648 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3649 Sized_relobj_file<size, big_endian>* object,
3650 unsigned int data_shndx,
3651 Output_section* output_section,
4d625b70
CC
3652 const Relatype* rela,
3653 const Reltype* rel,
9810d34d
SS
3654 unsigned int rel_type,
3655 unsigned int r_type,
3656 const elfcpp::Sym<size, big_endian>& lsym,
3657 bool is_discarded);
3658
3659 inline void
3660 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3661 Sized_relobj_file<size, big_endian>* object,
3662 unsigned int data_shndx,
3663 Output_section* output_section,
4d625b70 3664 const Reltype& reloc, unsigned int r_type,
9810d34d
SS
3665 Symbol* gsym);
3666
3667 inline void
3668 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3669 Sized_relobj_file<size, big_endian>* object,
3670 unsigned int data_shndx,
3671 Output_section* output_section,
4d625b70 3672 const Relatype& reloc, unsigned int r_type,
9810d34d
SS
3673 Symbol* gsym);
3674
3675 inline void
3676 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3677 Sized_relobj_file<size, big_endian>* object,
3678 unsigned int data_shndx,
3679 Output_section* output_section,
4d625b70
CC
3680 const Relatype* rela,
3681 const Reltype* rel,
9810d34d
SS
3682 unsigned int rel_type,
3683 unsigned int r_type,
3684 Symbol* gsym);
3685
3686 inline bool
3687 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3688 Target_mips*,
3689 Sized_relobj_file<size, big_endian>*,
3690 unsigned int,
3691 Output_section*,
4d625b70 3692 const Reltype&,
9810d34d
SS
3693 unsigned int,
3694 const elfcpp::Sym<size, big_endian>&)
3695 { return false; }
3696
3697 inline bool
3698 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3699 Target_mips*,
3700 Sized_relobj_file<size, big_endian>*,
3701 unsigned int,
3702 Output_section*,
4d625b70 3703 const Reltype&,
9810d34d
SS
3704 unsigned int, Symbol*)
3705 { return false; }
3706
3707 inline bool
3708 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3709 Target_mips*,
3710 Sized_relobj_file<size, big_endian>*,
3711 unsigned int,
3712 Output_section*,
4d625b70 3713 const Relatype&,
9810d34d
SS
3714 unsigned int,
3715 const elfcpp::Sym<size, big_endian>&)
3716 { return false; }
3717
3718 inline bool
3719 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3720 Target_mips*,
3721 Sized_relobj_file<size, big_endian>*,
3722 unsigned int,
3723 Output_section*,
4d625b70 3724 const Relatype&,
9810d34d
SS
3725 unsigned int, Symbol*)
3726 { return false; }
3727 private:
3728 static void
3729 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3730 unsigned int r_type);
3731
3732 static void
3733 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3734 unsigned int r_type, Symbol*);
3735 };
3736
3737 // The class which implements relocation.
3738 class Relocate
3739 {
3740 public:
3741 Relocate()
3742 { }
3743
3744 ~Relocate()
3745 { }
3746
47a9f4fc 3747 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
9810d34d 3748 inline bool
47a9f4fc
VR
3749 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3750 unsigned int r_type,
3751 Output_section* output_section,
3752 Target_mips* target);
9810d34d
SS
3753
3754 // Do a relocation. Return false if the caller should not issue
3755 // any warnings about this relocation.
3756 inline bool
91a65d2f
AM
3757 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3758 Target_mips*, Output_section*, size_t, const unsigned char*,
3759 const Sized_symbol<size>*, const Symbol_value<size>*,
3760 unsigned char*, Mips_address, section_size_type);
9810d34d
SS
3761 };
3762
9810d34d
SS
3763 // This POD class holds the dynamic relocations that should be emitted instead
3764 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3765 // relocations if it turns out that the symbol does not have static
3766 // relocations.
3767 class Dyn_reloc
3768 {
3769 public:
3770 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3771 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3772 Output_section* output_section, Mips_address r_offset)
3773 : sym_(sym), r_type_(r_type), relobj_(relobj),
3774 shndx_(shndx), output_section_(output_section),
3775 r_offset_(r_offset)
3776 { }
3777
3778 // Emit this reloc if appropriate. This is called after we have
3779 // scanned all the relocations, so we know whether the symbol has
3780 // static relocations.
3781 void
3782 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3783 Symbol_table* symtab)
3784 {
3785 if (!this->sym_->has_static_relocs())
3786 {
3787 got->record_global_got_symbol(this->sym_, this->relobj_,
3788 this->r_type_, true, false);
3789 if (!symbol_references_local(this->sym_,
3790 this->sym_->should_add_dynsym_entry(symtab)))
3791 rel_dyn->add_global(this->sym_, this->r_type_,
3792 this->output_section_, this->relobj_,
3793 this->shndx_, this->r_offset_);
3794 else
3795 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3796 this->output_section_, this->relobj_,
3797 this->shndx_, this->r_offset_);
3798 }
3799 }
3800
3801 private:
3802 Mips_symbol<size>* sym_;
3803 unsigned int r_type_;
3804 Mips_relobj<size, big_endian>* relobj_;
3805 unsigned int shndx_;
3806 Output_section* output_section_;
3807 Mips_address r_offset_;
3808 };
3809
3810 // Adjust TLS relocation type based on the options and whether this
3811 // is a local symbol.
3812 static tls::Tls_optimization
3813 optimize_tls_reloc(bool is_final, int r_type);
3814
3815 // Return whether there is a GOT section.
3816 bool
3817 has_got_section() const
3818 { return this->got_ != NULL; }
3819
3820 // Check whether the given ELF header flags describe a 32-bit binary.
3821 bool
3822 mips_32bit_flags(elfcpp::Elf_Word);
3823
3824 enum Mips_mach {
3825 mach_mips3000 = 3000,
3826 mach_mips3900 = 3900,
3827 mach_mips4000 = 4000,
3828 mach_mips4010 = 4010,
3829 mach_mips4100 = 4100,
3830 mach_mips4111 = 4111,
3831 mach_mips4120 = 4120,
3832 mach_mips4300 = 4300,
3833 mach_mips4400 = 4400,
3834 mach_mips4600 = 4600,
3835 mach_mips4650 = 4650,
3836 mach_mips5000 = 5000,
3837 mach_mips5400 = 5400,
3838 mach_mips5500 = 5500,
3839 mach_mips6000 = 6000,
3840 mach_mips7000 = 7000,
3841 mach_mips8000 = 8000,
3842 mach_mips9000 = 9000,
3843 mach_mips10000 = 10000,
3844 mach_mips12000 = 12000,
3845 mach_mips14000 = 14000,
3846 mach_mips16000 = 16000,
3847 mach_mips16 = 16,
3848 mach_mips5 = 5,
3849 mach_mips_loongson_2e = 3001,
3850 mach_mips_loongson_2f = 3002,
3851 mach_mips_loongson_3a = 3003,
3852 mach_mips_sb1 = 12310201, // octal 'SB', 01
3853 mach_mips_octeon = 6501,
3854 mach_mips_octeonp = 6601,
3855 mach_mips_octeon2 = 6502,
3856 mach_mips_xlr = 887682, // decimal 'XLR'
3857 mach_mipsisa32 = 32,
3858 mach_mipsisa32r2 = 33,
3859 mach_mipsisa64 = 64,
3860 mach_mipsisa64r2 = 65,
3861 mach_mips_micromips = 96
3862 };
3863
3864 // Return the MACH for a MIPS e_flags value.
3865 unsigned int
3866 elf_mips_mach(elfcpp::Elf_Word);
3867
3868 // Check whether machine EXTENSION is an extension of machine BASE.
3869 bool
3870 mips_mach_extends(unsigned int, unsigned int);
3871
3872 // Merge processor specific flags.
3873 void
01b84e25 3874 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word, bool);
9810d34d
SS
3875
3876 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
3877 static inline bool
3878 jal_to_bal()
3879 { return false; }
3880
3881 // True if we are linking for CPUs that are faster if JALR is converted to
3882 // BAL. This should be safe for all architectures. We enable this predicate
3883 // for all CPUs.
3884 static inline bool
3885 jalr_to_bal()
3886 { return true; }
3887
3888 // True if we are linking for CPUs that are faster if JR is converted to B.
3889 // This should be safe for all architectures. We enable this predicate for
3890 // all CPUs.
3891 static inline bool
3892 jr_to_b()
3893 { return true; }
3894
3895 // Return the size of the GOT section.
3896 section_size_type
3897 got_size() const
3898 {
3899 gold_assert(this->got_ != NULL);
3900 return this->got_->data_size();
3901 }
3902
3903 // Create a PLT entry for a global symbol referenced by r_type relocation.
3904 void
3905 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
3906 unsigned int r_type);
3907
3908 // Get the PLT section.
3909 Mips_output_data_plt<size, big_endian>*
3910 plt_section() const
3911 {
3912 gold_assert(this->plt_ != NULL);
3913 return this->plt_;
3914 }
3915
3916 // Get the GOT PLT section.
3917 const Mips_output_data_plt<size, big_endian>*
3918 got_plt_section() const
3919 {
3920 gold_assert(this->got_plt_ != NULL);
3921 return this->got_plt_;
3922 }
3923
3924 // Copy a relocation against a global symbol.
3925 void
3926 copy_reloc(Symbol_table* symtab, Layout* layout,
3927 Sized_relobj_file<size, big_endian>* object,
3928 unsigned int shndx, Output_section* output_section,
47a9f4fc 3929 Symbol* sym, unsigned int r_type, Mips_address r_offset)
9810d34d
SS
3930 {
3931 this->copy_relocs_.copy_reloc(symtab, layout,
3932 symtab->get_sized_symbol<size>(sym),
3933 object, shndx, output_section,
47a9f4fc 3934 r_type, r_offset, 0,
859d7987 3935 this->rel_dyn_section(layout));
9810d34d
SS
3936 }
3937
3938 void
3939 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3940 Mips_relobj<size, big_endian>* relobj,
3941 unsigned int shndx, Output_section* output_section,
3942 Mips_address r_offset)
3943 {
3944 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
3945 output_section, r_offset));
3946 }
3947
3948 // Calculate value of _gp symbol.
3949 void
3950 set_gp(Layout*, Symbol_table*);
3951
3952 const char*
01b84e25 3953 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
9810d34d
SS
3954 const char*
3955 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
3956
3957 // Adds entries that describe how machines relate to one another. The entries
3958 // are ordered topologically with MIPS I extensions listed last. First
3959 // element is extension, second element is base.
3960 void
3961 add_machine_extensions()
3962 {
3963 // MIPS64r2 extensions.
3964 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
3965 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
3966 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
3967
3968 // MIPS64 extensions.
3969 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
3970 this->add_extension(mach_mips_sb1, mach_mipsisa64);
3971 this->add_extension(mach_mips_xlr, mach_mipsisa64);
3972 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64);
3973
3974 // MIPS V extensions.
3975 this->add_extension(mach_mipsisa64, mach_mips5);
3976
3977 // R10000 extensions.
3978 this->add_extension(mach_mips12000, mach_mips10000);
3979 this->add_extension(mach_mips14000, mach_mips10000);
3980 this->add_extension(mach_mips16000, mach_mips10000);
3981
3982 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
3983 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
3984 // better to allow vr5400 and vr5500 code to be merged anyway, since
3985 // many libraries will just use the core ISA. Perhaps we could add
3986 // some sort of ASE flag if this ever proves a problem.
3987 this->add_extension(mach_mips5500, mach_mips5400);
3988 this->add_extension(mach_mips5400, mach_mips5000);
3989
3990 // MIPS IV extensions.
3991 this->add_extension(mach_mips5, mach_mips8000);
3992 this->add_extension(mach_mips10000, mach_mips8000);
3993 this->add_extension(mach_mips5000, mach_mips8000);
3994 this->add_extension(mach_mips7000, mach_mips8000);
3995 this->add_extension(mach_mips9000, mach_mips8000);
3996
3997 // VR4100 extensions.
3998 this->add_extension(mach_mips4120, mach_mips4100);
3999 this->add_extension(mach_mips4111, mach_mips4100);
4000
4001 // MIPS III extensions.
4002 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4003 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4004 this->add_extension(mach_mips8000, mach_mips4000);
4005 this->add_extension(mach_mips4650, mach_mips4000);
4006 this->add_extension(mach_mips4600, mach_mips4000);
4007 this->add_extension(mach_mips4400, mach_mips4000);
4008 this->add_extension(mach_mips4300, mach_mips4000);
4009 this->add_extension(mach_mips4100, mach_mips4000);
4010 this->add_extension(mach_mips4010, mach_mips4000);
4011
4012 // MIPS32 extensions.
4013 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4014
4015 // MIPS II extensions.
4016 this->add_extension(mach_mips4000, mach_mips6000);
4017 this->add_extension(mach_mipsisa32, mach_mips6000);
4018
4019 // MIPS I extensions.
4020 this->add_extension(mach_mips6000, mach_mips3000);
4021 this->add_extension(mach_mips3900, mach_mips3000);
4022 }
4023
4024 // Add value to MIPS extenstions.
4025 void
4026 add_extension(unsigned int base, unsigned int extension)
4027 {
4028 std::pair<unsigned int, unsigned int> ext(base, extension);
4029 this->mips_mach_extensions_.push_back(ext);
4030 }
4031
4032 // Return the number of entries in the .dynsym section.
4033 unsigned int get_dt_mips_symtabno() const
4034 {
4035 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4036 / elfcpp::Elf_sizes<size>::sym_size));
4037 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4038 }
4039
4040 // Information about this specific target which we pass to the
4041 // general Target structure.
62661c93 4042 static const Target::Target_info mips_info;
9810d34d
SS
4043 // The GOT section.
4044 Mips_output_data_got<size, big_endian>* got_;
4045 // gp symbol. It has the value of .got + 0x7FF0.
4046 Sized_symbol<size>* gp_;
4047 // The PLT section.
4048 Mips_output_data_plt<size, big_endian>* plt_;
4049 // The GOT PLT section.
4050 Output_data_space* got_plt_;
4051 // The dynamic reloc section.
4052 Reloc_section* rel_dyn_;
4053 // Relocs saved to avoid a COPY reloc.
4054 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4055
4056 // A list of dyn relocs to be saved.
4057 std::vector<Dyn_reloc> dyn_relocs_;
4058
4059 // The LA25 stub section.
4060 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4061 // Architecture extensions.
4062 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4063 // .MIPS.stubs
4064 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4065
9810d34d
SS
4066 unsigned int mach_;
4067 Layout* layout_;
4068
4069 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4070
4071 // Whether the entry symbol is mips16 or micromips.
4072 bool entry_symbol_is_compressed_;
4073
4074 // Whether we can use only 32-bit microMIPS instructions.
4075 // TODO(sasa): This should be a linker option.
4076 bool insn32_;
4077};
4078
9810d34d
SS
4079// Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4080// It records high part of the relocation pair.
4081
4082template<int size, bool big_endian>
4083struct reloc_high
4084{
4085 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4086
4087 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4088 const Symbol_value<size>* _psymval, Mips_address _addend,
3d0064a9 4089 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
9810d34d
SS
4090 Mips_address _address = 0, bool _gp_disp = false)
4091 : view(_view), object(_object), psymval(_psymval), addend(_addend),
3d0064a9
CC
4092 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4093 address(_address), gp_disp(_gp_disp)
9810d34d
SS
4094 { }
4095
4096 unsigned char* view;
4097 const Mips_relobj<size, big_endian>* object;
4098 const Symbol_value<size>* psymval;
4099 Mips_address addend;
4100 unsigned int r_type;
3d0064a9 4101 unsigned int r_sym;
9810d34d
SS
4102 bool extract_addend;
4103 Mips_address address;
4104 bool gp_disp;
4105};
4106
4107template<int size, bool big_endian>
4108class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4109{
4110 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
47a9f4fc 4111 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
9810d34d
SS
4112 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4113 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
47a9f4fc 4114 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
9810d34d
SS
4115
4116 public:
4117 typedef enum
4118 {
4119 STATUS_OKAY, // No error during relocation.
4120 STATUS_OVERFLOW, // Relocation overflow.
4121 STATUS_BAD_RELOC // Relocation cannot be applied.
4122 } Status;
4123
4124 private:
4125 typedef Relocate_functions<size, big_endian> Base;
4126 typedef Mips_relocate_functions<size, big_endian> This;
4127
4128 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4129 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
4130
47a9f4fc
VR
4131 template<int valsize>
4132 static inline typename This::Status
4133 check_overflow(Valtype value)
4134 {
4135 if (size == 32)
4136 return (Bits<valsize>::has_overflow32(value)
4137 ? This::STATUS_OVERFLOW
4138 : This::STATUS_OKAY);
4139
4140 return (Bits<valsize>::has_overflow(value)
4141 ? This::STATUS_OVERFLOW
4142 : This::STATUS_OKAY);
4143 }
4144
4145 static inline bool
4146 should_shuffle_micromips_reloc(unsigned int r_type)
4147 {
4148 return (micromips_reloc(r_type)
4149 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4150 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4151 }
4152
4153 public:
9810d34d
SS
4154 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4155 // Most mips16 instructions are 16 bits, but these instructions
4156 // are 32 bits.
4157 //
4158 // The format of these instructions is:
4159 //
4160 // +--------------+--------------------------------+
4161 // | JALX | X| Imm 20:16 | Imm 25:21 |
4162 // +--------------+--------------------------------+
4163 // | Immediate 15:0 |
4164 // +-----------------------------------------------+
4165 //
4166 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4167 // Note that the immediate value in the first word is swapped.
4168 //
4169 // When producing a relocatable object file, R_MIPS16_26 is
4170 // handled mostly like R_MIPS_26. In particular, the addend is
4171 // stored as a straight 26-bit value in a 32-bit instruction.
4172 // (gas makes life simpler for itself by never adjusting a
4173 // R_MIPS16_26 reloc to be against a section, so the addend is
4174 // always zero). However, the 32 bit instruction is stored as 2
4175 // 16-bit values, rather than a single 32-bit value. In a
4176 // big-endian file, the result is the same; in a little-endian
4177 // file, the two 16-bit halves of the 32 bit value are swapped.
4178 // This is so that a disassembler can recognize the jal
4179 // instruction.
4180 //
4181 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4182 // instruction stored as two 16-bit values. The addend A is the
4183 // contents of the targ26 field. The calculation is the same as
4184 // R_MIPS_26. When storing the calculated value, reorder the
4185 // immediate value as shown above, and don't forget to store the
4186 // value as two 16-bit values.
4187 //
4188 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4189 // defined as
4190 //
4191 // big-endian:
4192 // +--------+----------------------+
4193 // | | |
4194 // | | targ26-16 |
4195 // |31 26|25 0|
4196 // +--------+----------------------+
4197 //
4198 // little-endian:
4199 // +----------+------+-------------+
4200 // | | | |
4201 // | sub1 | | sub2 |
4202 // |0 9|10 15|16 31|
4203 // +----------+--------------------+
4204 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4205 // ((sub1 << 16) | sub2)).
4206 //
4207 // When producing a relocatable object file, the calculation is
4208 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4209 // When producing a fully linked file, the calculation is
4210 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4211 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4212 //
4213 // The table below lists the other MIPS16 instruction relocations.
4214 // Each one is calculated in the same way as the non-MIPS16 relocation
4215 // given on the right, but using the extended MIPS16 layout of 16-bit
4216 // immediate fields:
4217 //
4218 // R_MIPS16_GPREL R_MIPS_GPREL16
4219 // R_MIPS16_GOT16 R_MIPS_GOT16
4220 // R_MIPS16_CALL16 R_MIPS_CALL16
4221 // R_MIPS16_HI16 R_MIPS_HI16
4222 // R_MIPS16_LO16 R_MIPS_LO16
4223 //
4224 // A typical instruction will have a format like this:
4225 //
4226 // +--------------+--------------------------------+
4227 // | EXTEND | Imm 10:5 | Imm 15:11 |
4228 // +--------------+--------------------------------+
4229 // | Major | rx | ry | Imm 4:0 |
4230 // +--------------+--------------------------------+
4231 //
4232 // EXTEND is the five bit value 11110. Major is the instruction
4233 // opcode.
4234 //
4235 // All we need to do here is shuffle the bits appropriately.
4236 // As above, the two 16-bit halves must be swapped on a
4237 // little-endian system.
4238
4239 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4240 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4241 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4242
9810d34d
SS
4243 static void
4244 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4245 bool jal_shuffle)
4246 {
4247 if (!mips16_reloc(r_type)
4248 && !should_shuffle_micromips_reloc(r_type))
4249 return;
4250
4251 // Pick up the first and second halfwords of the instruction.
4252 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4253 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4254 Valtype32 val;
4255
4256 if (micromips_reloc(r_type)
4257 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4258 val = first << 16 | second;
4259 else if (r_type != elfcpp::R_MIPS16_26)
4260 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4261 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4262 else
4263 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4264 | ((first & 0x1f) << 21) | second);
4265
4266 elfcpp::Swap<32, big_endian>::writeval(view, val);
4267 }
4268
4269 static void
4270 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4271 {
4272 if (!mips16_reloc(r_type)
4273 && !should_shuffle_micromips_reloc(r_type))
4274 return;
4275
4276 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4277 Valtype16 first, second;
4278
4279 if (micromips_reloc(r_type)
4280 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4281 {
4282 second = val & 0xffff;
4283 first = val >> 16;
4284 }
4285 else if (r_type != elfcpp::R_MIPS16_26)
4286 {
4287 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4288 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4289 }
4290 else
4291 {
4292 second = val & 0xffff;
4293 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4294 | ((val >> 21) & 0x1f);
4295 }
4296
4297 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4298 elfcpp::Swap<16, big_endian>::writeval(view, first);
4299 }
4300
9810d34d
SS
4301 // R_MIPS_16: S + sign-extend(A)
4302 static inline typename This::Status
4303 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4304 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc 4305 bool extract_addend, bool calculate_only, Valtype* calculated_value)
9810d34d 4306 {
9810d34d
SS
4307 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4308 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4309
47a9f4fc
VR
4310 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4311 : addend_a);
9810d34d 4312
47a9f4fc 4313 Valtype x = psymval->value(object, addend);
9810d34d 4314 val = Bits<16>::bit_select32(val, x, 0xffffU);
47a9f4fc
VR
4315
4316 if (calculate_only)
4317 {
4318 *calculated_value = x;
4319 return This::STATUS_OKAY;
4320 }
4321 else
4322 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4323
4324 return check_overflow<16>(x);
9810d34d
SS
4325 }
4326
4327 // R_MIPS_32: S + A
4328 static inline typename This::Status
4329 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4330 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc 4331 bool extract_addend, bool calculate_only, Valtype* calculated_value)
9810d34d 4332 {
9810d34d 4333 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
47a9f4fc 4334 Valtype addend = (extract_addend
9810d34d 4335 ? elfcpp::Swap<32, big_endian>::readval(wv)
47a9f4fc
VR
4336 : addend_a);
4337 Valtype x = psymval->value(object, addend);
4338
4339 if (calculate_only)
4340 *calculated_value = x;
4341 else
4342 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4343
9810d34d
SS
4344 return This::STATUS_OKAY;
4345 }
4346
4347 // R_MIPS_JALR, R_MICROMIPS_JALR
4348 static inline typename This::Status
4349 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4350 const Symbol_value<size>* psymval, Mips_address address,
4351 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
47a9f4fc
VR
4352 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4353 bool calculate_only, Valtype* calculated_value)
9810d34d 4354 {
9810d34d 4355 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
47a9f4fc 4356 Valtype addend = extract_addend ? 0 : addend_a;
9810d34d
SS
4357 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4358
4359 // Try converting J(AL)R to B(AL), if the target is in range.
4360 if (!parameters->options().relocatable()
4361 && r_type == elfcpp::R_MIPS_JALR
4362 && !cross_mode_jump
4363 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4364 || (jr_to_b && val == 0x03200008))) // jr t9
4365 {
4366 int offset = psymval->value(object, addend) - (address + 4);
4367 if (!Bits<18>::has_overflow32(offset))
4368 {
4369 if (val == 0x03200008) // jr t9
4370 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4371 else
4372 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4373 }
4374 }
4375
47a9f4fc
VR
4376 if (calculate_only)
4377 *calculated_value = val;
4378 else
4379 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4380
9810d34d
SS
4381 return This::STATUS_OKAY;
4382 }
4383
4384 // R_MIPS_PC32: S + A - P
4385 static inline typename This::Status
4386 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4387 const Symbol_value<size>* psymval, Mips_address address,
47a9f4fc
VR
4388 Mips_address addend_a, bool extract_addend, bool calculate_only,
4389 Valtype* calculated_value)
9810d34d 4390 {
9810d34d 4391 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
47a9f4fc 4392 Valtype addend = (extract_addend
9810d34d 4393 ? elfcpp::Swap<32, big_endian>::readval(wv)
47a9f4fc
VR
4394 : addend_a);
4395 Valtype x = psymval->value(object, addend) - address;
4396
4397 if (calculate_only)
4398 *calculated_value = x;
4399 else
4400 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4401
9810d34d
SS
4402 return This::STATUS_OKAY;
4403 }
4404
4405 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4406 static inline typename This::Status
4407 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4408 const Symbol_value<size>* psymval, Mips_address address,
4409 bool local, Mips_address addend_a, bool extract_addend,
4410 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
47a9f4fc 4411 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
9810d34d 4412 {
9810d34d
SS
4413 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4414 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4415
47a9f4fc 4416 Valtype addend;
9810d34d
SS
4417 if (extract_addend)
4418 {
4419 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4420 addend = (val & 0x03ffffff) << 1;
4421 else
4422 addend = (val & 0x03ffffff) << 2;
4423 }
4424 else
4425 addend = addend_a;
4426
4427 // Make sure the target of JALX is word-aligned. Bit 0 must be
4428 // the correct ISA mode selector and bit 1 must be 0.
47a9f4fc 4429 if (!calculate_only && cross_mode_jump
9810d34d
SS
4430 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4431 {
4432 gold_warning(_("JALX to a non-word-aligned address"));
9810d34d
SS
4433 return This::STATUS_BAD_RELOC;
4434 }
4435
4436 // Shift is 2, unusually, for microMIPS JALX.
4437 unsigned int shift =
4438 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4439
47a9f4fc 4440 Valtype x;
9810d34d
SS
4441 if (local)
4442 x = addend | ((address + 4) & (0xfc000000 << shift));
4443 else
4444 {
4445 if (shift == 1)
4446 x = Bits<27>::sign_extend32(addend);
4447 else
4448 x = Bits<28>::sign_extend32(addend);
4449 }
4450 x = psymval->value(object, x) >> shift;
4451
47a9f4fc 4452 if (!calculate_only && !local && !gsym->is_weak_undefined())
9810d34d
SS
4453 {
4454 if ((x >> 26) != ((address + 4) >> (26 + shift)))
4455 {
4456 gold_error(_("relocation truncated to fit: %u against '%s'"),
4457 r_type, gsym->name());
4458 return This::STATUS_OVERFLOW;
4459 }
4460 }
4461
4462 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4463
4464 // If required, turn JAL into JALX.
4465 if (cross_mode_jump)
4466 {
4467 bool ok;
4468 Valtype32 opcode = val >> 26;
4469 Valtype32 jalx_opcode;
4470
4471 // Check to see if the opcode is already JAL or JALX.
4472 if (r_type == elfcpp::R_MIPS16_26)
4473 {
4474 ok = (opcode == 0x6) || (opcode == 0x7);
4475 jalx_opcode = 0x7;
4476 }
4477 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4478 {
4479 ok = (opcode == 0x3d) || (opcode == 0x3c);
4480 jalx_opcode = 0x3c;
4481 }
4482 else
4483 {
4484 ok = (opcode == 0x3) || (opcode == 0x1d);
4485 jalx_opcode = 0x1d;
4486 }
4487
4488 // If the opcode is not JAL or JALX, there's a problem. We cannot
4489 // convert J or JALS to JALX.
47a9f4fc 4490 if (!calculate_only && !ok)
9810d34d
SS
4491 {
4492 gold_error(_("Unsupported jump between ISA modes; consider "
4493 "recompiling with interlinking enabled."));
4494 return This::STATUS_BAD_RELOC;
4495 }
4496
4497 // Make this the JALX opcode.
4498 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4499 }
4500
4501 // Try converting JAL to BAL, if the target is in range.
4502 if (!parameters->options().relocatable()
4503 && !cross_mode_jump
4504 && ((jal_to_bal
4505 && r_type == elfcpp::R_MIPS_26
4506 && (val >> 26) == 0x3))) // jal addr
4507 {
4508 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4509 int offset = dest - (address + 4);
4510 if (!Bits<18>::has_overflow32(offset))
4511 {
4512 if (val == 0x03200008) // jr t9
4513 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4514 else
4515 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4516 }
4517 }
4518
47a9f4fc
VR
4519 if (calculate_only)
4520 *calculated_value = val;
4521 else
4522 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4523
9810d34d
SS
4524 return This::STATUS_OKAY;
4525 }
4526
4527 // R_MIPS_PC16
4528 static inline typename This::Status
4529 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4530 const Symbol_value<size>* psymval, Mips_address address,
47a9f4fc
VR
4531 Mips_address addend_a, bool extract_addend, bool calculate_only,
4532 Valtype* calculated_value)
9810d34d 4533 {
9810d34d
SS
4534 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4535 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4536
47a9f4fc
VR
4537 Valtype addend = (extract_addend
4538 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4539 : addend_a);
9810d34d 4540
47a9f4fc 4541 Valtype x = psymval->value(object, addend) - address;
9810d34d 4542 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
47a9f4fc
VR
4543
4544 if (calculate_only)
4545 {
4546 *calculated_value = x >> 2;
4547 return This::STATUS_OKAY;
4548 }
4549 else
4550 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4551
4552 return check_overflow<18>(x);
9810d34d
SS
4553 }
4554
4555 // R_MICROMIPS_PC7_S1
4556 static inline typename This::Status
4557 relmicromips_pc7_s1(unsigned char* view,
4558 const Mips_relobj<size, big_endian>* object,
4559 const Symbol_value<size>* psymval, Mips_address address,
4560 Mips_address addend_a, bool extract_addend,
47a9f4fc 4561 bool calculate_only, Valtype* calculated_value)
9810d34d 4562 {
9810d34d
SS
4563 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4564 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4565
47a9f4fc
VR
4566 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4567 : addend_a;
9810d34d 4568
47a9f4fc 4569 Valtype x = psymval->value(object, addend) - address;
9810d34d 4570 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
47a9f4fc
VR
4571
4572 if (calculate_only)
4573 {
4574 *calculated_value = x >> 1;
4575 return This::STATUS_OKAY;
4576 }
4577 else
4578 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4579
4580 return check_overflow<8>(x);
9810d34d
SS
4581 }
4582
4583 // R_MICROMIPS_PC10_S1
4584 static inline typename This::Status
4585 relmicromips_pc10_s1(unsigned char* view,
4586 const Mips_relobj<size, big_endian>* object,
4587 const Symbol_value<size>* psymval, Mips_address address,
4588 Mips_address addend_a, bool extract_addend,
47a9f4fc 4589 bool calculate_only, Valtype* calculated_value)
9810d34d 4590 {
9810d34d
SS
4591 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4592 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4593
47a9f4fc
VR
4594 Valtype addend = (extract_addend
4595 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4596 : addend_a);
9810d34d 4597
47a9f4fc 4598 Valtype x = psymval->value(object, addend) - address;
9810d34d 4599 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
47a9f4fc
VR
4600
4601 if (calculate_only)
4602 {
4603 *calculated_value = x >> 1;
4604 return This::STATUS_OKAY;
4605 }
4606 else
4607 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4608
4609 return check_overflow<11>(x);
9810d34d
SS
4610 }
4611
4612 // R_MICROMIPS_PC16_S1
4613 static inline typename This::Status
4614 relmicromips_pc16_s1(unsigned char* view,
4615 const Mips_relobj<size, big_endian>* object,
4616 const Symbol_value<size>* psymval, Mips_address address,
4617 Mips_address addend_a, bool extract_addend,
47a9f4fc 4618 bool calculate_only, Valtype* calculated_value)
9810d34d 4619 {
9810d34d
SS
4620 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4621 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4622
47a9f4fc
VR
4623 Valtype addend = (extract_addend
4624 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
4625 : addend_a);
9810d34d 4626
47a9f4fc 4627 Valtype x = psymval->value(object, addend) - address;
9810d34d 4628 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
47a9f4fc
VR
4629
4630 if (calculate_only)
4631 {
4632 *calculated_value = x >> 1;
4633 return This::STATUS_OKAY;
4634 }
4635 else
4636 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4637
4638 return check_overflow<17>(x);
9810d34d
SS
4639 }
4640
4641 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4642 static inline typename This::Status
4643 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4644 const Symbol_value<size>* psymval, Mips_address addend,
4645 Mips_address address, bool gp_disp, unsigned int r_type,
3d0064a9 4646 unsigned int r_sym, bool extract_addend)
9810d34d
SS
4647 {
4648 // Record the relocation. It will be resolved when we find lo16 part.
4649 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
3d0064a9
CC
4650 addend, r_type, r_sym, extract_addend, address,
4651 gp_disp));
9810d34d
SS
4652 return This::STATUS_OKAY;
4653 }
4654
4655 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
4656 static inline typename This::Status
4657 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4658 const Symbol_value<size>* psymval, Mips_address addend_hi,
4659 Mips_address address, bool is_gp_disp, unsigned int r_type,
4660 bool extract_addend, Valtype32 addend_lo,
47a9f4fc
VR
4661 Target_mips<size, big_endian>* target, bool calculate_only,
4662 Valtype* calculated_value)
9810d34d 4663 {
9810d34d
SS
4664 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4665 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4666
47a9f4fc 4667 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
9810d34d
SS
4668 : addend_hi);
4669
4670 Valtype32 value;
4671 if (!is_gp_disp)
4672 value = psymval->value(object, addend);
4673 else
4674 {
4675 // For MIPS16 ABI code we generate this sequence
4676 // 0: li $v0,%hi(_gp_disp)
4677 // 4: addiupc $v1,%lo(_gp_disp)
4678 // 8: sll $v0,16
4679 // 12: addu $v0,$v1
4680 // 14: move $gp,$v0
4681 // So the offsets of hi and lo relocs are the same, but the
4682 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
4683 // ADDIUPC clears the low two bits of the instruction address,
4684 // so the base is ($t9 + 4) & ~3.
4685 Valtype32 gp_disp;
4686 if (r_type == elfcpp::R_MIPS16_HI16)
4687 gp_disp = (target->adjusted_gp_value(object)
4688 - ((address + 4) & ~0x3));
4689 // The microMIPS .cpload sequence uses the same assembly
4690 // instructions as the traditional psABI version, but the
4691 // incoming $t9 has the low bit set.
4692 else if (r_type == elfcpp::R_MICROMIPS_HI16)
4693 gp_disp = target->adjusted_gp_value(object) - address - 1;
4694 else
4695 gp_disp = target->adjusted_gp_value(object) - address;
4696 value = gp_disp + addend;
4697 }
47a9f4fc 4698 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
9810d34d 4699 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
4700
4701 if (calculate_only)
4702 {
4703 *calculated_value = x;
4704 return This::STATUS_OKAY;
4705 }
4706 else
4707 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4708
4709 return (is_gp_disp ? check_overflow<16>(x)
4710 : This::STATUS_OKAY);
9810d34d
SS
4711 }
4712
4713 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4714 static inline typename This::Status
4715 relgot16_local(unsigned char* view,
4716 const Mips_relobj<size, big_endian>* object,
4717 const Symbol_value<size>* psymval, Mips_address addend_a,
3d0064a9 4718 bool extract_addend, unsigned int r_type, unsigned int r_sym)
9810d34d
SS
4719 {
4720 // Record the relocation. It will be resolved when we find lo16 part.
4721 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
3d0064a9 4722 addend_a, r_type, r_sym, extract_addend));
9810d34d
SS
4723 return This::STATUS_OKAY;
4724 }
4725
4726 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4727 static inline typename This::Status
4728 do_relgot16_local(unsigned char* view,
4729 const Mips_relobj<size, big_endian>* object,
4730 const Symbol_value<size>* psymval, Mips_address addend_hi,
47a9f4fc
VR
4731 bool extract_addend, Valtype32 addend_lo,
4732 Target_mips<size, big_endian>* target, bool calculate_only,
4733 Valtype* calculated_value)
9810d34d 4734 {
9810d34d
SS
4735 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4736 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4737
47a9f4fc 4738 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
9810d34d
SS
4739 : addend_hi);
4740
4741 // Find GOT page entry.
4742 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
4743 & 0xffff;
4744 value <<= 16;
4745 unsigned int got_offset =
4746 target->got_section()->get_got_page_offset(value, object);
4747
4748 // Resolve the relocation.
47a9f4fc 4749 Valtype x = target->got_section()->gp_offset(got_offset, object);
9810d34d 4750 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
4751
4752 if (calculate_only)
4753 {
4754 *calculated_value = x;
4755 return This::STATUS_OKAY;
4756 }
4757 else
4758 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4759
4760 return check_overflow<16>(x);
9810d34d
SS
4761 }
4762
4763 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
4764 static inline typename This::Status
4765 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
4766 const Mips_relobj<size, big_endian>* object,
4767 const Symbol_value<size>* psymval, Mips_address addend_a,
4768 bool extract_addend, Mips_address address, bool is_gp_disp,
47a9f4fc
VR
4769 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
4770 bool calculate_only, Valtype* calculated_value)
9810d34d 4771 {
9810d34d
SS
4772 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4773 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4774
47a9f4fc
VR
4775 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4776 : addend_a);
9810d34d 4777
47a9f4fc 4778 if (rel_type == elfcpp::SHT_REL)
9810d34d 4779 {
47a9f4fc
VR
4780 typename This::Status reloc_status = This::STATUS_OKAY;
4781 // Resolve pending R_MIPS_HI16 relocations.
4782 typename std::list<reloc_high<size, big_endian> >::iterator it =
4783 hi16_relocs.begin();
4784 while (it != hi16_relocs.end())
9810d34d 4785 {
47a9f4fc
VR
4786 reloc_high<size, big_endian> hi16 = *it;
4787 if (hi16.r_sym == r_sym
4788 && is_matching_lo16_reloc(hi16.r_type, r_type))
4789 {
4790 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
4791 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
4792 hi16.addend, hi16.address, hi16.gp_disp,
4793 hi16.r_type, hi16.extract_addend, addend,
4794 target, calculate_only, calculated_value);
4795 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
4796 if (reloc_status == This::STATUS_OVERFLOW)
4797 return This::STATUS_OVERFLOW;
4798 it = hi16_relocs.erase(it);
4799 }
4800 else
4801 ++it;
9810d34d 4802 }
9810d34d 4803
47a9f4fc
VR
4804 // Resolve pending local R_MIPS_GOT16 relocations.
4805 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
4806 got16_relocs.begin();
4807 while (it2 != got16_relocs.end())
9810d34d 4808 {
47a9f4fc
VR
4809 reloc_high<size, big_endian> got16 = *it2;
4810 if (got16.r_sym == r_sym
4811 && is_matching_lo16_reloc(got16.r_type, r_type))
4812 {
4813 mips_reloc_unshuffle(got16.view, got16.r_type, false);
4814
4815 reloc_status = do_relgot16_local(got16.view, got16.object,
4816 got16.psymval, got16.addend,
4817 got16.extract_addend, addend, target,
4818 calculate_only, calculated_value);
4819
4820 mips_reloc_shuffle(got16.view, got16.r_type, false);
4821 if (reloc_status == This::STATUS_OVERFLOW)
4822 return This::STATUS_OVERFLOW;
4823 it2 = got16_relocs.erase(it2);
4824 }
4825 else
4826 ++it2;
9810d34d 4827 }
9810d34d
SS
4828 }
4829
4830 // Resolve R_MIPS_LO16 relocation.
47a9f4fc 4831 Valtype x;
9810d34d
SS
4832 if (!is_gp_disp)
4833 x = psymval->value(object, addend);
4834 else
4835 {
4836 // See the comment for R_MIPS16_HI16 above for the reason
4837 // for this conditional.
4838 Valtype32 gp_disp;
4839 if (r_type == elfcpp::R_MIPS16_LO16)
4840 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
4841 else if (r_type == elfcpp::R_MICROMIPS_LO16
4842 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
4843 gp_disp = target->adjusted_gp_value(object) - address + 3;
4844 else
4845 gp_disp = target->adjusted_gp_value(object) - address + 4;
4846 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
4847 // for overflow. Relocations against _gp_disp are normally
4848 // generated from the .cpload pseudo-op. It generates code
4849 // that normally looks like this:
4850
4851 // lui $gp,%hi(_gp_disp)
4852 // addiu $gp,$gp,%lo(_gp_disp)
4853 // addu $gp,$gp,$t9
4854
4855 // Here $t9 holds the address of the function being called,
4856 // as required by the MIPS ELF ABI. The R_MIPS_LO16
4857 // relocation can easily overflow in this situation, but the
4858 // R_MIPS_HI16 relocation will handle the overflow.
4859 // Therefore, we consider this a bug in the MIPS ABI, and do
4860 // not check for overflow here.
4861 x = gp_disp + addend;
4862 }
4863 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
4864
4865 if (calculate_only)
4866 *calculated_value = x;
4867 else
4868 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4869
9810d34d
SS
4870 return This::STATUS_OKAY;
4871 }
4872
4873 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
4874 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
4875 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
4876 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
4877 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
4878 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
4879 static inline typename This::Status
47a9f4fc
VR
4880 relgot(unsigned char* view, int gp_offset, bool calculate_only,
4881 Valtype* calculated_value)
9810d34d 4882 {
9810d34d
SS
4883 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4884 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 4885 Valtype x = gp_offset;
9810d34d 4886 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
4887
4888 if (calculate_only)
4889 {
4890 *calculated_value = x;
4891 return This::STATUS_OKAY;
4892 }
4893 else
4894 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4895
4896 return check_overflow<16>(x);
4897 }
4898
4899 // R_MIPS_EH
4900 static inline typename This::Status
4901 releh(unsigned char* view, int gp_offset, bool calculate_only,
4902 Valtype* calculated_value)
4903 {
4904 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4905 Valtype x = gp_offset;
4906
4907 if (calculate_only)
4908 {
4909 *calculated_value = x;
4910 return This::STATUS_OKAY;
4911 }
4912 else
4913 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4914
4915 return check_overflow<32>(x);
9810d34d
SS
4916 }
4917
4918 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
4919 static inline typename This::Status
4920 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
4921 const Mips_relobj<size, big_endian>* object,
4922 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc
VR
4923 bool extract_addend, bool calculate_only,
4924 Valtype* calculated_value)
9810d34d 4925 {
9810d34d
SS
4926 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4927 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
47a9f4fc 4928 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
4929
4930 // Find a GOT page entry that points to within 32KB of symbol + addend.
4931 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
4932 unsigned int got_offset =
4933 target->got_section()->get_got_page_offset(value, object);
4934
47a9f4fc 4935 Valtype x = target->got_section()->gp_offset(got_offset, object);
9810d34d 4936 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
4937
4938 if (calculate_only)
4939 {
4940 *calculated_value = x;
4941 return This::STATUS_OKAY;
4942 }
4943 else
4944 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4945
4946 return check_overflow<16>(x);
9810d34d
SS
4947 }
4948
4949 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
4950 static inline typename This::Status
4951 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
4952 const Mips_relobj<size, big_endian>* object,
4953 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc
VR
4954 bool extract_addend, bool local, bool calculate_only,
4955 Valtype* calculated_value)
9810d34d 4956 {
9810d34d
SS
4957 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4958 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
47a9f4fc 4959 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
4960
4961 // For a local symbol, find a GOT page entry that points to within 32KB of
4962 // symbol + addend. Relocation value is the offset of the GOT page entry's
4963 // value from symbol + addend.
4964 // For a global symbol, relocation value is addend.
47a9f4fc 4965 Valtype x;
9810d34d
SS
4966 if (local)
4967 {
4968 // Find GOT page entry.
4969 Mips_address value = ((psymval->value(object, addend) + 0x8000)
4970 & ~0xffff);
4971 target->got_section()->get_got_page_offset(value, object);
4972
4973 x = psymval->value(object, addend) - value;
4974 }
4975 else
4976 x = addend;
4977 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
4978
4979 if (calculate_only)
4980 {
4981 *calculated_value = x;
4982 return This::STATUS_OKAY;
4983 }
4984 else
4985 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4986
4987 return check_overflow<16>(x);
9810d34d
SS
4988 }
4989
4990 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
4991 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
4992 static inline typename This::Status
47a9f4fc
VR
4993 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
4994 Valtype* calculated_value)
9810d34d 4995 {
9810d34d
SS
4996 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4997 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 4998 Valtype x = gp_offset;
9810d34d
SS
4999 x = ((x + 0x8000) >> 16) & 0xffff;
5000 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5001
5002 if (calculate_only)
5003 *calculated_value = x;
5004 else
5005 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5006
9810d34d
SS
5007 return This::STATUS_OKAY;
5008 }
5009
5010 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5011 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5012 static inline typename This::Status
47a9f4fc
VR
5013 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5014 Valtype* calculated_value)
9810d34d 5015 {
9810d34d
SS
5016 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5017 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5018 Valtype x = gp_offset;
9810d34d 5019 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5020
5021 if (calculate_only)
5022 *calculated_value = x;
5023 else
5024 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5025
9810d34d
SS
5026 return This::STATUS_OKAY;
5027 }
5028
5029 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5030 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5031 static inline typename This::Status
5032 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5033 const Symbol_value<size>* psymval, Mips_address gp,
5034 Mips_address addend_a, bool extract_addend, bool local,
47a9f4fc
VR
5035 unsigned int r_type, bool calculate_only,
5036 Valtype* calculated_value)
9810d34d 5037 {
9810d34d
SS
5038 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5039 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5040
47a9f4fc 5041 Valtype addend;
9810d34d
SS
5042 if (extract_addend)
5043 {
5044 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5045 addend = (val & 0x7f) << 2;
5046 else
5047 addend = val & 0xffff;
5048 // Only sign-extend the addend if it was extracted from the
5049 // instruction. If the addend was separate, leave it alone,
5050 // otherwise we may lose significant bits.
5051 addend = Bits<16>::sign_extend32(addend);
5052 }
5053 else
5054 addend = addend_a;
5055
47a9f4fc 5056 Valtype x = psymval->value(object, addend) - gp;
9810d34d
SS
5057
5058 // If the symbol was local, any earlier relocatable links will
5059 // have adjusted its addend with the gp offset, so compensate
5060 // for that now. Don't do it for symbols forced local in this
5061 // link, though, since they won't have had the gp offset applied
5062 // to them before.
5063 if (local)
5064 x += object->gp_value();
5065
5066 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5067 val = Bits<32>::bit_select32(val, x, 0x7f);
5068 else
5069 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5070
5071 if (calculate_only)
5072 {
5073 *calculated_value = x;
5074 return This::STATUS_OKAY;
5075 }
5076 else
5077 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5078
5079 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
9810d34d
SS
5080 {
5081 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5082 "limit (see option -G)"));
5083 return This::STATUS_OVERFLOW;
5084 }
5085 return This::STATUS_OKAY;
5086 }
5087
5088 // R_MIPS_GPREL32
5089 static inline typename This::Status
5090 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5091 const Symbol_value<size>* psymval, Mips_address gp,
47a9f4fc
VR
5092 Mips_address addend_a, bool extract_addend, bool calculate_only,
5093 Valtype* calculated_value)
9810d34d 5094 {
9810d34d
SS
5095 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5096 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5097 Valtype addend = extract_addend ? val : addend_a;
9810d34d
SS
5098
5099 // R_MIPS_GPREL32 relocations are defined for local symbols only.
47a9f4fc
VR
5100 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5101
5102 if (calculate_only)
5103 *calculated_value = x;
5104 else
5105 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5106
9810d34d
SS
5107 return This::STATUS_OKAY;
5108 }
5109
5110 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5111 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5112 // R_MICROMIPS_TLS_DTPREL_HI16
5113 static inline typename This::Status
5114 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5115 const Symbol_value<size>* psymval, Valtype32 tp_offset,
47a9f4fc
VR
5116 Mips_address addend_a, bool extract_addend, bool calculate_only,
5117 Valtype* calculated_value)
9810d34d 5118 {
9810d34d
SS
5119 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5120 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5121 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
5122
5123 // tls symbol values are relative to tls_segment()->vaddr()
47a9f4fc 5124 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
9810d34d 5125 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5126
5127 if (calculate_only)
5128 *calculated_value = x;
5129 else
5130 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5131
9810d34d
SS
5132 return This::STATUS_OKAY;
5133 }
5134
5135 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5136 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5137 // R_MICROMIPS_TLS_DTPREL_LO16,
5138 static inline typename This::Status
5139 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5140 const Symbol_value<size>* psymval, Valtype32 tp_offset,
47a9f4fc
VR
5141 Mips_address addend_a, bool extract_addend, bool calculate_only,
5142 Valtype* calculated_value)
9810d34d 5143 {
9810d34d
SS
5144 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5145 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5146 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
5147
5148 // tls symbol values are relative to tls_segment()->vaddr()
47a9f4fc 5149 Valtype x = psymval->value(object, addend) - tp_offset;
9810d34d 5150 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5151
5152 if (calculate_only)
5153 *calculated_value = x;
5154 else
5155 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5156
9810d34d
SS
5157 return This::STATUS_OKAY;
5158 }
5159
5160 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5161 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5162 static inline typename This::Status
5163 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5164 const Symbol_value<size>* psymval, Valtype32 tp_offset,
47a9f4fc
VR
5165 Mips_address addend_a, bool extract_addend, bool calculate_only,
5166 Valtype* calculated_value)
9810d34d 5167 {
9810d34d
SS
5168 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5169 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5170 Valtype addend = extract_addend ? val : addend_a;
9810d34d
SS
5171
5172 // tls symbol values are relative to tls_segment()->vaddr()
47a9f4fc
VR
5173 Valtype x = psymval->value(object, addend) - tp_offset;
5174
5175 if (calculate_only)
5176 *calculated_value = x;
5177 else
5178 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5179
9810d34d
SS
5180 return This::STATUS_OKAY;
5181 }
5182
5183 // R_MIPS_SUB, R_MICROMIPS_SUB
5184 static inline typename This::Status
5185 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5186 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc 5187 bool extract_addend, bool calculate_only, Valtype* calculated_value)
9810d34d 5188 {
47a9f4fc
VR
5189 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5190 Valtype64 addend = (extract_addend
5191 ? elfcpp::Swap<64, big_endian>::readval(wv)
5192 : addend_a);
5193
5194 Valtype64 x = psymval->value(object, -addend);
5195 if (calculate_only)
5196 *calculated_value = x;
5197 else
5198 elfcpp::Swap<64, big_endian>::writeval(wv, x);
9810d34d 5199
9810d34d 5200 return This::STATUS_OKAY;
47a9f4fc
VR
5201 }
5202
5203 // R_MIPS_64: S + A
5204 static inline typename This::Status
5205 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5206 const Symbol_value<size>* psymval, Mips_address addend_a,
5207 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5208 bool apply_addend_only)
5209 {
5210 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5211 Valtype64 addend = (extract_addend
5212 ? elfcpp::Swap<64, big_endian>::readval(wv)
5213 : addend_a);
5214
5215 Valtype64 x = psymval->value(object, addend);
5216 if (calculate_only)
5217 *calculated_value = x;
5218 else
5219 {
5220 if (apply_addend_only)
5221 x = addend;
5222 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5223 }
5224
5225 return This::STATUS_OKAY;
5226 }
5227
9810d34d
SS
5228};
5229
5230template<int size, bool big_endian>
5231typename std::list<reloc_high<size, big_endian> >
5232 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5233
5234template<int size, bool big_endian>
5235typename std::list<reloc_high<size, big_endian> >
5236 Mips_relocate_functions<size, big_endian>::got16_relocs;
5237
5238// Mips_got_info methods.
5239
5240// Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5241// SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5242
5243template<int size, bool big_endian>
5244void
5245Mips_got_info<size, big_endian>::record_local_got_symbol(
5246 Mips_relobj<size, big_endian>* object, unsigned int symndx,
47a9f4fc
VR
5247 Mips_address addend, unsigned int r_type, unsigned int shndx,
5248 bool is_section_symbol)
9810d34d
SS
5249{
5250 Mips_got_entry<size, big_endian>* entry =
5251 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5252 mips_elf_reloc_tls_type(r_type),
47a9f4fc 5253 shndx, is_section_symbol);
9810d34d
SS
5254 this->record_got_entry(entry, object);
5255}
5256
5257// Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5258// in OBJECT. FOR_CALL is true if the caller is only interested in
5259// using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5260// relocation.
5261
5262template<int size, bool big_endian>
5263void
5264Mips_got_info<size, big_endian>::record_global_got_symbol(
5265 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5266 unsigned int r_type, bool dyn_reloc, bool for_call)
5267{
5268 if (!for_call)
5269 mips_sym->set_got_not_only_for_calls();
5270
5271 // A global symbol in the GOT must also be in the dynamic symbol table.
5272 if (!mips_sym->needs_dynsym_entry())
5273 {
5274 switch (mips_sym->visibility())
5275 {
5276 case elfcpp::STV_INTERNAL:
5277 case elfcpp::STV_HIDDEN:
5278 mips_sym->set_is_forced_local();
5279 break;
5280 default:
5281 mips_sym->set_needs_dynsym_entry();
5282 break;
5283 }
5284 }
5285
5286 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5287 if (tls_type == GOT_TLS_NONE)
5288 this->global_got_symbols_.insert(mips_sym);
5289
5290 if (dyn_reloc)
5291 {
5292 if (mips_sym->global_got_area() == GGA_NONE)
5293 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5294 return;
5295 }
5296
5297 Mips_got_entry<size, big_endian>* entry =
15eb1beb 5298 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
9810d34d
SS
5299
5300 this->record_got_entry(entry, object);
5301}
5302
5303// Add ENTRY to master GOT and to OBJECT's GOT.
5304
5305template<int size, bool big_endian>
5306void
5307Mips_got_info<size, big_endian>::record_got_entry(
5308 Mips_got_entry<size, big_endian>* entry,
5309 Mips_relobj<size, big_endian>* object)
5310{
15eb1beb 5311 this->got_entries_.insert(entry);
9810d34d
SS
5312
5313 // Create the GOT entry for the OBJECT's GOT.
5314 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5315 Mips_got_entry<size, big_endian>* entry2 =
5316 new Mips_got_entry<size, big_endian>(*entry);
5317
15eb1beb 5318 g->got_entries_.insert(entry2);
9810d34d
SS
5319}
5320
5321// Record that OBJECT has a page relocation against symbol SYMNDX and
5322// that ADDEND is the addend for that relocation.
5323// This function creates an upper bound on the number of GOT slots
5324// required; no attempt is made to combine references to non-overridable
5325// global symbols across multiple input files.
5326
5327template<int size, bool big_endian>
5328void
5329Mips_got_info<size, big_endian>::record_got_page_entry(
5330 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5331{
5332 struct Got_page_range **range_ptr, *range;
5333 int old_pages, new_pages;
5334
5335 // Find the Got_page_entry for this symbol.
5336 Got_page_entry* entry = new Got_page_entry(object, symndx);
5337 typename Got_page_entry_set::iterator it =
5338 this->got_page_entries_.find(entry);
5339 if (it != this->got_page_entries_.end())
5340 entry = *it;
5341 else
5342 this->got_page_entries_.insert(entry);
5343
5344 // Add the same entry to the OBJECT's GOT.
5345 Got_page_entry* entry2 = NULL;
5346 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5347 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5348 {
5349 entry2 = new Got_page_entry(*entry);
5350 g2->got_page_entries_.insert(entry2);
5351 }
5352
5353 // Skip over ranges whose maximum extent cannot share a page entry
5354 // with ADDEND.
5355 range_ptr = &entry->ranges;
5356 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5357 range_ptr = &(*range_ptr)->next;
5358
5359 // If we scanned to the end of the list, or found a range whose
5360 // minimum extent cannot share a page entry with ADDEND, create
5361 // a new singleton range.
5362 range = *range_ptr;
5363 if (!range || addend < range->min_addend - 0xffff)
5364 {
5365 range = new Got_page_range();
5366 range->next = *range_ptr;
5367 range->min_addend = addend;
5368 range->max_addend = addend;
5369
5370 *range_ptr = range;
5371 ++entry->num_pages;
5372 if (entry2 != NULL)
5373 ++entry2->num_pages;
5374 ++this->page_gotno_;
5375 ++g2->page_gotno_;
5376 return;
5377 }
5378
5379 // Remember how many pages the old range contributed.
5380 old_pages = range->get_max_pages();
5381
5382 // Update the ranges.
5383 if (addend < range->min_addend)
5384 range->min_addend = addend;
5385 else if (addend > range->max_addend)
5386 {
5387 if (range->next && addend >= range->next->min_addend - 0xffff)
5388 {
5389 old_pages += range->next->get_max_pages();
5390 range->max_addend = range->next->max_addend;
5391 range->next = range->next->next;
5392 }
5393 else
5394 range->max_addend = addend;
5395 }
5396
5397 // Record any change in the total estimate.
5398 new_pages = range->get_max_pages();
5399 if (old_pages != new_pages)
5400 {
5401 entry->num_pages += new_pages - old_pages;
5402 if (entry2 != NULL)
5403 entry2->num_pages += new_pages - old_pages;
5404 this->page_gotno_ += new_pages - old_pages;
5405 g2->page_gotno_ += new_pages - old_pages;
5406 }
5407}
5408
5409// Create all entries that should be in the local part of the GOT.
5410
5411template<int size, bool big_endian>
5412void
5413Mips_got_info<size, big_endian>::add_local_entries(
5414 Target_mips<size, big_endian>* target, Layout* layout)
5415{
5416 Mips_output_data_got<size, big_endian>* got = target->got_section();
5417 // First two GOT entries are reserved. The first entry will be filled at
5418 // runtime. The second entry will be used by some runtime loaders.
5419 got->add_constant(0);
5420 got->add_constant(target->mips_elf_gnu_got1_mask());
5421
5422 for (typename Got_entry_set::iterator
5423 p = this->got_entries_.begin();
5424 p != this->got_entries_.end();
5425 ++p)
5426 {
5427 Mips_got_entry<size, big_endian>* entry = *p;
5428 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5429 {
5430 got->add_local(entry->object(), entry->symndx(),
47a9f4fc 5431 GOT_TYPE_STANDARD, entry->addend());
9810d34d 5432 unsigned int got_offset = entry->object()->local_got_offset(
47a9f4fc 5433 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
9810d34d
SS
5434 if (got->multi_got() && this->index_ > 0
5435 && parameters->options().output_is_position_independent())
47a9f4fc
VR
5436 {
5437 if (!entry->is_section_symbol())
5438 target->rel_dyn_section(layout)->add_local(entry->object(),
5439 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5440 else
5441 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5442 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5443 got, got_offset);
5444 }
9810d34d
SS
5445 }
5446 }
5447
5448 this->add_page_entries(target, layout);
5449
5450 // Add global entries that should be in the local area.
5451 for (typename Got_entry_set::iterator
5452 p = this->got_entries_.begin();
5453 p != this->got_entries_.end();
5454 ++p)
5455 {
5456 Mips_got_entry<size, big_endian>* entry = *p;
5457 if (!entry->is_for_global_symbol())
5458 continue;
5459
5460 Mips_symbol<size>* mips_sym = entry->sym();
5461 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5462 {
5463 unsigned int got_type;
5464 if (!got->multi_got())
5465 got_type = GOT_TYPE_STANDARD;
5466 else
5467 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5468 if (got->add_global(mips_sym, got_type))
5469 {
5470 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5471 if (got->multi_got() && this->index_ > 0
5472 && parameters->options().output_is_position_independent())
5473 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5474 mips_sym, elfcpp::R_MIPS_REL32, got,
5475 mips_sym->got_offset(got_type));
5476 }
5477 }
5478 }
5479}
5480
5481// Create GOT page entries.
5482
5483template<int size, bool big_endian>
5484void
5485Mips_got_info<size, big_endian>::add_page_entries(
5486 Target_mips<size, big_endian>* target, Layout* layout)
5487{
5488 if (this->page_gotno_ == 0)
5489 return;
5490
5491 Mips_output_data_got<size, big_endian>* got = target->got_section();
5492 this->got_page_offset_start_ = got->add_constant(0);
5493 if (got->multi_got() && this->index_ > 0
5494 && parameters->options().output_is_position_independent())
5495 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5496 this->got_page_offset_start_);
5497 int num_entries = this->page_gotno_;
5498 unsigned int prev_offset = this->got_page_offset_start_;
5499 while (--num_entries > 0)
5500 {
5501 unsigned int next_offset = got->add_constant(0);
5502 if (got->multi_got() && this->index_ > 0
5503 && parameters->options().output_is_position_independent())
5504 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5505 next_offset);
5506 gold_assert(next_offset == prev_offset + size/8);
5507 prev_offset = next_offset;
5508 }
5509 this->got_page_offset_next_ = this->got_page_offset_start_;
5510}
5511
5512// Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5513
5514template<int size, bool big_endian>
5515void
5516Mips_got_info<size, big_endian>::add_global_entries(
5517 Target_mips<size, big_endian>* target, Layout* layout,
5518 unsigned int non_reloc_only_global_gotno)
5519{
5520 Mips_output_data_got<size, big_endian>* got = target->got_section();
5521 // Add GGA_NORMAL entries.
5522 unsigned int count = 0;
5523 for (typename Got_entry_set::iterator
5524 p = this->got_entries_.begin();
5525 p != this->got_entries_.end();
5526 ++p)
5527 {
5528 Mips_got_entry<size, big_endian>* entry = *p;
5529 if (!entry->is_for_global_symbol())
5530 continue;
5531
5532 Mips_symbol<size>* mips_sym = entry->sym();
5533 if (mips_sym->global_got_area() != GGA_NORMAL)
5534 continue;
5535
5536 unsigned int got_type;
5537 if (!got->multi_got())
5538 got_type = GOT_TYPE_STANDARD;
5539 else
5540 // In multi-GOT links, global symbol can be in both primary and
5541 // secondary GOT(s). By creating custom GOT type
5542 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5543 // is added to secondary GOT(s).
5544 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5545 if (!got->add_global(mips_sym, got_type))
5546 continue;
5547
5548 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5549 if (got->multi_got() && this->index_ == 0)
5550 count++;
5551 if (got->multi_got() && this->index_ > 0)
5552 {
5553 if (parameters->options().output_is_position_independent()
5554 || (!parameters->doing_static_link()
5555 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
5556 {
5557 target->rel_dyn_section(layout)->add_global(
5558 mips_sym, elfcpp::R_MIPS_REL32, got,
5559 mips_sym->got_offset(got_type));
5560 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
5561 elfcpp::R_MIPS_REL32, mips_sym);
5562 }
5563 }
5564 }
5565
5566 if (!got->multi_got() || this->index_ == 0)
5567 {
5568 if (got->multi_got())
5569 {
5570 // We need to allocate space in the primary GOT for GGA_NORMAL entries
5571 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
5572 // entries correspond to dynamic symbol indexes.
5573 while (count < non_reloc_only_global_gotno)
5574 {
5575 got->add_constant(0);
5576 ++count;
5577 }
5578 }
5579
5580 // Add GGA_RELOC_ONLY entries.
5581 got->add_reloc_only_entries();
5582 }
5583}
5584
5585// Create global GOT entries that should be in the GGA_RELOC_ONLY area.
5586
5587template<int size, bool big_endian>
5588void
5589Mips_got_info<size, big_endian>::add_reloc_only_entries(
5590 Mips_output_data_got<size, big_endian>* got)
5591{
15eb1beb 5592 for (typename Global_got_entry_set::iterator
9810d34d
SS
5593 p = this->global_got_symbols_.begin();
5594 p != this->global_got_symbols_.end();
5595 ++p)
5596 {
5597 Mips_symbol<size>* mips_sym = *p;
5598 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
5599 {
5600 unsigned int got_type;
5601 if (!got->multi_got())
5602 got_type = GOT_TYPE_STANDARD;
5603 else
5604 got_type = GOT_TYPE_STANDARD_MULTIGOT;
5605 if (got->add_global(mips_sym, got_type))
5606 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5607 }
5608 }
5609}
5610
5611// Create TLS GOT entries.
5612
5613template<int size, bool big_endian>
5614void
5615Mips_got_info<size, big_endian>::add_tls_entries(
5616 Target_mips<size, big_endian>* target, Layout* layout)
5617{
5618 Mips_output_data_got<size, big_endian>* got = target->got_section();
5619 // Add local tls entries.
5620 for (typename Got_entry_set::iterator
5621 p = this->got_entries_.begin();
5622 p != this->got_entries_.end();
5623 ++p)
5624 {
5625 Mips_got_entry<size, big_endian>* entry = *p;
5626 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
5627 continue;
5628
5629 if (entry->tls_type() == GOT_TLS_GD)
5630 {
5631 unsigned int got_type = GOT_TYPE_TLS_PAIR;
5632 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5633 : elfcpp::R_MIPS_TLS_DTPMOD64);
5634 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5635 : elfcpp::R_MIPS_TLS_DTPREL64);
5636
5637 if (!parameters->doing_static_link())
5638 {
5639 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
5640 entry->shndx(), got_type,
5641 target->rel_dyn_section(layout),
47a9f4fc 5642 r_type1, entry->addend());
9810d34d 5643 unsigned int got_offset =
47a9f4fc
VR
5644 entry->object()->local_got_offset(entry->symndx(), got_type,
5645 entry->addend());
9810d34d
SS
5646 got->add_static_reloc(got_offset + size/8, r_type2,
5647 entry->object(), entry->symndx());
5648 }
5649 else
5650 {
5651 // We are doing a static link. Mark it as belong to module 1,
5652 // the executable.
5653 unsigned int got_offset = got->add_constant(1);
5654 entry->object()->set_local_got_offset(entry->symndx(), got_type,
47a9f4fc
VR
5655 got_offset,
5656 entry->addend());
9810d34d
SS
5657 got->add_constant(0);
5658 got->add_static_reloc(got_offset + size/8, r_type2,
5659 entry->object(), entry->symndx());
5660 }
5661 }
5662 else if (entry->tls_type() == GOT_TLS_IE)
5663 {
5664 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
5665 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5666 : elfcpp::R_MIPS_TLS_TPREL64);
5667 if (!parameters->doing_static_link())
5668 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
47a9f4fc
VR
5669 target->rel_dyn_section(layout), r_type,
5670 entry->addend());
9810d34d
SS
5671 else
5672 {
47a9f4fc
VR
5673 got->add_local(entry->object(), entry->symndx(), got_type,
5674 entry->addend());
9810d34d 5675 unsigned int got_offset =
47a9f4fc
VR
5676 entry->object()->local_got_offset(entry->symndx(), got_type,
5677 entry->addend());
9810d34d
SS
5678 got->add_static_reloc(got_offset, r_type, entry->object(),
5679 entry->symndx());
5680 }
5681 }
5682 else if (entry->tls_type() == GOT_TLS_LDM)
5683 {
5684 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5685 : elfcpp::R_MIPS_TLS_DTPMOD64);
5686 unsigned int got_offset;
5687 if (!parameters->doing_static_link())
5688 {
5689 got_offset = got->add_constant(0);
5690 target->rel_dyn_section(layout)->add_local(
5691 entry->object(), 0, r_type, got, got_offset);
5692 }
5693 else
5694 // We are doing a static link. Just mark it as belong to module 1,
5695 // the executable.
5696 got_offset = got->add_constant(1);
5697
5698 got->add_constant(0);
5699 got->set_tls_ldm_offset(got_offset, entry->object());
5700 }
5701 else
5702 gold_unreachable();
5703 }
5704
5705 // Add global tls entries.
5706 for (typename Got_entry_set::iterator
5707 p = this->got_entries_.begin();
5708 p != this->got_entries_.end();
5709 ++p)
5710 {
5711 Mips_got_entry<size, big_endian>* entry = *p;
5712 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
5713 continue;
5714
5715 Mips_symbol<size>* mips_sym = entry->sym();
5716 if (entry->tls_type() == GOT_TLS_GD)
5717 {
5718 unsigned int got_type;
5719 if (!got->multi_got())
5720 got_type = GOT_TYPE_TLS_PAIR;
5721 else
5722 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
5723 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
5724 : elfcpp::R_MIPS_TLS_DTPMOD64);
5725 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
5726 : elfcpp::R_MIPS_TLS_DTPREL64);
5727 if (!parameters->doing_static_link())
5728 got->add_global_pair_with_rel(mips_sym, got_type,
5729 target->rel_dyn_section(layout), r_type1, r_type2);
5730 else
5731 {
5732 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
5733 // GOT entries. The first one is initialized to be 1, which is the
5734 // module index for the main executable and the second one 0. A
5735 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
5736 // the second GOT entry and will be applied by gold.
5737 unsigned int got_offset = got->add_constant(1);
5738 mips_sym->set_got_offset(got_type, got_offset);
5739 got->add_constant(0);
5740 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
5741 }
5742 }
5743 else if (entry->tls_type() == GOT_TLS_IE)
5744 {
5745 unsigned int got_type;
5746 if (!got->multi_got())
5747 got_type = GOT_TYPE_TLS_OFFSET;
5748 else
5749 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
5750 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
5751 : elfcpp::R_MIPS_TLS_TPREL64);
5752 if (!parameters->doing_static_link())
5753 got->add_global_with_rel(mips_sym, got_type,
5754 target->rel_dyn_section(layout), r_type);
5755 else
5756 {
5757 got->add_global(mips_sym, got_type);
5758 unsigned int got_offset = mips_sym->got_offset(got_type);
5759 got->add_static_reloc(got_offset, r_type, mips_sym);
5760 }
5761 }
5762 else
5763 gold_unreachable();
5764 }
5765}
5766
5767// Decide whether the symbol needs an entry in the global part of the primary
5768// GOT, setting global_got_area accordingly. Count the number of global
5769// symbols that are in the primary GOT only because they have dynamic
5770// relocations R_MIPS_REL32 against them (reloc_only_gotno).
5771
5772template<int size, bool big_endian>
5773void
5774Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
5775{
15eb1beb 5776 for (typename Global_got_entry_set::iterator
9810d34d
SS
5777 p = this->global_got_symbols_.begin();
5778 p != this->global_got_symbols_.end();
5779 ++p)
5780 {
5781 Mips_symbol<size>* sym = *p;
5782 // Make a final decision about whether the symbol belongs in the
5783 // local or global GOT. Symbols that bind locally can (and in the
5784 // case of forced-local symbols, must) live in the local GOT.
5785 // Those that are aren't in the dynamic symbol table must also
5786 // live in the local GOT.
5787
5788 if (!sym->should_add_dynsym_entry(symtab)
5789 || (sym->got_only_for_calls()
5790 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
5791 : symbol_references_local(sym,
5792 sym->should_add_dynsym_entry(symtab))))
5793 // The symbol belongs in the local GOT. We no longer need this
5794 // entry if it was only used for relocations; those relocations
5795 // will be against the null or section symbol instead.
5796 sym->set_global_got_area(GGA_NONE);
5797 else if (sym->global_got_area() == GGA_RELOC_ONLY)
5798 {
5799 ++this->reloc_only_gotno_;
5800 ++this->global_gotno_ ;
5801 }
5802 }
5803}
5804
5805// Return the offset of GOT page entry for VALUE. Initialize the entry with
5806// VALUE if it is not initialized.
5807
5808template<int size, bool big_endian>
5809unsigned int
5810Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
5811 Mips_output_data_got<size, big_endian>* got)
5812{
5813 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
5814 if (it != this->got_page_offsets_.end())
5815 return it->second;
5816
5817 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
5818 + (size/8) * this->page_gotno_);
5819
5820 unsigned int got_offset = this->got_page_offset_next_;
5821 this->got_page_offsets_[value] = got_offset;
5822 this->got_page_offset_next_ += size/8;
5823 got->update_got_entry(got_offset, value);
5824 return got_offset;
5825}
5826
5827// Remove lazy-binding stubs for global symbols in this GOT.
5828
5829template<int size, bool big_endian>
5830void
5831Mips_got_info<size, big_endian>::remove_lazy_stubs(
5832 Target_mips<size, big_endian>* target)
5833{
5834 for (typename Got_entry_set::iterator
5835 p = this->got_entries_.begin();
5836 p != this->got_entries_.end();
5837 ++p)
5838 {
5839 Mips_got_entry<size, big_endian>* entry = *p;
5840 if (entry->is_for_global_symbol())
5841 target->remove_lazy_stub_entry(entry->sym());
5842 }
5843}
5844
5845// Count the number of GOT entries required.
5846
5847template<int size, bool big_endian>
5848void
5849Mips_got_info<size, big_endian>::count_got_entries()
5850{
5851 for (typename Got_entry_set::iterator
5852 p = this->got_entries_.begin();
5853 p != this->got_entries_.end();
5854 ++p)
5855 {
5856 this->count_got_entry(*p);
5857 }
5858}
5859
5860// Count the number of GOT entries required by ENTRY. Accumulate the result.
5861
5862template<int size, bool big_endian>
5863void
5864Mips_got_info<size, big_endian>::count_got_entry(
5865 Mips_got_entry<size, big_endian>* entry)
5866{
5867 if (entry->is_tls_entry())
5868 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
5869 else if (entry->is_for_local_symbol()
5870 || entry->sym()->global_got_area() == GGA_NONE)
5871 ++this->local_gotno_;
5872 else
5873 ++this->global_gotno_;
5874}
5875
5876// Add FROM's GOT entries.
5877
5878template<int size, bool big_endian>
5879void
5880Mips_got_info<size, big_endian>::add_got_entries(
5881 Mips_got_info<size, big_endian>* from)
5882{
5883 for (typename Got_entry_set::iterator
5884 p = from->got_entries_.begin();
5885 p != from->got_entries_.end();
5886 ++p)
5887 {
5888 Mips_got_entry<size, big_endian>* entry = *p;
5889 if (this->got_entries_.find(entry) == this->got_entries_.end())
5890 {
5891 Mips_got_entry<size, big_endian>* entry2 =
5892 new Mips_got_entry<size, big_endian>(*entry);
5893 this->got_entries_.insert(entry2);
5894 this->count_got_entry(entry);
5895 }
5896 }
5897}
5898
5899// Add FROM's GOT page entries.
5900
5901template<int size, bool big_endian>
5902void
5903Mips_got_info<size, big_endian>::add_got_page_entries(
5904 Mips_got_info<size, big_endian>* from)
5905{
5906 for (typename Got_page_entry_set::iterator
5907 p = from->got_page_entries_.begin();
5908 p != from->got_page_entries_.end();
5909 ++p)
5910 {
5911 Got_page_entry* entry = *p;
5912 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
5913 {
5914 Got_page_entry* entry2 = new Got_page_entry(*entry);
5915 this->got_page_entries_.insert(entry2);
5916 this->page_gotno_ += entry->num_pages;
5917 }
5918 }
5919}
5920
5921// Mips_output_data_got methods.
5922
5923// Lay out the GOT. Add local, global and TLS entries. If GOT is
5924// larger than 64K, create multi-GOT.
5925
5926template<int size, bool big_endian>
5927void
5928Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
5929 Symbol_table* symtab, const Input_objects* input_objects)
5930{
5931 // Decide which symbols need to go in the global part of the GOT and
5932 // count the number of reloc-only GOT symbols.
5933 this->master_got_info_->count_got_symbols(symtab);
5934
5935 // Count the number of GOT entries.
5936 this->master_got_info_->count_got_entries();
5937
5938 unsigned int got_size = this->master_got_info_->got_size();
5939 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
5940 this->lay_out_multi_got(layout, input_objects);
5941 else
5942 {
5943 // Record that all objects use single GOT.
5944 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
5945 p != input_objects->relobj_end();
5946 ++p)
5947 {
5948 Mips_relobj<size, big_endian>* object =
5949 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
5950 if (object->get_got_info() != NULL)
5951 object->set_got_info(this->master_got_info_);
5952 }
5953
5954 this->master_got_info_->add_local_entries(this->target_, layout);
5955 this->master_got_info_->add_global_entries(this->target_, layout,
5956 /*not used*/-1U);
5957 this->master_got_info_->add_tls_entries(this->target_, layout);
5958 }
5959}
5960
5961// Create multi-GOT. For every GOT, add local, global and TLS entries.
5962
5963template<int size, bool big_endian>
5964void
5965Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
5966 const Input_objects* input_objects)
5967{
5968 // Try to merge the GOTs of input objects together, as long as they
5969 // don't seem to exceed the maximum GOT size, choosing one of them
5970 // to be the primary GOT.
5971 this->merge_gots(input_objects);
5972
5973 // Every symbol that is referenced in a dynamic relocation must be
5974 // present in the primary GOT.
5975 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
5976
5977 // Add GOT entries.
5978 unsigned int i = 0;
5979 unsigned int offset = 0;
5980 Mips_got_info<size, big_endian>* g = this->primary_got_;
5981 do
5982 {
5983 g->set_index(i);
5984 g->set_offset(offset);
5985
5986 g->add_local_entries(this->target_, layout);
5987 if (i == 0)
5988 g->add_global_entries(this->target_, layout,
5989 (this->master_got_info_->global_gotno()
5990 - this->master_got_info_->reloc_only_gotno()));
5991 else
5992 g->add_global_entries(this->target_, layout, /*not used*/-1U);
5993 g->add_tls_entries(this->target_, layout);
5994
5995 // Forbid global symbols in every non-primary GOT from having
5996 // lazy-binding stubs.
5997 if (i > 0)
5998 g->remove_lazy_stubs(this->target_);
5999
6000 ++i;
6001 offset += g->got_size();
6002 g = g->next();
6003 }
6004 while (g);
6005}
6006
6007// Attempt to merge GOTs of different input objects. Try to use as much as
6008// possible of the primary GOT, since it doesn't require explicit dynamic
6009// relocations, but don't use objects that would reference global symbols
6010// out of the addressable range. Failing the primary GOT, attempt to merge
6011// with the current GOT, or finish the current GOT and then make make the new
6012// GOT current.
6013
6014template<int size, bool big_endian>
6015void
6016Mips_output_data_got<size, big_endian>::merge_gots(
6017 const Input_objects* input_objects)
6018{
6019 gold_assert(this->primary_got_ == NULL);
6020 Mips_got_info<size, big_endian>* current = NULL;
6021
6022 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6023 p != input_objects->relobj_end();
6024 ++p)
6025 {
6026 Mips_relobj<size, big_endian>* object =
6027 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6028
6029 Mips_got_info<size, big_endian>* g = object->get_got_info();
6030 if (g == NULL)
6031 continue;
6032
6033 g->count_got_entries();
6034
6035 // Work out the number of page, local and TLS entries.
6036 unsigned int estimate = this->master_got_info_->page_gotno();
6037 if (estimate > g->page_gotno())
6038 estimate = g->page_gotno();
6039 estimate += g->local_gotno() + g->tls_gotno();
6040
6041 // We place TLS GOT entries after both locals and globals. The globals
6042 // for the primary GOT may overflow the normal GOT size limit, so be
6043 // sure not to merge a GOT which requires TLS with the primary GOT in that
6044 // case. This doesn't affect non-primary GOTs.
6045 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6046 : g->global_gotno());
6047
6048 unsigned int max_count =
6049 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6050 if (estimate <= max_count)
6051 {
6052 // If we don't have a primary GOT, use it as
6053 // a starting point for the primary GOT.
6054 if (!this->primary_got_)
6055 {
6056 this->primary_got_ = g;
6057 continue;
6058 }
6059
6060 // Try merging with the primary GOT.
6061 if (this->merge_got_with(g, object, this->primary_got_))
6062 continue;
6063 }
6064
6065 // If we can merge with the last-created GOT, do it.
6066 if (current && this->merge_got_with(g, object, current))
6067 continue;
6068
6069 // Well, we couldn't merge, so create a new GOT. Don't check if it
6070 // fits; if it turns out that it doesn't, we'll get relocation
6071 // overflows anyway.
6072 g->set_next(current);
6073 current = g;
6074 }
6075
6076 // If we do not find any suitable primary GOT, create an empty one.
6077 if (this->primary_got_ == NULL)
6078 this->primary_got_ = new Mips_got_info<size, big_endian>();
6079
6080 // Link primary GOT with secondary GOTs.
6081 this->primary_got_->set_next(current);
6082}
6083
6084// Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6085// this would lead to overflow, true if they were merged successfully.
6086
6087template<int size, bool big_endian>
6088bool
6089Mips_output_data_got<size, big_endian>::merge_got_with(
6090 Mips_got_info<size, big_endian>* from,
6091 Mips_relobj<size, big_endian>* object,
6092 Mips_got_info<size, big_endian>* to)
6093{
6094 // Work out how many page entries we would need for the combined GOT.
6095 unsigned int estimate = this->master_got_info_->page_gotno();
6096 if (estimate >= from->page_gotno() + to->page_gotno())
6097 estimate = from->page_gotno() + to->page_gotno();
6098
6099 // Conservatively estimate how many local and TLS entries would be needed.
6100 estimate += from->local_gotno() + to->local_gotno();
6101 estimate += from->tls_gotno() + to->tls_gotno();
6102
6103 // If we're merging with the primary got, any TLS relocations will
6104 // come after the full set of global entries. Otherwise estimate those
6105 // conservatively as well.
6106 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6107 estimate += this->master_got_info_->global_gotno();
6108 else
6109 estimate += from->global_gotno() + to->global_gotno();
6110
6111 // Bail out if the combined GOT might be too big.
6112 unsigned int max_count =
6113 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6114 if (estimate > max_count)
6115 return false;
6116
6117 // Transfer the object's GOT information from FROM to TO.
6118 to->add_got_entries(from);
6119 to->add_got_page_entries(from);
6120
6121 // Record that OBJECT should use output GOT TO.
6122 object->set_got_info(to);
6123
6124 return true;
6125}
6126
6127// Write out the GOT.
6128
6129template<int size, bool big_endian>
6130void
6131Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6132{
a080d84d
AV
6133 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6134 Mips_stubs_entry_set;
6135
9810d34d
SS
6136 // Call parent to write out GOT.
6137 Output_data_got<size, big_endian>::do_write(of);
6138
6139 const off_t offset = this->offset();
6140 const section_size_type oview_size =
6141 convert_to_section_size_type(this->data_size());
6142 unsigned char* const oview = of->get_output_view(offset, oview_size);
6143
6144 // Needed for fixing values of .got section.
6145 this->got_view_ = oview;
6146
6147 // Write lazy stub addresses.
a080d84d 6148 for (typename Mips_stubs_entry_set::iterator
9810d34d
SS
6149 p = this->master_got_info_->global_got_symbols().begin();
6150 p != this->master_got_info_->global_got_symbols().end();
6151 ++p)
6152 {
6153 Mips_symbol<size>* mips_sym = *p;
6154 if (mips_sym->has_lazy_stub())
6155 {
6156 Valtype* wv = reinterpret_cast<Valtype*>(
6157 oview + this->get_primary_got_offset(mips_sym));
6158 Valtype value =
6159 this->target_->mips_stubs_section()->stub_address(mips_sym);
6160 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6161 }
6162 }
6163
6164 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
a080d84d 6165 for (typename Mips_stubs_entry_set::iterator
9810d34d
SS
6166 p = this->master_got_info_->global_got_symbols().begin();
6167 p != this->master_got_info_->global_got_symbols().end();
6168 ++p)
6169 {
6170 Mips_symbol<size>* mips_sym = *p;
6171 if (!this->multi_got()
6172 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6173 && mips_sym->global_got_area() == GGA_NONE
6174 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6175 {
6176 Valtype* wv = reinterpret_cast<Valtype*>(
6177 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6178 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6179 if (value != 0)
6180 {
6181 value |= 1;
6182 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6183 }
6184 }
6185 }
6186
6187 if (!this->secondary_got_relocs_.empty())
6188 {
6189 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6190 // secondary GOT entries with non-zero initial value copy the value
6191 // to the corresponding primary GOT entry, and set the secondary GOT
6192 // entry to zero.
6193 // TODO(sasa): This is workaround. It needs to be investigated further.
6194
6195 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6196 {
6197 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6198 if (reloc.symbol_is_global())
6199 {
6200 Mips_symbol<size>* gsym = reloc.symbol();
6201 gold_assert(gsym != NULL);
6202
6203 unsigned got_offset = reloc.got_offset();
6204 gold_assert(got_offset < oview_size);
6205
6206 // Find primary GOT entry.
6207 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6208 oview + this->get_primary_got_offset(gsym));
6209
6210 // Find secondary GOT entry.
6211 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6212
6213 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6214 if (value != 0)
6215 {
6216 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6217 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6218 gsym->set_applied_secondary_got_fixup();
6219 }
6220 }
6221 }
6222
6223 of->write_output_view(offset, oview_size, oview);
6224 }
6225
6226 // We are done if there is no fix up.
6227 if (this->static_relocs_.empty())
6228 return;
6229
6230 Output_segment* tls_segment = this->layout_->tls_segment();
6231 gold_assert(tls_segment != NULL);
6232
6233 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6234 {
6235 Static_reloc& reloc(this->static_relocs_[i]);
6236
6237 Mips_address value;
6238 if (!reloc.symbol_is_global())
6239 {
6240 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6241 const Symbol_value<size>* psymval =
6242 object->local_symbol(reloc.index());
6243
6244 // We are doing static linking. Issue an error and skip this
6245 // relocation if the symbol is undefined or in a discarded_section.
6246 bool is_ordinary;
6247 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6248 if ((shndx == elfcpp::SHN_UNDEF)
6249 || (is_ordinary
6250 && shndx != elfcpp::SHN_UNDEF
6251 && !object->is_section_included(shndx)
6252 && !this->symbol_table_->is_section_folded(object, shndx)))
6253 {
6254 gold_error(_("undefined or discarded local symbol %u from "
6255 " object %s in GOT"),
6256 reloc.index(), reloc.relobj()->name().c_str());
6257 continue;
6258 }
6259
6260 value = psymval->value(object, 0);
6261 }
6262 else
6263 {
6264 const Mips_symbol<size>* gsym = reloc.symbol();
6265 gold_assert(gsym != NULL);
6266
6267 // We are doing static linking. Issue an error and skip this
6268 // relocation if the symbol is undefined or in a discarded_section
6269 // unless it is a weakly_undefined symbol.
6270 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6271 && !gsym->is_weak_undefined())
6272 {
6273 gold_error(_("undefined or discarded symbol %s in GOT"),
6274 gsym->name());
6275 continue;
6276 }
6277
6278 if (!gsym->is_weak_undefined())
6279 value = gsym->value();
6280 else
6281 value = 0;
6282 }
6283
6284 unsigned got_offset = reloc.got_offset();
6285 gold_assert(got_offset < oview_size);
6286
6287 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6288 Valtype x;
6289
6290 switch (reloc.r_type())
6291 {
6292 case elfcpp::R_MIPS_TLS_DTPMOD32:
6293 case elfcpp::R_MIPS_TLS_DTPMOD64:
6294 x = value;
6295 break;
6296 case elfcpp::R_MIPS_TLS_DTPREL32:
6297 case elfcpp::R_MIPS_TLS_DTPREL64:
6298 x = value - elfcpp::DTP_OFFSET;
6299 break;
6300 case elfcpp::R_MIPS_TLS_TPREL32:
6301 case elfcpp::R_MIPS_TLS_TPREL64:
6302 x = value - elfcpp::TP_OFFSET;
6303 break;
6304 default:
6305 gold_unreachable();
6306 break;
6307 }
6308
6309 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6310 }
6311
6312 of->write_output_view(offset, oview_size, oview);
6313}
6314
6315// Mips_relobj methods.
6316
6317// Count the local symbols. The Mips backend needs to know if a symbol
6318// is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6319// because the Symbol object keeps the ELF symbol type and st_other field.
6320// For local symbol it is harder because we cannot access this information.
6321// So we override the do_count_local_symbol in parent and scan local symbols to
6322// mark MIPS16 and microMIPS functions. This is not the most efficient way but
6323// I do not want to slow down other ports by calling a per symbol target hook
6324// inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6325
6326template<int size, bool big_endian>
6327void
6328Mips_relobj<size, big_endian>::do_count_local_symbols(
6329 Stringpool_template<char>* pool,
6330 Stringpool_template<char>* dynpool)
6331{
6332 // Ask parent to count the local symbols.
6333 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6334 const unsigned int loccount = this->local_symbol_count();
6335 if (loccount == 0)
6336 return;
6337
6338 // Initialize the mips16 and micromips function bit-vector.
6339 this->local_symbol_is_mips16_.resize(loccount, false);
6340 this->local_symbol_is_micromips_.resize(loccount, false);
6341
6342 // Read the symbol table section header.
6343 const unsigned int symtab_shndx = this->symtab_shndx();
6344 elfcpp::Shdr<size, big_endian>
6345 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6346 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6347
6348 // Read the local symbols.
6349 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6350 gold_assert(loccount == symtabshdr.get_sh_info());
6351 off_t locsize = loccount * sym_size;
6352 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6353 locsize, true, true);
6354
6355 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6356
6357 // Skip the first dummy symbol.
6358 psyms += sym_size;
6359 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6360 {
6361 elfcpp::Sym<size, big_endian> sym(psyms);
6362 unsigned char st_other = sym.get_st_other();
6363 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6364 this->local_symbol_is_micromips_[i] =
6365 elfcpp::elf_st_is_micromips(st_other);
6366 }
6367}
6368
6369// Read the symbol information.
6370
6371template<int size, bool big_endian>
6372void
6373Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6374{
6375 // Call parent class to read symbol information.
f35c4853 6376 this->base_read_symbols(sd);
9810d34d
SS
6377
6378 // Read processor-specific flags in ELF file header.
6379 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6380 elfcpp::Elf_sizes<size>::ehdr_size,
6381 true, false);
6382 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6383 this->processor_specific_flags_ = ehdr.get_e_flags();
6384
6385 // Get the section names.
6386 const unsigned char* pnamesu = sd->section_names->data();
6387 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6388
6389 // Initialize the mips16 stub section bit-vectors.
6390 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6391 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6392 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6393
6394 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6395 const unsigned char* pshdrs = sd->section_headers->data();
6396 const unsigned char* ps = pshdrs + shdr_size;
6397 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6398 {
6399 elfcpp::Shdr<size, big_endian> shdr(ps);
6400
6401 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6402 {
6403 // Read the gp value that was used to create this object. We need the
6404 // gp value while processing relocs. The .reginfo section is not used
6405 // in the 64-bit MIPS ELF ABI.
6406 section_offset_type section_offset = shdr.get_sh_offset();
6407 section_size_type section_size =
6408 convert_to_section_size_type(shdr.get_sh_size());
6409 const unsigned char* view =
6410 this->get_view(section_offset, section_size, true, false);
6411
6412 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6413
6414 // Read the rest of .reginfo.
6415 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6416 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6417 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6418 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6419 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6420 }
6421
47a9f4fc
VR
6422 // In the 64-bit ABI, .MIPS.options section holds register information.
6423 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6424 // starts with this header:
6425 //
6426 // typedef struct
6427 // {
6428 // // Type of option.
6429 // unsigned char kind[1];
6430 // // Size of option descriptor, including header.
6431 // unsigned char size[1];
6432 // // Section index of affected section, or 0 for global option.
6433 // unsigned char section[2];
6434 // // Information specific to this kind of option.
6435 // unsigned char info[4];
6436 // };
6437 //
6438 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6439 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6440 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
9810d34d 6441
47a9f4fc 6442 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
9810d34d 6443 {
47a9f4fc
VR
6444 section_offset_type section_offset = shdr.get_sh_offset();
6445 section_size_type section_size =
6446 convert_to_section_size_type(shdr.get_sh_size());
6447 const unsigned char* view =
6448 this->get_view(section_offset, section_size, true, false);
6449 const unsigned char* end = view + section_size;
6450
6451 while (view + 8 <= end)
6452 {
6453 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6454 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6455 if (sz < 8)
6456 {
6457 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6458 "than its header"),
6459 this->name().c_str(),
6460 this->mips_elf_options_section_name(), sz);
6461 break;
6462 }
6463
6464 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6465 {
6466 // In the 64 bit ABI, an ODK_REGINFO option is the following
6467 // structure. The info field of the options header is not
6468 // used.
6469 //
6470 // typedef struct
6471 // {
6472 // // Mask of general purpose registers used.
6473 // unsigned char ri_gprmask[4];
6474 // // Padding.
6475 // unsigned char ri_pad[4];
6476 // // Mask of co-processor registers used.
6477 // unsigned char ri_cprmask[4][4];
6478 // // GP register value for this object file.
6479 // unsigned char ri_gp_value[8];
6480 // };
6481
6482 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6483 + 32);
6484 }
6485 else if (kind == elfcpp::ODK_REGINFO)
6486 {
6487 // In the 32 bit ABI, an ODK_REGINFO option is the following
6488 // structure. The info field of the options header is not
6489 // used. The same structure is used in .reginfo section.
6490 //
6491 // typedef struct
6492 // {
6493 // unsigned char ri_gprmask[4];
6494 // unsigned char ri_cprmask[4][4];
6495 // unsigned char ri_gp_value[4];
6496 // };
6497
6498 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
6499 + 28);
6500 }
6501 view += sz;
6502 }
6503 }
6504
6505 const char* name = pnames + shdr.get_sh_name();
6506 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
6507 this->section_is_mips16_call_stub_[i] =
6508 is_prefix_of(".mips16.call.", name);
6509 this->section_is_mips16_call_fp_stub_[i] =
6510 is_prefix_of(".mips16.call.fp.", name);
6511
6512 if (strcmp(name, ".pdr") == 0)
6513 {
6514 gold_assert(this->pdr_shndx_ == -1U);
6515 this->pdr_shndx_ = i;
6516 }
6517 }
6518}
6519
9810d34d
SS
6520// Discard MIPS16 stub secions that are not needed.
6521
6522template<int size, bool big_endian>
6523void
6524Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
6525{
6526 for (typename Mips16_stubs_int_map::const_iterator
6527 it = this->mips16_stub_sections_.begin();
6528 it != this->mips16_stub_sections_.end(); ++it)
6529 {
6530 Mips16_stub_section<size, big_endian>* stub_section = it->second;
6531 if (!stub_section->is_target_found())
6532 {
6533 gold_error(_("no relocation found in mips16 stub section '%s'"),
6534 stub_section->object()
6535 ->section_name(stub_section->shndx()).c_str());
6536 }
6537
6538 bool discard = false;
6539 if (stub_section->is_for_local_function())
6540 {
6541 if (stub_section->is_fn_stub())
6542 {
6543 // This stub is for a local symbol. This stub will only
6544 // be needed if there is some relocation in this object,
6545 // other than a 16 bit function call, which refers to this
6546 // symbol.
6547 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
6548 discard = true;
6549 else
6550 this->add_local_mips16_fn_stub(stub_section);
6551 }
6552 else
6553 {
6554 // This stub is for a local symbol. This stub will only
6555 // be needed if there is some relocation (R_MIPS16_26) in
6556 // this object that refers to this symbol.
6557 gold_assert(stub_section->is_call_stub()
6558 || stub_section->is_call_fp_stub());
6559 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
6560 discard = true;
6561 else
6562 this->add_local_mips16_call_stub(stub_section);
6563 }
6564 }
6565 else
6566 {
6567 Mips_symbol<size>* gsym = stub_section->gsym();
6568 if (stub_section->is_fn_stub())
6569 {
6570 if (gsym->has_mips16_fn_stub())
6571 // We already have a stub for this function.
6572 discard = true;
6573 else
6574 {
6575 gsym->set_mips16_fn_stub(stub_section);
6576 if (gsym->should_add_dynsym_entry(symtab))
6577 {
6578 // If we have a MIPS16 function with a stub, the
6579 // dynamic symbol must refer to the stub, since only
6580 // the stub uses the standard calling conventions.
6581 gsym->set_need_fn_stub();
6582 if (gsym->is_from_dynobj())
6583 gsym->set_needs_dynsym_value();
6584 }
6585 }
6586 if (!gsym->need_fn_stub())
6587 discard = true;
6588 }
6589 else if (stub_section->is_call_stub())
6590 {
6591 if (gsym->is_mips16())
6592 // We don't need the call_stub; this is a 16 bit
6593 // function, so calls from other 16 bit functions are
6594 // OK.
6595 discard = true;
6596 else if (gsym->has_mips16_call_stub())
6597 // We already have a stub for this function.
6598 discard = true;
6599 else
6600 gsym->set_mips16_call_stub(stub_section);
6601 }
6602 else
6603 {
6604 gold_assert(stub_section->is_call_fp_stub());
6605 if (gsym->is_mips16())
6606 // We don't need the call_stub; this is a 16 bit
6607 // function, so calls from other 16 bit functions are
6608 // OK.
6609 discard = true;
6610 else if (gsym->has_mips16_call_fp_stub())
6611 // We already have a stub for this function.
6612 discard = true;
6613 else
6614 gsym->set_mips16_call_fp_stub(stub_section);
6615 }
6616 }
6617 if (discard)
6618 this->set_output_section(stub_section->shndx(), NULL);
6619 }
6620}
6621
6622// Mips_output_data_la25_stub methods.
6623
6624// Template for standard LA25 stub.
6625template<int size, bool big_endian>
6626const uint32_t
6627Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
6628{
6629 0x3c190000, // lui $25,%hi(func)
6630 0x08000000, // j func
6631 0x27390000, // add $25,$25,%lo(func)
6632 0x00000000 // nop
6633};
6634
6635// Template for microMIPS LA25 stub.
6636template<int size, bool big_endian>
6637const uint32_t
6638Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
6639{
6640 0x41b9, 0x0000, // lui t9,%hi(func)
6641 0xd400, 0x0000, // j func
6642 0x3339, 0x0000, // addiu t9,t9,%lo(func)
6643 0x0000, 0x0000 // nop
6644};
6645
6646// Create la25 stub for a symbol.
6647
6648template<int size, bool big_endian>
6649void
6650Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
6651 Symbol_table* symtab, Target_mips<size, big_endian>* target,
6652 Mips_symbol<size>* gsym)
6653{
6654 if (!gsym->has_la25_stub())
6655 {
6656 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
15eb1beb 6657 this->symbols_.push_back(gsym);
9810d34d
SS
6658 this->create_stub_symbol(gsym, symtab, target, 16);
6659 }
6660}
6661
6662// Create a symbol for SYM stub's value and size, to help make the disassembly
6663// easier to read.
6664
6665template<int size, bool big_endian>
6666void
6667Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
6668 Mips_symbol<size>* sym, Symbol_table* symtab,
6669 Target_mips<size, big_endian>* target, uint64_t symsize)
6670{
6671 std::string name(".pic.");
6672 name += sym->name();
6673
6674 unsigned int offset = sym->la25_stub_offset();
6675 if (sym->is_micromips())
6676 offset |= 1;
6677
6678 // Make it a local function.
6679 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
6680 Symbol_table::PREDEFINED,
6681 target->la25_stub_section(),
6682 offset, symsize, elfcpp::STT_FUNC,
6683 elfcpp::STB_LOCAL,
6684 elfcpp::STV_DEFAULT, 0,
6685 false, false);
6686 new_sym->set_is_forced_local();
6687}
6688
6689// Write out la25 stubs. This uses the hand-coded instructions above,
6690// and adjusts them as needed.
6691
6692template<int size, bool big_endian>
6693void
6694Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
6695{
6696 const off_t offset = this->offset();
6697 const section_size_type oview_size =
6698 convert_to_section_size_type(this->data_size());
6699 unsigned char* const oview = of->get_output_view(offset, oview_size);
6700
15eb1beb 6701 for (typename std::vector<Mips_symbol<size>*>::iterator
9810d34d
SS
6702 p = this->symbols_.begin();
6703 p != this->symbols_.end();
6704 ++p)
6705 {
6706 Mips_symbol<size>* sym = *p;
6707 unsigned char* pov = oview + sym->la25_stub_offset();
6708
6709 Mips_address target = sym->value();
6710 if (!sym->is_micromips())
6711 {
6712 elfcpp::Swap<32, big_endian>::writeval(pov,
6713 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
6714 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
6715 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
6716 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
6717 la25_stub_entry[2] | (target & 0xffff));
6718 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
6719 }
6720 else
6721 {
6722 target |= 1;
6723 // First stub instruction. Paste high 16-bits of the target.
6724 elfcpp::Swap<16, big_endian>::writeval(pov,
6725 la25_stub_micromips_entry[0]);
6726 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
6727 ((target + 0x8000) >> 16) & 0xffff);
6728 // Second stub instruction. Paste low 26-bits of the target, shifted
6729 // right by 1.
6730 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
6731 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
6732 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
6733 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
6734 // Third stub instruction. Paste low 16-bits of the target.
6735 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
6736 la25_stub_micromips_entry[4]);
6737 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
6738 // Fourth stub instruction.
6739 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
6740 la25_stub_micromips_entry[6]);
6741 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
6742 la25_stub_micromips_entry[7]);
6743 }
6744 }
6745
6746 of->write_output_view(offset, oview_size, oview);
6747}
6748
6749// Mips_output_data_plt methods.
6750
6751// The format of the first PLT entry in an O32 executable.
6752template<int size, bool big_endian>
6753const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
6754{
6755 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
6756 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
6757 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
6758 0x031cc023, // subu $24, $24, $28
40fc1451 6759 0x03e07825, // or $15, $31, zero
9810d34d
SS
6760 0x0018c082, // srl $24, $24, 2
6761 0x0320f809, // jalr $25
6762 0x2718fffe // subu $24, $24, 2
6763};
6764
6765// The format of the first PLT entry in an N32 executable. Different
6766// because gp ($28) is not available; we use t2 ($14) instead.
6767template<int size, bool big_endian>
6768const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
6769{
6770 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6771 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
6772 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6773 0x030ec023, // subu $24, $24, $14
40fc1451 6774 0x03e07825, // or $15, $31, zero
9810d34d
SS
6775 0x0018c082, // srl $24, $24, 2
6776 0x0320f809, // jalr $25
6777 0x2718fffe // subu $24, $24, 2
6778};
6779
6780// The format of the first PLT entry in an N64 executable. Different
6781// from N32 because of the increased size of GOT entries.
6782template<int size, bool big_endian>
6783const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
6784{
6785 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
6786 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
6787 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
6788 0x030ec023, // subu $24, $24, $14
40fc1451 6789 0x03e07825, // or $15, $31, zero
9810d34d
SS
6790 0x0018c0c2, // srl $24, $24, 3
6791 0x0320f809, // jalr $25
6792 0x2718fffe // subu $24, $24, 2
6793};
6794
6795// The format of the microMIPS first PLT entry in an O32 executable.
6796// We rely on v0 ($2) rather than t8 ($24) to contain the address
6797// of the GOTPLT entry handled, so this stub may only be used when
6798// all the subsequent PLT entries are microMIPS code too.
6799//
6800// The trailing NOP is for alignment and correct disassembly only.
6801template<int size, bool big_endian>
6802const uint32_t Mips_output_data_plt<size, big_endian>::
6803plt0_entry_micromips_o32[] =
6804{
6805 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
6806 0xff23, 0x0000, // lw $25, 0($3)
6807 0x0535, // subu $2, $2, $3
6808 0x2525, // srl $2, $2, 2
6809 0x3302, 0xfffe, // subu $24, $2, 2
6810 0x0dff, // move $15, $31
6811 0x45f9, // jalrs $25
6812 0x0f83, // move $28, $3
6813 0x0c00 // nop
6814};
6815
6816// The format of the microMIPS first PLT entry in an O32 executable
6817// in the insn32 mode.
6818template<int size, bool big_endian>
6819const uint32_t Mips_output_data_plt<size, big_endian>::
6820plt0_entry_micromips32_o32[] =
6821{
6822 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
6823 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
6824 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
6825 0x0398, 0xc1d0, // subu $24, $24, $28
40fc1451 6826 0x001f, 0x7a90, // or $15, $31, zero
9810d34d
SS
6827 0x0318, 0x1040, // srl $24, $24, 2
6828 0x03f9, 0x0f3c, // jalr $25
6829 0x3318, 0xfffe // subu $24, $24, 2
6830};
6831
6832// The format of subsequent standard entries in the PLT.
6833template<int size, bool big_endian>
6834const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
6835{
6836 0x3c0f0000, // lui $15, %hi(.got.plt entry)
47a9f4fc 6837 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
9810d34d
SS
6838 0x03200008, // jr $25
6839 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
6840};
6841
6842// The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
6843// temporary because t8 ($24) and t9 ($25) are not directly addressable.
6844// Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
6845// We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
6846// target function address in register v0.
6847template<int size, bool big_endian>
6848const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
6849{
6850 0xb303, // lw $3, 12($pc)
6851 0x651b, // move $24, $3
6852 0x9b60, // lw $3, 0($3)
6853 0xeb00, // jr $3
6854 0x653b, // move $25, $3
6855 0x6500, // nop
6856 0x0000, 0x0000 // .word (.got.plt entry)
6857};
6858
6859// The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
6860// as a temporary because t8 ($24) is not addressable with ADDIUPC.
6861template<int size, bool big_endian>
6862const uint32_t Mips_output_data_plt<size, big_endian>::
6863plt_entry_micromips_o32[] =
6864{
6865 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
6866 0xff22, 0x0000, // lw $25, 0($2)
6867 0x4599, // jr $25
6868 0x0f02 // move $24, $2
6869};
6870
6871// The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
6872template<int size, bool big_endian>
6873const uint32_t Mips_output_data_plt<size, big_endian>::
6874plt_entry_micromips32_o32[] =
6875{
6876 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
6877 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
6878 0x0019, 0x0f3c, // jr $25
6879 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
6880};
6881
6882// Add an entry to the PLT for a symbol referenced by r_type relocation.
6883
6884template<int size, bool big_endian>
6885void
6886Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
6887 unsigned int r_type)
6888{
6889 gold_assert(!gsym->has_plt_offset());
6890
6891 // Final PLT offset for a symbol will be set in method set_plt_offsets().
6892 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
6893 + sizeof(plt0_entry_o32));
6894 this->symbols_.push_back(gsym);
6895
6896 // Record whether the relocation requires a standard MIPS
6897 // or a compressed code entry.
6898 if (jal_reloc(r_type))
6899 {
6900 if (r_type == elfcpp::R_MIPS_26)
6901 gsym->set_needs_mips_plt(true);
6902 else
6903 gsym->set_needs_comp_plt(true);
6904 }
6905
6906 section_offset_type got_offset = this->got_plt_->current_data_size();
6907
6908 // Every PLT entry needs a GOT entry which points back to the PLT
6909 // entry (this will be changed by the dynamic linker, normally
6910 // lazily when the function is called).
6911 this->got_plt_->set_current_data_size(got_offset + size/8);
6912
6913 gsym->set_needs_dynsym_entry();
6914 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
6915 got_offset);
6916}
6917
6918// Set final PLT offsets. For each symbol, determine whether standard or
6919// compressed (MIPS16 or microMIPS) PLT entry is used.
6920
6921template<int size, bool big_endian>
6922void
6923Mips_output_data_plt<size, big_endian>::set_plt_offsets()
6924{
6925 // The sizes of individual PLT entries.
6926 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
6927 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
6928 ? this->compressed_plt_entry_size() : 0);
6929
6930 for (typename std::vector<Mips_symbol<size>*>::const_iterator
6931 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
6932 {
6933 Mips_symbol<size>* mips_sym = *p;
6934
6935 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
6936 // so always use a standard entry there.
6937 //
6938 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
6939 // all MIPS16 calls will go via that stub, and there is no benefit
6940 // to having a MIPS16 entry. And in the case of call_stub a
6941 // standard entry actually has to be used as the stub ends with a J
6942 // instruction.
6943 if (this->target_->is_output_newabi()
6944 || mips_sym->has_mips16_call_stub()
6945 || mips_sym->has_mips16_call_fp_stub())
6946 {
6947 mips_sym->set_needs_mips_plt(true);
6948 mips_sym->set_needs_comp_plt(false);
6949 }
6950
6951 // Otherwise, if there are no direct calls to the function, we
6952 // have a free choice of whether to use standard or compressed
6953 // entries. Prefer microMIPS entries if the object is known to
6954 // contain microMIPS code, so that it becomes possible to create
6955 // pure microMIPS binaries. Prefer standard entries otherwise,
6956 // because MIPS16 ones are no smaller and are usually slower.
6957 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
6958 {
6959 if (this->target_->is_output_micromips())
6960 mips_sym->set_needs_comp_plt(true);
6961 else
6962 mips_sym->set_needs_mips_plt(true);
6963 }
6964
6965 if (mips_sym->needs_mips_plt())
6966 {
6967 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
6968 this->plt_mips_offset_ += plt_mips_entry_size;
6969 }
6970 if (mips_sym->needs_comp_plt())
6971 {
6972 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
6973 this->plt_comp_offset_ += plt_comp_entry_size;
6974 }
6975 }
6976
6977 // Figure out the size of the PLT header if we know that we are using it.
6978 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
6979 this->plt_header_size_ = this->get_plt_header_size();
6980}
6981
6982// Write out the PLT. This uses the hand-coded instructions above,
6983// and adjusts them as needed.
6984
6985template<int size, bool big_endian>
6986void
6987Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
6988{
6989 const off_t offset = this->offset();
6990 const section_size_type oview_size =
6991 convert_to_section_size_type(this->data_size());
6992 unsigned char* const oview = of->get_output_view(offset, oview_size);
6993
6994 const off_t gotplt_file_offset = this->got_plt_->offset();
6995 const section_size_type gotplt_size =
6996 convert_to_section_size_type(this->got_plt_->data_size());
6997 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
6998 gotplt_size);
6999 unsigned char* pov = oview;
7000
7001 Mips_address plt_address = this->address();
7002
7003 // Calculate the address of .got.plt.
7004 Mips_address gotplt_addr = this->got_plt_->address();
7005 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7006 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7007
7008 // The PLT sequence is not safe for N64 if .got.plt's address can
7009 // not be loaded in two instructions.
7010 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7011 || ~(gotplt_addr | 0x7fffffff) == 0);
7012
7013 // Write the PLT header.
7014 const uint32_t* plt0_entry = this->get_plt_header_entry();
7015 if (plt0_entry == plt0_entry_micromips_o32)
7016 {
7017 // Write microMIPS PLT header.
7018 gold_assert(gotplt_addr % 4 == 0);
7019
7020 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7021
7022 // ADDIUPC has a span of +/-16MB, check we're in range.
7023 if (gotpc_offset + 0x1000000 >= 0x2000000)
7024 {
7025 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7026 "ADDIUPC"), (long)gotpc_offset);
7027 return;
7028 }
7029
7030 elfcpp::Swap<16, big_endian>::writeval(pov,
7031 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7032 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7033 (gotpc_offset >> 2) & 0xffff);
7034 pov += 4;
7035 for (unsigned int i = 2;
7036 i < (sizeof(plt0_entry_micromips_o32)
7037 / sizeof(plt0_entry_micromips_o32[0]));
7038 i++)
7039 {
7040 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7041 pov += 2;
7042 }
7043 }
7044 else if (plt0_entry == plt0_entry_micromips32_o32)
7045 {
7046 // Write microMIPS PLT header in insn32 mode.
7047 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7048 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7049 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7050 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7051 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7052 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7053 pov += 12;
7054 for (unsigned int i = 6;
7055 i < (sizeof(plt0_entry_micromips32_o32)
7056 / sizeof(plt0_entry_micromips32_o32[0]));
7057 i++)
7058 {
7059 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7060 pov += 2;
7061 }
7062 }
7063 else
7064 {
7065 // Write standard PLT header.
7066 elfcpp::Swap<32, big_endian>::writeval(pov,
7067 plt0_entry[0] | gotplt_addr_high);
7068 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7069 plt0_entry[1] | gotplt_addr_low);
7070 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7071 plt0_entry[2] | gotplt_addr_low);
7072 pov += 12;
7073 for (int i = 3; i < 8; i++)
7074 {
7075 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7076 pov += 4;
7077 }
7078 }
7079
7080
7081 unsigned char* gotplt_pov = gotplt_view;
7082 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7083
7084 // The first two entries in .got.plt are reserved.
7085 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7086 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7087
7088 unsigned int gotplt_offset = 2 * got_entry_size;
7089 gotplt_pov += 2 * got_entry_size;
7090
7091 // Calculate the address of the PLT header.
7092 Mips_address header_address = (plt_address
7093 + (this->is_plt_header_compressed() ? 1 : 0));
7094
7095 // Initialize compressed PLT area view.
7096 unsigned char* pov2 = pov + this->plt_mips_offset_;
7097
7098 // Write the PLT entries.
7099 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7100 p = this->symbols_.begin();
7101 p != this->symbols_.end();
7102 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7103 {
7104 Mips_symbol<size>* mips_sym = *p;
7105
7106 // Calculate the address of the .got.plt entry.
7107 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7108 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7109 & 0xffff);
7110 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7111
7112 // Initially point the .got.plt entry at the PLT header.
7113 if (this->target_->is_output_n64())
7114 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7115 else
7116 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7117
7118 // Now handle the PLT itself. First the standard entry.
7119 if (mips_sym->has_mips_plt_offset())
7120 {
7121 // Pick the load opcode (LW or LD).
7122 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7123 : 0x8c000000;
7124
7125 // Fill in the PLT entry itself.
7126 elfcpp::Swap<32, big_endian>::writeval(pov,
7127 plt_entry[0] | gotplt_entry_addr_hi);
7128 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7129 plt_entry[1] | gotplt_entry_addr_lo | load);
7130 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_entry[2]);
7131 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
7132 plt_entry[3] | gotplt_entry_addr_lo);
7133 pov += 16;
7134 }
7135
7136 // Now the compressed entry. They come after any standard ones.
7137 if (mips_sym->has_comp_plt_offset())
7138 {
7139 if (!this->target_->is_output_micromips())
7140 {
7141 // Write MIPS16 PLT entry.
7142 const uint32_t* plt_entry = plt_entry_mips16_o32;
7143
7144 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7145 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7146 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7147 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7148 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7149 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7150 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7151 gotplt_entry_addr);
7152 pov2 += 16;
7153 }
7154 else if (this->target_->use_32bit_micromips_instructions())
7155 {
7156 // Write microMIPS PLT entry in insn32 mode.
7157 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7158
7159 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7160 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7161 gotplt_entry_addr_hi);
7162 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7163 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7164 gotplt_entry_addr_lo);
7165 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7166 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7167 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7168 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7169 gotplt_entry_addr_lo);
7170 pov2 += 16;
7171 }
7172 else
7173 {
7174 // Write microMIPS PLT entry.
7175 const uint32_t* plt_entry = plt_entry_micromips_o32;
7176
7177 gold_assert(gotplt_entry_addr % 4 == 0);
7178
7179 Mips_address loc_address = plt_address + pov2 - oview;
7180 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7181
7182 // ADDIUPC has a span of +/-16MB, check we're in range.
7183 if (gotpc_offset + 0x1000000 >= 0x2000000)
7184 {
7185 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7186 "range of ADDIUPC"), (long)gotpc_offset);
7187 return;
7188 }
7189
7190 elfcpp::Swap<16, big_endian>::writeval(pov2,
7191 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7192 elfcpp::Swap<16, big_endian>::writeval(
7193 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7194 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7195 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7196 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7197 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7198 pov2 += 12;
7199 }
7200 }
7201 }
7202
7203 // Check the number of bytes written for standard entries.
7204 gold_assert(static_cast<section_size_type>(
7205 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7206 // Check the number of bytes written for compressed entries.
7207 gold_assert((static_cast<section_size_type>(pov2 - pov)
7208 == this->plt_comp_offset_));
7209 // Check the total number of bytes written.
7210 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7211
7212 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7213 == gotplt_size);
7214
7215 of->write_output_view(offset, oview_size, oview);
7216 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7217}
7218
7219// Mips_output_data_mips_stubs methods.
7220
7221// The format of the lazy binding stub when dynamic symbol count is less than
7222// 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7223template<int size, bool big_endian>
7224const uint32_t
7225Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7226{
7227 0x8f998010, // lw t9,0x8010(gp)
40fc1451 7228 0x03e07825, // or t7,ra,zero
9810d34d
SS
7229 0x0320f809, // jalr t9,ra
7230 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7231};
7232
7233// The format of the lazy binding stub when dynamic symbol count is less than
7234// 64K, dynamic symbol index is less than 32K, and ABI is N64.
7235template<int size, bool big_endian>
7236const uint32_t
7237Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7238{
7239 0xdf998010, // ld t9,0x8010(gp)
40fc1451 7240 0x03e07825, // or t7,ra,zero
9810d34d
SS
7241 0x0320f809, // jalr t9,ra
7242 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7243};
7244
7245// The format of the lazy binding stub when dynamic symbol count is less than
7246// 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7247template<int size, bool big_endian>
7248const uint32_t
7249Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7250{
7251 0x8f998010, // lw t9,0x8010(gp)
40fc1451 7252 0x03e07825, // or t7,ra,zero
9810d34d
SS
7253 0x0320f809, // jalr t9,ra
7254 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7255};
7256
7257// The format of the lazy binding stub when dynamic symbol count is less than
7258// 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7259template<int size, bool big_endian>
7260const uint32_t
7261Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7262{
7263 0xdf998010, // ld t9,0x8010(gp)
40fc1451 7264 0x03e07825, // or t7,ra,zero
9810d34d
SS
7265 0x0320f809, // jalr t9,ra
7266 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7267};
7268
7269// The format of the lazy binding stub when dynamic symbol count is greater than
7270// 64K, and ABI is not N64.
7271template<int size, bool big_endian>
7272const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7273{
7274 0x8f998010, // lw t9,0x8010(gp)
40fc1451 7275 0x03e07825, // or t7,ra,zero
9810d34d
SS
7276 0x3c180000, // lui t8,DYN_INDEX
7277 0x0320f809, // jalr t9,ra
7278 0x37180000 // ori t8,t8,DYN_INDEX
7279};
7280
7281// The format of the lazy binding stub when dynamic symbol count is greater than
7282// 64K, and ABI is N64.
7283template<int size, bool big_endian>
7284const uint32_t
7285Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7286{
7287 0xdf998010, // ld t9,0x8010(gp)
40fc1451 7288 0x03e07825, // or t7,ra,zero
9810d34d
SS
7289 0x3c180000, // lui t8,DYN_INDEX
7290 0x0320f809, // jalr t9,ra
7291 0x37180000 // ori t8,t8,DYN_INDEX
7292};
7293
7294// microMIPS stubs.
7295
7296// The format of the microMIPS lazy binding stub when dynamic symbol count is
7297// less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7298template<int size, bool big_endian>
7299const uint32_t
7300Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7301{
7302 0xff3c, 0x8010, // lw t9,0x8010(gp)
7303 0x0dff, // move t7,ra
7304 0x45d9, // jalr t9
7305 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7306};
7307
7308// The format of the microMIPS lazy binding stub when dynamic symbol count is
7309// less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7310template<int size, bool big_endian>
7311const uint32_t
7312Mips_output_data_mips_stubs<size, big_endian>::
7313lazy_stub_micromips_normal_1_n64[] =
7314{
7315 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7316 0x0dff, // move t7,ra
7317 0x45d9, // jalr t9
7318 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7319};
7320
7321// The format of the microMIPS lazy binding stub when dynamic symbol
7322// count is less than 64K, dynamic symbol index is between 32K and 64K,
7323// and ABI is not N64.
7324template<int size, bool big_endian>
7325const uint32_t
7326Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7327{
7328 0xff3c, 0x8010, // lw t9,0x8010(gp)
7329 0x0dff, // move t7,ra
7330 0x45d9, // jalr t9
7331 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7332};
7333
7334// The format of the microMIPS lazy binding stub when dynamic symbol
7335// count is less than 64K, dynamic symbol index is between 32K and 64K,
7336// and ABI is N64.
7337template<int size, bool big_endian>
7338const uint32_t
7339Mips_output_data_mips_stubs<size, big_endian>::
7340lazy_stub_micromips_normal_2_n64[] =
7341{
7342 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7343 0x0dff, // move t7,ra
7344 0x45d9, // jalr t9
7345 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7346};
7347
7348// The format of the microMIPS lazy binding stub when dynamic symbol count is
7349// greater than 64K, and ABI is not N64.
7350template<int size, bool big_endian>
7351const uint32_t
7352Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7353{
7354 0xff3c, 0x8010, // lw t9,0x8010(gp)
7355 0x0dff, // move t7,ra
7356 0x41b8, 0x0000, // lui t8,DYN_INDEX
7357 0x45d9, // jalr t9
7358 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7359};
7360
7361// The format of the microMIPS lazy binding stub when dynamic symbol count is
7362// greater than 64K, and ABI is N64.
7363template<int size, bool big_endian>
7364const uint32_t
7365Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7366{
7367 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7368 0x0dff, // move t7,ra
7369 0x41b8, 0x0000, // lui t8,DYN_INDEX
7370 0x45d9, // jalr t9
7371 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7372};
7373
7374// 32-bit microMIPS stubs.
7375
7376// The format of the microMIPS lazy binding stub when dynamic symbol count is
7377// less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7378// can use only 32-bit instructions.
7379template<int size, bool big_endian>
7380const uint32_t
7381Mips_output_data_mips_stubs<size, big_endian>::
7382lazy_stub_micromips32_normal_1[] =
7383{
7384 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7385 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7386 0x03f9, 0x0f3c, // jalr ra,t9
7387 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7388};
7389
7390// The format of the microMIPS lazy binding stub when dynamic symbol count is
7391// less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7392// use only 32-bit instructions.
7393template<int size, bool big_endian>
7394const uint32_t
7395Mips_output_data_mips_stubs<size, big_endian>::
7396lazy_stub_micromips32_normal_1_n64[] =
7397{
7398 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7399 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7400 0x03f9, 0x0f3c, // jalr ra,t9
7401 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7402};
7403
7404// The format of the microMIPS lazy binding stub when dynamic symbol
7405// count is less than 64K, dynamic symbol index is between 32K and 64K,
7406// ABI is not N64, and we can use only 32-bit instructions.
7407template<int size, bool big_endian>
7408const uint32_t
7409Mips_output_data_mips_stubs<size, big_endian>::
7410lazy_stub_micromips32_normal_2[] =
7411{
7412 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7413 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7414 0x03f9, 0x0f3c, // jalr ra,t9
7415 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7416};
7417
7418// The format of the microMIPS lazy binding stub when dynamic symbol
7419// count is less than 64K, dynamic symbol index is between 32K and 64K,
7420// ABI is N64, and we can use only 32-bit instructions.
7421template<int size, bool big_endian>
7422const uint32_t
7423Mips_output_data_mips_stubs<size, big_endian>::
7424lazy_stub_micromips32_normal_2_n64[] =
7425{
7426 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7427 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7428 0x03f9, 0x0f3c, // jalr ra,t9
7429 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7430};
7431
7432// The format of the microMIPS lazy binding stub when dynamic symbol count is
7433// greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7434template<int size, bool big_endian>
7435const uint32_t
7436Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7437{
7438 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7439 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7440 0x41b8, 0x0000, // lui t8,DYN_INDEX
7441 0x03f9, 0x0f3c, // jalr ra,t9
7442 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7443};
7444
7445// The format of the microMIPS lazy binding stub when dynamic symbol count is
7446// greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7447template<int size, bool big_endian>
7448const uint32_t
7449Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
7450{
7451 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7452 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7453 0x41b8, 0x0000, // lui t8,DYN_INDEX
7454 0x03f9, 0x0f3c, // jalr ra,t9
7455 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7456};
7457
7458// Create entry for a symbol.
7459
7460template<int size, bool big_endian>
7461void
7462Mips_output_data_mips_stubs<size, big_endian>::make_entry(
7463 Mips_symbol<size>* gsym)
7464{
7465 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
7466 {
7467 this->symbols_.insert(gsym);
7468 gsym->set_has_lazy_stub(true);
7469 }
7470}
7471
7472// Remove entry for a symbol.
7473
7474template<int size, bool big_endian>
7475void
7476Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
7477 Mips_symbol<size>* gsym)
7478{
7479 if (gsym->has_lazy_stub())
7480 {
7481 this->symbols_.erase(gsym);
7482 gsym->set_has_lazy_stub(false);
7483 }
7484}
7485
7486// Set stub offsets for symbols. This method expects that the number of
7487// entries in dynamic symbol table is set.
7488
7489template<int size, bool big_endian>
7490void
7491Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
7492{
7493 gold_assert(this->dynsym_count_ != -1U);
7494
7495 if (this->stub_offsets_are_set_)
7496 return;
7497
7498 unsigned int stub_size = this->stub_size();
7499 unsigned int offset = 0;
15eb1beb 7500 for (typename Mips_stubs_entry_set::const_iterator
9810d34d
SS
7501 p = this->symbols_.begin();
7502 p != this->symbols_.end();
7503 ++p, offset += stub_size)
7504 {
7505 Mips_symbol<size>* mips_sym = *p;
7506 mips_sym->set_lazy_stub_offset(offset);
7507 }
7508 this->stub_offsets_are_set_ = true;
7509}
7510
7511template<int size, bool big_endian>
7512void
7513Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
7514{
15eb1beb 7515 for (typename Mips_stubs_entry_set::const_iterator
9810d34d
SS
7516 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7517 {
7518 Mips_symbol<size>* sym = *p;
7519 if (sym->is_from_dynobj())
7520 sym->set_needs_dynsym_value();
7521 }
7522}
7523
7524// Write out the .MIPS.stubs. This uses the hand-coded instructions and
7525// adjusts them as needed.
7526
7527template<int size, bool big_endian>
7528void
7529Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
7530{
7531 const off_t offset = this->offset();
7532 const section_size_type oview_size =
7533 convert_to_section_size_type(this->data_size());
7534 unsigned char* const oview = of->get_output_view(offset, oview_size);
7535
7536 bool big_stub = this->dynsym_count_ > 0x10000;
7537
7538 unsigned char* pov = oview;
15eb1beb 7539 for (typename Mips_stubs_entry_set::const_iterator
9810d34d
SS
7540 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7541 {
7542 Mips_symbol<size>* sym = *p;
7543 const uint32_t* lazy_stub;
7544 bool n64 = this->target_->is_output_n64();
7545
7546 if (!this->target_->is_output_micromips())
7547 {
7548 // Write standard (non-microMIPS) stub.
7549 if (!big_stub)
7550 {
7551 if (sym->dynsym_index() & ~0x7fff)
7552 // Dynsym index is between 32K and 64K.
7553 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
7554 else
7555 // Dynsym index is less than 32K.
7556 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
7557 }
7558 else
7559 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
7560
7561 unsigned int i = 0;
7562 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7563 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
7564 pov += 8;
7565
7566 i += 2;
7567 if (big_stub)
7568 {
7569 // LUI instruction of the big stub. Paste high 16 bits of the
7570 // dynsym index.
7571 elfcpp::Swap<32, big_endian>::writeval(pov,
7572 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
7573 pov += 4;
7574 i += 1;
7575 }
7576 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
7577 // Last stub instruction. Paste low 16 bits of the dynsym index.
7578 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7579 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
7580 pov += 8;
7581 }
7582 else if (this->target_->use_32bit_micromips_instructions())
7583 {
7584 // Write microMIPS stub in insn32 mode.
7585 if (!big_stub)
7586 {
7587 if (sym->dynsym_index() & ~0x7fff)
7588 // Dynsym index is between 32K and 64K.
7589 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
7590 : lazy_stub_micromips32_normal_2;
7591 else
7592 // Dynsym index is less than 32K.
7593 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
7594 : lazy_stub_micromips32_normal_1;
7595 }
7596 else
7597 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
7598 : lazy_stub_micromips32_big;
7599
7600 unsigned int i = 0;
7601 // First stub instruction. We emit 32-bit microMIPS instructions by
7602 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7603 // the instruction where the opcode is must always come first, for
7604 // both little and big endian.
7605 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7606 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7607 // Second stub instruction.
7608 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7609 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
7610 pov += 8;
7611 i += 4;
7612 if (big_stub)
7613 {
7614 // LUI instruction of the big stub. Paste high 16 bits of the
7615 // dynsym index.
7616 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7617 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7618 (sym->dynsym_index() >> 16) & 0x7fff);
7619 pov += 4;
7620 i += 2;
7621 }
7622 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7623 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7624 // Last stub instruction. Paste low 16 bits of the dynsym index.
7625 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7626 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7627 sym->dynsym_index() & 0xffff);
7628 pov += 8;
7629 }
7630 else
7631 {
7632 // Write microMIPS stub.
7633 if (!big_stub)
7634 {
7635 if (sym->dynsym_index() & ~0x7fff)
7636 // Dynsym index is between 32K and 64K.
7637 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
7638 : lazy_stub_micromips_normal_2;
7639 else
7640 // Dynsym index is less than 32K.
7641 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
7642 : lazy_stub_micromips_normal_1;
7643 }
7644 else
7645 lazy_stub = n64 ? lazy_stub_micromips_big_n64
7646 : lazy_stub_micromips_big;
7647
7648 unsigned int i = 0;
7649 // First stub instruction. We emit 32-bit microMIPS instructions by
7650 // emitting two 16-bit parts because on microMIPS the 16-bit part of
7651 // the instruction where the opcode is must always come first, for
7652 // both little and big endian.
7653 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7654 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7655 // Second stub instruction.
7656 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
7657 pov += 6;
7658 i += 3;
7659 if (big_stub)
7660 {
7661 // LUI instruction of the big stub. Paste high 16 bits of the
7662 // dynsym index.
7663 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7664 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7665 (sym->dynsym_index() >> 16) & 0x7fff);
7666 pov += 4;
7667 i += 2;
7668 }
7669 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
7670 // Last stub instruction. Paste low 16 bits of the dynsym index.
7671 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
7672 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7673 sym->dynsym_index() & 0xffff);
7674 pov += 6;
7675 }
7676 }
7677
7678 // We always allocate 20 bytes for every stub, because final dynsym count is
7679 // not known in method do_finalize_sections. There are 4 unused bytes per
7680 // stub if final dynsym count is less than 0x10000.
7681 unsigned int used = pov - oview;
7682 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
7683 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
7684
7685 // Fill the unused space with zeroes.
7686 // TODO(sasa): Can we strip unused bytes during the relaxation?
7687 if (unused > 0)
7688 memset(pov, 0, unused);
7689
7690 of->write_output_view(offset, oview_size, oview);
7691}
7692
7693// Mips_output_section_reginfo methods.
7694
7695template<int size, bool big_endian>
7696void
7697Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
7698{
7699 off_t offset = this->offset();
7700 off_t data_size = this->data_size();
7701
7702 unsigned char* view = of->get_output_view(offset, data_size);
7703 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
7704 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
7705 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
7706 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
7707 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
7708 // Write the gp value.
7709 elfcpp::Swap<size, big_endian>::writeval(view + 20,
7710 this->target_->gp_value());
7711
7712 of->write_output_view(offset, data_size, view);
7713}
7714
7715// Mips_copy_relocs methods.
7716
7717// Emit any saved relocs.
7718
7719template<int sh_type, int size, bool big_endian>
7720void
7721Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
7722 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7723 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7724{
7725 for (typename Copy_relocs<sh_type, size, big_endian>::
7726 Copy_reloc_entries::iterator p = this->entries_.begin();
7727 p != this->entries_.end();
7728 ++p)
7729 emit_entry(*p, reloc_section, symtab, layout, target);
7730
7731 // We no longer need the saved information.
7732 this->entries_.clear();
7733}
7734
7735// Emit the reloc if appropriate.
7736
7737template<int sh_type, int size, bool big_endian>
7738void
7739Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
7740 Copy_reloc_entry& entry,
7741 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
7742 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
7743{
7744 // If the symbol is no longer defined in a dynamic object, then we
7745 // emitted a COPY relocation, and we do not want to emit this
7746 // dynamic relocation.
7747 if (!entry.sym_->is_from_dynobj())
7748 return;
7749
7750 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
7751 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
7752 || entry.reloc_type_ == elfcpp::R_MIPS_64);
7753
7754 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
7755 if (can_make_dynamic && !sym->has_static_relocs())
7756 {
7757 Mips_relobj<size, big_endian>* object =
7758 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
7759 target->got_section(symtab, layout)->record_global_got_symbol(
7760 sym, object, entry.reloc_type_, true, false);
7761 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
7762 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
7763 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
7764 else
7765 target->rel_dyn_section(layout)->add_symbolless_global_addend(
7766 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
7767 entry.shndx_, entry.address_);
7768 }
7769 else
7770 this->make_copy_reloc(symtab, layout,
7771 static_cast<Sized_symbol<size>*>(entry.sym_),
6eeb0170 7772 entry.relobj_,
9810d34d
SS
7773 reloc_section);
7774}
7775
7776// Target_mips methods.
7777
7778// Return the value to use for a dynamic symbol which requires special
7779// treatment. This is how we support equality comparisons of function
7780// pointers across shared library boundaries, as described in the
7781// processor specific ABI supplement.
7782
7783template<int size, bool big_endian>
7784uint64_t
7785Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7786{
7787 uint64_t value = 0;
7788 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
7789
7790 if (!mips_sym->has_lazy_stub())
7791 {
7792 if (mips_sym->has_plt_offset())
7793 {
7794 // We distinguish between PLT entries and lazy-binding stubs by
7795 // giving the former an st_other value of STO_MIPS_PLT. Set the
7796 // value to the stub address if there are any relocations in the
7797 // binary where pointer equality matters.
7798 if (mips_sym->pointer_equality_needed())
7799 {
7800 // Prefer a standard MIPS PLT entry.
7801 if (mips_sym->has_mips_plt_offset())
7802 value = this->plt_section()->mips_entry_address(mips_sym);
7803 else
7804 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
7805 }
7806 else
7807 value = 0;
7808 }
7809 }
7810 else
7811 {
7812 // First, set stub offsets for symbols. This method expects that the
7813 // number of entries in dynamic symbol table is set.
7814 this->mips_stubs_section()->set_lazy_stub_offsets();
7815
7816 // The run-time linker uses the st_value field of the symbol
7817 // to reset the global offset table entry for this external
7818 // to its stub address when unlinking a shared object.
7819 value = this->mips_stubs_section()->stub_address(mips_sym);
7820 }
7821
7822 if (mips_sym->has_mips16_fn_stub())
7823 {
7824 // If we have a MIPS16 function with a stub, the dynamic symbol must
7825 // refer to the stub, since only the stub uses the standard calling
7826 // conventions.
7827 value = mips_sym->template
7828 get_mips16_fn_stub<big_endian>()->output_address();
7829 }
7830
7831 return value;
7832}
7833
7834// Get the dynamic reloc section, creating it if necessary. It's always
7835// .rel.dyn, even for MIPS64.
7836
7837template<int size, bool big_endian>
7838typename Target_mips<size, big_endian>::Reloc_section*
7839Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
7840{
7841 if (this->rel_dyn_ == NULL)
7842 {
7843 gold_assert(layout != NULL);
7844 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
7845 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
7846 elfcpp::SHF_ALLOC, this->rel_dyn_,
7847 ORDER_DYNAMIC_RELOCS, false);
7848
7849 // First entry in .rel.dyn has to be null.
7850 // This is hack - we define dummy output data and set its address to 0,
7851 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
7852 // This ensures that the entry is null.
7853 Output_data* od = new Output_data_zero_fill(0, 0);
7854 od->set_address(0);
7855 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
7856 }
7857 return this->rel_dyn_;
7858}
7859
7860// Get the GOT section, creating it if necessary.
7861
7862template<int size, bool big_endian>
7863Mips_output_data_got<size, big_endian>*
7864Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
7865 Layout* layout)
7866{
7867 if (this->got_ == NULL)
7868 {
7869 gold_assert(symtab != NULL && layout != NULL);
7870
7871 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
7872 layout);
7873 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
7874 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
7875 elfcpp::SHF_MIPS_GPREL),
7876 this->got_, ORDER_DATA, false);
7877
7878 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
7879 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
7880 Symbol_table::PREDEFINED,
7881 this->got_,
7882 0, 0, elfcpp::STT_OBJECT,
7883 elfcpp::STB_GLOBAL,
7884 elfcpp::STV_DEFAULT, 0,
7885 false, false);
7886 }
7887
7888 return this->got_;
7889}
7890
7891// Calculate value of _gp symbol.
7892
7893template<int size, bool big_endian>
7894void
7895Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
7896{
7897 if (this->gp_ != NULL)
7898 return;
7899
7900 Output_data* section = layout->find_output_section(".got");
7901 if (section == NULL)
7902 {
7903 // If there is no .got section, gp should be based on .sdata.
7904 // TODO(sasa): This is probably not needed. This was needed for older
7905 // MIPS architectures which accessed both GOT and .sdata section using
7906 // gp-relative addressing. Modern Mips Linux ELF architectures don't
7907 // access .sdata using gp-relative addressing.
7908 for (Layout::Section_list::const_iterator
7909 p = layout->section_list().begin();
7910 p != layout->section_list().end();
7911 ++p)
7912 {
7913 if (strcmp((*p)->name(), ".sdata") == 0)
7914 {
7915 section = *p;
7916 break;
7917 }
7918 }
7919 }
7920
7921 Sized_symbol<size>* gp =
7922 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
7923 if (gp != NULL)
7924 {
7925 if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
7926 gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
7927 elfcpp::STT_OBJECT,
7928 elfcpp::STB_GLOBAL,
7929 elfcpp::STV_DEFAULT, 0,
7930 false, false);
7931 this->gp_ = gp;
7932 }
7933 else if (section != NULL)
7934 {
7935 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
7936 "_gp", NULL, Symbol_table::PREDEFINED,
7937 section, MIPS_GP_OFFSET, 0,
7938 elfcpp::STT_OBJECT,
7939 elfcpp::STB_GLOBAL,
7940 elfcpp::STV_DEFAULT,
7941 0, false, false));
7942 this->gp_ = gp;
7943 }
7944}
7945
7946// Set the dynamic symbol indexes. INDEX is the index of the first
7947// global dynamic symbol. Pointers to the symbols are stored into the
7948// vector SYMS. The names are added to DYNPOOL. This returns an
7949// updated dynamic symbol index.
7950
7951template<int size, bool big_endian>
7952unsigned int
7953Target_mips<size, big_endian>::do_set_dynsym_indexes(
7954 std::vector<Symbol*>* dyn_symbols, unsigned int index,
7955 std::vector<Symbol*>* syms, Stringpool* dynpool,
7956 Versions* versions, Symbol_table* symtab) const
7957{
7958 std::vector<Symbol*> non_got_symbols;
7959 std::vector<Symbol*> got_symbols;
7960
7961 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
7962 &got_symbols);
7963
7964 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
7965 p != non_got_symbols.end();
7966 ++p)
7967 {
7968 Symbol* sym = *p;
7969
7970 // Note that SYM may already have a dynamic symbol index, since
7971 // some symbols appear more than once in the symbol table, with
7972 // and without a version.
7973
7974 if (!sym->has_dynsym_index())
7975 {
7976 sym->set_dynsym_index(index);
7977 ++index;
7978 syms->push_back(sym);
7979 dynpool->add(sym->name(), false, NULL);
7980
7981 // Record any version information.
7982 if (sym->version() != NULL)
7983 versions->record_version(symtab, dynpool, sym);
7984
7985 // If the symbol is defined in a dynamic object and is
7986 // referenced in a regular object, then mark the dynamic
7987 // object as needed. This is used to implement --as-needed.
7988 if (sym->is_from_dynobj() && sym->in_reg())
7989 sym->object()->set_is_needed();
7990 }
7991 }
7992
7993 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
7994 p != got_symbols.end();
7995 ++p)
7996 {
7997 Symbol* sym = *p;
7998 if (!sym->has_dynsym_index())
7999 {
8000 // Record any version information.
8001 if (sym->version() != NULL)
8002 versions->record_version(symtab, dynpool, sym);
8003 }
8004 }
8005
8006 index = versions->finalize(symtab, index, syms);
8007
8008 int got_sym_count = 0;
8009 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8010 p != got_symbols.end();
8011 ++p)
8012 {
8013 Symbol* sym = *p;
8014
8015 if (!sym->has_dynsym_index())
8016 {
8017 ++got_sym_count;
8018 sym->set_dynsym_index(index);
8019 ++index;
8020 syms->push_back(sym);
8021 dynpool->add(sym->name(), false, NULL);
8022
8023 // If the symbol is defined in a dynamic object and is
8024 // referenced in a regular object, then mark the dynamic
8025 // object as needed. This is used to implement --as-needed.
8026 if (sym->is_from_dynobj() && sym->in_reg())
8027 sym->object()->set_is_needed();
8028 }
8029 }
8030
8031 // Set index of the first symbol that has .got entry.
8032 this->got_->set_first_global_got_dynsym_index(
8033 got_sym_count > 0 ? index - got_sym_count : -1U);
8034
8035 if (this->mips_stubs_ != NULL)
8036 this->mips_stubs_->set_dynsym_count(index);
8037
8038 return index;
8039}
8040
8041// Create a PLT entry for a global symbol referenced by r_type relocation.
8042
8043template<int size, bool big_endian>
8044void
8045Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8046 Layout* layout,
8047 Mips_symbol<size>* gsym,
8048 unsigned int r_type)
8049{
8050 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8051 return;
8052
8053 if (this->plt_ == NULL)
8054 {
8055 // Create the GOT section first.
8056 this->got_section(symtab, layout);
8057
8058 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8059 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8060 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8061 this->got_plt_, ORDER_DATA, false);
8062
8063 // The first two entries are reserved.
8064 this->got_plt_->set_current_data_size(2 * size/8);
8065
8066 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8067 this->got_plt_,
8068 this);
8069 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8070 (elfcpp::SHF_ALLOC
8071 | elfcpp::SHF_EXECINSTR),
8072 this->plt_, ORDER_PLT, false);
8073 }
8074
8075 this->plt_->add_entry(gsym, r_type);
8076}
8077
8078
8079// Get the .MIPS.stubs section, creating it if necessary.
8080
8081template<int size, bool big_endian>
8082Mips_output_data_mips_stubs<size, big_endian>*
8083Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8084{
8085 if (this->mips_stubs_ == NULL)
8086 {
8087 this->mips_stubs_ =
8088 new Mips_output_data_mips_stubs<size, big_endian>(this);
8089 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8090 (elfcpp::SHF_ALLOC
8091 | elfcpp::SHF_EXECINSTR),
8092 this->mips_stubs_, ORDER_PLT, false);
8093 }
8094 return this->mips_stubs_;
8095}
8096
8097// Get the LA25 stub section, creating it if necessary.
8098
8099template<int size, bool big_endian>
8100Mips_output_data_la25_stub<size, big_endian>*
8101Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8102{
8103 if (this->la25_stub_ == NULL)
8104 {
8105 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8106 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8107 (elfcpp::SHF_ALLOC
8108 | elfcpp::SHF_EXECINSTR),
8109 this->la25_stub_, ORDER_TEXT, false);
8110 }
8111 return this->la25_stub_;
8112}
8113
8114// Process the relocations to determine unreferenced sections for
8115// garbage collection.
8116
8117template<int size, bool big_endian>
8118void
8119Target_mips<size, big_endian>::gc_process_relocs(
8120 Symbol_table* symtab,
8121 Layout* layout,
8122 Sized_relobj_file<size, big_endian>* object,
8123 unsigned int data_shndx,
47a9f4fc 8124 unsigned int sh_type,
9810d34d
SS
8125 const unsigned char* prelocs,
8126 size_t reloc_count,
8127 Output_section* output_section,
8128 bool needs_special_offset_handling,
8129 size_t local_symbol_count,
8130 const unsigned char* plocal_symbols)
8131{
8132 typedef Target_mips<size, big_endian> Mips;
9810d34d 8133
47a9f4fc
VR
8134 if (sh_type == elfcpp::SHT_REL)
8135 {
8136 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8137 Classify_reloc;
8138
8139 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8140 symtab,
8141 layout,
8142 this,
8143 object,
8144 data_shndx,
8145 prelocs,
8146 reloc_count,
8147 output_section,
8148 needs_special_offset_handling,
8149 local_symbol_count,
8150 plocal_symbols);
8151 }
8152 else if (sh_type == elfcpp::SHT_RELA)
8153 {
8154 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8155 Classify_reloc;
8156
8157 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8158 symtab,
8159 layout,
8160 this,
8161 object,
8162 data_shndx,
8163 prelocs,
8164 reloc_count,
8165 output_section,
8166 needs_special_offset_handling,
8167 local_symbol_count,
8168 plocal_symbols);
8169 }
8170 else
8171 gold_unreachable();
9810d34d
SS
8172}
8173
8174// Scan relocations for a section.
8175
8176template<int size, bool big_endian>
8177void
8178Target_mips<size, big_endian>::scan_relocs(
8179 Symbol_table* symtab,
8180 Layout* layout,
8181 Sized_relobj_file<size, big_endian>* object,
8182 unsigned int data_shndx,
8183 unsigned int sh_type,
8184 const unsigned char* prelocs,
8185 size_t reloc_count,
8186 Output_section* output_section,
8187 bool needs_special_offset_handling,
8188 size_t local_symbol_count,
8189 const unsigned char* plocal_symbols)
8190{
8191 typedef Target_mips<size, big_endian> Mips;
9810d34d
SS
8192
8193 if (sh_type == elfcpp::SHT_REL)
4d625b70
CC
8194 {
8195 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8196 Classify_reloc;
8197
8198 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8199 symtab,
8200 layout,
8201 this,
8202 object,
8203 data_shndx,
8204 prelocs,
8205 reloc_count,
8206 output_section,
8207 needs_special_offset_handling,
8208 local_symbol_count,
8209 plocal_symbols);
8210 }
9810d34d 8211 else if (sh_type == elfcpp::SHT_RELA)
4d625b70
CC
8212 {
8213 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8214 Classify_reloc;
8215
8216 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8217 symtab,
8218 layout,
8219 this,
8220 object,
8221 data_shndx,
8222 prelocs,
8223 reloc_count,
8224 output_section,
8225 needs_special_offset_handling,
8226 local_symbol_count,
8227 plocal_symbols);
8228 }
9810d34d
SS
8229}
8230
8231template<int size, bool big_endian>
8232bool
8233Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8234{
8235 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8236 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8237 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8238 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8239 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8240 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
8241 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
8242}
8243
8244// Return the MACH for a MIPS e_flags value.
8245template<int size, bool big_endian>
8246unsigned int
8247Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8248{
8249 switch (flags & elfcpp::EF_MIPS_MACH)
8250 {
8251 case elfcpp::E_MIPS_MACH_3900:
8252 return mach_mips3900;
8253
8254 case elfcpp::E_MIPS_MACH_4010:
8255 return mach_mips4010;
8256
8257 case elfcpp::E_MIPS_MACH_4100:
8258 return mach_mips4100;
8259
8260 case elfcpp::E_MIPS_MACH_4111:
8261 return mach_mips4111;
8262
8263 case elfcpp::E_MIPS_MACH_4120:
8264 return mach_mips4120;
8265
8266 case elfcpp::E_MIPS_MACH_4650:
8267 return mach_mips4650;
8268
8269 case elfcpp::E_MIPS_MACH_5400:
8270 return mach_mips5400;
8271
8272 case elfcpp::E_MIPS_MACH_5500:
8273 return mach_mips5500;
8274
8275 case elfcpp::E_MIPS_MACH_9000:
8276 return mach_mips9000;
8277
8278 case elfcpp::E_MIPS_MACH_SB1:
8279 return mach_mips_sb1;
8280
8281 case elfcpp::E_MIPS_MACH_LS2E:
8282 return mach_mips_loongson_2e;
8283
8284 case elfcpp::E_MIPS_MACH_LS2F:
8285 return mach_mips_loongson_2f;
8286
8287 case elfcpp::E_MIPS_MACH_LS3A:
8288 return mach_mips_loongson_3a;
8289
8290 case elfcpp::E_MIPS_MACH_OCTEON2:
8291 return mach_mips_octeon2;
8292
8293 case elfcpp::E_MIPS_MACH_OCTEON:
8294 return mach_mips_octeon;
8295
8296 case elfcpp::E_MIPS_MACH_XLR:
8297 return mach_mips_xlr;
8298
8299 default:
8300 switch (flags & elfcpp::EF_MIPS_ARCH)
8301 {
8302 default:
8303 case elfcpp::E_MIPS_ARCH_1:
8304 return mach_mips3000;
8305
8306 case elfcpp::E_MIPS_ARCH_2:
8307 return mach_mips6000;
8308
8309 case elfcpp::E_MIPS_ARCH_3:
8310 return mach_mips4000;
8311
8312 case elfcpp::E_MIPS_ARCH_4:
8313 return mach_mips8000;
8314
8315 case elfcpp::E_MIPS_ARCH_5:
8316 return mach_mips5;
8317
8318 case elfcpp::E_MIPS_ARCH_32:
8319 return mach_mipsisa32;
8320
8321 case elfcpp::E_MIPS_ARCH_64:
8322 return mach_mipsisa64;
8323
8324 case elfcpp::E_MIPS_ARCH_32R2:
8325 return mach_mipsisa32r2;
8326
8327 case elfcpp::E_MIPS_ARCH_64R2:
8328 return mach_mipsisa64r2;
8329 }
8330 }
8331
8332 return 0;
8333}
8334
8335// Check whether machine EXTENSION is an extension of machine BASE.
8336template<int size, bool big_endian>
8337bool
8338Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
8339 unsigned int extension)
8340{
8341 if (extension == base)
8342 return true;
8343
8344 if ((base == mach_mipsisa32)
8345 && this->mips_mach_extends(mach_mipsisa64, extension))
8346 return true;
8347
8348 if ((base == mach_mipsisa32r2)
8349 && this->mips_mach_extends(mach_mipsisa64r2, extension))
8350 return true;
8351
8352 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
8353 if (extension == this->mips_mach_extensions_[i].first)
8354 {
8355 extension = this->mips_mach_extensions_[i].second;
8356 if (extension == base)
8357 return true;
8358 }
8359
8360 return false;
8361}
8362
8363template<int size, bool big_endian>
8364void
8365Target_mips<size, big_endian>::merge_processor_specific_flags(
01b84e25 8366 const std::string& name, elfcpp::Elf_Word in_flags, bool dyn_obj)
9810d34d
SS
8367{
8368 // If flags are not set yet, just copy them.
8369 if (!this->are_processor_specific_flags_set())
8370 {
8371 this->set_processor_specific_flags(in_flags);
9810d34d
SS
8372 this->mach_ = this->elf_mips_mach(in_flags);
8373 return;
8374 }
8375
8376 elfcpp::Elf_Word new_flags = in_flags;
8377 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
8378 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
8379 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
8380
8381 // Check flag compatibility.
8382 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
8383 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
8384
8385 // Some IRIX 6 BSD-compatibility objects have this bit set. It
8386 // doesn't seem to matter.
8387 new_flags &= ~elfcpp::EF_MIPS_XGOT;
8388 old_flags &= ~elfcpp::EF_MIPS_XGOT;
8389
8390 // MIPSpro generates ucode info in n64 objects. Again, we should
8391 // just be able to ignore this.
8392 new_flags &= ~elfcpp::EF_MIPS_UCODE;
8393 old_flags &= ~elfcpp::EF_MIPS_UCODE;
8394
8395 // DSOs should only be linked with CPIC code.
8396 if (dyn_obj)
8397 new_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
8398
8399 if (new_flags == old_flags)
8400 {
8401 this->set_processor_specific_flags(merged_flags);
8402 return;
8403 }
8404
8405 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
8406 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
8407 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
8408 name.c_str());
8409
8410 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
8411 merged_flags |= elfcpp::EF_MIPS_CPIC;
8412 if (!(new_flags & elfcpp::EF_MIPS_PIC))
8413 merged_flags &= ~elfcpp::EF_MIPS_PIC;
8414
8415 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
8416 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
8417
8418 // Compare the ISAs.
8419 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
8420 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
8421 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
8422 {
8423 // Output ISA isn't the same as, or an extension of, input ISA.
8424 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
8425 {
8426 // Copy the architecture info from input object to output. Also copy
8427 // the 32-bit flag (if set) so that we continue to recognise
8428 // output as a 32-bit binary.
8429 this->mach_ = this->elf_mips_mach(in_flags);
8430 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
8431 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
8432 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
8433
8434 // Copy across the ABI flags if output doesn't use them
8435 // and if that was what caused us to treat input object as 32-bit.
8436 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
8437 && this->mips_32bit_flags(new_flags)
8438 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
8439 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
8440 }
8441 else
8442 // The ISAs aren't compatible.
8443 gold_error(_("%s: linking %s module with previous %s modules"),
8444 name.c_str(), this->elf_mips_mach_name(in_flags),
8445 this->elf_mips_mach_name(merged_flags));
8446 }
8447
8448 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
8449 | elfcpp::EF_MIPS_32BITMODE));
8450 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
8451 | elfcpp::EF_MIPS_32BITMODE));
8452
01b84e25
VR
8453 // Compare ABIs.
8454 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9810d34d
SS
8455 {
8456 // Only error if both are set (to different values).
01b84e25 8457 if ((new_flags & elfcpp::EF_MIPS_ABI)
9810d34d 8458 && (old_flags & elfcpp::EF_MIPS_ABI))
9810d34d
SS
8459 gold_error(_("%s: ABI mismatch: linking %s module with "
8460 "previous %s modules"), name.c_str(),
01b84e25
VR
8461 this->elf_mips_abi_name(in_flags),
8462 this->elf_mips_abi_name(merged_flags));
9810d34d
SS
8463
8464 new_flags &= ~elfcpp::EF_MIPS_ABI;
8465 old_flags &= ~elfcpp::EF_MIPS_ABI;
8466 }
8467
8468 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
8469 // and allow arbitrary mixing of the remaining ASEs (retain the union).
8470 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
8471 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
8472 {
8473 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
8474 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
8475 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
8476 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
8477 int micro_mis = old_m16 && new_micro;
8478 int m16_mis = old_micro && new_m16;
8479
8480 if (m16_mis || micro_mis)
8481 gold_error(_("%s: ASE mismatch: linking %s module with "
8482 "previous %s modules"), name.c_str(),
8483 m16_mis ? "MIPS16" : "microMIPS",
8484 m16_mis ? "microMIPS" : "MIPS16");
8485
8486 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
8487
8488 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
8489 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
8490 }
8491
8492 // Warn about any other mismatches.
8493 if (new_flags != old_flags)
8494 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
8495 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
8496
8497 this->set_processor_specific_flags(merged_flags);
8498}
8499
8500// Adjust ELF file header.
8501
8502template<int size, bool big_endian>
8503void
8504Target_mips<size, big_endian>::do_adjust_elf_header(
8505 unsigned char* view,
8506 int len)
8507{
8508 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
8509
01b84e25
VR
8510 if (!this->entry_symbol_is_compressed_)
8511 return;
9810d34d 8512
01b84e25 8513 elfcpp::Ehdr<size, big_endian> ehdr(view);
9810d34d 8514 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
01b84e25
VR
8515
8516 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9810d34d
SS
8517}
8518
8519// do_make_elf_object to override the same function in the base class.
8520// We need to use a target-specific sub-class of
8521// Sized_relobj_file<size, big_endian> to store Mips specific information.
8522// Hence we need to have our own ELF object creation.
8523
8524template<int size, bool big_endian>
8525Object*
8526Target_mips<size, big_endian>::do_make_elf_object(
8527 const std::string& name,
8528 Input_file* input_file,
8529 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
8530{
8531 int et = ehdr.get_e_type();
8532 // ET_EXEC files are valid input for --just-symbols/-R,
8533 // and we treat them as relocatable objects.
8534 if (et == elfcpp::ET_REL
8535 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
8536 {
8537 Mips_relobj<size, big_endian>* obj =
8538 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
8539 obj->setup();
8540 return obj;
8541 }
8542 else if (et == elfcpp::ET_DYN)
8543 {
8544 // TODO(sasa): Should we create Mips_dynobj?
8545 return Target::do_make_elf_object(name, input_file, offset, ehdr);
8546 }
8547 else
8548 {
8549 gold_error(_("%s: unsupported ELF file type %d"),
8550 name.c_str(), et);
8551 return NULL;
8552 }
8553}
8554
8555// Finalize the sections.
8556
8557template <int size, bool big_endian>
8558void
8559Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
8560 const Input_objects* input_objects,
8561 Symbol_table* symtab)
8562{
8563 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
8564 // DT_FINI have correct values.
8565 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
8566 symtab->lookup(parameters->options().init()));
8567 if (init != NULL && (init->is_mips16() || init->is_micromips()))
8568 init->set_value(init->value() | 1);
8569 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
8570 symtab->lookup(parameters->options().fini()));
8571 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
8572 fini->set_value(fini->value() | 1);
8573
8574 // Check whether the entry symbol is mips16 or micromips. This is needed to
8575 // adjust entry address in ELF header.
8576 Mips_symbol<size>* entry =
8577 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
8578 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
8579 || entry->is_micromips()));
8580
8581 if (!parameters->doing_static_link()
8582 && (strcmp(parameters->options().hash_style(), "gnu") == 0
8583 || strcmp(parameters->options().hash_style(), "both") == 0))
8584 {
8585 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
8586 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
8587 // MIPS ABI requires a mapping between the GOT and the symbol table.
8588 gold_error(".gnu.hash is incompatible with the MIPS ABI");
8589 }
8590
8591 // Check whether the final section that was scanned has HI16 or GOT16
8592 // relocations without the corresponding LO16 part.
8593 if (this->got16_addends_.size() > 0)
8594 gold_error("Can't find matching LO16 reloc");
8595
8596 // Set _gp value.
8597 this->set_gp(layout, symtab);
8598
8599 // Check for any mips16 stub sections that we can discard.
8600 if (!parameters->options().relocatable())
8601 {
8602 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8603 p != input_objects->relobj_end();
8604 ++p)
8605 {
8606 Mips_relobj<size, big_endian>* object =
8607 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8608 object->discard_mips16_stub_sections(symtab);
8609 }
8610 }
8611
8612 // Merge processor-specific flags.
8613 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8614 p != input_objects->relobj_end();
8615 ++p)
8616 {
8617 Mips_relobj<size, big_endian>* relobj =
8618 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
8619
8620 Input_file::Format format = relobj->input_file()->format();
8621 if (format == Input_file::FORMAT_ELF)
8622 {
8623 // Read processor-specific flags in ELF file header.
8624 const unsigned char* pehdr = relobj->get_view(
8625 elfcpp::file_header_offset,
8626 elfcpp::Elf_sizes<size>::ehdr_size,
8627 true, false);
8628
8629 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
8630 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
47a9f4fc
VR
8631 // If all input sections will be discarded, don't use this object
8632 // file for merging processor specific flags.
8633 bool should_merge_processor_specific_flags = false;
8634
8635 for (unsigned int i = 1; i < relobj->shnum(); ++i)
8636 if (relobj->output_section(i) != NULL)
8637 {
8638 should_merge_processor_specific_flags = true;
8639 break;
8640 }
9810d34d 8641
47a9f4fc
VR
8642 if (should_merge_processor_specific_flags)
8643 this->merge_processor_specific_flags(relobj->name(), in_flags,
01b84e25 8644 false);
9810d34d
SS
8645 }
8646 }
8647
8648 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8649 p != input_objects->dynobj_end();
8650 ++p)
8651 {
8652 Sized_dynobj<size, big_endian>* dynobj =
8653 static_cast<Sized_dynobj<size, big_endian>*>(*p);
8654
8655 // Read processor-specific flags.
8656 const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
8657 elfcpp::Elf_sizes<size>::ehdr_size,
8658 true, false);
8659
8660 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
8661 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
9810d34d 8662
01b84e25 8663 this->merge_processor_specific_flags(dynobj->name(), in_flags, true);
9810d34d
SS
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*
01b84e25 11241Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
9810d34d
SS
11242{
11243 switch (e_flags & elfcpp::EF_MIPS_ABI)
11244 {
11245 case 0:
11246 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
11247 return "N32";
01b84e25 11248 else if (size == 64)
9810d34d
SS
11249 return "64";
11250 else
11251 return "none";
11252 case elfcpp::E_MIPS_ABI_O32:
11253 return "O32";
11254 case elfcpp::E_MIPS_ABI_O64:
11255 return "O64";
11256 case elfcpp::E_MIPS_ABI_EABI32:
11257 return "EABI32";
11258 case elfcpp::E_MIPS_ABI_EABI64:
11259 return "EABI64";
11260 default:
11261 return "unknown abi";
11262 }
11263}
11264
11265template<int size, bool big_endian>
11266const char*
11267Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
11268{
11269 switch (e_flags & elfcpp::EF_MIPS_MACH)
11270 {
11271 case elfcpp::E_MIPS_MACH_3900:
11272 return "mips:3900";
11273 case elfcpp::E_MIPS_MACH_4010:
11274 return "mips:4010";
11275 case elfcpp::E_MIPS_MACH_4100:
11276 return "mips:4100";
11277 case elfcpp::E_MIPS_MACH_4111:
11278 return "mips:4111";
11279 case elfcpp::E_MIPS_MACH_4120:
11280 return "mips:4120";
11281 case elfcpp::E_MIPS_MACH_4650:
11282 return "mips:4650";
11283 case elfcpp::E_MIPS_MACH_5400:
11284 return "mips:5400";
11285 case elfcpp::E_MIPS_MACH_5500:
11286 return "mips:5500";
11287 case elfcpp::E_MIPS_MACH_SB1:
11288 return "mips:sb1";
11289 case elfcpp::E_MIPS_MACH_9000:
11290 return "mips:9000";
11291 case elfcpp::E_MIPS_MACH_LS2E:
11292 return "mips:loongson-2e";
11293 case elfcpp::E_MIPS_MACH_LS2F:
11294 return "mips:loongson-2f";
11295 case elfcpp::E_MIPS_MACH_LS3A:
11296 return "mips:loongson-3a";
11297 case elfcpp::E_MIPS_MACH_OCTEON:
11298 return "mips:octeon";
11299 case elfcpp::E_MIPS_MACH_OCTEON2:
11300 return "mips:octeon2";
11301 case elfcpp::E_MIPS_MACH_XLR:
11302 return "mips:xlr";
11303 default:
11304 switch (e_flags & elfcpp::EF_MIPS_ARCH)
11305 {
11306 default:
11307 case elfcpp::E_MIPS_ARCH_1:
11308 return "mips:3000";
11309
11310 case elfcpp::E_MIPS_ARCH_2:
11311 return "mips:6000";
11312
11313 case elfcpp::E_MIPS_ARCH_3:
11314 return "mips:4000";
11315
11316 case elfcpp::E_MIPS_ARCH_4:
11317 return "mips:8000";
11318
11319 case elfcpp::E_MIPS_ARCH_5:
11320 return "mips:mips5";
11321
11322 case elfcpp::E_MIPS_ARCH_32:
11323 return "mips:isa32";
11324
11325 case elfcpp::E_MIPS_ARCH_64:
11326 return "mips:isa64";
11327
11328 case elfcpp::E_MIPS_ARCH_32R2:
11329 return "mips:isa32r2";
11330
11331 case elfcpp::E_MIPS_ARCH_64R2:
11332 return "mips:isa64r2";
11333 }
11334 }
11335 return "unknown CPU";
11336}
11337
11338template<int size, bool big_endian>
62661c93 11339const Target::Target_info Target_mips<size, big_endian>::mips_info =
9810d34d
SS
11340{
11341 size, // size
11342 big_endian, // is_big_endian
11343 elfcpp::EM_MIPS, // machine_code
11344 true, // has_make_symbol
11345 false, // has_resolve
11346 false, // has_code_fill
11347 true, // is_default_stack_executable
11348 false, // can_icf_inline_merge_sections
11349 '\0', // wrap_char
47a9f4fc 11350 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
9810d34d
SS
11351 0x400000, // default_text_segment_address
11352 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
11353 4 * 1024, // common_pagesize (overridable by -z common-page-size)
11354 false, // isolate_execinstr
11355 0, // rosegment_gap
11356 elfcpp::SHN_UNDEF, // small_common_shndx
11357 elfcpp::SHN_UNDEF, // large_common_shndx
11358 0, // small_common_section_flags
11359 0, // large_common_section_flags
11360 NULL, // attributes_section
11361 NULL, // attributes_vendor
8d9743bd
MK
11362 "__start", // entry_symbol_name
11363 32, // hash_entry_size
9810d34d
SS
11364};
11365
62661c93
SS
11366template<int size, bool big_endian>
11367class Target_mips_nacl : public Target_mips<size, big_endian>
11368{
11369 public:
11370 Target_mips_nacl()
11371 : Target_mips<size, big_endian>(&mips_nacl_info)
11372 { }
11373
11374 private:
11375 static const Target::Target_info mips_nacl_info;
11376};
11377
11378template<int size, bool big_endian>
11379const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
11380{
11381 size, // size
11382 big_endian, // is_big_endian
11383 elfcpp::EM_MIPS, // machine_code
11384 true, // has_make_symbol
11385 false, // has_resolve
11386 false, // has_code_fill
11387 true, // is_default_stack_executable
11388 false, // can_icf_inline_merge_sections
11389 '\0', // wrap_char
11390 "/lib/ld.so.1", // dynamic_linker
11391 0x20000, // default_text_segment_address
11392 0x10000, // abi_pagesize (overridable by -z max-page-size)
11393 0x10000, // common_pagesize (overridable by -z common-page-size)
11394 true, // isolate_execinstr
11395 0x10000000, // rosegment_gap
11396 elfcpp::SHN_UNDEF, // small_common_shndx
11397 elfcpp::SHN_UNDEF, // large_common_shndx
11398 0, // small_common_section_flags
11399 0, // large_common_section_flags
11400 NULL, // attributes_section
11401 NULL, // attributes_vendor
8d9743bd
MK
11402 "_start", // entry_symbol_name
11403 32, // hash_entry_size
62661c93
SS
11404};
11405
11406// Target selector for Mips. Note this is never instantiated directly.
11407// It's only used in Target_selector_mips_nacl, below.
9810d34d
SS
11408
11409template<int size, bool big_endian>
11410class Target_selector_mips : public Target_selector
11411{
11412public:
11413 Target_selector_mips()
11414 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
11415 (size == 64 ?
11416 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
11417 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
11418 (size == 64 ?
47a9f4fc
VR
11419 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
11420 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
9810d34d
SS
11421 { }
11422
11423 Target* do_instantiate_target()
11424 { return new Target_mips<size, big_endian>(); }
11425};
11426
62661c93
SS
11427template<int size, bool big_endian>
11428class Target_selector_mips_nacl
11429 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
11430 Target_mips_nacl<size, big_endian> >
11431{
11432 public:
11433 Target_selector_mips_nacl()
11434 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
11435 Target_mips_nacl<size, big_endian> >(
11436 // NaCl currently supports only MIPS32 little-endian.
11437 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
11438 { }
11439};
9810d34d 11440
62661c93
SS
11441Target_selector_mips_nacl<32, true> target_selector_mips32;
11442Target_selector_mips_nacl<32, false> target_selector_mips32el;
11443Target_selector_mips_nacl<64, true> target_selector_mips64;
11444Target_selector_mips_nacl<64, false> target_selector_mips64el;
9810d34d
SS
11445
11446} // End anonymous namespace.
This page took 0.552931 seconds and 4 git commands to generate.