Add translations for various sub-directories
[binutils-gdb.git] / bfd / elfxx-mips.c
blobe428ae278ba6a0d82117ea210a44fac50fd62a75
1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2025 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 asection *elf_data_section;
597 asection *elf_text_section;
599 struct mips_hi16 *mips_hi16_list;
602 /* Get MIPS ELF private object data from BFD's tdata. */
604 #define mips_elf_tdata(bfd) \
605 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
607 #define TLS_RELOC_P(r_type) \
608 (r_type == R_MIPS_TLS_DTPMOD32 \
609 || r_type == R_MIPS_TLS_DTPMOD64 \
610 || r_type == R_MIPS_TLS_DTPREL32 \
611 || r_type == R_MIPS_TLS_DTPREL64 \
612 || r_type == R_MIPS_TLS_GD \
613 || r_type == R_MIPS_TLS_LDM \
614 || r_type == R_MIPS_TLS_DTPREL_HI16 \
615 || r_type == R_MIPS_TLS_DTPREL_LO16 \
616 || r_type == R_MIPS_TLS_GOTTPREL \
617 || r_type == R_MIPS_TLS_TPREL32 \
618 || r_type == R_MIPS_TLS_TPREL64 \
619 || r_type == R_MIPS_TLS_TPREL_HI16 \
620 || r_type == R_MIPS_TLS_TPREL_LO16 \
621 || r_type == R_MIPS16_TLS_GD \
622 || r_type == R_MIPS16_TLS_LDM \
623 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
624 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
625 || r_type == R_MIPS16_TLS_GOTTPREL \
626 || r_type == R_MIPS16_TLS_TPREL_HI16 \
627 || r_type == R_MIPS16_TLS_TPREL_LO16 \
628 || r_type == R_MICROMIPS_TLS_GD \
629 || r_type == R_MICROMIPS_TLS_LDM \
630 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
631 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
632 || r_type == R_MICROMIPS_TLS_GOTTPREL \
633 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
634 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
636 /* Structure used to pass information to mips_elf_output_extsym. */
638 struct extsym_info
640 bfd *abfd;
641 struct bfd_link_info *info;
642 struct ecoff_debug_info *debug;
643 const struct ecoff_debug_swap *swap;
644 bool failed;
647 /* The names of the runtime procedure table symbols used on IRIX5. */
649 static const char * const mips_elf_dynsym_rtproc_names[] =
651 "_procedure_table",
652 "_procedure_string_table",
653 "_procedure_table_size",
654 NULL
657 /* These structures are used to generate the .compact_rel section on
658 IRIX5. */
660 typedef struct
662 unsigned long id1; /* Always one? */
663 unsigned long num; /* Number of compact relocation entries. */
664 unsigned long id2; /* Always two? */
665 unsigned long offset; /* The file offset of the first relocation. */
666 unsigned long reserved0; /* Zero? */
667 unsigned long reserved1; /* Zero? */
668 } Elf32_compact_rel;
670 typedef struct
672 bfd_byte id1[4];
673 bfd_byte num[4];
674 bfd_byte id2[4];
675 bfd_byte offset[4];
676 bfd_byte reserved0[4];
677 bfd_byte reserved1[4];
678 } Elf32_External_compact_rel;
680 typedef struct
682 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
683 unsigned int rtype : 4; /* Relocation types. See below. */
684 unsigned int dist2to : 8;
685 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
686 unsigned long konst; /* KONST field. See below. */
687 unsigned long vaddr; /* VADDR to be relocated. */
688 } Elf32_crinfo;
690 typedef struct
692 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
693 unsigned int rtype : 4; /* Relocation types. See below. */
694 unsigned int dist2to : 8;
695 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
696 unsigned long konst; /* KONST field. See below. */
697 } Elf32_crinfo2;
699 typedef struct
701 bfd_byte info[4];
702 bfd_byte konst[4];
703 bfd_byte vaddr[4];
704 } Elf32_External_crinfo;
706 typedef struct
708 bfd_byte info[4];
709 bfd_byte konst[4];
710 } Elf32_External_crinfo2;
712 /* These are the constants used to swap the bitfields in a crinfo. */
714 #define CRINFO_CTYPE (0x1U)
715 #define CRINFO_CTYPE_SH (31)
716 #define CRINFO_RTYPE (0xfU)
717 #define CRINFO_RTYPE_SH (27)
718 #define CRINFO_DIST2TO (0xffU)
719 #define CRINFO_DIST2TO_SH (19)
720 #define CRINFO_RELVADDR (0x7ffffU)
721 #define CRINFO_RELVADDR_SH (0)
723 /* A compact relocation info has long (3 words) or short (2 words)
724 formats. A short format doesn't have VADDR field and relvaddr
725 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
726 #define CRF_MIPS_LONG 1
727 #define CRF_MIPS_SHORT 0
729 /* There are 4 types of compact relocation at least. The value KONST
730 has different meaning for each type:
732 (type) (konst)
733 CT_MIPS_REL32 Address in data
734 CT_MIPS_WORD Address in word (XXX)
735 CT_MIPS_GPHI_LO GP - vaddr
736 CT_MIPS_JMPAD Address to jump
739 #define CRT_MIPS_REL32 0xa
740 #define CRT_MIPS_WORD 0xb
741 #define CRT_MIPS_GPHI_LO 0xc
742 #define CRT_MIPS_JMPAD 0xd
744 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
745 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
746 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
747 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
749 /* The structure of the runtime procedure descriptor created by the
750 loader for use by the static exception system. */
752 typedef struct runtime_pdr {
753 bfd_vma adr; /* Memory address of start of procedure. */
754 long regmask; /* Save register mask. */
755 long regoffset; /* Save register offset. */
756 long fregmask; /* Save floating point register mask. */
757 long fregoffset; /* Save floating point register offset. */
758 long frameoffset; /* Frame size. */
759 short framereg; /* Frame pointer register. */
760 short pcreg; /* Offset or reg of return pc. */
761 long irpss; /* Index into the runtime string table. */
762 long reserved;
763 struct exception_info *exception_info;/* Pointer to exception array. */
764 } RPDR, *pRPDR;
765 #define cbRPDR sizeof (RPDR)
766 #define rpdNil ((pRPDR) 0)
768 static struct mips_got_entry *mips_elf_create_local_got_entry
769 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
770 struct mips_elf_link_hash_entry *, int);
771 static bool mips_elf_sort_hash_table_f
772 (struct mips_elf_link_hash_entry *, void *);
773 static bfd_vma mips_elf_high
774 (bfd_vma);
775 static bool mips_elf_create_dynamic_relocation
776 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
777 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
778 bfd_vma *, asection *);
779 static bfd_vma mips_elf_adjust_gp
780 (bfd *, struct mips_got_info *, bfd *);
782 /* This will be used when we sort the dynamic relocation records. */
783 static bfd *reldyn_sorting_bfd;
785 /* True if ABFD is for CPUs with load interlocking that include
786 non-MIPS1 CPUs and R3900. */
787 #define LOAD_INTERLOCKS_P(abfd) \
788 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
789 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
791 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
792 This should be safe for all architectures. We enable this predicate
793 for RM9000 for now. */
794 #define JAL_TO_BAL_P(abfd) \
795 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
797 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
798 This should be safe for all architectures. We enable this predicate for
799 all CPUs. */
800 #define JALR_TO_BAL_P(abfd) 1
802 /* True if ABFD is for CPUs that are faster if JR is converted to B.
803 This should be safe for all architectures. We enable this predicate for
804 all CPUs. */
805 #define JR_TO_B_P(abfd) 1
807 /* True if ABFD is a PIC object. */
808 #define PIC_OBJECT_P(abfd) \
809 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
811 /* Nonzero if ABFD is using the O32 ABI. */
812 #define ABI_O32_P(abfd) \
813 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
815 /* Nonzero if ABFD is using the N32 ABI. */
816 #define ABI_N32_P(abfd) \
817 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
819 /* Nonzero if ABFD is using the N64 ABI. */
820 #define ABI_64_P(abfd) \
821 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
823 /* Nonzero if ABFD is using NewABI conventions. */
824 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
826 /* Nonzero if ABFD has microMIPS code. */
827 #define MICROMIPS_P(abfd) \
828 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
830 /* Nonzero if ABFD is MIPS R6. */
831 #define MIPSR6_P(abfd) \
832 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
833 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
835 /* The IRIX compatibility level we are striving for. */
836 #define IRIX_COMPAT(abfd) \
837 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
839 /* Whether we are trying to be compatible with IRIX at all. */
840 #define SGI_COMPAT(abfd) \
841 (IRIX_COMPAT (abfd) != ict_none)
843 /* The name of the options section. */
844 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
845 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
847 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
848 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
849 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
850 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
852 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
853 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
854 (strcmp (NAME, ".MIPS.abiflags") == 0)
856 /* Whether the section is readonly. */
857 #define MIPS_ELF_READONLY_SECTION(sec) \
858 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
859 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
861 /* The name of the stub section. */
862 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
864 /* The size of an external REL relocation. */
865 #define MIPS_ELF_REL_SIZE(abfd) \
866 (get_elf_backend_data (abfd)->s->sizeof_rel)
868 /* The size of an external RELA relocation. */
869 #define MIPS_ELF_RELA_SIZE(abfd) \
870 (get_elf_backend_data (abfd)->s->sizeof_rela)
872 /* The size of an external dynamic table entry. */
873 #define MIPS_ELF_DYN_SIZE(abfd) \
874 (get_elf_backend_data (abfd)->s->sizeof_dyn)
876 /* The size of a GOT entry. */
877 #define MIPS_ELF_GOT_SIZE(abfd) \
878 (get_elf_backend_data (abfd)->s->arch_size / 8)
880 /* The size of the .rld_map section. */
881 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
882 (get_elf_backend_data (abfd)->s->arch_size / 8)
884 /* The size of a symbol-table entry. */
885 #define MIPS_ELF_SYM_SIZE(abfd) \
886 (get_elf_backend_data (abfd)->s->sizeof_sym)
888 /* The default alignment for sections, as a power of two. */
889 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
890 (get_elf_backend_data (abfd)->s->log_file_align)
892 /* Get word-sized data. */
893 #define MIPS_ELF_GET_WORD(abfd, ptr) \
894 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
896 /* Put out word-sized data. */
897 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
898 (ABI_64_P (abfd) \
899 ? bfd_put_64 (abfd, val, ptr) \
900 : bfd_put_32 (abfd, val, ptr))
902 /* The opcode for word-sized loads (LW or LD). */
903 #define MIPS_ELF_LOAD_WORD(abfd) \
904 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
906 /* Add a dynamic symbol table-entry. */
907 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
908 _bfd_elf_add_dynamic_entry (info, tag, val)
910 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
911 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
913 /* The name of the dynamic relocation section. */
914 #define MIPS_ELF_REL_DYN_NAME(INFO) \
915 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
916 ? ".rela.dyn" : ".rel.dyn")
918 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
919 from smaller values. Start with zero, widen, *then* decrement. */
920 #define MINUS_ONE (((bfd_vma)0) - 1)
921 #define MINUS_TWO (((bfd_vma)0) - 2)
923 /* The value to write into got[1] for SVR4 targets, to identify it is
924 a GNU object. The dynamic linker can then use got[1] to store the
925 module pointer. */
926 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
927 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
929 /* The offset of $gp from the beginning of the .got section. */
930 #define ELF_MIPS_GP_OFFSET(INFO) \
931 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
932 ? 0x0 : 0x7ff0)
934 /* The maximum size of the GOT for it to be addressable using 16-bit
935 offsets from $gp. */
936 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
938 /* Instructions which appear in a stub. */
939 #define STUB_LW(abfd) \
940 ((ABI_64_P (abfd) \
941 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
942 : 0x8f998010)) /* lw t9,0x8010(gp) */
943 #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
944 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
945 #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
946 #define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
947 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
948 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
949 #define STUB_LI16S(abfd, VAL) \
950 ((ABI_64_P (abfd) \
951 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
952 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
954 /* Likewise for the microMIPS ASE. */
955 #define STUB_LW_MICROMIPS(abfd) \
956 (ABI_64_P (abfd) \
957 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
958 : 0xff3c8010) /* lw t9,0x8010(gp) */
959 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
960 #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
961 #define STUB_LUI_MICROMIPS(VAL) \
962 (0x41b80000 + (VAL)) /* lui t8,VAL */
963 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
964 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
965 #define STUB_ORI_MICROMIPS(VAL) \
966 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
967 #define STUB_LI16U_MICROMIPS(VAL) \
968 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
969 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
970 (ABI_64_P (abfd) \
971 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
972 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
974 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
975 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
976 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
977 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
978 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
979 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
981 /* The name of the dynamic interpreter. This is put in the .interp
982 section. */
984 #define ELF_DYNAMIC_INTERPRETER(abfd) \
985 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
986 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
987 : "/usr/lib/libc.so.1")
989 #ifdef BFD64
990 #define MNAME(bfd,pre,pos) \
991 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
992 #define ELF_R_SYM(bfd, i) \
993 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
994 #define ELF_R_TYPE(bfd, i) \
995 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
996 #define ELF_R_INFO(bfd, s, t) \
997 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
998 #else
999 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1000 #define ELF_R_SYM(bfd, i) \
1001 (ELF32_R_SYM (i))
1002 #define ELF_R_TYPE(bfd, i) \
1003 (ELF32_R_TYPE (i))
1004 #define ELF_R_INFO(bfd, s, t) \
1005 (ELF32_R_INFO (s, t))
1006 #endif
1008 /* The mips16 compiler uses a couple of special sections to handle
1009 floating point arguments.
1011 Section names that look like .mips16.fn.FNNAME contain stubs that
1012 copy floating point arguments from the fp regs to the gp regs and
1013 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1014 call should be redirected to the stub instead. If no 32 bit
1015 function calls FNNAME, the stub should be discarded. We need to
1016 consider any reference to the function, not just a call, because
1017 if the address of the function is taken we will need the stub,
1018 since the address might be passed to a 32 bit function.
1020 Section names that look like .mips16.call.FNNAME contain stubs
1021 that copy floating point arguments from the gp regs to the fp
1022 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1023 then any 16 bit function that calls FNNAME should be redirected
1024 to the stub instead. If FNNAME is not a 32 bit function, the
1025 stub should be discarded.
1027 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1028 which call FNNAME and then copy the return value from the fp regs
1029 to the gp regs. These stubs store the return value in $18 while
1030 calling FNNAME; any function which might call one of these stubs
1031 must arrange to save $18 around the call. (This case is not
1032 needed for 32 bit functions that call 16 bit functions, because
1033 16 bit functions always return floating point values in both
1034 $f0/$f1 and $2/$3.)
1036 Note that in all cases FNNAME might be defined statically.
1037 Therefore, FNNAME is not used literally. Instead, the relocation
1038 information will indicate which symbol the section is for.
1040 We record any stubs that we find in the symbol table. */
1042 #define FN_STUB ".mips16.fn."
1043 #define CALL_STUB ".mips16.call."
1044 #define CALL_FP_STUB ".mips16.call.fp."
1046 #define FN_STUB_P(name) startswith (name, FN_STUB)
1047 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
1048 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1050 /* The format of the first PLT entry in an O32 executable. */
1051 static const bfd_vma mips_o32_exec_plt0_entry[] =
1053 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1054 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1055 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1056 0x031cc023, /* subu $24, $24, $28 */
1057 0x03e07825, /* or t7, ra, zero */
1058 0x0018c082, /* srl $24, $24, 2 */
1059 0x0320f809, /* jalr $25 */
1060 0x2718fffe /* subu $24, $24, 2 */
1063 /* The format of the first PLT entry in an O32 executable using compact
1064 jumps. */
1065 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1067 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1068 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1069 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1070 0x031cc023, /* subu $24, $24, $28 */
1071 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1072 0x0018c082, /* srl $24, $24, 2 */
1073 0x2718fffe, /* subu $24, $24, 2 */
1074 0xf8190000 /* jalrc $25 */
1077 /* The format of the first PLT entry in an N32 executable. Different
1078 because gp ($28) is not available; we use t2 ($14) instead. */
1079 static const bfd_vma mips_n32_exec_plt0_entry[] =
1081 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1082 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1083 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1084 0x030ec023, /* subu $24, $24, $14 */
1085 0x03e07825, /* or t7, ra, zero */
1086 0x0018c082, /* srl $24, $24, 2 */
1087 0x0320f809, /* jalr $25 */
1088 0x2718fffe /* subu $24, $24, 2 */
1091 /* The format of the first PLT entry in an N32 executable using compact
1092 jumps. Different because gp ($28) is not available; we use t2 ($14)
1093 instead. */
1094 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1096 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1097 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1098 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1099 0x030ec023, /* subu $24, $24, $14 */
1100 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1101 0x0018c082, /* srl $24, $24, 2 */
1102 0x2718fffe, /* subu $24, $24, 2 */
1103 0xf8190000 /* jalrc $25 */
1106 /* The format of the first PLT entry in an N64 executable. Different
1107 from N32 because of the increased size of GOT entries. */
1108 static const bfd_vma mips_n64_exec_plt0_entry[] =
1110 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1111 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1112 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1113 0x030ec023, /* subu $24, $24, $14 */
1114 0x03e07825, /* or t7, ra, zero */
1115 0x0018c0c2, /* srl $24, $24, 3 */
1116 0x0320f809, /* jalr $25 */
1117 0x2718fffe /* subu $24, $24, 2 */
1120 /* The format of the first PLT entry in an N64 executable using compact
1121 jumps. Different from N32 because of the increased size of GOT
1122 entries. */
1123 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1125 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1126 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1127 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1128 0x030ec023, /* subu $24, $24, $14 */
1129 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1130 0x0018c0c2, /* srl $24, $24, 3 */
1131 0x2718fffe, /* subu $24, $24, 2 */
1132 0xf8190000 /* jalrc $25 */
1136 /* The format of the microMIPS first PLT entry in an O32 executable.
1137 We rely on v0 ($2) rather than t8 ($24) to contain the address
1138 of the GOTPLT entry handled, so this stub may only be used when
1139 all the subsequent PLT entries are microMIPS code too.
1141 The trailing NOP is for alignment and correct disassembly only. */
1142 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1144 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1145 0xff23, 0x0000, /* lw $25, 0($3) */
1146 0x0535, /* subu $2, $2, $3 */
1147 0x2525, /* srl $2, $2, 2 */
1148 0x3302, 0xfffe, /* subu $24, $2, 2 */
1149 0x0dff, /* move $15, $31 */
1150 0x45f9, /* jalrs $25 */
1151 0x0f83, /* move $28, $3 */
1152 0x0c00 /* nop */
1155 /* The format of the microMIPS first PLT entry in an O32 executable
1156 in the insn32 mode. */
1157 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1159 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1160 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1161 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1162 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1163 0x001f, 0x7a90, /* or $15, $31, zero */
1164 0x0318, 0x1040, /* srl $24, $24, 2 */
1165 0x03f9, 0x0f3c, /* jalr $25 */
1166 0x3318, 0xfffe /* subu $24, $24, 2 */
1169 /* The format of subsequent standard PLT entries. */
1170 static const bfd_vma mips_exec_plt_entry[] =
1172 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1173 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1174 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1175 0x03200008 /* jr $25 */
1178 static const bfd_vma mipsr6_exec_plt_entry[] =
1180 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1181 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1182 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1183 0x03200009 /* jr $25 */
1186 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1188 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1189 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1190 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1191 0xd8190000 /* jic $25, 0 */
1194 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1195 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1196 directly addressable. */
1197 static const bfd_vma mips16_o32_exec_plt_entry[] =
1199 0xb203, /* lw $2, 12($pc) */
1200 0x9a60, /* lw $3, 0($2) */
1201 0x651a, /* move $24, $2 */
1202 0xeb00, /* jr $3 */
1203 0x653b, /* move $25, $3 */
1204 0x6500, /* nop */
1205 0x0000, 0x0000 /* .word (.got.plt entry) */
1208 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1209 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1210 static const bfd_vma micromips_o32_exec_plt_entry[] =
1212 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1213 0xff22, 0x0000, /* lw $25, 0($2) */
1214 0x4599, /* jr $25 */
1215 0x0f02 /* move $24, $2 */
1218 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1219 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1221 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1222 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1223 0x0019, 0x0f3c, /* jr $25 */
1224 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1227 /* The format of the first PLT entry in a VxWorks executable. */
1228 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1230 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1231 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1232 0x8f390008, /* lw t9, 8(t9) */
1233 0x00000000, /* nop */
1234 0x03200008, /* jr t9 */
1235 0x00000000 /* nop */
1238 /* The format of subsequent PLT entries. */
1239 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1241 0x10000000, /* b .PLT_resolver */
1242 0x24180000, /* li t8, <pltindex> */
1243 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1244 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1245 0x8f390000, /* lw t9, 0(t9) */
1246 0x00000000, /* nop */
1247 0x03200008, /* jr t9 */
1248 0x00000000 /* nop */
1251 /* The format of the first PLT entry in a VxWorks shared object. */
1252 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1254 0x8f990008, /* lw t9, 8(gp) */
1255 0x00000000, /* nop */
1256 0x03200008, /* jr t9 */
1257 0x00000000, /* nop */
1258 0x00000000, /* nop */
1259 0x00000000 /* nop */
1262 /* The format of subsequent PLT entries. */
1263 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1265 0x10000000, /* b .PLT_resolver */
1266 0x24180000 /* li t8, <pltindex> */
1269 /* microMIPS 32-bit opcode helper installer. */
1271 static void
1272 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1274 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1275 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1278 /* microMIPS 32-bit opcode helper retriever. */
1280 static bfd_vma
1281 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1283 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286 /* Look up an entry in a MIPS ELF linker hash table. */
1288 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1289 ((struct mips_elf_link_hash_entry *) \
1290 elf_link_hash_lookup (&(table)->root, (string), (create), \
1291 (copy), (follow)))
1293 /* Traverse a MIPS ELF linker hash table. */
1295 #define mips_elf_link_hash_traverse(table, func, info) \
1296 (elf_link_hash_traverse \
1297 (&(table)->root, \
1298 (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
1299 (info)))
1301 /* Find the base offsets for thread-local storage in this object,
1302 for GD/LD and IE/LE respectively. */
1304 #define TP_OFFSET 0x7000
1305 #define DTP_OFFSET 0x8000
1307 static bfd_vma
1308 dtprel_base (struct bfd_link_info *info)
1310 /* If tls_sec is NULL, we should have signalled an error already. */
1311 if (elf_hash_table (info)->tls_sec == NULL)
1312 return 0;
1313 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316 static bfd_vma
1317 tprel_base (struct bfd_link_info *info)
1319 /* If tls_sec is NULL, we should have signalled an error already. */
1320 if (elf_hash_table (info)->tls_sec == NULL)
1321 return 0;
1322 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325 /* Create an entry in a MIPS ELF linker hash table. */
1327 static struct bfd_hash_entry *
1328 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1329 struct bfd_hash_table *table, const char *string)
1331 struct mips_elf_link_hash_entry *ret =
1332 (struct mips_elf_link_hash_entry *) entry;
1334 /* Allocate the structure if it has not already been allocated by a
1335 subclass. */
1336 if (ret == NULL)
1337 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1338 if (ret == NULL)
1339 return (struct bfd_hash_entry *) ret;
1341 /* Call the allocation method of the superclass. */
1342 ret = ((struct mips_elf_link_hash_entry *)
1343 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1344 table, string));
1345 if (ret != NULL)
1347 /* Set local fields. */
1348 memset (&ret->esym, 0, sizeof (EXTR));
1349 /* We use -2 as a marker to indicate that the information has
1350 not been set. -1 means there is no associated ifd. */
1351 ret->esym.ifd = -2;
1352 ret->la25_stub = 0;
1353 ret->possibly_dynamic_relocs = 0;
1354 ret->fn_stub = NULL;
1355 ret->call_stub = NULL;
1356 ret->call_fp_stub = NULL;
1357 ret->mipsxhash_loc = 0;
1358 ret->global_got_area = GGA_NONE;
1359 ret->got_only_for_calls = true;
1360 ret->readonly_reloc = false;
1361 ret->has_static_relocs = false;
1362 ret->no_fn_stub = false;
1363 ret->need_fn_stub = false;
1364 ret->has_nonpic_branches = false;
1365 ret->needs_lazy_stub = false;
1366 ret->use_plt_entry = false;
1369 return (struct bfd_hash_entry *) ret;
1372 /* Allocate MIPS ELF private object data. */
1374 bool
1375 _bfd_mips_elf_mkobject (bfd *abfd)
1377 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata));
1380 /* MIPS ELF uses a special find_nearest_line routine in order the
1381 handle the ECOFF debugging information. */
1383 struct mips_elf_find_line
1385 struct ecoff_debug_info d;
1386 struct ecoff_find_line i;
1389 bool
1390 _bfd_mips_elf_free_cached_info (bfd *abfd)
1392 struct mips_elf_obj_tdata *tdata;
1394 if ((bfd_get_format (abfd) == bfd_object
1395 || bfd_get_format (abfd) == bfd_core)
1396 && (tdata = mips_elf_tdata (abfd)) != NULL)
1398 BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1399 while (tdata->mips_hi16_list != NULL)
1401 struct mips_hi16 *hi = tdata->mips_hi16_list;
1402 tdata->mips_hi16_list = hi->next;
1403 free (hi);
1405 if (tdata->find_line_info != NULL)
1406 _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1408 return _bfd_elf_free_cached_info (abfd);
1411 bool
1412 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1414 struct _mips_elf_section_data *sdata;
1416 sdata = bfd_zalloc (abfd, sizeof (*sdata));
1417 if (sdata == NULL)
1418 return false;
1419 sec->used_by_bfd = sdata;
1421 return _bfd_elf_new_section_hook (abfd, sec);
1424 /* Read ECOFF debugging information from a .mdebug section into a
1425 ecoff_debug_info structure. */
1427 bool
1428 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1429 struct ecoff_debug_info *debug)
1431 HDRR *symhdr;
1432 const struct ecoff_debug_swap *swap;
1433 char *ext_hdr;
1435 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1436 memset (debug, 0, sizeof (*debug));
1438 ext_hdr = bfd_malloc (swap->external_hdr_size);
1439 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1440 goto error_return;
1442 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1443 swap->external_hdr_size))
1444 goto error_return;
1446 symhdr = &debug->symbolic_header;
1447 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1448 free (ext_hdr);
1449 ext_hdr = NULL;
1451 /* The symbolic header contains absolute file offsets and sizes to
1452 read. */
1453 #define READ(ptr, offset, count, size) \
1454 do \
1456 size_t amt; \
1457 debug->ptr = NULL; \
1458 if (symhdr->count == 0) \
1459 break; \
1460 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1462 bfd_set_error (bfd_error_file_too_big); \
1463 goto error_return; \
1465 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
1466 goto error_return; \
1467 debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt); \
1468 if (debug->ptr == NULL) \
1469 goto error_return; \
1470 ((char *) debug->ptr)[amt] = 0; \
1471 } while (0)
1473 READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1474 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1475 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1476 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1477 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1478 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1479 READ (ss, cbSsOffset, issMax, sizeof (char));
1480 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1481 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1482 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1483 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1484 #undef READ
1486 return true;
1488 error_return:
1489 free (ext_hdr);
1490 _bfd_ecoff_free_ecoff_debug_info (debug);
1491 return false;
1494 /* Swap RPDR (runtime procedure table entry) for output. */
1496 static void
1497 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1499 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1500 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1501 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1502 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1503 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1504 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1506 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1507 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1509 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1512 /* Create a runtime procedure table from the .mdebug section. */
1514 static bool
1515 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1516 struct bfd_link_info *info, asection *s,
1517 struct ecoff_debug_info *debug)
1519 const struct ecoff_debug_swap *swap;
1520 HDRR *hdr = &debug->symbolic_header;
1521 RPDR *rpdr, *rp;
1522 struct rpdr_ext *erp;
1523 void *rtproc;
1524 struct pdr_ext *epdr;
1525 struct sym_ext *esym;
1526 char *ss, **sv;
1527 char *str;
1528 bfd_size_type size;
1529 bfd_size_type count;
1530 unsigned long sindex;
1531 unsigned long i;
1532 PDR pdr;
1533 SYMR sym;
1534 const char *no_name_func = _("static procedure (no name)");
1536 epdr = NULL;
1537 rpdr = NULL;
1538 esym = NULL;
1539 ss = NULL;
1540 sv = NULL;
1542 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1544 sindex = strlen (no_name_func) + 1;
1545 count = hdr->ipdMax;
1546 if (count > 0)
1548 size = swap->external_pdr_size;
1550 epdr = bfd_malloc (size * count);
1551 if (epdr == NULL)
1552 goto error_return;
1554 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1555 goto error_return;
1557 size = sizeof (RPDR);
1558 rp = rpdr = bfd_malloc (size * count);
1559 if (rpdr == NULL)
1560 goto error_return;
1562 size = sizeof (char *);
1563 sv = bfd_malloc (size * count);
1564 if (sv == NULL)
1565 goto error_return;
1567 count = hdr->isymMax;
1568 size = swap->external_sym_size;
1569 esym = bfd_malloc (size * count);
1570 if (esym == NULL)
1571 goto error_return;
1573 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1574 goto error_return;
1576 count = hdr->issMax;
1577 ss = bfd_malloc (count);
1578 if (ss == NULL)
1579 goto error_return;
1580 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1581 goto error_return;
1583 count = hdr->ipdMax;
1584 for (i = 0; i < (unsigned long) count; i++, rp++)
1586 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1587 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1588 rp->adr = sym.value;
1589 rp->regmask = pdr.regmask;
1590 rp->regoffset = pdr.regoffset;
1591 rp->fregmask = pdr.fregmask;
1592 rp->fregoffset = pdr.fregoffset;
1593 rp->frameoffset = pdr.frameoffset;
1594 rp->framereg = pdr.framereg;
1595 rp->pcreg = pdr.pcreg;
1596 rp->irpss = sindex;
1597 sv[i] = ss + sym.iss;
1598 sindex += strlen (sv[i]) + 1;
1602 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1603 size = BFD_ALIGN (size, 16);
1604 rtproc = bfd_alloc (abfd, size);
1605 if (rtproc == NULL)
1607 mips_elf_hash_table (info)->procedure_count = 0;
1608 goto error_return;
1611 mips_elf_hash_table (info)->procedure_count = count + 2;
1613 erp = rtproc;
1614 memset (erp, 0, sizeof (struct rpdr_ext));
1615 erp++;
1616 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1617 strcpy (str, no_name_func);
1618 str += strlen (no_name_func) + 1;
1619 for (i = 0; i < count; i++)
1621 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1622 strcpy (str, sv[i]);
1623 str += strlen (sv[i]) + 1;
1625 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1627 /* Set the size and contents of .rtproc section. */
1628 s->size = size;
1629 s->contents = rtproc;
1631 /* Skip this section later on (I don't think this currently
1632 matters, but someday it might). */
1633 s->map_head.link_order = NULL;
1635 free (epdr);
1636 free (rpdr);
1637 free (esym);
1638 free (ss);
1639 free (sv);
1640 return true;
1642 error_return:
1643 free (epdr);
1644 free (rpdr);
1645 free (esym);
1646 free (ss);
1647 free (sv);
1648 return false;
1651 /* We're going to create a stub for H. Create a symbol for the stub's
1652 value and size, to help make the disassembly easier to read. */
1654 static bool
1655 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1656 struct mips_elf_link_hash_entry *h,
1657 const char *prefix, asection *s, bfd_vma value,
1658 bfd_vma size)
1660 bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1661 struct bfd_link_hash_entry *bh;
1662 struct elf_link_hash_entry *elfh;
1663 char *name;
1664 bool res;
1666 if (micromips_p)
1667 value |= 1;
1669 /* Create a new symbol. */
1670 name = concat (prefix, h->root.root.root.string, NULL);
1671 bh = NULL;
1672 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1673 BSF_LOCAL, s, value, NULL,
1674 true, false, &bh);
1675 free (name);
1676 if (! res)
1677 return false;
1679 /* Make it a local function. */
1680 elfh = (struct elf_link_hash_entry *) bh;
1681 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1682 elfh->size = size;
1683 elfh->forced_local = 1;
1684 if (micromips_p)
1685 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1686 return true;
1689 /* We're about to redefine H. Create a symbol to represent H's
1690 current value and size, to help make the disassembly easier
1691 to read. */
1693 static bool
1694 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1695 struct mips_elf_link_hash_entry *h,
1696 const char *prefix)
1698 struct bfd_link_hash_entry *bh;
1699 struct elf_link_hash_entry *elfh;
1700 char *name;
1701 asection *s;
1702 bfd_vma value;
1703 bool res;
1705 /* Read the symbol's value. */
1706 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1707 || h->root.root.type == bfd_link_hash_defweak);
1708 s = h->root.root.u.def.section;
1709 value = h->root.root.u.def.value;
1711 /* Create a new symbol. */
1712 name = concat (prefix, h->root.root.root.string, NULL);
1713 bh = NULL;
1714 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1715 BSF_LOCAL, s, value, NULL,
1716 true, false, &bh);
1717 free (name);
1718 if (! res)
1719 return false;
1721 /* Make it local and copy the other attributes from H. */
1722 elfh = (struct elf_link_hash_entry *) bh;
1723 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1724 elfh->other = h->root.other;
1725 elfh->size = h->root.size;
1726 elfh->forced_local = 1;
1727 return true;
1730 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1731 function rather than to a hard-float stub. */
1733 static bool
1734 section_allows_mips16_refs_p (asection *section)
1736 const char *name;
1738 name = bfd_section_name (section);
1739 return (FN_STUB_P (name)
1740 || CALL_STUB_P (name)
1741 || CALL_FP_STUB_P (name)
1742 || strcmp (name, ".pdr") == 0);
1745 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1746 stub section of some kind. Return the R_SYMNDX of the target
1747 function, or 0 if we can't decide which function that is. */
1749 static unsigned long
1750 mips16_stub_symndx (const struct elf_backend_data *bed,
1751 asection *sec ATTRIBUTE_UNUSED,
1752 const Elf_Internal_Rela *relocs,
1753 const Elf_Internal_Rela *relend)
1755 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1756 const Elf_Internal_Rela *rel;
1758 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1759 one in a compound relocation. */
1760 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1761 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1762 return ELF_R_SYM (sec->owner, rel->r_info);
1764 /* Otherwise trust the first relocation, whatever its kind. This is
1765 the traditional behavior. */
1766 if (relocs < relend)
1767 return ELF_R_SYM (sec->owner, relocs->r_info);
1769 return 0;
1772 /* Check the mips16 stubs for a particular symbol, and see if we can
1773 discard them. */
1775 static void
1776 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1777 struct mips_elf_link_hash_entry *h)
1779 /* Dynamic symbols must use the standard call interface, in case other
1780 objects try to call them. */
1781 if (h->fn_stub != NULL
1782 && h->root.dynindx != -1)
1784 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1785 h->need_fn_stub = true;
1788 if (h->fn_stub != NULL
1789 && ! h->need_fn_stub)
1791 /* We don't need the fn_stub; the only references to this symbol
1792 are 16 bit calls. Clobber the size to 0 to prevent it from
1793 being included in the link. */
1794 h->fn_stub->size = 0;
1795 h->fn_stub->flags &= ~SEC_RELOC;
1796 h->fn_stub->reloc_count = 0;
1797 h->fn_stub->flags |= SEC_EXCLUDE;
1798 h->fn_stub->output_section = bfd_abs_section_ptr;
1801 if (h->call_stub != NULL
1802 && ELF_ST_IS_MIPS16 (h->root.other))
1804 /* We don't need the call_stub; this is a 16 bit function, so
1805 calls from other 16 bit functions are OK. Clobber the size
1806 to 0 to prevent it from being included in the link. */
1807 h->call_stub->size = 0;
1808 h->call_stub->flags &= ~SEC_RELOC;
1809 h->call_stub->reloc_count = 0;
1810 h->call_stub->flags |= SEC_EXCLUDE;
1811 h->call_stub->output_section = bfd_abs_section_ptr;
1814 if (h->call_fp_stub != NULL
1815 && ELF_ST_IS_MIPS16 (h->root.other))
1817 /* We don't need the call_stub; this is a 16 bit function, so
1818 calls from other 16 bit functions are OK. Clobber the size
1819 to 0 to prevent it from being included in the link. */
1820 h->call_fp_stub->size = 0;
1821 h->call_fp_stub->flags &= ~SEC_RELOC;
1822 h->call_fp_stub->reloc_count = 0;
1823 h->call_fp_stub->flags |= SEC_EXCLUDE;
1824 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1828 /* Hashtable callbacks for mips_elf_la25_stubs. */
1830 static hashval_t
1831 mips_elf_la25_stub_hash (const void *entry_)
1833 const struct mips_elf_la25_stub *entry;
1835 entry = (struct mips_elf_la25_stub *) entry_;
1836 return entry->h->root.root.u.def.section->id
1837 + entry->h->root.root.u.def.value;
1840 static int
1841 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1843 const struct mips_elf_la25_stub *entry1, *entry2;
1845 entry1 = (struct mips_elf_la25_stub *) entry1_;
1846 entry2 = (struct mips_elf_la25_stub *) entry2_;
1847 return ((entry1->h->root.root.u.def.section
1848 == entry2->h->root.root.u.def.section)
1849 && (entry1->h->root.root.u.def.value
1850 == entry2->h->root.root.u.def.value));
1853 /* Called by the linker to set up the la25 stub-creation code. FN is
1854 the linker's implementation of add_stub_function. Return true on
1855 success. */
1857 bool
1858 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1859 asection *(*fn) (const char *, asection *,
1860 asection *))
1862 struct mips_elf_link_hash_table *htab;
1864 htab = mips_elf_hash_table (info);
1865 if (htab == NULL)
1866 return false;
1868 htab->add_stub_section = fn;
1869 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1870 mips_elf_la25_stub_eq, NULL);
1871 if (htab->la25_stubs == NULL)
1872 return false;
1874 return true;
1877 /* Return true if H is a locally-defined PIC function, in the sense
1878 that it or its fn_stub might need $25 to be valid on entry.
1879 Note that MIPS16 functions set up $gp using PC-relative instructions,
1880 so they themselves never need $25 to be valid. Only non-MIPS16
1881 entry points are of interest here. */
1883 static bool
1884 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1886 return ((h->root.root.type == bfd_link_hash_defined
1887 || h->root.root.type == bfd_link_hash_defweak)
1888 && h->root.def_regular
1889 && !bfd_is_abs_section (h->root.root.u.def.section)
1890 && !bfd_is_und_section (h->root.root.u.def.section)
1891 && (!ELF_ST_IS_MIPS16 (h->root.other)
1892 || (h->fn_stub && h->need_fn_stub))
1893 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1894 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1897 /* Set *SEC to the input section that contains the target of STUB.
1898 Return the offset of the target from the start of that section. */
1900 static bfd_vma
1901 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1902 asection **sec)
1904 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1906 BFD_ASSERT (stub->h->need_fn_stub);
1907 *sec = stub->h->fn_stub;
1908 return 0;
1910 else
1912 *sec = stub->h->root.root.u.def.section;
1913 return stub->h->root.root.u.def.value;
1917 /* STUB describes an la25 stub that we have decided to implement
1918 by inserting an LUI/ADDIU pair before the target function.
1919 Create the section and redirect the function symbol to it. */
1921 static bool
1922 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1923 struct bfd_link_info *info)
1925 struct mips_elf_link_hash_table *htab;
1926 char *name;
1927 asection *s, *input_section;
1928 unsigned int align;
1930 htab = mips_elf_hash_table (info);
1931 if (htab == NULL)
1932 return false;
1934 /* Create a unique name for the new section. */
1935 name = bfd_malloc (11 + sizeof (".text.stub."));
1936 if (name == NULL)
1937 return false;
1938 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1940 /* Create the section. */
1941 mips_elf_get_la25_target (stub, &input_section);
1942 s = htab->add_stub_section (name, input_section,
1943 input_section->output_section);
1944 if (s == NULL)
1945 return false;
1947 /* Make sure that any padding goes before the stub. */
1948 align = input_section->alignment_power;
1949 if (!bfd_set_section_alignment (s, align))
1950 return false;
1951 if (align > 3)
1952 s->size = (1 << align) - 8;
1954 /* Create a symbol for the stub. */
1955 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1956 stub->stub_section = s;
1957 stub->offset = s->size;
1959 /* Allocate room for it. */
1960 s->size += 8;
1961 return true;
1964 /* STUB describes an la25 stub that we have decided to implement
1965 with a separate trampoline. Allocate room for it and redirect
1966 the function symbol to it. */
1968 static bool
1969 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1970 struct bfd_link_info *info)
1972 struct mips_elf_link_hash_table *htab;
1973 asection *s;
1975 htab = mips_elf_hash_table (info);
1976 if (htab == NULL)
1977 return false;
1979 /* Create a trampoline section, if we haven't already. */
1980 s = htab->strampoline;
1981 if (s == NULL)
1983 asection *input_section = stub->h->root.root.u.def.section;
1984 s = htab->add_stub_section (".text", NULL,
1985 input_section->output_section);
1986 if (s == NULL || !bfd_set_section_alignment (s, 4))
1987 return false;
1988 htab->strampoline = s;
1991 /* Create a symbol for the stub. */
1992 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1993 stub->stub_section = s;
1994 stub->offset = s->size;
1996 /* Allocate room for it. */
1997 s->size += 16;
1998 return true;
2001 /* H describes a symbol that needs an la25 stub. Make sure that an
2002 appropriate stub exists and point H at it. */
2004 static bool
2005 mips_elf_add_la25_stub (struct bfd_link_info *info,
2006 struct mips_elf_link_hash_entry *h)
2008 struct mips_elf_link_hash_table *htab;
2009 struct mips_elf_la25_stub search, *stub;
2010 bool use_trampoline_p;
2011 asection *s;
2012 bfd_vma value;
2013 void **slot;
2015 /* Describe the stub we want. */
2016 search.stub_section = NULL;
2017 search.offset = 0;
2018 search.h = h;
2020 /* See if we've already created an equivalent stub. */
2021 htab = mips_elf_hash_table (info);
2022 if (htab == NULL)
2023 return false;
2025 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2026 if (slot == NULL)
2027 return false;
2029 stub = (struct mips_elf_la25_stub *) *slot;
2030 if (stub != NULL)
2032 /* We can reuse the existing stub. */
2033 h->la25_stub = stub;
2034 return true;
2037 /* Create a permanent copy of ENTRY and add it to the hash table. */
2038 stub = bfd_malloc (sizeof (search));
2039 if (stub == NULL)
2040 return false;
2041 *stub = search;
2042 *slot = stub;
2044 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2045 of the section and if we would need no more than 2 nops. */
2046 value = mips_elf_get_la25_target (stub, &s);
2047 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2048 value &= ~1;
2049 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2051 h->la25_stub = stub;
2052 return (use_trampoline_p
2053 ? mips_elf_add_la25_trampoline (stub, info)
2054 : mips_elf_add_la25_intro (stub, info));
2057 /* A mips_elf_link_hash_traverse callback that is called before sizing
2058 sections. DATA points to a mips_htab_traverse_info structure. */
2060 static bool
2061 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2063 struct mips_htab_traverse_info *hti;
2065 hti = (struct mips_htab_traverse_info *) data;
2066 if (!bfd_link_relocatable (hti->info))
2067 mips_elf_check_mips16_stubs (hti->info, h);
2069 if (mips_elf_local_pic_function_p (h))
2071 /* PR 12845: If H is in a section that has been garbage
2072 collected it will have its output section set to *ABS*. */
2073 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2074 return true;
2076 /* H is a function that might need $25 to be valid on entry.
2077 If we're creating a non-PIC relocatable object, mark H as
2078 being PIC. If we're creating a non-relocatable object with
2079 non-PIC branches and jumps to H, make sure that H has an la25
2080 stub. */
2081 if (bfd_link_relocatable (hti->info))
2083 if (!PIC_OBJECT_P (hti->output_bfd))
2084 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2086 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2088 hti->error = true;
2089 return false;
2092 return true;
2095 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2096 Most mips16 instructions are 16 bits, but these instructions
2097 are 32 bits.
2099 The format of these instructions is:
2101 +--------------+--------------------------------+
2102 | JALX | X| Imm 20:16 | Imm 25:21 |
2103 +--------------+--------------------------------+
2104 | Immediate 15:0 |
2105 +-----------------------------------------------+
2107 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2108 Note that the immediate value in the first word is swapped.
2110 When producing a relocatable object file, R_MIPS16_26 is
2111 handled mostly like R_MIPS_26. In particular, the addend is
2112 stored as a straight 26-bit value in a 32-bit instruction.
2113 (gas makes life simpler for itself by never adjusting a
2114 R_MIPS16_26 reloc to be against a section, so the addend is
2115 always zero). However, the 32 bit instruction is stored as 2
2116 16-bit values, rather than a single 32-bit value. In a
2117 big-endian file, the result is the same; in a little-endian
2118 file, the two 16-bit halves of the 32 bit value are swapped.
2119 This is so that a disassembler can recognize the jal
2120 instruction.
2122 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2123 instruction stored as two 16-bit values. The addend A is the
2124 contents of the targ26 field. The calculation is the same as
2125 R_MIPS_26. When storing the calculated value, reorder the
2126 immediate value as shown above, and don't forget to store the
2127 value as two 16-bit values.
2129 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2130 defined as
2132 big-endian:
2133 +--------+----------------------+
2134 | | |
2135 | | targ26-16 |
2136 |31 26|25 0|
2137 +--------+----------------------+
2139 little-endian:
2140 +----------+------+-------------+
2141 | | | |
2142 | sub1 | | sub2 |
2143 |0 9|10 15|16 31|
2144 +----------+--------------------+
2145 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2146 ((sub1 << 16) | sub2)).
2148 When producing a relocatable object file, the calculation is
2149 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2150 When producing a fully linked file, the calculation is
2151 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2152 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2154 The table below lists the other MIPS16 instruction relocations.
2155 Each one is calculated in the same way as the non-MIPS16 relocation
2156 given on the right, but using the extended MIPS16 layout of 16-bit
2157 immediate fields:
2159 R_MIPS16_GPREL R_MIPS_GPREL16
2160 R_MIPS16_GOT16 R_MIPS_GOT16
2161 R_MIPS16_CALL16 R_MIPS_CALL16
2162 R_MIPS16_HI16 R_MIPS_HI16
2163 R_MIPS16_LO16 R_MIPS_LO16
2165 A typical instruction will have a format like this:
2167 +--------------+--------------------------------+
2168 | EXTEND | Imm 10:5 | Imm 15:11 |
2169 +--------------+--------------------------------+
2170 | Major | rx | ry | Imm 4:0 |
2171 +--------------+--------------------------------+
2173 EXTEND is the five bit value 11110. Major is the instruction
2174 opcode.
2176 All we need to do here is shuffle the bits appropriately.
2177 As above, the two 16-bit halves must be swapped on a
2178 little-endian system.
2180 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2181 relocatable field is shifted by 1 rather than 2 and the same bit
2182 shuffling is done as with the relocations above. */
2184 static inline bool
2185 mips16_reloc_p (int r_type)
2187 switch (r_type)
2189 case R_MIPS16_26:
2190 case R_MIPS16_GPREL:
2191 case R_MIPS16_GOT16:
2192 case R_MIPS16_CALL16:
2193 case R_MIPS16_HI16:
2194 case R_MIPS16_LO16:
2195 case R_MIPS16_TLS_GD:
2196 case R_MIPS16_TLS_LDM:
2197 case R_MIPS16_TLS_DTPREL_HI16:
2198 case R_MIPS16_TLS_DTPREL_LO16:
2199 case R_MIPS16_TLS_GOTTPREL:
2200 case R_MIPS16_TLS_TPREL_HI16:
2201 case R_MIPS16_TLS_TPREL_LO16:
2202 case R_MIPS16_PC16_S1:
2203 return true;
2205 default:
2206 return false;
2210 /* Check if a microMIPS reloc. */
2212 static inline bool
2213 micromips_reloc_p (unsigned int r_type)
2215 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2218 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2219 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1,
2220 R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
2221 16-bit instructions. */
2223 static inline bool
2224 micromips_reloc_shuffle_p (unsigned int r_type)
2226 return (micromips_reloc_p (r_type)
2227 && r_type != R_MICROMIPS_PC7_S1
2228 && r_type != R_MICROMIPS_PC10_S1
2229 && r_type != R_MICROMIPS_GPREL7_S2);
2232 static inline bool
2233 got16_reloc_p (int r_type)
2235 return (r_type == R_MIPS_GOT16
2236 || r_type == R_MIPS16_GOT16
2237 || r_type == R_MICROMIPS_GOT16);
2240 static inline bool
2241 call16_reloc_p (int r_type)
2243 return (r_type == R_MIPS_CALL16
2244 || r_type == R_MIPS16_CALL16
2245 || r_type == R_MICROMIPS_CALL16);
2248 static inline bool
2249 got_disp_reloc_p (unsigned int r_type)
2251 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2254 static inline bool
2255 got_page_reloc_p (unsigned int r_type)
2257 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2260 static inline bool
2261 got_lo16_reloc_p (unsigned int r_type)
2263 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2266 static inline bool
2267 call_hi16_reloc_p (unsigned int r_type)
2269 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2272 static inline bool
2273 call_lo16_reloc_p (unsigned int r_type)
2275 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2278 static inline bool
2279 hi16_reloc_p (int r_type)
2281 return (r_type == R_MIPS_HI16
2282 || r_type == R_MIPS16_HI16
2283 || r_type == R_MICROMIPS_HI16
2284 || r_type == R_MIPS_PCHI16);
2287 static inline bool
2288 lo16_reloc_p (int r_type)
2290 return (r_type == R_MIPS_LO16
2291 || r_type == R_MIPS16_LO16
2292 || r_type == R_MICROMIPS_LO16
2293 || r_type == R_MIPS_PCLO16);
2296 static inline bool
2297 mips16_call_reloc_p (int r_type)
2299 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2302 static inline bool
2303 jal_reloc_p (int r_type)
2305 return (r_type == R_MIPS_26
2306 || r_type == R_MIPS16_26
2307 || r_type == R_MICROMIPS_26_S1);
2310 static inline bool
2311 b_reloc_p (int r_type)
2313 return (r_type == R_MIPS_PC26_S2
2314 || r_type == R_MIPS_PC21_S2
2315 || r_type == R_MIPS_PC16
2316 || r_type == R_MIPS_GNU_REL16_S2
2317 || r_type == R_MIPS16_PC16_S1
2318 || r_type == R_MICROMIPS_PC16_S1
2319 || r_type == R_MICROMIPS_PC10_S1
2320 || r_type == R_MICROMIPS_PC7_S1);
2323 static inline bool
2324 aligned_pcrel_reloc_p (int r_type)
2326 return (r_type == R_MIPS_PC18_S3
2327 || r_type == R_MIPS_PC19_S2);
2330 static inline bool
2331 branch_reloc_p (int r_type)
2333 return (r_type == R_MIPS_26
2334 || r_type == R_MIPS_PC26_S2
2335 || r_type == R_MIPS_PC21_S2
2336 || r_type == R_MIPS_PC16
2337 || r_type == R_MIPS_GNU_REL16_S2);
2340 static inline bool
2341 mips16_branch_reloc_p (int r_type)
2343 return (r_type == R_MIPS16_26
2344 || r_type == R_MIPS16_PC16_S1);
2347 static inline bool
2348 micromips_branch_reloc_p (int r_type)
2350 return (r_type == R_MICROMIPS_26_S1
2351 || r_type == R_MICROMIPS_PC16_S1
2352 || r_type == R_MICROMIPS_PC10_S1
2353 || r_type == R_MICROMIPS_PC7_S1);
2356 static inline bool
2357 tls_gd_reloc_p (unsigned int r_type)
2359 return (r_type == R_MIPS_TLS_GD
2360 || r_type == R_MIPS16_TLS_GD
2361 || r_type == R_MICROMIPS_TLS_GD);
2364 static inline bool
2365 tls_ldm_reloc_p (unsigned int r_type)
2367 return (r_type == R_MIPS_TLS_LDM
2368 || r_type == R_MIPS16_TLS_LDM
2369 || r_type == R_MICROMIPS_TLS_LDM);
2372 static inline bool
2373 tls_gottprel_reloc_p (unsigned int r_type)
2375 return (r_type == R_MIPS_TLS_GOTTPREL
2376 || r_type == R_MIPS16_TLS_GOTTPREL
2377 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2380 static inline bool
2381 needs_shuffle (int r_type)
2383 return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2386 void
2387 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2388 bool jal_shuffle, bfd_byte *data)
2390 bfd_vma first, second, val;
2392 if (!needs_shuffle (r_type))
2393 return;
2395 /* Pick up the first and second halfwords of the instruction. */
2396 first = bfd_get_16 (abfd, data);
2397 second = bfd_get_16 (abfd, data + 2);
2398 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2399 val = first << 16 | second;
2400 else if (r_type != R_MIPS16_26)
2401 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2402 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2403 else
2404 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2405 | ((first & 0x1f) << 21) | second);
2406 bfd_put_32 (abfd, val, data);
2409 void
2410 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2411 bool jal_shuffle, bfd_byte *data)
2413 bfd_vma first, second, val;
2415 if (!needs_shuffle (r_type))
2416 return;
2418 val = bfd_get_32 (abfd, data);
2419 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2421 second = val & 0xffff;
2422 first = val >> 16;
2424 else if (r_type != R_MIPS16_26)
2426 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2427 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2429 else
2431 second = val & 0xffff;
2432 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2433 | ((val >> 21) & 0x1f);
2435 bfd_put_16 (abfd, second, data + 2);
2436 bfd_put_16 (abfd, first, data);
2439 /* Perform reloc offset checking.
2440 We can only use bfd_reloc_offset_in_range, which takes into account
2441 the size of the field being relocated, when section contents will
2442 be accessed because mips object files may use relocations that seem
2443 to access beyond section limits.
2444 gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2445 R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2446 section. The R_MIPS_SUB applies to the addend for the next reloc
2447 rather than the section contents.
2449 CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2450 CHECK_INPLACE to only check partial_inplace relocs, and
2451 CHECK_SHUFFLE to only check relocs that shuffle/unshuffle. */
2453 bool
2454 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2455 arelent *reloc_entry, enum reloc_check check)
2457 if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2458 return true;
2459 if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2460 return true;
2461 return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2462 input_section, reloc_entry->address);
2465 bfd_reloc_status_type
2466 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2467 arelent *reloc_entry, asection *input_section,
2468 bool relocatable, void *data, bfd_vma gp)
2470 bfd_vma relocation;
2471 bfd_signed_vma val;
2472 bfd_reloc_status_type status;
2474 if (bfd_is_com_section (symbol->section))
2475 relocation = 0;
2476 else
2477 relocation = symbol->value;
2479 if (symbol->section->output_section != NULL)
2481 relocation += symbol->section->output_section->vma;
2482 relocation += symbol->section->output_offset;
2485 /* Set val to the offset into the section or symbol. */
2486 val = reloc_entry->addend;
2488 _bfd_mips_elf_sign_extend (val, 16);
2490 /* Adjust val for the final section location and GP value. If we
2491 are producing relocatable output, we don't want to do this for
2492 an external symbol. */
2493 if (! relocatable
2494 || (symbol->flags & BSF_SECTION_SYM) != 0)
2495 val += relocation - gp;
2497 if (reloc_entry->howto->partial_inplace)
2499 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2500 reloc_entry->address))
2501 return bfd_reloc_outofrange;
2503 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2504 (bfd_byte *) data
2505 + reloc_entry->address);
2506 if (status != bfd_reloc_ok)
2507 return status;
2509 else
2510 reloc_entry->addend = val;
2512 if (relocatable)
2513 reloc_entry->address += input_section->output_offset;
2515 return bfd_reloc_ok;
2518 /* A howto special_function for REL *HI16 relocations. We can only
2519 calculate the correct value once we've seen the partnering
2520 *LO16 relocation, so just save the information for later.
2522 The ABI requires that the *LO16 immediately follow the *HI16.
2523 However, as a GNU extension, we permit an arbitrary number of
2524 *HI16s to be associated with a single *LO16. This significantly
2525 simplies the relocation handling in gcc. */
2527 bfd_reloc_status_type
2528 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2529 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2530 asection *input_section, bfd *output_bfd,
2531 char **error_message ATTRIBUTE_UNUSED)
2533 struct mips_hi16 *n;
2534 struct mips_elf_obj_tdata *tdata;
2536 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2537 return bfd_reloc_outofrange;
2539 n = bfd_malloc (sizeof *n);
2540 if (n == NULL)
2541 return bfd_reloc_outofrange;
2543 tdata = mips_elf_tdata (abfd);
2544 n->next = tdata->mips_hi16_list;
2545 n->data = data;
2546 n->input_section = input_section;
2547 n->rel = *reloc_entry;
2548 tdata->mips_hi16_list = n;
2550 if (output_bfd != NULL)
2551 reloc_entry->address += input_section->output_offset;
2553 return bfd_reloc_ok;
2556 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2557 like any other 16-bit relocation when applied to global symbols, but is
2558 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2560 bfd_reloc_status_type
2561 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2562 void *data, asection *input_section,
2563 bfd *output_bfd, char **error_message)
2565 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2566 || bfd_is_und_section (bfd_asymbol_section (symbol))
2567 || bfd_is_com_section (bfd_asymbol_section (symbol)))
2568 /* The relocation is against a global symbol. */
2569 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2570 input_section, output_bfd,
2571 error_message);
2573 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2574 input_section, output_bfd, error_message);
2577 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2578 is a straightforward 16 bit inplace relocation, but we must deal with
2579 any partnering high-part relocations as well. */
2581 bfd_reloc_status_type
2582 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2583 void *data, asection *input_section,
2584 bfd *output_bfd, char **error_message)
2586 bfd_vma vallo;
2587 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2588 struct mips_elf_obj_tdata *tdata;
2590 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2591 reloc_entry->address))
2592 return bfd_reloc_outofrange;
2594 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2595 location);
2596 /* The high 16 bits of the addend are stored in the high insn, the
2597 low 16 bits in the low insn, but there is a catch: You can't
2598 just concatenate the high and low parts. The high part of the
2599 addend is adjusted for the fact that the low part is sign
2600 extended. For example, an addend of 0x38000 would have 0x0004 in
2601 the high part and 0x8000 (=0xff..f8000) in the low part.
2602 To extract the actual addend, calculate (a)
2603 ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2604 We will be applying (symbol + addend) & 0xffff to the low insn,
2605 and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2606 high insn (the +0x8000 adjusting for when the applied low part is
2607 negative). Substituting (a) into (b) and recognising that
2608 (hi & 0xffff) is already in the high insn gives a high part
2609 addend adjustment of (lo & 0xffff) ^ 0x8000. */
2610 vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2611 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2612 location);
2614 tdata = mips_elf_tdata (abfd);
2615 while (tdata->mips_hi16_list != NULL)
2617 bfd_reloc_status_type ret;
2618 struct mips_hi16 *hi;
2620 hi = tdata->mips_hi16_list;
2622 /* R_MIPS*_GOT16 relocations are something of a special case. We
2623 want to install the addend in the same way as for a R_MIPS*_HI16
2624 relocation (with a rightshift of 16). However, since GOT16
2625 relocations can also be used with global symbols, their howto
2626 has a rightshift of 0. */
2627 if (hi->rel.howto->type == R_MIPS_GOT16)
2628 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2629 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2630 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2631 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2632 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2634 hi->rel.addend += vallo;
2636 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2637 hi->input_section, output_bfd,
2638 error_message);
2639 if (ret != bfd_reloc_ok)
2640 return ret;
2642 tdata->mips_hi16_list = hi->next;
2643 free (hi);
2646 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2647 input_section, output_bfd,
2648 error_message);
2651 /* A generic howto special_function. This calculates and installs the
2652 relocation itself, thus avoiding the oft-discussed problems in
2653 bfd_perform_relocation and bfd_install_relocation. */
2655 bfd_reloc_status_type
2656 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2657 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2658 asection *input_section, bfd *output_bfd,
2659 char **error_message ATTRIBUTE_UNUSED)
2661 bfd_signed_vma val;
2662 bfd_reloc_status_type status;
2663 bool relocatable;
2665 relocatable = (output_bfd != NULL);
2667 if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2668 (relocatable
2669 ? check_inplace : check_std)))
2670 return bfd_reloc_outofrange;
2672 /* Build up the field adjustment in VAL. */
2673 val = 0;
2674 if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2675 && symbol->section->output_section != NULL)
2677 /* Either we're calculating the final field value or we have a
2678 relocation against a section symbol. Add in the section's
2679 offset or address. */
2680 val += symbol->section->output_section->vma;
2681 val += symbol->section->output_offset;
2684 if (!relocatable)
2686 /* We're calculating the final field value. Add in the symbol's value
2687 and, if pc-relative, subtract the address of the field itself. */
2688 val += symbol->value;
2689 if (reloc_entry->howto->pc_relative)
2691 val -= input_section->output_section->vma;
2692 val -= input_section->output_offset;
2693 val -= reloc_entry->address;
2697 /* VAL is now the final adjustment. If we're keeping this relocation
2698 in the output file, and if the relocation uses a separate addend,
2699 we just need to add VAL to that addend. Otherwise we need to add
2700 VAL to the relocation field itself. */
2701 if (relocatable && !reloc_entry->howto->partial_inplace)
2702 reloc_entry->addend += val;
2703 else
2705 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2707 /* Add in the separate addend, if any. */
2708 val += reloc_entry->addend;
2710 /* Add VAL to the relocation field. */
2711 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2712 location);
2713 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2714 location);
2715 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2716 location);
2718 if (status != bfd_reloc_ok)
2719 return status;
2722 if (relocatable)
2723 reloc_entry->address += input_section->output_offset;
2725 return bfd_reloc_ok;
2728 /* Swap an entry in a .gptab section. Note that these routines rely
2729 on the equivalence of the two elements of the union. */
2731 static void
2732 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2733 Elf32_gptab *in)
2735 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2736 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2739 static void
2740 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2741 Elf32_External_gptab *ex)
2743 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2744 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2747 static void
2748 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2749 Elf32_External_compact_rel *ex)
2751 H_PUT_32 (abfd, in->id1, ex->id1);
2752 H_PUT_32 (abfd, in->num, ex->num);
2753 H_PUT_32 (abfd, in->id2, ex->id2);
2754 H_PUT_32 (abfd, in->offset, ex->offset);
2755 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2756 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2759 static void
2760 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2761 Elf32_External_crinfo *ex)
2763 unsigned long l;
2765 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2766 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2767 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2768 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2769 H_PUT_32 (abfd, l, ex->info);
2770 H_PUT_32 (abfd, in->konst, ex->konst);
2771 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2774 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2775 routines swap this structure in and out. They are used outside of
2776 BFD, so they are globally visible. */
2778 void
2779 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2780 Elf32_RegInfo *in)
2782 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2783 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2784 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2785 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2786 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2787 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2790 void
2791 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2792 Elf32_External_RegInfo *ex)
2794 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2795 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2796 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2797 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2798 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2799 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2802 /* In the 64 bit ABI, the .MIPS.options section holds register
2803 information in an Elf64_Reginfo structure. These routines swap
2804 them in and out. They are globally visible because they are used
2805 outside of BFD. These routines are here so that gas can call them
2806 without worrying about whether the 64 bit ABI has been included. */
2808 void
2809 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2810 Elf64_Internal_RegInfo *in)
2812 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2813 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2814 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2815 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2816 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2817 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2818 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2821 void
2822 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2823 Elf64_External_RegInfo *ex)
2825 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2826 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2827 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2828 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2829 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2830 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2831 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2834 /* Swap in an options header. */
2836 void
2837 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2838 Elf_Internal_Options *in)
2840 in->kind = H_GET_8 (abfd, ex->kind);
2841 in->size = H_GET_8 (abfd, ex->size);
2842 in->section = H_GET_16 (abfd, ex->section);
2843 in->info = H_GET_32 (abfd, ex->info);
2846 /* Swap out an options header. */
2848 void
2849 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2850 Elf_External_Options *ex)
2852 H_PUT_8 (abfd, in->kind, ex->kind);
2853 H_PUT_8 (abfd, in->size, ex->size);
2854 H_PUT_16 (abfd, in->section, ex->section);
2855 H_PUT_32 (abfd, in->info, ex->info);
2858 /* Swap in an abiflags structure. */
2860 void
2861 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2862 const Elf_External_ABIFlags_v0 *ex,
2863 Elf_Internal_ABIFlags_v0 *in)
2865 in->version = H_GET_16 (abfd, ex->version);
2866 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2867 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2868 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2869 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2870 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2871 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2872 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2873 in->ases = H_GET_32 (abfd, ex->ases);
2874 in->flags1 = H_GET_32 (abfd, ex->flags1);
2875 in->flags2 = H_GET_32 (abfd, ex->flags2);
2878 /* Swap out an abiflags structure. */
2880 void
2881 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2882 const Elf_Internal_ABIFlags_v0 *in,
2883 Elf_External_ABIFlags_v0 *ex)
2885 H_PUT_16 (abfd, in->version, ex->version);
2886 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2887 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2888 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2889 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2890 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2891 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2892 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2893 H_PUT_32 (abfd, in->ases, ex->ases);
2894 H_PUT_32 (abfd, in->flags1, ex->flags1);
2895 H_PUT_32 (abfd, in->flags2, ex->flags2);
2898 /* This function is called via qsort() to sort the dynamic relocation
2899 entries by increasing r_symndx value. */
2901 static int
2902 sort_dynamic_relocs (const void *arg1, const void *arg2)
2904 Elf_Internal_Rela int_reloc1;
2905 Elf_Internal_Rela int_reloc2;
2906 int diff;
2908 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2909 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2911 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2912 if (diff != 0)
2913 return diff;
2915 if (int_reloc1.r_offset < int_reloc2.r_offset)
2916 return -1;
2917 if (int_reloc1.r_offset > int_reloc2.r_offset)
2918 return 1;
2919 return 0;
2922 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2924 static int
2925 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2926 const void *arg2 ATTRIBUTE_UNUSED)
2928 #ifdef BFD64
2929 Elf_Internal_Rela int_reloc1[3];
2930 Elf_Internal_Rela int_reloc2[3];
2932 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2933 (reldyn_sorting_bfd, arg1, int_reloc1);
2934 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2935 (reldyn_sorting_bfd, arg2, int_reloc2);
2937 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2938 return -1;
2939 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2940 return 1;
2942 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2943 return -1;
2944 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2945 return 1;
2946 return 0;
2947 #else
2948 abort ();
2949 #endif
2953 /* This routine is used to write out ECOFF debugging external symbol
2954 information. It is called via mips_elf_link_hash_traverse. The
2955 ECOFF external symbol information must match the ELF external
2956 symbol information. Unfortunately, at this point we don't know
2957 whether a symbol is required by reloc information, so the two
2958 tables may wind up being different. We must sort out the external
2959 symbol information before we can set the final size of the .mdebug
2960 section, and we must set the size of the .mdebug section before we
2961 can relocate any sections, and we can't know which symbols are
2962 required by relocation until we relocate the sections.
2963 Fortunately, it is relatively unlikely that any symbol will be
2964 stripped but required by a reloc. In particular, it can not happen
2965 when generating a final executable. */
2967 static bool
2968 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2970 struct extsym_info *einfo = data;
2971 bool strip;
2972 asection *sec, *output_section;
2974 if (h->root.indx == -2)
2975 strip = false;
2976 else if ((h->root.def_dynamic
2977 || h->root.ref_dynamic
2978 || h->root.type == bfd_link_hash_new)
2979 && !h->root.def_regular
2980 && !h->root.ref_regular)
2981 strip = true;
2982 else if (einfo->info->strip == strip_all
2983 || (einfo->info->strip == strip_some
2984 && bfd_hash_lookup (einfo->info->keep_hash,
2985 h->root.root.root.string,
2986 false, false) == NULL))
2987 strip = true;
2988 else
2989 strip = false;
2991 if (strip)
2992 return true;
2994 if (h->esym.ifd == -2)
2996 h->esym.jmptbl = 0;
2997 h->esym.cobol_main = 0;
2998 h->esym.weakext = 0;
2999 h->esym.reserved = 0;
3000 h->esym.ifd = ifdNil;
3001 h->esym.asym.value = 0;
3002 h->esym.asym.st = stGlobal;
3004 if (h->root.root.type == bfd_link_hash_undefined
3005 || h->root.root.type == bfd_link_hash_undefweak)
3007 const char *name;
3009 /* Use undefined class. Also, set class and type for some
3010 special symbols. */
3011 name = h->root.root.root.string;
3012 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3013 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3015 h->esym.asym.sc = scData;
3016 h->esym.asym.st = stLabel;
3017 h->esym.asym.value = 0;
3019 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3021 h->esym.asym.sc = scAbs;
3022 h->esym.asym.st = stLabel;
3023 h->esym.asym.value =
3024 mips_elf_hash_table (einfo->info)->procedure_count;
3026 else
3027 h->esym.asym.sc = scUndefined;
3029 else if (h->root.root.type != bfd_link_hash_defined
3030 && h->root.root.type != bfd_link_hash_defweak)
3031 h->esym.asym.sc = scAbs;
3032 else
3034 const char *name;
3036 sec = h->root.root.u.def.section;
3037 output_section = sec->output_section;
3039 /* When making a shared library and symbol h is the one from
3040 the another shared library, OUTPUT_SECTION may be null. */
3041 if (output_section == NULL)
3042 h->esym.asym.sc = scUndefined;
3043 else
3045 name = bfd_section_name (output_section);
3047 if (strcmp (name, ".text") == 0)
3048 h->esym.asym.sc = scText;
3049 else if (strcmp (name, ".data") == 0)
3050 h->esym.asym.sc = scData;
3051 else if (strcmp (name, ".sdata") == 0)
3052 h->esym.asym.sc = scSData;
3053 else if (strcmp (name, ".rodata") == 0
3054 || strcmp (name, ".rdata") == 0)
3055 h->esym.asym.sc = scRData;
3056 else if (strcmp (name, ".bss") == 0)
3057 h->esym.asym.sc = scBss;
3058 else if (strcmp (name, ".sbss") == 0)
3059 h->esym.asym.sc = scSBss;
3060 else if (strcmp (name, ".init") == 0)
3061 h->esym.asym.sc = scInit;
3062 else if (strcmp (name, ".fini") == 0)
3063 h->esym.asym.sc = scFini;
3064 else
3065 h->esym.asym.sc = scAbs;
3069 h->esym.asym.reserved = 0;
3070 h->esym.asym.index = indexNil;
3073 if (h->root.root.type == bfd_link_hash_common)
3074 h->esym.asym.value = h->root.root.u.c.size;
3075 else if (h->root.root.type == bfd_link_hash_defined
3076 || h->root.root.type == bfd_link_hash_defweak)
3078 if (h->esym.asym.sc == scCommon)
3079 h->esym.asym.sc = scBss;
3080 else if (h->esym.asym.sc == scSCommon)
3081 h->esym.asym.sc = scSBss;
3083 sec = h->root.root.u.def.section;
3084 output_section = sec->output_section;
3085 if (output_section != NULL)
3086 h->esym.asym.value = (h->root.root.u.def.value
3087 + sec->output_offset
3088 + output_section->vma);
3089 else
3090 h->esym.asym.value = 0;
3092 else
3094 struct mips_elf_link_hash_entry *hd = h;
3096 while (hd->root.root.type == bfd_link_hash_indirect)
3097 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3099 if (hd->needs_lazy_stub)
3101 BFD_ASSERT (hd->root.plt.plist != NULL);
3102 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3103 /* Set type and value for a symbol with a function stub. */
3104 h->esym.asym.st = stProc;
3105 sec = hd->root.root.u.def.section;
3106 if (sec == NULL)
3107 h->esym.asym.value = 0;
3108 else
3110 output_section = sec->output_section;
3111 if (output_section != NULL)
3112 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3113 + sec->output_offset
3114 + output_section->vma);
3115 else
3116 h->esym.asym.value = 0;
3121 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3122 h->root.root.root.string,
3123 &h->esym))
3125 einfo->failed = true;
3126 return false;
3129 return true;
3132 /* A comparison routine used to sort .gptab entries. */
3134 static int
3135 gptab_compare (const void *p1, const void *p2)
3137 const Elf32_gptab *a1 = p1;
3138 const Elf32_gptab *a2 = p2;
3140 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3143 /* Functions to manage the got entry hash table. */
3145 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3146 hash number. */
3148 static inline hashval_t
3149 mips_elf_hash_bfd_vma (bfd_vma addr)
3151 #ifdef BFD64
3152 return addr + (addr >> 32);
3153 #else
3154 return addr;
3155 #endif
3158 static hashval_t
3159 mips_elf_got_entry_hash (const void *entry_)
3161 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3163 return (entry->symndx
3164 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3165 + (entry->tls_type == GOT_TLS_LDM ? 0
3166 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3167 : entry->symndx >= 0 ? (entry->abfd->id
3168 + mips_elf_hash_bfd_vma (entry->d.addend))
3169 : entry->d.h->root.root.root.hash));
3172 static int
3173 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3175 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3176 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3178 return (e1->symndx == e2->symndx
3179 && e1->tls_type == e2->tls_type
3180 && (e1->tls_type == GOT_TLS_LDM ? true
3181 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3182 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3183 && e1->d.addend == e2->d.addend)
3184 : e2->abfd && e1->d.h == e2->d.h));
3187 static hashval_t
3188 mips_got_page_ref_hash (const void *ref_)
3190 const struct mips_got_page_ref *ref;
3192 ref = (const struct mips_got_page_ref *) ref_;
3193 return ((ref->symndx >= 0
3194 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3195 : ref->u.h->root.root.root.hash)
3196 + mips_elf_hash_bfd_vma (ref->addend));
3199 static int
3200 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3202 const struct mips_got_page_ref *ref1, *ref2;
3204 ref1 = (const struct mips_got_page_ref *) ref1_;
3205 ref2 = (const struct mips_got_page_ref *) ref2_;
3206 return (ref1->symndx == ref2->symndx
3207 && (ref1->symndx < 0
3208 ? ref1->u.h == ref2->u.h
3209 : ref1->u.abfd == ref2->u.abfd)
3210 && ref1->addend == ref2->addend);
3213 static hashval_t
3214 mips_got_page_entry_hash (const void *entry_)
3216 const struct mips_got_page_entry *entry;
3218 entry = (const struct mips_got_page_entry *) entry_;
3219 return entry->sec->id;
3222 static int
3223 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3225 const struct mips_got_page_entry *entry1, *entry2;
3227 entry1 = (const struct mips_got_page_entry *) entry1_;
3228 entry2 = (const struct mips_got_page_entry *) entry2_;
3229 return entry1->sec == entry2->sec;
3232 /* Create and return a new mips_got_info structure. */
3234 static struct mips_got_info *
3235 mips_elf_create_got_info (bfd *abfd)
3237 struct mips_got_info *g;
3239 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3240 if (g == NULL)
3241 return NULL;
3243 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3244 mips_elf_got_entry_eq, NULL);
3245 if (g->got_entries == NULL)
3246 return NULL;
3248 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3249 mips_got_page_ref_eq, NULL);
3250 if (g->got_page_refs == NULL)
3251 return NULL;
3253 return g;
3256 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3257 CREATE_P and if ABFD doesn't already have a GOT. */
3259 static struct mips_got_info *
3260 mips_elf_bfd_got (bfd *abfd, bool create_p)
3262 struct mips_elf_obj_tdata *tdata;
3264 if (!is_mips_elf (abfd))
3265 return NULL;
3267 tdata = mips_elf_tdata (abfd);
3268 if (!tdata->got && create_p)
3269 tdata->got = mips_elf_create_got_info (abfd);
3270 return tdata->got;
3273 /* Record that ABFD should use output GOT G. */
3275 static void
3276 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3278 struct mips_elf_obj_tdata *tdata;
3280 BFD_ASSERT (is_mips_elf (abfd));
3281 tdata = mips_elf_tdata (abfd);
3282 if (tdata->got)
3284 /* The GOT structure itself and the hash table entries are
3285 allocated to a bfd, but the hash tables aren't. */
3286 htab_delete (tdata->got->got_entries);
3287 htab_delete (tdata->got->got_page_refs);
3288 if (tdata->got->got_page_entries)
3289 htab_delete (tdata->got->got_page_entries);
3291 tdata->got = g;
3294 /* Return the dynamic relocation section. If it doesn't exist, try to
3295 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3296 if creation fails. */
3298 static asection *
3299 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3301 const char *dname;
3302 asection *sreloc;
3303 bfd *dynobj;
3305 dname = MIPS_ELF_REL_DYN_NAME (info);
3306 dynobj = elf_hash_table (info)->dynobj;
3307 sreloc = bfd_get_linker_section (dynobj, dname);
3308 if (sreloc == NULL && create_p)
3310 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3311 (SEC_ALLOC
3312 | SEC_LOAD
3313 | SEC_HAS_CONTENTS
3314 | SEC_IN_MEMORY
3315 | SEC_LINKER_CREATED
3316 | SEC_READONLY));
3317 if (sreloc == NULL
3318 || !bfd_set_section_alignment (sreloc,
3319 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3320 return NULL;
3322 return sreloc;
3325 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3327 static int
3328 mips_elf_reloc_tls_type (unsigned int r_type)
3330 if (tls_gd_reloc_p (r_type))
3331 return GOT_TLS_GD;
3333 if (tls_ldm_reloc_p (r_type))
3334 return GOT_TLS_LDM;
3336 if (tls_gottprel_reloc_p (r_type))
3337 return GOT_TLS_IE;
3339 return GOT_TLS_NONE;
3342 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3344 static int
3345 mips_tls_got_entries (unsigned int type)
3347 switch (type)
3349 case GOT_TLS_GD:
3350 case GOT_TLS_LDM:
3351 return 2;
3353 case GOT_TLS_IE:
3354 return 1;
3356 case GOT_TLS_NONE:
3357 return 0;
3359 abort ();
3362 /* Count the number of relocations needed for a TLS GOT entry, with
3363 access types from TLS_TYPE, and symbol H (or a local symbol if H
3364 is NULL). */
3366 static int
3367 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3368 struct elf_link_hash_entry *h)
3370 int indx = 0;
3371 bool need_relocs = false;
3372 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3374 if (h != NULL
3375 && h->dynindx != -1
3376 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3377 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3378 indx = h->dynindx;
3380 if ((bfd_link_dll (info) || indx != 0)
3381 && (h == NULL
3382 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3383 || h->root.type != bfd_link_hash_undefweak))
3384 need_relocs = true;
3386 if (!need_relocs)
3387 return 0;
3389 switch (tls_type)
3391 case GOT_TLS_GD:
3392 return indx != 0 ? 2 : 1;
3394 case GOT_TLS_IE:
3395 return 1;
3397 case GOT_TLS_LDM:
3398 return bfd_link_dll (info) ? 1 : 0;
3400 default:
3401 return 0;
3405 /* Add the number of GOT entries and TLS relocations required by ENTRY
3406 to G. */
3408 static void
3409 mips_elf_count_got_entry (struct bfd_link_info *info,
3410 struct mips_got_info *g,
3411 struct mips_got_entry *entry)
3413 if (entry->tls_type)
3415 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3416 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3417 entry->symndx < 0
3418 ? &entry->d.h->root : NULL);
3420 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3421 g->local_gotno += 1;
3422 else
3423 g->global_gotno += 1;
3426 /* Output a simple dynamic relocation into SRELOC. */
3428 static void
3429 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3430 asection *sreloc,
3431 unsigned long reloc_index,
3432 unsigned long indx,
3433 int r_type,
3434 bfd_vma offset)
3436 Elf_Internal_Rela rel[3];
3438 memset (rel, 0, sizeof (rel));
3440 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3441 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3443 if (ABI_64_P (output_bfd))
3445 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3446 (output_bfd, &rel[0],
3447 (sreloc->contents
3448 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3450 else
3451 bfd_elf32_swap_reloc_out
3452 (output_bfd, &rel[0],
3453 (sreloc->contents
3454 + reloc_index * sizeof (Elf32_External_Rel)));
3457 /* Initialize a set of TLS GOT entries for one symbol. */
3459 static void
3460 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3461 struct mips_got_entry *entry,
3462 struct mips_elf_link_hash_entry *h,
3463 bfd_vma value)
3465 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3466 struct mips_elf_link_hash_table *htab;
3467 int indx;
3468 asection *sreloc, *sgot;
3469 bfd_vma got_offset, got_offset2;
3470 bool need_relocs = false;
3472 htab = mips_elf_hash_table (info);
3473 if (htab == NULL)
3474 return;
3476 sgot = htab->root.sgot;
3478 indx = 0;
3479 if (h != NULL
3480 && h->root.dynindx != -1
3481 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3482 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3483 indx = h->root.dynindx;
3485 if (entry->tls_initialized)
3486 return;
3488 if ((bfd_link_dll (info) || indx != 0)
3489 && (h == NULL
3490 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3491 || h->root.type != bfd_link_hash_undefweak))
3492 need_relocs = true;
3494 /* MINUS_ONE means the symbol is not defined in this object. It may not
3495 be defined at all; assume that the value doesn't matter in that
3496 case. Otherwise complain if we would use the value. */
3497 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3498 || h->root.root.type == bfd_link_hash_undefweak);
3500 /* Emit necessary relocations. */
3501 sreloc = mips_elf_rel_dyn_section (info, false);
3502 got_offset = entry->gotidx;
3504 switch (entry->tls_type)
3506 case GOT_TLS_GD:
3507 /* General Dynamic. */
3508 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3510 if (need_relocs)
3512 mips_elf_output_dynamic_relocation
3513 (abfd, sreloc, sreloc->reloc_count++, indx,
3514 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3515 sgot->output_offset + sgot->output_section->vma + got_offset);
3517 if (indx)
3518 mips_elf_output_dynamic_relocation
3519 (abfd, sreloc, sreloc->reloc_count++, indx,
3520 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3521 sgot->output_offset + sgot->output_section->vma + got_offset2);
3522 else
3523 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3524 sgot->contents + got_offset2);
3526 else
3528 MIPS_ELF_PUT_WORD (abfd, 1,
3529 sgot->contents + got_offset);
3530 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3531 sgot->contents + got_offset2);
3533 break;
3535 case GOT_TLS_IE:
3536 /* Initial Exec model. */
3537 if (need_relocs)
3539 if (indx == 0)
3540 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3541 sgot->contents + got_offset);
3542 else
3543 MIPS_ELF_PUT_WORD (abfd, 0,
3544 sgot->contents + got_offset);
3546 mips_elf_output_dynamic_relocation
3547 (abfd, sreloc, sreloc->reloc_count++, indx,
3548 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3549 sgot->output_offset + sgot->output_section->vma + got_offset);
3551 else
3552 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3553 sgot->contents + got_offset);
3554 break;
3556 case GOT_TLS_LDM:
3557 /* The initial offset is zero, and the LD offsets will include the
3558 bias by DTP_OFFSET. */
3559 MIPS_ELF_PUT_WORD (abfd, 0,
3560 sgot->contents + got_offset
3561 + MIPS_ELF_GOT_SIZE (abfd));
3563 if (!bfd_link_dll (info))
3564 MIPS_ELF_PUT_WORD (abfd, 1,
3565 sgot->contents + got_offset);
3566 else
3567 mips_elf_output_dynamic_relocation
3568 (abfd, sreloc, sreloc->reloc_count++, indx,
3569 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3570 sgot->output_offset + sgot->output_section->vma + got_offset);
3571 break;
3573 default:
3574 abort ();
3577 entry->tls_initialized = true;
3580 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3581 for global symbol H. .got.plt comes before the GOT, so the offset
3582 will be negative. */
3584 static bfd_vma
3585 mips_elf_gotplt_index (struct bfd_link_info *info,
3586 struct elf_link_hash_entry *h)
3588 bfd_vma got_address, got_value;
3589 struct mips_elf_link_hash_table *htab;
3591 htab = mips_elf_hash_table (info);
3592 BFD_ASSERT (htab != NULL);
3594 BFD_ASSERT (h->plt.plist != NULL);
3595 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3597 /* Calculate the address of the associated .got.plt entry. */
3598 got_address = (htab->root.sgotplt->output_section->vma
3599 + htab->root.sgotplt->output_offset
3600 + (h->plt.plist->gotplt_index
3601 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3603 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3604 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3605 + htab->root.hgot->root.u.def.section->output_offset
3606 + htab->root.hgot->root.u.def.value);
3608 return got_address - got_value;
3611 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3612 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3613 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3614 offset can be found. */
3616 static bfd_vma
3617 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3618 bfd_vma value, unsigned long r_symndx,
3619 struct mips_elf_link_hash_entry *h, int r_type)
3621 struct mips_elf_link_hash_table *htab;
3622 struct mips_got_entry *entry;
3624 htab = mips_elf_hash_table (info);
3625 BFD_ASSERT (htab != NULL);
3627 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3628 r_symndx, h, r_type);
3629 if (!entry)
3630 return MINUS_ONE;
3632 if (entry->tls_type)
3633 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3634 return entry->gotidx;
3637 /* Return the GOT index of global symbol H in the primary GOT. */
3639 static bfd_vma
3640 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3641 struct elf_link_hash_entry *h)
3643 struct mips_elf_link_hash_table *htab;
3644 long global_got_dynindx;
3645 struct mips_got_info *g;
3646 bfd_vma got_index;
3648 htab = mips_elf_hash_table (info);
3649 BFD_ASSERT (htab != NULL);
3651 global_got_dynindx = 0;
3652 if (htab->global_gotsym != NULL)
3653 global_got_dynindx = htab->global_gotsym->dynindx;
3655 /* Once we determine the global GOT entry with the lowest dynamic
3656 symbol table index, we must put all dynamic symbols with greater
3657 indices into the primary GOT. That makes it easy to calculate the
3658 GOT offset. */
3659 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3660 g = mips_elf_bfd_got (obfd, false);
3661 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3662 * MIPS_ELF_GOT_SIZE (obfd));
3663 BFD_ASSERT (got_index < htab->root.sgot->size);
3665 return got_index;
3668 /* Return the GOT index for the global symbol indicated by H, which is
3669 referenced by a relocation of type R_TYPE in IBFD. */
3671 static bfd_vma
3672 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3673 struct elf_link_hash_entry *h, int r_type)
3675 struct mips_elf_link_hash_table *htab;
3676 struct mips_got_info *g;
3677 struct mips_got_entry lookup, *entry;
3678 bfd_vma gotidx;
3680 htab = mips_elf_hash_table (info);
3681 BFD_ASSERT (htab != NULL);
3683 g = mips_elf_bfd_got (ibfd, false);
3684 BFD_ASSERT (g);
3686 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3687 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3688 return mips_elf_primary_global_got_index (obfd, info, h);
3690 lookup.abfd = ibfd;
3691 lookup.symndx = -1;
3692 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3693 entry = htab_find (g->got_entries, &lookup);
3694 BFD_ASSERT (entry);
3696 gotidx = entry->gotidx;
3697 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3699 if (lookup.tls_type)
3701 bfd_vma value = MINUS_ONE;
3703 if ((h->root.type == bfd_link_hash_defined
3704 || h->root.type == bfd_link_hash_defweak)
3705 && h->root.u.def.section->output_section)
3706 value = (h->root.u.def.value
3707 + h->root.u.def.section->output_offset
3708 + h->root.u.def.section->output_section->vma);
3710 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3712 return gotidx;
3715 /* Find a GOT page entry that points to within 32KB of VALUE. These
3716 entries are supposed to be placed at small offsets in the GOT, i.e.,
3717 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3718 entry could be created. If OFFSETP is nonnull, use it to return the
3719 offset of the GOT entry from VALUE. */
3721 static bfd_vma
3722 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3723 bfd_vma value, bfd_vma *offsetp)
3725 bfd_vma page, got_index;
3726 struct mips_got_entry *entry;
3728 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3729 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3730 NULL, R_MIPS_GOT_PAGE);
3732 if (!entry)
3733 return MINUS_ONE;
3735 got_index = entry->gotidx;
3737 if (offsetp)
3738 *offsetp = value - entry->d.address;
3740 return got_index;
3743 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3744 EXTERNAL is true if the relocation was originally against a global
3745 symbol that binds locally. */
3747 static bfd_vma
3748 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3749 bfd_vma value, bool external)
3751 struct mips_got_entry *entry;
3753 /* GOT16 relocations against local symbols are followed by a LO16
3754 relocation; those against global symbols are not. Thus if the
3755 symbol was originally local, the GOT16 relocation should load the
3756 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3757 if (! external)
3758 value = mips_elf_high (value) << 16;
3760 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3761 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3762 same in all cases. */
3763 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3764 NULL, R_MIPS_GOT16);
3765 if (entry)
3766 return entry->gotidx;
3767 else
3768 return MINUS_ONE;
3771 /* Returns the offset for the entry at the INDEXth position
3772 in the GOT. */
3774 static bfd_vma
3775 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3776 bfd *input_bfd, bfd_vma got_index)
3778 struct mips_elf_link_hash_table *htab;
3779 asection *sgot;
3780 bfd_vma gp;
3782 htab = mips_elf_hash_table (info);
3783 BFD_ASSERT (htab != NULL);
3785 sgot = htab->root.sgot;
3786 gp = _bfd_get_gp_value (output_bfd)
3787 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3789 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3792 /* Create and return a local GOT entry for VALUE, which was calculated
3793 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3794 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3795 instead. */
3797 static struct mips_got_entry *
3798 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3799 bfd *ibfd, bfd_vma value,
3800 unsigned long r_symndx,
3801 struct mips_elf_link_hash_entry *h,
3802 int r_type)
3804 struct mips_got_entry lookup, *entry;
3805 void **loc;
3806 struct mips_got_info *g;
3807 struct mips_elf_link_hash_table *htab;
3808 bfd_vma gotidx;
3810 htab = mips_elf_hash_table (info);
3811 BFD_ASSERT (htab != NULL);
3813 g = mips_elf_bfd_got (ibfd, false);
3814 if (g == NULL)
3816 g = mips_elf_bfd_got (abfd, false);
3817 BFD_ASSERT (g != NULL);
3820 /* This function shouldn't be called for symbols that live in the global
3821 area of the GOT. */
3822 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3824 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3825 if (lookup.tls_type)
3827 lookup.abfd = ibfd;
3828 if (tls_ldm_reloc_p (r_type))
3830 lookup.symndx = 0;
3831 lookup.d.addend = 0;
3833 else if (h == NULL)
3835 lookup.symndx = r_symndx;
3836 lookup.d.addend = 0;
3838 else
3840 lookup.symndx = -1;
3841 lookup.d.h = h;
3844 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3845 BFD_ASSERT (entry);
3847 gotidx = entry->gotidx;
3848 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3850 return entry;
3853 lookup.abfd = NULL;
3854 lookup.symndx = -1;
3855 lookup.d.address = value;
3856 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3857 if (!loc)
3858 return NULL;
3860 entry = (struct mips_got_entry *) *loc;
3861 if (entry)
3862 return entry;
3864 if (g->assigned_low_gotno > g->assigned_high_gotno)
3866 /* We didn't allocate enough space in the GOT. */
3867 _bfd_error_handler
3868 (_("not enough GOT space for local GOT entries"));
3869 bfd_set_error (bfd_error_bad_value);
3870 return NULL;
3873 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3874 if (!entry)
3875 return NULL;
3877 if (got16_reloc_p (r_type)
3878 || call16_reloc_p (r_type)
3879 || got_page_reloc_p (r_type)
3880 || got_disp_reloc_p (r_type))
3881 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3882 else
3883 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3885 *entry = lookup;
3886 *loc = entry;
3888 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3890 /* These GOT entries need a dynamic relocation on VxWorks. */
3891 if (htab->root.target_os == is_vxworks)
3893 Elf_Internal_Rela outrel;
3894 asection *s;
3895 bfd_byte *rloc;
3896 bfd_vma got_address;
3898 s = mips_elf_rel_dyn_section (info, false);
3899 got_address = (htab->root.sgot->output_section->vma
3900 + htab->root.sgot->output_offset
3901 + entry->gotidx);
3903 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3904 outrel.r_offset = got_address;
3905 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3906 outrel.r_addend = value;
3907 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3910 return entry;
3913 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3914 The number might be exact or a worst-case estimate, depending on how
3915 much information is available to elf_backend_omit_section_dynsym at
3916 the current linking stage. */
3918 static bfd_size_type
3919 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3921 bfd_size_type count;
3923 count = 0;
3924 if (bfd_link_pic (info))
3926 asection *p;
3927 const struct elf_backend_data *bed;
3929 bed = get_elf_backend_data (output_bfd);
3930 for (p = output_bfd->sections; p ; p = p->next)
3931 if ((p->flags & SEC_EXCLUDE) == 0
3932 && (p->flags & SEC_ALLOC) != 0
3933 && elf_hash_table (info)->dynamic_relocs
3934 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3935 ++count;
3937 return count;
3940 /* Sort the dynamic symbol table so that symbols that need GOT entries
3941 appear towards the end. */
3943 static bool
3944 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3946 struct mips_elf_link_hash_table *htab;
3947 struct mips_elf_hash_sort_data hsd;
3948 struct mips_got_info *g;
3950 htab = mips_elf_hash_table (info);
3951 BFD_ASSERT (htab != NULL);
3953 if (htab->root.dynsymcount == 0)
3954 return true;
3956 g = htab->got_info;
3957 if (g == NULL)
3958 return true;
3960 hsd.low = NULL;
3961 hsd.max_unref_got_dynindx
3962 = hsd.min_got_dynindx
3963 = (htab->root.dynsymcount - g->reloc_only_gotno);
3964 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3965 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3966 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3967 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3968 hsd.output_bfd = abfd;
3969 if (htab->root.dynobj != NULL
3970 && htab->root.dynamic_sections_created
3971 && info->emit_gnu_hash)
3973 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3974 BFD_ASSERT (s != NULL);
3975 hsd.mipsxhash = s->contents;
3976 BFD_ASSERT (hsd.mipsxhash != NULL);
3978 else
3979 hsd.mipsxhash = NULL;
3980 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3982 /* There should have been enough room in the symbol table to
3983 accommodate both the GOT and non-GOT symbols. */
3984 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3985 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3986 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3987 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3989 /* Now we know which dynamic symbol has the lowest dynamic symbol
3990 table index in the GOT. */
3991 htab->global_gotsym = hsd.low;
3993 return true;
3996 /* If H needs a GOT entry, assign it the highest available dynamic
3997 index. Otherwise, assign it the lowest available dynamic
3998 index. */
4000 static bool
4001 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4003 struct mips_elf_hash_sort_data *hsd = data;
4005 /* Symbols without dynamic symbol table entries aren't interesting
4006 at all. */
4007 if (h->root.dynindx == -1)
4008 return true;
4010 switch (h->global_got_area)
4012 case GGA_NONE:
4013 if (h->root.forced_local)
4014 h->root.dynindx = hsd->max_local_dynindx++;
4015 else
4016 h->root.dynindx = hsd->max_non_got_dynindx++;
4017 break;
4019 case GGA_NORMAL:
4020 h->root.dynindx = --hsd->min_got_dynindx;
4021 hsd->low = (struct elf_link_hash_entry *) h;
4022 break;
4024 case GGA_RELOC_ONLY:
4025 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4026 hsd->low = (struct elf_link_hash_entry *) h;
4027 h->root.dynindx = hsd->max_unref_got_dynindx++;
4028 break;
4031 /* Populate the .MIPS.xhash translation table entry with
4032 the symbol dynindx. */
4033 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4034 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4035 hsd->mipsxhash + h->mipsxhash_loc);
4037 return true;
4040 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4041 (which is owned by the caller and shouldn't be added to the
4042 hash table directly). */
4044 static bool
4045 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4046 struct mips_got_entry *lookup)
4048 struct mips_elf_link_hash_table *htab;
4049 struct mips_got_entry *entry;
4050 struct mips_got_info *g;
4051 void **loc, **bfd_loc;
4053 /* Make sure there's a slot for this entry in the master GOT. */
4054 htab = mips_elf_hash_table (info);
4055 g = htab->got_info;
4056 loc = htab_find_slot (g->got_entries, lookup, INSERT);
4057 if (!loc)
4058 return false;
4060 /* Populate the entry if it isn't already. */
4061 entry = (struct mips_got_entry *) *loc;
4062 if (!entry)
4064 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4065 if (!entry)
4066 return false;
4068 lookup->tls_initialized = false;
4069 lookup->gotidx = -1;
4070 *entry = *lookup;
4071 *loc = entry;
4074 /* Reuse the same GOT entry for the BFD's GOT. */
4075 g = mips_elf_bfd_got (abfd, true);
4076 if (!g)
4077 return false;
4079 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4080 if (!bfd_loc)
4081 return false;
4083 if (!*bfd_loc)
4084 *bfd_loc = entry;
4085 return true;
4088 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4089 entry for it. FOR_CALL is true if the caller is only interested in
4090 using the GOT entry for calls. */
4092 static bool
4093 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4094 bfd *abfd, struct bfd_link_info *info,
4095 bool for_call, int r_type)
4097 struct mips_elf_link_hash_table *htab;
4098 struct mips_elf_link_hash_entry *hmips;
4099 struct mips_got_entry entry;
4100 unsigned char tls_type;
4102 htab = mips_elf_hash_table (info);
4103 BFD_ASSERT (htab != NULL);
4105 hmips = (struct mips_elf_link_hash_entry *) h;
4106 if (!for_call)
4107 hmips->got_only_for_calls = false;
4109 /* A global symbol in the GOT must also be in the dynamic symbol
4110 table. */
4111 if (h->dynindx == -1)
4113 switch (ELF_ST_VISIBILITY (h->other))
4115 case STV_INTERNAL:
4116 case STV_HIDDEN:
4117 _bfd_mips_elf_hide_symbol (info, h, true);
4118 break;
4120 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4121 return false;
4124 tls_type = mips_elf_reloc_tls_type (r_type);
4125 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4126 hmips->global_got_area = GGA_NORMAL;
4128 entry.abfd = abfd;
4129 entry.symndx = -1;
4130 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4131 entry.tls_type = tls_type;
4132 return mips_elf_record_got_entry (info, abfd, &entry);
4135 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4136 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4138 static bool
4139 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4140 struct bfd_link_info *info, int r_type)
4142 struct mips_elf_link_hash_table *htab;
4143 struct mips_got_info *g;
4144 struct mips_got_entry entry;
4146 htab = mips_elf_hash_table (info);
4147 BFD_ASSERT (htab != NULL);
4149 g = htab->got_info;
4150 BFD_ASSERT (g != NULL);
4152 entry.abfd = abfd;
4153 entry.symndx = symndx;
4154 entry.d.addend = addend;
4155 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4156 return mips_elf_record_got_entry (info, abfd, &entry);
4159 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4160 H is the symbol's hash table entry, or null if SYMNDX is local
4161 to ABFD. */
4163 static bool
4164 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4165 long symndx, struct elf_link_hash_entry *h,
4166 bfd_signed_vma addend)
4168 struct mips_elf_link_hash_table *htab;
4169 struct mips_got_info *g1, *g2;
4170 struct mips_got_page_ref lookup, *entry;
4171 void **loc, **bfd_loc;
4173 htab = mips_elf_hash_table (info);
4174 BFD_ASSERT (htab != NULL);
4176 g1 = htab->got_info;
4177 BFD_ASSERT (g1 != NULL);
4179 if (h)
4181 lookup.symndx = -1;
4182 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4184 else
4186 lookup.symndx = symndx;
4187 lookup.u.abfd = abfd;
4189 lookup.addend = addend;
4190 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4191 if (loc == NULL)
4192 return false;
4194 entry = (struct mips_got_page_ref *) *loc;
4195 if (!entry)
4197 entry = bfd_alloc (abfd, sizeof (*entry));
4198 if (!entry)
4199 return false;
4201 *entry = lookup;
4202 *loc = entry;
4205 /* Add the same entry to the BFD's GOT. */
4206 g2 = mips_elf_bfd_got (abfd, true);
4207 if (!g2)
4208 return false;
4210 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4211 if (!bfd_loc)
4212 return false;
4214 if (!*bfd_loc)
4215 *bfd_loc = entry;
4217 return true;
4220 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4222 static void
4223 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4224 unsigned int n)
4226 asection *s;
4227 struct mips_elf_link_hash_table *htab;
4229 htab = mips_elf_hash_table (info);
4230 BFD_ASSERT (htab != NULL);
4232 s = mips_elf_rel_dyn_section (info, false);
4233 BFD_ASSERT (s != NULL);
4235 if (htab->root.target_os == is_vxworks)
4236 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4237 else
4239 if (s->size == 0)
4241 /* Make room for a null element. */
4242 s->size += MIPS_ELF_REL_SIZE (abfd);
4243 ++s->reloc_count;
4245 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4249 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4250 mips_elf_traverse_got_arg structure. Count the number of GOT
4251 entries and TLS relocs. Set DATA->value to true if we need
4252 to resolve indirect or warning symbols and then recreate the GOT. */
4254 static int
4255 mips_elf_check_recreate_got (void **entryp, void *data)
4257 struct mips_got_entry *entry;
4258 struct mips_elf_traverse_got_arg *arg;
4260 entry = (struct mips_got_entry *) *entryp;
4261 arg = (struct mips_elf_traverse_got_arg *) data;
4262 if (entry->abfd != NULL && entry->symndx == -1)
4264 struct mips_elf_link_hash_entry *h;
4266 h = entry->d.h;
4267 if (h->root.root.type == bfd_link_hash_indirect
4268 || h->root.root.type == bfd_link_hash_warning)
4270 arg->value = true;
4271 return 0;
4274 mips_elf_count_got_entry (arg->info, arg->g, entry);
4275 return 1;
4278 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4279 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4280 converting entries for indirect and warning symbols into entries
4281 for the target symbol. Set DATA->g to null on error. */
4283 static int
4284 mips_elf_recreate_got (void **entryp, void *data)
4286 struct mips_got_entry new_entry, *entry;
4287 struct mips_elf_traverse_got_arg *arg;
4288 void **slot;
4290 entry = (struct mips_got_entry *) *entryp;
4291 arg = (struct mips_elf_traverse_got_arg *) data;
4292 if (entry->abfd != NULL
4293 && entry->symndx == -1
4294 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4295 || entry->d.h->root.root.type == bfd_link_hash_warning))
4297 struct mips_elf_link_hash_entry *h;
4299 new_entry = *entry;
4300 entry = &new_entry;
4301 h = entry->d.h;
4304 BFD_ASSERT (h->global_got_area == GGA_NONE);
4305 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4307 while (h->root.root.type == bfd_link_hash_indirect
4308 || h->root.root.type == bfd_link_hash_warning);
4309 entry->d.h = h;
4311 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4312 if (slot == NULL)
4314 arg->g = NULL;
4315 return 0;
4317 if (*slot == NULL)
4319 if (entry == &new_entry)
4321 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4322 if (!entry)
4324 arg->g = NULL;
4325 return 0;
4327 *entry = new_entry;
4329 *slot = entry;
4330 mips_elf_count_got_entry (arg->info, arg->g, entry);
4332 return 1;
4335 /* Return the maximum number of GOT page entries required for RANGE. */
4337 static bfd_vma
4338 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4340 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4343 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4345 static bool
4346 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4347 asection *sec, bfd_signed_vma addend)
4349 struct mips_got_info *g = arg->g;
4350 struct mips_got_page_entry lookup, *entry;
4351 struct mips_got_page_range **range_ptr, *range;
4352 bfd_vma old_pages, new_pages;
4353 void **loc;
4355 /* Find the mips_got_page_entry hash table entry for this section. */
4356 lookup.sec = sec;
4357 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4358 if (loc == NULL)
4359 return false;
4361 /* Create a mips_got_page_entry if this is the first time we've
4362 seen the section. */
4363 entry = (struct mips_got_page_entry *) *loc;
4364 if (!entry)
4366 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4367 if (!entry)
4368 return false;
4370 entry->sec = sec;
4371 *loc = entry;
4374 /* Skip over ranges whose maximum extent cannot share a page entry
4375 with ADDEND. */
4376 range_ptr = &entry->ranges;
4377 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4378 range_ptr = &(*range_ptr)->next;
4380 /* If we scanned to the end of the list, or found a range whose
4381 minimum extent cannot share a page entry with ADDEND, create
4382 a new singleton range. */
4383 range = *range_ptr;
4384 if (!range || addend < range->min_addend - 0xffff)
4386 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4387 if (!range)
4388 return false;
4390 range->next = *range_ptr;
4391 range->min_addend = addend;
4392 range->max_addend = addend;
4394 *range_ptr = range;
4395 entry->num_pages++;
4396 g->page_gotno++;
4397 return true;
4400 /* Remember how many pages the old range contributed. */
4401 old_pages = mips_elf_pages_for_range (range);
4403 /* Update the ranges. */
4404 if (addend < range->min_addend)
4405 range->min_addend = addend;
4406 else if (addend > range->max_addend)
4408 if (range->next && addend >= range->next->min_addend - 0xffff)
4410 old_pages += mips_elf_pages_for_range (range->next);
4411 range->max_addend = range->next->max_addend;
4412 range->next = range->next->next;
4414 else
4415 range->max_addend = addend;
4418 /* Record any change in the total estimate. */
4419 new_pages = mips_elf_pages_for_range (range);
4420 if (old_pages != new_pages)
4422 entry->num_pages += new_pages - old_pages;
4423 g->page_gotno += new_pages - old_pages;
4426 return true;
4429 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4430 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4431 whether the page reference described by *REFP needs a GOT page entry,
4432 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4434 static int
4435 mips_elf_resolve_got_page_ref (void **refp, void *data)
4437 struct mips_got_page_ref *ref;
4438 struct mips_elf_traverse_got_arg *arg;
4439 struct mips_elf_link_hash_table *htab;
4440 asection *sec;
4441 bfd_vma addend;
4443 ref = (struct mips_got_page_ref *) *refp;
4444 arg = (struct mips_elf_traverse_got_arg *) data;
4445 htab = mips_elf_hash_table (arg->info);
4447 if (ref->symndx < 0)
4449 struct mips_elf_link_hash_entry *h;
4451 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4452 h = ref->u.h;
4453 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4454 return 1;
4456 /* Ignore undefined symbols; we'll issue an error later if
4457 appropriate. */
4458 if (!((h->root.root.type == bfd_link_hash_defined
4459 || h->root.root.type == bfd_link_hash_defweak)
4460 && h->root.root.u.def.section))
4461 return 1;
4463 sec = h->root.root.u.def.section;
4464 addend = h->root.root.u.def.value + ref->addend;
4466 else
4468 Elf_Internal_Sym *isym;
4470 /* Read in the symbol. */
4471 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4472 ref->symndx);
4473 if (isym == NULL)
4475 arg->g = NULL;
4476 return 0;
4479 /* Get the associated input section. */
4480 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4481 if (sec == NULL)
4483 arg->g = NULL;
4484 return 0;
4487 /* If this is a mergable section, work out the section and offset
4488 of the merged data. For section symbols, the addend specifies
4489 of the offset _of_ the first byte in the data, otherwise it
4490 specifies the offset _from_ the first byte. */
4491 if (sec->flags & SEC_MERGE)
4493 void *secinfo;
4495 secinfo = elf_section_data (sec)->sec_info;
4496 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4497 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4498 isym->st_value + ref->addend);
4499 else
4500 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4501 isym->st_value) + ref->addend;
4503 else
4504 addend = isym->st_value + ref->addend;
4506 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4508 arg->g = NULL;
4509 return 0;
4511 return 1;
4514 /* If any entries in G->got_entries are for indirect or warning symbols,
4515 replace them with entries for the target symbol. Convert g->got_page_refs
4516 into got_page_entry structures and estimate the number of page entries
4517 that they require. */
4519 static bool
4520 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4521 struct mips_got_info *g)
4523 struct mips_elf_traverse_got_arg tga;
4524 struct mips_got_info oldg;
4526 oldg = *g;
4528 tga.info = info;
4529 tga.g = g;
4530 tga.value = false;
4531 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4532 if (tga.value)
4534 *g = oldg;
4535 g->got_entries = htab_create (htab_size (oldg.got_entries),
4536 mips_elf_got_entry_hash,
4537 mips_elf_got_entry_eq, NULL);
4538 if (!g->got_entries)
4539 return false;
4541 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4542 if (!tga.g)
4543 return false;
4545 htab_delete (oldg.got_entries);
4548 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4549 mips_got_page_entry_eq, NULL);
4550 if (g->got_page_entries == NULL)
4551 return false;
4553 tga.info = info;
4554 tga.g = g;
4555 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4557 return true;
4560 /* Return true if a GOT entry for H should live in the local rather than
4561 global GOT area. */
4563 static bool
4564 mips_use_local_got_p (struct bfd_link_info *info,
4565 struct mips_elf_link_hash_entry *h)
4567 /* Symbols that aren't in the dynamic symbol table must live in the
4568 local GOT. This includes symbols that are completely undefined
4569 and which therefore don't bind locally. We'll report undefined
4570 symbols later if appropriate. */
4571 if (h->root.dynindx == -1)
4572 return true;
4574 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4575 to the local GOT, as they would be implicitly relocated by the
4576 base address by the dynamic loader. */
4577 if (bfd_is_abs_symbol (&h->root.root))
4578 return false;
4580 /* Symbols that bind locally can (and in the case of forced-local
4581 symbols, must) live in the local GOT. */
4582 if (h->got_only_for_calls
4583 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4584 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4585 return true;
4587 /* If this is an executable that must provide a definition of the symbol,
4588 either though PLTs or copy relocations, then that address should go in
4589 the local rather than global GOT. */
4590 if (bfd_link_executable (info) && h->has_static_relocs)
4591 return true;
4593 return false;
4596 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4597 link_info structure. Decide whether the hash entry needs an entry in
4598 the global part of the primary GOT, setting global_got_area accordingly.
4599 Count the number of global symbols that are in the primary GOT only
4600 because they have relocations against them (reloc_only_gotno). */
4602 static bool
4603 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4605 struct bfd_link_info *info;
4606 struct mips_elf_link_hash_table *htab;
4607 struct mips_got_info *g;
4609 info = (struct bfd_link_info *) data;
4610 htab = mips_elf_hash_table (info);
4611 g = htab->got_info;
4612 if (h->global_got_area != GGA_NONE)
4614 /* Make a final decision about whether the symbol belongs in the
4615 local or global GOT. */
4616 if (mips_use_local_got_p (info, h))
4617 /* The symbol belongs in the local GOT. We no longer need this
4618 entry if it was only used for relocations; those relocations
4619 will be against the null or section symbol instead of H. */
4620 h->global_got_area = GGA_NONE;
4621 else if (htab->root.target_os == is_vxworks
4622 && h->got_only_for_calls
4623 && h->root.plt.plist->mips_offset != MINUS_ONE)
4624 /* On VxWorks, calls can refer directly to the .got.plt entry;
4625 they don't need entries in the regular GOT. .got.plt entries
4626 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4627 h->global_got_area = GGA_NONE;
4628 else if (h->global_got_area == GGA_RELOC_ONLY)
4630 g->reloc_only_gotno++;
4631 g->global_gotno++;
4634 return 1;
4637 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4638 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4640 static int
4641 mips_elf_add_got_entry (void **entryp, void *data)
4643 struct mips_got_entry *entry;
4644 struct mips_elf_traverse_got_arg *arg;
4645 void **slot;
4647 entry = (struct mips_got_entry *) *entryp;
4648 arg = (struct mips_elf_traverse_got_arg *) data;
4649 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4650 if (!slot)
4652 arg->g = NULL;
4653 return 0;
4655 if (!*slot)
4657 *slot = entry;
4658 mips_elf_count_got_entry (arg->info, arg->g, entry);
4660 return 1;
4663 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4664 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4666 static int
4667 mips_elf_add_got_page_entry (void **entryp, void *data)
4669 struct mips_got_page_entry *entry;
4670 struct mips_elf_traverse_got_arg *arg;
4671 void **slot;
4673 entry = (struct mips_got_page_entry *) *entryp;
4674 arg = (struct mips_elf_traverse_got_arg *) data;
4675 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4676 if (!slot)
4678 arg->g = NULL;
4679 return 0;
4681 if (!*slot)
4683 *slot = entry;
4684 arg->g->page_gotno += entry->num_pages;
4686 return 1;
4689 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4690 this would lead to overflow, 1 if they were merged successfully,
4691 and 0 if a merge failed due to lack of memory. (These values are chosen
4692 so that nonnegative return values can be returned by a htab_traverse
4693 callback.) */
4695 static int
4696 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4697 struct mips_got_info *to,
4698 struct mips_elf_got_per_bfd_arg *arg)
4700 struct mips_elf_traverse_got_arg tga;
4701 unsigned int estimate;
4703 /* Work out how many page entries we would need for the combined GOT. */
4704 estimate = arg->max_pages;
4705 if (estimate >= from->page_gotno + to->page_gotno)
4706 estimate = from->page_gotno + to->page_gotno;
4708 /* And conservatively estimate how many local and TLS entries
4709 would be needed. */
4710 estimate += from->local_gotno + to->local_gotno;
4711 estimate += from->tls_gotno + to->tls_gotno;
4713 /* If we're merging with the primary got, any TLS relocations will
4714 come after the full set of global entries. Otherwise estimate those
4715 conservatively as well. */
4716 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4717 estimate += arg->global_count;
4718 else
4719 estimate += from->global_gotno + to->global_gotno;
4721 /* Bail out if the combined GOT might be too big. */
4722 if (estimate > arg->max_count)
4723 return -1;
4725 /* Transfer the bfd's got information from FROM to TO. */
4726 tga.info = arg->info;
4727 tga.g = to;
4728 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4729 if (!tga.g)
4730 return 0;
4732 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4733 if (!tga.g)
4734 return 0;
4736 mips_elf_replace_bfd_got (abfd, to);
4737 return 1;
4740 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4741 as possible of the primary got, since it doesn't require explicit
4742 dynamic relocations, but don't use bfds that would reference global
4743 symbols out of the addressable range. Failing the primary got,
4744 attempt to merge with the current got, or finish the current got
4745 and then make make the new got current. */
4747 static bool
4748 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4749 struct mips_elf_got_per_bfd_arg *arg)
4751 unsigned int estimate;
4752 int result;
4754 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4755 return false;
4757 /* Work out the number of page, local and TLS entries. */
4758 estimate = arg->max_pages;
4759 if (estimate > g->page_gotno)
4760 estimate = g->page_gotno;
4761 estimate += g->local_gotno + g->tls_gotno;
4763 /* We place TLS GOT entries after both locals and globals. The globals
4764 for the primary GOT may overflow the normal GOT size limit, so be
4765 sure not to merge a GOT which requires TLS with the primary GOT in that
4766 case. This doesn't affect non-primary GOTs. */
4767 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4769 if (estimate <= arg->max_count)
4771 /* If we don't have a primary GOT, use it as
4772 a starting point for the primary GOT. */
4773 if (!arg->primary)
4775 arg->primary = g;
4776 return true;
4779 /* Try merging with the primary GOT. */
4780 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4781 if (result >= 0)
4782 return result;
4785 /* If we can merge with the last-created got, do it. */
4786 if (arg->current)
4788 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4789 if (result >= 0)
4790 return result;
4793 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4794 fits; if it turns out that it doesn't, we'll get relocation
4795 overflows anyway. */
4796 g->next = arg->current;
4797 arg->current = g;
4799 return true;
4802 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4803 to GOTIDX, duplicating the entry if it has already been assigned
4804 an index in a different GOT. */
4806 static bool
4807 mips_elf_set_gotidx (void **entryp, long gotidx)
4809 struct mips_got_entry *entry;
4811 entry = (struct mips_got_entry *) *entryp;
4812 if (entry->gotidx > 0)
4814 struct mips_got_entry *new_entry;
4816 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4817 if (!new_entry)
4818 return false;
4820 *new_entry = *entry;
4821 *entryp = new_entry;
4822 entry = new_entry;
4824 entry->gotidx = gotidx;
4825 return true;
4828 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4829 mips_elf_traverse_got_arg in which DATA->value is the size of one
4830 GOT entry. Set DATA->g to null on failure. */
4832 static int
4833 mips_elf_initialize_tls_index (void **entryp, void *data)
4835 struct mips_got_entry *entry;
4836 struct mips_elf_traverse_got_arg *arg;
4838 /* We're only interested in TLS symbols. */
4839 entry = (struct mips_got_entry *) *entryp;
4840 if (entry->tls_type == GOT_TLS_NONE)
4841 return 1;
4843 arg = (struct mips_elf_traverse_got_arg *) data;
4844 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4846 arg->g = NULL;
4847 return 0;
4850 /* Account for the entries we've just allocated. */
4851 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4852 return 1;
4855 /* A htab_traverse callback for GOT entries, where DATA points to a
4856 mips_elf_traverse_got_arg. Set the global_got_area of each global
4857 symbol to DATA->value. */
4859 static int
4860 mips_elf_set_global_got_area (void **entryp, void *data)
4862 struct mips_got_entry *entry;
4863 struct mips_elf_traverse_got_arg *arg;
4865 entry = (struct mips_got_entry *) *entryp;
4866 arg = (struct mips_elf_traverse_got_arg *) data;
4867 if (entry->abfd != NULL
4868 && entry->symndx == -1
4869 && entry->d.h->global_got_area != GGA_NONE)
4870 entry->d.h->global_got_area = arg->value;
4871 return 1;
4874 /* A htab_traverse callback for secondary GOT entries, where DATA points
4875 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4876 and record the number of relocations they require. DATA->value is
4877 the size of one GOT entry. Set DATA->g to null on failure. */
4879 static int
4880 mips_elf_set_global_gotidx (void **entryp, void *data)
4882 struct mips_got_entry *entry;
4883 struct mips_elf_traverse_got_arg *arg;
4885 entry = (struct mips_got_entry *) *entryp;
4886 arg = (struct mips_elf_traverse_got_arg *) data;
4887 if (entry->abfd != NULL
4888 && entry->symndx == -1
4889 && entry->d.h->global_got_area != GGA_NONE)
4891 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4893 arg->g = NULL;
4894 return 0;
4896 arg->g->assigned_low_gotno += 1;
4898 if (bfd_link_pic (arg->info)
4899 || (elf_hash_table (arg->info)->dynamic_sections_created
4900 && entry->d.h->root.def_dynamic
4901 && !entry->d.h->root.def_regular))
4902 arg->g->relocs += 1;
4905 return 1;
4908 /* A htab_traverse callback for GOT entries for which DATA is the
4909 bfd_link_info. Forbid any global symbols from having traditional
4910 lazy-binding stubs. */
4912 static int
4913 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4915 struct bfd_link_info *info;
4916 struct mips_elf_link_hash_table *htab;
4917 struct mips_got_entry *entry;
4919 entry = (struct mips_got_entry *) *entryp;
4920 info = (struct bfd_link_info *) data;
4921 htab = mips_elf_hash_table (info);
4922 BFD_ASSERT (htab != NULL);
4924 if (entry->abfd != NULL
4925 && entry->symndx == -1
4926 && entry->d.h->needs_lazy_stub)
4928 entry->d.h->needs_lazy_stub = false;
4929 htab->lazy_stub_count--;
4932 return 1;
4935 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4936 the primary GOT. */
4937 static bfd_vma
4938 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4940 if (!g->next)
4941 return 0;
4943 g = mips_elf_bfd_got (ibfd, false);
4944 if (! g)
4945 return 0;
4947 BFD_ASSERT (g->next);
4949 g = g->next;
4951 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4952 * MIPS_ELF_GOT_SIZE (abfd);
4955 /* Turn a single GOT that is too big for 16-bit addressing into
4956 a sequence of GOTs, each one 16-bit addressable. */
4958 static bool
4959 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4960 asection *got, bfd_size_type pages)
4962 struct mips_elf_link_hash_table *htab;
4963 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4964 struct mips_elf_traverse_got_arg tga;
4965 struct mips_got_info *g, *gg;
4966 unsigned int assign, needed_relocs;
4967 bfd *dynobj, *ibfd;
4969 dynobj = elf_hash_table (info)->dynobj;
4970 htab = mips_elf_hash_table (info);
4971 BFD_ASSERT (htab != NULL);
4973 g = htab->got_info;
4975 got_per_bfd_arg.obfd = abfd;
4976 got_per_bfd_arg.info = info;
4977 got_per_bfd_arg.current = NULL;
4978 got_per_bfd_arg.primary = NULL;
4979 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4980 / MIPS_ELF_GOT_SIZE (abfd))
4981 - htab->reserved_gotno);
4982 got_per_bfd_arg.max_pages = pages;
4983 /* The number of globals that will be included in the primary GOT.
4984 See the calls to mips_elf_set_global_got_area below for more
4985 information. */
4986 got_per_bfd_arg.global_count = g->global_gotno;
4988 /* Try to merge the GOTs of input bfds together, as long as they
4989 don't seem to exceed the maximum GOT size, choosing one of them
4990 to be the primary GOT. */
4991 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4993 gg = mips_elf_bfd_got (ibfd, false);
4994 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4995 return false;
4998 /* If we do not find any suitable primary GOT, create an empty one. */
4999 if (got_per_bfd_arg.primary == NULL)
5000 g->next = mips_elf_create_got_info (abfd);
5001 else
5002 g->next = got_per_bfd_arg.primary;
5003 g->next->next = got_per_bfd_arg.current;
5005 /* GG is now the master GOT, and G is the primary GOT. */
5006 gg = g;
5007 g = g->next;
5009 /* Map the output bfd to the primary got. That's what we're going
5010 to use for bfds that use GOT16 or GOT_PAGE relocations that we
5011 didn't mark in check_relocs, and we want a quick way to find it.
5012 We can't just use gg->next because we're going to reverse the
5013 list. */
5014 mips_elf_replace_bfd_got (abfd, g);
5016 /* Every symbol that is referenced in a dynamic relocation must be
5017 present in the primary GOT, so arrange for them to appear after
5018 those that are actually referenced. */
5019 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5020 g->global_gotno = gg->global_gotno;
5022 tga.info = info;
5023 tga.value = GGA_RELOC_ONLY;
5024 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5025 tga.value = GGA_NORMAL;
5026 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5028 /* Now go through the GOTs assigning them offset ranges.
5029 [assigned_low_gotno, local_gotno[ will be set to the range of local
5030 entries in each GOT. We can then compute the end of a GOT by
5031 adding local_gotno to global_gotno. We reverse the list and make
5032 it circular since then we'll be able to quickly compute the
5033 beginning of a GOT, by computing the end of its predecessor. To
5034 avoid special cases for the primary GOT, while still preserving
5035 assertions that are valid for both single- and multi-got links,
5036 we arrange for the main got struct to have the right number of
5037 global entries, but set its local_gotno such that the initial
5038 offset of the primary GOT is zero. Remember that the primary GOT
5039 will become the last item in the circular linked list, so it
5040 points back to the master GOT. */
5041 gg->local_gotno = -g->global_gotno;
5042 gg->global_gotno = g->global_gotno;
5043 gg->tls_gotno = 0;
5044 assign = 0;
5045 gg->next = gg;
5049 struct mips_got_info *gn;
5051 assign += htab->reserved_gotno;
5052 g->assigned_low_gotno = assign;
5053 g->local_gotno += assign;
5054 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5055 g->assigned_high_gotno = g->local_gotno - 1;
5056 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5058 /* Take g out of the direct list, and push it onto the reversed
5059 list that gg points to. g->next is guaranteed to be nonnull after
5060 this operation, as required by mips_elf_initialize_tls_index. */
5061 gn = g->next;
5062 g->next = gg->next;
5063 gg->next = g;
5065 /* Set up any TLS entries. We always place the TLS entries after
5066 all non-TLS entries. */
5067 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5068 tga.g = g;
5069 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5070 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5071 if (!tga.g)
5072 return false;
5073 BFD_ASSERT (g->tls_assigned_gotno == assign);
5075 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
5076 g = gn;
5078 /* Forbid global symbols in every non-primary GOT from having
5079 lazy-binding stubs. */
5080 if (g)
5081 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5083 while (g);
5085 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5087 needed_relocs = 0;
5088 for (g = gg->next; g && g->next != gg; g = g->next)
5090 unsigned int save_assign;
5092 /* Assign offsets to global GOT entries and count how many
5093 relocations they need. */
5094 save_assign = g->assigned_low_gotno;
5095 g->assigned_low_gotno = g->local_gotno;
5096 tga.info = info;
5097 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5098 tga.g = g;
5099 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5100 if (!tga.g)
5101 return false;
5102 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5103 g->assigned_low_gotno = save_assign;
5105 if (bfd_link_pic (info))
5107 g->relocs += g->local_gotno - g->assigned_low_gotno;
5108 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5109 + g->next->global_gotno
5110 + g->next->tls_gotno
5111 + htab->reserved_gotno);
5113 needed_relocs += g->relocs;
5115 needed_relocs += g->relocs;
5117 if (needed_relocs)
5118 mips_elf_allocate_dynamic_relocations (dynobj, info,
5119 needed_relocs);
5121 return true;
5125 /* Returns the first relocation of type r_type found, beginning with
5126 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5128 static const Elf_Internal_Rela *
5129 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5130 const Elf_Internal_Rela *relocation,
5131 const Elf_Internal_Rela *relend)
5133 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5135 while (relocation < relend)
5137 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5138 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5139 return relocation;
5141 ++relocation;
5144 /* We didn't find it. */
5145 return NULL;
5148 /* Return whether an input relocation is against a local symbol. */
5150 static bool
5151 mips_elf_local_relocation_p (bfd *input_bfd,
5152 const Elf_Internal_Rela *relocation,
5153 asection **local_sections)
5155 unsigned long r_symndx;
5156 Elf_Internal_Shdr *symtab_hdr;
5157 size_t extsymoff;
5159 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5160 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5161 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5163 if (r_symndx < extsymoff)
5164 return true;
5165 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5166 return true;
5168 return false;
5171 /* Sign-extend VALUE, which has the indicated number of BITS. */
5173 bfd_vma
5174 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5176 if (value & ((bfd_vma) 1 << (bits - 1)))
5177 /* VALUE is negative. */
5178 value |= ((bfd_vma) - 1) << bits;
5180 return value;
5183 /* Return non-zero if the indicated VALUE has overflowed the maximum
5184 range expressible by a signed number with the indicated number of
5185 BITS. */
5187 static bool
5188 mips_elf_overflow_p (bfd_vma value, int bits)
5190 bfd_signed_vma svalue = (bfd_signed_vma) value;
5192 if (svalue > (1 << (bits - 1)) - 1)
5193 /* The value is too big. */
5194 return true;
5195 else if (svalue < -(1 << (bits - 1)))
5196 /* The value is too small. */
5197 return true;
5199 /* All is well. */
5200 return false;
5203 /* Calculate the %high function. */
5205 static bfd_vma
5206 mips_elf_high (bfd_vma value)
5208 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5211 /* Calculate the %higher function. */
5213 static bfd_vma
5214 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5216 #ifdef BFD64
5217 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5218 #else
5219 abort ();
5220 return MINUS_ONE;
5221 #endif
5224 /* Calculate the %highest function. */
5226 static bfd_vma
5227 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5229 #ifdef BFD64
5230 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5231 #else
5232 abort ();
5233 return MINUS_ONE;
5234 #endif
5237 /* Create the .compact_rel section. */
5239 static bool
5240 mips_elf_create_compact_rel_section
5241 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5243 flagword flags;
5244 register asection *s;
5246 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5248 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5249 | SEC_READONLY);
5251 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5252 if (s == NULL
5253 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5254 return false;
5256 s->size = sizeof (Elf32_External_compact_rel);
5259 return true;
5262 /* Create the .got section to hold the global offset table. */
5264 static bool
5265 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5267 flagword flags;
5268 register asection *s;
5269 struct elf_link_hash_entry *h;
5270 struct bfd_link_hash_entry *bh;
5271 struct mips_elf_link_hash_table *htab;
5273 htab = mips_elf_hash_table (info);
5274 BFD_ASSERT (htab != NULL);
5276 /* This function may be called more than once. */
5277 if (htab->root.sgot)
5278 return true;
5280 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5281 | SEC_LINKER_CREATED);
5283 /* We have to use an alignment of 2**4 here because this is hardcoded
5284 in the function stub generation and in the linker script. */
5285 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5286 if (s == NULL
5287 || !bfd_set_section_alignment (s, 4))
5288 return false;
5289 htab->root.sgot = s;
5291 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5292 linker script because we don't want to define the symbol if we
5293 are not creating a global offset table. */
5294 bh = NULL;
5295 if (! (_bfd_generic_link_add_one_symbol
5296 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5297 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5298 return false;
5300 h = (struct elf_link_hash_entry *) bh;
5301 h->non_elf = 0;
5302 h->def_regular = 1;
5303 h->type = STT_OBJECT;
5304 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5305 elf_hash_table (info)->hgot = h;
5307 if (bfd_link_pic (info)
5308 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5309 return false;
5311 htab->got_info = mips_elf_create_got_info (abfd);
5312 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5313 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5315 /* We also need a .got.plt section when generating PLTs. */
5316 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5317 SEC_ALLOC | SEC_LOAD
5318 | SEC_HAS_CONTENTS
5319 | SEC_IN_MEMORY
5320 | SEC_LINKER_CREATED);
5321 if (s == NULL)
5322 return false;
5323 htab->root.sgotplt = s;
5325 return true;
5328 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5329 __GOTT_INDEX__ symbols. These symbols are only special for
5330 shared objects; they are not used in executables. */
5332 static bool
5333 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5335 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5336 && bfd_link_pic (info)
5337 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5338 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5341 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5342 require an la25 stub. See also mips_elf_local_pic_function_p,
5343 which determines whether the destination function ever requires a
5344 stub. */
5346 static bool
5347 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5348 bool target_is_16_bit_code_p)
5350 /* We specifically ignore branches and jumps from EF_PIC objects,
5351 where the onus is on the compiler or programmer to perform any
5352 necessary initialization of $25. Sometimes such initialization
5353 is unnecessary; for example, -mno-shared functions do not use
5354 the incoming value of $25, and may therefore be called directly. */
5355 if (PIC_OBJECT_P (input_bfd))
5356 return false;
5358 switch (r_type)
5360 case R_MIPS_26:
5361 case R_MIPS_PC16:
5362 case R_MIPS_PC21_S2:
5363 case R_MIPS_PC26_S2:
5364 case R_MICROMIPS_26_S1:
5365 case R_MICROMIPS_PC7_S1:
5366 case R_MICROMIPS_PC10_S1:
5367 case R_MICROMIPS_PC16_S1:
5368 case R_MICROMIPS_PC23_S2:
5369 return true;
5371 case R_MIPS16_26:
5372 return !target_is_16_bit_code_p;
5374 default:
5375 return false;
5379 /* Obtain the field relocated by RELOCATION. */
5381 static bfd_vma
5382 mips_elf_obtain_contents (reloc_howto_type *howto,
5383 const Elf_Internal_Rela *relocation,
5384 bfd *input_bfd, bfd_byte *contents)
5386 bfd_vma x = 0;
5387 bfd_byte *location = contents + relocation->r_offset;
5388 unsigned int size = bfd_get_reloc_size (howto);
5390 /* Obtain the bytes. */
5391 if (size != 0)
5392 x = bfd_get (8 * size, input_bfd, location);
5394 return x;
5397 /* Store the field relocated by RELOCATION. */
5399 static void
5400 mips_elf_store_contents (reloc_howto_type *howto,
5401 const Elf_Internal_Rela *relocation,
5402 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5404 bfd_byte *location = contents + relocation->r_offset;
5405 unsigned int size = bfd_get_reloc_size (howto);
5407 /* Put the value into the output. */
5408 if (size != 0)
5409 bfd_put (8 * size, input_bfd, x, location);
5412 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5413 RELOCATION described by HOWTO, with a move of 0 to the load target
5414 register, returning TRUE if that is successful and FALSE otherwise.
5415 If DOIT is FALSE, then only determine it patching is possible and
5416 return status without actually changing CONTENTS.
5419 static bool
5420 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5421 const Elf_Internal_Rela *relocation,
5422 reloc_howto_type *howto, bool doit)
5424 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5425 bfd_byte *location = contents + relocation->r_offset;
5426 bool nullified = true;
5427 bfd_vma x;
5429 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5431 /* Obtain the current value. */
5432 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5434 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5435 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5436 if (mips16_reloc_p (r_type)
5437 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5438 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5439 x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
5440 else if (micromips_reloc_p (r_type)
5441 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5442 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5443 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5444 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5445 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5446 else
5447 nullified = false;
5449 /* Put the value into the output. */
5450 if (doit && nullified)
5451 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5453 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5455 return nullified;
5458 /* Calculate the value produced by the RELOCATION (which comes from
5459 the INPUT_BFD). The ADDEND is the addend to use for this
5460 RELOCATION; RELOCATION->R_ADDEND is ignored.
5462 The result of the relocation calculation is stored in VALUEP.
5463 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5464 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5466 This function returns bfd_reloc_continue if the caller need take no
5467 further action regarding this relocation, bfd_reloc_notsupported if
5468 something goes dramatically wrong, bfd_reloc_overflow if an
5469 overflow occurs, and bfd_reloc_ok to indicate success. */
5471 static bfd_reloc_status_type
5472 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5473 asection *input_section, bfd_byte *contents,
5474 struct bfd_link_info *info,
5475 const Elf_Internal_Rela *relocation,
5476 bfd_vma addend, reloc_howto_type *howto,
5477 Elf_Internal_Sym *local_syms,
5478 asection **local_sections, bfd_vma *valuep,
5479 const char **namep,
5480 bool *cross_mode_jump_p,
5481 bool save_addend)
5483 /* The eventual value we will return. */
5484 bfd_vma value;
5485 /* The address of the symbol against which the relocation is
5486 occurring. */
5487 bfd_vma symbol = 0;
5488 /* The final GP value to be used for the relocatable, executable, or
5489 shared object file being produced. */
5490 bfd_vma gp;
5491 /* The place (section offset or address) of the storage unit being
5492 relocated. */
5493 bfd_vma p;
5494 /* The value of GP used to create the relocatable object. */
5495 bfd_vma gp0;
5496 /* The offset into the global offset table at which the address of
5497 the relocation entry symbol, adjusted by the addend, resides
5498 during execution. */
5499 bfd_vma g = MINUS_ONE;
5500 /* The section in which the symbol referenced by the relocation is
5501 located. */
5502 asection *sec = NULL;
5503 struct mips_elf_link_hash_entry *h = NULL;
5504 /* TRUE if the symbol referred to by this relocation is a local
5505 symbol. */
5506 bool local_p, was_local_p;
5507 /* TRUE if the symbol referred to by this relocation is a section
5508 symbol. */
5509 bool section_p = false;
5510 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5511 bool gp_disp_p = false;
5512 /* TRUE if the symbol referred to by this relocation is
5513 "__gnu_local_gp". */
5514 bool gnu_local_gp_p = false;
5515 Elf_Internal_Shdr *symtab_hdr;
5516 size_t extsymoff;
5517 unsigned long r_symndx;
5518 int r_type;
5519 /* TRUE if overflow occurred during the calculation of the
5520 relocation value. */
5521 bool overflowed_p;
5522 /* TRUE if this relocation refers to a MIPS16 function. */
5523 bool target_is_16_bit_code_p = false;
5524 bool target_is_micromips_code_p = false;
5525 struct mips_elf_link_hash_table *htab;
5526 bfd *dynobj;
5527 bool resolved_to_zero;
5529 dynobj = elf_hash_table (info)->dynobj;
5530 htab = mips_elf_hash_table (info);
5531 BFD_ASSERT (htab != NULL);
5533 /* Parse the relocation. */
5534 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5535 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5536 p = (input_section->output_section->vma
5537 + input_section->output_offset
5538 + relocation->r_offset);
5540 /* Assume that there will be no overflow. */
5541 overflowed_p = false;
5543 /* Figure out whether or not the symbol is local, and get the offset
5544 used in the array of hash table entries. */
5545 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5546 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5547 local_sections);
5548 was_local_p = local_p;
5549 if (! elf_bad_symtab (input_bfd))
5550 extsymoff = symtab_hdr->sh_info;
5551 else
5553 /* The symbol table does not follow the rule that local symbols
5554 must come before globals. */
5555 extsymoff = 0;
5558 /* Figure out the value of the symbol. */
5559 if (local_p)
5561 bool micromips_p = MICROMIPS_P (abfd);
5562 Elf_Internal_Sym *sym;
5564 sym = local_syms + r_symndx;
5565 sec = local_sections[r_symndx];
5567 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5569 symbol = sec->output_section->vma + sec->output_offset;
5570 if (!section_p || (sec->flags & SEC_MERGE))
5571 symbol += sym->st_value;
5572 if ((sec->flags & SEC_MERGE) && section_p)
5574 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5575 addend -= symbol;
5576 addend += sec->output_section->vma + sec->output_offset;
5579 /* MIPS16/microMIPS text labels should be treated as odd. */
5580 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5581 ++symbol;
5583 /* Record the name of this symbol, for our caller. */
5584 *namep = bfd_elf_string_from_elf_section (input_bfd,
5585 symtab_hdr->sh_link,
5586 sym->st_name);
5587 if (*namep == NULL || **namep == '\0')
5588 *namep = bfd_section_name (sec);
5590 /* For relocations against a section symbol and ones against no
5591 symbol (absolute relocations) infer the ISA mode from the addend. */
5592 if (section_p || r_symndx == STN_UNDEF)
5594 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5595 target_is_micromips_code_p = (addend & 1) && micromips_p;
5597 /* For relocations against an absolute symbol infer the ISA mode
5598 from the value of the symbol plus addend. */
5599 else if (bfd_is_abs_section (sec))
5601 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5602 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5604 /* Otherwise just use the regular symbol annotation available. */
5605 else
5607 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5608 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5611 else
5613 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5615 /* For global symbols we look up the symbol in the hash-table. */
5616 h = ((struct mips_elf_link_hash_entry *)
5617 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5618 /* Find the real hash-table entry for this symbol. */
5619 while (h->root.root.type == bfd_link_hash_indirect
5620 || h->root.root.type == bfd_link_hash_warning)
5621 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5623 /* Record the name of this symbol, for our caller. */
5624 *namep = h->root.root.root.string;
5626 /* See if this is the special _gp_disp symbol. Note that such a
5627 symbol must always be a global symbol. */
5628 if (strcmp (*namep, "_gp_disp") == 0
5629 && ! NEWABI_P (input_bfd))
5631 /* Relocations against _gp_disp are permitted only with
5632 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5633 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5634 return bfd_reloc_notsupported;
5636 gp_disp_p = true;
5638 /* See if this is the special _gp symbol. Note that such a
5639 symbol must always be a global symbol. */
5640 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5641 gnu_local_gp_p = true;
5644 /* If this symbol is defined, calculate its address. Note that
5645 _gp_disp is a magic symbol, always implicitly defined by the
5646 linker, so it's inappropriate to check to see whether or not
5647 its defined. */
5648 else if ((h->root.root.type == bfd_link_hash_defined
5649 || h->root.root.type == bfd_link_hash_defweak)
5650 && h->root.root.u.def.section)
5652 sec = h->root.root.u.def.section;
5653 if (sec->output_section)
5654 symbol = (h->root.root.u.def.value
5655 + sec->output_section->vma
5656 + sec->output_offset);
5657 else
5658 symbol = h->root.root.u.def.value;
5660 else if (h->root.root.type == bfd_link_hash_undefweak)
5661 /* We allow relocations against undefined weak symbols, giving
5662 it the value zero, so that you can undefined weak functions
5663 and check to see if they exist by looking at their
5664 addresses. */
5665 symbol = 0;
5666 else if (info->unresolved_syms_in_objects == RM_IGNORE
5667 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5668 symbol = 0;
5669 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5670 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5672 /* If this is a dynamic link, we should have created a
5673 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5674 in _bfd_mips_elf_create_dynamic_sections.
5675 Otherwise, we should define the symbol with a value of 0.
5676 FIXME: It should probably get into the symbol table
5677 somehow as well. */
5678 BFD_ASSERT (! bfd_link_pic (info));
5679 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5680 symbol = 0;
5682 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5684 /* This is an optional symbol - an Irix specific extension to the
5685 ELF spec. Ignore it for now.
5686 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5687 than simply ignoring them, but we do not handle this for now.
5688 For information see the "64-bit ELF Object File Specification"
5689 which is available from here:
5690 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5691 symbol = 0;
5693 else
5695 bool reject_undefined
5696 = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5697 && !info->warn_unresolved_syms)
5698 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5700 info->callbacks->undefined_symbol
5701 (info, h->root.root.root.string, input_bfd,
5702 input_section, relocation->r_offset, reject_undefined);
5704 if (reject_undefined)
5705 return bfd_reloc_undefined;
5707 symbol = 0;
5710 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5711 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5714 /* If this is a reference to a 16-bit function with a stub, we need
5715 to redirect the relocation to the stub unless:
5717 (a) the relocation is for a MIPS16 JAL;
5719 (b) the relocation is for a MIPS16 PIC call, and there are no
5720 non-MIPS16 uses of the GOT slot; or
5722 (c) the section allows direct references to MIPS16 functions. */
5723 if (r_type != R_MIPS16_26
5724 && !bfd_link_relocatable (info)
5725 && ((h != NULL
5726 && h->fn_stub != NULL
5727 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5728 || (local_p
5729 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5730 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5731 && !section_allows_mips16_refs_p (input_section))
5733 /* This is a 32- or 64-bit call to a 16-bit function. We should
5734 have already noticed that we were going to need the
5735 stub. */
5736 if (local_p)
5738 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5739 value = 0;
5741 else
5743 BFD_ASSERT (h->need_fn_stub);
5744 if (h->la25_stub)
5746 /* If a LA25 header for the stub itself exists, point to the
5747 prepended LUI/ADDIU sequence. */
5748 sec = h->la25_stub->stub_section;
5749 value = h->la25_stub->offset;
5751 else
5753 sec = h->fn_stub;
5754 value = 0;
5758 symbol = sec->output_section->vma + sec->output_offset + value;
5759 /* The target is 16-bit, but the stub isn't. */
5760 target_is_16_bit_code_p = false;
5762 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5763 to a standard MIPS function, we need to redirect the call to the stub.
5764 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5765 indirect calls should use an indirect stub instead. */
5766 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5767 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5768 || (local_p
5769 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5770 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5771 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5773 if (local_p)
5774 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5775 else
5777 /* If both call_stub and call_fp_stub are defined, we can figure
5778 out which one to use by checking which one appears in the input
5779 file. */
5780 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5782 asection *o;
5784 sec = NULL;
5785 for (o = input_bfd->sections; o != NULL; o = o->next)
5787 if (CALL_FP_STUB_P (bfd_section_name (o)))
5789 sec = h->call_fp_stub;
5790 break;
5793 if (sec == NULL)
5794 sec = h->call_stub;
5796 else if (h->call_stub != NULL)
5797 sec = h->call_stub;
5798 else
5799 sec = h->call_fp_stub;
5802 BFD_ASSERT (sec->size > 0);
5803 symbol = sec->output_section->vma + sec->output_offset;
5805 /* If this is a direct call to a PIC function, redirect to the
5806 non-PIC stub. */
5807 else if (h != NULL && h->la25_stub
5808 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5809 target_is_16_bit_code_p))
5811 symbol = (h->la25_stub->stub_section->output_section->vma
5812 + h->la25_stub->stub_section->output_offset
5813 + h->la25_stub->offset);
5814 if (ELF_ST_IS_MICROMIPS (h->root.other))
5815 symbol |= 1;
5817 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5818 entry is used if a standard PLT entry has also been made. In this
5819 case the symbol will have been set by mips_elf_set_plt_sym_value
5820 to point to the standard PLT entry, so redirect to the compressed
5821 one. */
5822 else if ((mips16_branch_reloc_p (r_type)
5823 || micromips_branch_reloc_p (r_type))
5824 && !bfd_link_relocatable (info)
5825 && h != NULL
5826 && h->use_plt_entry
5827 && h->root.plt.plist->comp_offset != MINUS_ONE
5828 && h->root.plt.plist->mips_offset != MINUS_ONE)
5830 bool micromips_p = MICROMIPS_P (abfd);
5832 sec = htab->root.splt;
5833 symbol = (sec->output_section->vma
5834 + sec->output_offset
5835 + htab->plt_header_size
5836 + htab->plt_mips_offset
5837 + h->root.plt.plist->comp_offset
5838 + 1);
5840 target_is_16_bit_code_p = !micromips_p;
5841 target_is_micromips_code_p = micromips_p;
5844 /* Make sure MIPS16 and microMIPS are not used together. */
5845 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5846 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5848 _bfd_error_handler
5849 (_("MIPS16 and microMIPS functions cannot call each other"));
5850 return bfd_reloc_notsupported;
5853 /* Calls from 16-bit code to 32-bit code and vice versa require the
5854 mode change. However, we can ignore calls to undefined weak symbols,
5855 which should never be executed at runtime. This exception is important
5856 because the assembly writer may have "known" that any definition of the
5857 symbol would be 16-bit code, and that direct jumps were therefore
5858 acceptable. */
5859 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5860 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5861 && ((mips16_branch_reloc_p (r_type)
5862 && !target_is_16_bit_code_p)
5863 || (micromips_branch_reloc_p (r_type)
5864 && !target_is_micromips_code_p)
5865 || ((branch_reloc_p (r_type)
5866 || r_type == R_MIPS_JALR)
5867 && (target_is_16_bit_code_p
5868 || target_is_micromips_code_p))));
5870 resolved_to_zero = (h != NULL
5871 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5873 switch (r_type)
5875 case R_MIPS16_CALL16:
5876 case R_MIPS16_GOT16:
5877 case R_MIPS_CALL16:
5878 case R_MIPS_GOT16:
5879 case R_MIPS_GOT_PAGE:
5880 case R_MIPS_GOT_DISP:
5881 case R_MIPS_GOT_LO16:
5882 case R_MIPS_CALL_LO16:
5883 case R_MICROMIPS_CALL16:
5884 case R_MICROMIPS_GOT16:
5885 case R_MICROMIPS_GOT_PAGE:
5886 case R_MICROMIPS_GOT_DISP:
5887 case R_MICROMIPS_GOT_LO16:
5888 case R_MICROMIPS_CALL_LO16:
5889 if (resolved_to_zero
5890 && !bfd_link_relocatable (info)
5891 && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5892 relocation->r_offset)
5893 && mips_elf_nullify_got_load (input_bfd, contents,
5894 relocation, howto, true))
5895 return bfd_reloc_continue;
5897 /* Fall through. */
5898 case R_MIPS_GOT_HI16:
5899 case R_MIPS_CALL_HI16:
5900 case R_MICROMIPS_GOT_HI16:
5901 case R_MICROMIPS_CALL_HI16:
5902 if (resolved_to_zero
5903 && htab->use_absolute_zero
5904 && bfd_link_pic (info))
5906 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5907 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5908 false, false, false);
5909 BFD_ASSERT (h != NULL);
5911 break;
5914 local_p = (h == NULL || mips_use_local_got_p (info, h));
5916 gp0 = _bfd_get_gp_value (input_bfd);
5917 gp = _bfd_get_gp_value (abfd);
5918 if (htab->got_info)
5919 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5921 if (gnu_local_gp_p)
5922 symbol = gp;
5924 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5925 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5926 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5927 if (got_page_reloc_p (r_type) && !local_p)
5929 r_type = (micromips_reloc_p (r_type)
5930 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5931 addend = 0;
5934 /* If we haven't already determined the GOT offset, and we're going
5935 to need it, get it now. */
5936 switch (r_type)
5938 case R_MIPS16_CALL16:
5939 case R_MIPS16_GOT16:
5940 case R_MIPS_CALL16:
5941 case R_MIPS_GOT16:
5942 case R_MIPS_GOT_DISP:
5943 case R_MIPS_GOT_HI16:
5944 case R_MIPS_CALL_HI16:
5945 case R_MIPS_GOT_LO16:
5946 case R_MIPS_CALL_LO16:
5947 case R_MICROMIPS_CALL16:
5948 case R_MICROMIPS_GOT16:
5949 case R_MICROMIPS_GOT_DISP:
5950 case R_MICROMIPS_GOT_HI16:
5951 case R_MICROMIPS_CALL_HI16:
5952 case R_MICROMIPS_GOT_LO16:
5953 case R_MICROMIPS_CALL_LO16:
5954 case R_MIPS_TLS_GD:
5955 case R_MIPS_TLS_GOTTPREL:
5956 case R_MIPS_TLS_LDM:
5957 case R_MIPS16_TLS_GD:
5958 case R_MIPS16_TLS_GOTTPREL:
5959 case R_MIPS16_TLS_LDM:
5960 case R_MICROMIPS_TLS_GD:
5961 case R_MICROMIPS_TLS_GOTTPREL:
5962 case R_MICROMIPS_TLS_LDM:
5963 /* Find the index into the GOT where this value is located. */
5964 if (tls_ldm_reloc_p (r_type))
5966 g = mips_elf_local_got_index (abfd, input_bfd, info,
5967 0, 0, NULL, r_type);
5968 if (g == MINUS_ONE)
5969 return bfd_reloc_outofrange;
5971 else if (!local_p)
5973 /* On VxWorks, CALL relocations should refer to the .got.plt
5974 entry, which is initialized to point at the PLT stub. */
5975 if (htab->root.target_os == is_vxworks
5976 && (call_hi16_reloc_p (r_type)
5977 || call_lo16_reloc_p (r_type)
5978 || call16_reloc_p (r_type)))
5980 BFD_ASSERT (addend == 0);
5981 BFD_ASSERT (h->root.needs_plt);
5982 g = mips_elf_gotplt_index (info, &h->root);
5984 else
5986 BFD_ASSERT (addend == 0);
5987 g = mips_elf_global_got_index (abfd, info, input_bfd,
5988 &h->root, r_type);
5989 if (!TLS_RELOC_P (r_type)
5990 && !elf_hash_table (info)->dynamic_sections_created)
5991 /* This is a static link. We must initialize the GOT entry. */
5992 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5995 else if (htab->root.target_os != is_vxworks
5996 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5997 /* The calculation below does not involve "g". */
5998 break;
5999 else
6001 g = mips_elf_local_got_index (abfd, input_bfd, info,
6002 symbol + addend, r_symndx, h, r_type);
6003 if (g == MINUS_ONE)
6004 return bfd_reloc_outofrange;
6007 /* Convert GOT indices to actual offsets. */
6008 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6009 break;
6012 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6013 symbols are resolved by the loader. Add them to .rela.dyn. */
6014 if (h != NULL && is_gott_symbol (info, &h->root))
6016 Elf_Internal_Rela outrel;
6017 bfd_byte *loc;
6018 asection *s;
6020 s = mips_elf_rel_dyn_section (info, false);
6021 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6023 outrel.r_offset = (input_section->output_section->vma
6024 + input_section->output_offset
6025 + relocation->r_offset);
6026 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6027 outrel.r_addend = addend;
6028 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6030 /* If we've written this relocation for a readonly section,
6031 we need to set DF_TEXTREL again, so that we do not delete the
6032 DT_TEXTREL tag. */
6033 if (MIPS_ELF_READONLY_SECTION (input_section))
6034 info->flags |= DF_TEXTREL;
6036 *valuep = 0;
6037 return bfd_reloc_ok;
6040 /* Figure out what kind of relocation is being performed. */
6041 switch (r_type)
6043 case R_MIPS_NONE:
6044 return bfd_reloc_continue;
6046 case R_MIPS_16:
6047 if (howto->partial_inplace)
6048 addend = _bfd_mips_elf_sign_extend (addend, 16);
6049 value = symbol + addend;
6050 overflowed_p = mips_elf_overflow_p (value, 16);
6051 break;
6053 case R_MIPS_32:
6054 case R_MIPS_REL32:
6055 case R_MIPS_64:
6056 if ((bfd_link_pic (info)
6057 || (htab->root.dynamic_sections_created
6058 && h != NULL
6059 && h->root.def_dynamic
6060 && !h->root.def_regular
6061 && !h->has_static_relocs))
6062 && r_symndx != STN_UNDEF
6063 && (h == NULL
6064 || h->root.root.type != bfd_link_hash_undefweak
6065 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6066 && !resolved_to_zero))
6067 && (input_section->flags & SEC_ALLOC) != 0)
6069 /* If we're creating a shared library, then we can't know
6070 where the symbol will end up. So, we create a relocation
6071 record in the output, and leave the job up to the dynamic
6072 linker. We must do the same for executable references to
6073 shared library symbols, unless we've decided to use copy
6074 relocs or PLTs instead. */
6075 value = addend;
6076 if (!mips_elf_create_dynamic_relocation (abfd,
6077 info,
6078 relocation,
6080 sec,
6081 symbol,
6082 &value,
6083 input_section))
6084 return bfd_reloc_undefined;
6086 else
6088 if (r_type != R_MIPS_REL32)
6089 value = symbol + addend;
6090 else
6091 value = addend;
6093 value &= howto->dst_mask;
6094 break;
6096 case R_MIPS_PC32:
6097 value = symbol + addend - p;
6098 value &= howto->dst_mask;
6099 break;
6101 case R_MIPS16_26:
6102 /* The calculation for R_MIPS16_26 is just the same as for an
6103 R_MIPS_26. It's only the storage of the relocated field into
6104 the output file that's different. That's handled in
6105 mips_elf_perform_relocation. So, we just fall through to the
6106 R_MIPS_26 case here. */
6107 case R_MIPS_26:
6108 case R_MICROMIPS_26_S1:
6110 unsigned int shift;
6112 /* Shift is 2, unusually, for microMIPS JALX. */
6113 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6115 if (howto->partial_inplace && !section_p)
6116 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6117 else
6118 value = addend;
6119 value += symbol;
6121 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6122 be the correct ISA mode selector except for weak undefined
6123 symbols. */
6124 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6125 && (*cross_mode_jump_p
6126 ? (value & 3) != (r_type == R_MIPS_26)
6127 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6128 return bfd_reloc_outofrange;
6130 value >>= shift;
6131 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6132 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6133 value &= howto->dst_mask;
6135 break;
6137 case R_MIPS_TLS_DTPREL_HI16:
6138 case R_MIPS16_TLS_DTPREL_HI16:
6139 case R_MICROMIPS_TLS_DTPREL_HI16:
6140 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6141 & howto->dst_mask);
6142 break;
6144 case R_MIPS_TLS_DTPREL_LO16:
6145 case R_MIPS_TLS_DTPREL32:
6146 case R_MIPS_TLS_DTPREL64:
6147 case R_MIPS16_TLS_DTPREL_LO16:
6148 case R_MICROMIPS_TLS_DTPREL_LO16:
6149 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6150 break;
6152 case R_MIPS_TLS_TPREL_HI16:
6153 case R_MIPS16_TLS_TPREL_HI16:
6154 case R_MICROMIPS_TLS_TPREL_HI16:
6155 value = (mips_elf_high (addend + symbol - tprel_base (info))
6156 & howto->dst_mask);
6157 break;
6159 case R_MIPS_TLS_TPREL_LO16:
6160 case R_MIPS_TLS_TPREL32:
6161 case R_MIPS_TLS_TPREL64:
6162 case R_MIPS16_TLS_TPREL_LO16:
6163 case R_MICROMIPS_TLS_TPREL_LO16:
6164 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6165 break;
6167 case R_MIPS_HI16:
6168 case R_MIPS16_HI16:
6169 case R_MICROMIPS_HI16:
6170 if (!gp_disp_p)
6172 value = mips_elf_high (addend + symbol);
6173 value &= howto->dst_mask;
6175 else
6177 /* For MIPS16 ABI code we generate this sequence
6178 0: li $v0,%hi(_gp_disp)
6179 4: addiupc $v1,%lo(_gp_disp)
6180 8: sll $v0,16
6181 12: addu $v0,$v1
6182 14: move $gp,$v0
6183 So the offsets of hi and lo relocs are the same, but the
6184 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6185 ADDIUPC clears the low two bits of the instruction address,
6186 so the base is ($t9 + 4) & ~3. */
6187 if (r_type == R_MIPS16_HI16)
6188 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6189 /* The microMIPS .cpload sequence uses the same assembly
6190 instructions as the traditional psABI version, but the
6191 incoming $t9 has the low bit set. */
6192 else if (r_type == R_MICROMIPS_HI16)
6193 value = mips_elf_high (addend + gp - p - 1);
6194 else
6195 value = mips_elf_high (addend + gp - p);
6197 break;
6199 case R_MIPS_LO16:
6200 case R_MIPS16_LO16:
6201 case R_MICROMIPS_LO16:
6202 case R_MICROMIPS_HI0_LO16:
6203 if (!gp_disp_p)
6204 value = (symbol + addend) & howto->dst_mask;
6205 else
6207 /* See the comment for R_MIPS16_HI16 above for the reason
6208 for this conditional. */
6209 if (r_type == R_MIPS16_LO16)
6210 value = addend + gp - (p & ~(bfd_vma) 0x3);
6211 else if (r_type == R_MICROMIPS_LO16
6212 || r_type == R_MICROMIPS_HI0_LO16)
6213 value = addend + gp - p + 3;
6214 else
6215 value = addend + gp - p + 4;
6216 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6217 for overflow. But, on, say, IRIX5, relocations against
6218 _gp_disp are normally generated from the .cpload
6219 pseudo-op. It generates code that normally looks like
6220 this:
6222 lui $gp,%hi(_gp_disp)
6223 addiu $gp,$gp,%lo(_gp_disp)
6224 addu $gp,$gp,$t9
6226 Here $t9 holds the address of the function being called,
6227 as required by the MIPS ELF ABI. The R_MIPS_LO16
6228 relocation can easily overflow in this situation, but the
6229 R_MIPS_HI16 relocation will handle the overflow.
6230 Therefore, we consider this a bug in the MIPS ABI, and do
6231 not check for overflow here. */
6233 break;
6235 case R_MIPS_LITERAL:
6236 case R_MICROMIPS_LITERAL:
6237 /* Because we don't merge literal sections, we can handle this
6238 just like R_MIPS_GPREL16. In the long run, we should merge
6239 shared literals, and then we will need to additional work
6240 here. */
6242 /* Fall through. */
6244 case R_MIPS16_GPREL:
6245 /* The R_MIPS16_GPREL performs the same calculation as
6246 R_MIPS_GPREL16, but stores the relocated bits in a different
6247 order. We don't need to do anything special here; the
6248 differences are handled in mips_elf_perform_relocation. */
6249 case R_MIPS_GPREL16:
6250 case R_MICROMIPS_GPREL7_S2:
6251 case R_MICROMIPS_GPREL16:
6253 int bits = howto->bitsize + howto->rightshift;
6254 /* Only sign-extend the addend if it was extracted from the
6255 instruction. If the addend was separate, leave it alone,
6256 otherwise we may lose significant bits. */
6257 if (howto->partial_inplace)
6258 addend = _bfd_mips_elf_sign_extend (addend, bits);
6259 value = symbol + addend - gp;
6260 /* If the symbol was local, any earlier relocatable links will
6261 have adjusted its addend with the gp offset, so compensate
6262 for that now. Don't do it for symbols forced local in this
6263 link, though, since they won't have had the gp offset applied
6264 to them before. */
6265 if (was_local_p)
6266 value += gp0;
6267 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6268 overflowed_p = mips_elf_overflow_p (value, bits);
6270 break;
6272 case R_MIPS16_GOT16:
6273 case R_MIPS16_CALL16:
6274 case R_MIPS_GOT16:
6275 case R_MIPS_CALL16:
6276 case R_MICROMIPS_GOT16:
6277 case R_MICROMIPS_CALL16:
6278 /* VxWorks does not have separate local and global semantics for
6279 R_MIPS*_GOT16; every relocation evaluates to "G". */
6280 if (htab->root.target_os != is_vxworks && local_p)
6282 value = mips_elf_got16_entry (abfd, input_bfd, info,
6283 symbol + addend, !was_local_p);
6284 if (value == MINUS_ONE)
6285 return bfd_reloc_outofrange;
6286 value
6287 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6288 overflowed_p = mips_elf_overflow_p (value, 16);
6289 break;
6292 /* Fall through. */
6294 case R_MIPS_TLS_GD:
6295 case R_MIPS_TLS_GOTTPREL:
6296 case R_MIPS_TLS_LDM:
6297 case R_MIPS_GOT_DISP:
6298 case R_MIPS16_TLS_GD:
6299 case R_MIPS16_TLS_GOTTPREL:
6300 case R_MIPS16_TLS_LDM:
6301 case R_MICROMIPS_TLS_GD:
6302 case R_MICROMIPS_TLS_GOTTPREL:
6303 case R_MICROMIPS_TLS_LDM:
6304 case R_MICROMIPS_GOT_DISP:
6305 value = g;
6306 overflowed_p = mips_elf_overflow_p (value, 16);
6307 break;
6309 case R_MIPS_GPREL32:
6310 value = (addend + symbol + gp0 - gp);
6311 if (!save_addend)
6312 value &= howto->dst_mask;
6313 break;
6315 case R_MIPS_PC16:
6316 case R_MIPS_GNU_REL16_S2:
6317 if (howto->partial_inplace)
6318 addend = _bfd_mips_elf_sign_extend (addend, 18);
6320 /* No need to exclude weak undefined symbols here as they resolve
6321 to 0 and never set `*cross_mode_jump_p', so this alignment check
6322 will never trigger for them. */
6323 if (*cross_mode_jump_p
6324 ? ((symbol + addend) & 3) != 1
6325 : ((symbol + addend) & 3) != 0)
6326 return bfd_reloc_outofrange;
6328 value = symbol + addend - p;
6329 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6330 overflowed_p = mips_elf_overflow_p (value, 18);
6331 value >>= howto->rightshift;
6332 value &= howto->dst_mask;
6333 break;
6335 case R_MIPS16_PC16_S1:
6336 if (howto->partial_inplace)
6337 addend = _bfd_mips_elf_sign_extend (addend, 17);
6339 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6340 && (*cross_mode_jump_p
6341 ? ((symbol + addend) & 3) != 0
6342 : ((symbol + addend) & 1) == 0))
6343 return bfd_reloc_outofrange;
6345 value = symbol + addend - p;
6346 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6347 overflowed_p = mips_elf_overflow_p (value, 17);
6348 value >>= howto->rightshift;
6349 value &= howto->dst_mask;
6350 break;
6352 case R_MIPS_PC21_S2:
6353 if (howto->partial_inplace)
6354 addend = _bfd_mips_elf_sign_extend (addend, 23);
6356 if ((symbol + addend) & 3)
6357 return bfd_reloc_outofrange;
6359 value = symbol + addend - p;
6360 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6361 overflowed_p = mips_elf_overflow_p (value, 23);
6362 value >>= howto->rightshift;
6363 value &= howto->dst_mask;
6364 break;
6366 case R_MIPS_PC26_S2:
6367 if (howto->partial_inplace)
6368 addend = _bfd_mips_elf_sign_extend (addend, 28);
6370 if ((symbol + addend) & 3)
6371 return bfd_reloc_outofrange;
6373 value = symbol + addend - p;
6374 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6375 overflowed_p = mips_elf_overflow_p (value, 28);
6376 value >>= howto->rightshift;
6377 value &= howto->dst_mask;
6378 break;
6380 case R_MIPS_PC18_S3:
6381 if (howto->partial_inplace)
6382 addend = _bfd_mips_elf_sign_extend (addend, 21);
6384 if ((symbol + addend) & 7)
6385 return bfd_reloc_outofrange;
6387 value = symbol + addend - ((p | 7) ^ 7);
6388 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6389 overflowed_p = mips_elf_overflow_p (value, 21);
6390 value >>= howto->rightshift;
6391 value &= howto->dst_mask;
6392 break;
6394 case R_MIPS_PC19_S2:
6395 if (howto->partial_inplace)
6396 addend = _bfd_mips_elf_sign_extend (addend, 21);
6398 if ((symbol + addend) & 3)
6399 return bfd_reloc_outofrange;
6401 value = symbol + addend - p;
6402 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6403 overflowed_p = mips_elf_overflow_p (value, 21);
6404 value >>= howto->rightshift;
6405 value &= howto->dst_mask;
6406 break;
6408 case R_MIPS_PCHI16:
6409 value = mips_elf_high (symbol + addend - p);
6410 value &= howto->dst_mask;
6411 break;
6413 case R_MIPS_PCLO16:
6414 if (howto->partial_inplace)
6415 addend = _bfd_mips_elf_sign_extend (addend, 16);
6416 value = symbol + addend - p;
6417 value &= howto->dst_mask;
6418 break;
6420 case R_MICROMIPS_PC7_S1:
6421 if (howto->partial_inplace)
6422 addend = _bfd_mips_elf_sign_extend (addend, 8);
6424 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6425 && (*cross_mode_jump_p
6426 ? ((symbol + addend + 2) & 3) != 0
6427 : ((symbol + addend + 2) & 1) == 0))
6428 return bfd_reloc_outofrange;
6430 value = symbol + addend - p;
6431 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6432 overflowed_p = mips_elf_overflow_p (value, 8);
6433 value >>= howto->rightshift;
6434 value &= howto->dst_mask;
6435 break;
6437 case R_MICROMIPS_PC10_S1:
6438 if (howto->partial_inplace)
6439 addend = _bfd_mips_elf_sign_extend (addend, 11);
6441 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6442 && (*cross_mode_jump_p
6443 ? ((symbol + addend + 2) & 3) != 0
6444 : ((symbol + addend + 2) & 1) == 0))
6445 return bfd_reloc_outofrange;
6447 value = symbol + addend - p;
6448 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6449 overflowed_p = mips_elf_overflow_p (value, 11);
6450 value >>= howto->rightshift;
6451 value &= howto->dst_mask;
6452 break;
6454 case R_MICROMIPS_PC16_S1:
6455 if (howto->partial_inplace)
6456 addend = _bfd_mips_elf_sign_extend (addend, 17);
6458 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6459 && (*cross_mode_jump_p
6460 ? ((symbol + addend) & 3) != 0
6461 : ((symbol + addend) & 1) == 0))
6462 return bfd_reloc_outofrange;
6464 value = symbol + addend - p;
6465 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6466 overflowed_p = mips_elf_overflow_p (value, 17);
6467 value >>= howto->rightshift;
6468 value &= howto->dst_mask;
6469 break;
6471 case R_MICROMIPS_PC23_S2:
6472 if (howto->partial_inplace)
6473 addend = _bfd_mips_elf_sign_extend (addend, 25);
6474 value = symbol + addend - ((p | 3) ^ 3);
6475 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6476 overflowed_p = mips_elf_overflow_p (value, 25);
6477 value >>= howto->rightshift;
6478 value &= howto->dst_mask;
6479 break;
6481 case R_MIPS_GOT_HI16:
6482 case R_MIPS_CALL_HI16:
6483 case R_MICROMIPS_GOT_HI16:
6484 case R_MICROMIPS_CALL_HI16:
6485 /* We're allowed to handle these two relocations identically.
6486 The dynamic linker is allowed to handle the CALL relocations
6487 differently by creating a lazy evaluation stub. */
6488 value = g;
6489 value = mips_elf_high (value);
6490 value &= howto->dst_mask;
6491 break;
6493 case R_MIPS_GOT_LO16:
6494 case R_MIPS_CALL_LO16:
6495 case R_MICROMIPS_GOT_LO16:
6496 case R_MICROMIPS_CALL_LO16:
6497 value = g & howto->dst_mask;
6498 break;
6500 case R_MIPS_GOT_PAGE:
6501 case R_MICROMIPS_GOT_PAGE:
6502 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6503 if (value == MINUS_ONE)
6504 return bfd_reloc_outofrange;
6505 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6506 overflowed_p = mips_elf_overflow_p (value, 16);
6507 break;
6509 case R_MIPS_GOT_OFST:
6510 case R_MICROMIPS_GOT_OFST:
6511 if (local_p)
6512 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6513 else
6514 value = addend;
6515 overflowed_p = mips_elf_overflow_p (value, 16);
6516 break;
6518 case R_MIPS_SUB:
6519 case R_MICROMIPS_SUB:
6520 value = symbol - addend;
6521 value &= howto->dst_mask;
6522 break;
6524 case R_MIPS_HIGHER:
6525 case R_MICROMIPS_HIGHER:
6526 value = mips_elf_higher (addend + symbol);
6527 value &= howto->dst_mask;
6528 break;
6530 case R_MIPS_HIGHEST:
6531 case R_MICROMIPS_HIGHEST:
6532 value = mips_elf_highest (addend + symbol);
6533 value &= howto->dst_mask;
6534 break;
6536 case R_MIPS_SCN_DISP:
6537 case R_MICROMIPS_SCN_DISP:
6538 value = symbol + addend - sec->output_offset;
6539 value &= howto->dst_mask;
6540 break;
6542 case R_MIPS_JALR:
6543 case R_MICROMIPS_JALR:
6544 /* This relocation is only a hint. In some cases, we optimize
6545 it into a bal instruction. But we don't try to optimize
6546 when the symbol does not resolve locally. */
6547 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6548 return bfd_reloc_continue;
6549 /* We can't optimize cross-mode jumps either. */
6550 if (*cross_mode_jump_p)
6551 return bfd_reloc_continue;
6552 value = symbol + addend;
6553 /* Neither we can non-instruction-aligned targets. */
6554 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6555 return bfd_reloc_continue;
6556 break;
6558 case R_MIPS_PJUMP:
6559 case R_MIPS_GNU_VTINHERIT:
6560 case R_MIPS_GNU_VTENTRY:
6561 /* We don't do anything with these at present. */
6562 return bfd_reloc_continue;
6564 default:
6565 /* An unrecognized relocation type. */
6566 return bfd_reloc_notsupported;
6569 /* Store the VALUE for our caller. */
6570 *valuep = value;
6571 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6574 /* It has been determined that the result of the RELOCATION is the
6575 VALUE. Use HOWTO to place VALUE into the output file at the
6576 appropriate position. The SECTION is the section to which the
6577 relocation applies.
6578 CROSS_MODE_JUMP_P is true if the relocation field
6579 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6581 Returns FALSE if anything goes wrong. */
6583 static bool
6584 mips_elf_perform_relocation (struct bfd_link_info *info,
6585 reloc_howto_type *howto,
6586 const Elf_Internal_Rela *relocation,
6587 bfd_vma value, bfd *input_bfd,
6588 asection *input_section, bfd_byte *contents,
6589 bool cross_mode_jump_p)
6591 bfd_vma x;
6592 bfd_byte *location;
6593 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6595 /* Figure out where the relocation is occurring. */
6596 location = contents + relocation->r_offset;
6598 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6600 /* Obtain the current value. */
6601 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6603 /* Clear the field we are setting. */
6604 x &= ~howto->dst_mask;
6606 /* Set the field. */
6607 x |= (value & howto->dst_mask);
6609 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6610 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6612 bfd_vma opcode = x >> 26;
6614 if (r_type == R_MIPS16_26 ? opcode == 0x7
6615 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6616 : opcode == 0x1d)
6618 info->callbacks->einfo
6619 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6620 input_bfd, input_section, relocation->r_offset);
6621 return true;
6624 if (cross_mode_jump_p && jal_reloc_p (r_type))
6626 bool ok;
6627 bfd_vma opcode = x >> 26;
6628 bfd_vma jalx_opcode;
6630 /* Check to see if the opcode is already JAL or JALX. */
6631 if (r_type == R_MIPS16_26)
6633 ok = ((opcode == 0x6) || (opcode == 0x7));
6634 jalx_opcode = 0x7;
6636 else if (r_type == R_MICROMIPS_26_S1)
6638 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6639 jalx_opcode = 0x3c;
6641 else
6643 ok = ((opcode == 0x3) || (opcode == 0x1d));
6644 jalx_opcode = 0x1d;
6647 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6648 convert J or JALS to JALX. */
6649 if (!ok)
6651 info->callbacks->einfo
6652 (_("%X%H: unsupported jump between ISA modes; "
6653 "consider recompiling with interlinking enabled\n"),
6654 input_bfd, input_section, relocation->r_offset);
6655 return true;
6658 /* Make this the JALX opcode. */
6659 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6661 else if (cross_mode_jump_p && b_reloc_p (r_type))
6663 bool ok = false;
6664 bfd_vma opcode = x >> 16;
6665 bfd_vma jalx_opcode = 0;
6666 bfd_vma sign_bit = 0;
6667 bfd_vma addr;
6668 bfd_vma dest;
6670 if (r_type == R_MICROMIPS_PC16_S1)
6672 ok = opcode == 0x4060;
6673 jalx_opcode = 0x3c;
6674 sign_bit = 0x10000;
6675 value <<= 1;
6677 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6679 ok = opcode == 0x411;
6680 jalx_opcode = 0x1d;
6681 sign_bit = 0x20000;
6682 value <<= 2;
6685 if (ok && !bfd_link_pic (info))
6687 addr = (input_section->output_section->vma
6688 + input_section->output_offset
6689 + relocation->r_offset
6690 + 4);
6691 dest = (addr
6692 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6694 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6696 info->callbacks->einfo
6697 (_("%X%H: cannot convert branch between ISA modes "
6698 "to JALX: relocation out of range\n"),
6699 input_bfd, input_section, relocation->r_offset);
6700 return true;
6703 /* Make this the JALX opcode. */
6704 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6706 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6708 info->callbacks->einfo
6709 (_("%X%H: unsupported branch between ISA modes\n"),
6710 input_bfd, input_section, relocation->r_offset);
6711 return true;
6715 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6716 range. */
6717 if (!bfd_link_relocatable (info)
6718 && !cross_mode_jump_p
6719 && ((JAL_TO_BAL_P (input_bfd)
6720 && r_type == R_MIPS_26
6721 && (x >> 26) == 0x3) /* jal addr */
6722 || (JALR_TO_BAL_P (input_bfd)
6723 && r_type == R_MIPS_JALR
6724 && x == 0x0320f809) /* jalr t9 */
6725 || (JR_TO_B_P (input_bfd)
6726 && r_type == R_MIPS_JALR
6727 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6729 bfd_vma addr;
6730 bfd_vma dest;
6731 bfd_signed_vma off;
6733 addr = (input_section->output_section->vma
6734 + input_section->output_offset
6735 + relocation->r_offset
6736 + 4);
6737 if (r_type == R_MIPS_26)
6738 dest = (value << 2) | ((addr >> 28) << 28);
6739 else
6740 dest = value;
6741 off = dest - addr;
6742 if (off <= 0x1ffff && off >= -0x20000)
6744 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6745 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6746 else
6747 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6751 /* Put the value into the output. */
6752 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6754 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6755 location);
6757 return true;
6760 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6761 is the original relocation, which is now being transformed into a
6762 dynamic relocation. The ADDENDP is adjusted if necessary; the
6763 caller should store the result in place of the original addend. */
6765 static bool
6766 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6767 struct bfd_link_info *info,
6768 const Elf_Internal_Rela *rel,
6769 struct mips_elf_link_hash_entry *h,
6770 asection *sec, bfd_vma symbol,
6771 bfd_vma *addendp, asection *input_section)
6773 Elf_Internal_Rela outrel[3];
6774 asection *sreloc;
6775 bfd *dynobj;
6776 int r_type;
6777 long indx;
6778 bool defined_p;
6779 struct mips_elf_link_hash_table *htab;
6781 htab = mips_elf_hash_table (info);
6782 BFD_ASSERT (htab != NULL);
6784 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6785 dynobj = elf_hash_table (info)->dynobj;
6786 sreloc = mips_elf_rel_dyn_section (info, false);
6787 BFD_ASSERT (sreloc != NULL);
6788 BFD_ASSERT (sreloc->contents != NULL);
6789 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6790 < sreloc->size);
6792 outrel[0].r_offset =
6793 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6794 if (ABI_64_P (output_bfd))
6796 outrel[1].r_offset =
6797 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6798 outrel[2].r_offset =
6799 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6802 if (outrel[0].r_offset == MINUS_ONE)
6803 /* The relocation field has been deleted. */
6804 return true;
6806 if (outrel[0].r_offset == MINUS_TWO)
6808 /* The relocation field has been converted into a relative value of
6809 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6810 the field to be fully relocated, so add in the symbol's value. */
6811 *addendp += symbol;
6812 return true;
6815 /* We must now calculate the dynamic symbol table index to use
6816 in the relocation. */
6817 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6819 BFD_ASSERT (htab->root.target_os == is_vxworks
6820 || h->global_got_area != GGA_NONE);
6821 indx = h->root.dynindx;
6822 if (SGI_COMPAT (output_bfd))
6823 defined_p = h->root.def_regular;
6824 else
6825 /* ??? glibc's ld.so just adds the final GOT entry to the
6826 relocation field. It therefore treats relocs against
6827 defined symbols in the same way as relocs against
6828 undefined symbols. */
6829 defined_p = false;
6831 else
6833 if (sec != NULL && bfd_is_abs_section (sec))
6834 indx = 0;
6835 else if (sec == NULL || sec->owner == NULL)
6837 BFD_ASSERT (0);
6838 bfd_set_error (bfd_error_bad_value);
6839 return false;
6841 else
6843 indx = elf_section_data (sec->output_section)->dynindx;
6844 if (indx == 0)
6846 asection *osec = htab->root.text_index_section;
6847 indx = elf_section_data (osec)->dynindx;
6849 if (indx == 0)
6850 abort ();
6853 /* Instead of generating a relocation using the section
6854 symbol, we may as well make it a fully relative
6855 relocation. We want to avoid generating relocations to
6856 local symbols because we used to generate them
6857 incorrectly, without adding the original symbol value,
6858 which is mandated by the ABI for section symbols. In
6859 order to give dynamic loaders and applications time to
6860 phase out the incorrect use, we refrain from emitting
6861 section-relative relocations. It's not like they're
6862 useful, after all. This should be a bit more efficient
6863 as well. */
6864 /* ??? Although this behavior is compatible with glibc's ld.so,
6865 the ABI says that relocations against STN_UNDEF should have
6866 a symbol value of 0. Irix rld honors this, so relocations
6867 against STN_UNDEF have no effect. */
6868 if (!SGI_COMPAT (output_bfd))
6869 indx = 0;
6870 defined_p = true;
6873 /* If the relocation was previously an absolute relocation and
6874 this symbol will not be referred to by the relocation, we must
6875 adjust it by the value we give it in the dynamic symbol table.
6876 Otherwise leave the job up to the dynamic linker. */
6877 if (defined_p && r_type != R_MIPS_REL32)
6878 *addendp += symbol;
6880 if (htab->root.target_os == is_vxworks)
6881 /* VxWorks uses non-relative relocations for this. */
6882 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6883 else
6884 /* The relocation is always an REL32 relocation because we don't
6885 know where the shared library will wind up at load-time. */
6886 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6887 R_MIPS_REL32);
6889 /* For strict adherence to the ABI specification, we should
6890 generate a R_MIPS_64 relocation record by itself before the
6891 _REL32/_64 record as well, such that the addend is read in as
6892 a 64-bit value (REL32 is a 32-bit relocation, after all).
6893 However, since none of the existing ELF64 MIPS dynamic
6894 loaders seems to care, we don't waste space with these
6895 artificial relocations. If this turns out to not be true,
6896 mips_elf_allocate_dynamic_relocation() should be tweaked so
6897 as to make room for a pair of dynamic relocations per
6898 invocation if ABI_64_P, and here we should generate an
6899 additional relocation record with R_MIPS_64 by itself for a
6900 NULL symbol before this relocation record. */
6901 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6902 ABI_64_P (output_bfd)
6903 ? R_MIPS_64
6904 : R_MIPS_NONE);
6905 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6907 /* Adjust the output offset of the relocation to reference the
6908 correct location in the output file. */
6909 outrel[0].r_offset += (input_section->output_section->vma
6910 + input_section->output_offset);
6911 outrel[1].r_offset += (input_section->output_section->vma
6912 + input_section->output_offset);
6913 outrel[2].r_offset += (input_section->output_section->vma
6914 + input_section->output_offset);
6916 /* Put the relocation back out. We have to use the special
6917 relocation outputter in the 64-bit case since the 64-bit
6918 relocation format is non-standard. */
6919 if (ABI_64_P (output_bfd))
6921 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6922 (output_bfd, &outrel[0],
6923 (sreloc->contents
6924 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6926 else if (htab->root.target_os == is_vxworks)
6928 /* VxWorks uses RELA rather than REL dynamic relocations. */
6929 outrel[0].r_addend = *addendp;
6930 bfd_elf32_swap_reloca_out
6931 (output_bfd, &outrel[0],
6932 (sreloc->contents
6933 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6935 else
6936 bfd_elf32_swap_reloc_out
6937 (output_bfd, &outrel[0],
6938 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6940 /* We've now added another relocation. */
6941 ++sreloc->reloc_count;
6943 /* Make sure the output section is writable. The dynamic linker
6944 will be writing to it. */
6945 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6946 |= SHF_WRITE;
6948 /* On IRIX5, make an entry of compact relocation info. */
6949 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6951 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6952 bfd_byte *cr;
6954 if (scpt)
6956 Elf32_crinfo cptrel;
6958 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6959 cptrel.vaddr = (rel->r_offset
6960 + input_section->output_section->vma
6961 + input_section->output_offset);
6962 if (r_type == R_MIPS_REL32)
6963 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6964 else
6965 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6966 mips_elf_set_cr_dist2to (cptrel, 0);
6967 cptrel.konst = *addendp;
6969 cr = (scpt->contents
6970 + sizeof (Elf32_External_compact_rel));
6971 mips_elf_set_cr_relvaddr (cptrel, 0);
6972 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6973 ((Elf32_External_crinfo *) cr
6974 + scpt->reloc_count));
6975 ++scpt->reloc_count;
6979 /* If we've written this relocation for a readonly section,
6980 we need to set DF_TEXTREL again, so that we do not delete the
6981 DT_TEXTREL tag. */
6982 if (MIPS_ELF_READONLY_SECTION (input_section))
6983 info->flags |= DF_TEXTREL;
6985 return true;
6988 /* Return the MACH for a MIPS e_flags value. */
6990 unsigned long
6991 _bfd_elf_mips_mach (flagword flags)
6993 switch (flags & EF_MIPS_MACH)
6995 case EF_MIPS_MACH_3900:
6996 return bfd_mach_mips3900;
6998 case EF_MIPS_MACH_4010:
6999 return bfd_mach_mips4010;
7001 case EF_MIPS_MACH_ALLEGREX:
7002 return bfd_mach_mips_allegrex;
7004 case EF_MIPS_MACH_4100:
7005 return bfd_mach_mips4100;
7007 case EF_MIPS_MACH_4111:
7008 return bfd_mach_mips4111;
7010 case EF_MIPS_MACH_4120:
7011 return bfd_mach_mips4120;
7013 case EF_MIPS_MACH_4650:
7014 return bfd_mach_mips4650;
7016 case EF_MIPS_MACH_5400:
7017 return bfd_mach_mips5400;
7019 case EF_MIPS_MACH_5500:
7020 return bfd_mach_mips5500;
7022 case EF_MIPS_MACH_5900:
7023 return bfd_mach_mips5900;
7025 case EF_MIPS_MACH_9000:
7026 return bfd_mach_mips9000;
7028 case EF_MIPS_MACH_SB1:
7029 return bfd_mach_mips_sb1;
7031 case EF_MIPS_MACH_LS2E:
7032 return bfd_mach_mips_loongson_2e;
7034 case EF_MIPS_MACH_LS2F:
7035 return bfd_mach_mips_loongson_2f;
7037 case EF_MIPS_MACH_GS464:
7038 return bfd_mach_mips_gs464;
7040 case EF_MIPS_MACH_GS464E:
7041 return bfd_mach_mips_gs464e;
7043 case EF_MIPS_MACH_GS264E:
7044 return bfd_mach_mips_gs264e;
7046 case EF_MIPS_MACH_OCTEON3:
7047 return bfd_mach_mips_octeon3;
7049 case EF_MIPS_MACH_OCTEON2:
7050 return bfd_mach_mips_octeon2;
7052 case EF_MIPS_MACH_OCTEON:
7053 return bfd_mach_mips_octeon;
7055 case EF_MIPS_MACH_XLR:
7056 return bfd_mach_mips_xlr;
7058 case EF_MIPS_MACH_IAMR2:
7059 return bfd_mach_mips_interaptiv_mr2;
7061 default:
7062 switch (flags & EF_MIPS_ARCH)
7064 default:
7065 case EF_MIPS_ARCH_1:
7066 return bfd_mach_mips3000;
7068 case EF_MIPS_ARCH_2:
7069 return bfd_mach_mips6000;
7071 case EF_MIPS_ARCH_3:
7072 return bfd_mach_mips4000;
7074 case EF_MIPS_ARCH_4:
7075 return bfd_mach_mips8000;
7077 case EF_MIPS_ARCH_5:
7078 return bfd_mach_mips5;
7080 case EF_MIPS_ARCH_32:
7081 return bfd_mach_mipsisa32;
7083 case EF_MIPS_ARCH_64:
7084 return bfd_mach_mipsisa64;
7086 case EF_MIPS_ARCH_32R2:
7087 return bfd_mach_mipsisa32r2;
7089 case EF_MIPS_ARCH_64R2:
7090 return bfd_mach_mipsisa64r2;
7092 case EF_MIPS_ARCH_32R6:
7093 return bfd_mach_mipsisa32r6;
7095 case EF_MIPS_ARCH_64R6:
7096 return bfd_mach_mipsisa64r6;
7100 return 0;
7103 /* Return printable name for ABI. */
7105 static inline char *
7106 elf_mips_abi_name (bfd *abfd)
7108 flagword flags;
7110 flags = elf_elfheader (abfd)->e_flags;
7111 switch (flags & EF_MIPS_ABI)
7113 case 0:
7114 if (ABI_N32_P (abfd))
7115 return "N32";
7116 else if (ABI_64_P (abfd))
7117 return "64";
7118 else
7119 return "none";
7120 case EF_MIPS_ABI_O32:
7121 return "O32";
7122 case EF_MIPS_ABI_O64:
7123 return "O64";
7124 case EF_MIPS_ABI_EABI32:
7125 return "EABI32";
7126 case EF_MIPS_ABI_EABI64:
7127 return "EABI64";
7128 default:
7129 return "unknown abi";
7133 /* MIPS ELF uses two common sections. One is the usual one, and the
7134 other is for small objects. All the small objects are kept
7135 together, and then referenced via the gp pointer, which yields
7136 faster assembler code. This is what we use for the small common
7137 section. This approach is copied from ecoff.c. */
7138 static asection mips_elf_scom_section;
7139 static const asymbol mips_elf_scom_symbol =
7140 GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7141 static asection mips_elf_scom_section =
7142 BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7143 ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7145 /* MIPS ELF also uses an acommon section, which represents an
7146 allocated common symbol which may be overridden by a
7147 definition in a shared library. */
7148 static asection mips_elf_acom_section;
7149 static const asymbol mips_elf_acom_symbol =
7150 GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7151 static asection mips_elf_acom_section =
7152 BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7153 ".acommon", 0, SEC_ALLOC);
7155 /* This is used for both the 32-bit and the 64-bit ABI. */
7157 void
7158 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7160 elf_symbol_type *elfsym;
7162 /* Handle the special MIPS section numbers that a symbol may use. */
7163 elfsym = (elf_symbol_type *) asym;
7164 switch (elfsym->internal_elf_sym.st_shndx)
7166 case SHN_MIPS_ACOMMON:
7167 /* This section is used in a dynamically linked executable file.
7168 It is an allocated common section. The dynamic linker can
7169 either resolve these symbols to something in a shared
7170 library, or it can just leave them here. For our purposes,
7171 we can consider these symbols to be in a new section. */
7172 asym->section = &mips_elf_acom_section;
7173 break;
7175 case SHN_COMMON:
7176 /* Common symbols less than the GP size are automatically
7177 treated as SHN_MIPS_SCOMMON symbols, with some exceptions. */
7178 if (asym->value > elf_gp_size (abfd)
7179 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7180 || IRIX_COMPAT (abfd) == ict_irix6
7181 || strcmp (asym->name, "__gnu_lto_slim") == 0)
7182 break;
7183 /* Fall through. */
7184 case SHN_MIPS_SCOMMON:
7185 asym->section = &mips_elf_scom_section;
7186 asym->value = elfsym->internal_elf_sym.st_size;
7187 break;
7189 case SHN_MIPS_SUNDEFINED:
7190 asym->section = bfd_und_section_ptr;
7191 break;
7193 case SHN_MIPS_TEXT:
7195 asection *section = bfd_get_section_by_name (abfd, ".text");
7197 if (section != NULL)
7199 asym->section = section;
7200 /* MIPS_TEXT is a bit special, the address is not an offset
7201 to the base of the .text section. So subtract the section
7202 base address to make it an offset. */
7203 asym->value -= section->vma;
7206 break;
7208 case SHN_MIPS_DATA:
7210 asection *section = bfd_get_section_by_name (abfd, ".data");
7212 if (section != NULL)
7214 asym->section = section;
7215 /* MIPS_DATA is a bit special, the address is not an offset
7216 to the base of the .data section. So subtract the section
7217 base address to make it an offset. */
7218 asym->value -= section->vma;
7221 break;
7224 /* If this is an odd-valued function symbol, assume it's a MIPS16
7225 or microMIPS one. */
7226 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7227 && (asym->value & 1) != 0)
7229 asym->value--;
7230 if (MICROMIPS_P (abfd))
7231 elfsym->internal_elf_sym.st_other
7232 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7233 else
7234 elfsym->internal_elf_sym.st_other
7235 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7239 /* Implement elf_backend_eh_frame_address_size. This differs from
7240 the default in the way it handles EABI64.
7242 EABI64 was originally specified as an LP64 ABI, and that is what
7243 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7244 historically accepted the combination of -mabi=eabi and -mlong32,
7245 and this ILP32 variation has become semi-official over time.
7246 Both forms use elf32 and have pointer-sized FDE addresses.
7248 If an EABI object was generated by GCC 4.0 or above, it will have
7249 an empty .gcc_compiled_longXX section, where XX is the size of longs
7250 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7251 have no special marking to distinguish them from LP64 objects.
7253 We don't want users of the official LP64 ABI to be punished for the
7254 existence of the ILP32 variant, but at the same time, we don't want
7255 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7256 We therefore take the following approach:
7258 - If ABFD contains a .gcc_compiled_longXX section, use it to
7259 determine the pointer size.
7261 - Otherwise check the type of the first relocation. Assume that
7262 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7264 - Otherwise punt.
7266 The second check is enough to detect LP64 objects generated by pre-4.0
7267 compilers because, in the kind of output generated by those compilers,
7268 the first relocation will be associated with either a CIE personality
7269 routine or an FDE start address. Furthermore, the compilers never
7270 used a special (non-pointer) encoding for this ABI.
7272 Checking the relocation type should also be safe because there is no
7273 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7274 did so. */
7276 unsigned int
7277 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7279 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7280 return 8;
7281 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
7283 bool long32_p, long64_p;
7285 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7286 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7287 if (long32_p && long64_p)
7288 return 0;
7289 if (long32_p)
7290 return 4;
7291 if (long64_p)
7292 return 8;
7294 if (sec->reloc_count > 0)
7296 /* Load the relocations for this section. */
7297 Elf_Internal_Rela *internal_relocs =
7298 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
7299 if (internal_relocs == NULL)
7300 return 0;
7302 unsigned int size = 0;
7303 if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
7304 size = 8;
7306 if (elf_section_data (sec)->relocs != internal_relocs)
7307 free (internal_relocs);
7309 return size;
7312 return 0;
7314 return 4;
7317 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7318 relocations against two unnamed section symbols to resolve to the
7319 same address. For example, if we have code like:
7321 lw $4,%got_disp(.data)($gp)
7322 lw $25,%got_disp(.text)($gp)
7323 jalr $25
7325 then the linker will resolve both relocations to .data and the program
7326 will jump there rather than to .text.
7328 We can work around this problem by giving names to local section symbols.
7329 This is also what the MIPSpro tools do. */
7331 bool
7332 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7334 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7337 /* Work over a section just before writing it out. This routine is
7338 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7339 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7340 a better way. */
7342 bool
7343 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7345 if (hdr->sh_type == SHT_MIPS_REGINFO
7346 && hdr->sh_size > 0)
7348 bfd_byte buf[4];
7350 BFD_ASSERT (hdr->contents == NULL);
7352 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7354 _bfd_error_handler
7355 (_("%pB: incorrect `.reginfo' section size; "
7356 "expected %" PRIu64 ", got %" PRIu64),
7357 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7358 (uint64_t) hdr->sh_size);
7359 bfd_set_error (bfd_error_bad_value);
7360 return false;
7363 if (bfd_seek (abfd,
7364 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7365 SEEK_SET) != 0)
7366 return false;
7367 H_PUT_32 (abfd, elf_gp (abfd), buf);
7368 if (bfd_write (buf, 4, abfd) != 4)
7369 return false;
7372 if (hdr->sh_type == SHT_MIPS_OPTIONS
7373 && hdr->bfd_section != NULL
7374 && mips_elf_section_data (hdr->bfd_section) != NULL
7375 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7377 bfd_byte *contents, *l, *lend;
7379 /* We stored the section contents in the tdata field in the
7380 set_section_contents routine. We save the section contents
7381 so that we don't have to read them again.
7382 At this point we know that elf_gp is set, so we can look
7383 through the section contents to see if there is an
7384 ODK_REGINFO structure. */
7386 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7387 l = contents;
7388 lend = contents + hdr->sh_size;
7389 while (l + sizeof (Elf_External_Options) <= lend)
7391 Elf_Internal_Options intopt;
7393 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7394 &intopt);
7395 if (intopt.size < sizeof (Elf_External_Options))
7397 _bfd_error_handler
7398 /* xgettext:c-format */
7399 (_("%pB: warning: bad `%s' option size %u smaller than"
7400 " its header"),
7401 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7402 break;
7404 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7406 bfd_byte buf[8];
7408 if (bfd_seek (abfd,
7409 (hdr->sh_offset
7410 + (l - contents)
7411 + sizeof (Elf_External_Options)
7412 + (sizeof (Elf64_External_RegInfo) - 8)),
7413 SEEK_SET) != 0)
7414 return false;
7415 H_PUT_64 (abfd, elf_gp (abfd), buf);
7416 if (bfd_write (buf, 8, abfd) != 8)
7417 return false;
7419 else if (intopt.kind == ODK_REGINFO)
7421 bfd_byte buf[4];
7423 if (bfd_seek (abfd,
7424 (hdr->sh_offset
7425 + (l - contents)
7426 + sizeof (Elf_External_Options)
7427 + (sizeof (Elf32_External_RegInfo) - 4)),
7428 SEEK_SET) != 0)
7429 return false;
7430 H_PUT_32 (abfd, elf_gp (abfd), buf);
7431 if (bfd_write (buf, 4, abfd) != 4)
7432 return false;
7434 l += intopt.size;
7438 if (hdr->bfd_section != NULL)
7440 const char *name = bfd_section_name (hdr->bfd_section);
7442 /* .sbss is not handled specially here because the GNU/Linux
7443 prelinker can convert .sbss from NOBITS to PROGBITS and
7444 changing it back to NOBITS breaks the binary. The entry in
7445 _bfd_mips_elf_special_sections will ensure the correct flags
7446 are set on .sbss if BFD creates it without reading it from an
7447 input file, and without special handling here the flags set
7448 on it in an input file will be followed. */
7449 if (strcmp (name, ".sdata") == 0
7450 || strcmp (name, ".lit8") == 0
7451 || strcmp (name, ".lit4") == 0)
7452 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7453 else if (strcmp (name, ".srdata") == 0)
7454 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7455 else if (strcmp (name, ".compact_rel") == 0)
7456 hdr->sh_flags = 0;
7457 else if (strcmp (name, ".rtproc") == 0)
7459 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7461 unsigned int adjust;
7463 adjust = hdr->sh_size % hdr->sh_addralign;
7464 if (adjust != 0)
7465 hdr->sh_size += hdr->sh_addralign - adjust;
7470 return true;
7473 /* Handle a MIPS specific section when reading an object file. This
7474 is called when elfcode.h finds a section with an unknown type.
7475 This routine supports both the 32-bit and 64-bit ELF ABI. */
7477 bool
7478 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7479 Elf_Internal_Shdr *hdr,
7480 const char *name,
7481 int shindex)
7483 flagword flags = 0;
7485 /* There ought to be a place to keep ELF backend specific flags, but
7486 at the moment there isn't one. We just keep track of the
7487 sections by their name, instead. Fortunately, the ABI gives
7488 suggested names for all the MIPS specific sections, so we will
7489 probably get away with this. */
7490 switch (hdr->sh_type)
7492 case SHT_MIPS_LIBLIST:
7493 if (strcmp (name, ".liblist") != 0)
7494 return false;
7495 break;
7496 case SHT_MIPS_MSYM:
7497 if (strcmp (name, ".msym") != 0)
7498 return false;
7499 break;
7500 case SHT_MIPS_CONFLICT:
7501 if (strcmp (name, ".conflict") != 0)
7502 return false;
7503 break;
7504 case SHT_MIPS_GPTAB:
7505 if (! startswith (name, ".gptab."))
7506 return false;
7507 break;
7508 case SHT_MIPS_UCODE:
7509 if (strcmp (name, ".ucode") != 0)
7510 return false;
7511 break;
7512 case SHT_MIPS_DEBUG:
7513 if (strcmp (name, ".mdebug") != 0)
7514 return false;
7515 flags = SEC_DEBUGGING;
7516 break;
7517 case SHT_MIPS_REGINFO:
7518 if (strcmp (name, ".reginfo") != 0
7519 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7520 return false;
7521 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7522 break;
7523 case SHT_MIPS_IFACE:
7524 if (strcmp (name, ".MIPS.interfaces") != 0)
7525 return false;
7526 break;
7527 case SHT_MIPS_CONTENT:
7528 if (! startswith (name, ".MIPS.content"))
7529 return false;
7530 break;
7531 case SHT_MIPS_OPTIONS:
7532 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7533 return false;
7534 break;
7535 case SHT_MIPS_ABIFLAGS:
7536 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7537 return false;
7538 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7539 break;
7540 case SHT_MIPS_DWARF:
7541 if (! startswith (name, ".debug_")
7542 && ! startswith (name, ".gnu.debuglto_.debug_")
7543 && ! startswith (name, ".zdebug_")
7544 && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7545 return false;
7546 break;
7547 case SHT_MIPS_SYMBOL_LIB:
7548 if (strcmp (name, ".MIPS.symlib") != 0)
7549 return false;
7550 break;
7551 case SHT_MIPS_EVENTS:
7552 if (! startswith (name, ".MIPS.events")
7553 && ! startswith (name, ".MIPS.post_rel"))
7554 return false;
7555 break;
7556 case SHT_MIPS_XHASH:
7557 if (strcmp (name, ".MIPS.xhash") != 0)
7558 return false;
7559 default:
7560 break;
7563 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7564 return false;
7566 if (hdr->sh_flags & SHF_MIPS_GPREL)
7567 flags |= SEC_SMALL_DATA;
7569 if (flags)
7571 if (!bfd_set_section_flags (hdr->bfd_section,
7572 (bfd_section_flags (hdr->bfd_section)
7573 | flags)))
7574 return false;
7577 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7579 Elf_External_ABIFlags_v0 ext;
7581 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7582 &ext, 0, sizeof ext))
7583 return false;
7584 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7585 &mips_elf_tdata (abfd)->abiflags);
7586 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7587 return false;
7588 mips_elf_tdata (abfd)->abiflags_valid = true;
7591 /* FIXME: We should record sh_info for a .gptab section. */
7593 /* For a .reginfo section, set the gp value in the tdata information
7594 from the contents of this section. We need the gp value while
7595 processing relocs, so we just get it now. The .reginfo section
7596 is not used in the 64-bit MIPS ELF ABI. */
7597 if (hdr->sh_type == SHT_MIPS_REGINFO)
7599 Elf32_External_RegInfo ext;
7600 Elf32_RegInfo s;
7602 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7603 &ext, 0, sizeof ext))
7604 return false;
7605 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7606 elf_gp (abfd) = s.ri_gp_value;
7609 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7610 set the gp value based on what we find. We may see both
7611 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7612 they should agree. */
7613 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7615 bfd_byte *contents, *l, *lend;
7617 if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7619 free (contents);
7620 return false;
7622 l = contents;
7623 lend = contents + hdr->sh_size;
7624 while (l + sizeof (Elf_External_Options) <= lend)
7626 Elf_Internal_Options intopt;
7628 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7629 &intopt);
7630 if (intopt.size < sizeof (Elf_External_Options))
7632 bad_opt:
7633 _bfd_error_handler
7634 /* xgettext:c-format */
7635 (_("%pB: warning: truncated `%s' option"),
7636 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7637 break;
7639 if (intopt.kind == ODK_REGINFO)
7641 if (ABI_64_P (abfd))
7643 Elf64_Internal_RegInfo intreg;
7644 size_t needed = (sizeof (Elf_External_Options)
7645 + sizeof (Elf64_External_RegInfo));
7646 if (intopt.size < needed || (size_t) (lend - l) < needed)
7647 goto bad_opt;
7648 bfd_mips_elf64_swap_reginfo_in
7649 (abfd,
7650 ((Elf64_External_RegInfo *)
7651 (l + sizeof (Elf_External_Options))),
7652 &intreg);
7653 elf_gp (abfd) = intreg.ri_gp_value;
7655 else
7657 Elf32_RegInfo intreg;
7658 size_t needed = (sizeof (Elf_External_Options)
7659 + sizeof (Elf32_External_RegInfo));
7660 if (intopt.size < needed || (size_t) (lend - l) < needed)
7661 goto bad_opt;
7662 bfd_mips_elf32_swap_reginfo_in
7663 (abfd,
7664 ((Elf32_External_RegInfo *)
7665 (l + sizeof (Elf_External_Options))),
7666 &intreg);
7667 elf_gp (abfd) = intreg.ri_gp_value;
7670 l += intopt.size;
7672 free (contents);
7675 return true;
7678 /* Set the correct type for a MIPS ELF section. We do this by the
7679 section name, which is a hack, but ought to work. This routine is
7680 used by both the 32-bit and the 64-bit ABI. */
7682 bool
7683 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7685 const char *name = bfd_section_name (sec);
7687 if (strcmp (name, ".liblist") == 0)
7689 hdr->sh_type = SHT_MIPS_LIBLIST;
7690 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7691 /* The sh_link field is set in final_write_processing. */
7693 else if (strcmp (name, ".conflict") == 0)
7694 hdr->sh_type = SHT_MIPS_CONFLICT;
7695 else if (startswith (name, ".gptab."))
7697 hdr->sh_type = SHT_MIPS_GPTAB;
7698 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7699 /* The sh_info field is set in final_write_processing. */
7701 else if (strcmp (name, ".ucode") == 0)
7702 hdr->sh_type = SHT_MIPS_UCODE;
7703 else if (strcmp (name, ".mdebug") == 0)
7705 hdr->sh_type = SHT_MIPS_DEBUG;
7706 /* In a shared object on IRIX 5.3, the .mdebug section has an
7707 entsize of 0. FIXME: Does this matter? */
7708 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7709 hdr->sh_entsize = 0;
7710 else
7711 hdr->sh_entsize = 1;
7713 else if (strcmp (name, ".reginfo") == 0)
7715 hdr->sh_type = SHT_MIPS_REGINFO;
7716 /* In a shared object on IRIX 5.3, the .reginfo section has an
7717 entsize of 0x18. FIXME: Does this matter? */
7718 if (SGI_COMPAT (abfd))
7720 if ((abfd->flags & DYNAMIC) != 0)
7721 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7722 else
7723 hdr->sh_entsize = 1;
7725 else
7726 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7728 else if (SGI_COMPAT (abfd)
7729 && (strcmp (name, ".hash") == 0
7730 || strcmp (name, ".dynamic") == 0
7731 || strcmp (name, ".dynstr") == 0))
7733 if (SGI_COMPAT (abfd))
7734 hdr->sh_entsize = 0;
7735 #if 0
7736 /* This isn't how the IRIX6 linker behaves. */
7737 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7738 #endif
7740 else if (strcmp (name, ".got") == 0
7741 || strcmp (name, ".srdata") == 0
7742 || strcmp (name, ".sdata") == 0
7743 || strcmp (name, ".sbss") == 0
7744 || strcmp (name, ".lit4") == 0
7745 || strcmp (name, ".lit8") == 0)
7746 hdr->sh_flags |= SHF_MIPS_GPREL;
7747 else if (strcmp (name, ".MIPS.interfaces") == 0)
7749 hdr->sh_type = SHT_MIPS_IFACE;
7750 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7752 else if (startswith (name, ".MIPS.content"))
7754 hdr->sh_type = SHT_MIPS_CONTENT;
7755 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7756 /* The sh_info field is set in final_write_processing. */
7758 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7760 hdr->sh_type = SHT_MIPS_OPTIONS;
7761 hdr->sh_entsize = 1;
7762 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7764 else if (startswith (name, ".MIPS.abiflags"))
7766 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7767 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7769 else if (startswith (name, ".debug_")
7770 || startswith (name, ".gnu.debuglto_.debug_")
7771 || startswith (name, ".zdebug_")
7772 || startswith (name, ".gnu.debuglto_.zdebug_"))
7774 hdr->sh_type = SHT_MIPS_DWARF;
7776 /* Irix facilities such as libexc expect a single .debug_frame
7777 per executable, the system ones have NOSTRIP set and the linker
7778 doesn't merge sections with different flags so ... */
7779 if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7780 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7782 else if (strcmp (name, ".MIPS.symlib") == 0)
7784 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7785 /* The sh_link and sh_info fields are set in
7786 final_write_processing. */
7788 else if (startswith (name, ".MIPS.events")
7789 || startswith (name, ".MIPS.post_rel"))
7791 hdr->sh_type = SHT_MIPS_EVENTS;
7792 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7793 /* The sh_link field is set in final_write_processing. */
7795 else if (strcmp (name, ".msym") == 0)
7797 hdr->sh_type = SHT_MIPS_MSYM;
7798 hdr->sh_flags |= SHF_ALLOC;
7799 hdr->sh_entsize = 8;
7801 else if (strcmp (name, ".MIPS.xhash") == 0)
7803 hdr->sh_type = SHT_MIPS_XHASH;
7804 hdr->sh_flags |= SHF_ALLOC;
7805 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7808 /* The generic elf_fake_sections will set up REL_HDR using the default
7809 kind of relocations. We used to set up a second header for the
7810 non-default kind of relocations here, but only NewABI would use
7811 these, and the IRIX ld doesn't like resulting empty RELA sections.
7812 Thus we create those header only on demand now. */
7814 return true;
7817 /* Given a BFD section, try to locate the corresponding ELF section
7818 index. This is used by both the 32-bit and the 64-bit ABI.
7819 Actually, it's not clear to me that the 64-bit ABI supports these,
7820 but for non-PIC objects we will certainly want support for at least
7821 the .scommon section. */
7823 bool
7824 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7825 asection *sec, int *retval)
7827 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7829 *retval = SHN_MIPS_SCOMMON;
7830 return true;
7832 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7834 *retval = SHN_MIPS_ACOMMON;
7835 return true;
7837 return false;
7840 /* Hook called by the linker routine which adds symbols from an object
7841 file. We must handle the special MIPS section numbers here. */
7843 bool
7844 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7845 Elf_Internal_Sym *sym, const char **namep,
7846 flagword *flagsp ATTRIBUTE_UNUSED,
7847 asection **secp, bfd_vma *valp)
7849 if (SGI_COMPAT (abfd)
7850 && (abfd->flags & DYNAMIC) != 0
7851 && strcmp (*namep, "_rld_new_interface") == 0)
7853 /* Skip IRIX5 rld entry name. */
7854 *namep = NULL;
7855 return true;
7858 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7859 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7860 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7861 a magic symbol resolved by the linker, we ignore this bogus definition
7862 of _gp_disp. New ABI objects do not suffer from this problem so this
7863 is not done for them. */
7864 if (!NEWABI_P(abfd)
7865 && (sym->st_shndx == SHN_ABS)
7866 && (strcmp (*namep, "_gp_disp") == 0))
7868 *namep = NULL;
7869 return true;
7872 switch (sym->st_shndx)
7874 case SHN_COMMON:
7875 /* Common symbols less than the GP size are automatically
7876 treated as SHN_MIPS_SCOMMON symbols, with some exceptions. */
7877 if (sym->st_size > elf_gp_size (abfd)
7878 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7879 || IRIX_COMPAT (abfd) == ict_irix6
7880 || strcmp (*namep, "__gnu_lto_slim") == 0)
7881 break;
7882 /* Fall through. */
7883 case SHN_MIPS_SCOMMON:
7884 *secp = bfd_make_section_old_way (abfd, ".scommon");
7885 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7886 *valp = sym->st_size;
7887 break;
7889 case SHN_MIPS_TEXT:
7890 /* This section is used in a shared object. */
7891 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7893 asymbol *elf_text_symbol;
7894 asection *elf_text_section;
7895 size_t amt = sizeof (asection);
7897 elf_text_section = bfd_zalloc (abfd, amt);
7898 if (elf_text_section == NULL)
7899 return false;
7901 amt = sizeof (asymbol);
7902 elf_text_symbol = bfd_zalloc (abfd, amt);
7903 if (elf_text_symbol == NULL)
7904 return false;
7906 /* Initialize the section. */
7908 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7910 elf_text_section->symbol = elf_text_symbol;
7911 elf_text_section->name = ".text";
7912 elf_text_section->flags = SEC_NO_FLAGS;
7913 elf_text_section->output_section = NULL;
7914 elf_text_section->owner = abfd;
7915 elf_text_symbol->name = ".text";
7916 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7917 elf_text_symbol->section = elf_text_section;
7919 /* This code used to do *secp = bfd_und_section_ptr if
7920 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7921 so I took it out. */
7922 *secp = mips_elf_tdata (abfd)->elf_text_section;
7923 break;
7925 case SHN_MIPS_ACOMMON:
7926 /* Fall through. XXX Can we treat this as allocated data? */
7927 case SHN_MIPS_DATA:
7928 /* This section is used in a shared object. */
7929 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7931 asymbol *elf_data_symbol;
7932 asection *elf_data_section;
7933 size_t amt = sizeof (asection);
7935 elf_data_section = bfd_zalloc (abfd, amt);
7936 if (elf_data_section == NULL)
7937 return false;
7939 amt = sizeof (asymbol);
7940 elf_data_symbol = bfd_zalloc (abfd, amt);
7941 if (elf_data_symbol == NULL)
7942 return false;
7944 /* Initialize the section. */
7946 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7948 elf_data_section->symbol = elf_data_symbol;
7949 elf_data_section->name = ".data";
7950 elf_data_section->flags = SEC_NO_FLAGS;
7951 elf_data_section->output_section = NULL;
7952 elf_data_section->owner = abfd;
7953 elf_data_symbol->name = ".data";
7954 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7955 elf_data_symbol->section = elf_data_section;
7957 /* This code used to do *secp = bfd_und_section_ptr if
7958 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7959 so I took it out. */
7960 *secp = mips_elf_tdata (abfd)->elf_data_section;
7961 break;
7963 case SHN_MIPS_SUNDEFINED:
7964 *secp = bfd_und_section_ptr;
7965 break;
7968 if (SGI_COMPAT (abfd)
7969 && ! bfd_link_pic (info)
7970 && info->output_bfd->xvec == abfd->xvec
7971 && strcmp (*namep, "__rld_obj_head") == 0)
7973 struct elf_link_hash_entry *h;
7974 struct bfd_link_hash_entry *bh;
7976 /* Mark __rld_obj_head as dynamic. */
7977 bh = NULL;
7978 if (! (_bfd_generic_link_add_one_symbol
7979 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7980 get_elf_backend_data (abfd)->collect, &bh)))
7981 return false;
7983 h = (struct elf_link_hash_entry *) bh;
7984 h->non_elf = 0;
7985 h->def_regular = 1;
7986 h->type = STT_OBJECT;
7988 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7989 return false;
7991 mips_elf_hash_table (info)->use_rld_obj_head = true;
7992 mips_elf_hash_table (info)->rld_symbol = h;
7995 /* If this is a mips16 text symbol, add 1 to the value to make it
7996 odd. This will cause something like .word SYM to come up with
7997 the right value when it is loaded into the PC. */
7998 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7999 ++*valp;
8001 return true;
8004 /* This hook function is called before the linker writes out a global
8005 symbol. We mark symbols as small common if appropriate. This is
8006 also where we undo the increment of the value for a mips16 symbol. */
8009 _bfd_mips_elf_link_output_symbol_hook
8010 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8011 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8012 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8014 /* If we see a common symbol, which implies a relocatable link, then
8015 if a symbol was small common in an input file, mark it as small
8016 common in the output file. */
8017 if (sym->st_shndx == SHN_COMMON
8018 && strcmp (input_sec->name, ".scommon") == 0)
8019 sym->st_shndx = SHN_MIPS_SCOMMON;
8021 if (ELF_ST_IS_COMPRESSED (sym->st_other))
8022 sym->st_value &= ~1;
8024 return 1;
8027 /* Functions for the dynamic linker. */
8029 /* Create dynamic sections when linking against a dynamic object. */
8031 bool
8032 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8034 struct elf_link_hash_entry *h;
8035 struct bfd_link_hash_entry *bh;
8036 flagword flags;
8037 register asection *s;
8038 const char * const *namep;
8039 struct mips_elf_link_hash_table *htab;
8041 htab = mips_elf_hash_table (info);
8042 BFD_ASSERT (htab != NULL);
8044 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8045 | SEC_LINKER_CREATED | SEC_READONLY);
8047 /* The psABI requires a read-only .dynamic section, but the VxWorks
8048 EABI doesn't. */
8049 if (htab->root.target_os != is_vxworks)
8051 s = bfd_get_linker_section (abfd, ".dynamic");
8052 if (s != NULL)
8054 if (!bfd_set_section_flags (s, flags))
8055 return false;
8059 /* We need to create .got section. */
8060 if (!mips_elf_create_got_section (abfd, info))
8061 return false;
8063 if (! mips_elf_rel_dyn_section (info, true))
8064 return false;
8066 /* Create .stub section. */
8067 s = bfd_make_section_anyway_with_flags (abfd,
8068 MIPS_ELF_STUB_SECTION_NAME (abfd),
8069 flags | SEC_CODE);
8070 if (s == NULL
8071 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8072 return false;
8073 htab->sstubs = s;
8075 if (!mips_elf_hash_table (info)->use_rld_obj_head
8076 && bfd_link_executable (info)
8077 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8079 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8080 flags &~ (flagword) SEC_READONLY);
8081 if (s == NULL
8082 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8083 return false;
8086 /* Create .MIPS.xhash section. */
8087 if (info->emit_gnu_hash)
8088 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8089 flags | SEC_READONLY);
8091 /* On IRIX5, we adjust add some additional symbols and change the
8092 alignments of several sections. There is no ABI documentation
8093 indicating that this is necessary on IRIX6, nor any evidence that
8094 the linker takes such action. */
8095 if (IRIX_COMPAT (abfd) == ict_irix5)
8097 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8099 bh = NULL;
8100 if (! (_bfd_generic_link_add_one_symbol
8101 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8102 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8103 return false;
8105 h = (struct elf_link_hash_entry *) bh;
8106 h->mark = 1;
8107 h->non_elf = 0;
8108 h->def_regular = 1;
8109 h->type = STT_SECTION;
8111 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8112 return false;
8115 /* We need to create a .compact_rel section. */
8116 if (SGI_COMPAT (abfd))
8118 if (!mips_elf_create_compact_rel_section (abfd, info))
8119 return false;
8122 /* Change alignments of some sections. */
8123 s = bfd_get_linker_section (abfd, ".hash");
8124 if (s != NULL)
8125 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8127 s = bfd_get_linker_section (abfd, ".dynsym");
8128 if (s != NULL)
8129 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8131 s = bfd_get_linker_section (abfd, ".dynstr");
8132 if (s != NULL)
8133 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8135 /* ??? */
8136 s = bfd_get_section_by_name (abfd, ".reginfo");
8137 if (s != NULL)
8138 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8140 s = bfd_get_linker_section (abfd, ".dynamic");
8141 if (s != NULL)
8142 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8145 if (bfd_link_executable (info))
8147 const char *name;
8149 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8150 bh = NULL;
8151 if (!(_bfd_generic_link_add_one_symbol
8152 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8153 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8154 return false;
8156 h = (struct elf_link_hash_entry *) bh;
8157 h->non_elf = 0;
8158 h->def_regular = 1;
8159 h->type = STT_SECTION;
8161 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8162 return false;
8164 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8166 /* __rld_map is a four byte word located in the .data section
8167 and is filled in by the rtld to contain a pointer to
8168 the _r_debug structure. Its symbol value will be set in
8169 _bfd_mips_elf_finish_dynamic_symbol. */
8170 s = bfd_get_linker_section (abfd, ".rld_map");
8171 BFD_ASSERT (s != NULL);
8173 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8174 bh = NULL;
8175 if (!(_bfd_generic_link_add_one_symbol
8176 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8177 get_elf_backend_data (abfd)->collect, &bh)))
8178 return false;
8180 h = (struct elf_link_hash_entry *) bh;
8181 h->non_elf = 0;
8182 h->def_regular = 1;
8183 h->type = STT_OBJECT;
8185 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8186 return false;
8187 mips_elf_hash_table (info)->rld_symbol = h;
8191 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8192 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8193 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8194 return false;
8196 /* Do the usual VxWorks handling. */
8197 if (htab->root.target_os == is_vxworks
8198 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8199 return false;
8201 return true;
8204 /* Return true if relocation REL against section SEC is a REL rather than
8205 RELA relocation. RELOCS is the first relocation in the section and
8206 ABFD is the bfd that contains SEC. */
8208 static bool
8209 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8210 const Elf_Internal_Rela *relocs,
8211 const Elf_Internal_Rela *rel)
8213 Elf_Internal_Shdr *rel_hdr;
8214 const struct elf_backend_data *bed;
8216 /* To determine which flavor of relocation this is, we depend on the
8217 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8218 rel_hdr = elf_section_data (sec)->rel.hdr;
8219 if (rel_hdr == NULL)
8220 return false;
8221 bed = get_elf_backend_data (abfd);
8222 return ((size_t) (rel - relocs)
8223 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8226 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8227 HOWTO is the relocation's howto and CONTENTS points to the contents
8228 of the section that REL is against. */
8230 static bfd_vma
8231 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8232 const Elf_Internal_Rela *rel,
8233 reloc_howto_type *howto, bfd_byte *contents)
8235 bfd_byte *location;
8236 unsigned int r_type;
8237 bfd_vma addend;
8238 bfd_vma bytes;
8240 if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8241 return 0;
8243 r_type = ELF_R_TYPE (abfd, rel->r_info);
8244 location = contents + rel->r_offset;
8246 /* Get the addend, which is stored in the input file. */
8247 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8248 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8249 _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8251 addend = bytes & howto->src_mask;
8253 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8254 accordingly. */
8255 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8256 addend <<= 1;
8258 return addend;
8261 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8262 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8263 and update *ADDEND with the final addend. Return true on success
8264 or false if the LO16 could not be found. RELEND is the exclusive
8265 upper bound on the relocations for REL's section. */
8267 static bool
8268 mips_elf_add_lo16_rel_addend (bfd *abfd,
8269 asection *sec,
8270 const Elf_Internal_Rela *rel,
8271 const Elf_Internal_Rela *relend,
8272 bfd_byte *contents, bfd_vma *addend)
8274 unsigned int r_type, lo16_type;
8275 const Elf_Internal_Rela *lo16_relocation;
8276 reloc_howto_type *lo16_howto;
8277 bfd_vma l;
8279 r_type = ELF_R_TYPE (abfd, rel->r_info);
8280 if (mips16_reloc_p (r_type))
8281 lo16_type = R_MIPS16_LO16;
8282 else if (micromips_reloc_p (r_type))
8283 lo16_type = R_MICROMIPS_LO16;
8284 else if (r_type == R_MIPS_PCHI16)
8285 lo16_type = R_MIPS_PCLO16;
8286 else
8287 lo16_type = R_MIPS_LO16;
8289 /* The combined value is the sum of the HI16 addend, left-shifted by
8290 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8291 code does a `lui' of the HI16 value, and then an `addiu' of the
8292 LO16 value.)
8294 Scan ahead to find a matching LO16 relocation.
8296 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8297 be immediately following. However, for the IRIX6 ABI, the next
8298 relocation may be a composed relocation consisting of several
8299 relocations for the same address. In that case, the R_MIPS_LO16
8300 relocation may occur as one of these. We permit a similar
8301 extension in general, as that is useful for GCC.
8303 In some cases GCC dead code elimination removes the LO16 but keeps
8304 the corresponding HI16. This is strictly speaking a violation of
8305 the ABI but not immediately harmful. */
8306 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8307 if (lo16_relocation == NULL)
8308 return false;
8310 /* Obtain the addend kept there. */
8311 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8312 l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8313 contents);
8315 l <<= lo16_howto->rightshift;
8316 l = _bfd_mips_elf_sign_extend (l, 16);
8318 *addend <<= 16;
8319 *addend += l;
8320 return true;
8323 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8324 store the contents in *CONTENTS on success. Assume that *CONTENTS
8325 already holds the contents if it is nonull on entry. */
8327 static bool
8328 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8330 if (*contents)
8331 return true;
8333 /* Get cached copy if it exists. */
8334 if (elf_section_data (sec)->this_hdr.contents != NULL)
8336 *contents = elf_section_data (sec)->this_hdr.contents;
8337 return true;
8340 return bfd_malloc_and_get_section (abfd, sec, contents);
8343 /* Make a new PLT record to keep internal data. */
8345 static struct plt_entry *
8346 mips_elf_make_plt_record (bfd *abfd)
8348 struct plt_entry *entry;
8350 entry = bfd_zalloc (abfd, sizeof (*entry));
8351 if (entry == NULL)
8352 return NULL;
8354 entry->stub_offset = MINUS_ONE;
8355 entry->mips_offset = MINUS_ONE;
8356 entry->comp_offset = MINUS_ONE;
8357 entry->gotplt_index = MINUS_ONE;
8358 return entry;
8361 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8362 for PIC code, as otherwise there is no load-time relocation involved
8363 and local GOT entries whose value is zero at static link time will
8364 retain their value at load time. */
8366 static bool
8367 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8368 struct mips_elf_link_hash_table *htab,
8369 unsigned int r_type)
8371 union
8373 struct elf_link_hash_entry *eh;
8374 struct bfd_link_hash_entry *bh;
8376 hzero;
8378 BFD_ASSERT (!htab->use_absolute_zero);
8379 BFD_ASSERT (bfd_link_pic (info));
8381 hzero.bh = NULL;
8382 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8383 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8384 NULL, false, false, &hzero.bh))
8385 return false;
8387 BFD_ASSERT (hzero.bh != NULL);
8388 hzero.eh->size = 0;
8389 hzero.eh->type = STT_NOTYPE;
8390 hzero.eh->other = STV_PROTECTED;
8391 hzero.eh->def_regular = 1;
8392 hzero.eh->non_elf = 0;
8394 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8395 return false;
8397 htab->use_absolute_zero = true;
8399 return true;
8402 /* Look through the relocs for a section during the first phase, and
8403 allocate space in the global offset table and record the need for
8404 standard MIPS and compressed procedure linkage table entries. */
8406 bool
8407 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8408 asection *sec, const Elf_Internal_Rela *relocs)
8410 const char *name;
8411 bfd *dynobj;
8412 Elf_Internal_Shdr *symtab_hdr;
8413 struct elf_link_hash_entry **sym_hashes;
8414 size_t extsymoff;
8415 const Elf_Internal_Rela *rel;
8416 const Elf_Internal_Rela *rel_end;
8417 asection *sreloc;
8418 const struct elf_backend_data *bed;
8419 struct mips_elf_link_hash_table *htab;
8420 bfd_byte *contents;
8421 bfd_vma addend;
8422 reloc_howto_type *howto;
8424 if (bfd_link_relocatable (info))
8425 return true;
8427 htab = mips_elf_hash_table (info);
8428 BFD_ASSERT (htab != NULL);
8430 dynobj = elf_hash_table (info)->dynobj;
8431 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8432 sym_hashes = elf_sym_hashes (abfd);
8433 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8435 bed = get_elf_backend_data (abfd);
8436 rel_end = relocs + sec->reloc_count;
8438 /* Check for the mips16 stub sections. */
8440 name = bfd_section_name (sec);
8441 if (FN_STUB_P (name))
8443 unsigned long r_symndx;
8445 /* Look at the relocation information to figure out which symbol
8446 this is for. */
8448 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8449 if (r_symndx == 0)
8451 _bfd_error_handler
8452 /* xgettext:c-format */
8453 (_("%pB: warning: cannot determine the target function for"
8454 " stub section `%s'"),
8455 abfd, name);
8456 bfd_set_error (bfd_error_bad_value);
8457 return false;
8460 if (r_symndx < extsymoff
8461 || sym_hashes[r_symndx - extsymoff] == NULL)
8463 asection *o;
8465 /* This stub is for a local symbol. This stub will only be
8466 needed if there is some relocation in this BFD, other
8467 than a 16 bit function call, which refers to this symbol. */
8468 for (o = abfd->sections; o != NULL; o = o->next)
8470 Elf_Internal_Rela *sec_relocs;
8471 const Elf_Internal_Rela *r, *rend;
8473 /* We can ignore stub sections when looking for relocs. */
8474 if ((o->flags & SEC_RELOC) == 0
8475 || o->reloc_count == 0
8476 || section_allows_mips16_refs_p (o))
8477 continue;
8479 sec_relocs
8480 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8481 info->keep_memory);
8482 if (sec_relocs == NULL)
8483 return false;
8485 rend = sec_relocs + o->reloc_count;
8486 for (r = sec_relocs; r < rend; r++)
8487 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8488 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8489 break;
8491 if (elf_section_data (o)->relocs != sec_relocs)
8492 free (sec_relocs);
8494 if (r < rend)
8495 break;
8498 if (o == NULL)
8500 /* There is no non-call reloc for this stub, so we do
8501 not need it. Since this function is called before
8502 the linker maps input sections to output sections, we
8503 can easily discard it by setting the SEC_EXCLUDE
8504 flag. */
8505 sec->flags |= SEC_EXCLUDE;
8506 return true;
8509 /* Record this stub in an array of local symbol stubs for
8510 this BFD. */
8511 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8513 unsigned long symcount;
8514 asection **n;
8515 bfd_size_type amt;
8517 if (elf_bad_symtab (abfd))
8518 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8519 else
8520 symcount = symtab_hdr->sh_info;
8521 amt = symcount * sizeof (asection *);
8522 n = bfd_zalloc (abfd, amt);
8523 if (n == NULL)
8524 return false;
8525 mips_elf_tdata (abfd)->local_stubs = n;
8528 sec->flags |= SEC_KEEP;
8529 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8531 /* We don't need to set mips16_stubs_seen in this case.
8532 That flag is used to see whether we need to look through
8533 the global symbol table for stubs. We don't need to set
8534 it here, because we just have a local stub. */
8536 else
8538 struct mips_elf_link_hash_entry *h;
8540 h = ((struct mips_elf_link_hash_entry *)
8541 sym_hashes[r_symndx - extsymoff]);
8543 while (h->root.root.type == bfd_link_hash_indirect
8544 || h->root.root.type == bfd_link_hash_warning)
8545 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8547 /* H is the symbol this stub is for. */
8549 /* If we already have an appropriate stub for this function, we
8550 don't need another one, so we can discard this one. Since
8551 this function is called before the linker maps input sections
8552 to output sections, we can easily discard it by setting the
8553 SEC_EXCLUDE flag. */
8554 if (h->fn_stub != NULL)
8556 sec->flags |= SEC_EXCLUDE;
8557 return true;
8560 sec->flags |= SEC_KEEP;
8561 h->fn_stub = sec;
8562 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8565 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8567 unsigned long r_symndx;
8568 struct mips_elf_link_hash_entry *h;
8569 asection **loc;
8571 /* Look at the relocation information to figure out which symbol
8572 this is for. */
8574 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8575 if (r_symndx == 0)
8577 _bfd_error_handler
8578 /* xgettext:c-format */
8579 (_("%pB: warning: cannot determine the target function for"
8580 " stub section `%s'"),
8581 abfd, name);
8582 bfd_set_error (bfd_error_bad_value);
8583 return false;
8586 if (r_symndx < extsymoff
8587 || sym_hashes[r_symndx - extsymoff] == NULL)
8589 asection *o;
8591 /* This stub is for a local symbol. This stub will only be
8592 needed if there is some relocation (R_MIPS16_26) in this BFD
8593 that refers to this symbol. */
8594 for (o = abfd->sections; o != NULL; o = o->next)
8596 Elf_Internal_Rela *sec_relocs;
8597 const Elf_Internal_Rela *r, *rend;
8599 /* We can ignore stub sections when looking for relocs. */
8600 if ((o->flags & SEC_RELOC) == 0
8601 || o->reloc_count == 0
8602 || section_allows_mips16_refs_p (o))
8603 continue;
8605 sec_relocs
8606 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8607 info->keep_memory);
8608 if (sec_relocs == NULL)
8609 return false;
8611 rend = sec_relocs + o->reloc_count;
8612 for (r = sec_relocs; r < rend; r++)
8613 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8614 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8615 break;
8617 if (elf_section_data (o)->relocs != sec_relocs)
8618 free (sec_relocs);
8620 if (r < rend)
8621 break;
8624 if (o == NULL)
8626 /* There is no non-call reloc for this stub, so we do
8627 not need it. Since this function is called before
8628 the linker maps input sections to output sections, we
8629 can easily discard it by setting the SEC_EXCLUDE
8630 flag. */
8631 sec->flags |= SEC_EXCLUDE;
8632 return true;
8635 /* Record this stub in an array of local symbol call_stubs for
8636 this BFD. */
8637 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8639 unsigned long symcount;
8640 asection **n;
8641 bfd_size_type amt;
8643 if (elf_bad_symtab (abfd))
8644 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8645 else
8646 symcount = symtab_hdr->sh_info;
8647 amt = symcount * sizeof (asection *);
8648 n = bfd_zalloc (abfd, amt);
8649 if (n == NULL)
8650 return false;
8651 mips_elf_tdata (abfd)->local_call_stubs = n;
8654 sec->flags |= SEC_KEEP;
8655 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8657 /* We don't need to set mips16_stubs_seen in this case.
8658 That flag is used to see whether we need to look through
8659 the global symbol table for stubs. We don't need to set
8660 it here, because we just have a local stub. */
8662 else
8664 h = ((struct mips_elf_link_hash_entry *)
8665 sym_hashes[r_symndx - extsymoff]);
8667 /* H is the symbol this stub is for. */
8669 if (CALL_FP_STUB_P (name))
8670 loc = &h->call_fp_stub;
8671 else
8672 loc = &h->call_stub;
8674 /* If we already have an appropriate stub for this function, we
8675 don't need another one, so we can discard this one. Since
8676 this function is called before the linker maps input sections
8677 to output sections, we can easily discard it by setting the
8678 SEC_EXCLUDE flag. */
8679 if (*loc != NULL)
8681 sec->flags |= SEC_EXCLUDE;
8682 return true;
8685 sec->flags |= SEC_KEEP;
8686 *loc = sec;
8687 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8691 sreloc = NULL;
8692 contents = NULL;
8693 for (rel = relocs; rel < rel_end; ++rel)
8695 unsigned long r_symndx;
8696 unsigned int r_type;
8697 struct elf_link_hash_entry *h;
8698 bool can_make_dynamic_p;
8699 bool call_reloc_p;
8700 bool constrain_symbol_p;
8702 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8703 r_type = ELF_R_TYPE (abfd, rel->r_info);
8705 if (r_symndx < extsymoff)
8706 h = NULL;
8707 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8709 _bfd_error_handler
8710 /* xgettext:c-format */
8711 (_("%pB: malformed reloc detected for section %s"),
8712 abfd, name);
8713 bfd_set_error (bfd_error_bad_value);
8714 return false;
8716 else
8718 h = sym_hashes[r_symndx - extsymoff];
8719 if (h != NULL)
8721 while (h->root.type == bfd_link_hash_indirect
8722 || h->root.type == bfd_link_hash_warning)
8723 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8727 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8728 relocation into a dynamic one. */
8729 can_make_dynamic_p = false;
8731 /* Set CALL_RELOC_P to true if the relocation is for a call,
8732 and if pointer equality therefore doesn't matter. */
8733 call_reloc_p = false;
8735 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8736 into account when deciding how to define the symbol. */
8737 constrain_symbol_p = true;
8739 switch (r_type)
8741 case R_MIPS_CALL16:
8742 case R_MIPS_CALL_HI16:
8743 case R_MIPS_CALL_LO16:
8744 case R_MIPS16_CALL16:
8745 case R_MICROMIPS_CALL16:
8746 case R_MICROMIPS_CALL_HI16:
8747 case R_MICROMIPS_CALL_LO16:
8748 call_reloc_p = true;
8749 /* Fall through. */
8751 case R_MIPS_GOT16:
8752 case R_MIPS_GOT_LO16:
8753 case R_MIPS_GOT_PAGE:
8754 case R_MIPS_GOT_DISP:
8755 case R_MIPS16_GOT16:
8756 case R_MICROMIPS_GOT16:
8757 case R_MICROMIPS_GOT_LO16:
8758 case R_MICROMIPS_GOT_PAGE:
8759 case R_MICROMIPS_GOT_DISP:
8760 /* If we have a symbol that will resolve to zero at static link
8761 time and it is used by a GOT relocation applied to code we
8762 cannot relax to an immediate zero load, then we will be using
8763 the special `__gnu_absolute_zero' symbol whose value is zero
8764 at dynamic load time. We ignore HI16-type GOT relocations at
8765 this stage, because their handling will depend entirely on
8766 the corresponding LO16-type GOT relocation. */
8767 if (!call_hi16_reloc_p (r_type)
8768 && h != NULL
8769 && bfd_link_pic (info)
8770 && !htab->use_absolute_zero
8771 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8773 bool rel_reloc;
8775 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8776 return false;
8778 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8779 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8780 if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8781 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8782 false))
8783 if (!mips_elf_define_absolute_zero (abfd, info, htab,
8784 r_type))
8785 return false;
8788 /* Fall through. */
8789 case R_MIPS_GOT_HI16:
8790 case R_MIPS_GOT_OFST:
8791 case R_MIPS_TLS_GOTTPREL:
8792 case R_MIPS_TLS_GD:
8793 case R_MIPS_TLS_LDM:
8794 case R_MIPS16_TLS_GOTTPREL:
8795 case R_MIPS16_TLS_GD:
8796 case R_MIPS16_TLS_LDM:
8797 case R_MICROMIPS_GOT_HI16:
8798 case R_MICROMIPS_GOT_OFST:
8799 case R_MICROMIPS_TLS_GOTTPREL:
8800 case R_MICROMIPS_TLS_GD:
8801 case R_MICROMIPS_TLS_LDM:
8802 if (dynobj == NULL)
8803 elf_hash_table (info)->dynobj = dynobj = abfd;
8804 if (!mips_elf_create_got_section (dynobj, info))
8805 return false;
8806 if (htab->root.target_os == is_vxworks
8807 && !bfd_link_pic (info))
8809 _bfd_error_handler
8810 /* xgettext:c-format */
8811 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8812 abfd, (uint64_t) rel->r_offset);
8813 bfd_set_error (bfd_error_bad_value);
8814 return false;
8816 can_make_dynamic_p = true;
8817 break;
8819 case R_MIPS_NONE:
8820 case R_MIPS_JALR:
8821 case R_MICROMIPS_JALR:
8822 /* These relocations have empty fields and are purely there to
8823 provide link information. The symbol value doesn't matter. */
8824 constrain_symbol_p = false;
8825 break;
8827 case R_MIPS_GPREL16:
8828 case R_MIPS_GPREL32:
8829 case R_MIPS16_GPREL:
8830 case R_MICROMIPS_GPREL16:
8831 /* GP-relative relocations always resolve to a definition in a
8832 regular input file, ignoring the one-definition rule. This is
8833 important for the GP setup sequence in NewABI code, which
8834 always resolves to a local function even if other relocations
8835 against the symbol wouldn't. */
8836 constrain_symbol_p = false;
8837 break;
8839 case R_MIPS_32:
8840 case R_MIPS_REL32:
8841 case R_MIPS_64:
8842 /* In VxWorks executables, references to external symbols
8843 must be handled using copy relocs or PLT entries; it is not
8844 possible to convert this relocation into a dynamic one.
8846 For executables that use PLTs and copy-relocs, we have a
8847 choice between converting the relocation into a dynamic
8848 one or using copy relocations or PLT entries. It is
8849 usually better to do the former, unless the relocation is
8850 against a read-only section. */
8851 if ((bfd_link_pic (info)
8852 || (h != NULL
8853 && htab->root.target_os != is_vxworks
8854 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8855 && !(!info->nocopyreloc
8856 && !PIC_OBJECT_P (abfd)
8857 && MIPS_ELF_READONLY_SECTION (sec))))
8858 && (sec->flags & SEC_ALLOC) != 0)
8860 can_make_dynamic_p = true;
8861 if (dynobj == NULL)
8862 elf_hash_table (info)->dynobj = dynobj = abfd;
8864 break;
8866 case R_MIPS_26:
8867 case R_MIPS_PC16:
8868 case R_MIPS_PC21_S2:
8869 case R_MIPS_PC26_S2:
8870 case R_MIPS16_26:
8871 case R_MIPS16_PC16_S1:
8872 case R_MICROMIPS_26_S1:
8873 case R_MICROMIPS_PC7_S1:
8874 case R_MICROMIPS_PC10_S1:
8875 case R_MICROMIPS_PC16_S1:
8876 case R_MICROMIPS_PC23_S2:
8877 call_reloc_p = true;
8878 break;
8881 if (h)
8883 if (constrain_symbol_p)
8885 if (!can_make_dynamic_p)
8886 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8888 if (!call_reloc_p)
8889 h->pointer_equality_needed = 1;
8891 /* We must not create a stub for a symbol that has
8892 relocations related to taking the function's address.
8893 This doesn't apply to VxWorks, where CALL relocs refer
8894 to a .got.plt entry instead of a normal .got entry. */
8895 if (htab->root.target_os != is_vxworks
8896 && (!can_make_dynamic_p || !call_reloc_p))
8897 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8900 /* Relocations against the special VxWorks __GOTT_BASE__ and
8901 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8902 room for them in .rela.dyn. */
8903 if (is_gott_symbol (info, h))
8905 if (sreloc == NULL)
8907 sreloc = mips_elf_rel_dyn_section (info, true);
8908 if (sreloc == NULL)
8909 return false;
8911 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8912 if (MIPS_ELF_READONLY_SECTION (sec))
8913 /* We tell the dynamic linker that there are
8914 relocations against the text segment. */
8915 info->flags |= DF_TEXTREL;
8918 else if (call_lo16_reloc_p (r_type)
8919 || got_lo16_reloc_p (r_type)
8920 || got_disp_reloc_p (r_type)
8921 || (got16_reloc_p (r_type)
8922 && htab->root.target_os == is_vxworks))
8924 /* We may need a local GOT entry for this relocation. We
8925 don't count R_MIPS_GOT_PAGE because we can estimate the
8926 maximum number of pages needed by looking at the size of
8927 the segment. Similar comments apply to R_MIPS*_GOT16 and
8928 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8929 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8930 R_MIPS_CALL_HI16 because these are always followed by an
8931 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8932 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8933 rel->r_addend, info, r_type))
8934 return false;
8937 if (h != NULL
8938 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8939 ELF_ST_IS_MIPS16 (h->other)))
8940 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8942 switch (r_type)
8944 case R_MIPS_CALL16:
8945 case R_MIPS16_CALL16:
8946 case R_MICROMIPS_CALL16:
8947 if (h == NULL)
8949 _bfd_error_handler
8950 /* xgettext:c-format */
8951 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8952 abfd, (uint64_t) rel->r_offset);
8953 bfd_set_error (bfd_error_bad_value);
8954 return false;
8956 /* Fall through. */
8958 case R_MIPS_CALL_HI16:
8959 case R_MIPS_CALL_LO16:
8960 case R_MICROMIPS_CALL_HI16:
8961 case R_MICROMIPS_CALL_LO16:
8962 if (h != NULL)
8964 /* Make sure there is room in the regular GOT to hold the
8965 function's address. We may eliminate it in favour of
8966 a .got.plt entry later; see mips_elf_count_got_symbols. */
8967 if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8968 r_type))
8969 return false;
8971 /* We need a stub, not a plt entry for the undefined
8972 function. But we record it as if it needs plt. See
8973 _bfd_elf_adjust_dynamic_symbol. */
8974 h->needs_plt = 1;
8975 h->type = STT_FUNC;
8977 break;
8979 case R_MIPS_GOT_PAGE:
8980 case R_MICROMIPS_GOT_PAGE:
8981 case R_MIPS16_GOT16:
8982 case R_MIPS_GOT16:
8983 case R_MIPS_GOT_HI16:
8984 case R_MIPS_GOT_LO16:
8985 case R_MICROMIPS_GOT16:
8986 case R_MICROMIPS_GOT_HI16:
8987 case R_MICROMIPS_GOT_LO16:
8988 if (!h || got_page_reloc_p (r_type))
8990 /* This relocation needs (or may need, if h != NULL) a
8991 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8992 know for sure until we know whether the symbol is
8993 preemptible. */
8994 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8996 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8997 return false;
8998 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
8999 addend = mips_elf_read_rel_addend (abfd, sec, rel,
9000 howto, contents);
9001 if (got16_reloc_p (r_type))
9002 mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
9003 contents, &addend);
9004 else
9005 addend <<= howto->rightshift;
9007 else
9008 addend = rel->r_addend;
9009 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9010 h, addend))
9011 return false;
9013 if (h)
9015 struct mips_elf_link_hash_entry *hmips =
9016 (struct mips_elf_link_hash_entry *) h;
9018 /* This symbol is definitely not overridable. */
9019 if (hmips->root.def_regular
9020 && ! (bfd_link_pic (info) && ! info->symbolic
9021 && ! hmips->root.forced_local))
9022 h = NULL;
9025 /* If this is a global, overridable symbol, GOT_PAGE will
9026 decay to GOT_DISP, so we'll need a GOT entry for it. */
9027 /* Fall through. */
9029 case R_MIPS_GOT_DISP:
9030 case R_MICROMIPS_GOT_DISP:
9031 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9032 false, r_type))
9033 return false;
9034 break;
9036 case R_MIPS_TLS_GOTTPREL:
9037 case R_MIPS16_TLS_GOTTPREL:
9038 case R_MICROMIPS_TLS_GOTTPREL:
9039 if (bfd_link_pic (info))
9040 info->flags |= DF_STATIC_TLS;
9041 /* Fall through */
9043 case R_MIPS_TLS_LDM:
9044 case R_MIPS16_TLS_LDM:
9045 case R_MICROMIPS_TLS_LDM:
9046 if (tls_ldm_reloc_p (r_type))
9048 r_symndx = STN_UNDEF;
9049 h = NULL;
9051 /* Fall through */
9053 case R_MIPS_TLS_GD:
9054 case R_MIPS16_TLS_GD:
9055 case R_MICROMIPS_TLS_GD:
9056 /* This symbol requires a global offset table entry, or two
9057 for TLS GD relocations. */
9058 if (h != NULL)
9060 if (!mips_elf_record_global_got_symbol (h, abfd, info,
9061 false, r_type))
9062 return false;
9064 else
9066 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9067 rel->r_addend,
9068 info, r_type))
9069 return false;
9071 break;
9073 case R_MIPS_32:
9074 case R_MIPS_REL32:
9075 case R_MIPS_64:
9076 /* In VxWorks executables, references to external symbols
9077 are handled using copy relocs or PLT stubs, so there's
9078 no need to add a .rela.dyn entry for this relocation. */
9079 if (can_make_dynamic_p)
9081 if (sreloc == NULL)
9083 sreloc = mips_elf_rel_dyn_section (info, true);
9084 if (sreloc == NULL)
9085 return false;
9087 if (bfd_link_pic (info) && h == NULL)
9089 /* When creating a shared object, we must copy these
9090 reloc types into the output file as R_MIPS_REL32
9091 relocs. Make room for this reloc in .rel(a).dyn. */
9092 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9093 if (MIPS_ELF_READONLY_SECTION (sec))
9094 /* We tell the dynamic linker that there are
9095 relocations against the text segment. */
9096 info->flags |= DF_TEXTREL;
9098 else
9100 struct mips_elf_link_hash_entry *hmips;
9102 /* For a shared object, we must copy this relocation
9103 unless the symbol turns out to be undefined and
9104 weak with non-default visibility, in which case
9105 it will be left as zero.
9107 We could elide R_MIPS_REL32 for locally binding symbols
9108 in shared libraries, but do not yet do so.
9110 For an executable, we only need to copy this
9111 reloc if the symbol is defined in a dynamic
9112 object. */
9113 hmips = (struct mips_elf_link_hash_entry *) h;
9114 ++hmips->possibly_dynamic_relocs;
9115 if (MIPS_ELF_READONLY_SECTION (sec))
9116 /* We need it to tell the dynamic linker if there
9117 are relocations against the text segment. */
9118 hmips->readonly_reloc = true;
9122 if (SGI_COMPAT (abfd))
9123 mips_elf_hash_table (info)->compact_rel_size +=
9124 sizeof (Elf32_External_crinfo);
9125 break;
9127 case R_MIPS_26:
9128 case R_MIPS_GPREL16:
9129 case R_MIPS_LITERAL:
9130 case R_MIPS_GPREL32:
9131 case R_MICROMIPS_26_S1:
9132 case R_MICROMIPS_GPREL16:
9133 case R_MICROMIPS_LITERAL:
9134 case R_MICROMIPS_GPREL7_S2:
9135 if (SGI_COMPAT (abfd))
9136 mips_elf_hash_table (info)->compact_rel_size +=
9137 sizeof (Elf32_External_crinfo);
9138 break;
9140 /* This relocation describes the C++ object vtable hierarchy.
9141 Reconstruct it for later use during GC. */
9142 case R_MIPS_GNU_VTINHERIT:
9143 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9144 return false;
9145 break;
9147 /* This relocation describes which C++ vtable entries are actually
9148 used. Record for later use during GC. */
9149 case R_MIPS_GNU_VTENTRY:
9150 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9151 return false;
9152 break;
9154 default:
9155 break;
9158 /* Record the need for a PLT entry. At this point we don't know
9159 yet if we are going to create a PLT in the first place, but
9160 we only record whether the relocation requires a standard MIPS
9161 or a compressed code entry anyway. If we don't make a PLT after
9162 all, then we'll just ignore these arrangements. Likewise if
9163 a PLT entry is not created because the symbol is satisfied
9164 locally. */
9165 if (h != NULL
9166 && (branch_reloc_p (r_type)
9167 || mips16_branch_reloc_p (r_type)
9168 || micromips_branch_reloc_p (r_type))
9169 && !SYMBOL_CALLS_LOCAL (info, h))
9171 if (h->plt.plist == NULL)
9172 h->plt.plist = mips_elf_make_plt_record (abfd);
9173 if (h->plt.plist == NULL)
9174 return false;
9176 if (branch_reloc_p (r_type))
9177 h->plt.plist->need_mips = true;
9178 else
9179 h->plt.plist->need_comp = true;
9182 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9183 if there is one. We only need to handle global symbols here;
9184 we decide whether to keep or delete stubs for local symbols
9185 when processing the stub's relocations. */
9186 if (h != NULL
9187 && !mips16_call_reloc_p (r_type)
9188 && !section_allows_mips16_refs_p (sec))
9190 struct mips_elf_link_hash_entry *mh;
9192 mh = (struct mips_elf_link_hash_entry *) h;
9193 mh->need_fn_stub = true;
9196 /* Refuse some position-dependent relocations when creating a
9197 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9198 not PIC, but we can create dynamic relocations and the result
9199 will be fine. Also do not refuse R_MIPS_LO16, which can be
9200 combined with R_MIPS_GOT16. */
9201 if (bfd_link_pic (info))
9203 switch (r_type)
9205 case R_MIPS_TLS_TPREL_HI16:
9206 case R_MIPS16_TLS_TPREL_HI16:
9207 case R_MICROMIPS_TLS_TPREL_HI16:
9208 case R_MIPS_TLS_TPREL_LO16:
9209 case R_MIPS16_TLS_TPREL_LO16:
9210 case R_MICROMIPS_TLS_TPREL_LO16:
9211 /* These are okay in PIE, but not in a shared library. */
9212 if (bfd_link_executable (info))
9213 break;
9215 /* FALLTHROUGH */
9217 case R_MIPS16_HI16:
9218 case R_MIPS_HI16:
9219 case R_MIPS_HIGHER:
9220 case R_MIPS_HIGHEST:
9221 case R_MICROMIPS_HI16:
9222 case R_MICROMIPS_HIGHER:
9223 case R_MICROMIPS_HIGHEST:
9224 /* Don't refuse a high part relocation if it's against
9225 no symbol (e.g. part of a compound relocation). */
9226 if (r_symndx == STN_UNDEF)
9227 break;
9229 /* Likewise an absolute symbol. */
9230 if (h != NULL && bfd_is_abs_symbol (&h->root))
9231 break;
9233 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9234 and has a special meaning. */
9235 if (!NEWABI_P (abfd) && h != NULL
9236 && strcmp (h->root.root.string, "_gp_disp") == 0)
9237 break;
9239 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9240 if (is_gott_symbol (info, h))
9241 break;
9243 /* FALLTHROUGH */
9245 case R_MIPS16_26:
9246 case R_MIPS_26:
9247 case R_MICROMIPS_26_S1:
9248 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9249 /* An error for unsupported relocations is raised as part
9250 of the above search, so we can skip the following. */
9251 if (howto != NULL)
9252 info->callbacks->einfo
9253 /* xgettext:c-format */
9254 (_("%X%H: relocation %s against `%s' cannot be used"
9255 " when making a shared object; recompile with -fPIC\n"),
9256 abfd, sec, rel->r_offset, howto->name,
9257 (h) ? h->root.root.string : "a local symbol");
9258 break;
9259 default:
9260 break;
9265 return true;
9268 /* Allocate space for global sym dynamic relocs. */
9270 static bool
9271 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9273 struct bfd_link_info *info = inf;
9274 bfd *dynobj;
9275 struct mips_elf_link_hash_entry *hmips;
9276 struct mips_elf_link_hash_table *htab;
9278 htab = mips_elf_hash_table (info);
9279 BFD_ASSERT (htab != NULL);
9281 dynobj = elf_hash_table (info)->dynobj;
9282 hmips = (struct mips_elf_link_hash_entry *) h;
9284 /* VxWorks executables are handled elsewhere; we only need to
9285 allocate relocations in shared objects. */
9286 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9287 return true;
9289 /* Ignore indirect symbols. All relocations against such symbols
9290 will be redirected to the target symbol. */
9291 if (h->root.type == bfd_link_hash_indirect)
9292 return true;
9294 /* If this symbol is defined in a dynamic object, or we are creating
9295 a shared library, we will need to copy any R_MIPS_32 or
9296 R_MIPS_REL32 relocs against it into the output file. */
9297 if (! bfd_link_relocatable (info)
9298 && hmips->possibly_dynamic_relocs != 0
9299 && (h->root.type == bfd_link_hash_defweak
9300 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9301 || bfd_link_pic (info)))
9303 bool do_copy = true;
9305 if (h->root.type == bfd_link_hash_undefweak)
9307 /* Do not copy relocations for undefined weak symbols that
9308 we are not going to export. */
9309 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9310 do_copy = false;
9312 /* Make sure undefined weak symbols are output as a dynamic
9313 symbol in PIEs. */
9314 else if (h->dynindx == -1 && !h->forced_local)
9316 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9317 return false;
9321 if (do_copy)
9323 /* Even though we don't directly need a GOT entry for this symbol,
9324 the SVR4 psABI requires it to have a dynamic symbol table
9325 index greater that DT_MIPS_GOTSYM if there are dynamic
9326 relocations against it.
9328 VxWorks does not enforce the same mapping between the GOT
9329 and the symbol table, so the same requirement does not
9330 apply there. */
9331 if (htab->root.target_os != is_vxworks)
9333 if (hmips->global_got_area > GGA_RELOC_ONLY)
9334 hmips->global_got_area = GGA_RELOC_ONLY;
9335 hmips->got_only_for_calls = false;
9338 mips_elf_allocate_dynamic_relocations
9339 (dynobj, info, hmips->possibly_dynamic_relocs);
9340 if (hmips->readonly_reloc)
9341 /* We tell the dynamic linker that there are relocations
9342 against the text segment. */
9343 info->flags |= DF_TEXTREL;
9347 return true;
9350 /* Adjust a symbol defined by a dynamic object and referenced by a
9351 regular object. The current definition is in some section of the
9352 dynamic object, but we're not including those sections. We have to
9353 change the definition to something the rest of the link can
9354 understand. */
9356 bool
9357 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9358 struct elf_link_hash_entry *h)
9360 bfd *dynobj;
9361 struct mips_elf_link_hash_entry *hmips;
9362 struct mips_elf_link_hash_table *htab;
9363 asection *s, *srel;
9365 htab = mips_elf_hash_table (info);
9366 BFD_ASSERT (htab != NULL);
9368 dynobj = elf_hash_table (info)->dynobj;
9369 hmips = (struct mips_elf_link_hash_entry *) h;
9371 /* Make sure we know what is going on here. */
9372 if (dynobj == NULL
9373 || (! h->needs_plt
9374 && ! h->is_weakalias
9375 && (! h->def_dynamic
9376 || ! h->ref_regular
9377 || h->def_regular)))
9379 if (h->type == STT_GNU_IFUNC)
9380 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9381 h->root.root.string);
9382 else
9383 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9384 h->root.root.string);
9385 return true;
9388 hmips = (struct mips_elf_link_hash_entry *) h;
9390 /* If there are call relocations against an externally-defined symbol,
9391 see whether we can create a MIPS lazy-binding stub for it. We can
9392 only do this if all references to the function are through call
9393 relocations, and in that case, the traditional lazy-binding stubs
9394 are much more efficient than PLT entries.
9396 Traditional stubs are only available on SVR4 psABI-based systems;
9397 VxWorks always uses PLTs instead. */
9398 if (htab->root.target_os != is_vxworks
9399 && h->needs_plt
9400 && !hmips->no_fn_stub)
9402 if (! elf_hash_table (info)->dynamic_sections_created)
9403 return true;
9405 /* If this symbol is not defined in a regular file, then set
9406 the symbol to the stub location. This is required to make
9407 function pointers compare as equal between the normal
9408 executable and the shared library. */
9409 if (!h->def_regular
9410 && !bfd_is_abs_section (htab->sstubs->output_section))
9412 hmips->needs_lazy_stub = true;
9413 htab->lazy_stub_count++;
9414 return true;
9417 /* As above, VxWorks requires PLT entries for externally-defined
9418 functions that are only accessed through call relocations.
9420 Both VxWorks and non-VxWorks targets also need PLT entries if there
9421 are static-only relocations against an externally-defined function.
9422 This can technically occur for shared libraries if there are
9423 branches to the symbol, although it is unlikely that this will be
9424 used in practice due to the short ranges involved. It can occur
9425 for any relative or absolute relocation in executables; in that
9426 case, the PLT entry becomes the function's canonical address. */
9427 else if (((h->needs_plt && !hmips->no_fn_stub)
9428 || (h->type == STT_FUNC && hmips->has_static_relocs))
9429 && htab->use_plts_and_copy_relocs
9430 && !SYMBOL_CALLS_LOCAL (info, h)
9431 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9432 && h->root.type == bfd_link_hash_undefweak))
9434 bool micromips_p = MICROMIPS_P (info->output_bfd);
9435 bool newabi_p = NEWABI_P (info->output_bfd);
9437 /* If this is the first symbol to need a PLT entry, then make some
9438 basic setup. Also work out PLT entry sizes. We'll need them
9439 for PLT offset calculations. */
9440 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9442 BFD_ASSERT (htab->root.sgotplt->size == 0);
9443 BFD_ASSERT (htab->plt_got_index == 0);
9445 /* If we're using the PLT additions to the psABI, each PLT
9446 entry is 16 bytes and the PLT0 entry is 32 bytes.
9447 Encourage better cache usage by aligning. We do this
9448 lazily to avoid pessimizing traditional objects. */
9449 if (htab->root.target_os != is_vxworks
9450 && !bfd_set_section_alignment (htab->root.splt, 5))
9451 return false;
9453 /* Make sure that .got.plt is word-aligned. We do this lazily
9454 for the same reason as above. */
9455 if (!bfd_set_section_alignment (htab->root.sgotplt,
9456 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9457 return false;
9459 /* On non-VxWorks targets, the first two entries in .got.plt
9460 are reserved. */
9461 if (htab->root.target_os != is_vxworks)
9462 htab->plt_got_index
9463 += (get_elf_backend_data (dynobj)->got_header_size
9464 / MIPS_ELF_GOT_SIZE (dynobj));
9466 /* On VxWorks, also allocate room for the header's
9467 .rela.plt.unloaded entries. */
9468 if (htab->root.target_os == is_vxworks
9469 && !bfd_link_pic (info))
9470 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9472 /* Now work out the sizes of individual PLT entries. */
9473 if (htab->root.target_os == is_vxworks
9474 && bfd_link_pic (info))
9475 htab->plt_mips_entry_size
9476 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9477 else if (htab->root.target_os == is_vxworks)
9478 htab->plt_mips_entry_size
9479 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9480 else if (newabi_p)
9481 htab->plt_mips_entry_size
9482 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9483 else if (!micromips_p)
9485 htab->plt_mips_entry_size
9486 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9487 htab->plt_comp_entry_size
9488 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9490 else if (htab->insn32)
9492 htab->plt_mips_entry_size
9493 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9494 htab->plt_comp_entry_size
9495 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9497 else
9499 htab->plt_mips_entry_size
9500 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9501 htab->plt_comp_entry_size
9502 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9506 if (h->plt.plist == NULL)
9507 h->plt.plist = mips_elf_make_plt_record (dynobj);
9508 if (h->plt.plist == NULL)
9509 return false;
9511 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9512 n32 or n64, so always use a standard entry there.
9514 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9515 all MIPS16 calls will go via that stub, and there is no benefit
9516 to having a MIPS16 entry. And in the case of call_stub a
9517 standard entry actually has to be used as the stub ends with a J
9518 instruction. */
9519 if (newabi_p
9520 || htab->root.target_os == is_vxworks
9521 || hmips->call_stub
9522 || hmips->call_fp_stub)
9524 h->plt.plist->need_mips = true;
9525 h->plt.plist->need_comp = false;
9528 /* Otherwise, if there are no direct calls to the function, we
9529 have a free choice of whether to use standard or compressed
9530 entries. Prefer microMIPS entries if the object is known to
9531 contain microMIPS code, so that it becomes possible to create
9532 pure microMIPS binaries. Prefer standard entries otherwise,
9533 because MIPS16 ones are no smaller and are usually slower. */
9534 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9536 if (micromips_p)
9537 h->plt.plist->need_comp = true;
9538 else
9539 h->plt.plist->need_mips = true;
9542 if (h->plt.plist->need_mips)
9544 h->plt.plist->mips_offset = htab->plt_mips_offset;
9545 htab->plt_mips_offset += htab->plt_mips_entry_size;
9547 if (h->plt.plist->need_comp)
9549 h->plt.plist->comp_offset = htab->plt_comp_offset;
9550 htab->plt_comp_offset += htab->plt_comp_entry_size;
9553 /* Reserve the corresponding .got.plt entry now too. */
9554 h->plt.plist->gotplt_index = htab->plt_got_index++;
9556 /* If the output file has no definition of the symbol, set the
9557 symbol's value to the address of the stub. */
9558 if (!bfd_link_pic (info) && !h->def_regular)
9559 hmips->use_plt_entry = true;
9561 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9562 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9563 ? MIPS_ELF_RELA_SIZE (dynobj)
9564 : MIPS_ELF_REL_SIZE (dynobj));
9566 /* Make room for the .rela.plt.unloaded relocations. */
9567 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9568 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9570 /* All relocations against this symbol that could have been made
9571 dynamic will now refer to the PLT entry instead. */
9572 hmips->possibly_dynamic_relocs = 0;
9574 return true;
9577 /* If this is a weak symbol, and there is a real definition, the
9578 processor independent code will have arranged for us to see the
9579 real definition first, and we can just use the same value. */
9580 if (h->is_weakalias)
9582 struct elf_link_hash_entry *def = weakdef (h);
9583 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9584 h->root.u.def.section = def->root.u.def.section;
9585 h->root.u.def.value = def->root.u.def.value;
9586 return true;
9589 /* Otherwise, there is nothing further to do for symbols defined
9590 in regular objects. */
9591 if (h->def_regular)
9592 return true;
9594 /* There's also nothing more to do if we'll convert all relocations
9595 against this symbol into dynamic relocations. */
9596 if (!hmips->has_static_relocs)
9597 return true;
9599 /* We're now relying on copy relocations. Complain if we have
9600 some that we can't convert. */
9601 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9603 _bfd_error_handler (_("non-dynamic relocations refer to "
9604 "dynamic symbol %s"),
9605 h->root.root.string);
9606 bfd_set_error (bfd_error_bad_value);
9607 return false;
9610 /* We must allocate the symbol in our .dynbss section, which will
9611 become part of the .bss section of the executable. There will be
9612 an entry for this symbol in the .dynsym section. The dynamic
9613 object will contain position independent code, so all references
9614 from the dynamic object to this symbol will go through the global
9615 offset table. The dynamic linker will use the .dynsym entry to
9616 determine the address it must put in the global offset table, so
9617 both the dynamic object and the regular object will refer to the
9618 same memory location for the variable. */
9620 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9622 s = htab->root.sdynrelro;
9623 srel = htab->root.sreldynrelro;
9625 else
9627 s = htab->root.sdynbss;
9628 srel = htab->root.srelbss;
9630 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9632 if (htab->root.target_os == is_vxworks)
9633 srel->size += sizeof (Elf32_External_Rela);
9634 else
9635 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9636 h->needs_copy = 1;
9639 /* All relocations against this symbol that could have been made
9640 dynamic will now refer to the local copy instead. */
9641 hmips->possibly_dynamic_relocs = 0;
9643 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9646 /* If the link uses a GOT, lay it out and work out its size. */
9648 static bool
9649 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9651 bfd *dynobj;
9652 asection *s;
9653 struct mips_got_info *g;
9654 bfd_size_type loadable_size = 0;
9655 bfd_size_type page_gotno;
9656 bfd *ibfd;
9657 struct mips_elf_traverse_got_arg tga;
9658 struct mips_elf_link_hash_table *htab;
9660 htab = mips_elf_hash_table (info);
9661 BFD_ASSERT (htab != NULL);
9663 s = htab->root.sgot;
9664 if (s == NULL)
9665 return true;
9667 dynobj = elf_hash_table (info)->dynobj;
9668 g = htab->got_info;
9670 /* Allocate room for the reserved entries. VxWorks always reserves
9671 3 entries; other objects only reserve 2 entries. */
9672 BFD_ASSERT (g->assigned_low_gotno == 0);
9673 if (htab->root.target_os == is_vxworks)
9674 htab->reserved_gotno = 3;
9675 else
9676 htab->reserved_gotno = 2;
9677 g->local_gotno += htab->reserved_gotno;
9678 g->assigned_low_gotno = htab->reserved_gotno;
9680 /* Decide which symbols need to go in the global part of the GOT and
9681 count the number of reloc-only GOT symbols. */
9682 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9684 if (!mips_elf_resolve_final_got_entries (info, g))
9685 return false;
9687 /* Calculate the total loadable size of the output. That
9688 will give us the maximum number of GOT_PAGE entries
9689 required. */
9690 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9692 asection *subsection;
9694 for (subsection = ibfd->sections;
9695 subsection;
9696 subsection = subsection->next)
9698 if ((subsection->flags & SEC_ALLOC) == 0)
9699 continue;
9700 loadable_size += ((subsection->size + 0xf)
9701 &~ (bfd_size_type) 0xf);
9705 if (htab->root.target_os == is_vxworks)
9706 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9707 relocations against local symbols evaluate to "G", and the EABI does
9708 not include R_MIPS_GOT_PAGE. */
9709 page_gotno = 0;
9710 else
9711 /* Assume there are two loadable segments consisting of contiguous
9712 sections. Is 5 enough? */
9713 page_gotno = (loadable_size >> 16) + 5;
9715 /* Choose the smaller of the two page estimates; both are intended to be
9716 conservative. */
9717 if (page_gotno > g->page_gotno)
9718 page_gotno = g->page_gotno;
9720 g->local_gotno += page_gotno;
9721 g->assigned_high_gotno = g->local_gotno - 1;
9723 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9724 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9725 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9727 /* VxWorks does not support multiple GOTs. It initializes $gp to
9728 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9729 dynamic loader. */
9730 if (htab->root.target_os != is_vxworks
9731 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9733 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9734 return false;
9736 else
9738 /* Record that all bfds use G. This also has the effect of freeing
9739 the per-bfd GOTs, which we no longer need. */
9740 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9741 if (mips_elf_bfd_got (ibfd, false))
9742 mips_elf_replace_bfd_got (ibfd, g);
9743 mips_elf_replace_bfd_got (output_bfd, g);
9745 /* Set up TLS entries. */
9746 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9747 tga.info = info;
9748 tga.g = g;
9749 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9750 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9751 if (!tga.g)
9752 return false;
9753 BFD_ASSERT (g->tls_assigned_gotno
9754 == g->global_gotno + g->local_gotno + g->tls_gotno);
9756 /* Each VxWorks GOT entry needs an explicit relocation. */
9757 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9758 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9760 /* Allocate room for the TLS relocations. */
9761 if (g->relocs)
9762 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9765 return true;
9768 /* Estimate the size of the .MIPS.stubs section. */
9770 static void
9771 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9773 struct mips_elf_link_hash_table *htab;
9774 bfd_size_type dynsymcount;
9776 htab = mips_elf_hash_table (info);
9777 BFD_ASSERT (htab != NULL);
9779 if (htab->lazy_stub_count == 0)
9780 return;
9782 /* IRIX rld assumes that a function stub isn't at the end of the .text
9783 section, so add a dummy entry to the end. */
9784 htab->lazy_stub_count++;
9786 /* Get a worst-case estimate of the number of dynamic symbols needed.
9787 At this point, dynsymcount does not account for section symbols
9788 and count_section_dynsyms may overestimate the number that will
9789 be needed. */
9790 dynsymcount = (elf_hash_table (info)->dynsymcount
9791 + count_section_dynsyms (output_bfd, info));
9793 /* Determine the size of one stub entry. There's no disadvantage
9794 from using microMIPS code here, so for the sake of pure-microMIPS
9795 binaries we prefer it whenever there's any microMIPS code in
9796 output produced at all. This has a benefit of stubs being
9797 shorter by 4 bytes each too, unless in the insn32 mode. */
9798 if (!MICROMIPS_P (output_bfd))
9799 htab->function_stub_size = (dynsymcount > 0x10000
9800 ? MIPS_FUNCTION_STUB_BIG_SIZE
9801 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9802 else if (htab->insn32)
9803 htab->function_stub_size = (dynsymcount > 0x10000
9804 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9805 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9806 else
9807 htab->function_stub_size = (dynsymcount > 0x10000
9808 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9809 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9811 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9814 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9815 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9816 stub, allocate an entry in the stubs section. */
9818 static bool
9819 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9821 struct mips_htab_traverse_info *hti = data;
9822 struct mips_elf_link_hash_table *htab;
9823 struct bfd_link_info *info;
9824 bfd *output_bfd;
9826 info = hti->info;
9827 output_bfd = hti->output_bfd;
9828 htab = mips_elf_hash_table (info);
9829 BFD_ASSERT (htab != NULL);
9831 if (h->needs_lazy_stub)
9833 bool micromips_p = MICROMIPS_P (output_bfd);
9834 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9835 bfd_vma isa_bit = micromips_p;
9837 BFD_ASSERT (htab->root.dynobj != NULL);
9838 if (h->root.plt.plist == NULL)
9839 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9840 if (h->root.plt.plist == NULL)
9842 hti->error = true;
9843 return false;
9845 h->root.root.u.def.section = htab->sstubs;
9846 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9847 h->root.plt.plist->stub_offset = htab->sstubs->size;
9848 h->root.other = other;
9849 htab->sstubs->size += htab->function_stub_size;
9851 return true;
9854 /* Allocate offsets in the stubs section to each symbol that needs one.
9855 Set the final size of the .MIPS.stub section. */
9857 static bool
9858 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9860 bfd *output_bfd = info->output_bfd;
9861 bool micromips_p = MICROMIPS_P (output_bfd);
9862 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9863 bfd_vma isa_bit = micromips_p;
9864 struct mips_elf_link_hash_table *htab;
9865 struct mips_htab_traverse_info hti;
9866 struct elf_link_hash_entry *h;
9867 bfd *dynobj;
9869 htab = mips_elf_hash_table (info);
9870 BFD_ASSERT (htab != NULL);
9872 if (htab->lazy_stub_count == 0)
9873 return true;
9875 htab->sstubs->size = 0;
9876 hti.info = info;
9877 hti.output_bfd = output_bfd;
9878 hti.error = false;
9879 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9880 if (hti.error)
9881 return false;
9882 htab->sstubs->size += htab->function_stub_size;
9883 BFD_ASSERT (htab->sstubs->size
9884 == htab->lazy_stub_count * htab->function_stub_size);
9886 dynobj = elf_hash_table (info)->dynobj;
9887 BFD_ASSERT (dynobj != NULL);
9888 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9889 if (h == NULL)
9890 return false;
9891 h->root.u.def.value = isa_bit;
9892 h->other = other;
9893 h->type = STT_FUNC;
9895 return true;
9898 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9899 bfd_link_info. If H uses the address of a PLT entry as the value
9900 of the symbol, then set the entry in the symbol table now. Prefer
9901 a standard MIPS PLT entry. */
9903 static bool
9904 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9906 struct bfd_link_info *info = data;
9907 bool micromips_p = MICROMIPS_P (info->output_bfd);
9908 struct mips_elf_link_hash_table *htab;
9909 unsigned int other;
9910 bfd_vma isa_bit;
9911 bfd_vma val;
9913 htab = mips_elf_hash_table (info);
9914 BFD_ASSERT (htab != NULL);
9916 if (h->use_plt_entry)
9918 BFD_ASSERT (h->root.plt.plist != NULL);
9919 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9920 || h->root.plt.plist->comp_offset != MINUS_ONE);
9922 val = htab->plt_header_size;
9923 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9925 isa_bit = 0;
9926 val += h->root.plt.plist->mips_offset;
9927 other = 0;
9929 else
9931 isa_bit = 1;
9932 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9933 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9935 val += isa_bit;
9936 /* For VxWorks, point at the PLT load stub rather than the lazy
9937 resolution stub; this stub will become the canonical function
9938 address. */
9939 if (htab->root.target_os == is_vxworks)
9940 val += 8;
9942 h->root.root.u.def.section = htab->root.splt;
9943 h->root.root.u.def.value = val;
9944 h->root.other = other;
9947 return true;
9950 /* Set the sizes of the dynamic sections, some mips non-dynamic sections,
9951 and check for any mips16 stub sections that we can discard. */
9953 bool
9954 _bfd_mips_elf_late_size_sections (bfd *output_bfd,
9955 struct bfd_link_info *info)
9957 bfd *dynobj;
9958 asection *s, *sreldyn;
9959 bool reltext;
9960 struct mips_elf_link_hash_table *htab;
9961 struct mips_htab_traverse_info hti;
9963 htab = mips_elf_hash_table (info);
9964 BFD_ASSERT (htab != NULL);
9966 /* The .reginfo section has a fixed size. */
9967 s = bfd_get_section_by_name (output_bfd, ".reginfo");
9968 if (s != NULL)
9970 bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
9971 s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9974 /* The .MIPS.abiflags section has a fixed size. */
9975 s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9976 if (s != NULL)
9978 bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
9979 s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9982 hti.info = info;
9983 hti.output_bfd = output_bfd;
9984 hti.error = false;
9985 mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
9986 if (hti.error)
9987 return false;
9989 dynobj = htab->root.dynobj;
9990 if (dynobj == NULL)
9991 return true;
9993 if (htab->root.dynamic_sections_created)
9995 /* Set the contents of the .interp section to the interpreter. */
9996 if (bfd_link_executable (info) && !info->nointerp)
9998 s = bfd_get_linker_section (dynobj, ".interp");
9999 BFD_ASSERT (s != NULL);
10000 s->size
10001 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10002 s->contents
10003 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10004 s->alloced = 1;
10007 /* Figure out the size of the PLT header if we know that we
10008 are using it. For the sake of cache alignment always use
10009 a standard header whenever any standard entries are present
10010 even if microMIPS entries are present as well. This also
10011 lets the microMIPS header rely on the value of $v0 only set
10012 by microMIPS entries, for a small size reduction.
10014 Set symbol table entry values for symbols that use the
10015 address of their PLT entry now that we can calculate it.
10017 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10018 haven't already in _bfd_elf_create_dynamic_sections. */
10019 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10021 bool micromips_p = (MICROMIPS_P (output_bfd)
10022 && !htab->plt_mips_offset);
10023 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10024 bfd_vma isa_bit = micromips_p;
10025 struct elf_link_hash_entry *h;
10026 bfd_vma size;
10028 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10029 BFD_ASSERT (htab->root.sgotplt->size == 0);
10030 BFD_ASSERT (htab->root.splt->size == 0);
10032 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10033 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10034 else if (htab->root.target_os == is_vxworks)
10035 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10036 else if (ABI_64_P (output_bfd))
10037 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10038 else if (ABI_N32_P (output_bfd))
10039 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10040 else if (!micromips_p)
10041 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10042 else if (htab->insn32)
10043 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10044 else
10045 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10047 htab->plt_header_is_comp = micromips_p;
10048 htab->plt_header_size = size;
10049 htab->root.splt->size = (size
10050 + htab->plt_mips_offset
10051 + htab->plt_comp_offset);
10052 htab->root.sgotplt->size = (htab->plt_got_index
10053 * MIPS_ELF_GOT_SIZE (dynobj));
10055 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10057 if (htab->root.hplt == NULL)
10059 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10060 "_PROCEDURE_LINKAGE_TABLE_");
10061 htab->root.hplt = h;
10062 if (h == NULL)
10063 return false;
10066 h = htab->root.hplt;
10067 h->root.u.def.value = isa_bit;
10068 h->other = other;
10069 h->type = STT_FUNC;
10073 /* Allocate space for global sym dynamic relocs. */
10074 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10076 mips_elf_estimate_stub_size (output_bfd, info);
10078 if (!mips_elf_lay_out_got (output_bfd, info))
10079 return false;
10081 mips_elf_lay_out_lazy_stubs (info);
10083 /* The check_relocs and adjust_dynamic_symbol entry points have
10084 determined the sizes of the various dynamic sections. Allocate
10085 memory for them. */
10086 reltext = false;
10087 for (s = dynobj->sections; s != NULL; s = s->next)
10089 const char *name;
10091 /* It's OK to base decisions on the section name, because none
10092 of the dynobj section names depend upon the input files. */
10093 name = bfd_section_name (s);
10095 if ((s->flags & SEC_LINKER_CREATED) == 0)
10096 continue;
10098 if (startswith (name, ".rel"))
10100 if (s->size != 0)
10102 const char *outname;
10103 asection *target;
10105 /* If this relocation section applies to a read only
10106 section, then we probably need a DT_TEXTREL entry.
10107 If the relocation section is .rel(a).dyn, we always
10108 assert a DT_TEXTREL entry rather than testing whether
10109 there exists a relocation to a read only section or
10110 not. */
10111 outname = bfd_section_name (s->output_section);
10112 target = bfd_get_section_by_name (output_bfd, outname + 4);
10113 if ((target != NULL
10114 && (target->flags & SEC_READONLY) != 0
10115 && (target->flags & SEC_ALLOC) != 0)
10116 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10117 reltext = true;
10119 /* We use the reloc_count field as a counter if we need
10120 to copy relocs into the output file. */
10121 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10122 s->reloc_count = 0;
10124 /* If combreloc is enabled, elf_link_sort_relocs() will
10125 sort relocations, but in a different way than we do,
10126 and before we're done creating relocations. Also, it
10127 will move them around between input sections'
10128 relocation's contents, so our sorting would be
10129 broken, so don't let it run. */
10130 info->combreloc = 0;
10133 else if (bfd_link_executable (info)
10134 && !htab->use_rld_obj_head
10135 && startswith (name, ".rld_map"))
10137 /* We add a room for __rld_map. It will be filled in by the
10138 rtld to contain a pointer to the _r_debug structure. */
10139 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10141 else if (SGI_COMPAT (output_bfd)
10142 && startswith (name, ".compact_rel"))
10143 s->size += htab->compact_rel_size;
10144 else if (s == htab->root.splt)
10146 /* If the last PLT entry has a branch delay slot, allocate
10147 room for an extra nop to fill the delay slot. This is
10148 for CPUs without load interlocking. */
10149 if (! LOAD_INTERLOCKS_P (output_bfd)
10150 && htab->root.target_os != is_vxworks
10151 && s->size > 0)
10152 s->size += 4;
10154 else if (! startswith (name, ".init")
10155 && s != htab->root.sgot
10156 && s != htab->root.sgotplt
10157 && s != htab->sstubs
10158 && s != htab->root.sdynbss
10159 && s != htab->root.sdynrelro)
10161 /* It's not one of our sections, so don't allocate space. */
10162 continue;
10165 if (s->size == 0)
10167 s->flags |= SEC_EXCLUDE;
10168 continue;
10171 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10172 continue;
10174 /* Allocate memory for the section contents. */
10175 s->contents = bfd_zalloc (dynobj, s->size);
10176 if (s->contents == NULL)
10178 bfd_set_error (bfd_error_no_memory);
10179 return false;
10181 s->alloced = 1;
10184 if (htab->root.dynamic_sections_created)
10186 /* Add some entries to the .dynamic section. We fill in the
10187 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10188 must add the entries now so that we get the correct size for
10189 the .dynamic section. */
10191 /* SGI object has the equivalence of DT_DEBUG in the
10192 DT_MIPS_RLD_MAP entry. This must come first because glibc
10193 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10194 may only look at the first one they see. */
10195 if (!bfd_link_pic (info)
10196 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10197 return false;
10199 if (bfd_link_executable (info)
10200 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10201 return false;
10203 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10204 used by the debugger. */
10205 if (bfd_link_executable (info)
10206 && !SGI_COMPAT (output_bfd)
10207 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10208 return false;
10210 if (reltext
10211 && (SGI_COMPAT (output_bfd)
10212 || htab->root.target_os == is_vxworks))
10213 info->flags |= DF_TEXTREL;
10215 if ((info->flags & DF_TEXTREL) != 0)
10217 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10218 return false;
10220 /* Clear the DF_TEXTREL flag. It will be set again if we
10221 write out an actual text relocation; we may not, because
10222 at this point we do not know whether e.g. any .eh_frame
10223 absolute relocations have been converted to PC-relative. */
10224 info->flags &= ~DF_TEXTREL;
10227 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10228 return false;
10230 sreldyn = mips_elf_rel_dyn_section (info, false);
10231 if (htab->root.target_os == is_vxworks)
10233 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10234 use any of the DT_MIPS_* tags. */
10235 if (sreldyn && sreldyn->size > 0)
10237 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10238 return false;
10240 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10241 return false;
10243 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10244 return false;
10247 else
10249 if (sreldyn && sreldyn->size > 0
10250 && !bfd_is_abs_section (sreldyn->output_section))
10252 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10253 return false;
10255 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10256 return false;
10258 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10259 return false;
10262 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10263 return false;
10265 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10266 return false;
10268 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10269 return false;
10271 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10272 return false;
10274 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10275 return false;
10277 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10278 return false;
10280 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10281 return false;
10283 if (info->emit_gnu_hash
10284 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10285 return false;
10287 if (IRIX_COMPAT (dynobj) == ict_irix5
10288 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10289 return false;
10291 if (IRIX_COMPAT (dynobj) == ict_irix6
10292 && (bfd_get_section_by_name
10293 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10294 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10295 return false;
10297 if (htab->root.splt->size > 0)
10299 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10300 return false;
10302 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10303 return false;
10305 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10306 return false;
10308 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10309 return false;
10311 if (htab->root.target_os == is_vxworks
10312 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10313 return false;
10316 return true;
10319 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10320 Adjust its R_ADDEND field so that it is correct for the output file.
10321 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10322 and sections respectively; both use symbol indexes. */
10324 static void
10325 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10326 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10327 asection **local_sections, Elf_Internal_Rela *rel)
10329 unsigned int r_type, r_symndx;
10330 Elf_Internal_Sym *sym;
10331 asection *sec;
10333 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10335 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10336 if (gprel16_reloc_p (r_type)
10337 || r_type == R_MIPS_GPREL32
10338 || literal_reloc_p (r_type))
10340 rel->r_addend += _bfd_get_gp_value (input_bfd);
10341 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10344 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10345 sym = local_syms + r_symndx;
10347 /* Adjust REL's addend to account for section merging. */
10348 if (!bfd_link_relocatable (info))
10350 sec = local_sections[r_symndx];
10351 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10354 /* This would normally be done by the rela_normal code in elflink.c. */
10355 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10356 rel->r_addend += local_sections[r_symndx]->output_offset;
10360 /* Handle relocations against symbols from removed linkonce sections,
10361 or sections discarded by a linker script. We use this wrapper around
10362 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10363 on 64-bit ELF targets. In this case for any relocation handled, which
10364 always be the first in a triplet, the remaining two have to be processed
10365 together with the first, even if they are R_MIPS_NONE. It is the symbol
10366 index referred by the first reloc that applies to all the three and the
10367 remaining two never refer to an object symbol. And it is the final
10368 relocation (the last non-null one) that determines the output field of
10369 the whole relocation so retrieve the corresponding howto structure for
10370 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10372 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10373 and therefore requires to be pasted in a loop. It also defines a block
10374 and does not protect any of its arguments, hence the extra brackets. */
10376 static void
10377 mips_reloc_against_discarded_section (bfd *output_bfd,
10378 struct bfd_link_info *info,
10379 bfd *input_bfd, asection *input_section,
10380 Elf_Internal_Rela **rel,
10381 const Elf_Internal_Rela **relend,
10382 bool rel_reloc,
10383 reloc_howto_type *howto,
10384 bfd_byte *contents)
10386 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10387 int count = bed->s->int_rels_per_ext_rel;
10388 unsigned int r_type;
10389 int i;
10391 for (i = count - 1; i > 0; i--)
10393 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10394 if (r_type != R_MIPS_NONE)
10396 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10397 break;
10402 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10403 (*rel), count, (*relend),
10404 howto, i, contents);
10406 while (0);
10409 /* Relocate a MIPS ELF section. */
10412 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10413 bfd *input_bfd, asection *input_section,
10414 bfd_byte *contents, Elf_Internal_Rela *relocs,
10415 Elf_Internal_Sym *local_syms,
10416 asection **local_sections)
10418 Elf_Internal_Rela *rel;
10419 const Elf_Internal_Rela *relend;
10420 bfd_vma addend = 0;
10421 bool use_saved_addend_p = false;
10423 relend = relocs + input_section->reloc_count;
10424 for (rel = relocs; rel < relend; ++rel)
10426 const char *name;
10427 bfd_vma value = 0;
10428 reloc_howto_type *howto;
10429 bool cross_mode_jump_p = false;
10430 /* TRUE if the relocation is a RELA relocation, rather than a
10431 REL relocation. */
10432 bool rela_relocation_p = true;
10433 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10434 const char *msg;
10435 unsigned long r_symndx;
10436 asection *sec;
10437 Elf_Internal_Shdr *symtab_hdr;
10438 struct elf_link_hash_entry *h;
10439 bool rel_reloc;
10441 rel_reloc = (NEWABI_P (input_bfd)
10442 && mips_elf_rel_relocation_p (input_bfd, input_section,
10443 relocs, rel));
10444 /* Find the relocation howto for this relocation. */
10445 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10447 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10448 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10449 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10451 sec = local_sections[r_symndx];
10452 h = NULL;
10454 else
10456 unsigned long extsymoff;
10458 extsymoff = 0;
10459 if (!elf_bad_symtab (input_bfd))
10460 extsymoff = symtab_hdr->sh_info;
10461 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10462 while (h->root.type == bfd_link_hash_indirect
10463 || h->root.type == bfd_link_hash_warning)
10464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10466 sec = NULL;
10467 if (h->root.type == bfd_link_hash_defined
10468 || h->root.type == bfd_link_hash_defweak)
10469 sec = h->root.u.def.section;
10472 if (sec != NULL && discarded_section (sec))
10474 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10475 input_section, &rel, &relend,
10476 rel_reloc, howto, contents);
10477 continue;
10480 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10482 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10483 64-bit code, but make sure all their addresses are in the
10484 lowermost or uppermost 32-bit section of the 64-bit address
10485 space. Thus, when they use an R_MIPS_64 they mean what is
10486 usually meant by R_MIPS_32, with the exception that the
10487 stored value is sign-extended to 64 bits. */
10488 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10490 /* On big-endian systems, we need to lie about the position
10491 of the reloc. */
10492 if (bfd_big_endian (input_bfd))
10493 rel->r_offset += 4;
10496 if (!use_saved_addend_p)
10498 /* If these relocations were originally of the REL variety,
10499 we must pull the addend out of the field that will be
10500 relocated. Otherwise, we simply use the contents of the
10501 RELA relocation. */
10502 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10503 relocs, rel))
10505 rela_relocation_p = false;
10506 addend = mips_elf_read_rel_addend (input_bfd, input_section,
10507 rel, howto, contents);
10508 if (hi16_reloc_p (r_type)
10509 || (got16_reloc_p (r_type)
10510 && mips_elf_local_relocation_p (input_bfd, rel,
10511 local_sections)))
10513 if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10514 rel, relend,
10515 contents, &addend))
10517 if (h)
10518 name = h->root.root.string;
10519 else
10520 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10521 local_syms + r_symndx,
10522 sec);
10523 _bfd_error_handler
10524 /* xgettext:c-format */
10525 (_("%pB: can't find matching LO16 reloc against `%s'"
10526 " for %s at %#" PRIx64 " in section `%pA'"),
10527 input_bfd, name,
10528 howto->name, (uint64_t) rel->r_offset, input_section);
10531 else
10532 addend <<= howto->rightshift;
10534 else
10535 addend = rel->r_addend;
10536 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10537 local_syms, local_sections, rel);
10540 if (bfd_link_relocatable (info))
10542 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10543 && bfd_big_endian (input_bfd))
10544 rel->r_offset -= 4;
10546 if (!rela_relocation_p && rel->r_addend)
10548 addend += rel->r_addend;
10549 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10550 addend = mips_elf_high (addend);
10551 else if (r_type == R_MIPS_HIGHER)
10552 addend = mips_elf_higher (addend);
10553 else if (r_type == R_MIPS_HIGHEST)
10554 addend = mips_elf_highest (addend);
10555 else
10556 addend >>= howto->rightshift;
10558 /* We use the source mask, rather than the destination
10559 mask because the place to which we are writing will be
10560 source of the addend in the final link. */
10561 addend &= howto->src_mask;
10563 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10564 /* See the comment above about using R_MIPS_64 in the 32-bit
10565 ABI. Here, we need to update the addend. It would be
10566 possible to get away with just using the R_MIPS_32 reloc
10567 but for endianness. */
10569 bfd_vma sign_bits;
10570 bfd_vma low_bits;
10571 bfd_vma high_bits;
10573 if (addend & ((bfd_vma) 1 << 31))
10574 #ifdef BFD64
10575 sign_bits = ((bfd_vma) 1 << 32) - 1;
10576 #else
10577 sign_bits = -1;
10578 #endif
10579 else
10580 sign_bits = 0;
10582 /* If we don't know that we have a 64-bit type,
10583 do two separate stores. */
10584 if (bfd_big_endian (input_bfd))
10586 /* Store the sign-bits (which are most significant)
10587 first. */
10588 low_bits = sign_bits;
10589 high_bits = addend;
10591 else
10593 low_bits = addend;
10594 high_bits = sign_bits;
10596 bfd_put_32 (input_bfd, low_bits,
10597 contents + rel->r_offset);
10598 bfd_put_32 (input_bfd, high_bits,
10599 contents + rel->r_offset + 4);
10600 continue;
10603 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10604 input_bfd, input_section,
10605 contents, false))
10606 return false;
10609 /* Go on to the next relocation. */
10610 continue;
10613 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10614 relocations for the same offset. In that case we are
10615 supposed to treat the output of each relocation as the addend
10616 for the next. */
10617 if (rel + 1 < relend
10618 && rel->r_offset == rel[1].r_offset
10619 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10620 use_saved_addend_p = true;
10621 else
10622 use_saved_addend_p = false;
10624 /* Figure out what value we are supposed to relocate. */
10625 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10626 input_section, contents,
10627 info, rel, addend, howto,
10628 local_syms, local_sections,
10629 &value, &name, &cross_mode_jump_p,
10630 use_saved_addend_p))
10632 case bfd_reloc_continue:
10633 /* There's nothing to do. */
10634 continue;
10636 case bfd_reloc_undefined:
10637 /* mips_elf_calculate_relocation already called the
10638 undefined_symbol callback. There's no real point in
10639 trying to perform the relocation at this point, so we
10640 just skip ahead to the next relocation. */
10641 continue;
10643 case bfd_reloc_notsupported:
10644 msg = _("internal error: unsupported relocation error");
10645 info->callbacks->warning
10646 (info, msg, name, input_bfd, input_section, rel->r_offset);
10647 return false;
10649 case bfd_reloc_overflow:
10650 if (use_saved_addend_p)
10651 /* Ignore overflow until we reach the last relocation for
10652 a given location. */
10654 else
10656 struct mips_elf_link_hash_table *htab;
10658 htab = mips_elf_hash_table (info);
10659 BFD_ASSERT (htab != NULL);
10660 BFD_ASSERT (name != NULL);
10661 if (!htab->small_data_overflow_reported
10662 && (gprel16_reloc_p (howto->type)
10663 || literal_reloc_p (howto->type)))
10665 msg = _("small-data section too large;"
10666 " lower small-data size limit (see option -G)");
10668 htab->small_data_overflow_reported = true;
10669 (*info->callbacks->einfo) ("%P: %s\n", msg);
10671 (*info->callbacks->reloc_overflow)
10672 (info, NULL, name, howto->name, (bfd_vma) 0,
10673 input_bfd, input_section, rel->r_offset);
10675 break;
10677 case bfd_reloc_ok:
10678 break;
10680 case bfd_reloc_outofrange:
10681 msg = NULL;
10682 if (jal_reloc_p (howto->type))
10683 msg = (cross_mode_jump_p
10684 ? _("cannot convert a jump to JALX "
10685 "for a non-word-aligned address")
10686 : (howto->type == R_MIPS16_26
10687 ? _("jump to a non-word-aligned address")
10688 : _("jump to a non-instruction-aligned address")));
10689 else if (b_reloc_p (howto->type))
10690 msg = (cross_mode_jump_p
10691 ? _("cannot convert a branch to JALX "
10692 "for a non-word-aligned address")
10693 : _("branch to a non-instruction-aligned address"));
10694 else if (aligned_pcrel_reloc_p (howto->type))
10695 msg = _("PC-relative load from unaligned address");
10696 if (msg)
10698 info->callbacks->einfo
10699 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10700 break;
10702 /* Fall through. */
10704 default:
10705 abort ();
10706 break;
10709 /* If we've got another relocation for the address, keep going
10710 until we reach the last one. */
10711 if (use_saved_addend_p)
10713 addend = value;
10714 continue;
10717 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10718 /* See the comment above about using R_MIPS_64 in the 32-bit
10719 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10720 that calculated the right value. Now, however, we
10721 sign-extend the 32-bit result to 64-bits, and store it as a
10722 64-bit value. We are especially generous here in that we
10723 go to extreme lengths to support this usage on systems with
10724 only a 32-bit VMA. */
10726 bfd_vma sign_bits;
10727 bfd_vma low_bits;
10728 bfd_vma high_bits;
10730 if (value & ((bfd_vma) 1 << 31))
10731 #ifdef BFD64
10732 sign_bits = ((bfd_vma) 1 << 32) - 1;
10733 #else
10734 sign_bits = -1;
10735 #endif
10736 else
10737 sign_bits = 0;
10739 /* If we don't know that we have a 64-bit type,
10740 do two separate stores. */
10741 if (bfd_big_endian (input_bfd))
10743 /* Undo what we did above. */
10744 rel->r_offset -= 4;
10745 /* Store the sign-bits (which are most significant)
10746 first. */
10747 low_bits = sign_bits;
10748 high_bits = value;
10750 else
10752 low_bits = value;
10753 high_bits = sign_bits;
10755 bfd_put_32 (input_bfd, low_bits,
10756 contents + rel->r_offset);
10757 bfd_put_32 (input_bfd, high_bits,
10758 contents + rel->r_offset + 4);
10759 continue;
10762 /* Actually perform the relocation. */
10763 if (! mips_elf_perform_relocation (info, howto, rel, value,
10764 input_bfd, input_section,
10765 contents, cross_mode_jump_p))
10766 return false;
10769 return true;
10772 /* A function that iterates over each entry in la25_stubs and fills
10773 in the code for each one. DATA points to a mips_htab_traverse_info. */
10775 static int
10776 mips_elf_create_la25_stub (void **slot, void *data)
10778 struct mips_htab_traverse_info *hti;
10779 struct mips_elf_link_hash_table *htab;
10780 struct mips_elf_la25_stub *stub;
10781 asection *s;
10782 bfd_byte *loc;
10783 bfd_vma offset, target, target_high, target_low;
10784 bfd_vma branch_pc;
10785 bfd_signed_vma pcrel_offset = 0;
10787 stub = (struct mips_elf_la25_stub *) *slot;
10788 hti = (struct mips_htab_traverse_info *) data;
10789 htab = mips_elf_hash_table (hti->info);
10790 BFD_ASSERT (htab != NULL);
10792 /* Create the section contents, if we haven't already. */
10793 s = stub->stub_section;
10794 loc = s->contents;
10795 if (loc == NULL)
10797 loc = bfd_malloc (s->size);
10798 if (loc == NULL)
10800 hti->error = true;
10801 return false;
10803 s->contents = loc;
10806 /* Work out where in the section this stub should go. */
10807 offset = stub->offset;
10809 /* We add 8 here to account for the LUI/ADDIU instructions
10810 before the branch instruction. This cannot be moved down to
10811 where pcrel_offset is calculated as 's' is updated in
10812 mips_elf_get_la25_target. */
10813 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10815 /* Work out the target address. */
10816 target = mips_elf_get_la25_target (stub, &s);
10817 target += s->output_section->vma + s->output_offset;
10819 target_high = ((target + 0x8000) >> 16) & 0xffff;
10820 target_low = (target & 0xffff);
10822 /* Calculate the PC of the compact branch instruction (for the case where
10823 compact branches are used for either microMIPSR6 or MIPSR6 with
10824 compact branches. Add 4-bytes to account for BC using the PC of the
10825 next instruction as the base. */
10826 pcrel_offset = target - (branch_pc + 4);
10828 if (stub->stub_section != htab->strampoline)
10830 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10831 of the section and write the two instructions at the end. */
10832 memset (loc, 0, offset);
10833 loc += offset;
10834 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10836 bfd_put_micromips_32 (hti->output_bfd,
10837 LA25_LUI_MICROMIPS (target_high),
10838 loc);
10839 bfd_put_micromips_32 (hti->output_bfd,
10840 LA25_ADDIU_MICROMIPS (target_low),
10841 loc + 4);
10843 else
10845 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10846 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10849 else
10851 /* This is trampoline. */
10852 loc += offset;
10853 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10855 bfd_put_micromips_32 (hti->output_bfd,
10856 LA25_LUI_MICROMIPS (target_high), loc);
10857 bfd_put_micromips_32 (hti->output_bfd,
10858 LA25_J_MICROMIPS (target), loc + 4);
10859 bfd_put_micromips_32 (hti->output_bfd,
10860 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10861 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10863 else
10865 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10866 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10868 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10869 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10871 else
10873 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10874 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10876 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10879 return true;
10882 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10883 adjust it appropriately now. */
10885 static void
10886 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10887 const char *name, Elf_Internal_Sym *sym)
10889 /* The linker script takes care of providing names and values for
10890 these, but we must place them into the right sections. */
10891 static const char* const text_section_symbols[] = {
10892 "_ftext",
10893 "_etext",
10894 "__dso_displacement",
10895 "__elf_header",
10896 "__program_header_table",
10897 NULL
10900 static const char* const data_section_symbols[] = {
10901 "_fdata",
10902 "_edata",
10903 "_end",
10904 "_fbss",
10905 NULL
10908 const char* const *p;
10909 int i;
10911 for (i = 0; i < 2; ++i)
10912 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10914 ++p)
10915 if (strcmp (*p, name) == 0)
10917 /* All of these symbols are given type STT_SECTION by the
10918 IRIX6 linker. */
10919 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10920 sym->st_other = STO_PROTECTED;
10922 /* The IRIX linker puts these symbols in special sections. */
10923 if (i == 0)
10924 sym->st_shndx = SHN_MIPS_TEXT;
10925 else
10926 sym->st_shndx = SHN_MIPS_DATA;
10928 break;
10932 /* Finish up dynamic symbol handling. We set the contents of various
10933 dynamic sections here. */
10935 bool
10936 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10937 struct bfd_link_info *info,
10938 struct elf_link_hash_entry *h,
10939 Elf_Internal_Sym *sym)
10941 bfd *dynobj;
10942 asection *sgot;
10943 struct mips_got_info *g, *gg;
10944 const char *name;
10945 int idx;
10946 struct mips_elf_link_hash_table *htab;
10947 struct mips_elf_link_hash_entry *hmips;
10949 htab = mips_elf_hash_table (info);
10950 BFD_ASSERT (htab != NULL);
10951 dynobj = elf_hash_table (info)->dynobj;
10952 hmips = (struct mips_elf_link_hash_entry *) h;
10954 BFD_ASSERT (htab->root.target_os != is_vxworks);
10956 if (h->plt.plist != NULL
10957 && (h->plt.plist->mips_offset != MINUS_ONE
10958 || h->plt.plist->comp_offset != MINUS_ONE))
10960 /* We've decided to create a PLT entry for this symbol. */
10961 bfd_byte *loc;
10962 bfd_vma header_address, got_address;
10963 bfd_vma got_address_high, got_address_low, load;
10964 bfd_vma got_index;
10965 bfd_vma isa_bit;
10967 got_index = h->plt.plist->gotplt_index;
10969 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10970 BFD_ASSERT (h->dynindx != -1);
10971 BFD_ASSERT (htab->root.splt != NULL);
10972 BFD_ASSERT (got_index != MINUS_ONE);
10973 BFD_ASSERT (!h->def_regular);
10975 /* Calculate the address of the PLT header. */
10976 isa_bit = htab->plt_header_is_comp;
10977 header_address = (htab->root.splt->output_section->vma
10978 + htab->root.splt->output_offset + isa_bit);
10980 /* Calculate the address of the .got.plt entry. */
10981 got_address = (htab->root.sgotplt->output_section->vma
10982 + htab->root.sgotplt->output_offset
10983 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10985 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10986 got_address_low = got_address & 0xffff;
10988 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10989 cannot be loaded in two instructions. */
10990 if (ABI_64_P (output_bfd)
10991 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10993 _bfd_error_handler
10994 /* xgettext:c-format */
10995 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10996 "supported; consider using `-Ttext-segment=...'"),
10997 output_bfd,
10998 htab->root.sgotplt->output_section,
10999 (int64_t) got_address);
11000 bfd_set_error (bfd_error_no_error);
11001 return false;
11004 /* Initially point the .got.plt entry at the PLT header. */
11005 loc = (htab->root.sgotplt->contents
11006 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11007 if (ABI_64_P (output_bfd))
11008 bfd_put_64 (output_bfd, header_address, loc);
11009 else
11010 bfd_put_32 (output_bfd, header_address, loc);
11012 /* Now handle the PLT itself. First the standard entry (the order
11013 does not matter, we just have to pick one). */
11014 if (h->plt.plist->mips_offset != MINUS_ONE)
11016 const bfd_vma *plt_entry;
11017 bfd_vma plt_offset;
11019 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11021 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11023 /* Find out where the .plt entry should go. */
11024 loc = htab->root.splt->contents + plt_offset;
11026 /* Pick the load opcode. */
11027 load = MIPS_ELF_LOAD_WORD (output_bfd);
11029 /* Fill in the PLT entry itself. */
11031 if (MIPSR6_P (output_bfd))
11032 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11033 : mipsr6_exec_plt_entry;
11034 else
11035 plt_entry = mips_exec_plt_entry;
11036 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11037 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11038 loc + 4);
11040 if (! LOAD_INTERLOCKS_P (output_bfd)
11041 || (MIPSR6_P (output_bfd) && htab->compact_branches))
11043 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11044 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11046 else
11048 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11049 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11050 loc + 12);
11054 /* Now the compressed entry. They come after any standard ones. */
11055 if (h->plt.plist->comp_offset != MINUS_ONE)
11057 bfd_vma plt_offset;
11059 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11060 + h->plt.plist->comp_offset);
11062 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11064 /* Find out where the .plt entry should go. */
11065 loc = htab->root.splt->contents + plt_offset;
11067 /* Fill in the PLT entry itself. */
11068 if (!MICROMIPS_P (output_bfd))
11070 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11072 bfd_put_16 (output_bfd, plt_entry[0], loc);
11073 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11074 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11075 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11076 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11077 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11078 bfd_put_32 (output_bfd, got_address, loc + 12);
11080 else if (htab->insn32)
11082 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11084 bfd_put_16 (output_bfd, plt_entry[0], loc);
11085 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11086 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11087 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11088 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11089 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11090 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11091 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11093 else
11095 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11096 bfd_signed_vma gotpc_offset;
11097 bfd_vma loc_address;
11099 BFD_ASSERT (got_address % 4 == 0);
11101 loc_address = (htab->root.splt->output_section->vma
11102 + htab->root.splt->output_offset + plt_offset);
11103 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11105 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11106 if (gotpc_offset + 0x1000000 >= 0x2000000)
11108 _bfd_error_handler
11109 /* xgettext:c-format */
11110 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11111 "beyond the range of ADDIUPC"),
11112 output_bfd,
11113 htab->root.sgotplt->output_section,
11114 (int64_t) gotpc_offset,
11115 htab->root.splt->output_section);
11116 bfd_set_error (bfd_error_no_error);
11117 return false;
11119 bfd_put_16 (output_bfd,
11120 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11121 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11122 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11123 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11124 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11125 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11129 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11130 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11131 got_index - 2, h->dynindx,
11132 R_MIPS_JUMP_SLOT, got_address);
11134 /* We distinguish between PLT entries and lazy-binding stubs by
11135 giving the former an st_other value of STO_MIPS_PLT. Set the
11136 flag and leave the value if there are any relocations in the
11137 binary where pointer equality matters. */
11138 sym->st_shndx = SHN_UNDEF;
11139 if (h->pointer_equality_needed)
11140 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11141 else
11143 sym->st_value = 0;
11144 sym->st_other = 0;
11148 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11150 /* We've decided to create a lazy-binding stub. */
11151 bool micromips_p = MICROMIPS_P (output_bfd);
11152 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11153 bfd_vma stub_size = htab->function_stub_size;
11154 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11155 bfd_vma isa_bit = micromips_p;
11156 bfd_vma stub_big_size;
11158 if (!micromips_p)
11159 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11160 else if (htab->insn32)
11161 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11162 else
11163 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11165 /* This symbol has a stub. Set it up. */
11167 BFD_ASSERT (h->dynindx != -1);
11169 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11171 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11172 sign extension at runtime in the stub, resulting in a negative
11173 index value. */
11174 if (h->dynindx & ~0x7fffffff)
11176 _bfd_error_handler
11177 (_("%pB: cannot handle more than %d dynamic symbols"),
11178 output_bfd, 0x7fffffff);
11179 bfd_set_error (bfd_error_bad_value);
11180 return false;
11183 /* Fill the stub. */
11184 if (micromips_p)
11186 idx = 0;
11187 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11188 stub + idx);
11189 idx += 4;
11190 if (htab->insn32)
11192 bfd_put_micromips_32 (output_bfd,
11193 STUB_MOVE32_MICROMIPS, stub + idx);
11194 idx += 4;
11196 else
11198 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11199 idx += 2;
11201 if (stub_size == stub_big_size)
11203 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11205 bfd_put_micromips_32 (output_bfd,
11206 STUB_LUI_MICROMIPS (dynindx_hi),
11207 stub + idx);
11208 idx += 4;
11210 if (htab->insn32)
11212 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11213 stub + idx);
11214 idx += 4;
11216 else
11218 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11219 idx += 2;
11222 /* If a large stub is not required and sign extension is not a
11223 problem, then use legacy code in the stub. */
11224 if (stub_size == stub_big_size)
11225 bfd_put_micromips_32 (output_bfd,
11226 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11227 stub + idx);
11228 else if (h->dynindx & ~0x7fff)
11229 bfd_put_micromips_32 (output_bfd,
11230 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11231 stub + idx);
11232 else
11233 bfd_put_micromips_32 (output_bfd,
11234 STUB_LI16S_MICROMIPS (output_bfd,
11235 h->dynindx),
11236 stub + idx);
11238 else
11240 idx = 0;
11241 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11242 idx += 4;
11243 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11244 idx += 4;
11245 if (stub_size == stub_big_size)
11247 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11248 stub + idx);
11249 idx += 4;
11252 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11254 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11255 idx += 4;
11258 /* If a large stub is not required and sign extension is not a
11259 problem, then use legacy code in the stub. */
11260 if (stub_size == stub_big_size)
11261 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11262 stub + idx);
11263 else if (h->dynindx & ~0x7fff)
11264 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11265 stub + idx);
11266 else
11267 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11268 stub + idx);
11269 idx += 4;
11271 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11272 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11275 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11276 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11277 stub, stub_size);
11279 /* Mark the symbol as undefined. stub_offset != -1 occurs
11280 only for the referenced symbol. */
11281 sym->st_shndx = SHN_UNDEF;
11283 /* The run-time linker uses the st_value field of the symbol
11284 to reset the global offset table entry for this external
11285 to its stub address when unlinking a shared object. */
11286 sym->st_value = (htab->sstubs->output_section->vma
11287 + htab->sstubs->output_offset
11288 + h->plt.plist->stub_offset
11289 + isa_bit);
11290 sym->st_other = other;
11293 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11294 refer to the stub, since only the stub uses the standard calling
11295 conventions. */
11296 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11298 BFD_ASSERT (hmips->need_fn_stub);
11299 sym->st_value = (hmips->fn_stub->output_section->vma
11300 + hmips->fn_stub->output_offset);
11301 sym->st_size = hmips->fn_stub->size;
11302 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11305 BFD_ASSERT (h->dynindx != -1
11306 || h->forced_local);
11308 sgot = htab->root.sgot;
11309 g = htab->got_info;
11310 BFD_ASSERT (g != NULL);
11312 /* Run through the global symbol table, creating GOT entries for all
11313 the symbols that need them. */
11314 if (hmips->global_got_area != GGA_NONE)
11316 bfd_vma offset;
11317 bfd_vma value;
11319 value = sym->st_value;
11320 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11321 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11324 if (hmips->global_got_area != GGA_NONE && g->next)
11326 struct mips_got_entry e, *p;
11327 bfd_vma entry;
11328 bfd_vma offset;
11330 gg = g;
11332 e.abfd = output_bfd;
11333 e.symndx = -1;
11334 e.d.h = hmips;
11335 e.tls_type = GOT_TLS_NONE;
11337 for (g = g->next; g->next != gg; g = g->next)
11339 if (g->got_entries
11340 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11341 &e)))
11343 offset = p->gotidx;
11344 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11345 if (bfd_link_pic (info)
11346 || (elf_hash_table (info)->dynamic_sections_created
11347 && p->d.h != NULL
11348 && p->d.h->root.def_dynamic
11349 && !p->d.h->root.def_regular))
11351 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11352 the various compatibility problems, it's easier to mock
11353 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11354 mips_elf_create_dynamic_relocation to calculate the
11355 appropriate addend. */
11356 Elf_Internal_Rela rel[3];
11358 memset (rel, 0, sizeof (rel));
11359 if (ABI_64_P (output_bfd))
11360 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11361 else
11362 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11363 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11365 entry = 0;
11366 if (! (mips_elf_create_dynamic_relocation
11367 (output_bfd, info, rel,
11368 e.d.h, NULL, sym->st_value, &entry, sgot)))
11369 return false;
11371 else
11372 entry = sym->st_value;
11373 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11378 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11379 name = h->root.root.string;
11380 if (h == elf_hash_table (info)->hdynamic
11381 || h == elf_hash_table (info)->hgot)
11382 sym->st_shndx = SHN_ABS;
11383 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11384 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11386 sym->st_shndx = SHN_ABS;
11387 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11388 sym->st_value = 1;
11390 else if (SGI_COMPAT (output_bfd))
11392 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11393 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11395 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11396 sym->st_other = STO_PROTECTED;
11397 sym->st_value = 0;
11398 sym->st_shndx = SHN_MIPS_DATA;
11400 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11402 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11403 sym->st_other = STO_PROTECTED;
11404 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11405 sym->st_shndx = SHN_ABS;
11407 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11409 if (h->type == STT_FUNC)
11410 sym->st_shndx = SHN_MIPS_TEXT;
11411 else if (h->type == STT_OBJECT)
11412 sym->st_shndx = SHN_MIPS_DATA;
11416 /* Emit a copy reloc, if needed. */
11417 if (h->needs_copy)
11419 asection *s;
11420 bfd_vma symval;
11422 BFD_ASSERT (h->dynindx != -1);
11423 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11425 s = mips_elf_rel_dyn_section (info, false);
11426 symval = (h->root.u.def.section->output_section->vma
11427 + h->root.u.def.section->output_offset
11428 + h->root.u.def.value);
11429 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11430 h->dynindx, R_MIPS_COPY, symval);
11433 /* Handle the IRIX6-specific symbols. */
11434 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11435 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11437 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11438 to treat compressed symbols like any other. */
11439 if (ELF_ST_IS_MIPS16 (sym->st_other))
11441 BFD_ASSERT (sym->st_value & 1);
11442 sym->st_other -= STO_MIPS16;
11444 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11446 BFD_ASSERT (sym->st_value & 1);
11447 sym->st_other -= STO_MICROMIPS;
11450 return true;
11453 /* Likewise, for VxWorks. */
11455 bool
11456 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11457 struct bfd_link_info *info,
11458 struct elf_link_hash_entry *h,
11459 Elf_Internal_Sym *sym)
11461 bfd *dynobj;
11462 asection *sgot;
11463 struct mips_got_info *g;
11464 struct mips_elf_link_hash_table *htab;
11465 struct mips_elf_link_hash_entry *hmips;
11467 htab = mips_elf_hash_table (info);
11468 BFD_ASSERT (htab != NULL);
11469 dynobj = elf_hash_table (info)->dynobj;
11470 hmips = (struct mips_elf_link_hash_entry *) h;
11472 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11474 bfd_byte *loc;
11475 bfd_vma plt_address, got_address, got_offset, branch_offset;
11476 Elf_Internal_Rela rel;
11477 static const bfd_vma *plt_entry;
11478 bfd_vma gotplt_index;
11479 bfd_vma plt_offset;
11481 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11482 gotplt_index = h->plt.plist->gotplt_index;
11484 BFD_ASSERT (h->dynindx != -1);
11485 BFD_ASSERT (htab->root.splt != NULL);
11486 BFD_ASSERT (gotplt_index != MINUS_ONE);
11487 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11489 /* Calculate the address of the .plt entry. */
11490 plt_address = (htab->root.splt->output_section->vma
11491 + htab->root.splt->output_offset
11492 + plt_offset);
11494 /* Calculate the address of the .got.plt entry. */
11495 got_address = (htab->root.sgotplt->output_section->vma
11496 + htab->root.sgotplt->output_offset
11497 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11499 /* Calculate the offset of the .got.plt entry from
11500 _GLOBAL_OFFSET_TABLE_. */
11501 got_offset = mips_elf_gotplt_index (info, h);
11503 /* Calculate the offset for the branch at the start of the PLT
11504 entry. The branch jumps to the beginning of .plt. */
11505 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11507 /* Fill in the initial value of the .got.plt entry. */
11508 bfd_put_32 (output_bfd, plt_address,
11509 (htab->root.sgotplt->contents
11510 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11512 /* Find out where the .plt entry should go. */
11513 loc = htab->root.splt->contents + plt_offset;
11515 if (bfd_link_pic (info))
11517 plt_entry = mips_vxworks_shared_plt_entry;
11518 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11519 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11521 else
11523 bfd_vma got_address_high, got_address_low;
11525 plt_entry = mips_vxworks_exec_plt_entry;
11526 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11527 got_address_low = got_address & 0xffff;
11529 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11530 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11531 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11532 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11533 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11534 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11535 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11536 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11538 loc = (htab->srelplt2->contents
11539 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11541 /* Emit a relocation for the .got.plt entry. */
11542 rel.r_offset = got_address;
11543 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11544 rel.r_addend = plt_offset;
11545 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11547 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11548 loc += sizeof (Elf32_External_Rela);
11549 rel.r_offset = plt_address + 8;
11550 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11551 rel.r_addend = got_offset;
11552 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11554 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11555 loc += sizeof (Elf32_External_Rela);
11556 rel.r_offset += 4;
11557 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11558 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11561 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11562 loc = (htab->root.srelplt->contents
11563 + gotplt_index * sizeof (Elf32_External_Rela));
11564 rel.r_offset = got_address;
11565 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11566 rel.r_addend = 0;
11567 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11569 if (!h->def_regular)
11570 sym->st_shndx = SHN_UNDEF;
11573 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11575 sgot = htab->root.sgot;
11576 g = htab->got_info;
11577 BFD_ASSERT (g != NULL);
11579 /* See if this symbol has an entry in the GOT. */
11580 if (hmips->global_got_area != GGA_NONE)
11582 bfd_vma offset;
11583 Elf_Internal_Rela outrel;
11584 bfd_byte *loc;
11585 asection *s;
11587 /* Install the symbol value in the GOT. */
11588 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11589 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11591 /* Add a dynamic relocation for it. */
11592 s = mips_elf_rel_dyn_section (info, false);
11593 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11594 outrel.r_offset = (sgot->output_section->vma
11595 + sgot->output_offset
11596 + offset);
11597 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11598 outrel.r_addend = 0;
11599 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11602 /* Emit a copy reloc, if needed. */
11603 if (h->needs_copy)
11605 Elf_Internal_Rela rel;
11606 asection *srel;
11607 bfd_byte *loc;
11609 BFD_ASSERT (h->dynindx != -1);
11611 rel.r_offset = (h->root.u.def.section->output_section->vma
11612 + h->root.u.def.section->output_offset
11613 + h->root.u.def.value);
11614 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11615 rel.r_addend = 0;
11616 if (h->root.u.def.section == htab->root.sdynrelro)
11617 srel = htab->root.sreldynrelro;
11618 else
11619 srel = htab->root.srelbss;
11620 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11621 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11622 ++srel->reloc_count;
11625 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11626 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11627 sym->st_value &= ~1;
11629 return true;
11632 /* Write out a plt0 entry to the beginning of .plt. */
11634 static bool
11635 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11637 bfd_byte *loc;
11638 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11639 static const bfd_vma *plt_entry;
11640 struct mips_elf_link_hash_table *htab;
11642 htab = mips_elf_hash_table (info);
11643 BFD_ASSERT (htab != NULL);
11645 if (ABI_64_P (output_bfd))
11646 plt_entry = (htab->compact_branches
11647 ? mipsr6_n64_exec_plt0_entry_compact
11648 : mips_n64_exec_plt0_entry);
11649 else if (ABI_N32_P (output_bfd))
11650 plt_entry = (htab->compact_branches
11651 ? mipsr6_n32_exec_plt0_entry_compact
11652 : mips_n32_exec_plt0_entry);
11653 else if (!htab->plt_header_is_comp)
11654 plt_entry = (htab->compact_branches
11655 ? mipsr6_o32_exec_plt0_entry_compact
11656 : mips_o32_exec_plt0_entry);
11657 else if (htab->insn32)
11658 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11659 else
11660 plt_entry = micromips_o32_exec_plt0_entry;
11662 /* Calculate the value of .got.plt. */
11663 gotplt_value = (htab->root.sgotplt->output_section->vma
11664 + htab->root.sgotplt->output_offset);
11665 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11666 gotplt_value_low = gotplt_value & 0xffff;
11668 /* The PLT sequence is not safe for N64 if .got.plt's address can
11669 not be loaded in two instructions. */
11670 if (ABI_64_P (output_bfd)
11671 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11673 _bfd_error_handler
11674 /* xgettext:c-format */
11675 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11676 "supported; consider using `-Ttext-segment=...'"),
11677 output_bfd,
11678 htab->root.sgotplt->output_section,
11679 (int64_t) gotplt_value);
11680 bfd_set_error (bfd_error_no_error);
11681 return false;
11684 /* Install the PLT header. */
11685 loc = htab->root.splt->contents;
11686 if (plt_entry == micromips_o32_exec_plt0_entry)
11688 bfd_vma gotpc_offset;
11689 bfd_vma loc_address;
11690 size_t i;
11692 BFD_ASSERT (gotplt_value % 4 == 0);
11694 loc_address = (htab->root.splt->output_section->vma
11695 + htab->root.splt->output_offset);
11696 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11698 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11699 if (gotpc_offset + 0x1000000 >= 0x2000000)
11701 _bfd_error_handler
11702 /* xgettext:c-format */
11703 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11704 "beyond the range of ADDIUPC"),
11705 output_bfd,
11706 htab->root.sgotplt->output_section,
11707 (int64_t) gotpc_offset,
11708 htab->root.splt->output_section);
11709 bfd_set_error (bfd_error_no_error);
11710 return false;
11712 bfd_put_16 (output_bfd,
11713 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11714 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11715 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11716 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11718 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11720 size_t i;
11722 bfd_put_16 (output_bfd, plt_entry[0], loc);
11723 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11724 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11725 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11726 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11727 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11728 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11729 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11731 else
11733 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11734 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11735 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11736 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11737 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11738 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11739 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11740 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11743 return true;
11746 /* Install the PLT header for a VxWorks executable and finalize the
11747 contents of .rela.plt.unloaded. */
11749 static void
11750 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11752 Elf_Internal_Rela rela;
11753 bfd_byte *loc;
11754 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11755 static const bfd_vma *plt_entry;
11756 struct mips_elf_link_hash_table *htab;
11758 htab = mips_elf_hash_table (info);
11759 BFD_ASSERT (htab != NULL);
11761 plt_entry = mips_vxworks_exec_plt0_entry;
11763 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11764 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11765 + htab->root.hgot->root.u.def.section->output_offset
11766 + htab->root.hgot->root.u.def.value);
11768 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11769 got_value_low = got_value & 0xffff;
11771 /* Calculate the address of the PLT header. */
11772 plt_address = (htab->root.splt->output_section->vma
11773 + htab->root.splt->output_offset);
11775 /* Install the PLT header. */
11776 loc = htab->root.splt->contents;
11777 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11778 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11779 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11780 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11781 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11782 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11784 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11785 loc = htab->srelplt2->contents;
11786 rela.r_offset = plt_address;
11787 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11788 rela.r_addend = 0;
11789 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11790 loc += sizeof (Elf32_External_Rela);
11792 /* Output the relocation for the following addiu of
11793 %lo(_GLOBAL_OFFSET_TABLE_). */
11794 rela.r_offset += 4;
11795 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11796 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11797 loc += sizeof (Elf32_External_Rela);
11799 /* Fix up the remaining relocations. They may have the wrong
11800 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11801 in which symbols were output. */
11802 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11804 Elf_Internal_Rela rel;
11806 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11807 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11808 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11809 loc += sizeof (Elf32_External_Rela);
11811 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11812 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11813 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11814 loc += sizeof (Elf32_External_Rela);
11816 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11817 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11818 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11819 loc += sizeof (Elf32_External_Rela);
11823 /* Install the PLT header for a VxWorks shared library. */
11825 static void
11826 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11828 unsigned int i;
11829 struct mips_elf_link_hash_table *htab;
11831 htab = mips_elf_hash_table (info);
11832 BFD_ASSERT (htab != NULL);
11834 /* We just need to copy the entry byte-by-byte. */
11835 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11836 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11837 htab->root.splt->contents + i * 4);
11840 /* Finish up the dynamic sections. */
11842 bool
11843 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11844 struct bfd_link_info *info)
11846 bfd *dynobj;
11847 asection *sdyn;
11848 asection *sgot;
11849 struct mips_got_info *gg, *g;
11850 struct mips_elf_link_hash_table *htab;
11852 htab = mips_elf_hash_table (info);
11853 BFD_ASSERT (htab != NULL);
11855 dynobj = elf_hash_table (info)->dynobj;
11857 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11859 sgot = htab->root.sgot;
11860 gg = htab->got_info;
11862 if (elf_hash_table (info)->dynamic_sections_created)
11864 bfd_byte *b;
11865 int dyn_to_skip = 0, dyn_skipped = 0;
11867 BFD_ASSERT (sdyn != NULL);
11868 BFD_ASSERT (gg != NULL);
11870 g = mips_elf_bfd_got (output_bfd, false);
11871 BFD_ASSERT (g != NULL);
11873 for (b = sdyn->contents;
11874 b < sdyn->contents + sdyn->size;
11875 b += MIPS_ELF_DYN_SIZE (dynobj))
11877 Elf_Internal_Dyn dyn;
11878 const char *name;
11879 size_t elemsize;
11880 asection *s;
11881 bool swap_out_p;
11883 /* Read in the current dynamic entry. */
11884 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11886 /* Assume that we're going to modify it and write it out. */
11887 swap_out_p = true;
11889 switch (dyn.d_tag)
11891 case DT_RELENT:
11892 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11893 break;
11895 case DT_RELAENT:
11896 BFD_ASSERT (htab->root.target_os == is_vxworks);
11897 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11898 break;
11900 case DT_STRSZ:
11901 /* Rewrite DT_STRSZ. */
11902 dyn.d_un.d_val =
11903 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11904 break;
11906 case DT_PLTGOT:
11907 s = htab->root.sgot;
11908 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11909 break;
11911 case DT_MIPS_PLTGOT:
11912 s = htab->root.sgotplt;
11913 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11914 break;
11916 case DT_MIPS_RLD_VERSION:
11917 dyn.d_un.d_val = 1; /* XXX */
11918 break;
11920 case DT_MIPS_FLAGS:
11921 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11922 break;
11924 case DT_MIPS_TIME_STAMP:
11926 time_t t;
11927 time (&t);
11928 dyn.d_un.d_val = t;
11930 break;
11932 case DT_MIPS_ICHECKSUM:
11933 /* XXX FIXME: */
11934 swap_out_p = false;
11935 break;
11937 case DT_MIPS_IVERSION:
11938 /* XXX FIXME: */
11939 swap_out_p = false;
11940 break;
11942 case DT_MIPS_BASE_ADDRESS:
11943 s = output_bfd->sections;
11944 BFD_ASSERT (s != NULL);
11945 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11946 break;
11948 case DT_MIPS_LOCAL_GOTNO:
11949 dyn.d_un.d_val = g->local_gotno;
11950 break;
11952 case DT_MIPS_UNREFEXTNO:
11953 /* The index into the dynamic symbol table which is the
11954 entry of the first external symbol that is not
11955 referenced within the same object. */
11956 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11957 break;
11959 case DT_MIPS_GOTSYM:
11960 if (htab->global_gotsym)
11962 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11963 break;
11965 /* In case if we don't have global got symbols we default
11966 to setting DT_MIPS_GOTSYM to the same value as
11967 DT_MIPS_SYMTABNO. */
11968 /* Fall through. */
11970 case DT_MIPS_SYMTABNO:
11971 name = ".dynsym";
11972 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11973 s = bfd_get_linker_section (dynobj, name);
11975 if (s != NULL)
11976 dyn.d_un.d_val = s->size / elemsize;
11977 else
11978 dyn.d_un.d_val = 0;
11979 break;
11981 case DT_MIPS_HIPAGENO:
11982 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11983 break;
11985 case DT_MIPS_RLD_MAP:
11987 struct elf_link_hash_entry *h;
11988 h = mips_elf_hash_table (info)->rld_symbol;
11989 if (!h)
11991 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11992 swap_out_p = false;
11993 break;
11995 s = h->root.u.def.section;
11997 /* The MIPS_RLD_MAP tag stores the absolute address of the
11998 debug pointer. */
11999 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12000 + h->root.u.def.value);
12002 break;
12004 case DT_MIPS_RLD_MAP_REL:
12006 struct elf_link_hash_entry *h;
12007 bfd_vma dt_addr, rld_addr;
12008 h = mips_elf_hash_table (info)->rld_symbol;
12009 if (!h)
12011 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12012 swap_out_p = false;
12013 break;
12015 s = h->root.u.def.section;
12017 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12018 pointer, relative to the address of the tag. */
12019 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12020 + (b - sdyn->contents));
12021 rld_addr = (s->output_section->vma + s->output_offset
12022 + h->root.u.def.value);
12023 dyn.d_un.d_ptr = rld_addr - dt_addr;
12025 break;
12027 case DT_MIPS_OPTIONS:
12028 s = (bfd_get_section_by_name
12029 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12030 dyn.d_un.d_ptr = s->vma;
12031 break;
12033 case DT_PLTREL:
12034 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12035 if (htab->root.target_os == is_vxworks)
12036 dyn.d_un.d_val = DT_RELA;
12037 else
12038 dyn.d_un.d_val = DT_REL;
12039 break;
12041 case DT_PLTRELSZ:
12042 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12043 dyn.d_un.d_val = htab->root.srelplt->size;
12044 break;
12046 case DT_JMPREL:
12047 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12048 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12049 + htab->root.srelplt->output_offset);
12050 break;
12052 case DT_TEXTREL:
12053 /* If we didn't need any text relocations after all, delete
12054 the dynamic tag. */
12055 if (!(info->flags & DF_TEXTREL))
12057 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12058 swap_out_p = false;
12060 break;
12062 case DT_FLAGS:
12063 /* If we didn't need any text relocations after all, clear
12064 DF_TEXTREL from DT_FLAGS. */
12065 if (!(info->flags & DF_TEXTREL))
12066 dyn.d_un.d_val &= ~DF_TEXTREL;
12067 else
12068 swap_out_p = false;
12069 break;
12071 case DT_MIPS_XHASH:
12072 name = ".MIPS.xhash";
12073 s = bfd_get_linker_section (dynobj, name);
12074 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12075 break;
12077 default:
12078 swap_out_p = false;
12079 if (htab->root.target_os == is_vxworks
12080 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12081 swap_out_p = true;
12082 break;
12085 if (swap_out_p || dyn_skipped)
12086 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12087 (dynobj, &dyn, b - dyn_skipped);
12089 if (dyn_to_skip)
12091 dyn_skipped += dyn_to_skip;
12092 dyn_to_skip = 0;
12096 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12097 if (dyn_skipped > 0)
12098 memset (b - dyn_skipped, 0, dyn_skipped);
12101 if (sgot != NULL && sgot->size > 0
12102 && !bfd_is_abs_section (sgot->output_section))
12104 if (htab->root.target_os == is_vxworks)
12106 /* The first entry of the global offset table points to the
12107 ".dynamic" section. The second is initialized by the
12108 loader and contains the shared library identifier.
12109 The third is also initialized by the loader and points
12110 to the lazy resolution stub. */
12111 MIPS_ELF_PUT_WORD (output_bfd,
12112 sdyn->output_offset + sdyn->output_section->vma,
12113 sgot->contents);
12114 MIPS_ELF_PUT_WORD (output_bfd, 0,
12115 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12116 MIPS_ELF_PUT_WORD (output_bfd, 0,
12117 sgot->contents
12118 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12120 else
12122 /* The first entry of the global offset table will be filled at
12123 runtime. The second entry will be used by some runtime loaders.
12124 This isn't the case of IRIX rld. */
12125 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12126 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12127 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12130 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12131 = MIPS_ELF_GOT_SIZE (output_bfd);
12134 /* Generate dynamic relocations for the non-primary gots. */
12135 if (gg != NULL && gg->next)
12137 Elf_Internal_Rela rel[3];
12138 bfd_vma addend = 0;
12140 memset (rel, 0, sizeof (rel));
12141 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12143 for (g = gg->next; g->next != gg; g = g->next)
12145 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12146 + g->next->tls_gotno;
12148 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12149 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12150 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12151 sgot->contents
12152 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12154 if (! bfd_link_pic (info))
12155 continue;
12157 for (; got_index < g->local_gotno; got_index++)
12159 if (got_index >= g->assigned_low_gotno
12160 && got_index <= g->assigned_high_gotno)
12161 continue;
12163 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12164 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12165 if (!(mips_elf_create_dynamic_relocation
12166 (output_bfd, info, rel, NULL,
12167 bfd_abs_section_ptr,
12168 0, &addend, sgot)))
12169 return false;
12170 BFD_ASSERT (addend == 0);
12175 /* The generation of dynamic relocations for the non-primary gots
12176 adds more dynamic relocations. We cannot count them until
12177 here. */
12179 if (elf_hash_table (info)->dynamic_sections_created)
12181 bfd_byte *b;
12182 bool swap_out_p;
12184 BFD_ASSERT (sdyn != NULL);
12186 for (b = sdyn->contents;
12187 b < sdyn->contents + sdyn->size;
12188 b += MIPS_ELF_DYN_SIZE (dynobj))
12190 Elf_Internal_Dyn dyn;
12191 asection *s;
12193 /* Read in the current dynamic entry. */
12194 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12196 /* Assume that we're going to modify it and write it out. */
12197 swap_out_p = true;
12199 switch (dyn.d_tag)
12201 case DT_RELSZ:
12202 /* Reduce DT_RELSZ to account for any relocations we
12203 decided not to make. This is for the n64 irix rld,
12204 which doesn't seem to apply any relocations if there
12205 are trailing null entries. */
12206 s = mips_elf_rel_dyn_section (info, false);
12207 dyn.d_un.d_val = (s->reloc_count
12208 * (ABI_64_P (output_bfd)
12209 ? sizeof (Elf64_Mips_External_Rel)
12210 : sizeof (Elf32_External_Rel)));
12211 /* Adjust the section size too. Tools like the prelinker
12212 can reasonably expect the values to the same. */
12213 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12214 elf_section_data (s->output_section)->this_hdr.sh_size
12215 = dyn.d_un.d_val;
12216 break;
12218 default:
12219 swap_out_p = false;
12220 break;
12223 if (swap_out_p)
12224 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12225 (dynobj, &dyn, b);
12230 asection *s;
12231 Elf32_compact_rel cpt;
12233 if (SGI_COMPAT (output_bfd))
12235 /* Write .compact_rel section out. */
12236 s = bfd_get_linker_section (dynobj, ".compact_rel");
12237 if (s != NULL)
12239 cpt.id1 = 1;
12240 cpt.num = s->reloc_count;
12241 cpt.id2 = 2;
12242 cpt.offset = (s->output_section->filepos
12243 + sizeof (Elf32_External_compact_rel));
12244 cpt.reserved0 = 0;
12245 cpt.reserved1 = 0;
12246 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12247 ((Elf32_External_compact_rel *)
12248 s->contents));
12250 /* Clean up a dummy stub function entry in .text. */
12251 if (htab->sstubs != NULL
12252 && htab->sstubs->contents != NULL)
12254 file_ptr dummy_offset;
12256 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12257 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12258 memset (htab->sstubs->contents + dummy_offset, 0,
12259 htab->function_stub_size);
12264 /* The psABI says that the dynamic relocations must be sorted in
12265 increasing order of r_symndx. The VxWorks EABI doesn't require
12266 this, and because the code below handles REL rather than RELA
12267 relocations, using it for VxWorks would be outright harmful. */
12268 if (htab->root.target_os != is_vxworks)
12270 s = mips_elf_rel_dyn_section (info, false);
12271 if (s != NULL
12272 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12274 reldyn_sorting_bfd = output_bfd;
12276 if (ABI_64_P (output_bfd))
12277 qsort ((Elf64_External_Rel *) s->contents + 1,
12278 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12279 sort_dynamic_relocs_64);
12280 else
12281 qsort ((Elf32_External_Rel *) s->contents + 1,
12282 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12283 sort_dynamic_relocs);
12288 if (htab->root.splt && htab->root.splt->size > 0)
12290 if (htab->root.target_os == is_vxworks)
12292 if (bfd_link_pic (info))
12293 mips_vxworks_finish_shared_plt (output_bfd, info);
12294 else
12295 mips_vxworks_finish_exec_plt (output_bfd, info);
12297 else
12299 BFD_ASSERT (!bfd_link_pic (info));
12300 if (!mips_finish_exec_plt (output_bfd, info))
12301 return false;
12304 return true;
12308 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12310 static void
12311 mips_set_isa_flags (bfd *abfd)
12313 flagword val;
12315 switch (bfd_get_mach (abfd))
12317 default:
12318 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12319 val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
12320 else
12321 val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
12322 break;
12324 case bfd_mach_mips3000:
12325 val = EF_MIPS_ARCH_1;
12326 break;
12328 case bfd_mach_mips3900:
12329 val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
12330 break;
12332 case bfd_mach_mips6000:
12333 val = EF_MIPS_ARCH_2;
12334 break;
12336 case bfd_mach_mips4010:
12337 val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
12338 break;
12340 case bfd_mach_mips_allegrex:
12341 val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
12342 break;
12344 case bfd_mach_mips4000:
12345 case bfd_mach_mips4300:
12346 case bfd_mach_mips4400:
12347 case bfd_mach_mips4600:
12348 val = EF_MIPS_ARCH_3;
12349 break;
12351 case bfd_mach_mips4100:
12352 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
12353 break;
12355 case bfd_mach_mips4111:
12356 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
12357 break;
12359 case bfd_mach_mips4120:
12360 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
12361 break;
12363 case bfd_mach_mips4650:
12364 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
12365 break;
12367 case bfd_mach_mips5400:
12368 val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
12369 break;
12371 case bfd_mach_mips5500:
12372 val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
12373 break;
12375 case bfd_mach_mips5900:
12376 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
12377 break;
12379 case bfd_mach_mips9000:
12380 val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
12381 break;
12383 case bfd_mach_mips5000:
12384 case bfd_mach_mips7000:
12385 case bfd_mach_mips8000:
12386 case bfd_mach_mips10000:
12387 case bfd_mach_mips12000:
12388 case bfd_mach_mips14000:
12389 case bfd_mach_mips16000:
12390 val = EF_MIPS_ARCH_4;
12391 break;
12393 case bfd_mach_mips5:
12394 val = EF_MIPS_ARCH_5;
12395 break;
12397 case bfd_mach_mips_loongson_2e:
12398 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
12399 break;
12401 case bfd_mach_mips_loongson_2f:
12402 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
12403 break;
12405 case bfd_mach_mips_sb1:
12406 val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
12407 break;
12409 case bfd_mach_mips_gs464:
12410 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
12411 break;
12413 case bfd_mach_mips_gs464e:
12414 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
12415 break;
12417 case bfd_mach_mips_gs264e:
12418 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
12419 break;
12421 case bfd_mach_mips_octeon:
12422 case bfd_mach_mips_octeonp:
12423 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
12424 break;
12426 case bfd_mach_mips_octeon3:
12427 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
12428 break;
12430 case bfd_mach_mips_xlr:
12431 val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
12432 break;
12434 case bfd_mach_mips_octeon2:
12435 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
12436 break;
12438 case bfd_mach_mipsisa32:
12439 val = EF_MIPS_ARCH_32;
12440 break;
12442 case bfd_mach_mipsisa64:
12443 val = EF_MIPS_ARCH_64;
12444 break;
12446 case bfd_mach_mipsisa32r2:
12447 case bfd_mach_mipsisa32r3:
12448 case bfd_mach_mipsisa32r5:
12449 val = EF_MIPS_ARCH_32R2;
12450 break;
12452 case bfd_mach_mips_interaptiv_mr2:
12453 val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
12454 break;
12456 case bfd_mach_mipsisa64r2:
12457 case bfd_mach_mipsisa64r3:
12458 case bfd_mach_mipsisa64r5:
12459 val = EF_MIPS_ARCH_64R2;
12460 break;
12462 case bfd_mach_mipsisa32r6:
12463 val = EF_MIPS_ARCH_32R6;
12464 break;
12466 case bfd_mach_mipsisa64r6:
12467 val = EF_MIPS_ARCH_64R6;
12468 break;
12470 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12471 elf_elfheader (abfd)->e_flags |= val;
12476 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12477 Don't do so for code sections. We want to keep ordering of HI16/LO16
12478 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12479 relocs to be sorted. */
12481 bool
12482 _bfd_mips_elf_sort_relocs_p (asection *sec)
12484 return (sec->flags & SEC_CODE) == 0;
12488 /* The final processing done just before writing out a MIPS ELF object
12489 file. This gets the MIPS architecture right based on the machine
12490 number. This is used by both the 32-bit and the 64-bit ABI. */
12492 void
12493 _bfd_mips_final_write_processing (bfd *abfd)
12495 unsigned int i;
12496 Elf_Internal_Shdr **hdrpp;
12497 const char *name;
12498 asection *sec;
12500 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12501 is nonzero. This is for compatibility with old objects, which used
12502 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12503 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12504 mips_set_isa_flags (abfd);
12506 /* Set the sh_info field for .gptab sections and other appropriate
12507 info for each special section. */
12508 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12509 i < elf_numsections (abfd);
12510 i++, hdrpp++)
12512 switch ((*hdrpp)->sh_type)
12514 case SHT_MIPS_MSYM:
12515 case SHT_MIPS_LIBLIST:
12516 sec = bfd_get_section_by_name (abfd, ".dynstr");
12517 if (sec != NULL)
12518 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12519 break;
12521 case SHT_MIPS_GPTAB:
12522 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12523 name = bfd_section_name ((*hdrpp)->bfd_section);
12524 if (startswith (name, ".gptab."))
12526 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12527 if (sec != NULL)
12528 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12530 break;
12532 case SHT_MIPS_CONTENT:
12533 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12534 name = bfd_section_name ((*hdrpp)->bfd_section);
12535 if (startswith (name, ".MIPS.content"))
12537 sec = bfd_get_section_by_name (abfd,
12538 name + sizeof ".MIPS.content" - 1);
12539 if (sec != NULL)
12540 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12542 break;
12544 case SHT_MIPS_SYMBOL_LIB:
12545 sec = bfd_get_section_by_name (abfd, ".dynsym");
12546 if (sec != NULL)
12547 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12548 sec = bfd_get_section_by_name (abfd, ".liblist");
12549 if (sec != NULL)
12550 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12551 break;
12553 case SHT_MIPS_EVENTS:
12554 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12555 name = bfd_section_name ((*hdrpp)->bfd_section);
12556 if (startswith (name, ".MIPS.events"))
12557 sec = bfd_get_section_by_name (abfd,
12558 name + sizeof ".MIPS.events" - 1);
12559 else if (startswith (name, ".MIPS.post_rel"))
12560 sec = bfd_get_section_by_name (abfd,
12561 name + sizeof ".MIPS.post_rel" - 1);
12562 else
12563 sec = NULL;
12564 if (sec != NULL)
12565 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12566 break;
12568 case SHT_MIPS_XHASH:
12569 sec = bfd_get_section_by_name (abfd, ".dynsym");
12570 if (sec != NULL)
12571 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12576 bool
12577 _bfd_mips_elf_final_write_processing (bfd *abfd)
12579 _bfd_mips_final_write_processing (abfd);
12580 return _bfd_elf_final_write_processing (abfd);
12583 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12584 segments. */
12587 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12588 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12590 asection *s;
12591 int ret = 0;
12593 /* See if we need a PT_MIPS_REGINFO segment. */
12594 s = bfd_get_section_by_name (abfd, ".reginfo");
12595 if (s && (s->flags & SEC_LOAD))
12596 ++ret;
12598 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12599 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12600 ++ret;
12602 /* See if we need a PT_MIPS_OPTIONS segment. */
12603 if (IRIX_COMPAT (abfd) == ict_irix6
12604 && bfd_get_section_by_name (abfd,
12605 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12606 ++ret;
12608 /* See if we need a PT_MIPS_RTPROC segment. */
12609 if (IRIX_COMPAT (abfd) == ict_irix5
12610 && bfd_get_section_by_name (abfd, ".dynamic")
12611 && bfd_get_section_by_name (abfd, ".mdebug"))
12612 ++ret;
12614 /* Allocate a PT_NULL header in dynamic objects. See
12615 _bfd_mips_elf_modify_segment_map for details. */
12616 if (!SGI_COMPAT (abfd)
12617 && bfd_get_section_by_name (abfd, ".dynamic"))
12618 ++ret;
12620 return ret;
12623 /* Modify the segment map for an IRIX5 executable. */
12625 bool
12626 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12627 struct bfd_link_info *info)
12629 asection *s;
12630 struct elf_segment_map *m, **pm;
12631 size_t amt;
12633 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12634 segment. */
12635 s = bfd_get_section_by_name (abfd, ".reginfo");
12636 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12638 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12639 if (m->p_type == PT_MIPS_REGINFO)
12640 break;
12641 if (m == NULL)
12643 amt = sizeof *m;
12644 m = bfd_zalloc (abfd, amt);
12645 if (m == NULL)
12646 return false;
12648 m->p_type = PT_MIPS_REGINFO;
12649 m->count = 1;
12650 m->sections[0] = s;
12652 /* We want to put it after the PHDR and INTERP segments. */
12653 pm = &elf_seg_map (abfd);
12654 while (*pm != NULL
12655 && ((*pm)->p_type == PT_PHDR
12656 || (*pm)->p_type == PT_INTERP))
12657 pm = &(*pm)->next;
12659 m->next = *pm;
12660 *pm = m;
12664 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12665 segment. */
12666 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12667 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12669 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12670 if (m->p_type == PT_MIPS_ABIFLAGS)
12671 break;
12672 if (m == NULL)
12674 amt = sizeof *m;
12675 m = bfd_zalloc (abfd, amt);
12676 if (m == NULL)
12677 return false;
12679 m->p_type = PT_MIPS_ABIFLAGS;
12680 m->count = 1;
12681 m->sections[0] = s;
12683 /* We want to put it after the PHDR and INTERP segments. */
12684 pm = &elf_seg_map (abfd);
12685 while (*pm != NULL
12686 && ((*pm)->p_type == PT_PHDR
12687 || (*pm)->p_type == PT_INTERP))
12688 pm = &(*pm)->next;
12690 m->next = *pm;
12691 *pm = m;
12695 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12696 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12697 PT_MIPS_OPTIONS segment immediately following the program header
12698 table. */
12699 if (NEWABI_P (abfd)
12700 /* On non-IRIX6 new abi, we'll have already created a segment
12701 for this section, so don't create another. I'm not sure this
12702 is not also the case for IRIX 6, but I can't test it right
12703 now. */
12704 && IRIX_COMPAT (abfd) == ict_irix6)
12706 for (s = abfd->sections; s; s = s->next)
12707 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12708 break;
12710 if (s)
12712 struct elf_segment_map *options_segment;
12714 pm = &elf_seg_map (abfd);
12715 while (*pm != NULL
12716 && ((*pm)->p_type == PT_PHDR
12717 || (*pm)->p_type == PT_INTERP))
12718 pm = &(*pm)->next;
12720 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12722 amt = sizeof (struct elf_segment_map);
12723 options_segment = bfd_zalloc (abfd, amt);
12724 options_segment->next = *pm;
12725 options_segment->p_type = PT_MIPS_OPTIONS;
12726 options_segment->p_flags = PF_R;
12727 options_segment->p_flags_valid = true;
12728 options_segment->count = 1;
12729 options_segment->sections[0] = s;
12730 *pm = options_segment;
12734 else
12736 if (IRIX_COMPAT (abfd) == ict_irix5)
12738 /* If there are .dynamic and .mdebug sections, we make a room
12739 for the RTPROC header. FIXME: Rewrite without section names. */
12740 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12741 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12742 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12744 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12745 if (m->p_type == PT_MIPS_RTPROC)
12746 break;
12747 if (m == NULL)
12749 amt = sizeof *m;
12750 m = bfd_zalloc (abfd, amt);
12751 if (m == NULL)
12752 return false;
12754 m->p_type = PT_MIPS_RTPROC;
12756 s = bfd_get_section_by_name (abfd, ".rtproc");
12757 if (s == NULL)
12759 m->count = 0;
12760 m->p_flags = 0;
12761 m->p_flags_valid = 1;
12763 else
12765 m->count = 1;
12766 m->sections[0] = s;
12769 /* We want to put it after the DYNAMIC segment. */
12770 pm = &elf_seg_map (abfd);
12771 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12772 pm = &(*pm)->next;
12773 if (*pm != NULL)
12774 pm = &(*pm)->next;
12776 m->next = *pm;
12777 *pm = m;
12781 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12782 .dynstr, .dynsym, and .hash sections, and everything in
12783 between. */
12784 for (pm = &elf_seg_map (abfd); *pm != NULL;
12785 pm = &(*pm)->next)
12786 if ((*pm)->p_type == PT_DYNAMIC)
12787 break;
12788 m = *pm;
12789 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12790 glibc's dynamic linker has traditionally derived the number of
12791 tags from the p_filesz field, and sometimes allocates stack
12792 arrays of that size. An overly-big PT_DYNAMIC segment can
12793 be actively harmful in such cases. Making PT_DYNAMIC contain
12794 other sections can also make life hard for the prelinker,
12795 which might move one of the other sections to a different
12796 PT_LOAD segment. */
12797 if (SGI_COMPAT (abfd)
12798 && m != NULL
12799 && m->count == 1
12800 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12802 static const char *sec_names[] =
12804 ".dynamic", ".dynstr", ".dynsym", ".hash"
12806 bfd_vma low, high;
12807 unsigned int i, c;
12808 struct elf_segment_map *n;
12810 low = ~(bfd_vma) 0;
12811 high = 0;
12812 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12814 s = bfd_get_section_by_name (abfd, sec_names[i]);
12815 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12817 bfd_size_type sz;
12819 if (low > s->vma)
12820 low = s->vma;
12821 sz = s->size;
12822 if (high < s->vma + sz)
12823 high = s->vma + sz;
12827 c = 0;
12828 for (s = abfd->sections; s != NULL; s = s->next)
12829 if ((s->flags & SEC_LOAD) != 0
12830 && s->vma >= low
12831 && s->vma + s->size <= high)
12832 ++c;
12834 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12835 n = bfd_zalloc (abfd, amt);
12836 if (n == NULL)
12837 return false;
12838 *n = *m;
12839 n->count = c;
12841 i = 0;
12842 for (s = abfd->sections; s != NULL; s = s->next)
12844 if ((s->flags & SEC_LOAD) != 0
12845 && s->vma >= low
12846 && s->vma + s->size <= high)
12848 n->sections[i] = s;
12849 ++i;
12853 *pm = n;
12857 /* Allocate a spare program header in dynamic objects so that tools
12858 like the prelinker can add an extra PT_LOAD entry.
12860 If the prelinker needs to make room for a new PT_LOAD entry, its
12861 standard procedure is to move the first (read-only) sections into
12862 the new (writable) segment. However, the MIPS ABI requires
12863 .dynamic to be in a read-only segment, and the section will often
12864 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12866 Although the prelinker could in principle move .dynamic to a
12867 writable segment, it seems better to allocate a spare program
12868 header instead, and avoid the need to move any sections.
12869 There is a long tradition of allocating spare dynamic tags,
12870 so allocating a spare program header seems like a natural
12871 extension.
12873 If INFO is NULL, we may be copying an already prelinked binary
12874 with objcopy or strip, so do not add this header. */
12875 if (info != NULL
12876 && !SGI_COMPAT (abfd)
12877 && bfd_get_section_by_name (abfd, ".dynamic"))
12879 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12880 if ((*pm)->p_type == PT_NULL)
12881 break;
12882 if (*pm == NULL)
12884 m = bfd_zalloc (abfd, sizeof (*m));
12885 if (m == NULL)
12886 return false;
12888 m->p_type = PT_NULL;
12889 *pm = m;
12893 return true;
12896 /* Return the section that should be marked against GC for a given
12897 relocation. */
12899 asection *
12900 _bfd_mips_elf_gc_mark_hook (asection *sec,
12901 struct bfd_link_info *info,
12902 Elf_Internal_Rela *rel,
12903 struct elf_link_hash_entry *h,
12904 Elf_Internal_Sym *sym)
12906 /* ??? Do mips16 stub sections need to be handled special? */
12908 if (h != NULL)
12909 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12911 case R_MIPS_GNU_VTINHERIT:
12912 case R_MIPS_GNU_VTENTRY:
12913 return NULL;
12916 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12919 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12921 bool
12922 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12923 elf_gc_mark_hook_fn gc_mark_hook)
12925 bfd *sub;
12927 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12929 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12931 asection *o;
12933 if (! is_mips_elf (sub))
12934 continue;
12936 for (o = sub->sections; o != NULL; o = o->next)
12937 if (!o->gc_mark
12938 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12940 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12941 return false;
12945 return true;
12948 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12949 hiding the old indirect symbol. Process additional relocation
12950 information. Also called for weakdefs, in which case we just let
12951 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12953 void
12954 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12955 struct elf_link_hash_entry *dir,
12956 struct elf_link_hash_entry *ind)
12958 struct mips_elf_link_hash_entry *dirmips, *indmips;
12960 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12962 dirmips = (struct mips_elf_link_hash_entry *) dir;
12963 indmips = (struct mips_elf_link_hash_entry *) ind;
12964 /* Any absolute non-dynamic relocations against an indirect or weak
12965 definition will be against the target symbol. */
12966 if (indmips->has_static_relocs)
12967 dirmips->has_static_relocs = true;
12969 if (ind->root.type != bfd_link_hash_indirect)
12970 return;
12972 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12973 if (indmips->readonly_reloc)
12974 dirmips->readonly_reloc = true;
12975 if (indmips->no_fn_stub)
12976 dirmips->no_fn_stub = true;
12977 if (indmips->fn_stub)
12979 dirmips->fn_stub = indmips->fn_stub;
12980 indmips->fn_stub = NULL;
12982 if (indmips->need_fn_stub)
12984 dirmips->need_fn_stub = true;
12985 indmips->need_fn_stub = false;
12987 if (indmips->call_stub)
12989 dirmips->call_stub = indmips->call_stub;
12990 indmips->call_stub = NULL;
12992 if (indmips->call_fp_stub)
12994 dirmips->call_fp_stub = indmips->call_fp_stub;
12995 indmips->call_fp_stub = NULL;
12997 if (indmips->global_got_area < dirmips->global_got_area)
12998 dirmips->global_got_area = indmips->global_got_area;
12999 if (indmips->global_got_area < GGA_NONE)
13000 indmips->global_got_area = GGA_NONE;
13001 if (indmips->has_nonpic_branches)
13002 dirmips->has_nonpic_branches = true;
13005 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13006 to hide it. It has to remain global (it will also be protected) so as to
13007 be assigned a global GOT entry, which will then remain unchanged at load
13008 time. */
13010 void
13011 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13012 struct elf_link_hash_entry *entry,
13013 bool force_local)
13015 struct mips_elf_link_hash_table *htab;
13017 htab = mips_elf_hash_table (info);
13018 BFD_ASSERT (htab != NULL);
13019 if (htab->use_absolute_zero
13020 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13021 return;
13023 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13026 #define PDR_SIZE 32
13028 bool
13029 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13030 struct bfd_link_info *info)
13032 asection *o;
13033 bool ret = false;
13034 unsigned char *tdata;
13035 size_t i, skip;
13037 o = bfd_get_section_by_name (abfd, ".pdr");
13038 if (! o)
13039 return false;
13040 if (o->size == 0)
13041 return false;
13042 if (o->size % PDR_SIZE != 0)
13043 return false;
13044 if (o->output_section != NULL
13045 && bfd_is_abs_section (o->output_section))
13046 return false;
13048 tdata = bfd_zmalloc (o->size / PDR_SIZE);
13049 if (! tdata)
13050 return false;
13052 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13053 info->keep_memory);
13054 if (!cookie->rels)
13056 free (tdata);
13057 return false;
13060 cookie->rel = cookie->rels;
13061 cookie->relend = cookie->rels + o->reloc_count;
13063 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13065 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13067 tdata[i] = 1;
13068 skip ++;
13072 if (skip != 0)
13074 mips_elf_section_data (o)->u.tdata = tdata;
13075 if (o->rawsize == 0)
13076 o->rawsize = o->size;
13077 o->size -= skip * PDR_SIZE;
13078 ret = true;
13080 else
13081 free (tdata);
13083 if (! info->keep_memory)
13084 free (cookie->rels);
13086 return ret;
13089 bool
13090 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13092 if (strcmp (sec->name, ".pdr") == 0)
13093 return true;
13094 return false;
13097 bool
13098 _bfd_mips_elf_write_section (bfd *output_bfd,
13099 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13100 asection *sec, bfd_byte *contents)
13102 bfd_byte *to, *from, *end;
13103 int i;
13105 if (strcmp (sec->name, ".pdr") != 0)
13106 return false;
13108 if (mips_elf_section_data (sec)->u.tdata == NULL)
13109 return false;
13111 to = contents;
13112 end = contents + sec->size;
13113 for (from = contents, i = 0;
13114 from < end;
13115 from += PDR_SIZE, i++)
13117 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13118 continue;
13119 if (to != from)
13120 memcpy (to, from, PDR_SIZE);
13121 to += PDR_SIZE;
13123 bfd_set_section_contents (output_bfd, sec->output_section, contents,
13124 sec->output_offset, sec->size);
13125 return true;
13128 /* microMIPS code retains local labels for linker relaxation. Omit them
13129 from output by default for clarity. */
13131 bool
13132 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13134 return _bfd_elf_is_local_label_name (abfd, sym->name);
13137 bool
13138 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13139 asection *section, bfd_vma offset,
13140 const char **filename_ptr,
13141 const char **functionname_ptr,
13142 unsigned int *line_ptr,
13143 unsigned int *discriminator_ptr)
13145 asection *msec;
13147 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13148 filename_ptr, functionname_ptr,
13149 line_ptr, discriminator_ptr,
13150 dwarf_debug_sections,
13151 &elf_tdata (abfd)->dwarf2_find_line_info)
13152 == 1)
13153 return true;
13155 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13156 filename_ptr, functionname_ptr,
13157 line_ptr))
13159 if (!*functionname_ptr)
13160 _bfd_elf_find_function (abfd, symbols, section, offset,
13161 *filename_ptr ? NULL : filename_ptr,
13162 functionname_ptr);
13163 return true;
13166 msec = bfd_get_section_by_name (abfd, ".mdebug");
13167 if (msec != NULL)
13169 flagword origflags;
13170 struct mips_elf_find_line *fi;
13171 const struct ecoff_debug_swap * const swap =
13172 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13174 /* If we are called during a link, mips_elf_final_link may have
13175 cleared the SEC_HAS_CONTENTS field. We force it back on here
13176 if appropriate (which it normally will be). */
13177 origflags = msec->flags;
13178 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13179 msec->flags |= SEC_HAS_CONTENTS;
13181 fi = mips_elf_tdata (abfd)->find_line_info;
13182 if (fi == NULL)
13184 bfd_size_type external_fdr_size;
13185 char *fraw_src;
13186 char *fraw_end;
13187 struct fdr *fdr_ptr;
13188 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13190 fi = bfd_zalloc (abfd, amt);
13191 if (fi == NULL)
13193 msec->flags = origflags;
13194 return false;
13197 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13199 msec->flags = origflags;
13200 return false;
13203 /* Swap in the FDR information. */
13204 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13205 fi->d.fdr = bfd_alloc (abfd, amt);
13206 if (fi->d.fdr == NULL)
13208 _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13209 msec->flags = origflags;
13210 return false;
13212 external_fdr_size = swap->external_fdr_size;
13213 fdr_ptr = fi->d.fdr;
13214 fraw_src = (char *) fi->d.external_fdr;
13215 fraw_end = (fraw_src
13216 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13217 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13218 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13220 mips_elf_tdata (abfd)->find_line_info = fi;
13223 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13224 &fi->i, filename_ptr, functionname_ptr,
13225 line_ptr))
13227 msec->flags = origflags;
13228 return true;
13231 msec->flags = origflags;
13234 /* Fall back on the generic ELF find_nearest_line routine. */
13236 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13237 filename_ptr, functionname_ptr,
13238 line_ptr, discriminator_ptr);
13241 bool
13242 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13243 const char **filename_ptr,
13244 const char **functionname_ptr,
13245 unsigned int *line_ptr)
13247 bool found;
13248 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13249 functionname_ptr, line_ptr,
13250 & elf_tdata (abfd)->dwarf2_find_line_info);
13251 return found;
13255 /* When are writing out the .options or .MIPS.options section,
13256 remember the bytes we are writing out, so that we can install the
13257 GP value in the section_processing routine. */
13259 bool
13260 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13261 const void *location,
13262 file_ptr offset, bfd_size_type count)
13264 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13266 bfd_byte *c;
13268 if (elf_section_data (section) == NULL)
13270 size_t amt = sizeof (struct bfd_elf_section_data);
13271 section->used_by_bfd = bfd_zalloc (abfd, amt);
13272 if (elf_section_data (section) == NULL)
13273 return false;
13275 c = mips_elf_section_data (section)->u.tdata;
13276 if (c == NULL)
13278 c = bfd_zalloc (abfd, section->size);
13279 if (c == NULL)
13280 return false;
13281 mips_elf_section_data (section)->u.tdata = c;
13284 memcpy (c + offset, location, count);
13287 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13288 count);
13291 /* This is almost identical to bfd_generic_get_... except that some
13292 MIPS relocations need to be handled specially. Sigh. */
13294 bfd_byte *
13295 _bfd_elf_mips_get_relocated_section_contents
13296 (bfd *abfd,
13297 struct bfd_link_info *link_info,
13298 struct bfd_link_order *link_order,
13299 bfd_byte *data,
13300 bool relocatable,
13301 asymbol **symbols)
13303 bfd *input_bfd = link_order->u.indirect.section->owner;
13304 asection *input_section = link_order->u.indirect.section;
13305 long reloc_size;
13306 arelent **reloc_vector;
13307 long reloc_count;
13309 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13310 if (reloc_size < 0)
13311 return NULL;
13313 /* Read in the section. */
13314 bfd_byte *orig_data = data;
13315 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13316 return NULL;
13318 if (data == NULL)
13319 return NULL;
13321 if (reloc_size == 0)
13322 return data;
13324 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13325 if (reloc_vector == NULL)
13327 struct mips_elf_obj_tdata *tdata;
13328 struct mips_hi16 **hip, *hi;
13329 error_return:
13330 /* If we are going to return an error, remove entries on
13331 mips_hi16_list that point into this section's data. Data
13332 will typically be freed on return from this function. */
13333 tdata = mips_elf_tdata (abfd);
13334 hip = &tdata->mips_hi16_list;
13335 while ((hi = *hip) != NULL)
13337 if (hi->input_section == input_section)
13339 *hip = hi->next;
13340 free (hi);
13342 else
13343 hip = &hi->next;
13345 if (orig_data == NULL)
13346 free (data);
13347 data = NULL;
13348 goto out;
13351 reloc_count = bfd_canonicalize_reloc (input_bfd,
13352 input_section,
13353 reloc_vector,
13354 symbols);
13355 if (reloc_count < 0)
13356 goto error_return;
13358 if (reloc_count > 0)
13360 arelent **parent;
13361 /* for mips */
13362 int gp_found;
13363 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13366 struct bfd_hash_entry *h;
13367 struct bfd_link_hash_entry *lh;
13368 /* Skip all this stuff if we aren't mixing formats. */
13369 if (abfd && input_bfd
13370 && abfd->xvec == input_bfd->xvec)
13371 lh = 0;
13372 else
13374 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13375 lh = (struct bfd_link_hash_entry *) h;
13377 lookup:
13378 if (lh)
13380 switch (lh->type)
13382 case bfd_link_hash_undefined:
13383 case bfd_link_hash_undefweak:
13384 case bfd_link_hash_common:
13385 gp_found = 0;
13386 break;
13387 case bfd_link_hash_defined:
13388 case bfd_link_hash_defweak:
13389 gp_found = 1;
13390 gp = lh->u.def.value;
13391 break;
13392 case bfd_link_hash_indirect:
13393 case bfd_link_hash_warning:
13394 lh = lh->u.i.link;
13395 /* @@FIXME ignoring warning for now */
13396 goto lookup;
13397 case bfd_link_hash_new:
13398 default:
13399 abort ();
13402 else
13403 gp_found = 0;
13405 /* end mips */
13407 for (parent = reloc_vector; *parent != NULL; parent++)
13409 char *error_message = NULL;
13410 asymbol *symbol;
13411 bfd_reloc_status_type r;
13413 symbol = *(*parent)->sym_ptr_ptr;
13414 /* PR ld/19628: A specially crafted input file
13415 can result in a NULL symbol pointer here. */
13416 if (symbol == NULL)
13418 link_info->callbacks->einfo
13419 /* xgettext:c-format */
13420 (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13421 abfd, input_section, (* parent)->address);
13422 goto error_return;
13425 /* Zap reloc field when the symbol is from a discarded
13426 section, ignoring any addend. Do the same when called
13427 from bfd_simple_get_relocated_section_contents for
13428 undefined symbols in debug sections. This is to keep
13429 debug info reasonably sane, in particular so that
13430 DW_FORM_ref_addr to another file's .debug_info isn't
13431 confused with an offset into the current file's
13432 .debug_info. */
13433 if ((symbol->section != NULL && discarded_section (symbol->section))
13434 || (symbol->section == bfd_und_section_ptr
13435 && (input_section->flags & SEC_DEBUGGING) != 0
13436 && link_info->input_bfds == link_info->output_bfd))
13438 bfd_vma off;
13439 static reloc_howto_type none_howto
13440 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13441 "unused", false, 0, 0, false);
13443 off = ((*parent)->address
13444 * bfd_octets_per_byte (input_bfd, input_section));
13445 _bfd_clear_contents ((*parent)->howto, input_bfd,
13446 input_section, data, off);
13447 (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13448 (*parent)->addend = 0;
13449 (*parent)->howto = &none_howto;
13450 r = bfd_reloc_ok;
13453 /* Specific to MIPS: Deal with relocation types that require
13454 knowing the gp of the output bfd. */
13456 /* If we've managed to find the gp and have a special
13457 function for the relocation then go ahead, else default
13458 to the generic handling. */
13459 else if (gp_found
13460 && ((*parent)->howto->special_function
13461 == _bfd_mips_elf32_gprel16_reloc))
13462 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13463 input_section, relocatable,
13464 data, gp);
13465 else
13466 r = bfd_perform_relocation (input_bfd,
13467 *parent,
13468 data,
13469 input_section,
13470 relocatable ? abfd : NULL,
13471 &error_message);
13473 if (relocatable)
13475 asection *os = input_section->output_section;
13477 /* A partial link, so keep the relocs. */
13478 os->orelocation[os->reloc_count] = *parent;
13479 os->reloc_count++;
13482 if (r != bfd_reloc_ok)
13484 switch (r)
13486 case bfd_reloc_undefined:
13487 (*link_info->callbacks->undefined_symbol)
13488 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13489 input_bfd, input_section, (*parent)->address, true);
13490 break;
13491 case bfd_reloc_dangerous:
13492 BFD_ASSERT (error_message != NULL);
13493 (*link_info->callbacks->reloc_dangerous)
13494 (link_info, error_message,
13495 input_bfd, input_section, (*parent)->address);
13496 break;
13497 case bfd_reloc_overflow:
13498 (*link_info->callbacks->reloc_overflow)
13499 (link_info, NULL,
13500 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13501 (*parent)->howto->name, (*parent)->addend,
13502 input_bfd, input_section, (*parent)->address);
13503 break;
13504 case bfd_reloc_outofrange:
13505 /* PR ld/13730:
13506 This error can result when processing some partially
13507 complete binaries. Do not abort, but issue an error
13508 message instead. */
13509 link_info->callbacks->einfo
13510 /* xgettext:c-format */
13511 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13512 abfd, input_section, * parent);
13513 goto error_return;
13515 case bfd_reloc_notsupported:
13516 /* PR ld/17512
13517 This error can result when processing a corrupt binary.
13518 Do not abort. Issue an error message instead. */
13519 link_info->callbacks->einfo
13520 /* xgettext:c-format */
13521 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13522 abfd, input_section, * parent);
13523 goto error_return;
13525 default:
13526 /* PR 17512; file: 90c2a92e.
13527 Report unexpected results, without aborting. */
13528 link_info->callbacks->einfo
13529 /* xgettext:c-format */
13530 (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13531 abfd, input_section, * parent, r);
13532 break;
13539 out:
13540 free (reloc_vector);
13541 return data;
13544 static bool
13545 mips_elf_relax_delete_bytes (bfd *abfd,
13546 asection *sec, bfd_vma addr, int count)
13548 Elf_Internal_Shdr *symtab_hdr;
13549 unsigned int sec_shndx;
13550 bfd_byte *contents;
13551 Elf_Internal_Rela *irel, *irelend;
13552 Elf_Internal_Sym *isym;
13553 Elf_Internal_Sym *isymend;
13554 struct elf_link_hash_entry **sym_hashes;
13555 struct elf_link_hash_entry **end_hashes;
13556 struct elf_link_hash_entry **start_hashes;
13557 unsigned int symcount;
13559 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13560 contents = elf_section_data (sec)->this_hdr.contents;
13562 irel = elf_section_data (sec)->relocs;
13563 irelend = irel + sec->reloc_count;
13565 /* Actually delete the bytes. */
13566 memmove (contents + addr, contents + addr + count,
13567 (size_t) (sec->size - addr - count));
13568 sec->size -= count;
13570 /* Adjust all the relocs. */
13571 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13573 /* Get the new reloc address. */
13574 if (irel->r_offset > addr)
13575 irel->r_offset -= count;
13578 BFD_ASSERT (addr % 2 == 0);
13579 BFD_ASSERT (count % 2 == 0);
13581 /* Adjust the local symbols defined in this section. */
13582 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13583 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13584 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13585 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13586 isym->st_value -= count;
13588 /* Now adjust the global symbols defined in this section. */
13589 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13590 - symtab_hdr->sh_info);
13591 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13592 end_hashes = sym_hashes + symcount;
13594 for (; sym_hashes < end_hashes; sym_hashes++)
13596 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13598 if ((sym_hash->root.type == bfd_link_hash_defined
13599 || sym_hash->root.type == bfd_link_hash_defweak)
13600 && sym_hash->root.u.def.section == sec)
13602 bfd_vma value = sym_hash->root.u.def.value;
13604 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13605 value &= MINUS_TWO;
13606 if (value > addr)
13607 sym_hash->root.u.def.value -= count;
13611 return true;
13615 /* Opcodes needed for microMIPS relaxation as found in
13616 opcodes/micromips-opc.c. */
13618 struct opcode_descriptor {
13619 unsigned long match;
13620 unsigned long mask;
13623 /* The $ra register aka $31. */
13625 #define RA 31
13627 /* 32-bit instruction format register fields. */
13629 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13630 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13632 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13634 #define OP16_VALID_REG(r) \
13635 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13638 /* 32-bit and 16-bit branches. */
13640 static const struct opcode_descriptor b_insns_32[] = {
13641 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13642 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13643 { 0, 0 } /* End marker for find_match(). */
13646 static const struct opcode_descriptor bc_insn_32 =
13647 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13649 static const struct opcode_descriptor bz_insn_32 =
13650 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13652 static const struct opcode_descriptor bzal_insn_32 =
13653 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13655 static const struct opcode_descriptor beq_insn_32 =
13656 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13658 static const struct opcode_descriptor b_insn_16 =
13659 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13661 static const struct opcode_descriptor bz_insn_16 =
13662 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13665 /* 32-bit and 16-bit branch EQ and NE zero. */
13667 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13668 eq and second the ne. This convention is used when replacing a
13669 32-bit BEQ/BNE with the 16-bit version. */
13671 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13673 static const struct opcode_descriptor bz_rs_insns_32[] = {
13674 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13675 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13676 { 0, 0 } /* End marker for find_match(). */
13679 static const struct opcode_descriptor bz_rt_insns_32[] = {
13680 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13681 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13682 { 0, 0 } /* End marker for find_match(). */
13685 static const struct opcode_descriptor bzc_insns_32[] = {
13686 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13687 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13688 { 0, 0 } /* End marker for find_match(). */
13691 static const struct opcode_descriptor bz_insns_16[] = {
13692 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13693 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13694 { 0, 0 } /* End marker for find_match(). */
13697 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13699 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13700 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13703 /* 32-bit instructions with a delay slot. */
13705 static const struct opcode_descriptor jal_insn_32_bd16 =
13706 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13708 static const struct opcode_descriptor jal_insn_32_bd32 =
13709 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13711 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13712 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13714 static const struct opcode_descriptor j_insn_32 =
13715 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13717 static const struct opcode_descriptor jalr_insn_32 =
13718 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13720 /* This table can be compacted, because no opcode replacement is made. */
13722 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13723 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13725 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13726 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13728 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13729 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13730 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13731 { 0, 0 } /* End marker for find_match(). */
13734 /* This table can be compacted, because no opcode replacement is made. */
13736 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13737 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13739 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13740 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13741 { 0, 0 } /* End marker for find_match(). */
13745 /* 16-bit instructions with a delay slot. */
13747 static const struct opcode_descriptor jalr_insn_16_bd16 =
13748 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13750 static const struct opcode_descriptor jalr_insn_16_bd32 =
13751 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13753 static const struct opcode_descriptor jr_insn_16 =
13754 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13756 #define JR16_REG(opcode) ((opcode) & 0x1f)
13758 /* This table can be compacted, because no opcode replacement is made. */
13760 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13761 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13763 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13764 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13765 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13766 { 0, 0 } /* End marker for find_match(). */
13770 /* LUI instruction. */
13772 static const struct opcode_descriptor lui_insn =
13773 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13776 /* ADDIU instruction. */
13778 static const struct opcode_descriptor addiu_insn =
13779 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13781 static const struct opcode_descriptor addiupc_insn =
13782 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13784 #define ADDIUPC_REG_FIELD(r) \
13785 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13788 /* Relaxable instructions in a JAL delay slot: MOVE. */
13790 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13791 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13792 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13793 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13795 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13796 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13798 static const struct opcode_descriptor move_insns_32[] = {
13799 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13800 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13801 { 0, 0 } /* End marker for find_match(). */
13804 static const struct opcode_descriptor move_insn_16 =
13805 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13808 /* NOP instructions. */
13810 static const struct opcode_descriptor nop_insn_32 =
13811 { /* "nop", "", */ 0x00000000, 0xffffffff };
13813 static const struct opcode_descriptor nop_insn_16 =
13814 { /* "nop", "", */ 0x0c00, 0xffff };
13817 /* Instruction match support. */
13819 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13821 static int
13822 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13824 unsigned long indx;
13826 for (indx = 0; insn[indx].mask != 0; indx++)
13827 if (MATCH (opcode, insn[indx]))
13828 return indx;
13830 return -1;
13834 /* Branch and delay slot decoding support. */
13836 /* If PTR points to what *might* be a 16-bit branch or jump, then
13837 return the minimum length of its delay slot, otherwise return 0.
13838 Non-zero results are not definitive as we might be checking against
13839 the second half of another instruction. */
13841 static int
13842 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13844 unsigned long opcode;
13845 int bdsize;
13847 opcode = bfd_get_16 (abfd, ptr);
13848 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13849 /* 16-bit branch/jump with a 32-bit delay slot. */
13850 bdsize = 4;
13851 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13852 || find_match (opcode, ds_insns_16_bd16) >= 0)
13853 /* 16-bit branch/jump with a 16-bit delay slot. */
13854 bdsize = 2;
13855 else
13856 /* No delay slot. */
13857 bdsize = 0;
13859 return bdsize;
13862 /* If PTR points to what *might* be a 32-bit branch or jump, then
13863 return the minimum length of its delay slot, otherwise return 0.
13864 Non-zero results are not definitive as we might be checking against
13865 the second half of another instruction. */
13867 static int
13868 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13870 unsigned long opcode;
13871 int bdsize;
13873 opcode = bfd_get_micromips_32 (abfd, ptr);
13874 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13875 /* 32-bit branch/jump with a 32-bit delay slot. */
13876 bdsize = 4;
13877 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13878 /* 32-bit branch/jump with a 16-bit delay slot. */
13879 bdsize = 2;
13880 else
13881 /* No delay slot. */
13882 bdsize = 0;
13884 return bdsize;
13887 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13888 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13890 static bool
13891 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13893 unsigned long opcode;
13895 opcode = bfd_get_16 (abfd, ptr);
13896 if (MATCH (opcode, b_insn_16)
13897 /* B16 */
13898 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13899 /* JR16 */
13900 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13901 /* BEQZ16, BNEZ16 */
13902 || (MATCH (opcode, jalr_insn_16_bd32)
13903 /* JALR16 */
13904 && reg != JR16_REG (opcode) && reg != RA))
13905 return true;
13907 return false;
13910 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13911 then return TRUE, otherwise FALSE. */
13913 static bool
13914 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13916 unsigned long opcode;
13918 opcode = bfd_get_micromips_32 (abfd, ptr);
13919 if (MATCH (opcode, j_insn_32)
13920 /* J */
13921 || MATCH (opcode, bc_insn_32)
13922 /* BC1F, BC1T, BC2F, BC2T */
13923 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13924 /* JAL, JALX */
13925 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13926 /* BGEZ, BGTZ, BLEZ, BLTZ */
13927 || (MATCH (opcode, bzal_insn_32)
13928 /* BGEZAL, BLTZAL */
13929 && reg != OP32_SREG (opcode) && reg != RA)
13930 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13931 /* JALR, JALR.HB, BEQ, BNE */
13932 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13933 return true;
13935 return false;
13938 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13939 IRELEND) at OFFSET indicate that there must be a compact branch there,
13940 then return TRUE, otherwise FALSE. */
13942 static bool
13943 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13944 const Elf_Internal_Rela *internal_relocs,
13945 const Elf_Internal_Rela *irelend)
13947 const Elf_Internal_Rela *irel;
13948 unsigned long opcode;
13950 opcode = bfd_get_micromips_32 (abfd, ptr);
13951 if (find_match (opcode, bzc_insns_32) < 0)
13952 return false;
13954 for (irel = internal_relocs; irel < irelend; irel++)
13955 if (irel->r_offset == offset
13956 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13957 return true;
13959 return false;
13962 /* Bitsize checking. */
13963 #define IS_BITSIZE(val, N) \
13964 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13965 - (1ULL << ((N) - 1))) == (val))
13968 bool
13969 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13970 struct bfd_link_info *link_info,
13971 bool *again)
13973 bool insn32 = mips_elf_hash_table (link_info)->insn32;
13974 Elf_Internal_Shdr *symtab_hdr;
13975 Elf_Internal_Rela *internal_relocs;
13976 Elf_Internal_Rela *irel, *irelend;
13977 bfd_byte *contents = NULL;
13978 Elf_Internal_Sym *isymbuf = NULL;
13980 /* Assume nothing changes. */
13981 *again = false;
13983 /* We don't have to do anything for a relocatable link, if
13984 this section does not have relocs, or if this is not a
13985 code section. */
13987 if (bfd_link_relocatable (link_info)
13988 || sec->reloc_count == 0
13989 || (sec->flags & SEC_RELOC) == 0
13990 || (sec->flags & SEC_HAS_CONTENTS) == 0
13991 || (sec->flags & SEC_CODE) == 0)
13992 return true;
13994 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13996 /* Get a copy of the native relocations. */
13997 internal_relocs = (_bfd_elf_link_read_relocs
13998 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13999 link_info->keep_memory));
14000 if (internal_relocs == NULL)
14001 goto error_return;
14003 /* Walk through them looking for relaxing opportunities. */
14004 irelend = internal_relocs + sec->reloc_count;
14005 for (irel = internal_relocs; irel < irelend; irel++)
14007 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14008 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14009 bool target_is_micromips_code_p;
14010 unsigned long opcode;
14011 bfd_vma symval;
14012 bfd_vma pcrval;
14013 bfd_byte *ptr;
14014 int fndopc;
14016 /* The number of bytes to delete for relaxation and from where
14017 to delete these bytes starting at irel->r_offset. */
14018 int delcnt = 0;
14019 int deloff = 0;
14021 /* If this isn't something that can be relaxed, then ignore
14022 this reloc. */
14023 if (r_type != R_MICROMIPS_HI16
14024 && r_type != R_MICROMIPS_PC16_S1
14025 && r_type != R_MICROMIPS_26_S1)
14026 continue;
14028 /* Get the section contents if we haven't done so already. */
14029 if (contents == NULL)
14031 /* Get cached copy if it exists. */
14032 if (elf_section_data (sec)->this_hdr.contents != NULL)
14033 contents = elf_section_data (sec)->this_hdr.contents;
14034 /* Go get them off disk. */
14035 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14036 goto error_return;
14038 ptr = contents + irel->r_offset;
14040 /* Read this BFD's local symbols if we haven't done so already. */
14041 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14043 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14044 if (isymbuf == NULL)
14045 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14046 symtab_hdr->sh_info, 0,
14047 NULL, NULL, NULL);
14048 if (isymbuf == NULL)
14049 goto error_return;
14052 /* Get the value of the symbol referred to by the reloc. */
14053 if (r_symndx < symtab_hdr->sh_info)
14055 /* A local symbol. */
14056 Elf_Internal_Sym *isym;
14057 asection *sym_sec;
14059 isym = isymbuf + r_symndx;
14060 if (isym->st_shndx == SHN_UNDEF)
14061 sym_sec = bfd_und_section_ptr;
14062 else if (isym->st_shndx == SHN_ABS)
14063 sym_sec = bfd_abs_section_ptr;
14064 else if (isym->st_shndx == SHN_COMMON)
14065 sym_sec = bfd_com_section_ptr;
14066 else
14067 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14068 symval = (isym->st_value
14069 + sym_sec->output_section->vma
14070 + sym_sec->output_offset);
14071 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14073 else
14075 unsigned long indx;
14076 struct elf_link_hash_entry *h;
14078 /* An external symbol. */
14079 indx = r_symndx - symtab_hdr->sh_info;
14080 h = elf_sym_hashes (abfd)[indx];
14081 BFD_ASSERT (h != NULL);
14083 if (h->root.type != bfd_link_hash_defined
14084 && h->root.type != bfd_link_hash_defweak)
14085 /* This appears to be a reference to an undefined
14086 symbol. Just ignore it -- it will be caught by the
14087 regular reloc processing. */
14088 continue;
14090 symval = (h->root.u.def.value
14091 + h->root.u.def.section->output_section->vma
14092 + h->root.u.def.section->output_offset);
14093 target_is_micromips_code_p = (!h->needs_plt
14094 && ELF_ST_IS_MICROMIPS (h->other));
14098 /* For simplicity of coding, we are going to modify the
14099 section contents, the section relocs, and the BFD symbol
14100 table. We must tell the rest of the code not to free up this
14101 information. It would be possible to instead create a table
14102 of changes which have to be made, as is done in coff-mips.c;
14103 that would be more work, but would require less memory when
14104 the linker is run. */
14106 /* Only 32-bit instructions relaxed. */
14107 if (irel->r_offset + 4 > sec->size)
14108 continue;
14110 opcode = bfd_get_micromips_32 (abfd, ptr);
14112 /* This is the pc-relative distance from the instruction the
14113 relocation is applied to, to the symbol referred. */
14114 pcrval = (symval
14115 - (sec->output_section->vma + sec->output_offset)
14116 - irel->r_offset);
14118 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14119 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14120 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
14122 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14124 where pcrval has first to be adjusted to apply against the LO16
14125 location (we make the adjustment later on, when we have figured
14126 out the offset). */
14127 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14129 bool bzc = false;
14130 unsigned long nextopc;
14131 unsigned long reg;
14132 bfd_vma offset;
14134 /* Give up if the previous reloc was a HI16 against this symbol
14135 too. */
14136 if (irel > internal_relocs
14137 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14138 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14139 continue;
14141 /* Or if the next reloc is not a LO16 against this symbol. */
14142 if (irel + 1 >= irelend
14143 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14144 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14145 continue;
14147 /* Or if the second next reloc is a LO16 against this symbol too. */
14148 if (irel + 2 >= irelend
14149 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14150 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14151 continue;
14153 /* See if the LUI instruction *might* be in a branch delay slot.
14154 We check whether what looks like a 16-bit branch or jump is
14155 actually an immediate argument to a compact branch, and let
14156 it through if so. */
14157 if (irel->r_offset >= 2
14158 && check_br16_dslot (abfd, ptr - 2)
14159 && !(irel->r_offset >= 4
14160 && (bzc = check_relocated_bzc (abfd,
14161 ptr - 4, irel->r_offset - 4,
14162 internal_relocs, irelend))))
14163 continue;
14164 if (irel->r_offset >= 4
14165 && !bzc
14166 && check_br32_dslot (abfd, ptr - 4))
14167 continue;
14169 reg = OP32_SREG (opcode);
14171 /* We only relax adjacent instructions or ones separated with
14172 a branch or jump that has a delay slot. The branch or jump
14173 must not fiddle with the register used to hold the address.
14174 Subtract 4 for the LUI itself. */
14175 offset = irel[1].r_offset - irel[0].r_offset;
14176 switch (offset - 4)
14178 case 0:
14179 break;
14180 case 2:
14181 if (check_br16 (abfd, ptr + 4, reg))
14182 break;
14183 continue;
14184 case 4:
14185 if (check_br32 (abfd, ptr + 4, reg))
14186 break;
14187 continue;
14188 default:
14189 continue;
14192 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14194 /* Give up unless the same register is used with both
14195 relocations. */
14196 if (OP32_SREG (nextopc) != reg)
14197 continue;
14199 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14200 and rounding up to take masking of the two LSBs into account. */
14201 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14203 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14204 if (IS_BITSIZE (symval, 16))
14206 /* Fix the relocation's type. */
14207 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14209 /* Instructions using R_MICROMIPS_LO16 have the base or
14210 source register in bits 20:16. This register becomes $0
14211 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
14212 nextopc &= ~0x001f0000;
14213 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14214 contents + irel[1].r_offset);
14217 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14218 We add 4 to take LUI deletion into account while checking
14219 the PC-relative distance. */
14220 else if (symval % 4 == 0
14221 && IS_BITSIZE (pcrval + 4, 25)
14222 && MATCH (nextopc, addiu_insn)
14223 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14224 && OP16_VALID_REG (OP32_TREG (nextopc)))
14226 /* Fix the relocation's type. */
14227 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14229 /* Replace ADDIU with the ADDIUPC version. */
14230 nextopc = (addiupc_insn.match
14231 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14233 bfd_put_micromips_32 (abfd, nextopc,
14234 contents + irel[1].r_offset);
14237 /* Can't do anything, give up, sigh... */
14238 else
14239 continue;
14241 /* Fix the relocation's type. */
14242 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14244 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14245 delcnt = 4;
14246 deloff = 0;
14249 /* Compact branch relaxation -- due to the multitude of macros
14250 employed by the compiler/assembler, compact branches are not
14251 always generated. Obviously, this can/will be fixed elsewhere,
14252 but there is no drawback in double checking it here. */
14253 else if (r_type == R_MICROMIPS_PC16_S1
14254 && irel->r_offset + 5 < sec->size
14255 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14256 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14257 && ((!insn32
14258 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14259 nop_insn_16) ? 2 : 0))
14260 || (irel->r_offset + 7 < sec->size
14261 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14262 ptr + 4),
14263 nop_insn_32) ? 4 : 0))))
14265 unsigned long reg;
14267 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14269 /* Replace BEQZ/BNEZ with the compact version. */
14270 opcode = (bzc_insns_32[fndopc].match
14271 | BZC32_REG_FIELD (reg)
14272 | (opcode & 0xffff)); /* Addend value. */
14274 bfd_put_micromips_32 (abfd, opcode, ptr);
14276 /* Delete the delay slot NOP: two or four bytes from
14277 irel->offset + 4; delcnt has already been set above. */
14278 deloff = 4;
14281 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14282 to check the distance from the next instruction, so subtract 2. */
14283 else if (!insn32
14284 && r_type == R_MICROMIPS_PC16_S1
14285 && IS_BITSIZE (pcrval - 2, 11)
14286 && find_match (opcode, b_insns_32) >= 0)
14288 /* Fix the relocation's type. */
14289 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14291 /* Replace the 32-bit opcode with a 16-bit opcode. */
14292 bfd_put_16 (abfd,
14293 (b_insn_16.match
14294 | (opcode & 0x3ff)), /* Addend value. */
14295 ptr);
14297 /* Delete 2 bytes from irel->r_offset + 2. */
14298 delcnt = 2;
14299 deloff = 2;
14302 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14303 to check the distance from the next instruction, so subtract 2. */
14304 else if (!insn32
14305 && r_type == R_MICROMIPS_PC16_S1
14306 && IS_BITSIZE (pcrval - 2, 8)
14307 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14308 && OP16_VALID_REG (OP32_SREG (opcode)))
14309 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14310 && OP16_VALID_REG (OP32_TREG (opcode)))))
14312 unsigned long reg;
14314 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14316 /* Fix the relocation's type. */
14317 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14319 /* Replace the 32-bit opcode with a 16-bit opcode. */
14320 bfd_put_16 (abfd,
14321 (bz_insns_16[fndopc].match
14322 | BZ16_REG_FIELD (reg)
14323 | (opcode & 0x7f)), /* Addend value. */
14324 ptr);
14326 /* Delete 2 bytes from irel->r_offset + 2. */
14327 delcnt = 2;
14328 deloff = 2;
14331 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14332 else if (!insn32
14333 && r_type == R_MICROMIPS_26_S1
14334 && target_is_micromips_code_p
14335 && irel->r_offset + 7 < sec->size
14336 && MATCH (opcode, jal_insn_32_bd32))
14338 unsigned long n32opc;
14339 bool relaxed = false;
14341 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14343 if (MATCH (n32opc, nop_insn_32))
14345 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14346 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14348 relaxed = true;
14350 else if (find_match (n32opc, move_insns_32) >= 0)
14352 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14353 bfd_put_16 (abfd,
14354 (move_insn_16.match
14355 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14356 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14357 ptr + 4);
14359 relaxed = true;
14361 /* Other 32-bit instructions relaxable to 16-bit
14362 instructions will be handled here later. */
14364 if (relaxed)
14366 /* JAL with 32-bit delay slot that is changed to a JALS
14367 with 16-bit delay slot. */
14368 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14370 /* Delete 2 bytes from irel->r_offset + 6. */
14371 delcnt = 2;
14372 deloff = 6;
14376 if (delcnt != 0)
14378 /* Note that we've changed the relocs, section contents, etc. */
14379 elf_section_data (sec)->relocs = internal_relocs;
14380 elf_section_data (sec)->this_hdr.contents = contents;
14381 symtab_hdr->contents = (unsigned char *) isymbuf;
14383 /* Delete bytes depending on the delcnt and deloff. */
14384 if (!mips_elf_relax_delete_bytes (abfd, sec,
14385 irel->r_offset + deloff, delcnt))
14386 goto error_return;
14388 /* That will change things, so we should relax again.
14389 Note that this is not required, and it may be slow. */
14390 *again = true;
14394 if (isymbuf != NULL
14395 && symtab_hdr->contents != (unsigned char *) isymbuf)
14397 if (! link_info->keep_memory)
14398 free (isymbuf);
14399 else
14401 /* Cache the symbols for elf_link_input_bfd. */
14402 symtab_hdr->contents = (unsigned char *) isymbuf;
14406 if (contents != NULL
14407 && elf_section_data (sec)->this_hdr.contents != contents)
14409 if (! link_info->keep_memory)
14410 free (contents);
14411 else
14413 /* Cache the section contents for elf_link_input_bfd. */
14414 elf_section_data (sec)->this_hdr.contents = contents;
14418 if (elf_section_data (sec)->relocs != internal_relocs)
14419 free (internal_relocs);
14421 return true;
14423 error_return:
14424 if (symtab_hdr->contents != (unsigned char *) isymbuf)
14425 free (isymbuf);
14426 if (elf_section_data (sec)->this_hdr.contents != contents)
14427 free (contents);
14428 if (elf_section_data (sec)->relocs != internal_relocs)
14429 free (internal_relocs);
14431 return false;
14434 /* Create a MIPS ELF linker hash table. */
14436 struct bfd_link_hash_table *
14437 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14439 struct mips_elf_link_hash_table *ret;
14440 size_t amt = sizeof (struct mips_elf_link_hash_table);
14442 ret = bfd_zmalloc (amt);
14443 if (ret == NULL)
14444 return NULL;
14446 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14447 mips_elf_link_hash_newfunc,
14448 sizeof (struct mips_elf_link_hash_entry)))
14450 free (ret);
14451 return NULL;
14453 ret->root.init_plt_refcount.plist = NULL;
14454 ret->root.init_plt_offset.plist = NULL;
14456 return &ret->root.root;
14459 /* Likewise, but indicate that the target is VxWorks. */
14461 struct bfd_link_hash_table *
14462 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14464 struct bfd_link_hash_table *ret;
14466 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14467 if (ret)
14469 struct mips_elf_link_hash_table *htab;
14471 htab = (struct mips_elf_link_hash_table *) ret;
14472 htab->use_plts_and_copy_relocs = true;
14474 return ret;
14477 /* A function that the linker calls if we are allowed to use PLTs
14478 and copy relocs. */
14480 void
14481 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14483 mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14486 /* A function that the linker calls to select between all or only
14487 32-bit microMIPS instructions, and between making or ignoring
14488 branch relocation checks for invalid transitions between ISA modes.
14489 Also record whether we have been configured for a GNU target. */
14491 void
14492 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14493 bool ignore_branch_isa,
14494 bool gnu_target)
14496 mips_elf_hash_table (info)->insn32 = insn32;
14497 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14498 mips_elf_hash_table (info)->gnu_target = gnu_target;
14501 /* A function that the linker calls to enable use of compact branches in
14502 linker generated code for MIPSR6. */
14504 void
14505 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14507 mips_elf_hash_table (info)->compact_branches = on;
14511 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14513 struct mips_mach_extension
14515 unsigned long extension, base;
14518 /* An array that maps 64-bit architectures to the corresponding 32-bit
14519 architectures. */
14520 static const struct mips_mach_extension mips_mach_32_64[] =
14522 { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14523 { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14524 { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14525 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14526 { bfd_mach_mipsisa64, bfd_mach_mipsisa32 }
14529 /* An array describing how BFD machines relate to one another. The entries
14530 are ordered topologically with MIPS I extensions listed last. */
14532 static const struct mips_mach_extension mips_mach_extensions[] =
14534 /* MIPS64r2 extensions. */
14535 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14536 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14537 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14538 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14539 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14540 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14541 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14543 /* MIPS64 extensions. */
14544 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14545 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14546 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14548 /* MIPS V extensions. */
14549 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14551 /* R10000 extensions. */
14552 { bfd_mach_mips12000, bfd_mach_mips10000 },
14553 { bfd_mach_mips14000, bfd_mach_mips10000 },
14554 { bfd_mach_mips16000, bfd_mach_mips10000 },
14556 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14557 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14558 better to allow vr5400 and vr5500 code to be merged anyway, since
14559 many libraries will just use the core ISA. Perhaps we could add
14560 some sort of ASE flag if this ever proves a problem. */
14561 { bfd_mach_mips5500, bfd_mach_mips5400 },
14562 { bfd_mach_mips5400, bfd_mach_mips5000 },
14564 /* MIPS IV extensions. */
14565 { bfd_mach_mips5, bfd_mach_mips8000 },
14566 { bfd_mach_mips10000, bfd_mach_mips8000 },
14567 { bfd_mach_mips5000, bfd_mach_mips8000 },
14568 { bfd_mach_mips7000, bfd_mach_mips8000 },
14569 { bfd_mach_mips9000, bfd_mach_mips8000 },
14571 /* VR4100 extensions. */
14572 { bfd_mach_mips4120, bfd_mach_mips4100 },
14573 { bfd_mach_mips4111, bfd_mach_mips4100 },
14575 /* MIPS III extensions. */
14576 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14577 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14578 { bfd_mach_mips8000, bfd_mach_mips4000 },
14579 { bfd_mach_mips4650, bfd_mach_mips4000 },
14580 { bfd_mach_mips4600, bfd_mach_mips4000 },
14581 { bfd_mach_mips4400, bfd_mach_mips4000 },
14582 { bfd_mach_mips4300, bfd_mach_mips4000 },
14583 { bfd_mach_mips4100, bfd_mach_mips4000 },
14584 { bfd_mach_mips5900, bfd_mach_mips4000 },
14586 /* MIPS32r3 extensions. */
14587 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14589 /* MIPS32r2 extensions. */
14590 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14592 /* MIPS32 extensions. */
14593 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14595 /* MIPS II extensions. */
14596 { bfd_mach_mips4000, bfd_mach_mips6000 },
14597 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14598 { bfd_mach_mips4010, bfd_mach_mips6000 },
14599 { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14601 /* MIPS I extensions. */
14602 { bfd_mach_mips6000, bfd_mach_mips3000 },
14603 { bfd_mach_mips3900, bfd_mach_mips3000 }
14606 /* Return true if bfd machine EXTENSION is the same as BASE, or if
14607 EXTENSION is the 64-bit equivalent of a 32-bit BASE. */
14609 static bool
14610 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14612 size_t i;
14614 if (extension == base)
14615 return true;
14617 for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14618 if (extension == mips_mach_32_64[i].extension)
14619 return base == mips_mach_32_64[i].base;
14621 return false;
14624 static bool
14625 mips_mach_extends_p (unsigned long base, unsigned long extension)
14627 size_t i;
14629 if (mips_mach_extends_32_64 (base, extension))
14630 return true;
14632 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14633 if (extension == mips_mach_extensions[i].extension)
14635 extension = mips_mach_extensions[i].base;
14636 if (mips_mach_extends_32_64 (base, extension))
14637 return true;
14640 return false;
14643 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14645 static unsigned long
14646 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14648 switch (isa_ext)
14650 case AFL_EXT_3900: return bfd_mach_mips3900;
14651 case AFL_EXT_4010: return bfd_mach_mips4010;
14652 case AFL_EXT_4100: return bfd_mach_mips4100;
14653 case AFL_EXT_4111: return bfd_mach_mips4111;
14654 case AFL_EXT_4120: return bfd_mach_mips4120;
14655 case AFL_EXT_4650: return bfd_mach_mips4650;
14656 case AFL_EXT_5400: return bfd_mach_mips5400;
14657 case AFL_EXT_5500: return bfd_mach_mips5500;
14658 case AFL_EXT_5900: return bfd_mach_mips5900;
14659 case AFL_EXT_10000: return bfd_mach_mips10000;
14660 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14661 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14662 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14663 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14664 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14665 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14666 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14667 default: return bfd_mach_mips3000;
14671 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14673 unsigned int
14674 bfd_mips_isa_ext (bfd *abfd)
14676 switch (bfd_get_mach (abfd))
14678 case bfd_mach_mips3900: return AFL_EXT_3900;
14679 case bfd_mach_mips4010: return AFL_EXT_4010;
14680 case bfd_mach_mips4100: return AFL_EXT_4100;
14681 case bfd_mach_mips4111: return AFL_EXT_4111;
14682 case bfd_mach_mips4120: return AFL_EXT_4120;
14683 case bfd_mach_mips4650: return AFL_EXT_4650;
14684 case bfd_mach_mips5400: return AFL_EXT_5400;
14685 case bfd_mach_mips5500: return AFL_EXT_5500;
14686 case bfd_mach_mips5900: return AFL_EXT_5900;
14687 case bfd_mach_mips10000: return AFL_EXT_10000;
14688 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14689 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14690 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14691 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14692 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14693 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14694 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14695 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14696 case bfd_mach_mips_interaptiv_mr2:
14697 return AFL_EXT_INTERAPTIV_MR2;
14698 default: return 0;
14702 /* Encode ISA level and revision as a single value. */
14703 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14705 /* Decode a single value into level and revision. */
14706 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14707 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14709 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14711 static void
14712 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14714 int new_isa = 0;
14715 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14717 case EF_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14718 case EF_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14719 case EF_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14720 case EF_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14721 case EF_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14722 case EF_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14723 case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14724 case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14725 case EF_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14726 case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14727 case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14728 default:
14729 _bfd_error_handler
14730 /* xgettext:c-format */
14731 (_("%pB: unknown architecture %s"),
14732 abfd, bfd_printable_name (abfd));
14735 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14737 abiflags->isa_level = ISA_LEVEL (new_isa);
14738 abiflags->isa_rev = ISA_REV (new_isa);
14741 /* Update the isa_ext if ABFD describes a further extension. */
14742 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14743 bfd_get_mach (abfd)))
14744 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14747 /* Return true if the given ELF header flags describe a 32-bit binary. */
14749 static bool
14750 mips_32bit_flags_p (flagword flags)
14752 return ((flags & EF_MIPS_32BITMODE) != 0
14753 || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
14754 || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
14755 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
14756 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
14757 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
14758 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
14759 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
14762 /* Infer the content of the ABI flags based on the elf header. */
14764 static void
14765 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14767 obj_attribute *in_attr;
14769 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14770 update_mips_abiflags_isa (abfd, abiflags);
14772 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14773 abiflags->gpr_size = AFL_REG_32;
14774 else
14775 abiflags->gpr_size = AFL_REG_64;
14777 abiflags->cpr1_size = AFL_REG_NONE;
14779 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14780 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14782 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14783 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14784 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14785 && abiflags->gpr_size == AFL_REG_32))
14786 abiflags->cpr1_size = AFL_REG_32;
14787 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14788 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14789 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14790 abiflags->cpr1_size = AFL_REG_64;
14792 abiflags->cpr2_size = AFL_REG_NONE;
14794 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14795 abiflags->ases |= AFL_ASE_MDMX;
14796 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14797 abiflags->ases |= AFL_ASE_MIPS16;
14798 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14799 abiflags->ases |= AFL_ASE_MICROMIPS;
14801 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14802 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14803 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14804 && abiflags->isa_level >= 32
14805 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14806 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14809 /* We need to use a special link routine to handle the .reginfo and
14810 the .mdebug sections. We need to merge all instances of these
14811 sections together, not write them all out sequentially. */
14813 bool
14814 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14816 asection *o;
14817 struct bfd_link_order *p;
14818 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14819 asection *rtproc_sec, *abiflags_sec;
14820 Elf32_RegInfo reginfo;
14821 struct ecoff_debug_info debug;
14822 struct mips_htab_traverse_info hti;
14823 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14824 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14825 HDRR *symhdr = &debug.symbolic_header;
14826 void *mdebug_handle = NULL;
14827 asection *s;
14828 EXTR esym;
14829 unsigned int i;
14830 bfd_size_type amt;
14831 struct mips_elf_link_hash_table *htab;
14833 static const char * const secname[] =
14835 ".text", ".init", ".fini", ".data",
14836 ".rodata", ".sdata", ".sbss", ".bss"
14838 static const int sc[] =
14840 scText, scInit, scFini, scData,
14841 scRData, scSData, scSBss, scBss
14844 htab = mips_elf_hash_table (info);
14845 BFD_ASSERT (htab != NULL);
14847 /* Sort the dynamic symbols so that those with GOT entries come after
14848 those without. */
14849 if (!mips_elf_sort_hash_table (abfd, info))
14850 return false;
14852 /* Create any scheduled LA25 stubs. */
14853 hti.info = info;
14854 hti.output_bfd = abfd;
14855 hti.error = false;
14856 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14857 if (hti.error)
14858 return false;
14860 /* Get a value for the GP register. */
14861 if (elf_gp (abfd) == 0)
14863 struct bfd_link_hash_entry *h;
14865 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14866 if (h != NULL && h->type == bfd_link_hash_defined)
14867 elf_gp (abfd) = (h->u.def.value
14868 + h->u.def.section->output_section->vma
14869 + h->u.def.section->output_offset);
14870 else if (htab->root.target_os == is_vxworks
14871 && (h = bfd_link_hash_lookup (info->hash,
14872 "_GLOBAL_OFFSET_TABLE_",
14873 false, false, true))
14874 && h->type == bfd_link_hash_defined)
14875 elf_gp (abfd) = (h->u.def.section->output_section->vma
14876 + h->u.def.section->output_offset
14877 + h->u.def.value);
14878 else if (bfd_link_relocatable (info))
14880 bfd_vma lo = MINUS_ONE;
14882 /* Find the GP-relative section with the lowest offset. */
14883 for (o = abfd->sections; o != NULL; o = o->next)
14884 if (o->vma < lo
14885 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14886 lo = o->vma;
14888 /* And calculate GP relative to that. */
14889 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14891 else
14893 /* If the relocate_section function needs to do a reloc
14894 involving the GP value, it should make a reloc_dangerous
14895 callback to warn that GP is not defined. */
14899 /* Go through the sections and collect the .reginfo and .mdebug
14900 information. */
14901 abiflags_sec = NULL;
14902 reginfo_sec = NULL;
14903 mdebug_sec = NULL;
14904 gptab_data_sec = NULL;
14905 gptab_bss_sec = NULL;
14906 for (o = abfd->sections; o != NULL; o = o->next)
14908 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14910 /* We have found the .MIPS.abiflags section in the output file.
14911 Look through all the link_orders comprising it and remove them.
14912 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14913 for (p = o->map_head.link_order; p != NULL; p = p->next)
14915 asection *input_section;
14917 if (p->type != bfd_indirect_link_order)
14919 if (p->type == bfd_data_link_order)
14920 continue;
14921 abort ();
14924 input_section = p->u.indirect.section;
14926 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14927 elf_link_input_bfd ignores this section. */
14928 input_section->flags &= ~SEC_HAS_CONTENTS;
14931 /* Size has been set in _bfd_mips_elf_late_size_sections. */
14932 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14934 /* Skip this section later on (I don't think this currently
14935 matters, but someday it might). */
14936 o->map_head.link_order = NULL;
14938 abiflags_sec = o;
14941 if (strcmp (o->name, ".reginfo") == 0)
14943 memset (&reginfo, 0, sizeof reginfo);
14945 /* We have found the .reginfo section in the output file.
14946 Look through all the link_orders comprising it and merge
14947 the information together. */
14948 for (p = o->map_head.link_order; p != NULL; p = p->next)
14950 asection *input_section;
14951 bfd *input_bfd;
14952 Elf32_External_RegInfo ext;
14953 Elf32_RegInfo sub;
14954 bfd_size_type sz;
14956 if (p->type != bfd_indirect_link_order)
14958 if (p->type == bfd_data_link_order)
14959 continue;
14960 abort ();
14963 input_section = p->u.indirect.section;
14964 input_bfd = input_section->owner;
14966 sz = (input_section->size < sizeof (ext)
14967 ? input_section->size : sizeof (ext));
14968 memset (&ext, 0, sizeof (ext));
14969 if (! bfd_get_section_contents (input_bfd, input_section,
14970 &ext, 0, sz))
14971 return false;
14973 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14975 reginfo.ri_gprmask |= sub.ri_gprmask;
14976 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14977 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14978 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14979 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14981 /* ri_gp_value is set by the function
14982 `_bfd_mips_elf_section_processing' when the section is
14983 finally written out. */
14985 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14986 elf_link_input_bfd ignores this section. */
14987 input_section->flags &= ~SEC_HAS_CONTENTS;
14990 /* Size has been set in _bfd_mips_elf_late_size_sections. */
14991 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14993 /* Skip this section later on (I don't think this currently
14994 matters, but someday it might). */
14995 o->map_head.link_order = NULL;
14997 reginfo_sec = o;
15000 if (strcmp (o->name, ".mdebug") == 0)
15002 struct extsym_info einfo;
15003 bfd_vma last;
15005 /* We have found the .mdebug section in the output file.
15006 Look through all the link_orders comprising it and merge
15007 the information together. */
15008 symhdr->magic = swap->sym_magic;
15009 /* FIXME: What should the version stamp be? */
15010 symhdr->vstamp = 0;
15011 symhdr->ilineMax = 0;
15012 symhdr->cbLine = 0;
15013 symhdr->idnMax = 0;
15014 symhdr->ipdMax = 0;
15015 symhdr->isymMax = 0;
15016 symhdr->ioptMax = 0;
15017 symhdr->iauxMax = 0;
15018 symhdr->issMax = 0;
15019 symhdr->issExtMax = 0;
15020 symhdr->ifdMax = 0;
15021 symhdr->crfd = 0;
15022 symhdr->iextMax = 0;
15024 /* We accumulate the debugging information itself in the
15025 debug_info structure. */
15026 debug.alloc_syments = false;
15027 debug.line = NULL;
15028 debug.external_dnr = NULL;
15029 debug.external_pdr = NULL;
15030 debug.external_sym = NULL;
15031 debug.external_opt = NULL;
15032 debug.external_aux = NULL;
15033 debug.ss = NULL;
15034 debug.ssext = debug.ssext_end = NULL;
15035 debug.external_fdr = NULL;
15036 debug.external_rfd = NULL;
15037 debug.external_ext = debug.external_ext_end = NULL;
15039 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15040 if (mdebug_handle == NULL)
15041 return false;
15043 esym.jmptbl = 0;
15044 esym.cobol_main = 0;
15045 esym.weakext = 0;
15046 esym.reserved = 0;
15047 esym.ifd = ifdNil;
15048 esym.asym.iss = issNil;
15049 esym.asym.st = stLocal;
15050 esym.asym.reserved = 0;
15051 esym.asym.index = indexNil;
15052 last = 0;
15053 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15055 esym.asym.sc = sc[i];
15056 s = bfd_get_section_by_name (abfd, secname[i]);
15057 if (s != NULL)
15059 esym.asym.value = s->vma;
15060 last = s->vma + s->size;
15062 else
15063 esym.asym.value = last;
15064 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15065 secname[i], &esym))
15066 return false;
15069 for (p = o->map_head.link_order; p != NULL; p = p->next)
15071 asection *input_section;
15072 bfd *input_bfd;
15073 const struct ecoff_debug_swap *input_swap;
15074 struct ecoff_debug_info input_debug;
15075 char *eraw_src;
15076 char *eraw_end;
15078 if (p->type != bfd_indirect_link_order)
15080 if (p->type == bfd_data_link_order)
15081 continue;
15082 abort ();
15085 input_section = p->u.indirect.section;
15086 input_bfd = input_section->owner;
15088 if (!is_mips_elf (input_bfd))
15090 /* I don't know what a non MIPS ELF bfd would be
15091 doing with a .mdebug section, but I don't really
15092 want to deal with it. */
15093 continue;
15096 input_swap = (get_elf_backend_data (input_bfd)
15097 ->elf_backend_ecoff_debug_swap);
15099 BFD_ASSERT (p->size == input_section->size);
15101 /* The ECOFF linking code expects that we have already
15102 read in the debugging information and set up an
15103 ecoff_debug_info structure, so we do that now. */
15104 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15105 &input_debug))
15106 return false;
15108 if (! (bfd_ecoff_debug_accumulate
15109 (mdebug_handle, abfd, &debug, swap, input_bfd,
15110 &input_debug, input_swap, info)))
15112 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15113 return false;
15116 /* Loop through the external symbols. For each one with
15117 interesting information, try to find the symbol in
15118 the linker global hash table and save the information
15119 for the output external symbols. */
15120 eraw_src = input_debug.external_ext;
15121 eraw_end = (eraw_src
15122 + (input_debug.symbolic_header.iextMax
15123 * input_swap->external_ext_size));
15124 for (;
15125 eraw_src < eraw_end;
15126 eraw_src += input_swap->external_ext_size)
15128 EXTR ext;
15129 const char *name;
15130 struct mips_elf_link_hash_entry *h;
15132 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15133 if (ext.asym.sc == scNil
15134 || ext.asym.sc == scUndefined
15135 || ext.asym.sc == scSUndefined)
15136 continue;
15138 name = input_debug.ssext + ext.asym.iss;
15139 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15140 name, false, false, true);
15141 if (h == NULL || h->esym.ifd != -2)
15142 continue;
15144 if (ext.ifd != -1)
15146 BFD_ASSERT (ext.ifd
15147 < input_debug.symbolic_header.ifdMax);
15148 ext.ifd = input_debug.ifdmap[ext.ifd];
15151 h->esym = ext;
15154 /* Free up the information we just read. */
15155 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15157 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15158 elf_link_input_bfd ignores this section. */
15159 input_section->flags &= ~SEC_HAS_CONTENTS;
15162 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15164 /* Create .rtproc section. */
15165 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15166 if (rtproc_sec == NULL)
15168 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15169 | SEC_LINKER_CREATED | SEC_READONLY);
15171 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15172 ".rtproc",
15173 flags);
15174 if (rtproc_sec == NULL
15175 || !bfd_set_section_alignment (rtproc_sec, 4))
15176 return false;
15179 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15180 info, rtproc_sec,
15181 &debug))
15182 return false;
15185 /* Build the external symbol information. */
15186 einfo.abfd = abfd;
15187 einfo.info = info;
15188 einfo.debug = &debug;
15189 einfo.swap = swap;
15190 einfo.failed = false;
15191 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15192 mips_elf_output_extsym, &einfo);
15193 if (einfo.failed)
15194 return false;
15196 /* Set the size of the .mdebug section. */
15197 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15199 /* Skip this section later on (I don't think this currently
15200 matters, but someday it might). */
15201 o->map_head.link_order = NULL;
15203 mdebug_sec = o;
15206 if (startswith (o->name, ".gptab."))
15208 const char *subname;
15209 unsigned int c;
15210 Elf32_gptab *tab;
15211 Elf32_External_gptab *ext_tab;
15212 unsigned int j;
15214 /* The .gptab.sdata and .gptab.sbss sections hold
15215 information describing how the small data area would
15216 change depending upon the -G switch. These sections
15217 not used in executables files. */
15218 if (! bfd_link_relocatable (info))
15220 for (p = o->map_head.link_order; p != NULL; p = p->next)
15222 asection *input_section;
15224 if (p->type != bfd_indirect_link_order)
15226 if (p->type == bfd_data_link_order)
15227 continue;
15228 abort ();
15231 input_section = p->u.indirect.section;
15233 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15234 elf_link_input_bfd ignores this section. */
15235 input_section->flags &= ~SEC_HAS_CONTENTS;
15238 /* Skip this section later on (I don't think this
15239 currently matters, but someday it might). */
15240 o->map_head.link_order = NULL;
15242 /* Really remove the section. */
15243 bfd_section_list_remove (abfd, o);
15244 --abfd->section_count;
15246 continue;
15249 /* There is one gptab for initialized data, and one for
15250 uninitialized data. */
15251 if (strcmp (o->name, ".gptab.sdata") == 0)
15252 gptab_data_sec = o;
15253 else if (strcmp (o->name, ".gptab.sbss") == 0)
15254 gptab_bss_sec = o;
15255 else
15257 _bfd_error_handler
15258 /* xgettext:c-format */
15259 (_("%pB: illegal section name `%pA'"), abfd, o);
15260 bfd_set_error (bfd_error_nonrepresentable_section);
15261 return false;
15264 /* The linker script always combines .gptab.data and
15265 .gptab.sdata into .gptab.sdata, and likewise for
15266 .gptab.bss and .gptab.sbss. It is possible that there is
15267 no .sdata or .sbss section in the output file, in which
15268 case we must change the name of the output section. */
15269 subname = o->name + sizeof ".gptab" - 1;
15270 if (bfd_get_section_by_name (abfd, subname) == NULL)
15272 if (o == gptab_data_sec)
15273 o->name = ".gptab.data";
15274 else
15275 o->name = ".gptab.bss";
15276 subname = o->name + sizeof ".gptab" - 1;
15277 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15280 /* Set up the first entry. */
15281 c = 1;
15282 amt = c * sizeof (Elf32_gptab);
15283 tab = bfd_malloc (amt);
15284 if (tab == NULL)
15285 return false;
15286 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15287 tab[0].gt_header.gt_unused = 0;
15289 /* Combine the input sections. */
15290 for (p = o->map_head.link_order; p != NULL; p = p->next)
15292 asection *input_section;
15293 bfd *input_bfd;
15294 bfd_size_type size;
15295 unsigned long last;
15296 bfd_size_type gpentry;
15298 if (p->type != bfd_indirect_link_order)
15300 if (p->type == bfd_data_link_order)
15301 continue;
15302 abort ();
15305 input_section = p->u.indirect.section;
15306 input_bfd = input_section->owner;
15308 /* Combine the gptab entries for this input section one
15309 by one. We know that the input gptab entries are
15310 sorted by ascending -G value. */
15311 size = input_section->size;
15312 last = 0;
15313 for (gpentry = sizeof (Elf32_External_gptab);
15314 gpentry < size;
15315 gpentry += sizeof (Elf32_External_gptab))
15317 Elf32_External_gptab ext_gptab;
15318 Elf32_gptab int_gptab;
15319 unsigned long val;
15320 unsigned long add;
15321 bool exact;
15322 unsigned int look;
15324 if (! (bfd_get_section_contents
15325 (input_bfd, input_section, &ext_gptab, gpentry,
15326 sizeof (Elf32_External_gptab))))
15328 free (tab);
15329 return false;
15332 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15333 &int_gptab);
15334 val = int_gptab.gt_entry.gt_g_value;
15335 add = int_gptab.gt_entry.gt_bytes - last;
15337 exact = false;
15338 for (look = 1; look < c; look++)
15340 if (tab[look].gt_entry.gt_g_value >= val)
15341 tab[look].gt_entry.gt_bytes += add;
15343 if (tab[look].gt_entry.gt_g_value == val)
15344 exact = true;
15347 if (! exact)
15349 Elf32_gptab *new_tab;
15350 unsigned int max;
15352 /* We need a new table entry. */
15353 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15354 new_tab = bfd_realloc (tab, amt);
15355 if (new_tab == NULL)
15357 free (tab);
15358 return false;
15360 tab = new_tab;
15361 tab[c].gt_entry.gt_g_value = val;
15362 tab[c].gt_entry.gt_bytes = add;
15364 /* Merge in the size for the next smallest -G
15365 value, since that will be implied by this new
15366 value. */
15367 max = 0;
15368 for (look = 1; look < c; look++)
15370 if (tab[look].gt_entry.gt_g_value < val
15371 && (max == 0
15372 || (tab[look].gt_entry.gt_g_value
15373 > tab[max].gt_entry.gt_g_value)))
15374 max = look;
15376 if (max != 0)
15377 tab[c].gt_entry.gt_bytes +=
15378 tab[max].gt_entry.gt_bytes;
15380 ++c;
15383 last = int_gptab.gt_entry.gt_bytes;
15386 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15387 elf_link_input_bfd ignores this section. */
15388 input_section->flags &= ~SEC_HAS_CONTENTS;
15391 /* The table must be sorted by -G value. */
15392 if (c > 2)
15393 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15395 /* Swap out the table. */
15396 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15397 ext_tab = bfd_alloc (abfd, amt);
15398 if (ext_tab == NULL)
15400 free (tab);
15401 return false;
15404 for (j = 0; j < c; j++)
15405 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15406 free (tab);
15408 o->size = c * sizeof (Elf32_External_gptab);
15409 o->contents = (bfd_byte *) ext_tab;
15410 o->alloced = 1;
15412 /* Skip this section later on (I don't think this currently
15413 matters, but someday it might). */
15414 o->map_head.link_order = NULL;
15418 /* Invoke the regular ELF backend linker to do all the work. */
15419 if (!bfd_elf_final_link (abfd, info))
15420 return false;
15422 /* Now write out the computed sections. */
15424 if (abiflags_sec != NULL)
15426 Elf_External_ABIFlags_v0 ext;
15427 Elf_Internal_ABIFlags_v0 *abiflags;
15429 abiflags = &mips_elf_tdata (abfd)->abiflags;
15431 /* Set up the abiflags if no valid input sections were found. */
15432 if (!mips_elf_tdata (abfd)->abiflags_valid)
15434 infer_mips_abiflags (abfd, abiflags);
15435 mips_elf_tdata (abfd)->abiflags_valid = true;
15437 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15438 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15439 return false;
15442 if (reginfo_sec != NULL)
15444 Elf32_External_RegInfo ext;
15446 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15447 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15448 return false;
15451 if (mdebug_sec != NULL)
15453 BFD_ASSERT (abfd->output_has_begun);
15454 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15455 swap, info,
15456 mdebug_sec->filepos))
15457 return false;
15459 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15462 if (gptab_data_sec != NULL)
15464 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15465 gptab_data_sec->contents,
15466 0, gptab_data_sec->size))
15467 return false;
15470 if (gptab_bss_sec != NULL)
15472 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15473 gptab_bss_sec->contents,
15474 0, gptab_bss_sec->size))
15475 return false;
15478 if (SGI_COMPAT (abfd))
15480 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15481 if (rtproc_sec != NULL)
15483 if (! bfd_set_section_contents (abfd, rtproc_sec,
15484 rtproc_sec->contents,
15485 0, rtproc_sec->size))
15486 return false;
15490 return true;
15493 /* Merge object file header flags from IBFD into OBFD. Raise an error
15494 if there are conflicting settings. */
15496 static bool
15497 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15499 bfd *obfd = info->output_bfd;
15500 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15501 flagword old_flags;
15502 flagword new_flags;
15503 bool ok;
15505 new_flags = elf_elfheader (ibfd)->e_flags;
15506 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15507 old_flags = elf_elfheader (obfd)->e_flags;
15509 /* Check flag compatibility. */
15511 new_flags &= ~EF_MIPS_NOREORDER;
15512 old_flags &= ~EF_MIPS_NOREORDER;
15514 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15515 doesn't seem to matter. */
15516 new_flags &= ~EF_MIPS_XGOT;
15517 old_flags &= ~EF_MIPS_XGOT;
15519 /* MIPSpro generates ucode info in n64 objects. Again, we should
15520 just be able to ignore this. */
15521 new_flags &= ~EF_MIPS_UCODE;
15522 old_flags &= ~EF_MIPS_UCODE;
15524 /* DSOs should only be linked with CPIC code. */
15525 if ((ibfd->flags & DYNAMIC) != 0)
15526 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15528 if (new_flags == old_flags)
15529 return true;
15531 ok = true;
15533 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15534 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15536 _bfd_error_handler
15537 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15538 ibfd);
15539 ok = true;
15542 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15543 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15544 if (! (new_flags & EF_MIPS_PIC))
15545 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15547 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15548 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15550 /* Compare the ISAs. */
15551 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15553 _bfd_error_handler
15554 (_("%pB: linking 32-bit code with 64-bit code"),
15555 ibfd);
15556 ok = false;
15558 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15560 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15561 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15563 /* Copy the architecture info from IBFD to OBFD. Also copy
15564 the 32-bit flag (if set) so that we continue to recognise
15565 OBFD as a 32-bit binary. */
15566 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15567 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15568 elf_elfheader (obfd)->e_flags
15569 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15571 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15572 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15574 /* Copy across the ABI flags if OBFD doesn't use them
15575 and if that was what caused us to treat IBFD as 32-bit. */
15576 if ((old_flags & EF_MIPS_ABI) == 0
15577 && mips_32bit_flags_p (new_flags)
15578 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15579 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15581 else
15583 /* The ISAs aren't compatible. */
15584 _bfd_error_handler
15585 /* xgettext:c-format */
15586 (_("%pB: linking %s module with previous %s modules"),
15587 ibfd,
15588 bfd_printable_name (ibfd),
15589 bfd_printable_name (obfd));
15590 ok = false;
15594 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15595 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15597 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15598 does set EI_CLASS differently from any 32-bit ABI. */
15599 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15600 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15601 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15603 /* Only error if both are set (to different values). */
15604 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15605 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15606 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15608 _bfd_error_handler
15609 /* xgettext:c-format */
15610 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15611 ibfd,
15612 elf_mips_abi_name (ibfd),
15613 elf_mips_abi_name (obfd));
15614 ok = false;
15616 new_flags &= ~EF_MIPS_ABI;
15617 old_flags &= ~EF_MIPS_ABI;
15620 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15621 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15622 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15624 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15625 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15626 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15627 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15628 int micro_mis = old_m16 && new_micro;
15629 int m16_mis = old_micro && new_m16;
15631 if (m16_mis || micro_mis)
15633 _bfd_error_handler
15634 /* xgettext:c-format */
15635 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15636 ibfd,
15637 m16_mis ? "MIPS16" : "microMIPS",
15638 m16_mis ? "microMIPS" : "MIPS16");
15639 ok = false;
15642 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15644 new_flags &= ~ EF_MIPS_ARCH_ASE;
15645 old_flags &= ~ EF_MIPS_ARCH_ASE;
15648 /* Compare NaN encodings. */
15649 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15651 /* xgettext:c-format */
15652 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15653 ibfd,
15654 (new_flags & EF_MIPS_NAN2008
15655 ? "-mnan=2008" : "-mnan=legacy"),
15656 (old_flags & EF_MIPS_NAN2008
15657 ? "-mnan=2008" : "-mnan=legacy"));
15658 ok = false;
15659 new_flags &= ~EF_MIPS_NAN2008;
15660 old_flags &= ~EF_MIPS_NAN2008;
15663 /* Compare FP64 state. */
15664 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15666 /* xgettext:c-format */
15667 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15668 ibfd,
15669 (new_flags & EF_MIPS_FP64
15670 ? "-mfp64" : "-mfp32"),
15671 (old_flags & EF_MIPS_FP64
15672 ? "-mfp64" : "-mfp32"));
15673 ok = false;
15674 new_flags &= ~EF_MIPS_FP64;
15675 old_flags &= ~EF_MIPS_FP64;
15678 /* Warn about any other mismatches */
15679 if (new_flags != old_flags)
15681 /* xgettext:c-format */
15682 _bfd_error_handler
15683 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15684 "(%#x)"),
15685 ibfd, new_flags, old_flags);
15686 ok = false;
15689 return ok;
15692 /* Merge object attributes from IBFD into OBFD. Raise an error if
15693 there are conflicting attributes. */
15694 static bool
15695 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15697 bfd *obfd = info->output_bfd;
15698 obj_attribute *in_attr;
15699 obj_attribute *out_attr;
15700 bfd *abi_fp_bfd;
15701 bfd *abi_msa_bfd;
15703 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15704 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15705 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15706 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15708 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15709 if (!abi_msa_bfd
15710 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15711 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15713 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15715 /* This is the first object. Copy the attributes. */
15716 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15718 /* Use the Tag_null value to indicate the attributes have been
15719 initialized. */
15720 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15722 return true;
15725 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15726 non-conflicting ones. */
15727 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15728 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15730 int out_fp, in_fp;
15732 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15733 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15734 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15735 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15736 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15737 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15738 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15739 || in_fp == Val_GNU_MIPS_ABI_FP_64
15740 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15742 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15743 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15745 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15746 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15747 || out_fp == Val_GNU_MIPS_ABI_FP_64
15748 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15749 /* Keep the current setting. */;
15750 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15751 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15753 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15754 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15756 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15757 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15758 /* Keep the current setting. */;
15759 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15761 const char *out_string, *in_string;
15763 out_string = _bfd_mips_fp_abi_string (out_fp);
15764 in_string = _bfd_mips_fp_abi_string (in_fp);
15765 /* First warn about cases involving unrecognised ABIs. */
15766 if (!out_string && !in_string)
15767 /* xgettext:c-format */
15768 _bfd_error_handler
15769 (_("warning: %pB uses unknown floating point ABI %d "
15770 "(set by %pB), %pB uses unknown floating point ABI %d"),
15771 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15772 else if (!out_string)
15773 _bfd_error_handler
15774 /* xgettext:c-format */
15775 (_("warning: %pB uses unknown floating point ABI %d "
15776 "(set by %pB), %pB uses %s"),
15777 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15778 else if (!in_string)
15779 _bfd_error_handler
15780 /* xgettext:c-format */
15781 (_("warning: %pB uses %s (set by %pB), "
15782 "%pB uses unknown floating point ABI %d"),
15783 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15784 else
15786 /* If one of the bfds is soft-float, the other must be
15787 hard-float. The exact choice of hard-float ABI isn't
15788 really relevant to the error message. */
15789 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15790 out_string = "-mhard-float";
15791 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15792 in_string = "-mhard-float";
15793 _bfd_error_handler
15794 /* xgettext:c-format */
15795 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15796 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15801 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15802 non-conflicting ones. */
15803 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15805 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15806 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15807 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15808 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15809 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15811 case Val_GNU_MIPS_ABI_MSA_128:
15812 _bfd_error_handler
15813 /* xgettext:c-format */
15814 (_("warning: %pB uses %s (set by %pB), "
15815 "%pB uses unknown MSA ABI %d"),
15816 obfd, "-mmsa", abi_msa_bfd,
15817 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15818 break;
15820 default:
15821 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15823 case Val_GNU_MIPS_ABI_MSA_128:
15824 _bfd_error_handler
15825 /* xgettext:c-format */
15826 (_("warning: %pB uses unknown MSA ABI %d "
15827 "(set by %pB), %pB uses %s"),
15828 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15829 abi_msa_bfd, ibfd, "-mmsa");
15830 break;
15832 default:
15833 _bfd_error_handler
15834 /* xgettext:c-format */
15835 (_("warning: %pB uses unknown MSA ABI %d "
15836 "(set by %pB), %pB uses unknown MSA ABI %d"),
15837 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15838 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15839 break;
15844 /* Merge Tag_compatibility attributes and any common GNU ones. */
15845 return _bfd_elf_merge_object_attributes (ibfd, info);
15848 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15849 there are conflicting settings. */
15851 static bool
15852 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15854 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15855 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15856 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15858 /* Update the output abiflags fp_abi using the computed fp_abi. */
15859 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15861 #define max(a, b) ((a) > (b) ? (a) : (b))
15862 /* Merge abiflags. */
15863 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15864 in_tdata->abiflags.isa_level);
15865 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15866 in_tdata->abiflags.isa_rev);
15867 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15868 in_tdata->abiflags.gpr_size);
15869 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15870 in_tdata->abiflags.cpr1_size);
15871 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15872 in_tdata->abiflags.cpr2_size);
15873 #undef max
15874 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15875 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15877 return true;
15880 /* Merge backend specific data from an object file to the output
15881 object file when linking. */
15883 bool
15884 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15886 bfd *obfd = info->output_bfd;
15887 struct mips_elf_obj_tdata *out_tdata;
15888 struct mips_elf_obj_tdata *in_tdata;
15889 bool null_input_bfd = true;
15890 asection *sec;
15891 bool ok;
15893 /* Check if we have the same endianness. */
15894 if (! _bfd_generic_verify_endian_match (ibfd, info))
15896 _bfd_error_handler
15897 (_("%pB: endianness incompatible with that of the selected emulation"),
15898 ibfd);
15899 return false;
15902 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15903 return true;
15905 in_tdata = mips_elf_tdata (ibfd);
15906 out_tdata = mips_elf_tdata (obfd);
15908 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15910 _bfd_error_handler
15911 (_("%pB: ABI is incompatible with that of the selected emulation"),
15912 ibfd);
15913 return false;
15916 /* Check to see if the input BFD actually contains any sections. If not,
15917 then it has no attributes, and its flags may not have been initialized
15918 either, but it cannot actually cause any incompatibility. */
15919 /* FIXME: This excludes any input shared library from consideration. */
15920 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15922 /* Ignore synthetic sections and empty .text, .data and .bss sections
15923 which are automatically generated by gas. Also ignore fake
15924 (s)common sections, since merely defining a common symbol does
15925 not affect compatibility. */
15926 if ((sec->flags & SEC_IS_COMMON) == 0
15927 && strcmp (sec->name, ".reginfo")
15928 && strcmp (sec->name, ".mdebug")
15929 && (sec->size != 0
15930 || (strcmp (sec->name, ".text")
15931 && strcmp (sec->name, ".data")
15932 && strcmp (sec->name, ".bss"))))
15934 null_input_bfd = false;
15935 break;
15938 if (null_input_bfd)
15939 return true;
15941 /* Populate abiflags using existing information. */
15942 if (in_tdata->abiflags_valid)
15944 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15945 Elf_Internal_ABIFlags_v0 in_abiflags;
15946 Elf_Internal_ABIFlags_v0 abiflags;
15948 /* Set up the FP ABI attribute from the abiflags if it is not already
15949 set. */
15950 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15951 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15953 infer_mips_abiflags (ibfd, &abiflags);
15954 in_abiflags = in_tdata->abiflags;
15956 /* It is not possible to infer the correct ISA revision
15957 for R3 or R5 so drop down to R2 for the checks. */
15958 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15959 in_abiflags.isa_rev = 2;
15961 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15962 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15963 _bfd_error_handler
15964 (_("%pB: warning: inconsistent ISA between e_flags and "
15965 ".MIPS.abiflags"), ibfd);
15966 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15967 && in_abiflags.fp_abi != abiflags.fp_abi)
15968 _bfd_error_handler
15969 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15970 ".MIPS.abiflags"), ibfd);
15971 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15972 _bfd_error_handler
15973 (_("%pB: warning: inconsistent ASEs between e_flags and "
15974 ".MIPS.abiflags"), ibfd);
15975 /* The isa_ext is allowed to be an extension of what can be inferred
15976 from e_flags. */
15977 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15978 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15979 _bfd_error_handler
15980 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15981 ".MIPS.abiflags"), ibfd);
15982 if (in_abiflags.flags2 != 0)
15983 _bfd_error_handler
15984 (_("%pB: warning: unexpected flag in the flags2 field of "
15985 ".MIPS.abiflags (0x%lx)"), ibfd,
15986 in_abiflags.flags2);
15988 else
15990 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15991 in_tdata->abiflags_valid = true;
15994 if (!out_tdata->abiflags_valid)
15996 /* Copy input abiflags if output abiflags are not already valid. */
15997 out_tdata->abiflags = in_tdata->abiflags;
15998 out_tdata->abiflags_valid = true;
16001 if (! elf_flags_init (obfd))
16003 elf_flags_init (obfd) = true;
16004 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16005 elf_elfheader (obfd)->e_ident[EI_CLASS]
16006 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16008 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16009 && (bfd_get_arch_info (obfd)->the_default
16010 || mips_mach_extends_p (bfd_get_mach (obfd),
16011 bfd_get_mach (ibfd))))
16013 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16014 bfd_get_mach (ibfd)))
16015 return false;
16017 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
16018 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16021 ok = true;
16023 else
16024 ok = mips_elf_merge_obj_e_flags (ibfd, info);
16026 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16028 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16030 if (!ok)
16032 bfd_set_error (bfd_error_bad_value);
16033 return false;
16036 return true;
16039 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
16041 bool
16042 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16044 BFD_ASSERT (!elf_flags_init (abfd)
16045 || elf_elfheader (abfd)->e_flags == flags);
16047 elf_elfheader (abfd)->e_flags = flags;
16048 elf_flags_init (abfd) = true;
16049 return true;
16052 char *
16053 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16055 switch (dtag)
16057 default: return "";
16058 case DT_MIPS_RLD_VERSION:
16059 return "MIPS_RLD_VERSION";
16060 case DT_MIPS_TIME_STAMP:
16061 return "MIPS_TIME_STAMP";
16062 case DT_MIPS_ICHECKSUM:
16063 return "MIPS_ICHECKSUM";
16064 case DT_MIPS_IVERSION:
16065 return "MIPS_IVERSION";
16066 case DT_MIPS_FLAGS:
16067 return "MIPS_FLAGS";
16068 case DT_MIPS_BASE_ADDRESS:
16069 return "MIPS_BASE_ADDRESS";
16070 case DT_MIPS_MSYM:
16071 return "MIPS_MSYM";
16072 case DT_MIPS_CONFLICT:
16073 return "MIPS_CONFLICT";
16074 case DT_MIPS_LIBLIST:
16075 return "MIPS_LIBLIST";
16076 case DT_MIPS_LOCAL_GOTNO:
16077 return "MIPS_LOCAL_GOTNO";
16078 case DT_MIPS_CONFLICTNO:
16079 return "MIPS_CONFLICTNO";
16080 case DT_MIPS_LIBLISTNO:
16081 return "MIPS_LIBLISTNO";
16082 case DT_MIPS_SYMTABNO:
16083 return "MIPS_SYMTABNO";
16084 case DT_MIPS_UNREFEXTNO:
16085 return "MIPS_UNREFEXTNO";
16086 case DT_MIPS_GOTSYM:
16087 return "MIPS_GOTSYM";
16088 case DT_MIPS_HIPAGENO:
16089 return "MIPS_HIPAGENO";
16090 case DT_MIPS_RLD_MAP:
16091 return "MIPS_RLD_MAP";
16092 case DT_MIPS_RLD_MAP_REL:
16093 return "MIPS_RLD_MAP_REL";
16094 case DT_MIPS_DELTA_CLASS:
16095 return "MIPS_DELTA_CLASS";
16096 case DT_MIPS_DELTA_CLASS_NO:
16097 return "MIPS_DELTA_CLASS_NO";
16098 case DT_MIPS_DELTA_INSTANCE:
16099 return "MIPS_DELTA_INSTANCE";
16100 case DT_MIPS_DELTA_INSTANCE_NO:
16101 return "MIPS_DELTA_INSTANCE_NO";
16102 case DT_MIPS_DELTA_RELOC:
16103 return "MIPS_DELTA_RELOC";
16104 case DT_MIPS_DELTA_RELOC_NO:
16105 return "MIPS_DELTA_RELOC_NO";
16106 case DT_MIPS_DELTA_SYM:
16107 return "MIPS_DELTA_SYM";
16108 case DT_MIPS_DELTA_SYM_NO:
16109 return "MIPS_DELTA_SYM_NO";
16110 case DT_MIPS_DELTA_CLASSSYM:
16111 return "MIPS_DELTA_CLASSSYM";
16112 case DT_MIPS_DELTA_CLASSSYM_NO:
16113 return "MIPS_DELTA_CLASSSYM_NO";
16114 case DT_MIPS_CXX_FLAGS:
16115 return "MIPS_CXX_FLAGS";
16116 case DT_MIPS_PIXIE_INIT:
16117 return "MIPS_PIXIE_INIT";
16118 case DT_MIPS_SYMBOL_LIB:
16119 return "MIPS_SYMBOL_LIB";
16120 case DT_MIPS_LOCALPAGE_GOTIDX:
16121 return "MIPS_LOCALPAGE_GOTIDX";
16122 case DT_MIPS_LOCAL_GOTIDX:
16123 return "MIPS_LOCAL_GOTIDX";
16124 case DT_MIPS_HIDDEN_GOTIDX:
16125 return "MIPS_HIDDEN_GOTIDX";
16126 case DT_MIPS_PROTECTED_GOTIDX:
16127 return "MIPS_PROTECTED_GOT_IDX";
16128 case DT_MIPS_OPTIONS:
16129 return "MIPS_OPTIONS";
16130 case DT_MIPS_INTERFACE:
16131 return "MIPS_INTERFACE";
16132 case DT_MIPS_DYNSTR_ALIGN:
16133 return "DT_MIPS_DYNSTR_ALIGN";
16134 case DT_MIPS_INTERFACE_SIZE:
16135 return "DT_MIPS_INTERFACE_SIZE";
16136 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16137 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16138 case DT_MIPS_PERF_SUFFIX:
16139 return "DT_MIPS_PERF_SUFFIX";
16140 case DT_MIPS_COMPACT_SIZE:
16141 return "DT_MIPS_COMPACT_SIZE";
16142 case DT_MIPS_GP_VALUE:
16143 return "DT_MIPS_GP_VALUE";
16144 case DT_MIPS_AUX_DYNAMIC:
16145 return "DT_MIPS_AUX_DYNAMIC";
16146 case DT_MIPS_PLTGOT:
16147 return "DT_MIPS_PLTGOT";
16148 case DT_MIPS_RWPLT:
16149 return "DT_MIPS_RWPLT";
16150 case DT_MIPS_XHASH:
16151 return "DT_MIPS_XHASH";
16155 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16156 not known. */
16158 const char *
16159 _bfd_mips_fp_abi_string (int fp)
16161 switch (fp)
16163 /* These strings aren't translated because they're simply
16164 option lists. */
16165 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16166 return "-mdouble-float";
16168 case Val_GNU_MIPS_ABI_FP_SINGLE:
16169 return "-msingle-float";
16171 case Val_GNU_MIPS_ABI_FP_SOFT:
16172 return "-msoft-float";
16174 case Val_GNU_MIPS_ABI_FP_OLD_64:
16175 return _("-mips32r2 -mfp64 (12 callee-saved)");
16177 case Val_GNU_MIPS_ABI_FP_XX:
16178 return "-mfpxx";
16180 case Val_GNU_MIPS_ABI_FP_64:
16181 return "-mgp32 -mfp64";
16183 case Val_GNU_MIPS_ABI_FP_64A:
16184 return "-mgp32 -mfp64 -mno-odd-spreg";
16186 default:
16187 return 0;
16191 static void
16192 print_mips_ases (FILE *file, unsigned int mask)
16194 if (mask & AFL_ASE_DSP)
16195 fputs ("\n\tDSP ASE", file);
16196 if (mask & AFL_ASE_DSPR2)
16197 fputs ("\n\tDSP R2 ASE", file);
16198 if (mask & AFL_ASE_DSPR3)
16199 fputs ("\n\tDSP R3 ASE", file);
16200 if (mask & AFL_ASE_EVA)
16201 fputs ("\n\tEnhanced VA Scheme", file);
16202 if (mask & AFL_ASE_MCU)
16203 fputs ("\n\tMCU (MicroController) ASE", file);
16204 if (mask & AFL_ASE_MDMX)
16205 fputs ("\n\tMDMX ASE", file);
16206 if (mask & AFL_ASE_MIPS3D)
16207 fputs ("\n\tMIPS-3D ASE", file);
16208 if (mask & AFL_ASE_MT)
16209 fputs ("\n\tMT ASE", file);
16210 if (mask & AFL_ASE_SMARTMIPS)
16211 fputs ("\n\tSmartMIPS ASE", file);
16212 if (mask & AFL_ASE_VIRT)
16213 fputs ("\n\tVZ ASE", file);
16214 if (mask & AFL_ASE_MSA)
16215 fputs ("\n\tMSA ASE", file);
16216 if (mask & AFL_ASE_MIPS16)
16217 fputs ("\n\tMIPS16 ASE", file);
16218 if (mask & AFL_ASE_MICROMIPS)
16219 fputs ("\n\tMICROMIPS ASE", file);
16220 if (mask & AFL_ASE_XPA)
16221 fputs ("\n\tXPA ASE", file);
16222 if (mask & AFL_ASE_MIPS16E2)
16223 fputs ("\n\tMIPS16e2 ASE", file);
16224 if (mask & AFL_ASE_CRC)
16225 fputs ("\n\tCRC ASE", file);
16226 if (mask & AFL_ASE_GINV)
16227 fputs ("\n\tGINV ASE", file);
16228 if (mask & AFL_ASE_LOONGSON_MMI)
16229 fputs ("\n\tLoongson MMI ASE", file);
16230 if (mask & AFL_ASE_LOONGSON_CAM)
16231 fputs ("\n\tLoongson CAM ASE", file);
16232 if (mask & AFL_ASE_LOONGSON_EXT)
16233 fputs ("\n\tLoongson EXT ASE", file);
16234 if (mask & AFL_ASE_LOONGSON_EXT2)
16235 fputs ("\n\tLoongson EXT2 ASE", file);
16236 if (mask == 0)
16237 fprintf (file, "\n\t%s", _("None"));
16238 else if ((mask & ~AFL_ASE_MASK) != 0)
16239 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16242 static void
16243 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16245 switch (isa_ext)
16247 case 0:
16248 fputs (_("None"), file);
16249 break;
16250 case AFL_EXT_XLR:
16251 fputs ("RMI XLR", file);
16252 break;
16253 case AFL_EXT_OCTEON3:
16254 fputs ("Cavium Networks Octeon3", file);
16255 break;
16256 case AFL_EXT_OCTEON2:
16257 fputs ("Cavium Networks Octeon2", file);
16258 break;
16259 case AFL_EXT_OCTEONP:
16260 fputs ("Cavium Networks OcteonP", file);
16261 break;
16262 case AFL_EXT_OCTEON:
16263 fputs ("Cavium Networks Octeon", file);
16264 break;
16265 case AFL_EXT_5900:
16266 fputs ("Toshiba R5900", file);
16267 break;
16268 case AFL_EXT_4650:
16269 fputs ("MIPS R4650", file);
16270 break;
16271 case AFL_EXT_4010:
16272 fputs ("LSI R4010", file);
16273 break;
16274 case AFL_EXT_4100:
16275 fputs ("NEC VR4100", file);
16276 break;
16277 case AFL_EXT_3900:
16278 fputs ("Toshiba R3900", file);
16279 break;
16280 case AFL_EXT_10000:
16281 fputs ("MIPS R10000", file);
16282 break;
16283 case AFL_EXT_SB1:
16284 fputs ("Broadcom SB-1", file);
16285 break;
16286 case AFL_EXT_4111:
16287 fputs ("NEC VR4111/VR4181", file);
16288 break;
16289 case AFL_EXT_4120:
16290 fputs ("NEC VR4120", file);
16291 break;
16292 case AFL_EXT_5400:
16293 fputs ("NEC VR5400", file);
16294 break;
16295 case AFL_EXT_5500:
16296 fputs ("NEC VR5500", file);
16297 break;
16298 case AFL_EXT_LOONGSON_2E:
16299 fputs ("ST Microelectronics Loongson 2E", file);
16300 break;
16301 case AFL_EXT_LOONGSON_2F:
16302 fputs ("ST Microelectronics Loongson 2F", file);
16303 break;
16304 case AFL_EXT_INTERAPTIV_MR2:
16305 fputs ("Imagination interAptiv MR2", file);
16306 break;
16307 default:
16308 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16309 break;
16313 static void
16314 print_mips_fp_abi_value (FILE *file, int val)
16316 switch (val)
16318 case Val_GNU_MIPS_ABI_FP_ANY:
16319 fprintf (file, _("Hard or soft float\n"));
16320 break;
16321 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16322 fprintf (file, _("Hard float (double precision)\n"));
16323 break;
16324 case Val_GNU_MIPS_ABI_FP_SINGLE:
16325 fprintf (file, _("Hard float (single precision)\n"));
16326 break;
16327 case Val_GNU_MIPS_ABI_FP_SOFT:
16328 fprintf (file, _("Soft float\n"));
16329 break;
16330 case Val_GNU_MIPS_ABI_FP_OLD_64:
16331 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16332 break;
16333 case Val_GNU_MIPS_ABI_FP_XX:
16334 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16335 break;
16336 case Val_GNU_MIPS_ABI_FP_64:
16337 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16338 break;
16339 case Val_GNU_MIPS_ABI_FP_64A:
16340 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16341 break;
16342 default:
16343 fprintf (file, "??? (%d)\n", val);
16344 break;
16348 static int
16349 get_mips_reg_size (int reg_size)
16351 return (reg_size == AFL_REG_NONE) ? 0
16352 : (reg_size == AFL_REG_32) ? 32
16353 : (reg_size == AFL_REG_64) ? 64
16354 : (reg_size == AFL_REG_128) ? 128
16355 : -1;
16358 bool
16359 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16361 FILE *file = ptr;
16363 BFD_ASSERT (abfd != NULL && ptr != NULL);
16365 /* Print normal ELF private data. */
16366 _bfd_elf_print_private_bfd_data (abfd, ptr);
16368 /* xgettext:c-format */
16369 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16371 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
16372 fprintf (file, _(" [abi=O32]"));
16373 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
16374 fprintf (file, _(" [abi=O64]"));
16375 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
16376 fprintf (file, _(" [abi=EABI32]"));
16377 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
16378 fprintf (file, _(" [abi=EABI64]"));
16379 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16380 fprintf (file, _(" [abi unknown]"));
16381 else if (ABI_N32_P (abfd))
16382 fprintf (file, _(" [abi=N32]"));
16383 else if (ABI_64_P (abfd))
16384 fprintf (file, _(" [abi=64]"));
16385 else
16386 fprintf (file, _(" [no abi set]"));
16388 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
16389 fprintf (file, " [mips1]");
16390 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
16391 fprintf (file, " [mips2]");
16392 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
16393 fprintf (file, " [mips3]");
16394 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
16395 fprintf (file, " [mips4]");
16396 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
16397 fprintf (file, " [mips5]");
16398 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
16399 fprintf (file, " [mips32]");
16400 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
16401 fprintf (file, " [mips64]");
16402 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
16403 fprintf (file, " [mips32r2]");
16404 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
16405 fprintf (file, " [mips64r2]");
16406 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
16407 fprintf (file, " [mips32r6]");
16408 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
16409 fprintf (file, " [mips64r6]");
16410 else
16411 fprintf (file, _(" [unknown ISA]"));
16413 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16414 fprintf (file, " [mdmx]");
16416 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16417 fprintf (file, " [mips16]");
16419 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16420 fprintf (file, " [micromips]");
16422 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16423 fprintf (file, " [nan2008]");
16425 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16426 fprintf (file, " [old fp64]");
16428 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16429 fprintf (file, " [32bitmode]");
16430 else
16431 fprintf (file, _(" [not 32bitmode]"));
16433 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16434 fprintf (file, " [noreorder]");
16436 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16437 fprintf (file, " [PIC]");
16439 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16440 fprintf (file, " [CPIC]");
16442 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16443 fprintf (file, " [XGOT]");
16445 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16446 fprintf (file, " [UCODE]");
16448 fputc ('\n', file);
16450 if (mips_elf_tdata (abfd)->abiflags_valid)
16452 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16453 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16454 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16455 if (abiflags->isa_rev > 1)
16456 fprintf (file, "r%d", abiflags->isa_rev);
16457 fprintf (file, "\nGPR size: %d",
16458 get_mips_reg_size (abiflags->gpr_size));
16459 fprintf (file, "\nCPR1 size: %d",
16460 get_mips_reg_size (abiflags->cpr1_size));
16461 fprintf (file, "\nCPR2 size: %d",
16462 get_mips_reg_size (abiflags->cpr2_size));
16463 fputs ("\nFP ABI: ", file);
16464 print_mips_fp_abi_value (file, abiflags->fp_abi);
16465 fputs ("ISA Extension: ", file);
16466 print_mips_isa_ext (file, abiflags->isa_ext);
16467 fputs ("\nASEs:", file);
16468 print_mips_ases (file, abiflags->ases);
16469 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16470 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16471 fputc ('\n', file);
16474 return true;
16477 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16479 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16480 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16481 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16482 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16483 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16484 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16485 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
16486 { NULL, 0, 0, 0, 0 }
16489 /* Merge non visibility st_other attributes. Ensure that the
16490 STO_OPTIONAL flag is copied into h->other, even if this is not a
16491 definiton of the symbol. */
16492 void
16493 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16494 unsigned int st_other,
16495 bool definition,
16496 bool dynamic ATTRIBUTE_UNUSED)
16498 if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16500 unsigned char other;
16502 other = (definition ? st_other : h->other);
16503 other &= ~ELF_ST_VISIBILITY (-1);
16504 h->other = other | ELF_ST_VISIBILITY (h->other);
16507 if (!definition
16508 && ELF_MIPS_IS_OPTIONAL (st_other))
16509 h->other |= STO_OPTIONAL;
16512 /* Decide whether an undefined symbol is special and can be ignored.
16513 This is the case for OPTIONAL symbols on IRIX. */
16514 bool
16515 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16517 return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16520 bool
16521 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16523 return (sym->st_shndx == SHN_COMMON
16524 || sym->st_shndx == SHN_MIPS_ACOMMON
16525 || sym->st_shndx == SHN_MIPS_SCOMMON);
16528 /* Return address for Ith PLT stub in section PLT, for relocation REL
16529 or (bfd_vma) -1 if it should not be included. */
16531 bfd_vma
16532 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16533 const arelent *rel ATTRIBUTE_UNUSED)
16535 return (plt->vma
16536 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16537 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16540 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16541 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16542 and .got.plt and also the slots may be of a different size each we walk
16543 the PLT manually fetching instructions and matching them against known
16544 patterns. To make things easier standard MIPS slots, if any, always come
16545 first. As we don't create proper ELF symbols we use the UDATA.I member
16546 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16547 with the ST_OTHER member of the ELF symbol. */
16549 long
16550 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16551 long symcount ATTRIBUTE_UNUSED,
16552 asymbol **syms ATTRIBUTE_UNUSED,
16553 long dynsymcount, asymbol **dynsyms,
16554 asymbol **ret)
16556 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16557 static const char microsuffix[] = "@micromipsplt";
16558 static const char m16suffix[] = "@mips16plt";
16559 static const char mipssuffix[] = "@plt";
16561 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16563 bool micromips_p = MICROMIPS_P (abfd);
16564 Elf_Internal_Shdr *hdr;
16565 bfd_byte *plt_data;
16566 bfd_vma plt_offset;
16567 unsigned int other;
16568 bfd_vma entry_size;
16569 bfd_vma plt0_size;
16570 asection *relplt;
16571 bfd_vma opcode;
16572 asection *plt;
16573 asymbol *send;
16574 size_t size;
16575 char *names;
16576 long counti;
16577 arelent *p;
16578 asymbol *s;
16579 char *nend;
16580 long count;
16581 long pi;
16582 long i;
16583 long n;
16585 *ret = NULL;
16587 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16588 return 0;
16590 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16591 if (relplt == NULL)
16592 return 0;
16594 hdr = &elf_section_data (relplt)->this_hdr;
16595 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16596 return 0;
16598 plt = bfd_get_section_by_name (abfd, ".plt");
16599 if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16600 return 0;
16602 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16603 if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16604 return -1;
16605 p = relplt->relocation;
16607 /* Calculating the exact amount of space required for symbols would
16608 require two passes over the PLT, so just pessimise assuming two
16609 PLT slots per relocation. */
16610 count = NUM_SHDR_ENTRIES (hdr);
16611 counti = count * bed->s->int_rels_per_ext_rel;
16612 size = 2 * count * sizeof (asymbol);
16613 size += count * (sizeof (mipssuffix) +
16614 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16615 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16616 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16618 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16619 size += sizeof (asymbol) + sizeof (pltname);
16621 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16622 return -1;
16624 if (plt->size < 16)
16625 return -1;
16627 s = *ret = bfd_malloc (size);
16628 if (s == NULL)
16629 return -1;
16630 send = s + 2 * count + 1;
16632 names = (char *) send;
16633 nend = (char *) s + size;
16634 n = 0;
16636 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16637 if (opcode == 0x3302fffe)
16639 if (!micromips_p)
16640 return -1;
16641 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16642 other = STO_MICROMIPS;
16644 else if (opcode == 0x0398c1d0)
16646 if (!micromips_p)
16647 return -1;
16648 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16649 other = STO_MICROMIPS;
16651 else
16653 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16654 other = 0;
16657 s->the_bfd = abfd;
16658 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16659 s->section = plt;
16660 s->value = 0;
16661 s->name = names;
16662 s->udata.i = other;
16663 memcpy (names, pltname, sizeof (pltname));
16664 names += sizeof (pltname);
16665 ++s, ++n;
16667 pi = 0;
16668 for (plt_offset = plt0_size;
16669 plt_offset + 8 <= plt->size && s < send;
16670 plt_offset += entry_size)
16672 bfd_vma gotplt_addr;
16673 const char *suffix;
16674 bfd_vma gotplt_hi;
16675 bfd_vma gotplt_lo;
16676 size_t suffixlen;
16678 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16680 /* Check if the second word matches the expected MIPS16 instruction. */
16681 if (opcode == 0x651aeb00)
16683 if (micromips_p)
16684 return -1;
16685 /* Truncated table??? */
16686 if (plt_offset + 16 > plt->size)
16687 break;
16688 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16689 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16690 suffixlen = sizeof (m16suffix);
16691 suffix = m16suffix;
16692 other = STO_MIPS16;
16694 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16695 else if (opcode == 0xff220000)
16697 if (!micromips_p)
16698 return -1;
16699 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16700 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16701 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16702 gotplt_lo <<= 2;
16703 gotplt_addr = gotplt_hi + gotplt_lo;
16704 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16705 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16706 suffixlen = sizeof (microsuffix);
16707 suffix = microsuffix;
16708 other = STO_MICROMIPS;
16710 /* Likewise the expected microMIPS instruction (insn32 mode). */
16711 else if ((opcode & 0xffff0000) == 0xff2f0000)
16713 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16714 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16715 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16716 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16717 gotplt_addr = gotplt_hi + gotplt_lo;
16718 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16719 suffixlen = sizeof (microsuffix);
16720 suffix = microsuffix;
16721 other = STO_MICROMIPS;
16723 /* Otherwise assume standard MIPS code. */
16724 else
16726 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16727 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16728 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16729 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16730 gotplt_addr = gotplt_hi + gotplt_lo;
16731 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16732 suffixlen = sizeof (mipssuffix);
16733 suffix = mipssuffix;
16734 other = 0;
16736 /* Truncated table??? */
16737 if (plt_offset + entry_size > plt->size)
16738 break;
16740 for (i = 0;
16741 i < count && p[pi].address != gotplt_addr;
16742 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16744 if (i < count)
16746 size_t namelen;
16747 size_t len;
16749 *s = **p[pi].sym_ptr_ptr;
16750 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16751 we are defining a symbol, ensure one of them is set. */
16752 if ((s->flags & BSF_LOCAL) == 0)
16753 s->flags |= BSF_GLOBAL;
16754 s->flags |= BSF_SYNTHETIC;
16755 s->section = plt;
16756 s->value = plt_offset;
16757 s->name = names;
16758 s->udata.i = other;
16760 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16761 namelen = len + suffixlen;
16762 if (names + namelen > nend)
16763 break;
16765 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16766 names += len;
16767 memcpy (names, suffix, suffixlen);
16768 names += suffixlen;
16770 ++s, ++n;
16771 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16775 free (plt_data);
16777 return n;
16780 /* Return the ABI flags associated with ABFD if available. */
16782 Elf_Internal_ABIFlags_v0 *
16783 bfd_mips_elf_get_abiflags (bfd *abfd)
16785 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16787 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16790 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16791 field. Taken from `libc-abis.h' generated at GNU libc build time.
16792 Using a MIPS_ prefix as other libc targets use different values. */
16793 enum
16795 MIPS_LIBC_ABI_DEFAULT = 0,
16796 MIPS_LIBC_ABI_MIPS_PLT,
16797 MIPS_LIBC_ABI_UNIQUE,
16798 MIPS_LIBC_ABI_MIPS_O32_FP64,
16799 MIPS_LIBC_ABI_ABSOLUTE,
16800 MIPS_LIBC_ABI_XHASH,
16801 MIPS_LIBC_ABI_MAX
16804 bool
16805 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16807 struct mips_elf_link_hash_table *htab = NULL;
16808 Elf_Internal_Ehdr *i_ehdrp;
16810 if (!_bfd_elf_init_file_header (abfd, link_info))
16811 return false;
16813 i_ehdrp = elf_elfheader (abfd);
16814 if (link_info)
16816 htab = mips_elf_hash_table (link_info);
16817 BFD_ASSERT (htab != NULL);
16820 if (htab != NULL
16821 && htab->use_plts_and_copy_relocs
16822 && htab->root.target_os != is_vxworks)
16823 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16825 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16826 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16827 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16829 /* Mark that we need support for absolute symbols in the dynamic loader. */
16830 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16831 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16833 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16834 if it is the only hash section that will be created. */
16835 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16836 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16837 return true;
16841 _bfd_mips_elf_compact_eh_encoding
16842 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16844 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16847 /* Return the opcode for can't unwind. */
16850 _bfd_mips_elf_cant_unwind_opcode
16851 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16853 return COMPACT_EH_CANT_UNWIND_OPCODE;
16856 /* Record a position XLAT_LOC in the xlat translation table, associated with
16857 the hash entry H. The entry in the translation table will later be
16858 populated with the real symbol dynindx. */
16860 void
16861 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16862 bfd_vma xlat_loc)
16864 struct mips_elf_link_hash_entry *hmips;
16866 hmips = (struct mips_elf_link_hash_entry *) h;
16867 hmips->mipsxhash_loc = xlat_loc;