Mips: Omit empty objects for merging processor-specific data.
[deliverable/binutils-gdb.git] / gold / mips.cc
CommitLineData
9810d34d
SS
1// mips.cc -- mips target support for gold.
2
2571583a 3// Copyright (C) 2011-2017 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"
b52717c0 47#include "attributes.h"
62661c93 48#include "nacl.h"
9810d34d
SS
49
50namespace
51{
52using namespace gold;
53
54template<int size, bool big_endian>
55class Mips_output_data_plt;
56
57template<int size, bool big_endian>
58class Mips_output_data_got;
59
60template<int size, bool big_endian>
61class Target_mips;
62
63template<int size, bool big_endian>
64class Mips_output_section_reginfo;
65
1728969e
VR
66template<int size, bool big_endian>
67class Mips_output_section_options;
68
9810d34d
SS
69template<int size, bool big_endian>
70class Mips_output_data_la25_stub;
71
72template<int size, bool big_endian>
73class Mips_output_data_mips_stubs;
74
75template<int size>
76class Mips_symbol;
77
78template<int size, bool big_endian>
79class Mips_got_info;
80
81template<int size, bool big_endian>
82class Mips_relobj;
83
84class Mips16_stub_section_base;
85
86template<int size, bool big_endian>
87class Mips16_stub_section;
88
89// The ABI says that every symbol used by dynamic relocations must have
90// a global GOT entry. Among other things, this provides the dynamic
91// linker with a free, directly-indexed cache. The GOT can therefore
92// contain symbols that are not referenced by GOT relocations themselves
93// (in other words, it may have symbols that are not referenced by things
94// like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
95
96// GOT relocations are less likely to overflow if we put the associated
97// GOT entries towards the beginning. We therefore divide the global
98// GOT entries into two areas: "normal" and "reloc-only". Entries in
99// the first area can be used for both dynamic relocations and GP-relative
100// accesses, while those in the "reloc-only" area are for dynamic
101// relocations only.
102
103// These GGA_* ("Global GOT Area") values are organised so that lower
104// values are more general than higher values. Also, non-GGA_NONE
105// values are ordered by the position of the area in the GOT.
106
107enum Global_got_area
108{
109 GGA_NORMAL = 0,
110 GGA_RELOC_ONLY = 1,
111 GGA_NONE = 2
112};
113
114// The types of GOT entries needed for this platform.
115// These values are exposed to the ABI in an incremental link.
116// Do not renumber existing values without changing the version
117// number of the .gnu_incremental_inputs section.
118enum Got_type
119{
120 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
121 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
122 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
123
124 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
125 GOT_TYPE_STANDARD_MULTIGOT = 3,
126 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
127 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
128};
129
130// TLS type of GOT entry.
131enum Got_tls_type
132{
133 GOT_TLS_NONE = 0,
134 GOT_TLS_GD = 1,
135 GOT_TLS_LDM = 2,
136 GOT_TLS_IE = 4
137};
138
47a9f4fc
VR
139// Values found in the r_ssym field of a relocation entry.
140enum Special_relocation_symbol
141{
142 RSS_UNDEF = 0, // None - value is zero.
143 RSS_GP = 1, // Value of GP.
144 RSS_GP0 = 2, // Value of GP in object being relocated.
145 RSS_LOC = 3 // Address of location being relocated.
146};
147
148// Whether the section is readonly.
149static inline bool
150is_readonly_section(Output_section* output_section)
151{
152 elfcpp::Elf_Xword section_flags = output_section->flags();
153 elfcpp::Elf_Word section_type = output_section->type();
154
155 if (section_type == elfcpp::SHT_NOBITS)
156 return false;
157
158 if (section_flags & elfcpp::SHF_WRITE)
159 return false;
160
161 return true;
162}
163
9810d34d
SS
164// Return TRUE if a relocation of type R_TYPE from OBJECT might
165// require an la25 stub. See also local_pic_function, which determines
166// whether the destination function ever requires a stub.
167template<int size, bool big_endian>
168static inline bool
169relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
170 unsigned int r_type, bool target_is_16_bit_code)
171{
172 // We specifically ignore branches and jumps from EF_PIC objects,
173 // where the onus is on the compiler or programmer to perform any
174 // necessary initialization of $25. Sometimes such initialization
175 // is unnecessary; for example, -mno-shared functions do not use
176 // the incoming value of $25, and may therefore be called directly.
177 if (object->is_pic())
178 return false;
179
180 switch (r_type)
181 {
182 case elfcpp::R_MIPS_26:
183 case elfcpp::R_MIPS_PC16:
f5b11759
VR
184 case elfcpp::R_MIPS_PC21_S2:
185 case elfcpp::R_MIPS_PC26_S2:
9810d34d
SS
186 case elfcpp::R_MICROMIPS_26_S1:
187 case elfcpp::R_MICROMIPS_PC7_S1:
188 case elfcpp::R_MICROMIPS_PC10_S1:
189 case elfcpp::R_MICROMIPS_PC16_S1:
190 case elfcpp::R_MICROMIPS_PC23_S2:
191 return true;
192
193 case elfcpp::R_MIPS16_26:
194 return !target_is_16_bit_code;
195
196 default:
197 return false;
198 }
199}
200
201// Return true if SYM is a locally-defined PIC function, in the sense
202// that it or its fn_stub might need $25 to be valid on entry.
203// Note that MIPS16 functions set up $gp using PC-relative instructions,
204// so they themselves never need $25 to be valid. Only non-MIPS16
205// entry points are of interest here.
206template<int size, bool big_endian>
207static inline bool
208local_pic_function(Mips_symbol<size>* sym)
209{
210 bool def_regular = (sym->source() == Symbol::FROM_OBJECT
211 && !sym->object()->is_dynamic()
212 && !sym->is_undefined());
213
214 if (sym->is_defined() && def_regular)
215 {
216 Mips_relobj<size, big_endian>* object =
217 static_cast<Mips_relobj<size, big_endian>*>(sym->object());
218
219 if ((object->is_pic() || sym->is_pic())
220 && (!sym->is_mips16()
221 || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
222 return true;
223 }
224 return false;
225}
226
227static inline bool
228hi16_reloc(int r_type)
229{
230 return (r_type == elfcpp::R_MIPS_HI16
231 || r_type == elfcpp::R_MIPS16_HI16
f5b11759
VR
232 || r_type == elfcpp::R_MICROMIPS_HI16
233 || r_type == elfcpp::R_MIPS_PCHI16);
9810d34d
SS
234}
235
236static inline bool
237lo16_reloc(int r_type)
238{
239 return (r_type == elfcpp::R_MIPS_LO16
240 || r_type == elfcpp::R_MIPS16_LO16
f5b11759
VR
241 || r_type == elfcpp::R_MICROMIPS_LO16
242 || r_type == elfcpp::R_MIPS_PCLO16);
9810d34d
SS
243}
244
245static inline bool
246got16_reloc(unsigned int r_type)
247{
248 return (r_type == elfcpp::R_MIPS_GOT16
249 || r_type == elfcpp::R_MIPS16_GOT16
250 || r_type == elfcpp::R_MICROMIPS_GOT16);
251}
252
253static inline bool
254call_lo16_reloc(unsigned int r_type)
255{
256 return (r_type == elfcpp::R_MIPS_CALL_LO16
257 || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
258}
259
260static inline bool
261got_lo16_reloc(unsigned int r_type)
262{
263 return (r_type == elfcpp::R_MIPS_GOT_LO16
264 || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
265}
266
47a9f4fc
VR
267static inline bool
268eh_reloc(unsigned int r_type)
269{
270 return (r_type == elfcpp::R_MIPS_EH);
271}
272
9810d34d
SS
273static inline bool
274got_disp_reloc(unsigned int r_type)
275{
276 return (r_type == elfcpp::R_MIPS_GOT_DISP
277 || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
278}
279
280static inline bool
281got_page_reloc(unsigned int r_type)
282{
283 return (r_type == elfcpp::R_MIPS_GOT_PAGE
284 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
285}
286
287static inline bool
288tls_gd_reloc(unsigned int r_type)
289{
290 return (r_type == elfcpp::R_MIPS_TLS_GD
291 || r_type == elfcpp::R_MIPS16_TLS_GD
292 || r_type == elfcpp::R_MICROMIPS_TLS_GD);
293}
294
295static inline bool
296tls_gottprel_reloc(unsigned int r_type)
297{
298 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
299 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
300 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
301}
302
303static inline bool
304tls_ldm_reloc(unsigned int r_type)
305{
306 return (r_type == elfcpp::R_MIPS_TLS_LDM
307 || r_type == elfcpp::R_MIPS16_TLS_LDM
308 || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
309}
310
311static inline bool
312mips16_call_reloc(unsigned int r_type)
313{
314 return (r_type == elfcpp::R_MIPS16_26
315 || r_type == elfcpp::R_MIPS16_CALL16);
316}
317
318static inline bool
319jal_reloc(unsigned int r_type)
320{
321 return (r_type == elfcpp::R_MIPS_26
322 || r_type == elfcpp::R_MIPS16_26
323 || r_type == elfcpp::R_MICROMIPS_26_S1);
324}
325
326static inline bool
327micromips_branch_reloc(unsigned int r_type)
328{
329 return (r_type == elfcpp::R_MICROMIPS_26_S1
330 || r_type == elfcpp::R_MICROMIPS_PC16_S1
331 || r_type == elfcpp::R_MICROMIPS_PC10_S1
332 || r_type == elfcpp::R_MICROMIPS_PC7_S1);
333}
334
335// Check if R_TYPE is a MIPS16 reloc.
336static inline bool
337mips16_reloc(unsigned int r_type)
338{
339 switch (r_type)
340 {
341 case elfcpp::R_MIPS16_26:
342 case elfcpp::R_MIPS16_GPREL:
343 case elfcpp::R_MIPS16_GOT16:
344 case elfcpp::R_MIPS16_CALL16:
345 case elfcpp::R_MIPS16_HI16:
346 case elfcpp::R_MIPS16_LO16:
347 case elfcpp::R_MIPS16_TLS_GD:
348 case elfcpp::R_MIPS16_TLS_LDM:
349 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
350 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
351 case elfcpp::R_MIPS16_TLS_GOTTPREL:
352 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
353 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
354 return true;
355
356 default:
357 return false;
358 }
359}
360
361// Check if R_TYPE is a microMIPS reloc.
362static inline bool
363micromips_reloc(unsigned int r_type)
364{
365 switch (r_type)
366 {
367 case elfcpp::R_MICROMIPS_26_S1:
368 case elfcpp::R_MICROMIPS_HI16:
369 case elfcpp::R_MICROMIPS_LO16:
370 case elfcpp::R_MICROMIPS_GPREL16:
371 case elfcpp::R_MICROMIPS_LITERAL:
372 case elfcpp::R_MICROMIPS_GOT16:
373 case elfcpp::R_MICROMIPS_PC7_S1:
374 case elfcpp::R_MICROMIPS_PC10_S1:
375 case elfcpp::R_MICROMIPS_PC16_S1:
376 case elfcpp::R_MICROMIPS_CALL16:
377 case elfcpp::R_MICROMIPS_GOT_DISP:
378 case elfcpp::R_MICROMIPS_GOT_PAGE:
379 case elfcpp::R_MICROMIPS_GOT_OFST:
380 case elfcpp::R_MICROMIPS_GOT_HI16:
381 case elfcpp::R_MICROMIPS_GOT_LO16:
382 case elfcpp::R_MICROMIPS_SUB:
383 case elfcpp::R_MICROMIPS_HIGHER:
384 case elfcpp::R_MICROMIPS_HIGHEST:
385 case elfcpp::R_MICROMIPS_CALL_HI16:
386 case elfcpp::R_MICROMIPS_CALL_LO16:
387 case elfcpp::R_MICROMIPS_SCN_DISP:
388 case elfcpp::R_MICROMIPS_JALR:
389 case elfcpp::R_MICROMIPS_HI0_LO16:
390 case elfcpp::R_MICROMIPS_TLS_GD:
391 case elfcpp::R_MICROMIPS_TLS_LDM:
392 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
393 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
394 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
395 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
396 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
397 case elfcpp::R_MICROMIPS_GPREL7_S2:
398 case elfcpp::R_MICROMIPS_PC23_S2:
399 return true;
400
401 default:
402 return false;
403 }
404}
405
406static inline bool
407is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc)
408{
409 switch (high_reloc)
410 {
411 case elfcpp::R_MIPS_HI16:
412 case elfcpp::R_MIPS_GOT16:
413 return lo16_reloc == elfcpp::R_MIPS_LO16;
f5b11759
VR
414 case elfcpp::R_MIPS_PCHI16:
415 return lo16_reloc == elfcpp::R_MIPS_PCLO16;
9810d34d
SS
416 case elfcpp::R_MIPS16_HI16:
417 case elfcpp::R_MIPS16_GOT16:
418 return lo16_reloc == elfcpp::R_MIPS16_LO16;
419 case elfcpp::R_MICROMIPS_HI16:
420 case elfcpp::R_MICROMIPS_GOT16:
421 return lo16_reloc == elfcpp::R_MICROMIPS_LO16;
422 default:
423 return false;
424 }
425}
426
427// This class is used to hold information about one GOT entry.
428// There are three types of entry:
429//
430// (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
431// (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
432// (2) a SYMBOL address, where SYMBOL is not local to an input object
15eb1beb
VR
433// (sym != NULL, symndx == -1)
434// (3) a TLS LDM slot (there's only one of these per GOT.)
9810d34d
SS
435// (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
436
437template<int size, bool big_endian>
438class Mips_got_entry
439{
440 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
441
442 public:
443 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
444 Mips_address addend, unsigned char tls_type,
47a9f4fc 445 unsigned int shndx, bool is_section_symbol)
15eb1beb 446 : addend_(addend), symndx_(symndx), tls_type_(tls_type),
47a9f4fc 447 is_section_symbol_(is_section_symbol), shndx_(shndx)
15eb1beb 448 { this->d.object = object; }
9810d34d 449
15eb1beb
VR
450 Mips_got_entry(Mips_symbol<size>* sym, unsigned char tls_type)
451 : addend_(0), symndx_(-1U), tls_type_(tls_type),
47a9f4fc 452 is_section_symbol_(false), shndx_(-1U)
9810d34d
SS
453 { this->d.sym = sym; }
454
455 // Return whether this entry is for a local symbol.
456 bool
457 is_for_local_symbol() const
458 { return this->symndx_ != -1U; }
459
460 // Return whether this entry is for a global symbol.
461 bool
462 is_for_global_symbol() const
463 { return this->symndx_ == -1U; }
464
465 // Return the hash of this entry.
466 size_t
467 hash() const
468 {
469 if (this->tls_type_ == GOT_TLS_LDM)
470 return this->symndx_ + (1 << 18);
15eb1beb
VR
471
472 size_t name_hash_value = gold::string_hash<char>(
473 (this->symndx_ != -1U)
474 ? this->d.object->name().c_str()
475 : this->d.sym->name());
476 size_t addend = this->addend_;
477 return name_hash_value ^ this->symndx_ ^ addend;
9810d34d
SS
478 }
479
480 // Return whether this entry is equal to OTHER.
481 bool
482 equals(Mips_got_entry<size, big_endian>* other) const
483 {
9810d34d
SS
484 if (this->tls_type_ == GOT_TLS_LDM)
485 return true;
15eb1beb
VR
486
487 return ((this->tls_type_ == other->tls_type_)
488 && (this->symndx_ == other->symndx_)
489 && ((this->symndx_ != -1U)
490 ? (this->d.object == other->d.object)
491 : (this->d.sym == other->d.sym))
492 && (this->addend_ == other->addend_));
9810d34d
SS
493 }
494
495 // Return input object that needs this GOT entry.
496 Mips_relobj<size, big_endian>*
497 object() const
498 {
15eb1beb
VR
499 gold_assert(this->symndx_ != -1U);
500 return this->d.object;
9810d34d
SS
501 }
502
503 // Return local symbol index for local GOT entries.
504 unsigned int
505 symndx() const
506 {
507 gold_assert(this->symndx_ != -1U);
508 return this->symndx_;
509 }
510
511 // Return the relocation addend for local GOT entries.
512 Mips_address
513 addend() const
15eb1beb 514 { return this->addend_; }
9810d34d
SS
515
516 // Return global symbol for global GOT entries.
517 Mips_symbol<size>*
518 sym() const
519 {
520 gold_assert(this->symndx_ == -1U);
521 return this->d.sym;
522 }
523
524 // Return whether this is a TLS GOT entry.
525 bool
526 is_tls_entry() const
527 { return this->tls_type_ != GOT_TLS_NONE; }
528
529 // Return TLS type of this GOT entry.
530 unsigned char
531 tls_type() const
532 { return this->tls_type_; }
533
534 // Return section index of the local symbol for local GOT entries.
535 unsigned int
536 shndx() const
537 { return this->shndx_; }
538
47a9f4fc
VR
539 // Return whether this is a STT_SECTION symbol.
540 bool
541 is_section_symbol() const
542 { return this->is_section_symbol_; }
543
9810d34d 544 private:
15eb1beb
VR
545 // The addend.
546 Mips_address addend_;
547
9810d34d
SS
548 // The index of the symbol if we have a local symbol; -1 otherwise.
549 unsigned int symndx_;
550
551 union
552 {
15eb1beb
VR
553 // The input object for local symbols that needs the GOT entry.
554 Mips_relobj<size, big_endian>* object;
9810d34d
SS
555 // If symndx == -1, the global symbol corresponding to this GOT entry. The
556 // symbol's entry is in the local area if mips_sym->global_got_area is
557 // GGA_NONE, otherwise it is in the global area.
558 Mips_symbol<size>* sym;
559 } d;
560
561 // The TLS type of this GOT entry. An LDM GOT entry will be a local
562 // symbol entry with r_symndx == 0.
563 unsigned char tls_type_;
564
47a9f4fc
VR
565 // Whether this is a STT_SECTION symbol.
566 bool is_section_symbol_;
567
9810d34d
SS
568 // For local GOT entries, section index of the local symbol.
569 unsigned int shndx_;
570};
571
572// Hash for Mips_got_entry.
573
574template<int size, bool big_endian>
575class Mips_got_entry_hash
576{
577 public:
578 size_t
579 operator()(Mips_got_entry<size, big_endian>* entry) const
580 { return entry->hash(); }
581};
582
583// Equality for Mips_got_entry.
584
585template<int size, bool big_endian>
586class Mips_got_entry_eq
587{
588 public:
589 bool
590 operator()(Mips_got_entry<size, big_endian>* e1,
591 Mips_got_entry<size, big_endian>* e2) const
592 { return e1->equals(e2); }
593};
594
15eb1beb
VR
595// Hash for Mips_symbol.
596
597template<int size>
598class Mips_symbol_hash
599{
600 public:
601 size_t
602 operator()(Mips_symbol<size>* sym) const
603 { return sym->hash(); }
604};
605
9810d34d
SS
606// Got_page_range. This class describes a range of addends: [MIN_ADDEND,
607// MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
608// increasing MIN_ADDEND.
609
610struct Got_page_range
611{
612 Got_page_range()
613 : next(NULL), min_addend(0), max_addend(0)
614 { }
615
616 Got_page_range* next;
617 int min_addend;
618 int max_addend;
619
620 // Return the maximum number of GOT page entries required.
621 int
622 get_max_pages()
623 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
624};
625
626// Got_page_entry. This class describes the range of addends that are applied
627// to page relocations against a given symbol.
628
629struct Got_page_entry
630{
631 Got_page_entry()
632 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
633 { }
634
635 Got_page_entry(Object* object_, unsigned int symndx_)
636 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
637 { }
638
639 // The input object that needs the GOT page entry.
640 Object* object;
641 // The index of the symbol, as stored in the relocation r_info.
642 unsigned int symndx;
643 // The ranges for this page entry.
644 Got_page_range* ranges;
645 // The maximum number of page entries needed for RANGES.
646 unsigned int num_pages;
647};
648
649// Hash for Got_page_entry.
650
651struct Got_page_entry_hash
652{
653 size_t
654 operator()(Got_page_entry* entry) const
655 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
656};
657
658// Equality for Got_page_entry.
659
660struct Got_page_entry_eq
661{
662 bool
663 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
664 {
665 return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
666 }
667};
668
669// This class is used to hold .got information when linking.
670
671template<int size, bool big_endian>
672class Mips_got_info
673{
674 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
675 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
676 Reloc_section;
677 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
678
679 // Unordered set of GOT entries.
680 typedef Unordered_set<Mips_got_entry<size, big_endian>*,
681 Mips_got_entry_hash<size, big_endian>,
682 Mips_got_entry_eq<size, big_endian> > Got_entry_set;
683
684 // Unordered set of GOT page entries.
685 typedef Unordered_set<Got_page_entry*,
686 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
687
15eb1beb
VR
688 // Unordered set of global GOT entries.
689 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
690 Global_got_entry_set;
691
9810d34d
SS
692 public:
693 Mips_got_info()
694 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
695 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
696 got_entries_(), got_page_entries_(), got_page_offset_start_(0),
697 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
698 offset_(0)
699 { }
700
701 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
702 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
703 void
704 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
705 unsigned int symndx, Mips_address addend,
47a9f4fc
VR
706 unsigned int r_type, unsigned int shndx,
707 bool is_section_symbol);
9810d34d
SS
708
709 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
710 // in OBJECT. FOR_CALL is true if the caller is only interested in
711 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
712 // relocation.
713 void
714 record_global_got_symbol(Mips_symbol<size>* mips_sym,
715 Mips_relobj<size, big_endian>* object,
716 unsigned int r_type, bool dyn_reloc, bool for_call);
717
718 // Add ENTRY to master GOT and to OBJECT's GOT.
719 void
720 record_got_entry(Mips_got_entry<size, big_endian>* entry,
721 Mips_relobj<size, big_endian>* object);
722
723 // Record that OBJECT has a page relocation against symbol SYMNDX and
724 // that ADDEND is the addend for that relocation.
725 void
726 record_got_page_entry(Mips_relobj<size, big_endian>* object,
727 unsigned int symndx, int addend);
728
729 // Create all entries that should be in the local part of the GOT.
730 void
731 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
732
733 // Create GOT page entries.
734 void
735 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
736
737 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
738 void
739 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
740 unsigned int non_reloc_only_global_gotno);
741
742 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
743 void
744 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
745
746 // Create TLS GOT entries.
747 void
748 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
749
750 // Decide whether the symbol needs an entry in the global part of the primary
751 // GOT, setting global_got_area accordingly. Count the number of global
752 // symbols that are in the primary GOT only because they have dynamic
753 // relocations R_MIPS_REL32 against them (reloc_only_gotno).
754 void
755 count_got_symbols(Symbol_table* symtab);
756
757 // Return the offset of GOT page entry for VALUE.
758 unsigned int
759 get_got_page_offset(Mips_address value,
760 Mips_output_data_got<size, big_endian>* got);
761
762 // Count the number of GOT entries required.
763 void
764 count_got_entries();
765
766 // Count the number of GOT entries required by ENTRY. Accumulate the result.
767 void
768 count_got_entry(Mips_got_entry<size, big_endian>* entry);
769
770 // Add FROM's GOT entries.
771 void
772 add_got_entries(Mips_got_info<size, big_endian>* from);
773
774 // Add FROM's GOT page entries.
775 void
776 add_got_page_entries(Mips_got_info<size, big_endian>* from);
777
778 // Return GOT size.
779 unsigned int
780 got_size() const
781 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
782 + this->tls_gotno_) * size/8);
783 }
784
785 // Return the number of local GOT entries.
786 unsigned int
787 local_gotno() const
788 { return this->local_gotno_; }
789
790 // Return the maximum number of page GOT entries needed.
791 unsigned int
792 page_gotno() const
793 { return this->page_gotno_; }
794
795 // Return the number of global GOT entries.
796 unsigned int
797 global_gotno() const
798 { return this->global_gotno_; }
799
800 // Set the number of global GOT entries.
801 void
802 set_global_gotno(unsigned int global_gotno)
803 { this->global_gotno_ = global_gotno; }
804
805 // Return the number of GGA_RELOC_ONLY global GOT entries.
806 unsigned int
807 reloc_only_gotno() const
808 { return this->reloc_only_gotno_; }
809
810 // Return the number of TLS GOT entries.
811 unsigned int
812 tls_gotno() const
813 { return this->tls_gotno_; }
814
815 // Return the GOT type for this GOT. Used for multi-GOT links only.
816 unsigned int
817 multigot_got_type(unsigned int got_type) const
818 {
819 switch (got_type)
820 {
821 case GOT_TYPE_STANDARD:
822 return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
823 case GOT_TYPE_TLS_OFFSET:
824 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
825 case GOT_TYPE_TLS_PAIR:
826 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
827 default:
828 gold_unreachable();
829 }
830 }
831
832 // Remove lazy-binding stubs for global symbols in this GOT.
833 void
834 remove_lazy_stubs(Target_mips<size, big_endian>* target);
835
836 // Return offset of this GOT from the start of .got section.
837 unsigned int
838 offset() const
839 { return this->offset_; }
840
841 // Set offset of this GOT from the start of .got section.
842 void
843 set_offset(unsigned int offset)
844 { this->offset_ = offset; }
845
846 // Set index of this GOT in multi-GOT links.
847 void
848 set_index(unsigned int index)
849 { this->index_ = index; }
850
851 // Return next GOT in multi-GOT links.
852 Mips_got_info<size, big_endian>*
853 next() const
854 { return this->next_; }
855
856 // Set next GOT in multi-GOT links.
857 void
858 set_next(Mips_got_info<size, big_endian>* next)
859 { this->next_ = next; }
860
861 // Return the offset of TLS LDM entry for this GOT.
862 unsigned int
863 tls_ldm_offset() const
864 { return this->tls_ldm_offset_; }
865
866 // Set the offset of TLS LDM entry for this GOT.
867 void
868 set_tls_ldm_offset(unsigned int tls_ldm_offset)
869 { this->tls_ldm_offset_ = tls_ldm_offset; }
870
15eb1beb 871 Global_got_entry_set&
9810d34d
SS
872 global_got_symbols()
873 { return this->global_got_symbols_; }
874
875 // Return the GOT_TLS_* type required by relocation type R_TYPE.
876 static int
877 mips_elf_reloc_tls_type(unsigned int r_type)
878 {
879 if (tls_gd_reloc(r_type))
880 return GOT_TLS_GD;
881
882 if (tls_ldm_reloc(r_type))
883 return GOT_TLS_LDM;
884
885 if (tls_gottprel_reloc(r_type))
886 return GOT_TLS_IE;
887
888 return GOT_TLS_NONE;
889 }
890
891 // Return the number of GOT slots needed for GOT TLS type TYPE.
892 static int
893 mips_tls_got_entries(unsigned int type)
894 {
895 switch (type)
896 {
897 case GOT_TLS_GD:
898 case GOT_TLS_LDM:
899 return 2;
900
901 case GOT_TLS_IE:
902 return 1;
903
904 case GOT_TLS_NONE:
905 return 0;
906
907 default:
908 gold_unreachable();
909 }
910 }
911
912 private:
913 // The number of local GOT entries.
914 unsigned int local_gotno_;
915 // The maximum number of page GOT entries needed.
916 unsigned int page_gotno_;
917 // The number of global GOT entries.
918 unsigned int global_gotno_;
919 // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
920 unsigned int reloc_only_gotno_;
921 // The number of TLS GOT entries.
922 unsigned int tls_gotno_;
923 // The offset of TLS LDM entry for this GOT.
924 unsigned int tls_ldm_offset_;
925 // All symbols that have global GOT entry.
15eb1beb 926 Global_got_entry_set global_got_symbols_;
9810d34d
SS
927 // A hash table holding GOT entries.
928 Got_entry_set got_entries_;
929 // A hash table of GOT page entries.
930 Got_page_entry_set got_page_entries_;
931 // The offset of first GOT page entry for this GOT.
932 unsigned int got_page_offset_start_;
933 // The offset of next available GOT page entry for this GOT.
934 unsigned int got_page_offset_next_;
935 // A hash table that maps GOT page entry value to the GOT offset where
936 // the entry is located.
937 Got_page_offsets got_page_offsets_;
938 // In multi-GOT links, a pointer to the next GOT.
939 Mips_got_info<size, big_endian>* next_;
940 // Index of this GOT in multi-GOT links.
941 unsigned int index_;
942 // The offset of this GOT in multi-GOT links.
943 unsigned int offset_;
944};
945
946// This is a helper class used during relocation scan. It records GOT16 addend.
947
948template<int size, bool big_endian>
949struct got16_addend
950{
951 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
952
953 got16_addend(const Sized_relobj_file<size, big_endian>* _object,
954 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
955 Mips_address _addend)
956 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
957 addend(_addend)
958 { }
959
960 const Sized_relobj_file<size, big_endian>* object;
961 unsigned int shndx;
962 unsigned int r_type;
963 unsigned int r_sym;
964 Mips_address addend;
965};
966
b52717c0
VR
967// .MIPS.abiflags section content
968
969template<bool big_endian>
970struct Mips_abiflags
971{
972 typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype8;
973 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
974 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
975
976 Mips_abiflags()
977 : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
978 cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0)
979 { }
980
981 // Version of flags structure.
982 Valtype16 version;
983 // The level of the ISA: 1-5, 32, 64.
984 Valtype8 isa_level;
985 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
986 Valtype8 isa_rev;
987 // The size of general purpose registers.
988 Valtype8 gpr_size;
989 // The size of co-processor 1 registers.
990 Valtype8 cpr1_size;
991 // The size of co-processor 2 registers.
992 Valtype8 cpr2_size;
993 // The floating-point ABI.
994 Valtype8 fp_abi;
995 // Processor-specific extension.
996 Valtype32 isa_ext;
997 // Mask of ASEs used.
998 Valtype32 ases;
999 // Mask of general flags.
1000 Valtype32 flags1;
1001 Valtype32 flags2;
1002};
1003
9810d34d
SS
1004// Mips_symbol class. Holds additional symbol information needed for Mips.
1005
1006template<int size>
1007class Mips_symbol : public Sized_symbol<size>
1008{
1009 public:
1010 Mips_symbol()
1011 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
1012 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
1013 pointer_equality_needed_(false), global_got_area_(GGA_NONE),
1014 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
1015 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
1016 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
1017 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
1018 { }
1019
1020 // Return whether this is a MIPS16 symbol.
1021 bool
1022 is_mips16() const
1023 {
1024 // (st_other & STO_MIPS16) == STO_MIPS16
1025 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
1026 == elfcpp::STO_MIPS16 >> 2);
1027 }
1028
1029 // Return whether this is a microMIPS symbol.
1030 bool
1031 is_micromips() const
1032 {
1033 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
1034 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
1035 == elfcpp::STO_MICROMIPS >> 2);
1036 }
1037
1038 // Return whether the symbol needs MIPS16 fn_stub.
1039 bool
1040 need_fn_stub() const
1041 { return this->need_fn_stub_; }
1042
1043 // Set that the symbol needs MIPS16 fn_stub.
1044 void
1045 set_need_fn_stub()
1046 { this->need_fn_stub_ = true; }
1047
1048 // Return whether this symbol is referenced by branch relocations from
1049 // any non-PIC input file.
1050 bool
1051 has_nonpic_branches() const
1052 { return this->has_nonpic_branches_; }
1053
1054 // Set that this symbol is referenced by branch relocations from
1055 // any non-PIC input file.
1056 void
1057 set_has_nonpic_branches()
1058 { this->has_nonpic_branches_ = true; }
1059
1060 // Return the offset of the la25 stub for this symbol from the start of the
1061 // la25 stub section.
1062 unsigned int
1063 la25_stub_offset() const
1064 { return this->la25_stub_offset_; }
1065
1066 // Set the offset of the la25 stub for this symbol from the start of the
1067 // la25 stub section.
1068 void
1069 set_la25_stub_offset(unsigned int offset)
1070 { this->la25_stub_offset_ = offset; }
1071
1072 // Return whether the symbol has la25 stub. This is true if this symbol is
1073 // for a PIC function, and there are non-PIC branches and jumps to it.
1074 bool
1075 has_la25_stub() const
1076 { return this->la25_stub_offset_ != -1U; }
1077
1078 // Return whether there is a relocation against this symbol that must be
1079 // resolved by the static linker (that is, the relocation cannot possibly
1080 // be made dynamic).
1081 bool
1082 has_static_relocs() const
1083 { return this->has_static_relocs_; }
1084
1085 // Set that there is a relocation against this symbol that must be resolved
1086 // by the static linker (that is, the relocation cannot possibly be made
1087 // dynamic).
1088 void
1089 set_has_static_relocs()
1090 { this->has_static_relocs_ = true; }
1091
1092 // Return whether we must not create a lazy-binding stub for this symbol.
1093 bool
1094 no_lazy_stub() const
1095 { return this->no_lazy_stub_; }
1096
1097 // Set that we must not create a lazy-binding stub for this symbol.
1098 void
1099 set_no_lazy_stub()
1100 { this->no_lazy_stub_ = true; }
1101
1102 // Return the offset of the lazy-binding stub for this symbol from the start
1103 // of .MIPS.stubs section.
1104 unsigned int
1105 lazy_stub_offset() const
1106 { return this->lazy_stub_offset_; }
1107
1108 // Set the offset of the lazy-binding stub for this symbol from the start
1109 // of .MIPS.stubs section.
1110 void
1111 set_lazy_stub_offset(unsigned int offset)
1112 { this->lazy_stub_offset_ = offset; }
1113
1114 // Return whether there are any relocations for this symbol where
1115 // pointer equality matters.
1116 bool
1117 pointer_equality_needed() const
1118 { return this->pointer_equality_needed_; }
1119
1120 // Set that there are relocations for this symbol where pointer equality
1121 // matters.
1122 void
1123 set_pointer_equality_needed()
1124 { this->pointer_equality_needed_ = true; }
1125
1126 // Return global GOT area where this symbol in located.
1127 Global_got_area
1128 global_got_area() const
1129 { return this->global_got_area_; }
1130
1131 // Set global GOT area where this symbol in located.
1132 void
1133 set_global_got_area(Global_got_area global_got_area)
1134 { this->global_got_area_ = global_got_area; }
1135
1136 // Return the global GOT offset for this symbol. For multi-GOT links, this
1137 // returns the offset from the start of .got section to the first GOT entry
1138 // for the symbol. Note that in multi-GOT links the symbol can have entry
1139 // in more than one GOT.
1140 unsigned int
1141 global_gotoffset() const
1142 { return this->global_gotoffset_; }
1143
1144 // Set the global GOT offset for this symbol. Note that in multi-GOT links
1145 // the symbol can have entry in more than one GOT. This method will set
1146 // the offset only if it is less than current offset.
1147 void
1148 set_global_gotoffset(unsigned int offset)
1149 {
1150 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
1151 this->global_gotoffset_ = offset;
1152 }
1153
1154 // Return whether all GOT relocations for this symbol are for calls.
1155 bool
1156 got_only_for_calls() const
1157 { return this->got_only_for_calls_; }
1158
1159 // Set that there is a GOT relocation for this symbol that is not for call.
1160 void
1161 set_got_not_only_for_calls()
1162 { this->got_only_for_calls_ = false; }
1163
1164 // Return whether this is a PIC symbol.
1165 bool
1166 is_pic() const
1167 {
1168 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
1169 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
1170 == (elfcpp::STO_MIPS_PIC >> 2));
1171 }
1172
1173 // Set the flag in st_other field that marks this symbol as PIC.
1174 void
1175 set_pic()
1176 {
1177 if (this->is_mips16())
1178 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
1179 this->set_nonvis((this->nonvis()
1180 & ~((elfcpp::STO_MIPS16 >> 2)
1181 | (elfcpp::STO_MIPS_FLAGS >> 2)))
1182 | (elfcpp::STO_MIPS_PIC >> 2));
1183 else
1184 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
1185 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1186 | (elfcpp::STO_MIPS_PIC >> 2));
1187 }
1188
1189 // Set the flag in st_other field that marks this symbol as PLT.
1190 void
1191 set_mips_plt()
1192 {
1193 if (this->is_mips16())
1194 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
1195 this->set_nonvis((this->nonvis()
1196 & ((elfcpp::STO_MIPS16 >> 2)
1197 | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
1198 | (elfcpp::STO_MIPS_PLT >> 2));
1199
1200 else
1201 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
1202 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
1203 | (elfcpp::STO_MIPS_PLT >> 2));
1204 }
1205
1206 // Downcast a base pointer to a Mips_symbol pointer.
1207 static Mips_symbol<size>*
1208 as_mips_sym(Symbol* sym)
1209 { return static_cast<Mips_symbol<size>*>(sym); }
1210
1211 // Downcast a base pointer to a Mips_symbol pointer.
1212 static const Mips_symbol<size>*
1213 as_mips_sym(const Symbol* sym)
1214 { return static_cast<const Mips_symbol<size>*>(sym); }
1215
1216 // Return whether the symbol has lazy-binding stub.
1217 bool
1218 has_lazy_stub() const
1219 { return this->has_lazy_stub_; }
1220
1221 // Set whether the symbol has lazy-binding stub.
1222 void
1223 set_has_lazy_stub(bool has_lazy_stub)
1224 { this->has_lazy_stub_ = has_lazy_stub; }
1225
1226 // Return whether the symbol needs a standard PLT entry.
1227 bool
1228 needs_mips_plt() const
1229 { return this->needs_mips_plt_; }
1230
1231 // Set whether the symbol needs a standard PLT entry.
1232 void
1233 set_needs_mips_plt(bool needs_mips_plt)
1234 { this->needs_mips_plt_ = needs_mips_plt; }
1235
1236 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
1237 // entry.
1238 bool
1239 needs_comp_plt() const
1240 { return this->needs_comp_plt_; }
1241
1242 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1243 void
1244 set_needs_comp_plt(bool needs_comp_plt)
1245 { this->needs_comp_plt_ = needs_comp_plt; }
1246
1247 // Return standard PLT entry offset, or -1 if none.
1248 unsigned int
1249 mips_plt_offset() const
1250 { return this->mips_plt_offset_; }
1251
1252 // Set standard PLT entry offset.
1253 void
1254 set_mips_plt_offset(unsigned int mips_plt_offset)
1255 { this->mips_plt_offset_ = mips_plt_offset; }
1256
1257 // Return whether the symbol has standard PLT entry.
1258 bool
1259 has_mips_plt_offset() const
1260 { return this->mips_plt_offset_ != -1U; }
1261
1262 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1263 unsigned int
1264 comp_plt_offset() const
1265 { return this->comp_plt_offset_; }
1266
1267 // Set compressed (MIPS16 or microMIPS) PLT entry offset.
1268 void
1269 set_comp_plt_offset(unsigned int comp_plt_offset)
1270 { this->comp_plt_offset_ = comp_plt_offset; }
1271
1272 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
1273 bool
1274 has_comp_plt_offset() const
1275 { return this->comp_plt_offset_ != -1U; }
1276
1277 // Return MIPS16 fn stub for a symbol.
1278 template<bool big_endian>
1279 Mips16_stub_section<size, big_endian>*
1280 get_mips16_fn_stub() const
1281 {
1282 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
1283 }
1284
1285 // Set MIPS16 fn stub for a symbol.
1286 void
1287 set_mips16_fn_stub(Mips16_stub_section_base* stub)
1288 { this->mips16_fn_stub_ = stub; }
1289
1290 // Return whether symbol has MIPS16 fn stub.
1291 bool
1292 has_mips16_fn_stub() const
1293 { return this->mips16_fn_stub_ != NULL; }
1294
1295 // Return MIPS16 call stub for a symbol.
1296 template<bool big_endian>
1297 Mips16_stub_section<size, big_endian>*
1298 get_mips16_call_stub() const
1299 {
1300 return static_cast<Mips16_stub_section<size, big_endian>*>(
1301 mips16_call_stub_);
1302 }
1303
1304 // Set MIPS16 call stub for a symbol.
1305 void
1306 set_mips16_call_stub(Mips16_stub_section_base* stub)
1307 { this->mips16_call_stub_ = stub; }
1308
1309 // Return whether symbol has MIPS16 call stub.
1310 bool
1311 has_mips16_call_stub() const
1312 { return this->mips16_call_stub_ != NULL; }
1313
1314 // Return MIPS16 call_fp stub for a symbol.
1315 template<bool big_endian>
1316 Mips16_stub_section<size, big_endian>*
1317 get_mips16_call_fp_stub() const
1318 {
1319 return static_cast<Mips16_stub_section<size, big_endian>*>(
1320 mips16_call_fp_stub_);
1321 }
1322
1323 // Set MIPS16 call_fp stub for a symbol.
1324 void
1325 set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
1326 { this->mips16_call_fp_stub_ = stub; }
1327
1328 // Return whether symbol has MIPS16 call_fp stub.
1329 bool
1330 has_mips16_call_fp_stub() const
1331 { return this->mips16_call_fp_stub_ != NULL; }
1332
1333 bool
1334 get_applied_secondary_got_fixup() const
1335 { return applied_secondary_got_fixup_; }
1336
1337 void
1338 set_applied_secondary_got_fixup()
1339 { this->applied_secondary_got_fixup_ = true; }
1340
15eb1beb
VR
1341 // Return the hash of this symbol.
1342 size_t
1343 hash() const
1344 {
1345 return gold::string_hash<char>(this->name());
1346 }
1347
9810d34d
SS
1348 private:
1349 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
1350 // appears in any relocs other than a 16 bit call.
1351 bool need_fn_stub_;
1352
1353 // True if this symbol is referenced by branch relocations from
1354 // any non-PIC input file. This is used to determine whether an
1355 // la25 stub is required.
1356 bool has_nonpic_branches_;
1357
1358 // The offset of the la25 stub for this symbol from the start of the
1359 // la25 stub section.
1360 unsigned int la25_stub_offset_;
1361
1362 // True if there is a relocation against this symbol that must be
1363 // resolved by the static linker (that is, the relocation cannot
1364 // possibly be made dynamic).
1365 bool has_static_relocs_;
1366
1367 // Whether we must not create a lazy-binding stub for this symbol.
1368 // This is true if the symbol has relocations related to taking the
1369 // function's address.
1370 bool no_lazy_stub_;
1371
1372 // The offset of the lazy-binding stub for this symbol from the start of
1373 // .MIPS.stubs section.
1374 unsigned int lazy_stub_offset_;
1375
1376 // True if there are any relocations for this symbol where pointer equality
1377 // matters.
1378 bool pointer_equality_needed_;
1379
1380 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
1381 // in the global part of the GOT.
1382 Global_got_area global_got_area_;
1383
1384 // The global GOT offset for this symbol. For multi-GOT links, this is offset
1385 // from the start of .got section to the first GOT entry for the symbol.
1386 // Note that in multi-GOT links the symbol can have entry in more than one GOT.
1387 unsigned int global_gotoffset_;
1388
1389 // Whether all GOT relocations for this symbol are for calls.
1390 bool got_only_for_calls_;
1391 // Whether the symbol has lazy-binding stub.
1392 bool has_lazy_stub_;
1393 // Whether the symbol needs a standard PLT entry.
1394 bool needs_mips_plt_;
1395 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
1396 bool needs_comp_plt_;
1397 // Standard PLT entry offset, or -1 if none.
1398 unsigned int mips_plt_offset_;
1399 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
1400 unsigned int comp_plt_offset_;
1401 // MIPS16 fn stub for a symbol.
1402 Mips16_stub_section_base* mips16_fn_stub_;
1403 // MIPS16 call stub for a symbol.
1404 Mips16_stub_section_base* mips16_call_stub_;
1405 // MIPS16 call_fp stub for a symbol.
1406 Mips16_stub_section_base* mips16_call_fp_stub_;
1407
1408 bool applied_secondary_got_fixup_;
1409};
1410
1411// Mips16_stub_section class.
1412
1413// The mips16 compiler uses a couple of special sections to handle
1414// floating point arguments.
1415
1416// Section names that look like .mips16.fn.FNNAME contain stubs that
1417// copy floating point arguments from the fp regs to the gp regs and
1418// then jump to FNNAME. If any 32 bit function calls FNNAME, the
1419// call should be redirected to the stub instead. If no 32 bit
1420// function calls FNNAME, the stub should be discarded. We need to
1421// consider any reference to the function, not just a call, because
1422// if the address of the function is taken we will need the stub,
1423// since the address might be passed to a 32 bit function.
1424
1425// Section names that look like .mips16.call.FNNAME contain stubs
1426// that copy floating point arguments from the gp regs to the fp
1427// regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1428// then any 16 bit function that calls FNNAME should be redirected
1429// to the stub instead. If FNNAME is not a 32 bit function, the
1430// stub should be discarded.
1431
1432// .mips16.call.fp.FNNAME sections are similar, but contain stubs
1433// which call FNNAME and then copy the return value from the fp regs
1434// to the gp regs. These stubs store the return address in $18 while
1435// calling FNNAME; any function which might call one of these stubs
1436// must arrange to save $18 around the call. (This case is not
1437// needed for 32 bit functions that call 16 bit functions, because
1438// 16 bit functions always return floating point values in both
1439// $f0/$f1 and $2/$3.)
1440
1441// Note that in all cases FNNAME might be defined statically.
1442// Therefore, FNNAME is not used literally. Instead, the relocation
1443// information will indicate which symbol the section is for.
1444
1445// We record any stubs that we find in the symbol table.
1446
1447// TODO(sasa): All mips16 stub sections should be emitted in the .text section.
1448
1449class Mips16_stub_section_base { };
1450
1451template<int size, bool big_endian>
1452class Mips16_stub_section : public Mips16_stub_section_base
1453{
1454 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1455
1456 public:
1457 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
1458 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
1459 found_r_mips_none_(false)
1460 {
1461 gold_assert(object->is_mips16_fn_stub_section(shndx)
1462 || object->is_mips16_call_stub_section(shndx)
1463 || object->is_mips16_call_fp_stub_section(shndx));
1464 }
1465
1466 // Return the object of this stub section.
1467 Mips_relobj<size, big_endian>*
1468 object() const
1469 { return this->object_; }
1470
1471 // Return the size of a section.
1472 uint64_t
1473 section_size() const
1474 { return this->object_->section_size(this->shndx_); }
1475
1476 // Return section index of this stub section.
1477 unsigned int
1478 shndx() const
1479 { return this->shndx_; }
1480
1481 // Return symbol index, if stub is for a local function.
1482 unsigned int
1483 r_sym() const
1484 { return this->r_sym_; }
1485
1486 // Return symbol, if stub is for a global function.
1487 Mips_symbol<size>*
1488 gsym() const
1489 { return this->gsym_; }
1490
1491 // Return whether stub is for a local function.
1492 bool
1493 is_for_local_function() const
1494 { return this->gsym_ == NULL; }
1495
1496 // This method is called when a new relocation R_TYPE for local symbol R_SYM
1497 // is found in the stub section. Try to find stub target.
1498 void
1499 new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
1500 {
1501 // To find target symbol for this stub, trust the first R_MIPS_NONE
1502 // relocation, if any. Otherwise trust the first relocation, whatever
1503 // its kind.
1504 if (this->found_r_mips_none_)
1505 return;
1506 if (r_type == elfcpp::R_MIPS_NONE)
1507 {
1508 this->r_sym_ = r_sym;
1509 this->gsym_ = NULL;
1510 this->found_r_mips_none_ = true;
1511 }
1512 else if (!is_target_found())
1513 this->r_sym_ = r_sym;
1514 }
1515
1516 // This method is called when a new relocation R_TYPE for global symbol GSYM
1517 // is found in the stub section. Try to find stub target.
1518 void
1519 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
1520 {
1521 // To find target symbol for this stub, trust the first R_MIPS_NONE
1522 // relocation, if any. Otherwise trust the first relocation, whatever
1523 // its kind.
1524 if (this->found_r_mips_none_)
1525 return;
1526 if (r_type == elfcpp::R_MIPS_NONE)
1527 {
1528 this->gsym_ = gsym;
1529 this->r_sym_ = 0;
1530 this->found_r_mips_none_ = true;
1531 }
1532 else if (!is_target_found())
1533 this->gsym_ = gsym;
1534 }
1535
1536 // Return whether we found the stub target.
1537 bool
1538 is_target_found() const
1539 { return this->r_sym_ != 0 || this->gsym_ != NULL; }
1540
1541 // Return whether this is a fn stub.
1542 bool
1543 is_fn_stub() const
1544 { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
1545
1546 // Return whether this is a call stub.
1547 bool
1548 is_call_stub() const
1549 { return this->object_->is_mips16_call_stub_section(this->shndx_); }
1550
1551 // Return whether this is a call_fp stub.
1552 bool
1553 is_call_fp_stub() const
1554 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
1555
1556 // Return the output address.
1557 Mips_address
1558 output_address() const
1559 {
1560 return (this->object_->output_section(this->shndx_)->address()
1561 + this->object_->output_section_offset(this->shndx_));
1562 }
1563
1564 private:
1565 // The object of this stub section.
1566 Mips_relobj<size, big_endian>* object_;
1567 // The section index of this stub section.
1568 unsigned int shndx_;
1569 // The symbol index, if stub is for a local function.
1570 unsigned int r_sym_;
1571 // The symbol, if stub is for a global function.
1572 Mips_symbol<size>* gsym_;
1573 // True if we found R_MIPS_NONE relocation in this stub.
1574 bool found_r_mips_none_;
1575};
1576
1577// Mips_relobj class.
1578
1579template<int size, bool big_endian>
1580class Mips_relobj : public Sized_relobj_file<size, big_endian>
1581{
1582 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1583 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
1584 Mips16_stubs_int_map;
1585 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1586
1587 public:
1588 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
1589 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1590 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1591 processor_specific_flags_(0), local_symbol_is_mips16_(),
1592 local_symbol_is_micromips_(), mips16_stub_sections_(),
1593 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
82e49872 1594 local_mips16_call_stubs_(), gp_(0), has_reginfo_section_(false),
4d78db49
VR
1595 merge_processor_specific_data_(true), got_info_(NULL),
1596 section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
1597 section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U),
1598 attributes_section_data_(NULL), abiflags_(NULL), gprmask_(0),
1599 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0)
9810d34d
SS
1600 {
1601 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
1602 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
9810d34d
SS
1603 }
1604
1605 ~Mips_relobj()
b52717c0 1606 { delete this->attributes_section_data_; }
9810d34d
SS
1607
1608 // Downcast a base pointer to a Mips_relobj pointer. This is
1609 // not type-safe but we only use Mips_relobj not the base class.
1610 static Mips_relobj<size, big_endian>*
1611 as_mips_relobj(Relobj* relobj)
1612 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
1613
1614 // Downcast a base pointer to a Mips_relobj pointer. This is
1615 // not type-safe but we only use Mips_relobj not the base class.
1616 static const Mips_relobj<size, big_endian>*
1617 as_mips_relobj(const Relobj* relobj)
1618 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
1619
1620 // Processor-specific flags in ELF file header. This is valid only after
1621 // reading symbols.
1622 elfcpp::Elf_Word
1623 processor_specific_flags() const
1624 { return this->processor_specific_flags_; }
1625
1626 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
1627 // index. This is only valid after do_count_local_symbol is called.
1628 bool
1629 local_symbol_is_mips16(unsigned int r_sym) const
1630 {
1631 gold_assert(r_sym < this->local_symbol_is_mips16_.size());
1632 return this->local_symbol_is_mips16_[r_sym];
1633 }
1634
1635 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
1636 // index. This is only valid after do_count_local_symbol is called.
1637 bool
1638 local_symbol_is_micromips(unsigned int r_sym) const
1639 {
1640 gold_assert(r_sym < this->local_symbol_is_micromips_.size());
1641 return this->local_symbol_is_micromips_[r_sym];
1642 }
1643
1644 // Get or create MIPS16 stub section.
1645 Mips16_stub_section<size, big_endian>*
1646 get_mips16_stub_section(unsigned int shndx)
1647 {
1648 typename Mips16_stubs_int_map::const_iterator it =
1649 this->mips16_stub_sections_.find(shndx);
1650 if (it != this->mips16_stub_sections_.end())
1651 return (*it).second;
1652
1653 Mips16_stub_section<size, big_endian>* stub_section =
1654 new Mips16_stub_section<size, big_endian>(this, shndx);
1655 this->mips16_stub_sections_.insert(
1656 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1657 stub_section->shndx(), stub_section));
1658 return stub_section;
1659 }
1660
1661 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
1662 // object doesn't have fn stub for R_SYM.
1663 Mips16_stub_section<size, big_endian>*
1664 get_local_mips16_fn_stub(unsigned int r_sym) const
1665 {
1666 typename Mips16_stubs_int_map::const_iterator it =
1667 this->local_mips16_fn_stubs_.find(r_sym);
1668 if (it != this->local_mips16_fn_stubs_.end())
1669 return (*it).second;
1670 return NULL;
1671 }
1672
1673 // Record that this object has MIPS16 fn stub for local symbol. This method
1674 // is only called if we decided not to discard the stub.
1675 void
1676 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
1677 {
1678 gold_assert(stub->is_for_local_function());
1679 unsigned int r_sym = stub->r_sym();
1680 this->local_mips16_fn_stubs_.insert(
1681 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1682 r_sym, stub));
1683 }
1684
1685 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
1686 // object doesn't have call stub for R_SYM.
1687 Mips16_stub_section<size, big_endian>*
1688 get_local_mips16_call_stub(unsigned int r_sym) const
1689 {
1690 typename Mips16_stubs_int_map::const_iterator it =
1691 this->local_mips16_call_stubs_.find(r_sym);
1692 if (it != this->local_mips16_call_stubs_.end())
1693 return (*it).second;
1694 return NULL;
1695 }
1696
1697 // Record that this object has MIPS16 call stub for local symbol. This method
1698 // is only called if we decided not to discard the stub.
1699 void
1700 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
1701 {
1702 gold_assert(stub->is_for_local_function());
1703 unsigned int r_sym = stub->r_sym();
1704 this->local_mips16_call_stubs_.insert(
1705 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
1706 r_sym, stub));
1707 }
1708
1709 // Record that we found "non 16-bit" call relocation against local symbol
1710 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
1711 // is one.
1712 void
1713 add_local_non_16bit_call(unsigned int symndx)
1714 { this->local_non_16bit_calls_.insert(symndx); }
1715
1716 // Return true if there is any "non 16-bit" call relocation against local
1717 // symbol SYMNDX in this object.
1718 bool
1719 has_local_non_16bit_call_relocs(unsigned int symndx)
1720 {
1721 return (this->local_non_16bit_calls_.find(symndx)
1722 != this->local_non_16bit_calls_.end());
1723 }
1724
1725 // Record that we found 16-bit call relocation R_MIPS16_26 against local
1726 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
1727 // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
1728 void
1729 add_local_16bit_call(unsigned int symndx)
1730 { this->local_16bit_calls_.insert(symndx); }
1731
1732 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
1733 // symbol SYMNDX in this object.
1734 bool
1735 has_local_16bit_call_relocs(unsigned int symndx)
1736 {
1737 return (this->local_16bit_calls_.find(symndx)
1738 != this->local_16bit_calls_.end());
1739 }
1740
1741 // Get gp value that was used to create this object.
1742 Mips_address
1743 gp_value() const
1744 { return this->gp_; }
1745
1746 // Return whether the object is a PIC object.
1747 bool
1748 is_pic() const
1749 { return this->is_pic_; }
1750
1751 // Return whether the object uses N32 ABI.
1752 bool
1753 is_n32() const
1754 { return this->is_n32_; }
1755
1756 // Return whether the object uses N64 ABI.
1757 bool
1758 is_n64() const
01b84e25 1759 { return size == 64; }
9810d34d
SS
1760
1761 // Return whether the object uses NewABI conventions.
1762 bool
1763 is_newabi() const
01b84e25 1764 { return this->is_n32() || this->is_n64(); }
9810d34d
SS
1765
1766 // Return Mips_got_info for this object.
1767 Mips_got_info<size, big_endian>*
1768 get_got_info() const
1769 { return this->got_info_; }
1770
1771 // Return Mips_got_info for this object. Create new info if it doesn't exist.
1772 Mips_got_info<size, big_endian>*
1773 get_or_create_got_info()
1774 {
1775 if (!this->got_info_)
1776 this->got_info_ = new Mips_got_info<size, big_endian>();
1777 return this->got_info_;
1778 }
1779
1780 // Set Mips_got_info for this object.
1781 void
1782 set_got_info(Mips_got_info<size, big_endian>* got_info)
1783 { this->got_info_ = got_info; }
1784
1785 // Whether a section SHDNX is a MIPS16 stub section. This is only valid
1786 // after do_read_symbols is called.
1787 bool
1788 is_mips16_stub_section(unsigned int shndx)
1789 {
1790 return (is_mips16_fn_stub_section(shndx)
1791 || is_mips16_call_stub_section(shndx)
1792 || is_mips16_call_fp_stub_section(shndx));
1793 }
1794
1795 // Return TRUE if relocations in section SHNDX can refer directly to a
1796 // MIPS16 function rather than to a hard-float stub. This is only valid
1797 // after do_read_symbols is called.
1798 bool
1799 section_allows_mips16_refs(unsigned int shndx)
1800 {
1801 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
1802 }
1803
1804 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
1805 // after do_read_symbols is called.
1806 bool
1807 is_mips16_fn_stub_section(unsigned int shndx)
1808 {
1809 gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
1810 return this->section_is_mips16_fn_stub_[shndx];
1811 }
1812
1813 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
1814 // after do_read_symbols is called.
1815 bool
1816 is_mips16_call_stub_section(unsigned int shndx)
1817 {
1818 gold_assert(shndx < this->section_is_mips16_call_stub_.size());
1819 return this->section_is_mips16_call_stub_[shndx];
1820 }
1821
1822 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
1823 // valid after do_read_symbols is called.
1824 bool
1825 is_mips16_call_fp_stub_section(unsigned int shndx)
1826 {
1827 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
1828 return this->section_is_mips16_call_fp_stub_[shndx];
1829 }
1830
1831 // Discard MIPS16 stub secions that are not needed.
1832 void
1833 discard_mips16_stub_sections(Symbol_table* symtab);
1834
82e49872
VR
1835 // Return whether there is a .reginfo section.
1836 bool
1837 has_reginfo_section() const
1838 { return this->has_reginfo_section_; }
1839
4d78db49
VR
1840 // Return whether we want to merge processor-specific data.
1841 bool
1842 merge_processor_specific_data() const
1843 { return this->merge_processor_specific_data_; }
1844
9810d34d
SS
1845 // Return gprmask from the .reginfo section of this object.
1846 Valtype
1847 gprmask() const
1848 { return this->gprmask_; }
1849
1850 // Return cprmask1 from the .reginfo section of this object.
1851 Valtype
1852 cprmask1() const
1853 { return this->cprmask1_; }
1854
1855 // Return cprmask2 from the .reginfo section of this object.
1856 Valtype
1857 cprmask2() const
1858 { return this->cprmask2_; }
1859
1860 // Return cprmask3 from the .reginfo section of this object.
1861 Valtype
1862 cprmask3() const
1863 { return this->cprmask3_; }
1864
1865 // Return cprmask4 from the .reginfo section of this object.
1866 Valtype
1867 cprmask4() const
1868 { return this->cprmask4_; }
1869
b52717c0
VR
1870 // This is the contents of the .MIPS.abiflags section if there is one.
1871 Mips_abiflags<big_endian>*
1872 abiflags()
1873 { return this->abiflags_; }
1874
1875 // This is the contents of the .gnu.attribute section if there is one.
1876 const Attributes_section_data*
1877 attributes_section_data() const
1878 { return this->attributes_section_data_; }
1879
9810d34d
SS
1880 protected:
1881 // Count the local symbols.
1882 void
1883 do_count_local_symbols(Stringpool_template<char>*,
1884 Stringpool_template<char>*);
1885
1886 // Read the symbol information.
1887 void
1888 do_read_symbols(Read_symbols_data* sd);
1889
1890 private:
47a9f4fc
VR
1891 // The name of the options section.
1892 const char* mips_elf_options_section_name()
1893 { return this->is_newabi() ? ".MIPS.options" : ".options"; }
1894
9810d34d
SS
1895 // processor-specific flags in ELF file header.
1896 elfcpp::Elf_Word processor_specific_flags_;
1897
1898 // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
1899 // This is only valid after do_count_local_symbol is called.
1900 std::vector<bool> local_symbol_is_mips16_;
1901
1902 // Bit vector to tell if a local symbol is a microMIPS symbol or not.
1903 // This is only valid after do_count_local_symbol is called.
1904 std::vector<bool> local_symbol_is_micromips_;
1905
1906 // Map from section index to the MIPS16 stub for that section. This contains
1907 // all stubs found in this object.
1908 Mips16_stubs_int_map mips16_stub_sections_;
1909
1910 // Local symbols that have "non 16-bit" call relocation. This relocation
1911 // would need to refer to a MIPS16 fn stub, if there is one.
1912 std::set<unsigned int> local_non_16bit_calls_;
1913
1914 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
1915 // call or call_fp stubs will only be needed if there is some R_MIPS16_26
1916 // relocation that refers to the stub symbol.
1917 std::set<unsigned int> local_16bit_calls_;
1918
1919 // Map from local symbol index to the MIPS16 fn stub for that symbol.
1920 // This contains only the stubs that we decided not to discard.
1921 Mips16_stubs_int_map local_mips16_fn_stubs_;
1922
1923 // Map from local symbol index to the MIPS16 call stub for that symbol.
1924 // This contains only the stubs that we decided not to discard.
1925 Mips16_stubs_int_map local_mips16_call_stubs_;
1926
1927 // gp value that was used to create this object.
1928 Mips_address gp_;
1929 // Whether the object is a PIC object.
1930 bool is_pic_ : 1;
1931 // Whether the object uses N32 ABI.
1932 bool is_n32_ : 1;
82e49872
VR
1933 // Whether the object contains a .reginfo section.
1934 bool has_reginfo_section_ : 1;
4d78db49
VR
1935 // Whether we merge processor-specific data of this object to output.
1936 bool merge_processor_specific_data_ : 1;
9810d34d
SS
1937 // The Mips_got_info for this object.
1938 Mips_got_info<size, big_endian>* got_info_;
1939
1940 // Bit vector to tell if a section is a MIPS16 fn stub section or not.
1941 // This is only valid after do_read_symbols is called.
1942 std::vector<bool> section_is_mips16_fn_stub_;
1943
1944 // Bit vector to tell if a section is a MIPS16 call stub section or not.
1945 // This is only valid after do_read_symbols is called.
1946 std::vector<bool> section_is_mips16_call_stub_;
1947
1948 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
1949 // This is only valid after do_read_symbols is called.
1950 std::vector<bool> section_is_mips16_call_fp_stub_;
1951
1952 // .pdr section index.
1953 unsigned int pdr_shndx_;
1954
b52717c0
VR
1955 // Object attributes if there is a .gnu.attributes section or NULL.
1956 Attributes_section_data* attributes_section_data_;
1957
1958 // Object abiflags if there is a .MIPS.abiflags section or NULL.
1959 Mips_abiflags<big_endian>* abiflags_;
1960
9810d34d
SS
1961 // gprmask from the .reginfo section of this object.
1962 Valtype gprmask_;
1963 // cprmask1 from the .reginfo section of this object.
1964 Valtype cprmask1_;
1965 // cprmask2 from the .reginfo section of this object.
1966 Valtype cprmask2_;
1967 // cprmask3 from the .reginfo section of this object.
1968 Valtype cprmask3_;
1969 // cprmask4 from the .reginfo section of this object.
1970 Valtype cprmask4_;
1971};
1972
1973// Mips_output_data_got class.
1974
1975template<int size, bool big_endian>
1976class Mips_output_data_got : public Output_data_got<size, big_endian>
1977{
1978 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
1979 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
1980 Reloc_section;
1981 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1982
1983 public:
1984 Mips_output_data_got(Target_mips<size, big_endian>* target,
1985 Symbol_table* symtab, Layout* layout)
1986 : Output_data_got<size, big_endian>(), target_(target),
1987 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
1988 first_global_got_dynsym_index_(-1U), primary_got_(NULL),
1989 secondary_got_relocs_()
1990 {
1991 this->master_got_info_ = new Mips_got_info<size, big_endian>();
1992 this->set_addralign(16);
1993 }
1994
1995 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
1996 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
1997 void
1998 record_local_got_symbol(Mips_relobj<size, big_endian>* object,
1999 unsigned int symndx, Mips_address addend,
47a9f4fc
VR
2000 unsigned int r_type, unsigned int shndx,
2001 bool is_section_symbol)
9810d34d
SS
2002 {
2003 this->master_got_info_->record_local_got_symbol(object, symndx, addend,
47a9f4fc
VR
2004 r_type, shndx,
2005 is_section_symbol);
9810d34d
SS
2006 }
2007
2008 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
2009 // in OBJECT. FOR_CALL is true if the caller is only interested in
2010 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
2011 // relocation.
2012 void
2013 record_global_got_symbol(Mips_symbol<size>* mips_sym,
2014 Mips_relobj<size, big_endian>* object,
2015 unsigned int r_type, bool dyn_reloc, bool for_call)
2016 {
2017 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
2018 dyn_reloc, for_call);
2019 }
2020
2021 // Record that OBJECT has a page relocation against symbol SYMNDX and
2022 // that ADDEND is the addend for that relocation.
2023 void
2024 record_got_page_entry(Mips_relobj<size, big_endian>* object,
2025 unsigned int symndx, int addend)
2026 { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
2027
2028 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
2029 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
2030 // applied in a static link.
2031 void
2032 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2033 Mips_symbol<size>* gsym)
2034 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
2035
2036 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
2037 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
2038 // relocation that needs to be applied in a static link.
2039 void
2040 add_static_reloc(unsigned int got_offset, unsigned int r_type,
2041 Sized_relobj_file<size, big_endian>* relobj,
2042 unsigned int index)
2043 {
2044 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
2045 index));
2046 }
2047
2048 // Record that global symbol GSYM has R_TYPE dynamic relocation in the
2049 // secondary GOT at OFFSET.
2050 void
2051 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
2052 Mips_symbol<size>* gsym)
2053 {
2054 this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
2055 r_type, gsym));
2056 }
2057
2058 // Update GOT entry at OFFSET with VALUE.
2059 void
2060 update_got_entry(unsigned int offset, Mips_address value)
2061 {
2062 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
2063 }
2064
2065 // Return the number of entries in local part of the GOT. This includes
2066 // local entries, page entries and 2 reserved entries.
2067 unsigned int
2068 get_local_gotno() const
2069 {
2070 if (!this->multi_got())
2071 {
2072 return (2 + this->master_got_info_->local_gotno()
2073 + this->master_got_info_->page_gotno());
2074 }
2075 else
2076 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
2077 }
2078
2079 // Return dynamic symbol table index of the first symbol with global GOT
2080 // entry.
2081 unsigned int
2082 first_global_got_dynsym_index() const
2083 { return this->first_global_got_dynsym_index_; }
2084
2085 // Set dynamic symbol table index of the first symbol with global GOT entry.
2086 void
2087 set_first_global_got_dynsym_index(unsigned int index)
2088 { this->first_global_got_dynsym_index_ = index; }
2089
2090 // Lay out the GOT. Add local, global and TLS entries. If GOT is
2091 // larger than 64K, create multi-GOT.
2092 void
2093 lay_out_got(Layout* layout, Symbol_table* symtab,
2094 const Input_objects* input_objects);
2095
2096 // Create multi-GOT. For every GOT, add local, global and TLS entries.
2097 void
2098 lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
2099
2100 // Attempt to merge GOTs of different input objects.
2101 void
2102 merge_gots(const Input_objects* input_objects);
2103
2104 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
2105 // this would lead to overflow, true if they were merged successfully.
2106 bool
2107 merge_got_with(Mips_got_info<size, big_endian>* from,
2108 Mips_relobj<size, big_endian>* object,
2109 Mips_got_info<size, big_endian>* to);
2110
2111 // Return the offset of GOT page entry for VALUE. For multi-GOT links,
2112 // use OBJECT's GOT.
2113 unsigned int
2114 get_got_page_offset(Mips_address value,
2115 const Mips_relobj<size, big_endian>* object)
2116 {
2117 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2118 ? this->master_got_info_
2119 : object->get_got_info());
2120 gold_assert(g != NULL);
2121 return g->get_got_page_offset(value, this);
2122 }
2123
2124 // Return the GOT offset of type GOT_TYPE of the global symbol
2125 // GSYM. For multi-GOT links, use OBJECT's GOT.
2126 unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
2127 Mips_relobj<size, big_endian>* object) const
2128 {
2129 if (!this->multi_got())
2130 return gsym->got_offset(got_type);
2131 else
2132 {
2133 Mips_got_info<size, big_endian>* g = object->get_got_info();
2134 gold_assert(g != NULL);
2135 return gsym->got_offset(g->multigot_got_type(got_type));
2136 }
2137 }
2138
2139 // Return the GOT offset of type GOT_TYPE of the local symbol
2140 // SYMNDX.
2141 unsigned int
2142 got_offset(unsigned int symndx, unsigned int got_type,
47a9f4fc
VR
2143 Sized_relobj_file<size, big_endian>* object,
2144 uint64_t addend) const
2145 { return object->local_got_offset(symndx, got_type, addend); }
9810d34d
SS
2146
2147 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2148 unsigned int
2149 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
2150 {
2151 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2152 ? this->master_got_info_
2153 : object->get_got_info());
2154 gold_assert(g != NULL);
2155 return g->tls_ldm_offset();
2156 }
2157
2158 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
2159 void
2160 set_tls_ldm_offset(unsigned int tls_ldm_offset,
2161 Mips_relobj<size, big_endian>* object)
2162 {
2163 Mips_got_info<size, big_endian>* g = (!this->multi_got()
2164 ? this->master_got_info_
2165 : object->get_got_info());
2166 gold_assert(g != NULL);
2167 g->set_tls_ldm_offset(tls_ldm_offset);
2168 }
2169
2170 // Return true for multi-GOT links.
2171 bool
2172 multi_got() const
2173 { return this->primary_got_ != NULL; }
2174
2175 // Return the offset of OBJECT's GOT from the start of .got section.
2176 unsigned int
2177 get_got_offset(const Mips_relobj<size, big_endian>* object)
2178 {
2179 if (!this->multi_got())
2180 return 0;
2181 else
2182 {
2183 Mips_got_info<size, big_endian>* g = object->get_got_info();
2184 return g != NULL ? g->offset() : 0;
2185 }
2186 }
2187
2188 // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
2189 void
2190 add_reloc_only_entries()
2191 { this->master_got_info_->add_reloc_only_entries(this); }
2192
2193 // Return offset of the primary GOT's entry for global symbol.
2194 unsigned int
2195 get_primary_got_offset(const Mips_symbol<size>* sym) const
2196 {
2197 gold_assert(sym->global_got_area() != GGA_NONE);
2198 return (this->get_local_gotno() + sym->dynsym_index()
2199 - this->first_global_got_dynsym_index()) * size/8;
2200 }
2201
2202 // For the entry at offset GOT_OFFSET, return its offset from the gp.
2203 // Input argument GOT_OFFSET is always global offset from the start of
2204 // .got section, for both single and multi-GOT links.
2205 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
2206 // links, the return value is object_got_offset - 0x7FF0, where
2207 // object_got_offset is offset in the OBJECT's GOT.
2208 int
2209 gp_offset(unsigned int got_offset,
2210 const Mips_relobj<size, big_endian>* object) const
2211 {
2212 return (this->address() + got_offset
2213 - this->target_->adjusted_gp_value(object));
2214 }
2215
2216 protected:
2217 // Write out the GOT table.
2218 void
2219 do_write(Output_file*);
2220
2221 private:
2222
2223 // This class represent dynamic relocations that need to be applied by
2224 // gold because we are using TLS relocations in a static link.
2225 class Static_reloc
2226 {
2227 public:
2228 Static_reloc(unsigned int got_offset, unsigned int r_type,
2229 Mips_symbol<size>* gsym)
2230 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
2231 { this->u_.global.symbol = gsym; }
2232
2233 Static_reloc(unsigned int got_offset, unsigned int r_type,
2234 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
2235 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
2236 {
2237 this->u_.local.relobj = relobj;
2238 this->u_.local.index = index;
2239 }
2240
2241 // Return the GOT offset.
2242 unsigned int
2243 got_offset() const
2244 { return this->got_offset_; }
2245
2246 // Relocation type.
2247 unsigned int
2248 r_type() const
2249 { return this->r_type_; }
2250
2251 // Whether the symbol is global or not.
2252 bool
2253 symbol_is_global() const
2254 { return this->symbol_is_global_; }
2255
2256 // For a relocation against a global symbol, the global symbol.
2257 Mips_symbol<size>*
2258 symbol() const
2259 {
2260 gold_assert(this->symbol_is_global_);
2261 return this->u_.global.symbol;
2262 }
2263
2264 // For a relocation against a local symbol, the defining object.
2265 Sized_relobj_file<size, big_endian>*
2266 relobj() const
2267 {
2268 gold_assert(!this->symbol_is_global_);
2269 return this->u_.local.relobj;
2270 }
2271
2272 // For a relocation against a local symbol, the local symbol index.
2273 unsigned int
2274 index() const
2275 {
2276 gold_assert(!this->symbol_is_global_);
2277 return this->u_.local.index;
2278 }
2279
2280 private:
2281 // GOT offset of the entry to which this relocation is applied.
2282 unsigned int got_offset_;
2283 // Type of relocation.
2284 unsigned int r_type_;
2285 // Whether this relocation is against a global symbol.
2286 bool symbol_is_global_;
2287 // A global or local symbol.
2288 union
2289 {
2290 struct
2291 {
2292 // For a global symbol, the symbol itself.
2293 Mips_symbol<size>* symbol;
2294 } global;
2295 struct
2296 {
2297 // For a local symbol, the object defining object.
2298 Sized_relobj_file<size, big_endian>* relobj;
2299 // For a local symbol, the symbol index.
2300 unsigned int index;
2301 } local;
2302 } u_;
2303 };
2304
2305 // The target.
2306 Target_mips<size, big_endian>* target_;
2307 // The symbol table.
2308 Symbol_table* symbol_table_;
2309 // The layout.
2310 Layout* layout_;
2311 // Static relocs to be applied to the GOT.
2312 std::vector<Static_reloc> static_relocs_;
2313 // .got section view.
2314 unsigned char* got_view_;
2315 // The dynamic symbol table index of the first symbol with global GOT entry.
2316 unsigned int first_global_got_dynsym_index_;
2317 // The master GOT information.
2318 Mips_got_info<size, big_endian>* master_got_info_;
2319 // The primary GOT information.
2320 Mips_got_info<size, big_endian>* primary_got_;
2321 // Secondary GOT fixups.
2322 std::vector<Static_reloc> secondary_got_relocs_;
2323};
2324
2325// A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
2326// two ways of creating these interfaces. The first is to add:
2327//
2328// lui $25,%hi(func)
2329// j func
2330// addiu $25,$25,%lo(func)
2331//
2332// to a separate trampoline section. The second is to add:
2333//
2334// lui $25,%hi(func)
2335// addiu $25,$25,%lo(func)
2336//
2337// immediately before a PIC function "func", but only if a function is at the
2338// beginning of the section, and the section is not too heavily aligned (i.e we
2339// would need to add no more than 2 nops before the stub.)
2340//
2341// We only create stubs of the first type.
2342
2343template<int size, bool big_endian>
2344class Mips_output_data_la25_stub : public Output_section_data
2345{
2346 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2347
2348 public:
2349 Mips_output_data_la25_stub()
2350 : Output_section_data(size == 32 ? 4 : 8), symbols_()
2351 { }
2352
2353 // Create LA25 stub for a symbol.
2354 void
2355 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
2356 Mips_symbol<size>* gsym);
2357
2358 // Return output address of a stub.
2359 Mips_address
2360 stub_address(const Mips_symbol<size>* sym) const
2361 {
2362 gold_assert(sym->has_la25_stub());
2363 return this->address() + sym->la25_stub_offset();
2364 }
2365
2366 protected:
2367 void
2368 do_adjust_output_section(Output_section* os)
2369 { os->set_entsize(0); }
2370
2371 private:
2372 // Template for standard LA25 stub.
2373 static const uint32_t la25_stub_entry[];
2374 // Template for microMIPS LA25 stub.
2375 static const uint32_t la25_stub_micromips_entry[];
2376
2377 // Set the final size.
2378 void
2379 set_final_data_size()
2380 { this->set_data_size(this->symbols_.size() * 16); }
2381
2382 // Create a symbol for SYM stub's value and size, to help make the
2383 // disassembly easier to read.
2384 void
2385 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
2386 Target_mips<size, big_endian>* target, uint64_t symsize);
2387
1a08ae21
VR
2388 // Write to a map file.
2389 void
2390 do_print_to_mapfile(Mapfile* mapfile) const
2391 { mapfile->print_output_data(this, _(".LA25.stubs")); }
2392
9810d34d
SS
2393 // Write out the LA25 stub section.
2394 void
2395 do_write(Output_file*);
2396
2397 // Symbols that have LA25 stubs.
15eb1beb 2398 std::vector<Mips_symbol<size>*> symbols_;
9810d34d
SS
2399};
2400
47a9f4fc
VR
2401// MIPS-specific relocation writer.
2402
2403template<int sh_type, bool dynamic, int size, bool big_endian>
2404struct Mips_output_reloc_writer;
2405
2406template<int sh_type, bool dynamic, bool big_endian>
2407struct Mips_output_reloc_writer<sh_type, dynamic, 32, big_endian>
2408{
2409 typedef Output_reloc<sh_type, dynamic, 32, big_endian> Output_reloc_type;
2410 typedef std::vector<Output_reloc_type> Relocs;
2411
2412 static void
2413 write(typename Relocs::const_iterator p, unsigned char* pov)
2414 { p->write(pov); }
2415};
2416
2417template<int sh_type, bool dynamic, bool big_endian>
2418struct Mips_output_reloc_writer<sh_type, dynamic, 64, big_endian>
2419{
2420 typedef Output_reloc<sh_type, dynamic, 64, big_endian> Output_reloc_type;
2421 typedef std::vector<Output_reloc_type> Relocs;
2422
2423 static void
2424 write(typename Relocs::const_iterator p, unsigned char* pov)
2425 {
2426 elfcpp::Mips64_rel_write<big_endian> orel(pov);
2427 orel.put_r_offset(p->get_address());
2428 orel.put_r_sym(p->get_symbol_index());
2429 orel.put_r_ssym(RSS_UNDEF);
2430 orel.put_r_type(p->type());
2431 if (p->type() == elfcpp::R_MIPS_REL32)
2432 orel.put_r_type2(elfcpp::R_MIPS_64);
2433 else
2434 orel.put_r_type2(elfcpp::R_MIPS_NONE);
2435 orel.put_r_type3(elfcpp::R_MIPS_NONE);
2436 }
2437};
2438
2439template<int sh_type, bool dynamic, int size, bool big_endian>
2440class Mips_output_data_reloc : public Output_data_reloc<sh_type, dynamic,
2441 size, big_endian>
2442{
2443 public:
2444 Mips_output_data_reloc(bool sort_relocs)
2445 : Output_data_reloc<sh_type, dynamic, size, big_endian>(sort_relocs)
2446 { }
2447
2448 protected:
2449 // Write out the data.
2450 void
2451 do_write(Output_file* of)
2452 {
2453 typedef Mips_output_reloc_writer<sh_type, dynamic, size,
2454 big_endian> Writer;
2455 this->template do_write_generic<Writer>(of);
2456 }
2457};
2458
2459
9810d34d
SS
2460// A class to handle the PLT data.
2461
2462template<int size, bool big_endian>
2463class Mips_output_data_plt : public Output_section_data
2464{
2465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
47a9f4fc
VR
2466 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true,
2467 size, big_endian> Reloc_section;
9810d34d
SS
2468
2469 public:
2470 // Create the PLT section. The ordinary .got section is an argument,
2471 // since we need to refer to the start.
2472 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
2473 Target_mips<size, big_endian>* target)
2474 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
2475 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
2476 target_(target)
2477 {
2478 this->rel_ = new Reloc_section(false);
2479 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2480 elfcpp::SHF_ALLOC, this->rel_,
2481 ORDER_DYNAMIC_PLT_RELOCS, false);
2482 }
2483
2484 // Add an entry to the PLT for a symbol referenced by r_type relocation.
2485 void
2486 add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
2487
2488 // Return the .rel.plt section data.
04bc2a28 2489 Reloc_section*
9810d34d
SS
2490 rel_plt() const
2491 { return this->rel_; }
2492
2493 // Return the number of PLT entries.
2494 unsigned int
2495 entry_count() const
2496 { return this->symbols_.size(); }
2497
2498 // Return the offset of the first non-reserved PLT entry.
2499 unsigned int
2500 first_plt_entry_offset() const
2501 { return sizeof(plt0_entry_o32); }
2502
2503 // Return the size of a PLT entry.
2504 unsigned int
2505 plt_entry_size() const
2506 { return sizeof(plt_entry); }
2507
2508 // Set final PLT offsets. For each symbol, determine whether standard or
2509 // compressed (MIPS16 or microMIPS) PLT entry is used.
2510 void
2511 set_plt_offsets();
2512
2513 // Return the offset of the first standard PLT entry.
2514 unsigned int
2515 first_mips_plt_offset() const
2516 { return this->plt_header_size_; }
2517
2518 // Return the offset of the first compressed PLT entry.
2519 unsigned int
2520 first_comp_plt_offset() const
2521 { return this->plt_header_size_ + this->plt_mips_offset_; }
2522
2523 // Return whether there are any standard PLT entries.
2524 bool
2525 has_standard_entries() const
2526 { return this->plt_mips_offset_ > 0; }
2527
2528 // Return the output address of standard PLT entry.
2529 Mips_address
2530 mips_entry_address(const Mips_symbol<size>* sym) const
2531 {
2532 gold_assert (sym->has_mips_plt_offset());
2533 return (this->address() + this->first_mips_plt_offset()
2534 + sym->mips_plt_offset());
2535 }
2536
2537 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
2538 Mips_address
2539 comp_entry_address(const Mips_symbol<size>* sym) const
2540 {
2541 gold_assert (sym->has_comp_plt_offset());
2542 return (this->address() + this->first_comp_plt_offset()
2543 + sym->comp_plt_offset());
2544 }
2545
2546 protected:
2547 void
2548 do_adjust_output_section(Output_section* os)
2549 { os->set_entsize(0); }
2550
2551 // Write to a map file.
2552 void
2553 do_print_to_mapfile(Mapfile* mapfile) const
2554 { mapfile->print_output_data(this, _(".plt")); }
2555
2556 private:
2557 // Template for the first PLT entry.
2558 static const uint32_t plt0_entry_o32[];
2559 static const uint32_t plt0_entry_n32[];
2560 static const uint32_t plt0_entry_n64[];
2561 static const uint32_t plt0_entry_micromips_o32[];
2562 static const uint32_t plt0_entry_micromips32_o32[];
2563
2564 // Template for subsequent PLT entries.
2565 static const uint32_t plt_entry[];
f5b11759 2566 static const uint32_t plt_entry_r6[];
9810d34d
SS
2567 static const uint32_t plt_entry_mips16_o32[];
2568 static const uint32_t plt_entry_micromips_o32[];
2569 static const uint32_t plt_entry_micromips32_o32[];
2570
2571 // Set the final size.
2572 void
2573 set_final_data_size()
2574 {
2575 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
2576 + this->plt_comp_offset_);
2577 }
2578
2579 // Write out the PLT data.
2580 void
2581 do_write(Output_file*);
2582
2583 // Return whether the plt header contains microMIPS code. For the sake of
2584 // cache alignment always use a standard header whenever any standard entries
2585 // are present even if microMIPS entries are present as well. This also lets
2586 // the microMIPS header rely on the value of $v0 only set by microMIPS
2587 // entries, for a small size reduction.
2588 bool
2589 is_plt_header_compressed() const
2590 {
2591 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
2592 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
2593 }
2594
2595 // Return the size of the PLT header.
2596 unsigned int
2597 get_plt_header_size() const
2598 {
2599 if (this->target_->is_output_n64())
2600 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
2601 else if (this->target_->is_output_n32())
2602 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
2603 else if (!this->is_plt_header_compressed())
2604 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
2605 else if (this->target_->use_32bit_micromips_instructions())
2606 return (2 * sizeof(plt0_entry_micromips32_o32)
2607 / sizeof(plt0_entry_micromips32_o32[0]));
2608 else
2609 return (2 * sizeof(plt0_entry_micromips_o32)
2610 / sizeof(plt0_entry_micromips_o32[0]));
2611 }
2612
2613 // Return the PLT header entry.
2614 const uint32_t*
2615 get_plt_header_entry() const
2616 {
2617 if (this->target_->is_output_n64())
2618 return plt0_entry_n64;
2619 else if (this->target_->is_output_n32())
2620 return plt0_entry_n32;
2621 else if (!this->is_plt_header_compressed())
2622 return plt0_entry_o32;
2623 else if (this->target_->use_32bit_micromips_instructions())
2624 return plt0_entry_micromips32_o32;
2625 else
2626 return plt0_entry_micromips_o32;
2627 }
2628
2629 // Return the size of the standard PLT entry.
2630 unsigned int
2631 standard_plt_entry_size() const
2632 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
2633
2634 // Return the size of the compressed PLT entry.
2635 unsigned int
2636 compressed_plt_entry_size() const
2637 {
2638 gold_assert(!this->target_->is_output_newabi());
2639
2640 if (!this->target_->is_output_micromips())
2641 return (2 * sizeof(plt_entry_mips16_o32)
2642 / sizeof(plt_entry_mips16_o32[0]));
2643 else if (this->target_->use_32bit_micromips_instructions())
2644 return (2 * sizeof(plt_entry_micromips32_o32)
2645 / sizeof(plt_entry_micromips32_o32[0]));
2646 else
2647 return (2 * sizeof(plt_entry_micromips_o32)
2648 / sizeof(plt_entry_micromips_o32[0]));
2649 }
2650
2651 // The reloc section.
2652 Reloc_section* rel_;
2653 // The .got.plt section.
2654 Output_data_space* got_plt_;
2655 // Symbols that have PLT entry.
2656 std::vector<Mips_symbol<size>*> symbols_;
2657 // The offset of the next standard PLT entry to create.
2658 unsigned int plt_mips_offset_;
2659 // The offset of the next compressed PLT entry to create.
2660 unsigned int plt_comp_offset_;
2661 // The size of the PLT header in bytes.
2662 unsigned int plt_header_size_;
2663 // The target.
2664 Target_mips<size, big_endian>* target_;
2665};
2666
2667// A class to handle the .MIPS.stubs data.
2668
2669template<int size, bool big_endian>
2670class Mips_output_data_mips_stubs : public Output_section_data
2671{
2672 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2673
15eb1beb
VR
2674 // Unordered set of .MIPS.stubs entries.
2675 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
2676 Mips_stubs_entry_set;
2677
9810d34d
SS
2678 public:
2679 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
2680 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
2681 stub_offsets_are_set_(false), target_(target)
2682 { }
2683
2684 // Create entry for a symbol.
2685 void
2686 make_entry(Mips_symbol<size>*);
2687
2688 // Remove entry for a symbol.
2689 void
2690 remove_entry(Mips_symbol<size>* gsym);
2691
2692 // Set stub offsets for symbols. This method expects that the number of
2693 // entries in dynamic symbol table is set.
2694 void
2695 set_lazy_stub_offsets();
2696
2697 void
2698 set_needs_dynsym_value();
2699
2700 // Set the number of entries in dynamic symbol table.
2701 void
2702 set_dynsym_count(unsigned int dynsym_count)
2703 { this->dynsym_count_ = dynsym_count; }
2704
2705 // Return maximum size of the stub, ie. the stub size if the dynamic symbol
2706 // count is greater than 0x10000. If the dynamic symbol count is less than
2707 // 0x10000, the stub will be 4 bytes smaller.
2708 // There's no disadvantage from using microMIPS code here, so for the sake of
2709 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
2710 // output produced at all. This has a benefit of stubs being shorter by
2711 // 4 bytes each too, unless in the insn32 mode.
2712 unsigned int
2713 stub_max_size() const
2714 {
2715 if (!this->target_->is_output_micromips()
2716 || this->target_->use_32bit_micromips_instructions())
2717 return 20;
2718 else
2719 return 16;
2720 }
2721
2722 // Return the size of the stub. This method expects that the final dynsym
2723 // count is set.
2724 unsigned int
2725 stub_size() const
2726 {
2727 gold_assert(this->dynsym_count_ != -1U);
2728 if (this->dynsym_count_ > 0x10000)
2729 return this->stub_max_size();
2730 else
2731 return this->stub_max_size() - 4;
2732 }
2733
2734 // Return output address of a stub.
2735 Mips_address
2736 stub_address(const Mips_symbol<size>* sym) const
2737 {
2738 gold_assert(sym->has_lazy_stub());
2739 return this->address() + sym->lazy_stub_offset();
2740 }
2741
2742 protected:
2743 void
2744 do_adjust_output_section(Output_section* os)
2745 { os->set_entsize(0); }
2746
2747 // Write to a map file.
2748 void
2749 do_print_to_mapfile(Mapfile* mapfile) const
2750 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2751
2752 private:
2753 static const uint32_t lazy_stub_normal_1[];
2754 static const uint32_t lazy_stub_normal_1_n64[];
2755 static const uint32_t lazy_stub_normal_2[];
2756 static const uint32_t lazy_stub_normal_2_n64[];
2757 static const uint32_t lazy_stub_big[];
2758 static const uint32_t lazy_stub_big_n64[];
2759
2760 static const uint32_t lazy_stub_micromips_normal_1[];
2761 static const uint32_t lazy_stub_micromips_normal_1_n64[];
2762 static const uint32_t lazy_stub_micromips_normal_2[];
2763 static const uint32_t lazy_stub_micromips_normal_2_n64[];
2764 static const uint32_t lazy_stub_micromips_big[];
2765 static const uint32_t lazy_stub_micromips_big_n64[];
2766
2767 static const uint32_t lazy_stub_micromips32_normal_1[];
2768 static const uint32_t lazy_stub_micromips32_normal_1_n64[];
2769 static const uint32_t lazy_stub_micromips32_normal_2[];
2770 static const uint32_t lazy_stub_micromips32_normal_2_n64[];
2771 static const uint32_t lazy_stub_micromips32_big[];
2772 static const uint32_t lazy_stub_micromips32_big_n64[];
2773
2774 // Set the final size.
2775 void
2776 set_final_data_size()
2777 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
2778
2779 // Write out the .MIPS.stubs data.
2780 void
2781 do_write(Output_file*);
2782
2783 // .MIPS.stubs symbols
15eb1beb 2784 Mips_stubs_entry_set symbols_;
9810d34d
SS
2785 // Number of entries in dynamic symbol table.
2786 unsigned int dynsym_count_;
2787 // Whether the stub offsets are set.
2788 bool stub_offsets_are_set_;
2789 // The target.
2790 Target_mips<size, big_endian>* target_;
2791};
2792
2793// This class handles Mips .reginfo output section.
2794
2795template<int size, bool big_endian>
82e49872 2796class Mips_output_section_reginfo : public Output_section_data
9810d34d
SS
2797{
2798 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
2799
2800 public:
82e49872
VR
2801 Mips_output_section_reginfo(Target_mips<size, big_endian>* target,
2802 Valtype gprmask, Valtype cprmask1,
2803 Valtype cprmask2, Valtype cprmask3,
2804 Valtype cprmask4)
2805 : Output_section_data(24, 4, true), target_(target),
2806 gprmask_(gprmask), cprmask1_(cprmask1), cprmask2_(cprmask2),
2807 cprmask3_(cprmask3), cprmask4_(cprmask4)
9810d34d
SS
2808 { }
2809
9810d34d 2810 protected:
82e49872 2811 // Write to a map file.
9810d34d 2812 void
82e49872
VR
2813 do_print_to_mapfile(Mapfile* mapfile) const
2814 { mapfile->print_output_data(this, _(".reginfo")); }
9810d34d
SS
2815
2816 // Write out reginfo section.
2817 void
2818 do_write(Output_file* of);
2819
2820 private:
2821 Target_mips<size, big_endian>* target_;
2822
2823 // gprmask of the output .reginfo section.
2824 Valtype gprmask_;
2825 // cprmask1 of the output .reginfo section.
2826 Valtype cprmask1_;
2827 // cprmask2 of the output .reginfo section.
2828 Valtype cprmask2_;
2829 // cprmask3 of the output .reginfo section.
2830 Valtype cprmask3_;
2831 // cprmask4 of the output .reginfo section.
2832 Valtype cprmask4_;
2833};
2834
1728969e
VR
2835// This class handles .MIPS.options output section.
2836
2837template<int size, bool big_endian>
2838class Mips_output_section_options : public Output_section
2839{
2840 public:
2841 Mips_output_section_options(const char* name, elfcpp::Elf_Word type,
2842 elfcpp::Elf_Xword flags,
2843 Target_mips<size, big_endian>* target)
2844 : Output_section(name, type, flags), target_(target)
2845 {
2846 // After the input sections are written, we only need to update
2847 // ri_gp_value field of ODK_REGINFO entries.
2848 this->set_after_input_sections();
2849 }
2850
2851 protected:
2852 // Write out option section.
2853 void
2854 do_write(Output_file* of);
2855
2856 private:
2857 Target_mips<size, big_endian>* target_;
2858};
2859
b52717c0
VR
2860// This class handles .MIPS.abiflags output section.
2861
2862template<int size, bool big_endian>
2863class Mips_output_section_abiflags : public Output_section_data
2864{
2865 public:
2866 Mips_output_section_abiflags(const Mips_abiflags<big_endian>& abiflags)
2867 : Output_section_data(24, 8, true), abiflags_(abiflags)
2868 { }
2869
2870 protected:
2871 // Write to a map file.
2872 void
2873 do_print_to_mapfile(Mapfile* mapfile) const
2874 { mapfile->print_output_data(this, _(".MIPS.abiflags")); }
2875
2876 void
2877 do_write(Output_file* of);
2878
2879 private:
2880 const Mips_abiflags<big_endian>& abiflags_;
2881};
2882
9810d34d
SS
2883// The MIPS target has relocation types which default handling of relocatable
2884// relocation cannot process. So we have to extend the default code.
2885
4d625b70 2886template<bool big_endian, typename Classify_reloc>
9810d34d 2887class Mips_scan_relocatable_relocs :
4d625b70 2888 public Default_scan_relocatable_relocs<Classify_reloc>
9810d34d
SS
2889{
2890 public:
2891 // Return the strategy to use for a local symbol which is a section
2892 // symbol, given the relocation type.
2893 inline Relocatable_relocs::Reloc_strategy
2894 local_section_strategy(unsigned int r_type, Relobj* object)
2895 {
4d625b70 2896 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
9810d34d
SS
2897 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2898 else
2899 {
2900 switch (r_type)
2901 {
2902 case elfcpp::R_MIPS_26:
2903 return Relocatable_relocs::RELOC_SPECIAL;
2904
2905 default:
4d625b70 2906 return Default_scan_relocatable_relocs<Classify_reloc>::
9810d34d
SS
2907 local_section_strategy(r_type, object);
2908 }
2909 }
2910 }
2911};
2912
2913// Mips_copy_relocs class. The only difference from the base class is the
2914// method emit_mips, which should be called instead of Copy_reloc_entry::emit.
2915// Mips cannot convert all relocation types to dynamic relocs. If a reloc
2916// cannot be made dynamic, a COPY reloc is emitted.
2917
2918template<int sh_type, int size, bool big_endian>
2919class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
2920{
2921 public:
2922 Mips_copy_relocs()
2923 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY)
2924 { }
2925
2926 // Emit any saved relocations which turn out to be needed. This is
2927 // called after all the relocs have been scanned.
2928 void
2929 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*,
2930 Symbol_table*, Layout*, Target_mips<size, big_endian>*);
2931
2932 private:
2933 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry
2934 Copy_reloc_entry;
2935
2936 // Emit this reloc if appropriate. This is called after we have
2937 // scanned all the relocations, so we know whether we emitted a
2938 // COPY relocation for SYM_.
2939 void
2940 emit_entry(Copy_reloc_entry& entry,
2941 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
2942 Symbol_table* symtab, Layout* layout,
2943 Target_mips<size, big_endian>* target);
2944};
2945
2946
2947// Return true if the symbol SYM should be considered to resolve local
2948// to the current module, and false otherwise. The logic is taken from
2949// GNU ld's method _bfd_elf_symbol_refs_local_p.
2950static bool
2951symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
2952 bool local_protected)
2953{
2954 // If it's a local sym, of course we resolve locally.
2955 if (sym == NULL)
2956 return true;
2957
2958 // STV_HIDDEN or STV_INTERNAL ones must be local.
2959 if (sym->visibility() == elfcpp::STV_HIDDEN
2960 || sym->visibility() == elfcpp::STV_INTERNAL)
2961 return true;
2962
2963 // If we don't have a definition in a regular file, then we can't
2964 // resolve locally. The sym is either undefined or dynamic.
453018bf 2965 if (sym->is_from_dynobj() || sym->is_undefined())
9810d34d
SS
2966 return false;
2967
2968 // Forced local symbols resolve locally.
2969 if (sym->is_forced_local())
2970 return true;
2971
2972 // As do non-dynamic symbols.
2973 if (!has_dynsym_entry)
2974 return true;
2975
2976 // At this point, we know the symbol is defined and dynamic. In an
2977 // executable it must resolve locally, likewise when building symbolic
2978 // shared libraries.
2979 if (parameters->options().output_is_executable()
2980 || parameters->options().Bsymbolic())
2981 return true;
2982
2983 // Now deal with defined dynamic symbols in shared libraries. Ones
2984 // with default visibility might not resolve locally.
2985 if (sym->visibility() == elfcpp::STV_DEFAULT)
2986 return false;
2987
2988 // STV_PROTECTED non-function symbols are local.
2989 if (sym->type() != elfcpp::STT_FUNC)
2990 return true;
2991
2992 // Function pointer equality tests may require that STV_PROTECTED
2993 // symbols be treated as dynamic symbols. If the address of a
2994 // function not defined in an executable is set to that function's
2995 // plt entry in the executable, then the address of the function in
2996 // a shared library must also be the plt entry in the executable.
2997 return local_protected;
2998}
2999
3000// Return TRUE if references to this symbol always reference the symbol in this
3001// object.
3002static bool
3003symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
3004{
3005 return symbol_refs_local(sym, has_dynsym_entry, false);
3006}
3007
3008// Return TRUE if calls to this symbol always call the version in this object.
3009static bool
3010symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
3011{
3012 return symbol_refs_local(sym, has_dynsym_entry, true);
3013}
3014
3015// Compare GOT offsets of two symbols.
3016
3017template<int size, bool big_endian>
3018static bool
3019got_offset_compare(Symbol* sym1, Symbol* sym2)
3020{
3021 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
3022 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
3023 unsigned int area1 = mips_sym1->global_got_area();
3024 unsigned int area2 = mips_sym2->global_got_area();
3025 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
3026
3027 // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
3028 if (area1 != area2)
3029 return area1 < area2;
3030
3031 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
3032}
3033
3034// This method divides dynamic symbols into symbols that have GOT entry, and
3035// symbols that don't have GOT entry. It also sorts symbols with the GOT entry.
3036// Mips ABI requires that symbols with the GOT entry must be at the end of
3037// dynamic symbol table, and the order in dynamic symbol table must match the
3038// order in GOT.
3039
3040template<int size, bool big_endian>
3041static void
3042reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
3043 std::vector<Symbol*>* non_got_symbols,
3044 std::vector<Symbol*>* got_symbols)
3045{
3046 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
3047 p != dyn_symbols->end();
3048 ++p)
3049 {
3050 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
3051 if (mips_sym->global_got_area() == GGA_NORMAL
3052 || mips_sym->global_got_area() == GGA_RELOC_ONLY)
3053 got_symbols->push_back(mips_sym);
3054 else
3055 non_got_symbols->push_back(mips_sym);
3056 }
3057
3058 std::sort(got_symbols->begin(), got_symbols->end(),
3059 got_offset_compare<size, big_endian>);
3060}
3061
3062// Functor class for processing the global symbol table.
3063
3064template<int size, bool big_endian>
3065class Symbol_visitor_check_symbols
3066{
3067 public:
3068 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
3069 Layout* layout, Symbol_table* symtab)
3070 : target_(target), layout_(layout), symtab_(symtab)
3071 { }
3072
3073 void
3074 operator()(Sized_symbol<size>* sym)
3075 {
3076 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3077 if (local_pic_function<size, big_endian>(mips_sym))
3078 {
3079 // SYM is a function that might need $25 to be valid on entry.
3080 // If we're creating a non-PIC relocatable object, mark SYM as
3081 // being PIC. If we're creating a non-relocatable object with
3082 // non-PIC branches and jumps to SYM, make sure that SYM has an la25
3083 // stub.
3084 if (parameters->options().relocatable())
3085 {
3086 if (!parameters->options().output_is_position_independent())
3087 mips_sym->set_pic();
3088 }
3089 else if (mips_sym->has_nonpic_branches())
3090 {
3091 this->target_->la25_stub_section(layout_)
3092 ->create_la25_stub(this->symtab_, this->target_, mips_sym);
3093 }
3094 }
3095 }
3096
3097 private:
3098 Target_mips<size, big_endian>* target_;
3099 Layout* layout_;
3100 Symbol_table* symtab_;
3101};
3102
4d625b70
CC
3103// Relocation types, parameterized by SHT_REL vs. SHT_RELA, size,
3104// and endianness. The relocation format for MIPS-64 is non-standard.
3105
3106template<int sh_type, int size, bool big_endian>
3107struct Mips_reloc_types;
3108
3109template<bool big_endian>
3110struct Mips_reloc_types<elfcpp::SHT_REL, 32, big_endian>
3111{
3112 typedef typename elfcpp::Rel<32, big_endian> Reloc;
3113 typedef typename elfcpp::Rel_write<32, big_endian> Reloc_write;
3114
47a9f4fc 3115 static typename elfcpp::Elf_types<32>::Elf_Swxword
4d625b70
CC
3116 get_r_addend(const Reloc*)
3117 { return 0; }
3118
3119 static inline void
3120 set_reloc_addend(Reloc_write*,
3121 typename elfcpp::Elf_types<32>::Elf_Swxword)
3122 { gold_unreachable(); }
3123};
3124
3125template<bool big_endian>
3126struct Mips_reloc_types<elfcpp::SHT_RELA, 32, big_endian>
3127{
3128 typedef typename elfcpp::Rela<32, big_endian> Reloc;
3129 typedef typename elfcpp::Rela_write<32, big_endian> Reloc_write;
3130
47a9f4fc 3131 static typename elfcpp::Elf_types<32>::Elf_Swxword
4d625b70
CC
3132 get_r_addend(const Reloc* reloc)
3133 { return reloc->get_r_addend(); }
3134
3135 static inline void
3136 set_reloc_addend(Reloc_write* p,
3137 typename elfcpp::Elf_types<32>::Elf_Swxword val)
3138 { p->put_r_addend(val); }
3139};
3140
3141template<bool big_endian>
3142struct Mips_reloc_types<elfcpp::SHT_REL, 64, big_endian>
3143{
3144 typedef typename elfcpp::Mips64_rel<big_endian> Reloc;
3145 typedef typename elfcpp::Mips64_rel_write<big_endian> Reloc_write;
3146
47a9f4fc 3147 static typename elfcpp::Elf_types<64>::Elf_Swxword
4d625b70
CC
3148 get_r_addend(const Reloc*)
3149 { return 0; }
3150
3151 static inline void
3152 set_reloc_addend(Reloc_write*,
3153 typename elfcpp::Elf_types<64>::Elf_Swxword)
3154 { gold_unreachable(); }
3155};
3156
3157template<bool big_endian>
3158struct Mips_reloc_types<elfcpp::SHT_RELA, 64, big_endian>
3159{
3160 typedef typename elfcpp::Mips64_rela<big_endian> Reloc;
3161 typedef typename elfcpp::Mips64_rela_write<big_endian> Reloc_write;
3162
47a9f4fc 3163 static typename elfcpp::Elf_types<64>::Elf_Swxword
4d625b70
CC
3164 get_r_addend(const Reloc* reloc)
3165 { return reloc->get_r_addend(); }
3166
3167 static inline void
3168 set_reloc_addend(Reloc_write* p,
3169 typename elfcpp::Elf_types<64>::Elf_Swxword val)
3170 { p->put_r_addend(val); }
3171};
3172
3173// Forward declaration.
3174static unsigned int
3175mips_get_size_for_reloc(unsigned int, Relobj*);
3176
3177// A class for inquiring about properties of a relocation,
3178// used while scanning relocs during a relocatable link and
3179// garbage collection.
3180
3181template<int sh_type_, int size, bool big_endian>
3182class Mips_classify_reloc;
3183
3184template<int sh_type_, bool big_endian>
3185class Mips_classify_reloc<sh_type_, 32, big_endian> :
3186 public gold::Default_classify_reloc<sh_type_, 32, big_endian>
3187{
3188 public:
3189 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc
3190 Reltype;
3191 typedef typename Mips_reloc_types<sh_type_, 32, big_endian>::Reloc_write
3192 Reltype_write;
3193
3194 // Return the symbol referred to by the relocation.
3195 static inline unsigned int
3196 get_r_sym(const Reltype* reloc)
3197 { return elfcpp::elf_r_sym<32>(reloc->get_r_info()); }
3198
3199 // Return the type of the relocation.
3200 static inline unsigned int
3201 get_r_type(const Reltype* reloc)
3202 { return elfcpp::elf_r_type<32>(reloc->get_r_info()); }
3203
47a9f4fc
VR
3204 static inline unsigned int
3205 get_r_type2(const Reltype*)
3206 { return 0; }
3207
3208 static inline unsigned int
3209 get_r_type3(const Reltype*)
3210 { return 0; }
3211
3212 static inline unsigned int
3213 get_r_ssym(const Reltype*)
3214 { return 0; }
3215
4d625b70
CC
3216 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3217 static inline unsigned int
3218 get_r_addend(const Reltype* reloc)
8a8880cb
CC
3219 {
3220 if (sh_type_ == elfcpp::SHT_REL)
3221 return 0;
3222 return Mips_reloc_types<sh_type_, 32, big_endian>::get_r_addend(reloc);
3223 }
4d625b70
CC
3224
3225 // Write the r_info field to a new reloc, using the r_info field from
3226 // the original reloc, replacing the r_sym field with R_SYM.
3227 static inline void
3228 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3229 {
3230 unsigned int r_type = elfcpp::elf_r_type<32>(reloc->get_r_info());
c2fa9ced 3231 new_reloc->put_r_info(elfcpp::elf_r_info<32>(r_sym, r_type));
4d625b70
CC
3232 }
3233
3234 // Write the r_addend field to a new reloc.
3235 static inline void
3236 put_r_addend(Reltype_write* to,
3237 typename elfcpp::Elf_types<32>::Elf_Swxword addend)
3238 { Mips_reloc_types<sh_type_, 32, big_endian>::set_reloc_addend(to, addend); }
3239
3240 // Return the size of the addend of the relocation (only used for SHT_REL).
3241 static unsigned int
3242 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3243 { return mips_get_size_for_reloc(r_type, obj); }
3244};
3245
3246template<int sh_type_, bool big_endian>
3247class Mips_classify_reloc<sh_type_, 64, big_endian> :
3248 public gold::Default_classify_reloc<sh_type_, 64, big_endian>
3249{
3250 public:
3251 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc
3252 Reltype;
3253 typedef typename Mips_reloc_types<sh_type_, 64, big_endian>::Reloc_write
3254 Reltype_write;
3255
3256 // Return the symbol referred to by the relocation.
3257 static inline unsigned int
3258 get_r_sym(const Reltype* reloc)
3259 { return reloc->get_r_sym(); }
3260
47a9f4fc 3261 // Return the r_type of the relocation.
4d625b70
CC
3262 static inline unsigned int
3263 get_r_type(const Reltype* reloc)
3264 { return reloc->get_r_type(); }
3265
47a9f4fc
VR
3266 // Return the r_type2 of the relocation.
3267 static inline unsigned int
3268 get_r_type2(const Reltype* reloc)
3269 { return reloc->get_r_type2(); }
3270
3271 // Return the r_type3 of the relocation.
3272 static inline unsigned int
3273 get_r_type3(const Reltype* reloc)
3274 { return reloc->get_r_type3(); }
3275
3276 // Return the special symbol of the relocation.
3277 static inline unsigned int
3278 get_r_ssym(const Reltype* reloc)
3279 { return reloc->get_r_ssym(); }
3280
4d625b70
CC
3281 // Return the explicit addend of the relocation (return 0 for SHT_REL).
3282 static inline typename elfcpp::Elf_types<64>::Elf_Swxword
3283 get_r_addend(const Reltype* reloc)
8a8880cb
CC
3284 {
3285 if (sh_type_ == elfcpp::SHT_REL)
3286 return 0;
3287 return Mips_reloc_types<sh_type_, 64, big_endian>::get_r_addend(reloc);
3288 }
4d625b70
CC
3289
3290 // Write the r_info field to a new reloc, using the r_info field from
3291 // the original reloc, replacing the r_sym field with R_SYM.
3292 static inline void
3293 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
3294 {
3295 new_reloc->put_r_sym(r_sym);
3296 new_reloc->put_r_ssym(reloc->get_r_ssym());
3297 new_reloc->put_r_type3(reloc->get_r_type3());
3298 new_reloc->put_r_type2(reloc->get_r_type2());
3299 new_reloc->put_r_type(reloc->get_r_type());
3300 }
3301
3302 // Write the r_addend field to a new reloc.
3303 static inline void
3304 put_r_addend(Reltype_write* to,
3305 typename elfcpp::Elf_types<64>::Elf_Swxword addend)
3306 { Mips_reloc_types<sh_type_, 64, big_endian>::set_reloc_addend(to, addend); }
3307
3308 // Return the size of the addend of the relocation (only used for SHT_REL).
3309 static unsigned int
3310 get_size_for_reloc(unsigned int r_type, Relobj* obj)
3311 { return mips_get_size_for_reloc(r_type, obj); }
3312};
3313
9810d34d
SS
3314template<int size, bool big_endian>
3315class Target_mips : public Sized_target<size, big_endian>
3316{
3317 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
47a9f4fc 3318 typedef Mips_output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
9810d34d 3319 Reloc_section;
9810d34d
SS
3320 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
3321 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4d625b70
CC
3322 typedef typename Mips_reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
3323 Reltype;
3324 typedef typename Mips_reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3325 Relatype;
9810d34d
SS
3326
3327 public:
3328 Target_mips(const Target::Target_info* info = &mips_info)
3329 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
a8ecc9fe
VR
3330 got_plt_(NULL), rel_dyn_(NULL), rld_map_(NULL), copy_relocs_(),
3331 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
3332 mips_stubs_(NULL), attributes_section_data_(NULL), abiflags_(NULL),
3333 mach_(0), layout_(NULL), got16_addends_(), has_abiflags_section_(false),
01b84e25 3334 entry_symbol_is_compressed_(false), insn32_(false)
9810d34d
SS
3335 {
3336 this->add_machine_extensions();
3337 }
3338
3339 // The offset of $gp from the beginning of the .got section.
3340 static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
3341
3342 // The maximum size of the GOT for it to be addressable using 16-bit
3343 // offsets from $gp.
3344 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
3345
3346 // Make a new symbol table entry for the Mips target.
3347 Sized_symbol<size>*
dc1c8a16 3348 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
9810d34d
SS
3349 { return new Mips_symbol<size>(); }
3350
3351 // Process the relocations to determine unreferenced sections for
3352 // garbage collection.
3353 void
3354 gc_process_relocs(Symbol_table* symtab,
3355 Layout* layout,
3356 Sized_relobj_file<size, big_endian>* object,
3357 unsigned int data_shndx,
3358 unsigned int sh_type,
3359 const unsigned char* prelocs,
3360 size_t reloc_count,
3361 Output_section* output_section,
3362 bool needs_special_offset_handling,
3363 size_t local_symbol_count,
3364 const unsigned char* plocal_symbols);
3365
3366 // Scan the relocations to look for symbol adjustments.
3367 void
3368 scan_relocs(Symbol_table* symtab,
3369 Layout* layout,
3370 Sized_relobj_file<size, big_endian>* object,
3371 unsigned int data_shndx,
3372 unsigned int sh_type,
3373 const unsigned char* prelocs,
3374 size_t reloc_count,
3375 Output_section* output_section,
3376 bool needs_special_offset_handling,
3377 size_t local_symbol_count,
3378 const unsigned char* plocal_symbols);
3379
3380 // Finalize the sections.
3381 void
3382 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
3383
3384 // Relocate a section.
3385 void
3386 relocate_section(const Relocate_info<size, big_endian>*,
3387 unsigned int sh_type,
3388 const unsigned char* prelocs,
3389 size_t reloc_count,
3390 Output_section* output_section,
3391 bool needs_special_offset_handling,
3392 unsigned char* view,
3393 Mips_address view_address,
3394 section_size_type view_size,
3395 const Reloc_symbol_changes*);
3396
3397 // Scan the relocs during a relocatable link.
3398 void
3399 scan_relocatable_relocs(Symbol_table* symtab,
3400 Layout* layout,
3401 Sized_relobj_file<size, big_endian>* object,
3402 unsigned int data_shndx,
3403 unsigned int sh_type,
3404 const unsigned char* prelocs,
3405 size_t reloc_count,
3406 Output_section* output_section,
3407 bool needs_special_offset_handling,
3408 size_t local_symbol_count,
3409 const unsigned char* plocal_symbols,
3410 Relocatable_relocs*);
3411
4d625b70
CC
3412 // Scan the relocs for --emit-relocs.
3413 void
3414 emit_relocs_scan(Symbol_table* symtab,
3415 Layout* layout,
3416 Sized_relobj_file<size, big_endian>* object,
3417 unsigned int data_shndx,
3418 unsigned int sh_type,
3419 const unsigned char* prelocs,
3420 size_t reloc_count,
3421 Output_section* output_section,
3422 bool needs_special_offset_handling,
3423 size_t local_symbol_count,
3424 const unsigned char* plocal_syms,
3425 Relocatable_relocs* rr);
3426
9810d34d
SS
3427 // Emit relocations for a section.
3428 void
3429 relocate_relocs(const Relocate_info<size, big_endian>*,
3430 unsigned int sh_type,
3431 const unsigned char* prelocs,
3432 size_t reloc_count,
3433 Output_section* output_section,
3434 typename elfcpp::Elf_types<size>::Elf_Off
3435 offset_in_output_section,
9810d34d
SS
3436 unsigned char* view,
3437 Mips_address view_address,
3438 section_size_type view_size,
3439 unsigned char* reloc_view,
3440 section_size_type reloc_view_size);
3441
3442 // Perform target-specific processing in a relocatable link. This is
3443 // only used if we use the relocation strategy RELOC_SPECIAL.
3444 void
3445 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
3446 unsigned int sh_type,
3447 const unsigned char* preloc_in,
3448 size_t relnum,
3449 Output_section* output_section,
3450 typename elfcpp::Elf_types<size>::Elf_Off
3451 offset_in_output_section,
3452 unsigned char* view,
3453 Mips_address view_address,
3454 section_size_type view_size,
3455 unsigned char* preloc_out);
3456
3457 // Return whether SYM is defined by the ABI.
3458 bool
3459 do_is_defined_by_abi(const Symbol* sym) const
3460 {
3461 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
3462 || (strcmp(sym->name(), "_gp_disp") == 0)
3463 || (strcmp(sym->name(), "___tls_get_addr") == 0));
3464 }
3465
3466 // Return the number of entries in the GOT.
3467 unsigned int
3468 got_entry_count() const
3469 {
3470 if (!this->has_got_section())
3471 return 0;
3472 return this->got_size() / (size/8);
3473 }
3474
3475 // Return the number of entries in the PLT.
3476 unsigned int
3477 plt_entry_count() const
3478 {
3479 if (this->plt_ == NULL)
3480 return 0;
3481 return this->plt_->entry_count();
3482 }
3483
3484 // Return the offset of the first non-reserved PLT entry.
3485 unsigned int
3486 first_plt_entry_offset() const
3487 { return this->plt_->first_plt_entry_offset(); }
3488
3489 // Return the size of each PLT entry.
3490 unsigned int
3491 plt_entry_size() const
3492 { return this->plt_->plt_entry_size(); }
3493
3494 // Get the GOT section, creating it if necessary.
3495 Mips_output_data_got<size, big_endian>*
3496 got_section(Symbol_table*, Layout*);
3497
3498 // Get the GOT section.
3499 Mips_output_data_got<size, big_endian>*
3500 got_section() const
3501 {
3502 gold_assert(this->got_ != NULL);
3503 return this->got_;
3504 }
3505
3506 // Get the .MIPS.stubs section, creating it if necessary.
3507 Mips_output_data_mips_stubs<size, big_endian>*
3508 mips_stubs_section(Layout* layout);
3509
3510 // Get the .MIPS.stubs section.
3511 Mips_output_data_mips_stubs<size, big_endian>*
3512 mips_stubs_section() const
3513 {
3514 gold_assert(this->mips_stubs_ != NULL);
3515 return this->mips_stubs_;
3516 }
3517
3518 // Get the LA25 stub section, creating it if necessary.
3519 Mips_output_data_la25_stub<size, big_endian>*
3520 la25_stub_section(Layout*);
3521
3522 // Get the LA25 stub section.
3523 Mips_output_data_la25_stub<size, big_endian>*
3524 la25_stub_section()
3525 {
3526 gold_assert(this->la25_stub_ != NULL);
3527 return this->la25_stub_;
3528 }
3529
3530 // Get gp value. It has the value of .got + 0x7FF0.
3531 Mips_address
3532 gp_value() const
3533 {
3534 if (this->gp_ != NULL)
3535 return this->gp_->value();
3536 return 0;
3537 }
3538
3539 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
3540 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
3541 Mips_address
3542 adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
3543 {
3544 if (this->gp_ == NULL)
3545 return 0;
3546
3547 bool multi_got = false;
3548 if (this->has_got_section())
3549 multi_got = this->got_section()->multi_got();
3550 if (!multi_got)
3551 return this->gp_->value();
3552 else
3553 return this->gp_->value() + this->got_section()->get_got_offset(object);
3554 }
3555
3556 // Get the dynamic reloc section, creating it if necessary.
3557 Reloc_section*
3558 rel_dyn_section(Layout*);
3559
3560 bool
3561 do_has_custom_set_dynsym_indexes() const
3562 { return true; }
3563
b52717c0
VR
3564 // Don't emit input .reginfo/.MIPS.abiflags sections to
3565 // output .reginfo/.MIPS.abiflags.
9810d34d
SS
3566 bool
3567 do_should_include_section(elfcpp::Elf_Word sh_type) const
b52717c0
VR
3568 {
3569 return ((sh_type != elfcpp::SHT_MIPS_REGINFO)
3570 && (sh_type != elfcpp::SHT_MIPS_ABIFLAGS));
3571 }
9810d34d
SS
3572
3573 // Set the dynamic symbol indexes. INDEX is the index of the first
3574 // global dynamic symbol. Pointers to the symbols are stored into the
3575 // vector SYMS. The names are added to DYNPOOL. This returns an
3576 // updated dynamic symbol index.
3577 unsigned int
3578 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
3579 std::vector<Symbol*>* syms, Stringpool* dynpool,
3580 Versions* versions, Symbol_table* symtab) const;
3581
3582 // Remove .MIPS.stubs entry for a symbol.
3583 void
3584 remove_lazy_stub_entry(Mips_symbol<size>* sym)
3585 {
3586 if (this->mips_stubs_ != NULL)
3587 this->mips_stubs_->remove_entry(sym);
3588 }
3589
3590 // The value to write into got[1] for SVR4 targets, to identify it is
3591 // a GNU object. The dynamic linker can then use got[1] to store the
3592 // module pointer.
3593 uint64_t
3594 mips_elf_gnu_got1_mask()
3595 {
3596 if (this->is_output_n64())
3597 return (uint64_t)1 << 63;
3598 else
3599 return 1 << 31;
3600 }
3601
3602 // Whether the output has microMIPS code. This is valid only after
b52717c0 3603 // merge_obj_e_flags() is called.
9810d34d
SS
3604 bool
3605 is_output_micromips() const
3606 {
3607 gold_assert(this->are_processor_specific_flags_set());
3608 return elfcpp::is_micromips(this->processor_specific_flags());
3609 }
3610
3611 // Whether the output uses N32 ABI. This is valid only after
b52717c0 3612 // merge_obj_e_flags() is called.
9810d34d
SS
3613 bool
3614 is_output_n32() const
3615 {
3616 gold_assert(this->are_processor_specific_flags_set());
3617 return elfcpp::abi_n32(this->processor_specific_flags());
3618 }
3619
f5b11759
VR
3620 // Whether the output uses R6 ISA. This is valid only after
3621 // merge_obj_e_flags() is called.
3622 bool
3623 is_output_r6() const
3624 {
3625 gold_assert(this->are_processor_specific_flags_set());
3626 return elfcpp::r6_isa(this->processor_specific_flags());
3627 }
3628
01b84e25 3629 // Whether the output uses N64 ABI.
9810d34d
SS
3630 bool
3631 is_output_n64() const
01b84e25 3632 { return size == 64; }
9810d34d
SS
3633
3634 // Whether the output uses NEWABI. This is valid only after
b52717c0 3635 // merge_obj_e_flags() is called.
9810d34d
SS
3636 bool
3637 is_output_newabi() const
3638 { return this->is_output_n32() || this->is_output_n64(); }
3639
3640 // Whether we can only use 32-bit microMIPS instructions.
3641 bool
3642 use_32bit_micromips_instructions() const
3643 { return this->insn32_; }
3644
4d625b70
CC
3645 // Return the r_sym field from a relocation.
3646 unsigned int
3647 get_r_sym(const unsigned char* preloc) const
3648 {
3649 // Since REL and RELA relocs share the same structure through
3650 // the r_info field, we can just use REL here.
3651 Reltype rel(preloc);
3652 return Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
3653 get_r_sym(&rel);
3654 }
3655
9810d34d
SS
3656 protected:
3657 // Return the value to use for a dynamic symbol which requires special
3658 // treatment. This is how we support equality comparisons of function
3659 // pointers across shared library boundaries, as described in the
3660 // processor specific ABI supplement.
3661 uint64_t
3662 do_dynsym_value(const Symbol* gsym) const;
3663
3664 // Make an ELF object.
3665 Object*
3666 do_make_elf_object(const std::string&, Input_file*, off_t,
3667 const elfcpp::Ehdr<size, big_endian>& ehdr);
3668
3669 Object*
3670 do_make_elf_object(const std::string&, Input_file*, off_t,
3671 const elfcpp::Ehdr<size, !big_endian>&)
3672 { gold_unreachable(); }
3673
1728969e
VR
3674 // Make an output section.
3675 Output_section*
3676 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3677 elfcpp::Elf_Xword flags)
3678 {
3679 if (type == elfcpp::SHT_MIPS_OPTIONS)
3680 return new Mips_output_section_options<size, big_endian>(name, type,
3681 flags, this);
3682 else
3683 return new Output_section(name, type, flags);
3684 }
3685
9810d34d
SS
3686 // Adjust ELF file header.
3687 void
3688 do_adjust_elf_header(unsigned char* view, int len);
3689
3690 // Get the custom dynamic tag value.
3691 unsigned int
3692 do_dynamic_tag_custom_value(elfcpp::DT) const;
3693
3694 // Adjust the value written to the dynamic symbol table.
3695 virtual void
3696 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
3697 {
3698 elfcpp::Sym<size, big_endian> isym(view);
3699 elfcpp::Sym_write<size, big_endian> osym(view);
3700 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
3701
3702 // Keep dynamic compressed symbols odd. This allows the dynamic linker
3703 // to treat compressed symbols like any other.
3704 Mips_address value = isym.get_st_value();
3705 if (mips_sym->is_mips16() && value != 0)
3706 {
3707 if (!mips_sym->has_mips16_fn_stub())
3708 value |= 1;
3709 else
3710 {
3711 // If we have a MIPS16 function with a stub, the dynamic symbol
3712 // must refer to the stub, since only the stub uses the standard
3713 // calling conventions. Stub contains MIPS32 code, so don't add +1
3714 // in this case.
3715
3716 // There is a code which does this in the method
3717 // Target_mips::do_dynsym_value, but that code will only be
3718 // executed if the symbol is from dynobj.
3719 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
3720 // table.
3721
3722 Mips16_stub_section<size, big_endian>* fn_stub =
3723 mips_sym->template get_mips16_fn_stub<big_endian>();
3724 value = fn_stub->output_address();
3725 osym.put_st_size(fn_stub->section_size());
3726 }
3727
3728 osym.put_st_value(value);
3729 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3730 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
3731 }
3732 else if ((mips_sym->is_micromips()
3733 // Stubs are always microMIPS if there is any microMIPS code in
3734 // the output.
3735 || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
3736 && value != 0)
3737 {
3738 osym.put_st_value(value | 1);
3739 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
3740 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
3741 }
3742 }
3743
3744 private:
3745 // The class which scans relocations.
3746 class Scan
3747 {
3748 public:
3749 Scan()
3750 { }
3751
3752 static inline int
3753 get_reference_flags(unsigned int r_type);
3754
3755 inline void
3756 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3757 Sized_relobj_file<size, big_endian>* object,
3758 unsigned int data_shndx,
3759 Output_section* output_section,
4d625b70 3760 const Reltype& reloc, unsigned int r_type,
9810d34d
SS
3761 const elfcpp::Sym<size, big_endian>& lsym,
3762 bool is_discarded);
3763
3764 inline void
3765 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3766 Sized_relobj_file<size, big_endian>* object,
3767 unsigned int data_shndx,
3768 Output_section* output_section,
4d625b70 3769 const Relatype& reloc, unsigned int r_type,
9810d34d
SS
3770 const elfcpp::Sym<size, big_endian>& lsym,
3771 bool is_discarded);
3772
3773 inline void
3774 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
3775 Sized_relobj_file<size, big_endian>* object,
3776 unsigned int data_shndx,
3777 Output_section* output_section,
4d625b70
CC
3778 const Relatype* rela,
3779 const Reltype* rel,
9810d34d
SS
3780 unsigned int rel_type,
3781 unsigned int r_type,
3782 const elfcpp::Sym<size, big_endian>& lsym,
3783 bool is_discarded);
3784
3785 inline void
3786 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3787 Sized_relobj_file<size, big_endian>* object,
3788 unsigned int data_shndx,
3789 Output_section* output_section,
4d625b70 3790 const Reltype& reloc, unsigned int r_type,
9810d34d
SS
3791 Symbol* gsym);
3792
3793 inline void
3794 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3795 Sized_relobj_file<size, big_endian>* object,
3796 unsigned int data_shndx,
3797 Output_section* output_section,
4d625b70 3798 const Relatype& reloc, unsigned int r_type,
9810d34d
SS
3799 Symbol* gsym);
3800
3801 inline void
3802 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
3803 Sized_relobj_file<size, big_endian>* object,
3804 unsigned int data_shndx,
3805 Output_section* output_section,
4d625b70
CC
3806 const Relatype* rela,
3807 const Reltype* rel,
9810d34d
SS
3808 unsigned int rel_type,
3809 unsigned int r_type,
3810 Symbol* gsym);
3811
3812 inline bool
3813 local_reloc_may_be_function_pointer(Symbol_table* , Layout*,
3814 Target_mips*,
3815 Sized_relobj_file<size, big_endian>*,
3816 unsigned int,
3817 Output_section*,
4d625b70 3818 const Reltype&,
9810d34d
SS
3819 unsigned int,
3820 const elfcpp::Sym<size, big_endian>&)
3821 { return false; }
3822
3823 inline bool
3824 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3825 Target_mips*,
3826 Sized_relobj_file<size, big_endian>*,
3827 unsigned int,
3828 Output_section*,
4d625b70 3829 const Reltype&,
9810d34d
SS
3830 unsigned int, Symbol*)
3831 { return false; }
3832
3833 inline bool
3834 local_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3835 Target_mips*,
3836 Sized_relobj_file<size, big_endian>*,
3837 unsigned int,
3838 Output_section*,
4d625b70 3839 const Relatype&,
9810d34d
SS
3840 unsigned int,
3841 const elfcpp::Sym<size, big_endian>&)
3842 { return false; }
3843
3844 inline bool
3845 global_reloc_may_be_function_pointer(Symbol_table*, Layout*,
3846 Target_mips*,
3847 Sized_relobj_file<size, big_endian>*,
3848 unsigned int,
3849 Output_section*,
4d625b70 3850 const Relatype&,
9810d34d
SS
3851 unsigned int, Symbol*)
3852 { return false; }
3853 private:
3854 static void
3855 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3856 unsigned int r_type);
3857
3858 static void
3859 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3860 unsigned int r_type, Symbol*);
3861 };
3862
3863 // The class which implements relocation.
3864 class Relocate
3865 {
3866 public:
3867 Relocate()
152c92b2 3868 : calculated_value_(0), calculate_only_(false)
9810d34d
SS
3869 { }
3870
3871 ~Relocate()
3872 { }
3873
47a9f4fc 3874 // Return whether a R_MIPS_32/R_MIPS_64 relocation needs to be applied.
9810d34d 3875 inline bool
47a9f4fc
VR
3876 should_apply_static_reloc(const Mips_symbol<size>* gsym,
3877 unsigned int r_type,
3878 Output_section* output_section,
3879 Target_mips* target);
9810d34d
SS
3880
3881 // Do a relocation. Return false if the caller should not issue
3882 // any warnings about this relocation.
3883 inline bool
91a65d2f
AM
3884 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3885 Target_mips*, Output_section*, size_t, const unsigned char*,
3886 const Sized_symbol<size>*, const Symbol_value<size>*,
3887 unsigned char*, Mips_address, section_size_type);
152c92b2
VR
3888
3889 private:
3890 // Result of the relocation.
3891 Valtype calculated_value_;
3892 // Whether we have to calculate relocation instead of applying it.
3893 bool calculate_only_;
9810d34d
SS
3894 };
3895
9810d34d
SS
3896 // This POD class holds the dynamic relocations that should be emitted instead
3897 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
3898 // relocations if it turns out that the symbol does not have static
3899 // relocations.
3900 class Dyn_reloc
3901 {
3902 public:
3903 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
3904 Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
3905 Output_section* output_section, Mips_address r_offset)
3906 : sym_(sym), r_type_(r_type), relobj_(relobj),
3907 shndx_(shndx), output_section_(output_section),
3908 r_offset_(r_offset)
3909 { }
3910
3911 // Emit this reloc if appropriate. This is called after we have
3912 // scanned all the relocations, so we know whether the symbol has
3913 // static relocations.
3914 void
3915 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
3916 Symbol_table* symtab)
3917 {
3918 if (!this->sym_->has_static_relocs())
3919 {
3920 got->record_global_got_symbol(this->sym_, this->relobj_,
3921 this->r_type_, true, false);
3922 if (!symbol_references_local(this->sym_,
3923 this->sym_->should_add_dynsym_entry(symtab)))
3924 rel_dyn->add_global(this->sym_, this->r_type_,
3925 this->output_section_, this->relobj_,
3926 this->shndx_, this->r_offset_);
3927 else
3928 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
3929 this->output_section_, this->relobj_,
3930 this->shndx_, this->r_offset_);
3931 }
3932 }
3933
3934 private:
3935 Mips_symbol<size>* sym_;
3936 unsigned int r_type_;
3937 Mips_relobj<size, big_endian>* relobj_;
3938 unsigned int shndx_;
3939 Output_section* output_section_;
3940 Mips_address r_offset_;
3941 };
3942
3943 // Adjust TLS relocation type based on the options and whether this
3944 // is a local symbol.
3945 static tls::Tls_optimization
3946 optimize_tls_reloc(bool is_final, int r_type);
3947
3948 // Return whether there is a GOT section.
3949 bool
3950 has_got_section() const
3951 { return this->got_ != NULL; }
3952
3953 // Check whether the given ELF header flags describe a 32-bit binary.
3954 bool
3955 mips_32bit_flags(elfcpp::Elf_Word);
3956
3957 enum Mips_mach {
3958 mach_mips3000 = 3000,
3959 mach_mips3900 = 3900,
3960 mach_mips4000 = 4000,
3961 mach_mips4010 = 4010,
3962 mach_mips4100 = 4100,
3963 mach_mips4111 = 4111,
3964 mach_mips4120 = 4120,
3965 mach_mips4300 = 4300,
3966 mach_mips4400 = 4400,
3967 mach_mips4600 = 4600,
3968 mach_mips4650 = 4650,
3969 mach_mips5000 = 5000,
3970 mach_mips5400 = 5400,
3971 mach_mips5500 = 5500,
b52717c0 3972 mach_mips5900 = 5900,
9810d34d
SS
3973 mach_mips6000 = 6000,
3974 mach_mips7000 = 7000,
3975 mach_mips8000 = 8000,
3976 mach_mips9000 = 9000,
3977 mach_mips10000 = 10000,
3978 mach_mips12000 = 12000,
3979 mach_mips14000 = 14000,
3980 mach_mips16000 = 16000,
3981 mach_mips16 = 16,
3982 mach_mips5 = 5,
3983 mach_mips_loongson_2e = 3001,
3984 mach_mips_loongson_2f = 3002,
3985 mach_mips_loongson_3a = 3003,
3986 mach_mips_sb1 = 12310201, // octal 'SB', 01
3987 mach_mips_octeon = 6501,
3988 mach_mips_octeonp = 6601,
3989 mach_mips_octeon2 = 6502,
b52717c0 3990 mach_mips_octeon3 = 6503,
9810d34d
SS
3991 mach_mips_xlr = 887682, // decimal 'XLR'
3992 mach_mipsisa32 = 32,
3993 mach_mipsisa32r2 = 33,
b52717c0
VR
3994 mach_mipsisa32r3 = 34,
3995 mach_mipsisa32r5 = 36,
f5b11759 3996 mach_mipsisa32r6 = 37,
9810d34d
SS
3997 mach_mipsisa64 = 64,
3998 mach_mipsisa64r2 = 65,
b52717c0
VR
3999 mach_mipsisa64r3 = 66,
4000 mach_mipsisa64r5 = 68,
f5b11759 4001 mach_mipsisa64r6 = 69,
9810d34d
SS
4002 mach_mips_micromips = 96
4003 };
4004
4005 // Return the MACH for a MIPS e_flags value.
4006 unsigned int
4007 elf_mips_mach(elfcpp::Elf_Word);
4008
b52717c0
VR
4009 // Return the MACH for each .MIPS.abiflags ISA Extension.
4010 unsigned int
4011 mips_isa_ext_mach(unsigned int);
4012
4013 // Return the .MIPS.abiflags value representing each ISA Extension.
4014 unsigned int
4015 mips_isa_ext(unsigned int);
4016
4017 // Update the isa_level, isa_rev, isa_ext fields of abiflags.
4018 void
4019 update_abiflags_isa(const std::string&, elfcpp::Elf_Word,
4020 Mips_abiflags<big_endian>*);
4021
4022 // Infer the content of the ABI flags based on the elf header.
4023 void
4024 infer_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4025
4026 // Create abiflags from elf header or from .MIPS.abiflags section.
4027 void
4028 create_abiflags(Mips_relobj<size, big_endian>*, Mips_abiflags<big_endian>*);
4029
4030 // Return the meaning of fp_abi, or "unknown" if not known.
4031 const char*
4032 fp_abi_string(int);
4033
4034 // Select fp_abi.
4035 int
4036 select_fp_abi(const std::string&, int, int);
4037
4038 // Merge attributes from input object.
4039 void
4040 merge_obj_attributes(const std::string&, const Attributes_section_data*);
4041
4042 // Merge abiflags from input object.
4043 void
4044 merge_obj_abiflags(const std::string&, Mips_abiflags<big_endian>*);
4045
9810d34d
SS
4046 // Check whether machine EXTENSION is an extension of machine BASE.
4047 bool
4048 mips_mach_extends(unsigned int, unsigned int);
4049
b52717c0 4050 // Merge file header flags from input object.
9810d34d 4051 void
b52717c0
VR
4052 merge_obj_e_flags(const std::string&, elfcpp::Elf_Word);
4053
4054 // Encode ISA level and revision as a single value.
4055 int
4056 level_rev(unsigned char isa_level, unsigned char isa_rev) const
4057 { return (isa_level << 3) | isa_rev; }
9810d34d
SS
4058
4059 // True if we are linking for CPUs that are faster if JAL is converted to BAL.
4060 static inline bool
4061 jal_to_bal()
4062 { return false; }
4063
4064 // True if we are linking for CPUs that are faster if JALR is converted to
4065 // BAL. This should be safe for all architectures. We enable this predicate
4066 // for all CPUs.
4067 static inline bool
4068 jalr_to_bal()
4069 { return true; }
4070
4071 // True if we are linking for CPUs that are faster if JR is converted to B.
4072 // This should be safe for all architectures. We enable this predicate for
4073 // all CPUs.
4074 static inline bool
4075 jr_to_b()
4076 { return true; }
4077
4078 // Return the size of the GOT section.
4079 section_size_type
4080 got_size() const
4081 {
4082 gold_assert(this->got_ != NULL);
4083 return this->got_->data_size();
4084 }
4085
4086 // Create a PLT entry for a global symbol referenced by r_type relocation.
4087 void
4088 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
4089 unsigned int r_type);
4090
4091 // Get the PLT section.
4092 Mips_output_data_plt<size, big_endian>*
4093 plt_section() const
4094 {
4095 gold_assert(this->plt_ != NULL);
4096 return this->plt_;
4097 }
4098
4099 // Get the GOT PLT section.
4100 const Mips_output_data_plt<size, big_endian>*
4101 got_plt_section() const
4102 {
4103 gold_assert(this->got_plt_ != NULL);
4104 return this->got_plt_;
4105 }
4106
4107 // Copy a relocation against a global symbol.
4108 void
4109 copy_reloc(Symbol_table* symtab, Layout* layout,
4110 Sized_relobj_file<size, big_endian>* object,
4111 unsigned int shndx, Output_section* output_section,
47a9f4fc 4112 Symbol* sym, unsigned int r_type, Mips_address r_offset)
9810d34d
SS
4113 {
4114 this->copy_relocs_.copy_reloc(symtab, layout,
4115 symtab->get_sized_symbol<size>(sym),
4116 object, shndx, output_section,
47a9f4fc 4117 r_type, r_offset, 0,
859d7987 4118 this->rel_dyn_section(layout));
9810d34d
SS
4119 }
4120
4121 void
4122 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
4123 Mips_relobj<size, big_endian>* relobj,
4124 unsigned int shndx, Output_section* output_section,
4125 Mips_address r_offset)
4126 {
4127 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
4128 output_section, r_offset));
4129 }
4130
4131 // Calculate value of _gp symbol.
4132 void
4133 set_gp(Layout*, Symbol_table*);
4134
4135 const char*
01b84e25 4136 elf_mips_abi_name(elfcpp::Elf_Word e_flags);
9810d34d
SS
4137 const char*
4138 elf_mips_mach_name(elfcpp::Elf_Word e_flags);
4139
4140 // Adds entries that describe how machines relate to one another. The entries
4141 // are ordered topologically with MIPS I extensions listed last. First
4142 // element is extension, second element is base.
4143 void
4144 add_machine_extensions()
4145 {
4146 // MIPS64r2 extensions.
b52717c0 4147 this->add_extension(mach_mips_octeon3, mach_mips_octeon2);
9810d34d
SS
4148 this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
4149 this->add_extension(mach_mips_octeonp, mach_mips_octeon);
4150 this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
b52717c0 4151 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64r2);
9810d34d
SS
4152
4153 // MIPS64 extensions.
4154 this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
4155 this->add_extension(mach_mips_sb1, mach_mipsisa64);
4156 this->add_extension(mach_mips_xlr, mach_mipsisa64);
9810d34d
SS
4157
4158 // MIPS V extensions.
4159 this->add_extension(mach_mipsisa64, mach_mips5);
4160
4161 // R10000 extensions.
4162 this->add_extension(mach_mips12000, mach_mips10000);
4163 this->add_extension(mach_mips14000, mach_mips10000);
4164 this->add_extension(mach_mips16000, mach_mips10000);
4165
4166 // R5000 extensions. Note: the vr5500 ISA is an extension of the core
4167 // vr5400 ISA, but doesn't include the multimedia stuff. It seems
4168 // better to allow vr5400 and vr5500 code to be merged anyway, since
4169 // many libraries will just use the core ISA. Perhaps we could add
4170 // some sort of ASE flag if this ever proves a problem.
4171 this->add_extension(mach_mips5500, mach_mips5400);
4172 this->add_extension(mach_mips5400, mach_mips5000);
4173
4174 // MIPS IV extensions.
4175 this->add_extension(mach_mips5, mach_mips8000);
4176 this->add_extension(mach_mips10000, mach_mips8000);
4177 this->add_extension(mach_mips5000, mach_mips8000);
4178 this->add_extension(mach_mips7000, mach_mips8000);
4179 this->add_extension(mach_mips9000, mach_mips8000);
4180
4181 // VR4100 extensions.
4182 this->add_extension(mach_mips4120, mach_mips4100);
4183 this->add_extension(mach_mips4111, mach_mips4100);
4184
4185 // MIPS III extensions.
4186 this->add_extension(mach_mips_loongson_2e, mach_mips4000);
4187 this->add_extension(mach_mips_loongson_2f, mach_mips4000);
4188 this->add_extension(mach_mips8000, mach_mips4000);
4189 this->add_extension(mach_mips4650, mach_mips4000);
4190 this->add_extension(mach_mips4600, mach_mips4000);
4191 this->add_extension(mach_mips4400, mach_mips4000);
4192 this->add_extension(mach_mips4300, mach_mips4000);
4193 this->add_extension(mach_mips4100, mach_mips4000);
4194 this->add_extension(mach_mips4010, mach_mips4000);
b52717c0 4195 this->add_extension(mach_mips5900, mach_mips4000);
9810d34d
SS
4196
4197 // MIPS32 extensions.
4198 this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
4199
4200 // MIPS II extensions.
4201 this->add_extension(mach_mips4000, mach_mips6000);
4202 this->add_extension(mach_mipsisa32, mach_mips6000);
4203
4204 // MIPS I extensions.
4205 this->add_extension(mach_mips6000, mach_mips3000);
4206 this->add_extension(mach_mips3900, mach_mips3000);
4207 }
4208
4209 // Add value to MIPS extenstions.
4210 void
4211 add_extension(unsigned int base, unsigned int extension)
4212 {
4213 std::pair<unsigned int, unsigned int> ext(base, extension);
4214 this->mips_mach_extensions_.push_back(ext);
4215 }
4216
4217 // Return the number of entries in the .dynsym section.
4218 unsigned int get_dt_mips_symtabno() const
4219 {
4220 return ((unsigned int)(this->layout_->dynsym_section()->data_size()
4221 / elfcpp::Elf_sizes<size>::sym_size));
4222 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE.
4223 }
4224
4225 // Information about this specific target which we pass to the
4226 // general Target structure.
62661c93 4227 static const Target::Target_info mips_info;
9810d34d
SS
4228 // The GOT section.
4229 Mips_output_data_got<size, big_endian>* got_;
4230 // gp symbol. It has the value of .got + 0x7FF0.
4231 Sized_symbol<size>* gp_;
4232 // The PLT section.
4233 Mips_output_data_plt<size, big_endian>* plt_;
4234 // The GOT PLT section.
4235 Output_data_space* got_plt_;
4236 // The dynamic reloc section.
4237 Reloc_section* rel_dyn_;
a8ecc9fe
VR
4238 // The .rld_map section.
4239 Output_data_zero_fill* rld_map_;
9810d34d
SS
4240 // Relocs saved to avoid a COPY reloc.
4241 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
4242
4243 // A list of dyn relocs to be saved.
4244 std::vector<Dyn_reloc> dyn_relocs_;
4245
4246 // The LA25 stub section.
4247 Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
4248 // Architecture extensions.
4249 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
4250 // .MIPS.stubs
4251 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
4252
b52717c0
VR
4253 // Attributes section data in output.
4254 Attributes_section_data* attributes_section_data_;
4255 // .MIPS.abiflags section data in output.
4256 Mips_abiflags<big_endian>* abiflags_;
4257
9810d34d
SS
4258 unsigned int mach_;
4259 Layout* layout_;
4260
4261 typename std::list<got16_addend<size, big_endian> > got16_addends_;
4262
b52717c0
VR
4263 // Whether there is an input .MIPS.abiflags section.
4264 bool has_abiflags_section_;
4265
9810d34d
SS
4266 // Whether the entry symbol is mips16 or micromips.
4267 bool entry_symbol_is_compressed_;
4268
4269 // Whether we can use only 32-bit microMIPS instructions.
4270 // TODO(sasa): This should be a linker option.
4271 bool insn32_;
4272};
4273
9810d34d
SS
4274// Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
4275// It records high part of the relocation pair.
4276
4277template<int size, bool big_endian>
4278struct reloc_high
4279{
4280 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4281
4282 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
4283 const Symbol_value<size>* _psymval, Mips_address _addend,
3d0064a9 4284 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend,
9810d34d
SS
4285 Mips_address _address = 0, bool _gp_disp = false)
4286 : view(_view), object(_object), psymval(_psymval), addend(_addend),
3d0064a9
CC
4287 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend),
4288 address(_address), gp_disp(_gp_disp)
9810d34d
SS
4289 { }
4290
4291 unsigned char* view;
4292 const Mips_relobj<size, big_endian>* object;
4293 const Symbol_value<size>* psymval;
4294 Mips_address addend;
4295 unsigned int r_type;
3d0064a9 4296 unsigned int r_sym;
9810d34d
SS
4297 bool extract_addend;
4298 Mips_address address;
4299 bool gp_disp;
4300};
4301
4302template<int size, bool big_endian>
4303class Mips_relocate_functions : public Relocate_functions<size, big_endian>
4304{
4305 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
47a9f4fc 4306 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
9810d34d
SS
4307 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
4308 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
47a9f4fc 4309 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype64;
9810d34d
SS
4310
4311 public:
4312 typedef enum
4313 {
f5b11759
VR
4314 STATUS_OKAY, // No error during relocation.
4315 STATUS_OVERFLOW, // Relocation overflow.
4316 STATUS_BAD_RELOC, // Relocation cannot be applied.
4317 STATUS_PCREL_UNALIGNED // Unaligned PC-relative relocation.
9810d34d
SS
4318 } Status;
4319
4320 private:
4321 typedef Relocate_functions<size, big_endian> Base;
4322 typedef Mips_relocate_functions<size, big_endian> This;
4323
4324 static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
4325 static typename std::list<reloc_high<size, big_endian> > got16_relocs;
f5b11759 4326 static typename std::list<reloc_high<size, big_endian> > pchi16_relocs;
9810d34d 4327
47a9f4fc
VR
4328 template<int valsize>
4329 static inline typename This::Status
4330 check_overflow(Valtype value)
4331 {
4332 if (size == 32)
4333 return (Bits<valsize>::has_overflow32(value)
4334 ? This::STATUS_OVERFLOW
4335 : This::STATUS_OKAY);
4336
4337 return (Bits<valsize>::has_overflow(value)
4338 ? This::STATUS_OVERFLOW
4339 : This::STATUS_OKAY);
4340 }
4341
4342 static inline bool
4343 should_shuffle_micromips_reloc(unsigned int r_type)
4344 {
4345 return (micromips_reloc(r_type)
4346 && r_type != elfcpp::R_MICROMIPS_PC7_S1
4347 && r_type != elfcpp::R_MICROMIPS_PC10_S1);
4348 }
4349
4350 public:
9810d34d
SS
4351 // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4352 // Most mips16 instructions are 16 bits, but these instructions
4353 // are 32 bits.
4354 //
4355 // The format of these instructions is:
4356 //
4357 // +--------------+--------------------------------+
4358 // | JALX | X| Imm 20:16 | Imm 25:21 |
4359 // +--------------+--------------------------------+
4360 // | Immediate 15:0 |
4361 // +-----------------------------------------------+
4362 //
4363 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
4364 // Note that the immediate value in the first word is swapped.
4365 //
4366 // When producing a relocatable object file, R_MIPS16_26 is
4367 // handled mostly like R_MIPS_26. In particular, the addend is
4368 // stored as a straight 26-bit value in a 32-bit instruction.
4369 // (gas makes life simpler for itself by never adjusting a
4370 // R_MIPS16_26 reloc to be against a section, so the addend is
4371 // always zero). However, the 32 bit instruction is stored as 2
4372 // 16-bit values, rather than a single 32-bit value. In a
4373 // big-endian file, the result is the same; in a little-endian
4374 // file, the two 16-bit halves of the 32 bit value are swapped.
4375 // This is so that a disassembler can recognize the jal
4376 // instruction.
4377 //
4378 // When doing a final link, R_MIPS16_26 is treated as a 32 bit
4379 // instruction stored as two 16-bit values. The addend A is the
4380 // contents of the targ26 field. The calculation is the same as
4381 // R_MIPS_26. When storing the calculated value, reorder the
4382 // immediate value as shown above, and don't forget to store the
4383 // value as two 16-bit values.
4384 //
4385 // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4386 // defined as
4387 //
4388 // big-endian:
4389 // +--------+----------------------+
4390 // | | |
4391 // | | targ26-16 |
4392 // |31 26|25 0|
4393 // +--------+----------------------+
4394 //
4395 // little-endian:
4396 // +----------+------+-------------+
4397 // | | | |
4398 // | sub1 | | sub2 |
4399 // |0 9|10 15|16 31|
4400 // +----------+--------------------+
4401 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4402 // ((sub1 << 16) | sub2)).
4403 //
4404 // When producing a relocatable object file, the calculation is
4405 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4406 // When producing a fully linked file, the calculation is
4407 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4408 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
4409 //
4410 // The table below lists the other MIPS16 instruction relocations.
4411 // Each one is calculated in the same way as the non-MIPS16 relocation
4412 // given on the right, but using the extended MIPS16 layout of 16-bit
4413 // immediate fields:
4414 //
4415 // R_MIPS16_GPREL R_MIPS_GPREL16
4416 // R_MIPS16_GOT16 R_MIPS_GOT16
4417 // R_MIPS16_CALL16 R_MIPS_CALL16
4418 // R_MIPS16_HI16 R_MIPS_HI16
4419 // R_MIPS16_LO16 R_MIPS_LO16
4420 //
4421 // A typical instruction will have a format like this:
4422 //
4423 // +--------------+--------------------------------+
4424 // | EXTEND | Imm 10:5 | Imm 15:11 |
4425 // +--------------+--------------------------------+
4426 // | Major | rx | ry | Imm 4:0 |
4427 // +--------------+--------------------------------+
4428 //
4429 // EXTEND is the five bit value 11110. Major is the instruction
4430 // opcode.
4431 //
4432 // All we need to do here is shuffle the bits appropriately.
4433 // As above, the two 16-bit halves must be swapped on a
4434 // little-endian system.
4435
4436 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
4437 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
4438 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
4439
9810d34d
SS
4440 static void
4441 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
4442 bool jal_shuffle)
4443 {
4444 if (!mips16_reloc(r_type)
4445 && !should_shuffle_micromips_reloc(r_type))
4446 return;
4447
4448 // Pick up the first and second halfwords of the instruction.
4449 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
4450 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
4451 Valtype32 val;
4452
4453 if (micromips_reloc(r_type)
4454 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4455 val = first << 16 | second;
4456 else if (r_type != elfcpp::R_MIPS16_26)
4457 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
4458 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
4459 else
4460 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
4461 | ((first & 0x1f) << 21) | second);
4462
4463 elfcpp::Swap<32, big_endian>::writeval(view, val);
4464 }
4465
4466 static void
4467 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
4468 {
4469 if (!mips16_reloc(r_type)
4470 && !should_shuffle_micromips_reloc(r_type))
4471 return;
4472
4473 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
4474 Valtype16 first, second;
4475
4476 if (micromips_reloc(r_type)
4477 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
4478 {
4479 second = val & 0xffff;
4480 first = val >> 16;
4481 }
4482 else if (r_type != elfcpp::R_MIPS16_26)
4483 {
4484 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
4485 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
4486 }
4487 else
4488 {
4489 second = val & 0xffff;
4490 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
4491 | ((val >> 21) & 0x1f);
4492 }
4493
4494 elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
4495 elfcpp::Swap<16, big_endian>::writeval(view, first);
4496 }
4497
9810d34d
SS
4498 // R_MIPS_16: S + sign-extend(A)
4499 static inline typename This::Status
4500 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4501 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc 4502 bool extract_addend, bool calculate_only, Valtype* calculated_value)
9810d34d 4503 {
9810d34d
SS
4504 Valtype16* wv = reinterpret_cast<Valtype16*>(view);
4505 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
4506
47a9f4fc
VR
4507 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val)
4508 : addend_a);
9810d34d 4509
47a9f4fc 4510 Valtype x = psymval->value(object, addend);
9810d34d 4511 val = Bits<16>::bit_select32(val, x, 0xffffU);
47a9f4fc
VR
4512
4513 if (calculate_only)
4514 {
4515 *calculated_value = x;
4516 return This::STATUS_OKAY;
4517 }
4518 else
4519 elfcpp::Swap<16, big_endian>::writeval(wv, val);
4520
4521 return check_overflow<16>(x);
9810d34d
SS
4522 }
4523
4524 // R_MIPS_32: S + A
4525 static inline typename This::Status
4526 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4527 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc 4528 bool extract_addend, bool calculate_only, Valtype* calculated_value)
9810d34d 4529 {
9810d34d 4530 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
47a9f4fc 4531 Valtype addend = (extract_addend
9810d34d 4532 ? elfcpp::Swap<32, big_endian>::readval(wv)
47a9f4fc
VR
4533 : addend_a);
4534 Valtype x = psymval->value(object, addend);
4535
4536 if (calculate_only)
4537 *calculated_value = x;
4538 else
4539 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4540
9810d34d
SS
4541 return This::STATUS_OKAY;
4542 }
4543
4544 // R_MIPS_JALR, R_MICROMIPS_JALR
4545 static inline typename This::Status
4546 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4547 const Symbol_value<size>* psymval, Mips_address address,
4548 Mips_address addend_a, bool extract_addend, bool cross_mode_jump,
47a9f4fc
VR
4549 unsigned int r_type, bool jalr_to_bal, bool jr_to_b,
4550 bool calculate_only, Valtype* calculated_value)
9810d34d 4551 {
9810d34d 4552 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
47a9f4fc 4553 Valtype addend = extract_addend ? 0 : addend_a;
9810d34d
SS
4554 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4555
4556 // Try converting J(AL)R to B(AL), if the target is in range.
1e1247c8 4557 if (r_type == elfcpp::R_MIPS_JALR
9810d34d
SS
4558 && !cross_mode_jump
4559 && ((jalr_to_bal && val == 0x0320f809) // jalr t9
4560 || (jr_to_b && val == 0x03200008))) // jr t9
4561 {
4562 int offset = psymval->value(object, addend) - (address + 4);
4563 if (!Bits<18>::has_overflow32(offset))
4564 {
4565 if (val == 0x03200008) // jr t9
4566 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4567 else
4568 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4569 }
4570 }
4571
47a9f4fc
VR
4572 if (calculate_only)
4573 *calculated_value = val;
4574 else
4575 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4576
9810d34d
SS
4577 return This::STATUS_OKAY;
4578 }
4579
4580 // R_MIPS_PC32: S + A - P
4581 static inline typename This::Status
4582 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4583 const Symbol_value<size>* psymval, Mips_address address,
47a9f4fc
VR
4584 Mips_address addend_a, bool extract_addend, bool calculate_only,
4585 Valtype* calculated_value)
9810d34d 4586 {
9810d34d 4587 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
47a9f4fc 4588 Valtype addend = (extract_addend
9810d34d 4589 ? elfcpp::Swap<32, big_endian>::readval(wv)
47a9f4fc
VR
4590 : addend_a);
4591 Valtype x = psymval->value(object, addend) - address;
4592
4593 if (calculate_only)
4594 *calculated_value = x;
4595 else
4596 elfcpp::Swap<32, big_endian>::writeval(wv, x);
4597
9810d34d
SS
4598 return This::STATUS_OKAY;
4599 }
4600
4601 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
4602 static inline typename This::Status
4603 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4604 const Symbol_value<size>* psymval, Mips_address address,
4605 bool local, Mips_address addend_a, bool extract_addend,
4606 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
47a9f4fc 4607 bool jal_to_bal, bool calculate_only, Valtype* calculated_value)
9810d34d 4608 {
9810d34d
SS
4609 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4610 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4611
47a9f4fc 4612 Valtype addend;
9810d34d
SS
4613 if (extract_addend)
4614 {
4615 if (r_type == elfcpp::R_MICROMIPS_26_S1)
4616 addend = (val & 0x03ffffff) << 1;
4617 else
4618 addend = (val & 0x03ffffff) << 2;
4619 }
4620 else
4621 addend = addend_a;
4622
4623 // Make sure the target of JALX is word-aligned. Bit 0 must be
4624 // the correct ISA mode selector and bit 1 must be 0.
47a9f4fc 4625 if (!calculate_only && cross_mode_jump
9810d34d
SS
4626 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
4627 {
4628 gold_warning(_("JALX to a non-word-aligned address"));
9810d34d
SS
4629 return This::STATUS_BAD_RELOC;
4630 }
4631
4632 // Shift is 2, unusually, for microMIPS JALX.
4633 unsigned int shift =
4634 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
4635
47a9f4fc 4636 Valtype x;
9810d34d
SS
4637 if (local)
4638 x = addend | ((address + 4) & (0xfc000000 << shift));
4639 else
4640 {
4641 if (shift == 1)
4642 x = Bits<27>::sign_extend32(addend);
4643 else
4644 x = Bits<28>::sign_extend32(addend);
4645 }
4646 x = psymval->value(object, x) >> shift;
4647
c3847462
VR
4648 if (!calculate_only && !local && !gsym->is_weak_undefined()
4649 && ((x >> 26) != ((address + 4) >> (26 + shift))))
4650 return This::STATUS_OVERFLOW;
9810d34d
SS
4651
4652 val = Bits<32>::bit_select32(val, x, 0x03ffffff);
4653
4654 // If required, turn JAL into JALX.
4655 if (cross_mode_jump)
4656 {
4657 bool ok;
4658 Valtype32 opcode = val >> 26;
4659 Valtype32 jalx_opcode;
4660
4661 // Check to see if the opcode is already JAL or JALX.
4662 if (r_type == elfcpp::R_MIPS16_26)
4663 {
4664 ok = (opcode == 0x6) || (opcode == 0x7);
4665 jalx_opcode = 0x7;
4666 }
4667 else if (r_type == elfcpp::R_MICROMIPS_26_S1)
4668 {
4669 ok = (opcode == 0x3d) || (opcode == 0x3c);
4670 jalx_opcode = 0x3c;
4671 }
4672 else
4673 {
4674 ok = (opcode == 0x3) || (opcode == 0x1d);
4675 jalx_opcode = 0x1d;
4676 }
4677
4678 // If the opcode is not JAL or JALX, there's a problem. We cannot
4679 // convert J or JALS to JALX.
47a9f4fc 4680 if (!calculate_only && !ok)
9810d34d
SS
4681 {
4682 gold_error(_("Unsupported jump between ISA modes; consider "
4683 "recompiling with interlinking enabled."));
4684 return This::STATUS_BAD_RELOC;
4685 }
4686
4687 // Make this the JALX opcode.
4688 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
4689 }
4690
4691 // Try converting JAL to BAL, if the target is in range.
4692 if (!parameters->options().relocatable()
4693 && !cross_mode_jump
4694 && ((jal_to_bal
4695 && r_type == elfcpp::R_MIPS_26
4696 && (val >> 26) == 0x3))) // jal addr
4697 {
4698 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
4699 int offset = dest - (address + 4);
4700 if (!Bits<18>::has_overflow32(offset))
4701 {
4702 if (val == 0x03200008) // jr t9
4703 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
4704 else
4705 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
4706 }
4707 }
4708
47a9f4fc
VR
4709 if (calculate_only)
4710 *calculated_value = val;
4711 else
4712 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4713
9810d34d
SS
4714 return This::STATUS_OKAY;
4715 }
4716
4717 // R_MIPS_PC16
4718 static inline typename This::Status
4719 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4720 const Symbol_value<size>* psymval, Mips_address address,
47a9f4fc
VR
4721 Mips_address addend_a, bool extract_addend, bool calculate_only,
4722 Valtype* calculated_value)
9810d34d 4723 {
9810d34d
SS
4724 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4725 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4726
47a9f4fc
VR
4727 Valtype addend = (extract_addend
4728 ? Bits<18>::sign_extend32((val & 0xffff) << 2)
4729 : addend_a);
9810d34d 4730
47a9f4fc 4731 Valtype x = psymval->value(object, addend) - address;
9810d34d 4732 val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
47a9f4fc
VR
4733
4734 if (calculate_only)
4735 {
4736 *calculated_value = x >> 2;
4737 return This::STATUS_OKAY;
4738 }
4739 else
4740 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4741
beceef50
VR
4742 if (psymval->value(object, addend) & 3)
4743 return This::STATUS_PCREL_UNALIGNED;
4744
47a9f4fc 4745 return check_overflow<18>(x);
9810d34d
SS
4746 }
4747
f5b11759
VR
4748 // R_MIPS_PC21_S2
4749 static inline typename This::Status
4750 relpc21(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4751 const Symbol_value<size>* psymval, Mips_address address,
4752 Mips_address addend_a, bool extract_addend, bool calculate_only,
4753 Valtype* calculated_value)
4754 {
4755 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4756 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4757
4758 Valtype addend = (extract_addend
4759 ? Bits<23>::sign_extend32((val & 0x1fffff) << 2)
4760 : addend_a);
4761
4762 Valtype x = psymval->value(object, addend) - address;
4763 val = Bits<21>::bit_select32(val, x >> 2, 0x1fffff);
4764
4765 if (calculate_only)
4766 {
4767 *calculated_value = x >> 2;
4768 return This::STATUS_OKAY;
4769 }
4770 else
4771 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4772
4773 if (psymval->value(object, addend) & 3)
4774 return This::STATUS_PCREL_UNALIGNED;
4775
4776 return check_overflow<23>(x);
4777 }
4778
4779 // R_MIPS_PC26_S2
4780 static inline typename This::Status
4781 relpc26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4782 const Symbol_value<size>* psymval, Mips_address address,
4783 Mips_address addend_a, bool extract_addend, bool calculate_only,
4784 Valtype* calculated_value)
4785 {
4786 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4787 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4788
4789 Valtype addend = (extract_addend
4790 ? Bits<28>::sign_extend32((val & 0x3ffffff) << 2)
4791 : addend_a);
4792
4793 Valtype x = psymval->value(object, addend) - address;
4794 val = Bits<26>::bit_select32(val, x >> 2, 0x3ffffff);
4795
4796 if (calculate_only)
4797 {
4798 *calculated_value = x >> 2;
4799 return This::STATUS_OKAY;
4800 }
4801 else
4802 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4803
4804 if (psymval->value(object, addend) & 3)
4805 return This::STATUS_PCREL_UNALIGNED;
4806
4807 return check_overflow<28>(x);
4808 }
4809
4810 // R_MIPS_PC18_S3
4811 static inline typename This::Status
4812 relpc18(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4813 const Symbol_value<size>* psymval, Mips_address address,
4814 Mips_address addend_a, bool extract_addend, bool calculate_only,
4815 Valtype* calculated_value)
4816 {
4817 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4818 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4819
4820 Valtype addend = (extract_addend
4821 ? Bits<21>::sign_extend32((val & 0x3ffff) << 3)
4822 : addend_a);
4823
4824 Valtype x = psymval->value(object, addend) - ((address | 7) ^ 7);
4825 val = Bits<18>::bit_select32(val, x >> 3, 0x3ffff);
4826
4827 if (calculate_only)
4828 {
4829 *calculated_value = x >> 3;
4830 return This::STATUS_OKAY;
4831 }
4832 else
4833 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4834
4835 if (psymval->value(object, addend) & 7)
4836 return This::STATUS_PCREL_UNALIGNED;
4837
4838 return check_overflow<21>(x);
4839 }
4840
4841 // R_MIPS_PC19_S2
4842 static inline typename This::Status
4843 relpc19(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4844 const Symbol_value<size>* psymval, Mips_address address,
4845 Mips_address addend_a, bool extract_addend, bool calculate_only,
4846 Valtype* calculated_value)
4847 {
4848 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4849 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4850
4851 Valtype addend = (extract_addend
4852 ? Bits<21>::sign_extend32((val & 0x7ffff) << 2)
4853 : addend_a);
4854
4855 Valtype x = psymval->value(object, addend) - address;
4856 val = Bits<19>::bit_select32(val, x >> 2, 0x7ffff);
4857
4858 if (calculate_only)
4859 {
4860 *calculated_value = x >> 2;
4861 return This::STATUS_OKAY;
4862 }
4863 else
4864 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4865
4866 if (psymval->value(object, addend) & 3)
4867 return This::STATUS_PCREL_UNALIGNED;
4868
4869 return check_overflow<21>(x);
4870 }
4871
4872 // R_MIPS_PCHI16
4873 static inline typename This::Status
4874 relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4875 const Symbol_value<size>* psymval, Mips_address addend,
4876 Mips_address address, unsigned int r_sym, bool extract_addend)
4877 {
4878 // Record the relocation. It will be resolved when we find pclo16 part.
4879 pchi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
4880 addend, 0, r_sym, extract_addend, address));
4881 return This::STATUS_OKAY;
4882 }
4883
4884 // R_MIPS_PCHI16
4885 static inline typename This::Status
4886 do_relpchi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4887 const Symbol_value<size>* psymval, Mips_address addend_hi,
4888 Mips_address address, bool extract_addend, Valtype32 addend_lo,
4889 bool calculate_only, Valtype* calculated_value)
4890 {
4891 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4892 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4893
4894 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
4895 : addend_hi);
4896
4897 Valtype value = psymval->value(object, addend) - address;
4898 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
4899 val = Bits<32>::bit_select32(val, x, 0xffff);
4900
4901 if (calculate_only)
4902 *calculated_value = x;
4903 else
4904 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4905
4906 return This::STATUS_OKAY;
4907 }
4908
4909 // R_MIPS_PCLO16
4910 static inline typename This::Status
4911 relpclo16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
4912 const Symbol_value<size>* psymval, Mips_address addend_a,
4913 bool extract_addend, Mips_address address, unsigned int r_sym,
4914 unsigned int rel_type, bool calculate_only,
4915 Valtype* calculated_value)
4916 {
4917 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4918 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4919
4920 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
4921 : addend_a);
4922
4923 if (rel_type == elfcpp::SHT_REL)
4924 {
4925 // Resolve pending R_MIPS_PCHI16 relocations.
4926 typename std::list<reloc_high<size, big_endian> >::iterator it =
4927 pchi16_relocs.begin();
4928 while (it != pchi16_relocs.end())
4929 {
4930 reloc_high<size, big_endian> pchi16 = *it;
4931 if (pchi16.r_sym == r_sym)
4932 {
4933 do_relpchi16(pchi16.view, pchi16.object, pchi16.psymval,
4934 pchi16.addend, pchi16.address,
4935 pchi16.extract_addend, addend, calculate_only,
4936 calculated_value);
4937 it = pchi16_relocs.erase(it);
4938 }
4939 else
4940 ++it;
4941 }
4942 }
4943
4944 // Resolve R_MIPS_PCLO16 relocation.
4945 Valtype x = psymval->value(object, addend) - address;
4946 val = Bits<32>::bit_select32(val, x, 0xffff);
4947
4948 if (calculate_only)
4949 *calculated_value = x;
4950 else
4951 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4952
4953 return This::STATUS_OKAY;
4954 }
4955
9810d34d
SS
4956 // R_MICROMIPS_PC7_S1
4957 static inline typename This::Status
4958 relmicromips_pc7_s1(unsigned char* view,
4959 const Mips_relobj<size, big_endian>* object,
4960 const Symbol_value<size>* psymval, Mips_address address,
4961 Mips_address addend_a, bool extract_addend,
47a9f4fc 4962 bool calculate_only, Valtype* calculated_value)
9810d34d 4963 {
9810d34d
SS
4964 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4965 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4966
47a9f4fc
VR
4967 Valtype addend = extract_addend ? Bits<8>::sign_extend32((val & 0x7f) << 1)
4968 : addend_a;
9810d34d 4969
47a9f4fc 4970 Valtype x = psymval->value(object, addend) - address;
9810d34d 4971 val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
47a9f4fc
VR
4972
4973 if (calculate_only)
4974 {
4975 *calculated_value = x >> 1;
4976 return This::STATUS_OKAY;
4977 }
4978 else
4979 elfcpp::Swap<32, big_endian>::writeval(wv, val);
4980
4981 return check_overflow<8>(x);
9810d34d
SS
4982 }
4983
4984 // R_MICROMIPS_PC10_S1
4985 static inline typename This::Status
4986 relmicromips_pc10_s1(unsigned char* view,
4987 const Mips_relobj<size, big_endian>* object,
4988 const Symbol_value<size>* psymval, Mips_address address,
4989 Mips_address addend_a, bool extract_addend,
47a9f4fc 4990 bool calculate_only, Valtype* calculated_value)
9810d34d 4991 {
9810d34d
SS
4992 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
4993 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
4994
47a9f4fc
VR
4995 Valtype addend = (extract_addend
4996 ? Bits<11>::sign_extend32((val & 0x3ff) << 1)
4997 : addend_a);
9810d34d 4998
47a9f4fc 4999 Valtype x = psymval->value(object, addend) - address;
9810d34d 5000 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
47a9f4fc
VR
5001
5002 if (calculate_only)
5003 {
5004 *calculated_value = x >> 1;
5005 return This::STATUS_OKAY;
5006 }
5007 else
5008 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5009
5010 return check_overflow<11>(x);
9810d34d
SS
5011 }
5012
5013 // R_MICROMIPS_PC16_S1
5014 static inline typename This::Status
5015 relmicromips_pc16_s1(unsigned char* view,
5016 const Mips_relobj<size, big_endian>* object,
5017 const Symbol_value<size>* psymval, Mips_address address,
5018 Mips_address addend_a, bool extract_addend,
47a9f4fc 5019 bool calculate_only, Valtype* calculated_value)
9810d34d 5020 {
9810d34d
SS
5021 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5022 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5023
47a9f4fc
VR
5024 Valtype addend = (extract_addend
5025 ? Bits<17>::sign_extend32((val & 0xffff) << 1)
5026 : addend_a);
9810d34d 5027
47a9f4fc 5028 Valtype x = psymval->value(object, addend) - address;
9810d34d 5029 val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
47a9f4fc
VR
5030
5031 if (calculate_only)
5032 {
5033 *calculated_value = x >> 1;
5034 return This::STATUS_OKAY;
5035 }
5036 else
5037 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5038
5039 return check_overflow<17>(x);
9810d34d
SS
5040 }
5041
5042 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5043 static inline typename This::Status
5044 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5045 const Symbol_value<size>* psymval, Mips_address addend,
5046 Mips_address address, bool gp_disp, unsigned int r_type,
3d0064a9 5047 unsigned int r_sym, bool extract_addend)
9810d34d
SS
5048 {
5049 // Record the relocation. It will be resolved when we find lo16 part.
5050 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
3d0064a9
CC
5051 addend, r_type, r_sym, extract_addend, address,
5052 gp_disp));
9810d34d
SS
5053 return This::STATUS_OKAY;
5054 }
5055
5056 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
5057 static inline typename This::Status
5058 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5059 const Symbol_value<size>* psymval, Mips_address addend_hi,
5060 Mips_address address, bool is_gp_disp, unsigned int r_type,
5061 bool extract_addend, Valtype32 addend_lo,
47a9f4fc
VR
5062 Target_mips<size, big_endian>* target, bool calculate_only,
5063 Valtype* calculated_value)
9810d34d 5064 {
9810d34d
SS
5065 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5066 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5067
47a9f4fc 5068 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
9810d34d
SS
5069 : addend_hi);
5070
5071 Valtype32 value;
5072 if (!is_gp_disp)
5073 value = psymval->value(object, addend);
5074 else
5075 {
5076 // For MIPS16 ABI code we generate this sequence
5077 // 0: li $v0,%hi(_gp_disp)
5078 // 4: addiupc $v1,%lo(_gp_disp)
5079 // 8: sll $v0,16
5080 // 12: addu $v0,$v1
5081 // 14: move $gp,$v0
5082 // So the offsets of hi and lo relocs are the same, but the
5083 // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5084 // ADDIUPC clears the low two bits of the instruction address,
5085 // so the base is ($t9 + 4) & ~3.
5086 Valtype32 gp_disp;
5087 if (r_type == elfcpp::R_MIPS16_HI16)
5088 gp_disp = (target->adjusted_gp_value(object)
5089 - ((address + 4) & ~0x3));
5090 // The microMIPS .cpload sequence uses the same assembly
5091 // instructions as the traditional psABI version, but the
5092 // incoming $t9 has the low bit set.
5093 else if (r_type == elfcpp::R_MICROMIPS_HI16)
5094 gp_disp = target->adjusted_gp_value(object) - address - 1;
5095 else
5096 gp_disp = target->adjusted_gp_value(object) - address;
5097 value = gp_disp + addend;
5098 }
47a9f4fc 5099 Valtype x = ((value + 0x8000) >> 16) & 0xffff;
9810d34d 5100 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5101
5102 if (calculate_only)
5103 {
5104 *calculated_value = x;
5105 return This::STATUS_OKAY;
5106 }
5107 else
5108 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5109
5110 return (is_gp_disp ? check_overflow<16>(x)
5111 : This::STATUS_OKAY);
9810d34d
SS
5112 }
5113
5114 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5115 static inline typename This::Status
5116 relgot16_local(unsigned char* view,
5117 const Mips_relobj<size, big_endian>* object,
5118 const Symbol_value<size>* psymval, Mips_address addend_a,
3d0064a9 5119 bool extract_addend, unsigned int r_type, unsigned int r_sym)
9810d34d
SS
5120 {
5121 // Record the relocation. It will be resolved when we find lo16 part.
5122 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
3d0064a9 5123 addend_a, r_type, r_sym, extract_addend));
9810d34d
SS
5124 return This::STATUS_OKAY;
5125 }
5126
5127 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5128 static inline typename This::Status
5129 do_relgot16_local(unsigned char* view,
5130 const Mips_relobj<size, big_endian>* object,
5131 const Symbol_value<size>* psymval, Mips_address addend_hi,
47a9f4fc
VR
5132 bool extract_addend, Valtype32 addend_lo,
5133 Target_mips<size, big_endian>* target, bool calculate_only,
5134 Valtype* calculated_value)
9810d34d 5135 {
9810d34d
SS
5136 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5137 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5138
47a9f4fc 5139 Valtype addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
9810d34d
SS
5140 : addend_hi);
5141
5142 // Find GOT page entry.
5143 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
5144 & 0xffff;
5145 value <<= 16;
5146 unsigned int got_offset =
5147 target->got_section()->get_got_page_offset(value, object);
5148
5149 // Resolve the relocation.
47a9f4fc 5150 Valtype x = target->got_section()->gp_offset(got_offset, object);
9810d34d 5151 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5152
5153 if (calculate_only)
5154 {
5155 *calculated_value = x;
5156 return This::STATUS_OKAY;
5157 }
5158 else
5159 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5160
5161 return check_overflow<16>(x);
9810d34d
SS
5162 }
5163
5164 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
5165 static inline typename This::Status
5166 rello16(Target_mips<size, big_endian>* target, unsigned char* view,
5167 const Mips_relobj<size, big_endian>* object,
5168 const Symbol_value<size>* psymval, Mips_address addend_a,
5169 bool extract_addend, Mips_address address, bool is_gp_disp,
47a9f4fc
VR
5170 unsigned int r_type, unsigned int r_sym, unsigned int rel_type,
5171 bool calculate_only, Valtype* calculated_value)
9810d34d 5172 {
9810d34d
SS
5173 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5174 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5175
47a9f4fc
VR
5176 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5177 : addend_a);
9810d34d 5178
47a9f4fc 5179 if (rel_type == elfcpp::SHT_REL)
9810d34d 5180 {
47a9f4fc
VR
5181 typename This::Status reloc_status = This::STATUS_OKAY;
5182 // Resolve pending R_MIPS_HI16 relocations.
5183 typename std::list<reloc_high<size, big_endian> >::iterator it =
5184 hi16_relocs.begin();
5185 while (it != hi16_relocs.end())
9810d34d 5186 {
47a9f4fc
VR
5187 reloc_high<size, big_endian> hi16 = *it;
5188 if (hi16.r_sym == r_sym
5189 && is_matching_lo16_reloc(hi16.r_type, r_type))
5190 {
5191 mips_reloc_unshuffle(hi16.view, hi16.r_type, false);
5192 reloc_status = do_relhi16(hi16.view, hi16.object, hi16.psymval,
5193 hi16.addend, hi16.address, hi16.gp_disp,
5194 hi16.r_type, hi16.extract_addend, addend,
5195 target, calculate_only, calculated_value);
5196 mips_reloc_shuffle(hi16.view, hi16.r_type, false);
5197 if (reloc_status == This::STATUS_OVERFLOW)
5198 return This::STATUS_OVERFLOW;
5199 it = hi16_relocs.erase(it);
5200 }
5201 else
5202 ++it;
9810d34d 5203 }
9810d34d 5204
47a9f4fc
VR
5205 // Resolve pending local R_MIPS_GOT16 relocations.
5206 typename std::list<reloc_high<size, big_endian> >::iterator it2 =
5207 got16_relocs.begin();
5208 while (it2 != got16_relocs.end())
9810d34d 5209 {
47a9f4fc
VR
5210 reloc_high<size, big_endian> got16 = *it2;
5211 if (got16.r_sym == r_sym
5212 && is_matching_lo16_reloc(got16.r_type, r_type))
5213 {
5214 mips_reloc_unshuffle(got16.view, got16.r_type, false);
5215
5216 reloc_status = do_relgot16_local(got16.view, got16.object,
5217 got16.psymval, got16.addend,
5218 got16.extract_addend, addend, target,
5219 calculate_only, calculated_value);
5220
5221 mips_reloc_shuffle(got16.view, got16.r_type, false);
5222 if (reloc_status == This::STATUS_OVERFLOW)
5223 return This::STATUS_OVERFLOW;
5224 it2 = got16_relocs.erase(it2);
5225 }
5226 else
5227 ++it2;
9810d34d 5228 }
9810d34d
SS
5229 }
5230
5231 // Resolve R_MIPS_LO16 relocation.
47a9f4fc 5232 Valtype x;
9810d34d
SS
5233 if (!is_gp_disp)
5234 x = psymval->value(object, addend);
5235 else
5236 {
5237 // See the comment for R_MIPS16_HI16 above for the reason
5238 // for this conditional.
5239 Valtype32 gp_disp;
5240 if (r_type == elfcpp::R_MIPS16_LO16)
5241 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
5242 else if (r_type == elfcpp::R_MICROMIPS_LO16
5243 || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
5244 gp_disp = target->adjusted_gp_value(object) - address + 3;
5245 else
5246 gp_disp = target->adjusted_gp_value(object) - address + 4;
5247 // The MIPS ABI requires checking the R_MIPS_LO16 relocation
5248 // for overflow. Relocations against _gp_disp are normally
5249 // generated from the .cpload pseudo-op. It generates code
5250 // that normally looks like this:
5251
5252 // lui $gp,%hi(_gp_disp)
5253 // addiu $gp,$gp,%lo(_gp_disp)
5254 // addu $gp,$gp,$t9
5255
5256 // Here $t9 holds the address of the function being called,
5257 // as required by the MIPS ELF ABI. The R_MIPS_LO16
5258 // relocation can easily overflow in this situation, but the
5259 // R_MIPS_HI16 relocation will handle the overflow.
5260 // Therefore, we consider this a bug in the MIPS ABI, and do
5261 // not check for overflow here.
5262 x = gp_disp + addend;
5263 }
5264 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5265
5266 if (calculate_only)
5267 *calculated_value = x;
5268 else
5269 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5270
9810d34d
SS
5271 return This::STATUS_OKAY;
5272 }
5273
5274 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
5275 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
5276 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
5277 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
5278 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
5279 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
5280 static inline typename This::Status
47a9f4fc
VR
5281 relgot(unsigned char* view, int gp_offset, bool calculate_only,
5282 Valtype* calculated_value)
9810d34d 5283 {
9810d34d
SS
5284 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5285 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5286 Valtype x = gp_offset;
9810d34d 5287 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5288
5289 if (calculate_only)
5290 {
5291 *calculated_value = x;
5292 return This::STATUS_OKAY;
5293 }
5294 else
5295 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5296
5297 return check_overflow<16>(x);
5298 }
5299
5300 // R_MIPS_EH
5301 static inline typename This::Status
5302 releh(unsigned char* view, int gp_offset, bool calculate_only,
5303 Valtype* calculated_value)
5304 {
5305 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5306 Valtype x = gp_offset;
5307
5308 if (calculate_only)
5309 {
5310 *calculated_value = x;
5311 return This::STATUS_OKAY;
5312 }
5313 else
5314 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5315
5316 return check_overflow<32>(x);
9810d34d
SS
5317 }
5318
5319 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
5320 static inline typename This::Status
5321 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
5322 const Mips_relobj<size, big_endian>* object,
5323 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc
VR
5324 bool extract_addend, bool calculate_only,
5325 Valtype* calculated_value)
9810d34d 5326 {
9810d34d
SS
5327 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5328 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
47a9f4fc 5329 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
5330
5331 // Find a GOT page entry that points to within 32KB of symbol + addend.
5332 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
5333 unsigned int got_offset =
5334 target->got_section()->get_got_page_offset(value, object);
5335
47a9f4fc 5336 Valtype x = target->got_section()->gp_offset(got_offset, object);
9810d34d 5337 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5338
5339 if (calculate_only)
5340 {
5341 *calculated_value = x;
5342 return This::STATUS_OKAY;
5343 }
5344 else
5345 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5346
5347 return check_overflow<16>(x);
9810d34d
SS
5348 }
5349
5350 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
5351 static inline typename This::Status
5352 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
5353 const Mips_relobj<size, big_endian>* object,
5354 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc
VR
5355 bool extract_addend, bool local, bool calculate_only,
5356 Valtype* calculated_value)
9810d34d 5357 {
9810d34d
SS
5358 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5359 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
47a9f4fc 5360 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
5361
5362 // For a local symbol, find a GOT page entry that points to within 32KB of
5363 // symbol + addend. Relocation value is the offset of the GOT page entry's
5364 // value from symbol + addend.
5365 // For a global symbol, relocation value is addend.
47a9f4fc 5366 Valtype x;
9810d34d
SS
5367 if (local)
5368 {
5369 // Find GOT page entry.
5370 Mips_address value = ((psymval->value(object, addend) + 0x8000)
5371 & ~0xffff);
5372 target->got_section()->get_got_page_offset(value, object);
5373
5374 x = psymval->value(object, addend) - value;
5375 }
5376 else
5377 x = addend;
5378 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5379
5380 if (calculate_only)
5381 {
5382 *calculated_value = x;
5383 return This::STATUS_OKAY;
5384 }
5385 else
5386 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5387
5388 return check_overflow<16>(x);
9810d34d
SS
5389 }
5390
5391 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
5392 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
5393 static inline typename This::Status
47a9f4fc
VR
5394 relgot_hi16(unsigned char* view, int gp_offset, bool calculate_only,
5395 Valtype* calculated_value)
9810d34d 5396 {
9810d34d
SS
5397 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5398 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5399 Valtype x = gp_offset;
9810d34d
SS
5400 x = ((x + 0x8000) >> 16) & 0xffff;
5401 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5402
5403 if (calculate_only)
5404 *calculated_value = x;
5405 else
5406 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5407
9810d34d
SS
5408 return This::STATUS_OKAY;
5409 }
5410
5411 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
5412 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
5413 static inline typename This::Status
47a9f4fc
VR
5414 relgot_lo16(unsigned char* view, int gp_offset, bool calculate_only,
5415 Valtype* calculated_value)
9810d34d 5416 {
9810d34d
SS
5417 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5418 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5419 Valtype x = gp_offset;
9810d34d 5420 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5421
5422 if (calculate_only)
5423 *calculated_value = x;
5424 else
5425 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5426
9810d34d
SS
5427 return This::STATUS_OKAY;
5428 }
5429
5430 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
5431 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
5432 static inline typename This::Status
5433 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5434 const Symbol_value<size>* psymval, Mips_address gp,
5435 Mips_address addend_a, bool extract_addend, bool local,
47a9f4fc
VR
5436 unsigned int r_type, bool calculate_only,
5437 Valtype* calculated_value)
9810d34d 5438 {
9810d34d
SS
5439 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5440 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5441
47a9f4fc 5442 Valtype addend;
9810d34d
SS
5443 if (extract_addend)
5444 {
5445 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5446 addend = (val & 0x7f) << 2;
5447 else
5448 addend = val & 0xffff;
5449 // Only sign-extend the addend if it was extracted from the
5450 // instruction. If the addend was separate, leave it alone,
5451 // otherwise we may lose significant bits.
5452 addend = Bits<16>::sign_extend32(addend);
5453 }
5454 else
5455 addend = addend_a;
5456
47a9f4fc 5457 Valtype x = psymval->value(object, addend) - gp;
9810d34d
SS
5458
5459 // If the symbol was local, any earlier relocatable links will
5460 // have adjusted its addend with the gp offset, so compensate
5461 // for that now. Don't do it for symbols forced local in this
5462 // link, though, since they won't have had the gp offset applied
5463 // to them before.
5464 if (local)
5465 x += object->gp_value();
5466
5467 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
5468 val = Bits<32>::bit_select32(val, x, 0x7f);
5469 else
5470 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5471
5472 if (calculate_only)
5473 {
5474 *calculated_value = x;
5475 return This::STATUS_OKAY;
5476 }
5477 else
5478 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5479
5480 if (check_overflow<16>(x) == This::STATUS_OVERFLOW)
9810d34d
SS
5481 {
5482 gold_error(_("small-data section exceeds 64KB; lower small-data size "
5483 "limit (see option -G)"));
5484 return This::STATUS_OVERFLOW;
5485 }
5486 return This::STATUS_OKAY;
5487 }
5488
5489 // R_MIPS_GPREL32
5490 static inline typename This::Status
5491 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5492 const Symbol_value<size>* psymval, Mips_address gp,
47a9f4fc
VR
5493 Mips_address addend_a, bool extract_addend, bool calculate_only,
5494 Valtype* calculated_value)
9810d34d 5495 {
9810d34d
SS
5496 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5497 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5498 Valtype addend = extract_addend ? val : addend_a;
9810d34d
SS
5499
5500 // R_MIPS_GPREL32 relocations are defined for local symbols only.
47a9f4fc
VR
5501 Valtype x = psymval->value(object, addend) + object->gp_value() - gp;
5502
5503 if (calculate_only)
5504 *calculated_value = x;
5505 else
5506 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5507
9810d34d
SS
5508 return This::STATUS_OKAY;
5509 }
5510
5511 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
5512 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
5513 // R_MICROMIPS_TLS_DTPREL_HI16
5514 static inline typename This::Status
5515 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5516 const Symbol_value<size>* psymval, Valtype32 tp_offset,
47a9f4fc
VR
5517 Mips_address addend_a, bool extract_addend, bool calculate_only,
5518 Valtype* calculated_value)
9810d34d 5519 {
9810d34d
SS
5520 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5521 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5522 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
5523
5524 // tls symbol values are relative to tls_segment()->vaddr()
47a9f4fc 5525 Valtype x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
9810d34d 5526 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5527
5528 if (calculate_only)
5529 *calculated_value = x;
5530 else
5531 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5532
9810d34d
SS
5533 return This::STATUS_OKAY;
5534 }
5535
5536 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
5537 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
5538 // R_MICROMIPS_TLS_DTPREL_LO16,
5539 static inline typename This::Status
5540 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5541 const Symbol_value<size>* psymval, Valtype32 tp_offset,
47a9f4fc
VR
5542 Mips_address addend_a, bool extract_addend, bool calculate_only,
5543 Valtype* calculated_value)
9810d34d 5544 {
9810d34d
SS
5545 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5546 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5547 Valtype addend = extract_addend ? val & 0xffff : addend_a;
9810d34d
SS
5548
5549 // tls symbol values are relative to tls_segment()->vaddr()
47a9f4fc 5550 Valtype x = psymval->value(object, addend) - tp_offset;
9810d34d 5551 val = Bits<32>::bit_select32(val, x, 0xffff);
47a9f4fc
VR
5552
5553 if (calculate_only)
5554 *calculated_value = x;
5555 else
5556 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5557
9810d34d
SS
5558 return This::STATUS_OKAY;
5559 }
5560
5561 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
5562 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
5563 static inline typename This::Status
5564 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5565 const Symbol_value<size>* psymval, Valtype32 tp_offset,
47a9f4fc
VR
5566 Mips_address addend_a, bool extract_addend, bool calculate_only,
5567 Valtype* calculated_value)
9810d34d 5568 {
9810d34d
SS
5569 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5570 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
47a9f4fc 5571 Valtype addend = extract_addend ? val : addend_a;
9810d34d
SS
5572
5573 // tls symbol values are relative to tls_segment()->vaddr()
47a9f4fc
VR
5574 Valtype x = psymval->value(object, addend) - tp_offset;
5575
5576 if (calculate_only)
5577 *calculated_value = x;
5578 else
5579 elfcpp::Swap<32, big_endian>::writeval(wv, x);
5580
9810d34d
SS
5581 return This::STATUS_OKAY;
5582 }
5583
5584 // R_MIPS_SUB, R_MICROMIPS_SUB
5585 static inline typename This::Status
5586 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5587 const Symbol_value<size>* psymval, Mips_address addend_a,
47a9f4fc 5588 bool extract_addend, bool calculate_only, Valtype* calculated_value)
9810d34d 5589 {
47a9f4fc
VR
5590 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5591 Valtype64 addend = (extract_addend
5592 ? elfcpp::Swap<64, big_endian>::readval(wv)
5593 : addend_a);
5594
5595 Valtype64 x = psymval->value(object, -addend);
5596 if (calculate_only)
5597 *calculated_value = x;
5598 else
5599 elfcpp::Swap<64, big_endian>::writeval(wv, x);
9810d34d 5600
9810d34d 5601 return This::STATUS_OKAY;
47a9f4fc
VR
5602 }
5603
5604 // R_MIPS_64: S + A
5605 static inline typename This::Status
5606 rel64(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5607 const Symbol_value<size>* psymval, Mips_address addend_a,
5608 bool extract_addend, bool calculate_only, Valtype* calculated_value,
5609 bool apply_addend_only)
5610 {
5611 Valtype64* wv = reinterpret_cast<Valtype64*>(view);
5612 Valtype64 addend = (extract_addend
5613 ? elfcpp::Swap<64, big_endian>::readval(wv)
5614 : addend_a);
5615
5616 Valtype64 x = psymval->value(object, addend);
5617 if (calculate_only)
5618 *calculated_value = x;
5619 else
5620 {
5621 if (apply_addend_only)
5622 x = addend;
5623 elfcpp::Swap<64, big_endian>::writeval(wv, x);
5624 }
5625
5626 return This::STATUS_OKAY;
5627 }
5628
e242ece1
VR
5629 // R_MIPS_HIGHER, R_MICROMIPS_HIGHER
5630 static inline typename This::Status
5631 relhigher(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5632 const Symbol_value<size>* psymval, Mips_address addend_a,
5633 bool extract_addend, bool calculate_only, Valtype* calculated_value)
5634 {
5635 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5636 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5637 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5638 : addend_a);
5639
5640 Valtype x = psymval->value(object, addend);
5641 x = ((x + (uint64_t) 0x80008000) >> 32) & 0xffff;
5642 val = Bits<32>::bit_select32(val, x, 0xffff);
5643
5644 if (calculate_only)
5645 *calculated_value = x;
5646 else
5647 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5648
5649 return This::STATUS_OKAY;
5650 }
5651
5652 // R_MIPS_HIGHEST, R_MICROMIPS_HIGHEST
5653 static inline typename This::Status
5654 relhighest(unsigned char* view, const Mips_relobj<size, big_endian>* object,
5655 const Symbol_value<size>* psymval, Mips_address addend_a,
5656 bool extract_addend, bool calculate_only,
5657 Valtype* calculated_value)
5658 {
5659 Valtype32* wv = reinterpret_cast<Valtype32*>(view);
5660 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
5661 Valtype addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
5662 : addend_a);
5663
5664 Valtype x = psymval->value(object, addend);
5665 x = ((x + (uint64_t) 0x800080008000) >> 48) & 0xffff;
5666 val = Bits<32>::bit_select32(val, x, 0xffff);
5667
5668 if (calculate_only)
5669 *calculated_value = x;
5670 else
5671 elfcpp::Swap<32, big_endian>::writeval(wv, val);
5672
5673 return This::STATUS_OKAY;
5674 }
9810d34d
SS
5675};
5676
5677template<int size, bool big_endian>
5678typename std::list<reloc_high<size, big_endian> >
5679 Mips_relocate_functions<size, big_endian>::hi16_relocs;
5680
5681template<int size, bool big_endian>
5682typename std::list<reloc_high<size, big_endian> >
5683 Mips_relocate_functions<size, big_endian>::got16_relocs;
5684
f5b11759
VR
5685template<int size, bool big_endian>
5686typename std::list<reloc_high<size, big_endian> >
5687 Mips_relocate_functions<size, big_endian>::pchi16_relocs;
5688
9810d34d
SS
5689// Mips_got_info methods.
5690
5691// Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
5692// SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
5693
5694template<int size, bool big_endian>
5695void
5696Mips_got_info<size, big_endian>::record_local_got_symbol(
5697 Mips_relobj<size, big_endian>* object, unsigned int symndx,
47a9f4fc
VR
5698 Mips_address addend, unsigned int r_type, unsigned int shndx,
5699 bool is_section_symbol)
9810d34d
SS
5700{
5701 Mips_got_entry<size, big_endian>* entry =
5702 new Mips_got_entry<size, big_endian>(object, symndx, addend,
5703 mips_elf_reloc_tls_type(r_type),
47a9f4fc 5704 shndx, is_section_symbol);
9810d34d
SS
5705 this->record_got_entry(entry, object);
5706}
5707
5708// Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
5709// in OBJECT. FOR_CALL is true if the caller is only interested in
5710// using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
5711// relocation.
5712
5713template<int size, bool big_endian>
5714void
5715Mips_got_info<size, big_endian>::record_global_got_symbol(
5716 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
5717 unsigned int r_type, bool dyn_reloc, bool for_call)
5718{
5719 if (!for_call)
5720 mips_sym->set_got_not_only_for_calls();
5721
5722 // A global symbol in the GOT must also be in the dynamic symbol table.
c1f59f8f 5723 if (!mips_sym->needs_dynsym_entry() && !mips_sym->is_forced_local())
9810d34d
SS
5724 {
5725 switch (mips_sym->visibility())
5726 {
5727 case elfcpp::STV_INTERNAL:
5728 case elfcpp::STV_HIDDEN:
5729 mips_sym->set_is_forced_local();
5730 break;
5731 default:
5732 mips_sym->set_needs_dynsym_entry();
5733 break;
5734 }
5735 }
5736
5737 unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
5738 if (tls_type == GOT_TLS_NONE)
5739 this->global_got_symbols_.insert(mips_sym);
5740
5741 if (dyn_reloc)
5742 {
5743 if (mips_sym->global_got_area() == GGA_NONE)
5744 mips_sym->set_global_got_area(GGA_RELOC_ONLY);
5745 return;
5746 }
5747
5748 Mips_got_entry<size, big_endian>* entry =
15eb1beb 5749 new Mips_got_entry<size, big_endian>(mips_sym, tls_type);
9810d34d
SS
5750
5751 this->record_got_entry(entry, object);
5752}
5753
5754// Add ENTRY to master GOT and to OBJECT's GOT.
5755
5756template<int size, bool big_endian>
5757void
5758Mips_got_info<size, big_endian>::record_got_entry(
5759 Mips_got_entry<size, big_endian>* entry,
5760 Mips_relobj<size, big_endian>* object)
5761{
15eb1beb 5762 this->got_entries_.insert(entry);
9810d34d
SS
5763
5764 // Create the GOT entry for the OBJECT's GOT.
5765 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
5766 Mips_got_entry<size, big_endian>* entry2 =
5767 new Mips_got_entry<size, big_endian>(*entry);
5768
15eb1beb 5769 g->got_entries_.insert(entry2);
9810d34d
SS
5770}
5771
5772// Record that OBJECT has a page relocation against symbol SYMNDX and
5773// that ADDEND is the addend for that relocation.
5774// This function creates an upper bound on the number of GOT slots
5775// required; no attempt is made to combine references to non-overridable
5776// global symbols across multiple input files.
5777
5778template<int size, bool big_endian>
5779void
5780Mips_got_info<size, big_endian>::record_got_page_entry(
5781 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
5782{
5783 struct Got_page_range **range_ptr, *range;
5784 int old_pages, new_pages;
5785
5786 // Find the Got_page_entry for this symbol.
5787 Got_page_entry* entry = new Got_page_entry(object, symndx);
5788 typename Got_page_entry_set::iterator it =
5789 this->got_page_entries_.find(entry);
5790 if (it != this->got_page_entries_.end())
5791 entry = *it;
5792 else
5793 this->got_page_entries_.insert(entry);
5794
5795 // Add the same entry to the OBJECT's GOT.
5796 Got_page_entry* entry2 = NULL;
5797 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
5798 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
5799 {
5800 entry2 = new Got_page_entry(*entry);
5801 g2->got_page_entries_.insert(entry2);
5802 }
5803
5804 // Skip over ranges whose maximum extent cannot share a page entry
5805 // with ADDEND.
5806 range_ptr = &entry->ranges;
5807 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
5808 range_ptr = &(*range_ptr)->next;
5809
5810 // If we scanned to the end of the list, or found a range whose
5811 // minimum extent cannot share a page entry with ADDEND, create
5812 // a new singleton range.
5813 range = *range_ptr;
5814 if (!range || addend < range->min_addend - 0xffff)
5815 {
5816 range = new Got_page_range();
5817 range->next = *range_ptr;
5818 range->min_addend = addend;
5819 range->max_addend = addend;
5820
5821 *range_ptr = range;
5822 ++entry->num_pages;
5823 if (entry2 != NULL)
5824 ++entry2->num_pages;
5825 ++this->page_gotno_;
5826 ++g2->page_gotno_;
5827 return;
5828 }
5829
5830 // Remember how many pages the old range contributed.
5831 old_pages = range->get_max_pages();
5832
5833 // Update the ranges.
5834 if (addend < range->min_addend)
5835 range->min_addend = addend;
5836 else if (addend > range->max_addend)
5837 {
5838 if (range->next && addend >= range->next->min_addend - 0xffff)
5839 {
5840 old_pages += range->next->get_max_pages();
5841 range->max_addend = range->next->max_addend;
5842 range->next = range->next->next;
5843 }
5844 else
5845 range->max_addend = addend;
5846 }
5847
5848 // Record any change in the total estimate.
5849 new_pages = range->get_max_pages();
5850 if (old_pages != new_pages)
5851 {
5852 entry->num_pages += new_pages - old_pages;
5853 if (entry2 != NULL)
5854 entry2->num_pages += new_pages - old_pages;
5855 this->page_gotno_ += new_pages - old_pages;
5856 g2->page_gotno_ += new_pages - old_pages;
5857 }
5858}
5859
5860// Create all entries that should be in the local part of the GOT.
5861
5862template<int size, bool big_endian>
5863void
5864Mips_got_info<size, big_endian>::add_local_entries(
5865 Target_mips<size, big_endian>* target, Layout* layout)
5866{
5867 Mips_output_data_got<size, big_endian>* got = target->got_section();
5868 // First two GOT entries are reserved. The first entry will be filled at
5869 // runtime. The second entry will be used by some runtime loaders.
5870 got->add_constant(0);
5871 got->add_constant(target->mips_elf_gnu_got1_mask());
5872
5873 for (typename Got_entry_set::iterator
5874 p = this->got_entries_.begin();
5875 p != this->got_entries_.end();
5876 ++p)
5877 {
5878 Mips_got_entry<size, big_endian>* entry = *p;
5879 if (entry->is_for_local_symbol() && !entry->is_tls_entry())
5880 {
5881 got->add_local(entry->object(), entry->symndx(),
47a9f4fc 5882 GOT_TYPE_STANDARD, entry->addend());
9810d34d 5883 unsigned int got_offset = entry->object()->local_got_offset(
47a9f4fc 5884 entry->symndx(), GOT_TYPE_STANDARD, entry->addend());
9810d34d
SS
5885 if (got->multi_got() && this->index_ > 0
5886 && parameters->options().output_is_position_independent())
47a9f4fc
VR
5887 {
5888 if (!entry->is_section_symbol())
5889 target->rel_dyn_section(layout)->add_local(entry->object(),
5890 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
5891 else
5892 target->rel_dyn_section(layout)->add_symbolless_local_addend(
5893 entry->object(), entry->symndx(), elfcpp::R_MIPS_REL32,
5894 got, got_offset);
5895 }
9810d34d
SS
5896 }
5897 }
5898
5899 this->add_page_entries(target, layout);
5900
5901 // Add global entries that should be in the local area.
5902 for (typename Got_entry_set::iterator
5903 p = this->got_entries_.begin();
5904 p != this->got_entries_.end();
5905 ++p)
5906 {
5907 Mips_got_entry<size, big_endian>* entry = *p;
5908 if (!entry->is_for_global_symbol())
5909 continue;
5910
5911 Mips_symbol<size>* mips_sym = entry->sym();
5912 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
5913 {
5914 unsigned int got_type;
5915 if (!got->multi_got())
5916 got_type = GOT_TYPE_STANDARD;
5917 else
5918 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5919 if (got->add_global(mips_sym, got_type))
5920 {
5921 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
5922 if (got->multi_got() && this->index_ > 0
5923 && parameters->options().output_is_position_independent())
5924 target->rel_dyn_section(layout)->add_symbolless_global_addend(
5925 mips_sym, elfcpp::R_MIPS_REL32, got,
5926 mips_sym->got_offset(got_type));
5927 }
5928 }
5929 }
5930}
5931
5932// Create GOT page entries.
5933
5934template<int size, bool big_endian>
5935void
5936Mips_got_info<size, big_endian>::add_page_entries(
5937 Target_mips<size, big_endian>* target, Layout* layout)
5938{
5939 if (this->page_gotno_ == 0)
5940 return;
5941
5942 Mips_output_data_got<size, big_endian>* got = target->got_section();
5943 this->got_page_offset_start_ = got->add_constant(0);
5944 if (got->multi_got() && this->index_ > 0
5945 && parameters->options().output_is_position_independent())
5946 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5947 this->got_page_offset_start_);
5948 int num_entries = this->page_gotno_;
5949 unsigned int prev_offset = this->got_page_offset_start_;
5950 while (--num_entries > 0)
5951 {
5952 unsigned int next_offset = got->add_constant(0);
5953 if (got->multi_got() && this->index_ > 0
5954 && parameters->options().output_is_position_independent())
5955 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
5956 next_offset);
5957 gold_assert(next_offset == prev_offset + size/8);
5958 prev_offset = next_offset;
5959 }
5960 this->got_page_offset_next_ = this->got_page_offset_start_;
5961}
5962
5963// Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
5964
5965template<int size, bool big_endian>
5966void
5967Mips_got_info<size, big_endian>::add_global_entries(
5968 Target_mips<size, big_endian>* target, Layout* layout,
5969 unsigned int non_reloc_only_global_gotno)
5970{
5971 Mips_output_data_got<size, big_endian>* got = target->got_section();
5972 // Add GGA_NORMAL entries.
5973 unsigned int count = 0;
5974 for (typename Got_entry_set::iterator
5975 p = this->got_entries_.begin();
5976 p != this->got_entries_.end();
5977 ++p)
5978 {
5979 Mips_got_entry<size, big_endian>* entry = *p;
5980 if (!entry->is_for_global_symbol())
5981 continue;
5982
5983 Mips_symbol<size>* mips_sym = entry->sym();
5984 if (mips_sym->global_got_area() != GGA_NORMAL)
5985 continue;
5986
5987 unsigned int got_type;
5988 if (!got->multi_got())
5989 got_type = GOT_TYPE_STANDARD;
5990 else
5991 // In multi-GOT links, global symbol can be in both primary and
5992 // secondary GOT(s). By creating custom GOT type
5993 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
5994 // is added to secondary GOT(s).
5995 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
5996 if (!got->add_global(mips_sym, got_type))
5997 continue;
5998
5999 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6000 if (got->multi_got() && this->index_ == 0)
6001 count++;
6002 if (got->multi_got() && this->index_ > 0)
6003 {
6004 if (parameters->options().output_is_position_independent()
6005 || (!parameters->doing_static_link()
6006 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
6007 {
6008 target->rel_dyn_section(layout)->add_global(
6009 mips_sym, elfcpp::R_MIPS_REL32, got,
6010 mips_sym->got_offset(got_type));
6011 got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
6012 elfcpp::R_MIPS_REL32, mips_sym);
6013 }
6014 }
6015 }
6016
6017 if (!got->multi_got() || this->index_ == 0)
6018 {
6019 if (got->multi_got())
6020 {
6021 // We need to allocate space in the primary GOT for GGA_NORMAL entries
6022 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
6023 // entries correspond to dynamic symbol indexes.
6024 while (count < non_reloc_only_global_gotno)
6025 {
6026 got->add_constant(0);
6027 ++count;
6028 }
6029 }
6030
6031 // Add GGA_RELOC_ONLY entries.
6032 got->add_reloc_only_entries();
6033 }
6034}
6035
6036// Create global GOT entries that should be in the GGA_RELOC_ONLY area.
6037
6038template<int size, bool big_endian>
6039void
6040Mips_got_info<size, big_endian>::add_reloc_only_entries(
6041 Mips_output_data_got<size, big_endian>* got)
6042{
15eb1beb 6043 for (typename Global_got_entry_set::iterator
9810d34d
SS
6044 p = this->global_got_symbols_.begin();
6045 p != this->global_got_symbols_.end();
6046 ++p)
6047 {
6048 Mips_symbol<size>* mips_sym = *p;
6049 if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
6050 {
6051 unsigned int got_type;
6052 if (!got->multi_got())
6053 got_type = GOT_TYPE_STANDARD;
6054 else
6055 got_type = GOT_TYPE_STANDARD_MULTIGOT;
6056 if (got->add_global(mips_sym, got_type))
6057 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
6058 }
6059 }
6060}
6061
6062// Create TLS GOT entries.
6063
6064template<int size, bool big_endian>
6065void
6066Mips_got_info<size, big_endian>::add_tls_entries(
6067 Target_mips<size, big_endian>* target, Layout* layout)
6068{
6069 Mips_output_data_got<size, big_endian>* got = target->got_section();
6070 // Add local tls entries.
6071 for (typename Got_entry_set::iterator
6072 p = this->got_entries_.begin();
6073 p != this->got_entries_.end();
6074 ++p)
6075 {
6076 Mips_got_entry<size, big_endian>* entry = *p;
6077 if (!entry->is_tls_entry() || !entry->is_for_local_symbol())
6078 continue;
6079
6080 if (entry->tls_type() == GOT_TLS_GD)
6081 {
6082 unsigned int got_type = GOT_TYPE_TLS_PAIR;
6083 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6084 : elfcpp::R_MIPS_TLS_DTPMOD64);
6085 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6086 : elfcpp::R_MIPS_TLS_DTPREL64);
6087
6088 if (!parameters->doing_static_link())
6089 {
6090 got->add_local_pair_with_rel(entry->object(), entry->symndx(),
6091 entry->shndx(), got_type,
6092 target->rel_dyn_section(layout),
47a9f4fc 6093 r_type1, entry->addend());
9810d34d 6094 unsigned int got_offset =
47a9f4fc
VR
6095 entry->object()->local_got_offset(entry->symndx(), got_type,
6096 entry->addend());
9810d34d
SS
6097 got->add_static_reloc(got_offset + size/8, r_type2,
6098 entry->object(), entry->symndx());
6099 }
6100 else
6101 {
6102 // We are doing a static link. Mark it as belong to module 1,
6103 // the executable.
6104 unsigned int got_offset = got->add_constant(1);
6105 entry->object()->set_local_got_offset(entry->symndx(), got_type,
47a9f4fc
VR
6106 got_offset,
6107 entry->addend());
9810d34d
SS
6108 got->add_constant(0);
6109 got->add_static_reloc(got_offset + size/8, r_type2,
6110 entry->object(), entry->symndx());
6111 }
6112 }
6113 else if (entry->tls_type() == GOT_TLS_IE)
6114 {
6115 unsigned int got_type = GOT_TYPE_TLS_OFFSET;
6116 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6117 : elfcpp::R_MIPS_TLS_TPREL64);
6118 if (!parameters->doing_static_link())
6119 got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
47a9f4fc
VR
6120 target->rel_dyn_section(layout), r_type,
6121 entry->addend());
9810d34d
SS
6122 else
6123 {
47a9f4fc
VR
6124 got->add_local(entry->object(), entry->symndx(), got_type,
6125 entry->addend());
9810d34d 6126 unsigned int got_offset =
47a9f4fc
VR
6127 entry->object()->local_got_offset(entry->symndx(), got_type,
6128 entry->addend());
9810d34d
SS
6129 got->add_static_reloc(got_offset, r_type, entry->object(),
6130 entry->symndx());
6131 }
6132 }
6133 else if (entry->tls_type() == GOT_TLS_LDM)
6134 {
6135 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6136 : elfcpp::R_MIPS_TLS_DTPMOD64);
6137 unsigned int got_offset;
6138 if (!parameters->doing_static_link())
6139 {
6140 got_offset = got->add_constant(0);
6141 target->rel_dyn_section(layout)->add_local(
6142 entry->object(), 0, r_type, got, got_offset);
6143 }
6144 else
6145 // We are doing a static link. Just mark it as belong to module 1,
6146 // the executable.
6147 got_offset = got->add_constant(1);
6148
6149 got->add_constant(0);
6150 got->set_tls_ldm_offset(got_offset, entry->object());
6151 }
6152 else
6153 gold_unreachable();
6154 }
6155
6156 // Add global tls entries.
6157 for (typename Got_entry_set::iterator
6158 p = this->got_entries_.begin();
6159 p != this->got_entries_.end();
6160 ++p)
6161 {
6162 Mips_got_entry<size, big_endian>* entry = *p;
6163 if (!entry->is_tls_entry() || !entry->is_for_global_symbol())
6164 continue;
6165
6166 Mips_symbol<size>* mips_sym = entry->sym();
6167 if (entry->tls_type() == GOT_TLS_GD)
6168 {
6169 unsigned int got_type;
6170 if (!got->multi_got())
6171 got_type = GOT_TYPE_TLS_PAIR;
6172 else
6173 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
6174 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
6175 : elfcpp::R_MIPS_TLS_DTPMOD64);
6176 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
6177 : elfcpp::R_MIPS_TLS_DTPREL64);
6178 if (!parameters->doing_static_link())
6179 got->add_global_pair_with_rel(mips_sym, got_type,
6180 target->rel_dyn_section(layout), r_type1, r_type2);
6181 else
6182 {
6183 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of
6184 // GOT entries. The first one is initialized to be 1, which is the
6185 // module index for the main executable and the second one 0. A
6186 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
6187 // the second GOT entry and will be applied by gold.
6188 unsigned int got_offset = got->add_constant(1);
6189 mips_sym->set_got_offset(got_type, got_offset);
6190 got->add_constant(0);
6191 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
6192 }
6193 }
6194 else if (entry->tls_type() == GOT_TLS_IE)
6195 {
6196 unsigned int got_type;
6197 if (!got->multi_got())
6198 got_type = GOT_TYPE_TLS_OFFSET;
6199 else
6200 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
6201 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
6202 : elfcpp::R_MIPS_TLS_TPREL64);
6203 if (!parameters->doing_static_link())
6204 got->add_global_with_rel(mips_sym, got_type,
6205 target->rel_dyn_section(layout), r_type);
6206 else
6207 {
6208 got->add_global(mips_sym, got_type);
6209 unsigned int got_offset = mips_sym->got_offset(got_type);
6210 got->add_static_reloc(got_offset, r_type, mips_sym);
6211 }
6212 }
6213 else
6214 gold_unreachable();
6215 }
6216}
6217
6218// Decide whether the symbol needs an entry in the global part of the primary
6219// GOT, setting global_got_area accordingly. Count the number of global
6220// symbols that are in the primary GOT only because they have dynamic
6221// relocations R_MIPS_REL32 against them (reloc_only_gotno).
6222
6223template<int size, bool big_endian>
6224void
6225Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
6226{
15eb1beb 6227 for (typename Global_got_entry_set::iterator
9810d34d
SS
6228 p = this->global_got_symbols_.begin();
6229 p != this->global_got_symbols_.end();
6230 ++p)
6231 {
6232 Mips_symbol<size>* sym = *p;
6233 // Make a final decision about whether the symbol belongs in the
6234 // local or global GOT. Symbols that bind locally can (and in the
6235 // case of forced-local symbols, must) live in the local GOT.
6236 // Those that are aren't in the dynamic symbol table must also
6237 // live in the local GOT.
6238
6239 if (!sym->should_add_dynsym_entry(symtab)
6240 || (sym->got_only_for_calls()
6241 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab))
6242 : symbol_references_local(sym,
6243 sym->should_add_dynsym_entry(symtab))))
6244 // The symbol belongs in the local GOT. We no longer need this
6245 // entry if it was only used for relocations; those relocations
6246 // will be against the null or section symbol instead.
6247 sym->set_global_got_area(GGA_NONE);
6248 else if (sym->global_got_area() == GGA_RELOC_ONLY)
6249 {
6250 ++this->reloc_only_gotno_;
6251 ++this->global_gotno_ ;
6252 }
6253 }
6254}
6255
6256// Return the offset of GOT page entry for VALUE. Initialize the entry with
6257// VALUE if it is not initialized.
6258
6259template<int size, bool big_endian>
6260unsigned int
6261Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value,
6262 Mips_output_data_got<size, big_endian>* got)
6263{
6264 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
6265 if (it != this->got_page_offsets_.end())
6266 return it->second;
6267
6268 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
6269 + (size/8) * this->page_gotno_);
6270
6271 unsigned int got_offset = this->got_page_offset_next_;
6272 this->got_page_offsets_[value] = got_offset;
6273 this->got_page_offset_next_ += size/8;
6274 got->update_got_entry(got_offset, value);
6275 return got_offset;
6276}
6277
6278// Remove lazy-binding stubs for global symbols in this GOT.
6279
6280template<int size, bool big_endian>
6281void
6282Mips_got_info<size, big_endian>::remove_lazy_stubs(
6283 Target_mips<size, big_endian>* target)
6284{
6285 for (typename Got_entry_set::iterator
6286 p = this->got_entries_.begin();
6287 p != this->got_entries_.end();
6288 ++p)
6289 {
6290 Mips_got_entry<size, big_endian>* entry = *p;
6291 if (entry->is_for_global_symbol())
6292 target->remove_lazy_stub_entry(entry->sym());
6293 }
6294}
6295
6296// Count the number of GOT entries required.
6297
6298template<int size, bool big_endian>
6299void
6300Mips_got_info<size, big_endian>::count_got_entries()
6301{
6302 for (typename Got_entry_set::iterator
6303 p = this->got_entries_.begin();
6304 p != this->got_entries_.end();
6305 ++p)
6306 {
6307 this->count_got_entry(*p);
6308 }
6309}
6310
6311// Count the number of GOT entries required by ENTRY. Accumulate the result.
6312
6313template<int size, bool big_endian>
6314void
6315Mips_got_info<size, big_endian>::count_got_entry(
6316 Mips_got_entry<size, big_endian>* entry)
6317{
6318 if (entry->is_tls_entry())
6319 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
6320 else if (entry->is_for_local_symbol()
6321 || entry->sym()->global_got_area() == GGA_NONE)
6322 ++this->local_gotno_;
6323 else
6324 ++this->global_gotno_;
6325}
6326
6327// Add FROM's GOT entries.
6328
6329template<int size, bool big_endian>
6330void
6331Mips_got_info<size, big_endian>::add_got_entries(
6332 Mips_got_info<size, big_endian>* from)
6333{
6334 for (typename Got_entry_set::iterator
6335 p = from->got_entries_.begin();
6336 p != from->got_entries_.end();
6337 ++p)
6338 {
6339 Mips_got_entry<size, big_endian>* entry = *p;
6340 if (this->got_entries_.find(entry) == this->got_entries_.end())
6341 {
6342 Mips_got_entry<size, big_endian>* entry2 =
6343 new Mips_got_entry<size, big_endian>(*entry);
6344 this->got_entries_.insert(entry2);
6345 this->count_got_entry(entry);
6346 }
6347 }
6348}
6349
6350// Add FROM's GOT page entries.
6351
6352template<int size, bool big_endian>
6353void
6354Mips_got_info<size, big_endian>::add_got_page_entries(
6355 Mips_got_info<size, big_endian>* from)
6356{
6357 for (typename Got_page_entry_set::iterator
6358 p = from->got_page_entries_.begin();
6359 p != from->got_page_entries_.end();
6360 ++p)
6361 {
6362 Got_page_entry* entry = *p;
6363 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end())
6364 {
6365 Got_page_entry* entry2 = new Got_page_entry(*entry);
6366 this->got_page_entries_.insert(entry2);
6367 this->page_gotno_ += entry->num_pages;
6368 }
6369 }
6370}
6371
6372// Mips_output_data_got methods.
6373
6374// Lay out the GOT. Add local, global and TLS entries. If GOT is
6375// larger than 64K, create multi-GOT.
6376
6377template<int size, bool big_endian>
6378void
6379Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout,
6380 Symbol_table* symtab, const Input_objects* input_objects)
6381{
6382 // Decide which symbols need to go in the global part of the GOT and
6383 // count the number of reloc-only GOT symbols.
6384 this->master_got_info_->count_got_symbols(symtab);
6385
6386 // Count the number of GOT entries.
6387 this->master_got_info_->count_got_entries();
6388
6389 unsigned int got_size = this->master_got_info_->got_size();
6390 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
6391 this->lay_out_multi_got(layout, input_objects);
6392 else
6393 {
6394 // Record that all objects use single GOT.
6395 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6396 p != input_objects->relobj_end();
6397 ++p)
6398 {
6399 Mips_relobj<size, big_endian>* object =
6400 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6401 if (object->get_got_info() != NULL)
6402 object->set_got_info(this->master_got_info_);
6403 }
6404
6405 this->master_got_info_->add_local_entries(this->target_, layout);
6406 this->master_got_info_->add_global_entries(this->target_, layout,
6407 /*not used*/-1U);
6408 this->master_got_info_->add_tls_entries(this->target_, layout);
6409 }
6410}
6411
6412// Create multi-GOT. For every GOT, add local, global and TLS entries.
6413
6414template<int size, bool big_endian>
6415void
6416Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout,
6417 const Input_objects* input_objects)
6418{
6419 // Try to merge the GOTs of input objects together, as long as they
6420 // don't seem to exceed the maximum GOT size, choosing one of them
6421 // to be the primary GOT.
6422 this->merge_gots(input_objects);
6423
6424 // Every symbol that is referenced in a dynamic relocation must be
6425 // present in the primary GOT.
6426 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno());
6427
6428 // Add GOT entries.
6429 unsigned int i = 0;
6430 unsigned int offset = 0;
6431 Mips_got_info<size, big_endian>* g = this->primary_got_;
6432 do
6433 {
6434 g->set_index(i);
6435 g->set_offset(offset);
6436
6437 g->add_local_entries(this->target_, layout);
6438 if (i == 0)
6439 g->add_global_entries(this->target_, layout,
6440 (this->master_got_info_->global_gotno()
6441 - this->master_got_info_->reloc_only_gotno()));
6442 else
6443 g->add_global_entries(this->target_, layout, /*not used*/-1U);
6444 g->add_tls_entries(this->target_, layout);
6445
6446 // Forbid global symbols in every non-primary GOT from having
6447 // lazy-binding stubs.
6448 if (i > 0)
6449 g->remove_lazy_stubs(this->target_);
6450
6451 ++i;
6452 offset += g->got_size();
6453 g = g->next();
6454 }
6455 while (g);
6456}
6457
6458// Attempt to merge GOTs of different input objects. Try to use as much as
6459// possible of the primary GOT, since it doesn't require explicit dynamic
6460// relocations, but don't use objects that would reference global symbols
6461// out of the addressable range. Failing the primary GOT, attempt to merge
6462// with the current GOT, or finish the current GOT and then make make the new
6463// GOT current.
6464
6465template<int size, bool big_endian>
6466void
6467Mips_output_data_got<size, big_endian>::merge_gots(
6468 const Input_objects* input_objects)
6469{
6470 gold_assert(this->primary_got_ == NULL);
6471 Mips_got_info<size, big_endian>* current = NULL;
6472
6473 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
6474 p != input_objects->relobj_end();
6475 ++p)
6476 {
6477 Mips_relobj<size, big_endian>* object =
6478 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
6479
6480 Mips_got_info<size, big_endian>* g = object->get_got_info();
6481 if (g == NULL)
6482 continue;
6483
6484 g->count_got_entries();
6485
6486 // Work out the number of page, local and TLS entries.
6487 unsigned int estimate = this->master_got_info_->page_gotno();
6488 if (estimate > g->page_gotno())
6489 estimate = g->page_gotno();
6490 estimate += g->local_gotno() + g->tls_gotno();
6491
6492 // We place TLS GOT entries after both locals and globals. The globals
6493 // for the primary GOT may overflow the normal GOT size limit, so be
6494 // sure not to merge a GOT which requires TLS with the primary GOT in that
6495 // case. This doesn't affect non-primary GOTs.
6496 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno()
6497 : g->global_gotno());
6498
6499 unsigned int max_count =
6500 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6501 if (estimate <= max_count)
6502 {
6503 // If we don't have a primary GOT, use it as
6504 // a starting point for the primary GOT.
6505 if (!this->primary_got_)
6506 {
6507 this->primary_got_ = g;
6508 continue;
6509 }
6510
6511 // Try merging with the primary GOT.
6512 if (this->merge_got_with(g, object, this->primary_got_))
6513 continue;
6514 }
6515
6516 // If we can merge with the last-created GOT, do it.
6517 if (current && this->merge_got_with(g, object, current))
6518 continue;
6519
6520 // Well, we couldn't merge, so create a new GOT. Don't check if it
6521 // fits; if it turns out that it doesn't, we'll get relocation
6522 // overflows anyway.
6523 g->set_next(current);
6524 current = g;
6525 }
6526
6527 // If we do not find any suitable primary GOT, create an empty one.
6528 if (this->primary_got_ == NULL)
6529 this->primary_got_ = new Mips_got_info<size, big_endian>();
6530
6531 // Link primary GOT with secondary GOTs.
6532 this->primary_got_->set_next(current);
6533}
6534
6535// Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
6536// this would lead to overflow, true if they were merged successfully.
6537
6538template<int size, bool big_endian>
6539bool
6540Mips_output_data_got<size, big_endian>::merge_got_with(
6541 Mips_got_info<size, big_endian>* from,
6542 Mips_relobj<size, big_endian>* object,
6543 Mips_got_info<size, big_endian>* to)
6544{
6545 // Work out how many page entries we would need for the combined GOT.
6546 unsigned int estimate = this->master_got_info_->page_gotno();
6547 if (estimate >= from->page_gotno() + to->page_gotno())
6548 estimate = from->page_gotno() + to->page_gotno();
6549
6550 // Conservatively estimate how many local and TLS entries would be needed.
6551 estimate += from->local_gotno() + to->local_gotno();
6552 estimate += from->tls_gotno() + to->tls_gotno();
6553
6554 // If we're merging with the primary got, any TLS relocations will
6555 // come after the full set of global entries. Otherwise estimate those
6556 // conservatively as well.
6557 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0)
6558 estimate += this->master_got_info_->global_gotno();
6559 else
6560 estimate += from->global_gotno() + to->global_gotno();
6561
6562 // Bail out if the combined GOT might be too big.
6563 unsigned int max_count =
6564 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
6565 if (estimate > max_count)
6566 return false;
6567
6568 // Transfer the object's GOT information from FROM to TO.
6569 to->add_got_entries(from);
6570 to->add_got_page_entries(from);
6571
6572 // Record that OBJECT should use output GOT TO.
6573 object->set_got_info(to);
6574
6575 return true;
6576}
6577
6578// Write out the GOT.
6579
6580template<int size, bool big_endian>
6581void
6582Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
6583{
a080d84d
AV
6584 typedef Unordered_set<Mips_symbol<size>*, Mips_symbol_hash<size> >
6585 Mips_stubs_entry_set;
6586
9810d34d
SS
6587 // Call parent to write out GOT.
6588 Output_data_got<size, big_endian>::do_write(of);
6589
6590 const off_t offset = this->offset();
6591 const section_size_type oview_size =
6592 convert_to_section_size_type(this->data_size());
6593 unsigned char* const oview = of->get_output_view(offset, oview_size);
6594
6595 // Needed for fixing values of .got section.
6596 this->got_view_ = oview;
6597
6598 // Write lazy stub addresses.
a080d84d 6599 for (typename Mips_stubs_entry_set::iterator
9810d34d
SS
6600 p = this->master_got_info_->global_got_symbols().begin();
6601 p != this->master_got_info_->global_got_symbols().end();
6602 ++p)
6603 {
6604 Mips_symbol<size>* mips_sym = *p;
6605 if (mips_sym->has_lazy_stub())
6606 {
6607 Valtype* wv = reinterpret_cast<Valtype*>(
6608 oview + this->get_primary_got_offset(mips_sym));
6609 Valtype value =
6610 this->target_->mips_stubs_section()->stub_address(mips_sym);
6611 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6612 }
6613 }
6614
6615 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries.
a080d84d 6616 for (typename Mips_stubs_entry_set::iterator
9810d34d
SS
6617 p = this->master_got_info_->global_got_symbols().begin();
6618 p != this->master_got_info_->global_got_symbols().end();
6619 ++p)
6620 {
6621 Mips_symbol<size>* mips_sym = *p;
6622 if (!this->multi_got()
6623 && (mips_sym->is_mips16() || mips_sym->is_micromips())
6624 && mips_sym->global_got_area() == GGA_NONE
6625 && mips_sym->has_got_offset(GOT_TYPE_STANDARD))
6626 {
6627 Valtype* wv = reinterpret_cast<Valtype*>(
6628 oview + mips_sym->got_offset(GOT_TYPE_STANDARD));
6629 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv);
6630 if (value != 0)
6631 {
6632 value |= 1;
6633 elfcpp::Swap<size, big_endian>::writeval(wv, value);
6634 }
6635 }
6636 }
6637
6638 if (!this->secondary_got_relocs_.empty())
6639 {
6640 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global
6641 // secondary GOT entries with non-zero initial value copy the value
6642 // to the corresponding primary GOT entry, and set the secondary GOT
6643 // entry to zero.
6644 // TODO(sasa): This is workaround. It needs to be investigated further.
6645
6646 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
6647 {
6648 Static_reloc& reloc(this->secondary_got_relocs_[i]);
6649 if (reloc.symbol_is_global())
6650 {
6651 Mips_symbol<size>* gsym = reloc.symbol();
6652 gold_assert(gsym != NULL);
6653
6654 unsigned got_offset = reloc.got_offset();
6655 gold_assert(got_offset < oview_size);
6656
6657 // Find primary GOT entry.
6658 Valtype* wv_prim = reinterpret_cast<Valtype*>(
6659 oview + this->get_primary_got_offset(gsym));
6660
6661 // Find secondary GOT entry.
6662 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
6663
6664 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
6665 if (value != 0)
6666 {
6667 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
6668 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
6669 gsym->set_applied_secondary_got_fixup();
6670 }
6671 }
6672 }
6673
6674 of->write_output_view(offset, oview_size, oview);
6675 }
6676
6677 // We are done if there is no fix up.
6678 if (this->static_relocs_.empty())
6679 return;
6680
6681 Output_segment* tls_segment = this->layout_->tls_segment();
6682 gold_assert(tls_segment != NULL);
6683
6684 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
6685 {
6686 Static_reloc& reloc(this->static_relocs_[i]);
6687
6688 Mips_address value;
6689 if (!reloc.symbol_is_global())
6690 {
6691 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
6692 const Symbol_value<size>* psymval =
6693 object->local_symbol(reloc.index());
6694
6695 // We are doing static linking. Issue an error and skip this
6696 // relocation if the symbol is undefined or in a discarded_section.
6697 bool is_ordinary;
6698 unsigned int shndx = psymval->input_shndx(&is_ordinary);
6699 if ((shndx == elfcpp::SHN_UNDEF)
6700 || (is_ordinary
6701 && shndx != elfcpp::SHN_UNDEF
6702 && !object->is_section_included(shndx)
6703 && !this->symbol_table_->is_section_folded(object, shndx)))
6704 {
6705 gold_error(_("undefined or discarded local symbol %u from "
6706 " object %s in GOT"),
6707 reloc.index(), reloc.relobj()->name().c_str());
6708 continue;
6709 }
6710
6711 value = psymval->value(object, 0);
6712 }
6713 else
6714 {
6715 const Mips_symbol<size>* gsym = reloc.symbol();
6716 gold_assert(gsym != NULL);
6717
6718 // We are doing static linking. Issue an error and skip this
6719 // relocation if the symbol is undefined or in a discarded_section
6720 // unless it is a weakly_undefined symbol.
6721 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
6722 && !gsym->is_weak_undefined())
6723 {
6724 gold_error(_("undefined or discarded symbol %s in GOT"),
6725 gsym->name());
6726 continue;
6727 }
6728
6729 if (!gsym->is_weak_undefined())
6730 value = gsym->value();
6731 else
6732 value = 0;
6733 }
6734
6735 unsigned got_offset = reloc.got_offset();
6736 gold_assert(got_offset < oview_size);
6737
6738 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
6739 Valtype x;
6740
6741 switch (reloc.r_type())
6742 {
6743 case elfcpp::R_MIPS_TLS_DTPMOD32:
6744 case elfcpp::R_MIPS_TLS_DTPMOD64:
6745 x = value;
6746 break;
6747 case elfcpp::R_MIPS_TLS_DTPREL32:
6748 case elfcpp::R_MIPS_TLS_DTPREL64:
6749 x = value - elfcpp::DTP_OFFSET;
6750 break;
6751 case elfcpp::R_MIPS_TLS_TPREL32:
6752 case elfcpp::R_MIPS_TLS_TPREL64:
6753 x = value - elfcpp::TP_OFFSET;
6754 break;
6755 default:
6756 gold_unreachable();
6757 break;
6758 }
6759
6760 elfcpp::Swap<size, big_endian>::writeval(wv, x);
6761 }
6762
6763 of->write_output_view(offset, oview_size, oview);
6764}
6765
6766// Mips_relobj methods.
6767
6768// Count the local symbols. The Mips backend needs to know if a symbol
6769// is a MIPS16 or microMIPS function or not. For global symbols, it is easy
6770// because the Symbol object keeps the ELF symbol type and st_other field.
6771// For local symbol it is harder because we cannot access this information.
6772// So we override the do_count_local_symbol in parent and scan local symbols to
6773// mark MIPS16 and microMIPS functions. This is not the most efficient way but
6774// I do not want to slow down other ports by calling a per symbol target hook
6775// inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
6776
6777template<int size, bool big_endian>
6778void
6779Mips_relobj<size, big_endian>::do_count_local_symbols(
6780 Stringpool_template<char>* pool,
6781 Stringpool_template<char>* dynpool)
6782{
6783 // Ask parent to count the local symbols.
6784 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
6785 const unsigned int loccount = this->local_symbol_count();
6786 if (loccount == 0)
6787 return;
6788
6789 // Initialize the mips16 and micromips function bit-vector.
6790 this->local_symbol_is_mips16_.resize(loccount, false);
6791 this->local_symbol_is_micromips_.resize(loccount, false);
6792
6793 // Read the symbol table section header.
6794 const unsigned int symtab_shndx = this->symtab_shndx();
6795 elfcpp::Shdr<size, big_endian>
6796 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6797 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6798
6799 // Read the local symbols.
6800 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
6801 gold_assert(loccount == symtabshdr.get_sh_info());
6802 off_t locsize = loccount * sym_size;
6803 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6804 locsize, true, true);
6805
6806 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
6807
6808 // Skip the first dummy symbol.
6809 psyms += sym_size;
6810 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6811 {
6812 elfcpp::Sym<size, big_endian> sym(psyms);
6813 unsigned char st_other = sym.get_st_other();
6814 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
6815 this->local_symbol_is_micromips_[i] =
6816 elfcpp::elf_st_is_micromips(st_other);
6817 }
6818}
6819
6820// Read the symbol information.
6821
6822template<int size, bool big_endian>
6823void
6824Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
6825{
6826 // Call parent class to read symbol information.
f35c4853 6827 this->base_read_symbols(sd);
9810d34d 6828
4d78db49
VR
6829 // If this input file is a binary file, it has no processor
6830 // specific data.
6831 Input_file::Format format = this->input_file()->format();
6832 if (format != Input_file::FORMAT_ELF)
6833 {
6834 gold_assert(format == Input_file::FORMAT_BINARY);
6835 this->merge_processor_specific_data_ = false;
6836 return;
6837 }
6838
9810d34d
SS
6839 // Read processor-specific flags in ELF file header.
6840 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6841 elfcpp::Elf_sizes<size>::ehdr_size,
6842 true, false);
6843 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
6844 this->processor_specific_flags_ = ehdr.get_e_flags();
6845
6846 // Get the section names.
6847 const unsigned char* pnamesu = sd->section_names->data();
6848 const char* pnames = reinterpret_cast<const char*>(pnamesu);
6849
6850 // Initialize the mips16 stub section bit-vectors.
6851 this->section_is_mips16_fn_stub_.resize(this->shnum(), false);
6852 this->section_is_mips16_call_stub_.resize(this->shnum(), false);
6853 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false);
6854
6855 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
6856 const unsigned char* pshdrs = sd->section_headers->data();
6857 const unsigned char* ps = pshdrs + shdr_size;
4d78db49 6858 bool must_merge_processor_specific_data = false;
9810d34d
SS
6859 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6860 {
6861 elfcpp::Shdr<size, big_endian> shdr(ps);
6862
4d78db49
VR
6863 // Sometimes an object has no contents except the section name string
6864 // table and an empty symbol table with the undefined symbol. We
6865 // don't want to merge processor-specific data from such an object.
6866 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6867 {
6868 // Symbol table is not empty.
6869 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
6870 elfcpp::Elf_sizes<size>::sym_size;
6871 if (shdr.get_sh_size() > sym_size)
6872 must_merge_processor_specific_data = true;
6873 }
6874 else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6875 // If this is neither an empty symbol table nor a string table,
6876 // be conservative.
6877 must_merge_processor_specific_data = true;
6878
9810d34d
SS
6879 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
6880 {
82e49872 6881 this->has_reginfo_section_ = true;
9810d34d
SS
6882 // Read the gp value that was used to create this object. We need the
6883 // gp value while processing relocs. The .reginfo section is not used
6884 // in the 64-bit MIPS ELF ABI.
6885 section_offset_type section_offset = shdr.get_sh_offset();
6886 section_size_type section_size =
6887 convert_to_section_size_type(shdr.get_sh_size());
6888 const unsigned char* view =
6889 this->get_view(section_offset, section_size, true, false);
6890
6891 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
6892
6893 // Read the rest of .reginfo.
6894 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view);
6895 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4);
6896 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8);
6897 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12);
6898 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16);
6899 }
6900
b52717c0
VR
6901 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
6902 {
6903 gold_assert(this->attributes_section_data_ == NULL);
6904 section_offset_type section_offset = shdr.get_sh_offset();
6905 section_size_type section_size =
6906 convert_to_section_size_type(shdr.get_sh_size());
6907 const unsigned char* view =
6908 this->get_view(section_offset, section_size, true, false);
6909 this->attributes_section_data_ =
6910 new Attributes_section_data(view, section_size);
6911 }
6912
6913 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_ABIFLAGS)
6914 {
6915 gold_assert(this->abiflags_ == NULL);
6916 section_offset_type section_offset = shdr.get_sh_offset();
6917 section_size_type section_size =
6918 convert_to_section_size_type(shdr.get_sh_size());
6919 const unsigned char* view =
6920 this->get_view(section_offset, section_size, true, false);
6921 this->abiflags_ = new Mips_abiflags<big_endian>();
6922
6923 this->abiflags_->version =
6924 elfcpp::Swap<16, big_endian>::readval(view);
6925 if (this->abiflags_->version != 0)
6926 {
6927 gold_error(_("%s: .MIPS.abiflags section has "
6928 "unsupported version %u"),
6929 this->name().c_str(),
6930 this->abiflags_->version);
6931 break;
6932 }
6933 this->abiflags_->isa_level =
6934 elfcpp::Swap<8, big_endian>::readval(view + 2);
6935 this->abiflags_->isa_rev =
6936 elfcpp::Swap<8, big_endian>::readval(view + 3);
6937 this->abiflags_->gpr_size =
6938 elfcpp::Swap<8, big_endian>::readval(view + 4);
6939 this->abiflags_->cpr1_size =
6940 elfcpp::Swap<8, big_endian>::readval(view + 5);
6941 this->abiflags_->cpr2_size =
6942 elfcpp::Swap<8, big_endian>::readval(view + 6);
6943 this->abiflags_->fp_abi =
6944 elfcpp::Swap<8, big_endian>::readval(view + 7);
6945 this->abiflags_->isa_ext =
6946 elfcpp::Swap<32, big_endian>::readval(view + 8);
6947 this->abiflags_->ases =
6948 elfcpp::Swap<32, big_endian>::readval(view + 12);
6949 this->abiflags_->flags1 =
6950 elfcpp::Swap<32, big_endian>::readval(view + 16);
6951 this->abiflags_->flags2 =
6952 elfcpp::Swap<32, big_endian>::readval(view + 20);
6953 }
6954
47a9f4fc
VR
6955 // In the 64-bit ABI, .MIPS.options section holds register information.
6956 // A SHT_MIPS_OPTIONS section contains a series of options, each of which
6957 // starts with this header:
6958 //
6959 // typedef struct
6960 // {
6961 // // Type of option.
6962 // unsigned char kind[1];
6963 // // Size of option descriptor, including header.
6964 // unsigned char size[1];
6965 // // Section index of affected section, or 0 for global option.
6966 // unsigned char section[2];
6967 // // Information specific to this kind of option.
6968 // unsigned char info[4];
6969 // };
6970 //
6971 // For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and set
6972 // the gp value based on what we find. We may see both SHT_MIPS_REGINFO
6973 // and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case, they should agree.
9810d34d 6974
47a9f4fc 6975 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_OPTIONS)
9810d34d 6976 {
47a9f4fc
VR
6977 section_offset_type section_offset = shdr.get_sh_offset();
6978 section_size_type section_size =
6979 convert_to_section_size_type(shdr.get_sh_size());
6980 const unsigned char* view =
6981 this->get_view(section_offset, section_size, true, false);
6982 const unsigned char* end = view + section_size;
6983
6984 while (view + 8 <= end)
6985 {
6986 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
6987 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
6988 if (sz < 8)
6989 {
6990 gold_error(_("%s: Warning: bad `%s' option size %u smaller "
6991 "than its header"),
6992 this->name().c_str(),
6993 this->mips_elf_options_section_name(), sz);
6994 break;
6995 }
6996
6997 if (this->is_n64() && kind == elfcpp::ODK_REGINFO)
6998 {
6999 // In the 64 bit ABI, an ODK_REGINFO option is the following
7000 // structure. The info field of the options header is not
7001 // used.
7002 //
7003 // typedef struct
7004 // {
7005 // // Mask of general purpose registers used.
7006 // unsigned char ri_gprmask[4];
7007 // // Padding.
7008 // unsigned char ri_pad[4];
7009 // // Mask of co-processor registers used.
7010 // unsigned char ri_cprmask[4][4];
7011 // // GP register value for this object file.
7012 // unsigned char ri_gp_value[8];
7013 // };
7014
7015 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7016 + 32);
7017 }
7018 else if (kind == elfcpp::ODK_REGINFO)
7019 {
7020 // In the 32 bit ABI, an ODK_REGINFO option is the following
7021 // structure. The info field of the options header is not
7022 // used. The same structure is used in .reginfo section.
7023 //
7024 // typedef struct
7025 // {
7026 // unsigned char ri_gprmask[4];
7027 // unsigned char ri_cprmask[4][4];
7028 // unsigned char ri_gp_value[4];
7029 // };
7030
7031 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view
7032 + 28);
7033 }
7034 view += sz;
7035 }
7036 }
7037
7038 const char* name = pnames + shdr.get_sh_name();
7039 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name);
7040 this->section_is_mips16_call_stub_[i] =
7041 is_prefix_of(".mips16.call.", name);
7042 this->section_is_mips16_call_fp_stub_[i] =
7043 is_prefix_of(".mips16.call.fp.", name);
7044
7045 if (strcmp(name, ".pdr") == 0)
7046 {
7047 gold_assert(this->pdr_shndx_ == -1U);
7048 this->pdr_shndx_ = i;
7049 }
7050 }
4d78db49
VR
7051
7052 // This is rare.
7053 if (!must_merge_processor_specific_data)
7054 this->merge_processor_specific_data_ = false;
47a9f4fc
VR
7055}
7056
9810d34d
SS
7057// Discard MIPS16 stub secions that are not needed.
7058
7059template<int size, bool big_endian>
7060void
7061Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab)
7062{
7063 for (typename Mips16_stubs_int_map::const_iterator
7064 it = this->mips16_stub_sections_.begin();
7065 it != this->mips16_stub_sections_.end(); ++it)
7066 {
7067 Mips16_stub_section<size, big_endian>* stub_section = it->second;
7068 if (!stub_section->is_target_found())
7069 {
7070 gold_error(_("no relocation found in mips16 stub section '%s'"),
7071 stub_section->object()
7072 ->section_name(stub_section->shndx()).c_str());
7073 }
7074
7075 bool discard = false;
7076 if (stub_section->is_for_local_function())
7077 {
7078 if (stub_section->is_fn_stub())
7079 {
7080 // This stub is for a local symbol. This stub will only
7081 // be needed if there is some relocation in this object,
7082 // other than a 16 bit function call, which refers to this
7083 // symbol.
7084 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym()))
7085 discard = true;
7086 else
7087 this->add_local_mips16_fn_stub(stub_section);
7088 }
7089 else
7090 {
7091 // This stub is for a local symbol. This stub will only
7092 // be needed if there is some relocation (R_MIPS16_26) in
7093 // this object that refers to this symbol.
7094 gold_assert(stub_section->is_call_stub()
7095 || stub_section->is_call_fp_stub());
7096 if (!this->has_local_16bit_call_relocs(stub_section->r_sym()))
7097 discard = true;
7098 else
7099 this->add_local_mips16_call_stub(stub_section);
7100 }
7101 }
7102 else
7103 {
7104 Mips_symbol<size>* gsym = stub_section->gsym();
7105 if (stub_section->is_fn_stub())
7106 {
7107 if (gsym->has_mips16_fn_stub())
7108 // We already have a stub for this function.
7109 discard = true;
7110 else
7111 {
7112 gsym->set_mips16_fn_stub(stub_section);
7113 if (gsym->should_add_dynsym_entry(symtab))
7114 {
7115 // If we have a MIPS16 function with a stub, the
7116 // dynamic symbol must refer to the stub, since only
7117 // the stub uses the standard calling conventions.
7118 gsym->set_need_fn_stub();
7119 if (gsym->is_from_dynobj())
7120 gsym->set_needs_dynsym_value();
7121 }
7122 }
7123 if (!gsym->need_fn_stub())
7124 discard = true;
7125 }
7126 else if (stub_section->is_call_stub())
7127 {
7128 if (gsym->is_mips16())
7129 // We don't need the call_stub; this is a 16 bit
7130 // function, so calls from other 16 bit functions are
7131 // OK.
7132 discard = true;
7133 else if (gsym->has_mips16_call_stub())
7134 // We already have a stub for this function.
7135 discard = true;
7136 else
7137 gsym->set_mips16_call_stub(stub_section);
7138 }
7139 else
7140 {
7141 gold_assert(stub_section->is_call_fp_stub());
7142 if (gsym->is_mips16())
7143 // We don't need the call_stub; this is a 16 bit
7144 // function, so calls from other 16 bit functions are
7145 // OK.
7146 discard = true;
7147 else if (gsym->has_mips16_call_fp_stub())
7148 // We already have a stub for this function.
7149 discard = true;
7150 else
7151 gsym->set_mips16_call_fp_stub(stub_section);
7152 }
7153 }
7154 if (discard)
7155 this->set_output_section(stub_section->shndx(), NULL);
7156 }
7157}
7158
7159// Mips_output_data_la25_stub methods.
7160
7161// Template for standard LA25 stub.
7162template<int size, bool big_endian>
7163const uint32_t
7164Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] =
7165{
7166 0x3c190000, // lui $25,%hi(func)
7167 0x08000000, // j func
7168 0x27390000, // add $25,$25,%lo(func)
7169 0x00000000 // nop
7170};
7171
7172// Template for microMIPS LA25 stub.
7173template<int size, bool big_endian>
7174const uint32_t
7175Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] =
7176{
7177 0x41b9, 0x0000, // lui t9,%hi(func)
7178 0xd400, 0x0000, // j func
7179 0x3339, 0x0000, // addiu t9,t9,%lo(func)
7180 0x0000, 0x0000 // nop
7181};
7182
7183// Create la25 stub for a symbol.
7184
7185template<int size, bool big_endian>
7186void
7187Mips_output_data_la25_stub<size, big_endian>::create_la25_stub(
7188 Symbol_table* symtab, Target_mips<size, big_endian>* target,
7189 Mips_symbol<size>* gsym)
7190{
7191 if (!gsym->has_la25_stub())
7192 {
7193 gsym->set_la25_stub_offset(this->symbols_.size() * 16);
15eb1beb 7194 this->symbols_.push_back(gsym);
9810d34d
SS
7195 this->create_stub_symbol(gsym, symtab, target, 16);
7196 }
7197}
7198
7199// Create a symbol for SYM stub's value and size, to help make the disassembly
7200// easier to read.
7201
7202template<int size, bool big_endian>
7203void
7204Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
7205 Mips_symbol<size>* sym, Symbol_table* symtab,
7206 Target_mips<size, big_endian>* target, uint64_t symsize)
7207{
7208 std::string name(".pic.");
7209 name += sym->name();
7210
7211 unsigned int offset = sym->la25_stub_offset();
7212 if (sym->is_micromips())
7213 offset |= 1;
7214
7215 // Make it a local function.
7216 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL,
7217 Symbol_table::PREDEFINED,
7218 target->la25_stub_section(),
7219 offset, symsize, elfcpp::STT_FUNC,
7220 elfcpp::STB_LOCAL,
7221 elfcpp::STV_DEFAULT, 0,
7222 false, false);
7223 new_sym->set_is_forced_local();
7224}
7225
7226// Write out la25 stubs. This uses the hand-coded instructions above,
7227// and adjusts them as needed.
7228
7229template<int size, bool big_endian>
7230void
7231Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
7232{
7233 const off_t offset = this->offset();
7234 const section_size_type oview_size =
7235 convert_to_section_size_type(this->data_size());
7236 unsigned char* const oview = of->get_output_view(offset, oview_size);
7237
15eb1beb 7238 for (typename std::vector<Mips_symbol<size>*>::iterator
9810d34d
SS
7239 p = this->symbols_.begin();
7240 p != this->symbols_.end();
7241 ++p)
7242 {
7243 Mips_symbol<size>* sym = *p;
7244 unsigned char* pov = oview + sym->la25_stub_offset();
7245
7246 Mips_address target = sym->value();
7247 if (!sym->is_micromips())
7248 {
7249 elfcpp::Swap<32, big_endian>::writeval(pov,
7250 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff));
7251 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7252 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff));
7253 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7254 la25_stub_entry[2] | (target & 0xffff));
7255 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]);
7256 }
7257 else
7258 {
7259 target |= 1;
7260 // First stub instruction. Paste high 16-bits of the target.
7261 elfcpp::Swap<16, big_endian>::writeval(pov,
7262 la25_stub_micromips_entry[0]);
7263 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7264 ((target + 0x8000) >> 16) & 0xffff);
7265 // Second stub instruction. Paste low 26-bits of the target, shifted
7266 // right by 1.
7267 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
7268 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff));
7269 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
7270 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff));
7271 // Third stub instruction. Paste low 16-bits of the target.
7272 elfcpp::Swap<16, big_endian>::writeval(pov + 8,
7273 la25_stub_micromips_entry[4]);
7274 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff);
7275 // Fourth stub instruction.
7276 elfcpp::Swap<16, big_endian>::writeval(pov + 12,
7277 la25_stub_micromips_entry[6]);
7278 elfcpp::Swap<16, big_endian>::writeval(pov + 14,
7279 la25_stub_micromips_entry[7]);
7280 }
7281 }
7282
7283 of->write_output_view(offset, oview_size, oview);
7284}
7285
7286// Mips_output_data_plt methods.
7287
7288// The format of the first PLT entry in an O32 executable.
7289template<int size, bool big_endian>
7290const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] =
7291{
7292 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
7293 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
7294 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
7295 0x031cc023, // subu $24, $24, $28
40fc1451 7296 0x03e07825, // or $15, $31, zero
9810d34d
SS
7297 0x0018c082, // srl $24, $24, 2
7298 0x0320f809, // jalr $25
7299 0x2718fffe // subu $24, $24, 2
7300};
7301
7302// The format of the first PLT entry in an N32 executable. Different
7303// because gp ($28) is not available; we use t2 ($14) instead.
7304template<int size, bool big_endian>
7305const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] =
7306{
7307 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7308 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
7309 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7310 0x030ec023, // subu $24, $24, $14
40fc1451 7311 0x03e07825, // or $15, $31, zero
9810d34d
SS
7312 0x0018c082, // srl $24, $24, 2
7313 0x0320f809, // jalr $25
7314 0x2718fffe // subu $24, $24, 2
7315};
7316
7317// The format of the first PLT entry in an N64 executable. Different
7318// from N32 because of the increased size of GOT entries.
7319template<int size, bool big_endian>
7320const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] =
7321{
7322 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
7323 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
7324 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
7325 0x030ec023, // subu $24, $24, $14
40fc1451 7326 0x03e07825, // or $15, $31, zero
9810d34d
SS
7327 0x0018c0c2, // srl $24, $24, 3
7328 0x0320f809, // jalr $25
7329 0x2718fffe // subu $24, $24, 2
7330};
7331
7332// The format of the microMIPS first PLT entry in an O32 executable.
7333// We rely on v0 ($2) rather than t8 ($24) to contain the address
7334// of the GOTPLT entry handled, so this stub may only be used when
7335// all the subsequent PLT entries are microMIPS code too.
7336//
7337// The trailing NOP is for alignment and correct disassembly only.
7338template<int size, bool big_endian>
7339const uint32_t Mips_output_data_plt<size, big_endian>::
7340plt0_entry_micromips_o32[] =
7341{
7342 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - .
7343 0xff23, 0x0000, // lw $25, 0($3)
7344 0x0535, // subu $2, $2, $3
7345 0x2525, // srl $2, $2, 2
7346 0x3302, 0xfffe, // subu $24, $2, 2
7347 0x0dff, // move $15, $31
7348 0x45f9, // jalrs $25
7349 0x0f83, // move $28, $3
7350 0x0c00 // nop
7351};
7352
7353// The format of the microMIPS first PLT entry in an O32 executable
7354// in the insn32 mode.
7355template<int size, bool big_endian>
7356const uint32_t Mips_output_data_plt<size, big_endian>::
7357plt0_entry_micromips32_o32[] =
7358{
7359 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0])
7360 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28)
7361 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0])
7362 0x0398, 0xc1d0, // subu $24, $24, $28
40fc1451 7363 0x001f, 0x7a90, // or $15, $31, zero
9810d34d
SS
7364 0x0318, 0x1040, // srl $24, $24, 2
7365 0x03f9, 0x0f3c, // jalr $25
7366 0x3318, 0xfffe // subu $24, $24, 2
7367};
7368
7369// The format of subsequent standard entries in the PLT.
7370template<int size, bool big_endian>
7371const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] =
7372{
7373 0x3c0f0000, // lui $15, %hi(.got.plt entry)
47a9f4fc 7374 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
9810d34d
SS
7375 0x03200008, // jr $25
7376 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7377};
7378
f5b11759
VR
7379// The format of subsequent R6 PLT entries.
7380template<int size, bool big_endian>
7381const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_r6[] =
7382{
7383 0x3c0f0000, // lui $15, %hi(.got.plt entry)
7384 0x01f90000, // l[wd] $25, %lo(.got.plt entry)($15)
7385 0x03200009, // jr $25
7386 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
7387};
7388
9810d34d
SS
7389// The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a
7390// temporary because t8 ($24) and t9 ($25) are not directly addressable.
7391// Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3).
7392// We cannot use v0 because MIPS16 call stubs from the CS toolchain expect
7393// target function address in register v0.
7394template<int size, bool big_endian>
7395const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] =
7396{
7397 0xb303, // lw $3, 12($pc)
7398 0x651b, // move $24, $3
7399 0x9b60, // lw $3, 0($3)
7400 0xeb00, // jr $3
7401 0x653b, // move $25, $3
7402 0x6500, // nop
7403 0x0000, 0x0000 // .word (.got.plt entry)
7404};
7405
7406// The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
7407// as a temporary because t8 ($24) is not addressable with ADDIUPC.
7408template<int size, bool big_endian>
7409const uint32_t Mips_output_data_plt<size, big_endian>::
7410plt_entry_micromips_o32[] =
7411{
7412 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - .
7413 0xff22, 0x0000, // lw $25, 0($2)
7414 0x4599, // jr $25
7415 0x0f02 // move $24, $2
7416};
7417
7418// The format of subsequent microMIPS o32 PLT entries in the insn32 mode.
7419template<int size, bool big_endian>
7420const uint32_t Mips_output_data_plt<size, big_endian>::
7421plt_entry_micromips32_o32[] =
7422{
7423 0x41af, 0x0000, // lui $15, %hi(.got.plt entry)
7424 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15)
7425 0x0019, 0x0f3c, // jr $25
7426 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry)
7427};
7428
7429// Add an entry to the PLT for a symbol referenced by r_type relocation.
7430
7431template<int size, bool big_endian>
7432void
7433Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym,
7434 unsigned int r_type)
7435{
7436 gold_assert(!gsym->has_plt_offset());
7437
7438 // Final PLT offset for a symbol will be set in method set_plt_offsets().
7439 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry)
7440 + sizeof(plt0_entry_o32));
7441 this->symbols_.push_back(gsym);
7442
7443 // Record whether the relocation requires a standard MIPS
7444 // or a compressed code entry.
7445 if (jal_reloc(r_type))
7446 {
7447 if (r_type == elfcpp::R_MIPS_26)
7448 gsym->set_needs_mips_plt(true);
7449 else
7450 gsym->set_needs_comp_plt(true);
7451 }
7452
7453 section_offset_type got_offset = this->got_plt_->current_data_size();
7454
7455 // Every PLT entry needs a GOT entry which points back to the PLT
7456 // entry (this will be changed by the dynamic linker, normally
7457 // lazily when the function is called).
7458 this->got_plt_->set_current_data_size(got_offset + size/8);
7459
7460 gsym->set_needs_dynsym_entry();
7461 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
7462 got_offset);
7463}
7464
7465// Set final PLT offsets. For each symbol, determine whether standard or
7466// compressed (MIPS16 or microMIPS) PLT entry is used.
7467
7468template<int size, bool big_endian>
7469void
7470Mips_output_data_plt<size, big_endian>::set_plt_offsets()
7471{
7472 // The sizes of individual PLT entries.
7473 unsigned int plt_mips_entry_size = this->standard_plt_entry_size();
7474 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi()
7475 ? this->compressed_plt_entry_size() : 0);
7476
7477 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7478 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
7479 {
7480 Mips_symbol<size>* mips_sym = *p;
7481
7482 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64,
7483 // so always use a standard entry there.
7484 //
7485 // If the symbol has a MIPS16 call stub and gets a PLT entry, then
7486 // all MIPS16 calls will go via that stub, and there is no benefit
7487 // to having a MIPS16 entry. And in the case of call_stub a
7488 // standard entry actually has to be used as the stub ends with a J
7489 // instruction.
7490 if (this->target_->is_output_newabi()
7491 || mips_sym->has_mips16_call_stub()
7492 || mips_sym->has_mips16_call_fp_stub())
7493 {
7494 mips_sym->set_needs_mips_plt(true);
7495 mips_sym->set_needs_comp_plt(false);
7496 }
7497
7498 // Otherwise, if there are no direct calls to the function, we
7499 // have a free choice of whether to use standard or compressed
7500 // entries. Prefer microMIPS entries if the object is known to
7501 // contain microMIPS code, so that it becomes possible to create
7502 // pure microMIPS binaries. Prefer standard entries otherwise,
7503 // because MIPS16 ones are no smaller and are usually slower.
7504 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt())
7505 {
7506 if (this->target_->is_output_micromips())
7507 mips_sym->set_needs_comp_plt(true);
7508 else
7509 mips_sym->set_needs_mips_plt(true);
7510 }
7511
7512 if (mips_sym->needs_mips_plt())
7513 {
7514 mips_sym->set_mips_plt_offset(this->plt_mips_offset_);
7515 this->plt_mips_offset_ += plt_mips_entry_size;
7516 }
7517 if (mips_sym->needs_comp_plt())
7518 {
7519 mips_sym->set_comp_plt_offset(this->plt_comp_offset_);
7520 this->plt_comp_offset_ += plt_comp_entry_size;
7521 }
7522 }
7523
7524 // Figure out the size of the PLT header if we know that we are using it.
7525 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0)
7526 this->plt_header_size_ = this->get_plt_header_size();
7527}
7528
7529// Write out the PLT. This uses the hand-coded instructions above,
7530// and adjusts them as needed.
7531
7532template<int size, bool big_endian>
7533void
7534Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
7535{
7536 const off_t offset = this->offset();
7537 const section_size_type oview_size =
7538 convert_to_section_size_type(this->data_size());
7539 unsigned char* const oview = of->get_output_view(offset, oview_size);
7540
7541 const off_t gotplt_file_offset = this->got_plt_->offset();
7542 const section_size_type gotplt_size =
7543 convert_to_section_size_type(this->got_plt_->data_size());
7544 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset,
7545 gotplt_size);
7546 unsigned char* pov = oview;
7547
7548 Mips_address plt_address = this->address();
7549
7550 // Calculate the address of .got.plt.
7551 Mips_address gotplt_addr = this->got_plt_->address();
7552 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff;
7553 Mips_address gotplt_addr_low = gotplt_addr & 0xffff;
7554
7555 // The PLT sequence is not safe for N64 if .got.plt's address can
7556 // not be loaded in two instructions.
7557 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0
7558 || ~(gotplt_addr | 0x7fffffff) == 0);
7559
7560 // Write the PLT header.
7561 const uint32_t* plt0_entry = this->get_plt_header_entry();
7562 if (plt0_entry == plt0_entry_micromips_o32)
7563 {
7564 // Write microMIPS PLT header.
7565 gold_assert(gotplt_addr % 4 == 0);
7566
7567 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3);
7568
7569 // ADDIUPC has a span of +/-16MB, check we're in range.
7570 if (gotpc_offset + 0x1000000 >= 0x2000000)
7571 {
7572 gold_error(_(".got.plt offset of %ld from .plt beyond the range of "
7573 "ADDIUPC"), (long)gotpc_offset);
7574 return;
7575 }
7576
7577 elfcpp::Swap<16, big_endian>::writeval(pov,
7578 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7579 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
7580 (gotpc_offset >> 2) & 0xffff);
7581 pov += 4;
7582 for (unsigned int i = 2;
7583 i < (sizeof(plt0_entry_micromips_o32)
7584 / sizeof(plt0_entry_micromips_o32[0]));
7585 i++)
7586 {
7587 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7588 pov += 2;
7589 }
7590 }
7591 else if (plt0_entry == plt0_entry_micromips32_o32)
7592 {
7593 // Write microMIPS PLT header in insn32 mode.
7594 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]);
7595 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high);
7596 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]);
7597 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low);
7598 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]);
7599 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low);
7600 pov += 12;
7601 for (unsigned int i = 6;
7602 i < (sizeof(plt0_entry_micromips32_o32)
7603 / sizeof(plt0_entry_micromips32_o32[0]));
7604 i++)
7605 {
7606 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]);
7607 pov += 2;
7608 }
7609 }
7610 else
7611 {
7612 // Write standard PLT header.
7613 elfcpp::Swap<32, big_endian>::writeval(pov,
7614 plt0_entry[0] | gotplt_addr_high);
7615 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
7616 plt0_entry[1] | gotplt_addr_low);
7617 elfcpp::Swap<32, big_endian>::writeval(pov + 8,
7618 plt0_entry[2] | gotplt_addr_low);
7619 pov += 12;
7620 for (int i = 3; i < 8; i++)
7621 {
7622 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]);
7623 pov += 4;
7624 }
7625 }
7626
7627
7628 unsigned char* gotplt_pov = gotplt_view;
7629 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE
7630
7631 // The first two entries in .got.plt are reserved.
7632 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0);
7633 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0);
7634
7635 unsigned int gotplt_offset = 2 * got_entry_size;
7636 gotplt_pov += 2 * got_entry_size;
7637
7638 // Calculate the address of the PLT header.
7639 Mips_address header_address = (plt_address
7640 + (this->is_plt_header_compressed() ? 1 : 0));
7641
7642 // Initialize compressed PLT area view.
7643 unsigned char* pov2 = pov + this->plt_mips_offset_;
7644
7645 // Write the PLT entries.
7646 for (typename std::vector<Mips_symbol<size>*>::const_iterator
7647 p = this->symbols_.begin();
7648 p != this->symbols_.end();
7649 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size)
7650 {
7651 Mips_symbol<size>* mips_sym = *p;
7652
7653 // Calculate the address of the .got.plt entry.
7654 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset);
7655 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16)
7656 & 0xffff);
7657 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff;
7658
7659 // Initially point the .got.plt entry at the PLT header.
7660 if (this->target_->is_output_n64())
7661 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address);
7662 else
7663 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address);
7664
7665 // Now handle the PLT itself. First the standard entry.
7666 if (mips_sym->has_mips_plt_offset())
7667 {
7668 // Pick the load opcode (LW or LD).
7669 uint64_t load = this->target_->is_output_n64() ? 0xdc000000
7670 : 0x8c000000;
7671
f5b11759
VR
7672 const uint32_t* entry = this->target_->is_output_r6() ? plt_entry_r6
7673 : plt_entry;
7674
9810d34d
SS
7675 // Fill in the PLT entry itself.
7676 elfcpp::Swap<32, big_endian>::writeval(pov,
f5b11759 7677 entry[0] | gotplt_entry_addr_hi);
9810d34d 7678 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
f5b11759
VR
7679 entry[1] | gotplt_entry_addr_lo | load);
7680 elfcpp::Swap<32, big_endian>::writeval(pov + 8, entry[2]);
9810d34d 7681 elfcpp::Swap<32, big_endian>::writeval(pov + 12,
f5b11759 7682 entry[3] | gotplt_entry_addr_lo);
9810d34d
SS
7683 pov += 16;
7684 }
7685
7686 // Now the compressed entry. They come after any standard ones.
7687 if (mips_sym->has_comp_plt_offset())
7688 {
7689 if (!this->target_->is_output_micromips())
7690 {
7691 // Write MIPS16 PLT entry.
7692 const uint32_t* plt_entry = plt_entry_mips16_o32;
7693
7694 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7695 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]);
7696 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7697 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7698 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7699 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7700 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12,
7701 gotplt_entry_addr);
7702 pov2 += 16;
7703 }
7704 else if (this->target_->use_32bit_micromips_instructions())
7705 {
7706 // Write microMIPS PLT entry in insn32 mode.
7707 const uint32_t* plt_entry = plt_entry_micromips32_o32;
7708
7709 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]);
7710 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2,
7711 gotplt_entry_addr_hi);
7712 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7713 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6,
7714 gotplt_entry_addr_lo);
7715 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7716 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7717 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]);
7718 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14,
7719 gotplt_entry_addr_lo);
7720 pov2 += 16;
7721 }
7722 else
7723 {
7724 // Write microMIPS PLT entry.
7725 const uint32_t* plt_entry = plt_entry_micromips_o32;
7726
7727 gold_assert(gotplt_entry_addr % 4 == 0);
7728
7729 Mips_address loc_address = plt_address + pov2 - oview;
7730 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3);
7731
7732 // ADDIUPC has a span of +/-16MB, check we're in range.
7733 if (gotpc_offset + 0x1000000 >= 0x2000000)
7734 {
7735 gold_error(_(".got.plt offset of %ld from .plt beyond the "
7736 "range of ADDIUPC"), (long)gotpc_offset);
7737 return;
7738 }
7739
7740 elfcpp::Swap<16, big_endian>::writeval(pov2,
7741 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f));
7742 elfcpp::Swap<16, big_endian>::writeval(
7743 pov2 + 2, (gotpc_offset >> 2) & 0xffff);
7744 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]);
7745 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]);
7746 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]);
7747 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]);
7748 pov2 += 12;
7749 }
7750 }
7751 }
7752
7753 // Check the number of bytes written for standard entries.
7754 gold_assert(static_cast<section_size_type>(
7755 pov - oview - this->plt_header_size_) == this->plt_mips_offset_);
7756 // Check the number of bytes written for compressed entries.
7757 gold_assert((static_cast<section_size_type>(pov2 - pov)
7758 == this->plt_comp_offset_));
7759 // Check the total number of bytes written.
7760 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size);
7761
7762 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view)
7763 == gotplt_size);
7764
7765 of->write_output_view(offset, oview_size, oview);
7766 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view);
7767}
7768
7769// Mips_output_data_mips_stubs methods.
7770
7771// The format of the lazy binding stub when dynamic symbol count is less than
7772// 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7773template<int size, bool big_endian>
7774const uint32_t
7775Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
7776{
7777 0x8f998010, // lw t9,0x8010(gp)
40fc1451 7778 0x03e07825, // or t7,ra,zero
9810d34d
SS
7779 0x0320f809, // jalr t9,ra
7780 0x24180000 // addiu t8,zero,DYN_INDEX sign extended
7781};
7782
7783// The format of the lazy binding stub when dynamic symbol count is less than
7784// 64K, dynamic symbol index is less than 32K, and ABI is N64.
7785template<int size, bool big_endian>
7786const uint32_t
7787Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
7788{
7789 0xdf998010, // ld t9,0x8010(gp)
40fc1451 7790 0x03e07825, // or t7,ra,zero
9810d34d
SS
7791 0x0320f809, // jalr t9,ra
7792 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended
7793};
7794
7795// The format of the lazy binding stub when dynamic symbol count is less than
7796// 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
7797template<int size, bool big_endian>
7798const uint32_t
7799Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
7800{
7801 0x8f998010, // lw t9,0x8010(gp)
40fc1451 7802 0x03e07825, // or t7,ra,zero
9810d34d
SS
7803 0x0320f809, // jalr t9,ra
7804 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7805};
7806
7807// The format of the lazy binding stub when dynamic symbol count is less than
7808// 64K, dynamic symbol index is between 32K and 64K, and ABI is N64.
7809template<int size, bool big_endian>
7810const uint32_t
7811Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
7812{
7813 0xdf998010, // ld t9,0x8010(gp)
40fc1451 7814 0x03e07825, // or t7,ra,zero
9810d34d
SS
7815 0x0320f809, // jalr t9,ra
7816 0x34180000 // ori t8,zero,DYN_INDEX unsigned
7817};
7818
7819// The format of the lazy binding stub when dynamic symbol count is greater than
7820// 64K, and ABI is not N64.
7821template<int size, bool big_endian>
7822const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
7823{
7824 0x8f998010, // lw t9,0x8010(gp)
40fc1451 7825 0x03e07825, // or t7,ra,zero
9810d34d
SS
7826 0x3c180000, // lui t8,DYN_INDEX
7827 0x0320f809, // jalr t9,ra
7828 0x37180000 // ori t8,t8,DYN_INDEX
7829};
7830
7831// The format of the lazy binding stub when dynamic symbol count is greater than
7832// 64K, and ABI is N64.
7833template<int size, bool big_endian>
7834const uint32_t
7835Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
7836{
7837 0xdf998010, // ld t9,0x8010(gp)
40fc1451 7838 0x03e07825, // or t7,ra,zero
9810d34d
SS
7839 0x3c180000, // lui t8,DYN_INDEX
7840 0x0320f809, // jalr t9,ra
7841 0x37180000 // ori t8,t8,DYN_INDEX
7842};
7843
7844// microMIPS stubs.
7845
7846// The format of the microMIPS lazy binding stub when dynamic symbol count is
7847// less than 64K, dynamic symbol index is less than 32K, and ABI is not N64.
7848template<int size, bool big_endian>
7849const uint32_t
7850Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] =
7851{
7852 0xff3c, 0x8010, // lw t9,0x8010(gp)
7853 0x0dff, // move t7,ra
7854 0x45d9, // jalr t9
7855 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7856};
7857
7858// The format of the microMIPS lazy binding stub when dynamic symbol count is
7859// less than 64K, dynamic symbol index is less than 32K, and ABI is N64.
7860template<int size, bool big_endian>
7861const uint32_t
7862Mips_output_data_mips_stubs<size, big_endian>::
7863lazy_stub_micromips_normal_1_n64[] =
7864{
7865 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7866 0x0dff, // move t7,ra
7867 0x45d9, // jalr t9
7868 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7869};
7870
7871// The format of the microMIPS lazy binding stub when dynamic symbol
7872// count is less than 64K, dynamic symbol index is between 32K and 64K,
7873// and ABI is not N64.
7874template<int size, bool big_endian>
7875const uint32_t
7876Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] =
7877{
7878 0xff3c, 0x8010, // lw t9,0x8010(gp)
7879 0x0dff, // move t7,ra
7880 0x45d9, // jalr t9
7881 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7882};
7883
7884// The format of the microMIPS lazy binding stub when dynamic symbol
7885// count is less than 64K, dynamic symbol index is between 32K and 64K,
7886// and ABI is N64.
7887template<int size, bool big_endian>
7888const uint32_t
7889Mips_output_data_mips_stubs<size, big_endian>::
7890lazy_stub_micromips_normal_2_n64[] =
7891{
7892 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7893 0x0dff, // move t7,ra
7894 0x45d9, // jalr t9
7895 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7896};
7897
7898// The format of the microMIPS lazy binding stub when dynamic symbol count is
7899// greater than 64K, and ABI is not N64.
7900template<int size, bool big_endian>
7901const uint32_t
7902Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] =
7903{
7904 0xff3c, 0x8010, // lw t9,0x8010(gp)
7905 0x0dff, // move t7,ra
7906 0x41b8, 0x0000, // lui t8,DYN_INDEX
7907 0x45d9, // jalr t9
7908 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7909};
7910
7911// The format of the microMIPS lazy binding stub when dynamic symbol count is
7912// greater than 64K, and ABI is N64.
7913template<int size, bool big_endian>
7914const uint32_t
7915Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] =
7916{
7917 0xdf3c, 0x8010, // ld t9,0x8010(gp)
7918 0x0dff, // move t7,ra
7919 0x41b8, 0x0000, // lui t8,DYN_INDEX
7920 0x45d9, // jalr t9
7921 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7922};
7923
7924// 32-bit microMIPS stubs.
7925
7926// The format of the microMIPS lazy binding stub when dynamic symbol count is
7927// less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we
7928// can use only 32-bit instructions.
7929template<int size, bool big_endian>
7930const uint32_t
7931Mips_output_data_mips_stubs<size, big_endian>::
7932lazy_stub_micromips32_normal_1[] =
7933{
7934 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7935 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7936 0x03f9, 0x0f3c, // jalr ra,t9
7937 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended
7938};
7939
7940// The format of the microMIPS lazy binding stub when dynamic symbol count is
7941// less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can
7942// use only 32-bit instructions.
7943template<int size, bool big_endian>
7944const uint32_t
7945Mips_output_data_mips_stubs<size, big_endian>::
7946lazy_stub_micromips32_normal_1_n64[] =
7947{
7948 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7949 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7950 0x03f9, 0x0f3c, // jalr ra,t9
7951 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended
7952};
7953
7954// The format of the microMIPS lazy binding stub when dynamic symbol
7955// count is less than 64K, dynamic symbol index is between 32K and 64K,
7956// ABI is not N64, and we can use only 32-bit instructions.
7957template<int size, bool big_endian>
7958const uint32_t
7959Mips_output_data_mips_stubs<size, big_endian>::
7960lazy_stub_micromips32_normal_2[] =
7961{
7962 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7963 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7964 0x03f9, 0x0f3c, // jalr ra,t9
7965 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7966};
7967
7968// The format of the microMIPS lazy binding stub when dynamic symbol
7969// count is less than 64K, dynamic symbol index is between 32K and 64K,
7970// ABI is N64, and we can use only 32-bit instructions.
7971template<int size, bool big_endian>
7972const uint32_t
7973Mips_output_data_mips_stubs<size, big_endian>::
7974lazy_stub_micromips32_normal_2_n64[] =
7975{
7976 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 7977 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7978 0x03f9, 0x0f3c, // jalr ra,t9
7979 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned
7980};
7981
7982// The format of the microMIPS lazy binding stub when dynamic symbol count is
7983// greater than 64K, ABI is not N64, and we can use only 32-bit instructions.
7984template<int size, bool big_endian>
7985const uint32_t
7986Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] =
7987{
7988 0xff3c, 0x8010, // lw t9,0x8010(gp)
40fc1451 7989 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
7990 0x41b8, 0x0000, // lui t8,DYN_INDEX
7991 0x03f9, 0x0f3c, // jalr ra,t9
7992 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
7993};
7994
7995// The format of the microMIPS lazy binding stub when dynamic symbol count is
7996// greater than 64K, ABI is N64, and we can use only 32-bit instructions.
7997template<int size, bool big_endian>
7998const uint32_t
7999Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] =
8000{
8001 0xdf3c, 0x8010, // ld t9,0x8010(gp)
40fc1451 8002 0x001f, 0x7a90, // or t7,ra,zero
9810d34d
SS
8003 0x41b8, 0x0000, // lui t8,DYN_INDEX
8004 0x03f9, 0x0f3c, // jalr ra,t9
8005 0x5318, 0x0000 // ori t8,t8,DYN_INDEX
8006};
8007
8008// Create entry for a symbol.
8009
8010template<int size, bool big_endian>
8011void
8012Mips_output_data_mips_stubs<size, big_endian>::make_entry(
8013 Mips_symbol<size>* gsym)
8014{
8015 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset())
8016 {
8017 this->symbols_.insert(gsym);
8018 gsym->set_has_lazy_stub(true);
8019 }
8020}
8021
8022// Remove entry for a symbol.
8023
8024template<int size, bool big_endian>
8025void
8026Mips_output_data_mips_stubs<size, big_endian>::remove_entry(
8027 Mips_symbol<size>* gsym)
8028{
8029 if (gsym->has_lazy_stub())
8030 {
8031 this->symbols_.erase(gsym);
8032 gsym->set_has_lazy_stub(false);
8033 }
8034}
8035
8036// Set stub offsets for symbols. This method expects that the number of
8037// entries in dynamic symbol table is set.
8038
8039template<int size, bool big_endian>
8040void
8041Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
8042{
8043 gold_assert(this->dynsym_count_ != -1U);
8044
8045 if (this->stub_offsets_are_set_)
8046 return;
8047
8048 unsigned int stub_size = this->stub_size();
8049 unsigned int offset = 0;
15eb1beb 8050 for (typename Mips_stubs_entry_set::const_iterator
9810d34d
SS
8051 p = this->symbols_.begin();
8052 p != this->symbols_.end();
8053 ++p, offset += stub_size)
8054 {
8055 Mips_symbol<size>* mips_sym = *p;
8056 mips_sym->set_lazy_stub_offset(offset);
8057 }
8058 this->stub_offsets_are_set_ = true;
8059}
8060
8061template<int size, bool big_endian>
8062void
8063Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
8064{
15eb1beb 8065 for (typename Mips_stubs_entry_set::const_iterator
9810d34d
SS
8066 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8067 {
8068 Mips_symbol<size>* sym = *p;
8069 if (sym->is_from_dynobj())
8070 sym->set_needs_dynsym_value();
8071 }
8072}
8073
8074// Write out the .MIPS.stubs. This uses the hand-coded instructions and
8075// adjusts them as needed.
8076
8077template<int size, bool big_endian>
8078void
8079Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
8080{
8081 const off_t offset = this->offset();
8082 const section_size_type oview_size =
8083 convert_to_section_size_type(this->data_size());
8084 unsigned char* const oview = of->get_output_view(offset, oview_size);
8085
8086 bool big_stub = this->dynsym_count_ > 0x10000;
8087
8088 unsigned char* pov = oview;
15eb1beb 8089 for (typename Mips_stubs_entry_set::const_iterator
9810d34d
SS
8090 p = this->symbols_.begin(); p != this->symbols_.end(); ++p)
8091 {
8092 Mips_symbol<size>* sym = *p;
8093 const uint32_t* lazy_stub;
8094 bool n64 = this->target_->is_output_n64();
8095
8096 if (!this->target_->is_output_micromips())
8097 {
8098 // Write standard (non-microMIPS) stub.
8099 if (!big_stub)
8100 {
8101 if (sym->dynsym_index() & ~0x7fff)
8102 // Dynsym index is between 32K and 64K.
8103 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
8104 else
8105 // Dynsym index is less than 32K.
8106 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
8107 }
8108 else
8109 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
8110
8111 unsigned int i = 0;
8112 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8113 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]);
8114 pov += 8;
8115
8116 i += 2;
8117 if (big_stub)
8118 {
8119 // LUI instruction of the big stub. Paste high 16 bits of the
8120 // dynsym index.
8121 elfcpp::Swap<32, big_endian>::writeval(pov,
8122 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff));
8123 pov += 4;
8124 i += 1;
8125 }
8126 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]);
8127 // Last stub instruction. Paste low 16 bits of the dynsym index.
8128 elfcpp::Swap<32, big_endian>::writeval(pov + 4,
8129 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff));
8130 pov += 8;
8131 }
8132 else if (this->target_->use_32bit_micromips_instructions())
8133 {
8134 // Write microMIPS stub in insn32 mode.
8135 if (!big_stub)
8136 {
8137 if (sym->dynsym_index() & ~0x7fff)
8138 // Dynsym index is between 32K and 64K.
8139 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64
8140 : lazy_stub_micromips32_normal_2;
8141 else
8142 // Dynsym index is less than 32K.
8143 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64
8144 : lazy_stub_micromips32_normal_1;
8145 }
8146 else
8147 lazy_stub = n64 ? lazy_stub_micromips32_big_n64
8148 : lazy_stub_micromips32_big;
8149
8150 unsigned int i = 0;
8151 // First stub instruction. We emit 32-bit microMIPS instructions by
8152 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8153 // the instruction where the opcode is must always come first, for
8154 // both little and big endian.
8155 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8156 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8157 // Second stub instruction.
8158 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8159 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]);
8160 pov += 8;
8161 i += 4;
8162 if (big_stub)
8163 {
8164 // LUI instruction of the big stub. Paste high 16 bits of the
8165 // dynsym index.
8166 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8167 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8168 (sym->dynsym_index() >> 16) & 0x7fff);
8169 pov += 4;
8170 i += 2;
8171 }
8172 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8173 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8174 // Last stub instruction. Paste low 16 bits of the dynsym index.
8175 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8176 elfcpp::Swap<16, big_endian>::writeval(pov + 6,
8177 sym->dynsym_index() & 0xffff);
8178 pov += 8;
8179 }
8180 else
8181 {
8182 // Write microMIPS stub.
8183 if (!big_stub)
8184 {
8185 if (sym->dynsym_index() & ~0x7fff)
8186 // Dynsym index is between 32K and 64K.
8187 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64
8188 : lazy_stub_micromips_normal_2;
8189 else
8190 // Dynsym index is less than 32K.
8191 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64
8192 : lazy_stub_micromips_normal_1;
8193 }
8194 else
8195 lazy_stub = n64 ? lazy_stub_micromips_big_n64
8196 : lazy_stub_micromips_big;
8197
8198 unsigned int i = 0;
8199 // First stub instruction. We emit 32-bit microMIPS instructions by
8200 // emitting two 16-bit parts because on microMIPS the 16-bit part of
8201 // the instruction where the opcode is must always come first, for
8202 // both little and big endian.
8203 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8204 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8205 // Second stub instruction.
8206 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]);
8207 pov += 6;
8208 i += 3;
8209 if (big_stub)
8210 {
8211 // LUI instruction of the big stub. Paste high 16 bits of the
8212 // dynsym index.
8213 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8214 elfcpp::Swap<16, big_endian>::writeval(pov + 2,
8215 (sym->dynsym_index() >> 16) & 0x7fff);
8216 pov += 4;
8217 i += 2;
8218 }
8219 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]);
8220 // Last stub instruction. Paste low 16 bits of the dynsym index.
8221 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]);
8222 elfcpp::Swap<16, big_endian>::writeval(pov + 4,
8223 sym->dynsym_index() & 0xffff);
8224 pov += 6;
8225 }
8226 }
8227
8228 // We always allocate 20 bytes for every stub, because final dynsym count is
8229 // not known in method do_finalize_sections. There are 4 unused bytes per
8230 // stub if final dynsym count is less than 0x10000.
8231 unsigned int used = pov - oview;
8232 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
8233 gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
8234
8235 // Fill the unused space with zeroes.
8236 // TODO(sasa): Can we strip unused bytes during the relaxation?
8237 if (unused > 0)
8238 memset(pov, 0, unused);
8239
8240 of->write_output_view(offset, oview_size, oview);
8241}
8242
8243// Mips_output_section_reginfo methods.
8244
8245template<int size, bool big_endian>
8246void
8247Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
8248{
8249 off_t offset = this->offset();
8250 off_t data_size = this->data_size();
8251
8252 unsigned char* view = of->get_output_view(offset, data_size);
8253 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_);
8254 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_);
8255 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_);
8256 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_);
8257 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_);
8258 // Write the gp value.
8259 elfcpp::Swap<size, big_endian>::writeval(view + 20,
8260 this->target_->gp_value());
8261
8262 of->write_output_view(offset, data_size, view);
8263}
8264
1728969e
VR
8265// Mips_output_section_options methods.
8266
8267template<int size, bool big_endian>
8268void
8269Mips_output_section_options<size, big_endian>::do_write(Output_file* of)
8270{
8271 off_t offset = this->offset();
8272 const section_size_type oview_size =
8273 convert_to_section_size_type(this->data_size());
8274 unsigned char* view = of->get_output_view(offset, oview_size);
8275 const unsigned char* end = view + oview_size;
8276
8277 while (view + 8 <= end)
8278 {
8279 unsigned char kind = elfcpp::Swap<8, big_endian>::readval(view);
8280 unsigned char sz = elfcpp::Swap<8, big_endian>::readval(view + 1);
8281 if (sz < 8)
8282 {
8283 gold_error(_("Warning: bad `%s' option size %u smaller "
8284 "than its header in output section"),
8285 this->name(), sz);
8286 break;
8287 }
8288
8289 // Only update ri_gp_value (GP register value) field of ODK_REGINFO entry.
8290 if (this->target_->is_output_n64() && kind == elfcpp::ODK_REGINFO)
8291 elfcpp::Swap<size, big_endian>::writeval(view + 32,
8292 this->target_->gp_value());
8293 else if (kind == elfcpp::ODK_REGINFO)
8294 elfcpp::Swap<size, big_endian>::writeval(view + 28,
8295 this->target_->gp_value());
8296
8297 view += sz;
8298 }
8299
8300 of->write_output_view(offset, oview_size, view);
8301}
8302
b52717c0
VR
8303// Mips_output_section_abiflags methods.
8304
8305template<int size, bool big_endian>
8306void
8307Mips_output_section_abiflags<size, big_endian>::do_write(Output_file* of)
8308{
8309 off_t offset = this->offset();
8310 off_t data_size = this->data_size();
8311
8312 unsigned char* view = of->get_output_view(offset, data_size);
8313 elfcpp::Swap<16, big_endian>::writeval(view, this->abiflags_.version);
8314 elfcpp::Swap<8, big_endian>::writeval(view + 2, this->abiflags_.isa_level);
8315 elfcpp::Swap<8, big_endian>::writeval(view + 3, this->abiflags_.isa_rev);
8316 elfcpp::Swap<8, big_endian>::writeval(view + 4, this->abiflags_.gpr_size);
8317 elfcpp::Swap<8, big_endian>::writeval(view + 5, this->abiflags_.cpr1_size);
8318 elfcpp::Swap<8, big_endian>::writeval(view + 6, this->abiflags_.cpr2_size);
8319 elfcpp::Swap<8, big_endian>::writeval(view + 7, this->abiflags_.fp_abi);
8320 elfcpp::Swap<32, big_endian>::writeval(view + 8, this->abiflags_.isa_ext);
8321 elfcpp::Swap<32, big_endian>::writeval(view + 12, this->abiflags_.ases);
8322 elfcpp::Swap<32, big_endian>::writeval(view + 16, this->abiflags_.flags1);
8323 elfcpp::Swap<32, big_endian>::writeval(view + 20, this->abiflags_.flags2);
8324
8325 of->write_output_view(offset, data_size, view);
8326}
8327
9810d34d
SS
8328// Mips_copy_relocs methods.
8329
8330// Emit any saved relocs.
8331
8332template<int sh_type, int size, bool big_endian>
8333void
8334Mips_copy_relocs<sh_type, size, big_endian>::emit_mips(
8335 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8336 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8337{
8338 for (typename Copy_relocs<sh_type, size, big_endian>::
8339 Copy_reloc_entries::iterator p = this->entries_.begin();
8340 p != this->entries_.end();
8341 ++p)
8342 emit_entry(*p, reloc_section, symtab, layout, target);
8343
8344 // We no longer need the saved information.
8345 this->entries_.clear();
8346}
8347
8348// Emit the reloc if appropriate.
8349
8350template<int sh_type, int size, bool big_endian>
8351void
8352Mips_copy_relocs<sh_type, size, big_endian>::emit_entry(
8353 Copy_reloc_entry& entry,
8354 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section,
8355 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target)
8356{
8357 // If the symbol is no longer defined in a dynamic object, then we
8358 // emitted a COPY relocation, and we do not want to emit this
8359 // dynamic relocation.
8360 if (!entry.sym_->is_from_dynobj())
8361 return;
8362
8363 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32
8364 || entry.reloc_type_ == elfcpp::R_MIPS_REL32
8365 || entry.reloc_type_ == elfcpp::R_MIPS_64);
8366
8367 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_);
8368 if (can_make_dynamic && !sym->has_static_relocs())
8369 {
8370 Mips_relobj<size, big_endian>* object =
8371 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_);
8372 target->got_section(symtab, layout)->record_global_got_symbol(
8373 sym, object, entry.reloc_type_, true, false);
8374 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab)))
8375 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32,
8376 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_);
8377 else
8378 target->rel_dyn_section(layout)->add_symbolless_global_addend(
8379 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_,
8380 entry.shndx_, entry.address_);
8381 }
8382 else
8383 this->make_copy_reloc(symtab, layout,
8384 static_cast<Sized_symbol<size>*>(entry.sym_),
6eeb0170 8385 entry.relobj_,
9810d34d
SS
8386 reloc_section);
8387}
8388
8389// Target_mips methods.
8390
8391// Return the value to use for a dynamic symbol which requires special
8392// treatment. This is how we support equality comparisons of function
8393// pointers across shared library boundaries, as described in the
8394// processor specific ABI supplement.
8395
8396template<int size, bool big_endian>
8397uint64_t
8398Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8399{
8400 uint64_t value = 0;
8401 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
8402
8403 if (!mips_sym->has_lazy_stub())
8404 {
8405 if (mips_sym->has_plt_offset())
8406 {
8407 // We distinguish between PLT entries and lazy-binding stubs by
8408 // giving the former an st_other value of STO_MIPS_PLT. Set the
8409 // value to the stub address if there are any relocations in the
8410 // binary where pointer equality matters.
8411 if (mips_sym->pointer_equality_needed())
8412 {
8413 // Prefer a standard MIPS PLT entry.
8414 if (mips_sym->has_mips_plt_offset())
8415 value = this->plt_section()->mips_entry_address(mips_sym);
8416 else
8417 value = this->plt_section()->comp_entry_address(mips_sym) + 1;
8418 }
8419 else
8420 value = 0;
8421 }
8422 }
8423 else
8424 {
8425 // First, set stub offsets for symbols. This method expects that the
8426 // number of entries in dynamic symbol table is set.
8427 this->mips_stubs_section()->set_lazy_stub_offsets();
8428
8429 // The run-time linker uses the st_value field of the symbol
8430 // to reset the global offset table entry for this external
8431 // to its stub address when unlinking a shared object.
8432 value = this->mips_stubs_section()->stub_address(mips_sym);
8433 }
8434
8435 if (mips_sym->has_mips16_fn_stub())
8436 {
8437 // If we have a MIPS16 function with a stub, the dynamic symbol must
8438 // refer to the stub, since only the stub uses the standard calling
8439 // conventions.
8440 value = mips_sym->template
8441 get_mips16_fn_stub<big_endian>()->output_address();
8442 }
8443
8444 return value;
8445}
8446
8447// Get the dynamic reloc section, creating it if necessary. It's always
8448// .rel.dyn, even for MIPS64.
8449
8450template<int size, bool big_endian>
8451typename Target_mips<size, big_endian>::Reloc_section*
8452Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
8453{
8454 if (this->rel_dyn_ == NULL)
8455 {
8456 gold_assert(layout != NULL);
8457 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
8458 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
8459 elfcpp::SHF_ALLOC, this->rel_dyn_,
8460 ORDER_DYNAMIC_RELOCS, false);
8461
8462 // First entry in .rel.dyn has to be null.
8463 // This is hack - we define dummy output data and set its address to 0,
8464 // and define absolute R_MIPS_NONE relocation with offset 0 against it.
8465 // This ensures that the entry is null.
8466 Output_data* od = new Output_data_zero_fill(0, 0);
8467 od->set_address(0);
8468 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
8469 }
8470 return this->rel_dyn_;
8471}
8472
8473// Get the GOT section, creating it if necessary.
8474
8475template<int size, bool big_endian>
8476Mips_output_data_got<size, big_endian>*
8477Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
8478 Layout* layout)
8479{
8480 if (this->got_ == NULL)
8481 {
8482 gold_assert(symtab != NULL && layout != NULL);
8483
8484 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
8485 layout);
8486 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
8487 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
8488 elfcpp::SHF_MIPS_GPREL),
8489 this->got_, ORDER_DATA, false);
8490
8491 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
8492 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
8493 Symbol_table::PREDEFINED,
8494 this->got_,
8495 0, 0, elfcpp::STT_OBJECT,
8496 elfcpp::STB_GLOBAL,
453018bf 8497 elfcpp::STV_HIDDEN, 0,
9810d34d
SS
8498 false, false);
8499 }
8500
8501 return this->got_;
8502}
8503
8504// Calculate value of _gp symbol.
8505
8506template<int size, bool big_endian>
8507void
8508Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
8509{
453018bf 8510 gold_assert(this->gp_ == NULL);
9810d34d
SS
8511
8512 Sized_symbol<size>* gp =
8513 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
453018bf
VR
8514
8515 // Set _gp symbol if the linker script hasn't created it.
8516 if (gp == NULL || gp->source() != Symbol::IS_CONSTANT)
9810d34d 8517 {
453018bf
VR
8518 // If there is no .got section, gp should be based on .sdata.
8519 Output_data* gp_section = (this->got_ != NULL
8520 ? this->got_->output_section()
8521 : layout->find_output_section(".sdata"));
8522
8523 if (gp_section != NULL)
8524 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
8525 "_gp", NULL, Symbol_table::PREDEFINED,
8526 gp_section, MIPS_GP_OFFSET, 0,
8527 elfcpp::STT_NOTYPE,
8528 elfcpp::STB_LOCAL,
8529 elfcpp::STV_DEFAULT,
8530 0, false, false));
9810d34d 8531 }
453018bf
VR
8532
8533 this->gp_ = gp;
9810d34d
SS
8534}
8535
8536// Set the dynamic symbol indexes. INDEX is the index of the first
8537// global dynamic symbol. Pointers to the symbols are stored into the
8538// vector SYMS. The names are added to DYNPOOL. This returns an
8539// updated dynamic symbol index.
8540
8541template<int size, bool big_endian>
8542unsigned int
8543Target_mips<size, big_endian>::do_set_dynsym_indexes(
8544 std::vector<Symbol*>* dyn_symbols, unsigned int index,
8545 std::vector<Symbol*>* syms, Stringpool* dynpool,
8546 Versions* versions, Symbol_table* symtab) const
8547{
8548 std::vector<Symbol*> non_got_symbols;
8549 std::vector<Symbol*> got_symbols;
8550
8551 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols,
8552 &got_symbols);
8553
8554 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
8555 p != non_got_symbols.end();
8556 ++p)
8557 {
8558 Symbol* sym = *p;
8559
8560 // Note that SYM may already have a dynamic symbol index, since
8561 // some symbols appear more than once in the symbol table, with
8562 // and without a version.
8563
8564 if (!sym->has_dynsym_index())
8565 {
8566 sym->set_dynsym_index(index);
8567 ++index;
8568 syms->push_back(sym);
8569 dynpool->add(sym->name(), false, NULL);
8570
8571 // Record any version information.
8572 if (sym->version() != NULL)
8573 versions->record_version(symtab, dynpool, sym);
8574
8575 // If the symbol is defined in a dynamic object and is
8576 // referenced in a regular object, then mark the dynamic
8577 // object as needed. This is used to implement --as-needed.
8578 if (sym->is_from_dynobj() && sym->in_reg())
8579 sym->object()->set_is_needed();
8580 }
8581 }
8582
8583 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8584 p != got_symbols.end();
8585 ++p)
8586 {
8587 Symbol* sym = *p;
8588 if (!sym->has_dynsym_index())
8589 {
8590 // Record any version information.
8591 if (sym->version() != NULL)
8592 versions->record_version(symtab, dynpool, sym);
8593 }
8594 }
8595
8596 index = versions->finalize(symtab, index, syms);
8597
8598 int got_sym_count = 0;
8599 for (std::vector<Symbol*>::iterator p = got_symbols.begin();
8600 p != got_symbols.end();
8601 ++p)
8602 {
8603 Symbol* sym = *p;
8604
8605 if (!sym->has_dynsym_index())
8606 {
8607 ++got_sym_count;
8608 sym->set_dynsym_index(index);
8609 ++index;
8610 syms->push_back(sym);
8611 dynpool->add(sym->name(), false, NULL);
8612
8613 // If the symbol is defined in a dynamic object and is
8614 // referenced in a regular object, then mark the dynamic
8615 // object as needed. This is used to implement --as-needed.
8616 if (sym->is_from_dynobj() && sym->in_reg())
8617 sym->object()->set_is_needed();
8618 }
8619 }
8620
8621 // Set index of the first symbol that has .got entry.
8622 this->got_->set_first_global_got_dynsym_index(
8623 got_sym_count > 0 ? index - got_sym_count : -1U);
8624
8625 if (this->mips_stubs_ != NULL)
8626 this->mips_stubs_->set_dynsym_count(index);
8627
8628 return index;
8629}
8630
8631// Create a PLT entry for a global symbol referenced by r_type relocation.
8632
8633template<int size, bool big_endian>
8634void
8635Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
8636 Layout* layout,
8637 Mips_symbol<size>* gsym,
8638 unsigned int r_type)
8639{
8640 if (gsym->has_lazy_stub() || gsym->has_plt_offset())
8641 return;
8642
8643 if (this->plt_ == NULL)
8644 {
8645 // Create the GOT section first.
8646 this->got_section(symtab, layout);
8647
8648 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
8649 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
8650 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
8651 this->got_plt_, ORDER_DATA, false);
8652
8653 // The first two entries are reserved.
8654 this->got_plt_->set_current_data_size(2 * size/8);
8655
8656 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
8657 this->got_plt_,
8658 this);
8659 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
8660 (elfcpp::SHF_ALLOC
8661 | elfcpp::SHF_EXECINSTR),
8662 this->plt_, ORDER_PLT, false);
04bc2a28
VR
8663
8664 // Make the sh_info field of .rel.plt point to .plt.
8665 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
8666 rel_plt_os->set_info_section(this->plt_->output_section());
9810d34d
SS
8667 }
8668
8669 this->plt_->add_entry(gsym, r_type);
8670}
8671
8672
8673// Get the .MIPS.stubs section, creating it if necessary.
8674
8675template<int size, bool big_endian>
8676Mips_output_data_mips_stubs<size, big_endian>*
8677Target_mips<size, big_endian>::mips_stubs_section(Layout* layout)
8678{
8679 if (this->mips_stubs_ == NULL)
8680 {
8681 this->mips_stubs_ =
8682 new Mips_output_data_mips_stubs<size, big_endian>(this);
8683 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
8684 (elfcpp::SHF_ALLOC
8685 | elfcpp::SHF_EXECINSTR),
8686 this->mips_stubs_, ORDER_PLT, false);
8687 }
8688 return this->mips_stubs_;
8689}
8690
8691// Get the LA25 stub section, creating it if necessary.
8692
8693template<int size, bool big_endian>
8694Mips_output_data_la25_stub<size, big_endian>*
8695Target_mips<size, big_endian>::la25_stub_section(Layout* layout)
8696{
8697 if (this->la25_stub_ == NULL)
8698 {
8699 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
8700 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8701 (elfcpp::SHF_ALLOC
8702 | elfcpp::SHF_EXECINSTR),
8703 this->la25_stub_, ORDER_TEXT, false);
8704 }
8705 return this->la25_stub_;
8706}
8707
8708// Process the relocations to determine unreferenced sections for
8709// garbage collection.
8710
8711template<int size, bool big_endian>
8712void
8713Target_mips<size, big_endian>::gc_process_relocs(
8714 Symbol_table* symtab,
8715 Layout* layout,
8716 Sized_relobj_file<size, big_endian>* object,
8717 unsigned int data_shndx,
47a9f4fc 8718 unsigned int sh_type,
9810d34d
SS
8719 const unsigned char* prelocs,
8720 size_t reloc_count,
8721 Output_section* output_section,
8722 bool needs_special_offset_handling,
8723 size_t local_symbol_count,
8724 const unsigned char* plocal_symbols)
8725{
8726 typedef Target_mips<size, big_endian> Mips;
9810d34d 8727
47a9f4fc
VR
8728 if (sh_type == elfcpp::SHT_REL)
8729 {
8730 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8731 Classify_reloc;
8732
8733 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8734 symtab,
8735 layout,
8736 this,
8737 object,
8738 data_shndx,
8739 prelocs,
8740 reloc_count,
8741 output_section,
8742 needs_special_offset_handling,
8743 local_symbol_count,
8744 plocal_symbols);
8745 }
8746 else if (sh_type == elfcpp::SHT_RELA)
8747 {
8748 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8749 Classify_reloc;
8750
8751 gold::gc_process_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8752 symtab,
8753 layout,
8754 this,
8755 object,
8756 data_shndx,
8757 prelocs,
8758 reloc_count,
8759 output_section,
8760 needs_special_offset_handling,
8761 local_symbol_count,
8762 plocal_symbols);
8763 }
8764 else
8765 gold_unreachable();
9810d34d
SS
8766}
8767
8768// Scan relocations for a section.
8769
8770template<int size, bool big_endian>
8771void
8772Target_mips<size, big_endian>::scan_relocs(
8773 Symbol_table* symtab,
8774 Layout* layout,
8775 Sized_relobj_file<size, big_endian>* object,
8776 unsigned int data_shndx,
8777 unsigned int sh_type,
8778 const unsigned char* prelocs,
8779 size_t reloc_count,
8780 Output_section* output_section,
8781 bool needs_special_offset_handling,
8782 size_t local_symbol_count,
8783 const unsigned char* plocal_symbols)
8784{
8785 typedef Target_mips<size, big_endian> Mips;
9810d34d
SS
8786
8787 if (sh_type == elfcpp::SHT_REL)
4d625b70
CC
8788 {
8789 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
8790 Classify_reloc;
8791
8792 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8793 symtab,
8794 layout,
8795 this,
8796 object,
8797 data_shndx,
8798 prelocs,
8799 reloc_count,
8800 output_section,
8801 needs_special_offset_handling,
8802 local_symbol_count,
8803 plocal_symbols);
8804 }
9810d34d 8805 else if (sh_type == elfcpp::SHT_RELA)
4d625b70
CC
8806 {
8807 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8808 Classify_reloc;
8809
8810 gold::scan_relocs<size, big_endian, Mips, Scan, Classify_reloc>(
8811 symtab,
8812 layout,
8813 this,
8814 object,
8815 data_shndx,
8816 prelocs,
8817 reloc_count,
8818 output_section,
8819 needs_special_offset_handling,
8820 local_symbol_count,
8821 plocal_symbols);
8822 }
9810d34d
SS
8823}
8824
8825template<int size, bool big_endian>
8826bool
8827Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
8828{
8829 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
8830 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
8831 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
8832 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
8833 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
8834 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
f5b11759
VR
8835 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2
8836 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R6);
9810d34d
SS
8837}
8838
8839// Return the MACH for a MIPS e_flags value.
8840template<int size, bool big_endian>
8841unsigned int
8842Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
8843{
8844 switch (flags & elfcpp::EF_MIPS_MACH)
8845 {
8846 case elfcpp::E_MIPS_MACH_3900:
8847 return mach_mips3900;
8848
8849 case elfcpp::E_MIPS_MACH_4010:
8850 return mach_mips4010;
8851
8852 case elfcpp::E_MIPS_MACH_4100:
8853 return mach_mips4100;
8854
8855 case elfcpp::E_MIPS_MACH_4111:
8856 return mach_mips4111;
8857
8858 case elfcpp::E_MIPS_MACH_4120:
8859 return mach_mips4120;
8860
8861 case elfcpp::E_MIPS_MACH_4650:
8862 return mach_mips4650;
8863
8864 case elfcpp::E_MIPS_MACH_5400:
8865 return mach_mips5400;
8866
8867 case elfcpp::E_MIPS_MACH_5500:
8868 return mach_mips5500;
8869
b52717c0
VR
8870 case elfcpp::E_MIPS_MACH_5900:
8871 return mach_mips5900;
8872
9810d34d
SS
8873 case elfcpp::E_MIPS_MACH_9000:
8874 return mach_mips9000;
8875
8876 case elfcpp::E_MIPS_MACH_SB1:
8877 return mach_mips_sb1;
8878
8879 case elfcpp::E_MIPS_MACH_LS2E:
8880 return mach_mips_loongson_2e;
8881
8882 case elfcpp::E_MIPS_MACH_LS2F:
8883 return mach_mips_loongson_2f;
8884
8885 case elfcpp::E_MIPS_MACH_LS3A:
8886 return mach_mips_loongson_3a;
8887
b52717c0
VR
8888 case elfcpp::E_MIPS_MACH_OCTEON3:
8889 return mach_mips_octeon3;
8890
9810d34d
SS
8891 case elfcpp::E_MIPS_MACH_OCTEON2:
8892 return mach_mips_octeon2;
8893
8894 case elfcpp::E_MIPS_MACH_OCTEON:
8895 return mach_mips_octeon;
8896
8897 case elfcpp::E_MIPS_MACH_XLR:
8898 return mach_mips_xlr;
8899
8900 default:
8901 switch (flags & elfcpp::EF_MIPS_ARCH)
8902 {
8903 default:
8904 case elfcpp::E_MIPS_ARCH_1:
8905 return mach_mips3000;
8906
8907 case elfcpp::E_MIPS_ARCH_2:
8908 return mach_mips6000;
8909
8910 case elfcpp::E_MIPS_ARCH_3:
8911 return mach_mips4000;
8912
8913 case elfcpp::E_MIPS_ARCH_4:
8914 return mach_mips8000;
8915
8916 case elfcpp::E_MIPS_ARCH_5:
8917 return mach_mips5;
8918
8919 case elfcpp::E_MIPS_ARCH_32:
8920 return mach_mipsisa32;
8921
8922 case elfcpp::E_MIPS_ARCH_64:
8923 return mach_mipsisa64;
8924
8925 case elfcpp::E_MIPS_ARCH_32R2:
8926 return mach_mipsisa32r2;
8927
f5b11759
VR
8928 case elfcpp::E_MIPS_ARCH_32R6:
8929 return mach_mipsisa32r6;
8930
9810d34d
SS
8931 case elfcpp::E_MIPS_ARCH_64R2:
8932 return mach_mipsisa64r2;
f5b11759
VR
8933
8934 case elfcpp::E_MIPS_ARCH_64R6:
8935 return mach_mipsisa64r6;
9810d34d
SS
8936 }
8937 }
8938
8939 return 0;
8940}
8941
b52717c0
VR
8942// Return the MACH for each .MIPS.abiflags ISA Extension.
8943
8944template<int size, bool big_endian>
8945unsigned int
8946Target_mips<size, big_endian>::mips_isa_ext_mach(unsigned int isa_ext)
8947{
8948 switch (isa_ext)
8949 {
8950 case elfcpp::AFL_EXT_3900:
8951 return mach_mips3900;
8952
8953 case elfcpp::AFL_EXT_4010:
8954 return mach_mips4010;
8955
8956 case elfcpp::AFL_EXT_4100:
8957 return mach_mips4100;
8958
8959 case elfcpp::AFL_EXT_4111:
8960 return mach_mips4111;
8961
8962 case elfcpp::AFL_EXT_4120:
8963 return mach_mips4120;
8964
8965 case elfcpp::AFL_EXT_4650:
8966 return mach_mips4650;
8967
8968 case elfcpp::AFL_EXT_5400:
8969 return mach_mips5400;
8970
8971 case elfcpp::AFL_EXT_5500:
8972 return mach_mips5500;
8973
8974 case elfcpp::AFL_EXT_5900:
8975 return mach_mips5900;
8976
8977 case elfcpp::AFL_EXT_10000:
8978 return mach_mips10000;
8979
8980 case elfcpp::AFL_EXT_LOONGSON_2E:
8981 return mach_mips_loongson_2e;
8982
8983 case elfcpp::AFL_EXT_LOONGSON_2F:
8984 return mach_mips_loongson_2f;
8985
8986 case elfcpp::AFL_EXT_LOONGSON_3A:
8987 return mach_mips_loongson_3a;
8988
8989 case elfcpp::AFL_EXT_SB1:
8990 return mach_mips_sb1;
8991
8992 case elfcpp::AFL_EXT_OCTEON:
8993 return mach_mips_octeon;
8994
8995 case elfcpp::AFL_EXT_OCTEONP:
8996 return mach_mips_octeonp;
8997
8998 case elfcpp::AFL_EXT_OCTEON2:
8999 return mach_mips_octeon2;
9000
9001 case elfcpp::AFL_EXT_XLR:
9002 return mach_mips_xlr;
9003
9004 default:
9005 return mach_mips3000;
9006 }
9007}
9008
9009// Return the .MIPS.abiflags value representing each ISA Extension.
9010
9011template<int size, bool big_endian>
9012unsigned int
9013Target_mips<size, big_endian>::mips_isa_ext(unsigned int mips_mach)
9014{
9015 switch (mips_mach)
9016 {
9017 case mach_mips3900:
9018 return elfcpp::AFL_EXT_3900;
9019
9020 case mach_mips4010:
9021 return elfcpp::AFL_EXT_4010;
9022
9023 case mach_mips4100:
9024 return elfcpp::AFL_EXT_4100;
9025
9026 case mach_mips4111:
9027 return elfcpp::AFL_EXT_4111;
9028
9029 case mach_mips4120:
9030 return elfcpp::AFL_EXT_4120;
9031
9032 case mach_mips4650:
9033 return elfcpp::AFL_EXT_4650;
9034
9035 case mach_mips5400:
9036 return elfcpp::AFL_EXT_5400;
9037
9038 case mach_mips5500:
9039 return elfcpp::AFL_EXT_5500;
9040
9041 case mach_mips5900:
9042 return elfcpp::AFL_EXT_5900;
9043
9044 case mach_mips10000:
9045 return elfcpp::AFL_EXT_10000;
9046
9047 case mach_mips_loongson_2e:
9048 return elfcpp::AFL_EXT_LOONGSON_2E;
9049
9050 case mach_mips_loongson_2f:
9051 return elfcpp::AFL_EXT_LOONGSON_2F;
9052
9053 case mach_mips_loongson_3a:
9054 return elfcpp::AFL_EXT_LOONGSON_3A;
9055
9056 case mach_mips_sb1:
9057 return elfcpp::AFL_EXT_SB1;
9058
9059 case mach_mips_octeon:
9060 return elfcpp::AFL_EXT_OCTEON;
9061
9062 case mach_mips_octeonp:
9063 return elfcpp::AFL_EXT_OCTEONP;
9064
9065 case mach_mips_octeon3:
9066 return elfcpp::AFL_EXT_OCTEON3;
9067
9068 case mach_mips_octeon2:
9069 return elfcpp::AFL_EXT_OCTEON2;
9070
9071 case mach_mips_xlr:
9072 return elfcpp::AFL_EXT_XLR;
9073
9074 default:
9075 return 0;
9076 }
9077}
9078
9079// Update the isa_level, isa_rev, isa_ext fields of abiflags.
9080
9081template<int size, bool big_endian>
9082void
9083Target_mips<size, big_endian>::update_abiflags_isa(const std::string& name,
9084 elfcpp::Elf_Word e_flags, Mips_abiflags<big_endian>* abiflags)
9085{
9086 int new_isa = 0;
9087 switch (e_flags & elfcpp::EF_MIPS_ARCH)
9088 {
9089 case elfcpp::E_MIPS_ARCH_1:
9090 new_isa = this->level_rev(1, 0);
9091 break;
9092 case elfcpp::E_MIPS_ARCH_2:
9093 new_isa = this->level_rev(2, 0);
9094 break;
9095 case elfcpp::E_MIPS_ARCH_3:
9096 new_isa = this->level_rev(3, 0);
9097 break;
9098 case elfcpp::E_MIPS_ARCH_4:
9099 new_isa = this->level_rev(4, 0);
9100 break;
9101 case elfcpp::E_MIPS_ARCH_5:
9102 new_isa = this->level_rev(5, 0);
9103 break;
9104 case elfcpp::E_MIPS_ARCH_32:
9105 new_isa = this->level_rev(32, 1);
9106 break;
9107 case elfcpp::E_MIPS_ARCH_32R2:
9108 new_isa = this->level_rev(32, 2);
9109 break;
f5b11759
VR
9110 case elfcpp::E_MIPS_ARCH_32R6:
9111 new_isa = this->level_rev(32, 6);
9112 break;
b52717c0
VR
9113 case elfcpp::E_MIPS_ARCH_64:
9114 new_isa = this->level_rev(64, 1);
9115 break;
9116 case elfcpp::E_MIPS_ARCH_64R2:
9117 new_isa = this->level_rev(64, 2);
9118 break;
f5b11759
VR
9119 case elfcpp::E_MIPS_ARCH_64R6:
9120 new_isa = this->level_rev(64, 6);
9121 break;
b52717c0
VR
9122 default:
9123 gold_error(_("%s: Unknown architecture %s"), name.c_str(),
9124 this->elf_mips_mach_name(e_flags));
9125 }
9126
9127 if (new_isa > this->level_rev(abiflags->isa_level, abiflags->isa_rev))
9128 {
9129 // Decode a single value into level and revision.
9130 abiflags->isa_level = new_isa >> 3;
9131 abiflags->isa_rev = new_isa & 0x7;
9132 }
9133
9134 // Update the isa_ext if needed.
9135 if (this->mips_mach_extends(this->mips_isa_ext_mach(abiflags->isa_ext),
9136 this->elf_mips_mach(e_flags)))
9137 abiflags->isa_ext = this->mips_isa_ext(this->elf_mips_mach(e_flags));
9138}
9139
9140// Infer the content of the ABI flags based on the elf header.
9141
9142template<int size, bool big_endian>
9143void
9144Target_mips<size, big_endian>::infer_abiflags(
9145 Mips_relobj<size, big_endian>* relobj, Mips_abiflags<big_endian>* abiflags)
9146{
9147 const Attributes_section_data* pasd = relobj->attributes_section_data();
9148 int attr_fp_abi = elfcpp::Val_GNU_MIPS_ABI_FP_ANY;
9149 elfcpp::Elf_Word e_flags = relobj->processor_specific_flags();
9150
9151 this->update_abiflags_isa(relobj->name(), e_flags, abiflags);
9152 if (pasd != NULL)
9153 {
9154 // Read fp_abi from the .gnu.attribute section.
9155 const Object_attribute* attr =
9156 pasd->known_attributes(Object_attribute::OBJ_ATTR_GNU);
9157 attr_fp_abi = attr[elfcpp::Tag_GNU_MIPS_ABI_FP].int_value();
9158 }
9159
9160 abiflags->fp_abi = attr_fp_abi;
9161 abiflags->cpr1_size = elfcpp::AFL_REG_NONE;
9162 abiflags->cpr2_size = elfcpp::AFL_REG_NONE;
9163 abiflags->gpr_size = this->mips_32bit_flags(e_flags) ? elfcpp::AFL_REG_32
9164 : elfcpp::AFL_REG_64;
9165
9166 if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE
9167 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9168 || (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9169 && abiflags->gpr_size == elfcpp::AFL_REG_32))
9170 abiflags->cpr1_size = elfcpp::AFL_REG_32;
9171 else if (abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9172 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9173 || abiflags->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A)
9174 abiflags->cpr1_size = elfcpp::AFL_REG_64;
9175
9176 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MDMX)
9177 abiflags->ases |= elfcpp::AFL_ASE_MDMX;
9178 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_M16)
9179 abiflags->ases |= elfcpp::AFL_ASE_MIPS16;
9180 if (e_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS)
9181 abiflags->ases |= elfcpp::AFL_ASE_MICROMIPS;
9182
9183 if (abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9184 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_SOFT
9185 && abiflags->fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_64A
9186 && abiflags->isa_level >= 32
9187 && abiflags->isa_ext != elfcpp::AFL_EXT_LOONGSON_3A)
9188 abiflags->flags1 |= elfcpp::AFL_FLAGS1_ODDSPREG;
9189}
9190
9191// Create abiflags from elf header or from .MIPS.abiflags section.
9192
9193template<int size, bool big_endian>
9194void
9195Target_mips<size, big_endian>::create_abiflags(
9196 Mips_relobj<size, big_endian>* relobj,
9197 Mips_abiflags<big_endian>* abiflags)
9198{
9199 Mips_abiflags<big_endian>* sec_abiflags = relobj->abiflags();
9200 Mips_abiflags<big_endian> header_abiflags;
9201
9202 this->infer_abiflags(relobj, &header_abiflags);
9203
9204 if (sec_abiflags == NULL)
9205 {
9206 // If there is no input .MIPS.abiflags section, use abiflags created
9207 // from elf header.
9208 *abiflags = header_abiflags;
9209 return;
9210 }
9211
9212 this->has_abiflags_section_ = true;
9213
9214 // It is not possible to infer the correct ISA revision for R3 or R5
9215 // so drop down to R2 for the checks.
9216 unsigned char isa_rev = sec_abiflags->isa_rev;
9217 if (isa_rev == 3 || isa_rev == 5)
9218 isa_rev = 2;
9219
9220 // Check compatibility between abiflags created from elf header
9221 // and abiflags from .MIPS.abiflags section in this object file.
9222 if (this->level_rev(sec_abiflags->isa_level, isa_rev)
9223 < this->level_rev(header_abiflags.isa_level, header_abiflags.isa_rev))
9224 gold_warning(_("%s: Inconsistent ISA between e_flags and .MIPS.abiflags"),
9225 relobj->name().c_str());
9226 if (header_abiflags.fp_abi != elfcpp::Val_GNU_MIPS_ABI_FP_ANY
9227 && sec_abiflags->fp_abi != header_abiflags.fp_abi)
9228 gold_warning(_("%s: Inconsistent FP ABI between .gnu.attributes and "
9229 ".MIPS.abiflags"), relobj->name().c_str());
9230 if ((sec_abiflags->ases & header_abiflags.ases) != header_abiflags.ases)
9231 gold_warning(_("%s: Inconsistent ASEs between e_flags and .MIPS.abiflags"),
9232 relobj->name().c_str());
9233 // The isa_ext is allowed to be an extension of what can be inferred
9234 // from e_flags.
9235 if (!this->mips_mach_extends(this->mips_isa_ext_mach(header_abiflags.isa_ext),
9236 this->mips_isa_ext_mach(sec_abiflags->isa_ext)))
9237 gold_warning(_("%s: Inconsistent ISA extensions between e_flags and "
9238 ".MIPS.abiflags"), relobj->name().c_str());
9239 if (sec_abiflags->flags2 != 0)
9240 gold_warning(_("%s: Unexpected flag in the flags2 field of "
9241 ".MIPS.abiflags (0x%x)"), relobj->name().c_str(),
9242 sec_abiflags->flags2);
9243 // Use abiflags from .MIPS.abiflags section.
9244 *abiflags = *sec_abiflags;
9245}
9246
9247// Return the meaning of fp_abi, or "unknown" if not known.
9248
9249template<int size, bool big_endian>
9250const char*
9251Target_mips<size, big_endian>::fp_abi_string(int fp)
9252{
9253 switch (fp)
9254 {
9255 case elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE:
9256 return "-mdouble-float";
9257 case elfcpp::Val_GNU_MIPS_ABI_FP_SINGLE:
9258 return "-msingle-float";
9259 case elfcpp::Val_GNU_MIPS_ABI_FP_SOFT:
9260 return "-msoft-float";
9261 case elfcpp::Val_GNU_MIPS_ABI_FP_OLD_64:
9262 return _("-mips32r2 -mfp64 (12 callee-saved)");
9263 case elfcpp::Val_GNU_MIPS_ABI_FP_XX:
9264 return "-mfpxx";
9265 case elfcpp::Val_GNU_MIPS_ABI_FP_64:
9266 return "-mgp32 -mfp64";
9267 case elfcpp::Val_GNU_MIPS_ABI_FP_64A:
9268 return "-mgp32 -mfp64 -mno-odd-spreg";
9269 default:
9270 return "unknown";
9271 }
9272}
9273
9274// Select fp_abi.
9275
9276template<int size, bool big_endian>
9277int
9278Target_mips<size, big_endian>::select_fp_abi(const std::string& name, int in_fp,
9279 int out_fp)
9280{
9281 if (in_fp == out_fp)
9282 return out_fp;
9283
9284 if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9285 return in_fp;
9286 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9287 && (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9288 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9289 || in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9290 return in_fp;
9291 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_XX
9292 && (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_DOUBLE
9293 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64
9294 || out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9295 return out_fp; // Keep the current setting.
9296 else if (out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9297 && in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9298 return in_fp;
9299 else if (in_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64A
9300 && out_fp == elfcpp::Val_GNU_MIPS_ABI_FP_64)
9301 return out_fp; // Keep the current setting.
9302 else if (in_fp != elfcpp::Val_GNU_MIPS_ABI_FP_ANY)
9303 gold_warning(_("%s: FP ABI %s is incompatible with %s"), name.c_str(),
9304 fp_abi_string(in_fp), fp_abi_string(out_fp));
9305 return out_fp;
9306}
9307
9308// Merge attributes from input object.
9309
9310template<int size, bool big_endian>
9311void
9312Target_mips<size, big_endian>::merge_obj_attributes(const std::string& name,
9313 const Attributes_section_data* pasd)
9314{
9315 // Return if there is no attributes section data.
9316 if (pasd == NULL)
9317 return;
9318
9319 // If output has no object attributes, just copy.
9320 if (this->attributes_section_data_ == NULL)
9321 {
9322 this->attributes_section_data_ = new Attributes_section_data(*pasd);
9323 return;
9324 }
9325
9326 Object_attribute* out_attr = this->attributes_section_data_->known_attributes(
9327 Object_attribute::OBJ_ATTR_GNU);
9328
9329 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_type(1);
9330 out_attr[elfcpp::Tag_GNU_MIPS_ABI_FP].set_int_value(this->abiflags_->fp_abi);
9331
9332 // Merge Tag_compatibility attributes and any common GNU ones.
9333 this->attributes_section_data_->merge(name.c_str(), pasd);
9334}
9335
9336// Merge abiflags from input object.
9337
9338template<int size, bool big_endian>
9339void
9340Target_mips<size, big_endian>::merge_obj_abiflags(const std::string& name,
9341 Mips_abiflags<big_endian>* in_abiflags)
9342{
9343 // If output has no abiflags, just copy.
9344 if (this->abiflags_ == NULL)
9345 {
9346 this->abiflags_ = new Mips_abiflags<big_endian>(*in_abiflags);
9347 return;
9348 }
9349
9350 this->abiflags_->fp_abi = this->select_fp_abi(name, in_abiflags->fp_abi,
9351 this->abiflags_->fp_abi);
9352
9353 // Merge abiflags.
9354 this->abiflags_->isa_level = std::max(this->abiflags_->isa_level,
9355 in_abiflags->isa_level);
9356 this->abiflags_->isa_rev = std::max(this->abiflags_->isa_rev,
9357 in_abiflags->isa_rev);
9358 this->abiflags_->gpr_size = std::max(this->abiflags_->gpr_size,
9359 in_abiflags->gpr_size);
9360 this->abiflags_->cpr1_size = std::max(this->abiflags_->cpr1_size,
9361 in_abiflags->cpr1_size);
9362 this->abiflags_->cpr2_size = std::max(this->abiflags_->cpr2_size,
9363 in_abiflags->cpr2_size);
9364 this->abiflags_->ases |= in_abiflags->ases;
9365 this->abiflags_->flags1 |= in_abiflags->flags1;
9366}
9367
9810d34d
SS
9368// Check whether machine EXTENSION is an extension of machine BASE.
9369template<int size, bool big_endian>
9370bool
9371Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
9372 unsigned int extension)
9373{
9374 if (extension == base)
9375 return true;
9376
9377 if ((base == mach_mipsisa32)
9378 && this->mips_mach_extends(mach_mipsisa64, extension))
9379 return true;
9380
9381 if ((base == mach_mipsisa32r2)
9382 && this->mips_mach_extends(mach_mipsisa64r2, extension))
9383 return true;
9384
9385 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
9386 if (extension == this->mips_mach_extensions_[i].first)
9387 {
9388 extension = this->mips_mach_extensions_[i].second;
9389 if (extension == base)
9390 return true;
9391 }
9392
9393 return false;
9394}
9395
b52717c0
VR
9396// Merge file header flags from input object.
9397
9810d34d
SS
9398template<int size, bool big_endian>
9399void
b52717c0
VR
9400Target_mips<size, big_endian>::merge_obj_e_flags(const std::string& name,
9401 elfcpp::Elf_Word in_flags)
9810d34d
SS
9402{
9403 // If flags are not set yet, just copy them.
9404 if (!this->are_processor_specific_flags_set())
9405 {
9406 this->set_processor_specific_flags(in_flags);
9810d34d
SS
9407 this->mach_ = this->elf_mips_mach(in_flags);
9408 return;
9409 }
9410
9411 elfcpp::Elf_Word new_flags = in_flags;
9412 elfcpp::Elf_Word old_flags = this->processor_specific_flags();
9413 elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
9414 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
9415
9416 // Check flag compatibility.
9417 new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9418 old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
9419
9420 // Some IRIX 6 BSD-compatibility objects have this bit set. It
9421 // doesn't seem to matter.
9422 new_flags &= ~elfcpp::EF_MIPS_XGOT;
9423 old_flags &= ~elfcpp::EF_MIPS_XGOT;
9424
9425 // MIPSpro generates ucode info in n64 objects. Again, we should
9426 // just be able to ignore this.
9427 new_flags &= ~elfcpp::EF_MIPS_UCODE;
9428 old_flags &= ~elfcpp::EF_MIPS_UCODE;
9429
9810d34d
SS
9430 if (new_flags == old_flags)
9431 {
9432 this->set_processor_specific_flags(merged_flags);
9433 return;
9434 }
9435
9436 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
9437 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
9438 gold_warning(_("%s: linking abicalls files with non-abicalls files"),
9439 name.c_str());
9440
9441 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9442 merged_flags |= elfcpp::EF_MIPS_CPIC;
9443 if (!(new_flags & elfcpp::EF_MIPS_PIC))
9444 merged_flags &= ~elfcpp::EF_MIPS_PIC;
9445
9446 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9447 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
9448
9449 // Compare the ISAs.
9450 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
9451 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
9452 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
9453 {
9454 // Output ISA isn't the same as, or an extension of, input ISA.
9455 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
9456 {
9457 // Copy the architecture info from input object to output. Also copy
9458 // the 32-bit flag (if set) so that we continue to recognise
9459 // output as a 32-bit binary.
9460 this->mach_ = this->elf_mips_mach(in_flags);
9461 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
9462 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
9463 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
9464
b52717c0
VR
9465 // Update the ABI flags isa_level, isa_rev, isa_ext fields.
9466 this->update_abiflags_isa(name, merged_flags, this->abiflags_);
9467
9810d34d
SS
9468 // Copy across the ABI flags if output doesn't use them
9469 // and if that was what caused us to treat input object as 32-bit.
9470 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
9471 && this->mips_32bit_flags(new_flags)
9472 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
9473 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
9474 }
9475 else
9476 // The ISAs aren't compatible.
9477 gold_error(_("%s: linking %s module with previous %s modules"),
9478 name.c_str(), this->elf_mips_mach_name(in_flags),
9479 this->elf_mips_mach_name(merged_flags));
9480 }
9481
9482 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9483 | elfcpp::EF_MIPS_32BITMODE));
9484 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
9485 | elfcpp::EF_MIPS_32BITMODE));
9486
01b84e25
VR
9487 // Compare ABIs.
9488 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI))
9810d34d
SS
9489 {
9490 // Only error if both are set (to different values).
01b84e25 9491 if ((new_flags & elfcpp::EF_MIPS_ABI)
9810d34d 9492 && (old_flags & elfcpp::EF_MIPS_ABI))
9810d34d
SS
9493 gold_error(_("%s: ABI mismatch: linking %s module with "
9494 "previous %s modules"), name.c_str(),
01b84e25
VR
9495 this->elf_mips_abi_name(in_flags),
9496 this->elf_mips_abi_name(merged_flags));
9810d34d
SS
9497
9498 new_flags &= ~elfcpp::EF_MIPS_ABI;
9499 old_flags &= ~elfcpp::EF_MIPS_ABI;
9500 }
9501
9502 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
9503 // and allow arbitrary mixing of the remaining ASEs (retain the union).
9504 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
9505 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
9506 {
9507 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9508 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
9509 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9510 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
9511 int micro_mis = old_m16 && new_micro;
9512 int m16_mis = old_micro && new_m16;
9513
9514 if (m16_mis || micro_mis)
9515 gold_error(_("%s: ASE mismatch: linking %s module with "
9516 "previous %s modules"), name.c_str(),
9517 m16_mis ? "MIPS16" : "microMIPS",
9518 m16_mis ? "microMIPS" : "MIPS16");
9519
9520 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
9521
9522 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9523 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
9524 }
9525
b52717c0
VR
9526 // Compare NaN encodings.
9527 if ((new_flags & elfcpp::EF_MIPS_NAN2008) != (old_flags & elfcpp::EF_MIPS_NAN2008))
9528 {
9529 gold_error(_("%s: linking %s module with previous %s modules"),
9530 name.c_str(),
9531 (new_flags & elfcpp::EF_MIPS_NAN2008
9532 ? "-mnan=2008" : "-mnan=legacy"),
9533 (old_flags & elfcpp::EF_MIPS_NAN2008
9534 ? "-mnan=2008" : "-mnan=legacy"));
9535
9536 new_flags &= ~elfcpp::EF_MIPS_NAN2008;
9537 old_flags &= ~elfcpp::EF_MIPS_NAN2008;
9538 }
9539
9540 // Compare FP64 state.
9541 if ((new_flags & elfcpp::EF_MIPS_FP64) != (old_flags & elfcpp::EF_MIPS_FP64))
9542 {
9543 gold_error(_("%s: linking %s module with previous %s modules"),
9544 name.c_str(),
9545 (new_flags & elfcpp::EF_MIPS_FP64
9546 ? "-mfp64" : "-mfp32"),
9547 (old_flags & elfcpp::EF_MIPS_FP64
9548 ? "-mfp64" : "-mfp32"));
9549
9550 new_flags &= ~elfcpp::EF_MIPS_FP64;
9551 old_flags &= ~elfcpp::EF_MIPS_FP64;
9552 }
9553
9810d34d
SS
9554 // Warn about any other mismatches.
9555 if (new_flags != old_flags)
9556 gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
9557 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
9558
9559 this->set_processor_specific_flags(merged_flags);
9560}
9561
9562// Adjust ELF file header.
9563
9564template<int size, bool big_endian>
9565void
9566Target_mips<size, big_endian>::do_adjust_elf_header(
9567 unsigned char* view,
9568 int len)
9569{
9570 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
9571
01b84e25 9572 elfcpp::Ehdr<size, big_endian> ehdr(view);
b52717c0
VR
9573 unsigned char e_ident[elfcpp::EI_NIDENT];
9574 elfcpp::Elf_Word flags = this->processor_specific_flags();
9575 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9576
9577 unsigned char ei_abiversion = 0;
9578 elfcpp::Elf_Half type = ehdr.get_e_type();
9579 if (type == elfcpp::ET_EXEC
9580 && parameters->options().copyreloc()
9581 && (flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
9582 == elfcpp::EF_MIPS_CPIC)
9583 ei_abiversion = 1;
9584
9585 if (this->abiflags_ != NULL
9586 && (this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64
9587 || this->abiflags_->fp_abi == elfcpp::Val_GNU_MIPS_ABI_FP_64A))
9588 ei_abiversion = 3;
9589
9590 e_ident[elfcpp::EI_ABIVERSION] = ei_abiversion;
9810d34d 9591 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
b52717c0 9592 oehdr.put_e_ident(e_ident);
01b84e25 9593
b52717c0
VR
9594 if (this->entry_symbol_is_compressed_)
9595 oehdr.put_e_entry(ehdr.get_e_entry() + 1);
9810d34d
SS
9596}
9597
9598// do_make_elf_object to override the same function in the base class.
9599// We need to use a target-specific sub-class of
9600// Sized_relobj_file<size, big_endian> to store Mips specific information.
9601// Hence we need to have our own ELF object creation.
9602
9603template<int size, bool big_endian>
9604Object*
9605Target_mips<size, big_endian>::do_make_elf_object(
9606 const std::string& name,
9607 Input_file* input_file,
9608 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
9609{
9610 int et = ehdr.get_e_type();
9611 // ET_EXEC files are valid input for --just-symbols/-R,
9612 // and we treat them as relocatable objects.
9613 if (et == elfcpp::ET_REL
9614 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
9615 {
9616 Mips_relobj<size, big_endian>* obj =
9617 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
9618 obj->setup();
9619 return obj;
9620 }
9621 else if (et == elfcpp::ET_DYN)
9622 {
9623 // TODO(sasa): Should we create Mips_dynobj?
9624 return Target::do_make_elf_object(name, input_file, offset, ehdr);
9625 }
9626 else
9627 {
9628 gold_error(_("%s: unsupported ELF file type %d"),
9629 name.c_str(), et);
9630 return NULL;
9631 }
9632}
9633
9634// Finalize the sections.
9635
9636template <int size, bool big_endian>
9637void
9638Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
9639 const Input_objects* input_objects,
9640 Symbol_table* symtab)
9641{
4d78db49
VR
9642 const bool relocatable = parameters->options().relocatable();
9643
9810d34d
SS
9644 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and
9645 // DT_FINI have correct values.
9646 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>(
9647 symtab->lookup(parameters->options().init()));
9648 if (init != NULL && (init->is_mips16() || init->is_micromips()))
9649 init->set_value(init->value() | 1);
9650 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>(
9651 symtab->lookup(parameters->options().fini()));
9652 if (fini != NULL && (fini->is_mips16() || fini->is_micromips()))
9653 fini->set_value(fini->value() | 1);
9654
9655 // Check whether the entry symbol is mips16 or micromips. This is needed to
9656 // adjust entry address in ELF header.
9657 Mips_symbol<size>* entry =
9658 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name()));
9659 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16()
9660 || entry->is_micromips()));
9661
9662 if (!parameters->doing_static_link()
9663 && (strcmp(parameters->options().hash_style(), "gnu") == 0
9664 || strcmp(parameters->options().hash_style(), "both") == 0))
9665 {
9666 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
9667 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the
9668 // MIPS ABI requires a mapping between the GOT and the symbol table.
9669 gold_error(".gnu.hash is incompatible with the MIPS ABI");
9670 }
9671
9672 // Check whether the final section that was scanned has HI16 or GOT16
9673 // relocations without the corresponding LO16 part.
9674 if (this->got16_addends_.size() > 0)
9675 gold_error("Can't find matching LO16 reloc");
9676
82e49872
VR
9677 Valtype gprmask = 0;
9678 Valtype cprmask1 = 0;
9679 Valtype cprmask2 = 0;
9680 Valtype cprmask3 = 0;
9681 Valtype cprmask4 = 0;
9682 bool has_reginfo_section = false;
9683
9810d34d
SS
9684 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9685 p != input_objects->relobj_end();
9686 ++p)
9687 {
9688 Mips_relobj<size, big_endian>* relobj =
9689 Mips_relobj<size, big_endian>::as_mips_relobj(*p);
9690
4d78db49
VR
9691 // Check for any mips16 stub sections that we can discard.
9692 if (!relocatable)
9693 relobj->discard_mips16_stub_sections(symtab);
9694
9695 if (!relobj->merge_processor_specific_data())
9696 continue;
9697
82e49872
VR
9698 // Merge .reginfo contents of input objects.
9699 if (relobj->has_reginfo_section())
9700 {
9701 has_reginfo_section = true;
9702 gprmask |= relobj->gprmask();
9703 cprmask1 |= relobj->cprmask1();
9704 cprmask2 |= relobj->cprmask2();
9705 cprmask3 |= relobj->cprmask3();
9706 cprmask4 |= relobj->cprmask4();
9707 }
9708
b52717c0
VR
9709 // Merge processor specific flags.
9710 Mips_abiflags<big_endian> in_abiflags;
9711
9712 this->create_abiflags(relobj, &in_abiflags);
9713 this->merge_obj_e_flags(relobj->name(),
9714 relobj->processor_specific_flags());
9715 this->merge_obj_abiflags(relobj->name(), &in_abiflags);
9716 this->merge_obj_attributes(relobj->name(),
9717 relobj->attributes_section_data());
9810d34d
SS
9718 }
9719
b52717c0
VR
9720 // Create a .gnu.attributes section if we have merged any attributes
9721 // from inputs.
9722 if (this->attributes_section_data_ != NULL)
9810d34d 9723 {
b52717c0
VR
9724 Output_attributes_section_data* attributes_section =
9725 new Output_attributes_section_data(*this->attributes_section_data_);
9726 layout->add_output_section_data(".gnu.attributes",
9727 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9728 attributes_section, ORDER_INVALID, false);
9729 }
9810d34d 9730
b52717c0
VR
9731 // Create .MIPS.abiflags output section if there is an input section.
9732 if (this->has_abiflags_section_)
9733 {
9734 Mips_output_section_abiflags<size, big_endian>* abiflags_section =
9735 new Mips_output_section_abiflags<size, big_endian>(*this->abiflags_);
9810d34d 9736
b52717c0
VR
9737 Output_section* os =
9738 layout->add_output_section_data(".MIPS.abiflags",
9739 elfcpp::SHT_MIPS_ABIFLAGS,
9740 elfcpp::SHF_ALLOC,
9741 abiflags_section, ORDER_INVALID, false);
9810d34d 9742
4d78db49 9743 if (!relocatable && os != NULL)
b52717c0
VR
9744 {
9745 Output_segment* abiflags_segment =
9746 layout->make_output_segment(elfcpp::PT_MIPS_ABIFLAGS, elfcpp::PF_R);
9747 abiflags_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9748 }
9810d34d
SS
9749 }
9750
82e49872 9751 if (has_reginfo_section && !parameters->options().gc_sections())
9810d34d 9752 {
82e49872
VR
9753 // Create .reginfo output section.
9754 Mips_output_section_reginfo<size, big_endian>* reginfo_section =
9755 new Mips_output_section_reginfo<size, big_endian>(this, gprmask,
9756 cprmask1, cprmask2,
9757 cprmask3, cprmask4);
9758
9759 Output_section* os =
9760 layout->add_output_section_data(".reginfo", elfcpp::SHT_MIPS_REGINFO,
9761 elfcpp::SHF_ALLOC, reginfo_section,
9762 ORDER_INVALID, false);
9763
4d78db49 9764 if (!relocatable && os != NULL)
82e49872
VR
9765 {
9766 Output_segment* reginfo_segment =
9767 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
9768 elfcpp::PF_R);
9769 reginfo_segment->add_output_section_to_nonload(os, elfcpp::PF_R);
9770 }
9810d34d
SS
9771 }
9772
9773 if (this->plt_ != NULL)
9774 {
9775 // Set final PLT offsets for symbols.
9776 this->plt_section()->set_plt_offsets();
9777
9778 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
9779 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if
9780 // there are no standard PLT entries present.
9781 unsigned char nonvis = 0;
9782 if (this->is_output_micromips()
9783 && !this->plt_section()->has_standard_entries())
9784 nonvis = elfcpp::STO_MICROMIPS >> 2;
9785 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
9786 Symbol_table::PREDEFINED,
9787 this->plt_,
9788 0, 0, elfcpp::STT_FUNC,
9789 elfcpp::STB_LOCAL,
9790 elfcpp::STV_DEFAULT, nonvis,
9791 false, false);
9792 }
9793
9794 if (this->mips_stubs_ != NULL)
9795 {
9796 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section.
9797 unsigned char nonvis = 0;
9798 if (this->is_output_micromips())
9799 nonvis = elfcpp::STO_MICROMIPS >> 2;
9800 symtab->define_in_output_data("_MIPS_STUBS_", NULL,
9801 Symbol_table::PREDEFINED,
9802 this->mips_stubs_,
9803 0, 0, elfcpp::STT_FUNC,
9804 elfcpp::STB_LOCAL,
9805 elfcpp::STV_DEFAULT, nonvis,
9806 false, false);
9807 }
9808
4d78db49 9809 if (!relocatable && !parameters->doing_static_link())
9810d34d
SS
9810 // In case there is no .got section, create one.
9811 this->got_section(symtab, layout);
9812
9813 // Emit any relocs we saved in an attempt to avoid generating COPY
9814 // relocs.
9815 if (this->copy_relocs_.any_saved_relocs())
9816 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout,
9817 this);
9818
453018bf
VR
9819 // Set _gp value.
9820 this->set_gp(layout, symtab);
9821
9810d34d
SS
9822 // Emit dynamic relocs.
9823 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
9824 p != this->dyn_relocs_.end();
9825 ++p)
9826 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
9827
9828 if (this->has_got_section())
9829 this->got_section()->lay_out_got(layout, symtab, input_objects);
9830
9831 if (this->mips_stubs_ != NULL)
9832 this->mips_stubs_->set_needs_dynsym_value();
9833
9834 // Check for functions that might need $25 to be valid on entry.
9835 // TODO(sasa): Can we do this without iterating over all symbols?
9836 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
9837 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
9838 symtab));
9839
9840 // Add NULL segment.
4d78db49 9841 if (!relocatable)
9810d34d
SS
9842 layout->make_output_segment(elfcpp::PT_NULL, 0);
9843
9810d34d
SS
9844 // Fill in some more dynamic tags.
9845 // TODO(sasa): Add more dynamic tags.
9846 const Reloc_section* rel_plt = (this->plt_ == NULL
9847 ? NULL : this->plt_->rel_plt());
9848 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
9849 this->rel_dyn_, true, false);
9850
9851 Output_data_dynamic* const odyn = layout->dynamic_data();
9852 if (odyn != NULL
4d78db49 9853 && !relocatable
9810d34d
SS
9854 && !parameters->doing_static_link())
9855 {
9856 unsigned int d_val;
9857 // This element holds a 32-bit version id for the Runtime
9858 // Linker Interface. This will start at integer value 1.
9859 d_val = 0x01;
9860 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
9861
9862 // Dynamic flags
9863 d_val = elfcpp::RHF_NOTPOT;
9864 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
9865
5c3024d2 9866 // Save layout for using when emitting custom dynamic tags.
9810d34d
SS
9867 this->layout_ = layout;
9868
9869 // This member holds the base address of the segment.
9870 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
9871
9872 // This member holds the number of entries in the .dynsym section.
9873 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
9874
9875 // This member holds the index of the first dynamic symbol
9876 // table entry that corresponds to an entry in the global offset table.
9877 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
9878
9879 // This member holds the number of local GOT entries.
9880 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO,
9881 this->got_->get_local_gotno());
9882
9883 if (this->plt_ != NULL)
9884 // DT_MIPS_PLTGOT dynamic tag
9885 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
a8ecc9fe
VR
9886
9887 if (!parameters->options().shared())
9888 {
9889 this->rld_map_ = new Output_data_zero_fill(size / 8, size / 8);
9890
9891 layout->add_output_section_data(".rld_map", elfcpp::SHT_PROGBITS,
9892 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
9893 this->rld_map_, ORDER_INVALID, false);
9894
9895 // __RLD_MAP will be filled in by the runtime loader to contain
9896 // a pointer to the _r_debug structure.
9897 Symbol* rld_map = symtab->define_in_output_data("__RLD_MAP", NULL,
9898 Symbol_table::PREDEFINED,
9899 this->rld_map_,
9900 0, 0, elfcpp::STT_OBJECT,
9901 elfcpp::STB_GLOBAL,
9902 elfcpp::STV_DEFAULT, 0,
9903 false, false);
9904
c1f59f8f
VR
9905 if (!rld_map->is_forced_local())
9906 rld_map->set_needs_dynsym_entry();
a8ecc9fe
VR
9907
9908 if (!parameters->options().pie())
9909 // This member holds the absolute address of the debug pointer.
9910 odyn->add_section_address(elfcpp::DT_MIPS_RLD_MAP, this->rld_map_);
9911 else
9912 // This member holds the offset to the debug pointer,
9913 // relative to the address of the tag.
9914 odyn->add_custom(elfcpp::DT_MIPS_RLD_MAP_REL);
9915 }
9810d34d 9916 }
a8ecc9fe 9917}
9810d34d
SS
9918
9919// Get the custom dynamic tag value.
9920template<int size, bool big_endian>
9921unsigned int
9922Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const
9923{
9924 switch (tag)
9925 {
9926 case elfcpp::DT_MIPS_BASE_ADDRESS:
9927 {
9928 // The base address of the segment.
9929 // At this point, the segment list has been sorted into final order,
9930 // so just return vaddr of the first readable PT_LOAD segment.
9931 Output_segment* seg =
9932 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0);
9933 gold_assert(seg != NULL);
9934 return seg->vaddr();
9935 }
9936
9937 case elfcpp::DT_MIPS_SYMTABNO:
9938 // The number of entries in the .dynsym section.
9939 return this->get_dt_mips_symtabno();
9940
9941 case elfcpp::DT_MIPS_GOTSYM:
9942 {
9943 // The index of the first dynamic symbol table entry that corresponds
9944 // to an entry in the GOT.
9945 if (this->got_->first_global_got_dynsym_index() != -1U)
9946 return this->got_->first_global_got_dynsym_index();
9947 else
9948 // In case if we don't have global GOT symbols we default to setting
9949 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO.
9950 return this->get_dt_mips_symtabno();
9951 }
9952
a8ecc9fe
VR
9953 case elfcpp::DT_MIPS_RLD_MAP_REL:
9954 {
9955 // The MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,
9956 // relative to the address of the tag.
9957 Output_data_dynamic* const odyn = this->layout_->dynamic_data();
9958 unsigned int entry_offset =
9959 odyn->get_entry_offset(elfcpp::DT_MIPS_RLD_MAP_REL);
9960 gold_assert(entry_offset != -1U);
9961 return this->rld_map_->address() - (odyn->address() + entry_offset);
9962 }
9810d34d
SS
9963 default:
9964 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
9965 }
9966
9967 return (unsigned int)-1;
9968}
9969
9970// Relocate section data.
9971
9972template<int size, bool big_endian>
9973void
9974Target_mips<size, big_endian>::relocate_section(
9975 const Relocate_info<size, big_endian>* relinfo,
9976 unsigned int sh_type,
9977 const unsigned char* prelocs,
9978 size_t reloc_count,
9979 Output_section* output_section,
9980 bool needs_special_offset_handling,
9981 unsigned char* view,
9982 Mips_address address,
9983 section_size_type view_size,
9984 const Reloc_symbol_changes* reloc_symbol_changes)
9985{
9986 typedef Target_mips<size, big_endian> Mips;
9987 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
9988
9989 if (sh_type == elfcpp::SHT_REL)
4d625b70
CC
9990 {
9991 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
9992 Classify_reloc;
9993
9994 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
9995 gold::Default_comdat_behavior, Classify_reloc>(
9996 relinfo,
9997 this,
9998 prelocs,
9999 reloc_count,
10000 output_section,
10001 needs_special_offset_handling,
10002 view,
10003 address,
10004 view_size,
10005 reloc_symbol_changes);
10006 }
9810d34d 10007 else if (sh_type == elfcpp::SHT_RELA)
4d625b70
CC
10008 {
10009 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10010 Classify_reloc;
10011
10012 gold::relocate_section<size, big_endian, Mips, Mips_relocate,
10013 gold::Default_comdat_behavior, Classify_reloc>(
10014 relinfo,
10015 this,
10016 prelocs,
10017 reloc_count,
10018 output_section,
10019 needs_special_offset_handling,
10020 view,
10021 address,
10022 view_size,
10023 reloc_symbol_changes);
10024 }
9810d34d
SS
10025}
10026
10027// Return the size of a relocation while scanning during a relocatable
10028// link.
10029
9810d34d 10030unsigned int
4d625b70 10031mips_get_size_for_reloc(unsigned int r_type, Relobj* object)
9810d34d
SS
10032{
10033 switch (r_type)
10034 {
10035 case elfcpp::R_MIPS_NONE:
10036 case elfcpp::R_MIPS_TLS_DTPMOD64:
10037 case elfcpp::R_MIPS_TLS_DTPREL64:
10038 case elfcpp::R_MIPS_TLS_TPREL64:
10039 return 0;
10040
10041 case elfcpp::R_MIPS_32:
10042 case elfcpp::R_MIPS_TLS_DTPMOD32:
10043 case elfcpp::R_MIPS_TLS_DTPREL32:
10044 case elfcpp::R_MIPS_TLS_TPREL32:
10045 case elfcpp::R_MIPS_REL32:
10046 case elfcpp::R_MIPS_PC32:
10047 case elfcpp::R_MIPS_GPREL32:
10048 case elfcpp::R_MIPS_JALR:
47a9f4fc 10049 case elfcpp::R_MIPS_EH:
9810d34d
SS
10050 return 4;
10051
10052 case elfcpp::R_MIPS_16:
10053 case elfcpp::R_MIPS_HI16:
10054 case elfcpp::R_MIPS_LO16:
e242ece1
VR
10055 case elfcpp::R_MIPS_HIGHER:
10056 case elfcpp::R_MIPS_HIGHEST:
9810d34d
SS
10057 case elfcpp::R_MIPS_GPREL16:
10058 case elfcpp::R_MIPS16_HI16:
10059 case elfcpp::R_MIPS16_LO16:
10060 case elfcpp::R_MIPS_PC16:
f5b11759
VR
10061 case elfcpp::R_MIPS_PCHI16:
10062 case elfcpp::R_MIPS_PCLO16:
9810d34d
SS
10063 case elfcpp::R_MIPS_GOT16:
10064 case elfcpp::R_MIPS16_GOT16:
10065 case elfcpp::R_MIPS_CALL16:
10066 case elfcpp::R_MIPS16_CALL16:
10067 case elfcpp::R_MIPS_GOT_HI16:
10068 case elfcpp::R_MIPS_CALL_HI16:
10069 case elfcpp::R_MIPS_GOT_LO16:
10070 case elfcpp::R_MIPS_CALL_LO16:
10071 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
10072 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
10073 case elfcpp::R_MIPS_TLS_TPREL_HI16:
10074 case elfcpp::R_MIPS_TLS_TPREL_LO16:
10075 case elfcpp::R_MIPS16_GPREL:
10076 case elfcpp::R_MIPS_GOT_DISP:
10077 case elfcpp::R_MIPS_LITERAL:
10078 case elfcpp::R_MIPS_GOT_PAGE:
10079 case elfcpp::R_MIPS_GOT_OFST:
10080 case elfcpp::R_MIPS_TLS_GD:
10081 case elfcpp::R_MIPS_TLS_LDM:
10082 case elfcpp::R_MIPS_TLS_GOTTPREL:
10083 return 2;
10084
10085 // These relocations are not byte sized
10086 case elfcpp::R_MIPS_26:
10087 case elfcpp::R_MIPS16_26:
f5b11759
VR
10088 case elfcpp::R_MIPS_PC21_S2:
10089 case elfcpp::R_MIPS_PC26_S2:
10090 case elfcpp::R_MIPS_PC18_S3:
10091 case elfcpp::R_MIPS_PC19_S2:
9810d34d
SS
10092 return 4;
10093
10094 case elfcpp::R_MIPS_COPY:
10095 case elfcpp::R_MIPS_JUMP_SLOT:
10096 object->error(_("unexpected reloc %u in object file"), r_type);
10097 return 0;
10098
10099 default:
10100 object->error(_("unsupported reloc %u in object file"), r_type);
10101 return 0;
10102 }
10103}
10104
10105// Scan the relocs during a relocatable link.
10106
10107template<int size, bool big_endian>
10108void
10109Target_mips<size, big_endian>::scan_relocatable_relocs(
10110 Symbol_table* symtab,
10111 Layout* layout,
10112 Sized_relobj_file<size, big_endian>* object,
10113 unsigned int data_shndx,
10114 unsigned int sh_type,
10115 const unsigned char* prelocs,
10116 size_t reloc_count,
10117 Output_section* output_section,
10118 bool needs_special_offset_handling,
10119 size_t local_symbol_count,
10120 const unsigned char* plocal_symbols,
10121 Relocatable_relocs* rr)
10122{
47a9f4fc
VR
10123 if (sh_type == elfcpp::SHT_REL)
10124 {
10125 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10126 Classify_reloc;
10127 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10128 Scan_relocatable_relocs;
10129
10130 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10131 symtab,
10132 layout,
10133 object,
10134 data_shndx,
10135 prelocs,
10136 reloc_count,
10137 output_section,
10138 needs_special_offset_handling,
10139 local_symbol_count,
10140 plocal_symbols,
10141 rr);
10142 }
10143 else if (sh_type == elfcpp::SHT_RELA)
10144 {
10145 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10146 Classify_reloc;
10147 typedef Mips_scan_relocatable_relocs<big_endian, Classify_reloc>
10148 Scan_relocatable_relocs;
10149
10150 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
10151 symtab,
10152 layout,
10153 object,
10154 data_shndx,
10155 prelocs,
10156 reloc_count,
10157 output_section,
10158 needs_special_offset_handling,
10159 local_symbol_count,
10160 plocal_symbols,
10161 rr);
10162 }
10163 else
10164 gold_unreachable();
9810d34d
SS
10165}
10166
4d625b70
CC
10167// Scan the relocs for --emit-relocs.
10168
10169template<int size, bool big_endian>
10170void
10171Target_mips<size, big_endian>::emit_relocs_scan(
10172 Symbol_table* symtab,
10173 Layout* layout,
10174 Sized_relobj_file<size, big_endian>* object,
10175 unsigned int data_shndx,
10176 unsigned int sh_type,
10177 const unsigned char* prelocs,
10178 size_t reloc_count,
10179 Output_section* output_section,
10180 bool needs_special_offset_handling,
10181 size_t local_symbol_count,
10182 const unsigned char* plocal_syms,
10183 Relocatable_relocs* rr)
10184{
47a9f4fc
VR
10185 if (sh_type == elfcpp::SHT_REL)
10186 {
10187 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10188 Classify_reloc;
10189 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10190 Emit_relocs_strategy;
10191
10192 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10193 symtab,
10194 layout,
10195 object,
10196 data_shndx,
10197 prelocs,
10198 reloc_count,
10199 output_section,
10200 needs_special_offset_handling,
10201 local_symbol_count,
10202 plocal_syms,
10203 rr);
10204 }
10205 else if (sh_type == elfcpp::SHT_RELA)
10206 {
10207 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10208 Classify_reloc;
10209 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
10210 Emit_relocs_strategy;
10211
10212 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
10213 symtab,
10214 layout,
10215 object,
10216 data_shndx,
10217 prelocs,
10218 reloc_count,
10219 output_section,
10220 needs_special_offset_handling,
10221 local_symbol_count,
10222 plocal_syms,
10223 rr);
10224 }
10225 else
10226 gold_unreachable();
4d625b70
CC
10227}
10228
9810d34d
SS
10229// Emit relocations for a section.
10230
10231template<int size, bool big_endian>
10232void
10233Target_mips<size, big_endian>::relocate_relocs(
10234 const Relocate_info<size, big_endian>* relinfo,
10235 unsigned int sh_type,
10236 const unsigned char* prelocs,
10237 size_t reloc_count,
10238 Output_section* output_section,
10239 typename elfcpp::Elf_types<size>::Elf_Off
10240 offset_in_output_section,
9810d34d
SS
10241 unsigned char* view,
10242 Mips_address view_address,
10243 section_size_type view_size,
10244 unsigned char* reloc_view,
10245 section_size_type reloc_view_size)
10246{
47a9f4fc
VR
10247 if (sh_type == elfcpp::SHT_REL)
10248 {
10249 typedef Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>
10250 Classify_reloc;
10251
10252 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10253 relinfo,
10254 prelocs,
10255 reloc_count,
10256 output_section,
10257 offset_in_output_section,
10258 view,
10259 view_address,
10260 view_size,
10261 reloc_view,
10262 reloc_view_size);
10263 }
10264 else if (sh_type == elfcpp::SHT_RELA)
10265 {
10266 typedef Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
10267 Classify_reloc;
10268
10269 gold::relocate_relocs<size, big_endian, Classify_reloc>(
10270 relinfo,
10271 prelocs,
10272 reloc_count,
10273 output_section,
10274 offset_in_output_section,
10275 view,
10276 view_address,
10277 view_size,
10278 reloc_view,
10279 reloc_view_size);
10280 }
10281 else
10282 gold_unreachable();
9810d34d
SS
10283}
10284
10285// Perform target-specific processing in a relocatable link. This is
10286// only used if we use the relocation strategy RELOC_SPECIAL.
10287
10288template<int size, bool big_endian>
10289void
10290Target_mips<size, big_endian>::relocate_special_relocatable(
10291 const Relocate_info<size, big_endian>* relinfo,
10292 unsigned int sh_type,
10293 const unsigned char* preloc_in,
10294 size_t relnum,
10295 Output_section* output_section,
10296 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
10297 unsigned char* view,
10298 Mips_address view_address,
10299 section_size_type,
10300 unsigned char* preloc_out)
10301{
10302 // We can only handle REL type relocation sections.
10303 gold_assert(sh_type == elfcpp::SHT_REL);
10304
10305 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
10306 Reltype;
10307 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
10308 Reltype_write;
10309
10310 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
10311
10312 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
10313
10314 Mips_relobj<size, big_endian>* object =
10315 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
10316 const unsigned int local_count = object->local_symbol_count();
10317
10318 Reltype reloc(preloc_in);
10319 Reltype_write reloc_write(preloc_out);
10320
10321 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10322 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10323 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10324
10325 // Get the new symbol index.
10326 // We only use RELOC_SPECIAL strategy in local relocations.
10327 gold_assert(r_sym < local_count);
10328
10329 // We are adjusting a section symbol. We need to find
10330 // the symbol table index of the section symbol for
10331 // the output section corresponding to input section
10332 // in which this symbol is defined.
10333 bool is_ordinary;
10334 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
10335 gold_assert(is_ordinary);
10336 Output_section* os = object->output_section(shndx);
10337 gold_assert(os != NULL);
10338 gold_assert(os->needs_symtab_index());
10339 unsigned int new_symndx = os->symtab_index();
10340
10341 // Get the new offset--the location in the output section where
10342 // this relocation should be applied.
10343
10344 Mips_address offset = reloc.get_r_offset();
10345 Mips_address new_offset;
10346 if (offset_in_output_section != invalid_address)
10347 new_offset = offset + offset_in_output_section;
10348 else
10349 {
10350 section_offset_type sot_offset =
10351 convert_types<section_offset_type, Mips_address>(offset);
10352 section_offset_type new_sot_offset =
10353 output_section->output_offset(object, relinfo->data_shndx,
10354 sot_offset);
10355 gold_assert(new_sot_offset != -1);
10356 new_offset = new_sot_offset;
10357 }
10358
10359 // In an object file, r_offset is an offset within the section.
10360 // In an executable or dynamic object, generated by
10361 // --emit-relocs, r_offset is an absolute address.
10362 if (!parameters->options().relocatable())
10363 {
10364 new_offset += view_address;
10365 if (offset_in_output_section != invalid_address)
10366 new_offset -= offset_in_output_section;
10367 }
10368
10369 reloc_write.put_r_offset(new_offset);
10370 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
10371
10372 // Handle the reloc addend.
10373 // The relocation uses a section symbol in the input file.
10374 // We are adjusting it to use a section symbol in the output
10375 // file. The input section symbol refers to some address in
10376 // the input section. We need the relocation in the output
10377 // file to refer to that same address. This adjustment to
10378 // the addend is the same calculation we use for a simple
10379 // absolute relocation for the input section symbol.
47a9f4fc 10380 Valtype calculated_value = 0;
9810d34d
SS
10381 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
10382
10383 unsigned char* paddend = view + offset;
10384 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
10385 switch (r_type)
10386 {
10387 case elfcpp::R_MIPS_26:
10388 reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
10389 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL,
47a9f4fc
VR
10390 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal(),
10391 false, &calculated_value);
9810d34d
SS
10392 break;
10393
10394 default:
10395 gold_unreachable();
10396 }
10397
10398 // Report any errors.
10399 switch (reloc_status)
10400 {
10401 case Reloc_funcs::STATUS_OKAY:
10402 break;
10403 case Reloc_funcs::STATUS_OVERFLOW:
10404 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
c3847462
VR
10405 _("relocation overflow: "
10406 "%u against local symbol %u in %s"),
10407 r_type, r_sym, object->name().c_str());
9810d34d
SS
10408 break;
10409 case Reloc_funcs::STATUS_BAD_RELOC:
10410 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
10411 _("unexpected opcode while processing relocation"));
10412 break;
10413 default:
10414 gold_unreachable();
10415 }
10416}
10417
10418// Optimize the TLS relocation type based on what we know about the
10419// symbol. IS_FINAL is true if the final address of this symbol is
10420// known at link time.
10421
10422template<int size, bool big_endian>
10423tls::Tls_optimization
10424Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
10425{
10426 // FIXME: Currently we do not do any TLS optimization.
10427 return tls::TLSOPT_NONE;
10428}
10429
10430// Scan a relocation for a local symbol.
10431
10432template<int size, bool big_endian>
10433inline void
10434Target_mips<size, big_endian>::Scan::local(
10435 Symbol_table* symtab,
10436 Layout* layout,
10437 Target_mips<size, big_endian>* target,
10438 Sized_relobj_file<size, big_endian>* object,
10439 unsigned int data_shndx,
10440 Output_section* output_section,
4d625b70
CC
10441 const Relatype* rela,
10442 const Reltype* rel,
9810d34d
SS
10443 unsigned int rel_type,
10444 unsigned int r_type,
10445 const elfcpp::Sym<size, big_endian>& lsym,
10446 bool is_discarded)
10447{
10448 if (is_discarded)
10449 return;
10450
10451 Mips_address r_offset;
4d625b70 10452 unsigned int r_sym;
9810d34d
SS
10453 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10454
10455 if (rel_type == elfcpp::SHT_RELA)
10456 {
10457 r_offset = rela->get_r_offset();
4d625b70
CC
10458 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10459 get_r_sym(rela);
9810d34d
SS
10460 r_addend = rela->get_r_addend();
10461 }
10462 else
10463 {
10464 r_offset = rel->get_r_offset();
4d625b70
CC
10465 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10466 get_r_sym(rel);
9810d34d
SS
10467 r_addend = 0;
10468 }
10469
9810d34d
SS
10470 Mips_relobj<size, big_endian>* mips_obj =
10471 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10472
10473 if (mips_obj->is_mips16_stub_section(data_shndx))
10474 {
10475 mips_obj->get_mips16_stub_section(data_shndx)
10476 ->new_local_reloc_found(r_type, r_sym);
10477 }
10478
10479 if (r_type == elfcpp::R_MIPS_NONE)
10480 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10481 // mips16 stub.
10482 return;
10483
10484 if (!mips16_call_reloc(r_type)
10485 && !mips_obj->section_allows_mips16_refs(data_shndx))
10486 // This reloc would need to refer to a MIPS16 hard-float stub, if
10487 // there is one. We ignore MIPS16 stub sections and .pdr section when
10488 // looking for relocs that would need to refer to MIPS16 stubs.
10489 mips_obj->add_local_non_16bit_call(r_sym);
10490
10491 if (r_type == elfcpp::R_MIPS16_26
10492 && !mips_obj->section_allows_mips16_refs(data_shndx))
10493 mips_obj->add_local_16bit_call(r_sym);
10494
10495 switch (r_type)
10496 {
10497 case elfcpp::R_MIPS_GOT16:
10498 case elfcpp::R_MIPS_CALL16:
10499 case elfcpp::R_MIPS_CALL_HI16:
10500 case elfcpp::R_MIPS_CALL_LO16:
10501 case elfcpp::R_MIPS_GOT_HI16:
10502 case elfcpp::R_MIPS_GOT_LO16:
10503 case elfcpp::R_MIPS_GOT_PAGE:
10504 case elfcpp::R_MIPS_GOT_OFST:
10505 case elfcpp::R_MIPS_GOT_DISP:
10506 case elfcpp::R_MIPS_TLS_GOTTPREL:
10507 case elfcpp::R_MIPS_TLS_GD:
10508 case elfcpp::R_MIPS_TLS_LDM:
10509 case elfcpp::R_MIPS16_GOT16:
10510 case elfcpp::R_MIPS16_CALL16:
10511 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10512 case elfcpp::R_MIPS16_TLS_GD:
10513 case elfcpp::R_MIPS16_TLS_LDM:
10514 case elfcpp::R_MICROMIPS_GOT16:
10515 case elfcpp::R_MICROMIPS_CALL16:
10516 case elfcpp::R_MICROMIPS_CALL_HI16:
10517 case elfcpp::R_MICROMIPS_CALL_LO16:
10518 case elfcpp::R_MICROMIPS_GOT_HI16:
10519 case elfcpp::R_MICROMIPS_GOT_LO16:
10520 case elfcpp::R_MICROMIPS_GOT_PAGE:
10521 case elfcpp::R_MICROMIPS_GOT_OFST:
10522 case elfcpp::R_MICROMIPS_GOT_DISP:
10523 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10524 case elfcpp::R_MICROMIPS_TLS_GD:
10525 case elfcpp::R_MICROMIPS_TLS_LDM:
47a9f4fc 10526 case elfcpp::R_MIPS_EH:
9810d34d
SS
10527 // We need a GOT section.
10528 target->got_section(symtab, layout);
10529 break;
10530
10531 default:
10532 break;
10533 }
10534
10535 if (call_lo16_reloc(r_type)
10536 || got_lo16_reloc(r_type)
47a9f4fc
VR
10537 || got_disp_reloc(r_type)
10538 || eh_reloc(r_type))
9810d34d
SS
10539 {
10540 // We may need a local GOT entry for this relocation. We
10541 // don't count R_MIPS_GOT_PAGE because we can estimate the
10542 // maximum number of pages needed by looking at the size of
10543 // the segment. Similar comments apply to R_MIPS*_GOT16 and
10544 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or
10545 // R_MIPS_CALL_HI16 because these are always followed by an
10546 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
10547 Mips_output_data_got<size, big_endian>* got =
10548 target->got_section(symtab, layout);
47a9f4fc
VR
10549 bool is_section_symbol = lsym.get_st_type() == elfcpp::STT_SECTION;
10550 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U,
10551 is_section_symbol);
9810d34d
SS
10552 }
10553
10554 switch (r_type)
10555 {
10556 case elfcpp::R_MIPS_CALL16:
10557 case elfcpp::R_MIPS16_CALL16:
10558 case elfcpp::R_MICROMIPS_CALL16:
10559 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
10560 (unsigned long)r_offset);
10561 return;
10562
10563 case elfcpp::R_MIPS_GOT_PAGE:
10564 case elfcpp::R_MICROMIPS_GOT_PAGE:
10565 case elfcpp::R_MIPS16_GOT16:
10566 case elfcpp::R_MIPS_GOT16:
10567 case elfcpp::R_MIPS_GOT_HI16:
10568 case elfcpp::R_MIPS_GOT_LO16:
10569 case elfcpp::R_MICROMIPS_GOT16:
10570 case elfcpp::R_MICROMIPS_GOT_HI16:
10571 case elfcpp::R_MICROMIPS_GOT_LO16:
10572 {
10573 // This relocation needs a page entry in the GOT.
10574 // Get the section contents.
10575 section_size_type view_size = 0;
10576 const unsigned char* view = object->section_contents(data_shndx,
10577 &view_size, false);
10578 view += r_offset;
10579
10580 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10581 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
10582 : r_addend);
10583
10584 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type))
10585 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10586 object, data_shndx, r_type, r_sym, addend));
10587 else
10588 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend);
10589 break;
10590 }
10591
10592 case elfcpp::R_MIPS_HI16:
f5b11759 10593 case elfcpp::R_MIPS_PCHI16:
9810d34d
SS
10594 case elfcpp::R_MIPS16_HI16:
10595 case elfcpp::R_MICROMIPS_HI16:
10596 // Record the reloc so that we can check whether the corresponding LO16
10597 // part exists.
10598 if (rel_type == elfcpp::SHT_REL)
10599 target->got16_addends_.push_back(got16_addend<size, big_endian>(
10600 object, data_shndx, r_type, r_sym, 0));
10601 break;
10602
10603 case elfcpp::R_MIPS_LO16:
f5b11759 10604 case elfcpp::R_MIPS_PCLO16:
9810d34d
SS
10605 case elfcpp::R_MIPS16_LO16:
10606 case elfcpp::R_MICROMIPS_LO16:
10607 {
10608 if (rel_type != elfcpp::SHT_REL)
10609 break;
10610
10611 // Find corresponding GOT16/HI16 relocation.
10612
10613 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
10614 // be immediately following. However, for the IRIX6 ABI, the next
10615 // relocation may be a composed relocation consisting of several
10616 // relocations for the same address. In that case, the R_MIPS_LO16
10617 // relocation may occur as one of these. We permit a similar
10618 // extension in general, as that is useful for GCC.
10619
10620 // In some cases GCC dead code elimination removes the LO16 but
10621 // keeps the corresponding HI16. This is strictly speaking a
10622 // violation of the ABI but not immediately harmful.
10623
10624 typename std::list<got16_addend<size, big_endian> >::iterator it =
10625 target->got16_addends_.begin();
10626 while (it != target->got16_addends_.end())
10627 {
10628 got16_addend<size, big_endian> _got16_addend = *it;
10629
10630 // TODO(sasa): Split got16_addends_ list into two lists - one for
10631 // GOT16 relocs and the other for HI16 relocs.
10632
10633 // Report an error if we find HI16 or GOT16 reloc from the
10634 // previous section without the matching LO16 part.
10635 if (_got16_addend.object != object
10636 || _got16_addend.shndx != data_shndx)
10637 {
10638 gold_error("Can't find matching LO16 reloc");
10639 break;
10640 }
10641
10642 if (_got16_addend.r_sym != r_sym
10643 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type))
10644 {
10645 ++it;
10646 continue;
10647 }
10648
10649 // We found a matching HI16 or GOT16 reloc for this LO16 reloc.
10650 // For GOT16, we need to calculate combined addend and record GOT page
10651 // entry.
10652 if (got16_reloc(_got16_addend.r_type))
10653 {
10654
10655 section_size_type view_size = 0;
10656 const unsigned char* view = object->section_contents(data_shndx,
10657 &view_size,
10658 false);
10659 view += r_offset;
10660
10661 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
10662 int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
10663
10664 addend = (_got16_addend.addend << 16) + addend;
10665 target->got_section()->record_got_page_entry(mips_obj, r_sym,
10666 addend);
10667 }
10668
10669 it = target->got16_addends_.erase(it);
10670 }
10671 break;
10672 }
10673 }
10674
10675 switch (r_type)
10676 {
10677 case elfcpp::R_MIPS_32:
10678 case elfcpp::R_MIPS_REL32:
10679 case elfcpp::R_MIPS_64:
10680 {
10681 if (parameters->options().output_is_position_independent())
10682 {
10683 // If building a shared library (or a position-independent
10684 // executable), we need to create a dynamic relocation for
10685 // this location.
47a9f4fc
VR
10686 if (is_readonly_section(output_section))
10687 break;
9810d34d 10688 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9810d34d
SS
10689 rel_dyn->add_symbolless_local_addend(object, r_sym,
10690 elfcpp::R_MIPS_REL32,
10691 output_section, data_shndx,
10692 r_offset);
10693 }
10694 break;
10695 }
10696
10697 case elfcpp::R_MIPS_TLS_GOTTPREL:
10698 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10699 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10700 case elfcpp::R_MIPS_TLS_LDM:
10701 case elfcpp::R_MIPS16_TLS_LDM:
10702 case elfcpp::R_MICROMIPS_TLS_LDM:
10703 case elfcpp::R_MIPS_TLS_GD:
10704 case elfcpp::R_MIPS16_TLS_GD:
10705 case elfcpp::R_MICROMIPS_TLS_GD:
10706 {
9810d34d
SS
10707 bool output_is_shared = parameters->options().shared();
10708 const tls::Tls_optimization optimized_type
10709 = Target_mips<size, big_endian>::optimize_tls_reloc(
10710 !output_is_shared, r_type);
10711 switch (r_type)
10712 {
10713 case elfcpp::R_MIPS_TLS_GD:
10714 case elfcpp::R_MIPS16_TLS_GD:
10715 case elfcpp::R_MICROMIPS_TLS_GD:
10716 if (optimized_type == tls::TLSOPT_NONE)
10717 {
10718 // Create a pair of GOT entries for the module index and
10719 // dtv-relative offset.
10720 Mips_output_data_got<size, big_endian>* got =
10721 target->got_section(symtab, layout);
10722 unsigned int shndx = lsym.get_st_shndx();
10723 bool is_ordinary;
10724 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
10725 if (!is_ordinary)
10726 {
10727 object->error(_("local symbol %u has bad shndx %u"),
10728 r_sym, shndx);
10729 break;
10730 }
10731 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
47a9f4fc 10732 shndx, false);
9810d34d
SS
10733 }
10734 else
10735 {
10736 // FIXME: TLS optimization not supported yet.
10737 gold_unreachable();
10738 }
10739 break;
10740
10741 case elfcpp::R_MIPS_TLS_LDM:
10742 case elfcpp::R_MIPS16_TLS_LDM:
10743 case elfcpp::R_MICROMIPS_TLS_LDM:
10744 if (optimized_type == tls::TLSOPT_NONE)
10745 {
10746 // We always record LDM symbols as local with index 0.
10747 target->got_section()->record_local_got_symbol(mips_obj, 0,
10748 r_addend, r_type,
47a9f4fc 10749 -1U, false);
9810d34d
SS
10750 }
10751 else
10752 {
10753 // FIXME: TLS optimization not supported yet.
10754 gold_unreachable();
10755 }
10756 break;
10757 case elfcpp::R_MIPS_TLS_GOTTPREL:
10758 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10759 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10760 layout->set_has_static_tls();
10761 if (optimized_type == tls::TLSOPT_NONE)
10762 {
10763 // Create a GOT entry for the tp-relative offset.
10764 Mips_output_data_got<size, big_endian>* got =
10765 target->got_section(symtab, layout);
10766 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type,
47a9f4fc 10767 -1U, false);
9810d34d
SS
10768 }
10769 else
10770 {
10771 // FIXME: TLS optimization not supported yet.
10772 gold_unreachable();
10773 }
10774 break;
10775
10776 default:
10777 gold_unreachable();
10778 }
10779 }
10780 break;
10781
10782 default:
10783 break;
10784 }
10785
10786 // Refuse some position-dependent relocations when creating a
10787 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
10788 // not PIC, but we can create dynamic relocations and the result
10789 // will be fine. Also do not refuse R_MIPS_LO16, which can be
10790 // combined with R_MIPS_GOT16.
10791 if (parameters->options().shared())
10792 {
10793 switch (r_type)
10794 {
10795 case elfcpp::R_MIPS16_HI16:
10796 case elfcpp::R_MIPS_HI16:
e242ece1
VR
10797 case elfcpp::R_MIPS_HIGHER:
10798 case elfcpp::R_MIPS_HIGHEST:
9810d34d 10799 case elfcpp::R_MICROMIPS_HI16:
e242ece1
VR
10800 case elfcpp::R_MICROMIPS_HIGHER:
10801 case elfcpp::R_MICROMIPS_HIGHEST:
9810d34d
SS
10802 // Don't refuse a high part relocation if it's against
10803 // no symbol (e.g. part of a compound relocation).
10804 if (r_sym == 0)
10805 break;
d8e90251 10806 // Fall through.
9810d34d
SS
10807
10808 case elfcpp::R_MIPS16_26:
10809 case elfcpp::R_MIPS_26:
10810 case elfcpp::R_MICROMIPS_26_S1:
10811 gold_error(_("%s: relocation %u against `%s' can not be used when "
10812 "making a shared object; recompile with -fPIC"),
10813 object->name().c_str(), r_type, "a local symbol");
10814 default:
10815 break;
10816 }
10817 }
10818}
10819
10820template<int size, bool big_endian>
10821inline void
10822Target_mips<size, big_endian>::Scan::local(
10823 Symbol_table* symtab,
10824 Layout* layout,
10825 Target_mips<size, big_endian>* target,
10826 Sized_relobj_file<size, big_endian>* object,
10827 unsigned int data_shndx,
10828 Output_section* output_section,
4d625b70 10829 const Reltype& reloc,
9810d34d
SS
10830 unsigned int r_type,
10831 const elfcpp::Sym<size, big_endian>& lsym,
10832 bool is_discarded)
10833{
10834 if (is_discarded)
10835 return;
10836
10837 local(
10838 symtab,
10839 layout,
10840 target,
10841 object,
10842 data_shndx,
10843 output_section,
4d625b70 10844 (const Relatype*) NULL,
9810d34d
SS
10845 &reloc,
10846 elfcpp::SHT_REL,
10847 r_type,
10848 lsym, is_discarded);
10849}
10850
10851
10852template<int size, bool big_endian>
10853inline void
10854Target_mips<size, big_endian>::Scan::local(
10855 Symbol_table* symtab,
10856 Layout* layout,
10857 Target_mips<size, big_endian>* target,
10858 Sized_relobj_file<size, big_endian>* object,
10859 unsigned int data_shndx,
10860 Output_section* output_section,
4d625b70 10861 const Relatype& reloc,
9810d34d
SS
10862 unsigned int r_type,
10863 const elfcpp::Sym<size, big_endian>& lsym,
10864 bool is_discarded)
10865{
10866 if (is_discarded)
10867 return;
10868
10869 local(
10870 symtab,
10871 layout,
10872 target,
10873 object,
10874 data_shndx,
10875 output_section,
10876 &reloc,
4d625b70 10877 (const Reltype*) NULL,
9810d34d
SS
10878 elfcpp::SHT_RELA,
10879 r_type,
10880 lsym, is_discarded);
10881}
10882
10883// Scan a relocation for a global symbol.
10884
10885template<int size, bool big_endian>
10886inline void
10887Target_mips<size, big_endian>::Scan::global(
10888 Symbol_table* symtab,
10889 Layout* layout,
10890 Target_mips<size, big_endian>* target,
10891 Sized_relobj_file<size, big_endian>* object,
10892 unsigned int data_shndx,
10893 Output_section* output_section,
4d625b70
CC
10894 const Relatype* rela,
10895 const Reltype* rel,
9810d34d
SS
10896 unsigned int rel_type,
10897 unsigned int r_type,
10898 Symbol* gsym)
10899{
10900 Mips_address r_offset;
4d625b70 10901 unsigned int r_sym;
9810d34d
SS
10902 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
10903
10904 if (rel_type == elfcpp::SHT_RELA)
10905 {
10906 r_offset = rela->get_r_offset();
4d625b70
CC
10907 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
10908 get_r_sym(rela);
9810d34d
SS
10909 r_addend = rela->get_r_addend();
10910 }
10911 else
10912 {
10913 r_offset = rel->get_r_offset();
4d625b70
CC
10914 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
10915 get_r_sym(rel);
9810d34d
SS
10916 r_addend = 0;
10917 }
10918
9810d34d
SS
10919 Mips_relobj<size, big_endian>* mips_obj =
10920 Mips_relobj<size, big_endian>::as_mips_relobj(object);
10921 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
10922
10923 if (mips_obj->is_mips16_stub_section(data_shndx))
10924 {
10925 mips_obj->get_mips16_stub_section(data_shndx)
10926 ->new_global_reloc_found(r_type, mips_sym);
10927 }
10928
10929 if (r_type == elfcpp::R_MIPS_NONE)
10930 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
10931 // mips16 stub.
10932 return;
10933
10934 if (!mips16_call_reloc(r_type)
10935 && !mips_obj->section_allows_mips16_refs(data_shndx))
10936 // This reloc would need to refer to a MIPS16 hard-float stub, if
10937 // there is one. We ignore MIPS16 stub sections and .pdr section when
10938 // looking for relocs that would need to refer to MIPS16 stubs.
10939 mips_sym->set_need_fn_stub();
10940
9810d34d
SS
10941 // We need PLT entries if there are static-only relocations against
10942 // an externally-defined function. This can technically occur for
10943 // shared libraries if there are branches to the symbol, although it
10944 // is unlikely that this will be used in practice due to the short
10945 // ranges involved. It can occur for any relative or absolute relocation
10946 // in executables; in that case, the PLT entry becomes the function's
10947 // canonical address.
10948 bool static_reloc = false;
10949
10950 // Set CAN_MAKE_DYNAMIC to true if we can convert this
10951 // relocation into a dynamic one.
10952 bool can_make_dynamic = false;
10953 switch (r_type)
10954 {
10955 case elfcpp::R_MIPS_GOT16:
10956 case elfcpp::R_MIPS_CALL16:
10957 case elfcpp::R_MIPS_CALL_HI16:
10958 case elfcpp::R_MIPS_CALL_LO16:
10959 case elfcpp::R_MIPS_GOT_HI16:
10960 case elfcpp::R_MIPS_GOT_LO16:
10961 case elfcpp::R_MIPS_GOT_PAGE:
10962 case elfcpp::R_MIPS_GOT_OFST:
10963 case elfcpp::R_MIPS_GOT_DISP:
10964 case elfcpp::R_MIPS_TLS_GOTTPREL:
10965 case elfcpp::R_MIPS_TLS_GD:
10966 case elfcpp::R_MIPS_TLS_LDM:
10967 case elfcpp::R_MIPS16_GOT16:
10968 case elfcpp::R_MIPS16_CALL16:
10969 case elfcpp::R_MIPS16_TLS_GOTTPREL:
10970 case elfcpp::R_MIPS16_TLS_GD:
10971 case elfcpp::R_MIPS16_TLS_LDM:
10972 case elfcpp::R_MICROMIPS_GOT16:
10973 case elfcpp::R_MICROMIPS_CALL16:
10974 case elfcpp::R_MICROMIPS_CALL_HI16:
10975 case elfcpp::R_MICROMIPS_CALL_LO16:
10976 case elfcpp::R_MICROMIPS_GOT_HI16:
10977 case elfcpp::R_MICROMIPS_GOT_LO16:
10978 case elfcpp::R_MICROMIPS_GOT_PAGE:
10979 case elfcpp::R_MICROMIPS_GOT_OFST:
10980 case elfcpp::R_MICROMIPS_GOT_DISP:
10981 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
10982 case elfcpp::R_MICROMIPS_TLS_GD:
10983 case elfcpp::R_MICROMIPS_TLS_LDM:
47a9f4fc 10984 case elfcpp::R_MIPS_EH:
9810d34d
SS
10985 // We need a GOT section.
10986 target->got_section(symtab, layout);
10987 break;
10988
10989 // This is just a hint; it can safely be ignored. Don't set
10990 // has_static_relocs for the corresponding symbol.
10991 case elfcpp::R_MIPS_JALR:
10992 case elfcpp::R_MICROMIPS_JALR:
10993 break;
10994
10995 case elfcpp::R_MIPS_GPREL16:
10996 case elfcpp::R_MIPS_GPREL32:
10997 case elfcpp::R_MIPS16_GPREL:
10998 case elfcpp::R_MICROMIPS_GPREL16:
10999 // TODO(sasa)
11000 // GP-relative relocations always resolve to a definition in a
11001 // regular input file, ignoring the one-definition rule. This is
11002 // important for the GP setup sequence in NewABI code, which
11003 // always resolves to a local function even if other relocations
11004 // against the symbol wouldn't.
11005 //constrain_symbol_p = FALSE;
11006 break;
11007
11008 case elfcpp::R_MIPS_32:
11009 case elfcpp::R_MIPS_REL32:
11010 case elfcpp::R_MIPS_64:
47a9f4fc
VR
11011 if ((parameters->options().shared()
11012 || (strcmp(gsym->name(), "__gnu_local_gp") != 0
11013 && (!is_readonly_section(output_section)
11014 || mips_obj->is_pic())))
11015 && (output_section->flags() & elfcpp::SHF_ALLOC) != 0)
9810d34d
SS
11016 {
11017 if (r_type != elfcpp::R_MIPS_REL32)
47a9f4fc 11018 mips_sym->set_pointer_equality_needed();
9810d34d
SS
11019 can_make_dynamic = true;
11020 break;
11021 }
11022 // Fall through.
11023
11024 default:
11025 // Most static relocations require pointer equality, except
11026 // for branches.
11027 mips_sym->set_pointer_equality_needed();
9810d34d
SS
11028 // Fall through.
11029
11030 case elfcpp::R_MIPS_26:
11031 case elfcpp::R_MIPS_PC16:
f5b11759
VR
11032 case elfcpp::R_MIPS_PC21_S2:
11033 case elfcpp::R_MIPS_PC26_S2:
9810d34d
SS
11034 case elfcpp::R_MIPS16_26:
11035 case elfcpp::R_MICROMIPS_26_S1:
11036 case elfcpp::R_MICROMIPS_PC7_S1:
11037 case elfcpp::R_MICROMIPS_PC10_S1:
11038 case elfcpp::R_MICROMIPS_PC16_S1:
11039 case elfcpp::R_MICROMIPS_PC23_S2:
11040 static_reloc = true;
11041 mips_sym->set_has_static_relocs();
11042 break;
11043 }
11044
11045 // If there are call relocations against an externally-defined symbol,
11046 // see whether we can create a MIPS lazy-binding stub for it. We can
11047 // only do this if all references to the function are through call
11048 // relocations, and in that case, the traditional lazy-binding stubs
11049 // are much more efficient than PLT entries.
11050 switch (r_type)
11051 {
11052 case elfcpp::R_MIPS16_CALL16:
11053 case elfcpp::R_MIPS_CALL16:
11054 case elfcpp::R_MIPS_CALL_HI16:
11055 case elfcpp::R_MIPS_CALL_LO16:
11056 case elfcpp::R_MIPS_JALR:
11057 case elfcpp::R_MICROMIPS_CALL16:
11058 case elfcpp::R_MICROMIPS_CALL_HI16:
11059 case elfcpp::R_MICROMIPS_CALL_LO16:
11060 case elfcpp::R_MICROMIPS_JALR:
11061 if (!mips_sym->no_lazy_stub())
11062 {
11063 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj())
11064 // Calls from shared objects to undefined symbols of type
11065 // STT_NOTYPE need lazy-binding stub.
11066 || (mips_sym->is_undefined() && parameters->options().shared()))
11067 target->mips_stubs_section(layout)->make_entry(mips_sym);
11068 }
11069 break;
11070 default:
11071 {
11072 // We must not create a stub for a symbol that has relocations
11073 // related to taking the function's address.
11074 mips_sym->set_no_lazy_stub();
11075 target->remove_lazy_stub_entry(mips_sym);
11076 break;
11077 }
11078 }
11079
11080 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type,
11081 mips_sym->is_mips16()))
11082 mips_sym->set_has_nonpic_branches();
11083
11084 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11085 // and has a special meaning.
11086 bool gp_disp_against_hi16 = (!mips_obj->is_newabi()
11087 && strcmp(gsym->name(), "_gp_disp") == 0
11088 && (hi16_reloc(r_type) || lo16_reloc(r_type)));
11089 if (static_reloc && gsym->needs_plt_entry())
11090 {
11091 target->make_plt_entry(symtab, layout, mips_sym, r_type);
11092
11093 // Since this is not a PC-relative relocation, we may be
11094 // taking the address of a function. In that case we need to
11095 // set the entry in the dynamic symbol table to the address of
11096 // the PLT entry.
11097 if (gsym->is_from_dynobj() && !parameters->options().shared())
11098 {
11099 gsym->set_needs_dynsym_value();
11100 // We distinguish between PLT entries and lazy-binding stubs by
11101 // giving the former an st_other value of STO_MIPS_PLT. Set the
11102 // flag if there are any relocations in the binary where pointer
11103 // equality matters.
11104 if (mips_sym->pointer_equality_needed())
11105 mips_sym->set_mips_plt();
11106 }
11107 }
11108 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
11109 {
11110 // Absolute addressing relocations.
11111 // Make a dynamic relocation if necessary.
11112 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
11113 {
11114 if (gsym->may_need_copy_reloc())
11115 {
47a9f4fc
VR
11116 target->copy_reloc(symtab, layout, object, data_shndx,
11117 output_section, gsym, r_type, r_offset);
9810d34d
SS
11118 }
11119 else if (can_make_dynamic)
11120 {
11121 // Create .rel.dyn section.
11122 target->rel_dyn_section(layout);
11123 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj,
11124 data_shndx, output_section, r_offset);
11125 }
11126 else
11127 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
11128 gsym->name());
11129 }
11130 }
11131
11132 bool for_call = false;
11133 switch (r_type)
11134 {
11135 case elfcpp::R_MIPS_CALL16:
11136 case elfcpp::R_MIPS16_CALL16:
11137 case elfcpp::R_MICROMIPS_CALL16:
11138 case elfcpp::R_MIPS_CALL_HI16:
11139 case elfcpp::R_MIPS_CALL_LO16:
11140 case elfcpp::R_MICROMIPS_CALL_HI16:
11141 case elfcpp::R_MICROMIPS_CALL_LO16:
11142 for_call = true;
11143 // Fall through.
11144
11145 case elfcpp::R_MIPS16_GOT16:
11146 case elfcpp::R_MIPS_GOT16:
11147 case elfcpp::R_MIPS_GOT_HI16:
11148 case elfcpp::R_MIPS_GOT_LO16:
11149 case elfcpp::R_MICROMIPS_GOT16:
11150 case elfcpp::R_MICROMIPS_GOT_HI16:
11151 case elfcpp::R_MICROMIPS_GOT_LO16:
11152 case elfcpp::R_MIPS_GOT_DISP:
11153 case elfcpp::R_MICROMIPS_GOT_DISP:
47a9f4fc 11154 case elfcpp::R_MIPS_EH:
9810d34d
SS
11155 {
11156 // The symbol requires a GOT entry.
11157 Mips_output_data_got<size, big_endian>* got =
11158 target->got_section(symtab, layout);
11159 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11160 for_call);
11161 mips_sym->set_global_got_area(GGA_NORMAL);
11162 }
11163 break;
11164
11165 case elfcpp::R_MIPS_GOT_PAGE:
11166 case elfcpp::R_MICROMIPS_GOT_PAGE:
11167 {
11168 // This relocation needs a page entry in the GOT.
11169 // Get the section contents.
11170 section_size_type view_size = 0;
11171 const unsigned char* view =
11172 object->section_contents(data_shndx, &view_size, false);
11173 view += r_offset;
11174
11175 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
11176 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff
11177 : r_addend);
11178 Mips_output_data_got<size, big_endian>* got =
11179 target->got_section(symtab, layout);
11180 got->record_got_page_entry(mips_obj, r_sym, addend);
11181
11182 // If this is a global, overridable symbol, GOT_PAGE will
11183 // decay to GOT_DISP, so we'll need a GOT entry for it.
11184 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
11185 && !mips_sym->object()->is_dynamic()
11186 && !mips_sym->is_undefined());
11187 if (!def_regular
11188 || (parameters->options().output_is_position_independent()
11189 && !parameters->options().Bsymbolic()
11190 && !mips_sym->is_forced_local()))
11191 {
11192 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11193 for_call);
11194 mips_sym->set_global_got_area(GGA_NORMAL);
11195 }
11196 }
11197 break;
11198
11199 case elfcpp::R_MIPS_TLS_GOTTPREL:
11200 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11201 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11202 case elfcpp::R_MIPS_TLS_LDM:
11203 case elfcpp::R_MIPS16_TLS_LDM:
11204 case elfcpp::R_MICROMIPS_TLS_LDM:
11205 case elfcpp::R_MIPS_TLS_GD:
11206 case elfcpp::R_MIPS16_TLS_GD:
11207 case elfcpp::R_MICROMIPS_TLS_GD:
11208 {
11209 const bool is_final = gsym->final_value_is_known();
11210 const tls::Tls_optimization optimized_type =
11211 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
11212
11213 switch (r_type)
11214 {
11215 case elfcpp::R_MIPS_TLS_GD:
11216 case elfcpp::R_MIPS16_TLS_GD:
11217 case elfcpp::R_MICROMIPS_TLS_GD:
11218 if (optimized_type == tls::TLSOPT_NONE)
11219 {
11220 // Create a pair of GOT entries for the module index and
11221 // dtv-relative offset.
11222 Mips_output_data_got<size, big_endian>* got =
11223 target->got_section(symtab, layout);
11224 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11225 false);
11226 }
11227 else
11228 {
11229 // FIXME: TLS optimization not supported yet.
11230 gold_unreachable();
11231 }
11232 break;
11233
11234 case elfcpp::R_MIPS_TLS_LDM:
11235 case elfcpp::R_MIPS16_TLS_LDM:
11236 case elfcpp::R_MICROMIPS_TLS_LDM:
11237 if (optimized_type == tls::TLSOPT_NONE)
11238 {
11239 // We always record LDM symbols as local with index 0.
11240 target->got_section()->record_local_got_symbol(mips_obj, 0,
11241 r_addend, r_type,
47a9f4fc 11242 -1U, false);
9810d34d
SS
11243 }
11244 else
11245 {
11246 // FIXME: TLS optimization not supported yet.
11247 gold_unreachable();
11248 }
11249 break;
11250 case elfcpp::R_MIPS_TLS_GOTTPREL:
11251 case elfcpp::R_MIPS16_TLS_GOTTPREL:
11252 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
11253 layout->set_has_static_tls();
11254 if (optimized_type == tls::TLSOPT_NONE)
11255 {
11256 // Create a GOT entry for the tp-relative offset.
11257 Mips_output_data_got<size, big_endian>* got =
11258 target->got_section(symtab, layout);
11259 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false,
11260 false);
11261 }
11262 else
11263 {
11264 // FIXME: TLS optimization not supported yet.
11265 gold_unreachable();
11266 }
11267 break;
11268
11269 default:
11270 gold_unreachable();
11271 }
11272 }
11273 break;
11274 case elfcpp::R_MIPS_COPY:
11275 case elfcpp::R_MIPS_JUMP_SLOT:
11276 // These are relocations which should only be seen by the
11277 // dynamic linker, and should never be seen here.
11278 gold_error(_("%s: unexpected reloc %u in object file"),
11279 object->name().c_str(), r_type);
11280 break;
11281
11282 default:
11283 break;
11284 }
11285
11286 // Refuse some position-dependent relocations when creating a
11287 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
11288 // not PIC, but we can create dynamic relocations and the result
11289 // will be fine. Also do not refuse R_MIPS_LO16, which can be
11290 // combined with R_MIPS_GOT16.
11291 if (parameters->options().shared())
11292 {
11293 switch (r_type)
11294 {
11295 case elfcpp::R_MIPS16_HI16:
11296 case elfcpp::R_MIPS_HI16:
e242ece1
VR
11297 case elfcpp::R_MIPS_HIGHER:
11298 case elfcpp::R_MIPS_HIGHEST:
9810d34d 11299 case elfcpp::R_MICROMIPS_HI16:
e242ece1
VR
11300 case elfcpp::R_MICROMIPS_HIGHER:
11301 case elfcpp::R_MICROMIPS_HIGHEST:
9810d34d
SS
11302 // Don't refuse a high part relocation if it's against
11303 // no symbol (e.g. part of a compound relocation).
11304 if (r_sym == 0)
11305 break;
11306
11307 // R_MIPS_HI16 against _gp_disp is used for $gp setup,
11308 // and has a special meaning.
11309 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0)
11310 break;
d8e90251 11311 // Fall through.
9810d34d
SS
11312
11313 case elfcpp::R_MIPS16_26:
11314 case elfcpp::R_MIPS_26:
11315 case elfcpp::R_MICROMIPS_26_S1:
11316 gold_error(_("%s: relocation %u against `%s' can not be used when "
11317 "making a shared object; recompile with -fPIC"),
11318 object->name().c_str(), r_type, gsym->name());
11319 default:
11320 break;
11321 }
11322 }
11323}
11324
11325template<int size, bool big_endian>
11326inline void
11327Target_mips<size, big_endian>::Scan::global(
11328 Symbol_table* symtab,
11329 Layout* layout,
11330 Target_mips<size, big_endian>* target,
11331 Sized_relobj_file<size, big_endian>* object,
11332 unsigned int data_shndx,
11333 Output_section* output_section,
4d625b70 11334 const Relatype& reloc,
9810d34d
SS
11335 unsigned int r_type,
11336 Symbol* gsym)
11337{
11338 global(
11339 symtab,
11340 layout,
11341 target,
11342 object,
11343 data_shndx,
11344 output_section,
11345 &reloc,
4d625b70 11346 (const Reltype*) NULL,
9810d34d
SS
11347 elfcpp::SHT_RELA,
11348 r_type,
11349 gsym);
11350}
11351
11352template<int size, bool big_endian>
11353inline void
11354Target_mips<size, big_endian>::Scan::global(
11355 Symbol_table* symtab,
11356 Layout* layout,
11357 Target_mips<size, big_endian>* target,
11358 Sized_relobj_file<size, big_endian>* object,
11359 unsigned int data_shndx,
11360 Output_section* output_section,
4d625b70 11361 const Reltype& reloc,
9810d34d
SS
11362 unsigned int r_type,
11363 Symbol* gsym)
11364{
11365 global(
11366 symtab,
11367 layout,
11368 target,
11369 object,
11370 data_shndx,
11371 output_section,
4d625b70 11372 (const Relatype*) NULL,
9810d34d
SS
11373 &reloc,
11374 elfcpp::SHT_REL,
11375 r_type,
11376 gsym);
11377}
11378
47a9f4fc
VR
11379// Return whether a R_MIPS_32/R_MIPS64 relocation needs to be applied.
11380// In cases where Scan::local() or Scan::global() has created
11381// a dynamic relocation, the addend of the relocation is carried
11382// in the data, and we must not apply the static relocation.
9810d34d
SS
11383
11384template<int size, bool big_endian>
11385inline bool
47a9f4fc 11386Target_mips<size, big_endian>::Relocate::should_apply_static_reloc(
9810d34d
SS
11387 const Mips_symbol<size>* gsym,
11388 unsigned int r_type,
11389 Output_section* output_section,
11390 Target_mips* target)
11391{
11392 // If the output section is not allocated, then we didn't call
11393 // scan_relocs, we didn't create a dynamic reloc, and we must apply
11394 // the reloc here.
11395 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
11396 return true;
11397
11398 if (gsym == NULL)
11399 return true;
11400 else
11401 {
11402 // For global symbols, we use the same helper routines used in the
11403 // scan pass.
11404 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
11405 && !gsym->may_need_copy_reloc())
11406 {
11407 // We have generated dynamic reloc (R_MIPS_REL32).
11408
11409 bool multi_got = false;
11410 if (target->has_got_section())
11411 multi_got = target->got_section()->multi_got();
11412 bool has_got_offset;
11413 if (!multi_got)
11414 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
11415 else
11416 has_got_offset = gsym->global_gotoffset() != -1U;
11417 if (!has_got_offset)
11418 return true;
11419 else
11420 // Apply the relocation only if the symbol is in the local got.
11421 // Do not apply the relocation if the symbol is in the global
11422 // got.
11423 return symbol_references_local(gsym, gsym->has_dynsym_index());
11424 }
11425 else
11426 // We have not generated dynamic reloc.
11427 return true;
11428 }
11429}
11430
11431// Perform a relocation.
11432
11433template<int size, bool big_endian>
11434inline bool
11435Target_mips<size, big_endian>::Relocate::relocate(
11436 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 11437 unsigned int rel_type,
9810d34d
SS
11438 Target_mips* target,
11439 Output_section* output_section,
11440 size_t relnum,
91a65d2f 11441 const unsigned char* preloc,
9810d34d
SS
11442 const Sized_symbol<size>* gsym,
11443 const Symbol_value<size>* psymval,
11444 unsigned char* view,
11445 Mips_address address,
11446 section_size_type)
11447{
11448 Mips_address r_offset;
4d625b70
CC
11449 unsigned int r_sym;
11450 unsigned int r_type;
47a9f4fc
VR
11451 unsigned int r_type2;
11452 unsigned int r_type3;
11453 unsigned char r_ssym;
9810d34d 11454 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
152c92b2
VR
11455 // r_offset and r_type of the next relocation is needed for resolving multiple
11456 // consecutive relocations with the same offset.
11457 Mips_address next_r_offset = static_cast<Mips_address>(0) - 1;
11458 unsigned int next_r_type = elfcpp::R_MIPS_NONE;
11459
11460 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11461 size_t reloc_count = shdr.get_sh_size() / shdr.get_sh_entsize();
9810d34d
SS
11462
11463 if (rel_type == elfcpp::SHT_RELA)
11464 {
4d625b70 11465 const Relatype rela(preloc);
91a65d2f 11466 r_offset = rela.get_r_offset();
4d625b70
CC
11467 r_sym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11468 get_r_sym(&rela);
11469 r_type = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11470 get_r_type(&rela);
47a9f4fc
VR
11471 r_type2 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11472 get_r_type2(&rela);
11473 r_type3 = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11474 get_r_type3(&rela);
11475 r_ssym = Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11476 get_r_ssym(&rela);
91a65d2f 11477 r_addend = rela.get_r_addend();
152c92b2
VR
11478 // If this is not last relocation, get r_offset and r_type of the next
11479 // relocation.
11480 if (relnum + 1 < reloc_count)
11481 {
11482 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11483 const Relatype next_rela(preloc + reloc_size);
11484 next_r_offset = next_rela.get_r_offset();
11485 next_r_type =
11486 Mips_classify_reloc<elfcpp::SHT_RELA, size, big_endian>::
11487 get_r_type(&next_rela);
11488 }
9810d34d
SS
11489 }
11490 else
11491 {
4d625b70 11492 const Reltype rel(preloc);
91a65d2f 11493 r_offset = rel.get_r_offset();
4d625b70
CC
11494 r_sym = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11495 get_r_sym(&rel);
11496 r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11497 get_r_type(&rel);
47a9f4fc 11498 r_ssym = 0;
152c92b2
VR
11499 r_type2 = elfcpp::R_MIPS_NONE;
11500 r_type3 = elfcpp::R_MIPS_NONE;
9810d34d 11501 r_addend = 0;
152c92b2
VR
11502 // If this is not last relocation, get r_offset and r_type of the next
11503 // relocation.
11504 if (relnum + 1 < reloc_count)
11505 {
11506 const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
11507 const Reltype next_rel(preloc + reloc_size);
11508 next_r_offset = next_rel.get_r_offset();
11509 next_r_type = Mips_classify_reloc<elfcpp::SHT_REL, size, big_endian>::
11510 get_r_type(&next_rel);
11511 }
9810d34d
SS
11512 }
11513
11514 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
11515 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
11516
11517 Mips_relobj<size, big_endian>* object =
4d625b70 11518 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object);
9810d34d 11519
9810d34d
SS
11520 bool target_is_16_bit_code = false;
11521 bool target_is_micromips_code = false;
11522 bool cross_mode_jump;
11523
11524 Symbol_value<size> symval;
11525
11526 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
11527
11528 bool changed_symbol_value = false;
11529 if (gsym == NULL)
11530 {
11531 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym);
11532 target_is_micromips_code = object->local_symbol_is_micromips(r_sym);
11533 if (target_is_16_bit_code || target_is_micromips_code)
11534 {
11535 // MIPS16/microMIPS text labels should be treated as odd.
11536 symval.set_output_value(psymval->value(object, 1));
11537 psymval = &symval;
11538 changed_symbol_value = true;
11539 }
11540 }
11541 else
11542 {
11543 target_is_16_bit_code = mips_sym->is_mips16();
11544 target_is_micromips_code = mips_sym->is_micromips();
11545
11546 // If this is a mips16/microMIPS text symbol, add 1 to the value to make
11547 // it odd. This will cause something like .word SYM to come up with
11548 // the right value when it is loaded into the PC.
11549
11550 if ((mips_sym->is_mips16() || mips_sym->is_micromips())
11551 && psymval->value(object, 0) != 0)
11552 {
11553 symval.set_output_value(psymval->value(object, 0) | 1);
11554 psymval = &symval;
11555 changed_symbol_value = true;
11556 }
11557
11558 // Pick the value to use for symbols defined in shared objects.
11559 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
11560 || mips_sym->has_lazy_stub())
11561 {
11562 Mips_address value;
11563 if (!mips_sym->has_lazy_stub())
11564 {
11565 // Prefer a standard MIPS PLT entry.
11566 if (mips_sym->has_mips_plt_offset())
11567 {
11568 value = target->plt_section()->mips_entry_address(mips_sym);
11569 target_is_micromips_code = false;
11570 target_is_16_bit_code = false;
11571 }
11572 else
11573 {
11574 value = (target->plt_section()->comp_entry_address(mips_sym)
11575 + 1);
11576 if (target->is_output_micromips())
11577 target_is_micromips_code = true;
11578 else
11579 target_is_16_bit_code = true;
11580 }
11581 }
11582 else
11583 value = target->mips_stubs_section()->stub_address(mips_sym);
11584
11585 symval.set_output_value(value);
11586 psymval = &symval;
11587 }
11588 }
11589
11590 // TRUE if the symbol referred to by this relocation is "_gp_disp".
11591 // Note that such a symbol must always be a global symbol.
11592 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
11593 && !object->is_newabi());
11594
11595 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
11596 // Note that such a symbol must always be a global symbol.
11597 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
11598
11599
11600 if (gp_disp)
11601 {
11602 if (!hi16_reloc(r_type) && !lo16_reloc(r_type))
11603 gold_error_at_location(relinfo, relnum, r_offset,
11604 _("relocations against _gp_disp are permitted only"
11605 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
11606 }
11607 else if (gnu_local_gp)
11608 {
11609 // __gnu_local_gp is _gp symbol.
11610 symval.set_output_value(target->adjusted_gp_value(object));
11611 psymval = &symval;
11612 }
11613
11614 // If this is a reference to a 16-bit function with a stub, we need
11615 // to redirect the relocation to the stub unless:
11616 //
11617 // (a) the relocation is for a MIPS16 JAL;
11618 //
11619 // (b) the relocation is for a MIPS16 PIC call, and there are no
11620 // non-MIPS16 uses of the GOT slot; or
11621 //
11622 // (c) the section allows direct references to MIPS16 functions.
11623 if (r_type != elfcpp::R_MIPS16_26
9810d34d
SS
11624 && ((mips_sym != NULL
11625 && mips_sym->has_mips16_fn_stub()
11626 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
11627 || (mips_sym == NULL
11628 && object->get_local_mips16_fn_stub(r_sym) != NULL))
11629 && !object->section_allows_mips16_refs(relinfo->data_shndx))
11630 {
11631 // This is a 32- or 64-bit call to a 16-bit function. We should
11632 // have already noticed that we were going to need the
11633 // stub.
11634 Mips_address value;
11635 if (mips_sym == NULL)
11636 value = object->get_local_mips16_fn_stub(r_sym)->output_address();
11637 else
11638 {
11639 gold_assert(mips_sym->need_fn_stub());
11640 if (mips_sym->has_la25_stub())
11641 value = target->la25_stub_section()->stub_address(mips_sym);
11642 else
11643 {
11644 value = mips_sym->template
11645 get_mips16_fn_stub<big_endian>()->output_address();
11646 }
11647 }
11648 symval.set_output_value(value);
11649 psymval = &symval;
11650 changed_symbol_value = true;
11651
11652 // The target is 16-bit, but the stub isn't.
11653 target_is_16_bit_code = false;
11654 }
11655 // If this is a MIPS16 call with a stub, that is made through the PLT or
11656 // to a standard MIPS function, we need to redirect the call to the stub.
11657 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
11658 // indirect calls should use an indirect stub instead.
1e1247c8 11659 else if (r_type == elfcpp::R_MIPS16_26
9810d34d
SS
11660 && ((mips_sym != NULL
11661 && (mips_sym->has_mips16_call_stub()
11662 || mips_sym->has_mips16_call_fp_stub()))
11663 || (mips_sym == NULL
11664 && object->get_local_mips16_call_stub(r_sym) != NULL))
11665 && ((mips_sym != NULL && mips_sym->has_plt_offset())
11666 || !target_is_16_bit_code))
11667 {
11668 Mips16_stub_section<size, big_endian>* call_stub;
11669 if (mips_sym == NULL)
11670 call_stub = object->get_local_mips16_call_stub(r_sym);
11671 else
11672 {
11673 // If both call_stub and call_fp_stub are defined, we can figure
11674 // out which one to use by checking which one appears in the input
11675 // file.
11676 if (mips_sym->has_mips16_call_stub()
11677 && mips_sym->has_mips16_call_fp_stub())
11678 {
11679 call_stub = NULL;
11680 for (unsigned int i = 1; i < object->shnum(); ++i)
11681 {
11682 if (object->is_mips16_call_fp_stub_section(i))
11683 {
11684 call_stub = mips_sym->template
11685 get_mips16_call_fp_stub<big_endian>();
11686 break;
11687 }
11688
11689 }
11690 if (call_stub == NULL)
11691 call_stub =
11692 mips_sym->template get_mips16_call_stub<big_endian>();
11693 }
11694 else if (mips_sym->has_mips16_call_stub())
11695 call_stub = mips_sym->template get_mips16_call_stub<big_endian>();
11696 else
11697 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>();
11698 }
11699
11700 symval.set_output_value(call_stub->output_address());
11701 psymval = &symval;
11702 changed_symbol_value = true;
11703 }
11704 // If this is a direct call to a PIC function, redirect to the
11705 // non-PIC stub.
11706 else if (mips_sym != NULL
11707 && mips_sym->has_la25_stub()
11708 && relocation_needs_la25_stub<size, big_endian>(
11709 object, r_type, target_is_16_bit_code))
11710 {
11711 Mips_address value = target->la25_stub_section()->stub_address(mips_sym);
11712 if (mips_sym->is_micromips())
11713 value += 1;
11714 symval.set_output_value(value);
11715 psymval = &symval;
11716 }
11717 // For direct MIPS16 and microMIPS calls make sure the compressed PLT
11718 // entry is used if a standard PLT entry has also been made.
11719 else if ((r_type == elfcpp::R_MIPS16_26
11720 || r_type == elfcpp::R_MICROMIPS_26_S1)
9810d34d
SS
11721 && mips_sym != NULL
11722 && mips_sym->has_plt_offset()
11723 && mips_sym->has_comp_plt_offset()
11724 && mips_sym->has_mips_plt_offset())
11725 {
11726 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym)
11727 + 1);
11728 symval.set_output_value(value);
11729 psymval = &symval;
11730
11731 target_is_16_bit_code = !target->is_output_micromips();
11732 target_is_micromips_code = target->is_output_micromips();
11733 }
11734
11735 // Make sure MIPS16 and microMIPS are not used together.
11736 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code)
11737 || (micromips_branch_reloc(r_type) && target_is_16_bit_code))
11738 {
11739 gold_error(_("MIPS16 and microMIPS functions cannot call each other"));
11740 }
11741
11742 // Calls from 16-bit code to 32-bit code and vice versa require the
11743 // mode change. However, we can ignore calls to undefined weak symbols,
11744 // which should never be executed at runtime. This exception is important
11745 // because the assembly writer may have "known" that any definition of the
11746 // symbol would be 16-bit code, and that direct jumps were therefore
11747 // acceptable.
11748 cross_mode_jump =
1e1247c8 11749 (!(gsym != NULL && gsym->is_weak_undefined())
9810d34d
SS
11750 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
11751 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
11752 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
11753 && (target_is_16_bit_code || target_is_micromips_code))));
11754
11755 bool local = (mips_sym == NULL
11756 || (mips_sym->got_only_for_calls()
11757 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
11758 : symbol_references_local(mips_sym,
11759 mips_sym->has_dynsym_index())));
11760
11761 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
11762 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
11763 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
11764 if (got_page_reloc(r_type) && !local)
11765 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
11766 : elfcpp::R_MIPS_GOT_DISP);
11767
11768 unsigned int got_offset = 0;
11769 int gp_offset = 0;
11770
152c92b2 11771 // Whether we have to extract addend from instruction.
9810d34d 11772 bool extract_addend = rel_type == elfcpp::SHT_REL;
47a9f4fc
VR
11773 unsigned int r_types[3] = { r_type, r_type2, r_type3 };
11774
11775 Reloc_funcs::mips_reloc_unshuffle(view, r_type, false);
11776
11777 // For Mips64 N64 ABI, there may be up to three operations specified per
11778 // record, by the fields r_type, r_type2, and r_type3. The first operation
11779 // takes its addend from the relocation record. Each subsequent operation
11780 // takes as its addend the result of the previous operation.
11781 // The first operation in a record which references a symbol uses the symbol
11782 // implied by r_sym. The next operation in a record which references a symbol
11783 // uses the special symbol value given by the r_ssym field. A third operation
11784 // in a record which references a symbol will assume a NULL symbol,
11785 // i.e. value zero.
11786
11787 // TODO(Vladimir)
11788 // Check if a record references to a symbol.
11789 for (unsigned int i = 0; i < 3; ++i)
9810d34d 11790 {
47a9f4fc
VR
11791 if (r_types[i] == elfcpp::R_MIPS_NONE)
11792 break;
9810d34d 11793
152c92b2
VR
11794 // If we didn't apply previous relocation, use its result as addend
11795 // for current.
11796 if (this->calculate_only_)
11797 {
11798 r_addend = this->calculated_value_;
11799 extract_addend = false;
11800 }
11801
11802 // In the N32 and 64-bit ABIs there may be multiple consecutive
11803 // relocations for the same offset. In that case we are
11804 // supposed to treat the output of each relocation as the addend
11805 // for the next. For N64 ABI, we are checking offsets only in a
11806 // third operation in a record (r_type3).
11807 this->calculate_only_ =
11808 (object->is_n64() && i < 2
11809 ? r_types[i+1] != elfcpp::R_MIPS_NONE
11810 : (r_offset == next_r_offset) && (next_r_type != elfcpp::R_MIPS_NONE));
47a9f4fc
VR
11811
11812 if (object->is_n64())
9810d34d 11813 {
47a9f4fc 11814 if (i == 1)
9810d34d 11815 {
47a9f4fc
VR
11816 // Handle special symbol for r_type2 relocation type.
11817 switch (r_ssym)
11818 {
11819 case RSS_UNDEF:
11820 symval.set_output_value(0);
11821 break;
11822 case RSS_GP:
11823 symval.set_output_value(target->gp_value());
11824 break;
11825 case RSS_GP0:
11826 symval.set_output_value(object->gp_value());
11827 break;
11828 case RSS_LOC:
11829 symval.set_output_value(address);
11830 break;
11831 default:
11832 gold_unreachable();
11833 }
9810d34d
SS
11834 psymval = &symval;
11835 }
47a9f4fc
VR
11836 else if (i == 2)
11837 {
11838 // For r_type3 symbol value is 0.
11839 symval.set_output_value(0);
11840 }
9810d34d 11841 }
9810d34d 11842
47a9f4fc
VR
11843 bool update_got_entry = false;
11844 switch (r_types[i])
11845 {
11846 case elfcpp::R_MIPS_NONE:
11847 break;
11848 case elfcpp::R_MIPS_16:
11849 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
152c92b2
VR
11850 extract_addend,
11851 this->calculate_only_,
11852 &this->calculated_value_);
47a9f4fc 11853 break;
9810d34d 11854
47a9f4fc
VR
11855 case elfcpp::R_MIPS_32:
11856 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11857 target))
11858 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
152c92b2
VR
11859 extract_addend,
11860 this->calculate_only_,
11861 &this->calculated_value_);
47a9f4fc
VR
11862 if (mips_sym != NULL
11863 && (mips_sym->is_mips16() || mips_sym->is_micromips())
11864 && mips_sym->global_got_area() == GGA_RELOC_ONLY)
11865 {
11866 // If mips_sym->has_mips16_fn_stub() is false, symbol value is
11867 // already updated by adding +1.
11868 if (mips_sym->has_mips16_fn_stub())
11869 {
11870 gold_assert(mips_sym->need_fn_stub());
11871 Mips16_stub_section<size, big_endian>* fn_stub =
11872 mips_sym->template get_mips16_fn_stub<big_endian>();
9810d34d 11873
47a9f4fc
VR
11874 symval.set_output_value(fn_stub->output_address());
11875 psymval = &symval;
11876 }
11877 got_offset = mips_sym->global_gotoffset();
11878 update_got_entry = true;
11879 }
11880 break;
9810d34d 11881
47a9f4fc
VR
11882 case elfcpp::R_MIPS_64:
11883 if (should_apply_static_reloc(mips_sym, r_types[i], output_section,
11884 target))
11885 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
152c92b2
VR
11886 extract_addend,
11887 this->calculate_only_,
11888 &this->calculated_value_, false);
47a9f4fc
VR
11889 else if (target->is_output_n64() && r_addend != 0)
11890 // Only apply the addend. The static relocation was RELA, but the
11891 // dynamic relocation is REL, so we need to apply the addend.
11892 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend,
152c92b2
VR
11893 extract_addend,
11894 this->calculate_only_,
11895 &this->calculated_value_, true);
47a9f4fc
VR
11896 break;
11897 case elfcpp::R_MIPS_REL32:
11898 gold_unreachable();
9810d34d 11899
47a9f4fc
VR
11900 case elfcpp::R_MIPS_PC32:
11901 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
11902 r_addend, extract_addend,
152c92b2
VR
11903 this->calculate_only_,
11904 &this->calculated_value_);
47a9f4fc 11905 break;
9810d34d 11906
47a9f4fc
VR
11907 case elfcpp::R_MIPS16_26:
11908 // The calculation for R_MIPS16_26 is just the same as for an
11909 // R_MIPS_26. It's only the storage of the relocated field into
11910 // the output file that's different. So, we just fall through to the
11911 // R_MIPS_26 case here.
11912 case elfcpp::R_MIPS_26:
11913 case elfcpp::R_MICROMIPS_26_S1:
11914 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
11915 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump,
152c92b2
VR
11916 r_types[i], target->jal_to_bal(), this->calculate_only_,
11917 &this->calculated_value_);
47a9f4fc 11918 break;
9810d34d 11919
47a9f4fc
VR
11920 case elfcpp::R_MIPS_HI16:
11921 case elfcpp::R_MIPS16_HI16:
11922 case elfcpp::R_MICROMIPS_HI16:
11923 if (rel_type == elfcpp::SHT_RELA)
11924 reloc_status = Reloc_funcs::do_relhi16(view, object, psymval,
11925 r_addend, address,
11926 gp_disp, r_types[i],
11927 extract_addend, 0,
152c92b2
VR
11928 target,
11929 this->calculate_only_,
11930 &this->calculated_value_);
47a9f4fc
VR
11931 else if (rel_type == elfcpp::SHT_REL)
11932 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
11933 address, gp_disp, r_types[i],
11934 r_sym, extract_addend);
11935 else
11936 gold_unreachable();
11937 break;
9810d34d 11938
47a9f4fc
VR
11939 case elfcpp::R_MIPS_LO16:
11940 case elfcpp::R_MIPS16_LO16:
11941 case elfcpp::R_MICROMIPS_LO16:
11942 case elfcpp::R_MICROMIPS_HI0_LO16:
11943 reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
11944 r_addend, extract_addend, address,
11945 gp_disp, r_types[i], r_sym,
152c92b2
VR
11946 rel_type, this->calculate_only_,
11947 &this->calculated_value_);
47a9f4fc 11948 break;
9810d34d 11949
47a9f4fc
VR
11950 case elfcpp::R_MIPS_LITERAL:
11951 case elfcpp::R_MICROMIPS_LITERAL:
11952 // Because we don't merge literal sections, we can handle this
11953 // just like R_MIPS_GPREL16. In the long run, we should merge
11954 // shared literals, and then we will need to additional work
11955 // here.
9810d34d 11956
47a9f4fc 11957 // Fall through.
9810d34d 11958
47a9f4fc
VR
11959 case elfcpp::R_MIPS_GPREL16:
11960 case elfcpp::R_MIPS16_GPREL:
11961 case elfcpp::R_MICROMIPS_GPREL7_S2:
11962 case elfcpp::R_MICROMIPS_GPREL16:
11963 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
11964 target->adjusted_gp_value(object),
11965 r_addend, extract_addend,
11966 gsym == NULL, r_types[i],
152c92b2
VR
11967 this->calculate_only_,
11968 &this->calculated_value_);
47a9f4fc 11969 break;
9810d34d 11970
47a9f4fc
VR
11971 case elfcpp::R_MIPS_PC16:
11972 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
11973 r_addend, extract_addend,
152c92b2
VR
11974 this->calculate_only_,
11975 &this->calculated_value_);
47a9f4fc 11976 break;
f5b11759
VR
11977
11978 case elfcpp::R_MIPS_PC21_S2:
11979 reloc_status = Reloc_funcs::relpc21(view, object, psymval, address,
11980 r_addend, extract_addend,
152c92b2
VR
11981 this->calculate_only_,
11982 &this->calculated_value_);
f5b11759
VR
11983 break;
11984
11985 case elfcpp::R_MIPS_PC26_S2:
11986 reloc_status = Reloc_funcs::relpc26(view, object, psymval, address,
11987 r_addend, extract_addend,
152c92b2
VR
11988 this->calculate_only_,
11989 &this->calculated_value_);
f5b11759
VR
11990 break;
11991
11992 case elfcpp::R_MIPS_PC18_S3:
11993 reloc_status = Reloc_funcs::relpc18(view, object, psymval, address,
11994 r_addend, extract_addend,
152c92b2
VR
11995 this->calculate_only_,
11996 &this->calculated_value_);
f5b11759
VR
11997 break;
11998
11999 case elfcpp::R_MIPS_PC19_S2:
12000 reloc_status = Reloc_funcs::relpc19(view, object, psymval, address,
12001 r_addend, extract_addend,
152c92b2
VR
12002 this->calculate_only_,
12003 &this->calculated_value_);
f5b11759
VR
12004 break;
12005
12006 case elfcpp::R_MIPS_PCHI16:
12007 if (rel_type == elfcpp::SHT_RELA)
12008 reloc_status = Reloc_funcs::do_relpchi16(view, object, psymval,
12009 r_addend, address,
12010 extract_addend, 0,
152c92b2
VR
12011 this->calculate_only_,
12012 &this->calculated_value_);
f5b11759
VR
12013 else if (rel_type == elfcpp::SHT_REL)
12014 reloc_status = Reloc_funcs::relpchi16(view, object, psymval,
12015 r_addend, address, r_sym,
12016 extract_addend);
12017 else
12018 gold_unreachable();
12019 break;
12020
12021 case elfcpp::R_MIPS_PCLO16:
12022 reloc_status = Reloc_funcs::relpclo16(view, object, psymval, r_addend,
12023 extract_addend, address, r_sym,
152c92b2
VR
12024 rel_type, this->calculate_only_,
12025 &this->calculated_value_);
f5b11759 12026 break;
47a9f4fc
VR
12027 case elfcpp::R_MICROMIPS_PC7_S1:
12028 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval,
152c92b2
VR
12029 address, r_addend,
12030 extract_addend,
12031 this->calculate_only_,
12032 &this->calculated_value_);
47a9f4fc
VR
12033 break;
12034 case elfcpp::R_MICROMIPS_PC10_S1:
12035 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object,
152c92b2
VR
12036 psymval, address,
12037 r_addend, extract_addend,
12038 this->calculate_only_,
12039 &this->calculated_value_);
47a9f4fc
VR
12040 break;
12041 case elfcpp::R_MICROMIPS_PC16_S1:
12042 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object,
152c92b2
VR
12043 psymval, address,
12044 r_addend, extract_addend,
12045 this->calculate_only_,
12046 &this->calculated_value_);
47a9f4fc
VR
12047 break;
12048 case elfcpp::R_MIPS_GPREL32:
12049 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
12050 target->adjusted_gp_value(object),
12051 r_addend, extract_addend,
152c92b2
VR
12052 this->calculate_only_,
12053 &this->calculated_value_);
47a9f4fc
VR
12054 break;
12055 case elfcpp::R_MIPS_GOT_HI16:
12056 case elfcpp::R_MIPS_CALL_HI16:
12057 case elfcpp::R_MICROMIPS_GOT_HI16:
12058 case elfcpp::R_MICROMIPS_CALL_HI16:
12059 if (gsym != NULL)
12060 got_offset = target->got_section()->got_offset(gsym,
12061 GOT_TYPE_STANDARD,
12062 object);
12063 else
12064 got_offset = target->got_section()->got_offset(r_sym,
12065 GOT_TYPE_STANDARD,
12066 object, r_addend);
12067 gp_offset = target->got_section()->gp_offset(got_offset, object);
12068 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset,
152c92b2
VR
12069 this->calculate_only_,
12070 &this->calculated_value_);
47a9f4fc
VR
12071 update_got_entry = changed_symbol_value;
12072 break;
9810d34d 12073
47a9f4fc
VR
12074 case elfcpp::R_MIPS_GOT_LO16:
12075 case elfcpp::R_MIPS_CALL_LO16:
12076 case elfcpp::R_MICROMIPS_GOT_LO16:
12077 case elfcpp::R_MICROMIPS_CALL_LO16:
12078 if (gsym != NULL)
12079 got_offset = target->got_section()->got_offset(gsym,
12080 GOT_TYPE_STANDARD,
12081 object);
12082 else
12083 got_offset = target->got_section()->got_offset(r_sym,
12084 GOT_TYPE_STANDARD,
12085 object, r_addend);
12086 gp_offset = target->got_section()->gp_offset(got_offset, object);
12087 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset,
152c92b2
VR
12088 this->calculate_only_,
12089 &this->calculated_value_);
47a9f4fc
VR
12090 update_got_entry = changed_symbol_value;
12091 break;
12092
12093 case elfcpp::R_MIPS_GOT_DISP:
12094 case elfcpp::R_MICROMIPS_GOT_DISP:
12095 case elfcpp::R_MIPS_EH:
12096 if (gsym != NULL)
12097 got_offset = target->got_section()->got_offset(gsym,
12098 GOT_TYPE_STANDARD,
12099 object);
12100 else
12101 got_offset = target->got_section()->got_offset(r_sym,
12102 GOT_TYPE_STANDARD,
12103 object, r_addend);
12104 gp_offset = target->got_section()->gp_offset(got_offset, object);
12105 if (eh_reloc(r_types[i]))
12106 reloc_status = Reloc_funcs::releh(view, gp_offset,
152c92b2
VR
12107 this->calculate_only_,
12108 &this->calculated_value_);
47a9f4fc
VR
12109 else
12110 reloc_status = Reloc_funcs::relgot(view, gp_offset,
152c92b2
VR
12111 this->calculate_only_,
12112 &this->calculated_value_);
47a9f4fc
VR
12113 break;
12114 case elfcpp::R_MIPS_CALL16:
12115 case elfcpp::R_MIPS16_CALL16:
12116 case elfcpp::R_MICROMIPS_CALL16:
12117 gold_assert(gsym != NULL);
9810d34d
SS
12118 got_offset = target->got_section()->got_offset(gsym,
12119 GOT_TYPE_STANDARD,
12120 object);
12121 gp_offset = target->got_section()->gp_offset(got_offset, object);
47a9f4fc 12122 reloc_status = Reloc_funcs::relgot(view, gp_offset,
152c92b2
VR
12123 this->calculate_only_,
12124 &this->calculated_value_);
47a9f4fc
VR
12125 // TODO(sasa): We should also initialize update_got_entry
12126 // in other place swhere relgot is called.
12127 update_got_entry = changed_symbol_value;
12128 break;
9810d34d 12129
47a9f4fc
VR
12130 case elfcpp::R_MIPS_GOT16:
12131 case elfcpp::R_MIPS16_GOT16:
12132 case elfcpp::R_MICROMIPS_GOT16:
12133 if (gsym != NULL)
12134 {
12135 got_offset = target->got_section()->got_offset(gsym,
12136 GOT_TYPE_STANDARD,
12137 object);
12138 gp_offset = target->got_section()->gp_offset(got_offset, object);
12139 reloc_status = Reloc_funcs::relgot(view, gp_offset,
152c92b2
VR
12140 this->calculate_only_,
12141 &this->calculated_value_);
47a9f4fc
VR
12142 }
12143 else
12144 {
12145 if (rel_type == elfcpp::SHT_RELA)
12146 reloc_status = Reloc_funcs::do_relgot16_local(view, object,
152c92b2
VR
12147 psymval, r_addend,
12148 extract_addend, 0,
12149 target,
12150 this->calculate_only_,
12151 &this->calculated_value_);
47a9f4fc
VR
12152 else if (rel_type == elfcpp::SHT_REL)
12153 reloc_status = Reloc_funcs::relgot16_local(view, object,
12154 psymval, r_addend,
12155 extract_addend,
12156 r_types[i], r_sym);
12157 else
12158 gold_unreachable();
12159 }
12160 update_got_entry = changed_symbol_value;
12161 break;
9810d34d 12162
47a9f4fc
VR
12163 case elfcpp::R_MIPS_TLS_GD:
12164 case elfcpp::R_MIPS16_TLS_GD:
12165 case elfcpp::R_MICROMIPS_TLS_GD:
12166 if (gsym != NULL)
12167 got_offset = target->got_section()->got_offset(gsym,
12168 GOT_TYPE_TLS_PAIR,
12169 object);
12170 else
12171 got_offset = target->got_section()->got_offset(r_sym,
12172 GOT_TYPE_TLS_PAIR,
12173 object, r_addend);
12174 gp_offset = target->got_section()->gp_offset(got_offset, object);
152c92b2
VR
12175 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12176 this->calculate_only_,
12177 &this->calculated_value_);
47a9f4fc 12178 break;
9810d34d 12179
47a9f4fc
VR
12180 case elfcpp::R_MIPS_TLS_GOTTPREL:
12181 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12182 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12183 if (gsym != NULL)
12184 got_offset = target->got_section()->got_offset(gsym,
12185 GOT_TYPE_TLS_OFFSET,
12186 object);
12187 else
12188 got_offset = target->got_section()->got_offset(r_sym,
12189 GOT_TYPE_TLS_OFFSET,
12190 object, r_addend);
12191 gp_offset = target->got_section()->gp_offset(got_offset, object);
152c92b2
VR
12192 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12193 this->calculate_only_,
12194 &this->calculated_value_);
47a9f4fc 12195 break;
9810d34d 12196
47a9f4fc
VR
12197 case elfcpp::R_MIPS_TLS_LDM:
12198 case elfcpp::R_MIPS16_TLS_LDM:
12199 case elfcpp::R_MICROMIPS_TLS_LDM:
12200 // Relocate the field with the offset of the GOT entry for
12201 // the module index.
12202 got_offset = target->got_section()->tls_ldm_offset(object);
12203 gp_offset = target->got_section()->gp_offset(got_offset, object);
152c92b2
VR
12204 reloc_status = Reloc_funcs::relgot(view, gp_offset,
12205 this->calculate_only_,
12206 &this->calculated_value_);
47a9f4fc 12207 break;
9810d34d 12208
47a9f4fc
VR
12209 case elfcpp::R_MIPS_GOT_PAGE:
12210 case elfcpp::R_MICROMIPS_GOT_PAGE:
12211 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
12212 r_addend, extract_addend,
152c92b2
VR
12213 this->calculate_only_,
12214 &this->calculated_value_);
47a9f4fc 12215 break;
9810d34d 12216
47a9f4fc
VR
12217 case elfcpp::R_MIPS_GOT_OFST:
12218 case elfcpp::R_MICROMIPS_GOT_OFST:
12219 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
12220 r_addend, extract_addend,
152c92b2
VR
12221 local, this->calculate_only_,
12222 &this->calculated_value_);
47a9f4fc 12223 break;
9810d34d 12224
47a9f4fc
VR
12225 case elfcpp::R_MIPS_JALR:
12226 case elfcpp::R_MICROMIPS_JALR:
12227 // This relocation is only a hint. In some cases, we optimize
12228 // it into a bal instruction. But we don't try to optimize
12229 // when the symbol does not resolve locally.
12230 if (gsym == NULL
12231 || symbol_calls_local(gsym, gsym->has_dynsym_index()))
12232 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
12233 r_addend, extract_addend,
12234 cross_mode_jump, r_types[i],
12235 target->jalr_to_bal(),
12236 target->jr_to_b(),
152c92b2
VR
12237 this->calculate_only_,
12238 &this->calculated_value_);
47a9f4fc 12239 break;
9810d34d 12240
47a9f4fc
VR
12241 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12242 case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
12243 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
12244 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12245 elfcpp::DTP_OFFSET, r_addend,
152c92b2
VR
12246 extract_addend,
12247 this->calculate_only_,
12248 &this->calculated_value_);
47a9f4fc
VR
12249 break;
12250 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12251 case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
12252 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
12253 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12254 elfcpp::DTP_OFFSET, r_addend,
152c92b2
VR
12255 extract_addend,
12256 this->calculate_only_,
12257 &this->calculated_value_);
47a9f4fc
VR
12258 break;
12259 case elfcpp::R_MIPS_TLS_DTPREL32:
12260 case elfcpp::R_MIPS_TLS_DTPREL64:
12261 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12262 elfcpp::DTP_OFFSET, r_addend,
152c92b2
VR
12263 extract_addend,
12264 this->calculate_only_,
12265 &this->calculated_value_);
47a9f4fc
VR
12266 break;
12267 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12268 case elfcpp::R_MIPS16_TLS_TPREL_HI16:
12269 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12270 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
12271 elfcpp::TP_OFFSET, r_addend,
152c92b2
VR
12272 extract_addend,
12273 this->calculate_only_,
12274 &this->calculated_value_);
47a9f4fc
VR
12275 break;
12276 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12277 case elfcpp::R_MIPS16_TLS_TPREL_LO16:
12278 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12279 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
12280 elfcpp::TP_OFFSET, r_addend,
152c92b2
VR
12281 extract_addend,
12282 this->calculate_only_,
12283 &this->calculated_value_);
47a9f4fc
VR
12284 break;
12285 case elfcpp::R_MIPS_TLS_TPREL32:
12286 case elfcpp::R_MIPS_TLS_TPREL64:
12287 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
12288 elfcpp::TP_OFFSET, r_addend,
152c92b2
VR
12289 extract_addend,
12290 this->calculate_only_,
12291 &this->calculated_value_);
47a9f4fc
VR
12292 break;
12293 case elfcpp::R_MIPS_SUB:
12294 case elfcpp::R_MICROMIPS_SUB:
12295 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend,
12296 extract_addend,
152c92b2
VR
12297 this->calculate_only_,
12298 &this->calculated_value_);
47a9f4fc 12299 break;
e242ece1
VR
12300 case elfcpp::R_MIPS_HIGHER:
12301 case elfcpp::R_MICROMIPS_HIGHER:
12302 reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend,
152c92b2
VR
12303 extract_addend,
12304 this->calculate_only_,
12305 &this->calculated_value_);
e242ece1
VR
12306 break;
12307 case elfcpp::R_MIPS_HIGHEST:
12308 case elfcpp::R_MICROMIPS_HIGHEST:
12309 reloc_status = Reloc_funcs::relhighest(view, object, psymval,
12310 r_addend, extract_addend,
152c92b2
VR
12311 this->calculate_only_,
12312 &this->calculated_value_);
e242ece1 12313 break;
47a9f4fc
VR
12314 default:
12315 gold_error_at_location(relinfo, relnum, r_offset,
12316 _("unsupported reloc %u"), r_types[i]);
12317 break;
12318 }
12319
12320 if (update_got_entry)
12321 {
12322 Mips_output_data_got<size, big_endian>* got = target->got_section();
12323 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup())
12324 got->update_got_entry(got->get_primary_got_offset(mips_sym),
12325 psymval->value(object, 0));
12326 else
12327 got->update_got_entry(got_offset, psymval->value(object, 0));
12328 }
9810d34d
SS
12329 }
12330
1e1247c8 12331 bool jal_shuffle = jal_reloc(r_type);
47a9f4fc
VR
12332 Reloc_funcs::mips_reloc_shuffle(view, r_type, jal_shuffle);
12333
9810d34d
SS
12334 // Report any errors.
12335 switch (reloc_status)
12336 {
12337 case Reloc_funcs::STATUS_OKAY:
12338 break;
12339 case Reloc_funcs::STATUS_OVERFLOW:
c3847462
VR
12340 if (gsym == NULL)
12341 gold_error_at_location(relinfo, relnum, r_offset,
12342 _("relocation overflow: "
12343 "%u against local symbol %u in %s"),
12344 r_type, r_sym, object->name().c_str());
12345 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
12346 gold_error_at_location(relinfo, relnum, r_offset,
12347 _("relocation overflow: "
12348 "%u against '%s' defined in %s"),
12349 r_type, gsym->demangled_name().c_str(),
12350 gsym->object()->name().c_str());
12351 else
12352 gold_error_at_location(relinfo, relnum, r_offset,
12353 _("relocation overflow: %u against '%s'"),
12354 r_type, gsym->demangled_name().c_str());
9810d34d
SS
12355 break;
12356 case Reloc_funcs::STATUS_BAD_RELOC:
12357 gold_error_at_location(relinfo, relnum, r_offset,
12358 _("unexpected opcode while processing relocation"));
12359 break;
f5b11759
VR
12360 case Reloc_funcs::STATUS_PCREL_UNALIGNED:
12361 gold_error_at_location(relinfo, relnum, r_offset,
12362 _("unaligned PC-relative relocation"));
12363 break;
9810d34d
SS
12364 default:
12365 gold_unreachable();
12366 }
12367
12368 return true;
12369}
12370
9810d34d
SS
12371// Get the Reference_flags for a particular relocation.
12372
12373template<int size, bool big_endian>
12374int
12375Target_mips<size, big_endian>::Scan::get_reference_flags(
12376 unsigned int r_type)
12377{
12378 switch (r_type)
12379 {
12380 case elfcpp::R_MIPS_NONE:
12381 // No symbol reference.
12382 return 0;
12383
12384 case elfcpp::R_MIPS_16:
12385 case elfcpp::R_MIPS_32:
12386 case elfcpp::R_MIPS_64:
12387 case elfcpp::R_MIPS_HI16:
12388 case elfcpp::R_MIPS_LO16:
e242ece1
VR
12389 case elfcpp::R_MIPS_HIGHER:
12390 case elfcpp::R_MIPS_HIGHEST:
9810d34d
SS
12391 case elfcpp::R_MIPS16_HI16:
12392 case elfcpp::R_MIPS16_LO16:
12393 case elfcpp::R_MICROMIPS_HI16:
12394 case elfcpp::R_MICROMIPS_LO16:
e242ece1
VR
12395 case elfcpp::R_MICROMIPS_HIGHER:
12396 case elfcpp::R_MICROMIPS_HIGHEST:
9810d34d
SS
12397 return Symbol::ABSOLUTE_REF;
12398
12399 case elfcpp::R_MIPS_26:
12400 case elfcpp::R_MIPS16_26:
12401 case elfcpp::R_MICROMIPS_26_S1:
12402 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
12403
f5b11759
VR
12404 case elfcpp::R_MIPS_PC18_S3:
12405 case elfcpp::R_MIPS_PC19_S2:
12406 case elfcpp::R_MIPS_PCHI16:
12407 case elfcpp::R_MIPS_PCLO16:
9810d34d
SS
12408 case elfcpp::R_MIPS_GPREL32:
12409 case elfcpp::R_MIPS_GPREL16:
12410 case elfcpp::R_MIPS_REL32:
12411 case elfcpp::R_MIPS16_GPREL:
12412 return Symbol::RELATIVE_REF;
12413
12414 case elfcpp::R_MIPS_PC16:
12415 case elfcpp::R_MIPS_PC32:
f5b11759
VR
12416 case elfcpp::R_MIPS_PC21_S2:
12417 case elfcpp::R_MIPS_PC26_S2:
9810d34d
SS
12418 case elfcpp::R_MIPS_JALR:
12419 case elfcpp::R_MICROMIPS_JALR:
12420 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
12421
12422 case elfcpp::R_MIPS_GOT16:
12423 case elfcpp::R_MIPS_CALL16:
12424 case elfcpp::R_MIPS_GOT_DISP:
12425 case elfcpp::R_MIPS_GOT_HI16:
12426 case elfcpp::R_MIPS_GOT_LO16:
12427 case elfcpp::R_MIPS_CALL_HI16:
12428 case elfcpp::R_MIPS_CALL_LO16:
12429 case elfcpp::R_MIPS_LITERAL:
12430 case elfcpp::R_MIPS_GOT_PAGE:
12431 case elfcpp::R_MIPS_GOT_OFST:
12432 case elfcpp::R_MIPS16_GOT16:
12433 case elfcpp::R_MIPS16_CALL16:
12434 case elfcpp::R_MICROMIPS_GOT16:
12435 case elfcpp::R_MICROMIPS_CALL16:
12436 case elfcpp::R_MICROMIPS_GOT_HI16:
12437 case elfcpp::R_MICROMIPS_GOT_LO16:
12438 case elfcpp::R_MICROMIPS_CALL_HI16:
12439 case elfcpp::R_MICROMIPS_CALL_LO16:
47a9f4fc 12440 case elfcpp::R_MIPS_EH:
9810d34d
SS
12441 // Absolute in GOT.
12442 return Symbol::RELATIVE_REF;
12443
12444 case elfcpp::R_MIPS_TLS_DTPMOD32:
12445 case elfcpp::R_MIPS_TLS_DTPREL32:
12446 case elfcpp::R_MIPS_TLS_DTPMOD64:
12447 case elfcpp::R_MIPS_TLS_DTPREL64:
12448 case elfcpp::R_MIPS_TLS_GD:
12449 case elfcpp::R_MIPS_TLS_LDM:
12450 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
12451 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
12452 case elfcpp::R_MIPS_TLS_GOTTPREL:
12453 case elfcpp::R_MIPS_TLS_TPREL32:
12454 case elfcpp::R_MIPS_TLS_TPREL64:
12455 case elfcpp::R_MIPS_TLS_TPREL_HI16:
12456 case elfcpp::R_MIPS_TLS_TPREL_LO16:
12457 case elfcpp::R_MIPS16_TLS_GD:
12458 case elfcpp::R_MIPS16_TLS_GOTTPREL:
12459 case elfcpp::R_MICROMIPS_TLS_GD:
12460 case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
12461 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
12462 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
12463 return Symbol::TLS_REF;
12464
12465 case elfcpp::R_MIPS_COPY:
12466 case elfcpp::R_MIPS_JUMP_SLOT:
12467 default:
9810d34d
SS
12468 // Not expected. We will give an error later.
12469 return 0;
12470 }
12471}
12472
12473// Report an unsupported relocation against a local symbol.
12474
12475template<int size, bool big_endian>
12476void
12477Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
12478 Sized_relobj_file<size, big_endian>* object,
12479 unsigned int r_type)
12480{
12481 gold_error(_("%s: unsupported reloc %u against local symbol"),
12482 object->name().c_str(), r_type);
12483}
12484
12485// Report an unsupported relocation against a global symbol.
12486
12487template<int size, bool big_endian>
12488void
12489Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
12490 Sized_relobj_file<size, big_endian>* object,
12491 unsigned int r_type,
12492 Symbol* gsym)
12493{
12494 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
12495 object->name().c_str(), r_type, gsym->demangled_name().c_str());
12496}
12497
12498// Return printable name for ABI.
12499template<int size, bool big_endian>
12500const char*
01b84e25 12501Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags)
9810d34d
SS
12502{
12503 switch (e_flags & elfcpp::EF_MIPS_ABI)
12504 {
12505 case 0:
12506 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
12507 return "N32";
01b84e25 12508 else if (size == 64)
9810d34d
SS
12509 return "64";
12510 else
12511 return "none";
12512 case elfcpp::E_MIPS_ABI_O32:
12513 return "O32";
12514 case elfcpp::E_MIPS_ABI_O64:
12515 return "O64";
12516 case elfcpp::E_MIPS_ABI_EABI32:
12517 return "EABI32";
12518 case elfcpp::E_MIPS_ABI_EABI64:
12519 return "EABI64";
12520 default:
12521 return "unknown abi";
12522 }
12523}
12524
12525template<int size, bool big_endian>
12526const char*
12527Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
12528{
12529 switch (e_flags & elfcpp::EF_MIPS_MACH)
12530 {
12531 case elfcpp::E_MIPS_MACH_3900:
12532 return "mips:3900";
12533 case elfcpp::E_MIPS_MACH_4010:
12534 return "mips:4010";
12535 case elfcpp::E_MIPS_MACH_4100:
12536 return "mips:4100";
12537 case elfcpp::E_MIPS_MACH_4111:
12538 return "mips:4111";
12539 case elfcpp::E_MIPS_MACH_4120:
12540 return "mips:4120";
12541 case elfcpp::E_MIPS_MACH_4650:
12542 return "mips:4650";
12543 case elfcpp::E_MIPS_MACH_5400:
12544 return "mips:5400";
12545 case elfcpp::E_MIPS_MACH_5500:
12546 return "mips:5500";
b52717c0
VR
12547 case elfcpp::E_MIPS_MACH_5900:
12548 return "mips:5900";
9810d34d
SS
12549 case elfcpp::E_MIPS_MACH_SB1:
12550 return "mips:sb1";
12551 case elfcpp::E_MIPS_MACH_9000:
12552 return "mips:9000";
12553 case elfcpp::E_MIPS_MACH_LS2E:
b52717c0 12554 return "mips:loongson_2e";
9810d34d 12555 case elfcpp::E_MIPS_MACH_LS2F:
b52717c0 12556 return "mips:loongson_2f";
9810d34d 12557 case elfcpp::E_MIPS_MACH_LS3A:
b52717c0 12558 return "mips:loongson_3a";
9810d34d
SS
12559 case elfcpp::E_MIPS_MACH_OCTEON:
12560 return "mips:octeon";
12561 case elfcpp::E_MIPS_MACH_OCTEON2:
12562 return "mips:octeon2";
b52717c0
VR
12563 case elfcpp::E_MIPS_MACH_OCTEON3:
12564 return "mips:octeon3";
9810d34d
SS
12565 case elfcpp::E_MIPS_MACH_XLR:
12566 return "mips:xlr";
12567 default:
12568 switch (e_flags & elfcpp::EF_MIPS_ARCH)
12569 {
12570 default:
12571 case elfcpp::E_MIPS_ARCH_1:
12572 return "mips:3000";
12573
12574 case elfcpp::E_MIPS_ARCH_2:
12575 return "mips:6000";
12576
12577 case elfcpp::E_MIPS_ARCH_3:
12578 return "mips:4000";
12579
12580 case elfcpp::E_MIPS_ARCH_4:
12581 return "mips:8000";
12582
12583 case elfcpp::E_MIPS_ARCH_5:
12584 return "mips:mips5";
12585
12586 case elfcpp::E_MIPS_ARCH_32:
12587 return "mips:isa32";
12588
12589 case elfcpp::E_MIPS_ARCH_64:
12590 return "mips:isa64";
12591
12592 case elfcpp::E_MIPS_ARCH_32R2:
12593 return "mips:isa32r2";
12594
f5b11759
VR
12595 case elfcpp::E_MIPS_ARCH_32R6:
12596 return "mips:isa32r6";
12597
9810d34d
SS
12598 case elfcpp::E_MIPS_ARCH_64R2:
12599 return "mips:isa64r2";
f5b11759
VR
12600
12601 case elfcpp::E_MIPS_ARCH_64R6:
12602 return "mips:isa64r6";
9810d34d
SS
12603 }
12604 }
12605 return "unknown CPU";
12606}
12607
12608template<int size, bool big_endian>
62661c93 12609const Target::Target_info Target_mips<size, big_endian>::mips_info =
9810d34d
SS
12610{
12611 size, // size
12612 big_endian, // is_big_endian
12613 elfcpp::EM_MIPS, // machine_code
12614 true, // has_make_symbol
12615 false, // has_resolve
12616 false, // has_code_fill
12617 true, // is_default_stack_executable
12618 false, // can_icf_inline_merge_sections
12619 '\0', // wrap_char
47a9f4fc 12620 size == 32 ? "/lib/ld.so.1" : "/lib64/ld.so.1", // dynamic_linker
9810d34d
SS
12621 0x400000, // default_text_segment_address
12622 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
12623 4 * 1024, // common_pagesize (overridable by -z common-page-size)
12624 false, // isolate_execinstr
12625 0, // rosegment_gap
12626 elfcpp::SHN_UNDEF, // small_common_shndx
12627 elfcpp::SHN_UNDEF, // large_common_shndx
12628 0, // small_common_section_flags
12629 0, // large_common_section_flags
12630 NULL, // attributes_section
12631 NULL, // attributes_vendor
8d9743bd
MK
12632 "__start", // entry_symbol_name
12633 32, // hash_entry_size
9810d34d
SS
12634};
12635
62661c93
SS
12636template<int size, bool big_endian>
12637class Target_mips_nacl : public Target_mips<size, big_endian>
12638{
12639 public:
12640 Target_mips_nacl()
12641 : Target_mips<size, big_endian>(&mips_nacl_info)
12642 { }
12643
12644 private:
12645 static const Target::Target_info mips_nacl_info;
12646};
12647
12648template<int size, bool big_endian>
12649const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info =
12650{
12651 size, // size
12652 big_endian, // is_big_endian
12653 elfcpp::EM_MIPS, // machine_code
12654 true, // has_make_symbol
12655 false, // has_resolve
12656 false, // has_code_fill
12657 true, // is_default_stack_executable
12658 false, // can_icf_inline_merge_sections
12659 '\0', // wrap_char
12660 "/lib/ld.so.1", // dynamic_linker
12661 0x20000, // default_text_segment_address
12662 0x10000, // abi_pagesize (overridable by -z max-page-size)
12663 0x10000, // common_pagesize (overridable by -z common-page-size)
12664 true, // isolate_execinstr
12665 0x10000000, // rosegment_gap
12666 elfcpp::SHN_UNDEF, // small_common_shndx
12667 elfcpp::SHN_UNDEF, // large_common_shndx
12668 0, // small_common_section_flags
12669 0, // large_common_section_flags
12670 NULL, // attributes_section
12671 NULL, // attributes_vendor
8d9743bd
MK
12672 "_start", // entry_symbol_name
12673 32, // hash_entry_size
62661c93
SS
12674};
12675
12676// Target selector for Mips. Note this is never instantiated directly.
12677// It's only used in Target_selector_mips_nacl, below.
9810d34d
SS
12678
12679template<int size, bool big_endian>
12680class Target_selector_mips : public Target_selector
12681{
12682public:
12683 Target_selector_mips()
12684 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
12685 (size == 64 ?
12686 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
12687 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
12688 (size == 64 ?
47a9f4fc
VR
12689 (big_endian ? "elf64btsmip" : "elf64ltsmip") :
12690 (big_endian ? "elf32btsmip" : "elf32ltsmip")))
9810d34d
SS
12691 { }
12692
12693 Target* do_instantiate_target()
12694 { return new Target_mips<size, big_endian>(); }
12695};
12696
62661c93
SS
12697template<int size, bool big_endian>
12698class Target_selector_mips_nacl
12699 : public Target_selector_nacl<Target_selector_mips<size, big_endian>,
12700 Target_mips_nacl<size, big_endian> >
12701{
12702 public:
12703 Target_selector_mips_nacl()
12704 : Target_selector_nacl<Target_selector_mips<size, big_endian>,
12705 Target_mips_nacl<size, big_endian> >(
12706 // NaCl currently supports only MIPS32 little-endian.
12707 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl")
12708 { }
12709};
9810d34d 12710
62661c93
SS
12711Target_selector_mips_nacl<32, true> target_selector_mips32;
12712Target_selector_mips_nacl<32, false> target_selector_mips32el;
12713Target_selector_mips_nacl<64, true> target_selector_mips64;
12714Target_selector_mips_nacl<64, false> target_selector_mips64el;
9810d34d
SS
12715
12716} // End anonymous namespace.
This page took 0.765003 seconds and 4 git commands to generate.