ld x86_64 tests: Accept x86-64-v3 as a needed ISA
[binutils-gdb.git] / bfd / elfxx-mips.c
blob92dd4c20a7dd9c58980fc3e9768e1791bb967b49
1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2023 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11 This file is part of BFD, the Binary File Descriptor library.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
29 /* This file handles functionality common to the different MIPS ABI's. */
31 #include "sysdep.h"
32 #include "bfd.h"
33 #include "libbfd.h"
34 #include "libiberty.h"
35 #include "elf-bfd.h"
36 #include "ecoff-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
40 #include "dwarf2.h"
42 /* Get the ECOFF swapping routines. */
43 #include "coff/sym.h"
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
48 #include "hashtab.h"
50 /* Types of TLS GOT entry. */
51 enum mips_got_tls_type {
52 GOT_TLS_NONE,
53 GOT_TLS_GD,
54 GOT_TLS_LDM,
55 GOT_TLS_IE
58 /* This structure is used to hold information about one GOT entry.
59 There are four types of entry:
61 (1) an absolute address
62 requires: abfd == NULL
63 fields: d.address
65 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67 fields: abfd, symndx, d.addend, tls_type
69 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70 requires: abfd != NULL, symndx == -1
71 fields: d.h, tls_type
73 (4) a TLS LDM slot
74 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75 fields: none; there's only one of these per GOT. */
76 struct mips_got_entry
78 /* One input bfd that needs the GOT entry. */
79 bfd *abfd;
80 /* The index of the symbol, as stored in the relocation r_info, if
81 we have a local symbol; -1 otherwise. */
82 long symndx;
83 union
85 /* If abfd == NULL, an address that must be stored in the got. */
86 bfd_vma address;
87 /* If abfd != NULL && symndx != -1, the addend of the relocation
88 that should be added to the symbol value. */
89 bfd_vma addend;
90 /* If abfd != NULL && symndx == -1, the hash table entry
91 corresponding to a symbol in the GOT. The symbol's entry
92 is in the local area if h->global_got_area is GGA_NONE,
93 otherwise it is in the global area. */
94 struct mips_elf_link_hash_entry *h;
95 } d;
97 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
98 symbol entry with r_symndx == 0. */
99 unsigned char tls_type;
101 /* True if we have filled in the GOT contents for a TLS entry,
102 and created the associated relocations. */
103 unsigned char tls_initialized;
105 /* The offset from the beginning of the .got section to the entry
106 corresponding to this symbol+addend. If it's a global symbol
107 whose offset is yet to be decided, it's going to be -1. */
108 long gotidx;
111 /* This structure represents a GOT page reference from an input bfd.
112 Each instance represents a symbol + ADDEND, where the representation
113 of the symbol depends on whether it is local to the input bfd.
114 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
117 Page references with SYMNDX >= 0 always become page references
118 in the output. Page references with SYMNDX < 0 only become page
119 references if the symbol binds locally; in other cases, the page
120 reference decays to a global GOT reference. */
121 struct mips_got_page_ref
123 long symndx;
124 union
126 struct mips_elf_link_hash_entry *h;
127 bfd *abfd;
128 } u;
129 bfd_vma addend;
132 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133 The structures form a non-overlapping list that is sorted by increasing
134 MIN_ADDEND. */
135 struct mips_got_page_range
137 struct mips_got_page_range *next;
138 bfd_signed_vma min_addend;
139 bfd_signed_vma max_addend;
142 /* This structure describes the range of addends that are applied to page
143 relocations against a given section. */
144 struct mips_got_page_entry
146 /* The section that these entries are based on. */
147 asection *sec;
148 /* The ranges for this page entry. */
149 struct mips_got_page_range *ranges;
150 /* The maximum number of page entries needed for RANGES. */
151 bfd_vma num_pages;
154 /* This structure is used to hold .got information when linking. */
156 struct mips_got_info
158 /* The number of global .got entries. */
159 unsigned int global_gotno;
160 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
161 unsigned int reloc_only_gotno;
162 /* The number of .got slots used for TLS. */
163 unsigned int tls_gotno;
164 /* The first unused TLS .got entry. Used only during
165 mips_elf_initialize_tls_index. */
166 unsigned int tls_assigned_gotno;
167 /* The number of local .got entries, eventually including page entries. */
168 unsigned int local_gotno;
169 /* The maximum number of page entries needed. */
170 unsigned int page_gotno;
171 /* The number of relocations needed for the GOT entries. */
172 unsigned int relocs;
173 /* The first unused local .got entry. */
174 unsigned int assigned_low_gotno;
175 /* The last unused local .got entry. */
176 unsigned int assigned_high_gotno;
177 /* A hash table holding members of the got. */
178 struct htab *got_entries;
179 /* A hash table holding mips_got_page_ref structures. */
180 struct htab *got_page_refs;
181 /* A hash table of mips_got_page_entry structures. */
182 struct htab *got_page_entries;
183 /* In multi-got links, a pointer to the next got (err, rather, most
184 of the time, it points to the previous got). */
185 struct mips_got_info *next;
188 /* Structure passed when merging bfds' gots. */
190 struct mips_elf_got_per_bfd_arg
192 /* The output bfd. */
193 bfd *obfd;
194 /* The link information. */
195 struct bfd_link_info *info;
196 /* A pointer to the primary got, i.e., the one that's going to get
197 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198 DT_MIPS_GOTSYM. */
199 struct mips_got_info *primary;
200 /* A non-primary got we're trying to merge with other input bfd's
201 gots. */
202 struct mips_got_info *current;
203 /* The maximum number of got entries that can be addressed with a
204 16-bit offset. */
205 unsigned int max_count;
206 /* The maximum number of page entries needed by each got. */
207 unsigned int max_pages;
208 /* The total number of global entries which will live in the
209 primary got and be automatically relocated. This includes
210 those not referenced by the primary GOT but included in
211 the "master" GOT. */
212 unsigned int global_count;
215 /* A structure used to pass information to htab_traverse callbacks
216 when laying out the GOT. */
218 struct mips_elf_traverse_got_arg
220 struct bfd_link_info *info;
221 struct mips_got_info *g;
222 int value;
225 struct _mips_elf_section_data
227 struct bfd_elf_section_data elf;
228 union
230 bfd_byte *tdata;
231 } u;
234 #define mips_elf_section_data(sec) \
235 ((struct _mips_elf_section_data *) elf_section_data (sec))
237 #define is_mips_elf(bfd) \
238 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
239 && elf_tdata (bfd) != NULL \
240 && elf_object_id (bfd) == MIPS_ELF_DATA)
242 /* The ABI says that every symbol used by dynamic relocations must have
243 a global GOT entry. Among other things, this provides the dynamic
244 linker with a free, directly-indexed cache. The GOT can therefore
245 contain symbols that are not referenced by GOT relocations themselves
246 (in other words, it may have symbols that are not referenced by things
247 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
249 GOT relocations are less likely to overflow if we put the associated
250 GOT entries towards the beginning. We therefore divide the global
251 GOT entries into two areas: "normal" and "reloc-only". Entries in
252 the first area can be used for both dynamic relocations and GP-relative
253 accesses, while those in the "reloc-only" area are for dynamic
254 relocations only.
256 These GGA_* ("Global GOT Area") values are organised so that lower
257 values are more general than higher values. Also, non-GGA_NONE
258 values are ordered by the position of the area in the GOT. */
259 #define GGA_NORMAL 0
260 #define GGA_RELOC_ONLY 1
261 #define GGA_NONE 2
263 /* Information about a non-PIC interface to a PIC function. There are
264 two ways of creating these interfaces. The first is to add:
266 lui $25,%hi(func)
267 addiu $25,$25,%lo(func)
269 immediately before a PIC function "func". The second is to add:
271 lui $25,%hi(func)
272 j func
273 addiu $25,$25,%lo(func)
275 to a separate trampoline section.
277 Stubs of the first kind go in a new section immediately before the
278 target function. Stubs of the second kind go in a single section
279 pointed to by the hash table's "strampoline" field. */
280 struct mips_elf_la25_stub {
281 /* The generated section that contains this stub. */
282 asection *stub_section;
284 /* The offset of the stub from the start of STUB_SECTION. */
285 bfd_vma offset;
287 /* One symbol for the original function. Its location is available
288 in H->root.root.u.def. */
289 struct mips_elf_link_hash_entry *h;
292 /* Macros for populating a mips_elf_la25_stub. */
294 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
295 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296 #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
298 #define LA25_LUI_MICROMIPS(VAL) \
299 (0x41b90000 | (VAL)) /* lui t9,VAL */
300 #define LA25_J_MICROMIPS(VAL) \
301 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
302 #define LA25_ADDIU_MICROMIPS(VAL) \
303 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
305 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
306 the dynamic symbols. */
308 struct mips_elf_hash_sort_data
310 /* The symbol in the global GOT with the lowest dynamic symbol table
311 index. */
312 struct elf_link_hash_entry *low;
313 /* The least dynamic symbol table index corresponding to a non-TLS
314 symbol with a GOT entry. */
315 bfd_size_type min_got_dynindx;
316 /* The greatest dynamic symbol table index corresponding to a symbol
317 with a GOT entry that is not referenced (e.g., a dynamic symbol
318 with dynamic relocations pointing to it from non-primary GOTs). */
319 bfd_size_type max_unref_got_dynindx;
320 /* The greatest dynamic symbol table index corresponding to a local
321 symbol. */
322 bfd_size_type max_local_dynindx;
323 /* The greatest dynamic symbol table index corresponding to an external
324 symbol without a GOT entry. */
325 bfd_size_type max_non_got_dynindx;
326 /* If non-NULL, output BFD for .MIPS.xhash finalization. */
327 bfd *output_bfd;
328 /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329 real final dynindx. */
330 bfd_byte *mipsxhash;
333 /* We make up to two PLT entries if needed, one for standard MIPS code
334 and one for compressed code, either a MIPS16 or microMIPS one. We
335 keep a separate record of traditional lazy-binding stubs, for easier
336 processing. */
338 struct plt_entry
340 /* Traditional SVR4 stub offset, or -1 if none. */
341 bfd_vma stub_offset;
343 /* Standard PLT entry offset, or -1 if none. */
344 bfd_vma mips_offset;
346 /* Compressed PLT entry offset, or -1 if none. */
347 bfd_vma comp_offset;
349 /* The corresponding .got.plt index, or -1 if none. */
350 bfd_vma gotplt_index;
352 /* Whether we need a standard PLT entry. */
353 unsigned int need_mips : 1;
355 /* Whether we need a compressed PLT entry. */
356 unsigned int need_comp : 1;
359 /* The MIPS ELF linker needs additional information for each symbol in
360 the global hash table. */
362 struct mips_elf_link_hash_entry
364 struct elf_link_hash_entry root;
366 /* External symbol information. */
367 EXTR esym;
369 /* The la25 stub we have created for ths symbol, if any. */
370 struct mips_elf_la25_stub *la25_stub;
372 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373 this symbol. */
374 unsigned int possibly_dynamic_relocs;
376 /* If there is a stub that 32 bit functions should use to call this
377 16 bit function, this points to the section containing the stub. */
378 asection *fn_stub;
380 /* If there is a stub that 16 bit functions should use to call this
381 32 bit function, this points to the section containing the stub. */
382 asection *call_stub;
384 /* This is like the call_stub field, but it is used if the function
385 being called returns a floating point value. */
386 asection *call_fp_stub;
388 /* If non-zero, location in .MIPS.xhash to write real final dynindx. */
389 bfd_vma mipsxhash_loc;
391 /* The highest GGA_* value that satisfies all references to this symbol. */
392 unsigned int global_got_area : 2;
394 /* True if all GOT relocations against this symbol are for calls. This is
395 a looser condition than no_fn_stub below, because there may be other
396 non-call non-GOT relocations against the symbol. */
397 unsigned int got_only_for_calls : 1;
399 /* True if one of the relocations described by possibly_dynamic_relocs
400 is against a readonly section. */
401 unsigned int readonly_reloc : 1;
403 /* True if there is a relocation against this symbol that must be
404 resolved by the static linker (in other words, if the relocation
405 cannot possibly be made dynamic). */
406 unsigned int has_static_relocs : 1;
408 /* True if we must not create a .MIPS.stubs entry for this symbol.
409 This is set, for example, if there are relocations related to
410 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
412 unsigned int no_fn_stub : 1;
414 /* Whether we need the fn_stub; this is true if this symbol appears
415 in any relocs other than a 16 bit call. */
416 unsigned int need_fn_stub : 1;
418 /* True if this symbol is referenced by branch relocations from
419 any non-PIC input file. This is used to determine whether an
420 la25 stub is required. */
421 unsigned int has_nonpic_branches : 1;
423 /* Does this symbol need a traditional MIPS lazy-binding stub
424 (as opposed to a PLT entry)? */
425 unsigned int needs_lazy_stub : 1;
427 /* Does this symbol resolve to a PLT entry? */
428 unsigned int use_plt_entry : 1;
431 /* MIPS ELF linker hash table. */
433 struct mips_elf_link_hash_table
435 struct elf_link_hash_table root;
437 /* The number of .rtproc entries. */
438 bfd_size_type procedure_count;
440 /* The size of the .compact_rel section (if SGI_COMPAT). */
441 bfd_size_type compact_rel_size;
443 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
445 bool use_rld_obj_head;
447 /* The __rld_map or __rld_obj_head symbol. */
448 struct elf_link_hash_entry *rld_symbol;
450 /* This is set if we see any mips16 stub sections. */
451 bool mips16_stubs_seen;
453 /* True if we can generate copy relocs and PLTs. */
454 bool use_plts_and_copy_relocs;
456 /* True if we can only use 32-bit microMIPS instructions. */
457 bool insn32;
459 /* True if we suppress checks for invalid branches between ISA modes. */
460 bool ignore_branch_isa;
462 /* True if we are targetting R6 compact branches. */
463 bool compact_branches;
465 /* True if we already reported the small-data section overflow. */
466 bool small_data_overflow_reported;
468 /* True if we use the special `__gnu_absolute_zero' symbol. */
469 bool use_absolute_zero;
471 /* True if we have been configured for a GNU target. */
472 bool gnu_target;
474 /* Shortcuts to some dynamic sections, or NULL if they are not
475 being used. */
476 asection *srelplt2;
477 asection *sstubs;
479 /* The master GOT information. */
480 struct mips_got_info *got_info;
482 /* The global symbol in the GOT with the lowest index in the dynamic
483 symbol table. */
484 struct elf_link_hash_entry *global_gotsym;
486 /* The size of the PLT header in bytes. */
487 bfd_vma plt_header_size;
489 /* The size of a standard PLT entry in bytes. */
490 bfd_vma plt_mips_entry_size;
492 /* The size of a compressed PLT entry in bytes. */
493 bfd_vma plt_comp_entry_size;
495 /* The offset of the next standard PLT entry to create. */
496 bfd_vma plt_mips_offset;
498 /* The offset of the next compressed PLT entry to create. */
499 bfd_vma plt_comp_offset;
501 /* The index of the next .got.plt entry to create. */
502 bfd_vma plt_got_index;
504 /* The number of functions that need a lazy-binding stub. */
505 bfd_vma lazy_stub_count;
507 /* The size of a function stub entry in bytes. */
508 bfd_vma function_stub_size;
510 /* The number of reserved entries at the beginning of the GOT. */
511 unsigned int reserved_gotno;
513 /* The section used for mips_elf_la25_stub trampolines.
514 See the comment above that structure for details. */
515 asection *strampoline;
517 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518 pairs. */
519 htab_t la25_stubs;
521 /* A function FN (NAME, IS, OS) that creates a new input section
522 called NAME and links it to output section OS. If IS is nonnull,
523 the new section should go immediately before it, otherwise it
524 should go at the (current) beginning of OS.
526 The function returns the new section on success, otherwise it
527 returns null. */
528 asection *(*add_stub_section) (const char *, asection *, asection *);
530 /* Is the PLT header compressed? */
531 unsigned int plt_header_is_comp : 1;
534 /* Get the MIPS ELF linker hash table from a link_info structure. */
536 #define mips_elf_hash_table(p) \
537 ((is_elf_hash_table ((p)->hash) \
538 && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA) \
539 ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
541 /* A structure used to communicate with htab_traverse callbacks. */
542 struct mips_htab_traverse_info
544 /* The usual link-wide information. */
545 struct bfd_link_info *info;
546 bfd *output_bfd;
548 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
549 bool error;
552 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
554 that contains the relocation field and DATA points to the start of
555 INPUT_SECTION. */
557 struct mips_hi16
559 struct mips_hi16 *next;
560 bfd_byte *data;
561 asection *input_section;
562 arelent rel;
565 /* MIPS ELF private object data. */
567 struct mips_elf_obj_tdata
569 /* Generic ELF private object data. */
570 struct elf_obj_tdata root;
572 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
573 bfd *abi_fp_bfd;
575 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
576 bfd *abi_msa_bfd;
578 /* The abiflags for this object. */
579 Elf_Internal_ABIFlags_v0 abiflags;
580 bool abiflags_valid;
582 /* The GOT requirements of input bfds. */
583 struct mips_got_info *got;
585 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
586 included directly in this one, but there's no point to wasting
587 the memory just for the infrequently called find_nearest_line. */
588 struct mips_elf_find_line *find_line_info;
590 /* An array of stub sections indexed by symbol number. */
591 asection **local_stubs;
592 asection **local_call_stubs;
594 /* The Irix 5 support uses two virtual sections, which represent
595 text/data symbols defined in dynamic objects. */
596 asymbol *elf_data_symbol;
597 asymbol *elf_text_symbol;
598 asection *elf_data_section;
599 asection *elf_text_section;
601 struct mips_hi16 *mips_hi16_list;
604 /* Get MIPS ELF private object data from BFD's tdata. */
606 #define mips_elf_tdata(bfd) \
607 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
609 #define TLS_RELOC_P(r_type) \
610 (r_type == R_MIPS_TLS_DTPMOD32 \
611 || r_type == R_MIPS_TLS_DTPMOD64 \
612 || r_type == R_MIPS_TLS_DTPREL32 \
613 || r_type == R_MIPS_TLS_DTPREL64 \
614 || r_type == R_MIPS_TLS_GD \
615 || r_type == R_MIPS_TLS_LDM \
616 || r_type == R_MIPS_TLS_DTPREL_HI16 \
617 || r_type == R_MIPS_TLS_DTPREL_LO16 \
618 || r_type == R_MIPS_TLS_GOTTPREL \
619 || r_type == R_MIPS_TLS_TPREL32 \
620 || r_type == R_MIPS_TLS_TPREL64 \
621 || r_type == R_MIPS_TLS_TPREL_HI16 \
622 || r_type == R_MIPS_TLS_TPREL_LO16 \
623 || r_type == R_MIPS16_TLS_GD \
624 || r_type == R_MIPS16_TLS_LDM \
625 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
626 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
627 || r_type == R_MIPS16_TLS_GOTTPREL \
628 || r_type == R_MIPS16_TLS_TPREL_HI16 \
629 || r_type == R_MIPS16_TLS_TPREL_LO16 \
630 || r_type == R_MICROMIPS_TLS_GD \
631 || r_type == R_MICROMIPS_TLS_LDM \
632 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
633 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
634 || r_type == R_MICROMIPS_TLS_GOTTPREL \
635 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
636 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
638 /* Structure used to pass information to mips_elf_output_extsym. */
640 struct extsym_info
642 bfd *abfd;
643 struct bfd_link_info *info;
644 struct ecoff_debug_info *debug;
645 const struct ecoff_debug_swap *swap;
646 bool failed;
649 /* The names of the runtime procedure table symbols used on IRIX5. */
651 static const char * const mips_elf_dynsym_rtproc_names[] =
653 "_procedure_table",
654 "_procedure_string_table",
655 "_procedure_table_size",
656 NULL
659 /* These structures are used to generate the .compact_rel section on
660 IRIX5. */
662 typedef struct
664 unsigned long id1; /* Always one? */
665 unsigned long num; /* Number of compact relocation entries. */
666 unsigned long id2; /* Always two? */
667 unsigned long offset; /* The file offset of the first relocation. */
668 unsigned long reserved0; /* Zero? */
669 unsigned long reserved1; /* Zero? */
670 } Elf32_compact_rel;
672 typedef struct
674 bfd_byte id1[4];
675 bfd_byte num[4];
676 bfd_byte id2[4];
677 bfd_byte offset[4];
678 bfd_byte reserved0[4];
679 bfd_byte reserved1[4];
680 } Elf32_External_compact_rel;
682 typedef struct
684 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
685 unsigned int rtype : 4; /* Relocation types. See below. */
686 unsigned int dist2to : 8;
687 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688 unsigned long konst; /* KONST field. See below. */
689 unsigned long vaddr; /* VADDR to be relocated. */
690 } Elf32_crinfo;
692 typedef struct
694 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
695 unsigned int rtype : 4; /* Relocation types. See below. */
696 unsigned int dist2to : 8;
697 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698 unsigned long konst; /* KONST field. See below. */
699 } Elf32_crinfo2;
701 typedef struct
703 bfd_byte info[4];
704 bfd_byte konst[4];
705 bfd_byte vaddr[4];
706 } Elf32_External_crinfo;
708 typedef struct
710 bfd_byte info[4];
711 bfd_byte konst[4];
712 } Elf32_External_crinfo2;
714 /* These are the constants used to swap the bitfields in a crinfo. */
716 #define CRINFO_CTYPE (0x1U)
717 #define CRINFO_CTYPE_SH (31)
718 #define CRINFO_RTYPE (0xfU)
719 #define CRINFO_RTYPE_SH (27)
720 #define CRINFO_DIST2TO (0xffU)
721 #define CRINFO_DIST2TO_SH (19)
722 #define CRINFO_RELVADDR (0x7ffffU)
723 #define CRINFO_RELVADDR_SH (0)
725 /* A compact relocation info has long (3 words) or short (2 words)
726 formats. A short format doesn't have VADDR field and relvaddr
727 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
728 #define CRF_MIPS_LONG 1
729 #define CRF_MIPS_SHORT 0
731 /* There are 4 types of compact relocation at least. The value KONST
732 has different meaning for each type:
734 (type) (konst)
735 CT_MIPS_REL32 Address in data
736 CT_MIPS_WORD Address in word (XXX)
737 CT_MIPS_GPHI_LO GP - vaddr
738 CT_MIPS_JMPAD Address to jump
741 #define CRT_MIPS_REL32 0xa
742 #define CRT_MIPS_WORD 0xb
743 #define CRT_MIPS_GPHI_LO 0xc
744 #define CRT_MIPS_JMPAD 0xd
746 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
747 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
748 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
749 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
751 /* The structure of the runtime procedure descriptor created by the
752 loader for use by the static exception system. */
754 typedef struct runtime_pdr {
755 bfd_vma adr; /* Memory address of start of procedure. */
756 long regmask; /* Save register mask. */
757 long regoffset; /* Save register offset. */
758 long fregmask; /* Save floating point register mask. */
759 long fregoffset; /* Save floating point register offset. */
760 long frameoffset; /* Frame size. */
761 short framereg; /* Frame pointer register. */
762 short pcreg; /* Offset or reg of return pc. */
763 long irpss; /* Index into the runtime string table. */
764 long reserved;
765 struct exception_info *exception_info;/* Pointer to exception array. */
766 } RPDR, *pRPDR;
767 #define cbRPDR sizeof (RPDR)
768 #define rpdNil ((pRPDR) 0)
770 static struct mips_got_entry *mips_elf_create_local_got_entry
771 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772 struct mips_elf_link_hash_entry *, int);
773 static bool mips_elf_sort_hash_table_f
774 (struct mips_elf_link_hash_entry *, void *);
775 static bfd_vma mips_elf_high
776 (bfd_vma);
777 static bool mips_elf_create_dynamic_relocation
778 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780 bfd_vma *, asection *);
781 static bfd_vma mips_elf_adjust_gp
782 (bfd *, struct mips_got_info *, bfd *);
784 /* This will be used when we sort the dynamic relocation records. */
785 static bfd *reldyn_sorting_bfd;
787 /* True if ABFD is for CPUs with load interlocking that include
788 non-MIPS1 CPUs and R3900. */
789 #define LOAD_INTERLOCKS_P(abfd) \
790 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
791 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
793 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794 This should be safe for all architectures. We enable this predicate
795 for RM9000 for now. */
796 #define JAL_TO_BAL_P(abfd) \
797 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
799 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800 This should be safe for all architectures. We enable this predicate for
801 all CPUs. */
802 #define JALR_TO_BAL_P(abfd) 1
804 /* True if ABFD is for CPUs that are faster if JR is converted to B.
805 This should be safe for all architectures. We enable this predicate for
806 all CPUs. */
807 #define JR_TO_B_P(abfd) 1
809 /* True if ABFD is a PIC object. */
810 #define PIC_OBJECT_P(abfd) \
811 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
813 /* Nonzero if ABFD is using the O32 ABI. */
814 #define ABI_O32_P(abfd) \
815 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
817 /* Nonzero if ABFD is using the N32 ABI. */
818 #define ABI_N32_P(abfd) \
819 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
821 /* Nonzero if ABFD is using the N64 ABI. */
822 #define ABI_64_P(abfd) \
823 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
825 /* Nonzero if ABFD is using NewABI conventions. */
826 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
828 /* Nonzero if ABFD has microMIPS code. */
829 #define MICROMIPS_P(abfd) \
830 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
832 /* Nonzero if ABFD is MIPS R6. */
833 #define MIPSR6_P(abfd) \
834 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
835 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
837 /* The IRIX compatibility level we are striving for. */
838 #define IRIX_COMPAT(abfd) \
839 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
841 /* Whether we are trying to be compatible with IRIX at all. */
842 #define SGI_COMPAT(abfd) \
843 (IRIX_COMPAT (abfd) != ict_none)
845 /* The name of the options section. */
846 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
849 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
851 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
854 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
855 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856 (strcmp (NAME, ".MIPS.abiflags") == 0)
858 /* Whether the section is readonly. */
859 #define MIPS_ELF_READONLY_SECTION(sec) \
860 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
861 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
863 /* The name of the stub section. */
864 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
866 /* The size of an external REL relocation. */
867 #define MIPS_ELF_REL_SIZE(abfd) \
868 (get_elf_backend_data (abfd)->s->sizeof_rel)
870 /* The size of an external RELA relocation. */
871 #define MIPS_ELF_RELA_SIZE(abfd) \
872 (get_elf_backend_data (abfd)->s->sizeof_rela)
874 /* The size of an external dynamic table entry. */
875 #define MIPS_ELF_DYN_SIZE(abfd) \
876 (get_elf_backend_data (abfd)->s->sizeof_dyn)
878 /* The size of a GOT entry. */
879 #define MIPS_ELF_GOT_SIZE(abfd) \
880 (get_elf_backend_data (abfd)->s->arch_size / 8)
882 /* The size of the .rld_map section. */
883 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884 (get_elf_backend_data (abfd)->s->arch_size / 8)
886 /* The size of a symbol-table entry. */
887 #define MIPS_ELF_SYM_SIZE(abfd) \
888 (get_elf_backend_data (abfd)->s->sizeof_sym)
890 /* The default alignment for sections, as a power of two. */
891 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
892 (get_elf_backend_data (abfd)->s->log_file_align)
894 /* Get word-sized data. */
895 #define MIPS_ELF_GET_WORD(abfd, ptr) \
896 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
898 /* Put out word-sized data. */
899 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
900 (ABI_64_P (abfd) \
901 ? bfd_put_64 (abfd, val, ptr) \
902 : bfd_put_32 (abfd, val, ptr))
904 /* The opcode for word-sized loads (LW or LD). */
905 #define MIPS_ELF_LOAD_WORD(abfd) \
906 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
908 /* Add a dynamic symbol table-entry. */
909 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
910 _bfd_elf_add_dynamic_entry (info, tag, val)
912 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
913 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
915 /* The name of the dynamic relocation section. */
916 #define MIPS_ELF_REL_DYN_NAME(INFO) \
917 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918 ? ".rela.dyn" : ".rel.dyn")
920 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921 from smaller values. Start with zero, widen, *then* decrement. */
922 #define MINUS_ONE (((bfd_vma)0) - 1)
923 #define MINUS_TWO (((bfd_vma)0) - 2)
925 /* The value to write into got[1] for SVR4 targets, to identify it is
926 a GNU object. The dynamic linker can then use got[1] to store the
927 module pointer. */
928 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
931 /* The offset of $gp from the beginning of the .got section. */
932 #define ELF_MIPS_GP_OFFSET(INFO) \
933 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934 ? 0x0 : 0x7ff0)
936 /* The maximum size of the GOT for it to be addressable using 16-bit
937 offsets from $gp. */
938 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
940 /* Instructions which appear in a stub. */
941 #define STUB_LW(abfd) \
942 ((ABI_64_P (abfd) \
943 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
944 : 0x8f998010)) /* lw t9,0x8010(gp) */
945 #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
946 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
947 #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
948 #define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
949 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
950 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
951 #define STUB_LI16S(abfd, VAL) \
952 ((ABI_64_P (abfd) \
953 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
954 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
956 /* Likewise for the microMIPS ASE. */
957 #define STUB_LW_MICROMIPS(abfd) \
958 (ABI_64_P (abfd) \
959 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
960 : 0xff3c8010) /* lw t9,0x8010(gp) */
961 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
962 #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
963 #define STUB_LUI_MICROMIPS(VAL) \
964 (0x41b80000 + (VAL)) /* lui t8,VAL */
965 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
966 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
967 #define STUB_ORI_MICROMIPS(VAL) \
968 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
969 #define STUB_LI16U_MICROMIPS(VAL) \
970 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
971 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
972 (ABI_64_P (abfd) \
973 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
974 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
976 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
978 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
983 /* The name of the dynamic interpreter. This is put in the .interp
984 section. */
986 #define ELF_DYNAMIC_INTERPRETER(abfd) \
987 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
988 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
989 : "/usr/lib/libc.so.1")
991 #ifdef BFD64
992 #define MNAME(bfd,pre,pos) \
993 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994 #define ELF_R_SYM(bfd, i) \
995 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996 #define ELF_R_TYPE(bfd, i) \
997 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998 #define ELF_R_INFO(bfd, s, t) \
999 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000 #else
1001 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002 #define ELF_R_SYM(bfd, i) \
1003 (ELF32_R_SYM (i))
1004 #define ELF_R_TYPE(bfd, i) \
1005 (ELF32_R_TYPE (i))
1006 #define ELF_R_INFO(bfd, s, t) \
1007 (ELF32_R_INFO (s, t))
1008 #endif
1010 /* The mips16 compiler uses a couple of special sections to handle
1011 floating point arguments.
1013 Section names that look like .mips16.fn.FNNAME contain stubs that
1014 copy floating point arguments from the fp regs to the gp regs and
1015 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1016 call should be redirected to the stub instead. If no 32 bit
1017 function calls FNNAME, the stub should be discarded. We need to
1018 consider any reference to the function, not just a call, because
1019 if the address of the function is taken we will need the stub,
1020 since the address might be passed to a 32 bit function.
1022 Section names that look like .mips16.call.FNNAME contain stubs
1023 that copy floating point arguments from the gp regs to the fp
1024 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1025 then any 16 bit function that calls FNNAME should be redirected
1026 to the stub instead. If FNNAME is not a 32 bit function, the
1027 stub should be discarded.
1029 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030 which call FNNAME and then copy the return value from the fp regs
1031 to the gp regs. These stubs store the return value in $18 while
1032 calling FNNAME; any function which might call one of these stubs
1033 must arrange to save $18 around the call. (This case is not
1034 needed for 32 bit functions that call 16 bit functions, because
1035 16 bit functions always return floating point values in both
1036 $f0/$f1 and $2/$3.)
1038 Note that in all cases FNNAME might be defined statically.
1039 Therefore, FNNAME is not used literally. Instead, the relocation
1040 information will indicate which symbol the section is for.
1042 We record any stubs that we find in the symbol table. */
1044 #define FN_STUB ".mips16.fn."
1045 #define CALL_STUB ".mips16.call."
1046 #define CALL_FP_STUB ".mips16.call.fp."
1048 #define FN_STUB_P(name) startswith (name, FN_STUB)
1049 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1052 /* The format of the first PLT entry in an O32 executable. */
1053 static const bfd_vma mips_o32_exec_plt0_entry[] =
1055 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1056 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1057 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1058 0x031cc023, /* subu $24, $24, $28 */
1059 0x03e07825, /* or t7, ra, zero */
1060 0x0018c082, /* srl $24, $24, 2 */
1061 0x0320f809, /* jalr $25 */
1062 0x2718fffe /* subu $24, $24, 2 */
1065 /* The format of the first PLT entry in an O32 executable using compact
1066 jumps. */
1067 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1069 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1070 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1071 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1072 0x031cc023, /* subu $24, $24, $28 */
1073 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1074 0x0018c082, /* srl $24, $24, 2 */
1075 0x2718fffe, /* subu $24, $24, 2 */
1076 0xf8190000 /* jalrc $25 */
1079 /* The format of the first PLT entry in an N32 executable. Different
1080 because gp ($28) is not available; we use t2 ($14) instead. */
1081 static const bfd_vma mips_n32_exec_plt0_entry[] =
1083 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1084 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1085 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1086 0x030ec023, /* subu $24, $24, $14 */
1087 0x03e07825, /* or t7, ra, zero */
1088 0x0018c082, /* srl $24, $24, 2 */
1089 0x0320f809, /* jalr $25 */
1090 0x2718fffe /* subu $24, $24, 2 */
1093 /* The format of the first PLT entry in an N32 executable using compact
1094 jumps. Different because gp ($28) is not available; we use t2 ($14)
1095 instead. */
1096 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1098 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1099 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1100 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1101 0x030ec023, /* subu $24, $24, $14 */
1102 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1103 0x0018c082, /* srl $24, $24, 2 */
1104 0x2718fffe, /* subu $24, $24, 2 */
1105 0xf8190000 /* jalrc $25 */
1108 /* The format of the first PLT entry in an N64 executable. Different
1109 from N32 because of the increased size of GOT entries. */
1110 static const bfd_vma mips_n64_exec_plt0_entry[] =
1112 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1113 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1114 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1115 0x030ec023, /* subu $24, $24, $14 */
1116 0x03e07825, /* or t7, ra, zero */
1117 0x0018c0c2, /* srl $24, $24, 3 */
1118 0x0320f809, /* jalr $25 */
1119 0x2718fffe /* subu $24, $24, 2 */
1122 /* The format of the first PLT entry in an N64 executable using compact
1123 jumps. Different from N32 because of the increased size of GOT
1124 entries. */
1125 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1127 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1128 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1129 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1130 0x030ec023, /* subu $24, $24, $14 */
1131 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1132 0x0018c0c2, /* srl $24, $24, 3 */
1133 0x2718fffe, /* subu $24, $24, 2 */
1134 0xf8190000 /* jalrc $25 */
1138 /* The format of the microMIPS first PLT entry in an O32 executable.
1139 We rely on v0 ($2) rather than t8 ($24) to contain the address
1140 of the GOTPLT entry handled, so this stub may only be used when
1141 all the subsequent PLT entries are microMIPS code too.
1143 The trailing NOP is for alignment and correct disassembly only. */
1144 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1146 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1147 0xff23, 0x0000, /* lw $25, 0($3) */
1148 0x0535, /* subu $2, $2, $3 */
1149 0x2525, /* srl $2, $2, 2 */
1150 0x3302, 0xfffe, /* subu $24, $2, 2 */
1151 0x0dff, /* move $15, $31 */
1152 0x45f9, /* jalrs $25 */
1153 0x0f83, /* move $28, $3 */
1154 0x0c00 /* nop */
1157 /* The format of the microMIPS first PLT entry in an O32 executable
1158 in the insn32 mode. */
1159 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1161 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1162 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1163 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1164 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1165 0x001f, 0x7a90, /* or $15, $31, zero */
1166 0x0318, 0x1040, /* srl $24, $24, 2 */
1167 0x03f9, 0x0f3c, /* jalr $25 */
1168 0x3318, 0xfffe /* subu $24, $24, 2 */
1171 /* The format of subsequent standard PLT entries. */
1172 static const bfd_vma mips_exec_plt_entry[] =
1174 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1175 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1176 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1177 0x03200008 /* jr $25 */
1180 static const bfd_vma mipsr6_exec_plt_entry[] =
1182 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1183 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1184 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1185 0x03200009 /* jr $25 */
1188 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1190 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1191 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1192 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1193 0xd8190000 /* jic $25, 0 */
1196 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1197 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198 directly addressable. */
1199 static const bfd_vma mips16_o32_exec_plt_entry[] =
1201 0xb203, /* lw $2, 12($pc) */
1202 0x9a60, /* lw $3, 0($2) */
1203 0x651a, /* move $24, $2 */
1204 0xeb00, /* jr $3 */
1205 0x653b, /* move $25, $3 */
1206 0x6500, /* nop */
1207 0x0000, 0x0000 /* .word (.got.plt entry) */
1210 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1211 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1212 static const bfd_vma micromips_o32_exec_plt_entry[] =
1214 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215 0xff22, 0x0000, /* lw $25, 0($2) */
1216 0x4599, /* jr $25 */
1217 0x0f02 /* move $24, $2 */
1220 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1221 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1223 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1224 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225 0x0019, 0x0f3c, /* jr $25 */
1226 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1229 /* The format of the first PLT entry in a VxWorks executable. */
1230 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1232 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1233 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234 0x8f390008, /* lw t9, 8(t9) */
1235 0x00000000, /* nop */
1236 0x03200008, /* jr t9 */
1237 0x00000000 /* nop */
1240 /* The format of subsequent PLT entries. */
1241 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1243 0x10000000, /* b .PLT_resolver */
1244 0x24180000, /* li t8, <pltindex> */
1245 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1246 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247 0x8f390000, /* lw t9, 0(t9) */
1248 0x00000000, /* nop */
1249 0x03200008, /* jr t9 */
1250 0x00000000 /* nop */
1253 /* The format of the first PLT entry in a VxWorks shared object. */
1254 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1256 0x8f990008, /* lw t9, 8(gp) */
1257 0x00000000, /* nop */
1258 0x03200008, /* jr t9 */
1259 0x00000000, /* nop */
1260 0x00000000, /* nop */
1261 0x00000000 /* nop */
1264 /* The format of subsequent PLT entries. */
1265 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1267 0x10000000, /* b .PLT_resolver */
1268 0x24180000 /* li t8, <pltindex> */
1271 /* microMIPS 32-bit opcode helper installer. */
1273 static void
1274 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1276 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1280 /* microMIPS 32-bit opcode helper retriever. */
1282 static bfd_vma
1283 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1285 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1288 /* Look up an entry in a MIPS ELF linker hash table. */
1290 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1291 ((struct mips_elf_link_hash_entry *) \
1292 elf_link_hash_lookup (&(table)->root, (string), (create), \
1293 (copy), (follow)))
1295 /* Traverse a MIPS ELF linker hash table. */
1297 #define mips_elf_link_hash_traverse(table, func, info) \
1298 (elf_link_hash_traverse \
1299 (&(table)->root, \
1300 (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
1301 (info)))
1303 /* Find the base offsets for thread-local storage in this object,
1304 for GD/LD and IE/LE respectively. */
1306 #define TP_OFFSET 0x7000
1307 #define DTP_OFFSET 0x8000
1309 static bfd_vma
1310 dtprel_base (struct bfd_link_info *info)
1312 /* If tls_sec is NULL, we should have signalled an error already. */
1313 if (elf_hash_table (info)->tls_sec == NULL)
1314 return 0;
1315 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1318 static bfd_vma
1319 tprel_base (struct bfd_link_info *info)
1321 /* If tls_sec is NULL, we should have signalled an error already. */
1322 if (elf_hash_table (info)->tls_sec == NULL)
1323 return 0;
1324 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1327 /* Create an entry in a MIPS ELF linker hash table. */
1329 static struct bfd_hash_entry *
1330 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331 struct bfd_hash_table *table, const char *string)
1333 struct mips_elf_link_hash_entry *ret =
1334 (struct mips_elf_link_hash_entry *) entry;
1336 /* Allocate the structure if it has not already been allocated by a
1337 subclass. */
1338 if (ret == NULL)
1339 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340 if (ret == NULL)
1341 return (struct bfd_hash_entry *) ret;
1343 /* Call the allocation method of the superclass. */
1344 ret = ((struct mips_elf_link_hash_entry *)
1345 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346 table, string));
1347 if (ret != NULL)
1349 /* Set local fields. */
1350 memset (&ret->esym, 0, sizeof (EXTR));
1351 /* We use -2 as a marker to indicate that the information has
1352 not been set. -1 means there is no associated ifd. */
1353 ret->esym.ifd = -2;
1354 ret->la25_stub = 0;
1355 ret->possibly_dynamic_relocs = 0;
1356 ret->fn_stub = NULL;
1357 ret->call_stub = NULL;
1358 ret->call_fp_stub = NULL;
1359 ret->mipsxhash_loc = 0;
1360 ret->global_got_area = GGA_NONE;
1361 ret->got_only_for_calls = true;
1362 ret->readonly_reloc = false;
1363 ret->has_static_relocs = false;
1364 ret->no_fn_stub = false;
1365 ret->need_fn_stub = false;
1366 ret->has_nonpic_branches = false;
1367 ret->needs_lazy_stub = false;
1368 ret->use_plt_entry = false;
1371 return (struct bfd_hash_entry *) ret;
1374 /* Allocate MIPS ELF private object data. */
1376 bool
1377 _bfd_mips_elf_mkobject (bfd *abfd)
1379 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380 MIPS_ELF_DATA);
1383 /* MIPS ELF uses a special find_nearest_line routine in order the
1384 handle the ECOFF debugging information. */
1386 struct mips_elf_find_line
1388 struct ecoff_debug_info d;
1389 struct ecoff_find_line i;
1392 bool
1393 _bfd_mips_elf_free_cached_info (bfd *abfd)
1395 struct mips_elf_obj_tdata *tdata;
1397 if ((bfd_get_format (abfd) == bfd_object
1398 || bfd_get_format (abfd) == bfd_core)
1399 && (tdata = mips_elf_tdata (abfd)) != NULL)
1401 BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402 while (tdata->mips_hi16_list != NULL)
1404 struct mips_hi16 *hi = tdata->mips_hi16_list;
1405 tdata->mips_hi16_list = hi->next;
1406 free (hi);
1408 if (tdata->find_line_info != NULL)
1409 _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1411 return _bfd_elf_free_cached_info (abfd);
1414 bool
1415 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1417 if (!sec->used_by_bfd)
1419 struct _mips_elf_section_data *sdata;
1420 size_t amt = sizeof (*sdata);
1422 sdata = bfd_zalloc (abfd, amt);
1423 if (sdata == NULL)
1424 return false;
1425 sec->used_by_bfd = sdata;
1428 return _bfd_elf_new_section_hook (abfd, sec);
1431 /* Read ECOFF debugging information from a .mdebug section into a
1432 ecoff_debug_info structure. */
1434 bool
1435 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436 struct ecoff_debug_info *debug)
1438 HDRR *symhdr;
1439 const struct ecoff_debug_swap *swap;
1440 char *ext_hdr;
1442 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443 memset (debug, 0, sizeof (*debug));
1445 ext_hdr = bfd_malloc (swap->external_hdr_size);
1446 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447 goto error_return;
1449 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450 swap->external_hdr_size))
1451 goto error_return;
1453 symhdr = &debug->symbolic_header;
1454 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455 free (ext_hdr);
1456 ext_hdr = NULL;
1458 /* The symbolic header contains absolute file offsets and sizes to
1459 read. */
1460 #define READ(ptr, offset, count, size) \
1461 do \
1463 size_t amt; \
1464 debug->ptr = NULL; \
1465 if (symhdr->count == 0) \
1466 break; \
1467 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1469 bfd_set_error (bfd_error_file_too_big); \
1470 goto error_return; \
1472 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
1473 goto error_return; \
1474 debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt); \
1475 if (debug->ptr == NULL) \
1476 goto error_return; \
1477 ((char *) debug->ptr)[amt] = 0; \
1478 } while (0)
1480 READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486 READ (ss, cbSsOffset, issMax, sizeof (char));
1487 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491 #undef READ
1493 return true;
1495 error_return:
1496 free (ext_hdr);
1497 _bfd_ecoff_free_ecoff_debug_info (debug);
1498 return false;
1501 /* Swap RPDR (runtime procedure table entry) for output. */
1503 static void
1504 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1506 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1513 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1516 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1519 /* Create a runtime procedure table from the .mdebug section. */
1521 static bool
1522 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523 struct bfd_link_info *info, asection *s,
1524 struct ecoff_debug_info *debug)
1526 const struct ecoff_debug_swap *swap;
1527 HDRR *hdr = &debug->symbolic_header;
1528 RPDR *rpdr, *rp;
1529 struct rpdr_ext *erp;
1530 void *rtproc;
1531 struct pdr_ext *epdr;
1532 struct sym_ext *esym;
1533 char *ss, **sv;
1534 char *str;
1535 bfd_size_type size;
1536 bfd_size_type count;
1537 unsigned long sindex;
1538 unsigned long i;
1539 PDR pdr;
1540 SYMR sym;
1541 const char *no_name_func = _("static procedure (no name)");
1543 epdr = NULL;
1544 rpdr = NULL;
1545 esym = NULL;
1546 ss = NULL;
1547 sv = NULL;
1549 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1551 sindex = strlen (no_name_func) + 1;
1552 count = hdr->ipdMax;
1553 if (count > 0)
1555 size = swap->external_pdr_size;
1557 epdr = bfd_malloc (size * count);
1558 if (epdr == NULL)
1559 goto error_return;
1561 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562 goto error_return;
1564 size = sizeof (RPDR);
1565 rp = rpdr = bfd_malloc (size * count);
1566 if (rpdr == NULL)
1567 goto error_return;
1569 size = sizeof (char *);
1570 sv = bfd_malloc (size * count);
1571 if (sv == NULL)
1572 goto error_return;
1574 count = hdr->isymMax;
1575 size = swap->external_sym_size;
1576 esym = bfd_malloc (size * count);
1577 if (esym == NULL)
1578 goto error_return;
1580 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581 goto error_return;
1583 count = hdr->issMax;
1584 ss = bfd_malloc (count);
1585 if (ss == NULL)
1586 goto error_return;
1587 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588 goto error_return;
1590 count = hdr->ipdMax;
1591 for (i = 0; i < (unsigned long) count; i++, rp++)
1593 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595 rp->adr = sym.value;
1596 rp->regmask = pdr.regmask;
1597 rp->regoffset = pdr.regoffset;
1598 rp->fregmask = pdr.fregmask;
1599 rp->fregoffset = pdr.fregoffset;
1600 rp->frameoffset = pdr.frameoffset;
1601 rp->framereg = pdr.framereg;
1602 rp->pcreg = pdr.pcreg;
1603 rp->irpss = sindex;
1604 sv[i] = ss + sym.iss;
1605 sindex += strlen (sv[i]) + 1;
1609 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610 size = BFD_ALIGN (size, 16);
1611 rtproc = bfd_alloc (abfd, size);
1612 if (rtproc == NULL)
1614 mips_elf_hash_table (info)->procedure_count = 0;
1615 goto error_return;
1618 mips_elf_hash_table (info)->procedure_count = count + 2;
1620 erp = rtproc;
1621 memset (erp, 0, sizeof (struct rpdr_ext));
1622 erp++;
1623 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624 strcpy (str, no_name_func);
1625 str += strlen (no_name_func) + 1;
1626 for (i = 0; i < count; i++)
1628 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629 strcpy (str, sv[i]);
1630 str += strlen (sv[i]) + 1;
1632 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1634 /* Set the size and contents of .rtproc section. */
1635 s->size = size;
1636 s->contents = rtproc;
1638 /* Skip this section later on (I don't think this currently
1639 matters, but someday it might). */
1640 s->map_head.link_order = NULL;
1642 free (epdr);
1643 free (rpdr);
1644 free (esym);
1645 free (ss);
1646 free (sv);
1647 return true;
1649 error_return:
1650 free (epdr);
1651 free (rpdr);
1652 free (esym);
1653 free (ss);
1654 free (sv);
1655 return false;
1658 /* We're going to create a stub for H. Create a symbol for the stub's
1659 value and size, to help make the disassembly easier to read. */
1661 static bool
1662 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663 struct mips_elf_link_hash_entry *h,
1664 const char *prefix, asection *s, bfd_vma value,
1665 bfd_vma size)
1667 bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668 struct bfd_link_hash_entry *bh;
1669 struct elf_link_hash_entry *elfh;
1670 char *name;
1671 bool res;
1673 if (micromips_p)
1674 value |= 1;
1676 /* Create a new symbol. */
1677 name = concat (prefix, h->root.root.root.string, NULL);
1678 bh = NULL;
1679 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680 BSF_LOCAL, s, value, NULL,
1681 true, false, &bh);
1682 free (name);
1683 if (! res)
1684 return false;
1686 /* Make it a local function. */
1687 elfh = (struct elf_link_hash_entry *) bh;
1688 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689 elfh->size = size;
1690 elfh->forced_local = 1;
1691 if (micromips_p)
1692 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693 return true;
1696 /* We're about to redefine H. Create a symbol to represent H's
1697 current value and size, to help make the disassembly easier
1698 to read. */
1700 static bool
1701 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702 struct mips_elf_link_hash_entry *h,
1703 const char *prefix)
1705 struct bfd_link_hash_entry *bh;
1706 struct elf_link_hash_entry *elfh;
1707 char *name;
1708 asection *s;
1709 bfd_vma value;
1710 bool res;
1712 /* Read the symbol's value. */
1713 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714 || h->root.root.type == bfd_link_hash_defweak);
1715 s = h->root.root.u.def.section;
1716 value = h->root.root.u.def.value;
1718 /* Create a new symbol. */
1719 name = concat (prefix, h->root.root.root.string, NULL);
1720 bh = NULL;
1721 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722 BSF_LOCAL, s, value, NULL,
1723 true, false, &bh);
1724 free (name);
1725 if (! res)
1726 return false;
1728 /* Make it local and copy the other attributes from H. */
1729 elfh = (struct elf_link_hash_entry *) bh;
1730 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731 elfh->other = h->root.other;
1732 elfh->size = h->root.size;
1733 elfh->forced_local = 1;
1734 return true;
1737 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738 function rather than to a hard-float stub. */
1740 static bool
1741 section_allows_mips16_refs_p (asection *section)
1743 const char *name;
1745 name = bfd_section_name (section);
1746 return (FN_STUB_P (name)
1747 || CALL_STUB_P (name)
1748 || CALL_FP_STUB_P (name)
1749 || strcmp (name, ".pdr") == 0);
1752 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753 stub section of some kind. Return the R_SYMNDX of the target
1754 function, or 0 if we can't decide which function that is. */
1756 static unsigned long
1757 mips16_stub_symndx (const struct elf_backend_data *bed,
1758 asection *sec ATTRIBUTE_UNUSED,
1759 const Elf_Internal_Rela *relocs,
1760 const Elf_Internal_Rela *relend)
1762 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763 const Elf_Internal_Rela *rel;
1765 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766 one in a compound relocation. */
1767 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769 return ELF_R_SYM (sec->owner, rel->r_info);
1771 /* Otherwise trust the first relocation, whatever its kind. This is
1772 the traditional behavior. */
1773 if (relocs < relend)
1774 return ELF_R_SYM (sec->owner, relocs->r_info);
1776 return 0;
1779 /* Check the mips16 stubs for a particular symbol, and see if we can
1780 discard them. */
1782 static void
1783 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784 struct mips_elf_link_hash_entry *h)
1786 /* Dynamic symbols must use the standard call interface, in case other
1787 objects try to call them. */
1788 if (h->fn_stub != NULL
1789 && h->root.dynindx != -1)
1791 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792 h->need_fn_stub = true;
1795 if (h->fn_stub != NULL
1796 && ! h->need_fn_stub)
1798 /* We don't need the fn_stub; the only references to this symbol
1799 are 16 bit calls. Clobber the size to 0 to prevent it from
1800 being included in the link. */
1801 h->fn_stub->size = 0;
1802 h->fn_stub->flags &= ~SEC_RELOC;
1803 h->fn_stub->reloc_count = 0;
1804 h->fn_stub->flags |= SEC_EXCLUDE;
1805 h->fn_stub->output_section = bfd_abs_section_ptr;
1808 if (h->call_stub != NULL
1809 && ELF_ST_IS_MIPS16 (h->root.other))
1811 /* We don't need the call_stub; this is a 16 bit function, so
1812 calls from other 16 bit functions are OK. Clobber the size
1813 to 0 to prevent it from being included in the link. */
1814 h->call_stub->size = 0;
1815 h->call_stub->flags &= ~SEC_RELOC;
1816 h->call_stub->reloc_count = 0;
1817 h->call_stub->flags |= SEC_EXCLUDE;
1818 h->call_stub->output_section = bfd_abs_section_ptr;
1821 if (h->call_fp_stub != NULL
1822 && ELF_ST_IS_MIPS16 (h->root.other))
1824 /* We don't need the call_stub; this is a 16 bit function, so
1825 calls from other 16 bit functions are OK. Clobber the size
1826 to 0 to prevent it from being included in the link. */
1827 h->call_fp_stub->size = 0;
1828 h->call_fp_stub->flags &= ~SEC_RELOC;
1829 h->call_fp_stub->reloc_count = 0;
1830 h->call_fp_stub->flags |= SEC_EXCLUDE;
1831 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1835 /* Hashtable callbacks for mips_elf_la25_stubs. */
1837 static hashval_t
1838 mips_elf_la25_stub_hash (const void *entry_)
1840 const struct mips_elf_la25_stub *entry;
1842 entry = (struct mips_elf_la25_stub *) entry_;
1843 return entry->h->root.root.u.def.section->id
1844 + entry->h->root.root.u.def.value;
1847 static int
1848 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1850 const struct mips_elf_la25_stub *entry1, *entry2;
1852 entry1 = (struct mips_elf_la25_stub *) entry1_;
1853 entry2 = (struct mips_elf_la25_stub *) entry2_;
1854 return ((entry1->h->root.root.u.def.section
1855 == entry2->h->root.root.u.def.section)
1856 && (entry1->h->root.root.u.def.value
1857 == entry2->h->root.root.u.def.value));
1860 /* Called by the linker to set up the la25 stub-creation code. FN is
1861 the linker's implementation of add_stub_function. Return true on
1862 success. */
1864 bool
1865 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866 asection *(*fn) (const char *, asection *,
1867 asection *))
1869 struct mips_elf_link_hash_table *htab;
1871 htab = mips_elf_hash_table (info);
1872 if (htab == NULL)
1873 return false;
1875 htab->add_stub_section = fn;
1876 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877 mips_elf_la25_stub_eq, NULL);
1878 if (htab->la25_stubs == NULL)
1879 return false;
1881 return true;
1884 /* Return true if H is a locally-defined PIC function, in the sense
1885 that it or its fn_stub might need $25 to be valid on entry.
1886 Note that MIPS16 functions set up $gp using PC-relative instructions,
1887 so they themselves never need $25 to be valid. Only non-MIPS16
1888 entry points are of interest here. */
1890 static bool
1891 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1893 return ((h->root.root.type == bfd_link_hash_defined
1894 || h->root.root.type == bfd_link_hash_defweak)
1895 && h->root.def_regular
1896 && !bfd_is_abs_section (h->root.root.u.def.section)
1897 && !bfd_is_und_section (h->root.root.u.def.section)
1898 && (!ELF_ST_IS_MIPS16 (h->root.other)
1899 || (h->fn_stub && h->need_fn_stub))
1900 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1904 /* Set *SEC to the input section that contains the target of STUB.
1905 Return the offset of the target from the start of that section. */
1907 static bfd_vma
1908 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909 asection **sec)
1911 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1913 BFD_ASSERT (stub->h->need_fn_stub);
1914 *sec = stub->h->fn_stub;
1915 return 0;
1917 else
1919 *sec = stub->h->root.root.u.def.section;
1920 return stub->h->root.root.u.def.value;
1924 /* STUB describes an la25 stub that we have decided to implement
1925 by inserting an LUI/ADDIU pair before the target function.
1926 Create the section and redirect the function symbol to it. */
1928 static bool
1929 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930 struct bfd_link_info *info)
1932 struct mips_elf_link_hash_table *htab;
1933 char *name;
1934 asection *s, *input_section;
1935 unsigned int align;
1937 htab = mips_elf_hash_table (info);
1938 if (htab == NULL)
1939 return false;
1941 /* Create a unique name for the new section. */
1942 name = bfd_malloc (11 + sizeof (".text.stub."));
1943 if (name == NULL)
1944 return false;
1945 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1947 /* Create the section. */
1948 mips_elf_get_la25_target (stub, &input_section);
1949 s = htab->add_stub_section (name, input_section,
1950 input_section->output_section);
1951 if (s == NULL)
1952 return false;
1954 /* Make sure that any padding goes before the stub. */
1955 align = input_section->alignment_power;
1956 if (!bfd_set_section_alignment (s, align))
1957 return false;
1958 if (align > 3)
1959 s->size = (1 << align) - 8;
1961 /* Create a symbol for the stub. */
1962 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963 stub->stub_section = s;
1964 stub->offset = s->size;
1966 /* Allocate room for it. */
1967 s->size += 8;
1968 return true;
1971 /* STUB describes an la25 stub that we have decided to implement
1972 with a separate trampoline. Allocate room for it and redirect
1973 the function symbol to it. */
1975 static bool
1976 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977 struct bfd_link_info *info)
1979 struct mips_elf_link_hash_table *htab;
1980 asection *s;
1982 htab = mips_elf_hash_table (info);
1983 if (htab == NULL)
1984 return false;
1986 /* Create a trampoline section, if we haven't already. */
1987 s = htab->strampoline;
1988 if (s == NULL)
1990 asection *input_section = stub->h->root.root.u.def.section;
1991 s = htab->add_stub_section (".text", NULL,
1992 input_section->output_section);
1993 if (s == NULL || !bfd_set_section_alignment (s, 4))
1994 return false;
1995 htab->strampoline = s;
1998 /* Create a symbol for the stub. */
1999 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000 stub->stub_section = s;
2001 stub->offset = s->size;
2003 /* Allocate room for it. */
2004 s->size += 16;
2005 return true;
2008 /* H describes a symbol that needs an la25 stub. Make sure that an
2009 appropriate stub exists and point H at it. */
2011 static bool
2012 mips_elf_add_la25_stub (struct bfd_link_info *info,
2013 struct mips_elf_link_hash_entry *h)
2015 struct mips_elf_link_hash_table *htab;
2016 struct mips_elf_la25_stub search, *stub;
2017 bool use_trampoline_p;
2018 asection *s;
2019 bfd_vma value;
2020 void **slot;
2022 /* Describe the stub we want. */
2023 search.stub_section = NULL;
2024 search.offset = 0;
2025 search.h = h;
2027 /* See if we've already created an equivalent stub. */
2028 htab = mips_elf_hash_table (info);
2029 if (htab == NULL)
2030 return false;
2032 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033 if (slot == NULL)
2034 return false;
2036 stub = (struct mips_elf_la25_stub *) *slot;
2037 if (stub != NULL)
2039 /* We can reuse the existing stub. */
2040 h->la25_stub = stub;
2041 return true;
2044 /* Create a permanent copy of ENTRY and add it to the hash table. */
2045 stub = bfd_malloc (sizeof (search));
2046 if (stub == NULL)
2047 return false;
2048 *stub = search;
2049 *slot = stub;
2051 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052 of the section and if we would need no more than 2 nops. */
2053 value = mips_elf_get_la25_target (stub, &s);
2054 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055 value &= ~1;
2056 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2058 h->la25_stub = stub;
2059 return (use_trampoline_p
2060 ? mips_elf_add_la25_trampoline (stub, info)
2061 : mips_elf_add_la25_intro (stub, info));
2064 /* A mips_elf_link_hash_traverse callback that is called before sizing
2065 sections. DATA points to a mips_htab_traverse_info structure. */
2067 static bool
2068 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2070 struct mips_htab_traverse_info *hti;
2072 hti = (struct mips_htab_traverse_info *) data;
2073 if (!bfd_link_relocatable (hti->info))
2074 mips_elf_check_mips16_stubs (hti->info, h);
2076 if (mips_elf_local_pic_function_p (h))
2078 /* PR 12845: If H is in a section that has been garbage
2079 collected it will have its output section set to *ABS*. */
2080 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081 return true;
2083 /* H is a function that might need $25 to be valid on entry.
2084 If we're creating a non-PIC relocatable object, mark H as
2085 being PIC. If we're creating a non-relocatable object with
2086 non-PIC branches and jumps to H, make sure that H has an la25
2087 stub. */
2088 if (bfd_link_relocatable (hti->info))
2090 if (!PIC_OBJECT_P (hti->output_bfd))
2091 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2093 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2095 hti->error = true;
2096 return false;
2099 return true;
2102 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103 Most mips16 instructions are 16 bits, but these instructions
2104 are 32 bits.
2106 The format of these instructions is:
2108 +--------------+--------------------------------+
2109 | JALX | X| Imm 20:16 | Imm 25:21 |
2110 +--------------+--------------------------------+
2111 | Immediate 15:0 |
2112 +-----------------------------------------------+
2114 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2115 Note that the immediate value in the first word is swapped.
2117 When producing a relocatable object file, R_MIPS16_26 is
2118 handled mostly like R_MIPS_26. In particular, the addend is
2119 stored as a straight 26-bit value in a 32-bit instruction.
2120 (gas makes life simpler for itself by never adjusting a
2121 R_MIPS16_26 reloc to be against a section, so the addend is
2122 always zero). However, the 32 bit instruction is stored as 2
2123 16-bit values, rather than a single 32-bit value. In a
2124 big-endian file, the result is the same; in a little-endian
2125 file, the two 16-bit halves of the 32 bit value are swapped.
2126 This is so that a disassembler can recognize the jal
2127 instruction.
2129 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130 instruction stored as two 16-bit values. The addend A is the
2131 contents of the targ26 field. The calculation is the same as
2132 R_MIPS_26. When storing the calculated value, reorder the
2133 immediate value as shown above, and don't forget to store the
2134 value as two 16-bit values.
2136 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137 defined as
2139 big-endian:
2140 +--------+----------------------+
2141 | | |
2142 | | targ26-16 |
2143 |31 26|25 0|
2144 +--------+----------------------+
2146 little-endian:
2147 +----------+------+-------------+
2148 | | | |
2149 | sub1 | | sub2 |
2150 |0 9|10 15|16 31|
2151 +----------+--------------------+
2152 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153 ((sub1 << 16) | sub2)).
2155 When producing a relocatable object file, the calculation is
2156 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157 When producing a fully linked file, the calculation is
2158 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2161 The table below lists the other MIPS16 instruction relocations.
2162 Each one is calculated in the same way as the non-MIPS16 relocation
2163 given on the right, but using the extended MIPS16 layout of 16-bit
2164 immediate fields:
2166 R_MIPS16_GPREL R_MIPS_GPREL16
2167 R_MIPS16_GOT16 R_MIPS_GOT16
2168 R_MIPS16_CALL16 R_MIPS_CALL16
2169 R_MIPS16_HI16 R_MIPS_HI16
2170 R_MIPS16_LO16 R_MIPS_LO16
2172 A typical instruction will have a format like this:
2174 +--------------+--------------------------------+
2175 | EXTEND | Imm 10:5 | Imm 15:11 |
2176 +--------------+--------------------------------+
2177 | Major | rx | ry | Imm 4:0 |
2178 +--------------+--------------------------------+
2180 EXTEND is the five bit value 11110. Major is the instruction
2181 opcode.
2183 All we need to do here is shuffle the bits appropriately.
2184 As above, the two 16-bit halves must be swapped on a
2185 little-endian system.
2187 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188 relocatable field is shifted by 1 rather than 2 and the same bit
2189 shuffling is done as with the relocations above. */
2191 static inline bool
2192 mips16_reloc_p (int r_type)
2194 switch (r_type)
2196 case R_MIPS16_26:
2197 case R_MIPS16_GPREL:
2198 case R_MIPS16_GOT16:
2199 case R_MIPS16_CALL16:
2200 case R_MIPS16_HI16:
2201 case R_MIPS16_LO16:
2202 case R_MIPS16_TLS_GD:
2203 case R_MIPS16_TLS_LDM:
2204 case R_MIPS16_TLS_DTPREL_HI16:
2205 case R_MIPS16_TLS_DTPREL_LO16:
2206 case R_MIPS16_TLS_GOTTPREL:
2207 case R_MIPS16_TLS_TPREL_HI16:
2208 case R_MIPS16_TLS_TPREL_LO16:
2209 case R_MIPS16_PC16_S1:
2210 return true;
2212 default:
2213 return false;
2217 /* Check if a microMIPS reloc. */
2219 static inline bool
2220 micromips_reloc_p (unsigned int r_type)
2222 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2225 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2227 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2229 static inline bool
2230 micromips_reloc_shuffle_p (unsigned int r_type)
2232 return (micromips_reloc_p (r_type)
2233 && r_type != R_MICROMIPS_PC7_S1
2234 && r_type != R_MICROMIPS_PC10_S1);
2237 static inline bool
2238 got16_reloc_p (int r_type)
2240 return (r_type == R_MIPS_GOT16
2241 || r_type == R_MIPS16_GOT16
2242 || r_type == R_MICROMIPS_GOT16);
2245 static inline bool
2246 call16_reloc_p (int r_type)
2248 return (r_type == R_MIPS_CALL16
2249 || r_type == R_MIPS16_CALL16
2250 || r_type == R_MICROMIPS_CALL16);
2253 static inline bool
2254 got_disp_reloc_p (unsigned int r_type)
2256 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2259 static inline bool
2260 got_page_reloc_p (unsigned int r_type)
2262 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2265 static inline bool
2266 got_lo16_reloc_p (unsigned int r_type)
2268 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2271 static inline bool
2272 call_hi16_reloc_p (unsigned int r_type)
2274 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2277 static inline bool
2278 call_lo16_reloc_p (unsigned int r_type)
2280 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2283 static inline bool
2284 hi16_reloc_p (int r_type)
2286 return (r_type == R_MIPS_HI16
2287 || r_type == R_MIPS16_HI16
2288 || r_type == R_MICROMIPS_HI16
2289 || r_type == R_MIPS_PCHI16);
2292 static inline bool
2293 lo16_reloc_p (int r_type)
2295 return (r_type == R_MIPS_LO16
2296 || r_type == R_MIPS16_LO16
2297 || r_type == R_MICROMIPS_LO16
2298 || r_type == R_MIPS_PCLO16);
2301 static inline bool
2302 mips16_call_reloc_p (int r_type)
2304 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2307 static inline bool
2308 jal_reloc_p (int r_type)
2310 return (r_type == R_MIPS_26
2311 || r_type == R_MIPS16_26
2312 || r_type == R_MICROMIPS_26_S1);
2315 static inline bool
2316 b_reloc_p (int r_type)
2318 return (r_type == R_MIPS_PC26_S2
2319 || r_type == R_MIPS_PC21_S2
2320 || r_type == R_MIPS_PC16
2321 || r_type == R_MIPS_GNU_REL16_S2
2322 || r_type == R_MIPS16_PC16_S1
2323 || r_type == R_MICROMIPS_PC16_S1
2324 || r_type == R_MICROMIPS_PC10_S1
2325 || r_type == R_MICROMIPS_PC7_S1);
2328 static inline bool
2329 aligned_pcrel_reloc_p (int r_type)
2331 return (r_type == R_MIPS_PC18_S3
2332 || r_type == R_MIPS_PC19_S2);
2335 static inline bool
2336 branch_reloc_p (int r_type)
2338 return (r_type == R_MIPS_26
2339 || r_type == R_MIPS_PC26_S2
2340 || r_type == R_MIPS_PC21_S2
2341 || r_type == R_MIPS_PC16
2342 || r_type == R_MIPS_GNU_REL16_S2);
2345 static inline bool
2346 mips16_branch_reloc_p (int r_type)
2348 return (r_type == R_MIPS16_26
2349 || r_type == R_MIPS16_PC16_S1);
2352 static inline bool
2353 micromips_branch_reloc_p (int r_type)
2355 return (r_type == R_MICROMIPS_26_S1
2356 || r_type == R_MICROMIPS_PC16_S1
2357 || r_type == R_MICROMIPS_PC10_S1
2358 || r_type == R_MICROMIPS_PC7_S1);
2361 static inline bool
2362 tls_gd_reloc_p (unsigned int r_type)
2364 return (r_type == R_MIPS_TLS_GD
2365 || r_type == R_MIPS16_TLS_GD
2366 || r_type == R_MICROMIPS_TLS_GD);
2369 static inline bool
2370 tls_ldm_reloc_p (unsigned int r_type)
2372 return (r_type == R_MIPS_TLS_LDM
2373 || r_type == R_MIPS16_TLS_LDM
2374 || r_type == R_MICROMIPS_TLS_LDM);
2377 static inline bool
2378 tls_gottprel_reloc_p (unsigned int r_type)
2380 return (r_type == R_MIPS_TLS_GOTTPREL
2381 || r_type == R_MIPS16_TLS_GOTTPREL
2382 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2385 static inline bool
2386 needs_shuffle (int r_type)
2388 return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2391 void
2392 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2393 bool jal_shuffle, bfd_byte *data)
2395 bfd_vma first, second, val;
2397 if (!needs_shuffle (r_type))
2398 return;
2400 /* Pick up the first and second halfwords of the instruction. */
2401 first = bfd_get_16 (abfd, data);
2402 second = bfd_get_16 (abfd, data + 2);
2403 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2404 val = first << 16 | second;
2405 else if (r_type != R_MIPS16_26)
2406 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2407 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2408 else
2409 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2410 | ((first & 0x1f) << 21) | second);
2411 bfd_put_32 (abfd, val, data);
2414 void
2415 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2416 bool jal_shuffle, bfd_byte *data)
2418 bfd_vma first, second, val;
2420 if (!needs_shuffle (r_type))
2421 return;
2423 val = bfd_get_32 (abfd, data);
2424 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2426 second = val & 0xffff;
2427 first = val >> 16;
2429 else if (r_type != R_MIPS16_26)
2431 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2432 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2434 else
2436 second = val & 0xffff;
2437 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2438 | ((val >> 21) & 0x1f);
2440 bfd_put_16 (abfd, second, data + 2);
2441 bfd_put_16 (abfd, first, data);
2444 /* Perform reloc offset checking.
2445 We can only use bfd_reloc_offset_in_range, which takes into account
2446 the size of the field being relocated, when section contents will
2447 be accessed because mips object files may use relocations that seem
2448 to access beyond section limits.
2449 gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2450 R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2451 section. The R_MIPS_SUB applies to the addend for the next reloc
2452 rather than the section contents.
2454 CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2455 CHECK_INPLACE to only check partial_inplace relocs, and
2456 CHECK_SHUFFLE to only check relocs that shuffle/unshuffle. */
2458 bool
2459 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2460 arelent *reloc_entry, enum reloc_check check)
2462 if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2463 return true;
2464 if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2465 return true;
2466 return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2467 input_section, reloc_entry->address);
2470 bfd_reloc_status_type
2471 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2472 arelent *reloc_entry, asection *input_section,
2473 bool relocatable, void *data, bfd_vma gp)
2475 bfd_vma relocation;
2476 bfd_signed_vma val;
2477 bfd_reloc_status_type status;
2479 if (bfd_is_com_section (symbol->section))
2480 relocation = 0;
2481 else
2482 relocation = symbol->value;
2484 if (symbol->section->output_section != NULL)
2486 relocation += symbol->section->output_section->vma;
2487 relocation += symbol->section->output_offset;
2490 /* Set val to the offset into the section or symbol. */
2491 val = reloc_entry->addend;
2493 _bfd_mips_elf_sign_extend (val, 16);
2495 /* Adjust val for the final section location and GP value. If we
2496 are producing relocatable output, we don't want to do this for
2497 an external symbol. */
2498 if (! relocatable
2499 || (symbol->flags & BSF_SECTION_SYM) != 0)
2500 val += relocation - gp;
2502 if (reloc_entry->howto->partial_inplace)
2504 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2505 reloc_entry->address))
2506 return bfd_reloc_outofrange;
2508 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2509 (bfd_byte *) data
2510 + reloc_entry->address);
2511 if (status != bfd_reloc_ok)
2512 return status;
2514 else
2515 reloc_entry->addend = val;
2517 if (relocatable)
2518 reloc_entry->address += input_section->output_offset;
2520 return bfd_reloc_ok;
2523 /* A howto special_function for REL *HI16 relocations. We can only
2524 calculate the correct value once we've seen the partnering
2525 *LO16 relocation, so just save the information for later.
2527 The ABI requires that the *LO16 immediately follow the *HI16.
2528 However, as a GNU extension, we permit an arbitrary number of
2529 *HI16s to be associated with a single *LO16. This significantly
2530 simplies the relocation handling in gcc. */
2532 bfd_reloc_status_type
2533 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2534 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2535 asection *input_section, bfd *output_bfd,
2536 char **error_message ATTRIBUTE_UNUSED)
2538 struct mips_hi16 *n;
2539 struct mips_elf_obj_tdata *tdata;
2541 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2542 return bfd_reloc_outofrange;
2544 n = bfd_malloc (sizeof *n);
2545 if (n == NULL)
2546 return bfd_reloc_outofrange;
2548 tdata = mips_elf_tdata (abfd);
2549 n->next = tdata->mips_hi16_list;
2550 n->data = data;
2551 n->input_section = input_section;
2552 n->rel = *reloc_entry;
2553 tdata->mips_hi16_list = n;
2555 if (output_bfd != NULL)
2556 reloc_entry->address += input_section->output_offset;
2558 return bfd_reloc_ok;
2561 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2562 like any other 16-bit relocation when applied to global symbols, but is
2563 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2565 bfd_reloc_status_type
2566 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2567 void *data, asection *input_section,
2568 bfd *output_bfd, char **error_message)
2570 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2571 || bfd_is_und_section (bfd_asymbol_section (symbol))
2572 || bfd_is_com_section (bfd_asymbol_section (symbol)))
2573 /* The relocation is against a global symbol. */
2574 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2575 input_section, output_bfd,
2576 error_message);
2578 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2579 input_section, output_bfd, error_message);
2582 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2583 is a straightforward 16 bit inplace relocation, but we must deal with
2584 any partnering high-part relocations as well. */
2586 bfd_reloc_status_type
2587 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588 void *data, asection *input_section,
2589 bfd *output_bfd, char **error_message)
2591 bfd_vma vallo;
2592 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2593 struct mips_elf_obj_tdata *tdata;
2595 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2596 reloc_entry->address))
2597 return bfd_reloc_outofrange;
2599 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2600 location);
2601 /* The high 16 bits of the addend are stored in the high insn, the
2602 low 16 bits in the low insn, but there is a catch: You can't
2603 just concatenate the high and low parts. The high part of the
2604 addend is adjusted for the fact that the low part is sign
2605 extended. For example, an addend of 0x38000 would have 0x0004 in
2606 the high part and 0x8000 (=0xff..f8000) in the low part.
2607 To extract the actual addend, calculate (a)
2608 ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2609 We will be applying (symbol + addend) & 0xffff to the low insn,
2610 and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2611 high insn (the +0x8000 adjusting for when the applied low part is
2612 negative). Substituting (a) into (b) and recognising that
2613 (hi & 0xffff) is already in the high insn gives a high part
2614 addend adjustment of (lo & 0xffff) ^ 0x8000. */
2615 vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2616 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2617 location);
2619 tdata = mips_elf_tdata (abfd);
2620 while (tdata->mips_hi16_list != NULL)
2622 bfd_reloc_status_type ret;
2623 struct mips_hi16 *hi;
2625 hi = tdata->mips_hi16_list;
2627 /* R_MIPS*_GOT16 relocations are something of a special case. We
2628 want to install the addend in the same way as for a R_MIPS*_HI16
2629 relocation (with a rightshift of 16). However, since GOT16
2630 relocations can also be used with global symbols, their howto
2631 has a rightshift of 0. */
2632 if (hi->rel.howto->type == R_MIPS_GOT16)
2633 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2634 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2635 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2636 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2637 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2639 hi->rel.addend += vallo;
2641 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2642 hi->input_section, output_bfd,
2643 error_message);
2644 if (ret != bfd_reloc_ok)
2645 return ret;
2647 tdata->mips_hi16_list = hi->next;
2648 free (hi);
2651 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2652 input_section, output_bfd,
2653 error_message);
2656 /* A generic howto special_function. This calculates and installs the
2657 relocation itself, thus avoiding the oft-discussed problems in
2658 bfd_perform_relocation and bfd_install_relocation. */
2660 bfd_reloc_status_type
2661 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2662 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2663 asection *input_section, bfd *output_bfd,
2664 char **error_message ATTRIBUTE_UNUSED)
2666 bfd_signed_vma val;
2667 bfd_reloc_status_type status;
2668 bool relocatable;
2670 relocatable = (output_bfd != NULL);
2672 if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2673 (relocatable
2674 ? check_inplace : check_std)))
2675 return bfd_reloc_outofrange;
2677 /* Build up the field adjustment in VAL. */
2678 val = 0;
2679 if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2680 && symbol->section->output_section != NULL)
2682 /* Either we're calculating the final field value or we have a
2683 relocation against a section symbol. Add in the section's
2684 offset or address. */
2685 val += symbol->section->output_section->vma;
2686 val += symbol->section->output_offset;
2689 if (!relocatable)
2691 /* We're calculating the final field value. Add in the symbol's value
2692 and, if pc-relative, subtract the address of the field itself. */
2693 val += symbol->value;
2694 if (reloc_entry->howto->pc_relative)
2696 val -= input_section->output_section->vma;
2697 val -= input_section->output_offset;
2698 val -= reloc_entry->address;
2702 /* VAL is now the final adjustment. If we're keeping this relocation
2703 in the output file, and if the relocation uses a separate addend,
2704 we just need to add VAL to that addend. Otherwise we need to add
2705 VAL to the relocation field itself. */
2706 if (relocatable && !reloc_entry->howto->partial_inplace)
2707 reloc_entry->addend += val;
2708 else
2710 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2712 /* Add in the separate addend, if any. */
2713 val += reloc_entry->addend;
2715 /* Add VAL to the relocation field. */
2716 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2717 location);
2718 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2719 location);
2720 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2721 location);
2723 if (status != bfd_reloc_ok)
2724 return status;
2727 if (relocatable)
2728 reloc_entry->address += input_section->output_offset;
2730 return bfd_reloc_ok;
2733 /* Swap an entry in a .gptab section. Note that these routines rely
2734 on the equivalence of the two elements of the union. */
2736 static void
2737 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2738 Elf32_gptab *in)
2740 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2741 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2744 static void
2745 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2746 Elf32_External_gptab *ex)
2748 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2749 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2752 static void
2753 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2754 Elf32_External_compact_rel *ex)
2756 H_PUT_32 (abfd, in->id1, ex->id1);
2757 H_PUT_32 (abfd, in->num, ex->num);
2758 H_PUT_32 (abfd, in->id2, ex->id2);
2759 H_PUT_32 (abfd, in->offset, ex->offset);
2760 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2761 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2764 static void
2765 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2766 Elf32_External_crinfo *ex)
2768 unsigned long l;
2770 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2771 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2772 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2773 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2774 H_PUT_32 (abfd, l, ex->info);
2775 H_PUT_32 (abfd, in->konst, ex->konst);
2776 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2779 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2780 routines swap this structure in and out. They are used outside of
2781 BFD, so they are globally visible. */
2783 void
2784 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2785 Elf32_RegInfo *in)
2787 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2788 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2789 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2790 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2791 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2792 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2795 void
2796 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2797 Elf32_External_RegInfo *ex)
2799 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2800 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2801 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2802 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2803 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2804 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2807 /* In the 64 bit ABI, the .MIPS.options section holds register
2808 information in an Elf64_Reginfo structure. These routines swap
2809 them in and out. They are globally visible because they are used
2810 outside of BFD. These routines are here so that gas can call them
2811 without worrying about whether the 64 bit ABI has been included. */
2813 void
2814 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2815 Elf64_Internal_RegInfo *in)
2817 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2818 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2819 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2820 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2821 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2822 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2823 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2826 void
2827 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2828 Elf64_External_RegInfo *ex)
2830 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2831 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2832 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2833 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2834 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2835 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2836 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2839 /* Swap in an options header. */
2841 void
2842 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2843 Elf_Internal_Options *in)
2845 in->kind = H_GET_8 (abfd, ex->kind);
2846 in->size = H_GET_8 (abfd, ex->size);
2847 in->section = H_GET_16 (abfd, ex->section);
2848 in->info = H_GET_32 (abfd, ex->info);
2851 /* Swap out an options header. */
2853 void
2854 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2855 Elf_External_Options *ex)
2857 H_PUT_8 (abfd, in->kind, ex->kind);
2858 H_PUT_8 (abfd, in->size, ex->size);
2859 H_PUT_16 (abfd, in->section, ex->section);
2860 H_PUT_32 (abfd, in->info, ex->info);
2863 /* Swap in an abiflags structure. */
2865 void
2866 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2867 const Elf_External_ABIFlags_v0 *ex,
2868 Elf_Internal_ABIFlags_v0 *in)
2870 in->version = H_GET_16 (abfd, ex->version);
2871 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2872 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2873 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2874 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2875 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2876 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2877 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2878 in->ases = H_GET_32 (abfd, ex->ases);
2879 in->flags1 = H_GET_32 (abfd, ex->flags1);
2880 in->flags2 = H_GET_32 (abfd, ex->flags2);
2883 /* Swap out an abiflags structure. */
2885 void
2886 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2887 const Elf_Internal_ABIFlags_v0 *in,
2888 Elf_External_ABIFlags_v0 *ex)
2890 H_PUT_16 (abfd, in->version, ex->version);
2891 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2892 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2893 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2894 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2895 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2896 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2897 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2898 H_PUT_32 (abfd, in->ases, ex->ases);
2899 H_PUT_32 (abfd, in->flags1, ex->flags1);
2900 H_PUT_32 (abfd, in->flags2, ex->flags2);
2903 /* This function is called via qsort() to sort the dynamic relocation
2904 entries by increasing r_symndx value. */
2906 static int
2907 sort_dynamic_relocs (const void *arg1, const void *arg2)
2909 Elf_Internal_Rela int_reloc1;
2910 Elf_Internal_Rela int_reloc2;
2911 int diff;
2913 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2914 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2916 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2917 if (diff != 0)
2918 return diff;
2920 if (int_reloc1.r_offset < int_reloc2.r_offset)
2921 return -1;
2922 if (int_reloc1.r_offset > int_reloc2.r_offset)
2923 return 1;
2924 return 0;
2927 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2929 static int
2930 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2931 const void *arg2 ATTRIBUTE_UNUSED)
2933 #ifdef BFD64
2934 Elf_Internal_Rela int_reloc1[3];
2935 Elf_Internal_Rela int_reloc2[3];
2937 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2938 (reldyn_sorting_bfd, arg1, int_reloc1);
2939 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940 (reldyn_sorting_bfd, arg2, int_reloc2);
2942 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2943 return -1;
2944 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2945 return 1;
2947 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2948 return -1;
2949 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2950 return 1;
2951 return 0;
2952 #else
2953 abort ();
2954 #endif
2958 /* This routine is used to write out ECOFF debugging external symbol
2959 information. It is called via mips_elf_link_hash_traverse. The
2960 ECOFF external symbol information must match the ELF external
2961 symbol information. Unfortunately, at this point we don't know
2962 whether a symbol is required by reloc information, so the two
2963 tables may wind up being different. We must sort out the external
2964 symbol information before we can set the final size of the .mdebug
2965 section, and we must set the size of the .mdebug section before we
2966 can relocate any sections, and we can't know which symbols are
2967 required by relocation until we relocate the sections.
2968 Fortunately, it is relatively unlikely that any symbol will be
2969 stripped but required by a reloc. In particular, it can not happen
2970 when generating a final executable. */
2972 static bool
2973 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2975 struct extsym_info *einfo = data;
2976 bool strip;
2977 asection *sec, *output_section;
2979 if (h->root.indx == -2)
2980 strip = false;
2981 else if ((h->root.def_dynamic
2982 || h->root.ref_dynamic
2983 || h->root.type == bfd_link_hash_new)
2984 && !h->root.def_regular
2985 && !h->root.ref_regular)
2986 strip = true;
2987 else if (einfo->info->strip == strip_all
2988 || (einfo->info->strip == strip_some
2989 && bfd_hash_lookup (einfo->info->keep_hash,
2990 h->root.root.root.string,
2991 false, false) == NULL))
2992 strip = true;
2993 else
2994 strip = false;
2996 if (strip)
2997 return true;
2999 if (h->esym.ifd == -2)
3001 h->esym.jmptbl = 0;
3002 h->esym.cobol_main = 0;
3003 h->esym.weakext = 0;
3004 h->esym.reserved = 0;
3005 h->esym.ifd = ifdNil;
3006 h->esym.asym.value = 0;
3007 h->esym.asym.st = stGlobal;
3009 if (h->root.root.type == bfd_link_hash_undefined
3010 || h->root.root.type == bfd_link_hash_undefweak)
3012 const char *name;
3014 /* Use undefined class. Also, set class and type for some
3015 special symbols. */
3016 name = h->root.root.root.string;
3017 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3018 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3020 h->esym.asym.sc = scData;
3021 h->esym.asym.st = stLabel;
3022 h->esym.asym.value = 0;
3024 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3026 h->esym.asym.sc = scAbs;
3027 h->esym.asym.st = stLabel;
3028 h->esym.asym.value =
3029 mips_elf_hash_table (einfo->info)->procedure_count;
3031 else
3032 h->esym.asym.sc = scUndefined;
3034 else if (h->root.root.type != bfd_link_hash_defined
3035 && h->root.root.type != bfd_link_hash_defweak)
3036 h->esym.asym.sc = scAbs;
3037 else
3039 const char *name;
3041 sec = h->root.root.u.def.section;
3042 output_section = sec->output_section;
3044 /* When making a shared library and symbol h is the one from
3045 the another shared library, OUTPUT_SECTION may be null. */
3046 if (output_section == NULL)
3047 h->esym.asym.sc = scUndefined;
3048 else
3050 name = bfd_section_name (output_section);
3052 if (strcmp (name, ".text") == 0)
3053 h->esym.asym.sc = scText;
3054 else if (strcmp (name, ".data") == 0)
3055 h->esym.asym.sc = scData;
3056 else if (strcmp (name, ".sdata") == 0)
3057 h->esym.asym.sc = scSData;
3058 else if (strcmp (name, ".rodata") == 0
3059 || strcmp (name, ".rdata") == 0)
3060 h->esym.asym.sc = scRData;
3061 else if (strcmp (name, ".bss") == 0)
3062 h->esym.asym.sc = scBss;
3063 else if (strcmp (name, ".sbss") == 0)
3064 h->esym.asym.sc = scSBss;
3065 else if (strcmp (name, ".init") == 0)
3066 h->esym.asym.sc = scInit;
3067 else if (strcmp (name, ".fini") == 0)
3068 h->esym.asym.sc = scFini;
3069 else
3070 h->esym.asym.sc = scAbs;
3074 h->esym.asym.reserved = 0;
3075 h->esym.asym.index = indexNil;
3078 if (h->root.root.type == bfd_link_hash_common)
3079 h->esym.asym.value = h->root.root.u.c.size;
3080 else if (h->root.root.type == bfd_link_hash_defined
3081 || h->root.root.type == bfd_link_hash_defweak)
3083 if (h->esym.asym.sc == scCommon)
3084 h->esym.asym.sc = scBss;
3085 else if (h->esym.asym.sc == scSCommon)
3086 h->esym.asym.sc = scSBss;
3088 sec = h->root.root.u.def.section;
3089 output_section = sec->output_section;
3090 if (output_section != NULL)
3091 h->esym.asym.value = (h->root.root.u.def.value
3092 + sec->output_offset
3093 + output_section->vma);
3094 else
3095 h->esym.asym.value = 0;
3097 else
3099 struct mips_elf_link_hash_entry *hd = h;
3101 while (hd->root.root.type == bfd_link_hash_indirect)
3102 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3104 if (hd->needs_lazy_stub)
3106 BFD_ASSERT (hd->root.plt.plist != NULL);
3107 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3108 /* Set type and value for a symbol with a function stub. */
3109 h->esym.asym.st = stProc;
3110 sec = hd->root.root.u.def.section;
3111 if (sec == NULL)
3112 h->esym.asym.value = 0;
3113 else
3115 output_section = sec->output_section;
3116 if (output_section != NULL)
3117 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3118 + sec->output_offset
3119 + output_section->vma);
3120 else
3121 h->esym.asym.value = 0;
3126 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3127 h->root.root.root.string,
3128 &h->esym))
3130 einfo->failed = true;
3131 return false;
3134 return true;
3137 /* A comparison routine used to sort .gptab entries. */
3139 static int
3140 gptab_compare (const void *p1, const void *p2)
3142 const Elf32_gptab *a1 = p1;
3143 const Elf32_gptab *a2 = p2;
3145 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3148 /* Functions to manage the got entry hash table. */
3150 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3151 hash number. */
3153 static inline hashval_t
3154 mips_elf_hash_bfd_vma (bfd_vma addr)
3156 #ifdef BFD64
3157 return addr + (addr >> 32);
3158 #else
3159 return addr;
3160 #endif
3163 static hashval_t
3164 mips_elf_got_entry_hash (const void *entry_)
3166 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3168 return (entry->symndx
3169 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3170 + (entry->tls_type == GOT_TLS_LDM ? 0
3171 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3172 : entry->symndx >= 0 ? (entry->abfd->id
3173 + mips_elf_hash_bfd_vma (entry->d.addend))
3174 : entry->d.h->root.root.root.hash));
3177 static int
3178 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3180 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3181 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3183 return (e1->symndx == e2->symndx
3184 && e1->tls_type == e2->tls_type
3185 && (e1->tls_type == GOT_TLS_LDM ? true
3186 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3187 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3188 && e1->d.addend == e2->d.addend)
3189 : e2->abfd && e1->d.h == e2->d.h));
3192 static hashval_t
3193 mips_got_page_ref_hash (const void *ref_)
3195 const struct mips_got_page_ref *ref;
3197 ref = (const struct mips_got_page_ref *) ref_;
3198 return ((ref->symndx >= 0
3199 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3200 : ref->u.h->root.root.root.hash)
3201 + mips_elf_hash_bfd_vma (ref->addend));
3204 static int
3205 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3207 const struct mips_got_page_ref *ref1, *ref2;
3209 ref1 = (const struct mips_got_page_ref *) ref1_;
3210 ref2 = (const struct mips_got_page_ref *) ref2_;
3211 return (ref1->symndx == ref2->symndx
3212 && (ref1->symndx < 0
3213 ? ref1->u.h == ref2->u.h
3214 : ref1->u.abfd == ref2->u.abfd)
3215 && ref1->addend == ref2->addend);
3218 static hashval_t
3219 mips_got_page_entry_hash (const void *entry_)
3221 const struct mips_got_page_entry *entry;
3223 entry = (const struct mips_got_page_entry *) entry_;
3224 return entry->sec->id;
3227 static int
3228 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3230 const struct mips_got_page_entry *entry1, *entry2;
3232 entry1 = (const struct mips_got_page_entry *) entry1_;
3233 entry2 = (const struct mips_got_page_entry *) entry2_;
3234 return entry1->sec == entry2->sec;
3237 /* Create and return a new mips_got_info structure. */
3239 static struct mips_got_info *
3240 mips_elf_create_got_info (bfd *abfd)
3242 struct mips_got_info *g;
3244 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3245 if (g == NULL)
3246 return NULL;
3248 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3249 mips_elf_got_entry_eq, NULL);
3250 if (g->got_entries == NULL)
3251 return NULL;
3253 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3254 mips_got_page_ref_eq, NULL);
3255 if (g->got_page_refs == NULL)
3256 return NULL;
3258 return g;
3261 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3262 CREATE_P and if ABFD doesn't already have a GOT. */
3264 static struct mips_got_info *
3265 mips_elf_bfd_got (bfd *abfd, bool create_p)
3267 struct mips_elf_obj_tdata *tdata;
3269 if (!is_mips_elf (abfd))
3270 return NULL;
3272 tdata = mips_elf_tdata (abfd);
3273 if (!tdata->got && create_p)
3274 tdata->got = mips_elf_create_got_info (abfd);
3275 return tdata->got;
3278 /* Record that ABFD should use output GOT G. */
3280 static void
3281 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3283 struct mips_elf_obj_tdata *tdata;
3285 BFD_ASSERT (is_mips_elf (abfd));
3286 tdata = mips_elf_tdata (abfd);
3287 if (tdata->got)
3289 /* The GOT structure itself and the hash table entries are
3290 allocated to a bfd, but the hash tables aren't. */
3291 htab_delete (tdata->got->got_entries);
3292 htab_delete (tdata->got->got_page_refs);
3293 if (tdata->got->got_page_entries)
3294 htab_delete (tdata->got->got_page_entries);
3296 tdata->got = g;
3299 /* Return the dynamic relocation section. If it doesn't exist, try to
3300 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3301 if creation fails. */
3303 static asection *
3304 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3306 const char *dname;
3307 asection *sreloc;
3308 bfd *dynobj;
3310 dname = MIPS_ELF_REL_DYN_NAME (info);
3311 dynobj = elf_hash_table (info)->dynobj;
3312 sreloc = bfd_get_linker_section (dynobj, dname);
3313 if (sreloc == NULL && create_p)
3315 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3316 (SEC_ALLOC
3317 | SEC_LOAD
3318 | SEC_HAS_CONTENTS
3319 | SEC_IN_MEMORY
3320 | SEC_LINKER_CREATED
3321 | SEC_READONLY));
3322 if (sreloc == NULL
3323 || !bfd_set_section_alignment (sreloc,
3324 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3325 return NULL;
3327 return sreloc;
3330 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3332 static int
3333 mips_elf_reloc_tls_type (unsigned int r_type)
3335 if (tls_gd_reloc_p (r_type))
3336 return GOT_TLS_GD;
3338 if (tls_ldm_reloc_p (r_type))
3339 return GOT_TLS_LDM;
3341 if (tls_gottprel_reloc_p (r_type))
3342 return GOT_TLS_IE;
3344 return GOT_TLS_NONE;
3347 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3349 static int
3350 mips_tls_got_entries (unsigned int type)
3352 switch (type)
3354 case GOT_TLS_GD:
3355 case GOT_TLS_LDM:
3356 return 2;
3358 case GOT_TLS_IE:
3359 return 1;
3361 case GOT_TLS_NONE:
3362 return 0;
3364 abort ();
3367 /* Count the number of relocations needed for a TLS GOT entry, with
3368 access types from TLS_TYPE, and symbol H (or a local symbol if H
3369 is NULL). */
3371 static int
3372 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3373 struct elf_link_hash_entry *h)
3375 int indx = 0;
3376 bool need_relocs = false;
3377 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3379 if (h != NULL
3380 && h->dynindx != -1
3381 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3382 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3383 indx = h->dynindx;
3385 if ((bfd_link_dll (info) || indx != 0)
3386 && (h == NULL
3387 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3388 || h->root.type != bfd_link_hash_undefweak))
3389 need_relocs = true;
3391 if (!need_relocs)
3392 return 0;
3394 switch (tls_type)
3396 case GOT_TLS_GD:
3397 return indx != 0 ? 2 : 1;
3399 case GOT_TLS_IE:
3400 return 1;
3402 case GOT_TLS_LDM:
3403 return bfd_link_dll (info) ? 1 : 0;
3405 default:
3406 return 0;
3410 /* Add the number of GOT entries and TLS relocations required by ENTRY
3411 to G. */
3413 static void
3414 mips_elf_count_got_entry (struct bfd_link_info *info,
3415 struct mips_got_info *g,
3416 struct mips_got_entry *entry)
3418 if (entry->tls_type)
3420 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3421 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3422 entry->symndx < 0
3423 ? &entry->d.h->root : NULL);
3425 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3426 g->local_gotno += 1;
3427 else
3428 g->global_gotno += 1;
3431 /* Output a simple dynamic relocation into SRELOC. */
3433 static void
3434 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3435 asection *sreloc,
3436 unsigned long reloc_index,
3437 unsigned long indx,
3438 int r_type,
3439 bfd_vma offset)
3441 Elf_Internal_Rela rel[3];
3443 memset (rel, 0, sizeof (rel));
3445 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3446 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3448 if (ABI_64_P (output_bfd))
3450 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3451 (output_bfd, &rel[0],
3452 (sreloc->contents
3453 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3455 else
3456 bfd_elf32_swap_reloc_out
3457 (output_bfd, &rel[0],
3458 (sreloc->contents
3459 + reloc_index * sizeof (Elf32_External_Rel)));
3462 /* Initialize a set of TLS GOT entries for one symbol. */
3464 static void
3465 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3466 struct mips_got_entry *entry,
3467 struct mips_elf_link_hash_entry *h,
3468 bfd_vma value)
3470 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3471 struct mips_elf_link_hash_table *htab;
3472 int indx;
3473 asection *sreloc, *sgot;
3474 bfd_vma got_offset, got_offset2;
3475 bool need_relocs = false;
3477 htab = mips_elf_hash_table (info);
3478 if (htab == NULL)
3479 return;
3481 sgot = htab->root.sgot;
3483 indx = 0;
3484 if (h != NULL
3485 && h->root.dynindx != -1
3486 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3487 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3488 indx = h->root.dynindx;
3490 if (entry->tls_initialized)
3491 return;
3493 if ((bfd_link_dll (info) || indx != 0)
3494 && (h == NULL
3495 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3496 || h->root.type != bfd_link_hash_undefweak))
3497 need_relocs = true;
3499 /* MINUS_ONE means the symbol is not defined in this object. It may not
3500 be defined at all; assume that the value doesn't matter in that
3501 case. Otherwise complain if we would use the value. */
3502 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3503 || h->root.root.type == bfd_link_hash_undefweak);
3505 /* Emit necessary relocations. */
3506 sreloc = mips_elf_rel_dyn_section (info, false);
3507 got_offset = entry->gotidx;
3509 switch (entry->tls_type)
3511 case GOT_TLS_GD:
3512 /* General Dynamic. */
3513 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3515 if (need_relocs)
3517 mips_elf_output_dynamic_relocation
3518 (abfd, sreloc, sreloc->reloc_count++, indx,
3519 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3520 sgot->output_offset + sgot->output_section->vma + got_offset);
3522 if (indx)
3523 mips_elf_output_dynamic_relocation
3524 (abfd, sreloc, sreloc->reloc_count++, indx,
3525 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3526 sgot->output_offset + sgot->output_section->vma + got_offset2);
3527 else
3528 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3529 sgot->contents + got_offset2);
3531 else
3533 MIPS_ELF_PUT_WORD (abfd, 1,
3534 sgot->contents + got_offset);
3535 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3536 sgot->contents + got_offset2);
3538 break;
3540 case GOT_TLS_IE:
3541 /* Initial Exec model. */
3542 if (need_relocs)
3544 if (indx == 0)
3545 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3546 sgot->contents + got_offset);
3547 else
3548 MIPS_ELF_PUT_WORD (abfd, 0,
3549 sgot->contents + got_offset);
3551 mips_elf_output_dynamic_relocation
3552 (abfd, sreloc, sreloc->reloc_count++, indx,
3553 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3554 sgot->output_offset + sgot->output_section->vma + got_offset);
3556 else
3557 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3558 sgot->contents + got_offset);
3559 break;
3561 case GOT_TLS_LDM:
3562 /* The initial offset is zero, and the LD offsets will include the
3563 bias by DTP_OFFSET. */
3564 MIPS_ELF_PUT_WORD (abfd, 0,
3565 sgot->contents + got_offset
3566 + MIPS_ELF_GOT_SIZE (abfd));
3568 if (!bfd_link_dll (info))
3569 MIPS_ELF_PUT_WORD (abfd, 1,
3570 sgot->contents + got_offset);
3571 else
3572 mips_elf_output_dynamic_relocation
3573 (abfd, sreloc, sreloc->reloc_count++, indx,
3574 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3575 sgot->output_offset + sgot->output_section->vma + got_offset);
3576 break;
3578 default:
3579 abort ();
3582 entry->tls_initialized = true;
3585 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3586 for global symbol H. .got.plt comes before the GOT, so the offset
3587 will be negative. */
3589 static bfd_vma
3590 mips_elf_gotplt_index (struct bfd_link_info *info,
3591 struct elf_link_hash_entry *h)
3593 bfd_vma got_address, got_value;
3594 struct mips_elf_link_hash_table *htab;
3596 htab = mips_elf_hash_table (info);
3597 BFD_ASSERT (htab != NULL);
3599 BFD_ASSERT (h->plt.plist != NULL);
3600 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3602 /* Calculate the address of the associated .got.plt entry. */
3603 got_address = (htab->root.sgotplt->output_section->vma
3604 + htab->root.sgotplt->output_offset
3605 + (h->plt.plist->gotplt_index
3606 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3608 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3609 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3610 + htab->root.hgot->root.u.def.section->output_offset
3611 + htab->root.hgot->root.u.def.value);
3613 return got_address - got_value;
3616 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3617 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3618 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3619 offset can be found. */
3621 static bfd_vma
3622 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3623 bfd_vma value, unsigned long r_symndx,
3624 struct mips_elf_link_hash_entry *h, int r_type)
3626 struct mips_elf_link_hash_table *htab;
3627 struct mips_got_entry *entry;
3629 htab = mips_elf_hash_table (info);
3630 BFD_ASSERT (htab != NULL);
3632 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3633 r_symndx, h, r_type);
3634 if (!entry)
3635 return MINUS_ONE;
3637 if (entry->tls_type)
3638 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3639 return entry->gotidx;
3642 /* Return the GOT index of global symbol H in the primary GOT. */
3644 static bfd_vma
3645 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3646 struct elf_link_hash_entry *h)
3648 struct mips_elf_link_hash_table *htab;
3649 long global_got_dynindx;
3650 struct mips_got_info *g;
3651 bfd_vma got_index;
3653 htab = mips_elf_hash_table (info);
3654 BFD_ASSERT (htab != NULL);
3656 global_got_dynindx = 0;
3657 if (htab->global_gotsym != NULL)
3658 global_got_dynindx = htab->global_gotsym->dynindx;
3660 /* Once we determine the global GOT entry with the lowest dynamic
3661 symbol table index, we must put all dynamic symbols with greater
3662 indices into the primary GOT. That makes it easy to calculate the
3663 GOT offset. */
3664 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3665 g = mips_elf_bfd_got (obfd, false);
3666 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3667 * MIPS_ELF_GOT_SIZE (obfd));
3668 BFD_ASSERT (got_index < htab->root.sgot->size);
3670 return got_index;
3673 /* Return the GOT index for the global symbol indicated by H, which is
3674 referenced by a relocation of type R_TYPE in IBFD. */
3676 static bfd_vma
3677 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3678 struct elf_link_hash_entry *h, int r_type)
3680 struct mips_elf_link_hash_table *htab;
3681 struct mips_got_info *g;
3682 struct mips_got_entry lookup, *entry;
3683 bfd_vma gotidx;
3685 htab = mips_elf_hash_table (info);
3686 BFD_ASSERT (htab != NULL);
3688 g = mips_elf_bfd_got (ibfd, false);
3689 BFD_ASSERT (g);
3691 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3692 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3693 return mips_elf_primary_global_got_index (obfd, info, h);
3695 lookup.abfd = ibfd;
3696 lookup.symndx = -1;
3697 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3698 entry = htab_find (g->got_entries, &lookup);
3699 BFD_ASSERT (entry);
3701 gotidx = entry->gotidx;
3702 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3704 if (lookup.tls_type)
3706 bfd_vma value = MINUS_ONE;
3708 if ((h->root.type == bfd_link_hash_defined
3709 || h->root.type == bfd_link_hash_defweak)
3710 && h->root.u.def.section->output_section)
3711 value = (h->root.u.def.value
3712 + h->root.u.def.section->output_offset
3713 + h->root.u.def.section->output_section->vma);
3715 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3717 return gotidx;
3720 /* Find a GOT page entry that points to within 32KB of VALUE. These
3721 entries are supposed to be placed at small offsets in the GOT, i.e.,
3722 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3723 entry could be created. If OFFSETP is nonnull, use it to return the
3724 offset of the GOT entry from VALUE. */
3726 static bfd_vma
3727 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3728 bfd_vma value, bfd_vma *offsetp)
3730 bfd_vma page, got_index;
3731 struct mips_got_entry *entry;
3733 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3734 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3735 NULL, R_MIPS_GOT_PAGE);
3737 if (!entry)
3738 return MINUS_ONE;
3740 got_index = entry->gotidx;
3742 if (offsetp)
3743 *offsetp = value - entry->d.address;
3745 return got_index;
3748 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3749 EXTERNAL is true if the relocation was originally against a global
3750 symbol that binds locally. */
3752 static bfd_vma
3753 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3754 bfd_vma value, bool external)
3756 struct mips_got_entry *entry;
3758 /* GOT16 relocations against local symbols are followed by a LO16
3759 relocation; those against global symbols are not. Thus if the
3760 symbol was originally local, the GOT16 relocation should load the
3761 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3762 if (! external)
3763 value = mips_elf_high (value) << 16;
3765 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3766 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3767 same in all cases. */
3768 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3769 NULL, R_MIPS_GOT16);
3770 if (entry)
3771 return entry->gotidx;
3772 else
3773 return MINUS_ONE;
3776 /* Returns the offset for the entry at the INDEXth position
3777 in the GOT. */
3779 static bfd_vma
3780 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3781 bfd *input_bfd, bfd_vma got_index)
3783 struct mips_elf_link_hash_table *htab;
3784 asection *sgot;
3785 bfd_vma gp;
3787 htab = mips_elf_hash_table (info);
3788 BFD_ASSERT (htab != NULL);
3790 sgot = htab->root.sgot;
3791 gp = _bfd_get_gp_value (output_bfd)
3792 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3794 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3797 /* Create and return a local GOT entry for VALUE, which was calculated
3798 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3799 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3800 instead. */
3802 static struct mips_got_entry *
3803 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3804 bfd *ibfd, bfd_vma value,
3805 unsigned long r_symndx,
3806 struct mips_elf_link_hash_entry *h,
3807 int r_type)
3809 struct mips_got_entry lookup, *entry;
3810 void **loc;
3811 struct mips_got_info *g;
3812 struct mips_elf_link_hash_table *htab;
3813 bfd_vma gotidx;
3815 htab = mips_elf_hash_table (info);
3816 BFD_ASSERT (htab != NULL);
3818 g = mips_elf_bfd_got (ibfd, false);
3819 if (g == NULL)
3821 g = mips_elf_bfd_got (abfd, false);
3822 BFD_ASSERT (g != NULL);
3825 /* This function shouldn't be called for symbols that live in the global
3826 area of the GOT. */
3827 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3829 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3830 if (lookup.tls_type)
3832 lookup.abfd = ibfd;
3833 if (tls_ldm_reloc_p (r_type))
3835 lookup.symndx = 0;
3836 lookup.d.addend = 0;
3838 else if (h == NULL)
3840 lookup.symndx = r_symndx;
3841 lookup.d.addend = 0;
3843 else
3845 lookup.symndx = -1;
3846 lookup.d.h = h;
3849 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3850 BFD_ASSERT (entry);
3852 gotidx = entry->gotidx;
3853 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3855 return entry;
3858 lookup.abfd = NULL;
3859 lookup.symndx = -1;
3860 lookup.d.address = value;
3861 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3862 if (!loc)
3863 return NULL;
3865 entry = (struct mips_got_entry *) *loc;
3866 if (entry)
3867 return entry;
3869 if (g->assigned_low_gotno > g->assigned_high_gotno)
3871 /* We didn't allocate enough space in the GOT. */
3872 _bfd_error_handler
3873 (_("not enough GOT space for local GOT entries"));
3874 bfd_set_error (bfd_error_bad_value);
3875 return NULL;
3878 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3879 if (!entry)
3880 return NULL;
3882 if (got16_reloc_p (r_type)
3883 || call16_reloc_p (r_type)
3884 || got_page_reloc_p (r_type)
3885 || got_disp_reloc_p (r_type))
3886 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3887 else
3888 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3890 *entry = lookup;
3891 *loc = entry;
3893 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3895 /* These GOT entries need a dynamic relocation on VxWorks. */
3896 if (htab->root.target_os == is_vxworks)
3898 Elf_Internal_Rela outrel;
3899 asection *s;
3900 bfd_byte *rloc;
3901 bfd_vma got_address;
3903 s = mips_elf_rel_dyn_section (info, false);
3904 got_address = (htab->root.sgot->output_section->vma
3905 + htab->root.sgot->output_offset
3906 + entry->gotidx);
3908 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3909 outrel.r_offset = got_address;
3910 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3911 outrel.r_addend = value;
3912 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3915 return entry;
3918 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3919 The number might be exact or a worst-case estimate, depending on how
3920 much information is available to elf_backend_omit_section_dynsym at
3921 the current linking stage. */
3923 static bfd_size_type
3924 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3926 bfd_size_type count;
3928 count = 0;
3929 if (bfd_link_pic (info)
3930 || elf_hash_table (info)->is_relocatable_executable)
3932 asection *p;
3933 const struct elf_backend_data *bed;
3935 bed = get_elf_backend_data (output_bfd);
3936 for (p = output_bfd->sections; p ; p = p->next)
3937 if ((p->flags & SEC_EXCLUDE) == 0
3938 && (p->flags & SEC_ALLOC) != 0
3939 && elf_hash_table (info)->dynamic_relocs
3940 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3941 ++count;
3943 return count;
3946 /* Sort the dynamic symbol table so that symbols that need GOT entries
3947 appear towards the end. */
3949 static bool
3950 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3952 struct mips_elf_link_hash_table *htab;
3953 struct mips_elf_hash_sort_data hsd;
3954 struct mips_got_info *g;
3956 htab = mips_elf_hash_table (info);
3957 BFD_ASSERT (htab != NULL);
3959 if (htab->root.dynsymcount == 0)
3960 return true;
3962 g = htab->got_info;
3963 if (g == NULL)
3964 return true;
3966 hsd.low = NULL;
3967 hsd.max_unref_got_dynindx
3968 = hsd.min_got_dynindx
3969 = (htab->root.dynsymcount - g->reloc_only_gotno);
3970 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3971 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3972 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3973 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3974 hsd.output_bfd = abfd;
3975 if (htab->root.dynobj != NULL
3976 && htab->root.dynamic_sections_created
3977 && info->emit_gnu_hash)
3979 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3980 BFD_ASSERT (s != NULL);
3981 hsd.mipsxhash = s->contents;
3982 BFD_ASSERT (hsd.mipsxhash != NULL);
3984 else
3985 hsd.mipsxhash = NULL;
3986 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3988 /* There should have been enough room in the symbol table to
3989 accommodate both the GOT and non-GOT symbols. */
3990 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3991 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3992 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3993 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3995 /* Now we know which dynamic symbol has the lowest dynamic symbol
3996 table index in the GOT. */
3997 htab->global_gotsym = hsd.low;
3999 return true;
4002 /* If H needs a GOT entry, assign it the highest available dynamic
4003 index. Otherwise, assign it the lowest available dynamic
4004 index. */
4006 static bool
4007 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4009 struct mips_elf_hash_sort_data *hsd = data;
4011 /* Symbols without dynamic symbol table entries aren't interesting
4012 at all. */
4013 if (h->root.dynindx == -1)
4014 return true;
4016 switch (h->global_got_area)
4018 case GGA_NONE:
4019 if (h->root.forced_local)
4020 h->root.dynindx = hsd->max_local_dynindx++;
4021 else
4022 h->root.dynindx = hsd->max_non_got_dynindx++;
4023 break;
4025 case GGA_NORMAL:
4026 h->root.dynindx = --hsd->min_got_dynindx;
4027 hsd->low = (struct elf_link_hash_entry *) h;
4028 break;
4030 case GGA_RELOC_ONLY:
4031 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4032 hsd->low = (struct elf_link_hash_entry *) h;
4033 h->root.dynindx = hsd->max_unref_got_dynindx++;
4034 break;
4037 /* Populate the .MIPS.xhash translation table entry with
4038 the symbol dynindx. */
4039 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4040 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4041 hsd->mipsxhash + h->mipsxhash_loc);
4043 return true;
4046 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4047 (which is owned by the caller and shouldn't be added to the
4048 hash table directly). */
4050 static bool
4051 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4052 struct mips_got_entry *lookup)
4054 struct mips_elf_link_hash_table *htab;
4055 struct mips_got_entry *entry;
4056 struct mips_got_info *g;
4057 void **loc, **bfd_loc;
4059 /* Make sure there's a slot for this entry in the master GOT. */
4060 htab = mips_elf_hash_table (info);
4061 g = htab->got_info;
4062 loc = htab_find_slot (g->got_entries, lookup, INSERT);
4063 if (!loc)
4064 return false;
4066 /* Populate the entry if it isn't already. */
4067 entry = (struct mips_got_entry *) *loc;
4068 if (!entry)
4070 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4071 if (!entry)
4072 return false;
4074 lookup->tls_initialized = false;
4075 lookup->gotidx = -1;
4076 *entry = *lookup;
4077 *loc = entry;
4080 /* Reuse the same GOT entry for the BFD's GOT. */
4081 g = mips_elf_bfd_got (abfd, true);
4082 if (!g)
4083 return false;
4085 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4086 if (!bfd_loc)
4087 return false;
4089 if (!*bfd_loc)
4090 *bfd_loc = entry;
4091 return true;
4094 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4095 entry for it. FOR_CALL is true if the caller is only interested in
4096 using the GOT entry for calls. */
4098 static bool
4099 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4100 bfd *abfd, struct bfd_link_info *info,
4101 bool for_call, int r_type)
4103 struct mips_elf_link_hash_table *htab;
4104 struct mips_elf_link_hash_entry *hmips;
4105 struct mips_got_entry entry;
4106 unsigned char tls_type;
4108 htab = mips_elf_hash_table (info);
4109 BFD_ASSERT (htab != NULL);
4111 hmips = (struct mips_elf_link_hash_entry *) h;
4112 if (!for_call)
4113 hmips->got_only_for_calls = false;
4115 /* A global symbol in the GOT must also be in the dynamic symbol
4116 table. */
4117 if (h->dynindx == -1)
4119 switch (ELF_ST_VISIBILITY (h->other))
4121 case STV_INTERNAL:
4122 case STV_HIDDEN:
4123 _bfd_mips_elf_hide_symbol (info, h, true);
4124 break;
4126 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127 return false;
4130 tls_type = mips_elf_reloc_tls_type (r_type);
4131 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4132 hmips->global_got_area = GGA_NORMAL;
4134 entry.abfd = abfd;
4135 entry.symndx = -1;
4136 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4137 entry.tls_type = tls_type;
4138 return mips_elf_record_got_entry (info, abfd, &entry);
4141 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4142 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4144 static bool
4145 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4146 struct bfd_link_info *info, int r_type)
4148 struct mips_elf_link_hash_table *htab;
4149 struct mips_got_info *g;
4150 struct mips_got_entry entry;
4152 htab = mips_elf_hash_table (info);
4153 BFD_ASSERT (htab != NULL);
4155 g = htab->got_info;
4156 BFD_ASSERT (g != NULL);
4158 entry.abfd = abfd;
4159 entry.symndx = symndx;
4160 entry.d.addend = addend;
4161 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4162 return mips_elf_record_got_entry (info, abfd, &entry);
4165 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4166 H is the symbol's hash table entry, or null if SYMNDX is local
4167 to ABFD. */
4169 static bool
4170 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4171 long symndx, struct elf_link_hash_entry *h,
4172 bfd_signed_vma addend)
4174 struct mips_elf_link_hash_table *htab;
4175 struct mips_got_info *g1, *g2;
4176 struct mips_got_page_ref lookup, *entry;
4177 void **loc, **bfd_loc;
4179 htab = mips_elf_hash_table (info);
4180 BFD_ASSERT (htab != NULL);
4182 g1 = htab->got_info;
4183 BFD_ASSERT (g1 != NULL);
4185 if (h)
4187 lookup.symndx = -1;
4188 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4190 else
4192 lookup.symndx = symndx;
4193 lookup.u.abfd = abfd;
4195 lookup.addend = addend;
4196 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4197 if (loc == NULL)
4198 return false;
4200 entry = (struct mips_got_page_ref *) *loc;
4201 if (!entry)
4203 entry = bfd_alloc (abfd, sizeof (*entry));
4204 if (!entry)
4205 return false;
4207 *entry = lookup;
4208 *loc = entry;
4211 /* Add the same entry to the BFD's GOT. */
4212 g2 = mips_elf_bfd_got (abfd, true);
4213 if (!g2)
4214 return false;
4216 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4217 if (!bfd_loc)
4218 return false;
4220 if (!*bfd_loc)
4221 *bfd_loc = entry;
4223 return true;
4226 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4228 static void
4229 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4230 unsigned int n)
4232 asection *s;
4233 struct mips_elf_link_hash_table *htab;
4235 htab = mips_elf_hash_table (info);
4236 BFD_ASSERT (htab != NULL);
4238 s = mips_elf_rel_dyn_section (info, false);
4239 BFD_ASSERT (s != NULL);
4241 if (htab->root.target_os == is_vxworks)
4242 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4243 else
4245 if (s->size == 0)
4247 /* Make room for a null element. */
4248 s->size += MIPS_ELF_REL_SIZE (abfd);
4249 ++s->reloc_count;
4251 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4255 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4256 mips_elf_traverse_got_arg structure. Count the number of GOT
4257 entries and TLS relocs. Set DATA->value to true if we need
4258 to resolve indirect or warning symbols and then recreate the GOT. */
4260 static int
4261 mips_elf_check_recreate_got (void **entryp, void *data)
4263 struct mips_got_entry *entry;
4264 struct mips_elf_traverse_got_arg *arg;
4266 entry = (struct mips_got_entry *) *entryp;
4267 arg = (struct mips_elf_traverse_got_arg *) data;
4268 if (entry->abfd != NULL && entry->symndx == -1)
4270 struct mips_elf_link_hash_entry *h;
4272 h = entry->d.h;
4273 if (h->root.root.type == bfd_link_hash_indirect
4274 || h->root.root.type == bfd_link_hash_warning)
4276 arg->value = true;
4277 return 0;
4280 mips_elf_count_got_entry (arg->info, arg->g, entry);
4281 return 1;
4284 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4285 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4286 converting entries for indirect and warning symbols into entries
4287 for the target symbol. Set DATA->g to null on error. */
4289 static int
4290 mips_elf_recreate_got (void **entryp, void *data)
4292 struct mips_got_entry new_entry, *entry;
4293 struct mips_elf_traverse_got_arg *arg;
4294 void **slot;
4296 entry = (struct mips_got_entry *) *entryp;
4297 arg = (struct mips_elf_traverse_got_arg *) data;
4298 if (entry->abfd != NULL
4299 && entry->symndx == -1
4300 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4301 || entry->d.h->root.root.type == bfd_link_hash_warning))
4303 struct mips_elf_link_hash_entry *h;
4305 new_entry = *entry;
4306 entry = &new_entry;
4307 h = entry->d.h;
4310 BFD_ASSERT (h->global_got_area == GGA_NONE);
4311 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4313 while (h->root.root.type == bfd_link_hash_indirect
4314 || h->root.root.type == bfd_link_hash_warning);
4315 entry->d.h = h;
4317 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4318 if (slot == NULL)
4320 arg->g = NULL;
4321 return 0;
4323 if (*slot == NULL)
4325 if (entry == &new_entry)
4327 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4328 if (!entry)
4330 arg->g = NULL;
4331 return 0;
4333 *entry = new_entry;
4335 *slot = entry;
4336 mips_elf_count_got_entry (arg->info, arg->g, entry);
4338 return 1;
4341 /* Return the maximum number of GOT page entries required for RANGE. */
4343 static bfd_vma
4344 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4346 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4349 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4351 static bool
4352 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4353 asection *sec, bfd_signed_vma addend)
4355 struct mips_got_info *g = arg->g;
4356 struct mips_got_page_entry lookup, *entry;
4357 struct mips_got_page_range **range_ptr, *range;
4358 bfd_vma old_pages, new_pages;
4359 void **loc;
4361 /* Find the mips_got_page_entry hash table entry for this section. */
4362 lookup.sec = sec;
4363 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4364 if (loc == NULL)
4365 return false;
4367 /* Create a mips_got_page_entry if this is the first time we've
4368 seen the section. */
4369 entry = (struct mips_got_page_entry *) *loc;
4370 if (!entry)
4372 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4373 if (!entry)
4374 return false;
4376 entry->sec = sec;
4377 *loc = entry;
4380 /* Skip over ranges whose maximum extent cannot share a page entry
4381 with ADDEND. */
4382 range_ptr = &entry->ranges;
4383 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4384 range_ptr = &(*range_ptr)->next;
4386 /* If we scanned to the end of the list, or found a range whose
4387 minimum extent cannot share a page entry with ADDEND, create
4388 a new singleton range. */
4389 range = *range_ptr;
4390 if (!range || addend < range->min_addend - 0xffff)
4392 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4393 if (!range)
4394 return false;
4396 range->next = *range_ptr;
4397 range->min_addend = addend;
4398 range->max_addend = addend;
4400 *range_ptr = range;
4401 entry->num_pages++;
4402 g->page_gotno++;
4403 return true;
4406 /* Remember how many pages the old range contributed. */
4407 old_pages = mips_elf_pages_for_range (range);
4409 /* Update the ranges. */
4410 if (addend < range->min_addend)
4411 range->min_addend = addend;
4412 else if (addend > range->max_addend)
4414 if (range->next && addend >= range->next->min_addend - 0xffff)
4416 old_pages += mips_elf_pages_for_range (range->next);
4417 range->max_addend = range->next->max_addend;
4418 range->next = range->next->next;
4420 else
4421 range->max_addend = addend;
4424 /* Record any change in the total estimate. */
4425 new_pages = mips_elf_pages_for_range (range);
4426 if (old_pages != new_pages)
4428 entry->num_pages += new_pages - old_pages;
4429 g->page_gotno += new_pages - old_pages;
4432 return true;
4435 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4436 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4437 whether the page reference described by *REFP needs a GOT page entry,
4438 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4440 static int
4441 mips_elf_resolve_got_page_ref (void **refp, void *data)
4443 struct mips_got_page_ref *ref;
4444 struct mips_elf_traverse_got_arg *arg;
4445 struct mips_elf_link_hash_table *htab;
4446 asection *sec;
4447 bfd_vma addend;
4449 ref = (struct mips_got_page_ref *) *refp;
4450 arg = (struct mips_elf_traverse_got_arg *) data;
4451 htab = mips_elf_hash_table (arg->info);
4453 if (ref->symndx < 0)
4455 struct mips_elf_link_hash_entry *h;
4457 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4458 h = ref->u.h;
4459 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4460 return 1;
4462 /* Ignore undefined symbols; we'll issue an error later if
4463 appropriate. */
4464 if (!((h->root.root.type == bfd_link_hash_defined
4465 || h->root.root.type == bfd_link_hash_defweak)
4466 && h->root.root.u.def.section))
4467 return 1;
4469 sec = h->root.root.u.def.section;
4470 addend = h->root.root.u.def.value + ref->addend;
4472 else
4474 Elf_Internal_Sym *isym;
4476 /* Read in the symbol. */
4477 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4478 ref->symndx);
4479 if (isym == NULL)
4481 arg->g = NULL;
4482 return 0;
4485 /* Get the associated input section. */
4486 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4487 if (sec == NULL)
4489 arg->g = NULL;
4490 return 0;
4493 /* If this is a mergable section, work out the section and offset
4494 of the merged data. For section symbols, the addend specifies
4495 of the offset _of_ the first byte in the data, otherwise it
4496 specifies the offset _from_ the first byte. */
4497 if (sec->flags & SEC_MERGE)
4499 void *secinfo;
4501 secinfo = elf_section_data (sec)->sec_info;
4502 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4503 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4504 isym->st_value + ref->addend);
4505 else
4506 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4507 isym->st_value) + ref->addend;
4509 else
4510 addend = isym->st_value + ref->addend;
4512 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4514 arg->g = NULL;
4515 return 0;
4517 return 1;
4520 /* If any entries in G->got_entries are for indirect or warning symbols,
4521 replace them with entries for the target symbol. Convert g->got_page_refs
4522 into got_page_entry structures and estimate the number of page entries
4523 that they require. */
4525 static bool
4526 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4527 struct mips_got_info *g)
4529 struct mips_elf_traverse_got_arg tga;
4530 struct mips_got_info oldg;
4532 oldg = *g;
4534 tga.info = info;
4535 tga.g = g;
4536 tga.value = false;
4537 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4538 if (tga.value)
4540 *g = oldg;
4541 g->got_entries = htab_create (htab_size (oldg.got_entries),
4542 mips_elf_got_entry_hash,
4543 mips_elf_got_entry_eq, NULL);
4544 if (!g->got_entries)
4545 return false;
4547 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4548 if (!tga.g)
4549 return false;
4551 htab_delete (oldg.got_entries);
4554 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4555 mips_got_page_entry_eq, NULL);
4556 if (g->got_page_entries == NULL)
4557 return false;
4559 tga.info = info;
4560 tga.g = g;
4561 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4563 return true;
4566 /* Return true if a GOT entry for H should live in the local rather than
4567 global GOT area. */
4569 static bool
4570 mips_use_local_got_p (struct bfd_link_info *info,
4571 struct mips_elf_link_hash_entry *h)
4573 /* Symbols that aren't in the dynamic symbol table must live in the
4574 local GOT. This includes symbols that are completely undefined
4575 and which therefore don't bind locally. We'll report undefined
4576 symbols later if appropriate. */
4577 if (h->root.dynindx == -1)
4578 return true;
4580 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4581 to the local GOT, as they would be implicitly relocated by the
4582 base address by the dynamic loader. */
4583 if (bfd_is_abs_symbol (&h->root.root))
4584 return false;
4586 /* Symbols that bind locally can (and in the case of forced-local
4587 symbols, must) live in the local GOT. */
4588 if (h->got_only_for_calls
4589 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4590 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4591 return true;
4593 /* If this is an executable that must provide a definition of the symbol,
4594 either though PLTs or copy relocations, then that address should go in
4595 the local rather than global GOT. */
4596 if (bfd_link_executable (info) && h->has_static_relocs)
4597 return true;
4599 return false;
4602 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4603 link_info structure. Decide whether the hash entry needs an entry in
4604 the global part of the primary GOT, setting global_got_area accordingly.
4605 Count the number of global symbols that are in the primary GOT only
4606 because they have relocations against them (reloc_only_gotno). */
4608 static bool
4609 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4611 struct bfd_link_info *info;
4612 struct mips_elf_link_hash_table *htab;
4613 struct mips_got_info *g;
4615 info = (struct bfd_link_info *) data;
4616 htab = mips_elf_hash_table (info);
4617 g = htab->got_info;
4618 if (h->global_got_area != GGA_NONE)
4620 /* Make a final decision about whether the symbol belongs in the
4621 local or global GOT. */
4622 if (mips_use_local_got_p (info, h))
4623 /* The symbol belongs in the local GOT. We no longer need this
4624 entry if it was only used for relocations; those relocations
4625 will be against the null or section symbol instead of H. */
4626 h->global_got_area = GGA_NONE;
4627 else if (htab->root.target_os == is_vxworks
4628 && h->got_only_for_calls
4629 && h->root.plt.plist->mips_offset != MINUS_ONE)
4630 /* On VxWorks, calls can refer directly to the .got.plt entry;
4631 they don't need entries in the regular GOT. .got.plt entries
4632 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4633 h->global_got_area = GGA_NONE;
4634 else if (h->global_got_area == GGA_RELOC_ONLY)
4636 g->reloc_only_gotno++;
4637 g->global_gotno++;
4640 return 1;
4643 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4644 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4646 static int
4647 mips_elf_add_got_entry (void **entryp, void *data)
4649 struct mips_got_entry *entry;
4650 struct mips_elf_traverse_got_arg *arg;
4651 void **slot;
4653 entry = (struct mips_got_entry *) *entryp;
4654 arg = (struct mips_elf_traverse_got_arg *) data;
4655 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4656 if (!slot)
4658 arg->g = NULL;
4659 return 0;
4661 if (!*slot)
4663 *slot = entry;
4664 mips_elf_count_got_entry (arg->info, arg->g, entry);
4666 return 1;
4669 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4670 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4672 static int
4673 mips_elf_add_got_page_entry (void **entryp, void *data)
4675 struct mips_got_page_entry *entry;
4676 struct mips_elf_traverse_got_arg *arg;
4677 void **slot;
4679 entry = (struct mips_got_page_entry *) *entryp;
4680 arg = (struct mips_elf_traverse_got_arg *) data;
4681 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4682 if (!slot)
4684 arg->g = NULL;
4685 return 0;
4687 if (!*slot)
4689 *slot = entry;
4690 arg->g->page_gotno += entry->num_pages;
4692 return 1;
4695 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4696 this would lead to overflow, 1 if they were merged successfully,
4697 and 0 if a merge failed due to lack of memory. (These values are chosen
4698 so that nonnegative return values can be returned by a htab_traverse
4699 callback.) */
4701 static int
4702 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4703 struct mips_got_info *to,
4704 struct mips_elf_got_per_bfd_arg *arg)
4706 struct mips_elf_traverse_got_arg tga;
4707 unsigned int estimate;
4709 /* Work out how many page entries we would need for the combined GOT. */
4710 estimate = arg->max_pages;
4711 if (estimate >= from->page_gotno + to->page_gotno)
4712 estimate = from->page_gotno + to->page_gotno;
4714 /* And conservatively estimate how many local and TLS entries
4715 would be needed. */
4716 estimate += from->local_gotno + to->local_gotno;
4717 estimate += from->tls_gotno + to->tls_gotno;
4719 /* If we're merging with the primary got, any TLS relocations will
4720 come after the full set of global entries. Otherwise estimate those
4721 conservatively as well. */
4722 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4723 estimate += arg->global_count;
4724 else
4725 estimate += from->global_gotno + to->global_gotno;
4727 /* Bail out if the combined GOT might be too big. */
4728 if (estimate > arg->max_count)
4729 return -1;
4731 /* Transfer the bfd's got information from FROM to TO. */
4732 tga.info = arg->info;
4733 tga.g = to;
4734 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4735 if (!tga.g)
4736 return 0;
4738 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4739 if (!tga.g)
4740 return 0;
4742 mips_elf_replace_bfd_got (abfd, to);
4743 return 1;
4746 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4747 as possible of the primary got, since it doesn't require explicit
4748 dynamic relocations, but don't use bfds that would reference global
4749 symbols out of the addressable range. Failing the primary got,
4750 attempt to merge with the current got, or finish the current got
4751 and then make make the new got current. */
4753 static bool
4754 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4755 struct mips_elf_got_per_bfd_arg *arg)
4757 unsigned int estimate;
4758 int result;
4760 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4761 return false;
4763 /* Work out the number of page, local and TLS entries. */
4764 estimate = arg->max_pages;
4765 if (estimate > g->page_gotno)
4766 estimate = g->page_gotno;
4767 estimate += g->local_gotno + g->tls_gotno;
4769 /* We place TLS GOT entries after both locals and globals. The globals
4770 for the primary GOT may overflow the normal GOT size limit, so be
4771 sure not to merge a GOT which requires TLS with the primary GOT in that
4772 case. This doesn't affect non-primary GOTs. */
4773 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4775 if (estimate <= arg->max_count)
4777 /* If we don't have a primary GOT, use it as
4778 a starting point for the primary GOT. */
4779 if (!arg->primary)
4781 arg->primary = g;
4782 return true;
4785 /* Try merging with the primary GOT. */
4786 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4787 if (result >= 0)
4788 return result;
4791 /* If we can merge with the last-created got, do it. */
4792 if (arg->current)
4794 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4795 if (result >= 0)
4796 return result;
4799 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4800 fits; if it turns out that it doesn't, we'll get relocation
4801 overflows anyway. */
4802 g->next = arg->current;
4803 arg->current = g;
4805 return true;
4808 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4809 to GOTIDX, duplicating the entry if it has already been assigned
4810 an index in a different GOT. */
4812 static bool
4813 mips_elf_set_gotidx (void **entryp, long gotidx)
4815 struct mips_got_entry *entry;
4817 entry = (struct mips_got_entry *) *entryp;
4818 if (entry->gotidx > 0)
4820 struct mips_got_entry *new_entry;
4822 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4823 if (!new_entry)
4824 return false;
4826 *new_entry = *entry;
4827 *entryp = new_entry;
4828 entry = new_entry;
4830 entry->gotidx = gotidx;
4831 return true;
4834 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4835 mips_elf_traverse_got_arg in which DATA->value is the size of one
4836 GOT entry. Set DATA->g to null on failure. */
4838 static int
4839 mips_elf_initialize_tls_index (void **entryp, void *data)
4841 struct mips_got_entry *entry;
4842 struct mips_elf_traverse_got_arg *arg;
4844 /* We're only interested in TLS symbols. */
4845 entry = (struct mips_got_entry *) *entryp;
4846 if (entry->tls_type == GOT_TLS_NONE)
4847 return 1;
4849 arg = (struct mips_elf_traverse_got_arg *) data;
4850 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4852 arg->g = NULL;
4853 return 0;
4856 /* Account for the entries we've just allocated. */
4857 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4858 return 1;
4861 /* A htab_traverse callback for GOT entries, where DATA points to a
4862 mips_elf_traverse_got_arg. Set the global_got_area of each global
4863 symbol to DATA->value. */
4865 static int
4866 mips_elf_set_global_got_area (void **entryp, void *data)
4868 struct mips_got_entry *entry;
4869 struct mips_elf_traverse_got_arg *arg;
4871 entry = (struct mips_got_entry *) *entryp;
4872 arg = (struct mips_elf_traverse_got_arg *) data;
4873 if (entry->abfd != NULL
4874 && entry->symndx == -1
4875 && entry->d.h->global_got_area != GGA_NONE)
4876 entry->d.h->global_got_area = arg->value;
4877 return 1;
4880 /* A htab_traverse callback for secondary GOT entries, where DATA points
4881 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4882 and record the number of relocations they require. DATA->value is
4883 the size of one GOT entry. Set DATA->g to null on failure. */
4885 static int
4886 mips_elf_set_global_gotidx (void **entryp, void *data)
4888 struct mips_got_entry *entry;
4889 struct mips_elf_traverse_got_arg *arg;
4891 entry = (struct mips_got_entry *) *entryp;
4892 arg = (struct mips_elf_traverse_got_arg *) data;
4893 if (entry->abfd != NULL
4894 && entry->symndx == -1
4895 && entry->d.h->global_got_area != GGA_NONE)
4897 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4899 arg->g = NULL;
4900 return 0;
4902 arg->g->assigned_low_gotno += 1;
4904 if (bfd_link_pic (arg->info)
4905 || (elf_hash_table (arg->info)->dynamic_sections_created
4906 && entry->d.h->root.def_dynamic
4907 && !entry->d.h->root.def_regular))
4908 arg->g->relocs += 1;
4911 return 1;
4914 /* A htab_traverse callback for GOT entries for which DATA is the
4915 bfd_link_info. Forbid any global symbols from having traditional
4916 lazy-binding stubs. */
4918 static int
4919 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4921 struct bfd_link_info *info;
4922 struct mips_elf_link_hash_table *htab;
4923 struct mips_got_entry *entry;
4925 entry = (struct mips_got_entry *) *entryp;
4926 info = (struct bfd_link_info *) data;
4927 htab = mips_elf_hash_table (info);
4928 BFD_ASSERT (htab != NULL);
4930 if (entry->abfd != NULL
4931 && entry->symndx == -1
4932 && entry->d.h->needs_lazy_stub)
4934 entry->d.h->needs_lazy_stub = false;
4935 htab->lazy_stub_count--;
4938 return 1;
4941 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4942 the primary GOT. */
4943 static bfd_vma
4944 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4946 if (!g->next)
4947 return 0;
4949 g = mips_elf_bfd_got (ibfd, false);
4950 if (! g)
4951 return 0;
4953 BFD_ASSERT (g->next);
4955 g = g->next;
4957 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4958 * MIPS_ELF_GOT_SIZE (abfd);
4961 /* Turn a single GOT that is too big for 16-bit addressing into
4962 a sequence of GOTs, each one 16-bit addressable. */
4964 static bool
4965 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4966 asection *got, bfd_size_type pages)
4968 struct mips_elf_link_hash_table *htab;
4969 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4970 struct mips_elf_traverse_got_arg tga;
4971 struct mips_got_info *g, *gg;
4972 unsigned int assign, needed_relocs;
4973 bfd *dynobj, *ibfd;
4975 dynobj = elf_hash_table (info)->dynobj;
4976 htab = mips_elf_hash_table (info);
4977 BFD_ASSERT (htab != NULL);
4979 g = htab->got_info;
4981 got_per_bfd_arg.obfd = abfd;
4982 got_per_bfd_arg.info = info;
4983 got_per_bfd_arg.current = NULL;
4984 got_per_bfd_arg.primary = NULL;
4985 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4986 / MIPS_ELF_GOT_SIZE (abfd))
4987 - htab->reserved_gotno);
4988 got_per_bfd_arg.max_pages = pages;
4989 /* The number of globals that will be included in the primary GOT.
4990 See the calls to mips_elf_set_global_got_area below for more
4991 information. */
4992 got_per_bfd_arg.global_count = g->global_gotno;
4994 /* Try to merge the GOTs of input bfds together, as long as they
4995 don't seem to exceed the maximum GOT size, choosing one of them
4996 to be the primary GOT. */
4997 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4999 gg = mips_elf_bfd_got (ibfd, false);
5000 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5001 return false;
5004 /* If we do not find any suitable primary GOT, create an empty one. */
5005 if (got_per_bfd_arg.primary == NULL)
5006 g->next = mips_elf_create_got_info (abfd);
5007 else
5008 g->next = got_per_bfd_arg.primary;
5009 g->next->next = got_per_bfd_arg.current;
5011 /* GG is now the master GOT, and G is the primary GOT. */
5012 gg = g;
5013 g = g->next;
5015 /* Map the output bfd to the primary got. That's what we're going
5016 to use for bfds that use GOT16 or GOT_PAGE relocations that we
5017 didn't mark in check_relocs, and we want a quick way to find it.
5018 We can't just use gg->next because we're going to reverse the
5019 list. */
5020 mips_elf_replace_bfd_got (abfd, g);
5022 /* Every symbol that is referenced in a dynamic relocation must be
5023 present in the primary GOT, so arrange for them to appear after
5024 those that are actually referenced. */
5025 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5026 g->global_gotno = gg->global_gotno;
5028 tga.info = info;
5029 tga.value = GGA_RELOC_ONLY;
5030 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5031 tga.value = GGA_NORMAL;
5032 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5034 /* Now go through the GOTs assigning them offset ranges.
5035 [assigned_low_gotno, local_gotno[ will be set to the range of local
5036 entries in each GOT. We can then compute the end of a GOT by
5037 adding local_gotno to global_gotno. We reverse the list and make
5038 it circular since then we'll be able to quickly compute the
5039 beginning of a GOT, by computing the end of its predecessor. To
5040 avoid special cases for the primary GOT, while still preserving
5041 assertions that are valid for both single- and multi-got links,
5042 we arrange for the main got struct to have the right number of
5043 global entries, but set its local_gotno such that the initial
5044 offset of the primary GOT is zero. Remember that the primary GOT
5045 will become the last item in the circular linked list, so it
5046 points back to the master GOT. */
5047 gg->local_gotno = -g->global_gotno;
5048 gg->global_gotno = g->global_gotno;
5049 gg->tls_gotno = 0;
5050 assign = 0;
5051 gg->next = gg;
5055 struct mips_got_info *gn;
5057 assign += htab->reserved_gotno;
5058 g->assigned_low_gotno = assign;
5059 g->local_gotno += assign;
5060 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5061 g->assigned_high_gotno = g->local_gotno - 1;
5062 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5064 /* Take g out of the direct list, and push it onto the reversed
5065 list that gg points to. g->next is guaranteed to be nonnull after
5066 this operation, as required by mips_elf_initialize_tls_index. */
5067 gn = g->next;
5068 g->next = gg->next;
5069 gg->next = g;
5071 /* Set up any TLS entries. We always place the TLS entries after
5072 all non-TLS entries. */
5073 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5074 tga.g = g;
5075 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5076 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5077 if (!tga.g)
5078 return false;
5079 BFD_ASSERT (g->tls_assigned_gotno == assign);
5081 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
5082 g = gn;
5084 /* Forbid global symbols in every non-primary GOT from having
5085 lazy-binding stubs. */
5086 if (g)
5087 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5089 while (g);
5091 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5093 needed_relocs = 0;
5094 for (g = gg->next; g && g->next != gg; g = g->next)
5096 unsigned int save_assign;
5098 /* Assign offsets to global GOT entries and count how many
5099 relocations they need. */
5100 save_assign = g->assigned_low_gotno;
5101 g->assigned_low_gotno = g->local_gotno;
5102 tga.info = info;
5103 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5104 tga.g = g;
5105 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5106 if (!tga.g)
5107 return false;
5108 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5109 g->assigned_low_gotno = save_assign;
5111 if (bfd_link_pic (info))
5113 g->relocs += g->local_gotno - g->assigned_low_gotno;
5114 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5115 + g->next->global_gotno
5116 + g->next->tls_gotno
5117 + htab->reserved_gotno);
5119 needed_relocs += g->relocs;
5121 needed_relocs += g->relocs;
5123 if (needed_relocs)
5124 mips_elf_allocate_dynamic_relocations (dynobj, info,
5125 needed_relocs);
5127 return true;
5131 /* Returns the first relocation of type r_type found, beginning with
5132 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5134 static const Elf_Internal_Rela *
5135 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5136 const Elf_Internal_Rela *relocation,
5137 const Elf_Internal_Rela *relend)
5139 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5141 while (relocation < relend)
5143 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5144 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5145 return relocation;
5147 ++relocation;
5150 /* We didn't find it. */
5151 return NULL;
5154 /* Return whether an input relocation is against a local symbol. */
5156 static bool
5157 mips_elf_local_relocation_p (bfd *input_bfd,
5158 const Elf_Internal_Rela *relocation,
5159 asection **local_sections)
5161 unsigned long r_symndx;
5162 Elf_Internal_Shdr *symtab_hdr;
5163 size_t extsymoff;
5165 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5166 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5167 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5169 if (r_symndx < extsymoff)
5170 return true;
5171 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5172 return true;
5174 return false;
5177 /* Sign-extend VALUE, which has the indicated number of BITS. */
5179 bfd_vma
5180 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5182 if (value & ((bfd_vma) 1 << (bits - 1)))
5183 /* VALUE is negative. */
5184 value |= ((bfd_vma) - 1) << bits;
5186 return value;
5189 /* Return non-zero if the indicated VALUE has overflowed the maximum
5190 range expressible by a signed number with the indicated number of
5191 BITS. */
5193 static bool
5194 mips_elf_overflow_p (bfd_vma value, int bits)
5196 bfd_signed_vma svalue = (bfd_signed_vma) value;
5198 if (svalue > (1 << (bits - 1)) - 1)
5199 /* The value is too big. */
5200 return true;
5201 else if (svalue < -(1 << (bits - 1)))
5202 /* The value is too small. */
5203 return true;
5205 /* All is well. */
5206 return false;
5209 /* Calculate the %high function. */
5211 static bfd_vma
5212 mips_elf_high (bfd_vma value)
5214 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5217 /* Calculate the %higher function. */
5219 static bfd_vma
5220 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5222 #ifdef BFD64
5223 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5224 #else
5225 abort ();
5226 return MINUS_ONE;
5227 #endif
5230 /* Calculate the %highest function. */
5232 static bfd_vma
5233 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5235 #ifdef BFD64
5236 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5237 #else
5238 abort ();
5239 return MINUS_ONE;
5240 #endif
5243 /* Create the .compact_rel section. */
5245 static bool
5246 mips_elf_create_compact_rel_section
5247 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5249 flagword flags;
5250 register asection *s;
5252 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5254 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5255 | SEC_READONLY);
5257 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5258 if (s == NULL
5259 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5260 return false;
5262 s->size = sizeof (Elf32_External_compact_rel);
5265 return true;
5268 /* Create the .got section to hold the global offset table. */
5270 static bool
5271 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5273 flagword flags;
5274 register asection *s;
5275 struct elf_link_hash_entry *h;
5276 struct bfd_link_hash_entry *bh;
5277 struct mips_elf_link_hash_table *htab;
5279 htab = mips_elf_hash_table (info);
5280 BFD_ASSERT (htab != NULL);
5282 /* This function may be called more than once. */
5283 if (htab->root.sgot)
5284 return true;
5286 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5287 | SEC_LINKER_CREATED);
5289 /* We have to use an alignment of 2**4 here because this is hardcoded
5290 in the function stub generation and in the linker script. */
5291 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5292 if (s == NULL
5293 || !bfd_set_section_alignment (s, 4))
5294 return false;
5295 htab->root.sgot = s;
5297 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5298 linker script because we don't want to define the symbol if we
5299 are not creating a global offset table. */
5300 bh = NULL;
5301 if (! (_bfd_generic_link_add_one_symbol
5302 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5303 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5304 return false;
5306 h = (struct elf_link_hash_entry *) bh;
5307 h->non_elf = 0;
5308 h->def_regular = 1;
5309 h->type = STT_OBJECT;
5310 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5311 elf_hash_table (info)->hgot = h;
5313 if (bfd_link_pic (info)
5314 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5315 return false;
5317 htab->got_info = mips_elf_create_got_info (abfd);
5318 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5319 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5321 /* We also need a .got.plt section when generating PLTs. */
5322 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5323 SEC_ALLOC | SEC_LOAD
5324 | SEC_HAS_CONTENTS
5325 | SEC_IN_MEMORY
5326 | SEC_LINKER_CREATED);
5327 if (s == NULL)
5328 return false;
5329 htab->root.sgotplt = s;
5331 return true;
5334 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5335 __GOTT_INDEX__ symbols. These symbols are only special for
5336 shared objects; they are not used in executables. */
5338 static bool
5339 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5341 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5342 && bfd_link_pic (info)
5343 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5344 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5347 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5348 require an la25 stub. See also mips_elf_local_pic_function_p,
5349 which determines whether the destination function ever requires a
5350 stub. */
5352 static bool
5353 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5354 bool target_is_16_bit_code_p)
5356 /* We specifically ignore branches and jumps from EF_PIC objects,
5357 where the onus is on the compiler or programmer to perform any
5358 necessary initialization of $25. Sometimes such initialization
5359 is unnecessary; for example, -mno-shared functions do not use
5360 the incoming value of $25, and may therefore be called directly. */
5361 if (PIC_OBJECT_P (input_bfd))
5362 return false;
5364 switch (r_type)
5366 case R_MIPS_26:
5367 case R_MIPS_PC16:
5368 case R_MIPS_PC21_S2:
5369 case R_MIPS_PC26_S2:
5370 case R_MICROMIPS_26_S1:
5371 case R_MICROMIPS_PC7_S1:
5372 case R_MICROMIPS_PC10_S1:
5373 case R_MICROMIPS_PC16_S1:
5374 case R_MICROMIPS_PC23_S2:
5375 return true;
5377 case R_MIPS16_26:
5378 return !target_is_16_bit_code_p;
5380 default:
5381 return false;
5385 /* Obtain the field relocated by RELOCATION. */
5387 static bfd_vma
5388 mips_elf_obtain_contents (reloc_howto_type *howto,
5389 const Elf_Internal_Rela *relocation,
5390 bfd *input_bfd, bfd_byte *contents)
5392 bfd_vma x = 0;
5393 bfd_byte *location = contents + relocation->r_offset;
5394 unsigned int size = bfd_get_reloc_size (howto);
5396 /* Obtain the bytes. */
5397 if (size != 0)
5398 x = bfd_get (8 * size, input_bfd, location);
5400 return x;
5403 /* Store the field relocated by RELOCATION. */
5405 static void
5406 mips_elf_store_contents (reloc_howto_type *howto,
5407 const Elf_Internal_Rela *relocation,
5408 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5410 bfd_byte *location = contents + relocation->r_offset;
5411 unsigned int size = bfd_get_reloc_size (howto);
5413 /* Put the value into the output. */
5414 if (size != 0)
5415 bfd_put (8 * size, input_bfd, x, location);
5418 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5419 RELOCATION described by HOWTO, with a move of 0 to the load target
5420 register, returning TRUE if that is successful and FALSE otherwise.
5421 If DOIT is FALSE, then only determine it patching is possible and
5422 return status without actually changing CONTENTS.
5425 static bool
5426 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5427 const Elf_Internal_Rela *relocation,
5428 reloc_howto_type *howto, bool doit)
5430 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5431 bfd_byte *location = contents + relocation->r_offset;
5432 bool nullified = true;
5433 bfd_vma x;
5435 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5437 /* Obtain the current value. */
5438 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5440 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5441 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5442 if (mips16_reloc_p (r_type)
5443 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5444 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5445 x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
5446 else if (micromips_reloc_p (r_type)
5447 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5448 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5449 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5450 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5451 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5452 else
5453 nullified = false;
5455 /* Put the value into the output. */
5456 if (doit && nullified)
5457 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5459 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5461 return nullified;
5464 /* Calculate the value produced by the RELOCATION (which comes from
5465 the INPUT_BFD). The ADDEND is the addend to use for this
5466 RELOCATION; RELOCATION->R_ADDEND is ignored.
5468 The result of the relocation calculation is stored in VALUEP.
5469 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5470 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5472 This function returns bfd_reloc_continue if the caller need take no
5473 further action regarding this relocation, bfd_reloc_notsupported if
5474 something goes dramatically wrong, bfd_reloc_overflow if an
5475 overflow occurs, and bfd_reloc_ok to indicate success. */
5477 static bfd_reloc_status_type
5478 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5479 asection *input_section, bfd_byte *contents,
5480 struct bfd_link_info *info,
5481 const Elf_Internal_Rela *relocation,
5482 bfd_vma addend, reloc_howto_type *howto,
5483 Elf_Internal_Sym *local_syms,
5484 asection **local_sections, bfd_vma *valuep,
5485 const char **namep,
5486 bool *cross_mode_jump_p,
5487 bool save_addend)
5489 /* The eventual value we will return. */
5490 bfd_vma value;
5491 /* The address of the symbol against which the relocation is
5492 occurring. */
5493 bfd_vma symbol = 0;
5494 /* The final GP value to be used for the relocatable, executable, or
5495 shared object file being produced. */
5496 bfd_vma gp;
5497 /* The place (section offset or address) of the storage unit being
5498 relocated. */
5499 bfd_vma p;
5500 /* The value of GP used to create the relocatable object. */
5501 bfd_vma gp0;
5502 /* The offset into the global offset table at which the address of
5503 the relocation entry symbol, adjusted by the addend, resides
5504 during execution. */
5505 bfd_vma g = MINUS_ONE;
5506 /* The section in which the symbol referenced by the relocation is
5507 located. */
5508 asection *sec = NULL;
5509 struct mips_elf_link_hash_entry *h = NULL;
5510 /* TRUE if the symbol referred to by this relocation is a local
5511 symbol. */
5512 bool local_p, was_local_p;
5513 /* TRUE if the symbol referred to by this relocation is a section
5514 symbol. */
5515 bool section_p = false;
5516 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5517 bool gp_disp_p = false;
5518 /* TRUE if the symbol referred to by this relocation is
5519 "__gnu_local_gp". */
5520 bool gnu_local_gp_p = false;
5521 Elf_Internal_Shdr *symtab_hdr;
5522 size_t extsymoff;
5523 unsigned long r_symndx;
5524 int r_type;
5525 /* TRUE if overflow occurred during the calculation of the
5526 relocation value. */
5527 bool overflowed_p;
5528 /* TRUE if this relocation refers to a MIPS16 function. */
5529 bool target_is_16_bit_code_p = false;
5530 bool target_is_micromips_code_p = false;
5531 struct mips_elf_link_hash_table *htab;
5532 bfd *dynobj;
5533 bool resolved_to_zero;
5535 dynobj = elf_hash_table (info)->dynobj;
5536 htab = mips_elf_hash_table (info);
5537 BFD_ASSERT (htab != NULL);
5539 /* Parse the relocation. */
5540 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5541 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5542 p = (input_section->output_section->vma
5543 + input_section->output_offset
5544 + relocation->r_offset);
5546 /* Assume that there will be no overflow. */
5547 overflowed_p = false;
5549 /* Figure out whether or not the symbol is local, and get the offset
5550 used in the array of hash table entries. */
5551 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5552 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5553 local_sections);
5554 was_local_p = local_p;
5555 if (! elf_bad_symtab (input_bfd))
5556 extsymoff = symtab_hdr->sh_info;
5557 else
5559 /* The symbol table does not follow the rule that local symbols
5560 must come before globals. */
5561 extsymoff = 0;
5564 /* Figure out the value of the symbol. */
5565 if (local_p)
5567 bool micromips_p = MICROMIPS_P (abfd);
5568 Elf_Internal_Sym *sym;
5570 sym = local_syms + r_symndx;
5571 sec = local_sections[r_symndx];
5573 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5575 symbol = sec->output_section->vma + sec->output_offset;
5576 if (!section_p || (sec->flags & SEC_MERGE))
5577 symbol += sym->st_value;
5578 if ((sec->flags & SEC_MERGE) && section_p)
5580 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5581 addend -= symbol;
5582 addend += sec->output_section->vma + sec->output_offset;
5585 /* MIPS16/microMIPS text labels should be treated as odd. */
5586 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5587 ++symbol;
5589 /* Record the name of this symbol, for our caller. */
5590 *namep = bfd_elf_string_from_elf_section (input_bfd,
5591 symtab_hdr->sh_link,
5592 sym->st_name);
5593 if (*namep == NULL || **namep == '\0')
5594 *namep = bfd_section_name (sec);
5596 /* For relocations against a section symbol and ones against no
5597 symbol (absolute relocations) infer the ISA mode from the addend. */
5598 if (section_p || r_symndx == STN_UNDEF)
5600 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5601 target_is_micromips_code_p = (addend & 1) && micromips_p;
5603 /* For relocations against an absolute symbol infer the ISA mode
5604 from the value of the symbol plus addend. */
5605 else if (bfd_is_abs_section (sec))
5607 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5608 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5610 /* Otherwise just use the regular symbol annotation available. */
5611 else
5613 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5614 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5617 else
5619 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5621 /* For global symbols we look up the symbol in the hash-table. */
5622 h = ((struct mips_elf_link_hash_entry *)
5623 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5624 /* Find the real hash-table entry for this symbol. */
5625 while (h->root.root.type == bfd_link_hash_indirect
5626 || h->root.root.type == bfd_link_hash_warning)
5627 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5629 /* Record the name of this symbol, for our caller. */
5630 *namep = h->root.root.root.string;
5632 /* See if this is the special _gp_disp symbol. Note that such a
5633 symbol must always be a global symbol. */
5634 if (strcmp (*namep, "_gp_disp") == 0
5635 && ! NEWABI_P (input_bfd))
5637 /* Relocations against _gp_disp are permitted only with
5638 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5639 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5640 return bfd_reloc_notsupported;
5642 gp_disp_p = true;
5644 /* See if this is the special _gp symbol. Note that such a
5645 symbol must always be a global symbol. */
5646 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5647 gnu_local_gp_p = true;
5650 /* If this symbol is defined, calculate its address. Note that
5651 _gp_disp is a magic symbol, always implicitly defined by the
5652 linker, so it's inappropriate to check to see whether or not
5653 its defined. */
5654 else if ((h->root.root.type == bfd_link_hash_defined
5655 || h->root.root.type == bfd_link_hash_defweak)
5656 && h->root.root.u.def.section)
5658 sec = h->root.root.u.def.section;
5659 if (sec->output_section)
5660 symbol = (h->root.root.u.def.value
5661 + sec->output_section->vma
5662 + sec->output_offset);
5663 else
5664 symbol = h->root.root.u.def.value;
5666 else if (h->root.root.type == bfd_link_hash_undefweak)
5667 /* We allow relocations against undefined weak symbols, giving
5668 it the value zero, so that you can undefined weak functions
5669 and check to see if they exist by looking at their
5670 addresses. */
5671 symbol = 0;
5672 else if (info->unresolved_syms_in_objects == RM_IGNORE
5673 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5674 symbol = 0;
5675 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5676 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5678 /* If this is a dynamic link, we should have created a
5679 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5680 in _bfd_mips_elf_create_dynamic_sections.
5681 Otherwise, we should define the symbol with a value of 0.
5682 FIXME: It should probably get into the symbol table
5683 somehow as well. */
5684 BFD_ASSERT (! bfd_link_pic (info));
5685 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5686 symbol = 0;
5688 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5690 /* This is an optional symbol - an Irix specific extension to the
5691 ELF spec. Ignore it for now.
5692 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5693 than simply ignoring them, but we do not handle this for now.
5694 For information see the "64-bit ELF Object File Specification"
5695 which is available from here:
5696 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5697 symbol = 0;
5699 else
5701 bool reject_undefined
5702 = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5703 && !info->warn_unresolved_syms)
5704 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5706 info->callbacks->undefined_symbol
5707 (info, h->root.root.root.string, input_bfd,
5708 input_section, relocation->r_offset, reject_undefined);
5710 if (reject_undefined)
5711 return bfd_reloc_undefined;
5713 symbol = 0;
5716 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5717 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5720 /* If this is a reference to a 16-bit function with a stub, we need
5721 to redirect the relocation to the stub unless:
5723 (a) the relocation is for a MIPS16 JAL;
5725 (b) the relocation is for a MIPS16 PIC call, and there are no
5726 non-MIPS16 uses of the GOT slot; or
5728 (c) the section allows direct references to MIPS16 functions. */
5729 if (r_type != R_MIPS16_26
5730 && !bfd_link_relocatable (info)
5731 && ((h != NULL
5732 && h->fn_stub != NULL
5733 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5734 || (local_p
5735 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5736 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5737 && !section_allows_mips16_refs_p (input_section))
5739 /* This is a 32- or 64-bit call to a 16-bit function. We should
5740 have already noticed that we were going to need the
5741 stub. */
5742 if (local_p)
5744 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5745 value = 0;
5747 else
5749 BFD_ASSERT (h->need_fn_stub);
5750 if (h->la25_stub)
5752 /* If a LA25 header for the stub itself exists, point to the
5753 prepended LUI/ADDIU sequence. */
5754 sec = h->la25_stub->stub_section;
5755 value = h->la25_stub->offset;
5757 else
5759 sec = h->fn_stub;
5760 value = 0;
5764 symbol = sec->output_section->vma + sec->output_offset + value;
5765 /* The target is 16-bit, but the stub isn't. */
5766 target_is_16_bit_code_p = false;
5768 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5769 to a standard MIPS function, we need to redirect the call to the stub.
5770 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5771 indirect calls should use an indirect stub instead. */
5772 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5773 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5774 || (local_p
5775 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5776 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5777 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5779 if (local_p)
5780 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5781 else
5783 /* If both call_stub and call_fp_stub are defined, we can figure
5784 out which one to use by checking which one appears in the input
5785 file. */
5786 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5788 asection *o;
5790 sec = NULL;
5791 for (o = input_bfd->sections; o != NULL; o = o->next)
5793 if (CALL_FP_STUB_P (bfd_section_name (o)))
5795 sec = h->call_fp_stub;
5796 break;
5799 if (sec == NULL)
5800 sec = h->call_stub;
5802 else if (h->call_stub != NULL)
5803 sec = h->call_stub;
5804 else
5805 sec = h->call_fp_stub;
5808 BFD_ASSERT (sec->size > 0);
5809 symbol = sec->output_section->vma + sec->output_offset;
5811 /* If this is a direct call to a PIC function, redirect to the
5812 non-PIC stub. */
5813 else if (h != NULL && h->la25_stub
5814 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5815 target_is_16_bit_code_p))
5817 symbol = (h->la25_stub->stub_section->output_section->vma
5818 + h->la25_stub->stub_section->output_offset
5819 + h->la25_stub->offset);
5820 if (ELF_ST_IS_MICROMIPS (h->root.other))
5821 symbol |= 1;
5823 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5824 entry is used if a standard PLT entry has also been made. In this
5825 case the symbol will have been set by mips_elf_set_plt_sym_value
5826 to point to the standard PLT entry, so redirect to the compressed
5827 one. */
5828 else if ((mips16_branch_reloc_p (r_type)
5829 || micromips_branch_reloc_p (r_type))
5830 && !bfd_link_relocatable (info)
5831 && h != NULL
5832 && h->use_plt_entry
5833 && h->root.plt.plist->comp_offset != MINUS_ONE
5834 && h->root.plt.plist->mips_offset != MINUS_ONE)
5836 bool micromips_p = MICROMIPS_P (abfd);
5838 sec = htab->root.splt;
5839 symbol = (sec->output_section->vma
5840 + sec->output_offset
5841 + htab->plt_header_size
5842 + htab->plt_mips_offset
5843 + h->root.plt.plist->comp_offset
5844 + 1);
5846 target_is_16_bit_code_p = !micromips_p;
5847 target_is_micromips_code_p = micromips_p;
5850 /* Make sure MIPS16 and microMIPS are not used together. */
5851 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5852 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5854 _bfd_error_handler
5855 (_("MIPS16 and microMIPS functions cannot call each other"));
5856 return bfd_reloc_notsupported;
5859 /* Calls from 16-bit code to 32-bit code and vice versa require the
5860 mode change. However, we can ignore calls to undefined weak symbols,
5861 which should never be executed at runtime. This exception is important
5862 because the assembly writer may have "known" that any definition of the
5863 symbol would be 16-bit code, and that direct jumps were therefore
5864 acceptable. */
5865 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5866 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5867 && ((mips16_branch_reloc_p (r_type)
5868 && !target_is_16_bit_code_p)
5869 || (micromips_branch_reloc_p (r_type)
5870 && !target_is_micromips_code_p)
5871 || ((branch_reloc_p (r_type)
5872 || r_type == R_MIPS_JALR)
5873 && (target_is_16_bit_code_p
5874 || target_is_micromips_code_p))));
5876 resolved_to_zero = (h != NULL
5877 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5879 switch (r_type)
5881 case R_MIPS16_CALL16:
5882 case R_MIPS16_GOT16:
5883 case R_MIPS_CALL16:
5884 case R_MIPS_GOT16:
5885 case R_MIPS_GOT_PAGE:
5886 case R_MIPS_GOT_DISP:
5887 case R_MIPS_GOT_LO16:
5888 case R_MIPS_CALL_LO16:
5889 case R_MICROMIPS_CALL16:
5890 case R_MICROMIPS_GOT16:
5891 case R_MICROMIPS_GOT_PAGE:
5892 case R_MICROMIPS_GOT_DISP:
5893 case R_MICROMIPS_GOT_LO16:
5894 case R_MICROMIPS_CALL_LO16:
5895 if (resolved_to_zero
5896 && !bfd_link_relocatable (info)
5897 && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5898 relocation->r_offset)
5899 && mips_elf_nullify_got_load (input_bfd, contents,
5900 relocation, howto, true))
5901 return bfd_reloc_continue;
5903 /* Fall through. */
5904 case R_MIPS_GOT_HI16:
5905 case R_MIPS_CALL_HI16:
5906 case R_MICROMIPS_GOT_HI16:
5907 case R_MICROMIPS_CALL_HI16:
5908 if (resolved_to_zero
5909 && htab->use_absolute_zero
5910 && bfd_link_pic (info))
5912 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5913 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5914 false, false, false);
5915 BFD_ASSERT (h != NULL);
5917 break;
5920 local_p = (h == NULL || mips_use_local_got_p (info, h));
5922 gp0 = _bfd_get_gp_value (input_bfd);
5923 gp = _bfd_get_gp_value (abfd);
5924 if (htab->got_info)
5925 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5927 if (gnu_local_gp_p)
5928 symbol = gp;
5930 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5931 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5932 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5933 if (got_page_reloc_p (r_type) && !local_p)
5935 r_type = (micromips_reloc_p (r_type)
5936 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5937 addend = 0;
5940 /* If we haven't already determined the GOT offset, and we're going
5941 to need it, get it now. */
5942 switch (r_type)
5944 case R_MIPS16_CALL16:
5945 case R_MIPS16_GOT16:
5946 case R_MIPS_CALL16:
5947 case R_MIPS_GOT16:
5948 case R_MIPS_GOT_DISP:
5949 case R_MIPS_GOT_HI16:
5950 case R_MIPS_CALL_HI16:
5951 case R_MIPS_GOT_LO16:
5952 case R_MIPS_CALL_LO16:
5953 case R_MICROMIPS_CALL16:
5954 case R_MICROMIPS_GOT16:
5955 case R_MICROMIPS_GOT_DISP:
5956 case R_MICROMIPS_GOT_HI16:
5957 case R_MICROMIPS_CALL_HI16:
5958 case R_MICROMIPS_GOT_LO16:
5959 case R_MICROMIPS_CALL_LO16:
5960 case R_MIPS_TLS_GD:
5961 case R_MIPS_TLS_GOTTPREL:
5962 case R_MIPS_TLS_LDM:
5963 case R_MIPS16_TLS_GD:
5964 case R_MIPS16_TLS_GOTTPREL:
5965 case R_MIPS16_TLS_LDM:
5966 case R_MICROMIPS_TLS_GD:
5967 case R_MICROMIPS_TLS_GOTTPREL:
5968 case R_MICROMIPS_TLS_LDM:
5969 /* Find the index into the GOT where this value is located. */
5970 if (tls_ldm_reloc_p (r_type))
5972 g = mips_elf_local_got_index (abfd, input_bfd, info,
5973 0, 0, NULL, r_type);
5974 if (g == MINUS_ONE)
5975 return bfd_reloc_outofrange;
5977 else if (!local_p)
5979 /* On VxWorks, CALL relocations should refer to the .got.plt
5980 entry, which is initialized to point at the PLT stub. */
5981 if (htab->root.target_os == is_vxworks
5982 && (call_hi16_reloc_p (r_type)
5983 || call_lo16_reloc_p (r_type)
5984 || call16_reloc_p (r_type)))
5986 BFD_ASSERT (addend == 0);
5987 BFD_ASSERT (h->root.needs_plt);
5988 g = mips_elf_gotplt_index (info, &h->root);
5990 else
5992 BFD_ASSERT (addend == 0);
5993 g = mips_elf_global_got_index (abfd, info, input_bfd,
5994 &h->root, r_type);
5995 if (!TLS_RELOC_P (r_type)
5996 && !elf_hash_table (info)->dynamic_sections_created)
5997 /* This is a static link. We must initialize the GOT entry. */
5998 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
6001 else if (htab->root.target_os != is_vxworks
6002 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6003 /* The calculation below does not involve "g". */
6004 break;
6005 else
6007 g = mips_elf_local_got_index (abfd, input_bfd, info,
6008 symbol + addend, r_symndx, h, r_type);
6009 if (g == MINUS_ONE)
6010 return bfd_reloc_outofrange;
6013 /* Convert GOT indices to actual offsets. */
6014 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6015 break;
6018 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6019 symbols are resolved by the loader. Add them to .rela.dyn. */
6020 if (h != NULL && is_gott_symbol (info, &h->root))
6022 Elf_Internal_Rela outrel;
6023 bfd_byte *loc;
6024 asection *s;
6026 s = mips_elf_rel_dyn_section (info, false);
6027 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6029 outrel.r_offset = (input_section->output_section->vma
6030 + input_section->output_offset
6031 + relocation->r_offset);
6032 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6033 outrel.r_addend = addend;
6034 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6036 /* If we've written this relocation for a readonly section,
6037 we need to set DF_TEXTREL again, so that we do not delete the
6038 DT_TEXTREL tag. */
6039 if (MIPS_ELF_READONLY_SECTION (input_section))
6040 info->flags |= DF_TEXTREL;
6042 *valuep = 0;
6043 return bfd_reloc_ok;
6046 /* Figure out what kind of relocation is being performed. */
6047 switch (r_type)
6049 case R_MIPS_NONE:
6050 return bfd_reloc_continue;
6052 case R_MIPS_16:
6053 if (howto->partial_inplace)
6054 addend = _bfd_mips_elf_sign_extend (addend, 16);
6055 value = symbol + addend;
6056 overflowed_p = mips_elf_overflow_p (value, 16);
6057 break;
6059 case R_MIPS_32:
6060 case R_MIPS_REL32:
6061 case R_MIPS_64:
6062 if ((bfd_link_pic (info)
6063 || (htab->root.dynamic_sections_created
6064 && h != NULL
6065 && h->root.def_dynamic
6066 && !h->root.def_regular
6067 && !h->has_static_relocs))
6068 && r_symndx != STN_UNDEF
6069 && (h == NULL
6070 || h->root.root.type != bfd_link_hash_undefweak
6071 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6072 && !resolved_to_zero))
6073 && (input_section->flags & SEC_ALLOC) != 0)
6075 /* If we're creating a shared library, then we can't know
6076 where the symbol will end up. So, we create a relocation
6077 record in the output, and leave the job up to the dynamic
6078 linker. We must do the same for executable references to
6079 shared library symbols, unless we've decided to use copy
6080 relocs or PLTs instead. */
6081 value = addend;
6082 if (!mips_elf_create_dynamic_relocation (abfd,
6083 info,
6084 relocation,
6086 sec,
6087 symbol,
6088 &value,
6089 input_section))
6090 return bfd_reloc_undefined;
6092 else
6094 if (r_type != R_MIPS_REL32)
6095 value = symbol + addend;
6096 else
6097 value = addend;
6099 value &= howto->dst_mask;
6100 break;
6102 case R_MIPS_PC32:
6103 value = symbol + addend - p;
6104 value &= howto->dst_mask;
6105 break;
6107 case R_MIPS16_26:
6108 /* The calculation for R_MIPS16_26 is just the same as for an
6109 R_MIPS_26. It's only the storage of the relocated field into
6110 the output file that's different. That's handled in
6111 mips_elf_perform_relocation. So, we just fall through to the
6112 R_MIPS_26 case here. */
6113 case R_MIPS_26:
6114 case R_MICROMIPS_26_S1:
6116 unsigned int shift;
6118 /* Shift is 2, unusually, for microMIPS JALX. */
6119 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6121 if (howto->partial_inplace && !section_p)
6122 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6123 else
6124 value = addend;
6125 value += symbol;
6127 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6128 be the correct ISA mode selector except for weak undefined
6129 symbols. */
6130 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6131 && (*cross_mode_jump_p
6132 ? (value & 3) != (r_type == R_MIPS_26)
6133 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6134 return bfd_reloc_outofrange;
6136 value >>= shift;
6137 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6138 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6139 value &= howto->dst_mask;
6141 break;
6143 case R_MIPS_TLS_DTPREL_HI16:
6144 case R_MIPS16_TLS_DTPREL_HI16:
6145 case R_MICROMIPS_TLS_DTPREL_HI16:
6146 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6147 & howto->dst_mask);
6148 break;
6150 case R_MIPS_TLS_DTPREL_LO16:
6151 case R_MIPS_TLS_DTPREL32:
6152 case R_MIPS_TLS_DTPREL64:
6153 case R_MIPS16_TLS_DTPREL_LO16:
6154 case R_MICROMIPS_TLS_DTPREL_LO16:
6155 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6156 break;
6158 case R_MIPS_TLS_TPREL_HI16:
6159 case R_MIPS16_TLS_TPREL_HI16:
6160 case R_MICROMIPS_TLS_TPREL_HI16:
6161 value = (mips_elf_high (addend + symbol - tprel_base (info))
6162 & howto->dst_mask);
6163 break;
6165 case R_MIPS_TLS_TPREL_LO16:
6166 case R_MIPS_TLS_TPREL32:
6167 case R_MIPS_TLS_TPREL64:
6168 case R_MIPS16_TLS_TPREL_LO16:
6169 case R_MICROMIPS_TLS_TPREL_LO16:
6170 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6171 break;
6173 case R_MIPS_HI16:
6174 case R_MIPS16_HI16:
6175 case R_MICROMIPS_HI16:
6176 if (!gp_disp_p)
6178 value = mips_elf_high (addend + symbol);
6179 value &= howto->dst_mask;
6181 else
6183 /* For MIPS16 ABI code we generate this sequence
6184 0: li $v0,%hi(_gp_disp)
6185 4: addiupc $v1,%lo(_gp_disp)
6186 8: sll $v0,16
6187 12: addu $v0,$v1
6188 14: move $gp,$v0
6189 So the offsets of hi and lo relocs are the same, but the
6190 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6191 ADDIUPC clears the low two bits of the instruction address,
6192 so the base is ($t9 + 4) & ~3. */
6193 if (r_type == R_MIPS16_HI16)
6194 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6195 /* The microMIPS .cpload sequence uses the same assembly
6196 instructions as the traditional psABI version, but the
6197 incoming $t9 has the low bit set. */
6198 else if (r_type == R_MICROMIPS_HI16)
6199 value = mips_elf_high (addend + gp - p - 1);
6200 else
6201 value = mips_elf_high (addend + gp - p);
6203 break;
6205 case R_MIPS_LO16:
6206 case R_MIPS16_LO16:
6207 case R_MICROMIPS_LO16:
6208 case R_MICROMIPS_HI0_LO16:
6209 if (!gp_disp_p)
6210 value = (symbol + addend) & howto->dst_mask;
6211 else
6213 /* See the comment for R_MIPS16_HI16 above for the reason
6214 for this conditional. */
6215 if (r_type == R_MIPS16_LO16)
6216 value = addend + gp - (p & ~(bfd_vma) 0x3);
6217 else if (r_type == R_MICROMIPS_LO16
6218 || r_type == R_MICROMIPS_HI0_LO16)
6219 value = addend + gp - p + 3;
6220 else
6221 value = addend + gp - p + 4;
6222 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6223 for overflow. But, on, say, IRIX5, relocations against
6224 _gp_disp are normally generated from the .cpload
6225 pseudo-op. It generates code that normally looks like
6226 this:
6228 lui $gp,%hi(_gp_disp)
6229 addiu $gp,$gp,%lo(_gp_disp)
6230 addu $gp,$gp,$t9
6232 Here $t9 holds the address of the function being called,
6233 as required by the MIPS ELF ABI. The R_MIPS_LO16
6234 relocation can easily overflow in this situation, but the
6235 R_MIPS_HI16 relocation will handle the overflow.
6236 Therefore, we consider this a bug in the MIPS ABI, and do
6237 not check for overflow here. */
6239 break;
6241 case R_MIPS_LITERAL:
6242 case R_MICROMIPS_LITERAL:
6243 /* Because we don't merge literal sections, we can handle this
6244 just like R_MIPS_GPREL16. In the long run, we should merge
6245 shared literals, and then we will need to additional work
6246 here. */
6248 /* Fall through. */
6250 case R_MIPS16_GPREL:
6251 /* The R_MIPS16_GPREL performs the same calculation as
6252 R_MIPS_GPREL16, but stores the relocated bits in a different
6253 order. We don't need to do anything special here; the
6254 differences are handled in mips_elf_perform_relocation. */
6255 case R_MIPS_GPREL16:
6256 case R_MICROMIPS_GPREL7_S2:
6257 case R_MICROMIPS_GPREL16:
6258 /* Only sign-extend the addend if it was extracted from the
6259 instruction. If the addend was separate, leave it alone,
6260 otherwise we may lose significant bits. */
6261 if (howto->partial_inplace)
6262 addend = _bfd_mips_elf_sign_extend (addend, 16);
6263 value = symbol + addend - gp;
6264 /* If the symbol was local, any earlier relocatable links will
6265 have adjusted its addend with the gp offset, so compensate
6266 for that now. Don't do it for symbols forced local in this
6267 link, though, since they won't have had the gp offset applied
6268 to them before. */
6269 if (was_local_p)
6270 value += gp0;
6271 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6272 overflowed_p = mips_elf_overflow_p (value, 16);
6273 break;
6275 case R_MIPS16_GOT16:
6276 case R_MIPS16_CALL16:
6277 case R_MIPS_GOT16:
6278 case R_MIPS_CALL16:
6279 case R_MICROMIPS_GOT16:
6280 case R_MICROMIPS_CALL16:
6281 /* VxWorks does not have separate local and global semantics for
6282 R_MIPS*_GOT16; every relocation evaluates to "G". */
6283 if (htab->root.target_os != is_vxworks && local_p)
6285 value = mips_elf_got16_entry (abfd, input_bfd, info,
6286 symbol + addend, !was_local_p);
6287 if (value == MINUS_ONE)
6288 return bfd_reloc_outofrange;
6289 value
6290 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6291 overflowed_p = mips_elf_overflow_p (value, 16);
6292 break;
6295 /* Fall through. */
6297 case R_MIPS_TLS_GD:
6298 case R_MIPS_TLS_GOTTPREL:
6299 case R_MIPS_TLS_LDM:
6300 case R_MIPS_GOT_DISP:
6301 case R_MIPS16_TLS_GD:
6302 case R_MIPS16_TLS_GOTTPREL:
6303 case R_MIPS16_TLS_LDM:
6304 case R_MICROMIPS_TLS_GD:
6305 case R_MICROMIPS_TLS_GOTTPREL:
6306 case R_MICROMIPS_TLS_LDM:
6307 case R_MICROMIPS_GOT_DISP:
6308 value = g;
6309 overflowed_p = mips_elf_overflow_p (value, 16);
6310 break;
6312 case R_MIPS_GPREL32:
6313 value = (addend + symbol + gp0 - gp);
6314 if (!save_addend)
6315 value &= howto->dst_mask;
6316 break;
6318 case R_MIPS_PC16:
6319 case R_MIPS_GNU_REL16_S2:
6320 if (howto->partial_inplace)
6321 addend = _bfd_mips_elf_sign_extend (addend, 18);
6323 /* No need to exclude weak undefined symbols here as they resolve
6324 to 0 and never set `*cross_mode_jump_p', so this alignment check
6325 will never trigger for them. */
6326 if (*cross_mode_jump_p
6327 ? ((symbol + addend) & 3) != 1
6328 : ((symbol + addend) & 3) != 0)
6329 return bfd_reloc_outofrange;
6331 value = symbol + addend - p;
6332 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6333 overflowed_p = mips_elf_overflow_p (value, 18);
6334 value >>= howto->rightshift;
6335 value &= howto->dst_mask;
6336 break;
6338 case R_MIPS16_PC16_S1:
6339 if (howto->partial_inplace)
6340 addend = _bfd_mips_elf_sign_extend (addend, 17);
6342 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6343 && (*cross_mode_jump_p
6344 ? ((symbol + addend) & 3) != 0
6345 : ((symbol + addend) & 1) == 0))
6346 return bfd_reloc_outofrange;
6348 value = symbol + addend - p;
6349 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6350 overflowed_p = mips_elf_overflow_p (value, 17);
6351 value >>= howto->rightshift;
6352 value &= howto->dst_mask;
6353 break;
6355 case R_MIPS_PC21_S2:
6356 if (howto->partial_inplace)
6357 addend = _bfd_mips_elf_sign_extend (addend, 23);
6359 if ((symbol + addend) & 3)
6360 return bfd_reloc_outofrange;
6362 value = symbol + addend - p;
6363 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6364 overflowed_p = mips_elf_overflow_p (value, 23);
6365 value >>= howto->rightshift;
6366 value &= howto->dst_mask;
6367 break;
6369 case R_MIPS_PC26_S2:
6370 if (howto->partial_inplace)
6371 addend = _bfd_mips_elf_sign_extend (addend, 28);
6373 if ((symbol + addend) & 3)
6374 return bfd_reloc_outofrange;
6376 value = symbol + addend - p;
6377 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6378 overflowed_p = mips_elf_overflow_p (value, 28);
6379 value >>= howto->rightshift;
6380 value &= howto->dst_mask;
6381 break;
6383 case R_MIPS_PC18_S3:
6384 if (howto->partial_inplace)
6385 addend = _bfd_mips_elf_sign_extend (addend, 21);
6387 if ((symbol + addend) & 7)
6388 return bfd_reloc_outofrange;
6390 value = symbol + addend - ((p | 7) ^ 7);
6391 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6392 overflowed_p = mips_elf_overflow_p (value, 21);
6393 value >>= howto->rightshift;
6394 value &= howto->dst_mask;
6395 break;
6397 case R_MIPS_PC19_S2:
6398 if (howto->partial_inplace)
6399 addend = _bfd_mips_elf_sign_extend (addend, 21);
6401 if ((symbol + addend) & 3)
6402 return bfd_reloc_outofrange;
6404 value = symbol + addend - p;
6405 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6406 overflowed_p = mips_elf_overflow_p (value, 21);
6407 value >>= howto->rightshift;
6408 value &= howto->dst_mask;
6409 break;
6411 case R_MIPS_PCHI16:
6412 value = mips_elf_high (symbol + addend - p);
6413 value &= howto->dst_mask;
6414 break;
6416 case R_MIPS_PCLO16:
6417 if (howto->partial_inplace)
6418 addend = _bfd_mips_elf_sign_extend (addend, 16);
6419 value = symbol + addend - p;
6420 value &= howto->dst_mask;
6421 break;
6423 case R_MICROMIPS_PC7_S1:
6424 if (howto->partial_inplace)
6425 addend = _bfd_mips_elf_sign_extend (addend, 8);
6427 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6428 && (*cross_mode_jump_p
6429 ? ((symbol + addend + 2) & 3) != 0
6430 : ((symbol + addend + 2) & 1) == 0))
6431 return bfd_reloc_outofrange;
6433 value = symbol + addend - p;
6434 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6435 overflowed_p = mips_elf_overflow_p (value, 8);
6436 value >>= howto->rightshift;
6437 value &= howto->dst_mask;
6438 break;
6440 case R_MICROMIPS_PC10_S1:
6441 if (howto->partial_inplace)
6442 addend = _bfd_mips_elf_sign_extend (addend, 11);
6444 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6445 && (*cross_mode_jump_p
6446 ? ((symbol + addend + 2) & 3) != 0
6447 : ((symbol + addend + 2) & 1) == 0))
6448 return bfd_reloc_outofrange;
6450 value = symbol + addend - p;
6451 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6452 overflowed_p = mips_elf_overflow_p (value, 11);
6453 value >>= howto->rightshift;
6454 value &= howto->dst_mask;
6455 break;
6457 case R_MICROMIPS_PC16_S1:
6458 if (howto->partial_inplace)
6459 addend = _bfd_mips_elf_sign_extend (addend, 17);
6461 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6462 && (*cross_mode_jump_p
6463 ? ((symbol + addend) & 3) != 0
6464 : ((symbol + addend) & 1) == 0))
6465 return bfd_reloc_outofrange;
6467 value = symbol + addend - p;
6468 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6469 overflowed_p = mips_elf_overflow_p (value, 17);
6470 value >>= howto->rightshift;
6471 value &= howto->dst_mask;
6472 break;
6474 case R_MICROMIPS_PC23_S2:
6475 if (howto->partial_inplace)
6476 addend = _bfd_mips_elf_sign_extend (addend, 25);
6477 value = symbol + addend - ((p | 3) ^ 3);
6478 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6479 overflowed_p = mips_elf_overflow_p (value, 25);
6480 value >>= howto->rightshift;
6481 value &= howto->dst_mask;
6482 break;
6484 case R_MIPS_GOT_HI16:
6485 case R_MIPS_CALL_HI16:
6486 case R_MICROMIPS_GOT_HI16:
6487 case R_MICROMIPS_CALL_HI16:
6488 /* We're allowed to handle these two relocations identically.
6489 The dynamic linker is allowed to handle the CALL relocations
6490 differently by creating a lazy evaluation stub. */
6491 value = g;
6492 value = mips_elf_high (value);
6493 value &= howto->dst_mask;
6494 break;
6496 case R_MIPS_GOT_LO16:
6497 case R_MIPS_CALL_LO16:
6498 case R_MICROMIPS_GOT_LO16:
6499 case R_MICROMIPS_CALL_LO16:
6500 value = g & howto->dst_mask;
6501 break;
6503 case R_MIPS_GOT_PAGE:
6504 case R_MICROMIPS_GOT_PAGE:
6505 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6506 if (value == MINUS_ONE)
6507 return bfd_reloc_outofrange;
6508 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6509 overflowed_p = mips_elf_overflow_p (value, 16);
6510 break;
6512 case R_MIPS_GOT_OFST:
6513 case R_MICROMIPS_GOT_OFST:
6514 if (local_p)
6515 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6516 else
6517 value = addend;
6518 overflowed_p = mips_elf_overflow_p (value, 16);
6519 break;
6521 case R_MIPS_SUB:
6522 case R_MICROMIPS_SUB:
6523 value = symbol - addend;
6524 value &= howto->dst_mask;
6525 break;
6527 case R_MIPS_HIGHER:
6528 case R_MICROMIPS_HIGHER:
6529 value = mips_elf_higher (addend + symbol);
6530 value &= howto->dst_mask;
6531 break;
6533 case R_MIPS_HIGHEST:
6534 case R_MICROMIPS_HIGHEST:
6535 value = mips_elf_highest (addend + symbol);
6536 value &= howto->dst_mask;
6537 break;
6539 case R_MIPS_SCN_DISP:
6540 case R_MICROMIPS_SCN_DISP:
6541 value = symbol + addend - sec->output_offset;
6542 value &= howto->dst_mask;
6543 break;
6545 case R_MIPS_JALR:
6546 case R_MICROMIPS_JALR:
6547 /* This relocation is only a hint. In some cases, we optimize
6548 it into a bal instruction. But we don't try to optimize
6549 when the symbol does not resolve locally. */
6550 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6551 return bfd_reloc_continue;
6552 /* We can't optimize cross-mode jumps either. */
6553 if (*cross_mode_jump_p)
6554 return bfd_reloc_continue;
6555 value = symbol + addend;
6556 /* Neither we can non-instruction-aligned targets. */
6557 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6558 return bfd_reloc_continue;
6559 break;
6561 case R_MIPS_PJUMP:
6562 case R_MIPS_GNU_VTINHERIT:
6563 case R_MIPS_GNU_VTENTRY:
6564 /* We don't do anything with these at present. */
6565 return bfd_reloc_continue;
6567 default:
6568 /* An unrecognized relocation type. */
6569 return bfd_reloc_notsupported;
6572 /* Store the VALUE for our caller. */
6573 *valuep = value;
6574 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6577 /* It has been determined that the result of the RELOCATION is the
6578 VALUE. Use HOWTO to place VALUE into the output file at the
6579 appropriate position. The SECTION is the section to which the
6580 relocation applies.
6581 CROSS_MODE_JUMP_P is true if the relocation field
6582 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6584 Returns FALSE if anything goes wrong. */
6586 static bool
6587 mips_elf_perform_relocation (struct bfd_link_info *info,
6588 reloc_howto_type *howto,
6589 const Elf_Internal_Rela *relocation,
6590 bfd_vma value, bfd *input_bfd,
6591 asection *input_section, bfd_byte *contents,
6592 bool cross_mode_jump_p)
6594 bfd_vma x;
6595 bfd_byte *location;
6596 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6598 /* Figure out where the relocation is occurring. */
6599 location = contents + relocation->r_offset;
6601 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6603 /* Obtain the current value. */
6604 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6606 /* Clear the field we are setting. */
6607 x &= ~howto->dst_mask;
6609 /* Set the field. */
6610 x |= (value & howto->dst_mask);
6612 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6613 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6615 bfd_vma opcode = x >> 26;
6617 if (r_type == R_MIPS16_26 ? opcode == 0x7
6618 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6619 : opcode == 0x1d)
6621 info->callbacks->einfo
6622 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6623 input_bfd, input_section, relocation->r_offset);
6624 return true;
6627 if (cross_mode_jump_p && jal_reloc_p (r_type))
6629 bool ok;
6630 bfd_vma opcode = x >> 26;
6631 bfd_vma jalx_opcode;
6633 /* Check to see if the opcode is already JAL or JALX. */
6634 if (r_type == R_MIPS16_26)
6636 ok = ((opcode == 0x6) || (opcode == 0x7));
6637 jalx_opcode = 0x7;
6639 else if (r_type == R_MICROMIPS_26_S1)
6641 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6642 jalx_opcode = 0x3c;
6644 else
6646 ok = ((opcode == 0x3) || (opcode == 0x1d));
6647 jalx_opcode = 0x1d;
6650 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6651 convert J or JALS to JALX. */
6652 if (!ok)
6654 info->callbacks->einfo
6655 (_("%X%H: unsupported jump between ISA modes; "
6656 "consider recompiling with interlinking enabled\n"),
6657 input_bfd, input_section, relocation->r_offset);
6658 return true;
6661 /* Make this the JALX opcode. */
6662 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6664 else if (cross_mode_jump_p && b_reloc_p (r_type))
6666 bool ok = false;
6667 bfd_vma opcode = x >> 16;
6668 bfd_vma jalx_opcode = 0;
6669 bfd_vma sign_bit = 0;
6670 bfd_vma addr;
6671 bfd_vma dest;
6673 if (r_type == R_MICROMIPS_PC16_S1)
6675 ok = opcode == 0x4060;
6676 jalx_opcode = 0x3c;
6677 sign_bit = 0x10000;
6678 value <<= 1;
6680 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6682 ok = opcode == 0x411;
6683 jalx_opcode = 0x1d;
6684 sign_bit = 0x20000;
6685 value <<= 2;
6688 if (ok && !bfd_link_pic (info))
6690 addr = (input_section->output_section->vma
6691 + input_section->output_offset
6692 + relocation->r_offset
6693 + 4);
6694 dest = (addr
6695 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6697 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6699 info->callbacks->einfo
6700 (_("%X%H: cannot convert branch between ISA modes "
6701 "to JALX: relocation out of range\n"),
6702 input_bfd, input_section, relocation->r_offset);
6703 return true;
6706 /* Make this the JALX opcode. */
6707 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6709 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6711 info->callbacks->einfo
6712 (_("%X%H: unsupported branch between ISA modes\n"),
6713 input_bfd, input_section, relocation->r_offset);
6714 return true;
6718 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6719 range. */
6720 if (!bfd_link_relocatable (info)
6721 && !cross_mode_jump_p
6722 && ((JAL_TO_BAL_P (input_bfd)
6723 && r_type == R_MIPS_26
6724 && (x >> 26) == 0x3) /* jal addr */
6725 || (JALR_TO_BAL_P (input_bfd)
6726 && r_type == R_MIPS_JALR
6727 && x == 0x0320f809) /* jalr t9 */
6728 || (JR_TO_B_P (input_bfd)
6729 && r_type == R_MIPS_JALR
6730 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6732 bfd_vma addr;
6733 bfd_vma dest;
6734 bfd_signed_vma off;
6736 addr = (input_section->output_section->vma
6737 + input_section->output_offset
6738 + relocation->r_offset
6739 + 4);
6740 if (r_type == R_MIPS_26)
6741 dest = (value << 2) | ((addr >> 28) << 28);
6742 else
6743 dest = value;
6744 off = dest - addr;
6745 if (off <= 0x1ffff && off >= -0x20000)
6747 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6748 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6749 else
6750 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6754 /* Put the value into the output. */
6755 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6757 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6758 location);
6760 return true;
6763 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6764 is the original relocation, which is now being transformed into a
6765 dynamic relocation. The ADDENDP is adjusted if necessary; the
6766 caller should store the result in place of the original addend. */
6768 static bool
6769 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6770 struct bfd_link_info *info,
6771 const Elf_Internal_Rela *rel,
6772 struct mips_elf_link_hash_entry *h,
6773 asection *sec, bfd_vma symbol,
6774 bfd_vma *addendp, asection *input_section)
6776 Elf_Internal_Rela outrel[3];
6777 asection *sreloc;
6778 bfd *dynobj;
6779 int r_type;
6780 long indx;
6781 bool defined_p;
6782 struct mips_elf_link_hash_table *htab;
6784 htab = mips_elf_hash_table (info);
6785 BFD_ASSERT (htab != NULL);
6787 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6788 dynobj = elf_hash_table (info)->dynobj;
6789 sreloc = mips_elf_rel_dyn_section (info, false);
6790 BFD_ASSERT (sreloc != NULL);
6791 BFD_ASSERT (sreloc->contents != NULL);
6792 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6793 < sreloc->size);
6795 outrel[0].r_offset =
6796 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6797 if (ABI_64_P (output_bfd))
6799 outrel[1].r_offset =
6800 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6801 outrel[2].r_offset =
6802 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6805 if (outrel[0].r_offset == MINUS_ONE)
6806 /* The relocation field has been deleted. */
6807 return true;
6809 if (outrel[0].r_offset == MINUS_TWO)
6811 /* The relocation field has been converted into a relative value of
6812 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6813 the field to be fully relocated, so add in the symbol's value. */
6814 *addendp += symbol;
6815 return true;
6818 /* We must now calculate the dynamic symbol table index to use
6819 in the relocation. */
6820 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6822 BFD_ASSERT (htab->root.target_os == is_vxworks
6823 || h->global_got_area != GGA_NONE);
6824 indx = h->root.dynindx;
6825 if (SGI_COMPAT (output_bfd))
6826 defined_p = h->root.def_regular;
6827 else
6828 /* ??? glibc's ld.so just adds the final GOT entry to the
6829 relocation field. It therefore treats relocs against
6830 defined symbols in the same way as relocs against
6831 undefined symbols. */
6832 defined_p = false;
6834 else
6836 if (sec != NULL && bfd_is_abs_section (sec))
6837 indx = 0;
6838 else if (sec == NULL || sec->owner == NULL)
6840 bfd_set_error (bfd_error_bad_value);
6841 return false;
6843 else
6845 indx = elf_section_data (sec->output_section)->dynindx;
6846 if (indx == 0)
6848 asection *osec = htab->root.text_index_section;
6849 indx = elf_section_data (osec)->dynindx;
6851 if (indx == 0)
6852 abort ();
6855 /* Instead of generating a relocation using the section
6856 symbol, we may as well make it a fully relative
6857 relocation. We want to avoid generating relocations to
6858 local symbols because we used to generate them
6859 incorrectly, without adding the original symbol value,
6860 which is mandated by the ABI for section symbols. In
6861 order to give dynamic loaders and applications time to
6862 phase out the incorrect use, we refrain from emitting
6863 section-relative relocations. It's not like they're
6864 useful, after all. This should be a bit more efficient
6865 as well. */
6866 /* ??? Although this behavior is compatible with glibc's ld.so,
6867 the ABI says that relocations against STN_UNDEF should have
6868 a symbol value of 0. Irix rld honors this, so relocations
6869 against STN_UNDEF have no effect. */
6870 if (!SGI_COMPAT (output_bfd))
6871 indx = 0;
6872 defined_p = true;
6875 /* If the relocation was previously an absolute relocation and
6876 this symbol will not be referred to by the relocation, we must
6877 adjust it by the value we give it in the dynamic symbol table.
6878 Otherwise leave the job up to the dynamic linker. */
6879 if (defined_p && r_type != R_MIPS_REL32)
6880 *addendp += symbol;
6882 if (htab->root.target_os == is_vxworks)
6883 /* VxWorks uses non-relative relocations for this. */
6884 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6885 else
6886 /* The relocation is always an REL32 relocation because we don't
6887 know where the shared library will wind up at load-time. */
6888 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6889 R_MIPS_REL32);
6891 /* For strict adherence to the ABI specification, we should
6892 generate a R_MIPS_64 relocation record by itself before the
6893 _REL32/_64 record as well, such that the addend is read in as
6894 a 64-bit value (REL32 is a 32-bit relocation, after all).
6895 However, since none of the existing ELF64 MIPS dynamic
6896 loaders seems to care, we don't waste space with these
6897 artificial relocations. If this turns out to not be true,
6898 mips_elf_allocate_dynamic_relocation() should be tweaked so
6899 as to make room for a pair of dynamic relocations per
6900 invocation if ABI_64_P, and here we should generate an
6901 additional relocation record with R_MIPS_64 by itself for a
6902 NULL symbol before this relocation record. */
6903 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6904 ABI_64_P (output_bfd)
6905 ? R_MIPS_64
6906 : R_MIPS_NONE);
6907 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6909 /* Adjust the output offset of the relocation to reference the
6910 correct location in the output file. */
6911 outrel[0].r_offset += (input_section->output_section->vma
6912 + input_section->output_offset);
6913 outrel[1].r_offset += (input_section->output_section->vma
6914 + input_section->output_offset);
6915 outrel[2].r_offset += (input_section->output_section->vma
6916 + input_section->output_offset);
6918 /* Put the relocation back out. We have to use the special
6919 relocation outputter in the 64-bit case since the 64-bit
6920 relocation format is non-standard. */
6921 if (ABI_64_P (output_bfd))
6923 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6924 (output_bfd, &outrel[0],
6925 (sreloc->contents
6926 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6928 else if (htab->root.target_os == is_vxworks)
6930 /* VxWorks uses RELA rather than REL dynamic relocations. */
6931 outrel[0].r_addend = *addendp;
6932 bfd_elf32_swap_reloca_out
6933 (output_bfd, &outrel[0],
6934 (sreloc->contents
6935 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6937 else
6938 bfd_elf32_swap_reloc_out
6939 (output_bfd, &outrel[0],
6940 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6942 /* We've now added another relocation. */
6943 ++sreloc->reloc_count;
6945 /* Make sure the output section is writable. The dynamic linker
6946 will be writing to it. */
6947 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6948 |= SHF_WRITE;
6950 /* On IRIX5, make an entry of compact relocation info. */
6951 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6953 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6954 bfd_byte *cr;
6956 if (scpt)
6958 Elf32_crinfo cptrel;
6960 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6961 cptrel.vaddr = (rel->r_offset
6962 + input_section->output_section->vma
6963 + input_section->output_offset);
6964 if (r_type == R_MIPS_REL32)
6965 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6966 else
6967 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6968 mips_elf_set_cr_dist2to (cptrel, 0);
6969 cptrel.konst = *addendp;
6971 cr = (scpt->contents
6972 + sizeof (Elf32_External_compact_rel));
6973 mips_elf_set_cr_relvaddr (cptrel, 0);
6974 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6975 ((Elf32_External_crinfo *) cr
6976 + scpt->reloc_count));
6977 ++scpt->reloc_count;
6981 /* If we've written this relocation for a readonly section,
6982 we need to set DF_TEXTREL again, so that we do not delete the
6983 DT_TEXTREL tag. */
6984 if (MIPS_ELF_READONLY_SECTION (input_section))
6985 info->flags |= DF_TEXTREL;
6987 return true;
6990 /* Return the MACH for a MIPS e_flags value. */
6992 unsigned long
6993 _bfd_elf_mips_mach (flagword flags)
6995 switch (flags & EF_MIPS_MACH)
6997 case E_MIPS_MACH_3900:
6998 return bfd_mach_mips3900;
7000 case E_MIPS_MACH_4010:
7001 return bfd_mach_mips4010;
7003 case E_MIPS_MACH_ALLEGREX:
7004 return bfd_mach_mips_allegrex;
7006 case E_MIPS_MACH_4100:
7007 return bfd_mach_mips4100;
7009 case E_MIPS_MACH_4111:
7010 return bfd_mach_mips4111;
7012 case E_MIPS_MACH_4120:
7013 return bfd_mach_mips4120;
7015 case E_MIPS_MACH_4650:
7016 return bfd_mach_mips4650;
7018 case E_MIPS_MACH_5400:
7019 return bfd_mach_mips5400;
7021 case E_MIPS_MACH_5500:
7022 return bfd_mach_mips5500;
7024 case E_MIPS_MACH_5900:
7025 return bfd_mach_mips5900;
7027 case E_MIPS_MACH_9000:
7028 return bfd_mach_mips9000;
7030 case E_MIPS_MACH_SB1:
7031 return bfd_mach_mips_sb1;
7033 case E_MIPS_MACH_LS2E:
7034 return bfd_mach_mips_loongson_2e;
7036 case E_MIPS_MACH_LS2F:
7037 return bfd_mach_mips_loongson_2f;
7039 case E_MIPS_MACH_GS464:
7040 return bfd_mach_mips_gs464;
7042 case E_MIPS_MACH_GS464E:
7043 return bfd_mach_mips_gs464e;
7045 case E_MIPS_MACH_GS264E:
7046 return bfd_mach_mips_gs264e;
7048 case E_MIPS_MACH_OCTEON3:
7049 return bfd_mach_mips_octeon3;
7051 case E_MIPS_MACH_OCTEON2:
7052 return bfd_mach_mips_octeon2;
7054 case E_MIPS_MACH_OCTEON:
7055 return bfd_mach_mips_octeon;
7057 case E_MIPS_MACH_XLR:
7058 return bfd_mach_mips_xlr;
7060 case E_MIPS_MACH_IAMR2:
7061 return bfd_mach_mips_interaptiv_mr2;
7063 default:
7064 switch (flags & EF_MIPS_ARCH)
7066 default:
7067 case E_MIPS_ARCH_1:
7068 return bfd_mach_mips3000;
7070 case E_MIPS_ARCH_2:
7071 return bfd_mach_mips6000;
7073 case E_MIPS_ARCH_3:
7074 return bfd_mach_mips4000;
7076 case E_MIPS_ARCH_4:
7077 return bfd_mach_mips8000;
7079 case E_MIPS_ARCH_5:
7080 return bfd_mach_mips5;
7082 case E_MIPS_ARCH_32:
7083 return bfd_mach_mipsisa32;
7085 case E_MIPS_ARCH_64:
7086 return bfd_mach_mipsisa64;
7088 case E_MIPS_ARCH_32R2:
7089 return bfd_mach_mipsisa32r2;
7091 case E_MIPS_ARCH_64R2:
7092 return bfd_mach_mipsisa64r2;
7094 case E_MIPS_ARCH_32R6:
7095 return bfd_mach_mipsisa32r6;
7097 case E_MIPS_ARCH_64R6:
7098 return bfd_mach_mipsisa64r6;
7102 return 0;
7105 /* Return printable name for ABI. */
7107 static inline char *
7108 elf_mips_abi_name (bfd *abfd)
7110 flagword flags;
7112 flags = elf_elfheader (abfd)->e_flags;
7113 switch (flags & EF_MIPS_ABI)
7115 case 0:
7116 if (ABI_N32_P (abfd))
7117 return "N32";
7118 else if (ABI_64_P (abfd))
7119 return "64";
7120 else
7121 return "none";
7122 case E_MIPS_ABI_O32:
7123 return "O32";
7124 case E_MIPS_ABI_O64:
7125 return "O64";
7126 case E_MIPS_ABI_EABI32:
7127 return "EABI32";
7128 case E_MIPS_ABI_EABI64:
7129 return "EABI64";
7130 default:
7131 return "unknown abi";
7135 /* MIPS ELF uses two common sections. One is the usual one, and the
7136 other is for small objects. All the small objects are kept
7137 together, and then referenced via the gp pointer, which yields
7138 faster assembler code. This is what we use for the small common
7139 section. This approach is copied from ecoff.c. */
7140 static asection mips_elf_scom_section;
7141 static const asymbol mips_elf_scom_symbol =
7142 GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7143 static asection mips_elf_scom_section =
7144 BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7145 ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7147 /* MIPS ELF also uses an acommon section, which represents an
7148 allocated common symbol which may be overridden by a
7149 definition in a shared library. */
7150 static asection mips_elf_acom_section;
7151 static const asymbol mips_elf_acom_symbol =
7152 GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7153 static asection mips_elf_acom_section =
7154 BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7155 ".acommon", 0, SEC_ALLOC);
7157 /* This is used for both the 32-bit and the 64-bit ABI. */
7159 void
7160 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7162 elf_symbol_type *elfsym;
7164 /* Handle the special MIPS section numbers that a symbol may use. */
7165 elfsym = (elf_symbol_type *) asym;
7166 switch (elfsym->internal_elf_sym.st_shndx)
7168 case SHN_MIPS_ACOMMON:
7169 /* This section is used in a dynamically linked executable file.
7170 It is an allocated common section. The dynamic linker can
7171 either resolve these symbols to something in a shared
7172 library, or it can just leave them here. For our purposes,
7173 we can consider these symbols to be in a new section. */
7174 asym->section = &mips_elf_acom_section;
7175 break;
7177 case SHN_COMMON:
7178 /* Common symbols less than the GP size are automatically
7179 treated as SHN_MIPS_SCOMMON symbols, with some exceptions. */
7180 if (asym->value > elf_gp_size (abfd)
7181 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7182 || IRIX_COMPAT (abfd) == ict_irix6
7183 || strcmp (asym->name, "__gnu_lto_slim") == 0)
7184 break;
7185 /* Fall through. */
7186 case SHN_MIPS_SCOMMON:
7187 asym->section = &mips_elf_scom_section;
7188 asym->value = elfsym->internal_elf_sym.st_size;
7189 break;
7191 case SHN_MIPS_SUNDEFINED:
7192 asym->section = bfd_und_section_ptr;
7193 break;
7195 case SHN_MIPS_TEXT:
7197 asection *section = bfd_get_section_by_name (abfd, ".text");
7199 if (section != NULL)
7201 asym->section = section;
7202 /* MIPS_TEXT is a bit special, the address is not an offset
7203 to the base of the .text section. So subtract the section
7204 base address to make it an offset. */
7205 asym->value -= section->vma;
7208 break;
7210 case SHN_MIPS_DATA:
7212 asection *section = bfd_get_section_by_name (abfd, ".data");
7214 if (section != NULL)
7216 asym->section = section;
7217 /* MIPS_DATA is a bit special, the address is not an offset
7218 to the base of the .data section. So subtract the section
7219 base address to make it an offset. */
7220 asym->value -= section->vma;
7223 break;
7226 /* If this is an odd-valued function symbol, assume it's a MIPS16
7227 or microMIPS one. */
7228 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7229 && (asym->value & 1) != 0)
7231 asym->value--;
7232 if (MICROMIPS_P (abfd))
7233 elfsym->internal_elf_sym.st_other
7234 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7235 else
7236 elfsym->internal_elf_sym.st_other
7237 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7241 /* Implement elf_backend_eh_frame_address_size. This differs from
7242 the default in the way it handles EABI64.
7244 EABI64 was originally specified as an LP64 ABI, and that is what
7245 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7246 historically accepted the combination of -mabi=eabi and -mlong32,
7247 and this ILP32 variation has become semi-official over time.
7248 Both forms use elf32 and have pointer-sized FDE addresses.
7250 If an EABI object was generated by GCC 4.0 or above, it will have
7251 an empty .gcc_compiled_longXX section, where XX is the size of longs
7252 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7253 have no special marking to distinguish them from LP64 objects.
7255 We don't want users of the official LP64 ABI to be punished for the
7256 existence of the ILP32 variant, but at the same time, we don't want
7257 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7258 We therefore take the following approach:
7260 - If ABFD contains a .gcc_compiled_longXX section, use it to
7261 determine the pointer size.
7263 - Otherwise check the type of the first relocation. Assume that
7264 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7266 - Otherwise punt.
7268 The second check is enough to detect LP64 objects generated by pre-4.0
7269 compilers because, in the kind of output generated by those compilers,
7270 the first relocation will be associated with either a CIE personality
7271 routine or an FDE start address. Furthermore, the compilers never
7272 used a special (non-pointer) encoding for this ABI.
7274 Checking the relocation type should also be safe because there is no
7275 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7276 did so. */
7278 unsigned int
7279 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7281 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7282 return 8;
7283 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7285 bool long32_p, long64_p;
7287 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7288 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7289 if (long32_p && long64_p)
7290 return 0;
7291 if (long32_p)
7292 return 4;
7293 if (long64_p)
7294 return 8;
7296 if (sec->reloc_count > 0
7297 && elf_section_data (sec)->relocs != NULL
7298 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7299 == R_MIPS_64))
7300 return 8;
7302 return 0;
7304 return 4;
7307 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7308 relocations against two unnamed section symbols to resolve to the
7309 same address. For example, if we have code like:
7311 lw $4,%got_disp(.data)($gp)
7312 lw $25,%got_disp(.text)($gp)
7313 jalr $25
7315 then the linker will resolve both relocations to .data and the program
7316 will jump there rather than to .text.
7318 We can work around this problem by giving names to local section symbols.
7319 This is also what the MIPSpro tools do. */
7321 bool
7322 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7324 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7327 /* Work over a section just before writing it out. This routine is
7328 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7329 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7330 a better way. */
7332 bool
7333 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7335 if (hdr->sh_type == SHT_MIPS_REGINFO
7336 && hdr->sh_size > 0)
7338 bfd_byte buf[4];
7340 BFD_ASSERT (hdr->contents == NULL);
7342 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7344 _bfd_error_handler
7345 (_("%pB: incorrect `.reginfo' section size; "
7346 "expected %" PRIu64 ", got %" PRIu64),
7347 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7348 (uint64_t) hdr->sh_size);
7349 bfd_set_error (bfd_error_bad_value);
7350 return false;
7353 if (bfd_seek (abfd,
7354 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7355 SEEK_SET) != 0)
7356 return false;
7357 H_PUT_32 (abfd, elf_gp (abfd), buf);
7358 if (bfd_write (buf, 4, abfd) != 4)
7359 return false;
7362 if (hdr->sh_type == SHT_MIPS_OPTIONS
7363 && hdr->bfd_section != NULL
7364 && mips_elf_section_data (hdr->bfd_section) != NULL
7365 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7367 bfd_byte *contents, *l, *lend;
7369 /* We stored the section contents in the tdata field in the
7370 set_section_contents routine. We save the section contents
7371 so that we don't have to read them again.
7372 At this point we know that elf_gp is set, so we can look
7373 through the section contents to see if there is an
7374 ODK_REGINFO structure. */
7376 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7377 l = contents;
7378 lend = contents + hdr->sh_size;
7379 while (l + sizeof (Elf_External_Options) <= lend)
7381 Elf_Internal_Options intopt;
7383 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7384 &intopt);
7385 if (intopt.size < sizeof (Elf_External_Options))
7387 _bfd_error_handler
7388 /* xgettext:c-format */
7389 (_("%pB: warning: bad `%s' option size %u smaller than"
7390 " its header"),
7391 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7392 break;
7394 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7396 bfd_byte buf[8];
7398 if (bfd_seek (abfd,
7399 (hdr->sh_offset
7400 + (l - contents)
7401 + sizeof (Elf_External_Options)
7402 + (sizeof (Elf64_External_RegInfo) - 8)),
7403 SEEK_SET) != 0)
7404 return false;
7405 H_PUT_64 (abfd, elf_gp (abfd), buf);
7406 if (bfd_write (buf, 8, abfd) != 8)
7407 return false;
7409 else if (intopt.kind == ODK_REGINFO)
7411 bfd_byte buf[4];
7413 if (bfd_seek (abfd,
7414 (hdr->sh_offset
7415 + (l - contents)
7416 + sizeof (Elf_External_Options)
7417 + (sizeof (Elf32_External_RegInfo) - 4)),
7418 SEEK_SET) != 0)
7419 return false;
7420 H_PUT_32 (abfd, elf_gp (abfd), buf);
7421 if (bfd_write (buf, 4, abfd) != 4)
7422 return false;
7424 l += intopt.size;
7428 if (hdr->bfd_section != NULL)
7430 const char *name = bfd_section_name (hdr->bfd_section);
7432 /* .sbss is not handled specially here because the GNU/Linux
7433 prelinker can convert .sbss from NOBITS to PROGBITS and
7434 changing it back to NOBITS breaks the binary. The entry in
7435 _bfd_mips_elf_special_sections will ensure the correct flags
7436 are set on .sbss if BFD creates it without reading it from an
7437 input file, and without special handling here the flags set
7438 on it in an input file will be followed. */
7439 if (strcmp (name, ".sdata") == 0
7440 || strcmp (name, ".lit8") == 0
7441 || strcmp (name, ".lit4") == 0)
7442 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7443 else if (strcmp (name, ".srdata") == 0)
7444 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7445 else if (strcmp (name, ".compact_rel") == 0)
7446 hdr->sh_flags = 0;
7447 else if (strcmp (name, ".rtproc") == 0)
7449 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7451 unsigned int adjust;
7453 adjust = hdr->sh_size % hdr->sh_addralign;
7454 if (adjust != 0)
7455 hdr->sh_size += hdr->sh_addralign - adjust;
7460 return true;
7463 /* Handle a MIPS specific section when reading an object file. This
7464 is called when elfcode.h finds a section with an unknown type.
7465 This routine supports both the 32-bit and 64-bit ELF ABI. */
7467 bool
7468 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7469 Elf_Internal_Shdr *hdr,
7470 const char *name,
7471 int shindex)
7473 flagword flags = 0;
7475 /* There ought to be a place to keep ELF backend specific flags, but
7476 at the moment there isn't one. We just keep track of the
7477 sections by their name, instead. Fortunately, the ABI gives
7478 suggested names for all the MIPS specific sections, so we will
7479 probably get away with this. */
7480 switch (hdr->sh_type)
7482 case SHT_MIPS_LIBLIST:
7483 if (strcmp (name, ".liblist") != 0)
7484 return false;
7485 break;
7486 case SHT_MIPS_MSYM:
7487 if (strcmp (name, ".msym") != 0)
7488 return false;
7489 break;
7490 case SHT_MIPS_CONFLICT:
7491 if (strcmp (name, ".conflict") != 0)
7492 return false;
7493 break;
7494 case SHT_MIPS_GPTAB:
7495 if (! startswith (name, ".gptab."))
7496 return false;
7497 break;
7498 case SHT_MIPS_UCODE:
7499 if (strcmp (name, ".ucode") != 0)
7500 return false;
7501 break;
7502 case SHT_MIPS_DEBUG:
7503 if (strcmp (name, ".mdebug") != 0)
7504 return false;
7505 flags = SEC_DEBUGGING;
7506 break;
7507 case SHT_MIPS_REGINFO:
7508 if (strcmp (name, ".reginfo") != 0
7509 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7510 return false;
7511 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7512 break;
7513 case SHT_MIPS_IFACE:
7514 if (strcmp (name, ".MIPS.interfaces") != 0)
7515 return false;
7516 break;
7517 case SHT_MIPS_CONTENT:
7518 if (! startswith (name, ".MIPS.content"))
7519 return false;
7520 break;
7521 case SHT_MIPS_OPTIONS:
7522 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7523 return false;
7524 break;
7525 case SHT_MIPS_ABIFLAGS:
7526 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7527 return false;
7528 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7529 break;
7530 case SHT_MIPS_DWARF:
7531 if (! startswith (name, ".debug_")
7532 && ! startswith (name, ".gnu.debuglto_.debug_")
7533 && ! startswith (name, ".zdebug_")
7534 && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7535 return false;
7536 break;
7537 case SHT_MIPS_SYMBOL_LIB:
7538 if (strcmp (name, ".MIPS.symlib") != 0)
7539 return false;
7540 break;
7541 case SHT_MIPS_EVENTS:
7542 if (! startswith (name, ".MIPS.events")
7543 && ! startswith (name, ".MIPS.post_rel"))
7544 return false;
7545 break;
7546 case SHT_MIPS_XHASH:
7547 if (strcmp (name, ".MIPS.xhash") != 0)
7548 return false;
7549 default:
7550 break;
7553 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7554 return false;
7556 if (hdr->sh_flags & SHF_MIPS_GPREL)
7557 flags |= SEC_SMALL_DATA;
7559 if (flags)
7561 if (!bfd_set_section_flags (hdr->bfd_section,
7562 (bfd_section_flags (hdr->bfd_section)
7563 | flags)))
7564 return false;
7567 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7569 Elf_External_ABIFlags_v0 ext;
7571 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7572 &ext, 0, sizeof ext))
7573 return false;
7574 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7575 &mips_elf_tdata (abfd)->abiflags);
7576 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7577 return false;
7578 mips_elf_tdata (abfd)->abiflags_valid = true;
7581 /* FIXME: We should record sh_info for a .gptab section. */
7583 /* For a .reginfo section, set the gp value in the tdata information
7584 from the contents of this section. We need the gp value while
7585 processing relocs, so we just get it now. The .reginfo section
7586 is not used in the 64-bit MIPS ELF ABI. */
7587 if (hdr->sh_type == SHT_MIPS_REGINFO)
7589 Elf32_External_RegInfo ext;
7590 Elf32_RegInfo s;
7592 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7593 &ext, 0, sizeof ext))
7594 return false;
7595 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7596 elf_gp (abfd) = s.ri_gp_value;
7599 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7600 set the gp value based on what we find. We may see both
7601 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7602 they should agree. */
7603 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7605 bfd_byte *contents, *l, *lend;
7607 if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7609 free (contents);
7610 return false;
7612 l = contents;
7613 lend = contents + hdr->sh_size;
7614 while (l + sizeof (Elf_External_Options) <= lend)
7616 Elf_Internal_Options intopt;
7618 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7619 &intopt);
7620 if (intopt.size < sizeof (Elf_External_Options))
7622 bad_opt:
7623 _bfd_error_handler
7624 /* xgettext:c-format */
7625 (_("%pB: warning: truncated `%s' option"),
7626 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7627 break;
7629 if (intopt.kind == ODK_REGINFO)
7631 if (ABI_64_P (abfd))
7633 Elf64_Internal_RegInfo intreg;
7634 size_t needed = (sizeof (Elf_External_Options)
7635 + sizeof (Elf64_External_RegInfo));
7636 if (intopt.size < needed || (size_t) (lend - l) < needed)
7637 goto bad_opt;
7638 bfd_mips_elf64_swap_reginfo_in
7639 (abfd,
7640 ((Elf64_External_RegInfo *)
7641 (l + sizeof (Elf_External_Options))),
7642 &intreg);
7643 elf_gp (abfd) = intreg.ri_gp_value;
7645 else
7647 Elf32_RegInfo intreg;
7648 size_t needed = (sizeof (Elf_External_Options)
7649 + sizeof (Elf32_External_RegInfo));
7650 if (intopt.size < needed || (size_t) (lend - l) < needed)
7651 goto bad_opt;
7652 bfd_mips_elf32_swap_reginfo_in
7653 (abfd,
7654 ((Elf32_External_RegInfo *)
7655 (l + sizeof (Elf_External_Options))),
7656 &intreg);
7657 elf_gp (abfd) = intreg.ri_gp_value;
7660 l += intopt.size;
7662 free (contents);
7665 return true;
7668 /* Set the correct type for a MIPS ELF section. We do this by the
7669 section name, which is a hack, but ought to work. This routine is
7670 used by both the 32-bit and the 64-bit ABI. */
7672 bool
7673 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7675 const char *name = bfd_section_name (sec);
7677 if (strcmp (name, ".liblist") == 0)
7679 hdr->sh_type = SHT_MIPS_LIBLIST;
7680 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7681 /* The sh_link field is set in final_write_processing. */
7683 else if (strcmp (name, ".conflict") == 0)
7684 hdr->sh_type = SHT_MIPS_CONFLICT;
7685 else if (startswith (name, ".gptab."))
7687 hdr->sh_type = SHT_MIPS_GPTAB;
7688 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7689 /* The sh_info field is set in final_write_processing. */
7691 else if (strcmp (name, ".ucode") == 0)
7692 hdr->sh_type = SHT_MIPS_UCODE;
7693 else if (strcmp (name, ".mdebug") == 0)
7695 hdr->sh_type = SHT_MIPS_DEBUG;
7696 /* In a shared object on IRIX 5.3, the .mdebug section has an
7697 entsize of 0. FIXME: Does this matter? */
7698 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7699 hdr->sh_entsize = 0;
7700 else
7701 hdr->sh_entsize = 1;
7703 else if (strcmp (name, ".reginfo") == 0)
7705 hdr->sh_type = SHT_MIPS_REGINFO;
7706 /* In a shared object on IRIX 5.3, the .reginfo section has an
7707 entsize of 0x18. FIXME: Does this matter? */
7708 if (SGI_COMPAT (abfd))
7710 if ((abfd->flags & DYNAMIC) != 0)
7711 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7712 else
7713 hdr->sh_entsize = 1;
7715 else
7716 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7718 else if (SGI_COMPAT (abfd)
7719 && (strcmp (name, ".hash") == 0
7720 || strcmp (name, ".dynamic") == 0
7721 || strcmp (name, ".dynstr") == 0))
7723 if (SGI_COMPAT (abfd))
7724 hdr->sh_entsize = 0;
7725 #if 0
7726 /* This isn't how the IRIX6 linker behaves. */
7727 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7728 #endif
7730 else if (strcmp (name, ".got") == 0
7731 || strcmp (name, ".srdata") == 0
7732 || strcmp (name, ".sdata") == 0
7733 || strcmp (name, ".sbss") == 0
7734 || strcmp (name, ".lit4") == 0
7735 || strcmp (name, ".lit8") == 0)
7736 hdr->sh_flags |= SHF_MIPS_GPREL;
7737 else if (strcmp (name, ".MIPS.interfaces") == 0)
7739 hdr->sh_type = SHT_MIPS_IFACE;
7740 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7742 else if (startswith (name, ".MIPS.content"))
7744 hdr->sh_type = SHT_MIPS_CONTENT;
7745 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7746 /* The sh_info field is set in final_write_processing. */
7748 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7750 hdr->sh_type = SHT_MIPS_OPTIONS;
7751 hdr->sh_entsize = 1;
7752 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7754 else if (startswith (name, ".MIPS.abiflags"))
7756 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7757 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7759 else if (startswith (name, ".debug_")
7760 || startswith (name, ".gnu.debuglto_.debug_")
7761 || startswith (name, ".zdebug_")
7762 || startswith (name, ".gnu.debuglto_.zdebug_"))
7764 hdr->sh_type = SHT_MIPS_DWARF;
7766 /* Irix facilities such as libexc expect a single .debug_frame
7767 per executable, the system ones have NOSTRIP set and the linker
7768 doesn't merge sections with different flags so ... */
7769 if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7770 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7772 else if (strcmp (name, ".MIPS.symlib") == 0)
7774 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7775 /* The sh_link and sh_info fields are set in
7776 final_write_processing. */
7778 else if (startswith (name, ".MIPS.events")
7779 || startswith (name, ".MIPS.post_rel"))
7781 hdr->sh_type = SHT_MIPS_EVENTS;
7782 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7783 /* The sh_link field is set in final_write_processing. */
7785 else if (strcmp (name, ".msym") == 0)
7787 hdr->sh_type = SHT_MIPS_MSYM;
7788 hdr->sh_flags |= SHF_ALLOC;
7789 hdr->sh_entsize = 8;
7791 else if (strcmp (name, ".MIPS.xhash") == 0)
7793 hdr->sh_type = SHT_MIPS_XHASH;
7794 hdr->sh_flags |= SHF_ALLOC;
7795 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7798 /* The generic elf_fake_sections will set up REL_HDR using the default
7799 kind of relocations. We used to set up a second header for the
7800 non-default kind of relocations here, but only NewABI would use
7801 these, and the IRIX ld doesn't like resulting empty RELA sections.
7802 Thus we create those header only on demand now. */
7804 return true;
7807 /* Given a BFD section, try to locate the corresponding ELF section
7808 index. This is used by both the 32-bit and the 64-bit ABI.
7809 Actually, it's not clear to me that the 64-bit ABI supports these,
7810 but for non-PIC objects we will certainly want support for at least
7811 the .scommon section. */
7813 bool
7814 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7815 asection *sec, int *retval)
7817 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7819 *retval = SHN_MIPS_SCOMMON;
7820 return true;
7822 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7824 *retval = SHN_MIPS_ACOMMON;
7825 return true;
7827 return false;
7830 /* Hook called by the linker routine which adds symbols from an object
7831 file. We must handle the special MIPS section numbers here. */
7833 bool
7834 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7835 Elf_Internal_Sym *sym, const char **namep,
7836 flagword *flagsp ATTRIBUTE_UNUSED,
7837 asection **secp, bfd_vma *valp)
7839 if (SGI_COMPAT (abfd)
7840 && (abfd->flags & DYNAMIC) != 0
7841 && strcmp (*namep, "_rld_new_interface") == 0)
7843 /* Skip IRIX5 rld entry name. */
7844 *namep = NULL;
7845 return true;
7848 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7849 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7850 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7851 a magic symbol resolved by the linker, we ignore this bogus definition
7852 of _gp_disp. New ABI objects do not suffer from this problem so this
7853 is not done for them. */
7854 if (!NEWABI_P(abfd)
7855 && (sym->st_shndx == SHN_ABS)
7856 && (strcmp (*namep, "_gp_disp") == 0))
7858 *namep = NULL;
7859 return true;
7862 switch (sym->st_shndx)
7864 case SHN_COMMON:
7865 /* Common symbols less than the GP size are automatically
7866 treated as SHN_MIPS_SCOMMON symbols, with some exceptions. */
7867 if (sym->st_size > elf_gp_size (abfd)
7868 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7869 || IRIX_COMPAT (abfd) == ict_irix6
7870 || strcmp (*namep, "__gnu_lto_slim") == 0)
7871 break;
7872 /* Fall through. */
7873 case SHN_MIPS_SCOMMON:
7874 *secp = bfd_make_section_old_way (abfd, ".scommon");
7875 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7876 *valp = sym->st_size;
7877 break;
7879 case SHN_MIPS_TEXT:
7880 /* This section is used in a shared object. */
7881 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7883 asymbol *elf_text_symbol;
7884 asection *elf_text_section;
7885 size_t amt = sizeof (asection);
7887 elf_text_section = bfd_zalloc (abfd, amt);
7888 if (elf_text_section == NULL)
7889 return false;
7891 amt = sizeof (asymbol);
7892 elf_text_symbol = bfd_zalloc (abfd, amt);
7893 if (elf_text_symbol == NULL)
7894 return false;
7896 /* Initialize the section. */
7898 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7899 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7901 elf_text_section->symbol = elf_text_symbol;
7902 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7904 elf_text_section->name = ".text";
7905 elf_text_section->flags = SEC_NO_FLAGS;
7906 elf_text_section->output_section = NULL;
7907 elf_text_section->owner = abfd;
7908 elf_text_symbol->name = ".text";
7909 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7910 elf_text_symbol->section = elf_text_section;
7912 /* This code used to do *secp = bfd_und_section_ptr if
7913 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7914 so I took it out. */
7915 *secp = mips_elf_tdata (abfd)->elf_text_section;
7916 break;
7918 case SHN_MIPS_ACOMMON:
7919 /* Fall through. XXX Can we treat this as allocated data? */
7920 case SHN_MIPS_DATA:
7921 /* This section is used in a shared object. */
7922 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7924 asymbol *elf_data_symbol;
7925 asection *elf_data_section;
7926 size_t amt = sizeof (asection);
7928 elf_data_section = bfd_zalloc (abfd, amt);
7929 if (elf_data_section == NULL)
7930 return false;
7932 amt = sizeof (asymbol);
7933 elf_data_symbol = bfd_zalloc (abfd, amt);
7934 if (elf_data_symbol == NULL)
7935 return false;
7937 /* Initialize the section. */
7939 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7940 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7942 elf_data_section->symbol = elf_data_symbol;
7943 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7945 elf_data_section->name = ".data";
7946 elf_data_section->flags = SEC_NO_FLAGS;
7947 elf_data_section->output_section = NULL;
7948 elf_data_section->owner = abfd;
7949 elf_data_symbol->name = ".data";
7950 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7951 elf_data_symbol->section = elf_data_section;
7953 /* This code used to do *secp = bfd_und_section_ptr if
7954 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7955 so I took it out. */
7956 *secp = mips_elf_tdata (abfd)->elf_data_section;
7957 break;
7959 case SHN_MIPS_SUNDEFINED:
7960 *secp = bfd_und_section_ptr;
7961 break;
7964 if (SGI_COMPAT (abfd)
7965 && ! bfd_link_pic (info)
7966 && info->output_bfd->xvec == abfd->xvec
7967 && strcmp (*namep, "__rld_obj_head") == 0)
7969 struct elf_link_hash_entry *h;
7970 struct bfd_link_hash_entry *bh;
7972 /* Mark __rld_obj_head as dynamic. */
7973 bh = NULL;
7974 if (! (_bfd_generic_link_add_one_symbol
7975 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7976 get_elf_backend_data (abfd)->collect, &bh)))
7977 return false;
7979 h = (struct elf_link_hash_entry *) bh;
7980 h->non_elf = 0;
7981 h->def_regular = 1;
7982 h->type = STT_OBJECT;
7984 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7985 return false;
7987 mips_elf_hash_table (info)->use_rld_obj_head = true;
7988 mips_elf_hash_table (info)->rld_symbol = h;
7991 /* If this is a mips16 text symbol, add 1 to the value to make it
7992 odd. This will cause something like .word SYM to come up with
7993 the right value when it is loaded into the PC. */
7994 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7995 ++*valp;
7997 return true;
8000 /* This hook function is called before the linker writes out a global
8001 symbol. We mark symbols as small common if appropriate. This is
8002 also where we undo the increment of the value for a mips16 symbol. */
8005 _bfd_mips_elf_link_output_symbol_hook
8006 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8007 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8008 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8010 /* If we see a common symbol, which implies a relocatable link, then
8011 if a symbol was small common in an input file, mark it as small
8012 common in the output file. */
8013 if (sym->st_shndx == SHN_COMMON
8014 && strcmp (input_sec->name, ".scommon") == 0)
8015 sym->st_shndx = SHN_MIPS_SCOMMON;
8017 if (ELF_ST_IS_COMPRESSED (sym->st_other))
8018 sym->st_value &= ~1;
8020 return 1;
8023 /* Functions for the dynamic linker. */
8025 /* Create dynamic sections when linking against a dynamic object. */
8027 bool
8028 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8030 struct elf_link_hash_entry *h;
8031 struct bfd_link_hash_entry *bh;
8032 flagword flags;
8033 register asection *s;
8034 const char * const *namep;
8035 struct mips_elf_link_hash_table *htab;
8037 htab = mips_elf_hash_table (info);
8038 BFD_ASSERT (htab != NULL);
8040 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8041 | SEC_LINKER_CREATED | SEC_READONLY);
8043 /* The psABI requires a read-only .dynamic section, but the VxWorks
8044 EABI doesn't. */
8045 if (htab->root.target_os != is_vxworks)
8047 s = bfd_get_linker_section (abfd, ".dynamic");
8048 if (s != NULL)
8050 if (!bfd_set_section_flags (s, flags))
8051 return false;
8055 /* We need to create .got section. */
8056 if (!mips_elf_create_got_section (abfd, info))
8057 return false;
8059 if (! mips_elf_rel_dyn_section (info, true))
8060 return false;
8062 /* Create .stub section. */
8063 s = bfd_make_section_anyway_with_flags (abfd,
8064 MIPS_ELF_STUB_SECTION_NAME (abfd),
8065 flags | SEC_CODE);
8066 if (s == NULL
8067 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8068 return false;
8069 htab->sstubs = s;
8071 if (!mips_elf_hash_table (info)->use_rld_obj_head
8072 && bfd_link_executable (info)
8073 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8075 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8076 flags &~ (flagword) SEC_READONLY);
8077 if (s == NULL
8078 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8079 return false;
8082 /* Create .MIPS.xhash section. */
8083 if (info->emit_gnu_hash)
8084 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8085 flags | SEC_READONLY);
8087 /* On IRIX5, we adjust add some additional symbols and change the
8088 alignments of several sections. There is no ABI documentation
8089 indicating that this is necessary on IRIX6, nor any evidence that
8090 the linker takes such action. */
8091 if (IRIX_COMPAT (abfd) == ict_irix5)
8093 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8095 bh = NULL;
8096 if (! (_bfd_generic_link_add_one_symbol
8097 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8098 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8099 return false;
8101 h = (struct elf_link_hash_entry *) bh;
8102 h->mark = 1;
8103 h->non_elf = 0;
8104 h->def_regular = 1;
8105 h->type = STT_SECTION;
8107 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8108 return false;
8111 /* We need to create a .compact_rel section. */
8112 if (SGI_COMPAT (abfd))
8114 if (!mips_elf_create_compact_rel_section (abfd, info))
8115 return false;
8118 /* Change alignments of some sections. */
8119 s = bfd_get_linker_section (abfd, ".hash");
8120 if (s != NULL)
8121 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8123 s = bfd_get_linker_section (abfd, ".dynsym");
8124 if (s != NULL)
8125 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8127 s = bfd_get_linker_section (abfd, ".dynstr");
8128 if (s != NULL)
8129 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8131 /* ??? */
8132 s = bfd_get_section_by_name (abfd, ".reginfo");
8133 if (s != NULL)
8134 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8136 s = bfd_get_linker_section (abfd, ".dynamic");
8137 if (s != NULL)
8138 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8141 if (bfd_link_executable (info))
8143 const char *name;
8145 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8146 bh = NULL;
8147 if (!(_bfd_generic_link_add_one_symbol
8148 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8149 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8150 return false;
8152 h = (struct elf_link_hash_entry *) bh;
8153 h->non_elf = 0;
8154 h->def_regular = 1;
8155 h->type = STT_SECTION;
8157 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8158 return false;
8160 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8162 /* __rld_map is a four byte word located in the .data section
8163 and is filled in by the rtld to contain a pointer to
8164 the _r_debug structure. Its symbol value will be set in
8165 _bfd_mips_elf_finish_dynamic_symbol. */
8166 s = bfd_get_linker_section (abfd, ".rld_map");
8167 BFD_ASSERT (s != NULL);
8169 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8170 bh = NULL;
8171 if (!(_bfd_generic_link_add_one_symbol
8172 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8173 get_elf_backend_data (abfd)->collect, &bh)))
8174 return false;
8176 h = (struct elf_link_hash_entry *) bh;
8177 h->non_elf = 0;
8178 h->def_regular = 1;
8179 h->type = STT_OBJECT;
8181 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8182 return false;
8183 mips_elf_hash_table (info)->rld_symbol = h;
8187 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8188 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8189 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8190 return false;
8192 /* Do the usual VxWorks handling. */
8193 if (htab->root.target_os == is_vxworks
8194 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8195 return false;
8197 return true;
8200 /* Return true if relocation REL against section SEC is a REL rather than
8201 RELA relocation. RELOCS is the first relocation in the section and
8202 ABFD is the bfd that contains SEC. */
8204 static bool
8205 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8206 const Elf_Internal_Rela *relocs,
8207 const Elf_Internal_Rela *rel)
8209 Elf_Internal_Shdr *rel_hdr;
8210 const struct elf_backend_data *bed;
8212 /* To determine which flavor of relocation this is, we depend on the
8213 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8214 rel_hdr = elf_section_data (sec)->rel.hdr;
8215 if (rel_hdr == NULL)
8216 return false;
8217 bed = get_elf_backend_data (abfd);
8218 return ((size_t) (rel - relocs)
8219 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8222 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8223 HOWTO is the relocation's howto and CONTENTS points to the contents
8224 of the section that REL is against. */
8226 static bfd_vma
8227 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8228 const Elf_Internal_Rela *rel,
8229 reloc_howto_type *howto, bfd_byte *contents)
8231 bfd_byte *location;
8232 unsigned int r_type;
8233 bfd_vma addend;
8234 bfd_vma bytes;
8236 if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8237 return 0;
8239 r_type = ELF_R_TYPE (abfd, rel->r_info);
8240 location = contents + rel->r_offset;
8242 /* Get the addend, which is stored in the input file. */
8243 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8244 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8245 _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8247 addend = bytes & howto->src_mask;
8249 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8250 accordingly. */
8251 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8252 addend <<= 1;
8254 return addend;
8257 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8258 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8259 and update *ADDEND with the final addend. Return true on success
8260 or false if the LO16 could not be found. RELEND is the exclusive
8261 upper bound on the relocations for REL's section. */
8263 static bool
8264 mips_elf_add_lo16_rel_addend (bfd *abfd,
8265 asection *sec,
8266 const Elf_Internal_Rela *rel,
8267 const Elf_Internal_Rela *relend,
8268 bfd_byte *contents, bfd_vma *addend)
8270 unsigned int r_type, lo16_type;
8271 const Elf_Internal_Rela *lo16_relocation;
8272 reloc_howto_type *lo16_howto;
8273 bfd_vma l;
8275 r_type = ELF_R_TYPE (abfd, rel->r_info);
8276 if (mips16_reloc_p (r_type))
8277 lo16_type = R_MIPS16_LO16;
8278 else if (micromips_reloc_p (r_type))
8279 lo16_type = R_MICROMIPS_LO16;
8280 else if (r_type == R_MIPS_PCHI16)
8281 lo16_type = R_MIPS_PCLO16;
8282 else
8283 lo16_type = R_MIPS_LO16;
8285 /* The combined value is the sum of the HI16 addend, left-shifted by
8286 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8287 code does a `lui' of the HI16 value, and then an `addiu' of the
8288 LO16 value.)
8290 Scan ahead to find a matching LO16 relocation.
8292 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8293 be immediately following. However, for the IRIX6 ABI, the next
8294 relocation may be a composed relocation consisting of several
8295 relocations for the same address. In that case, the R_MIPS_LO16
8296 relocation may occur as one of these. We permit a similar
8297 extension in general, as that is useful for GCC.
8299 In some cases GCC dead code elimination removes the LO16 but keeps
8300 the corresponding HI16. This is strictly speaking a violation of
8301 the ABI but not immediately harmful. */
8302 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8303 if (lo16_relocation == NULL)
8304 return false;
8306 /* Obtain the addend kept there. */
8307 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8308 l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8309 contents);
8311 l <<= lo16_howto->rightshift;
8312 l = _bfd_mips_elf_sign_extend (l, 16);
8314 *addend <<= 16;
8315 *addend += l;
8316 return true;
8319 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8320 store the contents in *CONTENTS on success. Assume that *CONTENTS
8321 already holds the contents if it is nonull on entry. */
8323 static bool
8324 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8326 if (*contents)
8327 return true;
8329 /* Get cached copy if it exists. */
8330 if (elf_section_data (sec)->this_hdr.contents != NULL)
8332 *contents = elf_section_data (sec)->this_hdr.contents;
8333 return true;
8336 return bfd_malloc_and_get_section (abfd, sec, contents);
8339 /* Make a new PLT record to keep internal data. */
8341 static struct plt_entry *
8342 mips_elf_make_plt_record (bfd *abfd)
8344 struct plt_entry *entry;
8346 entry = bfd_zalloc (abfd, sizeof (*entry));
8347 if (entry == NULL)
8348 return NULL;
8350 entry->stub_offset = MINUS_ONE;
8351 entry->mips_offset = MINUS_ONE;
8352 entry->comp_offset = MINUS_ONE;
8353 entry->gotplt_index = MINUS_ONE;
8354 return entry;
8357 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8358 for PIC code, as otherwise there is no load-time relocation involved
8359 and local GOT entries whose value is zero at static link time will
8360 retain their value at load time. */
8362 static bool
8363 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8364 struct mips_elf_link_hash_table *htab,
8365 unsigned int r_type)
8367 union
8369 struct elf_link_hash_entry *eh;
8370 struct bfd_link_hash_entry *bh;
8372 hzero;
8374 BFD_ASSERT (!htab->use_absolute_zero);
8375 BFD_ASSERT (bfd_link_pic (info));
8377 hzero.bh = NULL;
8378 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8379 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8380 NULL, false, false, &hzero.bh))
8381 return false;
8383 BFD_ASSERT (hzero.bh != NULL);
8384 hzero.eh->size = 0;
8385 hzero.eh->type = STT_NOTYPE;
8386 hzero.eh->other = STV_PROTECTED;
8387 hzero.eh->def_regular = 1;
8388 hzero.eh->non_elf = 0;
8390 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8391 return false;
8393 htab->use_absolute_zero = true;
8395 return true;
8398 /* Look through the relocs for a section during the first phase, and
8399 allocate space in the global offset table and record the need for
8400 standard MIPS and compressed procedure linkage table entries. */
8402 bool
8403 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8404 asection *sec, const Elf_Internal_Rela *relocs)
8406 const char *name;
8407 bfd *dynobj;
8408 Elf_Internal_Shdr *symtab_hdr;
8409 struct elf_link_hash_entry **sym_hashes;
8410 size_t extsymoff;
8411 const Elf_Internal_Rela *rel;
8412 const Elf_Internal_Rela *rel_end;
8413 asection *sreloc;
8414 const struct elf_backend_data *bed;
8415 struct mips_elf_link_hash_table *htab;
8416 bfd_byte *contents;
8417 bfd_vma addend;
8418 reloc_howto_type *howto;
8420 if (bfd_link_relocatable (info))
8421 return true;
8423 htab = mips_elf_hash_table (info);
8424 BFD_ASSERT (htab != NULL);
8426 dynobj = elf_hash_table (info)->dynobj;
8427 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8428 sym_hashes = elf_sym_hashes (abfd);
8429 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8431 bed = get_elf_backend_data (abfd);
8432 rel_end = relocs + sec->reloc_count;
8434 /* Check for the mips16 stub sections. */
8436 name = bfd_section_name (sec);
8437 if (FN_STUB_P (name))
8439 unsigned long r_symndx;
8441 /* Look at the relocation information to figure out which symbol
8442 this is for. */
8444 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8445 if (r_symndx == 0)
8447 _bfd_error_handler
8448 /* xgettext:c-format */
8449 (_("%pB: warning: cannot determine the target function for"
8450 " stub section `%s'"),
8451 abfd, name);
8452 bfd_set_error (bfd_error_bad_value);
8453 return false;
8456 if (r_symndx < extsymoff
8457 || sym_hashes[r_symndx - extsymoff] == NULL)
8459 asection *o;
8461 /* This stub is for a local symbol. This stub will only be
8462 needed if there is some relocation in this BFD, other
8463 than a 16 bit function call, which refers to this symbol. */
8464 for (o = abfd->sections; o != NULL; o = o->next)
8466 Elf_Internal_Rela *sec_relocs;
8467 const Elf_Internal_Rela *r, *rend;
8469 /* We can ignore stub sections when looking for relocs. */
8470 if ((o->flags & SEC_RELOC) == 0
8471 || o->reloc_count == 0
8472 || section_allows_mips16_refs_p (o))
8473 continue;
8475 sec_relocs
8476 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8477 info->keep_memory);
8478 if (sec_relocs == NULL)
8479 return false;
8481 rend = sec_relocs + o->reloc_count;
8482 for (r = sec_relocs; r < rend; r++)
8483 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8484 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8485 break;
8487 if (elf_section_data (o)->relocs != sec_relocs)
8488 free (sec_relocs);
8490 if (r < rend)
8491 break;
8494 if (o == NULL)
8496 /* There is no non-call reloc for this stub, so we do
8497 not need it. Since this function is called before
8498 the linker maps input sections to output sections, we
8499 can easily discard it by setting the SEC_EXCLUDE
8500 flag. */
8501 sec->flags |= SEC_EXCLUDE;
8502 return true;
8505 /* Record this stub in an array of local symbol stubs for
8506 this BFD. */
8507 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8509 unsigned long symcount;
8510 asection **n;
8511 bfd_size_type amt;
8513 if (elf_bad_symtab (abfd))
8514 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8515 else
8516 symcount = symtab_hdr->sh_info;
8517 amt = symcount * sizeof (asection *);
8518 n = bfd_zalloc (abfd, amt);
8519 if (n == NULL)
8520 return false;
8521 mips_elf_tdata (abfd)->local_stubs = n;
8524 sec->flags |= SEC_KEEP;
8525 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8527 /* We don't need to set mips16_stubs_seen in this case.
8528 That flag is used to see whether we need to look through
8529 the global symbol table for stubs. We don't need to set
8530 it here, because we just have a local stub. */
8532 else
8534 struct mips_elf_link_hash_entry *h;
8536 h = ((struct mips_elf_link_hash_entry *)
8537 sym_hashes[r_symndx - extsymoff]);
8539 while (h->root.root.type == bfd_link_hash_indirect
8540 || h->root.root.type == bfd_link_hash_warning)
8541 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8543 /* H is the symbol this stub is for. */
8545 /* If we already have an appropriate stub for this function, we
8546 don't need another one, so we can discard this one. Since
8547 this function is called before the linker maps input sections
8548 to output sections, we can easily discard it by setting the
8549 SEC_EXCLUDE flag. */
8550 if (h->fn_stub != NULL)
8552 sec->flags |= SEC_EXCLUDE;
8553 return true;
8556 sec->flags |= SEC_KEEP;
8557 h->fn_stub = sec;
8558 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8561 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8563 unsigned long r_symndx;
8564 struct mips_elf_link_hash_entry *h;
8565 asection **loc;
8567 /* Look at the relocation information to figure out which symbol
8568 this is for. */
8570 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8571 if (r_symndx == 0)
8573 _bfd_error_handler
8574 /* xgettext:c-format */
8575 (_("%pB: warning: cannot determine the target function for"
8576 " stub section `%s'"),
8577 abfd, name);
8578 bfd_set_error (bfd_error_bad_value);
8579 return false;
8582 if (r_symndx < extsymoff
8583 || sym_hashes[r_symndx - extsymoff] == NULL)
8585 asection *o;
8587 /* This stub is for a local symbol. This stub will only be
8588 needed if there is some relocation (R_MIPS16_26) in this BFD
8589 that refers to this symbol. */
8590 for (o = abfd->sections; o != NULL; o = o->next)
8592 Elf_Internal_Rela *sec_relocs;
8593 const Elf_Internal_Rela *r, *rend;
8595 /* We can ignore stub sections when looking for relocs. */
8596 if ((o->flags & SEC_RELOC) == 0
8597 || o->reloc_count == 0
8598 || section_allows_mips16_refs_p (o))
8599 continue;
8601 sec_relocs
8602 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8603 info->keep_memory);
8604 if (sec_relocs == NULL)
8605 return false;
8607 rend = sec_relocs + o->reloc_count;
8608 for (r = sec_relocs; r < rend; r++)
8609 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8610 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8611 break;
8613 if (elf_section_data (o)->relocs != sec_relocs)
8614 free (sec_relocs);
8616 if (r < rend)
8617 break;
8620 if (o == NULL)
8622 /* There is no non-call reloc for this stub, so we do
8623 not need it. Since this function is called before
8624 the linker maps input sections to output sections, we
8625 can easily discard it by setting the SEC_EXCLUDE
8626 flag. */
8627 sec->flags |= SEC_EXCLUDE;
8628 return true;
8631 /* Record this stub in an array of local symbol call_stubs for
8632 this BFD. */
8633 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8635 unsigned long symcount;
8636 asection **n;
8637 bfd_size_type amt;
8639 if (elf_bad_symtab (abfd))
8640 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8641 else
8642 symcount = symtab_hdr->sh_info;
8643 amt = symcount * sizeof (asection *);
8644 n = bfd_zalloc (abfd, amt);
8645 if (n == NULL)
8646 return false;
8647 mips_elf_tdata (abfd)->local_call_stubs = n;
8650 sec->flags |= SEC_KEEP;
8651 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8653 /* We don't need to set mips16_stubs_seen in this case.
8654 That flag is used to see whether we need to look through
8655 the global symbol table for stubs. We don't need to set
8656 it here, because we just have a local stub. */
8658 else
8660 h = ((struct mips_elf_link_hash_entry *)
8661 sym_hashes[r_symndx - extsymoff]);
8663 /* H is the symbol this stub is for. */
8665 if (CALL_FP_STUB_P (name))
8666 loc = &h->call_fp_stub;
8667 else
8668 loc = &h->call_stub;
8670 /* If we already have an appropriate stub for this function, we
8671 don't need another one, so we can discard this one. Since
8672 this function is called before the linker maps input sections
8673 to output sections, we can easily discard it by setting the
8674 SEC_EXCLUDE flag. */
8675 if (*loc != NULL)
8677 sec->flags |= SEC_EXCLUDE;
8678 return true;
8681 sec->flags |= SEC_KEEP;
8682 *loc = sec;
8683 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8687 sreloc = NULL;
8688 contents = NULL;
8689 for (rel = relocs; rel < rel_end; ++rel)
8691 unsigned long r_symndx;
8692 unsigned int r_type;
8693 struct elf_link_hash_entry *h;
8694 bool can_make_dynamic_p;
8695 bool call_reloc_p;
8696 bool constrain_symbol_p;
8698 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8699 r_type = ELF_R_TYPE (abfd, rel->r_info);
8701 if (r_symndx < extsymoff)
8702 h = NULL;
8703 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8705 _bfd_error_handler
8706 /* xgettext:c-format */
8707 (_("%pB: malformed reloc detected for section %s"),
8708 abfd, name);
8709 bfd_set_error (bfd_error_bad_value);
8710 return false;
8712 else
8714 h = sym_hashes[r_symndx - extsymoff];
8715 if (h != NULL)
8717 while (h->root.type == bfd_link_hash_indirect
8718 || h->root.type == bfd_link_hash_warning)
8719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8723 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8724 relocation into a dynamic one. */
8725 can_make_dynamic_p = false;
8727 /* Set CALL_RELOC_P to true if the relocation is for a call,
8728 and if pointer equality therefore doesn't matter. */
8729 call_reloc_p = false;
8731 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8732 into account when deciding how to define the symbol. */
8733 constrain_symbol_p = true;
8735 switch (r_type)
8737 case R_MIPS_CALL16:
8738 case R_MIPS_CALL_HI16:
8739 case R_MIPS_CALL_LO16:
8740 case R_MIPS16_CALL16:
8741 case R_MICROMIPS_CALL16:
8742 case R_MICROMIPS_CALL_HI16:
8743 case R_MICROMIPS_CALL_LO16:
8744 call_reloc_p = true;
8745 /* Fall through. */
8747 case R_MIPS_GOT16:
8748 case R_MIPS_GOT_LO16:
8749 case R_MIPS_GOT_PAGE:
8750 case R_MIPS_GOT_DISP:
8751 case R_MIPS16_GOT16:
8752 case R_MICROMIPS_GOT16:
8753 case R_MICROMIPS_GOT_LO16:
8754 case R_MICROMIPS_GOT_PAGE:
8755 case R_MICROMIPS_GOT_DISP:
8756 /* If we have a symbol that will resolve to zero at static link
8757 time and it is used by a GOT relocation applied to code we
8758 cannot relax to an immediate zero load, then we will be using
8759 the special `__gnu_absolute_zero' symbol whose value is zero
8760 at dynamic load time. We ignore HI16-type GOT relocations at
8761 this stage, because their handling will depend entirely on
8762 the corresponding LO16-type GOT relocation. */
8763 if (!call_hi16_reloc_p (r_type)
8764 && h != NULL
8765 && bfd_link_pic (info)
8766 && !htab->use_absolute_zero
8767 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8769 bool rel_reloc;
8771 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8772 return false;
8774 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8775 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8776 if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8777 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8778 false))
8779 if (!mips_elf_define_absolute_zero (abfd, info, htab,
8780 r_type))
8781 return false;
8784 /* Fall through. */
8785 case R_MIPS_GOT_HI16:
8786 case R_MIPS_GOT_OFST:
8787 case R_MIPS_TLS_GOTTPREL:
8788 case R_MIPS_TLS_GD:
8789 case R_MIPS_TLS_LDM:
8790 case R_MIPS16_TLS_GOTTPREL:
8791 case R_MIPS16_TLS_GD:
8792 case R_MIPS16_TLS_LDM:
8793 case R_MICROMIPS_GOT_HI16:
8794 case R_MICROMIPS_GOT_OFST:
8795 case R_MICROMIPS_TLS_GOTTPREL:
8796 case R_MICROMIPS_TLS_GD:
8797 case R_MICROMIPS_TLS_LDM:
8798 if (dynobj == NULL)
8799 elf_hash_table (info)->dynobj = dynobj = abfd;
8800 if (!mips_elf_create_got_section (dynobj, info))
8801 return false;
8802 if (htab->root.target_os == is_vxworks
8803 && !bfd_link_pic (info))
8805 _bfd_error_handler
8806 /* xgettext:c-format */
8807 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8808 abfd, (uint64_t) rel->r_offset);
8809 bfd_set_error (bfd_error_bad_value);
8810 return false;
8812 can_make_dynamic_p = true;
8813 break;
8815 case R_MIPS_NONE:
8816 case R_MIPS_JALR:
8817 case R_MICROMIPS_JALR:
8818 /* These relocations have empty fields and are purely there to
8819 provide link information. The symbol value doesn't matter. */
8820 constrain_symbol_p = false;
8821 break;
8823 case R_MIPS_GPREL16:
8824 case R_MIPS_GPREL32:
8825 case R_MIPS16_GPREL:
8826 case R_MICROMIPS_GPREL16:
8827 /* GP-relative relocations always resolve to a definition in a
8828 regular input file, ignoring the one-definition rule. This is
8829 important for the GP setup sequence in NewABI code, which
8830 always resolves to a local function even if other relocations
8831 against the symbol wouldn't. */
8832 constrain_symbol_p = false;
8833 break;
8835 case R_MIPS_32:
8836 case R_MIPS_REL32:
8837 case R_MIPS_64:
8838 /* In VxWorks executables, references to external symbols
8839 must be handled using copy relocs or PLT entries; it is not
8840 possible to convert this relocation into a dynamic one.
8842 For executables that use PLTs and copy-relocs, we have a
8843 choice between converting the relocation into a dynamic
8844 one or using copy relocations or PLT entries. It is
8845 usually better to do the former, unless the relocation is
8846 against a read-only section. */
8847 if ((bfd_link_pic (info)
8848 || (h != NULL
8849 && htab->root.target_os != is_vxworks
8850 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8851 && !(!info->nocopyreloc
8852 && !PIC_OBJECT_P (abfd)
8853 && MIPS_ELF_READONLY_SECTION (sec))))
8854 && (sec->flags & SEC_ALLOC) != 0)
8856 can_make_dynamic_p = true;
8857 if (dynobj == NULL)
8858 elf_hash_table (info)->dynobj = dynobj = abfd;
8860 break;
8862 case R_MIPS_26:
8863 case R_MIPS_PC16:
8864 case R_MIPS_PC21_S2:
8865 case R_MIPS_PC26_S2:
8866 case R_MIPS16_26:
8867 case R_MIPS16_PC16_S1:
8868 case R_MICROMIPS_26_S1:
8869 case R_MICROMIPS_PC7_S1:
8870 case R_MICROMIPS_PC10_S1:
8871 case R_MICROMIPS_PC16_S1:
8872 case R_MICROMIPS_PC23_S2:
8873 call_reloc_p = true;
8874 break;
8877 if (h)
8879 if (constrain_symbol_p)
8881 if (!can_make_dynamic_p)
8882 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8884 if (!call_reloc_p)
8885 h->pointer_equality_needed = 1;
8887 /* We must not create a stub for a symbol that has
8888 relocations related to taking the function's address.
8889 This doesn't apply to VxWorks, where CALL relocs refer
8890 to a .got.plt entry instead of a normal .got entry. */
8891 if (htab->root.target_os != is_vxworks
8892 && (!can_make_dynamic_p || !call_reloc_p))
8893 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8896 /* Relocations against the special VxWorks __GOTT_BASE__ and
8897 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8898 room for them in .rela.dyn. */
8899 if (is_gott_symbol (info, h))
8901 if (sreloc == NULL)
8903 sreloc = mips_elf_rel_dyn_section (info, true);
8904 if (sreloc == NULL)
8905 return false;
8907 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8908 if (MIPS_ELF_READONLY_SECTION (sec))
8909 /* We tell the dynamic linker that there are
8910 relocations against the text segment. */
8911 info->flags |= DF_TEXTREL;
8914 else if (call_lo16_reloc_p (r_type)
8915 || got_lo16_reloc_p (r_type)
8916 || got_disp_reloc_p (r_type)
8917 || (got16_reloc_p (r_type)
8918 && htab->root.target_os == is_vxworks))
8920 /* We may need a local GOT entry for this relocation. We
8921 don't count R_MIPS_GOT_PAGE because we can estimate the
8922 maximum number of pages needed by looking at the size of
8923 the segment. Similar comments apply to R_MIPS*_GOT16 and
8924 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8925 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8926 R_MIPS_CALL_HI16 because these are always followed by an
8927 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8928 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8929 rel->r_addend, info, r_type))
8930 return false;
8933 if (h != NULL
8934 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8935 ELF_ST_IS_MIPS16 (h->other)))
8936 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8938 switch (r_type)
8940 case R_MIPS_CALL16:
8941 case R_MIPS16_CALL16:
8942 case R_MICROMIPS_CALL16:
8943 if (h == NULL)
8945 _bfd_error_handler
8946 /* xgettext:c-format */
8947 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8948 abfd, (uint64_t) rel->r_offset);
8949 bfd_set_error (bfd_error_bad_value);
8950 return false;
8952 /* Fall through. */
8954 case R_MIPS_CALL_HI16:
8955 case R_MIPS_CALL_LO16:
8956 case R_MICROMIPS_CALL_HI16:
8957 case R_MICROMIPS_CALL_LO16:
8958 if (h != NULL)
8960 /* Make sure there is room in the regular GOT to hold the
8961 function's address. We may eliminate it in favour of
8962 a .got.plt entry later; see mips_elf_count_got_symbols. */
8963 if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8964 r_type))
8965 return false;
8967 /* We need a stub, not a plt entry for the undefined
8968 function. But we record it as if it needs plt. See
8969 _bfd_elf_adjust_dynamic_symbol. */
8970 h->needs_plt = 1;
8971 h->type = STT_FUNC;
8973 break;
8975 case R_MIPS_GOT_PAGE:
8976 case R_MICROMIPS_GOT_PAGE:
8977 case R_MIPS16_GOT16:
8978 case R_MIPS_GOT16:
8979 case R_MIPS_GOT_HI16:
8980 case R_MIPS_GOT_LO16:
8981 case R_MICROMIPS_GOT16:
8982 case R_MICROMIPS_GOT_HI16:
8983 case R_MICROMIPS_GOT_LO16:
8984 if (!h || got_page_reloc_p (r_type))
8986 /* This relocation needs (or may need, if h != NULL) a
8987 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8988 know for sure until we know whether the symbol is
8989 preemptible. */
8990 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8992 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8993 return false;
8994 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
8995 addend = mips_elf_read_rel_addend (abfd, sec, rel,
8996 howto, contents);
8997 if (got16_reloc_p (r_type))
8998 mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
8999 contents, &addend);
9000 else
9001 addend <<= howto->rightshift;
9003 else
9004 addend = rel->r_addend;
9005 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9006 h, addend))
9007 return false;
9009 if (h)
9011 struct mips_elf_link_hash_entry *hmips =
9012 (struct mips_elf_link_hash_entry *) h;
9014 /* This symbol is definitely not overridable. */
9015 if (hmips->root.def_regular
9016 && ! (bfd_link_pic (info) && ! info->symbolic
9017 && ! hmips->root.forced_local))
9018 h = NULL;
9021 /* If this is a global, overridable symbol, GOT_PAGE will
9022 decay to GOT_DISP, so we'll need a GOT entry for it. */
9023 /* Fall through. */
9025 case R_MIPS_GOT_DISP:
9026 case R_MICROMIPS_GOT_DISP:
9027 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9028 false, r_type))
9029 return false;
9030 break;
9032 case R_MIPS_TLS_GOTTPREL:
9033 case R_MIPS16_TLS_GOTTPREL:
9034 case R_MICROMIPS_TLS_GOTTPREL:
9035 if (bfd_link_pic (info))
9036 info->flags |= DF_STATIC_TLS;
9037 /* Fall through */
9039 case R_MIPS_TLS_LDM:
9040 case R_MIPS16_TLS_LDM:
9041 case R_MICROMIPS_TLS_LDM:
9042 if (tls_ldm_reloc_p (r_type))
9044 r_symndx = STN_UNDEF;
9045 h = NULL;
9047 /* Fall through */
9049 case R_MIPS_TLS_GD:
9050 case R_MIPS16_TLS_GD:
9051 case R_MICROMIPS_TLS_GD:
9052 /* This symbol requires a global offset table entry, or two
9053 for TLS GD relocations. */
9054 if (h != NULL)
9056 if (!mips_elf_record_global_got_symbol (h, abfd, info,
9057 false, r_type))
9058 return false;
9060 else
9062 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9063 rel->r_addend,
9064 info, r_type))
9065 return false;
9067 break;
9069 case R_MIPS_32:
9070 case R_MIPS_REL32:
9071 case R_MIPS_64:
9072 /* In VxWorks executables, references to external symbols
9073 are handled using copy relocs or PLT stubs, so there's
9074 no need to add a .rela.dyn entry for this relocation. */
9075 if (can_make_dynamic_p)
9077 if (sreloc == NULL)
9079 sreloc = mips_elf_rel_dyn_section (info, true);
9080 if (sreloc == NULL)
9081 return false;
9083 if (bfd_link_pic (info) && h == NULL)
9085 /* When creating a shared object, we must copy these
9086 reloc types into the output file as R_MIPS_REL32
9087 relocs. Make room for this reloc in .rel(a).dyn. */
9088 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9089 if (MIPS_ELF_READONLY_SECTION (sec))
9090 /* We tell the dynamic linker that there are
9091 relocations against the text segment. */
9092 info->flags |= DF_TEXTREL;
9094 else
9096 struct mips_elf_link_hash_entry *hmips;
9098 /* For a shared object, we must copy this relocation
9099 unless the symbol turns out to be undefined and
9100 weak with non-default visibility, in which case
9101 it will be left as zero.
9103 We could elide R_MIPS_REL32 for locally binding symbols
9104 in shared libraries, but do not yet do so.
9106 For an executable, we only need to copy this
9107 reloc if the symbol is defined in a dynamic
9108 object. */
9109 hmips = (struct mips_elf_link_hash_entry *) h;
9110 ++hmips->possibly_dynamic_relocs;
9111 if (MIPS_ELF_READONLY_SECTION (sec))
9112 /* We need it to tell the dynamic linker if there
9113 are relocations against the text segment. */
9114 hmips->readonly_reloc = true;
9118 if (SGI_COMPAT (abfd))
9119 mips_elf_hash_table (info)->compact_rel_size +=
9120 sizeof (Elf32_External_crinfo);
9121 break;
9123 case R_MIPS_26:
9124 case R_MIPS_GPREL16:
9125 case R_MIPS_LITERAL:
9126 case R_MIPS_GPREL32:
9127 case R_MICROMIPS_26_S1:
9128 case R_MICROMIPS_GPREL16:
9129 case R_MICROMIPS_LITERAL:
9130 case R_MICROMIPS_GPREL7_S2:
9131 if (SGI_COMPAT (abfd))
9132 mips_elf_hash_table (info)->compact_rel_size +=
9133 sizeof (Elf32_External_crinfo);
9134 break;
9136 /* This relocation describes the C++ object vtable hierarchy.
9137 Reconstruct it for later use during GC. */
9138 case R_MIPS_GNU_VTINHERIT:
9139 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9140 return false;
9141 break;
9143 /* This relocation describes which C++ vtable entries are actually
9144 used. Record for later use during GC. */
9145 case R_MIPS_GNU_VTENTRY:
9146 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9147 return false;
9148 break;
9150 default:
9151 break;
9154 /* Record the need for a PLT entry. At this point we don't know
9155 yet if we are going to create a PLT in the first place, but
9156 we only record whether the relocation requires a standard MIPS
9157 or a compressed code entry anyway. If we don't make a PLT after
9158 all, then we'll just ignore these arrangements. Likewise if
9159 a PLT entry is not created because the symbol is satisfied
9160 locally. */
9161 if (h != NULL
9162 && (branch_reloc_p (r_type)
9163 || mips16_branch_reloc_p (r_type)
9164 || micromips_branch_reloc_p (r_type))
9165 && !SYMBOL_CALLS_LOCAL (info, h))
9167 if (h->plt.plist == NULL)
9168 h->plt.plist = mips_elf_make_plt_record (abfd);
9169 if (h->plt.plist == NULL)
9170 return false;
9172 if (branch_reloc_p (r_type))
9173 h->plt.plist->need_mips = true;
9174 else
9175 h->plt.plist->need_comp = true;
9178 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9179 if there is one. We only need to handle global symbols here;
9180 we decide whether to keep or delete stubs for local symbols
9181 when processing the stub's relocations. */
9182 if (h != NULL
9183 && !mips16_call_reloc_p (r_type)
9184 && !section_allows_mips16_refs_p (sec))
9186 struct mips_elf_link_hash_entry *mh;
9188 mh = (struct mips_elf_link_hash_entry *) h;
9189 mh->need_fn_stub = true;
9192 /* Refuse some position-dependent relocations when creating a
9193 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9194 not PIC, but we can create dynamic relocations and the result
9195 will be fine. Also do not refuse R_MIPS_LO16, which can be
9196 combined with R_MIPS_GOT16. */
9197 if (bfd_link_pic (info))
9199 switch (r_type)
9201 case R_MIPS_TLS_TPREL_HI16:
9202 case R_MIPS16_TLS_TPREL_HI16:
9203 case R_MICROMIPS_TLS_TPREL_HI16:
9204 case R_MIPS_TLS_TPREL_LO16:
9205 case R_MIPS16_TLS_TPREL_LO16:
9206 case R_MICROMIPS_TLS_TPREL_LO16:
9207 /* These are okay in PIE, but not in a shared library. */
9208 if (bfd_link_executable (info))
9209 break;
9211 /* FALLTHROUGH */
9213 case R_MIPS16_HI16:
9214 case R_MIPS_HI16:
9215 case R_MIPS_HIGHER:
9216 case R_MIPS_HIGHEST:
9217 case R_MICROMIPS_HI16:
9218 case R_MICROMIPS_HIGHER:
9219 case R_MICROMIPS_HIGHEST:
9220 /* Don't refuse a high part relocation if it's against
9221 no symbol (e.g. part of a compound relocation). */
9222 if (r_symndx == STN_UNDEF)
9223 break;
9225 /* Likewise an absolute symbol. */
9226 if (h != NULL && bfd_is_abs_symbol (&h->root))
9227 break;
9229 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9230 and has a special meaning. */
9231 if (!NEWABI_P (abfd) && h != NULL
9232 && strcmp (h->root.root.string, "_gp_disp") == 0)
9233 break;
9235 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9236 if (is_gott_symbol (info, h))
9237 break;
9239 /* FALLTHROUGH */
9241 case R_MIPS16_26:
9242 case R_MIPS_26:
9243 case R_MICROMIPS_26_S1:
9244 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9245 /* An error for unsupported relocations is raised as part
9246 of the above search, so we can skip the following. */
9247 if (howto != NULL)
9248 info->callbacks->einfo
9249 /* xgettext:c-format */
9250 (_("%X%H: relocation %s against `%s' cannot be used"
9251 " when making a shared object; recompile with -fPIC\n"),
9252 abfd, sec, rel->r_offset, howto->name,
9253 (h) ? h->root.root.string : "a local symbol");
9254 break;
9255 default:
9256 break;
9261 return true;
9264 /* Allocate space for global sym dynamic relocs. */
9266 static bool
9267 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9269 struct bfd_link_info *info = inf;
9270 bfd *dynobj;
9271 struct mips_elf_link_hash_entry *hmips;
9272 struct mips_elf_link_hash_table *htab;
9274 htab = mips_elf_hash_table (info);
9275 BFD_ASSERT (htab != NULL);
9277 dynobj = elf_hash_table (info)->dynobj;
9278 hmips = (struct mips_elf_link_hash_entry *) h;
9280 /* VxWorks executables are handled elsewhere; we only need to
9281 allocate relocations in shared objects. */
9282 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9283 return true;
9285 /* Ignore indirect symbols. All relocations against such symbols
9286 will be redirected to the target symbol. */
9287 if (h->root.type == bfd_link_hash_indirect)
9288 return true;
9290 /* If this symbol is defined in a dynamic object, or we are creating
9291 a shared library, we will need to copy any R_MIPS_32 or
9292 R_MIPS_REL32 relocs against it into the output file. */
9293 if (! bfd_link_relocatable (info)
9294 && hmips->possibly_dynamic_relocs != 0
9295 && (h->root.type == bfd_link_hash_defweak
9296 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9297 || bfd_link_pic (info)))
9299 bool do_copy = true;
9301 if (h->root.type == bfd_link_hash_undefweak)
9303 /* Do not copy relocations for undefined weak symbols that
9304 we are not going to export. */
9305 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9306 do_copy = false;
9308 /* Make sure undefined weak symbols are output as a dynamic
9309 symbol in PIEs. */
9310 else if (h->dynindx == -1 && !h->forced_local)
9312 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9313 return false;
9317 if (do_copy)
9319 /* Even though we don't directly need a GOT entry for this symbol,
9320 the SVR4 psABI requires it to have a dynamic symbol table
9321 index greater that DT_MIPS_GOTSYM if there are dynamic
9322 relocations against it.
9324 VxWorks does not enforce the same mapping between the GOT
9325 and the symbol table, so the same requirement does not
9326 apply there. */
9327 if (htab->root.target_os != is_vxworks)
9329 if (hmips->global_got_area > GGA_RELOC_ONLY)
9330 hmips->global_got_area = GGA_RELOC_ONLY;
9331 hmips->got_only_for_calls = false;
9334 mips_elf_allocate_dynamic_relocations
9335 (dynobj, info, hmips->possibly_dynamic_relocs);
9336 if (hmips->readonly_reloc)
9337 /* We tell the dynamic linker that there are relocations
9338 against the text segment. */
9339 info->flags |= DF_TEXTREL;
9343 return true;
9346 /* Adjust a symbol defined by a dynamic object and referenced by a
9347 regular object. The current definition is in some section of the
9348 dynamic object, but we're not including those sections. We have to
9349 change the definition to something the rest of the link can
9350 understand. */
9352 bool
9353 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9354 struct elf_link_hash_entry *h)
9356 bfd *dynobj;
9357 struct mips_elf_link_hash_entry *hmips;
9358 struct mips_elf_link_hash_table *htab;
9359 asection *s, *srel;
9361 htab = mips_elf_hash_table (info);
9362 BFD_ASSERT (htab != NULL);
9364 dynobj = elf_hash_table (info)->dynobj;
9365 hmips = (struct mips_elf_link_hash_entry *) h;
9367 /* Make sure we know what is going on here. */
9368 if (dynobj == NULL
9369 || (! h->needs_plt
9370 && ! h->is_weakalias
9371 && (! h->def_dynamic
9372 || ! h->ref_regular
9373 || h->def_regular)))
9375 if (h->type == STT_GNU_IFUNC)
9376 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9377 h->root.root.string);
9378 else
9379 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9380 h->root.root.string);
9381 return true;
9384 hmips = (struct mips_elf_link_hash_entry *) h;
9386 /* If there are call relocations against an externally-defined symbol,
9387 see whether we can create a MIPS lazy-binding stub for it. We can
9388 only do this if all references to the function are through call
9389 relocations, and in that case, the traditional lazy-binding stubs
9390 are much more efficient than PLT entries.
9392 Traditional stubs are only available on SVR4 psABI-based systems;
9393 VxWorks always uses PLTs instead. */
9394 if (htab->root.target_os != is_vxworks
9395 && h->needs_plt
9396 && !hmips->no_fn_stub)
9398 if (! elf_hash_table (info)->dynamic_sections_created)
9399 return true;
9401 /* If this symbol is not defined in a regular file, then set
9402 the symbol to the stub location. This is required to make
9403 function pointers compare as equal between the normal
9404 executable and the shared library. */
9405 if (!h->def_regular
9406 && !bfd_is_abs_section (htab->sstubs->output_section))
9408 hmips->needs_lazy_stub = true;
9409 htab->lazy_stub_count++;
9410 return true;
9413 /* As above, VxWorks requires PLT entries for externally-defined
9414 functions that are only accessed through call relocations.
9416 Both VxWorks and non-VxWorks targets also need PLT entries if there
9417 are static-only relocations against an externally-defined function.
9418 This can technically occur for shared libraries if there are
9419 branches to the symbol, although it is unlikely that this will be
9420 used in practice due to the short ranges involved. It can occur
9421 for any relative or absolute relocation in executables; in that
9422 case, the PLT entry becomes the function's canonical address. */
9423 else if (((h->needs_plt && !hmips->no_fn_stub)
9424 || (h->type == STT_FUNC && hmips->has_static_relocs))
9425 && htab->use_plts_and_copy_relocs
9426 && !SYMBOL_CALLS_LOCAL (info, h)
9427 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9428 && h->root.type == bfd_link_hash_undefweak))
9430 bool micromips_p = MICROMIPS_P (info->output_bfd);
9431 bool newabi_p = NEWABI_P (info->output_bfd);
9433 /* If this is the first symbol to need a PLT entry, then make some
9434 basic setup. Also work out PLT entry sizes. We'll need them
9435 for PLT offset calculations. */
9436 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9438 BFD_ASSERT (htab->root.sgotplt->size == 0);
9439 BFD_ASSERT (htab->plt_got_index == 0);
9441 /* If we're using the PLT additions to the psABI, each PLT
9442 entry is 16 bytes and the PLT0 entry is 32 bytes.
9443 Encourage better cache usage by aligning. We do this
9444 lazily to avoid pessimizing traditional objects. */
9445 if (htab->root.target_os != is_vxworks
9446 && !bfd_set_section_alignment (htab->root.splt, 5))
9447 return false;
9449 /* Make sure that .got.plt is word-aligned. We do this lazily
9450 for the same reason as above. */
9451 if (!bfd_set_section_alignment (htab->root.sgotplt,
9452 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9453 return false;
9455 /* On non-VxWorks targets, the first two entries in .got.plt
9456 are reserved. */
9457 if (htab->root.target_os != is_vxworks)
9458 htab->plt_got_index
9459 += (get_elf_backend_data (dynobj)->got_header_size
9460 / MIPS_ELF_GOT_SIZE (dynobj));
9462 /* On VxWorks, also allocate room for the header's
9463 .rela.plt.unloaded entries. */
9464 if (htab->root.target_os == is_vxworks
9465 && !bfd_link_pic (info))
9466 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9468 /* Now work out the sizes of individual PLT entries. */
9469 if (htab->root.target_os == is_vxworks
9470 && bfd_link_pic (info))
9471 htab->plt_mips_entry_size
9472 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9473 else if (htab->root.target_os == is_vxworks)
9474 htab->plt_mips_entry_size
9475 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9476 else if (newabi_p)
9477 htab->plt_mips_entry_size
9478 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9479 else if (!micromips_p)
9481 htab->plt_mips_entry_size
9482 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9483 htab->plt_comp_entry_size
9484 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9486 else if (htab->insn32)
9488 htab->plt_mips_entry_size
9489 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9490 htab->plt_comp_entry_size
9491 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9493 else
9495 htab->plt_mips_entry_size
9496 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9497 htab->plt_comp_entry_size
9498 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9502 if (h->plt.plist == NULL)
9503 h->plt.plist = mips_elf_make_plt_record (dynobj);
9504 if (h->plt.plist == NULL)
9505 return false;
9507 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9508 n32 or n64, so always use a standard entry there.
9510 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9511 all MIPS16 calls will go via that stub, and there is no benefit
9512 to having a MIPS16 entry. And in the case of call_stub a
9513 standard entry actually has to be used as the stub ends with a J
9514 instruction. */
9515 if (newabi_p
9516 || htab->root.target_os == is_vxworks
9517 || hmips->call_stub
9518 || hmips->call_fp_stub)
9520 h->plt.plist->need_mips = true;
9521 h->plt.plist->need_comp = false;
9524 /* Otherwise, if there are no direct calls to the function, we
9525 have a free choice of whether to use standard or compressed
9526 entries. Prefer microMIPS entries if the object is known to
9527 contain microMIPS code, so that it becomes possible to create
9528 pure microMIPS binaries. Prefer standard entries otherwise,
9529 because MIPS16 ones are no smaller and are usually slower. */
9530 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9532 if (micromips_p)
9533 h->plt.plist->need_comp = true;
9534 else
9535 h->plt.plist->need_mips = true;
9538 if (h->plt.plist->need_mips)
9540 h->plt.plist->mips_offset = htab->plt_mips_offset;
9541 htab->plt_mips_offset += htab->plt_mips_entry_size;
9543 if (h->plt.plist->need_comp)
9545 h->plt.plist->comp_offset = htab->plt_comp_offset;
9546 htab->plt_comp_offset += htab->plt_comp_entry_size;
9549 /* Reserve the corresponding .got.plt entry now too. */
9550 h->plt.plist->gotplt_index = htab->plt_got_index++;
9552 /* If the output file has no definition of the symbol, set the
9553 symbol's value to the address of the stub. */
9554 if (!bfd_link_pic (info) && !h->def_regular)
9555 hmips->use_plt_entry = true;
9557 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9558 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9559 ? MIPS_ELF_RELA_SIZE (dynobj)
9560 : MIPS_ELF_REL_SIZE (dynobj));
9562 /* Make room for the .rela.plt.unloaded relocations. */
9563 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9564 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9566 /* All relocations against this symbol that could have been made
9567 dynamic will now refer to the PLT entry instead. */
9568 hmips->possibly_dynamic_relocs = 0;
9570 return true;
9573 /* If this is a weak symbol, and there is a real definition, the
9574 processor independent code will have arranged for us to see the
9575 real definition first, and we can just use the same value. */
9576 if (h->is_weakalias)
9578 struct elf_link_hash_entry *def = weakdef (h);
9579 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9580 h->root.u.def.section = def->root.u.def.section;
9581 h->root.u.def.value = def->root.u.def.value;
9582 return true;
9585 /* Otherwise, there is nothing further to do for symbols defined
9586 in regular objects. */
9587 if (h->def_regular)
9588 return true;
9590 /* There's also nothing more to do if we'll convert all relocations
9591 against this symbol into dynamic relocations. */
9592 if (!hmips->has_static_relocs)
9593 return true;
9595 /* We're now relying on copy relocations. Complain if we have
9596 some that we can't convert. */
9597 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9599 _bfd_error_handler (_("non-dynamic relocations refer to "
9600 "dynamic symbol %s"),
9601 h->root.root.string);
9602 bfd_set_error (bfd_error_bad_value);
9603 return false;
9606 /* We must allocate the symbol in our .dynbss section, which will
9607 become part of the .bss section of the executable. There will be
9608 an entry for this symbol in the .dynsym section. The dynamic
9609 object will contain position independent code, so all references
9610 from the dynamic object to this symbol will go through the global
9611 offset table. The dynamic linker will use the .dynsym entry to
9612 determine the address it must put in the global offset table, so
9613 both the dynamic object and the regular object will refer to the
9614 same memory location for the variable. */
9616 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9618 s = htab->root.sdynrelro;
9619 srel = htab->root.sreldynrelro;
9621 else
9623 s = htab->root.sdynbss;
9624 srel = htab->root.srelbss;
9626 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9628 if (htab->root.target_os == is_vxworks)
9629 srel->size += sizeof (Elf32_External_Rela);
9630 else
9631 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9632 h->needs_copy = 1;
9635 /* All relocations against this symbol that could have been made
9636 dynamic will now refer to the local copy instead. */
9637 hmips->possibly_dynamic_relocs = 0;
9639 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9642 /* This function is called after all the input files have been read,
9643 and the input sections have been assigned to output sections. We
9644 check for any mips16 stub sections that we can discard. */
9646 bool
9647 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9648 struct bfd_link_info *info)
9650 asection *sect;
9651 struct mips_elf_link_hash_table *htab;
9652 struct mips_htab_traverse_info hti;
9654 htab = mips_elf_hash_table (info);
9655 BFD_ASSERT (htab != NULL);
9657 /* The .reginfo section has a fixed size. */
9658 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9659 if (sect != NULL)
9661 bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
9662 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9665 /* The .MIPS.abiflags section has a fixed size. */
9666 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9667 if (sect != NULL)
9669 bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
9670 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9673 hti.info = info;
9674 hti.output_bfd = output_bfd;
9675 hti.error = false;
9676 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9677 mips_elf_check_symbols, &hti);
9678 if (hti.error)
9679 return false;
9681 return true;
9684 /* If the link uses a GOT, lay it out and work out its size. */
9686 static bool
9687 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9689 bfd *dynobj;
9690 asection *s;
9691 struct mips_got_info *g;
9692 bfd_size_type loadable_size = 0;
9693 bfd_size_type page_gotno;
9694 bfd *ibfd;
9695 struct mips_elf_traverse_got_arg tga;
9696 struct mips_elf_link_hash_table *htab;
9698 htab = mips_elf_hash_table (info);
9699 BFD_ASSERT (htab != NULL);
9701 s = htab->root.sgot;
9702 if (s == NULL)
9703 return true;
9705 dynobj = elf_hash_table (info)->dynobj;
9706 g = htab->got_info;
9708 /* Allocate room for the reserved entries. VxWorks always reserves
9709 3 entries; other objects only reserve 2 entries. */
9710 BFD_ASSERT (g->assigned_low_gotno == 0);
9711 if (htab->root.target_os == is_vxworks)
9712 htab->reserved_gotno = 3;
9713 else
9714 htab->reserved_gotno = 2;
9715 g->local_gotno += htab->reserved_gotno;
9716 g->assigned_low_gotno = htab->reserved_gotno;
9718 /* Decide which symbols need to go in the global part of the GOT and
9719 count the number of reloc-only GOT symbols. */
9720 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9722 if (!mips_elf_resolve_final_got_entries (info, g))
9723 return false;
9725 /* Calculate the total loadable size of the output. That
9726 will give us the maximum number of GOT_PAGE entries
9727 required. */
9728 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9730 asection *subsection;
9732 for (subsection = ibfd->sections;
9733 subsection;
9734 subsection = subsection->next)
9736 if ((subsection->flags & SEC_ALLOC) == 0)
9737 continue;
9738 loadable_size += ((subsection->size + 0xf)
9739 &~ (bfd_size_type) 0xf);
9743 if (htab->root.target_os == is_vxworks)
9744 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9745 relocations against local symbols evaluate to "G", and the EABI does
9746 not include R_MIPS_GOT_PAGE. */
9747 page_gotno = 0;
9748 else
9749 /* Assume there are two loadable segments consisting of contiguous
9750 sections. Is 5 enough? */
9751 page_gotno = (loadable_size >> 16) + 5;
9753 /* Choose the smaller of the two page estimates; both are intended to be
9754 conservative. */
9755 if (page_gotno > g->page_gotno)
9756 page_gotno = g->page_gotno;
9758 g->local_gotno += page_gotno;
9759 g->assigned_high_gotno = g->local_gotno - 1;
9761 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9762 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9763 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9765 /* VxWorks does not support multiple GOTs. It initializes $gp to
9766 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9767 dynamic loader. */
9768 if (htab->root.target_os != is_vxworks
9769 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9771 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9772 return false;
9774 else
9776 /* Record that all bfds use G. This also has the effect of freeing
9777 the per-bfd GOTs, which we no longer need. */
9778 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9779 if (mips_elf_bfd_got (ibfd, false))
9780 mips_elf_replace_bfd_got (ibfd, g);
9781 mips_elf_replace_bfd_got (output_bfd, g);
9783 /* Set up TLS entries. */
9784 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9785 tga.info = info;
9786 tga.g = g;
9787 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9788 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9789 if (!tga.g)
9790 return false;
9791 BFD_ASSERT (g->tls_assigned_gotno
9792 == g->global_gotno + g->local_gotno + g->tls_gotno);
9794 /* Each VxWorks GOT entry needs an explicit relocation. */
9795 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9796 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9798 /* Allocate room for the TLS relocations. */
9799 if (g->relocs)
9800 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9803 return true;
9806 /* Estimate the size of the .MIPS.stubs section. */
9808 static void
9809 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9811 struct mips_elf_link_hash_table *htab;
9812 bfd_size_type dynsymcount;
9814 htab = mips_elf_hash_table (info);
9815 BFD_ASSERT (htab != NULL);
9817 if (htab->lazy_stub_count == 0)
9818 return;
9820 /* IRIX rld assumes that a function stub isn't at the end of the .text
9821 section, so add a dummy entry to the end. */
9822 htab->lazy_stub_count++;
9824 /* Get a worst-case estimate of the number of dynamic symbols needed.
9825 At this point, dynsymcount does not account for section symbols
9826 and count_section_dynsyms may overestimate the number that will
9827 be needed. */
9828 dynsymcount = (elf_hash_table (info)->dynsymcount
9829 + count_section_dynsyms (output_bfd, info));
9831 /* Determine the size of one stub entry. There's no disadvantage
9832 from using microMIPS code here, so for the sake of pure-microMIPS
9833 binaries we prefer it whenever there's any microMIPS code in
9834 output produced at all. This has a benefit of stubs being
9835 shorter by 4 bytes each too, unless in the insn32 mode. */
9836 if (!MICROMIPS_P (output_bfd))
9837 htab->function_stub_size = (dynsymcount > 0x10000
9838 ? MIPS_FUNCTION_STUB_BIG_SIZE
9839 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9840 else if (htab->insn32)
9841 htab->function_stub_size = (dynsymcount > 0x10000
9842 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9843 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9844 else
9845 htab->function_stub_size = (dynsymcount > 0x10000
9846 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9847 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9849 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9852 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9853 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9854 stub, allocate an entry in the stubs section. */
9856 static bool
9857 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9859 struct mips_htab_traverse_info *hti = data;
9860 struct mips_elf_link_hash_table *htab;
9861 struct bfd_link_info *info;
9862 bfd *output_bfd;
9864 info = hti->info;
9865 output_bfd = hti->output_bfd;
9866 htab = mips_elf_hash_table (info);
9867 BFD_ASSERT (htab != NULL);
9869 if (h->needs_lazy_stub)
9871 bool micromips_p = MICROMIPS_P (output_bfd);
9872 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9873 bfd_vma isa_bit = micromips_p;
9875 BFD_ASSERT (htab->root.dynobj != NULL);
9876 if (h->root.plt.plist == NULL)
9877 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9878 if (h->root.plt.plist == NULL)
9880 hti->error = true;
9881 return false;
9883 h->root.root.u.def.section = htab->sstubs;
9884 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9885 h->root.plt.plist->stub_offset = htab->sstubs->size;
9886 h->root.other = other;
9887 htab->sstubs->size += htab->function_stub_size;
9889 return true;
9892 /* Allocate offsets in the stubs section to each symbol that needs one.
9893 Set the final size of the .MIPS.stub section. */
9895 static bool
9896 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9898 bfd *output_bfd = info->output_bfd;
9899 bool micromips_p = MICROMIPS_P (output_bfd);
9900 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9901 bfd_vma isa_bit = micromips_p;
9902 struct mips_elf_link_hash_table *htab;
9903 struct mips_htab_traverse_info hti;
9904 struct elf_link_hash_entry *h;
9905 bfd *dynobj;
9907 htab = mips_elf_hash_table (info);
9908 BFD_ASSERT (htab != NULL);
9910 if (htab->lazy_stub_count == 0)
9911 return true;
9913 htab->sstubs->size = 0;
9914 hti.info = info;
9915 hti.output_bfd = output_bfd;
9916 hti.error = false;
9917 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9918 if (hti.error)
9919 return false;
9920 htab->sstubs->size += htab->function_stub_size;
9921 BFD_ASSERT (htab->sstubs->size
9922 == htab->lazy_stub_count * htab->function_stub_size);
9924 dynobj = elf_hash_table (info)->dynobj;
9925 BFD_ASSERT (dynobj != NULL);
9926 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9927 if (h == NULL)
9928 return false;
9929 h->root.u.def.value = isa_bit;
9930 h->other = other;
9931 h->type = STT_FUNC;
9933 return true;
9936 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9937 bfd_link_info. If H uses the address of a PLT entry as the value
9938 of the symbol, then set the entry in the symbol table now. Prefer
9939 a standard MIPS PLT entry. */
9941 static bool
9942 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9944 struct bfd_link_info *info = data;
9945 bool micromips_p = MICROMIPS_P (info->output_bfd);
9946 struct mips_elf_link_hash_table *htab;
9947 unsigned int other;
9948 bfd_vma isa_bit;
9949 bfd_vma val;
9951 htab = mips_elf_hash_table (info);
9952 BFD_ASSERT (htab != NULL);
9954 if (h->use_plt_entry)
9956 BFD_ASSERT (h->root.plt.plist != NULL);
9957 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9958 || h->root.plt.plist->comp_offset != MINUS_ONE);
9960 val = htab->plt_header_size;
9961 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9963 isa_bit = 0;
9964 val += h->root.plt.plist->mips_offset;
9965 other = 0;
9967 else
9969 isa_bit = 1;
9970 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9971 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9973 val += isa_bit;
9974 /* For VxWorks, point at the PLT load stub rather than the lazy
9975 resolution stub; this stub will become the canonical function
9976 address. */
9977 if (htab->root.target_os == is_vxworks)
9978 val += 8;
9980 h->root.root.u.def.section = htab->root.splt;
9981 h->root.root.u.def.value = val;
9982 h->root.other = other;
9985 return true;
9988 /* Set the sizes of the dynamic sections. */
9990 bool
9991 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9992 struct bfd_link_info *info)
9994 bfd *dynobj;
9995 asection *s, *sreldyn;
9996 bool reltext;
9997 struct mips_elf_link_hash_table *htab;
9999 htab = mips_elf_hash_table (info);
10000 BFD_ASSERT (htab != NULL);
10001 dynobj = elf_hash_table (info)->dynobj;
10002 BFD_ASSERT (dynobj != NULL);
10004 if (elf_hash_table (info)->dynamic_sections_created)
10006 /* Set the contents of the .interp section to the interpreter. */
10007 if (bfd_link_executable (info) && !info->nointerp)
10009 s = bfd_get_linker_section (dynobj, ".interp");
10010 BFD_ASSERT (s != NULL);
10011 s->size
10012 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10013 s->contents
10014 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10017 /* Figure out the size of the PLT header if we know that we
10018 are using it. For the sake of cache alignment always use
10019 a standard header whenever any standard entries are present
10020 even if microMIPS entries are present as well. This also
10021 lets the microMIPS header rely on the value of $v0 only set
10022 by microMIPS entries, for a small size reduction.
10024 Set symbol table entry values for symbols that use the
10025 address of their PLT entry now that we can calculate it.
10027 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10028 haven't already in _bfd_elf_create_dynamic_sections. */
10029 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10031 bool micromips_p = (MICROMIPS_P (output_bfd)
10032 && !htab->plt_mips_offset);
10033 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10034 bfd_vma isa_bit = micromips_p;
10035 struct elf_link_hash_entry *h;
10036 bfd_vma size;
10038 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10039 BFD_ASSERT (htab->root.sgotplt->size == 0);
10040 BFD_ASSERT (htab->root.splt->size == 0);
10042 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10043 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10044 else if (htab->root.target_os == is_vxworks)
10045 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10046 else if (ABI_64_P (output_bfd))
10047 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10048 else if (ABI_N32_P (output_bfd))
10049 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10050 else if (!micromips_p)
10051 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10052 else if (htab->insn32)
10053 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10054 else
10055 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10057 htab->plt_header_is_comp = micromips_p;
10058 htab->plt_header_size = size;
10059 htab->root.splt->size = (size
10060 + htab->plt_mips_offset
10061 + htab->plt_comp_offset);
10062 htab->root.sgotplt->size = (htab->plt_got_index
10063 * MIPS_ELF_GOT_SIZE (dynobj));
10065 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10067 if (htab->root.hplt == NULL)
10069 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10070 "_PROCEDURE_LINKAGE_TABLE_");
10071 htab->root.hplt = h;
10072 if (h == NULL)
10073 return false;
10076 h = htab->root.hplt;
10077 h->root.u.def.value = isa_bit;
10078 h->other = other;
10079 h->type = STT_FUNC;
10083 /* Allocate space for global sym dynamic relocs. */
10084 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10086 mips_elf_estimate_stub_size (output_bfd, info);
10088 if (!mips_elf_lay_out_got (output_bfd, info))
10089 return false;
10091 mips_elf_lay_out_lazy_stubs (info);
10093 /* The check_relocs and adjust_dynamic_symbol entry points have
10094 determined the sizes of the various dynamic sections. Allocate
10095 memory for them. */
10096 reltext = false;
10097 for (s = dynobj->sections; s != NULL; s = s->next)
10099 const char *name;
10101 /* It's OK to base decisions on the section name, because none
10102 of the dynobj section names depend upon the input files. */
10103 name = bfd_section_name (s);
10105 if ((s->flags & SEC_LINKER_CREATED) == 0)
10106 continue;
10108 if (startswith (name, ".rel"))
10110 if (s->size != 0)
10112 const char *outname;
10113 asection *target;
10115 /* If this relocation section applies to a read only
10116 section, then we probably need a DT_TEXTREL entry.
10117 If the relocation section is .rel(a).dyn, we always
10118 assert a DT_TEXTREL entry rather than testing whether
10119 there exists a relocation to a read only section or
10120 not. */
10121 outname = bfd_section_name (s->output_section);
10122 target = bfd_get_section_by_name (output_bfd, outname + 4);
10123 if ((target != NULL
10124 && (target->flags & SEC_READONLY) != 0
10125 && (target->flags & SEC_ALLOC) != 0)
10126 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10127 reltext = true;
10129 /* We use the reloc_count field as a counter if we need
10130 to copy relocs into the output file. */
10131 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10132 s->reloc_count = 0;
10134 /* If combreloc is enabled, elf_link_sort_relocs() will
10135 sort relocations, but in a different way than we do,
10136 and before we're done creating relocations. Also, it
10137 will move them around between input sections'
10138 relocation's contents, so our sorting would be
10139 broken, so don't let it run. */
10140 info->combreloc = 0;
10143 else if (bfd_link_executable (info)
10144 && ! mips_elf_hash_table (info)->use_rld_obj_head
10145 && startswith (name, ".rld_map"))
10147 /* We add a room for __rld_map. It will be filled in by the
10148 rtld to contain a pointer to the _r_debug structure. */
10149 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10151 else if (SGI_COMPAT (output_bfd)
10152 && startswith (name, ".compact_rel"))
10153 s->size += mips_elf_hash_table (info)->compact_rel_size;
10154 else if (s == htab->root.splt)
10156 /* If the last PLT entry has a branch delay slot, allocate
10157 room for an extra nop to fill the delay slot. This is
10158 for CPUs without load interlocking. */
10159 if (! LOAD_INTERLOCKS_P (output_bfd)
10160 && htab->root.target_os != is_vxworks
10161 && s->size > 0)
10162 s->size += 4;
10164 else if (! startswith (name, ".init")
10165 && s != htab->root.sgot
10166 && s != htab->root.sgotplt
10167 && s != htab->sstubs
10168 && s != htab->root.sdynbss
10169 && s != htab->root.sdynrelro)
10171 /* It's not one of our sections, so don't allocate space. */
10172 continue;
10175 if (s->size == 0)
10177 s->flags |= SEC_EXCLUDE;
10178 continue;
10181 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10182 continue;
10184 /* Allocate memory for the section contents. */
10185 s->contents = bfd_zalloc (dynobj, s->size);
10186 if (s->contents == NULL)
10188 bfd_set_error (bfd_error_no_memory);
10189 return false;
10193 if (elf_hash_table (info)->dynamic_sections_created)
10195 /* Add some entries to the .dynamic section. We fill in the
10196 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10197 must add the entries now so that we get the correct size for
10198 the .dynamic section. */
10200 /* SGI object has the equivalence of DT_DEBUG in the
10201 DT_MIPS_RLD_MAP entry. This must come first because glibc
10202 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10203 may only look at the first one they see. */
10204 if (!bfd_link_pic (info)
10205 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10206 return false;
10208 if (bfd_link_executable (info)
10209 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10210 return false;
10212 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10213 used by the debugger. */
10214 if (bfd_link_executable (info)
10215 && !SGI_COMPAT (output_bfd)
10216 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10217 return false;
10219 if (reltext
10220 && (SGI_COMPAT (output_bfd)
10221 || htab->root.target_os == is_vxworks))
10222 info->flags |= DF_TEXTREL;
10224 if ((info->flags & DF_TEXTREL) != 0)
10226 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10227 return false;
10229 /* Clear the DF_TEXTREL flag. It will be set again if we
10230 write out an actual text relocation; we may not, because
10231 at this point we do not know whether e.g. any .eh_frame
10232 absolute relocations have been converted to PC-relative. */
10233 info->flags &= ~DF_TEXTREL;
10236 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10237 return false;
10239 sreldyn = mips_elf_rel_dyn_section (info, false);
10240 if (htab->root.target_os == is_vxworks)
10242 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10243 use any of the DT_MIPS_* tags. */
10244 if (sreldyn && sreldyn->size > 0)
10246 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10247 return false;
10249 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10250 return false;
10252 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10253 return false;
10256 else
10258 if (sreldyn && sreldyn->size > 0
10259 && !bfd_is_abs_section (sreldyn->output_section))
10261 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10262 return false;
10264 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10265 return false;
10267 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10268 return false;
10271 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10272 return false;
10274 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10275 return false;
10277 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10278 return false;
10280 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10281 return false;
10283 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10284 return false;
10286 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10287 return false;
10289 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10290 return false;
10292 if (info->emit_gnu_hash
10293 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10294 return false;
10296 if (IRIX_COMPAT (dynobj) == ict_irix5
10297 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10298 return false;
10300 if (IRIX_COMPAT (dynobj) == ict_irix6
10301 && (bfd_get_section_by_name
10302 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10303 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10304 return false;
10306 if (htab->root.splt->size > 0)
10308 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10309 return false;
10311 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10312 return false;
10314 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10315 return false;
10317 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10318 return false;
10320 if (htab->root.target_os == is_vxworks
10321 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10322 return false;
10325 return true;
10328 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10329 Adjust its R_ADDEND field so that it is correct for the output file.
10330 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10331 and sections respectively; both use symbol indexes. */
10333 static void
10334 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10335 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10336 asection **local_sections, Elf_Internal_Rela *rel)
10338 unsigned int r_type, r_symndx;
10339 Elf_Internal_Sym *sym;
10340 asection *sec;
10342 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10344 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10345 if (gprel16_reloc_p (r_type)
10346 || r_type == R_MIPS_GPREL32
10347 || literal_reloc_p (r_type))
10349 rel->r_addend += _bfd_get_gp_value (input_bfd);
10350 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10353 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10354 sym = local_syms + r_symndx;
10356 /* Adjust REL's addend to account for section merging. */
10357 if (!bfd_link_relocatable (info))
10359 sec = local_sections[r_symndx];
10360 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10363 /* This would normally be done by the rela_normal code in elflink.c. */
10364 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10365 rel->r_addend += local_sections[r_symndx]->output_offset;
10369 /* Handle relocations against symbols from removed linkonce sections,
10370 or sections discarded by a linker script. We use this wrapper around
10371 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10372 on 64-bit ELF targets. In this case for any relocation handled, which
10373 always be the first in a triplet, the remaining two have to be processed
10374 together with the first, even if they are R_MIPS_NONE. It is the symbol
10375 index referred by the first reloc that applies to all the three and the
10376 remaining two never refer to an object symbol. And it is the final
10377 relocation (the last non-null one) that determines the output field of
10378 the whole relocation so retrieve the corresponding howto structure for
10379 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10381 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10382 and therefore requires to be pasted in a loop. It also defines a block
10383 and does not protect any of its arguments, hence the extra brackets. */
10385 static void
10386 mips_reloc_against_discarded_section (bfd *output_bfd,
10387 struct bfd_link_info *info,
10388 bfd *input_bfd, asection *input_section,
10389 Elf_Internal_Rela **rel,
10390 const Elf_Internal_Rela **relend,
10391 bool rel_reloc,
10392 reloc_howto_type *howto,
10393 bfd_byte *contents)
10395 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10396 int count = bed->s->int_rels_per_ext_rel;
10397 unsigned int r_type;
10398 int i;
10400 for (i = count - 1; i > 0; i--)
10402 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10403 if (r_type != R_MIPS_NONE)
10405 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10406 break;
10411 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10412 (*rel), count, (*relend),
10413 howto, i, contents);
10415 while (0);
10418 /* Relocate a MIPS ELF section. */
10421 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10422 bfd *input_bfd, asection *input_section,
10423 bfd_byte *contents, Elf_Internal_Rela *relocs,
10424 Elf_Internal_Sym *local_syms,
10425 asection **local_sections)
10427 Elf_Internal_Rela *rel;
10428 const Elf_Internal_Rela *relend;
10429 bfd_vma addend = 0;
10430 bool use_saved_addend_p = false;
10432 relend = relocs + input_section->reloc_count;
10433 for (rel = relocs; rel < relend; ++rel)
10435 const char *name;
10436 bfd_vma value = 0;
10437 reloc_howto_type *howto;
10438 bool cross_mode_jump_p = false;
10439 /* TRUE if the relocation is a RELA relocation, rather than a
10440 REL relocation. */
10441 bool rela_relocation_p = true;
10442 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10443 const char *msg;
10444 unsigned long r_symndx;
10445 asection *sec;
10446 Elf_Internal_Shdr *symtab_hdr;
10447 struct elf_link_hash_entry *h;
10448 bool rel_reloc;
10450 rel_reloc = (NEWABI_P (input_bfd)
10451 && mips_elf_rel_relocation_p (input_bfd, input_section,
10452 relocs, rel));
10453 /* Find the relocation howto for this relocation. */
10454 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10456 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10457 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10458 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10460 sec = local_sections[r_symndx];
10461 h = NULL;
10463 else
10465 unsigned long extsymoff;
10467 extsymoff = 0;
10468 if (!elf_bad_symtab (input_bfd))
10469 extsymoff = symtab_hdr->sh_info;
10470 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10471 while (h->root.type == bfd_link_hash_indirect
10472 || h->root.type == bfd_link_hash_warning)
10473 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10475 sec = NULL;
10476 if (h->root.type == bfd_link_hash_defined
10477 || h->root.type == bfd_link_hash_defweak)
10478 sec = h->root.u.def.section;
10481 if (sec != NULL && discarded_section (sec))
10483 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10484 input_section, &rel, &relend,
10485 rel_reloc, howto, contents);
10486 continue;
10489 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10491 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10492 64-bit code, but make sure all their addresses are in the
10493 lowermost or uppermost 32-bit section of the 64-bit address
10494 space. Thus, when they use an R_MIPS_64 they mean what is
10495 usually meant by R_MIPS_32, with the exception that the
10496 stored value is sign-extended to 64 bits. */
10497 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10499 /* On big-endian systems, we need to lie about the position
10500 of the reloc. */
10501 if (bfd_big_endian (input_bfd))
10502 rel->r_offset += 4;
10505 if (!use_saved_addend_p)
10507 /* If these relocations were originally of the REL variety,
10508 we must pull the addend out of the field that will be
10509 relocated. Otherwise, we simply use the contents of the
10510 RELA relocation. */
10511 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10512 relocs, rel))
10514 rela_relocation_p = false;
10515 addend = mips_elf_read_rel_addend (input_bfd, input_section,
10516 rel, howto, contents);
10517 if (hi16_reloc_p (r_type)
10518 || (got16_reloc_p (r_type)
10519 && mips_elf_local_relocation_p (input_bfd, rel,
10520 local_sections)))
10522 if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10523 rel, relend,
10524 contents, &addend))
10526 if (h)
10527 name = h->root.root.string;
10528 else
10529 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10530 local_syms + r_symndx,
10531 sec);
10532 _bfd_error_handler
10533 /* xgettext:c-format */
10534 (_("%pB: can't find matching LO16 reloc against `%s'"
10535 " for %s at %#" PRIx64 " in section `%pA'"),
10536 input_bfd, name,
10537 howto->name, (uint64_t) rel->r_offset, input_section);
10540 else
10541 addend <<= howto->rightshift;
10543 else
10544 addend = rel->r_addend;
10545 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10546 local_syms, local_sections, rel);
10549 if (bfd_link_relocatable (info))
10551 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10552 && bfd_big_endian (input_bfd))
10553 rel->r_offset -= 4;
10555 if (!rela_relocation_p && rel->r_addend)
10557 addend += rel->r_addend;
10558 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10559 addend = mips_elf_high (addend);
10560 else if (r_type == R_MIPS_HIGHER)
10561 addend = mips_elf_higher (addend);
10562 else if (r_type == R_MIPS_HIGHEST)
10563 addend = mips_elf_highest (addend);
10564 else
10565 addend >>= howto->rightshift;
10567 /* We use the source mask, rather than the destination
10568 mask because the place to which we are writing will be
10569 source of the addend in the final link. */
10570 addend &= howto->src_mask;
10572 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10573 /* See the comment above about using R_MIPS_64 in the 32-bit
10574 ABI. Here, we need to update the addend. It would be
10575 possible to get away with just using the R_MIPS_32 reloc
10576 but for endianness. */
10578 bfd_vma sign_bits;
10579 bfd_vma low_bits;
10580 bfd_vma high_bits;
10582 if (addend & ((bfd_vma) 1 << 31))
10583 #ifdef BFD64
10584 sign_bits = ((bfd_vma) 1 << 32) - 1;
10585 #else
10586 sign_bits = -1;
10587 #endif
10588 else
10589 sign_bits = 0;
10591 /* If we don't know that we have a 64-bit type,
10592 do two separate stores. */
10593 if (bfd_big_endian (input_bfd))
10595 /* Store the sign-bits (which are most significant)
10596 first. */
10597 low_bits = sign_bits;
10598 high_bits = addend;
10600 else
10602 low_bits = addend;
10603 high_bits = sign_bits;
10605 bfd_put_32 (input_bfd, low_bits,
10606 contents + rel->r_offset);
10607 bfd_put_32 (input_bfd, high_bits,
10608 contents + rel->r_offset + 4);
10609 continue;
10612 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10613 input_bfd, input_section,
10614 contents, false))
10615 return false;
10618 /* Go on to the next relocation. */
10619 continue;
10622 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10623 relocations for the same offset. In that case we are
10624 supposed to treat the output of each relocation as the addend
10625 for the next. */
10626 if (rel + 1 < relend
10627 && rel->r_offset == rel[1].r_offset
10628 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10629 use_saved_addend_p = true;
10630 else
10631 use_saved_addend_p = false;
10633 /* Figure out what value we are supposed to relocate. */
10634 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10635 input_section, contents,
10636 info, rel, addend, howto,
10637 local_syms, local_sections,
10638 &value, &name, &cross_mode_jump_p,
10639 use_saved_addend_p))
10641 case bfd_reloc_continue:
10642 /* There's nothing to do. */
10643 continue;
10645 case bfd_reloc_undefined:
10646 /* mips_elf_calculate_relocation already called the
10647 undefined_symbol callback. There's no real point in
10648 trying to perform the relocation at this point, so we
10649 just skip ahead to the next relocation. */
10650 continue;
10652 case bfd_reloc_notsupported:
10653 msg = _("internal error: unsupported relocation error");
10654 info->callbacks->warning
10655 (info, msg, name, input_bfd, input_section, rel->r_offset);
10656 return false;
10658 case bfd_reloc_overflow:
10659 if (use_saved_addend_p)
10660 /* Ignore overflow until we reach the last relocation for
10661 a given location. */
10663 else
10665 struct mips_elf_link_hash_table *htab;
10667 htab = mips_elf_hash_table (info);
10668 BFD_ASSERT (htab != NULL);
10669 BFD_ASSERT (name != NULL);
10670 if (!htab->small_data_overflow_reported
10671 && (gprel16_reloc_p (howto->type)
10672 || literal_reloc_p (howto->type)))
10674 msg = _("small-data section exceeds 64KB;"
10675 " lower small-data size limit (see option -G)");
10677 htab->small_data_overflow_reported = true;
10678 (*info->callbacks->einfo) ("%P: %s\n", msg);
10680 (*info->callbacks->reloc_overflow)
10681 (info, NULL, name, howto->name, (bfd_vma) 0,
10682 input_bfd, input_section, rel->r_offset);
10684 break;
10686 case bfd_reloc_ok:
10687 break;
10689 case bfd_reloc_outofrange:
10690 msg = NULL;
10691 if (jal_reloc_p (howto->type))
10692 msg = (cross_mode_jump_p
10693 ? _("cannot convert a jump to JALX "
10694 "for a non-word-aligned address")
10695 : (howto->type == R_MIPS16_26
10696 ? _("jump to a non-word-aligned address")
10697 : _("jump to a non-instruction-aligned address")));
10698 else if (b_reloc_p (howto->type))
10699 msg = (cross_mode_jump_p
10700 ? _("cannot convert a branch to JALX "
10701 "for a non-word-aligned address")
10702 : _("branch to a non-instruction-aligned address"));
10703 else if (aligned_pcrel_reloc_p (howto->type))
10704 msg = _("PC-relative load from unaligned address");
10705 if (msg)
10707 info->callbacks->einfo
10708 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10709 break;
10711 /* Fall through. */
10713 default:
10714 abort ();
10715 break;
10718 /* If we've got another relocation for the address, keep going
10719 until we reach the last one. */
10720 if (use_saved_addend_p)
10722 addend = value;
10723 continue;
10726 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10727 /* See the comment above about using R_MIPS_64 in the 32-bit
10728 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10729 that calculated the right value. Now, however, we
10730 sign-extend the 32-bit result to 64-bits, and store it as a
10731 64-bit value. We are especially generous here in that we
10732 go to extreme lengths to support this usage on systems with
10733 only a 32-bit VMA. */
10735 bfd_vma sign_bits;
10736 bfd_vma low_bits;
10737 bfd_vma high_bits;
10739 if (value & ((bfd_vma) 1 << 31))
10740 #ifdef BFD64
10741 sign_bits = ((bfd_vma) 1 << 32) - 1;
10742 #else
10743 sign_bits = -1;
10744 #endif
10745 else
10746 sign_bits = 0;
10748 /* If we don't know that we have a 64-bit type,
10749 do two separate stores. */
10750 if (bfd_big_endian (input_bfd))
10752 /* Undo what we did above. */
10753 rel->r_offset -= 4;
10754 /* Store the sign-bits (which are most significant)
10755 first. */
10756 low_bits = sign_bits;
10757 high_bits = value;
10759 else
10761 low_bits = value;
10762 high_bits = sign_bits;
10764 bfd_put_32 (input_bfd, low_bits,
10765 contents + rel->r_offset);
10766 bfd_put_32 (input_bfd, high_bits,
10767 contents + rel->r_offset + 4);
10768 continue;
10771 /* Actually perform the relocation. */
10772 if (! mips_elf_perform_relocation (info, howto, rel, value,
10773 input_bfd, input_section,
10774 contents, cross_mode_jump_p))
10775 return false;
10778 return true;
10781 /* A function that iterates over each entry in la25_stubs and fills
10782 in the code for each one. DATA points to a mips_htab_traverse_info. */
10784 static int
10785 mips_elf_create_la25_stub (void **slot, void *data)
10787 struct mips_htab_traverse_info *hti;
10788 struct mips_elf_link_hash_table *htab;
10789 struct mips_elf_la25_stub *stub;
10790 asection *s;
10791 bfd_byte *loc;
10792 bfd_vma offset, target, target_high, target_low;
10793 bfd_vma branch_pc;
10794 bfd_signed_vma pcrel_offset = 0;
10796 stub = (struct mips_elf_la25_stub *) *slot;
10797 hti = (struct mips_htab_traverse_info *) data;
10798 htab = mips_elf_hash_table (hti->info);
10799 BFD_ASSERT (htab != NULL);
10801 /* Create the section contents, if we haven't already. */
10802 s = stub->stub_section;
10803 loc = s->contents;
10804 if (loc == NULL)
10806 loc = bfd_malloc (s->size);
10807 if (loc == NULL)
10809 hti->error = true;
10810 return false;
10812 s->contents = loc;
10815 /* Work out where in the section this stub should go. */
10816 offset = stub->offset;
10818 /* We add 8 here to account for the LUI/ADDIU instructions
10819 before the branch instruction. This cannot be moved down to
10820 where pcrel_offset is calculated as 's' is updated in
10821 mips_elf_get_la25_target. */
10822 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10824 /* Work out the target address. */
10825 target = mips_elf_get_la25_target (stub, &s);
10826 target += s->output_section->vma + s->output_offset;
10828 target_high = ((target + 0x8000) >> 16) & 0xffff;
10829 target_low = (target & 0xffff);
10831 /* Calculate the PC of the compact branch instruction (for the case where
10832 compact branches are used for either microMIPSR6 or MIPSR6 with
10833 compact branches. Add 4-bytes to account for BC using the PC of the
10834 next instruction as the base. */
10835 pcrel_offset = target - (branch_pc + 4);
10837 if (stub->stub_section != htab->strampoline)
10839 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10840 of the section and write the two instructions at the end. */
10841 memset (loc, 0, offset);
10842 loc += offset;
10843 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10845 bfd_put_micromips_32 (hti->output_bfd,
10846 LA25_LUI_MICROMIPS (target_high),
10847 loc);
10848 bfd_put_micromips_32 (hti->output_bfd,
10849 LA25_ADDIU_MICROMIPS (target_low),
10850 loc + 4);
10852 else
10854 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10855 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10858 else
10860 /* This is trampoline. */
10861 loc += offset;
10862 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10864 bfd_put_micromips_32 (hti->output_bfd,
10865 LA25_LUI_MICROMIPS (target_high), loc);
10866 bfd_put_micromips_32 (hti->output_bfd,
10867 LA25_J_MICROMIPS (target), loc + 4);
10868 bfd_put_micromips_32 (hti->output_bfd,
10869 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10870 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10872 else
10874 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10875 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10877 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10878 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10880 else
10882 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10883 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10885 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10888 return true;
10891 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10892 adjust it appropriately now. */
10894 static void
10895 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10896 const char *name, Elf_Internal_Sym *sym)
10898 /* The linker script takes care of providing names and values for
10899 these, but we must place them into the right sections. */
10900 static const char* const text_section_symbols[] = {
10901 "_ftext",
10902 "_etext",
10903 "__dso_displacement",
10904 "__elf_header",
10905 "__program_header_table",
10906 NULL
10909 static const char* const data_section_symbols[] = {
10910 "_fdata",
10911 "_edata",
10912 "_end",
10913 "_fbss",
10914 NULL
10917 const char* const *p;
10918 int i;
10920 for (i = 0; i < 2; ++i)
10921 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10923 ++p)
10924 if (strcmp (*p, name) == 0)
10926 /* All of these symbols are given type STT_SECTION by the
10927 IRIX6 linker. */
10928 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10929 sym->st_other = STO_PROTECTED;
10931 /* The IRIX linker puts these symbols in special sections. */
10932 if (i == 0)
10933 sym->st_shndx = SHN_MIPS_TEXT;
10934 else
10935 sym->st_shndx = SHN_MIPS_DATA;
10937 break;
10941 /* Finish up dynamic symbol handling. We set the contents of various
10942 dynamic sections here. */
10944 bool
10945 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10946 struct bfd_link_info *info,
10947 struct elf_link_hash_entry *h,
10948 Elf_Internal_Sym *sym)
10950 bfd *dynobj;
10951 asection *sgot;
10952 struct mips_got_info *g, *gg;
10953 const char *name;
10954 int idx;
10955 struct mips_elf_link_hash_table *htab;
10956 struct mips_elf_link_hash_entry *hmips;
10958 htab = mips_elf_hash_table (info);
10959 BFD_ASSERT (htab != NULL);
10960 dynobj = elf_hash_table (info)->dynobj;
10961 hmips = (struct mips_elf_link_hash_entry *) h;
10963 BFD_ASSERT (htab->root.target_os != is_vxworks);
10965 if (h->plt.plist != NULL
10966 && (h->plt.plist->mips_offset != MINUS_ONE
10967 || h->plt.plist->comp_offset != MINUS_ONE))
10969 /* We've decided to create a PLT entry for this symbol. */
10970 bfd_byte *loc;
10971 bfd_vma header_address, got_address;
10972 bfd_vma got_address_high, got_address_low, load;
10973 bfd_vma got_index;
10974 bfd_vma isa_bit;
10976 got_index = h->plt.plist->gotplt_index;
10978 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10979 BFD_ASSERT (h->dynindx != -1);
10980 BFD_ASSERT (htab->root.splt != NULL);
10981 BFD_ASSERT (got_index != MINUS_ONE);
10982 BFD_ASSERT (!h->def_regular);
10984 /* Calculate the address of the PLT header. */
10985 isa_bit = htab->plt_header_is_comp;
10986 header_address = (htab->root.splt->output_section->vma
10987 + htab->root.splt->output_offset + isa_bit);
10989 /* Calculate the address of the .got.plt entry. */
10990 got_address = (htab->root.sgotplt->output_section->vma
10991 + htab->root.sgotplt->output_offset
10992 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10994 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10995 got_address_low = got_address & 0xffff;
10997 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10998 cannot be loaded in two instructions. */
10999 if (ABI_64_P (output_bfd)
11000 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11002 _bfd_error_handler
11003 /* xgettext:c-format */
11004 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11005 "supported; consider using `-Ttext-segment=...'"),
11006 output_bfd,
11007 htab->root.sgotplt->output_section,
11008 (int64_t) got_address);
11009 bfd_set_error (bfd_error_no_error);
11010 return false;
11013 /* Initially point the .got.plt entry at the PLT header. */
11014 loc = (htab->root.sgotplt->contents
11015 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11016 if (ABI_64_P (output_bfd))
11017 bfd_put_64 (output_bfd, header_address, loc);
11018 else
11019 bfd_put_32 (output_bfd, header_address, loc);
11021 /* Now handle the PLT itself. First the standard entry (the order
11022 does not matter, we just have to pick one). */
11023 if (h->plt.plist->mips_offset != MINUS_ONE)
11025 const bfd_vma *plt_entry;
11026 bfd_vma plt_offset;
11028 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11030 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11032 /* Find out where the .plt entry should go. */
11033 loc = htab->root.splt->contents + plt_offset;
11035 /* Pick the load opcode. */
11036 load = MIPS_ELF_LOAD_WORD (output_bfd);
11038 /* Fill in the PLT entry itself. */
11040 if (MIPSR6_P (output_bfd))
11041 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11042 : mipsr6_exec_plt_entry;
11043 else
11044 plt_entry = mips_exec_plt_entry;
11045 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11046 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11047 loc + 4);
11049 if (! LOAD_INTERLOCKS_P (output_bfd)
11050 || (MIPSR6_P (output_bfd) && htab->compact_branches))
11052 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11053 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11055 else
11057 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11058 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11059 loc + 12);
11063 /* Now the compressed entry. They come after any standard ones. */
11064 if (h->plt.plist->comp_offset != MINUS_ONE)
11066 bfd_vma plt_offset;
11068 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11069 + h->plt.plist->comp_offset);
11071 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11073 /* Find out where the .plt entry should go. */
11074 loc = htab->root.splt->contents + plt_offset;
11076 /* Fill in the PLT entry itself. */
11077 if (!MICROMIPS_P (output_bfd))
11079 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11081 bfd_put_16 (output_bfd, plt_entry[0], loc);
11082 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11083 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11084 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11085 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11086 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11087 bfd_put_32 (output_bfd, got_address, loc + 12);
11089 else if (htab->insn32)
11091 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11093 bfd_put_16 (output_bfd, plt_entry[0], loc);
11094 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11095 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11096 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11097 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11098 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11099 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11100 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11102 else
11104 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11105 bfd_signed_vma gotpc_offset;
11106 bfd_vma loc_address;
11108 BFD_ASSERT (got_address % 4 == 0);
11110 loc_address = (htab->root.splt->output_section->vma
11111 + htab->root.splt->output_offset + plt_offset);
11112 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11114 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11115 if (gotpc_offset + 0x1000000 >= 0x2000000)
11117 _bfd_error_handler
11118 /* xgettext:c-format */
11119 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11120 "beyond the range of ADDIUPC"),
11121 output_bfd,
11122 htab->root.sgotplt->output_section,
11123 (int64_t) gotpc_offset,
11124 htab->root.splt->output_section);
11125 bfd_set_error (bfd_error_no_error);
11126 return false;
11128 bfd_put_16 (output_bfd,
11129 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11130 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11131 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11132 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11133 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11134 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11138 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11139 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11140 got_index - 2, h->dynindx,
11141 R_MIPS_JUMP_SLOT, got_address);
11143 /* We distinguish between PLT entries and lazy-binding stubs by
11144 giving the former an st_other value of STO_MIPS_PLT. Set the
11145 flag and leave the value if there are any relocations in the
11146 binary where pointer equality matters. */
11147 sym->st_shndx = SHN_UNDEF;
11148 if (h->pointer_equality_needed)
11149 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11150 else
11152 sym->st_value = 0;
11153 sym->st_other = 0;
11157 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11159 /* We've decided to create a lazy-binding stub. */
11160 bool micromips_p = MICROMIPS_P (output_bfd);
11161 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11162 bfd_vma stub_size = htab->function_stub_size;
11163 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11164 bfd_vma isa_bit = micromips_p;
11165 bfd_vma stub_big_size;
11167 if (!micromips_p)
11168 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11169 else if (htab->insn32)
11170 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11171 else
11172 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11174 /* This symbol has a stub. Set it up. */
11176 BFD_ASSERT (h->dynindx != -1);
11178 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11180 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11181 sign extension at runtime in the stub, resulting in a negative
11182 index value. */
11183 if (h->dynindx & ~0x7fffffff)
11184 return false;
11186 /* Fill the stub. */
11187 if (micromips_p)
11189 idx = 0;
11190 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11191 stub + idx);
11192 idx += 4;
11193 if (htab->insn32)
11195 bfd_put_micromips_32 (output_bfd,
11196 STUB_MOVE32_MICROMIPS, stub + idx);
11197 idx += 4;
11199 else
11201 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11202 idx += 2;
11204 if (stub_size == stub_big_size)
11206 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11208 bfd_put_micromips_32 (output_bfd,
11209 STUB_LUI_MICROMIPS (dynindx_hi),
11210 stub + idx);
11211 idx += 4;
11213 if (htab->insn32)
11215 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11216 stub + idx);
11217 idx += 4;
11219 else
11221 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11222 idx += 2;
11225 /* If a large stub is not required and sign extension is not a
11226 problem, then use legacy code in the stub. */
11227 if (stub_size == stub_big_size)
11228 bfd_put_micromips_32 (output_bfd,
11229 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11230 stub + idx);
11231 else if (h->dynindx & ~0x7fff)
11232 bfd_put_micromips_32 (output_bfd,
11233 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11234 stub + idx);
11235 else
11236 bfd_put_micromips_32 (output_bfd,
11237 STUB_LI16S_MICROMIPS (output_bfd,
11238 h->dynindx),
11239 stub + idx);
11241 else
11243 idx = 0;
11244 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11245 idx += 4;
11246 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11247 idx += 4;
11248 if (stub_size == stub_big_size)
11250 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11251 stub + idx);
11252 idx += 4;
11255 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11257 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11258 idx += 4;
11261 /* If a large stub is not required and sign extension is not a
11262 problem, then use legacy code in the stub. */
11263 if (stub_size == stub_big_size)
11264 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11265 stub + idx);
11266 else if (h->dynindx & ~0x7fff)
11267 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11268 stub + idx);
11269 else
11270 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11271 stub + idx);
11272 idx += 4;
11274 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11275 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11278 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11279 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11280 stub, stub_size);
11282 /* Mark the symbol as undefined. stub_offset != -1 occurs
11283 only for the referenced symbol. */
11284 sym->st_shndx = SHN_UNDEF;
11286 /* The run-time linker uses the st_value field of the symbol
11287 to reset the global offset table entry for this external
11288 to its stub address when unlinking a shared object. */
11289 sym->st_value = (htab->sstubs->output_section->vma
11290 + htab->sstubs->output_offset
11291 + h->plt.plist->stub_offset
11292 + isa_bit);
11293 sym->st_other = other;
11296 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11297 refer to the stub, since only the stub uses the standard calling
11298 conventions. */
11299 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11301 BFD_ASSERT (hmips->need_fn_stub);
11302 sym->st_value = (hmips->fn_stub->output_section->vma
11303 + hmips->fn_stub->output_offset);
11304 sym->st_size = hmips->fn_stub->size;
11305 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11308 BFD_ASSERT (h->dynindx != -1
11309 || h->forced_local);
11311 sgot = htab->root.sgot;
11312 g = htab->got_info;
11313 BFD_ASSERT (g != NULL);
11315 /* Run through the global symbol table, creating GOT entries for all
11316 the symbols that need them. */
11317 if (hmips->global_got_area != GGA_NONE)
11319 bfd_vma offset;
11320 bfd_vma value;
11322 value = sym->st_value;
11323 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11324 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11327 if (hmips->global_got_area != GGA_NONE && g->next)
11329 struct mips_got_entry e, *p;
11330 bfd_vma entry;
11331 bfd_vma offset;
11333 gg = g;
11335 e.abfd = output_bfd;
11336 e.symndx = -1;
11337 e.d.h = hmips;
11338 e.tls_type = GOT_TLS_NONE;
11340 for (g = g->next; g->next != gg; g = g->next)
11342 if (g->got_entries
11343 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11344 &e)))
11346 offset = p->gotidx;
11347 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11348 if (bfd_link_pic (info)
11349 || (elf_hash_table (info)->dynamic_sections_created
11350 && p->d.h != NULL
11351 && p->d.h->root.def_dynamic
11352 && !p->d.h->root.def_regular))
11354 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11355 the various compatibility problems, it's easier to mock
11356 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11357 mips_elf_create_dynamic_relocation to calculate the
11358 appropriate addend. */
11359 Elf_Internal_Rela rel[3];
11361 memset (rel, 0, sizeof (rel));
11362 if (ABI_64_P (output_bfd))
11363 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11364 else
11365 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11366 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11368 entry = 0;
11369 if (! (mips_elf_create_dynamic_relocation
11370 (output_bfd, info, rel,
11371 e.d.h, NULL, sym->st_value, &entry, sgot)))
11372 return false;
11374 else
11375 entry = sym->st_value;
11376 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11381 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11382 name = h->root.root.string;
11383 if (h == elf_hash_table (info)->hdynamic
11384 || h == elf_hash_table (info)->hgot)
11385 sym->st_shndx = SHN_ABS;
11386 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11387 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11389 sym->st_shndx = SHN_ABS;
11390 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11391 sym->st_value = 1;
11393 else if (SGI_COMPAT (output_bfd))
11395 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11396 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11398 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11399 sym->st_other = STO_PROTECTED;
11400 sym->st_value = 0;
11401 sym->st_shndx = SHN_MIPS_DATA;
11403 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11405 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11406 sym->st_other = STO_PROTECTED;
11407 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11408 sym->st_shndx = SHN_ABS;
11410 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11412 if (h->type == STT_FUNC)
11413 sym->st_shndx = SHN_MIPS_TEXT;
11414 else if (h->type == STT_OBJECT)
11415 sym->st_shndx = SHN_MIPS_DATA;
11419 /* Emit a copy reloc, if needed. */
11420 if (h->needs_copy)
11422 asection *s;
11423 bfd_vma symval;
11425 BFD_ASSERT (h->dynindx != -1);
11426 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11428 s = mips_elf_rel_dyn_section (info, false);
11429 symval = (h->root.u.def.section->output_section->vma
11430 + h->root.u.def.section->output_offset
11431 + h->root.u.def.value);
11432 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11433 h->dynindx, R_MIPS_COPY, symval);
11436 /* Handle the IRIX6-specific symbols. */
11437 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11438 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11440 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11441 to treat compressed symbols like any other. */
11442 if (ELF_ST_IS_MIPS16 (sym->st_other))
11444 BFD_ASSERT (sym->st_value & 1);
11445 sym->st_other -= STO_MIPS16;
11447 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11449 BFD_ASSERT (sym->st_value & 1);
11450 sym->st_other -= STO_MICROMIPS;
11453 return true;
11456 /* Likewise, for VxWorks. */
11458 bool
11459 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11460 struct bfd_link_info *info,
11461 struct elf_link_hash_entry *h,
11462 Elf_Internal_Sym *sym)
11464 bfd *dynobj;
11465 asection *sgot;
11466 struct mips_got_info *g;
11467 struct mips_elf_link_hash_table *htab;
11468 struct mips_elf_link_hash_entry *hmips;
11470 htab = mips_elf_hash_table (info);
11471 BFD_ASSERT (htab != NULL);
11472 dynobj = elf_hash_table (info)->dynobj;
11473 hmips = (struct mips_elf_link_hash_entry *) h;
11475 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11477 bfd_byte *loc;
11478 bfd_vma plt_address, got_address, got_offset, branch_offset;
11479 Elf_Internal_Rela rel;
11480 static const bfd_vma *plt_entry;
11481 bfd_vma gotplt_index;
11482 bfd_vma plt_offset;
11484 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11485 gotplt_index = h->plt.plist->gotplt_index;
11487 BFD_ASSERT (h->dynindx != -1);
11488 BFD_ASSERT (htab->root.splt != NULL);
11489 BFD_ASSERT (gotplt_index != MINUS_ONE);
11490 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11492 /* Calculate the address of the .plt entry. */
11493 plt_address = (htab->root.splt->output_section->vma
11494 + htab->root.splt->output_offset
11495 + plt_offset);
11497 /* Calculate the address of the .got.plt entry. */
11498 got_address = (htab->root.sgotplt->output_section->vma
11499 + htab->root.sgotplt->output_offset
11500 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11502 /* Calculate the offset of the .got.plt entry from
11503 _GLOBAL_OFFSET_TABLE_. */
11504 got_offset = mips_elf_gotplt_index (info, h);
11506 /* Calculate the offset for the branch at the start of the PLT
11507 entry. The branch jumps to the beginning of .plt. */
11508 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11510 /* Fill in the initial value of the .got.plt entry. */
11511 bfd_put_32 (output_bfd, plt_address,
11512 (htab->root.sgotplt->contents
11513 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11515 /* Find out where the .plt entry should go. */
11516 loc = htab->root.splt->contents + plt_offset;
11518 if (bfd_link_pic (info))
11520 plt_entry = mips_vxworks_shared_plt_entry;
11521 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11522 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11524 else
11526 bfd_vma got_address_high, got_address_low;
11528 plt_entry = mips_vxworks_exec_plt_entry;
11529 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11530 got_address_low = got_address & 0xffff;
11532 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11533 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11534 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11535 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11536 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11537 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11538 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11539 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11541 loc = (htab->srelplt2->contents
11542 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11544 /* Emit a relocation for the .got.plt entry. */
11545 rel.r_offset = got_address;
11546 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11547 rel.r_addend = plt_offset;
11548 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11550 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11551 loc += sizeof (Elf32_External_Rela);
11552 rel.r_offset = plt_address + 8;
11553 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11554 rel.r_addend = got_offset;
11555 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11557 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11558 loc += sizeof (Elf32_External_Rela);
11559 rel.r_offset += 4;
11560 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11561 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11564 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11565 loc = (htab->root.srelplt->contents
11566 + gotplt_index * sizeof (Elf32_External_Rela));
11567 rel.r_offset = got_address;
11568 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11569 rel.r_addend = 0;
11570 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11572 if (!h->def_regular)
11573 sym->st_shndx = SHN_UNDEF;
11576 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11578 sgot = htab->root.sgot;
11579 g = htab->got_info;
11580 BFD_ASSERT (g != NULL);
11582 /* See if this symbol has an entry in the GOT. */
11583 if (hmips->global_got_area != GGA_NONE)
11585 bfd_vma offset;
11586 Elf_Internal_Rela outrel;
11587 bfd_byte *loc;
11588 asection *s;
11590 /* Install the symbol value in the GOT. */
11591 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11592 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11594 /* Add a dynamic relocation for it. */
11595 s = mips_elf_rel_dyn_section (info, false);
11596 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11597 outrel.r_offset = (sgot->output_section->vma
11598 + sgot->output_offset
11599 + offset);
11600 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11601 outrel.r_addend = 0;
11602 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11605 /* Emit a copy reloc, if needed. */
11606 if (h->needs_copy)
11608 Elf_Internal_Rela rel;
11609 asection *srel;
11610 bfd_byte *loc;
11612 BFD_ASSERT (h->dynindx != -1);
11614 rel.r_offset = (h->root.u.def.section->output_section->vma
11615 + h->root.u.def.section->output_offset
11616 + h->root.u.def.value);
11617 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11618 rel.r_addend = 0;
11619 if (h->root.u.def.section == htab->root.sdynrelro)
11620 srel = htab->root.sreldynrelro;
11621 else
11622 srel = htab->root.srelbss;
11623 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11624 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11625 ++srel->reloc_count;
11628 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11629 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11630 sym->st_value &= ~1;
11632 return true;
11635 /* Write out a plt0 entry to the beginning of .plt. */
11637 static bool
11638 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11640 bfd_byte *loc;
11641 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11642 static const bfd_vma *plt_entry;
11643 struct mips_elf_link_hash_table *htab;
11645 htab = mips_elf_hash_table (info);
11646 BFD_ASSERT (htab != NULL);
11648 if (ABI_64_P (output_bfd))
11649 plt_entry = (htab->compact_branches
11650 ? mipsr6_n64_exec_plt0_entry_compact
11651 : mips_n64_exec_plt0_entry);
11652 else if (ABI_N32_P (output_bfd))
11653 plt_entry = (htab->compact_branches
11654 ? mipsr6_n32_exec_plt0_entry_compact
11655 : mips_n32_exec_plt0_entry);
11656 else if (!htab->plt_header_is_comp)
11657 plt_entry = (htab->compact_branches
11658 ? mipsr6_o32_exec_plt0_entry_compact
11659 : mips_o32_exec_plt0_entry);
11660 else if (htab->insn32)
11661 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11662 else
11663 plt_entry = micromips_o32_exec_plt0_entry;
11665 /* Calculate the value of .got.plt. */
11666 gotplt_value = (htab->root.sgotplt->output_section->vma
11667 + htab->root.sgotplt->output_offset);
11668 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11669 gotplt_value_low = gotplt_value & 0xffff;
11671 /* The PLT sequence is not safe for N64 if .got.plt's address can
11672 not be loaded in two instructions. */
11673 if (ABI_64_P (output_bfd)
11674 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11676 _bfd_error_handler
11677 /* xgettext:c-format */
11678 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11679 "supported; consider using `-Ttext-segment=...'"),
11680 output_bfd,
11681 htab->root.sgotplt->output_section,
11682 (int64_t) gotplt_value);
11683 bfd_set_error (bfd_error_no_error);
11684 return false;
11687 /* Install the PLT header. */
11688 loc = htab->root.splt->contents;
11689 if (plt_entry == micromips_o32_exec_plt0_entry)
11691 bfd_vma gotpc_offset;
11692 bfd_vma loc_address;
11693 size_t i;
11695 BFD_ASSERT (gotplt_value % 4 == 0);
11697 loc_address = (htab->root.splt->output_section->vma
11698 + htab->root.splt->output_offset);
11699 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11701 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11702 if (gotpc_offset + 0x1000000 >= 0x2000000)
11704 _bfd_error_handler
11705 /* xgettext:c-format */
11706 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11707 "beyond the range of ADDIUPC"),
11708 output_bfd,
11709 htab->root.sgotplt->output_section,
11710 (int64_t) gotpc_offset,
11711 htab->root.splt->output_section);
11712 bfd_set_error (bfd_error_no_error);
11713 return false;
11715 bfd_put_16 (output_bfd,
11716 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11717 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11718 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11719 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11721 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11723 size_t i;
11725 bfd_put_16 (output_bfd, plt_entry[0], loc);
11726 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11727 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11728 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11729 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11730 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11731 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11732 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11734 else
11736 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11737 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11738 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11739 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11740 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11741 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11742 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11743 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11746 return true;
11749 /* Install the PLT header for a VxWorks executable and finalize the
11750 contents of .rela.plt.unloaded. */
11752 static void
11753 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11755 Elf_Internal_Rela rela;
11756 bfd_byte *loc;
11757 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11758 static const bfd_vma *plt_entry;
11759 struct mips_elf_link_hash_table *htab;
11761 htab = mips_elf_hash_table (info);
11762 BFD_ASSERT (htab != NULL);
11764 plt_entry = mips_vxworks_exec_plt0_entry;
11766 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11767 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11768 + htab->root.hgot->root.u.def.section->output_offset
11769 + htab->root.hgot->root.u.def.value);
11771 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11772 got_value_low = got_value & 0xffff;
11774 /* Calculate the address of the PLT header. */
11775 plt_address = (htab->root.splt->output_section->vma
11776 + htab->root.splt->output_offset);
11778 /* Install the PLT header. */
11779 loc = htab->root.splt->contents;
11780 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11781 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11782 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11783 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11784 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11785 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11787 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11788 loc = htab->srelplt2->contents;
11789 rela.r_offset = plt_address;
11790 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11791 rela.r_addend = 0;
11792 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11793 loc += sizeof (Elf32_External_Rela);
11795 /* Output the relocation for the following addiu of
11796 %lo(_GLOBAL_OFFSET_TABLE_). */
11797 rela.r_offset += 4;
11798 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11799 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11800 loc += sizeof (Elf32_External_Rela);
11802 /* Fix up the remaining relocations. They may have the wrong
11803 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11804 in which symbols were output. */
11805 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11807 Elf_Internal_Rela rel;
11809 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11810 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11811 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11812 loc += sizeof (Elf32_External_Rela);
11814 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11815 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11816 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11817 loc += sizeof (Elf32_External_Rela);
11819 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11820 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11821 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11822 loc += sizeof (Elf32_External_Rela);
11826 /* Install the PLT header for a VxWorks shared library. */
11828 static void
11829 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11831 unsigned int i;
11832 struct mips_elf_link_hash_table *htab;
11834 htab = mips_elf_hash_table (info);
11835 BFD_ASSERT (htab != NULL);
11837 /* We just need to copy the entry byte-by-byte. */
11838 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11839 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11840 htab->root.splt->contents + i * 4);
11843 /* Finish up the dynamic sections. */
11845 bool
11846 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11847 struct bfd_link_info *info)
11849 bfd *dynobj;
11850 asection *sdyn;
11851 asection *sgot;
11852 struct mips_got_info *gg, *g;
11853 struct mips_elf_link_hash_table *htab;
11855 htab = mips_elf_hash_table (info);
11856 BFD_ASSERT (htab != NULL);
11858 dynobj = elf_hash_table (info)->dynobj;
11860 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11862 sgot = htab->root.sgot;
11863 gg = htab->got_info;
11865 if (elf_hash_table (info)->dynamic_sections_created)
11867 bfd_byte *b;
11868 int dyn_to_skip = 0, dyn_skipped = 0;
11870 BFD_ASSERT (sdyn != NULL);
11871 BFD_ASSERT (gg != NULL);
11873 g = mips_elf_bfd_got (output_bfd, false);
11874 BFD_ASSERT (g != NULL);
11876 for (b = sdyn->contents;
11877 b < sdyn->contents + sdyn->size;
11878 b += MIPS_ELF_DYN_SIZE (dynobj))
11880 Elf_Internal_Dyn dyn;
11881 const char *name;
11882 size_t elemsize;
11883 asection *s;
11884 bool swap_out_p;
11886 /* Read in the current dynamic entry. */
11887 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11889 /* Assume that we're going to modify it and write it out. */
11890 swap_out_p = true;
11892 switch (dyn.d_tag)
11894 case DT_RELENT:
11895 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11896 break;
11898 case DT_RELAENT:
11899 BFD_ASSERT (htab->root.target_os == is_vxworks);
11900 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11901 break;
11903 case DT_STRSZ:
11904 /* Rewrite DT_STRSZ. */
11905 dyn.d_un.d_val =
11906 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11907 break;
11909 case DT_PLTGOT:
11910 s = htab->root.sgot;
11911 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11912 break;
11914 case DT_MIPS_PLTGOT:
11915 s = htab->root.sgotplt;
11916 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11917 break;
11919 case DT_MIPS_RLD_VERSION:
11920 dyn.d_un.d_val = 1; /* XXX */
11921 break;
11923 case DT_MIPS_FLAGS:
11924 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11925 break;
11927 case DT_MIPS_TIME_STAMP:
11929 time_t t;
11930 time (&t);
11931 dyn.d_un.d_val = t;
11933 break;
11935 case DT_MIPS_ICHECKSUM:
11936 /* XXX FIXME: */
11937 swap_out_p = false;
11938 break;
11940 case DT_MIPS_IVERSION:
11941 /* XXX FIXME: */
11942 swap_out_p = false;
11943 break;
11945 case DT_MIPS_BASE_ADDRESS:
11946 s = output_bfd->sections;
11947 BFD_ASSERT (s != NULL);
11948 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11949 break;
11951 case DT_MIPS_LOCAL_GOTNO:
11952 dyn.d_un.d_val = g->local_gotno;
11953 break;
11955 case DT_MIPS_UNREFEXTNO:
11956 /* The index into the dynamic symbol table which is the
11957 entry of the first external symbol that is not
11958 referenced within the same object. */
11959 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11960 break;
11962 case DT_MIPS_GOTSYM:
11963 if (htab->global_gotsym)
11965 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11966 break;
11968 /* In case if we don't have global got symbols we default
11969 to setting DT_MIPS_GOTSYM to the same value as
11970 DT_MIPS_SYMTABNO. */
11971 /* Fall through. */
11973 case DT_MIPS_SYMTABNO:
11974 name = ".dynsym";
11975 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11976 s = bfd_get_linker_section (dynobj, name);
11978 if (s != NULL)
11979 dyn.d_un.d_val = s->size / elemsize;
11980 else
11981 dyn.d_un.d_val = 0;
11982 break;
11984 case DT_MIPS_HIPAGENO:
11985 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11986 break;
11988 case DT_MIPS_RLD_MAP:
11990 struct elf_link_hash_entry *h;
11991 h = mips_elf_hash_table (info)->rld_symbol;
11992 if (!h)
11994 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11995 swap_out_p = false;
11996 break;
11998 s = h->root.u.def.section;
12000 /* The MIPS_RLD_MAP tag stores the absolute address of the
12001 debug pointer. */
12002 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12003 + h->root.u.def.value);
12005 break;
12007 case DT_MIPS_RLD_MAP_REL:
12009 struct elf_link_hash_entry *h;
12010 bfd_vma dt_addr, rld_addr;
12011 h = mips_elf_hash_table (info)->rld_symbol;
12012 if (!h)
12014 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12015 swap_out_p = false;
12016 break;
12018 s = h->root.u.def.section;
12020 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12021 pointer, relative to the address of the tag. */
12022 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12023 + (b - sdyn->contents));
12024 rld_addr = (s->output_section->vma + s->output_offset
12025 + h->root.u.def.value);
12026 dyn.d_un.d_ptr = rld_addr - dt_addr;
12028 break;
12030 case DT_MIPS_OPTIONS:
12031 s = (bfd_get_section_by_name
12032 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12033 dyn.d_un.d_ptr = s->vma;
12034 break;
12036 case DT_PLTREL:
12037 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12038 if (htab->root.target_os == is_vxworks)
12039 dyn.d_un.d_val = DT_RELA;
12040 else
12041 dyn.d_un.d_val = DT_REL;
12042 break;
12044 case DT_PLTRELSZ:
12045 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12046 dyn.d_un.d_val = htab->root.srelplt->size;
12047 break;
12049 case DT_JMPREL:
12050 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12051 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12052 + htab->root.srelplt->output_offset);
12053 break;
12055 case DT_TEXTREL:
12056 /* If we didn't need any text relocations after all, delete
12057 the dynamic tag. */
12058 if (!(info->flags & DF_TEXTREL))
12060 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12061 swap_out_p = false;
12063 break;
12065 case DT_FLAGS:
12066 /* If we didn't need any text relocations after all, clear
12067 DF_TEXTREL from DT_FLAGS. */
12068 if (!(info->flags & DF_TEXTREL))
12069 dyn.d_un.d_val &= ~DF_TEXTREL;
12070 else
12071 swap_out_p = false;
12072 break;
12074 case DT_MIPS_XHASH:
12075 name = ".MIPS.xhash";
12076 s = bfd_get_linker_section (dynobj, name);
12077 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12078 break;
12080 default:
12081 swap_out_p = false;
12082 if (htab->root.target_os == is_vxworks
12083 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12084 swap_out_p = true;
12085 break;
12088 if (swap_out_p || dyn_skipped)
12089 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12090 (dynobj, &dyn, b - dyn_skipped);
12092 if (dyn_to_skip)
12094 dyn_skipped += dyn_to_skip;
12095 dyn_to_skip = 0;
12099 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12100 if (dyn_skipped > 0)
12101 memset (b - dyn_skipped, 0, dyn_skipped);
12104 if (sgot != NULL && sgot->size > 0
12105 && !bfd_is_abs_section (sgot->output_section))
12107 if (htab->root.target_os == is_vxworks)
12109 /* The first entry of the global offset table points to the
12110 ".dynamic" section. The second is initialized by the
12111 loader and contains the shared library identifier.
12112 The third is also initialized by the loader and points
12113 to the lazy resolution stub. */
12114 MIPS_ELF_PUT_WORD (output_bfd,
12115 sdyn->output_offset + sdyn->output_section->vma,
12116 sgot->contents);
12117 MIPS_ELF_PUT_WORD (output_bfd, 0,
12118 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12119 MIPS_ELF_PUT_WORD (output_bfd, 0,
12120 sgot->contents
12121 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12123 else
12125 /* The first entry of the global offset table will be filled at
12126 runtime. The second entry will be used by some runtime loaders.
12127 This isn't the case of IRIX rld. */
12128 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12129 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12130 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12133 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12134 = MIPS_ELF_GOT_SIZE (output_bfd);
12137 /* Generate dynamic relocations for the non-primary gots. */
12138 if (gg != NULL && gg->next)
12140 Elf_Internal_Rela rel[3];
12141 bfd_vma addend = 0;
12143 memset (rel, 0, sizeof (rel));
12144 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12146 for (g = gg->next; g->next != gg; g = g->next)
12148 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12149 + g->next->tls_gotno;
12151 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12152 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12153 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12154 sgot->contents
12155 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12157 if (! bfd_link_pic (info))
12158 continue;
12160 for (; got_index < g->local_gotno; got_index++)
12162 if (got_index >= g->assigned_low_gotno
12163 && got_index <= g->assigned_high_gotno)
12164 continue;
12166 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12167 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12168 if (!(mips_elf_create_dynamic_relocation
12169 (output_bfd, info, rel, NULL,
12170 bfd_abs_section_ptr,
12171 0, &addend, sgot)))
12172 return false;
12173 BFD_ASSERT (addend == 0);
12178 /* The generation of dynamic relocations for the non-primary gots
12179 adds more dynamic relocations. We cannot count them until
12180 here. */
12182 if (elf_hash_table (info)->dynamic_sections_created)
12184 bfd_byte *b;
12185 bool swap_out_p;
12187 BFD_ASSERT (sdyn != NULL);
12189 for (b = sdyn->contents;
12190 b < sdyn->contents + sdyn->size;
12191 b += MIPS_ELF_DYN_SIZE (dynobj))
12193 Elf_Internal_Dyn dyn;
12194 asection *s;
12196 /* Read in the current dynamic entry. */
12197 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12199 /* Assume that we're going to modify it and write it out. */
12200 swap_out_p = true;
12202 switch (dyn.d_tag)
12204 case DT_RELSZ:
12205 /* Reduce DT_RELSZ to account for any relocations we
12206 decided not to make. This is for the n64 irix rld,
12207 which doesn't seem to apply any relocations if there
12208 are trailing null entries. */
12209 s = mips_elf_rel_dyn_section (info, false);
12210 dyn.d_un.d_val = (s->reloc_count
12211 * (ABI_64_P (output_bfd)
12212 ? sizeof (Elf64_Mips_External_Rel)
12213 : sizeof (Elf32_External_Rel)));
12214 /* Adjust the section size too. Tools like the prelinker
12215 can reasonably expect the values to the same. */
12216 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12217 elf_section_data (s->output_section)->this_hdr.sh_size
12218 = dyn.d_un.d_val;
12219 break;
12221 default:
12222 swap_out_p = false;
12223 break;
12226 if (swap_out_p)
12227 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12228 (dynobj, &dyn, b);
12233 asection *s;
12234 Elf32_compact_rel cpt;
12236 if (SGI_COMPAT (output_bfd))
12238 /* Write .compact_rel section out. */
12239 s = bfd_get_linker_section (dynobj, ".compact_rel");
12240 if (s != NULL)
12242 cpt.id1 = 1;
12243 cpt.num = s->reloc_count;
12244 cpt.id2 = 2;
12245 cpt.offset = (s->output_section->filepos
12246 + sizeof (Elf32_External_compact_rel));
12247 cpt.reserved0 = 0;
12248 cpt.reserved1 = 0;
12249 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12250 ((Elf32_External_compact_rel *)
12251 s->contents));
12253 /* Clean up a dummy stub function entry in .text. */
12254 if (htab->sstubs != NULL
12255 && htab->sstubs->contents != NULL)
12257 file_ptr dummy_offset;
12259 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12260 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12261 memset (htab->sstubs->contents + dummy_offset, 0,
12262 htab->function_stub_size);
12267 /* The psABI says that the dynamic relocations must be sorted in
12268 increasing order of r_symndx. The VxWorks EABI doesn't require
12269 this, and because the code below handles REL rather than RELA
12270 relocations, using it for VxWorks would be outright harmful. */
12271 if (htab->root.target_os != is_vxworks)
12273 s = mips_elf_rel_dyn_section (info, false);
12274 if (s != NULL
12275 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12277 reldyn_sorting_bfd = output_bfd;
12279 if (ABI_64_P (output_bfd))
12280 qsort ((Elf64_External_Rel *) s->contents + 1,
12281 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12282 sort_dynamic_relocs_64);
12283 else
12284 qsort ((Elf32_External_Rel *) s->contents + 1,
12285 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12286 sort_dynamic_relocs);
12291 if (htab->root.splt && htab->root.splt->size > 0)
12293 if (htab->root.target_os == is_vxworks)
12295 if (bfd_link_pic (info))
12296 mips_vxworks_finish_shared_plt (output_bfd, info);
12297 else
12298 mips_vxworks_finish_exec_plt (output_bfd, info);
12300 else
12302 BFD_ASSERT (!bfd_link_pic (info));
12303 if (!mips_finish_exec_plt (output_bfd, info))
12304 return false;
12307 return true;
12311 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12313 static void
12314 mips_set_isa_flags (bfd *abfd)
12316 flagword val;
12318 switch (bfd_get_mach (abfd))
12320 default:
12321 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12322 val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_64R6 : E_MIPS_ARCH_3;
12323 else
12324 val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_32R6 : E_MIPS_ARCH_1;
12325 break;
12327 case bfd_mach_mips3000:
12328 val = E_MIPS_ARCH_1;
12329 break;
12331 case bfd_mach_mips3900:
12332 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12333 break;
12335 case bfd_mach_mips6000:
12336 val = E_MIPS_ARCH_2;
12337 break;
12339 case bfd_mach_mips4010:
12340 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12341 break;
12343 case bfd_mach_mips_allegrex:
12344 val = E_MIPS_ARCH_2 | E_MIPS_MACH_ALLEGREX;
12345 break;
12347 case bfd_mach_mips4000:
12348 case bfd_mach_mips4300:
12349 case bfd_mach_mips4400:
12350 case bfd_mach_mips4600:
12351 val = E_MIPS_ARCH_3;
12352 break;
12354 case bfd_mach_mips4100:
12355 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12356 break;
12358 case bfd_mach_mips4111:
12359 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12360 break;
12362 case bfd_mach_mips4120:
12363 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12364 break;
12366 case bfd_mach_mips4650:
12367 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12368 break;
12370 case bfd_mach_mips5400:
12371 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12372 break;
12374 case bfd_mach_mips5500:
12375 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12376 break;
12378 case bfd_mach_mips5900:
12379 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12380 break;
12382 case bfd_mach_mips9000:
12383 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12384 break;
12386 case bfd_mach_mips5000:
12387 case bfd_mach_mips7000:
12388 case bfd_mach_mips8000:
12389 case bfd_mach_mips10000:
12390 case bfd_mach_mips12000:
12391 case bfd_mach_mips14000:
12392 case bfd_mach_mips16000:
12393 val = E_MIPS_ARCH_4;
12394 break;
12396 case bfd_mach_mips5:
12397 val = E_MIPS_ARCH_5;
12398 break;
12400 case bfd_mach_mips_loongson_2e:
12401 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12402 break;
12404 case bfd_mach_mips_loongson_2f:
12405 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12406 break;
12408 case bfd_mach_mips_sb1:
12409 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12410 break;
12412 case bfd_mach_mips_gs464:
12413 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12414 break;
12416 case bfd_mach_mips_gs464e:
12417 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12418 break;
12420 case bfd_mach_mips_gs264e:
12421 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12422 break;
12424 case bfd_mach_mips_octeon:
12425 case bfd_mach_mips_octeonp:
12426 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12427 break;
12429 case bfd_mach_mips_octeon3:
12430 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12431 break;
12433 case bfd_mach_mips_xlr:
12434 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12435 break;
12437 case bfd_mach_mips_octeon2:
12438 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12439 break;
12441 case bfd_mach_mipsisa32:
12442 val = E_MIPS_ARCH_32;
12443 break;
12445 case bfd_mach_mipsisa64:
12446 val = E_MIPS_ARCH_64;
12447 break;
12449 case bfd_mach_mipsisa32r2:
12450 case bfd_mach_mipsisa32r3:
12451 case bfd_mach_mipsisa32r5:
12452 val = E_MIPS_ARCH_32R2;
12453 break;
12455 case bfd_mach_mips_interaptiv_mr2:
12456 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12457 break;
12459 case bfd_mach_mipsisa64r2:
12460 case bfd_mach_mipsisa64r3:
12461 case bfd_mach_mipsisa64r5:
12462 val = E_MIPS_ARCH_64R2;
12463 break;
12465 case bfd_mach_mipsisa32r6:
12466 val = E_MIPS_ARCH_32R6;
12467 break;
12469 case bfd_mach_mipsisa64r6:
12470 val = E_MIPS_ARCH_64R6;
12471 break;
12473 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12474 elf_elfheader (abfd)->e_flags |= val;
12479 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12480 Don't do so for code sections. We want to keep ordering of HI16/LO16
12481 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12482 relocs to be sorted. */
12484 bool
12485 _bfd_mips_elf_sort_relocs_p (asection *sec)
12487 return (sec->flags & SEC_CODE) == 0;
12491 /* The final processing done just before writing out a MIPS ELF object
12492 file. This gets the MIPS architecture right based on the machine
12493 number. This is used by both the 32-bit and the 64-bit ABI. */
12495 void
12496 _bfd_mips_final_write_processing (bfd *abfd)
12498 unsigned int i;
12499 Elf_Internal_Shdr **hdrpp;
12500 const char *name;
12501 asection *sec;
12503 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12504 is nonzero. This is for compatibility with old objects, which used
12505 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12506 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12507 mips_set_isa_flags (abfd);
12509 /* Set the sh_info field for .gptab sections and other appropriate
12510 info for each special section. */
12511 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12512 i < elf_numsections (abfd);
12513 i++, hdrpp++)
12515 switch ((*hdrpp)->sh_type)
12517 case SHT_MIPS_MSYM:
12518 case SHT_MIPS_LIBLIST:
12519 sec = bfd_get_section_by_name (abfd, ".dynstr");
12520 if (sec != NULL)
12521 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12522 break;
12524 case SHT_MIPS_GPTAB:
12525 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12526 name = bfd_section_name ((*hdrpp)->bfd_section);
12527 BFD_ASSERT (name != NULL
12528 && startswith (name, ".gptab."));
12529 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12530 BFD_ASSERT (sec != NULL);
12531 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12532 break;
12534 case SHT_MIPS_CONTENT:
12535 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12536 name = bfd_section_name ((*hdrpp)->bfd_section);
12537 BFD_ASSERT (name != NULL
12538 && startswith (name, ".MIPS.content"));
12539 sec = bfd_get_section_by_name (abfd,
12540 name + sizeof ".MIPS.content" - 1);
12541 BFD_ASSERT (sec != NULL);
12542 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12543 break;
12545 case SHT_MIPS_SYMBOL_LIB:
12546 sec = bfd_get_section_by_name (abfd, ".dynsym");
12547 if (sec != NULL)
12548 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12549 sec = bfd_get_section_by_name (abfd, ".liblist");
12550 if (sec != NULL)
12551 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12552 break;
12554 case SHT_MIPS_EVENTS:
12555 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12556 name = bfd_section_name ((*hdrpp)->bfd_section);
12557 BFD_ASSERT (name != NULL);
12558 if (startswith (name, ".MIPS.events"))
12559 sec = bfd_get_section_by_name (abfd,
12560 name + sizeof ".MIPS.events" - 1);
12561 else
12563 BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
12564 sec = bfd_get_section_by_name (abfd,
12565 (name
12566 + sizeof ".MIPS.post_rel" - 1));
12568 BFD_ASSERT (sec != NULL);
12569 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12570 break;
12572 case SHT_MIPS_XHASH:
12573 sec = bfd_get_section_by_name (abfd, ".dynsym");
12574 if (sec != NULL)
12575 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12580 bool
12581 _bfd_mips_elf_final_write_processing (bfd *abfd)
12583 _bfd_mips_final_write_processing (abfd);
12584 return _bfd_elf_final_write_processing (abfd);
12587 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12588 segments. */
12591 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12592 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12594 asection *s;
12595 int ret = 0;
12597 /* See if we need a PT_MIPS_REGINFO segment. */
12598 s = bfd_get_section_by_name (abfd, ".reginfo");
12599 if (s && (s->flags & SEC_LOAD))
12600 ++ret;
12602 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12603 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12604 ++ret;
12606 /* See if we need a PT_MIPS_OPTIONS segment. */
12607 if (IRIX_COMPAT (abfd) == ict_irix6
12608 && bfd_get_section_by_name (abfd,
12609 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12610 ++ret;
12612 /* See if we need a PT_MIPS_RTPROC segment. */
12613 if (IRIX_COMPAT (abfd) == ict_irix5
12614 && bfd_get_section_by_name (abfd, ".dynamic")
12615 && bfd_get_section_by_name (abfd, ".mdebug"))
12616 ++ret;
12618 /* Allocate a PT_NULL header in dynamic objects. See
12619 _bfd_mips_elf_modify_segment_map for details. */
12620 if (!SGI_COMPAT (abfd)
12621 && bfd_get_section_by_name (abfd, ".dynamic"))
12622 ++ret;
12624 return ret;
12627 /* Modify the segment map for an IRIX5 executable. */
12629 bool
12630 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12631 struct bfd_link_info *info)
12633 asection *s;
12634 struct elf_segment_map *m, **pm;
12635 size_t amt;
12637 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12638 segment. */
12639 s = bfd_get_section_by_name (abfd, ".reginfo");
12640 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12642 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12643 if (m->p_type == PT_MIPS_REGINFO)
12644 break;
12645 if (m == NULL)
12647 amt = sizeof *m;
12648 m = bfd_zalloc (abfd, amt);
12649 if (m == NULL)
12650 return false;
12652 m->p_type = PT_MIPS_REGINFO;
12653 m->count = 1;
12654 m->sections[0] = s;
12656 /* We want to put it after the PHDR and INTERP segments. */
12657 pm = &elf_seg_map (abfd);
12658 while (*pm != NULL
12659 && ((*pm)->p_type == PT_PHDR
12660 || (*pm)->p_type == PT_INTERP))
12661 pm = &(*pm)->next;
12663 m->next = *pm;
12664 *pm = m;
12668 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12669 segment. */
12670 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12671 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12673 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12674 if (m->p_type == PT_MIPS_ABIFLAGS)
12675 break;
12676 if (m == NULL)
12678 amt = sizeof *m;
12679 m = bfd_zalloc (abfd, amt);
12680 if (m == NULL)
12681 return false;
12683 m->p_type = PT_MIPS_ABIFLAGS;
12684 m->count = 1;
12685 m->sections[0] = s;
12687 /* We want to put it after the PHDR and INTERP segments. */
12688 pm = &elf_seg_map (abfd);
12689 while (*pm != NULL
12690 && ((*pm)->p_type == PT_PHDR
12691 || (*pm)->p_type == PT_INTERP))
12692 pm = &(*pm)->next;
12694 m->next = *pm;
12695 *pm = m;
12699 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12700 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12701 PT_MIPS_OPTIONS segment immediately following the program header
12702 table. */
12703 if (NEWABI_P (abfd)
12704 /* On non-IRIX6 new abi, we'll have already created a segment
12705 for this section, so don't create another. I'm not sure this
12706 is not also the case for IRIX 6, but I can't test it right
12707 now. */
12708 && IRIX_COMPAT (abfd) == ict_irix6)
12710 for (s = abfd->sections; s; s = s->next)
12711 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12712 break;
12714 if (s)
12716 struct elf_segment_map *options_segment;
12718 pm = &elf_seg_map (abfd);
12719 while (*pm != NULL
12720 && ((*pm)->p_type == PT_PHDR
12721 || (*pm)->p_type == PT_INTERP))
12722 pm = &(*pm)->next;
12724 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12726 amt = sizeof (struct elf_segment_map);
12727 options_segment = bfd_zalloc (abfd, amt);
12728 options_segment->next = *pm;
12729 options_segment->p_type = PT_MIPS_OPTIONS;
12730 options_segment->p_flags = PF_R;
12731 options_segment->p_flags_valid = true;
12732 options_segment->count = 1;
12733 options_segment->sections[0] = s;
12734 *pm = options_segment;
12738 else
12740 if (IRIX_COMPAT (abfd) == ict_irix5)
12742 /* If there are .dynamic and .mdebug sections, we make a room
12743 for the RTPROC header. FIXME: Rewrite without section names. */
12744 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12745 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12746 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12748 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12749 if (m->p_type == PT_MIPS_RTPROC)
12750 break;
12751 if (m == NULL)
12753 amt = sizeof *m;
12754 m = bfd_zalloc (abfd, amt);
12755 if (m == NULL)
12756 return false;
12758 m->p_type = PT_MIPS_RTPROC;
12760 s = bfd_get_section_by_name (abfd, ".rtproc");
12761 if (s == NULL)
12763 m->count = 0;
12764 m->p_flags = 0;
12765 m->p_flags_valid = 1;
12767 else
12769 m->count = 1;
12770 m->sections[0] = s;
12773 /* We want to put it after the DYNAMIC segment. */
12774 pm = &elf_seg_map (abfd);
12775 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12776 pm = &(*pm)->next;
12777 if (*pm != NULL)
12778 pm = &(*pm)->next;
12780 m->next = *pm;
12781 *pm = m;
12785 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12786 .dynstr, .dynsym, and .hash sections, and everything in
12787 between. */
12788 for (pm = &elf_seg_map (abfd); *pm != NULL;
12789 pm = &(*pm)->next)
12790 if ((*pm)->p_type == PT_DYNAMIC)
12791 break;
12792 m = *pm;
12793 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12794 glibc's dynamic linker has traditionally derived the number of
12795 tags from the p_filesz field, and sometimes allocates stack
12796 arrays of that size. An overly-big PT_DYNAMIC segment can
12797 be actively harmful in such cases. Making PT_DYNAMIC contain
12798 other sections can also make life hard for the prelinker,
12799 which might move one of the other sections to a different
12800 PT_LOAD segment. */
12801 if (SGI_COMPAT (abfd)
12802 && m != NULL
12803 && m->count == 1
12804 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12806 static const char *sec_names[] =
12808 ".dynamic", ".dynstr", ".dynsym", ".hash"
12810 bfd_vma low, high;
12811 unsigned int i, c;
12812 struct elf_segment_map *n;
12814 low = ~(bfd_vma) 0;
12815 high = 0;
12816 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12818 s = bfd_get_section_by_name (abfd, sec_names[i]);
12819 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12821 bfd_size_type sz;
12823 if (low > s->vma)
12824 low = s->vma;
12825 sz = s->size;
12826 if (high < s->vma + sz)
12827 high = s->vma + sz;
12831 c = 0;
12832 for (s = abfd->sections; s != NULL; s = s->next)
12833 if ((s->flags & SEC_LOAD) != 0
12834 && s->vma >= low
12835 && s->vma + s->size <= high)
12836 ++c;
12838 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12839 n = bfd_zalloc (abfd, amt);
12840 if (n == NULL)
12841 return false;
12842 *n = *m;
12843 n->count = c;
12845 i = 0;
12846 for (s = abfd->sections; s != NULL; s = s->next)
12848 if ((s->flags & SEC_LOAD) != 0
12849 && s->vma >= low
12850 && s->vma + s->size <= high)
12852 n->sections[i] = s;
12853 ++i;
12857 *pm = n;
12861 /* Allocate a spare program header in dynamic objects so that tools
12862 like the prelinker can add an extra PT_LOAD entry.
12864 If the prelinker needs to make room for a new PT_LOAD entry, its
12865 standard procedure is to move the first (read-only) sections into
12866 the new (writable) segment. However, the MIPS ABI requires
12867 .dynamic to be in a read-only segment, and the section will often
12868 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12870 Although the prelinker could in principle move .dynamic to a
12871 writable segment, it seems better to allocate a spare program
12872 header instead, and avoid the need to move any sections.
12873 There is a long tradition of allocating spare dynamic tags,
12874 so allocating a spare program header seems like a natural
12875 extension.
12877 If INFO is NULL, we may be copying an already prelinked binary
12878 with objcopy or strip, so do not add this header. */
12879 if (info != NULL
12880 && !SGI_COMPAT (abfd)
12881 && bfd_get_section_by_name (abfd, ".dynamic"))
12883 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12884 if ((*pm)->p_type == PT_NULL)
12885 break;
12886 if (*pm == NULL)
12888 m = bfd_zalloc (abfd, sizeof (*m));
12889 if (m == NULL)
12890 return false;
12892 m->p_type = PT_NULL;
12893 *pm = m;
12897 return true;
12900 /* Return the section that should be marked against GC for a given
12901 relocation. */
12903 asection *
12904 _bfd_mips_elf_gc_mark_hook (asection *sec,
12905 struct bfd_link_info *info,
12906 Elf_Internal_Rela *rel,
12907 struct elf_link_hash_entry *h,
12908 Elf_Internal_Sym *sym)
12910 /* ??? Do mips16 stub sections need to be handled special? */
12912 if (h != NULL)
12913 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12915 case R_MIPS_GNU_VTINHERIT:
12916 case R_MIPS_GNU_VTENTRY:
12917 return NULL;
12920 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12923 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12925 bool
12926 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12927 elf_gc_mark_hook_fn gc_mark_hook)
12929 bfd *sub;
12931 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12933 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12935 asection *o;
12937 if (! is_mips_elf (sub))
12938 continue;
12940 for (o = sub->sections; o != NULL; o = o->next)
12941 if (!o->gc_mark
12942 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12944 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12945 return false;
12949 return true;
12952 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12953 hiding the old indirect symbol. Process additional relocation
12954 information. Also called for weakdefs, in which case we just let
12955 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12957 void
12958 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12959 struct elf_link_hash_entry *dir,
12960 struct elf_link_hash_entry *ind)
12962 struct mips_elf_link_hash_entry *dirmips, *indmips;
12964 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12966 dirmips = (struct mips_elf_link_hash_entry *) dir;
12967 indmips = (struct mips_elf_link_hash_entry *) ind;
12968 /* Any absolute non-dynamic relocations against an indirect or weak
12969 definition will be against the target symbol. */
12970 if (indmips->has_static_relocs)
12971 dirmips->has_static_relocs = true;
12973 if (ind->root.type != bfd_link_hash_indirect)
12974 return;
12976 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12977 if (indmips->readonly_reloc)
12978 dirmips->readonly_reloc = true;
12979 if (indmips->no_fn_stub)
12980 dirmips->no_fn_stub = true;
12981 if (indmips->fn_stub)
12983 dirmips->fn_stub = indmips->fn_stub;
12984 indmips->fn_stub = NULL;
12986 if (indmips->need_fn_stub)
12988 dirmips->need_fn_stub = true;
12989 indmips->need_fn_stub = false;
12991 if (indmips->call_stub)
12993 dirmips->call_stub = indmips->call_stub;
12994 indmips->call_stub = NULL;
12996 if (indmips->call_fp_stub)
12998 dirmips->call_fp_stub = indmips->call_fp_stub;
12999 indmips->call_fp_stub = NULL;
13001 if (indmips->global_got_area < dirmips->global_got_area)
13002 dirmips->global_got_area = indmips->global_got_area;
13003 if (indmips->global_got_area < GGA_NONE)
13004 indmips->global_got_area = GGA_NONE;
13005 if (indmips->has_nonpic_branches)
13006 dirmips->has_nonpic_branches = true;
13009 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13010 to hide it. It has to remain global (it will also be protected) so as to
13011 be assigned a global GOT entry, which will then remain unchanged at load
13012 time. */
13014 void
13015 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13016 struct elf_link_hash_entry *entry,
13017 bool force_local)
13019 struct mips_elf_link_hash_table *htab;
13021 htab = mips_elf_hash_table (info);
13022 BFD_ASSERT (htab != NULL);
13023 if (htab->use_absolute_zero
13024 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13025 return;
13027 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13030 #define PDR_SIZE 32
13032 bool
13033 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13034 struct bfd_link_info *info)
13036 asection *o;
13037 bool ret = false;
13038 unsigned char *tdata;
13039 size_t i, skip;
13041 o = bfd_get_section_by_name (abfd, ".pdr");
13042 if (! o)
13043 return false;
13044 if (o->size == 0)
13045 return false;
13046 if (o->size % PDR_SIZE != 0)
13047 return false;
13048 if (o->output_section != NULL
13049 && bfd_is_abs_section (o->output_section))
13050 return false;
13052 tdata = bfd_zmalloc (o->size / PDR_SIZE);
13053 if (! tdata)
13054 return false;
13056 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13057 info->keep_memory);
13058 if (!cookie->rels)
13060 free (tdata);
13061 return false;
13064 cookie->rel = cookie->rels;
13065 cookie->relend = cookie->rels + o->reloc_count;
13067 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13069 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13071 tdata[i] = 1;
13072 skip ++;
13076 if (skip != 0)
13078 mips_elf_section_data (o)->u.tdata = tdata;
13079 if (o->rawsize == 0)
13080 o->rawsize = o->size;
13081 o->size -= skip * PDR_SIZE;
13082 ret = true;
13084 else
13085 free (tdata);
13087 if (! info->keep_memory)
13088 free (cookie->rels);
13090 return ret;
13093 bool
13094 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13096 if (strcmp (sec->name, ".pdr") == 0)
13097 return true;
13098 return false;
13101 bool
13102 _bfd_mips_elf_write_section (bfd *output_bfd,
13103 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13104 asection *sec, bfd_byte *contents)
13106 bfd_byte *to, *from, *end;
13107 int i;
13109 if (strcmp (sec->name, ".pdr") != 0)
13110 return false;
13112 if (mips_elf_section_data (sec)->u.tdata == NULL)
13113 return false;
13115 to = contents;
13116 end = contents + sec->size;
13117 for (from = contents, i = 0;
13118 from < end;
13119 from += PDR_SIZE, i++)
13121 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13122 continue;
13123 if (to != from)
13124 memcpy (to, from, PDR_SIZE);
13125 to += PDR_SIZE;
13127 bfd_set_section_contents (output_bfd, sec->output_section, contents,
13128 sec->output_offset, sec->size);
13129 return true;
13132 /* microMIPS code retains local labels for linker relaxation. Omit them
13133 from output by default for clarity. */
13135 bool
13136 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13138 return _bfd_elf_is_local_label_name (abfd, sym->name);
13141 bool
13142 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13143 asection *section, bfd_vma offset,
13144 const char **filename_ptr,
13145 const char **functionname_ptr,
13146 unsigned int *line_ptr,
13147 unsigned int *discriminator_ptr)
13149 asection *msec;
13151 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13152 filename_ptr, functionname_ptr,
13153 line_ptr, discriminator_ptr,
13154 dwarf_debug_sections,
13155 &elf_tdata (abfd)->dwarf2_find_line_info)
13156 == 1)
13157 return true;
13159 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13160 filename_ptr, functionname_ptr,
13161 line_ptr))
13163 if (!*functionname_ptr)
13164 _bfd_elf_find_function (abfd, symbols, section, offset,
13165 *filename_ptr ? NULL : filename_ptr,
13166 functionname_ptr);
13167 return true;
13170 msec = bfd_get_section_by_name (abfd, ".mdebug");
13171 if (msec != NULL)
13173 flagword origflags;
13174 struct mips_elf_find_line *fi;
13175 const struct ecoff_debug_swap * const swap =
13176 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13178 /* If we are called during a link, mips_elf_final_link may have
13179 cleared the SEC_HAS_CONTENTS field. We force it back on here
13180 if appropriate (which it normally will be). */
13181 origflags = msec->flags;
13182 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13183 msec->flags |= SEC_HAS_CONTENTS;
13185 fi = mips_elf_tdata (abfd)->find_line_info;
13186 if (fi == NULL)
13188 bfd_size_type external_fdr_size;
13189 char *fraw_src;
13190 char *fraw_end;
13191 struct fdr *fdr_ptr;
13192 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13194 fi = bfd_zalloc (abfd, amt);
13195 if (fi == NULL)
13197 msec->flags = origflags;
13198 return false;
13201 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13203 msec->flags = origflags;
13204 return false;
13207 /* Swap in the FDR information. */
13208 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13209 fi->d.fdr = bfd_alloc (abfd, amt);
13210 if (fi->d.fdr == NULL)
13212 _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13213 msec->flags = origflags;
13214 return false;
13216 external_fdr_size = swap->external_fdr_size;
13217 fdr_ptr = fi->d.fdr;
13218 fraw_src = (char *) fi->d.external_fdr;
13219 fraw_end = (fraw_src
13220 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13221 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13222 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13224 mips_elf_tdata (abfd)->find_line_info = fi;
13227 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13228 &fi->i, filename_ptr, functionname_ptr,
13229 line_ptr))
13231 msec->flags = origflags;
13232 return true;
13235 msec->flags = origflags;
13238 /* Fall back on the generic ELF find_nearest_line routine. */
13240 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13241 filename_ptr, functionname_ptr,
13242 line_ptr, discriminator_ptr);
13245 bool
13246 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13247 const char **filename_ptr,
13248 const char **functionname_ptr,
13249 unsigned int *line_ptr)
13251 bool found;
13252 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13253 functionname_ptr, line_ptr,
13254 & elf_tdata (abfd)->dwarf2_find_line_info);
13255 return found;
13259 /* When are writing out the .options or .MIPS.options section,
13260 remember the bytes we are writing out, so that we can install the
13261 GP value in the section_processing routine. */
13263 bool
13264 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13265 const void *location,
13266 file_ptr offset, bfd_size_type count)
13268 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13270 bfd_byte *c;
13272 if (elf_section_data (section) == NULL)
13274 size_t amt = sizeof (struct bfd_elf_section_data);
13275 section->used_by_bfd = bfd_zalloc (abfd, amt);
13276 if (elf_section_data (section) == NULL)
13277 return false;
13279 c = mips_elf_section_data (section)->u.tdata;
13280 if (c == NULL)
13282 c = bfd_zalloc (abfd, section->size);
13283 if (c == NULL)
13284 return false;
13285 mips_elf_section_data (section)->u.tdata = c;
13288 memcpy (c + offset, location, count);
13291 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13292 count);
13295 /* This is almost identical to bfd_generic_get_... except that some
13296 MIPS relocations need to be handled specially. Sigh. */
13298 bfd_byte *
13299 _bfd_elf_mips_get_relocated_section_contents
13300 (bfd *abfd,
13301 struct bfd_link_info *link_info,
13302 struct bfd_link_order *link_order,
13303 bfd_byte *data,
13304 bool relocatable,
13305 asymbol **symbols)
13307 bfd *input_bfd = link_order->u.indirect.section->owner;
13308 asection *input_section = link_order->u.indirect.section;
13309 long reloc_size;
13310 arelent **reloc_vector;
13311 long reloc_count;
13313 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13314 if (reloc_size < 0)
13315 return NULL;
13317 /* Read in the section. */
13318 bfd_byte *orig_data = data;
13319 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13320 return NULL;
13322 if (data == NULL)
13323 return NULL;
13325 if (reloc_size == 0)
13326 return data;
13328 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13329 if (reloc_vector == NULL)
13331 struct mips_elf_obj_tdata *tdata;
13332 struct mips_hi16 **hip, *hi;
13333 error_return:
13334 /* If we are going to return an error, remove entries on
13335 mips_hi16_list that point into this section's data. Data
13336 will typically be freed on return from this function. */
13337 tdata = mips_elf_tdata (abfd);
13338 hip = &tdata->mips_hi16_list;
13339 while ((hi = *hip) != NULL)
13341 if (hi->input_section == input_section)
13343 *hip = hi->next;
13344 free (hi);
13346 else
13347 hip = &hi->next;
13349 if (orig_data == NULL)
13350 free (data);
13351 data = NULL;
13352 goto out;
13355 reloc_count = bfd_canonicalize_reloc (input_bfd,
13356 input_section,
13357 reloc_vector,
13358 symbols);
13359 if (reloc_count < 0)
13360 goto error_return;
13362 if (reloc_count > 0)
13364 arelent **parent;
13365 /* for mips */
13366 int gp_found;
13367 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13370 struct bfd_hash_entry *h;
13371 struct bfd_link_hash_entry *lh;
13372 /* Skip all this stuff if we aren't mixing formats. */
13373 if (abfd && input_bfd
13374 && abfd->xvec == input_bfd->xvec)
13375 lh = 0;
13376 else
13378 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13379 lh = (struct bfd_link_hash_entry *) h;
13381 lookup:
13382 if (lh)
13384 switch (lh->type)
13386 case bfd_link_hash_undefined:
13387 case bfd_link_hash_undefweak:
13388 case bfd_link_hash_common:
13389 gp_found = 0;
13390 break;
13391 case bfd_link_hash_defined:
13392 case bfd_link_hash_defweak:
13393 gp_found = 1;
13394 gp = lh->u.def.value;
13395 break;
13396 case bfd_link_hash_indirect:
13397 case bfd_link_hash_warning:
13398 lh = lh->u.i.link;
13399 /* @@FIXME ignoring warning for now */
13400 goto lookup;
13401 case bfd_link_hash_new:
13402 default:
13403 abort ();
13406 else
13407 gp_found = 0;
13409 /* end mips */
13411 for (parent = reloc_vector; *parent != NULL; parent++)
13413 char *error_message = NULL;
13414 asymbol *symbol;
13415 bfd_reloc_status_type r;
13417 symbol = *(*parent)->sym_ptr_ptr;
13418 /* PR ld/19628: A specially crafted input file
13419 can result in a NULL symbol pointer here. */
13420 if (symbol == NULL)
13422 link_info->callbacks->einfo
13423 /* xgettext:c-format */
13424 (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13425 abfd, input_section, (* parent)->address);
13426 goto error_return;
13429 /* Zap reloc field when the symbol is from a discarded
13430 section, ignoring any addend. Do the same when called
13431 from bfd_simple_get_relocated_section_contents for
13432 undefined symbols in debug sections. This is to keep
13433 debug info reasonably sane, in particular so that
13434 DW_FORM_ref_addr to another file's .debug_info isn't
13435 confused with an offset into the current file's
13436 .debug_info. */
13437 if ((symbol->section != NULL && discarded_section (symbol->section))
13438 || (symbol->section == bfd_und_section_ptr
13439 && (input_section->flags & SEC_DEBUGGING) != 0
13440 && link_info->input_bfds == link_info->output_bfd))
13442 bfd_vma off;
13443 static reloc_howto_type none_howto
13444 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13445 "unused", false, 0, 0, false);
13447 off = ((*parent)->address
13448 * bfd_octets_per_byte (input_bfd, input_section));
13449 _bfd_clear_contents ((*parent)->howto, input_bfd,
13450 input_section, data, off);
13451 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13452 (*parent)->addend = 0;
13453 (*parent)->howto = &none_howto;
13454 r = bfd_reloc_ok;
13457 /* Specific to MIPS: Deal with relocation types that require
13458 knowing the gp of the output bfd. */
13460 /* If we've managed to find the gp and have a special
13461 function for the relocation then go ahead, else default
13462 to the generic handling. */
13463 else if (gp_found
13464 && ((*parent)->howto->special_function
13465 == _bfd_mips_elf32_gprel16_reloc))
13466 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13467 input_section, relocatable,
13468 data, gp);
13469 else
13470 r = bfd_perform_relocation (input_bfd,
13471 *parent,
13472 data,
13473 input_section,
13474 relocatable ? abfd : NULL,
13475 &error_message);
13477 if (relocatable)
13479 asection *os = input_section->output_section;
13481 /* A partial link, so keep the relocs. */
13482 os->orelocation[os->reloc_count] = *parent;
13483 os->reloc_count++;
13486 if (r != bfd_reloc_ok)
13488 switch (r)
13490 case bfd_reloc_undefined:
13491 (*link_info->callbacks->undefined_symbol)
13492 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13493 input_bfd, input_section, (*parent)->address, true);
13494 break;
13495 case bfd_reloc_dangerous:
13496 BFD_ASSERT (error_message != NULL);
13497 (*link_info->callbacks->reloc_dangerous)
13498 (link_info, error_message,
13499 input_bfd, input_section, (*parent)->address);
13500 break;
13501 case bfd_reloc_overflow:
13502 (*link_info->callbacks->reloc_overflow)
13503 (link_info, NULL,
13504 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13505 (*parent)->howto->name, (*parent)->addend,
13506 input_bfd, input_section, (*parent)->address);
13507 break;
13508 case bfd_reloc_outofrange:
13509 /* PR ld/13730:
13510 This error can result when processing some partially
13511 complete binaries. Do not abort, but issue an error
13512 message instead. */
13513 link_info->callbacks->einfo
13514 /* xgettext:c-format */
13515 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13516 abfd, input_section, * parent);
13517 goto error_return;
13519 case bfd_reloc_notsupported:
13520 /* PR ld/17512
13521 This error can result when processing a corrupt binary.
13522 Do not abort. Issue an error message instead. */
13523 link_info->callbacks->einfo
13524 /* xgettext:c-format */
13525 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13526 abfd, input_section, * parent);
13527 goto error_return;
13529 default:
13530 /* PR 17512; file: 90c2a92e.
13531 Report unexpected results, without aborting. */
13532 link_info->callbacks->einfo
13533 /* xgettext:c-format */
13534 (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13535 abfd, input_section, * parent, r);
13536 break;
13543 out:
13544 free (reloc_vector);
13545 return data;
13548 static bool
13549 mips_elf_relax_delete_bytes (bfd *abfd,
13550 asection *sec, bfd_vma addr, int count)
13552 Elf_Internal_Shdr *symtab_hdr;
13553 unsigned int sec_shndx;
13554 bfd_byte *contents;
13555 Elf_Internal_Rela *irel, *irelend;
13556 Elf_Internal_Sym *isym;
13557 Elf_Internal_Sym *isymend;
13558 struct elf_link_hash_entry **sym_hashes;
13559 struct elf_link_hash_entry **end_hashes;
13560 struct elf_link_hash_entry **start_hashes;
13561 unsigned int symcount;
13563 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13564 contents = elf_section_data (sec)->this_hdr.contents;
13566 irel = elf_section_data (sec)->relocs;
13567 irelend = irel + sec->reloc_count;
13569 /* Actually delete the bytes. */
13570 memmove (contents + addr, contents + addr + count,
13571 (size_t) (sec->size - addr - count));
13572 sec->size -= count;
13574 /* Adjust all the relocs. */
13575 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13577 /* Get the new reloc address. */
13578 if (irel->r_offset > addr)
13579 irel->r_offset -= count;
13582 BFD_ASSERT (addr % 2 == 0);
13583 BFD_ASSERT (count % 2 == 0);
13585 /* Adjust the local symbols defined in this section. */
13586 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13587 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13588 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13589 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13590 isym->st_value -= count;
13592 /* Now adjust the global symbols defined in this section. */
13593 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13594 - symtab_hdr->sh_info);
13595 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13596 end_hashes = sym_hashes + symcount;
13598 for (; sym_hashes < end_hashes; sym_hashes++)
13600 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13602 if ((sym_hash->root.type == bfd_link_hash_defined
13603 || sym_hash->root.type == bfd_link_hash_defweak)
13604 && sym_hash->root.u.def.section == sec)
13606 bfd_vma value = sym_hash->root.u.def.value;
13608 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13609 value &= MINUS_TWO;
13610 if (value > addr)
13611 sym_hash->root.u.def.value -= count;
13615 return true;
13619 /* Opcodes needed for microMIPS relaxation as found in
13620 opcodes/micromips-opc.c. */
13622 struct opcode_descriptor {
13623 unsigned long match;
13624 unsigned long mask;
13627 /* The $ra register aka $31. */
13629 #define RA 31
13631 /* 32-bit instruction format register fields. */
13633 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13634 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13636 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13638 #define OP16_VALID_REG(r) \
13639 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13642 /* 32-bit and 16-bit branches. */
13644 static const struct opcode_descriptor b_insns_32[] = {
13645 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13646 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13647 { 0, 0 } /* End marker for find_match(). */
13650 static const struct opcode_descriptor bc_insn_32 =
13651 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13653 static const struct opcode_descriptor bz_insn_32 =
13654 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13656 static const struct opcode_descriptor bzal_insn_32 =
13657 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13659 static const struct opcode_descriptor beq_insn_32 =
13660 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13662 static const struct opcode_descriptor b_insn_16 =
13663 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13665 static const struct opcode_descriptor bz_insn_16 =
13666 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13669 /* 32-bit and 16-bit branch EQ and NE zero. */
13671 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13672 eq and second the ne. This convention is used when replacing a
13673 32-bit BEQ/BNE with the 16-bit version. */
13675 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13677 static const struct opcode_descriptor bz_rs_insns_32[] = {
13678 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13679 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13680 { 0, 0 } /* End marker for find_match(). */
13683 static const struct opcode_descriptor bz_rt_insns_32[] = {
13684 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13685 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13686 { 0, 0 } /* End marker for find_match(). */
13689 static const struct opcode_descriptor bzc_insns_32[] = {
13690 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13691 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13692 { 0, 0 } /* End marker for find_match(). */
13695 static const struct opcode_descriptor bz_insns_16[] = {
13696 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13697 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13698 { 0, 0 } /* End marker for find_match(). */
13701 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13703 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13704 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13707 /* 32-bit instructions with a delay slot. */
13709 static const struct opcode_descriptor jal_insn_32_bd16 =
13710 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13712 static const struct opcode_descriptor jal_insn_32_bd32 =
13713 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13715 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13716 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13718 static const struct opcode_descriptor j_insn_32 =
13719 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13721 static const struct opcode_descriptor jalr_insn_32 =
13722 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13724 /* This table can be compacted, because no opcode replacement is made. */
13726 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13727 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13729 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13730 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13732 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13733 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13734 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13735 { 0, 0 } /* End marker for find_match(). */
13738 /* This table can be compacted, because no opcode replacement is made. */
13740 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13741 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13743 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13744 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13745 { 0, 0 } /* End marker for find_match(). */
13749 /* 16-bit instructions with a delay slot. */
13751 static const struct opcode_descriptor jalr_insn_16_bd16 =
13752 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13754 static const struct opcode_descriptor jalr_insn_16_bd32 =
13755 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13757 static const struct opcode_descriptor jr_insn_16 =
13758 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13760 #define JR16_REG(opcode) ((opcode) & 0x1f)
13762 /* This table can be compacted, because no opcode replacement is made. */
13764 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13765 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13767 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13768 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13769 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13770 { 0, 0 } /* End marker for find_match(). */
13774 /* LUI instruction. */
13776 static const struct opcode_descriptor lui_insn =
13777 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13780 /* ADDIU instruction. */
13782 static const struct opcode_descriptor addiu_insn =
13783 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13785 static const struct opcode_descriptor addiupc_insn =
13786 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13788 #define ADDIUPC_REG_FIELD(r) \
13789 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13792 /* Relaxable instructions in a JAL delay slot: MOVE. */
13794 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13795 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13796 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13797 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13799 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13800 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13802 static const struct opcode_descriptor move_insns_32[] = {
13803 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13804 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13805 { 0, 0 } /* End marker for find_match(). */
13808 static const struct opcode_descriptor move_insn_16 =
13809 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13812 /* NOP instructions. */
13814 static const struct opcode_descriptor nop_insn_32 =
13815 { /* "nop", "", */ 0x00000000, 0xffffffff };
13817 static const struct opcode_descriptor nop_insn_16 =
13818 { /* "nop", "", */ 0x0c00, 0xffff };
13821 /* Instruction match support. */
13823 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13825 static int
13826 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13828 unsigned long indx;
13830 for (indx = 0; insn[indx].mask != 0; indx++)
13831 if (MATCH (opcode, insn[indx]))
13832 return indx;
13834 return -1;
13838 /* Branch and delay slot decoding support. */
13840 /* If PTR points to what *might* be a 16-bit branch or jump, then
13841 return the minimum length of its delay slot, otherwise return 0.
13842 Non-zero results are not definitive as we might be checking against
13843 the second half of another instruction. */
13845 static int
13846 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13848 unsigned long opcode;
13849 int bdsize;
13851 opcode = bfd_get_16 (abfd, ptr);
13852 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13853 /* 16-bit branch/jump with a 32-bit delay slot. */
13854 bdsize = 4;
13855 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13856 || find_match (opcode, ds_insns_16_bd16) >= 0)
13857 /* 16-bit branch/jump with a 16-bit delay slot. */
13858 bdsize = 2;
13859 else
13860 /* No delay slot. */
13861 bdsize = 0;
13863 return bdsize;
13866 /* If PTR points to what *might* be a 32-bit branch or jump, then
13867 return the minimum length of its delay slot, otherwise return 0.
13868 Non-zero results are not definitive as we might be checking against
13869 the second half of another instruction. */
13871 static int
13872 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13874 unsigned long opcode;
13875 int bdsize;
13877 opcode = bfd_get_micromips_32 (abfd, ptr);
13878 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13879 /* 32-bit branch/jump with a 32-bit delay slot. */
13880 bdsize = 4;
13881 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13882 /* 32-bit branch/jump with a 16-bit delay slot. */
13883 bdsize = 2;
13884 else
13885 /* No delay slot. */
13886 bdsize = 0;
13888 return bdsize;
13891 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13892 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13894 static bool
13895 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13897 unsigned long opcode;
13899 opcode = bfd_get_16 (abfd, ptr);
13900 if (MATCH (opcode, b_insn_16)
13901 /* B16 */
13902 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13903 /* JR16 */
13904 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13905 /* BEQZ16, BNEZ16 */
13906 || (MATCH (opcode, jalr_insn_16_bd32)
13907 /* JALR16 */
13908 && reg != JR16_REG (opcode) && reg != RA))
13909 return true;
13911 return false;
13914 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13915 then return TRUE, otherwise FALSE. */
13917 static bool
13918 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13920 unsigned long opcode;
13922 opcode = bfd_get_micromips_32 (abfd, ptr);
13923 if (MATCH (opcode, j_insn_32)
13924 /* J */
13925 || MATCH (opcode, bc_insn_32)
13926 /* BC1F, BC1T, BC2F, BC2T */
13927 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13928 /* JAL, JALX */
13929 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13930 /* BGEZ, BGTZ, BLEZ, BLTZ */
13931 || (MATCH (opcode, bzal_insn_32)
13932 /* BGEZAL, BLTZAL */
13933 && reg != OP32_SREG (opcode) && reg != RA)
13934 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13935 /* JALR, JALR.HB, BEQ, BNE */
13936 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13937 return true;
13939 return false;
13942 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13943 IRELEND) at OFFSET indicate that there must be a compact branch there,
13944 then return TRUE, otherwise FALSE. */
13946 static bool
13947 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13948 const Elf_Internal_Rela *internal_relocs,
13949 const Elf_Internal_Rela *irelend)
13951 const Elf_Internal_Rela *irel;
13952 unsigned long opcode;
13954 opcode = bfd_get_micromips_32 (abfd, ptr);
13955 if (find_match (opcode, bzc_insns_32) < 0)
13956 return false;
13958 for (irel = internal_relocs; irel < irelend; irel++)
13959 if (irel->r_offset == offset
13960 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13961 return true;
13963 return false;
13966 /* Bitsize checking. */
13967 #define IS_BITSIZE(val, N) \
13968 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13969 - (1ULL << ((N) - 1))) == (val))
13972 bool
13973 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13974 struct bfd_link_info *link_info,
13975 bool *again)
13977 bool insn32 = mips_elf_hash_table (link_info)->insn32;
13978 Elf_Internal_Shdr *symtab_hdr;
13979 Elf_Internal_Rela *internal_relocs;
13980 Elf_Internal_Rela *irel, *irelend;
13981 bfd_byte *contents = NULL;
13982 Elf_Internal_Sym *isymbuf = NULL;
13984 /* Assume nothing changes. */
13985 *again = false;
13987 /* We don't have to do anything for a relocatable link, if
13988 this section does not have relocs, or if this is not a
13989 code section. */
13991 if (bfd_link_relocatable (link_info)
13992 || sec->reloc_count == 0
13993 || (sec->flags & SEC_RELOC) == 0
13994 || (sec->flags & SEC_HAS_CONTENTS) == 0
13995 || (sec->flags & SEC_CODE) == 0)
13996 return true;
13998 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14000 /* Get a copy of the native relocations. */
14001 internal_relocs = (_bfd_elf_link_read_relocs
14002 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14003 link_info->keep_memory));
14004 if (internal_relocs == NULL)
14005 goto error_return;
14007 /* Walk through them looking for relaxing opportunities. */
14008 irelend = internal_relocs + sec->reloc_count;
14009 for (irel = internal_relocs; irel < irelend; irel++)
14011 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14012 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14013 bool target_is_micromips_code_p;
14014 unsigned long opcode;
14015 bfd_vma symval;
14016 bfd_vma pcrval;
14017 bfd_byte *ptr;
14018 int fndopc;
14020 /* The number of bytes to delete for relaxation and from where
14021 to delete these bytes starting at irel->r_offset. */
14022 int delcnt = 0;
14023 int deloff = 0;
14025 /* If this isn't something that can be relaxed, then ignore
14026 this reloc. */
14027 if (r_type != R_MICROMIPS_HI16
14028 && r_type != R_MICROMIPS_PC16_S1
14029 && r_type != R_MICROMIPS_26_S1)
14030 continue;
14032 /* Get the section contents if we haven't done so already. */
14033 if (contents == NULL)
14035 /* Get cached copy if it exists. */
14036 if (elf_section_data (sec)->this_hdr.contents != NULL)
14037 contents = elf_section_data (sec)->this_hdr.contents;
14038 /* Go get them off disk. */
14039 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14040 goto error_return;
14042 ptr = contents + irel->r_offset;
14044 /* Read this BFD's local symbols if we haven't done so already. */
14045 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14047 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14048 if (isymbuf == NULL)
14049 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14050 symtab_hdr->sh_info, 0,
14051 NULL, NULL, NULL);
14052 if (isymbuf == NULL)
14053 goto error_return;
14056 /* Get the value of the symbol referred to by the reloc. */
14057 if (r_symndx < symtab_hdr->sh_info)
14059 /* A local symbol. */
14060 Elf_Internal_Sym *isym;
14061 asection *sym_sec;
14063 isym = isymbuf + r_symndx;
14064 if (isym->st_shndx == SHN_UNDEF)
14065 sym_sec = bfd_und_section_ptr;
14066 else if (isym->st_shndx == SHN_ABS)
14067 sym_sec = bfd_abs_section_ptr;
14068 else if (isym->st_shndx == SHN_COMMON)
14069 sym_sec = bfd_com_section_ptr;
14070 else
14071 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14072 symval = (isym->st_value
14073 + sym_sec->output_section->vma
14074 + sym_sec->output_offset);
14075 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14077 else
14079 unsigned long indx;
14080 struct elf_link_hash_entry *h;
14082 /* An external symbol. */
14083 indx = r_symndx - symtab_hdr->sh_info;
14084 h = elf_sym_hashes (abfd)[indx];
14085 BFD_ASSERT (h != NULL);
14087 if (h->root.type != bfd_link_hash_defined
14088 && h->root.type != bfd_link_hash_defweak)
14089 /* This appears to be a reference to an undefined
14090 symbol. Just ignore it -- it will be caught by the
14091 regular reloc processing. */
14092 continue;
14094 symval = (h->root.u.def.value
14095 + h->root.u.def.section->output_section->vma
14096 + h->root.u.def.section->output_offset);
14097 target_is_micromips_code_p = (!h->needs_plt
14098 && ELF_ST_IS_MICROMIPS (h->other));
14102 /* For simplicity of coding, we are going to modify the
14103 section contents, the section relocs, and the BFD symbol
14104 table. We must tell the rest of the code not to free up this
14105 information. It would be possible to instead create a table
14106 of changes which have to be made, as is done in coff-mips.c;
14107 that would be more work, but would require less memory when
14108 the linker is run. */
14110 /* Only 32-bit instructions relaxed. */
14111 if (irel->r_offset + 4 > sec->size)
14112 continue;
14114 opcode = bfd_get_micromips_32 (abfd, ptr);
14116 /* This is the pc-relative distance from the instruction the
14117 relocation is applied to, to the symbol referred. */
14118 pcrval = (symval
14119 - (sec->output_section->vma + sec->output_offset)
14120 - irel->r_offset);
14122 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14123 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14124 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
14126 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14128 where pcrval has first to be adjusted to apply against the LO16
14129 location (we make the adjustment later on, when we have figured
14130 out the offset). */
14131 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14133 bool bzc = false;
14134 unsigned long nextopc;
14135 unsigned long reg;
14136 bfd_vma offset;
14138 /* Give up if the previous reloc was a HI16 against this symbol
14139 too. */
14140 if (irel > internal_relocs
14141 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14142 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14143 continue;
14145 /* Or if the next reloc is not a LO16 against this symbol. */
14146 if (irel + 1 >= irelend
14147 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14148 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14149 continue;
14151 /* Or if the second next reloc is a LO16 against this symbol too. */
14152 if (irel + 2 >= irelend
14153 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14154 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14155 continue;
14157 /* See if the LUI instruction *might* be in a branch delay slot.
14158 We check whether what looks like a 16-bit branch or jump is
14159 actually an immediate argument to a compact branch, and let
14160 it through if so. */
14161 if (irel->r_offset >= 2
14162 && check_br16_dslot (abfd, ptr - 2)
14163 && !(irel->r_offset >= 4
14164 && (bzc = check_relocated_bzc (abfd,
14165 ptr - 4, irel->r_offset - 4,
14166 internal_relocs, irelend))))
14167 continue;
14168 if (irel->r_offset >= 4
14169 && !bzc
14170 && check_br32_dslot (abfd, ptr - 4))
14171 continue;
14173 reg = OP32_SREG (opcode);
14175 /* We only relax adjacent instructions or ones separated with
14176 a branch or jump that has a delay slot. The branch or jump
14177 must not fiddle with the register used to hold the address.
14178 Subtract 4 for the LUI itself. */
14179 offset = irel[1].r_offset - irel[0].r_offset;
14180 switch (offset - 4)
14182 case 0:
14183 break;
14184 case 2:
14185 if (check_br16 (abfd, ptr + 4, reg))
14186 break;
14187 continue;
14188 case 4:
14189 if (check_br32 (abfd, ptr + 4, reg))
14190 break;
14191 continue;
14192 default:
14193 continue;
14196 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14198 /* Give up unless the same register is used with both
14199 relocations. */
14200 if (OP32_SREG (nextopc) != reg)
14201 continue;
14203 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14204 and rounding up to take masking of the two LSBs into account. */
14205 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14207 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14208 if (IS_BITSIZE (symval, 16))
14210 /* Fix the relocation's type. */
14211 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14213 /* Instructions using R_MICROMIPS_LO16 have the base or
14214 source register in bits 20:16. This register becomes $0
14215 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
14216 nextopc &= ~0x001f0000;
14217 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14218 contents + irel[1].r_offset);
14221 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14222 We add 4 to take LUI deletion into account while checking
14223 the PC-relative distance. */
14224 else if (symval % 4 == 0
14225 && IS_BITSIZE (pcrval + 4, 25)
14226 && MATCH (nextopc, addiu_insn)
14227 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14228 && OP16_VALID_REG (OP32_TREG (nextopc)))
14230 /* Fix the relocation's type. */
14231 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14233 /* Replace ADDIU with the ADDIUPC version. */
14234 nextopc = (addiupc_insn.match
14235 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14237 bfd_put_micromips_32 (abfd, nextopc,
14238 contents + irel[1].r_offset);
14241 /* Can't do anything, give up, sigh... */
14242 else
14243 continue;
14245 /* Fix the relocation's type. */
14246 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14248 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14249 delcnt = 4;
14250 deloff = 0;
14253 /* Compact branch relaxation -- due to the multitude of macros
14254 employed by the compiler/assembler, compact branches are not
14255 always generated. Obviously, this can/will be fixed elsewhere,
14256 but there is no drawback in double checking it here. */
14257 else if (r_type == R_MICROMIPS_PC16_S1
14258 && irel->r_offset + 5 < sec->size
14259 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14260 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14261 && ((!insn32
14262 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14263 nop_insn_16) ? 2 : 0))
14264 || (irel->r_offset + 7 < sec->size
14265 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14266 ptr + 4),
14267 nop_insn_32) ? 4 : 0))))
14269 unsigned long reg;
14271 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14273 /* Replace BEQZ/BNEZ with the compact version. */
14274 opcode = (bzc_insns_32[fndopc].match
14275 | BZC32_REG_FIELD (reg)
14276 | (opcode & 0xffff)); /* Addend value. */
14278 bfd_put_micromips_32 (abfd, opcode, ptr);
14280 /* Delete the delay slot NOP: two or four bytes from
14281 irel->offset + 4; delcnt has already been set above. */
14282 deloff = 4;
14285 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14286 to check the distance from the next instruction, so subtract 2. */
14287 else if (!insn32
14288 && r_type == R_MICROMIPS_PC16_S1
14289 && IS_BITSIZE (pcrval - 2, 11)
14290 && find_match (opcode, b_insns_32) >= 0)
14292 /* Fix the relocation's type. */
14293 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14295 /* Replace the 32-bit opcode with a 16-bit opcode. */
14296 bfd_put_16 (abfd,
14297 (b_insn_16.match
14298 | (opcode & 0x3ff)), /* Addend value. */
14299 ptr);
14301 /* Delete 2 bytes from irel->r_offset + 2. */
14302 delcnt = 2;
14303 deloff = 2;
14306 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14307 to check the distance from the next instruction, so subtract 2. */
14308 else if (!insn32
14309 && r_type == R_MICROMIPS_PC16_S1
14310 && IS_BITSIZE (pcrval - 2, 8)
14311 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14312 && OP16_VALID_REG (OP32_SREG (opcode)))
14313 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14314 && OP16_VALID_REG (OP32_TREG (opcode)))))
14316 unsigned long reg;
14318 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14320 /* Fix the relocation's type. */
14321 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14323 /* Replace the 32-bit opcode with a 16-bit opcode. */
14324 bfd_put_16 (abfd,
14325 (bz_insns_16[fndopc].match
14326 | BZ16_REG_FIELD (reg)
14327 | (opcode & 0x7f)), /* Addend value. */
14328 ptr);
14330 /* Delete 2 bytes from irel->r_offset + 2. */
14331 delcnt = 2;
14332 deloff = 2;
14335 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14336 else if (!insn32
14337 && r_type == R_MICROMIPS_26_S1
14338 && target_is_micromips_code_p
14339 && irel->r_offset + 7 < sec->size
14340 && MATCH (opcode, jal_insn_32_bd32))
14342 unsigned long n32opc;
14343 bool relaxed = false;
14345 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14347 if (MATCH (n32opc, nop_insn_32))
14349 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14350 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14352 relaxed = true;
14354 else if (find_match (n32opc, move_insns_32) >= 0)
14356 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14357 bfd_put_16 (abfd,
14358 (move_insn_16.match
14359 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14360 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14361 ptr + 4);
14363 relaxed = true;
14365 /* Other 32-bit instructions relaxable to 16-bit
14366 instructions will be handled here later. */
14368 if (relaxed)
14370 /* JAL with 32-bit delay slot that is changed to a JALS
14371 with 16-bit delay slot. */
14372 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14374 /* Delete 2 bytes from irel->r_offset + 6. */
14375 delcnt = 2;
14376 deloff = 6;
14380 if (delcnt != 0)
14382 /* Note that we've changed the relocs, section contents, etc. */
14383 elf_section_data (sec)->relocs = internal_relocs;
14384 elf_section_data (sec)->this_hdr.contents = contents;
14385 symtab_hdr->contents = (unsigned char *) isymbuf;
14387 /* Delete bytes depending on the delcnt and deloff. */
14388 if (!mips_elf_relax_delete_bytes (abfd, sec,
14389 irel->r_offset + deloff, delcnt))
14390 goto error_return;
14392 /* That will change things, so we should relax again.
14393 Note that this is not required, and it may be slow. */
14394 *again = true;
14398 if (isymbuf != NULL
14399 && symtab_hdr->contents != (unsigned char *) isymbuf)
14401 if (! link_info->keep_memory)
14402 free (isymbuf);
14403 else
14405 /* Cache the symbols for elf_link_input_bfd. */
14406 symtab_hdr->contents = (unsigned char *) isymbuf;
14410 if (contents != NULL
14411 && elf_section_data (sec)->this_hdr.contents != contents)
14413 if (! link_info->keep_memory)
14414 free (contents);
14415 else
14417 /* Cache the section contents for elf_link_input_bfd. */
14418 elf_section_data (sec)->this_hdr.contents = contents;
14422 if (elf_section_data (sec)->relocs != internal_relocs)
14423 free (internal_relocs);
14425 return true;
14427 error_return:
14428 if (symtab_hdr->contents != (unsigned char *) isymbuf)
14429 free (isymbuf);
14430 if (elf_section_data (sec)->this_hdr.contents != contents)
14431 free (contents);
14432 if (elf_section_data (sec)->relocs != internal_relocs)
14433 free (internal_relocs);
14435 return false;
14438 /* Create a MIPS ELF linker hash table. */
14440 struct bfd_link_hash_table *
14441 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14443 struct mips_elf_link_hash_table *ret;
14444 size_t amt = sizeof (struct mips_elf_link_hash_table);
14446 ret = bfd_zmalloc (amt);
14447 if (ret == NULL)
14448 return NULL;
14450 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14451 mips_elf_link_hash_newfunc,
14452 sizeof (struct mips_elf_link_hash_entry),
14453 MIPS_ELF_DATA))
14455 free (ret);
14456 return NULL;
14458 ret->root.init_plt_refcount.plist = NULL;
14459 ret->root.init_plt_offset.plist = NULL;
14461 return &ret->root.root;
14464 /* Likewise, but indicate that the target is VxWorks. */
14466 struct bfd_link_hash_table *
14467 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14469 struct bfd_link_hash_table *ret;
14471 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14472 if (ret)
14474 struct mips_elf_link_hash_table *htab;
14476 htab = (struct mips_elf_link_hash_table *) ret;
14477 htab->use_plts_and_copy_relocs = true;
14479 return ret;
14482 /* A function that the linker calls if we are allowed to use PLTs
14483 and copy relocs. */
14485 void
14486 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14488 mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14491 /* A function that the linker calls to select between all or only
14492 32-bit microMIPS instructions, and between making or ignoring
14493 branch relocation checks for invalid transitions between ISA modes.
14494 Also record whether we have been configured for a GNU target. */
14496 void
14497 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14498 bool ignore_branch_isa,
14499 bool gnu_target)
14501 mips_elf_hash_table (info)->insn32 = insn32;
14502 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14503 mips_elf_hash_table (info)->gnu_target = gnu_target;
14506 /* A function that the linker calls to enable use of compact branches in
14507 linker generated code for MIPSR6. */
14509 void
14510 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14512 mips_elf_hash_table (info)->compact_branches = on;
14516 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14518 struct mips_mach_extension
14520 unsigned long extension, base;
14523 /* An array that maps 64-bit architectures to the corresponding 32-bit
14524 architectures. */
14525 static const struct mips_mach_extension mips_mach_32_64[] =
14527 { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14528 { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14529 { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14530 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14531 { bfd_mach_mipsisa64, bfd_mach_mipsisa32 }
14534 /* An array describing how BFD machines relate to one another. The entries
14535 are ordered topologically with MIPS I extensions listed last. */
14537 static const struct mips_mach_extension mips_mach_extensions[] =
14539 /* MIPS64r2 extensions. */
14540 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14541 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14542 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14543 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14544 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14545 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14546 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14548 /* MIPS64 extensions. */
14549 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14550 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14551 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14553 /* MIPS V extensions. */
14554 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14556 /* R10000 extensions. */
14557 { bfd_mach_mips12000, bfd_mach_mips10000 },
14558 { bfd_mach_mips14000, bfd_mach_mips10000 },
14559 { bfd_mach_mips16000, bfd_mach_mips10000 },
14561 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14562 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14563 better to allow vr5400 and vr5500 code to be merged anyway, since
14564 many libraries will just use the core ISA. Perhaps we could add
14565 some sort of ASE flag if this ever proves a problem. */
14566 { bfd_mach_mips5500, bfd_mach_mips5400 },
14567 { bfd_mach_mips5400, bfd_mach_mips5000 },
14569 /* MIPS IV extensions. */
14570 { bfd_mach_mips5, bfd_mach_mips8000 },
14571 { bfd_mach_mips10000, bfd_mach_mips8000 },
14572 { bfd_mach_mips5000, bfd_mach_mips8000 },
14573 { bfd_mach_mips7000, bfd_mach_mips8000 },
14574 { bfd_mach_mips9000, bfd_mach_mips8000 },
14576 /* VR4100 extensions. */
14577 { bfd_mach_mips4120, bfd_mach_mips4100 },
14578 { bfd_mach_mips4111, bfd_mach_mips4100 },
14580 /* MIPS III extensions. */
14581 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14582 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14583 { bfd_mach_mips8000, bfd_mach_mips4000 },
14584 { bfd_mach_mips4650, bfd_mach_mips4000 },
14585 { bfd_mach_mips4600, bfd_mach_mips4000 },
14586 { bfd_mach_mips4400, bfd_mach_mips4000 },
14587 { bfd_mach_mips4300, bfd_mach_mips4000 },
14588 { bfd_mach_mips4100, bfd_mach_mips4000 },
14589 { bfd_mach_mips5900, bfd_mach_mips4000 },
14591 /* MIPS32r3 extensions. */
14592 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14594 /* MIPS32r2 extensions. */
14595 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14597 /* MIPS32 extensions. */
14598 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14600 /* MIPS II extensions. */
14601 { bfd_mach_mips4000, bfd_mach_mips6000 },
14602 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14603 { bfd_mach_mips4010, bfd_mach_mips6000 },
14604 { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14606 /* MIPS I extensions. */
14607 { bfd_mach_mips6000, bfd_mach_mips3000 },
14608 { bfd_mach_mips3900, bfd_mach_mips3000 }
14611 /* Return true if bfd machine EXTENSION is the same as BASE, or if
14612 EXTENSION is the 64-bit equivalent of a 32-bit BASE. */
14614 static bool
14615 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14617 size_t i;
14619 if (extension == base)
14620 return true;
14622 for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14623 if (extension == mips_mach_32_64[i].extension)
14624 return base == mips_mach_32_64[i].base;
14626 return false;
14629 static bool
14630 mips_mach_extends_p (unsigned long base, unsigned long extension)
14632 size_t i;
14634 if (mips_mach_extends_32_64 (base, extension))
14635 return true;
14637 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14638 if (extension == mips_mach_extensions[i].extension)
14640 extension = mips_mach_extensions[i].base;
14641 if (mips_mach_extends_32_64 (base, extension))
14642 return true;
14645 return false;
14648 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14650 static unsigned long
14651 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14653 switch (isa_ext)
14655 case AFL_EXT_3900: return bfd_mach_mips3900;
14656 case AFL_EXT_4010: return bfd_mach_mips4010;
14657 case AFL_EXT_4100: return bfd_mach_mips4100;
14658 case AFL_EXT_4111: return bfd_mach_mips4111;
14659 case AFL_EXT_4120: return bfd_mach_mips4120;
14660 case AFL_EXT_4650: return bfd_mach_mips4650;
14661 case AFL_EXT_5400: return bfd_mach_mips5400;
14662 case AFL_EXT_5500: return bfd_mach_mips5500;
14663 case AFL_EXT_5900: return bfd_mach_mips5900;
14664 case AFL_EXT_10000: return bfd_mach_mips10000;
14665 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14666 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14667 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14668 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14669 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14670 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14671 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14672 default: return bfd_mach_mips3000;
14676 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14678 unsigned int
14679 bfd_mips_isa_ext (bfd *abfd)
14681 switch (bfd_get_mach (abfd))
14683 case bfd_mach_mips3900: return AFL_EXT_3900;
14684 case bfd_mach_mips4010: return AFL_EXT_4010;
14685 case bfd_mach_mips4100: return AFL_EXT_4100;
14686 case bfd_mach_mips4111: return AFL_EXT_4111;
14687 case bfd_mach_mips4120: return AFL_EXT_4120;
14688 case bfd_mach_mips4650: return AFL_EXT_4650;
14689 case bfd_mach_mips5400: return AFL_EXT_5400;
14690 case bfd_mach_mips5500: return AFL_EXT_5500;
14691 case bfd_mach_mips5900: return AFL_EXT_5900;
14692 case bfd_mach_mips10000: return AFL_EXT_10000;
14693 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14694 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14695 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14696 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14697 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14698 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14699 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14700 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14701 case bfd_mach_mips_interaptiv_mr2:
14702 return AFL_EXT_INTERAPTIV_MR2;
14703 default: return 0;
14707 /* Encode ISA level and revision as a single value. */
14708 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14710 /* Decode a single value into level and revision. */
14711 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14712 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14714 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14716 static void
14717 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14719 int new_isa = 0;
14720 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14722 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14723 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14724 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14725 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14726 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14727 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14728 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14729 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14730 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14731 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14732 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14733 default:
14734 _bfd_error_handler
14735 /* xgettext:c-format */
14736 (_("%pB: unknown architecture %s"),
14737 abfd, bfd_printable_name (abfd));
14740 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14742 abiflags->isa_level = ISA_LEVEL (new_isa);
14743 abiflags->isa_rev = ISA_REV (new_isa);
14746 /* Update the isa_ext if ABFD describes a further extension. */
14747 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14748 bfd_get_mach (abfd)))
14749 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14752 /* Return true if the given ELF header flags describe a 32-bit binary. */
14754 static bool
14755 mips_32bit_flags_p (flagword flags)
14757 return ((flags & EF_MIPS_32BITMODE) != 0
14758 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14759 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14760 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14761 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14762 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14763 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14764 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14767 /* Infer the content of the ABI flags based on the elf header. */
14769 static void
14770 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14772 obj_attribute *in_attr;
14774 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14775 update_mips_abiflags_isa (abfd, abiflags);
14777 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14778 abiflags->gpr_size = AFL_REG_32;
14779 else
14780 abiflags->gpr_size = AFL_REG_64;
14782 abiflags->cpr1_size = AFL_REG_NONE;
14784 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14785 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14787 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14788 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14789 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14790 && abiflags->gpr_size == AFL_REG_32))
14791 abiflags->cpr1_size = AFL_REG_32;
14792 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14793 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14794 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14795 abiflags->cpr1_size = AFL_REG_64;
14797 abiflags->cpr2_size = AFL_REG_NONE;
14799 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14800 abiflags->ases |= AFL_ASE_MDMX;
14801 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14802 abiflags->ases |= AFL_ASE_MIPS16;
14803 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14804 abiflags->ases |= AFL_ASE_MICROMIPS;
14806 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14807 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14808 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14809 && abiflags->isa_level >= 32
14810 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14811 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14814 /* We need to use a special link routine to handle the .reginfo and
14815 the .mdebug sections. We need to merge all instances of these
14816 sections together, not write them all out sequentially. */
14818 bool
14819 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14821 asection *o;
14822 struct bfd_link_order *p;
14823 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14824 asection *rtproc_sec, *abiflags_sec;
14825 Elf32_RegInfo reginfo;
14826 struct ecoff_debug_info debug;
14827 struct mips_htab_traverse_info hti;
14828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14829 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14830 HDRR *symhdr = &debug.symbolic_header;
14831 void *mdebug_handle = NULL;
14832 asection *s;
14833 EXTR esym;
14834 unsigned int i;
14835 bfd_size_type amt;
14836 struct mips_elf_link_hash_table *htab;
14838 static const char * const secname[] =
14840 ".text", ".init", ".fini", ".data",
14841 ".rodata", ".sdata", ".sbss", ".bss"
14843 static const int sc[] =
14845 scText, scInit, scFini, scData,
14846 scRData, scSData, scSBss, scBss
14849 htab = mips_elf_hash_table (info);
14850 BFD_ASSERT (htab != NULL);
14852 /* Sort the dynamic symbols so that those with GOT entries come after
14853 those without. */
14854 if (!mips_elf_sort_hash_table (abfd, info))
14855 return false;
14857 /* Create any scheduled LA25 stubs. */
14858 hti.info = info;
14859 hti.output_bfd = abfd;
14860 hti.error = false;
14861 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14862 if (hti.error)
14863 return false;
14865 /* Get a value for the GP register. */
14866 if (elf_gp (abfd) == 0)
14868 struct bfd_link_hash_entry *h;
14870 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14871 if (h != NULL && h->type == bfd_link_hash_defined)
14872 elf_gp (abfd) = (h->u.def.value
14873 + h->u.def.section->output_section->vma
14874 + h->u.def.section->output_offset);
14875 else if (htab->root.target_os == is_vxworks
14876 && (h = bfd_link_hash_lookup (info->hash,
14877 "_GLOBAL_OFFSET_TABLE_",
14878 false, false, true))
14879 && h->type == bfd_link_hash_defined)
14880 elf_gp (abfd) = (h->u.def.section->output_section->vma
14881 + h->u.def.section->output_offset
14882 + h->u.def.value);
14883 else if (bfd_link_relocatable (info))
14885 bfd_vma lo = MINUS_ONE;
14887 /* Find the GP-relative section with the lowest offset. */
14888 for (o = abfd->sections; o != NULL; o = o->next)
14889 if (o->vma < lo
14890 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14891 lo = o->vma;
14893 /* And calculate GP relative to that. */
14894 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14896 else
14898 /* If the relocate_section function needs to do a reloc
14899 involving the GP value, it should make a reloc_dangerous
14900 callback to warn that GP is not defined. */
14904 /* Go through the sections and collect the .reginfo and .mdebug
14905 information. */
14906 abiflags_sec = NULL;
14907 reginfo_sec = NULL;
14908 mdebug_sec = NULL;
14909 gptab_data_sec = NULL;
14910 gptab_bss_sec = NULL;
14911 for (o = abfd->sections; o != NULL; o = o->next)
14913 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14915 /* We have found the .MIPS.abiflags section in the output file.
14916 Look through all the link_orders comprising it and remove them.
14917 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14918 for (p = o->map_head.link_order; p != NULL; p = p->next)
14920 asection *input_section;
14922 if (p->type != bfd_indirect_link_order)
14924 if (p->type == bfd_data_link_order)
14925 continue;
14926 abort ();
14929 input_section = p->u.indirect.section;
14931 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14932 elf_link_input_bfd ignores this section. */
14933 input_section->flags &= ~SEC_HAS_CONTENTS;
14936 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14937 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14939 /* Skip this section later on (I don't think this currently
14940 matters, but someday it might). */
14941 o->map_head.link_order = NULL;
14943 abiflags_sec = o;
14946 if (strcmp (o->name, ".reginfo") == 0)
14948 memset (&reginfo, 0, sizeof reginfo);
14950 /* We have found the .reginfo section in the output file.
14951 Look through all the link_orders comprising it and merge
14952 the information together. */
14953 for (p = o->map_head.link_order; p != NULL; p = p->next)
14955 asection *input_section;
14956 bfd *input_bfd;
14957 Elf32_External_RegInfo ext;
14958 Elf32_RegInfo sub;
14959 bfd_size_type sz;
14961 if (p->type != bfd_indirect_link_order)
14963 if (p->type == bfd_data_link_order)
14964 continue;
14965 abort ();
14968 input_section = p->u.indirect.section;
14969 input_bfd = input_section->owner;
14971 sz = (input_section->size < sizeof (ext)
14972 ? input_section->size : sizeof (ext));
14973 memset (&ext, 0, sizeof (ext));
14974 if (! bfd_get_section_contents (input_bfd, input_section,
14975 &ext, 0, sz))
14976 return false;
14978 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14980 reginfo.ri_gprmask |= sub.ri_gprmask;
14981 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14982 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14983 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14984 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14986 /* ri_gp_value is set by the function
14987 `_bfd_mips_elf_section_processing' when the section is
14988 finally written out. */
14990 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14991 elf_link_input_bfd ignores this section. */
14992 input_section->flags &= ~SEC_HAS_CONTENTS;
14995 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14996 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14998 /* Skip this section later on (I don't think this currently
14999 matters, but someday it might). */
15000 o->map_head.link_order = NULL;
15002 reginfo_sec = o;
15005 if (strcmp (o->name, ".mdebug") == 0)
15007 struct extsym_info einfo;
15008 bfd_vma last;
15010 /* We have found the .mdebug section in the output file.
15011 Look through all the link_orders comprising it and merge
15012 the information together. */
15013 symhdr->magic = swap->sym_magic;
15014 /* FIXME: What should the version stamp be? */
15015 symhdr->vstamp = 0;
15016 symhdr->ilineMax = 0;
15017 symhdr->cbLine = 0;
15018 symhdr->idnMax = 0;
15019 symhdr->ipdMax = 0;
15020 symhdr->isymMax = 0;
15021 symhdr->ioptMax = 0;
15022 symhdr->iauxMax = 0;
15023 symhdr->issMax = 0;
15024 symhdr->issExtMax = 0;
15025 symhdr->ifdMax = 0;
15026 symhdr->crfd = 0;
15027 symhdr->iextMax = 0;
15029 /* We accumulate the debugging information itself in the
15030 debug_info structure. */
15031 debug.alloc_syments = false;
15032 debug.line = NULL;
15033 debug.external_dnr = NULL;
15034 debug.external_pdr = NULL;
15035 debug.external_sym = NULL;
15036 debug.external_opt = NULL;
15037 debug.external_aux = NULL;
15038 debug.ss = NULL;
15039 debug.ssext = debug.ssext_end = NULL;
15040 debug.external_fdr = NULL;
15041 debug.external_rfd = NULL;
15042 debug.external_ext = debug.external_ext_end = NULL;
15044 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15045 if (mdebug_handle == NULL)
15046 return false;
15048 esym.jmptbl = 0;
15049 esym.cobol_main = 0;
15050 esym.weakext = 0;
15051 esym.reserved = 0;
15052 esym.ifd = ifdNil;
15053 esym.asym.iss = issNil;
15054 esym.asym.st = stLocal;
15055 esym.asym.reserved = 0;
15056 esym.asym.index = indexNil;
15057 last = 0;
15058 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15060 esym.asym.sc = sc[i];
15061 s = bfd_get_section_by_name (abfd, secname[i]);
15062 if (s != NULL)
15064 esym.asym.value = s->vma;
15065 last = s->vma + s->size;
15067 else
15068 esym.asym.value = last;
15069 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15070 secname[i], &esym))
15071 return false;
15074 for (p = o->map_head.link_order; p != NULL; p = p->next)
15076 asection *input_section;
15077 bfd *input_bfd;
15078 const struct ecoff_debug_swap *input_swap;
15079 struct ecoff_debug_info input_debug;
15080 char *eraw_src;
15081 char *eraw_end;
15083 if (p->type != bfd_indirect_link_order)
15085 if (p->type == bfd_data_link_order)
15086 continue;
15087 abort ();
15090 input_section = p->u.indirect.section;
15091 input_bfd = input_section->owner;
15093 if (!is_mips_elf (input_bfd))
15095 /* I don't know what a non MIPS ELF bfd would be
15096 doing with a .mdebug section, but I don't really
15097 want to deal with it. */
15098 continue;
15101 input_swap = (get_elf_backend_data (input_bfd)
15102 ->elf_backend_ecoff_debug_swap);
15104 BFD_ASSERT (p->size == input_section->size);
15106 /* The ECOFF linking code expects that we have already
15107 read in the debugging information and set up an
15108 ecoff_debug_info structure, so we do that now. */
15109 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15110 &input_debug))
15111 return false;
15113 if (! (bfd_ecoff_debug_accumulate
15114 (mdebug_handle, abfd, &debug, swap, input_bfd,
15115 &input_debug, input_swap, info)))
15117 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15118 return false;
15121 /* Loop through the external symbols. For each one with
15122 interesting information, try to find the symbol in
15123 the linker global hash table and save the information
15124 for the output external symbols. */
15125 eraw_src = input_debug.external_ext;
15126 eraw_end = (eraw_src
15127 + (input_debug.symbolic_header.iextMax
15128 * input_swap->external_ext_size));
15129 for (;
15130 eraw_src < eraw_end;
15131 eraw_src += input_swap->external_ext_size)
15133 EXTR ext;
15134 const char *name;
15135 struct mips_elf_link_hash_entry *h;
15137 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15138 if (ext.asym.sc == scNil
15139 || ext.asym.sc == scUndefined
15140 || ext.asym.sc == scSUndefined)
15141 continue;
15143 name = input_debug.ssext + ext.asym.iss;
15144 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15145 name, false, false, true);
15146 if (h == NULL || h->esym.ifd != -2)
15147 continue;
15149 if (ext.ifd != -1)
15151 BFD_ASSERT (ext.ifd
15152 < input_debug.symbolic_header.ifdMax);
15153 ext.ifd = input_debug.ifdmap[ext.ifd];
15156 h->esym = ext;
15159 /* Free up the information we just read. */
15160 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15162 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15163 elf_link_input_bfd ignores this section. */
15164 input_section->flags &= ~SEC_HAS_CONTENTS;
15167 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15169 /* Create .rtproc section. */
15170 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15171 if (rtproc_sec == NULL)
15173 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15174 | SEC_LINKER_CREATED | SEC_READONLY);
15176 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15177 ".rtproc",
15178 flags);
15179 if (rtproc_sec == NULL
15180 || !bfd_set_section_alignment (rtproc_sec, 4))
15181 return false;
15184 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15185 info, rtproc_sec,
15186 &debug))
15187 return false;
15190 /* Build the external symbol information. */
15191 einfo.abfd = abfd;
15192 einfo.info = info;
15193 einfo.debug = &debug;
15194 einfo.swap = swap;
15195 einfo.failed = false;
15196 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15197 mips_elf_output_extsym, &einfo);
15198 if (einfo.failed)
15199 return false;
15201 /* Set the size of the .mdebug section. */
15202 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15204 /* Skip this section later on (I don't think this currently
15205 matters, but someday it might). */
15206 o->map_head.link_order = NULL;
15208 mdebug_sec = o;
15211 if (startswith (o->name, ".gptab."))
15213 const char *subname;
15214 unsigned int c;
15215 Elf32_gptab *tab;
15216 Elf32_External_gptab *ext_tab;
15217 unsigned int j;
15219 /* The .gptab.sdata and .gptab.sbss sections hold
15220 information describing how the small data area would
15221 change depending upon the -G switch. These sections
15222 not used in executables files. */
15223 if (! bfd_link_relocatable (info))
15225 for (p = o->map_head.link_order; p != NULL; p = p->next)
15227 asection *input_section;
15229 if (p->type != bfd_indirect_link_order)
15231 if (p->type == bfd_data_link_order)
15232 continue;
15233 abort ();
15236 input_section = p->u.indirect.section;
15238 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15239 elf_link_input_bfd ignores this section. */
15240 input_section->flags &= ~SEC_HAS_CONTENTS;
15243 /* Skip this section later on (I don't think this
15244 currently matters, but someday it might). */
15245 o->map_head.link_order = NULL;
15247 /* Really remove the section. */
15248 bfd_section_list_remove (abfd, o);
15249 --abfd->section_count;
15251 continue;
15254 /* There is one gptab for initialized data, and one for
15255 uninitialized data. */
15256 if (strcmp (o->name, ".gptab.sdata") == 0)
15257 gptab_data_sec = o;
15258 else if (strcmp (o->name, ".gptab.sbss") == 0)
15259 gptab_bss_sec = o;
15260 else
15262 _bfd_error_handler
15263 /* xgettext:c-format */
15264 (_("%pB: illegal section name `%pA'"), abfd, o);
15265 bfd_set_error (bfd_error_nonrepresentable_section);
15266 return false;
15269 /* The linker script always combines .gptab.data and
15270 .gptab.sdata into .gptab.sdata, and likewise for
15271 .gptab.bss and .gptab.sbss. It is possible that there is
15272 no .sdata or .sbss section in the output file, in which
15273 case we must change the name of the output section. */
15274 subname = o->name + sizeof ".gptab" - 1;
15275 if (bfd_get_section_by_name (abfd, subname) == NULL)
15277 if (o == gptab_data_sec)
15278 o->name = ".gptab.data";
15279 else
15280 o->name = ".gptab.bss";
15281 subname = o->name + sizeof ".gptab" - 1;
15282 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15285 /* Set up the first entry. */
15286 c = 1;
15287 amt = c * sizeof (Elf32_gptab);
15288 tab = bfd_malloc (amt);
15289 if (tab == NULL)
15290 return false;
15291 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15292 tab[0].gt_header.gt_unused = 0;
15294 /* Combine the input sections. */
15295 for (p = o->map_head.link_order; p != NULL; p = p->next)
15297 asection *input_section;
15298 bfd *input_bfd;
15299 bfd_size_type size;
15300 unsigned long last;
15301 bfd_size_type gpentry;
15303 if (p->type != bfd_indirect_link_order)
15305 if (p->type == bfd_data_link_order)
15306 continue;
15307 abort ();
15310 input_section = p->u.indirect.section;
15311 input_bfd = input_section->owner;
15313 /* Combine the gptab entries for this input section one
15314 by one. We know that the input gptab entries are
15315 sorted by ascending -G value. */
15316 size = input_section->size;
15317 last = 0;
15318 for (gpentry = sizeof (Elf32_External_gptab);
15319 gpentry < size;
15320 gpentry += sizeof (Elf32_External_gptab))
15322 Elf32_External_gptab ext_gptab;
15323 Elf32_gptab int_gptab;
15324 unsigned long val;
15325 unsigned long add;
15326 bool exact;
15327 unsigned int look;
15329 if (! (bfd_get_section_contents
15330 (input_bfd, input_section, &ext_gptab, gpentry,
15331 sizeof (Elf32_External_gptab))))
15333 free (tab);
15334 return false;
15337 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15338 &int_gptab);
15339 val = int_gptab.gt_entry.gt_g_value;
15340 add = int_gptab.gt_entry.gt_bytes - last;
15342 exact = false;
15343 for (look = 1; look < c; look++)
15345 if (tab[look].gt_entry.gt_g_value >= val)
15346 tab[look].gt_entry.gt_bytes += add;
15348 if (tab[look].gt_entry.gt_g_value == val)
15349 exact = true;
15352 if (! exact)
15354 Elf32_gptab *new_tab;
15355 unsigned int max;
15357 /* We need a new table entry. */
15358 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15359 new_tab = bfd_realloc (tab, amt);
15360 if (new_tab == NULL)
15362 free (tab);
15363 return false;
15365 tab = new_tab;
15366 tab[c].gt_entry.gt_g_value = val;
15367 tab[c].gt_entry.gt_bytes = add;
15369 /* Merge in the size for the next smallest -G
15370 value, since that will be implied by this new
15371 value. */
15372 max = 0;
15373 for (look = 1; look < c; look++)
15375 if (tab[look].gt_entry.gt_g_value < val
15376 && (max == 0
15377 || (tab[look].gt_entry.gt_g_value
15378 > tab[max].gt_entry.gt_g_value)))
15379 max = look;
15381 if (max != 0)
15382 tab[c].gt_entry.gt_bytes +=
15383 tab[max].gt_entry.gt_bytes;
15385 ++c;
15388 last = int_gptab.gt_entry.gt_bytes;
15391 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15392 elf_link_input_bfd ignores this section. */
15393 input_section->flags &= ~SEC_HAS_CONTENTS;
15396 /* The table must be sorted by -G value. */
15397 if (c > 2)
15398 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15400 /* Swap out the table. */
15401 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15402 ext_tab = bfd_alloc (abfd, amt);
15403 if (ext_tab == NULL)
15405 free (tab);
15406 return false;
15409 for (j = 0; j < c; j++)
15410 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15411 free (tab);
15413 o->size = c * sizeof (Elf32_External_gptab);
15414 o->contents = (bfd_byte *) ext_tab;
15416 /* Skip this section later on (I don't think this currently
15417 matters, but someday it might). */
15418 o->map_head.link_order = NULL;
15422 /* Invoke the regular ELF backend linker to do all the work. */
15423 if (!bfd_elf_final_link (abfd, info))
15424 return false;
15426 /* Now write out the computed sections. */
15428 if (abiflags_sec != NULL)
15430 Elf_External_ABIFlags_v0 ext;
15431 Elf_Internal_ABIFlags_v0 *abiflags;
15433 abiflags = &mips_elf_tdata (abfd)->abiflags;
15435 /* Set up the abiflags if no valid input sections were found. */
15436 if (!mips_elf_tdata (abfd)->abiflags_valid)
15438 infer_mips_abiflags (abfd, abiflags);
15439 mips_elf_tdata (abfd)->abiflags_valid = true;
15441 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15442 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15443 return false;
15446 if (reginfo_sec != NULL)
15448 Elf32_External_RegInfo ext;
15450 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15451 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15452 return false;
15455 if (mdebug_sec != NULL)
15457 BFD_ASSERT (abfd->output_has_begun);
15458 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15459 swap, info,
15460 mdebug_sec->filepos))
15461 return false;
15463 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15466 if (gptab_data_sec != NULL)
15468 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15469 gptab_data_sec->contents,
15470 0, gptab_data_sec->size))
15471 return false;
15474 if (gptab_bss_sec != NULL)
15476 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15477 gptab_bss_sec->contents,
15478 0, gptab_bss_sec->size))
15479 return false;
15482 if (SGI_COMPAT (abfd))
15484 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15485 if (rtproc_sec != NULL)
15487 if (! bfd_set_section_contents (abfd, rtproc_sec,
15488 rtproc_sec->contents,
15489 0, rtproc_sec->size))
15490 return false;
15494 return true;
15497 /* Merge object file header flags from IBFD into OBFD. Raise an error
15498 if there are conflicting settings. */
15500 static bool
15501 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15503 bfd *obfd = info->output_bfd;
15504 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15505 flagword old_flags;
15506 flagword new_flags;
15507 bool ok;
15509 new_flags = elf_elfheader (ibfd)->e_flags;
15510 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15511 old_flags = elf_elfheader (obfd)->e_flags;
15513 /* Check flag compatibility. */
15515 new_flags &= ~EF_MIPS_NOREORDER;
15516 old_flags &= ~EF_MIPS_NOREORDER;
15518 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15519 doesn't seem to matter. */
15520 new_flags &= ~EF_MIPS_XGOT;
15521 old_flags &= ~EF_MIPS_XGOT;
15523 /* MIPSpro generates ucode info in n64 objects. Again, we should
15524 just be able to ignore this. */
15525 new_flags &= ~EF_MIPS_UCODE;
15526 old_flags &= ~EF_MIPS_UCODE;
15528 /* DSOs should only be linked with CPIC code. */
15529 if ((ibfd->flags & DYNAMIC) != 0)
15530 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15532 if (new_flags == old_flags)
15533 return true;
15535 ok = true;
15537 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15538 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15540 _bfd_error_handler
15541 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15542 ibfd);
15543 ok = true;
15546 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15547 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15548 if (! (new_flags & EF_MIPS_PIC))
15549 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15551 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15552 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15554 /* Compare the ISAs. */
15555 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15557 _bfd_error_handler
15558 (_("%pB: linking 32-bit code with 64-bit code"),
15559 ibfd);
15560 ok = false;
15562 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15564 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15565 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15567 /* Copy the architecture info from IBFD to OBFD. Also copy
15568 the 32-bit flag (if set) so that we continue to recognise
15569 OBFD as a 32-bit binary. */
15570 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15571 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15572 elf_elfheader (obfd)->e_flags
15573 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15575 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15576 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15578 /* Copy across the ABI flags if OBFD doesn't use them
15579 and if that was what caused us to treat IBFD as 32-bit. */
15580 if ((old_flags & EF_MIPS_ABI) == 0
15581 && mips_32bit_flags_p (new_flags)
15582 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15583 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15585 else
15587 /* The ISAs aren't compatible. */
15588 _bfd_error_handler
15589 /* xgettext:c-format */
15590 (_("%pB: linking %s module with previous %s modules"),
15591 ibfd,
15592 bfd_printable_name (ibfd),
15593 bfd_printable_name (obfd));
15594 ok = false;
15598 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15599 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15601 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15602 does set EI_CLASS differently from any 32-bit ABI. */
15603 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15604 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15605 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15607 /* Only error if both are set (to different values). */
15608 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15609 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15610 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15612 _bfd_error_handler
15613 /* xgettext:c-format */
15614 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15615 ibfd,
15616 elf_mips_abi_name (ibfd),
15617 elf_mips_abi_name (obfd));
15618 ok = false;
15620 new_flags &= ~EF_MIPS_ABI;
15621 old_flags &= ~EF_MIPS_ABI;
15624 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15625 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15626 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15628 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15629 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15630 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15631 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15632 int micro_mis = old_m16 && new_micro;
15633 int m16_mis = old_micro && new_m16;
15635 if (m16_mis || micro_mis)
15637 _bfd_error_handler
15638 /* xgettext:c-format */
15639 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15640 ibfd,
15641 m16_mis ? "MIPS16" : "microMIPS",
15642 m16_mis ? "microMIPS" : "MIPS16");
15643 ok = false;
15646 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15648 new_flags &= ~ EF_MIPS_ARCH_ASE;
15649 old_flags &= ~ EF_MIPS_ARCH_ASE;
15652 /* Compare NaN encodings. */
15653 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15655 /* xgettext:c-format */
15656 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15657 ibfd,
15658 (new_flags & EF_MIPS_NAN2008
15659 ? "-mnan=2008" : "-mnan=legacy"),
15660 (old_flags & EF_MIPS_NAN2008
15661 ? "-mnan=2008" : "-mnan=legacy"));
15662 ok = false;
15663 new_flags &= ~EF_MIPS_NAN2008;
15664 old_flags &= ~EF_MIPS_NAN2008;
15667 /* Compare FP64 state. */
15668 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15670 /* xgettext:c-format */
15671 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15672 ibfd,
15673 (new_flags & EF_MIPS_FP64
15674 ? "-mfp64" : "-mfp32"),
15675 (old_flags & EF_MIPS_FP64
15676 ? "-mfp64" : "-mfp32"));
15677 ok = false;
15678 new_flags &= ~EF_MIPS_FP64;
15679 old_flags &= ~EF_MIPS_FP64;
15682 /* Warn about any other mismatches */
15683 if (new_flags != old_flags)
15685 /* xgettext:c-format */
15686 _bfd_error_handler
15687 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15688 "(%#x)"),
15689 ibfd, new_flags, old_flags);
15690 ok = false;
15693 return ok;
15696 /* Merge object attributes from IBFD into OBFD. Raise an error if
15697 there are conflicting attributes. */
15698 static bool
15699 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15701 bfd *obfd = info->output_bfd;
15702 obj_attribute *in_attr;
15703 obj_attribute *out_attr;
15704 bfd *abi_fp_bfd;
15705 bfd *abi_msa_bfd;
15707 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15708 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15709 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15710 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15712 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15713 if (!abi_msa_bfd
15714 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15715 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15717 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15719 /* This is the first object. Copy the attributes. */
15720 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15722 /* Use the Tag_null value to indicate the attributes have been
15723 initialized. */
15724 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15726 return true;
15729 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15730 non-conflicting ones. */
15731 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15732 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15734 int out_fp, in_fp;
15736 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15737 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15738 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15739 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15740 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15741 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15742 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15743 || in_fp == Val_GNU_MIPS_ABI_FP_64
15744 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15746 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15747 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15749 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15750 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15751 || out_fp == Val_GNU_MIPS_ABI_FP_64
15752 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15753 /* Keep the current setting. */;
15754 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15755 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15757 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15758 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15760 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15761 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15762 /* Keep the current setting. */;
15763 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15765 const char *out_string, *in_string;
15767 out_string = _bfd_mips_fp_abi_string (out_fp);
15768 in_string = _bfd_mips_fp_abi_string (in_fp);
15769 /* First warn about cases involving unrecognised ABIs. */
15770 if (!out_string && !in_string)
15771 /* xgettext:c-format */
15772 _bfd_error_handler
15773 (_("warning: %pB uses unknown floating point ABI %d "
15774 "(set by %pB), %pB uses unknown floating point ABI %d"),
15775 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15776 else if (!out_string)
15777 _bfd_error_handler
15778 /* xgettext:c-format */
15779 (_("warning: %pB uses unknown floating point ABI %d "
15780 "(set by %pB), %pB uses %s"),
15781 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15782 else if (!in_string)
15783 _bfd_error_handler
15784 /* xgettext:c-format */
15785 (_("warning: %pB uses %s (set by %pB), "
15786 "%pB uses unknown floating point ABI %d"),
15787 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15788 else
15790 /* If one of the bfds is soft-float, the other must be
15791 hard-float. The exact choice of hard-float ABI isn't
15792 really relevant to the error message. */
15793 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15794 out_string = "-mhard-float";
15795 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15796 in_string = "-mhard-float";
15797 _bfd_error_handler
15798 /* xgettext:c-format */
15799 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15800 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15805 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15806 non-conflicting ones. */
15807 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15809 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15810 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15811 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15812 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15813 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15815 case Val_GNU_MIPS_ABI_MSA_128:
15816 _bfd_error_handler
15817 /* xgettext:c-format */
15818 (_("warning: %pB uses %s (set by %pB), "
15819 "%pB uses unknown MSA ABI %d"),
15820 obfd, "-mmsa", abi_msa_bfd,
15821 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15822 break;
15824 default:
15825 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15827 case Val_GNU_MIPS_ABI_MSA_128:
15828 _bfd_error_handler
15829 /* xgettext:c-format */
15830 (_("warning: %pB uses unknown MSA ABI %d "
15831 "(set by %pB), %pB uses %s"),
15832 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15833 abi_msa_bfd, ibfd, "-mmsa");
15834 break;
15836 default:
15837 _bfd_error_handler
15838 /* xgettext:c-format */
15839 (_("warning: %pB uses unknown MSA ABI %d "
15840 "(set by %pB), %pB uses unknown MSA ABI %d"),
15841 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15842 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15843 break;
15848 /* Merge Tag_compatibility attributes and any common GNU ones. */
15849 return _bfd_elf_merge_object_attributes (ibfd, info);
15852 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15853 there are conflicting settings. */
15855 static bool
15856 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15858 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15859 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15860 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15862 /* Update the output abiflags fp_abi using the computed fp_abi. */
15863 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15865 #define max(a, b) ((a) > (b) ? (a) : (b))
15866 /* Merge abiflags. */
15867 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15868 in_tdata->abiflags.isa_level);
15869 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15870 in_tdata->abiflags.isa_rev);
15871 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15872 in_tdata->abiflags.gpr_size);
15873 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15874 in_tdata->abiflags.cpr1_size);
15875 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15876 in_tdata->abiflags.cpr2_size);
15877 #undef max
15878 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15879 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15881 return true;
15884 /* Merge backend specific data from an object file to the output
15885 object file when linking. */
15887 bool
15888 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15890 bfd *obfd = info->output_bfd;
15891 struct mips_elf_obj_tdata *out_tdata;
15892 struct mips_elf_obj_tdata *in_tdata;
15893 bool null_input_bfd = true;
15894 asection *sec;
15895 bool ok;
15897 /* Check if we have the same endianness. */
15898 if (! _bfd_generic_verify_endian_match (ibfd, info))
15900 _bfd_error_handler
15901 (_("%pB: endianness incompatible with that of the selected emulation"),
15902 ibfd);
15903 return false;
15906 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15907 return true;
15909 in_tdata = mips_elf_tdata (ibfd);
15910 out_tdata = mips_elf_tdata (obfd);
15912 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15914 _bfd_error_handler
15915 (_("%pB: ABI is incompatible with that of the selected emulation"),
15916 ibfd);
15917 return false;
15920 /* Check to see if the input BFD actually contains any sections. If not,
15921 then it has no attributes, and its flags may not have been initialized
15922 either, but it cannot actually cause any incompatibility. */
15923 /* FIXME: This excludes any input shared library from consideration. */
15924 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15926 /* Ignore synthetic sections and empty .text, .data and .bss sections
15927 which are automatically generated by gas. Also ignore fake
15928 (s)common sections, since merely defining a common symbol does
15929 not affect compatibility. */
15930 if ((sec->flags & SEC_IS_COMMON) == 0
15931 && strcmp (sec->name, ".reginfo")
15932 && strcmp (sec->name, ".mdebug")
15933 && (sec->size != 0
15934 || (strcmp (sec->name, ".text")
15935 && strcmp (sec->name, ".data")
15936 && strcmp (sec->name, ".bss"))))
15938 null_input_bfd = false;
15939 break;
15942 if (null_input_bfd)
15943 return true;
15945 /* Populate abiflags using existing information. */
15946 if (in_tdata->abiflags_valid)
15948 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15949 Elf_Internal_ABIFlags_v0 in_abiflags;
15950 Elf_Internal_ABIFlags_v0 abiflags;
15952 /* Set up the FP ABI attribute from the abiflags if it is not already
15953 set. */
15954 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15955 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15957 infer_mips_abiflags (ibfd, &abiflags);
15958 in_abiflags = in_tdata->abiflags;
15960 /* It is not possible to infer the correct ISA revision
15961 for R3 or R5 so drop down to R2 for the checks. */
15962 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15963 in_abiflags.isa_rev = 2;
15965 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15966 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15967 _bfd_error_handler
15968 (_("%pB: warning: inconsistent ISA between e_flags and "
15969 ".MIPS.abiflags"), ibfd);
15970 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15971 && in_abiflags.fp_abi != abiflags.fp_abi)
15972 _bfd_error_handler
15973 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15974 ".MIPS.abiflags"), ibfd);
15975 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15976 _bfd_error_handler
15977 (_("%pB: warning: inconsistent ASEs between e_flags and "
15978 ".MIPS.abiflags"), ibfd);
15979 /* The isa_ext is allowed to be an extension of what can be inferred
15980 from e_flags. */
15981 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15982 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15983 _bfd_error_handler
15984 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15985 ".MIPS.abiflags"), ibfd);
15986 if (in_abiflags.flags2 != 0)
15987 _bfd_error_handler
15988 (_("%pB: warning: unexpected flag in the flags2 field of "
15989 ".MIPS.abiflags (0x%lx)"), ibfd,
15990 in_abiflags.flags2);
15992 else
15994 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15995 in_tdata->abiflags_valid = true;
15998 if (!out_tdata->abiflags_valid)
16000 /* Copy input abiflags if output abiflags are not already valid. */
16001 out_tdata->abiflags = in_tdata->abiflags;
16002 out_tdata->abiflags_valid = true;
16005 if (! elf_flags_init (obfd))
16007 elf_flags_init (obfd) = true;
16008 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16009 elf_elfheader (obfd)->e_ident[EI_CLASS]
16010 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16012 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16013 && (bfd_get_arch_info (obfd)->the_default
16014 || mips_mach_extends_p (bfd_get_mach (obfd),
16015 bfd_get_mach (ibfd))))
16017 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16018 bfd_get_mach (ibfd)))
16019 return false;
16021 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
16022 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16025 ok = true;
16027 else
16028 ok = mips_elf_merge_obj_e_flags (ibfd, info);
16030 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16032 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16034 if (!ok)
16036 bfd_set_error (bfd_error_bad_value);
16037 return false;
16040 return true;
16043 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
16045 bool
16046 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16048 BFD_ASSERT (!elf_flags_init (abfd)
16049 || elf_elfheader (abfd)->e_flags == flags);
16051 elf_elfheader (abfd)->e_flags = flags;
16052 elf_flags_init (abfd) = true;
16053 return true;
16056 char *
16057 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16059 switch (dtag)
16061 default: return "";
16062 case DT_MIPS_RLD_VERSION:
16063 return "MIPS_RLD_VERSION";
16064 case DT_MIPS_TIME_STAMP:
16065 return "MIPS_TIME_STAMP";
16066 case DT_MIPS_ICHECKSUM:
16067 return "MIPS_ICHECKSUM";
16068 case DT_MIPS_IVERSION:
16069 return "MIPS_IVERSION";
16070 case DT_MIPS_FLAGS:
16071 return "MIPS_FLAGS";
16072 case DT_MIPS_BASE_ADDRESS:
16073 return "MIPS_BASE_ADDRESS";
16074 case DT_MIPS_MSYM:
16075 return "MIPS_MSYM";
16076 case DT_MIPS_CONFLICT:
16077 return "MIPS_CONFLICT";
16078 case DT_MIPS_LIBLIST:
16079 return "MIPS_LIBLIST";
16080 case DT_MIPS_LOCAL_GOTNO:
16081 return "MIPS_LOCAL_GOTNO";
16082 case DT_MIPS_CONFLICTNO:
16083 return "MIPS_CONFLICTNO";
16084 case DT_MIPS_LIBLISTNO:
16085 return "MIPS_LIBLISTNO";
16086 case DT_MIPS_SYMTABNO:
16087 return "MIPS_SYMTABNO";
16088 case DT_MIPS_UNREFEXTNO:
16089 return "MIPS_UNREFEXTNO";
16090 case DT_MIPS_GOTSYM:
16091 return "MIPS_GOTSYM";
16092 case DT_MIPS_HIPAGENO:
16093 return "MIPS_HIPAGENO";
16094 case DT_MIPS_RLD_MAP:
16095 return "MIPS_RLD_MAP";
16096 case DT_MIPS_RLD_MAP_REL:
16097 return "MIPS_RLD_MAP_REL";
16098 case DT_MIPS_DELTA_CLASS:
16099 return "MIPS_DELTA_CLASS";
16100 case DT_MIPS_DELTA_CLASS_NO:
16101 return "MIPS_DELTA_CLASS_NO";
16102 case DT_MIPS_DELTA_INSTANCE:
16103 return "MIPS_DELTA_INSTANCE";
16104 case DT_MIPS_DELTA_INSTANCE_NO:
16105 return "MIPS_DELTA_INSTANCE_NO";
16106 case DT_MIPS_DELTA_RELOC:
16107 return "MIPS_DELTA_RELOC";
16108 case DT_MIPS_DELTA_RELOC_NO:
16109 return "MIPS_DELTA_RELOC_NO";
16110 case DT_MIPS_DELTA_SYM:
16111 return "MIPS_DELTA_SYM";
16112 case DT_MIPS_DELTA_SYM_NO:
16113 return "MIPS_DELTA_SYM_NO";
16114 case DT_MIPS_DELTA_CLASSSYM:
16115 return "MIPS_DELTA_CLASSSYM";
16116 case DT_MIPS_DELTA_CLASSSYM_NO:
16117 return "MIPS_DELTA_CLASSSYM_NO";
16118 case DT_MIPS_CXX_FLAGS:
16119 return "MIPS_CXX_FLAGS";
16120 case DT_MIPS_PIXIE_INIT:
16121 return "MIPS_PIXIE_INIT";
16122 case DT_MIPS_SYMBOL_LIB:
16123 return "MIPS_SYMBOL_LIB";
16124 case DT_MIPS_LOCALPAGE_GOTIDX:
16125 return "MIPS_LOCALPAGE_GOTIDX";
16126 case DT_MIPS_LOCAL_GOTIDX:
16127 return "MIPS_LOCAL_GOTIDX";
16128 case DT_MIPS_HIDDEN_GOTIDX:
16129 return "MIPS_HIDDEN_GOTIDX";
16130 case DT_MIPS_PROTECTED_GOTIDX:
16131 return "MIPS_PROTECTED_GOT_IDX";
16132 case DT_MIPS_OPTIONS:
16133 return "MIPS_OPTIONS";
16134 case DT_MIPS_INTERFACE:
16135 return "MIPS_INTERFACE";
16136 case DT_MIPS_DYNSTR_ALIGN:
16137 return "DT_MIPS_DYNSTR_ALIGN";
16138 case DT_MIPS_INTERFACE_SIZE:
16139 return "DT_MIPS_INTERFACE_SIZE";
16140 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16141 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16142 case DT_MIPS_PERF_SUFFIX:
16143 return "DT_MIPS_PERF_SUFFIX";
16144 case DT_MIPS_COMPACT_SIZE:
16145 return "DT_MIPS_COMPACT_SIZE";
16146 case DT_MIPS_GP_VALUE:
16147 return "DT_MIPS_GP_VALUE";
16148 case DT_MIPS_AUX_DYNAMIC:
16149 return "DT_MIPS_AUX_DYNAMIC";
16150 case DT_MIPS_PLTGOT:
16151 return "DT_MIPS_PLTGOT";
16152 case DT_MIPS_RWPLT:
16153 return "DT_MIPS_RWPLT";
16154 case DT_MIPS_XHASH:
16155 return "DT_MIPS_XHASH";
16159 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16160 not known. */
16162 const char *
16163 _bfd_mips_fp_abi_string (int fp)
16165 switch (fp)
16167 /* These strings aren't translated because they're simply
16168 option lists. */
16169 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16170 return "-mdouble-float";
16172 case Val_GNU_MIPS_ABI_FP_SINGLE:
16173 return "-msingle-float";
16175 case Val_GNU_MIPS_ABI_FP_SOFT:
16176 return "-msoft-float";
16178 case Val_GNU_MIPS_ABI_FP_OLD_64:
16179 return _("-mips32r2 -mfp64 (12 callee-saved)");
16181 case Val_GNU_MIPS_ABI_FP_XX:
16182 return "-mfpxx";
16184 case Val_GNU_MIPS_ABI_FP_64:
16185 return "-mgp32 -mfp64";
16187 case Val_GNU_MIPS_ABI_FP_64A:
16188 return "-mgp32 -mfp64 -mno-odd-spreg";
16190 default:
16191 return 0;
16195 static void
16196 print_mips_ases (FILE *file, unsigned int mask)
16198 if (mask & AFL_ASE_DSP)
16199 fputs ("\n\tDSP ASE", file);
16200 if (mask & AFL_ASE_DSPR2)
16201 fputs ("\n\tDSP R2 ASE", file);
16202 if (mask & AFL_ASE_DSPR3)
16203 fputs ("\n\tDSP R3 ASE", file);
16204 if (mask & AFL_ASE_EVA)
16205 fputs ("\n\tEnhanced VA Scheme", file);
16206 if (mask & AFL_ASE_MCU)
16207 fputs ("\n\tMCU (MicroController) ASE", file);
16208 if (mask & AFL_ASE_MDMX)
16209 fputs ("\n\tMDMX ASE", file);
16210 if (mask & AFL_ASE_MIPS3D)
16211 fputs ("\n\tMIPS-3D ASE", file);
16212 if (mask & AFL_ASE_MT)
16213 fputs ("\n\tMT ASE", file);
16214 if (mask & AFL_ASE_SMARTMIPS)
16215 fputs ("\n\tSmartMIPS ASE", file);
16216 if (mask & AFL_ASE_VIRT)
16217 fputs ("\n\tVZ ASE", file);
16218 if (mask & AFL_ASE_MSA)
16219 fputs ("\n\tMSA ASE", file);
16220 if (mask & AFL_ASE_MIPS16)
16221 fputs ("\n\tMIPS16 ASE", file);
16222 if (mask & AFL_ASE_MICROMIPS)
16223 fputs ("\n\tMICROMIPS ASE", file);
16224 if (mask & AFL_ASE_XPA)
16225 fputs ("\n\tXPA ASE", file);
16226 if (mask & AFL_ASE_MIPS16E2)
16227 fputs ("\n\tMIPS16e2 ASE", file);
16228 if (mask & AFL_ASE_CRC)
16229 fputs ("\n\tCRC ASE", file);
16230 if (mask & AFL_ASE_GINV)
16231 fputs ("\n\tGINV ASE", file);
16232 if (mask & AFL_ASE_LOONGSON_MMI)
16233 fputs ("\n\tLoongson MMI ASE", file);
16234 if (mask & AFL_ASE_LOONGSON_CAM)
16235 fputs ("\n\tLoongson CAM ASE", file);
16236 if (mask & AFL_ASE_LOONGSON_EXT)
16237 fputs ("\n\tLoongson EXT ASE", file);
16238 if (mask & AFL_ASE_LOONGSON_EXT2)
16239 fputs ("\n\tLoongson EXT2 ASE", file);
16240 if (mask == 0)
16241 fprintf (file, "\n\t%s", _("None"));
16242 else if ((mask & ~AFL_ASE_MASK) != 0)
16243 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16246 static void
16247 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16249 switch (isa_ext)
16251 case 0:
16252 fputs (_("None"), file);
16253 break;
16254 case AFL_EXT_XLR:
16255 fputs ("RMI XLR", file);
16256 break;
16257 case AFL_EXT_OCTEON3:
16258 fputs ("Cavium Networks Octeon3", file);
16259 break;
16260 case AFL_EXT_OCTEON2:
16261 fputs ("Cavium Networks Octeon2", file);
16262 break;
16263 case AFL_EXT_OCTEONP:
16264 fputs ("Cavium Networks OcteonP", file);
16265 break;
16266 case AFL_EXT_OCTEON:
16267 fputs ("Cavium Networks Octeon", file);
16268 break;
16269 case AFL_EXT_5900:
16270 fputs ("Toshiba R5900", file);
16271 break;
16272 case AFL_EXT_4650:
16273 fputs ("MIPS R4650", file);
16274 break;
16275 case AFL_EXT_4010:
16276 fputs ("LSI R4010", file);
16277 break;
16278 case AFL_EXT_4100:
16279 fputs ("NEC VR4100", file);
16280 break;
16281 case AFL_EXT_3900:
16282 fputs ("Toshiba R3900", file);
16283 break;
16284 case AFL_EXT_10000:
16285 fputs ("MIPS R10000", file);
16286 break;
16287 case AFL_EXT_SB1:
16288 fputs ("Broadcom SB-1", file);
16289 break;
16290 case AFL_EXT_4111:
16291 fputs ("NEC VR4111/VR4181", file);
16292 break;
16293 case AFL_EXT_4120:
16294 fputs ("NEC VR4120", file);
16295 break;
16296 case AFL_EXT_5400:
16297 fputs ("NEC VR5400", file);
16298 break;
16299 case AFL_EXT_5500:
16300 fputs ("NEC VR5500", file);
16301 break;
16302 case AFL_EXT_LOONGSON_2E:
16303 fputs ("ST Microelectronics Loongson 2E", file);
16304 break;
16305 case AFL_EXT_LOONGSON_2F:
16306 fputs ("ST Microelectronics Loongson 2F", file);
16307 break;
16308 case AFL_EXT_INTERAPTIV_MR2:
16309 fputs ("Imagination interAptiv MR2", file);
16310 break;
16311 default:
16312 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16313 break;
16317 static void
16318 print_mips_fp_abi_value (FILE *file, int val)
16320 switch (val)
16322 case Val_GNU_MIPS_ABI_FP_ANY:
16323 fprintf (file, _("Hard or soft float\n"));
16324 break;
16325 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16326 fprintf (file, _("Hard float (double precision)\n"));
16327 break;
16328 case Val_GNU_MIPS_ABI_FP_SINGLE:
16329 fprintf (file, _("Hard float (single precision)\n"));
16330 break;
16331 case Val_GNU_MIPS_ABI_FP_SOFT:
16332 fprintf (file, _("Soft float\n"));
16333 break;
16334 case Val_GNU_MIPS_ABI_FP_OLD_64:
16335 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16336 break;
16337 case Val_GNU_MIPS_ABI_FP_XX:
16338 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16339 break;
16340 case Val_GNU_MIPS_ABI_FP_64:
16341 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16342 break;
16343 case Val_GNU_MIPS_ABI_FP_64A:
16344 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16345 break;
16346 default:
16347 fprintf (file, "??? (%d)\n", val);
16348 break;
16352 static int
16353 get_mips_reg_size (int reg_size)
16355 return (reg_size == AFL_REG_NONE) ? 0
16356 : (reg_size == AFL_REG_32) ? 32
16357 : (reg_size == AFL_REG_64) ? 64
16358 : (reg_size == AFL_REG_128) ? 128
16359 : -1;
16362 bool
16363 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16365 FILE *file = ptr;
16367 BFD_ASSERT (abfd != NULL && ptr != NULL);
16369 /* Print normal ELF private data. */
16370 _bfd_elf_print_private_bfd_data (abfd, ptr);
16372 /* xgettext:c-format */
16373 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16375 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16376 fprintf (file, _(" [abi=O32]"));
16377 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16378 fprintf (file, _(" [abi=O64]"));
16379 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16380 fprintf (file, _(" [abi=EABI32]"));
16381 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16382 fprintf (file, _(" [abi=EABI64]"));
16383 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16384 fprintf (file, _(" [abi unknown]"));
16385 else if (ABI_N32_P (abfd))
16386 fprintf (file, _(" [abi=N32]"));
16387 else if (ABI_64_P (abfd))
16388 fprintf (file, _(" [abi=64]"));
16389 else
16390 fprintf (file, _(" [no abi set]"));
16392 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16393 fprintf (file, " [mips1]");
16394 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16395 fprintf (file, " [mips2]");
16396 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16397 fprintf (file, " [mips3]");
16398 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16399 fprintf (file, " [mips4]");
16400 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16401 fprintf (file, " [mips5]");
16402 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16403 fprintf (file, " [mips32]");
16404 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16405 fprintf (file, " [mips64]");
16406 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16407 fprintf (file, " [mips32r2]");
16408 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16409 fprintf (file, " [mips64r2]");
16410 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16411 fprintf (file, " [mips32r6]");
16412 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16413 fprintf (file, " [mips64r6]");
16414 else
16415 fprintf (file, _(" [unknown ISA]"));
16417 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16418 fprintf (file, " [mdmx]");
16420 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16421 fprintf (file, " [mips16]");
16423 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16424 fprintf (file, " [micromips]");
16426 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16427 fprintf (file, " [nan2008]");
16429 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16430 fprintf (file, " [old fp64]");
16432 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16433 fprintf (file, " [32bitmode]");
16434 else
16435 fprintf (file, _(" [not 32bitmode]"));
16437 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16438 fprintf (file, " [noreorder]");
16440 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16441 fprintf (file, " [PIC]");
16443 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16444 fprintf (file, " [CPIC]");
16446 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16447 fprintf (file, " [XGOT]");
16449 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16450 fprintf (file, " [UCODE]");
16452 fputc ('\n', file);
16454 if (mips_elf_tdata (abfd)->abiflags_valid)
16456 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16457 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16458 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16459 if (abiflags->isa_rev > 1)
16460 fprintf (file, "r%d", abiflags->isa_rev);
16461 fprintf (file, "\nGPR size: %d",
16462 get_mips_reg_size (abiflags->gpr_size));
16463 fprintf (file, "\nCPR1 size: %d",
16464 get_mips_reg_size (abiflags->cpr1_size));
16465 fprintf (file, "\nCPR2 size: %d",
16466 get_mips_reg_size (abiflags->cpr2_size));
16467 fputs ("\nFP ABI: ", file);
16468 print_mips_fp_abi_value (file, abiflags->fp_abi);
16469 fputs ("ISA Extension: ", file);
16470 print_mips_isa_ext (file, abiflags->isa_ext);
16471 fputs ("\nASEs:", file);
16472 print_mips_ases (file, abiflags->ases);
16473 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16474 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16475 fputc ('\n', file);
16478 return true;
16481 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16483 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16484 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16485 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16486 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16487 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16488 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16489 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
16490 { NULL, 0, 0, 0, 0 }
16493 /* Merge non visibility st_other attributes. Ensure that the
16494 STO_OPTIONAL flag is copied into h->other, even if this is not a
16495 definiton of the symbol. */
16496 void
16497 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16498 unsigned int st_other,
16499 bool definition,
16500 bool dynamic ATTRIBUTE_UNUSED)
16502 if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16504 unsigned char other;
16506 other = (definition ? st_other : h->other);
16507 other &= ~ELF_ST_VISIBILITY (-1);
16508 h->other = other | ELF_ST_VISIBILITY (h->other);
16511 if (!definition
16512 && ELF_MIPS_IS_OPTIONAL (st_other))
16513 h->other |= STO_OPTIONAL;
16516 /* Decide whether an undefined symbol is special and can be ignored.
16517 This is the case for OPTIONAL symbols on IRIX. */
16518 bool
16519 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16521 return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16524 bool
16525 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16527 return (sym->st_shndx == SHN_COMMON
16528 || sym->st_shndx == SHN_MIPS_ACOMMON
16529 || sym->st_shndx == SHN_MIPS_SCOMMON);
16532 /* Return address for Ith PLT stub in section PLT, for relocation REL
16533 or (bfd_vma) -1 if it should not be included. */
16535 bfd_vma
16536 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16537 const arelent *rel ATTRIBUTE_UNUSED)
16539 return (plt->vma
16540 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16541 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16544 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16545 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16546 and .got.plt and also the slots may be of a different size each we walk
16547 the PLT manually fetching instructions and matching them against known
16548 patterns. To make things easier standard MIPS slots, if any, always come
16549 first. As we don't create proper ELF symbols we use the UDATA.I member
16550 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16551 with the ST_OTHER member of the ELF symbol. */
16553 long
16554 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16555 long symcount ATTRIBUTE_UNUSED,
16556 asymbol **syms ATTRIBUTE_UNUSED,
16557 long dynsymcount, asymbol **dynsyms,
16558 asymbol **ret)
16560 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16561 static const char microsuffix[] = "@micromipsplt";
16562 static const char m16suffix[] = "@mips16plt";
16563 static const char mipssuffix[] = "@plt";
16565 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16566 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16567 bool micromips_p = MICROMIPS_P (abfd);
16568 Elf_Internal_Shdr *hdr;
16569 bfd_byte *plt_data;
16570 bfd_vma plt_offset;
16571 unsigned int other;
16572 bfd_vma entry_size;
16573 bfd_vma plt0_size;
16574 asection *relplt;
16575 bfd_vma opcode;
16576 asection *plt;
16577 asymbol *send;
16578 size_t size;
16579 char *names;
16580 long counti;
16581 arelent *p;
16582 asymbol *s;
16583 char *nend;
16584 long count;
16585 long pi;
16586 long i;
16587 long n;
16589 *ret = NULL;
16591 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16592 return 0;
16594 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16595 if (relplt == NULL)
16596 return 0;
16598 hdr = &elf_section_data (relplt)->this_hdr;
16599 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16600 return 0;
16602 plt = bfd_get_section_by_name (abfd, ".plt");
16603 if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16604 return 0;
16606 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16607 if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16608 return -1;
16609 p = relplt->relocation;
16611 /* Calculating the exact amount of space required for symbols would
16612 require two passes over the PLT, so just pessimise assuming two
16613 PLT slots per relocation. */
16614 count = NUM_SHDR_ENTRIES (hdr);
16615 counti = count * bed->s->int_rels_per_ext_rel;
16616 size = 2 * count * sizeof (asymbol);
16617 size += count * (sizeof (mipssuffix) +
16618 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16619 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16620 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16622 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16623 size += sizeof (asymbol) + sizeof (pltname);
16625 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16626 return -1;
16628 if (plt->size < 16)
16629 return -1;
16631 s = *ret = bfd_malloc (size);
16632 if (s == NULL)
16633 return -1;
16634 send = s + 2 * count + 1;
16636 names = (char *) send;
16637 nend = (char *) s + size;
16638 n = 0;
16640 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16641 if (opcode == 0x3302fffe)
16643 if (!micromips_p)
16644 return -1;
16645 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16646 other = STO_MICROMIPS;
16648 else if (opcode == 0x0398c1d0)
16650 if (!micromips_p)
16651 return -1;
16652 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16653 other = STO_MICROMIPS;
16655 else
16657 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16658 other = 0;
16661 s->the_bfd = abfd;
16662 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16663 s->section = plt;
16664 s->value = 0;
16665 s->name = names;
16666 s->udata.i = other;
16667 memcpy (names, pltname, sizeof (pltname));
16668 names += sizeof (pltname);
16669 ++s, ++n;
16671 pi = 0;
16672 for (plt_offset = plt0_size;
16673 plt_offset + 8 <= plt->size && s < send;
16674 plt_offset += entry_size)
16676 bfd_vma gotplt_addr;
16677 const char *suffix;
16678 bfd_vma gotplt_hi;
16679 bfd_vma gotplt_lo;
16680 size_t suffixlen;
16682 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16684 /* Check if the second word matches the expected MIPS16 instruction. */
16685 if (opcode == 0x651aeb00)
16687 if (micromips_p)
16688 return -1;
16689 /* Truncated table??? */
16690 if (plt_offset + 16 > plt->size)
16691 break;
16692 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16693 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16694 suffixlen = sizeof (m16suffix);
16695 suffix = m16suffix;
16696 other = STO_MIPS16;
16698 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16699 else if (opcode == 0xff220000)
16701 if (!micromips_p)
16702 return -1;
16703 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16704 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16705 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16706 gotplt_lo <<= 2;
16707 gotplt_addr = gotplt_hi + gotplt_lo;
16708 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16709 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16710 suffixlen = sizeof (microsuffix);
16711 suffix = microsuffix;
16712 other = STO_MICROMIPS;
16714 /* Likewise the expected microMIPS instruction (insn32 mode). */
16715 else if ((opcode & 0xffff0000) == 0xff2f0000)
16717 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16718 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16719 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16720 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16721 gotplt_addr = gotplt_hi + gotplt_lo;
16722 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16723 suffixlen = sizeof (microsuffix);
16724 suffix = microsuffix;
16725 other = STO_MICROMIPS;
16727 /* Otherwise assume standard MIPS code. */
16728 else
16730 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16731 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16732 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16733 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16734 gotplt_addr = gotplt_hi + gotplt_lo;
16735 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16736 suffixlen = sizeof (mipssuffix);
16737 suffix = mipssuffix;
16738 other = 0;
16740 /* Truncated table??? */
16741 if (plt_offset + entry_size > plt->size)
16742 break;
16744 for (i = 0;
16745 i < count && p[pi].address != gotplt_addr;
16746 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16748 if (i < count)
16750 size_t namelen;
16751 size_t len;
16753 *s = **p[pi].sym_ptr_ptr;
16754 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16755 we are defining a symbol, ensure one of them is set. */
16756 if ((s->flags & BSF_LOCAL) == 0)
16757 s->flags |= BSF_GLOBAL;
16758 s->flags |= BSF_SYNTHETIC;
16759 s->section = plt;
16760 s->value = plt_offset;
16761 s->name = names;
16762 s->udata.i = other;
16764 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16765 namelen = len + suffixlen;
16766 if (names + namelen > nend)
16767 break;
16769 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16770 names += len;
16771 memcpy (names, suffix, suffixlen);
16772 names += suffixlen;
16774 ++s, ++n;
16775 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16779 free (plt_data);
16781 return n;
16784 /* Return the ABI flags associated with ABFD if available. */
16786 Elf_Internal_ABIFlags_v0 *
16787 bfd_mips_elf_get_abiflags (bfd *abfd)
16789 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16791 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16794 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16795 field. Taken from `libc-abis.h' generated at GNU libc build time.
16796 Using a MIPS_ prefix as other libc targets use different values. */
16797 enum
16799 MIPS_LIBC_ABI_DEFAULT = 0,
16800 MIPS_LIBC_ABI_MIPS_PLT,
16801 MIPS_LIBC_ABI_UNIQUE,
16802 MIPS_LIBC_ABI_MIPS_O32_FP64,
16803 MIPS_LIBC_ABI_ABSOLUTE,
16804 MIPS_LIBC_ABI_XHASH,
16805 MIPS_LIBC_ABI_MAX
16808 bool
16809 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16811 struct mips_elf_link_hash_table *htab = NULL;
16812 Elf_Internal_Ehdr *i_ehdrp;
16814 if (!_bfd_elf_init_file_header (abfd, link_info))
16815 return false;
16817 i_ehdrp = elf_elfheader (abfd);
16818 if (link_info)
16820 htab = mips_elf_hash_table (link_info);
16821 BFD_ASSERT (htab != NULL);
16824 if (htab != NULL
16825 && htab->use_plts_and_copy_relocs
16826 && htab->root.target_os != is_vxworks)
16827 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16829 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16830 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16831 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16833 /* Mark that we need support for absolute symbols in the dynamic loader. */
16834 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16835 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16837 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16838 if it is the only hash section that will be created. */
16839 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16840 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16841 return true;
16845 _bfd_mips_elf_compact_eh_encoding
16846 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16848 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16851 /* Return the opcode for can't unwind. */
16854 _bfd_mips_elf_cant_unwind_opcode
16855 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16857 return COMPACT_EH_CANT_UNWIND_OPCODE;
16860 /* Record a position XLAT_LOC in the xlat translation table, associated with
16861 the hash entry H. The entry in the translation table will later be
16862 populated with the real symbol dynindx. */
16864 void
16865 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16866 bfd_vma xlat_loc)
16868 struct mips_elf_link_hash_entry *hmips;
16870 hmips = (struct mips_elf_link_hash_entry *) h;
16871 hmips->mipsxhash_loc = xlat_loc;