Automatic date update in version.in
[binutils-gdb.git] / bfd / elfxx-mips.c
blob59444b063a440b457e67e35d9640d830cc276f9d
1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2024 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11 This file is part of BFD, the Binary File Descriptor library.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
29 /* This file handles functionality common to the different MIPS ABI's. */
31 #include "sysdep.h"
32 #include "bfd.h"
33 #include "libbfd.h"
34 #include "libiberty.h"
35 #include "elf-bfd.h"
36 #include "ecoff-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
40 #include "dwarf2.h"
42 /* Get the ECOFF swapping routines. */
43 #include "coff/sym.h"
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
48 #include "hashtab.h"
50 /* Types of TLS GOT entry. */
51 enum mips_got_tls_type {
52 GOT_TLS_NONE,
53 GOT_TLS_GD,
54 GOT_TLS_LDM,
55 GOT_TLS_IE
58 /* This structure is used to hold information about one GOT entry.
59 There are four types of entry:
61 (1) an absolute address
62 requires: abfd == NULL
63 fields: d.address
65 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67 fields: abfd, symndx, d.addend, tls_type
69 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70 requires: abfd != NULL, symndx == -1
71 fields: d.h, tls_type
73 (4) a TLS LDM slot
74 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75 fields: none; there's only one of these per GOT. */
76 struct mips_got_entry
78 /* One input bfd that needs the GOT entry. */
79 bfd *abfd;
80 /* The index of the symbol, as stored in the relocation r_info, if
81 we have a local symbol; -1 otherwise. */
82 long symndx;
83 union
85 /* If abfd == NULL, an address that must be stored in the got. */
86 bfd_vma address;
87 /* If abfd != NULL && symndx != -1, the addend of the relocation
88 that should be added to the symbol value. */
89 bfd_vma addend;
90 /* If abfd != NULL && symndx == -1, the hash table entry
91 corresponding to a symbol in the GOT. The symbol's entry
92 is in the local area if h->global_got_area is GGA_NONE,
93 otherwise it is in the global area. */
94 struct mips_elf_link_hash_entry *h;
95 } d;
97 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
98 symbol entry with r_symndx == 0. */
99 unsigned char tls_type;
101 /* True if we have filled in the GOT contents for a TLS entry,
102 and created the associated relocations. */
103 unsigned char tls_initialized;
105 /* The offset from the beginning of the .got section to the entry
106 corresponding to this symbol+addend. If it's a global symbol
107 whose offset is yet to be decided, it's going to be -1. */
108 long gotidx;
111 /* This structure represents a GOT page reference from an input bfd.
112 Each instance represents a symbol + ADDEND, where the representation
113 of the symbol depends on whether it is local to the input bfd.
114 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
117 Page references with SYMNDX >= 0 always become page references
118 in the output. Page references with SYMNDX < 0 only become page
119 references if the symbol binds locally; in other cases, the page
120 reference decays to a global GOT reference. */
121 struct mips_got_page_ref
123 long symndx;
124 union
126 struct mips_elf_link_hash_entry *h;
127 bfd *abfd;
128 } u;
129 bfd_vma addend;
132 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133 The structures form a non-overlapping list that is sorted by increasing
134 MIN_ADDEND. */
135 struct mips_got_page_range
137 struct mips_got_page_range *next;
138 bfd_signed_vma min_addend;
139 bfd_signed_vma max_addend;
142 /* This structure describes the range of addends that are applied to page
143 relocations against a given section. */
144 struct mips_got_page_entry
146 /* The section that these entries are based on. */
147 asection *sec;
148 /* The ranges for this page entry. */
149 struct mips_got_page_range *ranges;
150 /* The maximum number of page entries needed for RANGES. */
151 bfd_vma num_pages;
154 /* This structure is used to hold .got information when linking. */
156 struct mips_got_info
158 /* The number of global .got entries. */
159 unsigned int global_gotno;
160 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
161 unsigned int reloc_only_gotno;
162 /* The number of .got slots used for TLS. */
163 unsigned int tls_gotno;
164 /* The first unused TLS .got entry. Used only during
165 mips_elf_initialize_tls_index. */
166 unsigned int tls_assigned_gotno;
167 /* The number of local .got entries, eventually including page entries. */
168 unsigned int local_gotno;
169 /* The maximum number of page entries needed. */
170 unsigned int page_gotno;
171 /* The number of relocations needed for the GOT entries. */
172 unsigned int relocs;
173 /* The first unused local .got entry. */
174 unsigned int assigned_low_gotno;
175 /* The last unused local .got entry. */
176 unsigned int assigned_high_gotno;
177 /* A hash table holding members of the got. */
178 struct htab *got_entries;
179 /* A hash table holding mips_got_page_ref structures. */
180 struct htab *got_page_refs;
181 /* A hash table of mips_got_page_entry structures. */
182 struct htab *got_page_entries;
183 /* In multi-got links, a pointer to the next got (err, rather, most
184 of the time, it points to the previous got). */
185 struct mips_got_info *next;
188 /* Structure passed when merging bfds' gots. */
190 struct mips_elf_got_per_bfd_arg
192 /* The output bfd. */
193 bfd *obfd;
194 /* The link information. */
195 struct bfd_link_info *info;
196 /* A pointer to the primary got, i.e., the one that's going to get
197 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198 DT_MIPS_GOTSYM. */
199 struct mips_got_info *primary;
200 /* A non-primary got we're trying to merge with other input bfd's
201 gots. */
202 struct mips_got_info *current;
203 /* The maximum number of got entries that can be addressed with a
204 16-bit offset. */
205 unsigned int max_count;
206 /* The maximum number of page entries needed by each got. */
207 unsigned int max_pages;
208 /* The total number of global entries which will live in the
209 primary got and be automatically relocated. This includes
210 those not referenced by the primary GOT but included in
211 the "master" GOT. */
212 unsigned int global_count;
215 /* A structure used to pass information to htab_traverse callbacks
216 when laying out the GOT. */
218 struct mips_elf_traverse_got_arg
220 struct bfd_link_info *info;
221 struct mips_got_info *g;
222 int value;
225 struct _mips_elf_section_data
227 struct bfd_elf_section_data elf;
228 union
230 bfd_byte *tdata;
231 } u;
234 #define mips_elf_section_data(sec) \
235 ((struct _mips_elf_section_data *) elf_section_data (sec))
237 #define is_mips_elf(bfd) \
238 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
239 && elf_tdata (bfd) != NULL \
240 && elf_object_id (bfd) == MIPS_ELF_DATA)
242 /* The ABI says that every symbol used by dynamic relocations must have
243 a global GOT entry. Among other things, this provides the dynamic
244 linker with a free, directly-indexed cache. The GOT can therefore
245 contain symbols that are not referenced by GOT relocations themselves
246 (in other words, it may have symbols that are not referenced by things
247 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
249 GOT relocations are less likely to overflow if we put the associated
250 GOT entries towards the beginning. We therefore divide the global
251 GOT entries into two areas: "normal" and "reloc-only". Entries in
252 the first area can be used for both dynamic relocations and GP-relative
253 accesses, while those in the "reloc-only" area are for dynamic
254 relocations only.
256 These GGA_* ("Global GOT Area") values are organised so that lower
257 values are more general than higher values. Also, non-GGA_NONE
258 values are ordered by the position of the area in the GOT. */
259 #define GGA_NORMAL 0
260 #define GGA_RELOC_ONLY 1
261 #define GGA_NONE 2
263 /* Information about a non-PIC interface to a PIC function. There are
264 two ways of creating these interfaces. The first is to add:
266 lui $25,%hi(func)
267 addiu $25,$25,%lo(func)
269 immediately before a PIC function "func". The second is to add:
271 lui $25,%hi(func)
272 j func
273 addiu $25,$25,%lo(func)
275 to a separate trampoline section.
277 Stubs of the first kind go in a new section immediately before the
278 target function. Stubs of the second kind go in a single section
279 pointed to by the hash table's "strampoline" field. */
280 struct mips_elf_la25_stub {
281 /* The generated section that contains this stub. */
282 asection *stub_section;
284 /* The offset of the stub from the start of STUB_SECTION. */
285 bfd_vma offset;
287 /* One symbol for the original function. Its location is available
288 in H->root.root.u.def. */
289 struct mips_elf_link_hash_entry *h;
292 /* Macros for populating a mips_elf_la25_stub. */
294 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
295 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296 #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
298 #define LA25_LUI_MICROMIPS(VAL) \
299 (0x41b90000 | (VAL)) /* lui t9,VAL */
300 #define LA25_J_MICROMIPS(VAL) \
301 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
302 #define LA25_ADDIU_MICROMIPS(VAL) \
303 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
305 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
306 the dynamic symbols. */
308 struct mips_elf_hash_sort_data
310 /* The symbol in the global GOT with the lowest dynamic symbol table
311 index. */
312 struct elf_link_hash_entry *low;
313 /* The least dynamic symbol table index corresponding to a non-TLS
314 symbol with a GOT entry. */
315 bfd_size_type min_got_dynindx;
316 /* The greatest dynamic symbol table index corresponding to a symbol
317 with a GOT entry that is not referenced (e.g., a dynamic symbol
318 with dynamic relocations pointing to it from non-primary GOTs). */
319 bfd_size_type max_unref_got_dynindx;
320 /* The greatest dynamic symbol table index corresponding to a local
321 symbol. */
322 bfd_size_type max_local_dynindx;
323 /* The greatest dynamic symbol table index corresponding to an external
324 symbol without a GOT entry. */
325 bfd_size_type max_non_got_dynindx;
326 /* If non-NULL, output BFD for .MIPS.xhash finalization. */
327 bfd *output_bfd;
328 /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329 real final dynindx. */
330 bfd_byte *mipsxhash;
333 /* We make up to two PLT entries if needed, one for standard MIPS code
334 and one for compressed code, either a MIPS16 or microMIPS one. We
335 keep a separate record of traditional lazy-binding stubs, for easier
336 processing. */
338 struct plt_entry
340 /* Traditional SVR4 stub offset, or -1 if none. */
341 bfd_vma stub_offset;
343 /* Standard PLT entry offset, or -1 if none. */
344 bfd_vma mips_offset;
346 /* Compressed PLT entry offset, or -1 if none. */
347 bfd_vma comp_offset;
349 /* The corresponding .got.plt index, or -1 if none. */
350 bfd_vma gotplt_index;
352 /* Whether we need a standard PLT entry. */
353 unsigned int need_mips : 1;
355 /* Whether we need a compressed PLT entry. */
356 unsigned int need_comp : 1;
359 /* The MIPS ELF linker needs additional information for each symbol in
360 the global hash table. */
362 struct mips_elf_link_hash_entry
364 struct elf_link_hash_entry root;
366 /* External symbol information. */
367 EXTR esym;
369 /* The la25 stub we have created for ths symbol, if any. */
370 struct mips_elf_la25_stub *la25_stub;
372 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373 this symbol. */
374 unsigned int possibly_dynamic_relocs;
376 /* If there is a stub that 32 bit functions should use to call this
377 16 bit function, this points to the section containing the stub. */
378 asection *fn_stub;
380 /* If there is a stub that 16 bit functions should use to call this
381 32 bit function, this points to the section containing the stub. */
382 asection *call_stub;
384 /* This is like the call_stub field, but it is used if the function
385 being called returns a floating point value. */
386 asection *call_fp_stub;
388 /* If non-zero, location in .MIPS.xhash to write real final dynindx. */
389 bfd_vma mipsxhash_loc;
391 /* The highest GGA_* value that satisfies all references to this symbol. */
392 unsigned int global_got_area : 2;
394 /* True if all GOT relocations against this symbol are for calls. This is
395 a looser condition than no_fn_stub below, because there may be other
396 non-call non-GOT relocations against the symbol. */
397 unsigned int got_only_for_calls : 1;
399 /* True if one of the relocations described by possibly_dynamic_relocs
400 is against a readonly section. */
401 unsigned int readonly_reloc : 1;
403 /* True if there is a relocation against this symbol that must be
404 resolved by the static linker (in other words, if the relocation
405 cannot possibly be made dynamic). */
406 unsigned int has_static_relocs : 1;
408 /* True if we must not create a .MIPS.stubs entry for this symbol.
409 This is set, for example, if there are relocations related to
410 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
412 unsigned int no_fn_stub : 1;
414 /* Whether we need the fn_stub; this is true if this symbol appears
415 in any relocs other than a 16 bit call. */
416 unsigned int need_fn_stub : 1;
418 /* True if this symbol is referenced by branch relocations from
419 any non-PIC input file. This is used to determine whether an
420 la25 stub is required. */
421 unsigned int has_nonpic_branches : 1;
423 /* Does this symbol need a traditional MIPS lazy-binding stub
424 (as opposed to a PLT entry)? */
425 unsigned int needs_lazy_stub : 1;
427 /* Does this symbol resolve to a PLT entry? */
428 unsigned int use_plt_entry : 1;
431 /* MIPS ELF linker hash table. */
433 struct mips_elf_link_hash_table
435 struct elf_link_hash_table root;
437 /* The number of .rtproc entries. */
438 bfd_size_type procedure_count;
440 /* The size of the .compact_rel section (if SGI_COMPAT). */
441 bfd_size_type compact_rel_size;
443 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
445 bool use_rld_obj_head;
447 /* The __rld_map or __rld_obj_head symbol. */
448 struct elf_link_hash_entry *rld_symbol;
450 /* This is set if we see any mips16 stub sections. */
451 bool mips16_stubs_seen;
453 /* True if we can generate copy relocs and PLTs. */
454 bool use_plts_and_copy_relocs;
456 /* True if we can only use 32-bit microMIPS instructions. */
457 bool insn32;
459 /* True if we suppress checks for invalid branches between ISA modes. */
460 bool ignore_branch_isa;
462 /* True if we are targetting R6 compact branches. */
463 bool compact_branches;
465 /* True if we already reported the small-data section overflow. */
466 bool small_data_overflow_reported;
468 /* True if we use the special `__gnu_absolute_zero' symbol. */
469 bool use_absolute_zero;
471 /* True if we have been configured for a GNU target. */
472 bool gnu_target;
474 /* Shortcuts to some dynamic sections, or NULL if they are not
475 being used. */
476 asection *srelplt2;
477 asection *sstubs;
479 /* The master GOT information. */
480 struct mips_got_info *got_info;
482 /* The global symbol in the GOT with the lowest index in the dynamic
483 symbol table. */
484 struct elf_link_hash_entry *global_gotsym;
486 /* The size of the PLT header in bytes. */
487 bfd_vma plt_header_size;
489 /* The size of a standard PLT entry in bytes. */
490 bfd_vma plt_mips_entry_size;
492 /* The size of a compressed PLT entry in bytes. */
493 bfd_vma plt_comp_entry_size;
495 /* The offset of the next standard PLT entry to create. */
496 bfd_vma plt_mips_offset;
498 /* The offset of the next compressed PLT entry to create. */
499 bfd_vma plt_comp_offset;
501 /* The index of the next .got.plt entry to create. */
502 bfd_vma plt_got_index;
504 /* The number of functions that need a lazy-binding stub. */
505 bfd_vma lazy_stub_count;
507 /* The size of a function stub entry in bytes. */
508 bfd_vma function_stub_size;
510 /* The number of reserved entries at the beginning of the GOT. */
511 unsigned int reserved_gotno;
513 /* The section used for mips_elf_la25_stub trampolines.
514 See the comment above that structure for details. */
515 asection *strampoline;
517 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518 pairs. */
519 htab_t la25_stubs;
521 /* A function FN (NAME, IS, OS) that creates a new input section
522 called NAME and links it to output section OS. If IS is nonnull,
523 the new section should go immediately before it, otherwise it
524 should go at the (current) beginning of OS.
526 The function returns the new section on success, otherwise it
527 returns null. */
528 asection *(*add_stub_section) (const char *, asection *, asection *);
530 /* Is the PLT header compressed? */
531 unsigned int plt_header_is_comp : 1;
534 /* Get the MIPS ELF linker hash table from a link_info structure. */
536 #define mips_elf_hash_table(p) \
537 ((is_elf_hash_table ((p)->hash) \
538 && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA) \
539 ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
541 /* A structure used to communicate with htab_traverse callbacks. */
542 struct mips_htab_traverse_info
544 /* The usual link-wide information. */
545 struct bfd_link_info *info;
546 bfd *output_bfd;
548 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
549 bool error;
552 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
554 that contains the relocation field and DATA points to the start of
555 INPUT_SECTION. */
557 struct mips_hi16
559 struct mips_hi16 *next;
560 bfd_byte *data;
561 asection *input_section;
562 arelent rel;
565 /* MIPS ELF private object data. */
567 struct mips_elf_obj_tdata
569 /* Generic ELF private object data. */
570 struct elf_obj_tdata root;
572 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
573 bfd *abi_fp_bfd;
575 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
576 bfd *abi_msa_bfd;
578 /* The abiflags for this object. */
579 Elf_Internal_ABIFlags_v0 abiflags;
580 bool abiflags_valid;
582 /* The GOT requirements of input bfds. */
583 struct mips_got_info *got;
585 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
586 included directly in this one, but there's no point to wasting
587 the memory just for the infrequently called find_nearest_line. */
588 struct mips_elf_find_line *find_line_info;
590 /* An array of stub sections indexed by symbol number. */
591 asection **local_stubs;
592 asection **local_call_stubs;
594 /* The Irix 5 support uses two virtual sections, which represent
595 text/data symbols defined in dynamic objects. */
596 asymbol *elf_data_symbol;
597 asymbol *elf_text_symbol;
598 asection *elf_data_section;
599 asection *elf_text_section;
601 struct mips_hi16 *mips_hi16_list;
604 /* Get MIPS ELF private object data from BFD's tdata. */
606 #define mips_elf_tdata(bfd) \
607 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
609 #define TLS_RELOC_P(r_type) \
610 (r_type == R_MIPS_TLS_DTPMOD32 \
611 || r_type == R_MIPS_TLS_DTPMOD64 \
612 || r_type == R_MIPS_TLS_DTPREL32 \
613 || r_type == R_MIPS_TLS_DTPREL64 \
614 || r_type == R_MIPS_TLS_GD \
615 || r_type == R_MIPS_TLS_LDM \
616 || r_type == R_MIPS_TLS_DTPREL_HI16 \
617 || r_type == R_MIPS_TLS_DTPREL_LO16 \
618 || r_type == R_MIPS_TLS_GOTTPREL \
619 || r_type == R_MIPS_TLS_TPREL32 \
620 || r_type == R_MIPS_TLS_TPREL64 \
621 || r_type == R_MIPS_TLS_TPREL_HI16 \
622 || r_type == R_MIPS_TLS_TPREL_LO16 \
623 || r_type == R_MIPS16_TLS_GD \
624 || r_type == R_MIPS16_TLS_LDM \
625 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
626 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
627 || r_type == R_MIPS16_TLS_GOTTPREL \
628 || r_type == R_MIPS16_TLS_TPREL_HI16 \
629 || r_type == R_MIPS16_TLS_TPREL_LO16 \
630 || r_type == R_MICROMIPS_TLS_GD \
631 || r_type == R_MICROMIPS_TLS_LDM \
632 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
633 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
634 || r_type == R_MICROMIPS_TLS_GOTTPREL \
635 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
636 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
638 /* Structure used to pass information to mips_elf_output_extsym. */
640 struct extsym_info
642 bfd *abfd;
643 struct bfd_link_info *info;
644 struct ecoff_debug_info *debug;
645 const struct ecoff_debug_swap *swap;
646 bool failed;
649 /* The names of the runtime procedure table symbols used on IRIX5. */
651 static const char * const mips_elf_dynsym_rtproc_names[] =
653 "_procedure_table",
654 "_procedure_string_table",
655 "_procedure_table_size",
656 NULL
659 /* These structures are used to generate the .compact_rel section on
660 IRIX5. */
662 typedef struct
664 unsigned long id1; /* Always one? */
665 unsigned long num; /* Number of compact relocation entries. */
666 unsigned long id2; /* Always two? */
667 unsigned long offset; /* The file offset of the first relocation. */
668 unsigned long reserved0; /* Zero? */
669 unsigned long reserved1; /* Zero? */
670 } Elf32_compact_rel;
672 typedef struct
674 bfd_byte id1[4];
675 bfd_byte num[4];
676 bfd_byte id2[4];
677 bfd_byte offset[4];
678 bfd_byte reserved0[4];
679 bfd_byte reserved1[4];
680 } Elf32_External_compact_rel;
682 typedef struct
684 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
685 unsigned int rtype : 4; /* Relocation types. See below. */
686 unsigned int dist2to : 8;
687 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688 unsigned long konst; /* KONST field. See below. */
689 unsigned long vaddr; /* VADDR to be relocated. */
690 } Elf32_crinfo;
692 typedef struct
694 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
695 unsigned int rtype : 4; /* Relocation types. See below. */
696 unsigned int dist2to : 8;
697 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698 unsigned long konst; /* KONST field. See below. */
699 } Elf32_crinfo2;
701 typedef struct
703 bfd_byte info[4];
704 bfd_byte konst[4];
705 bfd_byte vaddr[4];
706 } Elf32_External_crinfo;
708 typedef struct
710 bfd_byte info[4];
711 bfd_byte konst[4];
712 } Elf32_External_crinfo2;
714 /* These are the constants used to swap the bitfields in a crinfo. */
716 #define CRINFO_CTYPE (0x1U)
717 #define CRINFO_CTYPE_SH (31)
718 #define CRINFO_RTYPE (0xfU)
719 #define CRINFO_RTYPE_SH (27)
720 #define CRINFO_DIST2TO (0xffU)
721 #define CRINFO_DIST2TO_SH (19)
722 #define CRINFO_RELVADDR (0x7ffffU)
723 #define CRINFO_RELVADDR_SH (0)
725 /* A compact relocation info has long (3 words) or short (2 words)
726 formats. A short format doesn't have VADDR field and relvaddr
727 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
728 #define CRF_MIPS_LONG 1
729 #define CRF_MIPS_SHORT 0
731 /* There are 4 types of compact relocation at least. The value KONST
732 has different meaning for each type:
734 (type) (konst)
735 CT_MIPS_REL32 Address in data
736 CT_MIPS_WORD Address in word (XXX)
737 CT_MIPS_GPHI_LO GP - vaddr
738 CT_MIPS_JMPAD Address to jump
741 #define CRT_MIPS_REL32 0xa
742 #define CRT_MIPS_WORD 0xb
743 #define CRT_MIPS_GPHI_LO 0xc
744 #define CRT_MIPS_JMPAD 0xd
746 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
747 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
748 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
749 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
751 /* The structure of the runtime procedure descriptor created by the
752 loader for use by the static exception system. */
754 typedef struct runtime_pdr {
755 bfd_vma adr; /* Memory address of start of procedure. */
756 long regmask; /* Save register mask. */
757 long regoffset; /* Save register offset. */
758 long fregmask; /* Save floating point register mask. */
759 long fregoffset; /* Save floating point register offset. */
760 long frameoffset; /* Frame size. */
761 short framereg; /* Frame pointer register. */
762 short pcreg; /* Offset or reg of return pc. */
763 long irpss; /* Index into the runtime string table. */
764 long reserved;
765 struct exception_info *exception_info;/* Pointer to exception array. */
766 } RPDR, *pRPDR;
767 #define cbRPDR sizeof (RPDR)
768 #define rpdNil ((pRPDR) 0)
770 static struct mips_got_entry *mips_elf_create_local_got_entry
771 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772 struct mips_elf_link_hash_entry *, int);
773 static bool mips_elf_sort_hash_table_f
774 (struct mips_elf_link_hash_entry *, void *);
775 static bfd_vma mips_elf_high
776 (bfd_vma);
777 static bool mips_elf_create_dynamic_relocation
778 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780 bfd_vma *, asection *);
781 static bfd_vma mips_elf_adjust_gp
782 (bfd *, struct mips_got_info *, bfd *);
784 /* This will be used when we sort the dynamic relocation records. */
785 static bfd *reldyn_sorting_bfd;
787 /* True if ABFD is for CPUs with load interlocking that include
788 non-MIPS1 CPUs and R3900. */
789 #define LOAD_INTERLOCKS_P(abfd) \
790 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
791 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
793 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794 This should be safe for all architectures. We enable this predicate
795 for RM9000 for now. */
796 #define JAL_TO_BAL_P(abfd) \
797 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
799 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800 This should be safe for all architectures. We enable this predicate for
801 all CPUs. */
802 #define JALR_TO_BAL_P(abfd) 1
804 /* True if ABFD is for CPUs that are faster if JR is converted to B.
805 This should be safe for all architectures. We enable this predicate for
806 all CPUs. */
807 #define JR_TO_B_P(abfd) 1
809 /* True if ABFD is a PIC object. */
810 #define PIC_OBJECT_P(abfd) \
811 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
813 /* Nonzero if ABFD is using the O32 ABI. */
814 #define ABI_O32_P(abfd) \
815 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
817 /* Nonzero if ABFD is using the N32 ABI. */
818 #define ABI_N32_P(abfd) \
819 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
821 /* Nonzero if ABFD is using the N64 ABI. */
822 #define ABI_64_P(abfd) \
823 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
825 /* Nonzero if ABFD is using NewABI conventions. */
826 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
828 /* Nonzero if ABFD has microMIPS code. */
829 #define MICROMIPS_P(abfd) \
830 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
832 /* Nonzero if ABFD is MIPS R6. */
833 #define MIPSR6_P(abfd) \
834 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
835 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
837 /* The IRIX compatibility level we are striving for. */
838 #define IRIX_COMPAT(abfd) \
839 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
841 /* Whether we are trying to be compatible with IRIX at all. */
842 #define SGI_COMPAT(abfd) \
843 (IRIX_COMPAT (abfd) != ict_none)
845 /* The name of the options section. */
846 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
849 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
851 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
854 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
855 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856 (strcmp (NAME, ".MIPS.abiflags") == 0)
858 /* Whether the section is readonly. */
859 #define MIPS_ELF_READONLY_SECTION(sec) \
860 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
861 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
863 /* The name of the stub section. */
864 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
866 /* The size of an external REL relocation. */
867 #define MIPS_ELF_REL_SIZE(abfd) \
868 (get_elf_backend_data (abfd)->s->sizeof_rel)
870 /* The size of an external RELA relocation. */
871 #define MIPS_ELF_RELA_SIZE(abfd) \
872 (get_elf_backend_data (abfd)->s->sizeof_rela)
874 /* The size of an external dynamic table entry. */
875 #define MIPS_ELF_DYN_SIZE(abfd) \
876 (get_elf_backend_data (abfd)->s->sizeof_dyn)
878 /* The size of a GOT entry. */
879 #define MIPS_ELF_GOT_SIZE(abfd) \
880 (get_elf_backend_data (abfd)->s->arch_size / 8)
882 /* The size of the .rld_map section. */
883 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884 (get_elf_backend_data (abfd)->s->arch_size / 8)
886 /* The size of a symbol-table entry. */
887 #define MIPS_ELF_SYM_SIZE(abfd) \
888 (get_elf_backend_data (abfd)->s->sizeof_sym)
890 /* The default alignment for sections, as a power of two. */
891 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
892 (get_elf_backend_data (abfd)->s->log_file_align)
894 /* Get word-sized data. */
895 #define MIPS_ELF_GET_WORD(abfd, ptr) \
896 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
898 /* Put out word-sized data. */
899 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
900 (ABI_64_P (abfd) \
901 ? bfd_put_64 (abfd, val, ptr) \
902 : bfd_put_32 (abfd, val, ptr))
904 /* The opcode for word-sized loads (LW or LD). */
905 #define MIPS_ELF_LOAD_WORD(abfd) \
906 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
908 /* Add a dynamic symbol table-entry. */
909 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
910 _bfd_elf_add_dynamic_entry (info, tag, val)
912 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
913 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
915 /* The name of the dynamic relocation section. */
916 #define MIPS_ELF_REL_DYN_NAME(INFO) \
917 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918 ? ".rela.dyn" : ".rel.dyn")
920 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921 from smaller values. Start with zero, widen, *then* decrement. */
922 #define MINUS_ONE (((bfd_vma)0) - 1)
923 #define MINUS_TWO (((bfd_vma)0) - 2)
925 /* The value to write into got[1] for SVR4 targets, to identify it is
926 a GNU object. The dynamic linker can then use got[1] to store the
927 module pointer. */
928 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
931 /* The offset of $gp from the beginning of the .got section. */
932 #define ELF_MIPS_GP_OFFSET(INFO) \
933 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934 ? 0x0 : 0x7ff0)
936 /* The maximum size of the GOT for it to be addressable using 16-bit
937 offsets from $gp. */
938 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
940 /* Instructions which appear in a stub. */
941 #define STUB_LW(abfd) \
942 ((ABI_64_P (abfd) \
943 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
944 : 0x8f998010)) /* lw t9,0x8010(gp) */
945 #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
946 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
947 #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
948 #define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
949 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
950 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
951 #define STUB_LI16S(abfd, VAL) \
952 ((ABI_64_P (abfd) \
953 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
954 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
956 /* Likewise for the microMIPS ASE. */
957 #define STUB_LW_MICROMIPS(abfd) \
958 (ABI_64_P (abfd) \
959 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
960 : 0xff3c8010) /* lw t9,0x8010(gp) */
961 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
962 #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
963 #define STUB_LUI_MICROMIPS(VAL) \
964 (0x41b80000 + (VAL)) /* lui t8,VAL */
965 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
966 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
967 #define STUB_ORI_MICROMIPS(VAL) \
968 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
969 #define STUB_LI16U_MICROMIPS(VAL) \
970 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
971 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
972 (ABI_64_P (abfd) \
973 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
974 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
976 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
978 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
983 /* The name of the dynamic interpreter. This is put in the .interp
984 section. */
986 #define ELF_DYNAMIC_INTERPRETER(abfd) \
987 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
988 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
989 : "/usr/lib/libc.so.1")
991 #ifdef BFD64
992 #define MNAME(bfd,pre,pos) \
993 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994 #define ELF_R_SYM(bfd, i) \
995 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996 #define ELF_R_TYPE(bfd, i) \
997 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998 #define ELF_R_INFO(bfd, s, t) \
999 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000 #else
1001 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002 #define ELF_R_SYM(bfd, i) \
1003 (ELF32_R_SYM (i))
1004 #define ELF_R_TYPE(bfd, i) \
1005 (ELF32_R_TYPE (i))
1006 #define ELF_R_INFO(bfd, s, t) \
1007 (ELF32_R_INFO (s, t))
1008 #endif
1010 /* The mips16 compiler uses a couple of special sections to handle
1011 floating point arguments.
1013 Section names that look like .mips16.fn.FNNAME contain stubs that
1014 copy floating point arguments from the fp regs to the gp regs and
1015 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1016 call should be redirected to the stub instead. If no 32 bit
1017 function calls FNNAME, the stub should be discarded. We need to
1018 consider any reference to the function, not just a call, because
1019 if the address of the function is taken we will need the stub,
1020 since the address might be passed to a 32 bit function.
1022 Section names that look like .mips16.call.FNNAME contain stubs
1023 that copy floating point arguments from the gp regs to the fp
1024 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1025 then any 16 bit function that calls FNNAME should be redirected
1026 to the stub instead. If FNNAME is not a 32 bit function, the
1027 stub should be discarded.
1029 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030 which call FNNAME and then copy the return value from the fp regs
1031 to the gp regs. These stubs store the return value in $18 while
1032 calling FNNAME; any function which might call one of these stubs
1033 must arrange to save $18 around the call. (This case is not
1034 needed for 32 bit functions that call 16 bit functions, because
1035 16 bit functions always return floating point values in both
1036 $f0/$f1 and $2/$3.)
1038 Note that in all cases FNNAME might be defined statically.
1039 Therefore, FNNAME is not used literally. Instead, the relocation
1040 information will indicate which symbol the section is for.
1042 We record any stubs that we find in the symbol table. */
1044 #define FN_STUB ".mips16.fn."
1045 #define CALL_STUB ".mips16.call."
1046 #define CALL_FP_STUB ".mips16.call.fp."
1048 #define FN_STUB_P(name) startswith (name, FN_STUB)
1049 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1052 /* The format of the first PLT entry in an O32 executable. */
1053 static const bfd_vma mips_o32_exec_plt0_entry[] =
1055 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1056 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1057 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1058 0x031cc023, /* subu $24, $24, $28 */
1059 0x03e07825, /* or t7, ra, zero */
1060 0x0018c082, /* srl $24, $24, 2 */
1061 0x0320f809, /* jalr $25 */
1062 0x2718fffe /* subu $24, $24, 2 */
1065 /* The format of the first PLT entry in an O32 executable using compact
1066 jumps. */
1067 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1069 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1070 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1071 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1072 0x031cc023, /* subu $24, $24, $28 */
1073 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1074 0x0018c082, /* srl $24, $24, 2 */
1075 0x2718fffe, /* subu $24, $24, 2 */
1076 0xf8190000 /* jalrc $25 */
1079 /* The format of the first PLT entry in an N32 executable. Different
1080 because gp ($28) is not available; we use t2 ($14) instead. */
1081 static const bfd_vma mips_n32_exec_plt0_entry[] =
1083 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1084 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1085 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1086 0x030ec023, /* subu $24, $24, $14 */
1087 0x03e07825, /* or t7, ra, zero */
1088 0x0018c082, /* srl $24, $24, 2 */
1089 0x0320f809, /* jalr $25 */
1090 0x2718fffe /* subu $24, $24, 2 */
1093 /* The format of the first PLT entry in an N32 executable using compact
1094 jumps. Different because gp ($28) is not available; we use t2 ($14)
1095 instead. */
1096 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1098 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1099 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1100 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1101 0x030ec023, /* subu $24, $24, $14 */
1102 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1103 0x0018c082, /* srl $24, $24, 2 */
1104 0x2718fffe, /* subu $24, $24, 2 */
1105 0xf8190000 /* jalrc $25 */
1108 /* The format of the first PLT entry in an N64 executable. Different
1109 from N32 because of the increased size of GOT entries. */
1110 static const bfd_vma mips_n64_exec_plt0_entry[] =
1112 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1113 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1114 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1115 0x030ec023, /* subu $24, $24, $14 */
1116 0x03e07825, /* or t7, ra, zero */
1117 0x0018c0c2, /* srl $24, $24, 3 */
1118 0x0320f809, /* jalr $25 */
1119 0x2718fffe /* subu $24, $24, 2 */
1122 /* The format of the first PLT entry in an N64 executable using compact
1123 jumps. Different from N32 because of the increased size of GOT
1124 entries. */
1125 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1127 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1128 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1129 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1130 0x030ec023, /* subu $24, $24, $14 */
1131 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1132 0x0018c0c2, /* srl $24, $24, 3 */
1133 0x2718fffe, /* subu $24, $24, 2 */
1134 0xf8190000 /* jalrc $25 */
1138 /* The format of the microMIPS first PLT entry in an O32 executable.
1139 We rely on v0 ($2) rather than t8 ($24) to contain the address
1140 of the GOTPLT entry handled, so this stub may only be used when
1141 all the subsequent PLT entries are microMIPS code too.
1143 The trailing NOP is for alignment and correct disassembly only. */
1144 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1146 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1147 0xff23, 0x0000, /* lw $25, 0($3) */
1148 0x0535, /* subu $2, $2, $3 */
1149 0x2525, /* srl $2, $2, 2 */
1150 0x3302, 0xfffe, /* subu $24, $2, 2 */
1151 0x0dff, /* move $15, $31 */
1152 0x45f9, /* jalrs $25 */
1153 0x0f83, /* move $28, $3 */
1154 0x0c00 /* nop */
1157 /* The format of the microMIPS first PLT entry in an O32 executable
1158 in the insn32 mode. */
1159 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1161 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1162 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1163 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1164 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1165 0x001f, 0x7a90, /* or $15, $31, zero */
1166 0x0318, 0x1040, /* srl $24, $24, 2 */
1167 0x03f9, 0x0f3c, /* jalr $25 */
1168 0x3318, 0xfffe /* subu $24, $24, 2 */
1171 /* The format of subsequent standard PLT entries. */
1172 static const bfd_vma mips_exec_plt_entry[] =
1174 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1175 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1176 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1177 0x03200008 /* jr $25 */
1180 static const bfd_vma mipsr6_exec_plt_entry[] =
1182 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1183 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1184 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1185 0x03200009 /* jr $25 */
1188 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1190 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1191 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1192 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1193 0xd8190000 /* jic $25, 0 */
1196 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1197 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198 directly addressable. */
1199 static const bfd_vma mips16_o32_exec_plt_entry[] =
1201 0xb203, /* lw $2, 12($pc) */
1202 0x9a60, /* lw $3, 0($2) */
1203 0x651a, /* move $24, $2 */
1204 0xeb00, /* jr $3 */
1205 0x653b, /* move $25, $3 */
1206 0x6500, /* nop */
1207 0x0000, 0x0000 /* .word (.got.plt entry) */
1210 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1211 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1212 static const bfd_vma micromips_o32_exec_plt_entry[] =
1214 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215 0xff22, 0x0000, /* lw $25, 0($2) */
1216 0x4599, /* jr $25 */
1217 0x0f02 /* move $24, $2 */
1220 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1221 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1223 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1224 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225 0x0019, 0x0f3c, /* jr $25 */
1226 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1229 /* The format of the first PLT entry in a VxWorks executable. */
1230 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1232 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1233 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234 0x8f390008, /* lw t9, 8(t9) */
1235 0x00000000, /* nop */
1236 0x03200008, /* jr t9 */
1237 0x00000000 /* nop */
1240 /* The format of subsequent PLT entries. */
1241 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1243 0x10000000, /* b .PLT_resolver */
1244 0x24180000, /* li t8, <pltindex> */
1245 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1246 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247 0x8f390000, /* lw t9, 0(t9) */
1248 0x00000000, /* nop */
1249 0x03200008, /* jr t9 */
1250 0x00000000 /* nop */
1253 /* The format of the first PLT entry in a VxWorks shared object. */
1254 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1256 0x8f990008, /* lw t9, 8(gp) */
1257 0x00000000, /* nop */
1258 0x03200008, /* jr t9 */
1259 0x00000000, /* nop */
1260 0x00000000, /* nop */
1261 0x00000000 /* nop */
1264 /* The format of subsequent PLT entries. */
1265 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1267 0x10000000, /* b .PLT_resolver */
1268 0x24180000 /* li t8, <pltindex> */
1271 /* microMIPS 32-bit opcode helper installer. */
1273 static void
1274 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1276 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1280 /* microMIPS 32-bit opcode helper retriever. */
1282 static bfd_vma
1283 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1285 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1288 /* Look up an entry in a MIPS ELF linker hash table. */
1290 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1291 ((struct mips_elf_link_hash_entry *) \
1292 elf_link_hash_lookup (&(table)->root, (string), (create), \
1293 (copy), (follow)))
1295 /* Traverse a MIPS ELF linker hash table. */
1297 #define mips_elf_link_hash_traverse(table, func, info) \
1298 (elf_link_hash_traverse \
1299 (&(table)->root, \
1300 (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
1301 (info)))
1303 /* Find the base offsets for thread-local storage in this object,
1304 for GD/LD and IE/LE respectively. */
1306 #define TP_OFFSET 0x7000
1307 #define DTP_OFFSET 0x8000
1309 static bfd_vma
1310 dtprel_base (struct bfd_link_info *info)
1312 /* If tls_sec is NULL, we should have signalled an error already. */
1313 if (elf_hash_table (info)->tls_sec == NULL)
1314 return 0;
1315 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1318 static bfd_vma
1319 tprel_base (struct bfd_link_info *info)
1321 /* If tls_sec is NULL, we should have signalled an error already. */
1322 if (elf_hash_table (info)->tls_sec == NULL)
1323 return 0;
1324 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1327 /* Create an entry in a MIPS ELF linker hash table. */
1329 static struct bfd_hash_entry *
1330 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331 struct bfd_hash_table *table, const char *string)
1333 struct mips_elf_link_hash_entry *ret =
1334 (struct mips_elf_link_hash_entry *) entry;
1336 /* Allocate the structure if it has not already been allocated by a
1337 subclass. */
1338 if (ret == NULL)
1339 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340 if (ret == NULL)
1341 return (struct bfd_hash_entry *) ret;
1343 /* Call the allocation method of the superclass. */
1344 ret = ((struct mips_elf_link_hash_entry *)
1345 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346 table, string));
1347 if (ret != NULL)
1349 /* Set local fields. */
1350 memset (&ret->esym, 0, sizeof (EXTR));
1351 /* We use -2 as a marker to indicate that the information has
1352 not been set. -1 means there is no associated ifd. */
1353 ret->esym.ifd = -2;
1354 ret->la25_stub = 0;
1355 ret->possibly_dynamic_relocs = 0;
1356 ret->fn_stub = NULL;
1357 ret->call_stub = NULL;
1358 ret->call_fp_stub = NULL;
1359 ret->mipsxhash_loc = 0;
1360 ret->global_got_area = GGA_NONE;
1361 ret->got_only_for_calls = true;
1362 ret->readonly_reloc = false;
1363 ret->has_static_relocs = false;
1364 ret->no_fn_stub = false;
1365 ret->need_fn_stub = false;
1366 ret->has_nonpic_branches = false;
1367 ret->needs_lazy_stub = false;
1368 ret->use_plt_entry = false;
1371 return (struct bfd_hash_entry *) ret;
1374 /* Allocate MIPS ELF private object data. */
1376 bool
1377 _bfd_mips_elf_mkobject (bfd *abfd)
1379 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380 MIPS_ELF_DATA);
1383 /* MIPS ELF uses a special find_nearest_line routine in order the
1384 handle the ECOFF debugging information. */
1386 struct mips_elf_find_line
1388 struct ecoff_debug_info d;
1389 struct ecoff_find_line i;
1392 bool
1393 _bfd_mips_elf_free_cached_info (bfd *abfd)
1395 struct mips_elf_obj_tdata *tdata;
1397 if ((bfd_get_format (abfd) == bfd_object
1398 || bfd_get_format (abfd) == bfd_core)
1399 && (tdata = mips_elf_tdata (abfd)) != NULL)
1401 BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402 while (tdata->mips_hi16_list != NULL)
1404 struct mips_hi16 *hi = tdata->mips_hi16_list;
1405 tdata->mips_hi16_list = hi->next;
1406 free (hi);
1408 if (tdata->find_line_info != NULL)
1409 _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1411 return _bfd_elf_free_cached_info (abfd);
1414 bool
1415 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1417 if (!sec->used_by_bfd)
1419 struct _mips_elf_section_data *sdata;
1420 size_t amt = sizeof (*sdata);
1422 sdata = bfd_zalloc (abfd, amt);
1423 if (sdata == NULL)
1424 return false;
1425 sec->used_by_bfd = sdata;
1428 return _bfd_elf_new_section_hook (abfd, sec);
1431 /* Read ECOFF debugging information from a .mdebug section into a
1432 ecoff_debug_info structure. */
1434 bool
1435 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436 struct ecoff_debug_info *debug)
1438 HDRR *symhdr;
1439 const struct ecoff_debug_swap *swap;
1440 char *ext_hdr;
1442 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443 memset (debug, 0, sizeof (*debug));
1445 ext_hdr = bfd_malloc (swap->external_hdr_size);
1446 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447 goto error_return;
1449 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450 swap->external_hdr_size))
1451 goto error_return;
1453 symhdr = &debug->symbolic_header;
1454 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455 free (ext_hdr);
1456 ext_hdr = NULL;
1458 /* The symbolic header contains absolute file offsets and sizes to
1459 read. */
1460 #define READ(ptr, offset, count, size) \
1461 do \
1463 size_t amt; \
1464 debug->ptr = NULL; \
1465 if (symhdr->count == 0) \
1466 break; \
1467 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1469 bfd_set_error (bfd_error_file_too_big); \
1470 goto error_return; \
1472 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
1473 goto error_return; \
1474 debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt); \
1475 if (debug->ptr == NULL) \
1476 goto error_return; \
1477 ((char *) debug->ptr)[amt] = 0; \
1478 } while (0)
1480 READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486 READ (ss, cbSsOffset, issMax, sizeof (char));
1487 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491 #undef READ
1493 return true;
1495 error_return:
1496 free (ext_hdr);
1497 _bfd_ecoff_free_ecoff_debug_info (debug);
1498 return false;
1501 /* Swap RPDR (runtime procedure table entry) for output. */
1503 static void
1504 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1506 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1513 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1516 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1519 /* Create a runtime procedure table from the .mdebug section. */
1521 static bool
1522 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523 struct bfd_link_info *info, asection *s,
1524 struct ecoff_debug_info *debug)
1526 const struct ecoff_debug_swap *swap;
1527 HDRR *hdr = &debug->symbolic_header;
1528 RPDR *rpdr, *rp;
1529 struct rpdr_ext *erp;
1530 void *rtproc;
1531 struct pdr_ext *epdr;
1532 struct sym_ext *esym;
1533 char *ss, **sv;
1534 char *str;
1535 bfd_size_type size;
1536 bfd_size_type count;
1537 unsigned long sindex;
1538 unsigned long i;
1539 PDR pdr;
1540 SYMR sym;
1541 const char *no_name_func = _("static procedure (no name)");
1543 epdr = NULL;
1544 rpdr = NULL;
1545 esym = NULL;
1546 ss = NULL;
1547 sv = NULL;
1549 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1551 sindex = strlen (no_name_func) + 1;
1552 count = hdr->ipdMax;
1553 if (count > 0)
1555 size = swap->external_pdr_size;
1557 epdr = bfd_malloc (size * count);
1558 if (epdr == NULL)
1559 goto error_return;
1561 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562 goto error_return;
1564 size = sizeof (RPDR);
1565 rp = rpdr = bfd_malloc (size * count);
1566 if (rpdr == NULL)
1567 goto error_return;
1569 size = sizeof (char *);
1570 sv = bfd_malloc (size * count);
1571 if (sv == NULL)
1572 goto error_return;
1574 count = hdr->isymMax;
1575 size = swap->external_sym_size;
1576 esym = bfd_malloc (size * count);
1577 if (esym == NULL)
1578 goto error_return;
1580 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581 goto error_return;
1583 count = hdr->issMax;
1584 ss = bfd_malloc (count);
1585 if (ss == NULL)
1586 goto error_return;
1587 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588 goto error_return;
1590 count = hdr->ipdMax;
1591 for (i = 0; i < (unsigned long) count; i++, rp++)
1593 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595 rp->adr = sym.value;
1596 rp->regmask = pdr.regmask;
1597 rp->regoffset = pdr.regoffset;
1598 rp->fregmask = pdr.fregmask;
1599 rp->fregoffset = pdr.fregoffset;
1600 rp->frameoffset = pdr.frameoffset;
1601 rp->framereg = pdr.framereg;
1602 rp->pcreg = pdr.pcreg;
1603 rp->irpss = sindex;
1604 sv[i] = ss + sym.iss;
1605 sindex += strlen (sv[i]) + 1;
1609 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610 size = BFD_ALIGN (size, 16);
1611 rtproc = bfd_alloc (abfd, size);
1612 if (rtproc == NULL)
1614 mips_elf_hash_table (info)->procedure_count = 0;
1615 goto error_return;
1618 mips_elf_hash_table (info)->procedure_count = count + 2;
1620 erp = rtproc;
1621 memset (erp, 0, sizeof (struct rpdr_ext));
1622 erp++;
1623 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624 strcpy (str, no_name_func);
1625 str += strlen (no_name_func) + 1;
1626 for (i = 0; i < count; i++)
1628 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629 strcpy (str, sv[i]);
1630 str += strlen (sv[i]) + 1;
1632 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1634 /* Set the size and contents of .rtproc section. */
1635 s->size = size;
1636 s->contents = rtproc;
1638 /* Skip this section later on (I don't think this currently
1639 matters, but someday it might). */
1640 s->map_head.link_order = NULL;
1642 free (epdr);
1643 free (rpdr);
1644 free (esym);
1645 free (ss);
1646 free (sv);
1647 return true;
1649 error_return:
1650 free (epdr);
1651 free (rpdr);
1652 free (esym);
1653 free (ss);
1654 free (sv);
1655 return false;
1658 /* We're going to create a stub for H. Create a symbol for the stub's
1659 value and size, to help make the disassembly easier to read. */
1661 static bool
1662 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663 struct mips_elf_link_hash_entry *h,
1664 const char *prefix, asection *s, bfd_vma value,
1665 bfd_vma size)
1667 bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668 struct bfd_link_hash_entry *bh;
1669 struct elf_link_hash_entry *elfh;
1670 char *name;
1671 bool res;
1673 if (micromips_p)
1674 value |= 1;
1676 /* Create a new symbol. */
1677 name = concat (prefix, h->root.root.root.string, NULL);
1678 bh = NULL;
1679 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680 BSF_LOCAL, s, value, NULL,
1681 true, false, &bh);
1682 free (name);
1683 if (! res)
1684 return false;
1686 /* Make it a local function. */
1687 elfh = (struct elf_link_hash_entry *) bh;
1688 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689 elfh->size = size;
1690 elfh->forced_local = 1;
1691 if (micromips_p)
1692 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693 return true;
1696 /* We're about to redefine H. Create a symbol to represent H's
1697 current value and size, to help make the disassembly easier
1698 to read. */
1700 static bool
1701 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702 struct mips_elf_link_hash_entry *h,
1703 const char *prefix)
1705 struct bfd_link_hash_entry *bh;
1706 struct elf_link_hash_entry *elfh;
1707 char *name;
1708 asection *s;
1709 bfd_vma value;
1710 bool res;
1712 /* Read the symbol's value. */
1713 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714 || h->root.root.type == bfd_link_hash_defweak);
1715 s = h->root.root.u.def.section;
1716 value = h->root.root.u.def.value;
1718 /* Create a new symbol. */
1719 name = concat (prefix, h->root.root.root.string, NULL);
1720 bh = NULL;
1721 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722 BSF_LOCAL, s, value, NULL,
1723 true, false, &bh);
1724 free (name);
1725 if (! res)
1726 return false;
1728 /* Make it local and copy the other attributes from H. */
1729 elfh = (struct elf_link_hash_entry *) bh;
1730 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731 elfh->other = h->root.other;
1732 elfh->size = h->root.size;
1733 elfh->forced_local = 1;
1734 return true;
1737 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738 function rather than to a hard-float stub. */
1740 static bool
1741 section_allows_mips16_refs_p (asection *section)
1743 const char *name;
1745 name = bfd_section_name (section);
1746 return (FN_STUB_P (name)
1747 || CALL_STUB_P (name)
1748 || CALL_FP_STUB_P (name)
1749 || strcmp (name, ".pdr") == 0);
1752 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753 stub section of some kind. Return the R_SYMNDX of the target
1754 function, or 0 if we can't decide which function that is. */
1756 static unsigned long
1757 mips16_stub_symndx (const struct elf_backend_data *bed,
1758 asection *sec ATTRIBUTE_UNUSED,
1759 const Elf_Internal_Rela *relocs,
1760 const Elf_Internal_Rela *relend)
1762 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763 const Elf_Internal_Rela *rel;
1765 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766 one in a compound relocation. */
1767 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769 return ELF_R_SYM (sec->owner, rel->r_info);
1771 /* Otherwise trust the first relocation, whatever its kind. This is
1772 the traditional behavior. */
1773 if (relocs < relend)
1774 return ELF_R_SYM (sec->owner, relocs->r_info);
1776 return 0;
1779 /* Check the mips16 stubs for a particular symbol, and see if we can
1780 discard them. */
1782 static void
1783 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784 struct mips_elf_link_hash_entry *h)
1786 /* Dynamic symbols must use the standard call interface, in case other
1787 objects try to call them. */
1788 if (h->fn_stub != NULL
1789 && h->root.dynindx != -1)
1791 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792 h->need_fn_stub = true;
1795 if (h->fn_stub != NULL
1796 && ! h->need_fn_stub)
1798 /* We don't need the fn_stub; the only references to this symbol
1799 are 16 bit calls. Clobber the size to 0 to prevent it from
1800 being included in the link. */
1801 h->fn_stub->size = 0;
1802 h->fn_stub->flags &= ~SEC_RELOC;
1803 h->fn_stub->reloc_count = 0;
1804 h->fn_stub->flags |= SEC_EXCLUDE;
1805 h->fn_stub->output_section = bfd_abs_section_ptr;
1808 if (h->call_stub != NULL
1809 && ELF_ST_IS_MIPS16 (h->root.other))
1811 /* We don't need the call_stub; this is a 16 bit function, so
1812 calls from other 16 bit functions are OK. Clobber the size
1813 to 0 to prevent it from being included in the link. */
1814 h->call_stub->size = 0;
1815 h->call_stub->flags &= ~SEC_RELOC;
1816 h->call_stub->reloc_count = 0;
1817 h->call_stub->flags |= SEC_EXCLUDE;
1818 h->call_stub->output_section = bfd_abs_section_ptr;
1821 if (h->call_fp_stub != NULL
1822 && ELF_ST_IS_MIPS16 (h->root.other))
1824 /* We don't need the call_stub; this is a 16 bit function, so
1825 calls from other 16 bit functions are OK. Clobber the size
1826 to 0 to prevent it from being included in the link. */
1827 h->call_fp_stub->size = 0;
1828 h->call_fp_stub->flags &= ~SEC_RELOC;
1829 h->call_fp_stub->reloc_count = 0;
1830 h->call_fp_stub->flags |= SEC_EXCLUDE;
1831 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1835 /* Hashtable callbacks for mips_elf_la25_stubs. */
1837 static hashval_t
1838 mips_elf_la25_stub_hash (const void *entry_)
1840 const struct mips_elf_la25_stub *entry;
1842 entry = (struct mips_elf_la25_stub *) entry_;
1843 return entry->h->root.root.u.def.section->id
1844 + entry->h->root.root.u.def.value;
1847 static int
1848 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1850 const struct mips_elf_la25_stub *entry1, *entry2;
1852 entry1 = (struct mips_elf_la25_stub *) entry1_;
1853 entry2 = (struct mips_elf_la25_stub *) entry2_;
1854 return ((entry1->h->root.root.u.def.section
1855 == entry2->h->root.root.u.def.section)
1856 && (entry1->h->root.root.u.def.value
1857 == entry2->h->root.root.u.def.value));
1860 /* Called by the linker to set up the la25 stub-creation code. FN is
1861 the linker's implementation of add_stub_function. Return true on
1862 success. */
1864 bool
1865 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866 asection *(*fn) (const char *, asection *,
1867 asection *))
1869 struct mips_elf_link_hash_table *htab;
1871 htab = mips_elf_hash_table (info);
1872 if (htab == NULL)
1873 return false;
1875 htab->add_stub_section = fn;
1876 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877 mips_elf_la25_stub_eq, NULL);
1878 if (htab->la25_stubs == NULL)
1879 return false;
1881 return true;
1884 /* Return true if H is a locally-defined PIC function, in the sense
1885 that it or its fn_stub might need $25 to be valid on entry.
1886 Note that MIPS16 functions set up $gp using PC-relative instructions,
1887 so they themselves never need $25 to be valid. Only non-MIPS16
1888 entry points are of interest here. */
1890 static bool
1891 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1893 return ((h->root.root.type == bfd_link_hash_defined
1894 || h->root.root.type == bfd_link_hash_defweak)
1895 && h->root.def_regular
1896 && !bfd_is_abs_section (h->root.root.u.def.section)
1897 && !bfd_is_und_section (h->root.root.u.def.section)
1898 && (!ELF_ST_IS_MIPS16 (h->root.other)
1899 || (h->fn_stub && h->need_fn_stub))
1900 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1904 /* Set *SEC to the input section that contains the target of STUB.
1905 Return the offset of the target from the start of that section. */
1907 static bfd_vma
1908 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909 asection **sec)
1911 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1913 BFD_ASSERT (stub->h->need_fn_stub);
1914 *sec = stub->h->fn_stub;
1915 return 0;
1917 else
1919 *sec = stub->h->root.root.u.def.section;
1920 return stub->h->root.root.u.def.value;
1924 /* STUB describes an la25 stub that we have decided to implement
1925 by inserting an LUI/ADDIU pair before the target function.
1926 Create the section and redirect the function symbol to it. */
1928 static bool
1929 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930 struct bfd_link_info *info)
1932 struct mips_elf_link_hash_table *htab;
1933 char *name;
1934 asection *s, *input_section;
1935 unsigned int align;
1937 htab = mips_elf_hash_table (info);
1938 if (htab == NULL)
1939 return false;
1941 /* Create a unique name for the new section. */
1942 name = bfd_malloc (11 + sizeof (".text.stub."));
1943 if (name == NULL)
1944 return false;
1945 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1947 /* Create the section. */
1948 mips_elf_get_la25_target (stub, &input_section);
1949 s = htab->add_stub_section (name, input_section,
1950 input_section->output_section);
1951 if (s == NULL)
1952 return false;
1954 /* Make sure that any padding goes before the stub. */
1955 align = input_section->alignment_power;
1956 if (!bfd_set_section_alignment (s, align))
1957 return false;
1958 if (align > 3)
1959 s->size = (1 << align) - 8;
1961 /* Create a symbol for the stub. */
1962 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963 stub->stub_section = s;
1964 stub->offset = s->size;
1966 /* Allocate room for it. */
1967 s->size += 8;
1968 return true;
1971 /* STUB describes an la25 stub that we have decided to implement
1972 with a separate trampoline. Allocate room for it and redirect
1973 the function symbol to it. */
1975 static bool
1976 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977 struct bfd_link_info *info)
1979 struct mips_elf_link_hash_table *htab;
1980 asection *s;
1982 htab = mips_elf_hash_table (info);
1983 if (htab == NULL)
1984 return false;
1986 /* Create a trampoline section, if we haven't already. */
1987 s = htab->strampoline;
1988 if (s == NULL)
1990 asection *input_section = stub->h->root.root.u.def.section;
1991 s = htab->add_stub_section (".text", NULL,
1992 input_section->output_section);
1993 if (s == NULL || !bfd_set_section_alignment (s, 4))
1994 return false;
1995 htab->strampoline = s;
1998 /* Create a symbol for the stub. */
1999 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000 stub->stub_section = s;
2001 stub->offset = s->size;
2003 /* Allocate room for it. */
2004 s->size += 16;
2005 return true;
2008 /* H describes a symbol that needs an la25 stub. Make sure that an
2009 appropriate stub exists and point H at it. */
2011 static bool
2012 mips_elf_add_la25_stub (struct bfd_link_info *info,
2013 struct mips_elf_link_hash_entry *h)
2015 struct mips_elf_link_hash_table *htab;
2016 struct mips_elf_la25_stub search, *stub;
2017 bool use_trampoline_p;
2018 asection *s;
2019 bfd_vma value;
2020 void **slot;
2022 /* Describe the stub we want. */
2023 search.stub_section = NULL;
2024 search.offset = 0;
2025 search.h = h;
2027 /* See if we've already created an equivalent stub. */
2028 htab = mips_elf_hash_table (info);
2029 if (htab == NULL)
2030 return false;
2032 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033 if (slot == NULL)
2034 return false;
2036 stub = (struct mips_elf_la25_stub *) *slot;
2037 if (stub != NULL)
2039 /* We can reuse the existing stub. */
2040 h->la25_stub = stub;
2041 return true;
2044 /* Create a permanent copy of ENTRY and add it to the hash table. */
2045 stub = bfd_malloc (sizeof (search));
2046 if (stub == NULL)
2047 return false;
2048 *stub = search;
2049 *slot = stub;
2051 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052 of the section and if we would need no more than 2 nops. */
2053 value = mips_elf_get_la25_target (stub, &s);
2054 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055 value &= ~1;
2056 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2058 h->la25_stub = stub;
2059 return (use_trampoline_p
2060 ? mips_elf_add_la25_trampoline (stub, info)
2061 : mips_elf_add_la25_intro (stub, info));
2064 /* A mips_elf_link_hash_traverse callback that is called before sizing
2065 sections. DATA points to a mips_htab_traverse_info structure. */
2067 static bool
2068 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2070 struct mips_htab_traverse_info *hti;
2072 hti = (struct mips_htab_traverse_info *) data;
2073 if (!bfd_link_relocatable (hti->info))
2074 mips_elf_check_mips16_stubs (hti->info, h);
2076 if (mips_elf_local_pic_function_p (h))
2078 /* PR 12845: If H is in a section that has been garbage
2079 collected it will have its output section set to *ABS*. */
2080 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081 return true;
2083 /* H is a function that might need $25 to be valid on entry.
2084 If we're creating a non-PIC relocatable object, mark H as
2085 being PIC. If we're creating a non-relocatable object with
2086 non-PIC branches and jumps to H, make sure that H has an la25
2087 stub. */
2088 if (bfd_link_relocatable (hti->info))
2090 if (!PIC_OBJECT_P (hti->output_bfd))
2091 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2093 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2095 hti->error = true;
2096 return false;
2099 return true;
2102 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103 Most mips16 instructions are 16 bits, but these instructions
2104 are 32 bits.
2106 The format of these instructions is:
2108 +--------------+--------------------------------+
2109 | JALX | X| Imm 20:16 | Imm 25:21 |
2110 +--------------+--------------------------------+
2111 | Immediate 15:0 |
2112 +-----------------------------------------------+
2114 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2115 Note that the immediate value in the first word is swapped.
2117 When producing a relocatable object file, R_MIPS16_26 is
2118 handled mostly like R_MIPS_26. In particular, the addend is
2119 stored as a straight 26-bit value in a 32-bit instruction.
2120 (gas makes life simpler for itself by never adjusting a
2121 R_MIPS16_26 reloc to be against a section, so the addend is
2122 always zero). However, the 32 bit instruction is stored as 2
2123 16-bit values, rather than a single 32-bit value. In a
2124 big-endian file, the result is the same; in a little-endian
2125 file, the two 16-bit halves of the 32 bit value are swapped.
2126 This is so that a disassembler can recognize the jal
2127 instruction.
2129 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130 instruction stored as two 16-bit values. The addend A is the
2131 contents of the targ26 field. The calculation is the same as
2132 R_MIPS_26. When storing the calculated value, reorder the
2133 immediate value as shown above, and don't forget to store the
2134 value as two 16-bit values.
2136 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137 defined as
2139 big-endian:
2140 +--------+----------------------+
2141 | | |
2142 | | targ26-16 |
2143 |31 26|25 0|
2144 +--------+----------------------+
2146 little-endian:
2147 +----------+------+-------------+
2148 | | | |
2149 | sub1 | | sub2 |
2150 |0 9|10 15|16 31|
2151 +----------+--------------------+
2152 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153 ((sub1 << 16) | sub2)).
2155 When producing a relocatable object file, the calculation is
2156 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157 When producing a fully linked file, the calculation is
2158 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2161 The table below lists the other MIPS16 instruction relocations.
2162 Each one is calculated in the same way as the non-MIPS16 relocation
2163 given on the right, but using the extended MIPS16 layout of 16-bit
2164 immediate fields:
2166 R_MIPS16_GPREL R_MIPS_GPREL16
2167 R_MIPS16_GOT16 R_MIPS_GOT16
2168 R_MIPS16_CALL16 R_MIPS_CALL16
2169 R_MIPS16_HI16 R_MIPS_HI16
2170 R_MIPS16_LO16 R_MIPS_LO16
2172 A typical instruction will have a format like this:
2174 +--------------+--------------------------------+
2175 | EXTEND | Imm 10:5 | Imm 15:11 |
2176 +--------------+--------------------------------+
2177 | Major | rx | ry | Imm 4:0 |
2178 +--------------+--------------------------------+
2180 EXTEND is the five bit value 11110. Major is the instruction
2181 opcode.
2183 All we need to do here is shuffle the bits appropriately.
2184 As above, the two 16-bit halves must be swapped on a
2185 little-endian system.
2187 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188 relocatable field is shifted by 1 rather than 2 and the same bit
2189 shuffling is done as with the relocations above. */
2191 static inline bool
2192 mips16_reloc_p (int r_type)
2194 switch (r_type)
2196 case R_MIPS16_26:
2197 case R_MIPS16_GPREL:
2198 case R_MIPS16_GOT16:
2199 case R_MIPS16_CALL16:
2200 case R_MIPS16_HI16:
2201 case R_MIPS16_LO16:
2202 case R_MIPS16_TLS_GD:
2203 case R_MIPS16_TLS_LDM:
2204 case R_MIPS16_TLS_DTPREL_HI16:
2205 case R_MIPS16_TLS_DTPREL_LO16:
2206 case R_MIPS16_TLS_GOTTPREL:
2207 case R_MIPS16_TLS_TPREL_HI16:
2208 case R_MIPS16_TLS_TPREL_LO16:
2209 case R_MIPS16_PC16_S1:
2210 return true;
2212 default:
2213 return false;
2217 /* Check if a microMIPS reloc. */
2219 static inline bool
2220 micromips_reloc_p (unsigned int r_type)
2222 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2225 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1,
2227 R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
2228 16-bit instructions. */
2230 static inline bool
2231 micromips_reloc_shuffle_p (unsigned int r_type)
2233 return (micromips_reloc_p (r_type)
2234 && r_type != R_MICROMIPS_PC7_S1
2235 && r_type != R_MICROMIPS_PC10_S1
2236 && r_type != R_MICROMIPS_GPREL7_S2);
2239 static inline bool
2240 got16_reloc_p (int r_type)
2242 return (r_type == R_MIPS_GOT16
2243 || r_type == R_MIPS16_GOT16
2244 || r_type == R_MICROMIPS_GOT16);
2247 static inline bool
2248 call16_reloc_p (int r_type)
2250 return (r_type == R_MIPS_CALL16
2251 || r_type == R_MIPS16_CALL16
2252 || r_type == R_MICROMIPS_CALL16);
2255 static inline bool
2256 got_disp_reloc_p (unsigned int r_type)
2258 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2261 static inline bool
2262 got_page_reloc_p (unsigned int r_type)
2264 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2267 static inline bool
2268 got_lo16_reloc_p (unsigned int r_type)
2270 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2273 static inline bool
2274 call_hi16_reloc_p (unsigned int r_type)
2276 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2279 static inline bool
2280 call_lo16_reloc_p (unsigned int r_type)
2282 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2285 static inline bool
2286 hi16_reloc_p (int r_type)
2288 return (r_type == R_MIPS_HI16
2289 || r_type == R_MIPS16_HI16
2290 || r_type == R_MICROMIPS_HI16
2291 || r_type == R_MIPS_PCHI16);
2294 static inline bool
2295 lo16_reloc_p (int r_type)
2297 return (r_type == R_MIPS_LO16
2298 || r_type == R_MIPS16_LO16
2299 || r_type == R_MICROMIPS_LO16
2300 || r_type == R_MIPS_PCLO16);
2303 static inline bool
2304 mips16_call_reloc_p (int r_type)
2306 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2309 static inline bool
2310 jal_reloc_p (int r_type)
2312 return (r_type == R_MIPS_26
2313 || r_type == R_MIPS16_26
2314 || r_type == R_MICROMIPS_26_S1);
2317 static inline bool
2318 b_reloc_p (int r_type)
2320 return (r_type == R_MIPS_PC26_S2
2321 || r_type == R_MIPS_PC21_S2
2322 || r_type == R_MIPS_PC16
2323 || r_type == R_MIPS_GNU_REL16_S2
2324 || r_type == R_MIPS16_PC16_S1
2325 || r_type == R_MICROMIPS_PC16_S1
2326 || r_type == R_MICROMIPS_PC10_S1
2327 || r_type == R_MICROMIPS_PC7_S1);
2330 static inline bool
2331 aligned_pcrel_reloc_p (int r_type)
2333 return (r_type == R_MIPS_PC18_S3
2334 || r_type == R_MIPS_PC19_S2);
2337 static inline bool
2338 branch_reloc_p (int r_type)
2340 return (r_type == R_MIPS_26
2341 || r_type == R_MIPS_PC26_S2
2342 || r_type == R_MIPS_PC21_S2
2343 || r_type == R_MIPS_PC16
2344 || r_type == R_MIPS_GNU_REL16_S2);
2347 static inline bool
2348 mips16_branch_reloc_p (int r_type)
2350 return (r_type == R_MIPS16_26
2351 || r_type == R_MIPS16_PC16_S1);
2354 static inline bool
2355 micromips_branch_reloc_p (int r_type)
2357 return (r_type == R_MICROMIPS_26_S1
2358 || r_type == R_MICROMIPS_PC16_S1
2359 || r_type == R_MICROMIPS_PC10_S1
2360 || r_type == R_MICROMIPS_PC7_S1);
2363 static inline bool
2364 tls_gd_reloc_p (unsigned int r_type)
2366 return (r_type == R_MIPS_TLS_GD
2367 || r_type == R_MIPS16_TLS_GD
2368 || r_type == R_MICROMIPS_TLS_GD);
2371 static inline bool
2372 tls_ldm_reloc_p (unsigned int r_type)
2374 return (r_type == R_MIPS_TLS_LDM
2375 || r_type == R_MIPS16_TLS_LDM
2376 || r_type == R_MICROMIPS_TLS_LDM);
2379 static inline bool
2380 tls_gottprel_reloc_p (unsigned int r_type)
2382 return (r_type == R_MIPS_TLS_GOTTPREL
2383 || r_type == R_MIPS16_TLS_GOTTPREL
2384 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2387 static inline bool
2388 needs_shuffle (int r_type)
2390 return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2393 void
2394 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2395 bool jal_shuffle, bfd_byte *data)
2397 bfd_vma first, second, val;
2399 if (!needs_shuffle (r_type))
2400 return;
2402 /* Pick up the first and second halfwords of the instruction. */
2403 first = bfd_get_16 (abfd, data);
2404 second = bfd_get_16 (abfd, data + 2);
2405 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2406 val = first << 16 | second;
2407 else if (r_type != R_MIPS16_26)
2408 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2409 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2410 else
2411 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2412 | ((first & 0x1f) << 21) | second);
2413 bfd_put_32 (abfd, val, data);
2416 void
2417 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2418 bool jal_shuffle, bfd_byte *data)
2420 bfd_vma first, second, val;
2422 if (!needs_shuffle (r_type))
2423 return;
2425 val = bfd_get_32 (abfd, data);
2426 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2428 second = val & 0xffff;
2429 first = val >> 16;
2431 else if (r_type != R_MIPS16_26)
2433 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2434 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2436 else
2438 second = val & 0xffff;
2439 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2440 | ((val >> 21) & 0x1f);
2442 bfd_put_16 (abfd, second, data + 2);
2443 bfd_put_16 (abfd, first, data);
2446 /* Perform reloc offset checking.
2447 We can only use bfd_reloc_offset_in_range, which takes into account
2448 the size of the field being relocated, when section contents will
2449 be accessed because mips object files may use relocations that seem
2450 to access beyond section limits.
2451 gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2452 R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2453 section. The R_MIPS_SUB applies to the addend for the next reloc
2454 rather than the section contents.
2456 CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2457 CHECK_INPLACE to only check partial_inplace relocs, and
2458 CHECK_SHUFFLE to only check relocs that shuffle/unshuffle. */
2460 bool
2461 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2462 arelent *reloc_entry, enum reloc_check check)
2464 if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2465 return true;
2466 if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2467 return true;
2468 return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2469 input_section, reloc_entry->address);
2472 bfd_reloc_status_type
2473 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2474 arelent *reloc_entry, asection *input_section,
2475 bool relocatable, void *data, bfd_vma gp)
2477 bfd_vma relocation;
2478 bfd_signed_vma val;
2479 bfd_reloc_status_type status;
2481 if (bfd_is_com_section (symbol->section))
2482 relocation = 0;
2483 else
2484 relocation = symbol->value;
2486 if (symbol->section->output_section != NULL)
2488 relocation += symbol->section->output_section->vma;
2489 relocation += symbol->section->output_offset;
2492 /* Set val to the offset into the section or symbol. */
2493 val = reloc_entry->addend;
2495 _bfd_mips_elf_sign_extend (val, 16);
2497 /* Adjust val for the final section location and GP value. If we
2498 are producing relocatable output, we don't want to do this for
2499 an external symbol. */
2500 if (! relocatable
2501 || (symbol->flags & BSF_SECTION_SYM) != 0)
2502 val += relocation - gp;
2504 if (reloc_entry->howto->partial_inplace)
2506 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2507 reloc_entry->address))
2508 return bfd_reloc_outofrange;
2510 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2511 (bfd_byte *) data
2512 + reloc_entry->address);
2513 if (status != bfd_reloc_ok)
2514 return status;
2516 else
2517 reloc_entry->addend = val;
2519 if (relocatable)
2520 reloc_entry->address += input_section->output_offset;
2522 return bfd_reloc_ok;
2525 /* A howto special_function for REL *HI16 relocations. We can only
2526 calculate the correct value once we've seen the partnering
2527 *LO16 relocation, so just save the information for later.
2529 The ABI requires that the *LO16 immediately follow the *HI16.
2530 However, as a GNU extension, we permit an arbitrary number of
2531 *HI16s to be associated with a single *LO16. This significantly
2532 simplies the relocation handling in gcc. */
2534 bfd_reloc_status_type
2535 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2536 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2537 asection *input_section, bfd *output_bfd,
2538 char **error_message ATTRIBUTE_UNUSED)
2540 struct mips_hi16 *n;
2541 struct mips_elf_obj_tdata *tdata;
2543 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2544 return bfd_reloc_outofrange;
2546 n = bfd_malloc (sizeof *n);
2547 if (n == NULL)
2548 return bfd_reloc_outofrange;
2550 tdata = mips_elf_tdata (abfd);
2551 n->next = tdata->mips_hi16_list;
2552 n->data = data;
2553 n->input_section = input_section;
2554 n->rel = *reloc_entry;
2555 tdata->mips_hi16_list = n;
2557 if (output_bfd != NULL)
2558 reloc_entry->address += input_section->output_offset;
2560 return bfd_reloc_ok;
2563 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2564 like any other 16-bit relocation when applied to global symbols, but is
2565 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2567 bfd_reloc_status_type
2568 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2569 void *data, asection *input_section,
2570 bfd *output_bfd, char **error_message)
2572 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2573 || bfd_is_und_section (bfd_asymbol_section (symbol))
2574 || bfd_is_com_section (bfd_asymbol_section (symbol)))
2575 /* The relocation is against a global symbol. */
2576 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577 input_section, output_bfd,
2578 error_message);
2580 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2581 input_section, output_bfd, error_message);
2584 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2585 is a straightforward 16 bit inplace relocation, but we must deal with
2586 any partnering high-part relocations as well. */
2588 bfd_reloc_status_type
2589 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2590 void *data, asection *input_section,
2591 bfd *output_bfd, char **error_message)
2593 bfd_vma vallo;
2594 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2595 struct mips_elf_obj_tdata *tdata;
2597 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2598 reloc_entry->address))
2599 return bfd_reloc_outofrange;
2601 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2602 location);
2603 /* The high 16 bits of the addend are stored in the high insn, the
2604 low 16 bits in the low insn, but there is a catch: You can't
2605 just concatenate the high and low parts. The high part of the
2606 addend is adjusted for the fact that the low part is sign
2607 extended. For example, an addend of 0x38000 would have 0x0004 in
2608 the high part and 0x8000 (=0xff..f8000) in the low part.
2609 To extract the actual addend, calculate (a)
2610 ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2611 We will be applying (symbol + addend) & 0xffff to the low insn,
2612 and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2613 high insn (the +0x8000 adjusting for when the applied low part is
2614 negative). Substituting (a) into (b) and recognising that
2615 (hi & 0xffff) is already in the high insn gives a high part
2616 addend adjustment of (lo & 0xffff) ^ 0x8000. */
2617 vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2618 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2619 location);
2621 tdata = mips_elf_tdata (abfd);
2622 while (tdata->mips_hi16_list != NULL)
2624 bfd_reloc_status_type ret;
2625 struct mips_hi16 *hi;
2627 hi = tdata->mips_hi16_list;
2629 /* R_MIPS*_GOT16 relocations are something of a special case. We
2630 want to install the addend in the same way as for a R_MIPS*_HI16
2631 relocation (with a rightshift of 16). However, since GOT16
2632 relocations can also be used with global symbols, their howto
2633 has a rightshift of 0. */
2634 if (hi->rel.howto->type == R_MIPS_GOT16)
2635 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2636 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2637 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2638 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2639 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2641 hi->rel.addend += vallo;
2643 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2644 hi->input_section, output_bfd,
2645 error_message);
2646 if (ret != bfd_reloc_ok)
2647 return ret;
2649 tdata->mips_hi16_list = hi->next;
2650 free (hi);
2653 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2654 input_section, output_bfd,
2655 error_message);
2658 /* A generic howto special_function. This calculates and installs the
2659 relocation itself, thus avoiding the oft-discussed problems in
2660 bfd_perform_relocation and bfd_install_relocation. */
2662 bfd_reloc_status_type
2663 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2664 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2665 asection *input_section, bfd *output_bfd,
2666 char **error_message ATTRIBUTE_UNUSED)
2668 bfd_signed_vma val;
2669 bfd_reloc_status_type status;
2670 bool relocatable;
2672 relocatable = (output_bfd != NULL);
2674 if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2675 (relocatable
2676 ? check_inplace : check_std)))
2677 return bfd_reloc_outofrange;
2679 /* Build up the field adjustment in VAL. */
2680 val = 0;
2681 if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2682 && symbol->section->output_section != NULL)
2684 /* Either we're calculating the final field value or we have a
2685 relocation against a section symbol. Add in the section's
2686 offset or address. */
2687 val += symbol->section->output_section->vma;
2688 val += symbol->section->output_offset;
2691 if (!relocatable)
2693 /* We're calculating the final field value. Add in the symbol's value
2694 and, if pc-relative, subtract the address of the field itself. */
2695 val += symbol->value;
2696 if (reloc_entry->howto->pc_relative)
2698 val -= input_section->output_section->vma;
2699 val -= input_section->output_offset;
2700 val -= reloc_entry->address;
2704 /* VAL is now the final adjustment. If we're keeping this relocation
2705 in the output file, and if the relocation uses a separate addend,
2706 we just need to add VAL to that addend. Otherwise we need to add
2707 VAL to the relocation field itself. */
2708 if (relocatable && !reloc_entry->howto->partial_inplace)
2709 reloc_entry->addend += val;
2710 else
2712 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2714 /* Add in the separate addend, if any. */
2715 val += reloc_entry->addend;
2717 /* Add VAL to the relocation field. */
2718 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2719 location);
2720 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2721 location);
2722 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2723 location);
2725 if (status != bfd_reloc_ok)
2726 return status;
2729 if (relocatable)
2730 reloc_entry->address += input_section->output_offset;
2732 return bfd_reloc_ok;
2735 /* Swap an entry in a .gptab section. Note that these routines rely
2736 on the equivalence of the two elements of the union. */
2738 static void
2739 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2740 Elf32_gptab *in)
2742 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2743 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2746 static void
2747 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2748 Elf32_External_gptab *ex)
2750 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2751 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2754 static void
2755 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2756 Elf32_External_compact_rel *ex)
2758 H_PUT_32 (abfd, in->id1, ex->id1);
2759 H_PUT_32 (abfd, in->num, ex->num);
2760 H_PUT_32 (abfd, in->id2, ex->id2);
2761 H_PUT_32 (abfd, in->offset, ex->offset);
2762 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2763 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2766 static void
2767 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2768 Elf32_External_crinfo *ex)
2770 unsigned long l;
2772 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2773 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2774 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2775 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2776 H_PUT_32 (abfd, l, ex->info);
2777 H_PUT_32 (abfd, in->konst, ex->konst);
2778 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2781 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2782 routines swap this structure in and out. They are used outside of
2783 BFD, so they are globally visible. */
2785 void
2786 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2787 Elf32_RegInfo *in)
2789 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2790 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2791 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2792 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2793 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2794 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2797 void
2798 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2799 Elf32_External_RegInfo *ex)
2801 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2802 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2803 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2804 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2805 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2806 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2809 /* In the 64 bit ABI, the .MIPS.options section holds register
2810 information in an Elf64_Reginfo structure. These routines swap
2811 them in and out. They are globally visible because they are used
2812 outside of BFD. These routines are here so that gas can call them
2813 without worrying about whether the 64 bit ABI has been included. */
2815 void
2816 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2817 Elf64_Internal_RegInfo *in)
2819 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2820 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2821 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2822 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2823 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2824 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2825 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2828 void
2829 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2830 Elf64_External_RegInfo *ex)
2832 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2833 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2834 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2835 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2836 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2837 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2838 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2841 /* Swap in an options header. */
2843 void
2844 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2845 Elf_Internal_Options *in)
2847 in->kind = H_GET_8 (abfd, ex->kind);
2848 in->size = H_GET_8 (abfd, ex->size);
2849 in->section = H_GET_16 (abfd, ex->section);
2850 in->info = H_GET_32 (abfd, ex->info);
2853 /* Swap out an options header. */
2855 void
2856 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2857 Elf_External_Options *ex)
2859 H_PUT_8 (abfd, in->kind, ex->kind);
2860 H_PUT_8 (abfd, in->size, ex->size);
2861 H_PUT_16 (abfd, in->section, ex->section);
2862 H_PUT_32 (abfd, in->info, ex->info);
2865 /* Swap in an abiflags structure. */
2867 void
2868 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2869 const Elf_External_ABIFlags_v0 *ex,
2870 Elf_Internal_ABIFlags_v0 *in)
2872 in->version = H_GET_16 (abfd, ex->version);
2873 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2874 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2875 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2876 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2877 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2878 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2879 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2880 in->ases = H_GET_32 (abfd, ex->ases);
2881 in->flags1 = H_GET_32 (abfd, ex->flags1);
2882 in->flags2 = H_GET_32 (abfd, ex->flags2);
2885 /* Swap out an abiflags structure. */
2887 void
2888 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2889 const Elf_Internal_ABIFlags_v0 *in,
2890 Elf_External_ABIFlags_v0 *ex)
2892 H_PUT_16 (abfd, in->version, ex->version);
2893 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2894 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2895 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2896 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2897 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2898 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2899 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2900 H_PUT_32 (abfd, in->ases, ex->ases);
2901 H_PUT_32 (abfd, in->flags1, ex->flags1);
2902 H_PUT_32 (abfd, in->flags2, ex->flags2);
2905 /* This function is called via qsort() to sort the dynamic relocation
2906 entries by increasing r_symndx value. */
2908 static int
2909 sort_dynamic_relocs (const void *arg1, const void *arg2)
2911 Elf_Internal_Rela int_reloc1;
2912 Elf_Internal_Rela int_reloc2;
2913 int diff;
2915 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2916 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2918 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2919 if (diff != 0)
2920 return diff;
2922 if (int_reloc1.r_offset < int_reloc2.r_offset)
2923 return -1;
2924 if (int_reloc1.r_offset > int_reloc2.r_offset)
2925 return 1;
2926 return 0;
2929 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2931 static int
2932 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2933 const void *arg2 ATTRIBUTE_UNUSED)
2935 #ifdef BFD64
2936 Elf_Internal_Rela int_reloc1[3];
2937 Elf_Internal_Rela int_reloc2[3];
2939 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940 (reldyn_sorting_bfd, arg1, int_reloc1);
2941 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2942 (reldyn_sorting_bfd, arg2, int_reloc2);
2944 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2945 return -1;
2946 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2947 return 1;
2949 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2950 return -1;
2951 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2952 return 1;
2953 return 0;
2954 #else
2955 abort ();
2956 #endif
2960 /* This routine is used to write out ECOFF debugging external symbol
2961 information. It is called via mips_elf_link_hash_traverse. The
2962 ECOFF external symbol information must match the ELF external
2963 symbol information. Unfortunately, at this point we don't know
2964 whether a symbol is required by reloc information, so the two
2965 tables may wind up being different. We must sort out the external
2966 symbol information before we can set the final size of the .mdebug
2967 section, and we must set the size of the .mdebug section before we
2968 can relocate any sections, and we can't know which symbols are
2969 required by relocation until we relocate the sections.
2970 Fortunately, it is relatively unlikely that any symbol will be
2971 stripped but required by a reloc. In particular, it can not happen
2972 when generating a final executable. */
2974 static bool
2975 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2977 struct extsym_info *einfo = data;
2978 bool strip;
2979 asection *sec, *output_section;
2981 if (h->root.indx == -2)
2982 strip = false;
2983 else if ((h->root.def_dynamic
2984 || h->root.ref_dynamic
2985 || h->root.type == bfd_link_hash_new)
2986 && !h->root.def_regular
2987 && !h->root.ref_regular)
2988 strip = true;
2989 else if (einfo->info->strip == strip_all
2990 || (einfo->info->strip == strip_some
2991 && bfd_hash_lookup (einfo->info->keep_hash,
2992 h->root.root.root.string,
2993 false, false) == NULL))
2994 strip = true;
2995 else
2996 strip = false;
2998 if (strip)
2999 return true;
3001 if (h->esym.ifd == -2)
3003 h->esym.jmptbl = 0;
3004 h->esym.cobol_main = 0;
3005 h->esym.weakext = 0;
3006 h->esym.reserved = 0;
3007 h->esym.ifd = ifdNil;
3008 h->esym.asym.value = 0;
3009 h->esym.asym.st = stGlobal;
3011 if (h->root.root.type == bfd_link_hash_undefined
3012 || h->root.root.type == bfd_link_hash_undefweak)
3014 const char *name;
3016 /* Use undefined class. Also, set class and type for some
3017 special symbols. */
3018 name = h->root.root.root.string;
3019 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3020 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3022 h->esym.asym.sc = scData;
3023 h->esym.asym.st = stLabel;
3024 h->esym.asym.value = 0;
3026 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3028 h->esym.asym.sc = scAbs;
3029 h->esym.asym.st = stLabel;
3030 h->esym.asym.value =
3031 mips_elf_hash_table (einfo->info)->procedure_count;
3033 else
3034 h->esym.asym.sc = scUndefined;
3036 else if (h->root.root.type != bfd_link_hash_defined
3037 && h->root.root.type != bfd_link_hash_defweak)
3038 h->esym.asym.sc = scAbs;
3039 else
3041 const char *name;
3043 sec = h->root.root.u.def.section;
3044 output_section = sec->output_section;
3046 /* When making a shared library and symbol h is the one from
3047 the another shared library, OUTPUT_SECTION may be null. */
3048 if (output_section == NULL)
3049 h->esym.asym.sc = scUndefined;
3050 else
3052 name = bfd_section_name (output_section);
3054 if (strcmp (name, ".text") == 0)
3055 h->esym.asym.sc = scText;
3056 else if (strcmp (name, ".data") == 0)
3057 h->esym.asym.sc = scData;
3058 else if (strcmp (name, ".sdata") == 0)
3059 h->esym.asym.sc = scSData;
3060 else if (strcmp (name, ".rodata") == 0
3061 || strcmp (name, ".rdata") == 0)
3062 h->esym.asym.sc = scRData;
3063 else if (strcmp (name, ".bss") == 0)
3064 h->esym.asym.sc = scBss;
3065 else if (strcmp (name, ".sbss") == 0)
3066 h->esym.asym.sc = scSBss;
3067 else if (strcmp (name, ".init") == 0)
3068 h->esym.asym.sc = scInit;
3069 else if (strcmp (name, ".fini") == 0)
3070 h->esym.asym.sc = scFini;
3071 else
3072 h->esym.asym.sc = scAbs;
3076 h->esym.asym.reserved = 0;
3077 h->esym.asym.index = indexNil;
3080 if (h->root.root.type == bfd_link_hash_common)
3081 h->esym.asym.value = h->root.root.u.c.size;
3082 else if (h->root.root.type == bfd_link_hash_defined
3083 || h->root.root.type == bfd_link_hash_defweak)
3085 if (h->esym.asym.sc == scCommon)
3086 h->esym.asym.sc = scBss;
3087 else if (h->esym.asym.sc == scSCommon)
3088 h->esym.asym.sc = scSBss;
3090 sec = h->root.root.u.def.section;
3091 output_section = sec->output_section;
3092 if (output_section != NULL)
3093 h->esym.asym.value = (h->root.root.u.def.value
3094 + sec->output_offset
3095 + output_section->vma);
3096 else
3097 h->esym.asym.value = 0;
3099 else
3101 struct mips_elf_link_hash_entry *hd = h;
3103 while (hd->root.root.type == bfd_link_hash_indirect)
3104 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3106 if (hd->needs_lazy_stub)
3108 BFD_ASSERT (hd->root.plt.plist != NULL);
3109 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3110 /* Set type and value for a symbol with a function stub. */
3111 h->esym.asym.st = stProc;
3112 sec = hd->root.root.u.def.section;
3113 if (sec == NULL)
3114 h->esym.asym.value = 0;
3115 else
3117 output_section = sec->output_section;
3118 if (output_section != NULL)
3119 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3120 + sec->output_offset
3121 + output_section->vma);
3122 else
3123 h->esym.asym.value = 0;
3128 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3129 h->root.root.root.string,
3130 &h->esym))
3132 einfo->failed = true;
3133 return false;
3136 return true;
3139 /* A comparison routine used to sort .gptab entries. */
3141 static int
3142 gptab_compare (const void *p1, const void *p2)
3144 const Elf32_gptab *a1 = p1;
3145 const Elf32_gptab *a2 = p2;
3147 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3150 /* Functions to manage the got entry hash table. */
3152 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3153 hash number. */
3155 static inline hashval_t
3156 mips_elf_hash_bfd_vma (bfd_vma addr)
3158 #ifdef BFD64
3159 return addr + (addr >> 32);
3160 #else
3161 return addr;
3162 #endif
3165 static hashval_t
3166 mips_elf_got_entry_hash (const void *entry_)
3168 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3170 return (entry->symndx
3171 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3172 + (entry->tls_type == GOT_TLS_LDM ? 0
3173 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3174 : entry->symndx >= 0 ? (entry->abfd->id
3175 + mips_elf_hash_bfd_vma (entry->d.addend))
3176 : entry->d.h->root.root.root.hash));
3179 static int
3180 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3182 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3183 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3185 return (e1->symndx == e2->symndx
3186 && e1->tls_type == e2->tls_type
3187 && (e1->tls_type == GOT_TLS_LDM ? true
3188 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3189 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3190 && e1->d.addend == e2->d.addend)
3191 : e2->abfd && e1->d.h == e2->d.h));
3194 static hashval_t
3195 mips_got_page_ref_hash (const void *ref_)
3197 const struct mips_got_page_ref *ref;
3199 ref = (const struct mips_got_page_ref *) ref_;
3200 return ((ref->symndx >= 0
3201 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3202 : ref->u.h->root.root.root.hash)
3203 + mips_elf_hash_bfd_vma (ref->addend));
3206 static int
3207 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3209 const struct mips_got_page_ref *ref1, *ref2;
3211 ref1 = (const struct mips_got_page_ref *) ref1_;
3212 ref2 = (const struct mips_got_page_ref *) ref2_;
3213 return (ref1->symndx == ref2->symndx
3214 && (ref1->symndx < 0
3215 ? ref1->u.h == ref2->u.h
3216 : ref1->u.abfd == ref2->u.abfd)
3217 && ref1->addend == ref2->addend);
3220 static hashval_t
3221 mips_got_page_entry_hash (const void *entry_)
3223 const struct mips_got_page_entry *entry;
3225 entry = (const struct mips_got_page_entry *) entry_;
3226 return entry->sec->id;
3229 static int
3230 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3232 const struct mips_got_page_entry *entry1, *entry2;
3234 entry1 = (const struct mips_got_page_entry *) entry1_;
3235 entry2 = (const struct mips_got_page_entry *) entry2_;
3236 return entry1->sec == entry2->sec;
3239 /* Create and return a new mips_got_info structure. */
3241 static struct mips_got_info *
3242 mips_elf_create_got_info (bfd *abfd)
3244 struct mips_got_info *g;
3246 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3247 if (g == NULL)
3248 return NULL;
3250 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3251 mips_elf_got_entry_eq, NULL);
3252 if (g->got_entries == NULL)
3253 return NULL;
3255 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3256 mips_got_page_ref_eq, NULL);
3257 if (g->got_page_refs == NULL)
3258 return NULL;
3260 return g;
3263 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3264 CREATE_P and if ABFD doesn't already have a GOT. */
3266 static struct mips_got_info *
3267 mips_elf_bfd_got (bfd *abfd, bool create_p)
3269 struct mips_elf_obj_tdata *tdata;
3271 if (!is_mips_elf (abfd))
3272 return NULL;
3274 tdata = mips_elf_tdata (abfd);
3275 if (!tdata->got && create_p)
3276 tdata->got = mips_elf_create_got_info (abfd);
3277 return tdata->got;
3280 /* Record that ABFD should use output GOT G. */
3282 static void
3283 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3285 struct mips_elf_obj_tdata *tdata;
3287 BFD_ASSERT (is_mips_elf (abfd));
3288 tdata = mips_elf_tdata (abfd);
3289 if (tdata->got)
3291 /* The GOT structure itself and the hash table entries are
3292 allocated to a bfd, but the hash tables aren't. */
3293 htab_delete (tdata->got->got_entries);
3294 htab_delete (tdata->got->got_page_refs);
3295 if (tdata->got->got_page_entries)
3296 htab_delete (tdata->got->got_page_entries);
3298 tdata->got = g;
3301 /* Return the dynamic relocation section. If it doesn't exist, try to
3302 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3303 if creation fails. */
3305 static asection *
3306 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3308 const char *dname;
3309 asection *sreloc;
3310 bfd *dynobj;
3312 dname = MIPS_ELF_REL_DYN_NAME (info);
3313 dynobj = elf_hash_table (info)->dynobj;
3314 sreloc = bfd_get_linker_section (dynobj, dname);
3315 if (sreloc == NULL && create_p)
3317 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3318 (SEC_ALLOC
3319 | SEC_LOAD
3320 | SEC_HAS_CONTENTS
3321 | SEC_IN_MEMORY
3322 | SEC_LINKER_CREATED
3323 | SEC_READONLY));
3324 if (sreloc == NULL
3325 || !bfd_set_section_alignment (sreloc,
3326 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3327 return NULL;
3329 return sreloc;
3332 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3334 static int
3335 mips_elf_reloc_tls_type (unsigned int r_type)
3337 if (tls_gd_reloc_p (r_type))
3338 return GOT_TLS_GD;
3340 if (tls_ldm_reloc_p (r_type))
3341 return GOT_TLS_LDM;
3343 if (tls_gottprel_reloc_p (r_type))
3344 return GOT_TLS_IE;
3346 return GOT_TLS_NONE;
3349 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3351 static int
3352 mips_tls_got_entries (unsigned int type)
3354 switch (type)
3356 case GOT_TLS_GD:
3357 case GOT_TLS_LDM:
3358 return 2;
3360 case GOT_TLS_IE:
3361 return 1;
3363 case GOT_TLS_NONE:
3364 return 0;
3366 abort ();
3369 /* Count the number of relocations needed for a TLS GOT entry, with
3370 access types from TLS_TYPE, and symbol H (or a local symbol if H
3371 is NULL). */
3373 static int
3374 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3375 struct elf_link_hash_entry *h)
3377 int indx = 0;
3378 bool need_relocs = false;
3379 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3381 if (h != NULL
3382 && h->dynindx != -1
3383 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3384 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3385 indx = h->dynindx;
3387 if ((bfd_link_dll (info) || indx != 0)
3388 && (h == NULL
3389 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3390 || h->root.type != bfd_link_hash_undefweak))
3391 need_relocs = true;
3393 if (!need_relocs)
3394 return 0;
3396 switch (tls_type)
3398 case GOT_TLS_GD:
3399 return indx != 0 ? 2 : 1;
3401 case GOT_TLS_IE:
3402 return 1;
3404 case GOT_TLS_LDM:
3405 return bfd_link_dll (info) ? 1 : 0;
3407 default:
3408 return 0;
3412 /* Add the number of GOT entries and TLS relocations required by ENTRY
3413 to G. */
3415 static void
3416 mips_elf_count_got_entry (struct bfd_link_info *info,
3417 struct mips_got_info *g,
3418 struct mips_got_entry *entry)
3420 if (entry->tls_type)
3422 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3423 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3424 entry->symndx < 0
3425 ? &entry->d.h->root : NULL);
3427 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3428 g->local_gotno += 1;
3429 else
3430 g->global_gotno += 1;
3433 /* Output a simple dynamic relocation into SRELOC. */
3435 static void
3436 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3437 asection *sreloc,
3438 unsigned long reloc_index,
3439 unsigned long indx,
3440 int r_type,
3441 bfd_vma offset)
3443 Elf_Internal_Rela rel[3];
3445 memset (rel, 0, sizeof (rel));
3447 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3448 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3450 if (ABI_64_P (output_bfd))
3452 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3453 (output_bfd, &rel[0],
3454 (sreloc->contents
3455 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3457 else
3458 bfd_elf32_swap_reloc_out
3459 (output_bfd, &rel[0],
3460 (sreloc->contents
3461 + reloc_index * sizeof (Elf32_External_Rel)));
3464 /* Initialize a set of TLS GOT entries for one symbol. */
3466 static void
3467 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3468 struct mips_got_entry *entry,
3469 struct mips_elf_link_hash_entry *h,
3470 bfd_vma value)
3472 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3473 struct mips_elf_link_hash_table *htab;
3474 int indx;
3475 asection *sreloc, *sgot;
3476 bfd_vma got_offset, got_offset2;
3477 bool need_relocs = false;
3479 htab = mips_elf_hash_table (info);
3480 if (htab == NULL)
3481 return;
3483 sgot = htab->root.sgot;
3485 indx = 0;
3486 if (h != NULL
3487 && h->root.dynindx != -1
3488 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3489 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3490 indx = h->root.dynindx;
3492 if (entry->tls_initialized)
3493 return;
3495 if ((bfd_link_dll (info) || indx != 0)
3496 && (h == NULL
3497 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3498 || h->root.type != bfd_link_hash_undefweak))
3499 need_relocs = true;
3501 /* MINUS_ONE means the symbol is not defined in this object. It may not
3502 be defined at all; assume that the value doesn't matter in that
3503 case. Otherwise complain if we would use the value. */
3504 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3505 || h->root.root.type == bfd_link_hash_undefweak);
3507 /* Emit necessary relocations. */
3508 sreloc = mips_elf_rel_dyn_section (info, false);
3509 got_offset = entry->gotidx;
3511 switch (entry->tls_type)
3513 case GOT_TLS_GD:
3514 /* General Dynamic. */
3515 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3517 if (need_relocs)
3519 mips_elf_output_dynamic_relocation
3520 (abfd, sreloc, sreloc->reloc_count++, indx,
3521 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3522 sgot->output_offset + sgot->output_section->vma + got_offset);
3524 if (indx)
3525 mips_elf_output_dynamic_relocation
3526 (abfd, sreloc, sreloc->reloc_count++, indx,
3527 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3528 sgot->output_offset + sgot->output_section->vma + got_offset2);
3529 else
3530 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3531 sgot->contents + got_offset2);
3533 else
3535 MIPS_ELF_PUT_WORD (abfd, 1,
3536 sgot->contents + got_offset);
3537 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3538 sgot->contents + got_offset2);
3540 break;
3542 case GOT_TLS_IE:
3543 /* Initial Exec model. */
3544 if (need_relocs)
3546 if (indx == 0)
3547 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3548 sgot->contents + got_offset);
3549 else
3550 MIPS_ELF_PUT_WORD (abfd, 0,
3551 sgot->contents + got_offset);
3553 mips_elf_output_dynamic_relocation
3554 (abfd, sreloc, sreloc->reloc_count++, indx,
3555 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3556 sgot->output_offset + sgot->output_section->vma + got_offset);
3558 else
3559 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3560 sgot->contents + got_offset);
3561 break;
3563 case GOT_TLS_LDM:
3564 /* The initial offset is zero, and the LD offsets will include the
3565 bias by DTP_OFFSET. */
3566 MIPS_ELF_PUT_WORD (abfd, 0,
3567 sgot->contents + got_offset
3568 + MIPS_ELF_GOT_SIZE (abfd));
3570 if (!bfd_link_dll (info))
3571 MIPS_ELF_PUT_WORD (abfd, 1,
3572 sgot->contents + got_offset);
3573 else
3574 mips_elf_output_dynamic_relocation
3575 (abfd, sreloc, sreloc->reloc_count++, indx,
3576 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3577 sgot->output_offset + sgot->output_section->vma + got_offset);
3578 break;
3580 default:
3581 abort ();
3584 entry->tls_initialized = true;
3587 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3588 for global symbol H. .got.plt comes before the GOT, so the offset
3589 will be negative. */
3591 static bfd_vma
3592 mips_elf_gotplt_index (struct bfd_link_info *info,
3593 struct elf_link_hash_entry *h)
3595 bfd_vma got_address, got_value;
3596 struct mips_elf_link_hash_table *htab;
3598 htab = mips_elf_hash_table (info);
3599 BFD_ASSERT (htab != NULL);
3601 BFD_ASSERT (h->plt.plist != NULL);
3602 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3604 /* Calculate the address of the associated .got.plt entry. */
3605 got_address = (htab->root.sgotplt->output_section->vma
3606 + htab->root.sgotplt->output_offset
3607 + (h->plt.plist->gotplt_index
3608 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3610 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3611 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3612 + htab->root.hgot->root.u.def.section->output_offset
3613 + htab->root.hgot->root.u.def.value);
3615 return got_address - got_value;
3618 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3619 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3620 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3621 offset can be found. */
3623 static bfd_vma
3624 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3625 bfd_vma value, unsigned long r_symndx,
3626 struct mips_elf_link_hash_entry *h, int r_type)
3628 struct mips_elf_link_hash_table *htab;
3629 struct mips_got_entry *entry;
3631 htab = mips_elf_hash_table (info);
3632 BFD_ASSERT (htab != NULL);
3634 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3635 r_symndx, h, r_type);
3636 if (!entry)
3637 return MINUS_ONE;
3639 if (entry->tls_type)
3640 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3641 return entry->gotidx;
3644 /* Return the GOT index of global symbol H in the primary GOT. */
3646 static bfd_vma
3647 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3648 struct elf_link_hash_entry *h)
3650 struct mips_elf_link_hash_table *htab;
3651 long global_got_dynindx;
3652 struct mips_got_info *g;
3653 bfd_vma got_index;
3655 htab = mips_elf_hash_table (info);
3656 BFD_ASSERT (htab != NULL);
3658 global_got_dynindx = 0;
3659 if (htab->global_gotsym != NULL)
3660 global_got_dynindx = htab->global_gotsym->dynindx;
3662 /* Once we determine the global GOT entry with the lowest dynamic
3663 symbol table index, we must put all dynamic symbols with greater
3664 indices into the primary GOT. That makes it easy to calculate the
3665 GOT offset. */
3666 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3667 g = mips_elf_bfd_got (obfd, false);
3668 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3669 * MIPS_ELF_GOT_SIZE (obfd));
3670 BFD_ASSERT (got_index < htab->root.sgot->size);
3672 return got_index;
3675 /* Return the GOT index for the global symbol indicated by H, which is
3676 referenced by a relocation of type R_TYPE in IBFD. */
3678 static bfd_vma
3679 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3680 struct elf_link_hash_entry *h, int r_type)
3682 struct mips_elf_link_hash_table *htab;
3683 struct mips_got_info *g;
3684 struct mips_got_entry lookup, *entry;
3685 bfd_vma gotidx;
3687 htab = mips_elf_hash_table (info);
3688 BFD_ASSERT (htab != NULL);
3690 g = mips_elf_bfd_got (ibfd, false);
3691 BFD_ASSERT (g);
3693 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3694 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3695 return mips_elf_primary_global_got_index (obfd, info, h);
3697 lookup.abfd = ibfd;
3698 lookup.symndx = -1;
3699 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3700 entry = htab_find (g->got_entries, &lookup);
3701 BFD_ASSERT (entry);
3703 gotidx = entry->gotidx;
3704 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3706 if (lookup.tls_type)
3708 bfd_vma value = MINUS_ONE;
3710 if ((h->root.type == bfd_link_hash_defined
3711 || h->root.type == bfd_link_hash_defweak)
3712 && h->root.u.def.section->output_section)
3713 value = (h->root.u.def.value
3714 + h->root.u.def.section->output_offset
3715 + h->root.u.def.section->output_section->vma);
3717 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3719 return gotidx;
3722 /* Find a GOT page entry that points to within 32KB of VALUE. These
3723 entries are supposed to be placed at small offsets in the GOT, i.e.,
3724 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3725 entry could be created. If OFFSETP is nonnull, use it to return the
3726 offset of the GOT entry from VALUE. */
3728 static bfd_vma
3729 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3730 bfd_vma value, bfd_vma *offsetp)
3732 bfd_vma page, got_index;
3733 struct mips_got_entry *entry;
3735 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3736 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3737 NULL, R_MIPS_GOT_PAGE);
3739 if (!entry)
3740 return MINUS_ONE;
3742 got_index = entry->gotidx;
3744 if (offsetp)
3745 *offsetp = value - entry->d.address;
3747 return got_index;
3750 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3751 EXTERNAL is true if the relocation was originally against a global
3752 symbol that binds locally. */
3754 static bfd_vma
3755 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3756 bfd_vma value, bool external)
3758 struct mips_got_entry *entry;
3760 /* GOT16 relocations against local symbols are followed by a LO16
3761 relocation; those against global symbols are not. Thus if the
3762 symbol was originally local, the GOT16 relocation should load the
3763 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3764 if (! external)
3765 value = mips_elf_high (value) << 16;
3767 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3768 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3769 same in all cases. */
3770 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3771 NULL, R_MIPS_GOT16);
3772 if (entry)
3773 return entry->gotidx;
3774 else
3775 return MINUS_ONE;
3778 /* Returns the offset for the entry at the INDEXth position
3779 in the GOT. */
3781 static bfd_vma
3782 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3783 bfd *input_bfd, bfd_vma got_index)
3785 struct mips_elf_link_hash_table *htab;
3786 asection *sgot;
3787 bfd_vma gp;
3789 htab = mips_elf_hash_table (info);
3790 BFD_ASSERT (htab != NULL);
3792 sgot = htab->root.sgot;
3793 gp = _bfd_get_gp_value (output_bfd)
3794 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3796 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3799 /* Create and return a local GOT entry for VALUE, which was calculated
3800 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3801 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3802 instead. */
3804 static struct mips_got_entry *
3805 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3806 bfd *ibfd, bfd_vma value,
3807 unsigned long r_symndx,
3808 struct mips_elf_link_hash_entry *h,
3809 int r_type)
3811 struct mips_got_entry lookup, *entry;
3812 void **loc;
3813 struct mips_got_info *g;
3814 struct mips_elf_link_hash_table *htab;
3815 bfd_vma gotidx;
3817 htab = mips_elf_hash_table (info);
3818 BFD_ASSERT (htab != NULL);
3820 g = mips_elf_bfd_got (ibfd, false);
3821 if (g == NULL)
3823 g = mips_elf_bfd_got (abfd, false);
3824 BFD_ASSERT (g != NULL);
3827 /* This function shouldn't be called for symbols that live in the global
3828 area of the GOT. */
3829 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3831 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3832 if (lookup.tls_type)
3834 lookup.abfd = ibfd;
3835 if (tls_ldm_reloc_p (r_type))
3837 lookup.symndx = 0;
3838 lookup.d.addend = 0;
3840 else if (h == NULL)
3842 lookup.symndx = r_symndx;
3843 lookup.d.addend = 0;
3845 else
3847 lookup.symndx = -1;
3848 lookup.d.h = h;
3851 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3852 BFD_ASSERT (entry);
3854 gotidx = entry->gotidx;
3855 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3857 return entry;
3860 lookup.abfd = NULL;
3861 lookup.symndx = -1;
3862 lookup.d.address = value;
3863 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3864 if (!loc)
3865 return NULL;
3867 entry = (struct mips_got_entry *) *loc;
3868 if (entry)
3869 return entry;
3871 if (g->assigned_low_gotno > g->assigned_high_gotno)
3873 /* We didn't allocate enough space in the GOT. */
3874 _bfd_error_handler
3875 (_("not enough GOT space for local GOT entries"));
3876 bfd_set_error (bfd_error_bad_value);
3877 return NULL;
3880 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3881 if (!entry)
3882 return NULL;
3884 if (got16_reloc_p (r_type)
3885 || call16_reloc_p (r_type)
3886 || got_page_reloc_p (r_type)
3887 || got_disp_reloc_p (r_type))
3888 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3889 else
3890 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3892 *entry = lookup;
3893 *loc = entry;
3895 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3897 /* These GOT entries need a dynamic relocation on VxWorks. */
3898 if (htab->root.target_os == is_vxworks)
3900 Elf_Internal_Rela outrel;
3901 asection *s;
3902 bfd_byte *rloc;
3903 bfd_vma got_address;
3905 s = mips_elf_rel_dyn_section (info, false);
3906 got_address = (htab->root.sgot->output_section->vma
3907 + htab->root.sgot->output_offset
3908 + entry->gotidx);
3910 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3911 outrel.r_offset = got_address;
3912 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3913 outrel.r_addend = value;
3914 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3917 return entry;
3920 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3921 The number might be exact or a worst-case estimate, depending on how
3922 much information is available to elf_backend_omit_section_dynsym at
3923 the current linking stage. */
3925 static bfd_size_type
3926 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3928 bfd_size_type count;
3930 count = 0;
3931 if (bfd_link_pic (info))
3933 asection *p;
3934 const struct elf_backend_data *bed;
3936 bed = get_elf_backend_data (output_bfd);
3937 for (p = output_bfd->sections; p ; p = p->next)
3938 if ((p->flags & SEC_EXCLUDE) == 0
3939 && (p->flags & SEC_ALLOC) != 0
3940 && elf_hash_table (info)->dynamic_relocs
3941 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3942 ++count;
3944 return count;
3947 /* Sort the dynamic symbol table so that symbols that need GOT entries
3948 appear towards the end. */
3950 static bool
3951 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3953 struct mips_elf_link_hash_table *htab;
3954 struct mips_elf_hash_sort_data hsd;
3955 struct mips_got_info *g;
3957 htab = mips_elf_hash_table (info);
3958 BFD_ASSERT (htab != NULL);
3960 if (htab->root.dynsymcount == 0)
3961 return true;
3963 g = htab->got_info;
3964 if (g == NULL)
3965 return true;
3967 hsd.low = NULL;
3968 hsd.max_unref_got_dynindx
3969 = hsd.min_got_dynindx
3970 = (htab->root.dynsymcount - g->reloc_only_gotno);
3971 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3972 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3973 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3974 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3975 hsd.output_bfd = abfd;
3976 if (htab->root.dynobj != NULL
3977 && htab->root.dynamic_sections_created
3978 && info->emit_gnu_hash)
3980 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3981 BFD_ASSERT (s != NULL);
3982 hsd.mipsxhash = s->contents;
3983 BFD_ASSERT (hsd.mipsxhash != NULL);
3985 else
3986 hsd.mipsxhash = NULL;
3987 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3989 /* There should have been enough room in the symbol table to
3990 accommodate both the GOT and non-GOT symbols. */
3991 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3992 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3993 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3994 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3996 /* Now we know which dynamic symbol has the lowest dynamic symbol
3997 table index in the GOT. */
3998 htab->global_gotsym = hsd.low;
4000 return true;
4003 /* If H needs a GOT entry, assign it the highest available dynamic
4004 index. Otherwise, assign it the lowest available dynamic
4005 index. */
4007 static bool
4008 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4010 struct mips_elf_hash_sort_data *hsd = data;
4012 /* Symbols without dynamic symbol table entries aren't interesting
4013 at all. */
4014 if (h->root.dynindx == -1)
4015 return true;
4017 switch (h->global_got_area)
4019 case GGA_NONE:
4020 if (h->root.forced_local)
4021 h->root.dynindx = hsd->max_local_dynindx++;
4022 else
4023 h->root.dynindx = hsd->max_non_got_dynindx++;
4024 break;
4026 case GGA_NORMAL:
4027 h->root.dynindx = --hsd->min_got_dynindx;
4028 hsd->low = (struct elf_link_hash_entry *) h;
4029 break;
4031 case GGA_RELOC_ONLY:
4032 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4033 hsd->low = (struct elf_link_hash_entry *) h;
4034 h->root.dynindx = hsd->max_unref_got_dynindx++;
4035 break;
4038 /* Populate the .MIPS.xhash translation table entry with
4039 the symbol dynindx. */
4040 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4041 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4042 hsd->mipsxhash + h->mipsxhash_loc);
4044 return true;
4047 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4048 (which is owned by the caller and shouldn't be added to the
4049 hash table directly). */
4051 static bool
4052 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4053 struct mips_got_entry *lookup)
4055 struct mips_elf_link_hash_table *htab;
4056 struct mips_got_entry *entry;
4057 struct mips_got_info *g;
4058 void **loc, **bfd_loc;
4060 /* Make sure there's a slot for this entry in the master GOT. */
4061 htab = mips_elf_hash_table (info);
4062 g = htab->got_info;
4063 loc = htab_find_slot (g->got_entries, lookup, INSERT);
4064 if (!loc)
4065 return false;
4067 /* Populate the entry if it isn't already. */
4068 entry = (struct mips_got_entry *) *loc;
4069 if (!entry)
4071 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4072 if (!entry)
4073 return false;
4075 lookup->tls_initialized = false;
4076 lookup->gotidx = -1;
4077 *entry = *lookup;
4078 *loc = entry;
4081 /* Reuse the same GOT entry for the BFD's GOT. */
4082 g = mips_elf_bfd_got (abfd, true);
4083 if (!g)
4084 return false;
4086 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4087 if (!bfd_loc)
4088 return false;
4090 if (!*bfd_loc)
4091 *bfd_loc = entry;
4092 return true;
4095 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4096 entry for it. FOR_CALL is true if the caller is only interested in
4097 using the GOT entry for calls. */
4099 static bool
4100 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4101 bfd *abfd, struct bfd_link_info *info,
4102 bool for_call, int r_type)
4104 struct mips_elf_link_hash_table *htab;
4105 struct mips_elf_link_hash_entry *hmips;
4106 struct mips_got_entry entry;
4107 unsigned char tls_type;
4109 htab = mips_elf_hash_table (info);
4110 BFD_ASSERT (htab != NULL);
4112 hmips = (struct mips_elf_link_hash_entry *) h;
4113 if (!for_call)
4114 hmips->got_only_for_calls = false;
4116 /* A global symbol in the GOT must also be in the dynamic symbol
4117 table. */
4118 if (h->dynindx == -1)
4120 switch (ELF_ST_VISIBILITY (h->other))
4122 case STV_INTERNAL:
4123 case STV_HIDDEN:
4124 _bfd_mips_elf_hide_symbol (info, h, true);
4125 break;
4127 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4128 return false;
4131 tls_type = mips_elf_reloc_tls_type (r_type);
4132 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4133 hmips->global_got_area = GGA_NORMAL;
4135 entry.abfd = abfd;
4136 entry.symndx = -1;
4137 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4138 entry.tls_type = tls_type;
4139 return mips_elf_record_got_entry (info, abfd, &entry);
4142 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4143 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4145 static bool
4146 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4147 struct bfd_link_info *info, int r_type)
4149 struct mips_elf_link_hash_table *htab;
4150 struct mips_got_info *g;
4151 struct mips_got_entry entry;
4153 htab = mips_elf_hash_table (info);
4154 BFD_ASSERT (htab != NULL);
4156 g = htab->got_info;
4157 BFD_ASSERT (g != NULL);
4159 entry.abfd = abfd;
4160 entry.symndx = symndx;
4161 entry.d.addend = addend;
4162 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4163 return mips_elf_record_got_entry (info, abfd, &entry);
4166 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4167 H is the symbol's hash table entry, or null if SYMNDX is local
4168 to ABFD. */
4170 static bool
4171 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4172 long symndx, struct elf_link_hash_entry *h,
4173 bfd_signed_vma addend)
4175 struct mips_elf_link_hash_table *htab;
4176 struct mips_got_info *g1, *g2;
4177 struct mips_got_page_ref lookup, *entry;
4178 void **loc, **bfd_loc;
4180 htab = mips_elf_hash_table (info);
4181 BFD_ASSERT (htab != NULL);
4183 g1 = htab->got_info;
4184 BFD_ASSERT (g1 != NULL);
4186 if (h)
4188 lookup.symndx = -1;
4189 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4191 else
4193 lookup.symndx = symndx;
4194 lookup.u.abfd = abfd;
4196 lookup.addend = addend;
4197 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4198 if (loc == NULL)
4199 return false;
4201 entry = (struct mips_got_page_ref *) *loc;
4202 if (!entry)
4204 entry = bfd_alloc (abfd, sizeof (*entry));
4205 if (!entry)
4206 return false;
4208 *entry = lookup;
4209 *loc = entry;
4212 /* Add the same entry to the BFD's GOT. */
4213 g2 = mips_elf_bfd_got (abfd, true);
4214 if (!g2)
4215 return false;
4217 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4218 if (!bfd_loc)
4219 return false;
4221 if (!*bfd_loc)
4222 *bfd_loc = entry;
4224 return true;
4227 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4229 static void
4230 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4231 unsigned int n)
4233 asection *s;
4234 struct mips_elf_link_hash_table *htab;
4236 htab = mips_elf_hash_table (info);
4237 BFD_ASSERT (htab != NULL);
4239 s = mips_elf_rel_dyn_section (info, false);
4240 BFD_ASSERT (s != NULL);
4242 if (htab->root.target_os == is_vxworks)
4243 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4244 else
4246 if (s->size == 0)
4248 /* Make room for a null element. */
4249 s->size += MIPS_ELF_REL_SIZE (abfd);
4250 ++s->reloc_count;
4252 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4256 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4257 mips_elf_traverse_got_arg structure. Count the number of GOT
4258 entries and TLS relocs. Set DATA->value to true if we need
4259 to resolve indirect or warning symbols and then recreate the GOT. */
4261 static int
4262 mips_elf_check_recreate_got (void **entryp, void *data)
4264 struct mips_got_entry *entry;
4265 struct mips_elf_traverse_got_arg *arg;
4267 entry = (struct mips_got_entry *) *entryp;
4268 arg = (struct mips_elf_traverse_got_arg *) data;
4269 if (entry->abfd != NULL && entry->symndx == -1)
4271 struct mips_elf_link_hash_entry *h;
4273 h = entry->d.h;
4274 if (h->root.root.type == bfd_link_hash_indirect
4275 || h->root.root.type == bfd_link_hash_warning)
4277 arg->value = true;
4278 return 0;
4281 mips_elf_count_got_entry (arg->info, arg->g, entry);
4282 return 1;
4285 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4286 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4287 converting entries for indirect and warning symbols into entries
4288 for the target symbol. Set DATA->g to null on error. */
4290 static int
4291 mips_elf_recreate_got (void **entryp, void *data)
4293 struct mips_got_entry new_entry, *entry;
4294 struct mips_elf_traverse_got_arg *arg;
4295 void **slot;
4297 entry = (struct mips_got_entry *) *entryp;
4298 arg = (struct mips_elf_traverse_got_arg *) data;
4299 if (entry->abfd != NULL
4300 && entry->symndx == -1
4301 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4302 || entry->d.h->root.root.type == bfd_link_hash_warning))
4304 struct mips_elf_link_hash_entry *h;
4306 new_entry = *entry;
4307 entry = &new_entry;
4308 h = entry->d.h;
4311 BFD_ASSERT (h->global_got_area == GGA_NONE);
4312 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4314 while (h->root.root.type == bfd_link_hash_indirect
4315 || h->root.root.type == bfd_link_hash_warning);
4316 entry->d.h = h;
4318 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4319 if (slot == NULL)
4321 arg->g = NULL;
4322 return 0;
4324 if (*slot == NULL)
4326 if (entry == &new_entry)
4328 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4329 if (!entry)
4331 arg->g = NULL;
4332 return 0;
4334 *entry = new_entry;
4336 *slot = entry;
4337 mips_elf_count_got_entry (arg->info, arg->g, entry);
4339 return 1;
4342 /* Return the maximum number of GOT page entries required for RANGE. */
4344 static bfd_vma
4345 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4347 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4350 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4352 static bool
4353 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4354 asection *sec, bfd_signed_vma addend)
4356 struct mips_got_info *g = arg->g;
4357 struct mips_got_page_entry lookup, *entry;
4358 struct mips_got_page_range **range_ptr, *range;
4359 bfd_vma old_pages, new_pages;
4360 void **loc;
4362 /* Find the mips_got_page_entry hash table entry for this section. */
4363 lookup.sec = sec;
4364 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4365 if (loc == NULL)
4366 return false;
4368 /* Create a mips_got_page_entry if this is the first time we've
4369 seen the section. */
4370 entry = (struct mips_got_page_entry *) *loc;
4371 if (!entry)
4373 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4374 if (!entry)
4375 return false;
4377 entry->sec = sec;
4378 *loc = entry;
4381 /* Skip over ranges whose maximum extent cannot share a page entry
4382 with ADDEND. */
4383 range_ptr = &entry->ranges;
4384 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4385 range_ptr = &(*range_ptr)->next;
4387 /* If we scanned to the end of the list, or found a range whose
4388 minimum extent cannot share a page entry with ADDEND, create
4389 a new singleton range. */
4390 range = *range_ptr;
4391 if (!range || addend < range->min_addend - 0xffff)
4393 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4394 if (!range)
4395 return false;
4397 range->next = *range_ptr;
4398 range->min_addend = addend;
4399 range->max_addend = addend;
4401 *range_ptr = range;
4402 entry->num_pages++;
4403 g->page_gotno++;
4404 return true;
4407 /* Remember how many pages the old range contributed. */
4408 old_pages = mips_elf_pages_for_range (range);
4410 /* Update the ranges. */
4411 if (addend < range->min_addend)
4412 range->min_addend = addend;
4413 else if (addend > range->max_addend)
4415 if (range->next && addend >= range->next->min_addend - 0xffff)
4417 old_pages += mips_elf_pages_for_range (range->next);
4418 range->max_addend = range->next->max_addend;
4419 range->next = range->next->next;
4421 else
4422 range->max_addend = addend;
4425 /* Record any change in the total estimate. */
4426 new_pages = mips_elf_pages_for_range (range);
4427 if (old_pages != new_pages)
4429 entry->num_pages += new_pages - old_pages;
4430 g->page_gotno += new_pages - old_pages;
4433 return true;
4436 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4437 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4438 whether the page reference described by *REFP needs a GOT page entry,
4439 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4441 static int
4442 mips_elf_resolve_got_page_ref (void **refp, void *data)
4444 struct mips_got_page_ref *ref;
4445 struct mips_elf_traverse_got_arg *arg;
4446 struct mips_elf_link_hash_table *htab;
4447 asection *sec;
4448 bfd_vma addend;
4450 ref = (struct mips_got_page_ref *) *refp;
4451 arg = (struct mips_elf_traverse_got_arg *) data;
4452 htab = mips_elf_hash_table (arg->info);
4454 if (ref->symndx < 0)
4456 struct mips_elf_link_hash_entry *h;
4458 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4459 h = ref->u.h;
4460 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4461 return 1;
4463 /* Ignore undefined symbols; we'll issue an error later if
4464 appropriate. */
4465 if (!((h->root.root.type == bfd_link_hash_defined
4466 || h->root.root.type == bfd_link_hash_defweak)
4467 && h->root.root.u.def.section))
4468 return 1;
4470 sec = h->root.root.u.def.section;
4471 addend = h->root.root.u.def.value + ref->addend;
4473 else
4475 Elf_Internal_Sym *isym;
4477 /* Read in the symbol. */
4478 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4479 ref->symndx);
4480 if (isym == NULL)
4482 arg->g = NULL;
4483 return 0;
4486 /* Get the associated input section. */
4487 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4488 if (sec == NULL)
4490 arg->g = NULL;
4491 return 0;
4494 /* If this is a mergable section, work out the section and offset
4495 of the merged data. For section symbols, the addend specifies
4496 of the offset _of_ the first byte in the data, otherwise it
4497 specifies the offset _from_ the first byte. */
4498 if (sec->flags & SEC_MERGE)
4500 void *secinfo;
4502 secinfo = elf_section_data (sec)->sec_info;
4503 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4504 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4505 isym->st_value + ref->addend);
4506 else
4507 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4508 isym->st_value) + ref->addend;
4510 else
4511 addend = isym->st_value + ref->addend;
4513 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4515 arg->g = NULL;
4516 return 0;
4518 return 1;
4521 /* If any entries in G->got_entries are for indirect or warning symbols,
4522 replace them with entries for the target symbol. Convert g->got_page_refs
4523 into got_page_entry structures and estimate the number of page entries
4524 that they require. */
4526 static bool
4527 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4528 struct mips_got_info *g)
4530 struct mips_elf_traverse_got_arg tga;
4531 struct mips_got_info oldg;
4533 oldg = *g;
4535 tga.info = info;
4536 tga.g = g;
4537 tga.value = false;
4538 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4539 if (tga.value)
4541 *g = oldg;
4542 g->got_entries = htab_create (htab_size (oldg.got_entries),
4543 mips_elf_got_entry_hash,
4544 mips_elf_got_entry_eq, NULL);
4545 if (!g->got_entries)
4546 return false;
4548 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4549 if (!tga.g)
4550 return false;
4552 htab_delete (oldg.got_entries);
4555 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4556 mips_got_page_entry_eq, NULL);
4557 if (g->got_page_entries == NULL)
4558 return false;
4560 tga.info = info;
4561 tga.g = g;
4562 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4564 return true;
4567 /* Return true if a GOT entry for H should live in the local rather than
4568 global GOT area. */
4570 static bool
4571 mips_use_local_got_p (struct bfd_link_info *info,
4572 struct mips_elf_link_hash_entry *h)
4574 /* Symbols that aren't in the dynamic symbol table must live in the
4575 local GOT. This includes symbols that are completely undefined
4576 and which therefore don't bind locally. We'll report undefined
4577 symbols later if appropriate. */
4578 if (h->root.dynindx == -1)
4579 return true;
4581 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4582 to the local GOT, as they would be implicitly relocated by the
4583 base address by the dynamic loader. */
4584 if (bfd_is_abs_symbol (&h->root.root))
4585 return false;
4587 /* Symbols that bind locally can (and in the case of forced-local
4588 symbols, must) live in the local GOT. */
4589 if (h->got_only_for_calls
4590 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4591 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4592 return true;
4594 /* If this is an executable that must provide a definition of the symbol,
4595 either though PLTs or copy relocations, then that address should go in
4596 the local rather than global GOT. */
4597 if (bfd_link_executable (info) && h->has_static_relocs)
4598 return true;
4600 return false;
4603 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4604 link_info structure. Decide whether the hash entry needs an entry in
4605 the global part of the primary GOT, setting global_got_area accordingly.
4606 Count the number of global symbols that are in the primary GOT only
4607 because they have relocations against them (reloc_only_gotno). */
4609 static bool
4610 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4612 struct bfd_link_info *info;
4613 struct mips_elf_link_hash_table *htab;
4614 struct mips_got_info *g;
4616 info = (struct bfd_link_info *) data;
4617 htab = mips_elf_hash_table (info);
4618 g = htab->got_info;
4619 if (h->global_got_area != GGA_NONE)
4621 /* Make a final decision about whether the symbol belongs in the
4622 local or global GOT. */
4623 if (mips_use_local_got_p (info, h))
4624 /* The symbol belongs in the local GOT. We no longer need this
4625 entry if it was only used for relocations; those relocations
4626 will be against the null or section symbol instead of H. */
4627 h->global_got_area = GGA_NONE;
4628 else if (htab->root.target_os == is_vxworks
4629 && h->got_only_for_calls
4630 && h->root.plt.plist->mips_offset != MINUS_ONE)
4631 /* On VxWorks, calls can refer directly to the .got.plt entry;
4632 they don't need entries in the regular GOT. .got.plt entries
4633 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4634 h->global_got_area = GGA_NONE;
4635 else if (h->global_got_area == GGA_RELOC_ONLY)
4637 g->reloc_only_gotno++;
4638 g->global_gotno++;
4641 return 1;
4644 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4645 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4647 static int
4648 mips_elf_add_got_entry (void **entryp, void *data)
4650 struct mips_got_entry *entry;
4651 struct mips_elf_traverse_got_arg *arg;
4652 void **slot;
4654 entry = (struct mips_got_entry *) *entryp;
4655 arg = (struct mips_elf_traverse_got_arg *) data;
4656 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4657 if (!slot)
4659 arg->g = NULL;
4660 return 0;
4662 if (!*slot)
4664 *slot = entry;
4665 mips_elf_count_got_entry (arg->info, arg->g, entry);
4667 return 1;
4670 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4671 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4673 static int
4674 mips_elf_add_got_page_entry (void **entryp, void *data)
4676 struct mips_got_page_entry *entry;
4677 struct mips_elf_traverse_got_arg *arg;
4678 void **slot;
4680 entry = (struct mips_got_page_entry *) *entryp;
4681 arg = (struct mips_elf_traverse_got_arg *) data;
4682 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4683 if (!slot)
4685 arg->g = NULL;
4686 return 0;
4688 if (!*slot)
4690 *slot = entry;
4691 arg->g->page_gotno += entry->num_pages;
4693 return 1;
4696 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4697 this would lead to overflow, 1 if they were merged successfully,
4698 and 0 if a merge failed due to lack of memory. (These values are chosen
4699 so that nonnegative return values can be returned by a htab_traverse
4700 callback.) */
4702 static int
4703 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4704 struct mips_got_info *to,
4705 struct mips_elf_got_per_bfd_arg *arg)
4707 struct mips_elf_traverse_got_arg tga;
4708 unsigned int estimate;
4710 /* Work out how many page entries we would need for the combined GOT. */
4711 estimate = arg->max_pages;
4712 if (estimate >= from->page_gotno + to->page_gotno)
4713 estimate = from->page_gotno + to->page_gotno;
4715 /* And conservatively estimate how many local and TLS entries
4716 would be needed. */
4717 estimate += from->local_gotno + to->local_gotno;
4718 estimate += from->tls_gotno + to->tls_gotno;
4720 /* If we're merging with the primary got, any TLS relocations will
4721 come after the full set of global entries. Otherwise estimate those
4722 conservatively as well. */
4723 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4724 estimate += arg->global_count;
4725 else
4726 estimate += from->global_gotno + to->global_gotno;
4728 /* Bail out if the combined GOT might be too big. */
4729 if (estimate > arg->max_count)
4730 return -1;
4732 /* Transfer the bfd's got information from FROM to TO. */
4733 tga.info = arg->info;
4734 tga.g = to;
4735 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4736 if (!tga.g)
4737 return 0;
4739 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4740 if (!tga.g)
4741 return 0;
4743 mips_elf_replace_bfd_got (abfd, to);
4744 return 1;
4747 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4748 as possible of the primary got, since it doesn't require explicit
4749 dynamic relocations, but don't use bfds that would reference global
4750 symbols out of the addressable range. Failing the primary got,
4751 attempt to merge with the current got, or finish the current got
4752 and then make make the new got current. */
4754 static bool
4755 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4756 struct mips_elf_got_per_bfd_arg *arg)
4758 unsigned int estimate;
4759 int result;
4761 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4762 return false;
4764 /* Work out the number of page, local and TLS entries. */
4765 estimate = arg->max_pages;
4766 if (estimate > g->page_gotno)
4767 estimate = g->page_gotno;
4768 estimate += g->local_gotno + g->tls_gotno;
4770 /* We place TLS GOT entries after both locals and globals. The globals
4771 for the primary GOT may overflow the normal GOT size limit, so be
4772 sure not to merge a GOT which requires TLS with the primary GOT in that
4773 case. This doesn't affect non-primary GOTs. */
4774 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4776 if (estimate <= arg->max_count)
4778 /* If we don't have a primary GOT, use it as
4779 a starting point for the primary GOT. */
4780 if (!arg->primary)
4782 arg->primary = g;
4783 return true;
4786 /* Try merging with the primary GOT. */
4787 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4788 if (result >= 0)
4789 return result;
4792 /* If we can merge with the last-created got, do it. */
4793 if (arg->current)
4795 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4796 if (result >= 0)
4797 return result;
4800 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4801 fits; if it turns out that it doesn't, we'll get relocation
4802 overflows anyway. */
4803 g->next = arg->current;
4804 arg->current = g;
4806 return true;
4809 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4810 to GOTIDX, duplicating the entry if it has already been assigned
4811 an index in a different GOT. */
4813 static bool
4814 mips_elf_set_gotidx (void **entryp, long gotidx)
4816 struct mips_got_entry *entry;
4818 entry = (struct mips_got_entry *) *entryp;
4819 if (entry->gotidx > 0)
4821 struct mips_got_entry *new_entry;
4823 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4824 if (!new_entry)
4825 return false;
4827 *new_entry = *entry;
4828 *entryp = new_entry;
4829 entry = new_entry;
4831 entry->gotidx = gotidx;
4832 return true;
4835 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4836 mips_elf_traverse_got_arg in which DATA->value is the size of one
4837 GOT entry. Set DATA->g to null on failure. */
4839 static int
4840 mips_elf_initialize_tls_index (void **entryp, void *data)
4842 struct mips_got_entry *entry;
4843 struct mips_elf_traverse_got_arg *arg;
4845 /* We're only interested in TLS symbols. */
4846 entry = (struct mips_got_entry *) *entryp;
4847 if (entry->tls_type == GOT_TLS_NONE)
4848 return 1;
4850 arg = (struct mips_elf_traverse_got_arg *) data;
4851 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4853 arg->g = NULL;
4854 return 0;
4857 /* Account for the entries we've just allocated. */
4858 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4859 return 1;
4862 /* A htab_traverse callback for GOT entries, where DATA points to a
4863 mips_elf_traverse_got_arg. Set the global_got_area of each global
4864 symbol to DATA->value. */
4866 static int
4867 mips_elf_set_global_got_area (void **entryp, void *data)
4869 struct mips_got_entry *entry;
4870 struct mips_elf_traverse_got_arg *arg;
4872 entry = (struct mips_got_entry *) *entryp;
4873 arg = (struct mips_elf_traverse_got_arg *) data;
4874 if (entry->abfd != NULL
4875 && entry->symndx == -1
4876 && entry->d.h->global_got_area != GGA_NONE)
4877 entry->d.h->global_got_area = arg->value;
4878 return 1;
4881 /* A htab_traverse callback for secondary GOT entries, where DATA points
4882 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4883 and record the number of relocations they require. DATA->value is
4884 the size of one GOT entry. Set DATA->g to null on failure. */
4886 static int
4887 mips_elf_set_global_gotidx (void **entryp, void *data)
4889 struct mips_got_entry *entry;
4890 struct mips_elf_traverse_got_arg *arg;
4892 entry = (struct mips_got_entry *) *entryp;
4893 arg = (struct mips_elf_traverse_got_arg *) data;
4894 if (entry->abfd != NULL
4895 && entry->symndx == -1
4896 && entry->d.h->global_got_area != GGA_NONE)
4898 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4900 arg->g = NULL;
4901 return 0;
4903 arg->g->assigned_low_gotno += 1;
4905 if (bfd_link_pic (arg->info)
4906 || (elf_hash_table (arg->info)->dynamic_sections_created
4907 && entry->d.h->root.def_dynamic
4908 && !entry->d.h->root.def_regular))
4909 arg->g->relocs += 1;
4912 return 1;
4915 /* A htab_traverse callback for GOT entries for which DATA is the
4916 bfd_link_info. Forbid any global symbols from having traditional
4917 lazy-binding stubs. */
4919 static int
4920 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4922 struct bfd_link_info *info;
4923 struct mips_elf_link_hash_table *htab;
4924 struct mips_got_entry *entry;
4926 entry = (struct mips_got_entry *) *entryp;
4927 info = (struct bfd_link_info *) data;
4928 htab = mips_elf_hash_table (info);
4929 BFD_ASSERT (htab != NULL);
4931 if (entry->abfd != NULL
4932 && entry->symndx == -1
4933 && entry->d.h->needs_lazy_stub)
4935 entry->d.h->needs_lazy_stub = false;
4936 htab->lazy_stub_count--;
4939 return 1;
4942 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4943 the primary GOT. */
4944 static bfd_vma
4945 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4947 if (!g->next)
4948 return 0;
4950 g = mips_elf_bfd_got (ibfd, false);
4951 if (! g)
4952 return 0;
4954 BFD_ASSERT (g->next);
4956 g = g->next;
4958 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4959 * MIPS_ELF_GOT_SIZE (abfd);
4962 /* Turn a single GOT that is too big for 16-bit addressing into
4963 a sequence of GOTs, each one 16-bit addressable. */
4965 static bool
4966 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4967 asection *got, bfd_size_type pages)
4969 struct mips_elf_link_hash_table *htab;
4970 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4971 struct mips_elf_traverse_got_arg tga;
4972 struct mips_got_info *g, *gg;
4973 unsigned int assign, needed_relocs;
4974 bfd *dynobj, *ibfd;
4976 dynobj = elf_hash_table (info)->dynobj;
4977 htab = mips_elf_hash_table (info);
4978 BFD_ASSERT (htab != NULL);
4980 g = htab->got_info;
4982 got_per_bfd_arg.obfd = abfd;
4983 got_per_bfd_arg.info = info;
4984 got_per_bfd_arg.current = NULL;
4985 got_per_bfd_arg.primary = NULL;
4986 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4987 / MIPS_ELF_GOT_SIZE (abfd))
4988 - htab->reserved_gotno);
4989 got_per_bfd_arg.max_pages = pages;
4990 /* The number of globals that will be included in the primary GOT.
4991 See the calls to mips_elf_set_global_got_area below for more
4992 information. */
4993 got_per_bfd_arg.global_count = g->global_gotno;
4995 /* Try to merge the GOTs of input bfds together, as long as they
4996 don't seem to exceed the maximum GOT size, choosing one of them
4997 to be the primary GOT. */
4998 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
5000 gg = mips_elf_bfd_got (ibfd, false);
5001 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5002 return false;
5005 /* If we do not find any suitable primary GOT, create an empty one. */
5006 if (got_per_bfd_arg.primary == NULL)
5007 g->next = mips_elf_create_got_info (abfd);
5008 else
5009 g->next = got_per_bfd_arg.primary;
5010 g->next->next = got_per_bfd_arg.current;
5012 /* GG is now the master GOT, and G is the primary GOT. */
5013 gg = g;
5014 g = g->next;
5016 /* Map the output bfd to the primary got. That's what we're going
5017 to use for bfds that use GOT16 or GOT_PAGE relocations that we
5018 didn't mark in check_relocs, and we want a quick way to find it.
5019 We can't just use gg->next because we're going to reverse the
5020 list. */
5021 mips_elf_replace_bfd_got (abfd, g);
5023 /* Every symbol that is referenced in a dynamic relocation must be
5024 present in the primary GOT, so arrange for them to appear after
5025 those that are actually referenced. */
5026 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5027 g->global_gotno = gg->global_gotno;
5029 tga.info = info;
5030 tga.value = GGA_RELOC_ONLY;
5031 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5032 tga.value = GGA_NORMAL;
5033 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5035 /* Now go through the GOTs assigning them offset ranges.
5036 [assigned_low_gotno, local_gotno[ will be set to the range of local
5037 entries in each GOT. We can then compute the end of a GOT by
5038 adding local_gotno to global_gotno. We reverse the list and make
5039 it circular since then we'll be able to quickly compute the
5040 beginning of a GOT, by computing the end of its predecessor. To
5041 avoid special cases for the primary GOT, while still preserving
5042 assertions that are valid for both single- and multi-got links,
5043 we arrange for the main got struct to have the right number of
5044 global entries, but set its local_gotno such that the initial
5045 offset of the primary GOT is zero. Remember that the primary GOT
5046 will become the last item in the circular linked list, so it
5047 points back to the master GOT. */
5048 gg->local_gotno = -g->global_gotno;
5049 gg->global_gotno = g->global_gotno;
5050 gg->tls_gotno = 0;
5051 assign = 0;
5052 gg->next = gg;
5056 struct mips_got_info *gn;
5058 assign += htab->reserved_gotno;
5059 g->assigned_low_gotno = assign;
5060 g->local_gotno += assign;
5061 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5062 g->assigned_high_gotno = g->local_gotno - 1;
5063 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5065 /* Take g out of the direct list, and push it onto the reversed
5066 list that gg points to. g->next is guaranteed to be nonnull after
5067 this operation, as required by mips_elf_initialize_tls_index. */
5068 gn = g->next;
5069 g->next = gg->next;
5070 gg->next = g;
5072 /* Set up any TLS entries. We always place the TLS entries after
5073 all non-TLS entries. */
5074 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5075 tga.g = g;
5076 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5077 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5078 if (!tga.g)
5079 return false;
5080 BFD_ASSERT (g->tls_assigned_gotno == assign);
5082 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
5083 g = gn;
5085 /* Forbid global symbols in every non-primary GOT from having
5086 lazy-binding stubs. */
5087 if (g)
5088 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5090 while (g);
5092 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5094 needed_relocs = 0;
5095 for (g = gg->next; g && g->next != gg; g = g->next)
5097 unsigned int save_assign;
5099 /* Assign offsets to global GOT entries and count how many
5100 relocations they need. */
5101 save_assign = g->assigned_low_gotno;
5102 g->assigned_low_gotno = g->local_gotno;
5103 tga.info = info;
5104 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5105 tga.g = g;
5106 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5107 if (!tga.g)
5108 return false;
5109 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5110 g->assigned_low_gotno = save_assign;
5112 if (bfd_link_pic (info))
5114 g->relocs += g->local_gotno - g->assigned_low_gotno;
5115 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5116 + g->next->global_gotno
5117 + g->next->tls_gotno
5118 + htab->reserved_gotno);
5120 needed_relocs += g->relocs;
5122 needed_relocs += g->relocs;
5124 if (needed_relocs)
5125 mips_elf_allocate_dynamic_relocations (dynobj, info,
5126 needed_relocs);
5128 return true;
5132 /* Returns the first relocation of type r_type found, beginning with
5133 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5135 static const Elf_Internal_Rela *
5136 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5137 const Elf_Internal_Rela *relocation,
5138 const Elf_Internal_Rela *relend)
5140 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5142 while (relocation < relend)
5144 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5145 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5146 return relocation;
5148 ++relocation;
5151 /* We didn't find it. */
5152 return NULL;
5155 /* Return whether an input relocation is against a local symbol. */
5157 static bool
5158 mips_elf_local_relocation_p (bfd *input_bfd,
5159 const Elf_Internal_Rela *relocation,
5160 asection **local_sections)
5162 unsigned long r_symndx;
5163 Elf_Internal_Shdr *symtab_hdr;
5164 size_t extsymoff;
5166 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5167 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5168 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5170 if (r_symndx < extsymoff)
5171 return true;
5172 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5173 return true;
5175 return false;
5178 /* Sign-extend VALUE, which has the indicated number of BITS. */
5180 bfd_vma
5181 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5183 if (value & ((bfd_vma) 1 << (bits - 1)))
5184 /* VALUE is negative. */
5185 value |= ((bfd_vma) - 1) << bits;
5187 return value;
5190 /* Return non-zero if the indicated VALUE has overflowed the maximum
5191 range expressible by a signed number with the indicated number of
5192 BITS. */
5194 static bool
5195 mips_elf_overflow_p (bfd_vma value, int bits)
5197 bfd_signed_vma svalue = (bfd_signed_vma) value;
5199 if (svalue > (1 << (bits - 1)) - 1)
5200 /* The value is too big. */
5201 return true;
5202 else if (svalue < -(1 << (bits - 1)))
5203 /* The value is too small. */
5204 return true;
5206 /* All is well. */
5207 return false;
5210 /* Calculate the %high function. */
5212 static bfd_vma
5213 mips_elf_high (bfd_vma value)
5215 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5218 /* Calculate the %higher function. */
5220 static bfd_vma
5221 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5223 #ifdef BFD64
5224 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5225 #else
5226 abort ();
5227 return MINUS_ONE;
5228 #endif
5231 /* Calculate the %highest function. */
5233 static bfd_vma
5234 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5236 #ifdef BFD64
5237 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5238 #else
5239 abort ();
5240 return MINUS_ONE;
5241 #endif
5244 /* Create the .compact_rel section. */
5246 static bool
5247 mips_elf_create_compact_rel_section
5248 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5250 flagword flags;
5251 register asection *s;
5253 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5255 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5256 | SEC_READONLY);
5258 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5259 if (s == NULL
5260 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5261 return false;
5263 s->size = sizeof (Elf32_External_compact_rel);
5266 return true;
5269 /* Create the .got section to hold the global offset table. */
5271 static bool
5272 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5274 flagword flags;
5275 register asection *s;
5276 struct elf_link_hash_entry *h;
5277 struct bfd_link_hash_entry *bh;
5278 struct mips_elf_link_hash_table *htab;
5280 htab = mips_elf_hash_table (info);
5281 BFD_ASSERT (htab != NULL);
5283 /* This function may be called more than once. */
5284 if (htab->root.sgot)
5285 return true;
5287 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5288 | SEC_LINKER_CREATED);
5290 /* We have to use an alignment of 2**4 here because this is hardcoded
5291 in the function stub generation and in the linker script. */
5292 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5293 if (s == NULL
5294 || !bfd_set_section_alignment (s, 4))
5295 return false;
5296 htab->root.sgot = s;
5298 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5299 linker script because we don't want to define the symbol if we
5300 are not creating a global offset table. */
5301 bh = NULL;
5302 if (! (_bfd_generic_link_add_one_symbol
5303 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5304 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5305 return false;
5307 h = (struct elf_link_hash_entry *) bh;
5308 h->non_elf = 0;
5309 h->def_regular = 1;
5310 h->type = STT_OBJECT;
5311 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5312 elf_hash_table (info)->hgot = h;
5314 if (bfd_link_pic (info)
5315 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5316 return false;
5318 htab->got_info = mips_elf_create_got_info (abfd);
5319 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5320 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5322 /* We also need a .got.plt section when generating PLTs. */
5323 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5324 SEC_ALLOC | SEC_LOAD
5325 | SEC_HAS_CONTENTS
5326 | SEC_IN_MEMORY
5327 | SEC_LINKER_CREATED);
5328 if (s == NULL)
5329 return false;
5330 htab->root.sgotplt = s;
5332 return true;
5335 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5336 __GOTT_INDEX__ symbols. These symbols are only special for
5337 shared objects; they are not used in executables. */
5339 static bool
5340 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5342 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5343 && bfd_link_pic (info)
5344 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5345 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5348 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5349 require an la25 stub. See also mips_elf_local_pic_function_p,
5350 which determines whether the destination function ever requires a
5351 stub. */
5353 static bool
5354 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5355 bool target_is_16_bit_code_p)
5357 /* We specifically ignore branches and jumps from EF_PIC objects,
5358 where the onus is on the compiler or programmer to perform any
5359 necessary initialization of $25. Sometimes such initialization
5360 is unnecessary; for example, -mno-shared functions do not use
5361 the incoming value of $25, and may therefore be called directly. */
5362 if (PIC_OBJECT_P (input_bfd))
5363 return false;
5365 switch (r_type)
5367 case R_MIPS_26:
5368 case R_MIPS_PC16:
5369 case R_MIPS_PC21_S2:
5370 case R_MIPS_PC26_S2:
5371 case R_MICROMIPS_26_S1:
5372 case R_MICROMIPS_PC7_S1:
5373 case R_MICROMIPS_PC10_S1:
5374 case R_MICROMIPS_PC16_S1:
5375 case R_MICROMIPS_PC23_S2:
5376 return true;
5378 case R_MIPS16_26:
5379 return !target_is_16_bit_code_p;
5381 default:
5382 return false;
5386 /* Obtain the field relocated by RELOCATION. */
5388 static bfd_vma
5389 mips_elf_obtain_contents (reloc_howto_type *howto,
5390 const Elf_Internal_Rela *relocation,
5391 bfd *input_bfd, bfd_byte *contents)
5393 bfd_vma x = 0;
5394 bfd_byte *location = contents + relocation->r_offset;
5395 unsigned int size = bfd_get_reloc_size (howto);
5397 /* Obtain the bytes. */
5398 if (size != 0)
5399 x = bfd_get (8 * size, input_bfd, location);
5401 return x;
5404 /* Store the field relocated by RELOCATION. */
5406 static void
5407 mips_elf_store_contents (reloc_howto_type *howto,
5408 const Elf_Internal_Rela *relocation,
5409 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5411 bfd_byte *location = contents + relocation->r_offset;
5412 unsigned int size = bfd_get_reloc_size (howto);
5414 /* Put the value into the output. */
5415 if (size != 0)
5416 bfd_put (8 * size, input_bfd, x, location);
5419 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5420 RELOCATION described by HOWTO, with a move of 0 to the load target
5421 register, returning TRUE if that is successful and FALSE otherwise.
5422 If DOIT is FALSE, then only determine it patching is possible and
5423 return status without actually changing CONTENTS.
5426 static bool
5427 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5428 const Elf_Internal_Rela *relocation,
5429 reloc_howto_type *howto, bool doit)
5431 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5432 bfd_byte *location = contents + relocation->r_offset;
5433 bool nullified = true;
5434 bfd_vma x;
5436 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5438 /* Obtain the current value. */
5439 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5441 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5442 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5443 if (mips16_reloc_p (r_type)
5444 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5445 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5446 x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
5447 else if (micromips_reloc_p (r_type)
5448 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5449 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5450 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5451 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5452 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5453 else
5454 nullified = false;
5456 /* Put the value into the output. */
5457 if (doit && nullified)
5458 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5460 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5462 return nullified;
5465 /* Calculate the value produced by the RELOCATION (which comes from
5466 the INPUT_BFD). The ADDEND is the addend to use for this
5467 RELOCATION; RELOCATION->R_ADDEND is ignored.
5469 The result of the relocation calculation is stored in VALUEP.
5470 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5471 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5473 This function returns bfd_reloc_continue if the caller need take no
5474 further action regarding this relocation, bfd_reloc_notsupported if
5475 something goes dramatically wrong, bfd_reloc_overflow if an
5476 overflow occurs, and bfd_reloc_ok to indicate success. */
5478 static bfd_reloc_status_type
5479 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5480 asection *input_section, bfd_byte *contents,
5481 struct bfd_link_info *info,
5482 const Elf_Internal_Rela *relocation,
5483 bfd_vma addend, reloc_howto_type *howto,
5484 Elf_Internal_Sym *local_syms,
5485 asection **local_sections, bfd_vma *valuep,
5486 const char **namep,
5487 bool *cross_mode_jump_p,
5488 bool save_addend)
5490 /* The eventual value we will return. */
5491 bfd_vma value;
5492 /* The address of the symbol against which the relocation is
5493 occurring. */
5494 bfd_vma symbol = 0;
5495 /* The final GP value to be used for the relocatable, executable, or
5496 shared object file being produced. */
5497 bfd_vma gp;
5498 /* The place (section offset or address) of the storage unit being
5499 relocated. */
5500 bfd_vma p;
5501 /* The value of GP used to create the relocatable object. */
5502 bfd_vma gp0;
5503 /* The offset into the global offset table at which the address of
5504 the relocation entry symbol, adjusted by the addend, resides
5505 during execution. */
5506 bfd_vma g = MINUS_ONE;
5507 /* The section in which the symbol referenced by the relocation is
5508 located. */
5509 asection *sec = NULL;
5510 struct mips_elf_link_hash_entry *h = NULL;
5511 /* TRUE if the symbol referred to by this relocation is a local
5512 symbol. */
5513 bool local_p, was_local_p;
5514 /* TRUE if the symbol referred to by this relocation is a section
5515 symbol. */
5516 bool section_p = false;
5517 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5518 bool gp_disp_p = false;
5519 /* TRUE if the symbol referred to by this relocation is
5520 "__gnu_local_gp". */
5521 bool gnu_local_gp_p = false;
5522 Elf_Internal_Shdr *symtab_hdr;
5523 size_t extsymoff;
5524 unsigned long r_symndx;
5525 int r_type;
5526 /* TRUE if overflow occurred during the calculation of the
5527 relocation value. */
5528 bool overflowed_p;
5529 /* TRUE if this relocation refers to a MIPS16 function. */
5530 bool target_is_16_bit_code_p = false;
5531 bool target_is_micromips_code_p = false;
5532 struct mips_elf_link_hash_table *htab;
5533 bfd *dynobj;
5534 bool resolved_to_zero;
5536 dynobj = elf_hash_table (info)->dynobj;
5537 htab = mips_elf_hash_table (info);
5538 BFD_ASSERT (htab != NULL);
5540 /* Parse the relocation. */
5541 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5542 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5543 p = (input_section->output_section->vma
5544 + input_section->output_offset
5545 + relocation->r_offset);
5547 /* Assume that there will be no overflow. */
5548 overflowed_p = false;
5550 /* Figure out whether or not the symbol is local, and get the offset
5551 used in the array of hash table entries. */
5552 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5553 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5554 local_sections);
5555 was_local_p = local_p;
5556 if (! elf_bad_symtab (input_bfd))
5557 extsymoff = symtab_hdr->sh_info;
5558 else
5560 /* The symbol table does not follow the rule that local symbols
5561 must come before globals. */
5562 extsymoff = 0;
5565 /* Figure out the value of the symbol. */
5566 if (local_p)
5568 bool micromips_p = MICROMIPS_P (abfd);
5569 Elf_Internal_Sym *sym;
5571 sym = local_syms + r_symndx;
5572 sec = local_sections[r_symndx];
5574 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5576 symbol = sec->output_section->vma + sec->output_offset;
5577 if (!section_p || (sec->flags & SEC_MERGE))
5578 symbol += sym->st_value;
5579 if ((sec->flags & SEC_MERGE) && section_p)
5581 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5582 addend -= symbol;
5583 addend += sec->output_section->vma + sec->output_offset;
5586 /* MIPS16/microMIPS text labels should be treated as odd. */
5587 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5588 ++symbol;
5590 /* Record the name of this symbol, for our caller. */
5591 *namep = bfd_elf_string_from_elf_section (input_bfd,
5592 symtab_hdr->sh_link,
5593 sym->st_name);
5594 if (*namep == NULL || **namep == '\0')
5595 *namep = bfd_section_name (sec);
5597 /* For relocations against a section symbol and ones against no
5598 symbol (absolute relocations) infer the ISA mode from the addend. */
5599 if (section_p || r_symndx == STN_UNDEF)
5601 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5602 target_is_micromips_code_p = (addend & 1) && micromips_p;
5604 /* For relocations against an absolute symbol infer the ISA mode
5605 from the value of the symbol plus addend. */
5606 else if (bfd_is_abs_section (sec))
5608 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5609 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5611 /* Otherwise just use the regular symbol annotation available. */
5612 else
5614 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5615 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5618 else
5620 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5622 /* For global symbols we look up the symbol in the hash-table. */
5623 h = ((struct mips_elf_link_hash_entry *)
5624 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5625 /* Find the real hash-table entry for this symbol. */
5626 while (h->root.root.type == bfd_link_hash_indirect
5627 || h->root.root.type == bfd_link_hash_warning)
5628 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5630 /* Record the name of this symbol, for our caller. */
5631 *namep = h->root.root.root.string;
5633 /* See if this is the special _gp_disp symbol. Note that such a
5634 symbol must always be a global symbol. */
5635 if (strcmp (*namep, "_gp_disp") == 0
5636 && ! NEWABI_P (input_bfd))
5638 /* Relocations against _gp_disp are permitted only with
5639 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5640 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5641 return bfd_reloc_notsupported;
5643 gp_disp_p = true;
5645 /* See if this is the special _gp symbol. Note that such a
5646 symbol must always be a global symbol. */
5647 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5648 gnu_local_gp_p = true;
5651 /* If this symbol is defined, calculate its address. Note that
5652 _gp_disp is a magic symbol, always implicitly defined by the
5653 linker, so it's inappropriate to check to see whether or not
5654 its defined. */
5655 else if ((h->root.root.type == bfd_link_hash_defined
5656 || h->root.root.type == bfd_link_hash_defweak)
5657 && h->root.root.u.def.section)
5659 sec = h->root.root.u.def.section;
5660 if (sec->output_section)
5661 symbol = (h->root.root.u.def.value
5662 + sec->output_section->vma
5663 + sec->output_offset);
5664 else
5665 symbol = h->root.root.u.def.value;
5667 else if (h->root.root.type == bfd_link_hash_undefweak)
5668 /* We allow relocations against undefined weak symbols, giving
5669 it the value zero, so that you can undefined weak functions
5670 and check to see if they exist by looking at their
5671 addresses. */
5672 symbol = 0;
5673 else if (info->unresolved_syms_in_objects == RM_IGNORE
5674 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5675 symbol = 0;
5676 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5677 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5679 /* If this is a dynamic link, we should have created a
5680 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5681 in _bfd_mips_elf_create_dynamic_sections.
5682 Otherwise, we should define the symbol with a value of 0.
5683 FIXME: It should probably get into the symbol table
5684 somehow as well. */
5685 BFD_ASSERT (! bfd_link_pic (info));
5686 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5687 symbol = 0;
5689 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5691 /* This is an optional symbol - an Irix specific extension to the
5692 ELF spec. Ignore it for now.
5693 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5694 than simply ignoring them, but we do not handle this for now.
5695 For information see the "64-bit ELF Object File Specification"
5696 which is available from here:
5697 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5698 symbol = 0;
5700 else
5702 bool reject_undefined
5703 = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5704 && !info->warn_unresolved_syms)
5705 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5707 info->callbacks->undefined_symbol
5708 (info, h->root.root.root.string, input_bfd,
5709 input_section, relocation->r_offset, reject_undefined);
5711 if (reject_undefined)
5712 return bfd_reloc_undefined;
5714 symbol = 0;
5717 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5718 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5721 /* If this is a reference to a 16-bit function with a stub, we need
5722 to redirect the relocation to the stub unless:
5724 (a) the relocation is for a MIPS16 JAL;
5726 (b) the relocation is for a MIPS16 PIC call, and there are no
5727 non-MIPS16 uses of the GOT slot; or
5729 (c) the section allows direct references to MIPS16 functions. */
5730 if (r_type != R_MIPS16_26
5731 && !bfd_link_relocatable (info)
5732 && ((h != NULL
5733 && h->fn_stub != NULL
5734 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5735 || (local_p
5736 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5737 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5738 && !section_allows_mips16_refs_p (input_section))
5740 /* This is a 32- or 64-bit call to a 16-bit function. We should
5741 have already noticed that we were going to need the
5742 stub. */
5743 if (local_p)
5745 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5746 value = 0;
5748 else
5750 BFD_ASSERT (h->need_fn_stub);
5751 if (h->la25_stub)
5753 /* If a LA25 header for the stub itself exists, point to the
5754 prepended LUI/ADDIU sequence. */
5755 sec = h->la25_stub->stub_section;
5756 value = h->la25_stub->offset;
5758 else
5760 sec = h->fn_stub;
5761 value = 0;
5765 symbol = sec->output_section->vma + sec->output_offset + value;
5766 /* The target is 16-bit, but the stub isn't. */
5767 target_is_16_bit_code_p = false;
5769 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5770 to a standard MIPS function, we need to redirect the call to the stub.
5771 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5772 indirect calls should use an indirect stub instead. */
5773 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5774 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5775 || (local_p
5776 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5777 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5778 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5780 if (local_p)
5781 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5782 else
5784 /* If both call_stub and call_fp_stub are defined, we can figure
5785 out which one to use by checking which one appears in the input
5786 file. */
5787 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5789 asection *o;
5791 sec = NULL;
5792 for (o = input_bfd->sections; o != NULL; o = o->next)
5794 if (CALL_FP_STUB_P (bfd_section_name (o)))
5796 sec = h->call_fp_stub;
5797 break;
5800 if (sec == NULL)
5801 sec = h->call_stub;
5803 else if (h->call_stub != NULL)
5804 sec = h->call_stub;
5805 else
5806 sec = h->call_fp_stub;
5809 BFD_ASSERT (sec->size > 0);
5810 symbol = sec->output_section->vma + sec->output_offset;
5812 /* If this is a direct call to a PIC function, redirect to the
5813 non-PIC stub. */
5814 else if (h != NULL && h->la25_stub
5815 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5816 target_is_16_bit_code_p))
5818 symbol = (h->la25_stub->stub_section->output_section->vma
5819 + h->la25_stub->stub_section->output_offset
5820 + h->la25_stub->offset);
5821 if (ELF_ST_IS_MICROMIPS (h->root.other))
5822 symbol |= 1;
5824 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5825 entry is used if a standard PLT entry has also been made. In this
5826 case the symbol will have been set by mips_elf_set_plt_sym_value
5827 to point to the standard PLT entry, so redirect to the compressed
5828 one. */
5829 else if ((mips16_branch_reloc_p (r_type)
5830 || micromips_branch_reloc_p (r_type))
5831 && !bfd_link_relocatable (info)
5832 && h != NULL
5833 && h->use_plt_entry
5834 && h->root.plt.plist->comp_offset != MINUS_ONE
5835 && h->root.plt.plist->mips_offset != MINUS_ONE)
5837 bool micromips_p = MICROMIPS_P (abfd);
5839 sec = htab->root.splt;
5840 symbol = (sec->output_section->vma
5841 + sec->output_offset
5842 + htab->plt_header_size
5843 + htab->plt_mips_offset
5844 + h->root.plt.plist->comp_offset
5845 + 1);
5847 target_is_16_bit_code_p = !micromips_p;
5848 target_is_micromips_code_p = micromips_p;
5851 /* Make sure MIPS16 and microMIPS are not used together. */
5852 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5853 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5855 _bfd_error_handler
5856 (_("MIPS16 and microMIPS functions cannot call each other"));
5857 return bfd_reloc_notsupported;
5860 /* Calls from 16-bit code to 32-bit code and vice versa require the
5861 mode change. However, we can ignore calls to undefined weak symbols,
5862 which should never be executed at runtime. This exception is important
5863 because the assembly writer may have "known" that any definition of the
5864 symbol would be 16-bit code, and that direct jumps were therefore
5865 acceptable. */
5866 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5867 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5868 && ((mips16_branch_reloc_p (r_type)
5869 && !target_is_16_bit_code_p)
5870 || (micromips_branch_reloc_p (r_type)
5871 && !target_is_micromips_code_p)
5872 || ((branch_reloc_p (r_type)
5873 || r_type == R_MIPS_JALR)
5874 && (target_is_16_bit_code_p
5875 || target_is_micromips_code_p))));
5877 resolved_to_zero = (h != NULL
5878 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5880 switch (r_type)
5882 case R_MIPS16_CALL16:
5883 case R_MIPS16_GOT16:
5884 case R_MIPS_CALL16:
5885 case R_MIPS_GOT16:
5886 case R_MIPS_GOT_PAGE:
5887 case R_MIPS_GOT_DISP:
5888 case R_MIPS_GOT_LO16:
5889 case R_MIPS_CALL_LO16:
5890 case R_MICROMIPS_CALL16:
5891 case R_MICROMIPS_GOT16:
5892 case R_MICROMIPS_GOT_PAGE:
5893 case R_MICROMIPS_GOT_DISP:
5894 case R_MICROMIPS_GOT_LO16:
5895 case R_MICROMIPS_CALL_LO16:
5896 if (resolved_to_zero
5897 && !bfd_link_relocatable (info)
5898 && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5899 relocation->r_offset)
5900 && mips_elf_nullify_got_load (input_bfd, contents,
5901 relocation, howto, true))
5902 return bfd_reloc_continue;
5904 /* Fall through. */
5905 case R_MIPS_GOT_HI16:
5906 case R_MIPS_CALL_HI16:
5907 case R_MICROMIPS_GOT_HI16:
5908 case R_MICROMIPS_CALL_HI16:
5909 if (resolved_to_zero
5910 && htab->use_absolute_zero
5911 && bfd_link_pic (info))
5913 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5914 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5915 false, false, false);
5916 BFD_ASSERT (h != NULL);
5918 break;
5921 local_p = (h == NULL || mips_use_local_got_p (info, h));
5923 gp0 = _bfd_get_gp_value (input_bfd);
5924 gp = _bfd_get_gp_value (abfd);
5925 if (htab->got_info)
5926 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5928 if (gnu_local_gp_p)
5929 symbol = gp;
5931 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5932 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5933 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5934 if (got_page_reloc_p (r_type) && !local_p)
5936 r_type = (micromips_reloc_p (r_type)
5937 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5938 addend = 0;
5941 /* If we haven't already determined the GOT offset, and we're going
5942 to need it, get it now. */
5943 switch (r_type)
5945 case R_MIPS16_CALL16:
5946 case R_MIPS16_GOT16:
5947 case R_MIPS_CALL16:
5948 case R_MIPS_GOT16:
5949 case R_MIPS_GOT_DISP:
5950 case R_MIPS_GOT_HI16:
5951 case R_MIPS_CALL_HI16:
5952 case R_MIPS_GOT_LO16:
5953 case R_MIPS_CALL_LO16:
5954 case R_MICROMIPS_CALL16:
5955 case R_MICROMIPS_GOT16:
5956 case R_MICROMIPS_GOT_DISP:
5957 case R_MICROMIPS_GOT_HI16:
5958 case R_MICROMIPS_CALL_HI16:
5959 case R_MICROMIPS_GOT_LO16:
5960 case R_MICROMIPS_CALL_LO16:
5961 case R_MIPS_TLS_GD:
5962 case R_MIPS_TLS_GOTTPREL:
5963 case R_MIPS_TLS_LDM:
5964 case R_MIPS16_TLS_GD:
5965 case R_MIPS16_TLS_GOTTPREL:
5966 case R_MIPS16_TLS_LDM:
5967 case R_MICROMIPS_TLS_GD:
5968 case R_MICROMIPS_TLS_GOTTPREL:
5969 case R_MICROMIPS_TLS_LDM:
5970 /* Find the index into the GOT where this value is located. */
5971 if (tls_ldm_reloc_p (r_type))
5973 g = mips_elf_local_got_index (abfd, input_bfd, info,
5974 0, 0, NULL, r_type);
5975 if (g == MINUS_ONE)
5976 return bfd_reloc_outofrange;
5978 else if (!local_p)
5980 /* On VxWorks, CALL relocations should refer to the .got.plt
5981 entry, which is initialized to point at the PLT stub. */
5982 if (htab->root.target_os == is_vxworks
5983 && (call_hi16_reloc_p (r_type)
5984 || call_lo16_reloc_p (r_type)
5985 || call16_reloc_p (r_type)))
5987 BFD_ASSERT (addend == 0);
5988 BFD_ASSERT (h->root.needs_plt);
5989 g = mips_elf_gotplt_index (info, &h->root);
5991 else
5993 BFD_ASSERT (addend == 0);
5994 g = mips_elf_global_got_index (abfd, info, input_bfd,
5995 &h->root, r_type);
5996 if (!TLS_RELOC_P (r_type)
5997 && !elf_hash_table (info)->dynamic_sections_created)
5998 /* This is a static link. We must initialize the GOT entry. */
5999 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
6002 else if (htab->root.target_os != is_vxworks
6003 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6004 /* The calculation below does not involve "g". */
6005 break;
6006 else
6008 g = mips_elf_local_got_index (abfd, input_bfd, info,
6009 symbol + addend, r_symndx, h, r_type);
6010 if (g == MINUS_ONE)
6011 return bfd_reloc_outofrange;
6014 /* Convert GOT indices to actual offsets. */
6015 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6016 break;
6019 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6020 symbols are resolved by the loader. Add them to .rela.dyn. */
6021 if (h != NULL && is_gott_symbol (info, &h->root))
6023 Elf_Internal_Rela outrel;
6024 bfd_byte *loc;
6025 asection *s;
6027 s = mips_elf_rel_dyn_section (info, false);
6028 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6030 outrel.r_offset = (input_section->output_section->vma
6031 + input_section->output_offset
6032 + relocation->r_offset);
6033 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6034 outrel.r_addend = addend;
6035 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6037 /* If we've written this relocation for a readonly section,
6038 we need to set DF_TEXTREL again, so that we do not delete the
6039 DT_TEXTREL tag. */
6040 if (MIPS_ELF_READONLY_SECTION (input_section))
6041 info->flags |= DF_TEXTREL;
6043 *valuep = 0;
6044 return bfd_reloc_ok;
6047 /* Figure out what kind of relocation is being performed. */
6048 switch (r_type)
6050 case R_MIPS_NONE:
6051 return bfd_reloc_continue;
6053 case R_MIPS_16:
6054 if (howto->partial_inplace)
6055 addend = _bfd_mips_elf_sign_extend (addend, 16);
6056 value = symbol + addend;
6057 overflowed_p = mips_elf_overflow_p (value, 16);
6058 break;
6060 case R_MIPS_32:
6061 case R_MIPS_REL32:
6062 case R_MIPS_64:
6063 if ((bfd_link_pic (info)
6064 || (htab->root.dynamic_sections_created
6065 && h != NULL
6066 && h->root.def_dynamic
6067 && !h->root.def_regular
6068 && !h->has_static_relocs))
6069 && r_symndx != STN_UNDEF
6070 && (h == NULL
6071 || h->root.root.type != bfd_link_hash_undefweak
6072 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6073 && !resolved_to_zero))
6074 && (input_section->flags & SEC_ALLOC) != 0)
6076 /* If we're creating a shared library, then we can't know
6077 where the symbol will end up. So, we create a relocation
6078 record in the output, and leave the job up to the dynamic
6079 linker. We must do the same for executable references to
6080 shared library symbols, unless we've decided to use copy
6081 relocs or PLTs instead. */
6082 value = addend;
6083 if (!mips_elf_create_dynamic_relocation (abfd,
6084 info,
6085 relocation,
6087 sec,
6088 symbol,
6089 &value,
6090 input_section))
6091 return bfd_reloc_undefined;
6093 else
6095 if (r_type != R_MIPS_REL32)
6096 value = symbol + addend;
6097 else
6098 value = addend;
6100 value &= howto->dst_mask;
6101 break;
6103 case R_MIPS_PC32:
6104 value = symbol + addend - p;
6105 value &= howto->dst_mask;
6106 break;
6108 case R_MIPS16_26:
6109 /* The calculation for R_MIPS16_26 is just the same as for an
6110 R_MIPS_26. It's only the storage of the relocated field into
6111 the output file that's different. That's handled in
6112 mips_elf_perform_relocation. So, we just fall through to the
6113 R_MIPS_26 case here. */
6114 case R_MIPS_26:
6115 case R_MICROMIPS_26_S1:
6117 unsigned int shift;
6119 /* Shift is 2, unusually, for microMIPS JALX. */
6120 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6122 if (howto->partial_inplace && !section_p)
6123 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6124 else
6125 value = addend;
6126 value += symbol;
6128 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6129 be the correct ISA mode selector except for weak undefined
6130 symbols. */
6131 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6132 && (*cross_mode_jump_p
6133 ? (value & 3) != (r_type == R_MIPS_26)
6134 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6135 return bfd_reloc_outofrange;
6137 value >>= shift;
6138 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6139 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6140 value &= howto->dst_mask;
6142 break;
6144 case R_MIPS_TLS_DTPREL_HI16:
6145 case R_MIPS16_TLS_DTPREL_HI16:
6146 case R_MICROMIPS_TLS_DTPREL_HI16:
6147 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6148 & howto->dst_mask);
6149 break;
6151 case R_MIPS_TLS_DTPREL_LO16:
6152 case R_MIPS_TLS_DTPREL32:
6153 case R_MIPS_TLS_DTPREL64:
6154 case R_MIPS16_TLS_DTPREL_LO16:
6155 case R_MICROMIPS_TLS_DTPREL_LO16:
6156 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6157 break;
6159 case R_MIPS_TLS_TPREL_HI16:
6160 case R_MIPS16_TLS_TPREL_HI16:
6161 case R_MICROMIPS_TLS_TPREL_HI16:
6162 value = (mips_elf_high (addend + symbol - tprel_base (info))
6163 & howto->dst_mask);
6164 break;
6166 case R_MIPS_TLS_TPREL_LO16:
6167 case R_MIPS_TLS_TPREL32:
6168 case R_MIPS_TLS_TPREL64:
6169 case R_MIPS16_TLS_TPREL_LO16:
6170 case R_MICROMIPS_TLS_TPREL_LO16:
6171 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6172 break;
6174 case R_MIPS_HI16:
6175 case R_MIPS16_HI16:
6176 case R_MICROMIPS_HI16:
6177 if (!gp_disp_p)
6179 value = mips_elf_high (addend + symbol);
6180 value &= howto->dst_mask;
6182 else
6184 /* For MIPS16 ABI code we generate this sequence
6185 0: li $v0,%hi(_gp_disp)
6186 4: addiupc $v1,%lo(_gp_disp)
6187 8: sll $v0,16
6188 12: addu $v0,$v1
6189 14: move $gp,$v0
6190 So the offsets of hi and lo relocs are the same, but the
6191 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6192 ADDIUPC clears the low two bits of the instruction address,
6193 so the base is ($t9 + 4) & ~3. */
6194 if (r_type == R_MIPS16_HI16)
6195 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6196 /* The microMIPS .cpload sequence uses the same assembly
6197 instructions as the traditional psABI version, but the
6198 incoming $t9 has the low bit set. */
6199 else if (r_type == R_MICROMIPS_HI16)
6200 value = mips_elf_high (addend + gp - p - 1);
6201 else
6202 value = mips_elf_high (addend + gp - p);
6204 break;
6206 case R_MIPS_LO16:
6207 case R_MIPS16_LO16:
6208 case R_MICROMIPS_LO16:
6209 case R_MICROMIPS_HI0_LO16:
6210 if (!gp_disp_p)
6211 value = (symbol + addend) & howto->dst_mask;
6212 else
6214 /* See the comment for R_MIPS16_HI16 above for the reason
6215 for this conditional. */
6216 if (r_type == R_MIPS16_LO16)
6217 value = addend + gp - (p & ~(bfd_vma) 0x3);
6218 else if (r_type == R_MICROMIPS_LO16
6219 || r_type == R_MICROMIPS_HI0_LO16)
6220 value = addend + gp - p + 3;
6221 else
6222 value = addend + gp - p + 4;
6223 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6224 for overflow. But, on, say, IRIX5, relocations against
6225 _gp_disp are normally generated from the .cpload
6226 pseudo-op. It generates code that normally looks like
6227 this:
6229 lui $gp,%hi(_gp_disp)
6230 addiu $gp,$gp,%lo(_gp_disp)
6231 addu $gp,$gp,$t9
6233 Here $t9 holds the address of the function being called,
6234 as required by the MIPS ELF ABI. The R_MIPS_LO16
6235 relocation can easily overflow in this situation, but the
6236 R_MIPS_HI16 relocation will handle the overflow.
6237 Therefore, we consider this a bug in the MIPS ABI, and do
6238 not check for overflow here. */
6240 break;
6242 case R_MIPS_LITERAL:
6243 case R_MICROMIPS_LITERAL:
6244 /* Because we don't merge literal sections, we can handle this
6245 just like R_MIPS_GPREL16. In the long run, we should merge
6246 shared literals, and then we will need to additional work
6247 here. */
6249 /* Fall through. */
6251 case R_MIPS16_GPREL:
6252 /* The R_MIPS16_GPREL performs the same calculation as
6253 R_MIPS_GPREL16, but stores the relocated bits in a different
6254 order. We don't need to do anything special here; the
6255 differences are handled in mips_elf_perform_relocation. */
6256 case R_MIPS_GPREL16:
6257 case R_MICROMIPS_GPREL7_S2:
6258 case R_MICROMIPS_GPREL16:
6260 int bits = howto->bitsize + howto->rightshift;
6261 /* Only sign-extend the addend if it was extracted from the
6262 instruction. If the addend was separate, leave it alone,
6263 otherwise we may lose significant bits. */
6264 if (howto->partial_inplace)
6265 addend = _bfd_mips_elf_sign_extend (addend, bits);
6266 value = symbol + addend - gp;
6267 /* If the symbol was local, any earlier relocatable links will
6268 have adjusted its addend with the gp offset, so compensate
6269 for that now. Don't do it for symbols forced local in this
6270 link, though, since they won't have had the gp offset applied
6271 to them before. */
6272 if (was_local_p)
6273 value += gp0;
6274 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6275 overflowed_p = mips_elf_overflow_p (value, bits);
6277 break;
6279 case R_MIPS16_GOT16:
6280 case R_MIPS16_CALL16:
6281 case R_MIPS_GOT16:
6282 case R_MIPS_CALL16:
6283 case R_MICROMIPS_GOT16:
6284 case R_MICROMIPS_CALL16:
6285 /* VxWorks does not have separate local and global semantics for
6286 R_MIPS*_GOT16; every relocation evaluates to "G". */
6287 if (htab->root.target_os != is_vxworks && local_p)
6289 value = mips_elf_got16_entry (abfd, input_bfd, info,
6290 symbol + addend, !was_local_p);
6291 if (value == MINUS_ONE)
6292 return bfd_reloc_outofrange;
6293 value
6294 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6295 overflowed_p = mips_elf_overflow_p (value, 16);
6296 break;
6299 /* Fall through. */
6301 case R_MIPS_TLS_GD:
6302 case R_MIPS_TLS_GOTTPREL:
6303 case R_MIPS_TLS_LDM:
6304 case R_MIPS_GOT_DISP:
6305 case R_MIPS16_TLS_GD:
6306 case R_MIPS16_TLS_GOTTPREL:
6307 case R_MIPS16_TLS_LDM:
6308 case R_MICROMIPS_TLS_GD:
6309 case R_MICROMIPS_TLS_GOTTPREL:
6310 case R_MICROMIPS_TLS_LDM:
6311 case R_MICROMIPS_GOT_DISP:
6312 value = g;
6313 overflowed_p = mips_elf_overflow_p (value, 16);
6314 break;
6316 case R_MIPS_GPREL32:
6317 value = (addend + symbol + gp0 - gp);
6318 if (!save_addend)
6319 value &= howto->dst_mask;
6320 break;
6322 case R_MIPS_PC16:
6323 case R_MIPS_GNU_REL16_S2:
6324 if (howto->partial_inplace)
6325 addend = _bfd_mips_elf_sign_extend (addend, 18);
6327 /* No need to exclude weak undefined symbols here as they resolve
6328 to 0 and never set `*cross_mode_jump_p', so this alignment check
6329 will never trigger for them. */
6330 if (*cross_mode_jump_p
6331 ? ((symbol + addend) & 3) != 1
6332 : ((symbol + addend) & 3) != 0)
6333 return bfd_reloc_outofrange;
6335 value = symbol + addend - p;
6336 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6337 overflowed_p = mips_elf_overflow_p (value, 18);
6338 value >>= howto->rightshift;
6339 value &= howto->dst_mask;
6340 break;
6342 case R_MIPS16_PC16_S1:
6343 if (howto->partial_inplace)
6344 addend = _bfd_mips_elf_sign_extend (addend, 17);
6346 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6347 && (*cross_mode_jump_p
6348 ? ((symbol + addend) & 3) != 0
6349 : ((symbol + addend) & 1) == 0))
6350 return bfd_reloc_outofrange;
6352 value = symbol + addend - p;
6353 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6354 overflowed_p = mips_elf_overflow_p (value, 17);
6355 value >>= howto->rightshift;
6356 value &= howto->dst_mask;
6357 break;
6359 case R_MIPS_PC21_S2:
6360 if (howto->partial_inplace)
6361 addend = _bfd_mips_elf_sign_extend (addend, 23);
6363 if ((symbol + addend) & 3)
6364 return bfd_reloc_outofrange;
6366 value = symbol + addend - p;
6367 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6368 overflowed_p = mips_elf_overflow_p (value, 23);
6369 value >>= howto->rightshift;
6370 value &= howto->dst_mask;
6371 break;
6373 case R_MIPS_PC26_S2:
6374 if (howto->partial_inplace)
6375 addend = _bfd_mips_elf_sign_extend (addend, 28);
6377 if ((symbol + addend) & 3)
6378 return bfd_reloc_outofrange;
6380 value = symbol + addend - p;
6381 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6382 overflowed_p = mips_elf_overflow_p (value, 28);
6383 value >>= howto->rightshift;
6384 value &= howto->dst_mask;
6385 break;
6387 case R_MIPS_PC18_S3:
6388 if (howto->partial_inplace)
6389 addend = _bfd_mips_elf_sign_extend (addend, 21);
6391 if ((symbol + addend) & 7)
6392 return bfd_reloc_outofrange;
6394 value = symbol + addend - ((p | 7) ^ 7);
6395 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6396 overflowed_p = mips_elf_overflow_p (value, 21);
6397 value >>= howto->rightshift;
6398 value &= howto->dst_mask;
6399 break;
6401 case R_MIPS_PC19_S2:
6402 if (howto->partial_inplace)
6403 addend = _bfd_mips_elf_sign_extend (addend, 21);
6405 if ((symbol + addend) & 3)
6406 return bfd_reloc_outofrange;
6408 value = symbol + addend - p;
6409 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6410 overflowed_p = mips_elf_overflow_p (value, 21);
6411 value >>= howto->rightshift;
6412 value &= howto->dst_mask;
6413 break;
6415 case R_MIPS_PCHI16:
6416 value = mips_elf_high (symbol + addend - p);
6417 value &= howto->dst_mask;
6418 break;
6420 case R_MIPS_PCLO16:
6421 if (howto->partial_inplace)
6422 addend = _bfd_mips_elf_sign_extend (addend, 16);
6423 value = symbol + addend - p;
6424 value &= howto->dst_mask;
6425 break;
6427 case R_MICROMIPS_PC7_S1:
6428 if (howto->partial_inplace)
6429 addend = _bfd_mips_elf_sign_extend (addend, 8);
6431 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6432 && (*cross_mode_jump_p
6433 ? ((symbol + addend + 2) & 3) != 0
6434 : ((symbol + addend + 2) & 1) == 0))
6435 return bfd_reloc_outofrange;
6437 value = symbol + addend - p;
6438 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6439 overflowed_p = mips_elf_overflow_p (value, 8);
6440 value >>= howto->rightshift;
6441 value &= howto->dst_mask;
6442 break;
6444 case R_MICROMIPS_PC10_S1:
6445 if (howto->partial_inplace)
6446 addend = _bfd_mips_elf_sign_extend (addend, 11);
6448 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6449 && (*cross_mode_jump_p
6450 ? ((symbol + addend + 2) & 3) != 0
6451 : ((symbol + addend + 2) & 1) == 0))
6452 return bfd_reloc_outofrange;
6454 value = symbol + addend - p;
6455 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6456 overflowed_p = mips_elf_overflow_p (value, 11);
6457 value >>= howto->rightshift;
6458 value &= howto->dst_mask;
6459 break;
6461 case R_MICROMIPS_PC16_S1:
6462 if (howto->partial_inplace)
6463 addend = _bfd_mips_elf_sign_extend (addend, 17);
6465 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6466 && (*cross_mode_jump_p
6467 ? ((symbol + addend) & 3) != 0
6468 : ((symbol + addend) & 1) == 0))
6469 return bfd_reloc_outofrange;
6471 value = symbol + addend - p;
6472 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6473 overflowed_p = mips_elf_overflow_p (value, 17);
6474 value >>= howto->rightshift;
6475 value &= howto->dst_mask;
6476 break;
6478 case R_MICROMIPS_PC23_S2:
6479 if (howto->partial_inplace)
6480 addend = _bfd_mips_elf_sign_extend (addend, 25);
6481 value = symbol + addend - ((p | 3) ^ 3);
6482 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6483 overflowed_p = mips_elf_overflow_p (value, 25);
6484 value >>= howto->rightshift;
6485 value &= howto->dst_mask;
6486 break;
6488 case R_MIPS_GOT_HI16:
6489 case R_MIPS_CALL_HI16:
6490 case R_MICROMIPS_GOT_HI16:
6491 case R_MICROMIPS_CALL_HI16:
6492 /* We're allowed to handle these two relocations identically.
6493 The dynamic linker is allowed to handle the CALL relocations
6494 differently by creating a lazy evaluation stub. */
6495 value = g;
6496 value = mips_elf_high (value);
6497 value &= howto->dst_mask;
6498 break;
6500 case R_MIPS_GOT_LO16:
6501 case R_MIPS_CALL_LO16:
6502 case R_MICROMIPS_GOT_LO16:
6503 case R_MICROMIPS_CALL_LO16:
6504 value = g & howto->dst_mask;
6505 break;
6507 case R_MIPS_GOT_PAGE:
6508 case R_MICROMIPS_GOT_PAGE:
6509 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6510 if (value == MINUS_ONE)
6511 return bfd_reloc_outofrange;
6512 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6513 overflowed_p = mips_elf_overflow_p (value, 16);
6514 break;
6516 case R_MIPS_GOT_OFST:
6517 case R_MICROMIPS_GOT_OFST:
6518 if (local_p)
6519 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6520 else
6521 value = addend;
6522 overflowed_p = mips_elf_overflow_p (value, 16);
6523 break;
6525 case R_MIPS_SUB:
6526 case R_MICROMIPS_SUB:
6527 value = symbol - addend;
6528 value &= howto->dst_mask;
6529 break;
6531 case R_MIPS_HIGHER:
6532 case R_MICROMIPS_HIGHER:
6533 value = mips_elf_higher (addend + symbol);
6534 value &= howto->dst_mask;
6535 break;
6537 case R_MIPS_HIGHEST:
6538 case R_MICROMIPS_HIGHEST:
6539 value = mips_elf_highest (addend + symbol);
6540 value &= howto->dst_mask;
6541 break;
6543 case R_MIPS_SCN_DISP:
6544 case R_MICROMIPS_SCN_DISP:
6545 value = symbol + addend - sec->output_offset;
6546 value &= howto->dst_mask;
6547 break;
6549 case R_MIPS_JALR:
6550 case R_MICROMIPS_JALR:
6551 /* This relocation is only a hint. In some cases, we optimize
6552 it into a bal instruction. But we don't try to optimize
6553 when the symbol does not resolve locally. */
6554 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6555 return bfd_reloc_continue;
6556 /* We can't optimize cross-mode jumps either. */
6557 if (*cross_mode_jump_p)
6558 return bfd_reloc_continue;
6559 value = symbol + addend;
6560 /* Neither we can non-instruction-aligned targets. */
6561 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6562 return bfd_reloc_continue;
6563 break;
6565 case R_MIPS_PJUMP:
6566 case R_MIPS_GNU_VTINHERIT:
6567 case R_MIPS_GNU_VTENTRY:
6568 /* We don't do anything with these at present. */
6569 return bfd_reloc_continue;
6571 default:
6572 /* An unrecognized relocation type. */
6573 return bfd_reloc_notsupported;
6576 /* Store the VALUE for our caller. */
6577 *valuep = value;
6578 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6581 /* It has been determined that the result of the RELOCATION is the
6582 VALUE. Use HOWTO to place VALUE into the output file at the
6583 appropriate position. The SECTION is the section to which the
6584 relocation applies.
6585 CROSS_MODE_JUMP_P is true if the relocation field
6586 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6588 Returns FALSE if anything goes wrong. */
6590 static bool
6591 mips_elf_perform_relocation (struct bfd_link_info *info,
6592 reloc_howto_type *howto,
6593 const Elf_Internal_Rela *relocation,
6594 bfd_vma value, bfd *input_bfd,
6595 asection *input_section, bfd_byte *contents,
6596 bool cross_mode_jump_p)
6598 bfd_vma x;
6599 bfd_byte *location;
6600 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6602 /* Figure out where the relocation is occurring. */
6603 location = contents + relocation->r_offset;
6605 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6607 /* Obtain the current value. */
6608 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6610 /* Clear the field we are setting. */
6611 x &= ~howto->dst_mask;
6613 /* Set the field. */
6614 x |= (value & howto->dst_mask);
6616 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6617 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6619 bfd_vma opcode = x >> 26;
6621 if (r_type == R_MIPS16_26 ? opcode == 0x7
6622 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6623 : opcode == 0x1d)
6625 info->callbacks->einfo
6626 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6627 input_bfd, input_section, relocation->r_offset);
6628 return true;
6631 if (cross_mode_jump_p && jal_reloc_p (r_type))
6633 bool ok;
6634 bfd_vma opcode = x >> 26;
6635 bfd_vma jalx_opcode;
6637 /* Check to see if the opcode is already JAL or JALX. */
6638 if (r_type == R_MIPS16_26)
6640 ok = ((opcode == 0x6) || (opcode == 0x7));
6641 jalx_opcode = 0x7;
6643 else if (r_type == R_MICROMIPS_26_S1)
6645 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6646 jalx_opcode = 0x3c;
6648 else
6650 ok = ((opcode == 0x3) || (opcode == 0x1d));
6651 jalx_opcode = 0x1d;
6654 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6655 convert J or JALS to JALX. */
6656 if (!ok)
6658 info->callbacks->einfo
6659 (_("%X%H: unsupported jump between ISA modes; "
6660 "consider recompiling with interlinking enabled\n"),
6661 input_bfd, input_section, relocation->r_offset);
6662 return true;
6665 /* Make this the JALX opcode. */
6666 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6668 else if (cross_mode_jump_p && b_reloc_p (r_type))
6670 bool ok = false;
6671 bfd_vma opcode = x >> 16;
6672 bfd_vma jalx_opcode = 0;
6673 bfd_vma sign_bit = 0;
6674 bfd_vma addr;
6675 bfd_vma dest;
6677 if (r_type == R_MICROMIPS_PC16_S1)
6679 ok = opcode == 0x4060;
6680 jalx_opcode = 0x3c;
6681 sign_bit = 0x10000;
6682 value <<= 1;
6684 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6686 ok = opcode == 0x411;
6687 jalx_opcode = 0x1d;
6688 sign_bit = 0x20000;
6689 value <<= 2;
6692 if (ok && !bfd_link_pic (info))
6694 addr = (input_section->output_section->vma
6695 + input_section->output_offset
6696 + relocation->r_offset
6697 + 4);
6698 dest = (addr
6699 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6701 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6703 info->callbacks->einfo
6704 (_("%X%H: cannot convert branch between ISA modes "
6705 "to JALX: relocation out of range\n"),
6706 input_bfd, input_section, relocation->r_offset);
6707 return true;
6710 /* Make this the JALX opcode. */
6711 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6713 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6715 info->callbacks->einfo
6716 (_("%X%H: unsupported branch between ISA modes\n"),
6717 input_bfd, input_section, relocation->r_offset);
6718 return true;
6722 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6723 range. */
6724 if (!bfd_link_relocatable (info)
6725 && !cross_mode_jump_p
6726 && ((JAL_TO_BAL_P (input_bfd)
6727 && r_type == R_MIPS_26
6728 && (x >> 26) == 0x3) /* jal addr */
6729 || (JALR_TO_BAL_P (input_bfd)
6730 && r_type == R_MIPS_JALR
6731 && x == 0x0320f809) /* jalr t9 */
6732 || (JR_TO_B_P (input_bfd)
6733 && r_type == R_MIPS_JALR
6734 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6736 bfd_vma addr;
6737 bfd_vma dest;
6738 bfd_signed_vma off;
6740 addr = (input_section->output_section->vma
6741 + input_section->output_offset
6742 + relocation->r_offset
6743 + 4);
6744 if (r_type == R_MIPS_26)
6745 dest = (value << 2) | ((addr >> 28) << 28);
6746 else
6747 dest = value;
6748 off = dest - addr;
6749 if (off <= 0x1ffff && off >= -0x20000)
6751 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6752 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6753 else
6754 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6758 /* Put the value into the output. */
6759 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6761 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6762 location);
6764 return true;
6767 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6768 is the original relocation, which is now being transformed into a
6769 dynamic relocation. The ADDENDP is adjusted if necessary; the
6770 caller should store the result in place of the original addend. */
6772 static bool
6773 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6774 struct bfd_link_info *info,
6775 const Elf_Internal_Rela *rel,
6776 struct mips_elf_link_hash_entry *h,
6777 asection *sec, bfd_vma symbol,
6778 bfd_vma *addendp, asection *input_section)
6780 Elf_Internal_Rela outrel[3];
6781 asection *sreloc;
6782 bfd *dynobj;
6783 int r_type;
6784 long indx;
6785 bool defined_p;
6786 struct mips_elf_link_hash_table *htab;
6788 htab = mips_elf_hash_table (info);
6789 BFD_ASSERT (htab != NULL);
6791 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6792 dynobj = elf_hash_table (info)->dynobj;
6793 sreloc = mips_elf_rel_dyn_section (info, false);
6794 BFD_ASSERT (sreloc != NULL);
6795 BFD_ASSERT (sreloc->contents != NULL);
6796 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6797 < sreloc->size);
6799 outrel[0].r_offset =
6800 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6801 if (ABI_64_P (output_bfd))
6803 outrel[1].r_offset =
6804 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6805 outrel[2].r_offset =
6806 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6809 if (outrel[0].r_offset == MINUS_ONE)
6810 /* The relocation field has been deleted. */
6811 return true;
6813 if (outrel[0].r_offset == MINUS_TWO)
6815 /* The relocation field has been converted into a relative value of
6816 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6817 the field to be fully relocated, so add in the symbol's value. */
6818 *addendp += symbol;
6819 return true;
6822 /* We must now calculate the dynamic symbol table index to use
6823 in the relocation. */
6824 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6826 BFD_ASSERT (htab->root.target_os == is_vxworks
6827 || h->global_got_area != GGA_NONE);
6828 indx = h->root.dynindx;
6829 if (SGI_COMPAT (output_bfd))
6830 defined_p = h->root.def_regular;
6831 else
6832 /* ??? glibc's ld.so just adds the final GOT entry to the
6833 relocation field. It therefore treats relocs against
6834 defined symbols in the same way as relocs against
6835 undefined symbols. */
6836 defined_p = false;
6838 else
6840 if (sec != NULL && bfd_is_abs_section (sec))
6841 indx = 0;
6842 else if (sec == NULL || sec->owner == NULL)
6844 BFD_ASSERT (0);
6845 bfd_set_error (bfd_error_bad_value);
6846 return false;
6848 else
6850 indx = elf_section_data (sec->output_section)->dynindx;
6851 if (indx == 0)
6853 asection *osec = htab->root.text_index_section;
6854 indx = elf_section_data (osec)->dynindx;
6856 if (indx == 0)
6857 abort ();
6860 /* Instead of generating a relocation using the section
6861 symbol, we may as well make it a fully relative
6862 relocation. We want to avoid generating relocations to
6863 local symbols because we used to generate them
6864 incorrectly, without adding the original symbol value,
6865 which is mandated by the ABI for section symbols. In
6866 order to give dynamic loaders and applications time to
6867 phase out the incorrect use, we refrain from emitting
6868 section-relative relocations. It's not like they're
6869 useful, after all. This should be a bit more efficient
6870 as well. */
6871 /* ??? Although this behavior is compatible with glibc's ld.so,
6872 the ABI says that relocations against STN_UNDEF should have
6873 a symbol value of 0. Irix rld honors this, so relocations
6874 against STN_UNDEF have no effect. */
6875 if (!SGI_COMPAT (output_bfd))
6876 indx = 0;
6877 defined_p = true;
6880 /* If the relocation was previously an absolute relocation and
6881 this symbol will not be referred to by the relocation, we must
6882 adjust it by the value we give it in the dynamic symbol table.
6883 Otherwise leave the job up to the dynamic linker. */
6884 if (defined_p && r_type != R_MIPS_REL32)
6885 *addendp += symbol;
6887 if (htab->root.target_os == is_vxworks)
6888 /* VxWorks uses non-relative relocations for this. */
6889 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6890 else
6891 /* The relocation is always an REL32 relocation because we don't
6892 know where the shared library will wind up at load-time. */
6893 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6894 R_MIPS_REL32);
6896 /* For strict adherence to the ABI specification, we should
6897 generate a R_MIPS_64 relocation record by itself before the
6898 _REL32/_64 record as well, such that the addend is read in as
6899 a 64-bit value (REL32 is a 32-bit relocation, after all).
6900 However, since none of the existing ELF64 MIPS dynamic
6901 loaders seems to care, we don't waste space with these
6902 artificial relocations. If this turns out to not be true,
6903 mips_elf_allocate_dynamic_relocation() should be tweaked so
6904 as to make room for a pair of dynamic relocations per
6905 invocation if ABI_64_P, and here we should generate an
6906 additional relocation record with R_MIPS_64 by itself for a
6907 NULL symbol before this relocation record. */
6908 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6909 ABI_64_P (output_bfd)
6910 ? R_MIPS_64
6911 : R_MIPS_NONE);
6912 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6914 /* Adjust the output offset of the relocation to reference the
6915 correct location in the output file. */
6916 outrel[0].r_offset += (input_section->output_section->vma
6917 + input_section->output_offset);
6918 outrel[1].r_offset += (input_section->output_section->vma
6919 + input_section->output_offset);
6920 outrel[2].r_offset += (input_section->output_section->vma
6921 + input_section->output_offset);
6923 /* Put the relocation back out. We have to use the special
6924 relocation outputter in the 64-bit case since the 64-bit
6925 relocation format is non-standard. */
6926 if (ABI_64_P (output_bfd))
6928 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6929 (output_bfd, &outrel[0],
6930 (sreloc->contents
6931 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6933 else if (htab->root.target_os == is_vxworks)
6935 /* VxWorks uses RELA rather than REL dynamic relocations. */
6936 outrel[0].r_addend = *addendp;
6937 bfd_elf32_swap_reloca_out
6938 (output_bfd, &outrel[0],
6939 (sreloc->contents
6940 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6942 else
6943 bfd_elf32_swap_reloc_out
6944 (output_bfd, &outrel[0],
6945 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6947 /* We've now added another relocation. */
6948 ++sreloc->reloc_count;
6950 /* Make sure the output section is writable. The dynamic linker
6951 will be writing to it. */
6952 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6953 |= SHF_WRITE;
6955 /* On IRIX5, make an entry of compact relocation info. */
6956 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6958 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6959 bfd_byte *cr;
6961 if (scpt)
6963 Elf32_crinfo cptrel;
6965 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6966 cptrel.vaddr = (rel->r_offset
6967 + input_section->output_section->vma
6968 + input_section->output_offset);
6969 if (r_type == R_MIPS_REL32)
6970 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6971 else
6972 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6973 mips_elf_set_cr_dist2to (cptrel, 0);
6974 cptrel.konst = *addendp;
6976 cr = (scpt->contents
6977 + sizeof (Elf32_External_compact_rel));
6978 mips_elf_set_cr_relvaddr (cptrel, 0);
6979 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6980 ((Elf32_External_crinfo *) cr
6981 + scpt->reloc_count));
6982 ++scpt->reloc_count;
6986 /* If we've written this relocation for a readonly section,
6987 we need to set DF_TEXTREL again, so that we do not delete the
6988 DT_TEXTREL tag. */
6989 if (MIPS_ELF_READONLY_SECTION (input_section))
6990 info->flags |= DF_TEXTREL;
6992 return true;
6995 /* Return the MACH for a MIPS e_flags value. */
6997 unsigned long
6998 _bfd_elf_mips_mach (flagword flags)
7000 switch (flags & EF_MIPS_MACH)
7002 case EF_MIPS_MACH_3900:
7003 return bfd_mach_mips3900;
7005 case EF_MIPS_MACH_4010:
7006 return bfd_mach_mips4010;
7008 case EF_MIPS_MACH_ALLEGREX:
7009 return bfd_mach_mips_allegrex;
7011 case EF_MIPS_MACH_4100:
7012 return bfd_mach_mips4100;
7014 case EF_MIPS_MACH_4111:
7015 return bfd_mach_mips4111;
7017 case EF_MIPS_MACH_4120:
7018 return bfd_mach_mips4120;
7020 case EF_MIPS_MACH_4650:
7021 return bfd_mach_mips4650;
7023 case EF_MIPS_MACH_5400:
7024 return bfd_mach_mips5400;
7026 case EF_MIPS_MACH_5500:
7027 return bfd_mach_mips5500;
7029 case EF_MIPS_MACH_5900:
7030 return bfd_mach_mips5900;
7032 case EF_MIPS_MACH_9000:
7033 return bfd_mach_mips9000;
7035 case EF_MIPS_MACH_SB1:
7036 return bfd_mach_mips_sb1;
7038 case EF_MIPS_MACH_LS2E:
7039 return bfd_mach_mips_loongson_2e;
7041 case EF_MIPS_MACH_LS2F:
7042 return bfd_mach_mips_loongson_2f;
7044 case EF_MIPS_MACH_GS464:
7045 return bfd_mach_mips_gs464;
7047 case EF_MIPS_MACH_GS464E:
7048 return bfd_mach_mips_gs464e;
7050 case EF_MIPS_MACH_GS264E:
7051 return bfd_mach_mips_gs264e;
7053 case EF_MIPS_MACH_OCTEON3:
7054 return bfd_mach_mips_octeon3;
7056 case EF_MIPS_MACH_OCTEON2:
7057 return bfd_mach_mips_octeon2;
7059 case EF_MIPS_MACH_OCTEON:
7060 return bfd_mach_mips_octeon;
7062 case EF_MIPS_MACH_XLR:
7063 return bfd_mach_mips_xlr;
7065 case EF_MIPS_MACH_IAMR2:
7066 return bfd_mach_mips_interaptiv_mr2;
7068 default:
7069 switch (flags & EF_MIPS_ARCH)
7071 default:
7072 case EF_MIPS_ARCH_1:
7073 return bfd_mach_mips3000;
7075 case EF_MIPS_ARCH_2:
7076 return bfd_mach_mips6000;
7078 case EF_MIPS_ARCH_3:
7079 return bfd_mach_mips4000;
7081 case EF_MIPS_ARCH_4:
7082 return bfd_mach_mips8000;
7084 case EF_MIPS_ARCH_5:
7085 return bfd_mach_mips5;
7087 case EF_MIPS_ARCH_32:
7088 return bfd_mach_mipsisa32;
7090 case EF_MIPS_ARCH_64:
7091 return bfd_mach_mipsisa64;
7093 case EF_MIPS_ARCH_32R2:
7094 return bfd_mach_mipsisa32r2;
7096 case EF_MIPS_ARCH_64R2:
7097 return bfd_mach_mipsisa64r2;
7099 case EF_MIPS_ARCH_32R6:
7100 return bfd_mach_mipsisa32r6;
7102 case EF_MIPS_ARCH_64R6:
7103 return bfd_mach_mipsisa64r6;
7107 return 0;
7110 /* Return printable name for ABI. */
7112 static inline char *
7113 elf_mips_abi_name (bfd *abfd)
7115 flagword flags;
7117 flags = elf_elfheader (abfd)->e_flags;
7118 switch (flags & EF_MIPS_ABI)
7120 case 0:
7121 if (ABI_N32_P (abfd))
7122 return "N32";
7123 else if (ABI_64_P (abfd))
7124 return "64";
7125 else
7126 return "none";
7127 case EF_MIPS_ABI_O32:
7128 return "O32";
7129 case EF_MIPS_ABI_O64:
7130 return "O64";
7131 case EF_MIPS_ABI_EABI32:
7132 return "EABI32";
7133 case EF_MIPS_ABI_EABI64:
7134 return "EABI64";
7135 default:
7136 return "unknown abi";
7140 /* MIPS ELF uses two common sections. One is the usual one, and the
7141 other is for small objects. All the small objects are kept
7142 together, and then referenced via the gp pointer, which yields
7143 faster assembler code. This is what we use for the small common
7144 section. This approach is copied from ecoff.c. */
7145 static asection mips_elf_scom_section;
7146 static const asymbol mips_elf_scom_symbol =
7147 GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7148 static asection mips_elf_scom_section =
7149 BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7150 ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7152 /* MIPS ELF also uses an acommon section, which represents an
7153 allocated common symbol which may be overridden by a
7154 definition in a shared library. */
7155 static asection mips_elf_acom_section;
7156 static const asymbol mips_elf_acom_symbol =
7157 GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7158 static asection mips_elf_acom_section =
7159 BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7160 ".acommon", 0, SEC_ALLOC);
7162 /* This is used for both the 32-bit and the 64-bit ABI. */
7164 void
7165 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7167 elf_symbol_type *elfsym;
7169 /* Handle the special MIPS section numbers that a symbol may use. */
7170 elfsym = (elf_symbol_type *) asym;
7171 switch (elfsym->internal_elf_sym.st_shndx)
7173 case SHN_MIPS_ACOMMON:
7174 /* This section is used in a dynamically linked executable file.
7175 It is an allocated common section. The dynamic linker can
7176 either resolve these symbols to something in a shared
7177 library, or it can just leave them here. For our purposes,
7178 we can consider these symbols to be in a new section. */
7179 asym->section = &mips_elf_acom_section;
7180 break;
7182 case SHN_COMMON:
7183 /* Common symbols less than the GP size are automatically
7184 treated as SHN_MIPS_SCOMMON symbols, with some exceptions. */
7185 if (asym->value > elf_gp_size (abfd)
7186 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7187 || IRIX_COMPAT (abfd) == ict_irix6
7188 || strcmp (asym->name, "__gnu_lto_slim") == 0)
7189 break;
7190 /* Fall through. */
7191 case SHN_MIPS_SCOMMON:
7192 asym->section = &mips_elf_scom_section;
7193 asym->value = elfsym->internal_elf_sym.st_size;
7194 break;
7196 case SHN_MIPS_SUNDEFINED:
7197 asym->section = bfd_und_section_ptr;
7198 break;
7200 case SHN_MIPS_TEXT:
7202 asection *section = bfd_get_section_by_name (abfd, ".text");
7204 if (section != NULL)
7206 asym->section = section;
7207 /* MIPS_TEXT is a bit special, the address is not an offset
7208 to the base of the .text section. So subtract the section
7209 base address to make it an offset. */
7210 asym->value -= section->vma;
7213 break;
7215 case SHN_MIPS_DATA:
7217 asection *section = bfd_get_section_by_name (abfd, ".data");
7219 if (section != NULL)
7221 asym->section = section;
7222 /* MIPS_DATA is a bit special, the address is not an offset
7223 to the base of the .data section. So subtract the section
7224 base address to make it an offset. */
7225 asym->value -= section->vma;
7228 break;
7231 /* If this is an odd-valued function symbol, assume it's a MIPS16
7232 or microMIPS one. */
7233 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7234 && (asym->value & 1) != 0)
7236 asym->value--;
7237 if (MICROMIPS_P (abfd))
7238 elfsym->internal_elf_sym.st_other
7239 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7240 else
7241 elfsym->internal_elf_sym.st_other
7242 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7246 /* Implement elf_backend_eh_frame_address_size. This differs from
7247 the default in the way it handles EABI64.
7249 EABI64 was originally specified as an LP64 ABI, and that is what
7250 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7251 historically accepted the combination of -mabi=eabi and -mlong32,
7252 and this ILP32 variation has become semi-official over time.
7253 Both forms use elf32 and have pointer-sized FDE addresses.
7255 If an EABI object was generated by GCC 4.0 or above, it will have
7256 an empty .gcc_compiled_longXX section, where XX is the size of longs
7257 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7258 have no special marking to distinguish them from LP64 objects.
7260 We don't want users of the official LP64 ABI to be punished for the
7261 existence of the ILP32 variant, but at the same time, we don't want
7262 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7263 We therefore take the following approach:
7265 - If ABFD contains a .gcc_compiled_longXX section, use it to
7266 determine the pointer size.
7268 - Otherwise check the type of the first relocation. Assume that
7269 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7271 - Otherwise punt.
7273 The second check is enough to detect LP64 objects generated by pre-4.0
7274 compilers because, in the kind of output generated by those compilers,
7275 the first relocation will be associated with either a CIE personality
7276 routine or an FDE start address. Furthermore, the compilers never
7277 used a special (non-pointer) encoding for this ABI.
7279 Checking the relocation type should also be safe because there is no
7280 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7281 did so. */
7283 unsigned int
7284 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7286 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7287 return 8;
7288 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
7290 bool long32_p, long64_p;
7292 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7293 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7294 if (long32_p && long64_p)
7295 return 0;
7296 if (long32_p)
7297 return 4;
7298 if (long64_p)
7299 return 8;
7301 if (sec->reloc_count > 0)
7303 /* Load the relocations for this section. */
7304 Elf_Internal_Rela *internal_relocs =
7305 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
7306 if (internal_relocs == NULL)
7307 return 0;
7309 unsigned int size = 0;
7310 if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
7311 size = 8;
7313 if (elf_section_data (sec)->relocs != internal_relocs)
7314 free (internal_relocs);
7316 return size;
7319 return 0;
7321 return 4;
7324 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7325 relocations against two unnamed section symbols to resolve to the
7326 same address. For example, if we have code like:
7328 lw $4,%got_disp(.data)($gp)
7329 lw $25,%got_disp(.text)($gp)
7330 jalr $25
7332 then the linker will resolve both relocations to .data and the program
7333 will jump there rather than to .text.
7335 We can work around this problem by giving names to local section symbols.
7336 This is also what the MIPSpro tools do. */
7338 bool
7339 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7341 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7344 /* Work over a section just before writing it out. This routine is
7345 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7346 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7347 a better way. */
7349 bool
7350 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7352 if (hdr->sh_type == SHT_MIPS_REGINFO
7353 && hdr->sh_size > 0)
7355 bfd_byte buf[4];
7357 BFD_ASSERT (hdr->contents == NULL);
7359 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7361 _bfd_error_handler
7362 (_("%pB: incorrect `.reginfo' section size; "
7363 "expected %" PRIu64 ", got %" PRIu64),
7364 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7365 (uint64_t) hdr->sh_size);
7366 bfd_set_error (bfd_error_bad_value);
7367 return false;
7370 if (bfd_seek (abfd,
7371 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7372 SEEK_SET) != 0)
7373 return false;
7374 H_PUT_32 (abfd, elf_gp (abfd), buf);
7375 if (bfd_write (buf, 4, abfd) != 4)
7376 return false;
7379 if (hdr->sh_type == SHT_MIPS_OPTIONS
7380 && hdr->bfd_section != NULL
7381 && mips_elf_section_data (hdr->bfd_section) != NULL
7382 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7384 bfd_byte *contents, *l, *lend;
7386 /* We stored the section contents in the tdata field in the
7387 set_section_contents routine. We save the section contents
7388 so that we don't have to read them again.
7389 At this point we know that elf_gp is set, so we can look
7390 through the section contents to see if there is an
7391 ODK_REGINFO structure. */
7393 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7394 l = contents;
7395 lend = contents + hdr->sh_size;
7396 while (l + sizeof (Elf_External_Options) <= lend)
7398 Elf_Internal_Options intopt;
7400 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7401 &intopt);
7402 if (intopt.size < sizeof (Elf_External_Options))
7404 _bfd_error_handler
7405 /* xgettext:c-format */
7406 (_("%pB: warning: bad `%s' option size %u smaller than"
7407 " its header"),
7408 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7409 break;
7411 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7413 bfd_byte buf[8];
7415 if (bfd_seek (abfd,
7416 (hdr->sh_offset
7417 + (l - contents)
7418 + sizeof (Elf_External_Options)
7419 + (sizeof (Elf64_External_RegInfo) - 8)),
7420 SEEK_SET) != 0)
7421 return false;
7422 H_PUT_64 (abfd, elf_gp (abfd), buf);
7423 if (bfd_write (buf, 8, abfd) != 8)
7424 return false;
7426 else if (intopt.kind == ODK_REGINFO)
7428 bfd_byte buf[4];
7430 if (bfd_seek (abfd,
7431 (hdr->sh_offset
7432 + (l - contents)
7433 + sizeof (Elf_External_Options)
7434 + (sizeof (Elf32_External_RegInfo) - 4)),
7435 SEEK_SET) != 0)
7436 return false;
7437 H_PUT_32 (abfd, elf_gp (abfd), buf);
7438 if (bfd_write (buf, 4, abfd) != 4)
7439 return false;
7441 l += intopt.size;
7445 if (hdr->bfd_section != NULL)
7447 const char *name = bfd_section_name (hdr->bfd_section);
7449 /* .sbss is not handled specially here because the GNU/Linux
7450 prelinker can convert .sbss from NOBITS to PROGBITS and
7451 changing it back to NOBITS breaks the binary. The entry in
7452 _bfd_mips_elf_special_sections will ensure the correct flags
7453 are set on .sbss if BFD creates it without reading it from an
7454 input file, and without special handling here the flags set
7455 on it in an input file will be followed. */
7456 if (strcmp (name, ".sdata") == 0
7457 || strcmp (name, ".lit8") == 0
7458 || strcmp (name, ".lit4") == 0)
7459 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7460 else if (strcmp (name, ".srdata") == 0)
7461 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7462 else if (strcmp (name, ".compact_rel") == 0)
7463 hdr->sh_flags = 0;
7464 else if (strcmp (name, ".rtproc") == 0)
7466 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7468 unsigned int adjust;
7470 adjust = hdr->sh_size % hdr->sh_addralign;
7471 if (adjust != 0)
7472 hdr->sh_size += hdr->sh_addralign - adjust;
7477 return true;
7480 /* Handle a MIPS specific section when reading an object file. This
7481 is called when elfcode.h finds a section with an unknown type.
7482 This routine supports both the 32-bit and 64-bit ELF ABI. */
7484 bool
7485 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7486 Elf_Internal_Shdr *hdr,
7487 const char *name,
7488 int shindex)
7490 flagword flags = 0;
7492 /* There ought to be a place to keep ELF backend specific flags, but
7493 at the moment there isn't one. We just keep track of the
7494 sections by their name, instead. Fortunately, the ABI gives
7495 suggested names for all the MIPS specific sections, so we will
7496 probably get away with this. */
7497 switch (hdr->sh_type)
7499 case SHT_MIPS_LIBLIST:
7500 if (strcmp (name, ".liblist") != 0)
7501 return false;
7502 break;
7503 case SHT_MIPS_MSYM:
7504 if (strcmp (name, ".msym") != 0)
7505 return false;
7506 break;
7507 case SHT_MIPS_CONFLICT:
7508 if (strcmp (name, ".conflict") != 0)
7509 return false;
7510 break;
7511 case SHT_MIPS_GPTAB:
7512 if (! startswith (name, ".gptab."))
7513 return false;
7514 break;
7515 case SHT_MIPS_UCODE:
7516 if (strcmp (name, ".ucode") != 0)
7517 return false;
7518 break;
7519 case SHT_MIPS_DEBUG:
7520 if (strcmp (name, ".mdebug") != 0)
7521 return false;
7522 flags = SEC_DEBUGGING;
7523 break;
7524 case SHT_MIPS_REGINFO:
7525 if (strcmp (name, ".reginfo") != 0
7526 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7527 return false;
7528 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7529 break;
7530 case SHT_MIPS_IFACE:
7531 if (strcmp (name, ".MIPS.interfaces") != 0)
7532 return false;
7533 break;
7534 case SHT_MIPS_CONTENT:
7535 if (! startswith (name, ".MIPS.content"))
7536 return false;
7537 break;
7538 case SHT_MIPS_OPTIONS:
7539 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7540 return false;
7541 break;
7542 case SHT_MIPS_ABIFLAGS:
7543 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7544 return false;
7545 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7546 break;
7547 case SHT_MIPS_DWARF:
7548 if (! startswith (name, ".debug_")
7549 && ! startswith (name, ".gnu.debuglto_.debug_")
7550 && ! startswith (name, ".zdebug_")
7551 && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7552 return false;
7553 break;
7554 case SHT_MIPS_SYMBOL_LIB:
7555 if (strcmp (name, ".MIPS.symlib") != 0)
7556 return false;
7557 break;
7558 case SHT_MIPS_EVENTS:
7559 if (! startswith (name, ".MIPS.events")
7560 && ! startswith (name, ".MIPS.post_rel"))
7561 return false;
7562 break;
7563 case SHT_MIPS_XHASH:
7564 if (strcmp (name, ".MIPS.xhash") != 0)
7565 return false;
7566 default:
7567 break;
7570 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7571 return false;
7573 if (hdr->sh_flags & SHF_MIPS_GPREL)
7574 flags |= SEC_SMALL_DATA;
7576 if (flags)
7578 if (!bfd_set_section_flags (hdr->bfd_section,
7579 (bfd_section_flags (hdr->bfd_section)
7580 | flags)))
7581 return false;
7584 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7586 Elf_External_ABIFlags_v0 ext;
7588 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7589 &ext, 0, sizeof ext))
7590 return false;
7591 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7592 &mips_elf_tdata (abfd)->abiflags);
7593 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7594 return false;
7595 mips_elf_tdata (abfd)->abiflags_valid = true;
7598 /* FIXME: We should record sh_info for a .gptab section. */
7600 /* For a .reginfo section, set the gp value in the tdata information
7601 from the contents of this section. We need the gp value while
7602 processing relocs, so we just get it now. The .reginfo section
7603 is not used in the 64-bit MIPS ELF ABI. */
7604 if (hdr->sh_type == SHT_MIPS_REGINFO)
7606 Elf32_External_RegInfo ext;
7607 Elf32_RegInfo s;
7609 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7610 &ext, 0, sizeof ext))
7611 return false;
7612 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7613 elf_gp (abfd) = s.ri_gp_value;
7616 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7617 set the gp value based on what we find. We may see both
7618 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7619 they should agree. */
7620 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7622 bfd_byte *contents, *l, *lend;
7624 if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7626 free (contents);
7627 return false;
7629 l = contents;
7630 lend = contents + hdr->sh_size;
7631 while (l + sizeof (Elf_External_Options) <= lend)
7633 Elf_Internal_Options intopt;
7635 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7636 &intopt);
7637 if (intopt.size < sizeof (Elf_External_Options))
7639 bad_opt:
7640 _bfd_error_handler
7641 /* xgettext:c-format */
7642 (_("%pB: warning: truncated `%s' option"),
7643 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7644 break;
7646 if (intopt.kind == ODK_REGINFO)
7648 if (ABI_64_P (abfd))
7650 Elf64_Internal_RegInfo intreg;
7651 size_t needed = (sizeof (Elf_External_Options)
7652 + sizeof (Elf64_External_RegInfo));
7653 if (intopt.size < needed || (size_t) (lend - l) < needed)
7654 goto bad_opt;
7655 bfd_mips_elf64_swap_reginfo_in
7656 (abfd,
7657 ((Elf64_External_RegInfo *)
7658 (l + sizeof (Elf_External_Options))),
7659 &intreg);
7660 elf_gp (abfd) = intreg.ri_gp_value;
7662 else
7664 Elf32_RegInfo intreg;
7665 size_t needed = (sizeof (Elf_External_Options)
7666 + sizeof (Elf32_External_RegInfo));
7667 if (intopt.size < needed || (size_t) (lend - l) < needed)
7668 goto bad_opt;
7669 bfd_mips_elf32_swap_reginfo_in
7670 (abfd,
7671 ((Elf32_External_RegInfo *)
7672 (l + sizeof (Elf_External_Options))),
7673 &intreg);
7674 elf_gp (abfd) = intreg.ri_gp_value;
7677 l += intopt.size;
7679 free (contents);
7682 return true;
7685 /* Set the correct type for a MIPS ELF section. We do this by the
7686 section name, which is a hack, but ought to work. This routine is
7687 used by both the 32-bit and the 64-bit ABI. */
7689 bool
7690 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7692 const char *name = bfd_section_name (sec);
7694 if (strcmp (name, ".liblist") == 0)
7696 hdr->sh_type = SHT_MIPS_LIBLIST;
7697 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7698 /* The sh_link field is set in final_write_processing. */
7700 else if (strcmp (name, ".conflict") == 0)
7701 hdr->sh_type = SHT_MIPS_CONFLICT;
7702 else if (startswith (name, ".gptab."))
7704 hdr->sh_type = SHT_MIPS_GPTAB;
7705 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7706 /* The sh_info field is set in final_write_processing. */
7708 else if (strcmp (name, ".ucode") == 0)
7709 hdr->sh_type = SHT_MIPS_UCODE;
7710 else if (strcmp (name, ".mdebug") == 0)
7712 hdr->sh_type = SHT_MIPS_DEBUG;
7713 /* In a shared object on IRIX 5.3, the .mdebug section has an
7714 entsize of 0. FIXME: Does this matter? */
7715 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7716 hdr->sh_entsize = 0;
7717 else
7718 hdr->sh_entsize = 1;
7720 else if (strcmp (name, ".reginfo") == 0)
7722 hdr->sh_type = SHT_MIPS_REGINFO;
7723 /* In a shared object on IRIX 5.3, the .reginfo section has an
7724 entsize of 0x18. FIXME: Does this matter? */
7725 if (SGI_COMPAT (abfd))
7727 if ((abfd->flags & DYNAMIC) != 0)
7728 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7729 else
7730 hdr->sh_entsize = 1;
7732 else
7733 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7735 else if (SGI_COMPAT (abfd)
7736 && (strcmp (name, ".hash") == 0
7737 || strcmp (name, ".dynamic") == 0
7738 || strcmp (name, ".dynstr") == 0))
7740 if (SGI_COMPAT (abfd))
7741 hdr->sh_entsize = 0;
7742 #if 0
7743 /* This isn't how the IRIX6 linker behaves. */
7744 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7745 #endif
7747 else if (strcmp (name, ".got") == 0
7748 || strcmp (name, ".srdata") == 0
7749 || strcmp (name, ".sdata") == 0
7750 || strcmp (name, ".sbss") == 0
7751 || strcmp (name, ".lit4") == 0
7752 || strcmp (name, ".lit8") == 0)
7753 hdr->sh_flags |= SHF_MIPS_GPREL;
7754 else if (strcmp (name, ".MIPS.interfaces") == 0)
7756 hdr->sh_type = SHT_MIPS_IFACE;
7757 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7759 else if (startswith (name, ".MIPS.content"))
7761 hdr->sh_type = SHT_MIPS_CONTENT;
7762 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7763 /* The sh_info field is set in final_write_processing. */
7765 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7767 hdr->sh_type = SHT_MIPS_OPTIONS;
7768 hdr->sh_entsize = 1;
7769 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7771 else if (startswith (name, ".MIPS.abiflags"))
7773 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7774 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7776 else if (startswith (name, ".debug_")
7777 || startswith (name, ".gnu.debuglto_.debug_")
7778 || startswith (name, ".zdebug_")
7779 || startswith (name, ".gnu.debuglto_.zdebug_"))
7781 hdr->sh_type = SHT_MIPS_DWARF;
7783 /* Irix facilities such as libexc expect a single .debug_frame
7784 per executable, the system ones have NOSTRIP set and the linker
7785 doesn't merge sections with different flags so ... */
7786 if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7787 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7789 else if (strcmp (name, ".MIPS.symlib") == 0)
7791 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7792 /* The sh_link and sh_info fields are set in
7793 final_write_processing. */
7795 else if (startswith (name, ".MIPS.events")
7796 || startswith (name, ".MIPS.post_rel"))
7798 hdr->sh_type = SHT_MIPS_EVENTS;
7799 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7800 /* The sh_link field is set in final_write_processing. */
7802 else if (strcmp (name, ".msym") == 0)
7804 hdr->sh_type = SHT_MIPS_MSYM;
7805 hdr->sh_flags |= SHF_ALLOC;
7806 hdr->sh_entsize = 8;
7808 else if (strcmp (name, ".MIPS.xhash") == 0)
7810 hdr->sh_type = SHT_MIPS_XHASH;
7811 hdr->sh_flags |= SHF_ALLOC;
7812 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7815 /* The generic elf_fake_sections will set up REL_HDR using the default
7816 kind of relocations. We used to set up a second header for the
7817 non-default kind of relocations here, but only NewABI would use
7818 these, and the IRIX ld doesn't like resulting empty RELA sections.
7819 Thus we create those header only on demand now. */
7821 return true;
7824 /* Given a BFD section, try to locate the corresponding ELF section
7825 index. This is used by both the 32-bit and the 64-bit ABI.
7826 Actually, it's not clear to me that the 64-bit ABI supports these,
7827 but for non-PIC objects we will certainly want support for at least
7828 the .scommon section. */
7830 bool
7831 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7832 asection *sec, int *retval)
7834 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7836 *retval = SHN_MIPS_SCOMMON;
7837 return true;
7839 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7841 *retval = SHN_MIPS_ACOMMON;
7842 return true;
7844 return false;
7847 /* Hook called by the linker routine which adds symbols from an object
7848 file. We must handle the special MIPS section numbers here. */
7850 bool
7851 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7852 Elf_Internal_Sym *sym, const char **namep,
7853 flagword *flagsp ATTRIBUTE_UNUSED,
7854 asection **secp, bfd_vma *valp)
7856 if (SGI_COMPAT (abfd)
7857 && (abfd->flags & DYNAMIC) != 0
7858 && strcmp (*namep, "_rld_new_interface") == 0)
7860 /* Skip IRIX5 rld entry name. */
7861 *namep = NULL;
7862 return true;
7865 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7866 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7867 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7868 a magic symbol resolved by the linker, we ignore this bogus definition
7869 of _gp_disp. New ABI objects do not suffer from this problem so this
7870 is not done for them. */
7871 if (!NEWABI_P(abfd)
7872 && (sym->st_shndx == SHN_ABS)
7873 && (strcmp (*namep, "_gp_disp") == 0))
7875 *namep = NULL;
7876 return true;
7879 switch (sym->st_shndx)
7881 case SHN_COMMON:
7882 /* Common symbols less than the GP size are automatically
7883 treated as SHN_MIPS_SCOMMON symbols, with some exceptions. */
7884 if (sym->st_size > elf_gp_size (abfd)
7885 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7886 || IRIX_COMPAT (abfd) == ict_irix6
7887 || strcmp (*namep, "__gnu_lto_slim") == 0)
7888 break;
7889 /* Fall through. */
7890 case SHN_MIPS_SCOMMON:
7891 *secp = bfd_make_section_old_way (abfd, ".scommon");
7892 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7893 *valp = sym->st_size;
7894 break;
7896 case SHN_MIPS_TEXT:
7897 /* This section is used in a shared object. */
7898 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7900 asymbol *elf_text_symbol;
7901 asection *elf_text_section;
7902 size_t amt = sizeof (asection);
7904 elf_text_section = bfd_zalloc (abfd, amt);
7905 if (elf_text_section == NULL)
7906 return false;
7908 amt = sizeof (asymbol);
7909 elf_text_symbol = bfd_zalloc (abfd, amt);
7910 if (elf_text_symbol == NULL)
7911 return false;
7913 /* Initialize the section. */
7915 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7916 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7918 elf_text_section->symbol = elf_text_symbol;
7919 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7921 elf_text_section->name = ".text";
7922 elf_text_section->flags = SEC_NO_FLAGS;
7923 elf_text_section->output_section = NULL;
7924 elf_text_section->owner = abfd;
7925 elf_text_symbol->name = ".text";
7926 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7927 elf_text_symbol->section = elf_text_section;
7929 /* This code used to do *secp = bfd_und_section_ptr if
7930 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7931 so I took it out. */
7932 *secp = mips_elf_tdata (abfd)->elf_text_section;
7933 break;
7935 case SHN_MIPS_ACOMMON:
7936 /* Fall through. XXX Can we treat this as allocated data? */
7937 case SHN_MIPS_DATA:
7938 /* This section is used in a shared object. */
7939 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7941 asymbol *elf_data_symbol;
7942 asection *elf_data_section;
7943 size_t amt = sizeof (asection);
7945 elf_data_section = bfd_zalloc (abfd, amt);
7946 if (elf_data_section == NULL)
7947 return false;
7949 amt = sizeof (asymbol);
7950 elf_data_symbol = bfd_zalloc (abfd, amt);
7951 if (elf_data_symbol == NULL)
7952 return false;
7954 /* Initialize the section. */
7956 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7957 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7959 elf_data_section->symbol = elf_data_symbol;
7960 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7962 elf_data_section->name = ".data";
7963 elf_data_section->flags = SEC_NO_FLAGS;
7964 elf_data_section->output_section = NULL;
7965 elf_data_section->owner = abfd;
7966 elf_data_symbol->name = ".data";
7967 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7968 elf_data_symbol->section = elf_data_section;
7970 /* This code used to do *secp = bfd_und_section_ptr if
7971 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7972 so I took it out. */
7973 *secp = mips_elf_tdata (abfd)->elf_data_section;
7974 break;
7976 case SHN_MIPS_SUNDEFINED:
7977 *secp = bfd_und_section_ptr;
7978 break;
7981 if (SGI_COMPAT (abfd)
7982 && ! bfd_link_pic (info)
7983 && info->output_bfd->xvec == abfd->xvec
7984 && strcmp (*namep, "__rld_obj_head") == 0)
7986 struct elf_link_hash_entry *h;
7987 struct bfd_link_hash_entry *bh;
7989 /* Mark __rld_obj_head as dynamic. */
7990 bh = NULL;
7991 if (! (_bfd_generic_link_add_one_symbol
7992 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7993 get_elf_backend_data (abfd)->collect, &bh)))
7994 return false;
7996 h = (struct elf_link_hash_entry *) bh;
7997 h->non_elf = 0;
7998 h->def_regular = 1;
7999 h->type = STT_OBJECT;
8001 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8002 return false;
8004 mips_elf_hash_table (info)->use_rld_obj_head = true;
8005 mips_elf_hash_table (info)->rld_symbol = h;
8008 /* If this is a mips16 text symbol, add 1 to the value to make it
8009 odd. This will cause something like .word SYM to come up with
8010 the right value when it is loaded into the PC. */
8011 if (ELF_ST_IS_COMPRESSED (sym->st_other))
8012 ++*valp;
8014 return true;
8017 /* This hook function is called before the linker writes out a global
8018 symbol. We mark symbols as small common if appropriate. This is
8019 also where we undo the increment of the value for a mips16 symbol. */
8022 _bfd_mips_elf_link_output_symbol_hook
8023 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8024 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8025 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8027 /* If we see a common symbol, which implies a relocatable link, then
8028 if a symbol was small common in an input file, mark it as small
8029 common in the output file. */
8030 if (sym->st_shndx == SHN_COMMON
8031 && strcmp (input_sec->name, ".scommon") == 0)
8032 sym->st_shndx = SHN_MIPS_SCOMMON;
8034 if (ELF_ST_IS_COMPRESSED (sym->st_other))
8035 sym->st_value &= ~1;
8037 return 1;
8040 /* Functions for the dynamic linker. */
8042 /* Create dynamic sections when linking against a dynamic object. */
8044 bool
8045 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8047 struct elf_link_hash_entry *h;
8048 struct bfd_link_hash_entry *bh;
8049 flagword flags;
8050 register asection *s;
8051 const char * const *namep;
8052 struct mips_elf_link_hash_table *htab;
8054 htab = mips_elf_hash_table (info);
8055 BFD_ASSERT (htab != NULL);
8057 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8058 | SEC_LINKER_CREATED | SEC_READONLY);
8060 /* The psABI requires a read-only .dynamic section, but the VxWorks
8061 EABI doesn't. */
8062 if (htab->root.target_os != is_vxworks)
8064 s = bfd_get_linker_section (abfd, ".dynamic");
8065 if (s != NULL)
8067 if (!bfd_set_section_flags (s, flags))
8068 return false;
8072 /* We need to create .got section. */
8073 if (!mips_elf_create_got_section (abfd, info))
8074 return false;
8076 if (! mips_elf_rel_dyn_section (info, true))
8077 return false;
8079 /* Create .stub section. */
8080 s = bfd_make_section_anyway_with_flags (abfd,
8081 MIPS_ELF_STUB_SECTION_NAME (abfd),
8082 flags | SEC_CODE);
8083 if (s == NULL
8084 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8085 return false;
8086 htab->sstubs = s;
8088 if (!mips_elf_hash_table (info)->use_rld_obj_head
8089 && bfd_link_executable (info)
8090 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8092 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8093 flags &~ (flagword) SEC_READONLY);
8094 if (s == NULL
8095 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8096 return false;
8099 /* Create .MIPS.xhash section. */
8100 if (info->emit_gnu_hash)
8101 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8102 flags | SEC_READONLY);
8104 /* On IRIX5, we adjust add some additional symbols and change the
8105 alignments of several sections. There is no ABI documentation
8106 indicating that this is necessary on IRIX6, nor any evidence that
8107 the linker takes such action. */
8108 if (IRIX_COMPAT (abfd) == ict_irix5)
8110 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8112 bh = NULL;
8113 if (! (_bfd_generic_link_add_one_symbol
8114 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8115 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8116 return false;
8118 h = (struct elf_link_hash_entry *) bh;
8119 h->mark = 1;
8120 h->non_elf = 0;
8121 h->def_regular = 1;
8122 h->type = STT_SECTION;
8124 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8125 return false;
8128 /* We need to create a .compact_rel section. */
8129 if (SGI_COMPAT (abfd))
8131 if (!mips_elf_create_compact_rel_section (abfd, info))
8132 return false;
8135 /* Change alignments of some sections. */
8136 s = bfd_get_linker_section (abfd, ".hash");
8137 if (s != NULL)
8138 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8140 s = bfd_get_linker_section (abfd, ".dynsym");
8141 if (s != NULL)
8142 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8144 s = bfd_get_linker_section (abfd, ".dynstr");
8145 if (s != NULL)
8146 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8148 /* ??? */
8149 s = bfd_get_section_by_name (abfd, ".reginfo");
8150 if (s != NULL)
8151 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8153 s = bfd_get_linker_section (abfd, ".dynamic");
8154 if (s != NULL)
8155 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8158 if (bfd_link_executable (info))
8160 const char *name;
8162 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8163 bh = NULL;
8164 if (!(_bfd_generic_link_add_one_symbol
8165 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8166 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8167 return false;
8169 h = (struct elf_link_hash_entry *) bh;
8170 h->non_elf = 0;
8171 h->def_regular = 1;
8172 h->type = STT_SECTION;
8174 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8175 return false;
8177 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8179 /* __rld_map is a four byte word located in the .data section
8180 and is filled in by the rtld to contain a pointer to
8181 the _r_debug structure. Its symbol value will be set in
8182 _bfd_mips_elf_finish_dynamic_symbol. */
8183 s = bfd_get_linker_section (abfd, ".rld_map");
8184 BFD_ASSERT (s != NULL);
8186 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8187 bh = NULL;
8188 if (!(_bfd_generic_link_add_one_symbol
8189 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8190 get_elf_backend_data (abfd)->collect, &bh)))
8191 return false;
8193 h = (struct elf_link_hash_entry *) bh;
8194 h->non_elf = 0;
8195 h->def_regular = 1;
8196 h->type = STT_OBJECT;
8198 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8199 return false;
8200 mips_elf_hash_table (info)->rld_symbol = h;
8204 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8205 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8206 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8207 return false;
8209 /* Do the usual VxWorks handling. */
8210 if (htab->root.target_os == is_vxworks
8211 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8212 return false;
8214 return true;
8217 /* Return true if relocation REL against section SEC is a REL rather than
8218 RELA relocation. RELOCS is the first relocation in the section and
8219 ABFD is the bfd that contains SEC. */
8221 static bool
8222 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8223 const Elf_Internal_Rela *relocs,
8224 const Elf_Internal_Rela *rel)
8226 Elf_Internal_Shdr *rel_hdr;
8227 const struct elf_backend_data *bed;
8229 /* To determine which flavor of relocation this is, we depend on the
8230 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8231 rel_hdr = elf_section_data (sec)->rel.hdr;
8232 if (rel_hdr == NULL)
8233 return false;
8234 bed = get_elf_backend_data (abfd);
8235 return ((size_t) (rel - relocs)
8236 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8239 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8240 HOWTO is the relocation's howto and CONTENTS points to the contents
8241 of the section that REL is against. */
8243 static bfd_vma
8244 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8245 const Elf_Internal_Rela *rel,
8246 reloc_howto_type *howto, bfd_byte *contents)
8248 bfd_byte *location;
8249 unsigned int r_type;
8250 bfd_vma addend;
8251 bfd_vma bytes;
8253 if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8254 return 0;
8256 r_type = ELF_R_TYPE (abfd, rel->r_info);
8257 location = contents + rel->r_offset;
8259 /* Get the addend, which is stored in the input file. */
8260 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8261 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8262 _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8264 addend = bytes & howto->src_mask;
8266 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8267 accordingly. */
8268 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8269 addend <<= 1;
8271 return addend;
8274 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8275 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8276 and update *ADDEND with the final addend. Return true on success
8277 or false if the LO16 could not be found. RELEND is the exclusive
8278 upper bound on the relocations for REL's section. */
8280 static bool
8281 mips_elf_add_lo16_rel_addend (bfd *abfd,
8282 asection *sec,
8283 const Elf_Internal_Rela *rel,
8284 const Elf_Internal_Rela *relend,
8285 bfd_byte *contents, bfd_vma *addend)
8287 unsigned int r_type, lo16_type;
8288 const Elf_Internal_Rela *lo16_relocation;
8289 reloc_howto_type *lo16_howto;
8290 bfd_vma l;
8292 r_type = ELF_R_TYPE (abfd, rel->r_info);
8293 if (mips16_reloc_p (r_type))
8294 lo16_type = R_MIPS16_LO16;
8295 else if (micromips_reloc_p (r_type))
8296 lo16_type = R_MICROMIPS_LO16;
8297 else if (r_type == R_MIPS_PCHI16)
8298 lo16_type = R_MIPS_PCLO16;
8299 else
8300 lo16_type = R_MIPS_LO16;
8302 /* The combined value is the sum of the HI16 addend, left-shifted by
8303 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8304 code does a `lui' of the HI16 value, and then an `addiu' of the
8305 LO16 value.)
8307 Scan ahead to find a matching LO16 relocation.
8309 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8310 be immediately following. However, for the IRIX6 ABI, the next
8311 relocation may be a composed relocation consisting of several
8312 relocations for the same address. In that case, the R_MIPS_LO16
8313 relocation may occur as one of these. We permit a similar
8314 extension in general, as that is useful for GCC.
8316 In some cases GCC dead code elimination removes the LO16 but keeps
8317 the corresponding HI16. This is strictly speaking a violation of
8318 the ABI but not immediately harmful. */
8319 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8320 if (lo16_relocation == NULL)
8321 return false;
8323 /* Obtain the addend kept there. */
8324 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8325 l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8326 contents);
8328 l <<= lo16_howto->rightshift;
8329 l = _bfd_mips_elf_sign_extend (l, 16);
8331 *addend <<= 16;
8332 *addend += l;
8333 return true;
8336 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8337 store the contents in *CONTENTS on success. Assume that *CONTENTS
8338 already holds the contents if it is nonull on entry. */
8340 static bool
8341 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8343 if (*contents)
8344 return true;
8346 /* Get cached copy if it exists. */
8347 if (elf_section_data (sec)->this_hdr.contents != NULL)
8349 *contents = elf_section_data (sec)->this_hdr.contents;
8350 return true;
8353 return bfd_malloc_and_get_section (abfd, sec, contents);
8356 /* Make a new PLT record to keep internal data. */
8358 static struct plt_entry *
8359 mips_elf_make_plt_record (bfd *abfd)
8361 struct plt_entry *entry;
8363 entry = bfd_zalloc (abfd, sizeof (*entry));
8364 if (entry == NULL)
8365 return NULL;
8367 entry->stub_offset = MINUS_ONE;
8368 entry->mips_offset = MINUS_ONE;
8369 entry->comp_offset = MINUS_ONE;
8370 entry->gotplt_index = MINUS_ONE;
8371 return entry;
8374 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8375 for PIC code, as otherwise there is no load-time relocation involved
8376 and local GOT entries whose value is zero at static link time will
8377 retain their value at load time. */
8379 static bool
8380 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8381 struct mips_elf_link_hash_table *htab,
8382 unsigned int r_type)
8384 union
8386 struct elf_link_hash_entry *eh;
8387 struct bfd_link_hash_entry *bh;
8389 hzero;
8391 BFD_ASSERT (!htab->use_absolute_zero);
8392 BFD_ASSERT (bfd_link_pic (info));
8394 hzero.bh = NULL;
8395 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8396 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8397 NULL, false, false, &hzero.bh))
8398 return false;
8400 BFD_ASSERT (hzero.bh != NULL);
8401 hzero.eh->size = 0;
8402 hzero.eh->type = STT_NOTYPE;
8403 hzero.eh->other = STV_PROTECTED;
8404 hzero.eh->def_regular = 1;
8405 hzero.eh->non_elf = 0;
8407 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8408 return false;
8410 htab->use_absolute_zero = true;
8412 return true;
8415 /* Look through the relocs for a section during the first phase, and
8416 allocate space in the global offset table and record the need for
8417 standard MIPS and compressed procedure linkage table entries. */
8419 bool
8420 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8421 asection *sec, const Elf_Internal_Rela *relocs)
8423 const char *name;
8424 bfd *dynobj;
8425 Elf_Internal_Shdr *symtab_hdr;
8426 struct elf_link_hash_entry **sym_hashes;
8427 size_t extsymoff;
8428 const Elf_Internal_Rela *rel;
8429 const Elf_Internal_Rela *rel_end;
8430 asection *sreloc;
8431 const struct elf_backend_data *bed;
8432 struct mips_elf_link_hash_table *htab;
8433 bfd_byte *contents;
8434 bfd_vma addend;
8435 reloc_howto_type *howto;
8437 if (bfd_link_relocatable (info))
8438 return true;
8440 htab = mips_elf_hash_table (info);
8441 BFD_ASSERT (htab != NULL);
8443 dynobj = elf_hash_table (info)->dynobj;
8444 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8445 sym_hashes = elf_sym_hashes (abfd);
8446 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8448 bed = get_elf_backend_data (abfd);
8449 rel_end = relocs + sec->reloc_count;
8451 /* Check for the mips16 stub sections. */
8453 name = bfd_section_name (sec);
8454 if (FN_STUB_P (name))
8456 unsigned long r_symndx;
8458 /* Look at the relocation information to figure out which symbol
8459 this is for. */
8461 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8462 if (r_symndx == 0)
8464 _bfd_error_handler
8465 /* xgettext:c-format */
8466 (_("%pB: warning: cannot determine the target function for"
8467 " stub section `%s'"),
8468 abfd, name);
8469 bfd_set_error (bfd_error_bad_value);
8470 return false;
8473 if (r_symndx < extsymoff
8474 || sym_hashes[r_symndx - extsymoff] == NULL)
8476 asection *o;
8478 /* This stub is for a local symbol. This stub will only be
8479 needed if there is some relocation in this BFD, other
8480 than a 16 bit function call, which refers to this symbol. */
8481 for (o = abfd->sections; o != NULL; o = o->next)
8483 Elf_Internal_Rela *sec_relocs;
8484 const Elf_Internal_Rela *r, *rend;
8486 /* We can ignore stub sections when looking for relocs. */
8487 if ((o->flags & SEC_RELOC) == 0
8488 || o->reloc_count == 0
8489 || section_allows_mips16_refs_p (o))
8490 continue;
8492 sec_relocs
8493 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8494 info->keep_memory);
8495 if (sec_relocs == NULL)
8496 return false;
8498 rend = sec_relocs + o->reloc_count;
8499 for (r = sec_relocs; r < rend; r++)
8500 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8501 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8502 break;
8504 if (elf_section_data (o)->relocs != sec_relocs)
8505 free (sec_relocs);
8507 if (r < rend)
8508 break;
8511 if (o == NULL)
8513 /* There is no non-call reloc for this stub, so we do
8514 not need it. Since this function is called before
8515 the linker maps input sections to output sections, we
8516 can easily discard it by setting the SEC_EXCLUDE
8517 flag. */
8518 sec->flags |= SEC_EXCLUDE;
8519 return true;
8522 /* Record this stub in an array of local symbol stubs for
8523 this BFD. */
8524 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8526 unsigned long symcount;
8527 asection **n;
8528 bfd_size_type amt;
8530 if (elf_bad_symtab (abfd))
8531 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8532 else
8533 symcount = symtab_hdr->sh_info;
8534 amt = symcount * sizeof (asection *);
8535 n = bfd_zalloc (abfd, amt);
8536 if (n == NULL)
8537 return false;
8538 mips_elf_tdata (abfd)->local_stubs = n;
8541 sec->flags |= SEC_KEEP;
8542 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8544 /* We don't need to set mips16_stubs_seen in this case.
8545 That flag is used to see whether we need to look through
8546 the global symbol table for stubs. We don't need to set
8547 it here, because we just have a local stub. */
8549 else
8551 struct mips_elf_link_hash_entry *h;
8553 h = ((struct mips_elf_link_hash_entry *)
8554 sym_hashes[r_symndx - extsymoff]);
8556 while (h->root.root.type == bfd_link_hash_indirect
8557 || h->root.root.type == bfd_link_hash_warning)
8558 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8560 /* H is the symbol this stub is for. */
8562 /* If we already have an appropriate stub for this function, we
8563 don't need another one, so we can discard this one. Since
8564 this function is called before the linker maps input sections
8565 to output sections, we can easily discard it by setting the
8566 SEC_EXCLUDE flag. */
8567 if (h->fn_stub != NULL)
8569 sec->flags |= SEC_EXCLUDE;
8570 return true;
8573 sec->flags |= SEC_KEEP;
8574 h->fn_stub = sec;
8575 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8578 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8580 unsigned long r_symndx;
8581 struct mips_elf_link_hash_entry *h;
8582 asection **loc;
8584 /* Look at the relocation information to figure out which symbol
8585 this is for. */
8587 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8588 if (r_symndx == 0)
8590 _bfd_error_handler
8591 /* xgettext:c-format */
8592 (_("%pB: warning: cannot determine the target function for"
8593 " stub section `%s'"),
8594 abfd, name);
8595 bfd_set_error (bfd_error_bad_value);
8596 return false;
8599 if (r_symndx < extsymoff
8600 || sym_hashes[r_symndx - extsymoff] == NULL)
8602 asection *o;
8604 /* This stub is for a local symbol. This stub will only be
8605 needed if there is some relocation (R_MIPS16_26) in this BFD
8606 that refers to this symbol. */
8607 for (o = abfd->sections; o != NULL; o = o->next)
8609 Elf_Internal_Rela *sec_relocs;
8610 const Elf_Internal_Rela *r, *rend;
8612 /* We can ignore stub sections when looking for relocs. */
8613 if ((o->flags & SEC_RELOC) == 0
8614 || o->reloc_count == 0
8615 || section_allows_mips16_refs_p (o))
8616 continue;
8618 sec_relocs
8619 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8620 info->keep_memory);
8621 if (sec_relocs == NULL)
8622 return false;
8624 rend = sec_relocs + o->reloc_count;
8625 for (r = sec_relocs; r < rend; r++)
8626 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8627 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8628 break;
8630 if (elf_section_data (o)->relocs != sec_relocs)
8631 free (sec_relocs);
8633 if (r < rend)
8634 break;
8637 if (o == NULL)
8639 /* There is no non-call reloc for this stub, so we do
8640 not need it. Since this function is called before
8641 the linker maps input sections to output sections, we
8642 can easily discard it by setting the SEC_EXCLUDE
8643 flag. */
8644 sec->flags |= SEC_EXCLUDE;
8645 return true;
8648 /* Record this stub in an array of local symbol call_stubs for
8649 this BFD. */
8650 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8652 unsigned long symcount;
8653 asection **n;
8654 bfd_size_type amt;
8656 if (elf_bad_symtab (abfd))
8657 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8658 else
8659 symcount = symtab_hdr->sh_info;
8660 amt = symcount * sizeof (asection *);
8661 n = bfd_zalloc (abfd, amt);
8662 if (n == NULL)
8663 return false;
8664 mips_elf_tdata (abfd)->local_call_stubs = n;
8667 sec->flags |= SEC_KEEP;
8668 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8670 /* We don't need to set mips16_stubs_seen in this case.
8671 That flag is used to see whether we need to look through
8672 the global symbol table for stubs. We don't need to set
8673 it here, because we just have a local stub. */
8675 else
8677 h = ((struct mips_elf_link_hash_entry *)
8678 sym_hashes[r_symndx - extsymoff]);
8680 /* H is the symbol this stub is for. */
8682 if (CALL_FP_STUB_P (name))
8683 loc = &h->call_fp_stub;
8684 else
8685 loc = &h->call_stub;
8687 /* If we already have an appropriate stub for this function, we
8688 don't need another one, so we can discard this one. Since
8689 this function is called before the linker maps input sections
8690 to output sections, we can easily discard it by setting the
8691 SEC_EXCLUDE flag. */
8692 if (*loc != NULL)
8694 sec->flags |= SEC_EXCLUDE;
8695 return true;
8698 sec->flags |= SEC_KEEP;
8699 *loc = sec;
8700 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8704 sreloc = NULL;
8705 contents = NULL;
8706 for (rel = relocs; rel < rel_end; ++rel)
8708 unsigned long r_symndx;
8709 unsigned int r_type;
8710 struct elf_link_hash_entry *h;
8711 bool can_make_dynamic_p;
8712 bool call_reloc_p;
8713 bool constrain_symbol_p;
8715 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8716 r_type = ELF_R_TYPE (abfd, rel->r_info);
8718 if (r_symndx < extsymoff)
8719 h = NULL;
8720 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8722 _bfd_error_handler
8723 /* xgettext:c-format */
8724 (_("%pB: malformed reloc detected for section %s"),
8725 abfd, name);
8726 bfd_set_error (bfd_error_bad_value);
8727 return false;
8729 else
8731 h = sym_hashes[r_symndx - extsymoff];
8732 if (h != NULL)
8734 while (h->root.type == bfd_link_hash_indirect
8735 || h->root.type == bfd_link_hash_warning)
8736 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8740 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8741 relocation into a dynamic one. */
8742 can_make_dynamic_p = false;
8744 /* Set CALL_RELOC_P to true if the relocation is for a call,
8745 and if pointer equality therefore doesn't matter. */
8746 call_reloc_p = false;
8748 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8749 into account when deciding how to define the symbol. */
8750 constrain_symbol_p = true;
8752 switch (r_type)
8754 case R_MIPS_CALL16:
8755 case R_MIPS_CALL_HI16:
8756 case R_MIPS_CALL_LO16:
8757 case R_MIPS16_CALL16:
8758 case R_MICROMIPS_CALL16:
8759 case R_MICROMIPS_CALL_HI16:
8760 case R_MICROMIPS_CALL_LO16:
8761 call_reloc_p = true;
8762 /* Fall through. */
8764 case R_MIPS_GOT16:
8765 case R_MIPS_GOT_LO16:
8766 case R_MIPS_GOT_PAGE:
8767 case R_MIPS_GOT_DISP:
8768 case R_MIPS16_GOT16:
8769 case R_MICROMIPS_GOT16:
8770 case R_MICROMIPS_GOT_LO16:
8771 case R_MICROMIPS_GOT_PAGE:
8772 case R_MICROMIPS_GOT_DISP:
8773 /* If we have a symbol that will resolve to zero at static link
8774 time and it is used by a GOT relocation applied to code we
8775 cannot relax to an immediate zero load, then we will be using
8776 the special `__gnu_absolute_zero' symbol whose value is zero
8777 at dynamic load time. We ignore HI16-type GOT relocations at
8778 this stage, because their handling will depend entirely on
8779 the corresponding LO16-type GOT relocation. */
8780 if (!call_hi16_reloc_p (r_type)
8781 && h != NULL
8782 && bfd_link_pic (info)
8783 && !htab->use_absolute_zero
8784 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8786 bool rel_reloc;
8788 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8789 return false;
8791 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8792 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8793 if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8794 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8795 false))
8796 if (!mips_elf_define_absolute_zero (abfd, info, htab,
8797 r_type))
8798 return false;
8801 /* Fall through. */
8802 case R_MIPS_GOT_HI16:
8803 case R_MIPS_GOT_OFST:
8804 case R_MIPS_TLS_GOTTPREL:
8805 case R_MIPS_TLS_GD:
8806 case R_MIPS_TLS_LDM:
8807 case R_MIPS16_TLS_GOTTPREL:
8808 case R_MIPS16_TLS_GD:
8809 case R_MIPS16_TLS_LDM:
8810 case R_MICROMIPS_GOT_HI16:
8811 case R_MICROMIPS_GOT_OFST:
8812 case R_MICROMIPS_TLS_GOTTPREL:
8813 case R_MICROMIPS_TLS_GD:
8814 case R_MICROMIPS_TLS_LDM:
8815 if (dynobj == NULL)
8816 elf_hash_table (info)->dynobj = dynobj = abfd;
8817 if (!mips_elf_create_got_section (dynobj, info))
8818 return false;
8819 if (htab->root.target_os == is_vxworks
8820 && !bfd_link_pic (info))
8822 _bfd_error_handler
8823 /* xgettext:c-format */
8824 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8825 abfd, (uint64_t) rel->r_offset);
8826 bfd_set_error (bfd_error_bad_value);
8827 return false;
8829 can_make_dynamic_p = true;
8830 break;
8832 case R_MIPS_NONE:
8833 case R_MIPS_JALR:
8834 case R_MICROMIPS_JALR:
8835 /* These relocations have empty fields and are purely there to
8836 provide link information. The symbol value doesn't matter. */
8837 constrain_symbol_p = false;
8838 break;
8840 case R_MIPS_GPREL16:
8841 case R_MIPS_GPREL32:
8842 case R_MIPS16_GPREL:
8843 case R_MICROMIPS_GPREL16:
8844 /* GP-relative relocations always resolve to a definition in a
8845 regular input file, ignoring the one-definition rule. This is
8846 important for the GP setup sequence in NewABI code, which
8847 always resolves to a local function even if other relocations
8848 against the symbol wouldn't. */
8849 constrain_symbol_p = false;
8850 break;
8852 case R_MIPS_32:
8853 case R_MIPS_REL32:
8854 case R_MIPS_64:
8855 /* In VxWorks executables, references to external symbols
8856 must be handled using copy relocs or PLT entries; it is not
8857 possible to convert this relocation into a dynamic one.
8859 For executables that use PLTs and copy-relocs, we have a
8860 choice between converting the relocation into a dynamic
8861 one or using copy relocations or PLT entries. It is
8862 usually better to do the former, unless the relocation is
8863 against a read-only section. */
8864 if ((bfd_link_pic (info)
8865 || (h != NULL
8866 && htab->root.target_os != is_vxworks
8867 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8868 && !(!info->nocopyreloc
8869 && !PIC_OBJECT_P (abfd)
8870 && MIPS_ELF_READONLY_SECTION (sec))))
8871 && (sec->flags & SEC_ALLOC) != 0)
8873 can_make_dynamic_p = true;
8874 if (dynobj == NULL)
8875 elf_hash_table (info)->dynobj = dynobj = abfd;
8877 break;
8879 case R_MIPS_26:
8880 case R_MIPS_PC16:
8881 case R_MIPS_PC21_S2:
8882 case R_MIPS_PC26_S2:
8883 case R_MIPS16_26:
8884 case R_MIPS16_PC16_S1:
8885 case R_MICROMIPS_26_S1:
8886 case R_MICROMIPS_PC7_S1:
8887 case R_MICROMIPS_PC10_S1:
8888 case R_MICROMIPS_PC16_S1:
8889 case R_MICROMIPS_PC23_S2:
8890 call_reloc_p = true;
8891 break;
8894 if (h)
8896 if (constrain_symbol_p)
8898 if (!can_make_dynamic_p)
8899 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8901 if (!call_reloc_p)
8902 h->pointer_equality_needed = 1;
8904 /* We must not create a stub for a symbol that has
8905 relocations related to taking the function's address.
8906 This doesn't apply to VxWorks, where CALL relocs refer
8907 to a .got.plt entry instead of a normal .got entry. */
8908 if (htab->root.target_os != is_vxworks
8909 && (!can_make_dynamic_p || !call_reloc_p))
8910 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8913 /* Relocations against the special VxWorks __GOTT_BASE__ and
8914 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8915 room for them in .rela.dyn. */
8916 if (is_gott_symbol (info, h))
8918 if (sreloc == NULL)
8920 sreloc = mips_elf_rel_dyn_section (info, true);
8921 if (sreloc == NULL)
8922 return false;
8924 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8925 if (MIPS_ELF_READONLY_SECTION (sec))
8926 /* We tell the dynamic linker that there are
8927 relocations against the text segment. */
8928 info->flags |= DF_TEXTREL;
8931 else if (call_lo16_reloc_p (r_type)
8932 || got_lo16_reloc_p (r_type)
8933 || got_disp_reloc_p (r_type)
8934 || (got16_reloc_p (r_type)
8935 && htab->root.target_os == is_vxworks))
8937 /* We may need a local GOT entry for this relocation. We
8938 don't count R_MIPS_GOT_PAGE because we can estimate the
8939 maximum number of pages needed by looking at the size of
8940 the segment. Similar comments apply to R_MIPS*_GOT16 and
8941 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8942 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8943 R_MIPS_CALL_HI16 because these are always followed by an
8944 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8945 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8946 rel->r_addend, info, r_type))
8947 return false;
8950 if (h != NULL
8951 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8952 ELF_ST_IS_MIPS16 (h->other)))
8953 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8955 switch (r_type)
8957 case R_MIPS_CALL16:
8958 case R_MIPS16_CALL16:
8959 case R_MICROMIPS_CALL16:
8960 if (h == NULL)
8962 _bfd_error_handler
8963 /* xgettext:c-format */
8964 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8965 abfd, (uint64_t) rel->r_offset);
8966 bfd_set_error (bfd_error_bad_value);
8967 return false;
8969 /* Fall through. */
8971 case R_MIPS_CALL_HI16:
8972 case R_MIPS_CALL_LO16:
8973 case R_MICROMIPS_CALL_HI16:
8974 case R_MICROMIPS_CALL_LO16:
8975 if (h != NULL)
8977 /* Make sure there is room in the regular GOT to hold the
8978 function's address. We may eliminate it in favour of
8979 a .got.plt entry later; see mips_elf_count_got_symbols. */
8980 if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8981 r_type))
8982 return false;
8984 /* We need a stub, not a plt entry for the undefined
8985 function. But we record it as if it needs plt. See
8986 _bfd_elf_adjust_dynamic_symbol. */
8987 h->needs_plt = 1;
8988 h->type = STT_FUNC;
8990 break;
8992 case R_MIPS_GOT_PAGE:
8993 case R_MICROMIPS_GOT_PAGE:
8994 case R_MIPS16_GOT16:
8995 case R_MIPS_GOT16:
8996 case R_MIPS_GOT_HI16:
8997 case R_MIPS_GOT_LO16:
8998 case R_MICROMIPS_GOT16:
8999 case R_MICROMIPS_GOT_HI16:
9000 case R_MICROMIPS_GOT_LO16:
9001 if (!h || got_page_reloc_p (r_type))
9003 /* This relocation needs (or may need, if h != NULL) a
9004 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
9005 know for sure until we know whether the symbol is
9006 preemptible. */
9007 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
9009 if (!mips_elf_get_section_contents (abfd, sec, &contents))
9010 return false;
9011 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
9012 addend = mips_elf_read_rel_addend (abfd, sec, rel,
9013 howto, contents);
9014 if (got16_reloc_p (r_type))
9015 mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
9016 contents, &addend);
9017 else
9018 addend <<= howto->rightshift;
9020 else
9021 addend = rel->r_addend;
9022 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9023 h, addend))
9024 return false;
9026 if (h)
9028 struct mips_elf_link_hash_entry *hmips =
9029 (struct mips_elf_link_hash_entry *) h;
9031 /* This symbol is definitely not overridable. */
9032 if (hmips->root.def_regular
9033 && ! (bfd_link_pic (info) && ! info->symbolic
9034 && ! hmips->root.forced_local))
9035 h = NULL;
9038 /* If this is a global, overridable symbol, GOT_PAGE will
9039 decay to GOT_DISP, so we'll need a GOT entry for it. */
9040 /* Fall through. */
9042 case R_MIPS_GOT_DISP:
9043 case R_MICROMIPS_GOT_DISP:
9044 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9045 false, r_type))
9046 return false;
9047 break;
9049 case R_MIPS_TLS_GOTTPREL:
9050 case R_MIPS16_TLS_GOTTPREL:
9051 case R_MICROMIPS_TLS_GOTTPREL:
9052 if (bfd_link_pic (info))
9053 info->flags |= DF_STATIC_TLS;
9054 /* Fall through */
9056 case R_MIPS_TLS_LDM:
9057 case R_MIPS16_TLS_LDM:
9058 case R_MICROMIPS_TLS_LDM:
9059 if (tls_ldm_reloc_p (r_type))
9061 r_symndx = STN_UNDEF;
9062 h = NULL;
9064 /* Fall through */
9066 case R_MIPS_TLS_GD:
9067 case R_MIPS16_TLS_GD:
9068 case R_MICROMIPS_TLS_GD:
9069 /* This symbol requires a global offset table entry, or two
9070 for TLS GD relocations. */
9071 if (h != NULL)
9073 if (!mips_elf_record_global_got_symbol (h, abfd, info,
9074 false, r_type))
9075 return false;
9077 else
9079 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9080 rel->r_addend,
9081 info, r_type))
9082 return false;
9084 break;
9086 case R_MIPS_32:
9087 case R_MIPS_REL32:
9088 case R_MIPS_64:
9089 /* In VxWorks executables, references to external symbols
9090 are handled using copy relocs or PLT stubs, so there's
9091 no need to add a .rela.dyn entry for this relocation. */
9092 if (can_make_dynamic_p)
9094 if (sreloc == NULL)
9096 sreloc = mips_elf_rel_dyn_section (info, true);
9097 if (sreloc == NULL)
9098 return false;
9100 if (bfd_link_pic (info) && h == NULL)
9102 /* When creating a shared object, we must copy these
9103 reloc types into the output file as R_MIPS_REL32
9104 relocs. Make room for this reloc in .rel(a).dyn. */
9105 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9106 if (MIPS_ELF_READONLY_SECTION (sec))
9107 /* We tell the dynamic linker that there are
9108 relocations against the text segment. */
9109 info->flags |= DF_TEXTREL;
9111 else
9113 struct mips_elf_link_hash_entry *hmips;
9115 /* For a shared object, we must copy this relocation
9116 unless the symbol turns out to be undefined and
9117 weak with non-default visibility, in which case
9118 it will be left as zero.
9120 We could elide R_MIPS_REL32 for locally binding symbols
9121 in shared libraries, but do not yet do so.
9123 For an executable, we only need to copy this
9124 reloc if the symbol is defined in a dynamic
9125 object. */
9126 hmips = (struct mips_elf_link_hash_entry *) h;
9127 ++hmips->possibly_dynamic_relocs;
9128 if (MIPS_ELF_READONLY_SECTION (sec))
9129 /* We need it to tell the dynamic linker if there
9130 are relocations against the text segment. */
9131 hmips->readonly_reloc = true;
9135 if (SGI_COMPAT (abfd))
9136 mips_elf_hash_table (info)->compact_rel_size +=
9137 sizeof (Elf32_External_crinfo);
9138 break;
9140 case R_MIPS_26:
9141 case R_MIPS_GPREL16:
9142 case R_MIPS_LITERAL:
9143 case R_MIPS_GPREL32:
9144 case R_MICROMIPS_26_S1:
9145 case R_MICROMIPS_GPREL16:
9146 case R_MICROMIPS_LITERAL:
9147 case R_MICROMIPS_GPREL7_S2:
9148 if (SGI_COMPAT (abfd))
9149 mips_elf_hash_table (info)->compact_rel_size +=
9150 sizeof (Elf32_External_crinfo);
9151 break;
9153 /* This relocation describes the C++ object vtable hierarchy.
9154 Reconstruct it for later use during GC. */
9155 case R_MIPS_GNU_VTINHERIT:
9156 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9157 return false;
9158 break;
9160 /* This relocation describes which C++ vtable entries are actually
9161 used. Record for later use during GC. */
9162 case R_MIPS_GNU_VTENTRY:
9163 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9164 return false;
9165 break;
9167 default:
9168 break;
9171 /* Record the need for a PLT entry. At this point we don't know
9172 yet if we are going to create a PLT in the first place, but
9173 we only record whether the relocation requires a standard MIPS
9174 or a compressed code entry anyway. If we don't make a PLT after
9175 all, then we'll just ignore these arrangements. Likewise if
9176 a PLT entry is not created because the symbol is satisfied
9177 locally. */
9178 if (h != NULL
9179 && (branch_reloc_p (r_type)
9180 || mips16_branch_reloc_p (r_type)
9181 || micromips_branch_reloc_p (r_type))
9182 && !SYMBOL_CALLS_LOCAL (info, h))
9184 if (h->plt.plist == NULL)
9185 h->plt.plist = mips_elf_make_plt_record (abfd);
9186 if (h->plt.plist == NULL)
9187 return false;
9189 if (branch_reloc_p (r_type))
9190 h->plt.plist->need_mips = true;
9191 else
9192 h->plt.plist->need_comp = true;
9195 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9196 if there is one. We only need to handle global symbols here;
9197 we decide whether to keep or delete stubs for local symbols
9198 when processing the stub's relocations. */
9199 if (h != NULL
9200 && !mips16_call_reloc_p (r_type)
9201 && !section_allows_mips16_refs_p (sec))
9203 struct mips_elf_link_hash_entry *mh;
9205 mh = (struct mips_elf_link_hash_entry *) h;
9206 mh->need_fn_stub = true;
9209 /* Refuse some position-dependent relocations when creating a
9210 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9211 not PIC, but we can create dynamic relocations and the result
9212 will be fine. Also do not refuse R_MIPS_LO16, which can be
9213 combined with R_MIPS_GOT16. */
9214 if (bfd_link_pic (info))
9216 switch (r_type)
9218 case R_MIPS_TLS_TPREL_HI16:
9219 case R_MIPS16_TLS_TPREL_HI16:
9220 case R_MICROMIPS_TLS_TPREL_HI16:
9221 case R_MIPS_TLS_TPREL_LO16:
9222 case R_MIPS16_TLS_TPREL_LO16:
9223 case R_MICROMIPS_TLS_TPREL_LO16:
9224 /* These are okay in PIE, but not in a shared library. */
9225 if (bfd_link_executable (info))
9226 break;
9228 /* FALLTHROUGH */
9230 case R_MIPS16_HI16:
9231 case R_MIPS_HI16:
9232 case R_MIPS_HIGHER:
9233 case R_MIPS_HIGHEST:
9234 case R_MICROMIPS_HI16:
9235 case R_MICROMIPS_HIGHER:
9236 case R_MICROMIPS_HIGHEST:
9237 /* Don't refuse a high part relocation if it's against
9238 no symbol (e.g. part of a compound relocation). */
9239 if (r_symndx == STN_UNDEF)
9240 break;
9242 /* Likewise an absolute symbol. */
9243 if (h != NULL && bfd_is_abs_symbol (&h->root))
9244 break;
9246 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9247 and has a special meaning. */
9248 if (!NEWABI_P (abfd) && h != NULL
9249 && strcmp (h->root.root.string, "_gp_disp") == 0)
9250 break;
9252 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9253 if (is_gott_symbol (info, h))
9254 break;
9256 /* FALLTHROUGH */
9258 case R_MIPS16_26:
9259 case R_MIPS_26:
9260 case R_MICROMIPS_26_S1:
9261 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9262 /* An error for unsupported relocations is raised as part
9263 of the above search, so we can skip the following. */
9264 if (howto != NULL)
9265 info->callbacks->einfo
9266 /* xgettext:c-format */
9267 (_("%X%H: relocation %s against `%s' cannot be used"
9268 " when making a shared object; recompile with -fPIC\n"),
9269 abfd, sec, rel->r_offset, howto->name,
9270 (h) ? h->root.root.string : "a local symbol");
9271 break;
9272 default:
9273 break;
9278 return true;
9281 /* Allocate space for global sym dynamic relocs. */
9283 static bool
9284 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9286 struct bfd_link_info *info = inf;
9287 bfd *dynobj;
9288 struct mips_elf_link_hash_entry *hmips;
9289 struct mips_elf_link_hash_table *htab;
9291 htab = mips_elf_hash_table (info);
9292 BFD_ASSERT (htab != NULL);
9294 dynobj = elf_hash_table (info)->dynobj;
9295 hmips = (struct mips_elf_link_hash_entry *) h;
9297 /* VxWorks executables are handled elsewhere; we only need to
9298 allocate relocations in shared objects. */
9299 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9300 return true;
9302 /* Ignore indirect symbols. All relocations against such symbols
9303 will be redirected to the target symbol. */
9304 if (h->root.type == bfd_link_hash_indirect)
9305 return true;
9307 /* If this symbol is defined in a dynamic object, or we are creating
9308 a shared library, we will need to copy any R_MIPS_32 or
9309 R_MIPS_REL32 relocs against it into the output file. */
9310 if (! bfd_link_relocatable (info)
9311 && hmips->possibly_dynamic_relocs != 0
9312 && (h->root.type == bfd_link_hash_defweak
9313 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9314 || bfd_link_pic (info)))
9316 bool do_copy = true;
9318 if (h->root.type == bfd_link_hash_undefweak)
9320 /* Do not copy relocations for undefined weak symbols that
9321 we are not going to export. */
9322 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9323 do_copy = false;
9325 /* Make sure undefined weak symbols are output as a dynamic
9326 symbol in PIEs. */
9327 else if (h->dynindx == -1 && !h->forced_local)
9329 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9330 return false;
9334 if (do_copy)
9336 /* Even though we don't directly need a GOT entry for this symbol,
9337 the SVR4 psABI requires it to have a dynamic symbol table
9338 index greater that DT_MIPS_GOTSYM if there are dynamic
9339 relocations against it.
9341 VxWorks does not enforce the same mapping between the GOT
9342 and the symbol table, so the same requirement does not
9343 apply there. */
9344 if (htab->root.target_os != is_vxworks)
9346 if (hmips->global_got_area > GGA_RELOC_ONLY)
9347 hmips->global_got_area = GGA_RELOC_ONLY;
9348 hmips->got_only_for_calls = false;
9351 mips_elf_allocate_dynamic_relocations
9352 (dynobj, info, hmips->possibly_dynamic_relocs);
9353 if (hmips->readonly_reloc)
9354 /* We tell the dynamic linker that there are relocations
9355 against the text segment. */
9356 info->flags |= DF_TEXTREL;
9360 return true;
9363 /* Adjust a symbol defined by a dynamic object and referenced by a
9364 regular object. The current definition is in some section of the
9365 dynamic object, but we're not including those sections. We have to
9366 change the definition to something the rest of the link can
9367 understand. */
9369 bool
9370 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9371 struct elf_link_hash_entry *h)
9373 bfd *dynobj;
9374 struct mips_elf_link_hash_entry *hmips;
9375 struct mips_elf_link_hash_table *htab;
9376 asection *s, *srel;
9378 htab = mips_elf_hash_table (info);
9379 BFD_ASSERT (htab != NULL);
9381 dynobj = elf_hash_table (info)->dynobj;
9382 hmips = (struct mips_elf_link_hash_entry *) h;
9384 /* Make sure we know what is going on here. */
9385 if (dynobj == NULL
9386 || (! h->needs_plt
9387 && ! h->is_weakalias
9388 && (! h->def_dynamic
9389 || ! h->ref_regular
9390 || h->def_regular)))
9392 if (h->type == STT_GNU_IFUNC)
9393 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9394 h->root.root.string);
9395 else
9396 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9397 h->root.root.string);
9398 return true;
9401 hmips = (struct mips_elf_link_hash_entry *) h;
9403 /* If there are call relocations against an externally-defined symbol,
9404 see whether we can create a MIPS lazy-binding stub for it. We can
9405 only do this if all references to the function are through call
9406 relocations, and in that case, the traditional lazy-binding stubs
9407 are much more efficient than PLT entries.
9409 Traditional stubs are only available on SVR4 psABI-based systems;
9410 VxWorks always uses PLTs instead. */
9411 if (htab->root.target_os != is_vxworks
9412 && h->needs_plt
9413 && !hmips->no_fn_stub)
9415 if (! elf_hash_table (info)->dynamic_sections_created)
9416 return true;
9418 /* If this symbol is not defined in a regular file, then set
9419 the symbol to the stub location. This is required to make
9420 function pointers compare as equal between the normal
9421 executable and the shared library. */
9422 if (!h->def_regular
9423 && !bfd_is_abs_section (htab->sstubs->output_section))
9425 hmips->needs_lazy_stub = true;
9426 htab->lazy_stub_count++;
9427 return true;
9430 /* As above, VxWorks requires PLT entries for externally-defined
9431 functions that are only accessed through call relocations.
9433 Both VxWorks and non-VxWorks targets also need PLT entries if there
9434 are static-only relocations against an externally-defined function.
9435 This can technically occur for shared libraries if there are
9436 branches to the symbol, although it is unlikely that this will be
9437 used in practice due to the short ranges involved. It can occur
9438 for any relative or absolute relocation in executables; in that
9439 case, the PLT entry becomes the function's canonical address. */
9440 else if (((h->needs_plt && !hmips->no_fn_stub)
9441 || (h->type == STT_FUNC && hmips->has_static_relocs))
9442 && htab->use_plts_and_copy_relocs
9443 && !SYMBOL_CALLS_LOCAL (info, h)
9444 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9445 && h->root.type == bfd_link_hash_undefweak))
9447 bool micromips_p = MICROMIPS_P (info->output_bfd);
9448 bool newabi_p = NEWABI_P (info->output_bfd);
9450 /* If this is the first symbol to need a PLT entry, then make some
9451 basic setup. Also work out PLT entry sizes. We'll need them
9452 for PLT offset calculations. */
9453 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9455 BFD_ASSERT (htab->root.sgotplt->size == 0);
9456 BFD_ASSERT (htab->plt_got_index == 0);
9458 /* If we're using the PLT additions to the psABI, each PLT
9459 entry is 16 bytes and the PLT0 entry is 32 bytes.
9460 Encourage better cache usage by aligning. We do this
9461 lazily to avoid pessimizing traditional objects. */
9462 if (htab->root.target_os != is_vxworks
9463 && !bfd_set_section_alignment (htab->root.splt, 5))
9464 return false;
9466 /* Make sure that .got.plt is word-aligned. We do this lazily
9467 for the same reason as above. */
9468 if (!bfd_set_section_alignment (htab->root.sgotplt,
9469 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9470 return false;
9472 /* On non-VxWorks targets, the first two entries in .got.plt
9473 are reserved. */
9474 if (htab->root.target_os != is_vxworks)
9475 htab->plt_got_index
9476 += (get_elf_backend_data (dynobj)->got_header_size
9477 / MIPS_ELF_GOT_SIZE (dynobj));
9479 /* On VxWorks, also allocate room for the header's
9480 .rela.plt.unloaded entries. */
9481 if (htab->root.target_os == is_vxworks
9482 && !bfd_link_pic (info))
9483 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9485 /* Now work out the sizes of individual PLT entries. */
9486 if (htab->root.target_os == is_vxworks
9487 && bfd_link_pic (info))
9488 htab->plt_mips_entry_size
9489 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9490 else if (htab->root.target_os == is_vxworks)
9491 htab->plt_mips_entry_size
9492 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9493 else if (newabi_p)
9494 htab->plt_mips_entry_size
9495 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9496 else if (!micromips_p)
9498 htab->plt_mips_entry_size
9499 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9500 htab->plt_comp_entry_size
9501 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9503 else if (htab->insn32)
9505 htab->plt_mips_entry_size
9506 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9507 htab->plt_comp_entry_size
9508 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9510 else
9512 htab->plt_mips_entry_size
9513 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9514 htab->plt_comp_entry_size
9515 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9519 if (h->plt.plist == NULL)
9520 h->plt.plist = mips_elf_make_plt_record (dynobj);
9521 if (h->plt.plist == NULL)
9522 return false;
9524 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9525 n32 or n64, so always use a standard entry there.
9527 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9528 all MIPS16 calls will go via that stub, and there is no benefit
9529 to having a MIPS16 entry. And in the case of call_stub a
9530 standard entry actually has to be used as the stub ends with a J
9531 instruction. */
9532 if (newabi_p
9533 || htab->root.target_os == is_vxworks
9534 || hmips->call_stub
9535 || hmips->call_fp_stub)
9537 h->plt.plist->need_mips = true;
9538 h->plt.plist->need_comp = false;
9541 /* Otherwise, if there are no direct calls to the function, we
9542 have a free choice of whether to use standard or compressed
9543 entries. Prefer microMIPS entries if the object is known to
9544 contain microMIPS code, so that it becomes possible to create
9545 pure microMIPS binaries. Prefer standard entries otherwise,
9546 because MIPS16 ones are no smaller and are usually slower. */
9547 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9549 if (micromips_p)
9550 h->plt.plist->need_comp = true;
9551 else
9552 h->plt.plist->need_mips = true;
9555 if (h->plt.plist->need_mips)
9557 h->plt.plist->mips_offset = htab->plt_mips_offset;
9558 htab->plt_mips_offset += htab->plt_mips_entry_size;
9560 if (h->plt.plist->need_comp)
9562 h->plt.plist->comp_offset = htab->plt_comp_offset;
9563 htab->plt_comp_offset += htab->plt_comp_entry_size;
9566 /* Reserve the corresponding .got.plt entry now too. */
9567 h->plt.plist->gotplt_index = htab->plt_got_index++;
9569 /* If the output file has no definition of the symbol, set the
9570 symbol's value to the address of the stub. */
9571 if (!bfd_link_pic (info) && !h->def_regular)
9572 hmips->use_plt_entry = true;
9574 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9575 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9576 ? MIPS_ELF_RELA_SIZE (dynobj)
9577 : MIPS_ELF_REL_SIZE (dynobj));
9579 /* Make room for the .rela.plt.unloaded relocations. */
9580 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9581 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9583 /* All relocations against this symbol that could have been made
9584 dynamic will now refer to the PLT entry instead. */
9585 hmips->possibly_dynamic_relocs = 0;
9587 return true;
9590 /* If this is a weak symbol, and there is a real definition, the
9591 processor independent code will have arranged for us to see the
9592 real definition first, and we can just use the same value. */
9593 if (h->is_weakalias)
9595 struct elf_link_hash_entry *def = weakdef (h);
9596 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9597 h->root.u.def.section = def->root.u.def.section;
9598 h->root.u.def.value = def->root.u.def.value;
9599 return true;
9602 /* Otherwise, there is nothing further to do for symbols defined
9603 in regular objects. */
9604 if (h->def_regular)
9605 return true;
9607 /* There's also nothing more to do if we'll convert all relocations
9608 against this symbol into dynamic relocations. */
9609 if (!hmips->has_static_relocs)
9610 return true;
9612 /* We're now relying on copy relocations. Complain if we have
9613 some that we can't convert. */
9614 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9616 _bfd_error_handler (_("non-dynamic relocations refer to "
9617 "dynamic symbol %s"),
9618 h->root.root.string);
9619 bfd_set_error (bfd_error_bad_value);
9620 return false;
9623 /* We must allocate the symbol in our .dynbss section, which will
9624 become part of the .bss section of the executable. There will be
9625 an entry for this symbol in the .dynsym section. The dynamic
9626 object will contain position independent code, so all references
9627 from the dynamic object to this symbol will go through the global
9628 offset table. The dynamic linker will use the .dynsym entry to
9629 determine the address it must put in the global offset table, so
9630 both the dynamic object and the regular object will refer to the
9631 same memory location for the variable. */
9633 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9635 s = htab->root.sdynrelro;
9636 srel = htab->root.sreldynrelro;
9638 else
9640 s = htab->root.sdynbss;
9641 srel = htab->root.srelbss;
9643 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9645 if (htab->root.target_os == is_vxworks)
9646 srel->size += sizeof (Elf32_External_Rela);
9647 else
9648 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9649 h->needs_copy = 1;
9652 /* All relocations against this symbol that could have been made
9653 dynamic will now refer to the local copy instead. */
9654 hmips->possibly_dynamic_relocs = 0;
9656 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9659 /* If the link uses a GOT, lay it out and work out its size. */
9661 static bool
9662 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9664 bfd *dynobj;
9665 asection *s;
9666 struct mips_got_info *g;
9667 bfd_size_type loadable_size = 0;
9668 bfd_size_type page_gotno;
9669 bfd *ibfd;
9670 struct mips_elf_traverse_got_arg tga;
9671 struct mips_elf_link_hash_table *htab;
9673 htab = mips_elf_hash_table (info);
9674 BFD_ASSERT (htab != NULL);
9676 s = htab->root.sgot;
9677 if (s == NULL)
9678 return true;
9680 dynobj = elf_hash_table (info)->dynobj;
9681 g = htab->got_info;
9683 /* Allocate room for the reserved entries. VxWorks always reserves
9684 3 entries; other objects only reserve 2 entries. */
9685 BFD_ASSERT (g->assigned_low_gotno == 0);
9686 if (htab->root.target_os == is_vxworks)
9687 htab->reserved_gotno = 3;
9688 else
9689 htab->reserved_gotno = 2;
9690 g->local_gotno += htab->reserved_gotno;
9691 g->assigned_low_gotno = htab->reserved_gotno;
9693 /* Decide which symbols need to go in the global part of the GOT and
9694 count the number of reloc-only GOT symbols. */
9695 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9697 if (!mips_elf_resolve_final_got_entries (info, g))
9698 return false;
9700 /* Calculate the total loadable size of the output. That
9701 will give us the maximum number of GOT_PAGE entries
9702 required. */
9703 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9705 asection *subsection;
9707 for (subsection = ibfd->sections;
9708 subsection;
9709 subsection = subsection->next)
9711 if ((subsection->flags & SEC_ALLOC) == 0)
9712 continue;
9713 loadable_size += ((subsection->size + 0xf)
9714 &~ (bfd_size_type) 0xf);
9718 if (htab->root.target_os == is_vxworks)
9719 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9720 relocations against local symbols evaluate to "G", and the EABI does
9721 not include R_MIPS_GOT_PAGE. */
9722 page_gotno = 0;
9723 else
9724 /* Assume there are two loadable segments consisting of contiguous
9725 sections. Is 5 enough? */
9726 page_gotno = (loadable_size >> 16) + 5;
9728 /* Choose the smaller of the two page estimates; both are intended to be
9729 conservative. */
9730 if (page_gotno > g->page_gotno)
9731 page_gotno = g->page_gotno;
9733 g->local_gotno += page_gotno;
9734 g->assigned_high_gotno = g->local_gotno - 1;
9736 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9737 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9738 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9740 /* VxWorks does not support multiple GOTs. It initializes $gp to
9741 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9742 dynamic loader. */
9743 if (htab->root.target_os != is_vxworks
9744 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9746 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9747 return false;
9749 else
9751 /* Record that all bfds use G. This also has the effect of freeing
9752 the per-bfd GOTs, which we no longer need. */
9753 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9754 if (mips_elf_bfd_got (ibfd, false))
9755 mips_elf_replace_bfd_got (ibfd, g);
9756 mips_elf_replace_bfd_got (output_bfd, g);
9758 /* Set up TLS entries. */
9759 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9760 tga.info = info;
9761 tga.g = g;
9762 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9763 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9764 if (!tga.g)
9765 return false;
9766 BFD_ASSERT (g->tls_assigned_gotno
9767 == g->global_gotno + g->local_gotno + g->tls_gotno);
9769 /* Each VxWorks GOT entry needs an explicit relocation. */
9770 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9771 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9773 /* Allocate room for the TLS relocations. */
9774 if (g->relocs)
9775 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9778 return true;
9781 /* Estimate the size of the .MIPS.stubs section. */
9783 static void
9784 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9786 struct mips_elf_link_hash_table *htab;
9787 bfd_size_type dynsymcount;
9789 htab = mips_elf_hash_table (info);
9790 BFD_ASSERT (htab != NULL);
9792 if (htab->lazy_stub_count == 0)
9793 return;
9795 /* IRIX rld assumes that a function stub isn't at the end of the .text
9796 section, so add a dummy entry to the end. */
9797 htab->lazy_stub_count++;
9799 /* Get a worst-case estimate of the number of dynamic symbols needed.
9800 At this point, dynsymcount does not account for section symbols
9801 and count_section_dynsyms may overestimate the number that will
9802 be needed. */
9803 dynsymcount = (elf_hash_table (info)->dynsymcount
9804 + count_section_dynsyms (output_bfd, info));
9806 /* Determine the size of one stub entry. There's no disadvantage
9807 from using microMIPS code here, so for the sake of pure-microMIPS
9808 binaries we prefer it whenever there's any microMIPS code in
9809 output produced at all. This has a benefit of stubs being
9810 shorter by 4 bytes each too, unless in the insn32 mode. */
9811 if (!MICROMIPS_P (output_bfd))
9812 htab->function_stub_size = (dynsymcount > 0x10000
9813 ? MIPS_FUNCTION_STUB_BIG_SIZE
9814 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9815 else if (htab->insn32)
9816 htab->function_stub_size = (dynsymcount > 0x10000
9817 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9818 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9819 else
9820 htab->function_stub_size = (dynsymcount > 0x10000
9821 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9822 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9824 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9827 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9828 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9829 stub, allocate an entry in the stubs section. */
9831 static bool
9832 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9834 struct mips_htab_traverse_info *hti = data;
9835 struct mips_elf_link_hash_table *htab;
9836 struct bfd_link_info *info;
9837 bfd *output_bfd;
9839 info = hti->info;
9840 output_bfd = hti->output_bfd;
9841 htab = mips_elf_hash_table (info);
9842 BFD_ASSERT (htab != NULL);
9844 if (h->needs_lazy_stub)
9846 bool micromips_p = MICROMIPS_P (output_bfd);
9847 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9848 bfd_vma isa_bit = micromips_p;
9850 BFD_ASSERT (htab->root.dynobj != NULL);
9851 if (h->root.plt.plist == NULL)
9852 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9853 if (h->root.plt.plist == NULL)
9855 hti->error = true;
9856 return false;
9858 h->root.root.u.def.section = htab->sstubs;
9859 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9860 h->root.plt.plist->stub_offset = htab->sstubs->size;
9861 h->root.other = other;
9862 htab->sstubs->size += htab->function_stub_size;
9864 return true;
9867 /* Allocate offsets in the stubs section to each symbol that needs one.
9868 Set the final size of the .MIPS.stub section. */
9870 static bool
9871 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9873 bfd *output_bfd = info->output_bfd;
9874 bool micromips_p = MICROMIPS_P (output_bfd);
9875 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9876 bfd_vma isa_bit = micromips_p;
9877 struct mips_elf_link_hash_table *htab;
9878 struct mips_htab_traverse_info hti;
9879 struct elf_link_hash_entry *h;
9880 bfd *dynobj;
9882 htab = mips_elf_hash_table (info);
9883 BFD_ASSERT (htab != NULL);
9885 if (htab->lazy_stub_count == 0)
9886 return true;
9888 htab->sstubs->size = 0;
9889 hti.info = info;
9890 hti.output_bfd = output_bfd;
9891 hti.error = false;
9892 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9893 if (hti.error)
9894 return false;
9895 htab->sstubs->size += htab->function_stub_size;
9896 BFD_ASSERT (htab->sstubs->size
9897 == htab->lazy_stub_count * htab->function_stub_size);
9899 dynobj = elf_hash_table (info)->dynobj;
9900 BFD_ASSERT (dynobj != NULL);
9901 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9902 if (h == NULL)
9903 return false;
9904 h->root.u.def.value = isa_bit;
9905 h->other = other;
9906 h->type = STT_FUNC;
9908 return true;
9911 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9912 bfd_link_info. If H uses the address of a PLT entry as the value
9913 of the symbol, then set the entry in the symbol table now. Prefer
9914 a standard MIPS PLT entry. */
9916 static bool
9917 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9919 struct bfd_link_info *info = data;
9920 bool micromips_p = MICROMIPS_P (info->output_bfd);
9921 struct mips_elf_link_hash_table *htab;
9922 unsigned int other;
9923 bfd_vma isa_bit;
9924 bfd_vma val;
9926 htab = mips_elf_hash_table (info);
9927 BFD_ASSERT (htab != NULL);
9929 if (h->use_plt_entry)
9931 BFD_ASSERT (h->root.plt.plist != NULL);
9932 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9933 || h->root.plt.plist->comp_offset != MINUS_ONE);
9935 val = htab->plt_header_size;
9936 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9938 isa_bit = 0;
9939 val += h->root.plt.plist->mips_offset;
9940 other = 0;
9942 else
9944 isa_bit = 1;
9945 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9946 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9948 val += isa_bit;
9949 /* For VxWorks, point at the PLT load stub rather than the lazy
9950 resolution stub; this stub will become the canonical function
9951 address. */
9952 if (htab->root.target_os == is_vxworks)
9953 val += 8;
9955 h->root.root.u.def.section = htab->root.splt;
9956 h->root.root.u.def.value = val;
9957 h->root.other = other;
9960 return true;
9963 /* Set the sizes of the dynamic sections, some mips non-dynamic sections,
9964 and check for any mips16 stub sections that we can discard. */
9966 bool
9967 _bfd_mips_elf_late_size_sections (bfd *output_bfd,
9968 struct bfd_link_info *info)
9970 bfd *dynobj;
9971 asection *s, *sreldyn;
9972 bool reltext;
9973 struct mips_elf_link_hash_table *htab;
9974 struct mips_htab_traverse_info hti;
9976 htab = mips_elf_hash_table (info);
9977 BFD_ASSERT (htab != NULL);
9979 /* The .reginfo section has a fixed size. */
9980 s = bfd_get_section_by_name (output_bfd, ".reginfo");
9981 if (s != NULL)
9983 bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
9984 s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9987 /* The .MIPS.abiflags section has a fixed size. */
9988 s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9989 if (s != NULL)
9991 bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
9992 s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9995 hti.info = info;
9996 hti.output_bfd = output_bfd;
9997 hti.error = false;
9998 mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
9999 if (hti.error)
10000 return false;
10002 dynobj = htab->root.dynobj;
10003 if (dynobj == NULL)
10004 return true;
10006 if (htab->root.dynamic_sections_created)
10008 /* Set the contents of the .interp section to the interpreter. */
10009 if (bfd_link_executable (info) && !info->nointerp)
10011 s = bfd_get_linker_section (dynobj, ".interp");
10012 BFD_ASSERT (s != NULL);
10013 s->size
10014 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10015 s->contents
10016 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10019 /* Figure out the size of the PLT header if we know that we
10020 are using it. For the sake of cache alignment always use
10021 a standard header whenever any standard entries are present
10022 even if microMIPS entries are present as well. This also
10023 lets the microMIPS header rely on the value of $v0 only set
10024 by microMIPS entries, for a small size reduction.
10026 Set symbol table entry values for symbols that use the
10027 address of their PLT entry now that we can calculate it.
10029 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10030 haven't already in _bfd_elf_create_dynamic_sections. */
10031 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10033 bool micromips_p = (MICROMIPS_P (output_bfd)
10034 && !htab->plt_mips_offset);
10035 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10036 bfd_vma isa_bit = micromips_p;
10037 struct elf_link_hash_entry *h;
10038 bfd_vma size;
10040 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10041 BFD_ASSERT (htab->root.sgotplt->size == 0);
10042 BFD_ASSERT (htab->root.splt->size == 0);
10044 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10045 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10046 else if (htab->root.target_os == is_vxworks)
10047 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10048 else if (ABI_64_P (output_bfd))
10049 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10050 else if (ABI_N32_P (output_bfd))
10051 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10052 else if (!micromips_p)
10053 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10054 else if (htab->insn32)
10055 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10056 else
10057 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10059 htab->plt_header_is_comp = micromips_p;
10060 htab->plt_header_size = size;
10061 htab->root.splt->size = (size
10062 + htab->plt_mips_offset
10063 + htab->plt_comp_offset);
10064 htab->root.sgotplt->size = (htab->plt_got_index
10065 * MIPS_ELF_GOT_SIZE (dynobj));
10067 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10069 if (htab->root.hplt == NULL)
10071 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10072 "_PROCEDURE_LINKAGE_TABLE_");
10073 htab->root.hplt = h;
10074 if (h == NULL)
10075 return false;
10078 h = htab->root.hplt;
10079 h->root.u.def.value = isa_bit;
10080 h->other = other;
10081 h->type = STT_FUNC;
10085 /* Allocate space for global sym dynamic relocs. */
10086 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10088 mips_elf_estimate_stub_size (output_bfd, info);
10090 if (!mips_elf_lay_out_got (output_bfd, info))
10091 return false;
10093 mips_elf_lay_out_lazy_stubs (info);
10095 /* The check_relocs and adjust_dynamic_symbol entry points have
10096 determined the sizes of the various dynamic sections. Allocate
10097 memory for them. */
10098 reltext = false;
10099 for (s = dynobj->sections; s != NULL; s = s->next)
10101 const char *name;
10103 /* It's OK to base decisions on the section name, because none
10104 of the dynobj section names depend upon the input files. */
10105 name = bfd_section_name (s);
10107 if ((s->flags & SEC_LINKER_CREATED) == 0)
10108 continue;
10110 if (startswith (name, ".rel"))
10112 if (s->size != 0)
10114 const char *outname;
10115 asection *target;
10117 /* If this relocation section applies to a read only
10118 section, then we probably need a DT_TEXTREL entry.
10119 If the relocation section is .rel(a).dyn, we always
10120 assert a DT_TEXTREL entry rather than testing whether
10121 there exists a relocation to a read only section or
10122 not. */
10123 outname = bfd_section_name (s->output_section);
10124 target = bfd_get_section_by_name (output_bfd, outname + 4);
10125 if ((target != NULL
10126 && (target->flags & SEC_READONLY) != 0
10127 && (target->flags & SEC_ALLOC) != 0)
10128 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10129 reltext = true;
10131 /* We use the reloc_count field as a counter if we need
10132 to copy relocs into the output file. */
10133 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10134 s->reloc_count = 0;
10136 /* If combreloc is enabled, elf_link_sort_relocs() will
10137 sort relocations, but in a different way than we do,
10138 and before we're done creating relocations. Also, it
10139 will move them around between input sections'
10140 relocation's contents, so our sorting would be
10141 broken, so don't let it run. */
10142 info->combreloc = 0;
10145 else if (bfd_link_executable (info)
10146 && !htab->use_rld_obj_head
10147 && startswith (name, ".rld_map"))
10149 /* We add a room for __rld_map. It will be filled in by the
10150 rtld to contain a pointer to the _r_debug structure. */
10151 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10153 else if (SGI_COMPAT (output_bfd)
10154 && startswith (name, ".compact_rel"))
10155 s->size += htab->compact_rel_size;
10156 else if (s == htab->root.splt)
10158 /* If the last PLT entry has a branch delay slot, allocate
10159 room for an extra nop to fill the delay slot. This is
10160 for CPUs without load interlocking. */
10161 if (! LOAD_INTERLOCKS_P (output_bfd)
10162 && htab->root.target_os != is_vxworks
10163 && s->size > 0)
10164 s->size += 4;
10166 else if (! startswith (name, ".init")
10167 && s != htab->root.sgot
10168 && s != htab->root.sgotplt
10169 && s != htab->sstubs
10170 && s != htab->root.sdynbss
10171 && s != htab->root.sdynrelro)
10173 /* It's not one of our sections, so don't allocate space. */
10174 continue;
10177 if (s->size == 0)
10179 s->flags |= SEC_EXCLUDE;
10180 continue;
10183 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10184 continue;
10186 /* Allocate memory for the section contents. */
10187 s->contents = bfd_zalloc (dynobj, s->size);
10188 if (s->contents == NULL)
10190 bfd_set_error (bfd_error_no_memory);
10191 return false;
10195 if (htab->root.dynamic_sections_created)
10197 /* Add some entries to the .dynamic section. We fill in the
10198 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10199 must add the entries now so that we get the correct size for
10200 the .dynamic section. */
10202 /* SGI object has the equivalence of DT_DEBUG in the
10203 DT_MIPS_RLD_MAP entry. This must come first because glibc
10204 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10205 may only look at the first one they see. */
10206 if (!bfd_link_pic (info)
10207 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10208 return false;
10210 if (bfd_link_executable (info)
10211 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10212 return false;
10214 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10215 used by the debugger. */
10216 if (bfd_link_executable (info)
10217 && !SGI_COMPAT (output_bfd)
10218 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10219 return false;
10221 if (reltext
10222 && (SGI_COMPAT (output_bfd)
10223 || htab->root.target_os == is_vxworks))
10224 info->flags |= DF_TEXTREL;
10226 if ((info->flags & DF_TEXTREL) != 0)
10228 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10229 return false;
10231 /* Clear the DF_TEXTREL flag. It will be set again if we
10232 write out an actual text relocation; we may not, because
10233 at this point we do not know whether e.g. any .eh_frame
10234 absolute relocations have been converted to PC-relative. */
10235 info->flags &= ~DF_TEXTREL;
10238 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10239 return false;
10241 sreldyn = mips_elf_rel_dyn_section (info, false);
10242 if (htab->root.target_os == is_vxworks)
10244 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10245 use any of the DT_MIPS_* tags. */
10246 if (sreldyn && sreldyn->size > 0)
10248 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10249 return false;
10251 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10252 return false;
10254 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10255 return false;
10258 else
10260 if (sreldyn && sreldyn->size > 0
10261 && !bfd_is_abs_section (sreldyn->output_section))
10263 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10264 return false;
10266 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10267 return false;
10269 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10270 return false;
10273 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10274 return false;
10276 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10277 return false;
10279 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10280 return false;
10282 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10283 return false;
10285 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10286 return false;
10288 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10289 return false;
10291 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10292 return false;
10294 if (info->emit_gnu_hash
10295 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10296 return false;
10298 if (IRIX_COMPAT (dynobj) == ict_irix5
10299 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10300 return false;
10302 if (IRIX_COMPAT (dynobj) == ict_irix6
10303 && (bfd_get_section_by_name
10304 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10305 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10306 return false;
10308 if (htab->root.splt->size > 0)
10310 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10311 return false;
10313 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10314 return false;
10316 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10317 return false;
10319 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10320 return false;
10322 if (htab->root.target_os == is_vxworks
10323 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10324 return false;
10327 return true;
10330 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10331 Adjust its R_ADDEND field so that it is correct for the output file.
10332 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10333 and sections respectively; both use symbol indexes. */
10335 static void
10336 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10337 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10338 asection **local_sections, Elf_Internal_Rela *rel)
10340 unsigned int r_type, r_symndx;
10341 Elf_Internal_Sym *sym;
10342 asection *sec;
10344 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10346 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10347 if (gprel16_reloc_p (r_type)
10348 || r_type == R_MIPS_GPREL32
10349 || literal_reloc_p (r_type))
10351 rel->r_addend += _bfd_get_gp_value (input_bfd);
10352 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10355 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10356 sym = local_syms + r_symndx;
10358 /* Adjust REL's addend to account for section merging. */
10359 if (!bfd_link_relocatable (info))
10361 sec = local_sections[r_symndx];
10362 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10365 /* This would normally be done by the rela_normal code in elflink.c. */
10366 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10367 rel->r_addend += local_sections[r_symndx]->output_offset;
10371 /* Handle relocations against symbols from removed linkonce sections,
10372 or sections discarded by a linker script. We use this wrapper around
10373 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10374 on 64-bit ELF targets. In this case for any relocation handled, which
10375 always be the first in a triplet, the remaining two have to be processed
10376 together with the first, even if they are R_MIPS_NONE. It is the symbol
10377 index referred by the first reloc that applies to all the three and the
10378 remaining two never refer to an object symbol. And it is the final
10379 relocation (the last non-null one) that determines the output field of
10380 the whole relocation so retrieve the corresponding howto structure for
10381 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10383 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10384 and therefore requires to be pasted in a loop. It also defines a block
10385 and does not protect any of its arguments, hence the extra brackets. */
10387 static void
10388 mips_reloc_against_discarded_section (bfd *output_bfd,
10389 struct bfd_link_info *info,
10390 bfd *input_bfd, asection *input_section,
10391 Elf_Internal_Rela **rel,
10392 const Elf_Internal_Rela **relend,
10393 bool rel_reloc,
10394 reloc_howto_type *howto,
10395 bfd_byte *contents)
10397 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10398 int count = bed->s->int_rels_per_ext_rel;
10399 unsigned int r_type;
10400 int i;
10402 for (i = count - 1; i > 0; i--)
10404 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10405 if (r_type != R_MIPS_NONE)
10407 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10408 break;
10413 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10414 (*rel), count, (*relend),
10415 howto, i, contents);
10417 while (0);
10420 /* Relocate a MIPS ELF section. */
10423 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10424 bfd *input_bfd, asection *input_section,
10425 bfd_byte *contents, Elf_Internal_Rela *relocs,
10426 Elf_Internal_Sym *local_syms,
10427 asection **local_sections)
10429 Elf_Internal_Rela *rel;
10430 const Elf_Internal_Rela *relend;
10431 bfd_vma addend = 0;
10432 bool use_saved_addend_p = false;
10434 relend = relocs + input_section->reloc_count;
10435 for (rel = relocs; rel < relend; ++rel)
10437 const char *name;
10438 bfd_vma value = 0;
10439 reloc_howto_type *howto;
10440 bool cross_mode_jump_p = false;
10441 /* TRUE if the relocation is a RELA relocation, rather than a
10442 REL relocation. */
10443 bool rela_relocation_p = true;
10444 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10445 const char *msg;
10446 unsigned long r_symndx;
10447 asection *sec;
10448 Elf_Internal_Shdr *symtab_hdr;
10449 struct elf_link_hash_entry *h;
10450 bool rel_reloc;
10452 rel_reloc = (NEWABI_P (input_bfd)
10453 && mips_elf_rel_relocation_p (input_bfd, input_section,
10454 relocs, rel));
10455 /* Find the relocation howto for this relocation. */
10456 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10458 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10459 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10460 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10462 sec = local_sections[r_symndx];
10463 h = NULL;
10465 else
10467 unsigned long extsymoff;
10469 extsymoff = 0;
10470 if (!elf_bad_symtab (input_bfd))
10471 extsymoff = symtab_hdr->sh_info;
10472 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10473 while (h->root.type == bfd_link_hash_indirect
10474 || h->root.type == bfd_link_hash_warning)
10475 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10477 sec = NULL;
10478 if (h->root.type == bfd_link_hash_defined
10479 || h->root.type == bfd_link_hash_defweak)
10480 sec = h->root.u.def.section;
10483 if (sec != NULL && discarded_section (sec))
10485 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10486 input_section, &rel, &relend,
10487 rel_reloc, howto, contents);
10488 continue;
10491 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10493 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10494 64-bit code, but make sure all their addresses are in the
10495 lowermost or uppermost 32-bit section of the 64-bit address
10496 space. Thus, when they use an R_MIPS_64 they mean what is
10497 usually meant by R_MIPS_32, with the exception that the
10498 stored value is sign-extended to 64 bits. */
10499 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10501 /* On big-endian systems, we need to lie about the position
10502 of the reloc. */
10503 if (bfd_big_endian (input_bfd))
10504 rel->r_offset += 4;
10507 if (!use_saved_addend_p)
10509 /* If these relocations were originally of the REL variety,
10510 we must pull the addend out of the field that will be
10511 relocated. Otherwise, we simply use the contents of the
10512 RELA relocation. */
10513 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10514 relocs, rel))
10516 rela_relocation_p = false;
10517 addend = mips_elf_read_rel_addend (input_bfd, input_section,
10518 rel, howto, contents);
10519 if (hi16_reloc_p (r_type)
10520 || (got16_reloc_p (r_type)
10521 && mips_elf_local_relocation_p (input_bfd, rel,
10522 local_sections)))
10524 if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10525 rel, relend,
10526 contents, &addend))
10528 if (h)
10529 name = h->root.root.string;
10530 else
10531 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10532 local_syms + r_symndx,
10533 sec);
10534 _bfd_error_handler
10535 /* xgettext:c-format */
10536 (_("%pB: can't find matching LO16 reloc against `%s'"
10537 " for %s at %#" PRIx64 " in section `%pA'"),
10538 input_bfd, name,
10539 howto->name, (uint64_t) rel->r_offset, input_section);
10542 else
10543 addend <<= howto->rightshift;
10545 else
10546 addend = rel->r_addend;
10547 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10548 local_syms, local_sections, rel);
10551 if (bfd_link_relocatable (info))
10553 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10554 && bfd_big_endian (input_bfd))
10555 rel->r_offset -= 4;
10557 if (!rela_relocation_p && rel->r_addend)
10559 addend += rel->r_addend;
10560 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10561 addend = mips_elf_high (addend);
10562 else if (r_type == R_MIPS_HIGHER)
10563 addend = mips_elf_higher (addend);
10564 else if (r_type == R_MIPS_HIGHEST)
10565 addend = mips_elf_highest (addend);
10566 else
10567 addend >>= howto->rightshift;
10569 /* We use the source mask, rather than the destination
10570 mask because the place to which we are writing will be
10571 source of the addend in the final link. */
10572 addend &= howto->src_mask;
10574 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10575 /* See the comment above about using R_MIPS_64 in the 32-bit
10576 ABI. Here, we need to update the addend. It would be
10577 possible to get away with just using the R_MIPS_32 reloc
10578 but for endianness. */
10580 bfd_vma sign_bits;
10581 bfd_vma low_bits;
10582 bfd_vma high_bits;
10584 if (addend & ((bfd_vma) 1 << 31))
10585 #ifdef BFD64
10586 sign_bits = ((bfd_vma) 1 << 32) - 1;
10587 #else
10588 sign_bits = -1;
10589 #endif
10590 else
10591 sign_bits = 0;
10593 /* If we don't know that we have a 64-bit type,
10594 do two separate stores. */
10595 if (bfd_big_endian (input_bfd))
10597 /* Store the sign-bits (which are most significant)
10598 first. */
10599 low_bits = sign_bits;
10600 high_bits = addend;
10602 else
10604 low_bits = addend;
10605 high_bits = sign_bits;
10607 bfd_put_32 (input_bfd, low_bits,
10608 contents + rel->r_offset);
10609 bfd_put_32 (input_bfd, high_bits,
10610 contents + rel->r_offset + 4);
10611 continue;
10614 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10615 input_bfd, input_section,
10616 contents, false))
10617 return false;
10620 /* Go on to the next relocation. */
10621 continue;
10624 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10625 relocations for the same offset. In that case we are
10626 supposed to treat the output of each relocation as the addend
10627 for the next. */
10628 if (rel + 1 < relend
10629 && rel->r_offset == rel[1].r_offset
10630 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10631 use_saved_addend_p = true;
10632 else
10633 use_saved_addend_p = false;
10635 /* Figure out what value we are supposed to relocate. */
10636 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10637 input_section, contents,
10638 info, rel, addend, howto,
10639 local_syms, local_sections,
10640 &value, &name, &cross_mode_jump_p,
10641 use_saved_addend_p))
10643 case bfd_reloc_continue:
10644 /* There's nothing to do. */
10645 continue;
10647 case bfd_reloc_undefined:
10648 /* mips_elf_calculate_relocation already called the
10649 undefined_symbol callback. There's no real point in
10650 trying to perform the relocation at this point, so we
10651 just skip ahead to the next relocation. */
10652 continue;
10654 case bfd_reloc_notsupported:
10655 msg = _("internal error: unsupported relocation error");
10656 info->callbacks->warning
10657 (info, msg, name, input_bfd, input_section, rel->r_offset);
10658 return false;
10660 case bfd_reloc_overflow:
10661 if (use_saved_addend_p)
10662 /* Ignore overflow until we reach the last relocation for
10663 a given location. */
10665 else
10667 struct mips_elf_link_hash_table *htab;
10669 htab = mips_elf_hash_table (info);
10670 BFD_ASSERT (htab != NULL);
10671 BFD_ASSERT (name != NULL);
10672 if (!htab->small_data_overflow_reported
10673 && (gprel16_reloc_p (howto->type)
10674 || literal_reloc_p (howto->type)))
10676 msg = _("small-data section too large;"
10677 " lower small-data size limit (see option -G)");
10679 htab->small_data_overflow_reported = true;
10680 (*info->callbacks->einfo) ("%P: %s\n", msg);
10682 (*info->callbacks->reloc_overflow)
10683 (info, NULL, name, howto->name, (bfd_vma) 0,
10684 input_bfd, input_section, rel->r_offset);
10686 break;
10688 case bfd_reloc_ok:
10689 break;
10691 case bfd_reloc_outofrange:
10692 msg = NULL;
10693 if (jal_reloc_p (howto->type))
10694 msg = (cross_mode_jump_p
10695 ? _("cannot convert a jump to JALX "
10696 "for a non-word-aligned address")
10697 : (howto->type == R_MIPS16_26
10698 ? _("jump to a non-word-aligned address")
10699 : _("jump to a non-instruction-aligned address")));
10700 else if (b_reloc_p (howto->type))
10701 msg = (cross_mode_jump_p
10702 ? _("cannot convert a branch to JALX "
10703 "for a non-word-aligned address")
10704 : _("branch to a non-instruction-aligned address"));
10705 else if (aligned_pcrel_reloc_p (howto->type))
10706 msg = _("PC-relative load from unaligned address");
10707 if (msg)
10709 info->callbacks->einfo
10710 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10711 break;
10713 /* Fall through. */
10715 default:
10716 abort ();
10717 break;
10720 /* If we've got another relocation for the address, keep going
10721 until we reach the last one. */
10722 if (use_saved_addend_p)
10724 addend = value;
10725 continue;
10728 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10729 /* See the comment above about using R_MIPS_64 in the 32-bit
10730 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10731 that calculated the right value. Now, however, we
10732 sign-extend the 32-bit result to 64-bits, and store it as a
10733 64-bit value. We are especially generous here in that we
10734 go to extreme lengths to support this usage on systems with
10735 only a 32-bit VMA. */
10737 bfd_vma sign_bits;
10738 bfd_vma low_bits;
10739 bfd_vma high_bits;
10741 if (value & ((bfd_vma) 1 << 31))
10742 #ifdef BFD64
10743 sign_bits = ((bfd_vma) 1 << 32) - 1;
10744 #else
10745 sign_bits = -1;
10746 #endif
10747 else
10748 sign_bits = 0;
10750 /* If we don't know that we have a 64-bit type,
10751 do two separate stores. */
10752 if (bfd_big_endian (input_bfd))
10754 /* Undo what we did above. */
10755 rel->r_offset -= 4;
10756 /* Store the sign-bits (which are most significant)
10757 first. */
10758 low_bits = sign_bits;
10759 high_bits = value;
10761 else
10763 low_bits = value;
10764 high_bits = sign_bits;
10766 bfd_put_32 (input_bfd, low_bits,
10767 contents + rel->r_offset);
10768 bfd_put_32 (input_bfd, high_bits,
10769 contents + rel->r_offset + 4);
10770 continue;
10773 /* Actually perform the relocation. */
10774 if (! mips_elf_perform_relocation (info, howto, rel, value,
10775 input_bfd, input_section,
10776 contents, cross_mode_jump_p))
10777 return false;
10780 return true;
10783 /* A function that iterates over each entry in la25_stubs and fills
10784 in the code for each one. DATA points to a mips_htab_traverse_info. */
10786 static int
10787 mips_elf_create_la25_stub (void **slot, void *data)
10789 struct mips_htab_traverse_info *hti;
10790 struct mips_elf_link_hash_table *htab;
10791 struct mips_elf_la25_stub *stub;
10792 asection *s;
10793 bfd_byte *loc;
10794 bfd_vma offset, target, target_high, target_low;
10795 bfd_vma branch_pc;
10796 bfd_signed_vma pcrel_offset = 0;
10798 stub = (struct mips_elf_la25_stub *) *slot;
10799 hti = (struct mips_htab_traverse_info *) data;
10800 htab = mips_elf_hash_table (hti->info);
10801 BFD_ASSERT (htab != NULL);
10803 /* Create the section contents, if we haven't already. */
10804 s = stub->stub_section;
10805 loc = s->contents;
10806 if (loc == NULL)
10808 loc = bfd_malloc (s->size);
10809 if (loc == NULL)
10811 hti->error = true;
10812 return false;
10814 s->contents = loc;
10817 /* Work out where in the section this stub should go. */
10818 offset = stub->offset;
10820 /* We add 8 here to account for the LUI/ADDIU instructions
10821 before the branch instruction. This cannot be moved down to
10822 where pcrel_offset is calculated as 's' is updated in
10823 mips_elf_get_la25_target. */
10824 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10826 /* Work out the target address. */
10827 target = mips_elf_get_la25_target (stub, &s);
10828 target += s->output_section->vma + s->output_offset;
10830 target_high = ((target + 0x8000) >> 16) & 0xffff;
10831 target_low = (target & 0xffff);
10833 /* Calculate the PC of the compact branch instruction (for the case where
10834 compact branches are used for either microMIPSR6 or MIPSR6 with
10835 compact branches. Add 4-bytes to account for BC using the PC of the
10836 next instruction as the base. */
10837 pcrel_offset = target - (branch_pc + 4);
10839 if (stub->stub_section != htab->strampoline)
10841 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10842 of the section and write the two instructions at the end. */
10843 memset (loc, 0, offset);
10844 loc += offset;
10845 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10847 bfd_put_micromips_32 (hti->output_bfd,
10848 LA25_LUI_MICROMIPS (target_high),
10849 loc);
10850 bfd_put_micromips_32 (hti->output_bfd,
10851 LA25_ADDIU_MICROMIPS (target_low),
10852 loc + 4);
10854 else
10856 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10857 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10860 else
10862 /* This is trampoline. */
10863 loc += offset;
10864 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10866 bfd_put_micromips_32 (hti->output_bfd,
10867 LA25_LUI_MICROMIPS (target_high), loc);
10868 bfd_put_micromips_32 (hti->output_bfd,
10869 LA25_J_MICROMIPS (target), loc + 4);
10870 bfd_put_micromips_32 (hti->output_bfd,
10871 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10872 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10874 else
10876 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10877 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10879 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10880 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10882 else
10884 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10885 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10887 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10890 return true;
10893 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10894 adjust it appropriately now. */
10896 static void
10897 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10898 const char *name, Elf_Internal_Sym *sym)
10900 /* The linker script takes care of providing names and values for
10901 these, but we must place them into the right sections. */
10902 static const char* const text_section_symbols[] = {
10903 "_ftext",
10904 "_etext",
10905 "__dso_displacement",
10906 "__elf_header",
10907 "__program_header_table",
10908 NULL
10911 static const char* const data_section_symbols[] = {
10912 "_fdata",
10913 "_edata",
10914 "_end",
10915 "_fbss",
10916 NULL
10919 const char* const *p;
10920 int i;
10922 for (i = 0; i < 2; ++i)
10923 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10925 ++p)
10926 if (strcmp (*p, name) == 0)
10928 /* All of these symbols are given type STT_SECTION by the
10929 IRIX6 linker. */
10930 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10931 sym->st_other = STO_PROTECTED;
10933 /* The IRIX linker puts these symbols in special sections. */
10934 if (i == 0)
10935 sym->st_shndx = SHN_MIPS_TEXT;
10936 else
10937 sym->st_shndx = SHN_MIPS_DATA;
10939 break;
10943 /* Finish up dynamic symbol handling. We set the contents of various
10944 dynamic sections here. */
10946 bool
10947 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10948 struct bfd_link_info *info,
10949 struct elf_link_hash_entry *h,
10950 Elf_Internal_Sym *sym)
10952 bfd *dynobj;
10953 asection *sgot;
10954 struct mips_got_info *g, *gg;
10955 const char *name;
10956 int idx;
10957 struct mips_elf_link_hash_table *htab;
10958 struct mips_elf_link_hash_entry *hmips;
10960 htab = mips_elf_hash_table (info);
10961 BFD_ASSERT (htab != NULL);
10962 dynobj = elf_hash_table (info)->dynobj;
10963 hmips = (struct mips_elf_link_hash_entry *) h;
10965 BFD_ASSERT (htab->root.target_os != is_vxworks);
10967 if (h->plt.plist != NULL
10968 && (h->plt.plist->mips_offset != MINUS_ONE
10969 || h->plt.plist->comp_offset != MINUS_ONE))
10971 /* We've decided to create a PLT entry for this symbol. */
10972 bfd_byte *loc;
10973 bfd_vma header_address, got_address;
10974 bfd_vma got_address_high, got_address_low, load;
10975 bfd_vma got_index;
10976 bfd_vma isa_bit;
10978 got_index = h->plt.plist->gotplt_index;
10980 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10981 BFD_ASSERT (h->dynindx != -1);
10982 BFD_ASSERT (htab->root.splt != NULL);
10983 BFD_ASSERT (got_index != MINUS_ONE);
10984 BFD_ASSERT (!h->def_regular);
10986 /* Calculate the address of the PLT header. */
10987 isa_bit = htab->plt_header_is_comp;
10988 header_address = (htab->root.splt->output_section->vma
10989 + htab->root.splt->output_offset + isa_bit);
10991 /* Calculate the address of the .got.plt entry. */
10992 got_address = (htab->root.sgotplt->output_section->vma
10993 + htab->root.sgotplt->output_offset
10994 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10996 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10997 got_address_low = got_address & 0xffff;
10999 /* The PLT sequence is not safe for N64 if .got.plt entry's address
11000 cannot be loaded in two instructions. */
11001 if (ABI_64_P (output_bfd)
11002 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11004 _bfd_error_handler
11005 /* xgettext:c-format */
11006 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11007 "supported; consider using `-Ttext-segment=...'"),
11008 output_bfd,
11009 htab->root.sgotplt->output_section,
11010 (int64_t) got_address);
11011 bfd_set_error (bfd_error_no_error);
11012 return false;
11015 /* Initially point the .got.plt entry at the PLT header. */
11016 loc = (htab->root.sgotplt->contents
11017 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11018 if (ABI_64_P (output_bfd))
11019 bfd_put_64 (output_bfd, header_address, loc);
11020 else
11021 bfd_put_32 (output_bfd, header_address, loc);
11023 /* Now handle the PLT itself. First the standard entry (the order
11024 does not matter, we just have to pick one). */
11025 if (h->plt.plist->mips_offset != MINUS_ONE)
11027 const bfd_vma *plt_entry;
11028 bfd_vma plt_offset;
11030 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11032 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11034 /* Find out where the .plt entry should go. */
11035 loc = htab->root.splt->contents + plt_offset;
11037 /* Pick the load opcode. */
11038 load = MIPS_ELF_LOAD_WORD (output_bfd);
11040 /* Fill in the PLT entry itself. */
11042 if (MIPSR6_P (output_bfd))
11043 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11044 : mipsr6_exec_plt_entry;
11045 else
11046 plt_entry = mips_exec_plt_entry;
11047 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11048 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11049 loc + 4);
11051 if (! LOAD_INTERLOCKS_P (output_bfd)
11052 || (MIPSR6_P (output_bfd) && htab->compact_branches))
11054 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11055 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11057 else
11059 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11060 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11061 loc + 12);
11065 /* Now the compressed entry. They come after any standard ones. */
11066 if (h->plt.plist->comp_offset != MINUS_ONE)
11068 bfd_vma plt_offset;
11070 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11071 + h->plt.plist->comp_offset);
11073 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11075 /* Find out where the .plt entry should go. */
11076 loc = htab->root.splt->contents + plt_offset;
11078 /* Fill in the PLT entry itself. */
11079 if (!MICROMIPS_P (output_bfd))
11081 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11083 bfd_put_16 (output_bfd, plt_entry[0], loc);
11084 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11085 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11086 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11087 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11088 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11089 bfd_put_32 (output_bfd, got_address, loc + 12);
11091 else if (htab->insn32)
11093 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11095 bfd_put_16 (output_bfd, plt_entry[0], loc);
11096 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11097 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11098 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11099 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11100 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11101 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11102 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11104 else
11106 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11107 bfd_signed_vma gotpc_offset;
11108 bfd_vma loc_address;
11110 BFD_ASSERT (got_address % 4 == 0);
11112 loc_address = (htab->root.splt->output_section->vma
11113 + htab->root.splt->output_offset + plt_offset);
11114 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11116 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11117 if (gotpc_offset + 0x1000000 >= 0x2000000)
11119 _bfd_error_handler
11120 /* xgettext:c-format */
11121 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11122 "beyond the range of ADDIUPC"),
11123 output_bfd,
11124 htab->root.sgotplt->output_section,
11125 (int64_t) gotpc_offset,
11126 htab->root.splt->output_section);
11127 bfd_set_error (bfd_error_no_error);
11128 return false;
11130 bfd_put_16 (output_bfd,
11131 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11132 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11133 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11134 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11135 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11136 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11140 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11141 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11142 got_index - 2, h->dynindx,
11143 R_MIPS_JUMP_SLOT, got_address);
11145 /* We distinguish between PLT entries and lazy-binding stubs by
11146 giving the former an st_other value of STO_MIPS_PLT. Set the
11147 flag and leave the value if there are any relocations in the
11148 binary where pointer equality matters. */
11149 sym->st_shndx = SHN_UNDEF;
11150 if (h->pointer_equality_needed)
11151 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11152 else
11154 sym->st_value = 0;
11155 sym->st_other = 0;
11159 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11161 /* We've decided to create a lazy-binding stub. */
11162 bool micromips_p = MICROMIPS_P (output_bfd);
11163 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11164 bfd_vma stub_size = htab->function_stub_size;
11165 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11166 bfd_vma isa_bit = micromips_p;
11167 bfd_vma stub_big_size;
11169 if (!micromips_p)
11170 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11171 else if (htab->insn32)
11172 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11173 else
11174 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11176 /* This symbol has a stub. Set it up. */
11178 BFD_ASSERT (h->dynindx != -1);
11180 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11182 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11183 sign extension at runtime in the stub, resulting in a negative
11184 index value. */
11185 if (h->dynindx & ~0x7fffffff)
11187 _bfd_error_handler
11188 (_("%pB: cannot handle more than %d dynamic symbols"),
11189 output_bfd, 0x7fffffff);
11190 bfd_set_error (bfd_error_bad_value);
11191 return false;
11194 /* Fill the stub. */
11195 if (micromips_p)
11197 idx = 0;
11198 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11199 stub + idx);
11200 idx += 4;
11201 if (htab->insn32)
11203 bfd_put_micromips_32 (output_bfd,
11204 STUB_MOVE32_MICROMIPS, stub + idx);
11205 idx += 4;
11207 else
11209 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11210 idx += 2;
11212 if (stub_size == stub_big_size)
11214 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11216 bfd_put_micromips_32 (output_bfd,
11217 STUB_LUI_MICROMIPS (dynindx_hi),
11218 stub + idx);
11219 idx += 4;
11221 if (htab->insn32)
11223 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11224 stub + idx);
11225 idx += 4;
11227 else
11229 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11230 idx += 2;
11233 /* If a large stub is not required and sign extension is not a
11234 problem, then use legacy code in the stub. */
11235 if (stub_size == stub_big_size)
11236 bfd_put_micromips_32 (output_bfd,
11237 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11238 stub + idx);
11239 else if (h->dynindx & ~0x7fff)
11240 bfd_put_micromips_32 (output_bfd,
11241 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11242 stub + idx);
11243 else
11244 bfd_put_micromips_32 (output_bfd,
11245 STUB_LI16S_MICROMIPS (output_bfd,
11246 h->dynindx),
11247 stub + idx);
11249 else
11251 idx = 0;
11252 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11253 idx += 4;
11254 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11255 idx += 4;
11256 if (stub_size == stub_big_size)
11258 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11259 stub + idx);
11260 idx += 4;
11263 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11265 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11266 idx += 4;
11269 /* If a large stub is not required and sign extension is not a
11270 problem, then use legacy code in the stub. */
11271 if (stub_size == stub_big_size)
11272 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11273 stub + idx);
11274 else if (h->dynindx & ~0x7fff)
11275 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11276 stub + idx);
11277 else
11278 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11279 stub + idx);
11280 idx += 4;
11282 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11283 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11286 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11287 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11288 stub, stub_size);
11290 /* Mark the symbol as undefined. stub_offset != -1 occurs
11291 only for the referenced symbol. */
11292 sym->st_shndx = SHN_UNDEF;
11294 /* The run-time linker uses the st_value field of the symbol
11295 to reset the global offset table entry for this external
11296 to its stub address when unlinking a shared object. */
11297 sym->st_value = (htab->sstubs->output_section->vma
11298 + htab->sstubs->output_offset
11299 + h->plt.plist->stub_offset
11300 + isa_bit);
11301 sym->st_other = other;
11304 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11305 refer to the stub, since only the stub uses the standard calling
11306 conventions. */
11307 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11309 BFD_ASSERT (hmips->need_fn_stub);
11310 sym->st_value = (hmips->fn_stub->output_section->vma
11311 + hmips->fn_stub->output_offset);
11312 sym->st_size = hmips->fn_stub->size;
11313 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11316 BFD_ASSERT (h->dynindx != -1
11317 || h->forced_local);
11319 sgot = htab->root.sgot;
11320 g = htab->got_info;
11321 BFD_ASSERT (g != NULL);
11323 /* Run through the global symbol table, creating GOT entries for all
11324 the symbols that need them. */
11325 if (hmips->global_got_area != GGA_NONE)
11327 bfd_vma offset;
11328 bfd_vma value;
11330 value = sym->st_value;
11331 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11332 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11335 if (hmips->global_got_area != GGA_NONE && g->next)
11337 struct mips_got_entry e, *p;
11338 bfd_vma entry;
11339 bfd_vma offset;
11341 gg = g;
11343 e.abfd = output_bfd;
11344 e.symndx = -1;
11345 e.d.h = hmips;
11346 e.tls_type = GOT_TLS_NONE;
11348 for (g = g->next; g->next != gg; g = g->next)
11350 if (g->got_entries
11351 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11352 &e)))
11354 offset = p->gotidx;
11355 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11356 if (bfd_link_pic (info)
11357 || (elf_hash_table (info)->dynamic_sections_created
11358 && p->d.h != NULL
11359 && p->d.h->root.def_dynamic
11360 && !p->d.h->root.def_regular))
11362 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11363 the various compatibility problems, it's easier to mock
11364 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11365 mips_elf_create_dynamic_relocation to calculate the
11366 appropriate addend. */
11367 Elf_Internal_Rela rel[3];
11369 memset (rel, 0, sizeof (rel));
11370 if (ABI_64_P (output_bfd))
11371 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11372 else
11373 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11374 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11376 entry = 0;
11377 if (! (mips_elf_create_dynamic_relocation
11378 (output_bfd, info, rel,
11379 e.d.h, NULL, sym->st_value, &entry, sgot)))
11380 return false;
11382 else
11383 entry = sym->st_value;
11384 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11389 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11390 name = h->root.root.string;
11391 if (h == elf_hash_table (info)->hdynamic
11392 || h == elf_hash_table (info)->hgot)
11393 sym->st_shndx = SHN_ABS;
11394 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11395 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11397 sym->st_shndx = SHN_ABS;
11398 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11399 sym->st_value = 1;
11401 else if (SGI_COMPAT (output_bfd))
11403 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11404 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11406 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11407 sym->st_other = STO_PROTECTED;
11408 sym->st_value = 0;
11409 sym->st_shndx = SHN_MIPS_DATA;
11411 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11413 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11414 sym->st_other = STO_PROTECTED;
11415 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11416 sym->st_shndx = SHN_ABS;
11418 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11420 if (h->type == STT_FUNC)
11421 sym->st_shndx = SHN_MIPS_TEXT;
11422 else if (h->type == STT_OBJECT)
11423 sym->st_shndx = SHN_MIPS_DATA;
11427 /* Emit a copy reloc, if needed. */
11428 if (h->needs_copy)
11430 asection *s;
11431 bfd_vma symval;
11433 BFD_ASSERT (h->dynindx != -1);
11434 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11436 s = mips_elf_rel_dyn_section (info, false);
11437 symval = (h->root.u.def.section->output_section->vma
11438 + h->root.u.def.section->output_offset
11439 + h->root.u.def.value);
11440 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11441 h->dynindx, R_MIPS_COPY, symval);
11444 /* Handle the IRIX6-specific symbols. */
11445 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11446 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11448 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11449 to treat compressed symbols like any other. */
11450 if (ELF_ST_IS_MIPS16 (sym->st_other))
11452 BFD_ASSERT (sym->st_value & 1);
11453 sym->st_other -= STO_MIPS16;
11455 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11457 BFD_ASSERT (sym->st_value & 1);
11458 sym->st_other -= STO_MICROMIPS;
11461 return true;
11464 /* Likewise, for VxWorks. */
11466 bool
11467 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11468 struct bfd_link_info *info,
11469 struct elf_link_hash_entry *h,
11470 Elf_Internal_Sym *sym)
11472 bfd *dynobj;
11473 asection *sgot;
11474 struct mips_got_info *g;
11475 struct mips_elf_link_hash_table *htab;
11476 struct mips_elf_link_hash_entry *hmips;
11478 htab = mips_elf_hash_table (info);
11479 BFD_ASSERT (htab != NULL);
11480 dynobj = elf_hash_table (info)->dynobj;
11481 hmips = (struct mips_elf_link_hash_entry *) h;
11483 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11485 bfd_byte *loc;
11486 bfd_vma plt_address, got_address, got_offset, branch_offset;
11487 Elf_Internal_Rela rel;
11488 static const bfd_vma *plt_entry;
11489 bfd_vma gotplt_index;
11490 bfd_vma plt_offset;
11492 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11493 gotplt_index = h->plt.plist->gotplt_index;
11495 BFD_ASSERT (h->dynindx != -1);
11496 BFD_ASSERT (htab->root.splt != NULL);
11497 BFD_ASSERT (gotplt_index != MINUS_ONE);
11498 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11500 /* Calculate the address of the .plt entry. */
11501 plt_address = (htab->root.splt->output_section->vma
11502 + htab->root.splt->output_offset
11503 + plt_offset);
11505 /* Calculate the address of the .got.plt entry. */
11506 got_address = (htab->root.sgotplt->output_section->vma
11507 + htab->root.sgotplt->output_offset
11508 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11510 /* Calculate the offset of the .got.plt entry from
11511 _GLOBAL_OFFSET_TABLE_. */
11512 got_offset = mips_elf_gotplt_index (info, h);
11514 /* Calculate the offset for the branch at the start of the PLT
11515 entry. The branch jumps to the beginning of .plt. */
11516 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11518 /* Fill in the initial value of the .got.plt entry. */
11519 bfd_put_32 (output_bfd, plt_address,
11520 (htab->root.sgotplt->contents
11521 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11523 /* Find out where the .plt entry should go. */
11524 loc = htab->root.splt->contents + plt_offset;
11526 if (bfd_link_pic (info))
11528 plt_entry = mips_vxworks_shared_plt_entry;
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);
11532 else
11534 bfd_vma got_address_high, got_address_low;
11536 plt_entry = mips_vxworks_exec_plt_entry;
11537 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11538 got_address_low = got_address & 0xffff;
11540 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11541 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11542 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11543 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11544 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11545 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11546 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11547 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11549 loc = (htab->srelplt2->contents
11550 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11552 /* Emit a relocation for the .got.plt entry. */
11553 rel.r_offset = got_address;
11554 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11555 rel.r_addend = plt_offset;
11556 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11558 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11559 loc += sizeof (Elf32_External_Rela);
11560 rel.r_offset = plt_address + 8;
11561 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11562 rel.r_addend = got_offset;
11563 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11565 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11566 loc += sizeof (Elf32_External_Rela);
11567 rel.r_offset += 4;
11568 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11569 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11572 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11573 loc = (htab->root.srelplt->contents
11574 + gotplt_index * sizeof (Elf32_External_Rela));
11575 rel.r_offset = got_address;
11576 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11577 rel.r_addend = 0;
11578 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11580 if (!h->def_regular)
11581 sym->st_shndx = SHN_UNDEF;
11584 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11586 sgot = htab->root.sgot;
11587 g = htab->got_info;
11588 BFD_ASSERT (g != NULL);
11590 /* See if this symbol has an entry in the GOT. */
11591 if (hmips->global_got_area != GGA_NONE)
11593 bfd_vma offset;
11594 Elf_Internal_Rela outrel;
11595 bfd_byte *loc;
11596 asection *s;
11598 /* Install the symbol value in the GOT. */
11599 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11600 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11602 /* Add a dynamic relocation for it. */
11603 s = mips_elf_rel_dyn_section (info, false);
11604 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11605 outrel.r_offset = (sgot->output_section->vma
11606 + sgot->output_offset
11607 + offset);
11608 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11609 outrel.r_addend = 0;
11610 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11613 /* Emit a copy reloc, if needed. */
11614 if (h->needs_copy)
11616 Elf_Internal_Rela rel;
11617 asection *srel;
11618 bfd_byte *loc;
11620 BFD_ASSERT (h->dynindx != -1);
11622 rel.r_offset = (h->root.u.def.section->output_section->vma
11623 + h->root.u.def.section->output_offset
11624 + h->root.u.def.value);
11625 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11626 rel.r_addend = 0;
11627 if (h->root.u.def.section == htab->root.sdynrelro)
11628 srel = htab->root.sreldynrelro;
11629 else
11630 srel = htab->root.srelbss;
11631 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11632 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11633 ++srel->reloc_count;
11636 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11637 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11638 sym->st_value &= ~1;
11640 return true;
11643 /* Write out a plt0 entry to the beginning of .plt. */
11645 static bool
11646 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11648 bfd_byte *loc;
11649 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11650 static const bfd_vma *plt_entry;
11651 struct mips_elf_link_hash_table *htab;
11653 htab = mips_elf_hash_table (info);
11654 BFD_ASSERT (htab != NULL);
11656 if (ABI_64_P (output_bfd))
11657 plt_entry = (htab->compact_branches
11658 ? mipsr6_n64_exec_plt0_entry_compact
11659 : mips_n64_exec_plt0_entry);
11660 else if (ABI_N32_P (output_bfd))
11661 plt_entry = (htab->compact_branches
11662 ? mipsr6_n32_exec_plt0_entry_compact
11663 : mips_n32_exec_plt0_entry);
11664 else if (!htab->plt_header_is_comp)
11665 plt_entry = (htab->compact_branches
11666 ? mipsr6_o32_exec_plt0_entry_compact
11667 : mips_o32_exec_plt0_entry);
11668 else if (htab->insn32)
11669 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11670 else
11671 plt_entry = micromips_o32_exec_plt0_entry;
11673 /* Calculate the value of .got.plt. */
11674 gotplt_value = (htab->root.sgotplt->output_section->vma
11675 + htab->root.sgotplt->output_offset);
11676 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11677 gotplt_value_low = gotplt_value & 0xffff;
11679 /* The PLT sequence is not safe for N64 if .got.plt's address can
11680 not be loaded in two instructions. */
11681 if (ABI_64_P (output_bfd)
11682 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11684 _bfd_error_handler
11685 /* xgettext:c-format */
11686 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11687 "supported; consider using `-Ttext-segment=...'"),
11688 output_bfd,
11689 htab->root.sgotplt->output_section,
11690 (int64_t) gotplt_value);
11691 bfd_set_error (bfd_error_no_error);
11692 return false;
11695 /* Install the PLT header. */
11696 loc = htab->root.splt->contents;
11697 if (plt_entry == micromips_o32_exec_plt0_entry)
11699 bfd_vma gotpc_offset;
11700 bfd_vma loc_address;
11701 size_t i;
11703 BFD_ASSERT (gotplt_value % 4 == 0);
11705 loc_address = (htab->root.splt->output_section->vma
11706 + htab->root.splt->output_offset);
11707 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11709 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11710 if (gotpc_offset + 0x1000000 >= 0x2000000)
11712 _bfd_error_handler
11713 /* xgettext:c-format */
11714 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11715 "beyond the range of ADDIUPC"),
11716 output_bfd,
11717 htab->root.sgotplt->output_section,
11718 (int64_t) gotpc_offset,
11719 htab->root.splt->output_section);
11720 bfd_set_error (bfd_error_no_error);
11721 return false;
11723 bfd_put_16 (output_bfd,
11724 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11725 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11726 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11727 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11729 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11731 size_t i;
11733 bfd_put_16 (output_bfd, plt_entry[0], loc);
11734 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11735 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11736 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11737 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11738 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11739 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11740 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11742 else
11744 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11745 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11746 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11747 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11748 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11749 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11750 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11751 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11754 return true;
11757 /* Install the PLT header for a VxWorks executable and finalize the
11758 contents of .rela.plt.unloaded. */
11760 static void
11761 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11763 Elf_Internal_Rela rela;
11764 bfd_byte *loc;
11765 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11766 static const bfd_vma *plt_entry;
11767 struct mips_elf_link_hash_table *htab;
11769 htab = mips_elf_hash_table (info);
11770 BFD_ASSERT (htab != NULL);
11772 plt_entry = mips_vxworks_exec_plt0_entry;
11774 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11775 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11776 + htab->root.hgot->root.u.def.section->output_offset
11777 + htab->root.hgot->root.u.def.value);
11779 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11780 got_value_low = got_value & 0xffff;
11782 /* Calculate the address of the PLT header. */
11783 plt_address = (htab->root.splt->output_section->vma
11784 + htab->root.splt->output_offset);
11786 /* Install the PLT header. */
11787 loc = htab->root.splt->contents;
11788 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11789 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11790 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11791 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11792 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11793 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11795 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11796 loc = htab->srelplt2->contents;
11797 rela.r_offset = plt_address;
11798 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11799 rela.r_addend = 0;
11800 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11801 loc += sizeof (Elf32_External_Rela);
11803 /* Output the relocation for the following addiu of
11804 %lo(_GLOBAL_OFFSET_TABLE_). */
11805 rela.r_offset += 4;
11806 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11807 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11808 loc += sizeof (Elf32_External_Rela);
11810 /* Fix up the remaining relocations. They may have the wrong
11811 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11812 in which symbols were output. */
11813 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11815 Elf_Internal_Rela rel;
11817 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11818 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11819 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11820 loc += sizeof (Elf32_External_Rela);
11822 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11823 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11824 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11825 loc += sizeof (Elf32_External_Rela);
11827 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11828 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11829 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11830 loc += sizeof (Elf32_External_Rela);
11834 /* Install the PLT header for a VxWorks shared library. */
11836 static void
11837 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11839 unsigned int i;
11840 struct mips_elf_link_hash_table *htab;
11842 htab = mips_elf_hash_table (info);
11843 BFD_ASSERT (htab != NULL);
11845 /* We just need to copy the entry byte-by-byte. */
11846 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11847 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11848 htab->root.splt->contents + i * 4);
11851 /* Finish up the dynamic sections. */
11853 bool
11854 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11855 struct bfd_link_info *info)
11857 bfd *dynobj;
11858 asection *sdyn;
11859 asection *sgot;
11860 struct mips_got_info *gg, *g;
11861 struct mips_elf_link_hash_table *htab;
11863 htab = mips_elf_hash_table (info);
11864 BFD_ASSERT (htab != NULL);
11866 dynobj = elf_hash_table (info)->dynobj;
11868 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11870 sgot = htab->root.sgot;
11871 gg = htab->got_info;
11873 if (elf_hash_table (info)->dynamic_sections_created)
11875 bfd_byte *b;
11876 int dyn_to_skip = 0, dyn_skipped = 0;
11878 BFD_ASSERT (sdyn != NULL);
11879 BFD_ASSERT (gg != NULL);
11881 g = mips_elf_bfd_got (output_bfd, false);
11882 BFD_ASSERT (g != NULL);
11884 for (b = sdyn->contents;
11885 b < sdyn->contents + sdyn->size;
11886 b += MIPS_ELF_DYN_SIZE (dynobj))
11888 Elf_Internal_Dyn dyn;
11889 const char *name;
11890 size_t elemsize;
11891 asection *s;
11892 bool swap_out_p;
11894 /* Read in the current dynamic entry. */
11895 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11897 /* Assume that we're going to modify it and write it out. */
11898 swap_out_p = true;
11900 switch (dyn.d_tag)
11902 case DT_RELENT:
11903 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11904 break;
11906 case DT_RELAENT:
11907 BFD_ASSERT (htab->root.target_os == is_vxworks);
11908 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11909 break;
11911 case DT_STRSZ:
11912 /* Rewrite DT_STRSZ. */
11913 dyn.d_un.d_val =
11914 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11915 break;
11917 case DT_PLTGOT:
11918 s = htab->root.sgot;
11919 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11920 break;
11922 case DT_MIPS_PLTGOT:
11923 s = htab->root.sgotplt;
11924 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11925 break;
11927 case DT_MIPS_RLD_VERSION:
11928 dyn.d_un.d_val = 1; /* XXX */
11929 break;
11931 case DT_MIPS_FLAGS:
11932 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11933 break;
11935 case DT_MIPS_TIME_STAMP:
11937 time_t t;
11938 time (&t);
11939 dyn.d_un.d_val = t;
11941 break;
11943 case DT_MIPS_ICHECKSUM:
11944 /* XXX FIXME: */
11945 swap_out_p = false;
11946 break;
11948 case DT_MIPS_IVERSION:
11949 /* XXX FIXME: */
11950 swap_out_p = false;
11951 break;
11953 case DT_MIPS_BASE_ADDRESS:
11954 s = output_bfd->sections;
11955 BFD_ASSERT (s != NULL);
11956 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11957 break;
11959 case DT_MIPS_LOCAL_GOTNO:
11960 dyn.d_un.d_val = g->local_gotno;
11961 break;
11963 case DT_MIPS_UNREFEXTNO:
11964 /* The index into the dynamic symbol table which is the
11965 entry of the first external symbol that is not
11966 referenced within the same object. */
11967 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11968 break;
11970 case DT_MIPS_GOTSYM:
11971 if (htab->global_gotsym)
11973 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11974 break;
11976 /* In case if we don't have global got symbols we default
11977 to setting DT_MIPS_GOTSYM to the same value as
11978 DT_MIPS_SYMTABNO. */
11979 /* Fall through. */
11981 case DT_MIPS_SYMTABNO:
11982 name = ".dynsym";
11983 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11984 s = bfd_get_linker_section (dynobj, name);
11986 if (s != NULL)
11987 dyn.d_un.d_val = s->size / elemsize;
11988 else
11989 dyn.d_un.d_val = 0;
11990 break;
11992 case DT_MIPS_HIPAGENO:
11993 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11994 break;
11996 case DT_MIPS_RLD_MAP:
11998 struct elf_link_hash_entry *h;
11999 h = mips_elf_hash_table (info)->rld_symbol;
12000 if (!h)
12002 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12003 swap_out_p = false;
12004 break;
12006 s = h->root.u.def.section;
12008 /* The MIPS_RLD_MAP tag stores the absolute address of the
12009 debug pointer. */
12010 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12011 + h->root.u.def.value);
12013 break;
12015 case DT_MIPS_RLD_MAP_REL:
12017 struct elf_link_hash_entry *h;
12018 bfd_vma dt_addr, rld_addr;
12019 h = mips_elf_hash_table (info)->rld_symbol;
12020 if (!h)
12022 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12023 swap_out_p = false;
12024 break;
12026 s = h->root.u.def.section;
12028 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12029 pointer, relative to the address of the tag. */
12030 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12031 + (b - sdyn->contents));
12032 rld_addr = (s->output_section->vma + s->output_offset
12033 + h->root.u.def.value);
12034 dyn.d_un.d_ptr = rld_addr - dt_addr;
12036 break;
12038 case DT_MIPS_OPTIONS:
12039 s = (bfd_get_section_by_name
12040 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12041 dyn.d_un.d_ptr = s->vma;
12042 break;
12044 case DT_PLTREL:
12045 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12046 if (htab->root.target_os == is_vxworks)
12047 dyn.d_un.d_val = DT_RELA;
12048 else
12049 dyn.d_un.d_val = DT_REL;
12050 break;
12052 case DT_PLTRELSZ:
12053 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12054 dyn.d_un.d_val = htab->root.srelplt->size;
12055 break;
12057 case DT_JMPREL:
12058 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12059 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12060 + htab->root.srelplt->output_offset);
12061 break;
12063 case DT_TEXTREL:
12064 /* If we didn't need any text relocations after all, delete
12065 the dynamic tag. */
12066 if (!(info->flags & DF_TEXTREL))
12068 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12069 swap_out_p = false;
12071 break;
12073 case DT_FLAGS:
12074 /* If we didn't need any text relocations after all, clear
12075 DF_TEXTREL from DT_FLAGS. */
12076 if (!(info->flags & DF_TEXTREL))
12077 dyn.d_un.d_val &= ~DF_TEXTREL;
12078 else
12079 swap_out_p = false;
12080 break;
12082 case DT_MIPS_XHASH:
12083 name = ".MIPS.xhash";
12084 s = bfd_get_linker_section (dynobj, name);
12085 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12086 break;
12088 default:
12089 swap_out_p = false;
12090 if (htab->root.target_os == is_vxworks
12091 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12092 swap_out_p = true;
12093 break;
12096 if (swap_out_p || dyn_skipped)
12097 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12098 (dynobj, &dyn, b - dyn_skipped);
12100 if (dyn_to_skip)
12102 dyn_skipped += dyn_to_skip;
12103 dyn_to_skip = 0;
12107 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12108 if (dyn_skipped > 0)
12109 memset (b - dyn_skipped, 0, dyn_skipped);
12112 if (sgot != NULL && sgot->size > 0
12113 && !bfd_is_abs_section (sgot->output_section))
12115 if (htab->root.target_os == is_vxworks)
12117 /* The first entry of the global offset table points to the
12118 ".dynamic" section. The second is initialized by the
12119 loader and contains the shared library identifier.
12120 The third is also initialized by the loader and points
12121 to the lazy resolution stub. */
12122 MIPS_ELF_PUT_WORD (output_bfd,
12123 sdyn->output_offset + sdyn->output_section->vma,
12124 sgot->contents);
12125 MIPS_ELF_PUT_WORD (output_bfd, 0,
12126 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12127 MIPS_ELF_PUT_WORD (output_bfd, 0,
12128 sgot->contents
12129 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12131 else
12133 /* The first entry of the global offset table will be filled at
12134 runtime. The second entry will be used by some runtime loaders.
12135 This isn't the case of IRIX rld. */
12136 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12137 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12138 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12141 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12142 = MIPS_ELF_GOT_SIZE (output_bfd);
12145 /* Generate dynamic relocations for the non-primary gots. */
12146 if (gg != NULL && gg->next)
12148 Elf_Internal_Rela rel[3];
12149 bfd_vma addend = 0;
12151 memset (rel, 0, sizeof (rel));
12152 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12154 for (g = gg->next; g->next != gg; g = g->next)
12156 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12157 + g->next->tls_gotno;
12159 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12160 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12161 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12162 sgot->contents
12163 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12165 if (! bfd_link_pic (info))
12166 continue;
12168 for (; got_index < g->local_gotno; got_index++)
12170 if (got_index >= g->assigned_low_gotno
12171 && got_index <= g->assigned_high_gotno)
12172 continue;
12174 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12175 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12176 if (!(mips_elf_create_dynamic_relocation
12177 (output_bfd, info, rel, NULL,
12178 bfd_abs_section_ptr,
12179 0, &addend, sgot)))
12180 return false;
12181 BFD_ASSERT (addend == 0);
12186 /* The generation of dynamic relocations for the non-primary gots
12187 adds more dynamic relocations. We cannot count them until
12188 here. */
12190 if (elf_hash_table (info)->dynamic_sections_created)
12192 bfd_byte *b;
12193 bool swap_out_p;
12195 BFD_ASSERT (sdyn != NULL);
12197 for (b = sdyn->contents;
12198 b < sdyn->contents + sdyn->size;
12199 b += MIPS_ELF_DYN_SIZE (dynobj))
12201 Elf_Internal_Dyn dyn;
12202 asection *s;
12204 /* Read in the current dynamic entry. */
12205 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12207 /* Assume that we're going to modify it and write it out. */
12208 swap_out_p = true;
12210 switch (dyn.d_tag)
12212 case DT_RELSZ:
12213 /* Reduce DT_RELSZ to account for any relocations we
12214 decided not to make. This is for the n64 irix rld,
12215 which doesn't seem to apply any relocations if there
12216 are trailing null entries. */
12217 s = mips_elf_rel_dyn_section (info, false);
12218 dyn.d_un.d_val = (s->reloc_count
12219 * (ABI_64_P (output_bfd)
12220 ? sizeof (Elf64_Mips_External_Rel)
12221 : sizeof (Elf32_External_Rel)));
12222 /* Adjust the section size too. Tools like the prelinker
12223 can reasonably expect the values to the same. */
12224 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12225 elf_section_data (s->output_section)->this_hdr.sh_size
12226 = dyn.d_un.d_val;
12227 break;
12229 default:
12230 swap_out_p = false;
12231 break;
12234 if (swap_out_p)
12235 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12236 (dynobj, &dyn, b);
12241 asection *s;
12242 Elf32_compact_rel cpt;
12244 if (SGI_COMPAT (output_bfd))
12246 /* Write .compact_rel section out. */
12247 s = bfd_get_linker_section (dynobj, ".compact_rel");
12248 if (s != NULL)
12250 cpt.id1 = 1;
12251 cpt.num = s->reloc_count;
12252 cpt.id2 = 2;
12253 cpt.offset = (s->output_section->filepos
12254 + sizeof (Elf32_External_compact_rel));
12255 cpt.reserved0 = 0;
12256 cpt.reserved1 = 0;
12257 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12258 ((Elf32_External_compact_rel *)
12259 s->contents));
12261 /* Clean up a dummy stub function entry in .text. */
12262 if (htab->sstubs != NULL
12263 && htab->sstubs->contents != NULL)
12265 file_ptr dummy_offset;
12267 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12268 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12269 memset (htab->sstubs->contents + dummy_offset, 0,
12270 htab->function_stub_size);
12275 /* The psABI says that the dynamic relocations must be sorted in
12276 increasing order of r_symndx. The VxWorks EABI doesn't require
12277 this, and because the code below handles REL rather than RELA
12278 relocations, using it for VxWorks would be outright harmful. */
12279 if (htab->root.target_os != is_vxworks)
12281 s = mips_elf_rel_dyn_section (info, false);
12282 if (s != NULL
12283 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12285 reldyn_sorting_bfd = output_bfd;
12287 if (ABI_64_P (output_bfd))
12288 qsort ((Elf64_External_Rel *) s->contents + 1,
12289 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12290 sort_dynamic_relocs_64);
12291 else
12292 qsort ((Elf32_External_Rel *) s->contents + 1,
12293 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12294 sort_dynamic_relocs);
12299 if (htab->root.splt && htab->root.splt->size > 0)
12301 if (htab->root.target_os == is_vxworks)
12303 if (bfd_link_pic (info))
12304 mips_vxworks_finish_shared_plt (output_bfd, info);
12305 else
12306 mips_vxworks_finish_exec_plt (output_bfd, info);
12308 else
12310 BFD_ASSERT (!bfd_link_pic (info));
12311 if (!mips_finish_exec_plt (output_bfd, info))
12312 return false;
12315 return true;
12319 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12321 static void
12322 mips_set_isa_flags (bfd *abfd)
12324 flagword val;
12326 switch (bfd_get_mach (abfd))
12328 default:
12329 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12330 val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
12331 else
12332 val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
12333 break;
12335 case bfd_mach_mips3000:
12336 val = EF_MIPS_ARCH_1;
12337 break;
12339 case bfd_mach_mips3900:
12340 val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
12341 break;
12343 case bfd_mach_mips6000:
12344 val = EF_MIPS_ARCH_2;
12345 break;
12347 case bfd_mach_mips4010:
12348 val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
12349 break;
12351 case bfd_mach_mips_allegrex:
12352 val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
12353 break;
12355 case bfd_mach_mips4000:
12356 case bfd_mach_mips4300:
12357 case bfd_mach_mips4400:
12358 case bfd_mach_mips4600:
12359 val = EF_MIPS_ARCH_3;
12360 break;
12362 case bfd_mach_mips4100:
12363 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
12364 break;
12366 case bfd_mach_mips4111:
12367 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
12368 break;
12370 case bfd_mach_mips4120:
12371 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
12372 break;
12374 case bfd_mach_mips4650:
12375 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
12376 break;
12378 case bfd_mach_mips5400:
12379 val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
12380 break;
12382 case bfd_mach_mips5500:
12383 val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
12384 break;
12386 case bfd_mach_mips5900:
12387 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
12388 break;
12390 case bfd_mach_mips9000:
12391 val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
12392 break;
12394 case bfd_mach_mips5000:
12395 case bfd_mach_mips7000:
12396 case bfd_mach_mips8000:
12397 case bfd_mach_mips10000:
12398 case bfd_mach_mips12000:
12399 case bfd_mach_mips14000:
12400 case bfd_mach_mips16000:
12401 val = EF_MIPS_ARCH_4;
12402 break;
12404 case bfd_mach_mips5:
12405 val = EF_MIPS_ARCH_5;
12406 break;
12408 case bfd_mach_mips_loongson_2e:
12409 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
12410 break;
12412 case bfd_mach_mips_loongson_2f:
12413 val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
12414 break;
12416 case bfd_mach_mips_sb1:
12417 val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
12418 break;
12420 case bfd_mach_mips_gs464:
12421 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
12422 break;
12424 case bfd_mach_mips_gs464e:
12425 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
12426 break;
12428 case bfd_mach_mips_gs264e:
12429 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
12430 break;
12432 case bfd_mach_mips_octeon:
12433 case bfd_mach_mips_octeonp:
12434 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
12435 break;
12437 case bfd_mach_mips_octeon3:
12438 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
12439 break;
12441 case bfd_mach_mips_xlr:
12442 val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
12443 break;
12445 case bfd_mach_mips_octeon2:
12446 val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
12447 break;
12449 case bfd_mach_mipsisa32:
12450 val = EF_MIPS_ARCH_32;
12451 break;
12453 case bfd_mach_mipsisa64:
12454 val = EF_MIPS_ARCH_64;
12455 break;
12457 case bfd_mach_mipsisa32r2:
12458 case bfd_mach_mipsisa32r3:
12459 case bfd_mach_mipsisa32r5:
12460 val = EF_MIPS_ARCH_32R2;
12461 break;
12463 case bfd_mach_mips_interaptiv_mr2:
12464 val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
12465 break;
12467 case bfd_mach_mipsisa64r2:
12468 case bfd_mach_mipsisa64r3:
12469 case bfd_mach_mipsisa64r5:
12470 val = EF_MIPS_ARCH_64R2;
12471 break;
12473 case bfd_mach_mipsisa32r6:
12474 val = EF_MIPS_ARCH_32R6;
12475 break;
12477 case bfd_mach_mipsisa64r6:
12478 val = EF_MIPS_ARCH_64R6;
12479 break;
12481 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12482 elf_elfheader (abfd)->e_flags |= val;
12487 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12488 Don't do so for code sections. We want to keep ordering of HI16/LO16
12489 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12490 relocs to be sorted. */
12492 bool
12493 _bfd_mips_elf_sort_relocs_p (asection *sec)
12495 return (sec->flags & SEC_CODE) == 0;
12499 /* The final processing done just before writing out a MIPS ELF object
12500 file. This gets the MIPS architecture right based on the machine
12501 number. This is used by both the 32-bit and the 64-bit ABI. */
12503 void
12504 _bfd_mips_final_write_processing (bfd *abfd)
12506 unsigned int i;
12507 Elf_Internal_Shdr **hdrpp;
12508 const char *name;
12509 asection *sec;
12511 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12512 is nonzero. This is for compatibility with old objects, which used
12513 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12514 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12515 mips_set_isa_flags (abfd);
12517 /* Set the sh_info field for .gptab sections and other appropriate
12518 info for each special section. */
12519 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12520 i < elf_numsections (abfd);
12521 i++, hdrpp++)
12523 switch ((*hdrpp)->sh_type)
12525 case SHT_MIPS_MSYM:
12526 case SHT_MIPS_LIBLIST:
12527 sec = bfd_get_section_by_name (abfd, ".dynstr");
12528 if (sec != NULL)
12529 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12530 break;
12532 case SHT_MIPS_GPTAB:
12533 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12534 name = bfd_section_name ((*hdrpp)->bfd_section);
12535 if (startswith (name, ".gptab."))
12537 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12538 if (sec != NULL)
12539 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12541 break;
12543 case SHT_MIPS_CONTENT:
12544 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12545 name = bfd_section_name ((*hdrpp)->bfd_section);
12546 if (startswith (name, ".MIPS.content"))
12548 sec = bfd_get_section_by_name (abfd,
12549 name + sizeof ".MIPS.content" - 1);
12550 if (sec != NULL)
12551 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12553 break;
12555 case SHT_MIPS_SYMBOL_LIB:
12556 sec = bfd_get_section_by_name (abfd, ".dynsym");
12557 if (sec != NULL)
12558 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12559 sec = bfd_get_section_by_name (abfd, ".liblist");
12560 if (sec != NULL)
12561 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12562 break;
12564 case SHT_MIPS_EVENTS:
12565 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12566 name = bfd_section_name ((*hdrpp)->bfd_section);
12567 if (startswith (name, ".MIPS.events"))
12568 sec = bfd_get_section_by_name (abfd,
12569 name + sizeof ".MIPS.events" - 1);
12570 else if (startswith (name, ".MIPS.post_rel"))
12571 sec = bfd_get_section_by_name (abfd,
12572 name + sizeof ".MIPS.post_rel" - 1);
12573 else
12574 sec = NULL;
12575 if (sec != NULL)
12576 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12577 break;
12579 case SHT_MIPS_XHASH:
12580 sec = bfd_get_section_by_name (abfd, ".dynsym");
12581 if (sec != NULL)
12582 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12587 bool
12588 _bfd_mips_elf_final_write_processing (bfd *abfd)
12590 _bfd_mips_final_write_processing (abfd);
12591 return _bfd_elf_final_write_processing (abfd);
12594 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12595 segments. */
12598 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12599 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12601 asection *s;
12602 int ret = 0;
12604 /* See if we need a PT_MIPS_REGINFO segment. */
12605 s = bfd_get_section_by_name (abfd, ".reginfo");
12606 if (s && (s->flags & SEC_LOAD))
12607 ++ret;
12609 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12610 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12611 ++ret;
12613 /* See if we need a PT_MIPS_OPTIONS segment. */
12614 if (IRIX_COMPAT (abfd) == ict_irix6
12615 && bfd_get_section_by_name (abfd,
12616 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12617 ++ret;
12619 /* See if we need a PT_MIPS_RTPROC segment. */
12620 if (IRIX_COMPAT (abfd) == ict_irix5
12621 && bfd_get_section_by_name (abfd, ".dynamic")
12622 && bfd_get_section_by_name (abfd, ".mdebug"))
12623 ++ret;
12625 /* Allocate a PT_NULL header in dynamic objects. See
12626 _bfd_mips_elf_modify_segment_map for details. */
12627 if (!SGI_COMPAT (abfd)
12628 && bfd_get_section_by_name (abfd, ".dynamic"))
12629 ++ret;
12631 return ret;
12634 /* Modify the segment map for an IRIX5 executable. */
12636 bool
12637 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12638 struct bfd_link_info *info)
12640 asection *s;
12641 struct elf_segment_map *m, **pm;
12642 size_t amt;
12644 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12645 segment. */
12646 s = bfd_get_section_by_name (abfd, ".reginfo");
12647 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12649 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12650 if (m->p_type == PT_MIPS_REGINFO)
12651 break;
12652 if (m == NULL)
12654 amt = sizeof *m;
12655 m = bfd_zalloc (abfd, amt);
12656 if (m == NULL)
12657 return false;
12659 m->p_type = PT_MIPS_REGINFO;
12660 m->count = 1;
12661 m->sections[0] = s;
12663 /* We want to put it after the PHDR and INTERP segments. */
12664 pm = &elf_seg_map (abfd);
12665 while (*pm != NULL
12666 && ((*pm)->p_type == PT_PHDR
12667 || (*pm)->p_type == PT_INTERP))
12668 pm = &(*pm)->next;
12670 m->next = *pm;
12671 *pm = m;
12675 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12676 segment. */
12677 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12678 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12680 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12681 if (m->p_type == PT_MIPS_ABIFLAGS)
12682 break;
12683 if (m == NULL)
12685 amt = sizeof *m;
12686 m = bfd_zalloc (abfd, amt);
12687 if (m == NULL)
12688 return false;
12690 m->p_type = PT_MIPS_ABIFLAGS;
12691 m->count = 1;
12692 m->sections[0] = s;
12694 /* We want to put it after the PHDR and INTERP segments. */
12695 pm = &elf_seg_map (abfd);
12696 while (*pm != NULL
12697 && ((*pm)->p_type == PT_PHDR
12698 || (*pm)->p_type == PT_INTERP))
12699 pm = &(*pm)->next;
12701 m->next = *pm;
12702 *pm = m;
12706 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12707 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12708 PT_MIPS_OPTIONS segment immediately following the program header
12709 table. */
12710 if (NEWABI_P (abfd)
12711 /* On non-IRIX6 new abi, we'll have already created a segment
12712 for this section, so don't create another. I'm not sure this
12713 is not also the case for IRIX 6, but I can't test it right
12714 now. */
12715 && IRIX_COMPAT (abfd) == ict_irix6)
12717 for (s = abfd->sections; s; s = s->next)
12718 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12719 break;
12721 if (s)
12723 struct elf_segment_map *options_segment;
12725 pm = &elf_seg_map (abfd);
12726 while (*pm != NULL
12727 && ((*pm)->p_type == PT_PHDR
12728 || (*pm)->p_type == PT_INTERP))
12729 pm = &(*pm)->next;
12731 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12733 amt = sizeof (struct elf_segment_map);
12734 options_segment = bfd_zalloc (abfd, amt);
12735 options_segment->next = *pm;
12736 options_segment->p_type = PT_MIPS_OPTIONS;
12737 options_segment->p_flags = PF_R;
12738 options_segment->p_flags_valid = true;
12739 options_segment->count = 1;
12740 options_segment->sections[0] = s;
12741 *pm = options_segment;
12745 else
12747 if (IRIX_COMPAT (abfd) == ict_irix5)
12749 /* If there are .dynamic and .mdebug sections, we make a room
12750 for the RTPROC header. FIXME: Rewrite without section names. */
12751 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12752 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12753 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12755 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12756 if (m->p_type == PT_MIPS_RTPROC)
12757 break;
12758 if (m == NULL)
12760 amt = sizeof *m;
12761 m = bfd_zalloc (abfd, amt);
12762 if (m == NULL)
12763 return false;
12765 m->p_type = PT_MIPS_RTPROC;
12767 s = bfd_get_section_by_name (abfd, ".rtproc");
12768 if (s == NULL)
12770 m->count = 0;
12771 m->p_flags = 0;
12772 m->p_flags_valid = 1;
12774 else
12776 m->count = 1;
12777 m->sections[0] = s;
12780 /* We want to put it after the DYNAMIC segment. */
12781 pm = &elf_seg_map (abfd);
12782 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12783 pm = &(*pm)->next;
12784 if (*pm != NULL)
12785 pm = &(*pm)->next;
12787 m->next = *pm;
12788 *pm = m;
12792 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12793 .dynstr, .dynsym, and .hash sections, and everything in
12794 between. */
12795 for (pm = &elf_seg_map (abfd); *pm != NULL;
12796 pm = &(*pm)->next)
12797 if ((*pm)->p_type == PT_DYNAMIC)
12798 break;
12799 m = *pm;
12800 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12801 glibc's dynamic linker has traditionally derived the number of
12802 tags from the p_filesz field, and sometimes allocates stack
12803 arrays of that size. An overly-big PT_DYNAMIC segment can
12804 be actively harmful in such cases. Making PT_DYNAMIC contain
12805 other sections can also make life hard for the prelinker,
12806 which might move one of the other sections to a different
12807 PT_LOAD segment. */
12808 if (SGI_COMPAT (abfd)
12809 && m != NULL
12810 && m->count == 1
12811 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12813 static const char *sec_names[] =
12815 ".dynamic", ".dynstr", ".dynsym", ".hash"
12817 bfd_vma low, high;
12818 unsigned int i, c;
12819 struct elf_segment_map *n;
12821 low = ~(bfd_vma) 0;
12822 high = 0;
12823 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12825 s = bfd_get_section_by_name (abfd, sec_names[i]);
12826 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12828 bfd_size_type sz;
12830 if (low > s->vma)
12831 low = s->vma;
12832 sz = s->size;
12833 if (high < s->vma + sz)
12834 high = s->vma + sz;
12838 c = 0;
12839 for (s = abfd->sections; s != NULL; s = s->next)
12840 if ((s->flags & SEC_LOAD) != 0
12841 && s->vma >= low
12842 && s->vma + s->size <= high)
12843 ++c;
12845 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12846 n = bfd_zalloc (abfd, amt);
12847 if (n == NULL)
12848 return false;
12849 *n = *m;
12850 n->count = c;
12852 i = 0;
12853 for (s = abfd->sections; s != NULL; s = s->next)
12855 if ((s->flags & SEC_LOAD) != 0
12856 && s->vma >= low
12857 && s->vma + s->size <= high)
12859 n->sections[i] = s;
12860 ++i;
12864 *pm = n;
12868 /* Allocate a spare program header in dynamic objects so that tools
12869 like the prelinker can add an extra PT_LOAD entry.
12871 If the prelinker needs to make room for a new PT_LOAD entry, its
12872 standard procedure is to move the first (read-only) sections into
12873 the new (writable) segment. However, the MIPS ABI requires
12874 .dynamic to be in a read-only segment, and the section will often
12875 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12877 Although the prelinker could in principle move .dynamic to a
12878 writable segment, it seems better to allocate a spare program
12879 header instead, and avoid the need to move any sections.
12880 There is a long tradition of allocating spare dynamic tags,
12881 so allocating a spare program header seems like a natural
12882 extension.
12884 If INFO is NULL, we may be copying an already prelinked binary
12885 with objcopy or strip, so do not add this header. */
12886 if (info != NULL
12887 && !SGI_COMPAT (abfd)
12888 && bfd_get_section_by_name (abfd, ".dynamic"))
12890 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12891 if ((*pm)->p_type == PT_NULL)
12892 break;
12893 if (*pm == NULL)
12895 m = bfd_zalloc (abfd, sizeof (*m));
12896 if (m == NULL)
12897 return false;
12899 m->p_type = PT_NULL;
12900 *pm = m;
12904 return true;
12907 /* Return the section that should be marked against GC for a given
12908 relocation. */
12910 asection *
12911 _bfd_mips_elf_gc_mark_hook (asection *sec,
12912 struct bfd_link_info *info,
12913 Elf_Internal_Rela *rel,
12914 struct elf_link_hash_entry *h,
12915 Elf_Internal_Sym *sym)
12917 /* ??? Do mips16 stub sections need to be handled special? */
12919 if (h != NULL)
12920 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12922 case R_MIPS_GNU_VTINHERIT:
12923 case R_MIPS_GNU_VTENTRY:
12924 return NULL;
12927 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12930 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12932 bool
12933 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12934 elf_gc_mark_hook_fn gc_mark_hook)
12936 bfd *sub;
12938 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12940 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12942 asection *o;
12944 if (! is_mips_elf (sub))
12945 continue;
12947 for (o = sub->sections; o != NULL; o = o->next)
12948 if (!o->gc_mark
12949 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12951 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12952 return false;
12956 return true;
12959 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12960 hiding the old indirect symbol. Process additional relocation
12961 information. Also called for weakdefs, in which case we just let
12962 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12964 void
12965 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12966 struct elf_link_hash_entry *dir,
12967 struct elf_link_hash_entry *ind)
12969 struct mips_elf_link_hash_entry *dirmips, *indmips;
12971 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12973 dirmips = (struct mips_elf_link_hash_entry *) dir;
12974 indmips = (struct mips_elf_link_hash_entry *) ind;
12975 /* Any absolute non-dynamic relocations against an indirect or weak
12976 definition will be against the target symbol. */
12977 if (indmips->has_static_relocs)
12978 dirmips->has_static_relocs = true;
12980 if (ind->root.type != bfd_link_hash_indirect)
12981 return;
12983 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12984 if (indmips->readonly_reloc)
12985 dirmips->readonly_reloc = true;
12986 if (indmips->no_fn_stub)
12987 dirmips->no_fn_stub = true;
12988 if (indmips->fn_stub)
12990 dirmips->fn_stub = indmips->fn_stub;
12991 indmips->fn_stub = NULL;
12993 if (indmips->need_fn_stub)
12995 dirmips->need_fn_stub = true;
12996 indmips->need_fn_stub = false;
12998 if (indmips->call_stub)
13000 dirmips->call_stub = indmips->call_stub;
13001 indmips->call_stub = NULL;
13003 if (indmips->call_fp_stub)
13005 dirmips->call_fp_stub = indmips->call_fp_stub;
13006 indmips->call_fp_stub = NULL;
13008 if (indmips->global_got_area < dirmips->global_got_area)
13009 dirmips->global_got_area = indmips->global_got_area;
13010 if (indmips->global_got_area < GGA_NONE)
13011 indmips->global_got_area = GGA_NONE;
13012 if (indmips->has_nonpic_branches)
13013 dirmips->has_nonpic_branches = true;
13016 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13017 to hide it. It has to remain global (it will also be protected) so as to
13018 be assigned a global GOT entry, which will then remain unchanged at load
13019 time. */
13021 void
13022 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13023 struct elf_link_hash_entry *entry,
13024 bool force_local)
13026 struct mips_elf_link_hash_table *htab;
13028 htab = mips_elf_hash_table (info);
13029 BFD_ASSERT (htab != NULL);
13030 if (htab->use_absolute_zero
13031 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13032 return;
13034 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13037 #define PDR_SIZE 32
13039 bool
13040 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13041 struct bfd_link_info *info)
13043 asection *o;
13044 bool ret = false;
13045 unsigned char *tdata;
13046 size_t i, skip;
13048 o = bfd_get_section_by_name (abfd, ".pdr");
13049 if (! o)
13050 return false;
13051 if (o->size == 0)
13052 return false;
13053 if (o->size % PDR_SIZE != 0)
13054 return false;
13055 if (o->output_section != NULL
13056 && bfd_is_abs_section (o->output_section))
13057 return false;
13059 tdata = bfd_zmalloc (o->size / PDR_SIZE);
13060 if (! tdata)
13061 return false;
13063 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13064 info->keep_memory);
13065 if (!cookie->rels)
13067 free (tdata);
13068 return false;
13071 cookie->rel = cookie->rels;
13072 cookie->relend = cookie->rels + o->reloc_count;
13074 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13076 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13078 tdata[i] = 1;
13079 skip ++;
13083 if (skip != 0)
13085 mips_elf_section_data (o)->u.tdata = tdata;
13086 if (o->rawsize == 0)
13087 o->rawsize = o->size;
13088 o->size -= skip * PDR_SIZE;
13089 ret = true;
13091 else
13092 free (tdata);
13094 if (! info->keep_memory)
13095 free (cookie->rels);
13097 return ret;
13100 bool
13101 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13103 if (strcmp (sec->name, ".pdr") == 0)
13104 return true;
13105 return false;
13108 bool
13109 _bfd_mips_elf_write_section (bfd *output_bfd,
13110 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13111 asection *sec, bfd_byte *contents)
13113 bfd_byte *to, *from, *end;
13114 int i;
13116 if (strcmp (sec->name, ".pdr") != 0)
13117 return false;
13119 if (mips_elf_section_data (sec)->u.tdata == NULL)
13120 return false;
13122 to = contents;
13123 end = contents + sec->size;
13124 for (from = contents, i = 0;
13125 from < end;
13126 from += PDR_SIZE, i++)
13128 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13129 continue;
13130 if (to != from)
13131 memcpy (to, from, PDR_SIZE);
13132 to += PDR_SIZE;
13134 bfd_set_section_contents (output_bfd, sec->output_section, contents,
13135 sec->output_offset, sec->size);
13136 return true;
13139 /* microMIPS code retains local labels for linker relaxation. Omit them
13140 from output by default for clarity. */
13142 bool
13143 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13145 return _bfd_elf_is_local_label_name (abfd, sym->name);
13148 bool
13149 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13150 asection *section, bfd_vma offset,
13151 const char **filename_ptr,
13152 const char **functionname_ptr,
13153 unsigned int *line_ptr,
13154 unsigned int *discriminator_ptr)
13156 asection *msec;
13158 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13159 filename_ptr, functionname_ptr,
13160 line_ptr, discriminator_ptr,
13161 dwarf_debug_sections,
13162 &elf_tdata (abfd)->dwarf2_find_line_info)
13163 == 1)
13164 return true;
13166 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13167 filename_ptr, functionname_ptr,
13168 line_ptr))
13170 if (!*functionname_ptr)
13171 _bfd_elf_find_function (abfd, symbols, section, offset,
13172 *filename_ptr ? NULL : filename_ptr,
13173 functionname_ptr);
13174 return true;
13177 msec = bfd_get_section_by_name (abfd, ".mdebug");
13178 if (msec != NULL)
13180 flagword origflags;
13181 struct mips_elf_find_line *fi;
13182 const struct ecoff_debug_swap * const swap =
13183 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13185 /* If we are called during a link, mips_elf_final_link may have
13186 cleared the SEC_HAS_CONTENTS field. We force it back on here
13187 if appropriate (which it normally will be). */
13188 origflags = msec->flags;
13189 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13190 msec->flags |= SEC_HAS_CONTENTS;
13192 fi = mips_elf_tdata (abfd)->find_line_info;
13193 if (fi == NULL)
13195 bfd_size_type external_fdr_size;
13196 char *fraw_src;
13197 char *fraw_end;
13198 struct fdr *fdr_ptr;
13199 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13201 fi = bfd_zalloc (abfd, amt);
13202 if (fi == NULL)
13204 msec->flags = origflags;
13205 return false;
13208 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13210 msec->flags = origflags;
13211 return false;
13214 /* Swap in the FDR information. */
13215 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13216 fi->d.fdr = bfd_alloc (abfd, amt);
13217 if (fi->d.fdr == NULL)
13219 _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13220 msec->flags = origflags;
13221 return false;
13223 external_fdr_size = swap->external_fdr_size;
13224 fdr_ptr = fi->d.fdr;
13225 fraw_src = (char *) fi->d.external_fdr;
13226 fraw_end = (fraw_src
13227 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13228 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13229 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13231 mips_elf_tdata (abfd)->find_line_info = fi;
13234 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13235 &fi->i, filename_ptr, functionname_ptr,
13236 line_ptr))
13238 msec->flags = origflags;
13239 return true;
13242 msec->flags = origflags;
13245 /* Fall back on the generic ELF find_nearest_line routine. */
13247 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13248 filename_ptr, functionname_ptr,
13249 line_ptr, discriminator_ptr);
13252 bool
13253 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13254 const char **filename_ptr,
13255 const char **functionname_ptr,
13256 unsigned int *line_ptr)
13258 bool found;
13259 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13260 functionname_ptr, line_ptr,
13261 & elf_tdata (abfd)->dwarf2_find_line_info);
13262 return found;
13266 /* When are writing out the .options or .MIPS.options section,
13267 remember the bytes we are writing out, so that we can install the
13268 GP value in the section_processing routine. */
13270 bool
13271 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13272 const void *location,
13273 file_ptr offset, bfd_size_type count)
13275 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13277 bfd_byte *c;
13279 if (elf_section_data (section) == NULL)
13281 size_t amt = sizeof (struct bfd_elf_section_data);
13282 section->used_by_bfd = bfd_zalloc (abfd, amt);
13283 if (elf_section_data (section) == NULL)
13284 return false;
13286 c = mips_elf_section_data (section)->u.tdata;
13287 if (c == NULL)
13289 c = bfd_zalloc (abfd, section->size);
13290 if (c == NULL)
13291 return false;
13292 mips_elf_section_data (section)->u.tdata = c;
13295 memcpy (c + offset, location, count);
13298 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13299 count);
13302 /* This is almost identical to bfd_generic_get_... except that some
13303 MIPS relocations need to be handled specially. Sigh. */
13305 bfd_byte *
13306 _bfd_elf_mips_get_relocated_section_contents
13307 (bfd *abfd,
13308 struct bfd_link_info *link_info,
13309 struct bfd_link_order *link_order,
13310 bfd_byte *data,
13311 bool relocatable,
13312 asymbol **symbols)
13314 bfd *input_bfd = link_order->u.indirect.section->owner;
13315 asection *input_section = link_order->u.indirect.section;
13316 long reloc_size;
13317 arelent **reloc_vector;
13318 long reloc_count;
13320 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13321 if (reloc_size < 0)
13322 return NULL;
13324 /* Read in the section. */
13325 bfd_byte *orig_data = data;
13326 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13327 return NULL;
13329 if (data == NULL)
13330 return NULL;
13332 if (reloc_size == 0)
13333 return data;
13335 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13336 if (reloc_vector == NULL)
13338 struct mips_elf_obj_tdata *tdata;
13339 struct mips_hi16 **hip, *hi;
13340 error_return:
13341 /* If we are going to return an error, remove entries on
13342 mips_hi16_list that point into this section's data. Data
13343 will typically be freed on return from this function. */
13344 tdata = mips_elf_tdata (abfd);
13345 hip = &tdata->mips_hi16_list;
13346 while ((hi = *hip) != NULL)
13348 if (hi->input_section == input_section)
13350 *hip = hi->next;
13351 free (hi);
13353 else
13354 hip = &hi->next;
13356 if (orig_data == NULL)
13357 free (data);
13358 data = NULL;
13359 goto out;
13362 reloc_count = bfd_canonicalize_reloc (input_bfd,
13363 input_section,
13364 reloc_vector,
13365 symbols);
13366 if (reloc_count < 0)
13367 goto error_return;
13369 if (reloc_count > 0)
13371 arelent **parent;
13372 /* for mips */
13373 int gp_found;
13374 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13377 struct bfd_hash_entry *h;
13378 struct bfd_link_hash_entry *lh;
13379 /* Skip all this stuff if we aren't mixing formats. */
13380 if (abfd && input_bfd
13381 && abfd->xvec == input_bfd->xvec)
13382 lh = 0;
13383 else
13385 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13386 lh = (struct bfd_link_hash_entry *) h;
13388 lookup:
13389 if (lh)
13391 switch (lh->type)
13393 case bfd_link_hash_undefined:
13394 case bfd_link_hash_undefweak:
13395 case bfd_link_hash_common:
13396 gp_found = 0;
13397 break;
13398 case bfd_link_hash_defined:
13399 case bfd_link_hash_defweak:
13400 gp_found = 1;
13401 gp = lh->u.def.value;
13402 break;
13403 case bfd_link_hash_indirect:
13404 case bfd_link_hash_warning:
13405 lh = lh->u.i.link;
13406 /* @@FIXME ignoring warning for now */
13407 goto lookup;
13408 case bfd_link_hash_new:
13409 default:
13410 abort ();
13413 else
13414 gp_found = 0;
13416 /* end mips */
13418 for (parent = reloc_vector; *parent != NULL; parent++)
13420 char *error_message = NULL;
13421 asymbol *symbol;
13422 bfd_reloc_status_type r;
13424 symbol = *(*parent)->sym_ptr_ptr;
13425 /* PR ld/19628: A specially crafted input file
13426 can result in a NULL symbol pointer here. */
13427 if (symbol == NULL)
13429 link_info->callbacks->einfo
13430 /* xgettext:c-format */
13431 (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13432 abfd, input_section, (* parent)->address);
13433 goto error_return;
13436 /* Zap reloc field when the symbol is from a discarded
13437 section, ignoring any addend. Do the same when called
13438 from bfd_simple_get_relocated_section_contents for
13439 undefined symbols in debug sections. This is to keep
13440 debug info reasonably sane, in particular so that
13441 DW_FORM_ref_addr to another file's .debug_info isn't
13442 confused with an offset into the current file's
13443 .debug_info. */
13444 if ((symbol->section != NULL && discarded_section (symbol->section))
13445 || (symbol->section == bfd_und_section_ptr
13446 && (input_section->flags & SEC_DEBUGGING) != 0
13447 && link_info->input_bfds == link_info->output_bfd))
13449 bfd_vma off;
13450 static reloc_howto_type none_howto
13451 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13452 "unused", false, 0, 0, false);
13454 off = ((*parent)->address
13455 * bfd_octets_per_byte (input_bfd, input_section));
13456 _bfd_clear_contents ((*parent)->howto, input_bfd,
13457 input_section, data, off);
13458 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13459 (*parent)->addend = 0;
13460 (*parent)->howto = &none_howto;
13461 r = bfd_reloc_ok;
13464 /* Specific to MIPS: Deal with relocation types that require
13465 knowing the gp of the output bfd. */
13467 /* If we've managed to find the gp and have a special
13468 function for the relocation then go ahead, else default
13469 to the generic handling. */
13470 else if (gp_found
13471 && ((*parent)->howto->special_function
13472 == _bfd_mips_elf32_gprel16_reloc))
13473 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13474 input_section, relocatable,
13475 data, gp);
13476 else
13477 r = bfd_perform_relocation (input_bfd,
13478 *parent,
13479 data,
13480 input_section,
13481 relocatable ? abfd : NULL,
13482 &error_message);
13484 if (relocatable)
13486 asection *os = input_section->output_section;
13488 /* A partial link, so keep the relocs. */
13489 os->orelocation[os->reloc_count] = *parent;
13490 os->reloc_count++;
13493 if (r != bfd_reloc_ok)
13495 switch (r)
13497 case bfd_reloc_undefined:
13498 (*link_info->callbacks->undefined_symbol)
13499 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13500 input_bfd, input_section, (*parent)->address, true);
13501 break;
13502 case bfd_reloc_dangerous:
13503 BFD_ASSERT (error_message != NULL);
13504 (*link_info->callbacks->reloc_dangerous)
13505 (link_info, error_message,
13506 input_bfd, input_section, (*parent)->address);
13507 break;
13508 case bfd_reloc_overflow:
13509 (*link_info->callbacks->reloc_overflow)
13510 (link_info, NULL,
13511 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13512 (*parent)->howto->name, (*parent)->addend,
13513 input_bfd, input_section, (*parent)->address);
13514 break;
13515 case bfd_reloc_outofrange:
13516 /* PR ld/13730:
13517 This error can result when processing some partially
13518 complete binaries. Do not abort, but issue an error
13519 message instead. */
13520 link_info->callbacks->einfo
13521 /* xgettext:c-format */
13522 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13523 abfd, input_section, * parent);
13524 goto error_return;
13526 case bfd_reloc_notsupported:
13527 /* PR ld/17512
13528 This error can result when processing a corrupt binary.
13529 Do not abort. Issue an error message instead. */
13530 link_info->callbacks->einfo
13531 /* xgettext:c-format */
13532 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13533 abfd, input_section, * parent);
13534 goto error_return;
13536 default:
13537 /* PR 17512; file: 90c2a92e.
13538 Report unexpected results, without aborting. */
13539 link_info->callbacks->einfo
13540 /* xgettext:c-format */
13541 (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13542 abfd, input_section, * parent, r);
13543 break;
13550 out:
13551 free (reloc_vector);
13552 return data;
13555 static bool
13556 mips_elf_relax_delete_bytes (bfd *abfd,
13557 asection *sec, bfd_vma addr, int count)
13559 Elf_Internal_Shdr *symtab_hdr;
13560 unsigned int sec_shndx;
13561 bfd_byte *contents;
13562 Elf_Internal_Rela *irel, *irelend;
13563 Elf_Internal_Sym *isym;
13564 Elf_Internal_Sym *isymend;
13565 struct elf_link_hash_entry **sym_hashes;
13566 struct elf_link_hash_entry **end_hashes;
13567 struct elf_link_hash_entry **start_hashes;
13568 unsigned int symcount;
13570 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13571 contents = elf_section_data (sec)->this_hdr.contents;
13573 irel = elf_section_data (sec)->relocs;
13574 irelend = irel + sec->reloc_count;
13576 /* Actually delete the bytes. */
13577 memmove (contents + addr, contents + addr + count,
13578 (size_t) (sec->size - addr - count));
13579 sec->size -= count;
13581 /* Adjust all the relocs. */
13582 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13584 /* Get the new reloc address. */
13585 if (irel->r_offset > addr)
13586 irel->r_offset -= count;
13589 BFD_ASSERT (addr % 2 == 0);
13590 BFD_ASSERT (count % 2 == 0);
13592 /* Adjust the local symbols defined in this section. */
13593 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13594 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13595 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13596 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13597 isym->st_value -= count;
13599 /* Now adjust the global symbols defined in this section. */
13600 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13601 - symtab_hdr->sh_info);
13602 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13603 end_hashes = sym_hashes + symcount;
13605 for (; sym_hashes < end_hashes; sym_hashes++)
13607 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13609 if ((sym_hash->root.type == bfd_link_hash_defined
13610 || sym_hash->root.type == bfd_link_hash_defweak)
13611 && sym_hash->root.u.def.section == sec)
13613 bfd_vma value = sym_hash->root.u.def.value;
13615 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13616 value &= MINUS_TWO;
13617 if (value > addr)
13618 sym_hash->root.u.def.value -= count;
13622 return true;
13626 /* Opcodes needed for microMIPS relaxation as found in
13627 opcodes/micromips-opc.c. */
13629 struct opcode_descriptor {
13630 unsigned long match;
13631 unsigned long mask;
13634 /* The $ra register aka $31. */
13636 #define RA 31
13638 /* 32-bit instruction format register fields. */
13640 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13641 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13643 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13645 #define OP16_VALID_REG(r) \
13646 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13649 /* 32-bit and 16-bit branches. */
13651 static const struct opcode_descriptor b_insns_32[] = {
13652 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13653 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13654 { 0, 0 } /* End marker for find_match(). */
13657 static const struct opcode_descriptor bc_insn_32 =
13658 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13660 static const struct opcode_descriptor bz_insn_32 =
13661 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13663 static const struct opcode_descriptor bzal_insn_32 =
13664 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13666 static const struct opcode_descriptor beq_insn_32 =
13667 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13669 static const struct opcode_descriptor b_insn_16 =
13670 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13672 static const struct opcode_descriptor bz_insn_16 =
13673 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13676 /* 32-bit and 16-bit branch EQ and NE zero. */
13678 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13679 eq and second the ne. This convention is used when replacing a
13680 32-bit BEQ/BNE with the 16-bit version. */
13682 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13684 static const struct opcode_descriptor bz_rs_insns_32[] = {
13685 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13686 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13687 { 0, 0 } /* End marker for find_match(). */
13690 static const struct opcode_descriptor bz_rt_insns_32[] = {
13691 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13692 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13693 { 0, 0 } /* End marker for find_match(). */
13696 static const struct opcode_descriptor bzc_insns_32[] = {
13697 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13698 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13699 { 0, 0 } /* End marker for find_match(). */
13702 static const struct opcode_descriptor bz_insns_16[] = {
13703 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13704 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13705 { 0, 0 } /* End marker for find_match(). */
13708 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13710 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13711 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13714 /* 32-bit instructions with a delay slot. */
13716 static const struct opcode_descriptor jal_insn_32_bd16 =
13717 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13719 static const struct opcode_descriptor jal_insn_32_bd32 =
13720 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13722 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13723 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13725 static const struct opcode_descriptor j_insn_32 =
13726 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13728 static const struct opcode_descriptor jalr_insn_32 =
13729 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13731 /* This table can be compacted, because no opcode replacement is made. */
13733 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13734 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13736 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13737 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13739 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13740 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13741 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13742 { 0, 0 } /* End marker for find_match(). */
13745 /* This table can be compacted, because no opcode replacement is made. */
13747 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13748 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13750 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13751 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13752 { 0, 0 } /* End marker for find_match(). */
13756 /* 16-bit instructions with a delay slot. */
13758 static const struct opcode_descriptor jalr_insn_16_bd16 =
13759 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13761 static const struct opcode_descriptor jalr_insn_16_bd32 =
13762 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13764 static const struct opcode_descriptor jr_insn_16 =
13765 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13767 #define JR16_REG(opcode) ((opcode) & 0x1f)
13769 /* This table can be compacted, because no opcode replacement is made. */
13771 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13772 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13774 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13775 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13776 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13777 { 0, 0 } /* End marker for find_match(). */
13781 /* LUI instruction. */
13783 static const struct opcode_descriptor lui_insn =
13784 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13787 /* ADDIU instruction. */
13789 static const struct opcode_descriptor addiu_insn =
13790 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13792 static const struct opcode_descriptor addiupc_insn =
13793 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13795 #define ADDIUPC_REG_FIELD(r) \
13796 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13799 /* Relaxable instructions in a JAL delay slot: MOVE. */
13801 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13802 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13803 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13804 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13806 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13807 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13809 static const struct opcode_descriptor move_insns_32[] = {
13810 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13811 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13812 { 0, 0 } /* End marker for find_match(). */
13815 static const struct opcode_descriptor move_insn_16 =
13816 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13819 /* NOP instructions. */
13821 static const struct opcode_descriptor nop_insn_32 =
13822 { /* "nop", "", */ 0x00000000, 0xffffffff };
13824 static const struct opcode_descriptor nop_insn_16 =
13825 { /* "nop", "", */ 0x0c00, 0xffff };
13828 /* Instruction match support. */
13830 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13832 static int
13833 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13835 unsigned long indx;
13837 for (indx = 0; insn[indx].mask != 0; indx++)
13838 if (MATCH (opcode, insn[indx]))
13839 return indx;
13841 return -1;
13845 /* Branch and delay slot decoding support. */
13847 /* If PTR points to what *might* be a 16-bit branch or jump, then
13848 return the minimum length of its delay slot, otherwise return 0.
13849 Non-zero results are not definitive as we might be checking against
13850 the second half of another instruction. */
13852 static int
13853 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13855 unsigned long opcode;
13856 int bdsize;
13858 opcode = bfd_get_16 (abfd, ptr);
13859 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13860 /* 16-bit branch/jump with a 32-bit delay slot. */
13861 bdsize = 4;
13862 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13863 || find_match (opcode, ds_insns_16_bd16) >= 0)
13864 /* 16-bit branch/jump with a 16-bit delay slot. */
13865 bdsize = 2;
13866 else
13867 /* No delay slot. */
13868 bdsize = 0;
13870 return bdsize;
13873 /* If PTR points to what *might* be a 32-bit branch or jump, then
13874 return the minimum length of its delay slot, otherwise return 0.
13875 Non-zero results are not definitive as we might be checking against
13876 the second half of another instruction. */
13878 static int
13879 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13881 unsigned long opcode;
13882 int bdsize;
13884 opcode = bfd_get_micromips_32 (abfd, ptr);
13885 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13886 /* 32-bit branch/jump with a 32-bit delay slot. */
13887 bdsize = 4;
13888 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13889 /* 32-bit branch/jump with a 16-bit delay slot. */
13890 bdsize = 2;
13891 else
13892 /* No delay slot. */
13893 bdsize = 0;
13895 return bdsize;
13898 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13899 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13901 static bool
13902 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13904 unsigned long opcode;
13906 opcode = bfd_get_16 (abfd, ptr);
13907 if (MATCH (opcode, b_insn_16)
13908 /* B16 */
13909 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13910 /* JR16 */
13911 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13912 /* BEQZ16, BNEZ16 */
13913 || (MATCH (opcode, jalr_insn_16_bd32)
13914 /* JALR16 */
13915 && reg != JR16_REG (opcode) && reg != RA))
13916 return true;
13918 return false;
13921 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13922 then return TRUE, otherwise FALSE. */
13924 static bool
13925 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13927 unsigned long opcode;
13929 opcode = bfd_get_micromips_32 (abfd, ptr);
13930 if (MATCH (opcode, j_insn_32)
13931 /* J */
13932 || MATCH (opcode, bc_insn_32)
13933 /* BC1F, BC1T, BC2F, BC2T */
13934 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13935 /* JAL, JALX */
13936 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13937 /* BGEZ, BGTZ, BLEZ, BLTZ */
13938 || (MATCH (opcode, bzal_insn_32)
13939 /* BGEZAL, BLTZAL */
13940 && reg != OP32_SREG (opcode) && reg != RA)
13941 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13942 /* JALR, JALR.HB, BEQ, BNE */
13943 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13944 return true;
13946 return false;
13949 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13950 IRELEND) at OFFSET indicate that there must be a compact branch there,
13951 then return TRUE, otherwise FALSE. */
13953 static bool
13954 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13955 const Elf_Internal_Rela *internal_relocs,
13956 const Elf_Internal_Rela *irelend)
13958 const Elf_Internal_Rela *irel;
13959 unsigned long opcode;
13961 opcode = bfd_get_micromips_32 (abfd, ptr);
13962 if (find_match (opcode, bzc_insns_32) < 0)
13963 return false;
13965 for (irel = internal_relocs; irel < irelend; irel++)
13966 if (irel->r_offset == offset
13967 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13968 return true;
13970 return false;
13973 /* Bitsize checking. */
13974 #define IS_BITSIZE(val, N) \
13975 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13976 - (1ULL << ((N) - 1))) == (val))
13979 bool
13980 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13981 struct bfd_link_info *link_info,
13982 bool *again)
13984 bool insn32 = mips_elf_hash_table (link_info)->insn32;
13985 Elf_Internal_Shdr *symtab_hdr;
13986 Elf_Internal_Rela *internal_relocs;
13987 Elf_Internal_Rela *irel, *irelend;
13988 bfd_byte *contents = NULL;
13989 Elf_Internal_Sym *isymbuf = NULL;
13991 /* Assume nothing changes. */
13992 *again = false;
13994 /* We don't have to do anything for a relocatable link, if
13995 this section does not have relocs, or if this is not a
13996 code section. */
13998 if (bfd_link_relocatable (link_info)
13999 || sec->reloc_count == 0
14000 || (sec->flags & SEC_RELOC) == 0
14001 || (sec->flags & SEC_HAS_CONTENTS) == 0
14002 || (sec->flags & SEC_CODE) == 0)
14003 return true;
14005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14007 /* Get a copy of the native relocations. */
14008 internal_relocs = (_bfd_elf_link_read_relocs
14009 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14010 link_info->keep_memory));
14011 if (internal_relocs == NULL)
14012 goto error_return;
14014 /* Walk through them looking for relaxing opportunities. */
14015 irelend = internal_relocs + sec->reloc_count;
14016 for (irel = internal_relocs; irel < irelend; irel++)
14018 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14019 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14020 bool target_is_micromips_code_p;
14021 unsigned long opcode;
14022 bfd_vma symval;
14023 bfd_vma pcrval;
14024 bfd_byte *ptr;
14025 int fndopc;
14027 /* The number of bytes to delete for relaxation and from where
14028 to delete these bytes starting at irel->r_offset. */
14029 int delcnt = 0;
14030 int deloff = 0;
14032 /* If this isn't something that can be relaxed, then ignore
14033 this reloc. */
14034 if (r_type != R_MICROMIPS_HI16
14035 && r_type != R_MICROMIPS_PC16_S1
14036 && r_type != R_MICROMIPS_26_S1)
14037 continue;
14039 /* Get the section contents if we haven't done so already. */
14040 if (contents == NULL)
14042 /* Get cached copy if it exists. */
14043 if (elf_section_data (sec)->this_hdr.contents != NULL)
14044 contents = elf_section_data (sec)->this_hdr.contents;
14045 /* Go get them off disk. */
14046 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14047 goto error_return;
14049 ptr = contents + irel->r_offset;
14051 /* Read this BFD's local symbols if we haven't done so already. */
14052 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14054 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14055 if (isymbuf == NULL)
14056 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14057 symtab_hdr->sh_info, 0,
14058 NULL, NULL, NULL);
14059 if (isymbuf == NULL)
14060 goto error_return;
14063 /* Get the value of the symbol referred to by the reloc. */
14064 if (r_symndx < symtab_hdr->sh_info)
14066 /* A local symbol. */
14067 Elf_Internal_Sym *isym;
14068 asection *sym_sec;
14070 isym = isymbuf + r_symndx;
14071 if (isym->st_shndx == SHN_UNDEF)
14072 sym_sec = bfd_und_section_ptr;
14073 else if (isym->st_shndx == SHN_ABS)
14074 sym_sec = bfd_abs_section_ptr;
14075 else if (isym->st_shndx == SHN_COMMON)
14076 sym_sec = bfd_com_section_ptr;
14077 else
14078 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14079 symval = (isym->st_value
14080 + sym_sec->output_section->vma
14081 + sym_sec->output_offset);
14082 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14084 else
14086 unsigned long indx;
14087 struct elf_link_hash_entry *h;
14089 /* An external symbol. */
14090 indx = r_symndx - symtab_hdr->sh_info;
14091 h = elf_sym_hashes (abfd)[indx];
14092 BFD_ASSERT (h != NULL);
14094 if (h->root.type != bfd_link_hash_defined
14095 && h->root.type != bfd_link_hash_defweak)
14096 /* This appears to be a reference to an undefined
14097 symbol. Just ignore it -- it will be caught by the
14098 regular reloc processing. */
14099 continue;
14101 symval = (h->root.u.def.value
14102 + h->root.u.def.section->output_section->vma
14103 + h->root.u.def.section->output_offset);
14104 target_is_micromips_code_p = (!h->needs_plt
14105 && ELF_ST_IS_MICROMIPS (h->other));
14109 /* For simplicity of coding, we are going to modify the
14110 section contents, the section relocs, and the BFD symbol
14111 table. We must tell the rest of the code not to free up this
14112 information. It would be possible to instead create a table
14113 of changes which have to be made, as is done in coff-mips.c;
14114 that would be more work, but would require less memory when
14115 the linker is run. */
14117 /* Only 32-bit instructions relaxed. */
14118 if (irel->r_offset + 4 > sec->size)
14119 continue;
14121 opcode = bfd_get_micromips_32 (abfd, ptr);
14123 /* This is the pc-relative distance from the instruction the
14124 relocation is applied to, to the symbol referred. */
14125 pcrval = (symval
14126 - (sec->output_section->vma + sec->output_offset)
14127 - irel->r_offset);
14129 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14130 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14131 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
14133 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14135 where pcrval has first to be adjusted to apply against the LO16
14136 location (we make the adjustment later on, when we have figured
14137 out the offset). */
14138 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14140 bool bzc = false;
14141 unsigned long nextopc;
14142 unsigned long reg;
14143 bfd_vma offset;
14145 /* Give up if the previous reloc was a HI16 against this symbol
14146 too. */
14147 if (irel > internal_relocs
14148 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14149 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14150 continue;
14152 /* Or if the next reloc is not a LO16 against this symbol. */
14153 if (irel + 1 >= irelend
14154 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14155 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14156 continue;
14158 /* Or if the second next reloc is a LO16 against this symbol too. */
14159 if (irel + 2 >= irelend
14160 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14161 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14162 continue;
14164 /* See if the LUI instruction *might* be in a branch delay slot.
14165 We check whether what looks like a 16-bit branch or jump is
14166 actually an immediate argument to a compact branch, and let
14167 it through if so. */
14168 if (irel->r_offset >= 2
14169 && check_br16_dslot (abfd, ptr - 2)
14170 && !(irel->r_offset >= 4
14171 && (bzc = check_relocated_bzc (abfd,
14172 ptr - 4, irel->r_offset - 4,
14173 internal_relocs, irelend))))
14174 continue;
14175 if (irel->r_offset >= 4
14176 && !bzc
14177 && check_br32_dslot (abfd, ptr - 4))
14178 continue;
14180 reg = OP32_SREG (opcode);
14182 /* We only relax adjacent instructions or ones separated with
14183 a branch or jump that has a delay slot. The branch or jump
14184 must not fiddle with the register used to hold the address.
14185 Subtract 4 for the LUI itself. */
14186 offset = irel[1].r_offset - irel[0].r_offset;
14187 switch (offset - 4)
14189 case 0:
14190 break;
14191 case 2:
14192 if (check_br16 (abfd, ptr + 4, reg))
14193 break;
14194 continue;
14195 case 4:
14196 if (check_br32 (abfd, ptr + 4, reg))
14197 break;
14198 continue;
14199 default:
14200 continue;
14203 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14205 /* Give up unless the same register is used with both
14206 relocations. */
14207 if (OP32_SREG (nextopc) != reg)
14208 continue;
14210 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14211 and rounding up to take masking of the two LSBs into account. */
14212 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14214 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14215 if (IS_BITSIZE (symval, 16))
14217 /* Fix the relocation's type. */
14218 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14220 /* Instructions using R_MICROMIPS_LO16 have the base or
14221 source register in bits 20:16. This register becomes $0
14222 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
14223 nextopc &= ~0x001f0000;
14224 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14225 contents + irel[1].r_offset);
14228 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14229 We add 4 to take LUI deletion into account while checking
14230 the PC-relative distance. */
14231 else if (symval % 4 == 0
14232 && IS_BITSIZE (pcrval + 4, 25)
14233 && MATCH (nextopc, addiu_insn)
14234 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14235 && OP16_VALID_REG (OP32_TREG (nextopc)))
14237 /* Fix the relocation's type. */
14238 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14240 /* Replace ADDIU with the ADDIUPC version. */
14241 nextopc = (addiupc_insn.match
14242 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14244 bfd_put_micromips_32 (abfd, nextopc,
14245 contents + irel[1].r_offset);
14248 /* Can't do anything, give up, sigh... */
14249 else
14250 continue;
14252 /* Fix the relocation's type. */
14253 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14255 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14256 delcnt = 4;
14257 deloff = 0;
14260 /* Compact branch relaxation -- due to the multitude of macros
14261 employed by the compiler/assembler, compact branches are not
14262 always generated. Obviously, this can/will be fixed elsewhere,
14263 but there is no drawback in double checking it here. */
14264 else if (r_type == R_MICROMIPS_PC16_S1
14265 && irel->r_offset + 5 < sec->size
14266 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14267 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14268 && ((!insn32
14269 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14270 nop_insn_16) ? 2 : 0))
14271 || (irel->r_offset + 7 < sec->size
14272 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14273 ptr + 4),
14274 nop_insn_32) ? 4 : 0))))
14276 unsigned long reg;
14278 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14280 /* Replace BEQZ/BNEZ with the compact version. */
14281 opcode = (bzc_insns_32[fndopc].match
14282 | BZC32_REG_FIELD (reg)
14283 | (opcode & 0xffff)); /* Addend value. */
14285 bfd_put_micromips_32 (abfd, opcode, ptr);
14287 /* Delete the delay slot NOP: two or four bytes from
14288 irel->offset + 4; delcnt has already been set above. */
14289 deloff = 4;
14292 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14293 to check the distance from the next instruction, so subtract 2. */
14294 else if (!insn32
14295 && r_type == R_MICROMIPS_PC16_S1
14296 && IS_BITSIZE (pcrval - 2, 11)
14297 && find_match (opcode, b_insns_32) >= 0)
14299 /* Fix the relocation's type. */
14300 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14302 /* Replace the 32-bit opcode with a 16-bit opcode. */
14303 bfd_put_16 (abfd,
14304 (b_insn_16.match
14305 | (opcode & 0x3ff)), /* Addend value. */
14306 ptr);
14308 /* Delete 2 bytes from irel->r_offset + 2. */
14309 delcnt = 2;
14310 deloff = 2;
14313 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14314 to check the distance from the next instruction, so subtract 2. */
14315 else if (!insn32
14316 && r_type == R_MICROMIPS_PC16_S1
14317 && IS_BITSIZE (pcrval - 2, 8)
14318 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14319 && OP16_VALID_REG (OP32_SREG (opcode)))
14320 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14321 && OP16_VALID_REG (OP32_TREG (opcode)))))
14323 unsigned long reg;
14325 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14327 /* Fix the relocation's type. */
14328 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14330 /* Replace the 32-bit opcode with a 16-bit opcode. */
14331 bfd_put_16 (abfd,
14332 (bz_insns_16[fndopc].match
14333 | BZ16_REG_FIELD (reg)
14334 | (opcode & 0x7f)), /* Addend value. */
14335 ptr);
14337 /* Delete 2 bytes from irel->r_offset + 2. */
14338 delcnt = 2;
14339 deloff = 2;
14342 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14343 else if (!insn32
14344 && r_type == R_MICROMIPS_26_S1
14345 && target_is_micromips_code_p
14346 && irel->r_offset + 7 < sec->size
14347 && MATCH (opcode, jal_insn_32_bd32))
14349 unsigned long n32opc;
14350 bool relaxed = false;
14352 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14354 if (MATCH (n32opc, nop_insn_32))
14356 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14357 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14359 relaxed = true;
14361 else if (find_match (n32opc, move_insns_32) >= 0)
14363 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14364 bfd_put_16 (abfd,
14365 (move_insn_16.match
14366 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14367 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14368 ptr + 4);
14370 relaxed = true;
14372 /* Other 32-bit instructions relaxable to 16-bit
14373 instructions will be handled here later. */
14375 if (relaxed)
14377 /* JAL with 32-bit delay slot that is changed to a JALS
14378 with 16-bit delay slot. */
14379 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14381 /* Delete 2 bytes from irel->r_offset + 6. */
14382 delcnt = 2;
14383 deloff = 6;
14387 if (delcnt != 0)
14389 /* Note that we've changed the relocs, section contents, etc. */
14390 elf_section_data (sec)->relocs = internal_relocs;
14391 elf_section_data (sec)->this_hdr.contents = contents;
14392 symtab_hdr->contents = (unsigned char *) isymbuf;
14394 /* Delete bytes depending on the delcnt and deloff. */
14395 if (!mips_elf_relax_delete_bytes (abfd, sec,
14396 irel->r_offset + deloff, delcnt))
14397 goto error_return;
14399 /* That will change things, so we should relax again.
14400 Note that this is not required, and it may be slow. */
14401 *again = true;
14405 if (isymbuf != NULL
14406 && symtab_hdr->contents != (unsigned char *) isymbuf)
14408 if (! link_info->keep_memory)
14409 free (isymbuf);
14410 else
14412 /* Cache the symbols for elf_link_input_bfd. */
14413 symtab_hdr->contents = (unsigned char *) isymbuf;
14417 if (contents != NULL
14418 && elf_section_data (sec)->this_hdr.contents != contents)
14420 if (! link_info->keep_memory)
14421 free (contents);
14422 else
14424 /* Cache the section contents for elf_link_input_bfd. */
14425 elf_section_data (sec)->this_hdr.contents = contents;
14429 if (elf_section_data (sec)->relocs != internal_relocs)
14430 free (internal_relocs);
14432 return true;
14434 error_return:
14435 if (symtab_hdr->contents != (unsigned char *) isymbuf)
14436 free (isymbuf);
14437 if (elf_section_data (sec)->this_hdr.contents != contents)
14438 free (contents);
14439 if (elf_section_data (sec)->relocs != internal_relocs)
14440 free (internal_relocs);
14442 return false;
14445 /* Create a MIPS ELF linker hash table. */
14447 struct bfd_link_hash_table *
14448 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14450 struct mips_elf_link_hash_table *ret;
14451 size_t amt = sizeof (struct mips_elf_link_hash_table);
14453 ret = bfd_zmalloc (amt);
14454 if (ret == NULL)
14455 return NULL;
14457 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14458 mips_elf_link_hash_newfunc,
14459 sizeof (struct mips_elf_link_hash_entry),
14460 MIPS_ELF_DATA))
14462 free (ret);
14463 return NULL;
14465 ret->root.init_plt_refcount.plist = NULL;
14466 ret->root.init_plt_offset.plist = NULL;
14468 return &ret->root.root;
14471 /* Likewise, but indicate that the target is VxWorks. */
14473 struct bfd_link_hash_table *
14474 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14476 struct bfd_link_hash_table *ret;
14478 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14479 if (ret)
14481 struct mips_elf_link_hash_table *htab;
14483 htab = (struct mips_elf_link_hash_table *) ret;
14484 htab->use_plts_and_copy_relocs = true;
14486 return ret;
14489 /* A function that the linker calls if we are allowed to use PLTs
14490 and copy relocs. */
14492 void
14493 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14495 mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14498 /* A function that the linker calls to select between all or only
14499 32-bit microMIPS instructions, and between making or ignoring
14500 branch relocation checks for invalid transitions between ISA modes.
14501 Also record whether we have been configured for a GNU target. */
14503 void
14504 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14505 bool ignore_branch_isa,
14506 bool gnu_target)
14508 mips_elf_hash_table (info)->insn32 = insn32;
14509 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14510 mips_elf_hash_table (info)->gnu_target = gnu_target;
14513 /* A function that the linker calls to enable use of compact branches in
14514 linker generated code for MIPSR6. */
14516 void
14517 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14519 mips_elf_hash_table (info)->compact_branches = on;
14523 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14525 struct mips_mach_extension
14527 unsigned long extension, base;
14530 /* An array that maps 64-bit architectures to the corresponding 32-bit
14531 architectures. */
14532 static const struct mips_mach_extension mips_mach_32_64[] =
14534 { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14535 { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14536 { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14537 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14538 { bfd_mach_mipsisa64, bfd_mach_mipsisa32 }
14541 /* An array describing how BFD machines relate to one another. The entries
14542 are ordered topologically with MIPS I extensions listed last. */
14544 static const struct mips_mach_extension mips_mach_extensions[] =
14546 /* MIPS64r2 extensions. */
14547 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14548 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14549 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14550 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14551 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14552 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14553 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14555 /* MIPS64 extensions. */
14556 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14557 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14558 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14560 /* MIPS V extensions. */
14561 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14563 /* R10000 extensions. */
14564 { bfd_mach_mips12000, bfd_mach_mips10000 },
14565 { bfd_mach_mips14000, bfd_mach_mips10000 },
14566 { bfd_mach_mips16000, bfd_mach_mips10000 },
14568 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14569 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14570 better to allow vr5400 and vr5500 code to be merged anyway, since
14571 many libraries will just use the core ISA. Perhaps we could add
14572 some sort of ASE flag if this ever proves a problem. */
14573 { bfd_mach_mips5500, bfd_mach_mips5400 },
14574 { bfd_mach_mips5400, bfd_mach_mips5000 },
14576 /* MIPS IV extensions. */
14577 { bfd_mach_mips5, bfd_mach_mips8000 },
14578 { bfd_mach_mips10000, bfd_mach_mips8000 },
14579 { bfd_mach_mips5000, bfd_mach_mips8000 },
14580 { bfd_mach_mips7000, bfd_mach_mips8000 },
14581 { bfd_mach_mips9000, bfd_mach_mips8000 },
14583 /* VR4100 extensions. */
14584 { bfd_mach_mips4120, bfd_mach_mips4100 },
14585 { bfd_mach_mips4111, bfd_mach_mips4100 },
14587 /* MIPS III extensions. */
14588 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14589 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14590 { bfd_mach_mips8000, bfd_mach_mips4000 },
14591 { bfd_mach_mips4650, bfd_mach_mips4000 },
14592 { bfd_mach_mips4600, bfd_mach_mips4000 },
14593 { bfd_mach_mips4400, bfd_mach_mips4000 },
14594 { bfd_mach_mips4300, bfd_mach_mips4000 },
14595 { bfd_mach_mips4100, bfd_mach_mips4000 },
14596 { bfd_mach_mips5900, bfd_mach_mips4000 },
14598 /* MIPS32r3 extensions. */
14599 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14601 /* MIPS32r2 extensions. */
14602 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14604 /* MIPS32 extensions. */
14605 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14607 /* MIPS II extensions. */
14608 { bfd_mach_mips4000, bfd_mach_mips6000 },
14609 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14610 { bfd_mach_mips4010, bfd_mach_mips6000 },
14611 { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14613 /* MIPS I extensions. */
14614 { bfd_mach_mips6000, bfd_mach_mips3000 },
14615 { bfd_mach_mips3900, bfd_mach_mips3000 }
14618 /* Return true if bfd machine EXTENSION is the same as BASE, or if
14619 EXTENSION is the 64-bit equivalent of a 32-bit BASE. */
14621 static bool
14622 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14624 size_t i;
14626 if (extension == base)
14627 return true;
14629 for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14630 if (extension == mips_mach_32_64[i].extension)
14631 return base == mips_mach_32_64[i].base;
14633 return false;
14636 static bool
14637 mips_mach_extends_p (unsigned long base, unsigned long extension)
14639 size_t i;
14641 if (mips_mach_extends_32_64 (base, extension))
14642 return true;
14644 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14645 if (extension == mips_mach_extensions[i].extension)
14647 extension = mips_mach_extensions[i].base;
14648 if (mips_mach_extends_32_64 (base, extension))
14649 return true;
14652 return false;
14655 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14657 static unsigned long
14658 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14660 switch (isa_ext)
14662 case AFL_EXT_3900: return bfd_mach_mips3900;
14663 case AFL_EXT_4010: return bfd_mach_mips4010;
14664 case AFL_EXT_4100: return bfd_mach_mips4100;
14665 case AFL_EXT_4111: return bfd_mach_mips4111;
14666 case AFL_EXT_4120: return bfd_mach_mips4120;
14667 case AFL_EXT_4650: return bfd_mach_mips4650;
14668 case AFL_EXT_5400: return bfd_mach_mips5400;
14669 case AFL_EXT_5500: return bfd_mach_mips5500;
14670 case AFL_EXT_5900: return bfd_mach_mips5900;
14671 case AFL_EXT_10000: return bfd_mach_mips10000;
14672 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14673 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14674 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14675 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14676 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14677 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14678 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14679 default: return bfd_mach_mips3000;
14683 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14685 unsigned int
14686 bfd_mips_isa_ext (bfd *abfd)
14688 switch (bfd_get_mach (abfd))
14690 case bfd_mach_mips3900: return AFL_EXT_3900;
14691 case bfd_mach_mips4010: return AFL_EXT_4010;
14692 case bfd_mach_mips4100: return AFL_EXT_4100;
14693 case bfd_mach_mips4111: return AFL_EXT_4111;
14694 case bfd_mach_mips4120: return AFL_EXT_4120;
14695 case bfd_mach_mips4650: return AFL_EXT_4650;
14696 case bfd_mach_mips5400: return AFL_EXT_5400;
14697 case bfd_mach_mips5500: return AFL_EXT_5500;
14698 case bfd_mach_mips5900: return AFL_EXT_5900;
14699 case bfd_mach_mips10000: return AFL_EXT_10000;
14700 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14701 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14702 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14703 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14704 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14705 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14706 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14707 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14708 case bfd_mach_mips_interaptiv_mr2:
14709 return AFL_EXT_INTERAPTIV_MR2;
14710 default: return 0;
14714 /* Encode ISA level and revision as a single value. */
14715 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14717 /* Decode a single value into level and revision. */
14718 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14719 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14721 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14723 static void
14724 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14726 int new_isa = 0;
14727 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14729 case EF_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14730 case EF_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14731 case EF_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14732 case EF_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14733 case EF_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14734 case EF_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14735 case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14736 case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14737 case EF_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14738 case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14739 case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14740 default:
14741 _bfd_error_handler
14742 /* xgettext:c-format */
14743 (_("%pB: unknown architecture %s"),
14744 abfd, bfd_printable_name (abfd));
14747 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14749 abiflags->isa_level = ISA_LEVEL (new_isa);
14750 abiflags->isa_rev = ISA_REV (new_isa);
14753 /* Update the isa_ext if ABFD describes a further extension. */
14754 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14755 bfd_get_mach (abfd)))
14756 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14759 /* Return true if the given ELF header flags describe a 32-bit binary. */
14761 static bool
14762 mips_32bit_flags_p (flagword flags)
14764 return ((flags & EF_MIPS_32BITMODE) != 0
14765 || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
14766 || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
14767 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
14768 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
14769 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
14770 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
14771 || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
14774 /* Infer the content of the ABI flags based on the elf header. */
14776 static void
14777 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14779 obj_attribute *in_attr;
14781 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14782 update_mips_abiflags_isa (abfd, abiflags);
14784 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14785 abiflags->gpr_size = AFL_REG_32;
14786 else
14787 abiflags->gpr_size = AFL_REG_64;
14789 abiflags->cpr1_size = AFL_REG_NONE;
14791 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14792 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14794 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14795 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14796 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14797 && abiflags->gpr_size == AFL_REG_32))
14798 abiflags->cpr1_size = AFL_REG_32;
14799 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14800 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14801 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14802 abiflags->cpr1_size = AFL_REG_64;
14804 abiflags->cpr2_size = AFL_REG_NONE;
14806 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14807 abiflags->ases |= AFL_ASE_MDMX;
14808 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14809 abiflags->ases |= AFL_ASE_MIPS16;
14810 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14811 abiflags->ases |= AFL_ASE_MICROMIPS;
14813 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14814 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14815 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14816 && abiflags->isa_level >= 32
14817 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14818 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14821 /* We need to use a special link routine to handle the .reginfo and
14822 the .mdebug sections. We need to merge all instances of these
14823 sections together, not write them all out sequentially. */
14825 bool
14826 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14828 asection *o;
14829 struct bfd_link_order *p;
14830 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14831 asection *rtproc_sec, *abiflags_sec;
14832 Elf32_RegInfo reginfo;
14833 struct ecoff_debug_info debug;
14834 struct mips_htab_traverse_info hti;
14835 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14836 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14837 HDRR *symhdr = &debug.symbolic_header;
14838 void *mdebug_handle = NULL;
14839 asection *s;
14840 EXTR esym;
14841 unsigned int i;
14842 bfd_size_type amt;
14843 struct mips_elf_link_hash_table *htab;
14845 static const char * const secname[] =
14847 ".text", ".init", ".fini", ".data",
14848 ".rodata", ".sdata", ".sbss", ".bss"
14850 static const int sc[] =
14852 scText, scInit, scFini, scData,
14853 scRData, scSData, scSBss, scBss
14856 htab = mips_elf_hash_table (info);
14857 BFD_ASSERT (htab != NULL);
14859 /* Sort the dynamic symbols so that those with GOT entries come after
14860 those without. */
14861 if (!mips_elf_sort_hash_table (abfd, info))
14862 return false;
14864 /* Create any scheduled LA25 stubs. */
14865 hti.info = info;
14866 hti.output_bfd = abfd;
14867 hti.error = false;
14868 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14869 if (hti.error)
14870 return false;
14872 /* Get a value for the GP register. */
14873 if (elf_gp (abfd) == 0)
14875 struct bfd_link_hash_entry *h;
14877 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14878 if (h != NULL && h->type == bfd_link_hash_defined)
14879 elf_gp (abfd) = (h->u.def.value
14880 + h->u.def.section->output_section->vma
14881 + h->u.def.section->output_offset);
14882 else if (htab->root.target_os == is_vxworks
14883 && (h = bfd_link_hash_lookup (info->hash,
14884 "_GLOBAL_OFFSET_TABLE_",
14885 false, false, true))
14886 && h->type == bfd_link_hash_defined)
14887 elf_gp (abfd) = (h->u.def.section->output_section->vma
14888 + h->u.def.section->output_offset
14889 + h->u.def.value);
14890 else if (bfd_link_relocatable (info))
14892 bfd_vma lo = MINUS_ONE;
14894 /* Find the GP-relative section with the lowest offset. */
14895 for (o = abfd->sections; o != NULL; o = o->next)
14896 if (o->vma < lo
14897 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14898 lo = o->vma;
14900 /* And calculate GP relative to that. */
14901 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14903 else
14905 /* If the relocate_section function needs to do a reloc
14906 involving the GP value, it should make a reloc_dangerous
14907 callback to warn that GP is not defined. */
14911 /* Go through the sections and collect the .reginfo and .mdebug
14912 information. */
14913 abiflags_sec = NULL;
14914 reginfo_sec = NULL;
14915 mdebug_sec = NULL;
14916 gptab_data_sec = NULL;
14917 gptab_bss_sec = NULL;
14918 for (o = abfd->sections; o != NULL; o = o->next)
14920 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14922 /* We have found the .MIPS.abiflags section in the output file.
14923 Look through all the link_orders comprising it and remove them.
14924 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14925 for (p = o->map_head.link_order; p != NULL; p = p->next)
14927 asection *input_section;
14929 if (p->type != bfd_indirect_link_order)
14931 if (p->type == bfd_data_link_order)
14932 continue;
14933 abort ();
14936 input_section = p->u.indirect.section;
14938 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14939 elf_link_input_bfd ignores this section. */
14940 input_section->flags &= ~SEC_HAS_CONTENTS;
14943 /* Size has been set in _bfd_mips_elf_late_size_sections. */
14944 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14946 /* Skip this section later on (I don't think this currently
14947 matters, but someday it might). */
14948 o->map_head.link_order = NULL;
14950 abiflags_sec = o;
14953 if (strcmp (o->name, ".reginfo") == 0)
14955 memset (&reginfo, 0, sizeof reginfo);
14957 /* We have found the .reginfo section in the output file.
14958 Look through all the link_orders comprising it and merge
14959 the information together. */
14960 for (p = o->map_head.link_order; p != NULL; p = p->next)
14962 asection *input_section;
14963 bfd *input_bfd;
14964 Elf32_External_RegInfo ext;
14965 Elf32_RegInfo sub;
14966 bfd_size_type sz;
14968 if (p->type != bfd_indirect_link_order)
14970 if (p->type == bfd_data_link_order)
14971 continue;
14972 abort ();
14975 input_section = p->u.indirect.section;
14976 input_bfd = input_section->owner;
14978 sz = (input_section->size < sizeof (ext)
14979 ? input_section->size : sizeof (ext));
14980 memset (&ext, 0, sizeof (ext));
14981 if (! bfd_get_section_contents (input_bfd, input_section,
14982 &ext, 0, sz))
14983 return false;
14985 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14987 reginfo.ri_gprmask |= sub.ri_gprmask;
14988 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14989 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14990 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14991 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14993 /* ri_gp_value is set by the function
14994 `_bfd_mips_elf_section_processing' when the section is
14995 finally written out. */
14997 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14998 elf_link_input_bfd ignores this section. */
14999 input_section->flags &= ~SEC_HAS_CONTENTS;
15002 /* Size has been set in _bfd_mips_elf_late_size_sections. */
15003 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
15005 /* Skip this section later on (I don't think this currently
15006 matters, but someday it might). */
15007 o->map_head.link_order = NULL;
15009 reginfo_sec = o;
15012 if (strcmp (o->name, ".mdebug") == 0)
15014 struct extsym_info einfo;
15015 bfd_vma last;
15017 /* We have found the .mdebug section in the output file.
15018 Look through all the link_orders comprising it and merge
15019 the information together. */
15020 symhdr->magic = swap->sym_magic;
15021 /* FIXME: What should the version stamp be? */
15022 symhdr->vstamp = 0;
15023 symhdr->ilineMax = 0;
15024 symhdr->cbLine = 0;
15025 symhdr->idnMax = 0;
15026 symhdr->ipdMax = 0;
15027 symhdr->isymMax = 0;
15028 symhdr->ioptMax = 0;
15029 symhdr->iauxMax = 0;
15030 symhdr->issMax = 0;
15031 symhdr->issExtMax = 0;
15032 symhdr->ifdMax = 0;
15033 symhdr->crfd = 0;
15034 symhdr->iextMax = 0;
15036 /* We accumulate the debugging information itself in the
15037 debug_info structure. */
15038 debug.alloc_syments = false;
15039 debug.line = NULL;
15040 debug.external_dnr = NULL;
15041 debug.external_pdr = NULL;
15042 debug.external_sym = NULL;
15043 debug.external_opt = NULL;
15044 debug.external_aux = NULL;
15045 debug.ss = NULL;
15046 debug.ssext = debug.ssext_end = NULL;
15047 debug.external_fdr = NULL;
15048 debug.external_rfd = NULL;
15049 debug.external_ext = debug.external_ext_end = NULL;
15051 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15052 if (mdebug_handle == NULL)
15053 return false;
15055 esym.jmptbl = 0;
15056 esym.cobol_main = 0;
15057 esym.weakext = 0;
15058 esym.reserved = 0;
15059 esym.ifd = ifdNil;
15060 esym.asym.iss = issNil;
15061 esym.asym.st = stLocal;
15062 esym.asym.reserved = 0;
15063 esym.asym.index = indexNil;
15064 last = 0;
15065 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15067 esym.asym.sc = sc[i];
15068 s = bfd_get_section_by_name (abfd, secname[i]);
15069 if (s != NULL)
15071 esym.asym.value = s->vma;
15072 last = s->vma + s->size;
15074 else
15075 esym.asym.value = last;
15076 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15077 secname[i], &esym))
15078 return false;
15081 for (p = o->map_head.link_order; p != NULL; p = p->next)
15083 asection *input_section;
15084 bfd *input_bfd;
15085 const struct ecoff_debug_swap *input_swap;
15086 struct ecoff_debug_info input_debug;
15087 char *eraw_src;
15088 char *eraw_end;
15090 if (p->type != bfd_indirect_link_order)
15092 if (p->type == bfd_data_link_order)
15093 continue;
15094 abort ();
15097 input_section = p->u.indirect.section;
15098 input_bfd = input_section->owner;
15100 if (!is_mips_elf (input_bfd))
15102 /* I don't know what a non MIPS ELF bfd would be
15103 doing with a .mdebug section, but I don't really
15104 want to deal with it. */
15105 continue;
15108 input_swap = (get_elf_backend_data (input_bfd)
15109 ->elf_backend_ecoff_debug_swap);
15111 BFD_ASSERT (p->size == input_section->size);
15113 /* The ECOFF linking code expects that we have already
15114 read in the debugging information and set up an
15115 ecoff_debug_info structure, so we do that now. */
15116 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15117 &input_debug))
15118 return false;
15120 if (! (bfd_ecoff_debug_accumulate
15121 (mdebug_handle, abfd, &debug, swap, input_bfd,
15122 &input_debug, input_swap, info)))
15124 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15125 return false;
15128 /* Loop through the external symbols. For each one with
15129 interesting information, try to find the symbol in
15130 the linker global hash table and save the information
15131 for the output external symbols. */
15132 eraw_src = input_debug.external_ext;
15133 eraw_end = (eraw_src
15134 + (input_debug.symbolic_header.iextMax
15135 * input_swap->external_ext_size));
15136 for (;
15137 eraw_src < eraw_end;
15138 eraw_src += input_swap->external_ext_size)
15140 EXTR ext;
15141 const char *name;
15142 struct mips_elf_link_hash_entry *h;
15144 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15145 if (ext.asym.sc == scNil
15146 || ext.asym.sc == scUndefined
15147 || ext.asym.sc == scSUndefined)
15148 continue;
15150 name = input_debug.ssext + ext.asym.iss;
15151 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15152 name, false, false, true);
15153 if (h == NULL || h->esym.ifd != -2)
15154 continue;
15156 if (ext.ifd != -1)
15158 BFD_ASSERT (ext.ifd
15159 < input_debug.symbolic_header.ifdMax);
15160 ext.ifd = input_debug.ifdmap[ext.ifd];
15163 h->esym = ext;
15166 /* Free up the information we just read. */
15167 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15169 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15170 elf_link_input_bfd ignores this section. */
15171 input_section->flags &= ~SEC_HAS_CONTENTS;
15174 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15176 /* Create .rtproc section. */
15177 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15178 if (rtproc_sec == NULL)
15180 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15181 | SEC_LINKER_CREATED | SEC_READONLY);
15183 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15184 ".rtproc",
15185 flags);
15186 if (rtproc_sec == NULL
15187 || !bfd_set_section_alignment (rtproc_sec, 4))
15188 return false;
15191 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15192 info, rtproc_sec,
15193 &debug))
15194 return false;
15197 /* Build the external symbol information. */
15198 einfo.abfd = abfd;
15199 einfo.info = info;
15200 einfo.debug = &debug;
15201 einfo.swap = swap;
15202 einfo.failed = false;
15203 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15204 mips_elf_output_extsym, &einfo);
15205 if (einfo.failed)
15206 return false;
15208 /* Set the size of the .mdebug section. */
15209 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15211 /* Skip this section later on (I don't think this currently
15212 matters, but someday it might). */
15213 o->map_head.link_order = NULL;
15215 mdebug_sec = o;
15218 if (startswith (o->name, ".gptab."))
15220 const char *subname;
15221 unsigned int c;
15222 Elf32_gptab *tab;
15223 Elf32_External_gptab *ext_tab;
15224 unsigned int j;
15226 /* The .gptab.sdata and .gptab.sbss sections hold
15227 information describing how the small data area would
15228 change depending upon the -G switch. These sections
15229 not used in executables files. */
15230 if (! bfd_link_relocatable (info))
15232 for (p = o->map_head.link_order; p != NULL; p = p->next)
15234 asection *input_section;
15236 if (p->type != bfd_indirect_link_order)
15238 if (p->type == bfd_data_link_order)
15239 continue;
15240 abort ();
15243 input_section = p->u.indirect.section;
15245 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15246 elf_link_input_bfd ignores this section. */
15247 input_section->flags &= ~SEC_HAS_CONTENTS;
15250 /* Skip this section later on (I don't think this
15251 currently matters, but someday it might). */
15252 o->map_head.link_order = NULL;
15254 /* Really remove the section. */
15255 bfd_section_list_remove (abfd, o);
15256 --abfd->section_count;
15258 continue;
15261 /* There is one gptab for initialized data, and one for
15262 uninitialized data. */
15263 if (strcmp (o->name, ".gptab.sdata") == 0)
15264 gptab_data_sec = o;
15265 else if (strcmp (o->name, ".gptab.sbss") == 0)
15266 gptab_bss_sec = o;
15267 else
15269 _bfd_error_handler
15270 /* xgettext:c-format */
15271 (_("%pB: illegal section name `%pA'"), abfd, o);
15272 bfd_set_error (bfd_error_nonrepresentable_section);
15273 return false;
15276 /* The linker script always combines .gptab.data and
15277 .gptab.sdata into .gptab.sdata, and likewise for
15278 .gptab.bss and .gptab.sbss. It is possible that there is
15279 no .sdata or .sbss section in the output file, in which
15280 case we must change the name of the output section. */
15281 subname = o->name + sizeof ".gptab" - 1;
15282 if (bfd_get_section_by_name (abfd, subname) == NULL)
15284 if (o == gptab_data_sec)
15285 o->name = ".gptab.data";
15286 else
15287 o->name = ".gptab.bss";
15288 subname = o->name + sizeof ".gptab" - 1;
15289 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15292 /* Set up the first entry. */
15293 c = 1;
15294 amt = c * sizeof (Elf32_gptab);
15295 tab = bfd_malloc (amt);
15296 if (tab == NULL)
15297 return false;
15298 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15299 tab[0].gt_header.gt_unused = 0;
15301 /* Combine the input sections. */
15302 for (p = o->map_head.link_order; p != NULL; p = p->next)
15304 asection *input_section;
15305 bfd *input_bfd;
15306 bfd_size_type size;
15307 unsigned long last;
15308 bfd_size_type gpentry;
15310 if (p->type != bfd_indirect_link_order)
15312 if (p->type == bfd_data_link_order)
15313 continue;
15314 abort ();
15317 input_section = p->u.indirect.section;
15318 input_bfd = input_section->owner;
15320 /* Combine the gptab entries for this input section one
15321 by one. We know that the input gptab entries are
15322 sorted by ascending -G value. */
15323 size = input_section->size;
15324 last = 0;
15325 for (gpentry = sizeof (Elf32_External_gptab);
15326 gpentry < size;
15327 gpentry += sizeof (Elf32_External_gptab))
15329 Elf32_External_gptab ext_gptab;
15330 Elf32_gptab int_gptab;
15331 unsigned long val;
15332 unsigned long add;
15333 bool exact;
15334 unsigned int look;
15336 if (! (bfd_get_section_contents
15337 (input_bfd, input_section, &ext_gptab, gpentry,
15338 sizeof (Elf32_External_gptab))))
15340 free (tab);
15341 return false;
15344 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15345 &int_gptab);
15346 val = int_gptab.gt_entry.gt_g_value;
15347 add = int_gptab.gt_entry.gt_bytes - last;
15349 exact = false;
15350 for (look = 1; look < c; look++)
15352 if (tab[look].gt_entry.gt_g_value >= val)
15353 tab[look].gt_entry.gt_bytes += add;
15355 if (tab[look].gt_entry.gt_g_value == val)
15356 exact = true;
15359 if (! exact)
15361 Elf32_gptab *new_tab;
15362 unsigned int max;
15364 /* We need a new table entry. */
15365 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15366 new_tab = bfd_realloc (tab, amt);
15367 if (new_tab == NULL)
15369 free (tab);
15370 return false;
15372 tab = new_tab;
15373 tab[c].gt_entry.gt_g_value = val;
15374 tab[c].gt_entry.gt_bytes = add;
15376 /* Merge in the size for the next smallest -G
15377 value, since that will be implied by this new
15378 value. */
15379 max = 0;
15380 for (look = 1; look < c; look++)
15382 if (tab[look].gt_entry.gt_g_value < val
15383 && (max == 0
15384 || (tab[look].gt_entry.gt_g_value
15385 > tab[max].gt_entry.gt_g_value)))
15386 max = look;
15388 if (max != 0)
15389 tab[c].gt_entry.gt_bytes +=
15390 tab[max].gt_entry.gt_bytes;
15392 ++c;
15395 last = int_gptab.gt_entry.gt_bytes;
15398 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15399 elf_link_input_bfd ignores this section. */
15400 input_section->flags &= ~SEC_HAS_CONTENTS;
15403 /* The table must be sorted by -G value. */
15404 if (c > 2)
15405 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15407 /* Swap out the table. */
15408 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15409 ext_tab = bfd_alloc (abfd, amt);
15410 if (ext_tab == NULL)
15412 free (tab);
15413 return false;
15416 for (j = 0; j < c; j++)
15417 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15418 free (tab);
15420 o->size = c * sizeof (Elf32_External_gptab);
15421 o->contents = (bfd_byte *) ext_tab;
15423 /* Skip this section later on (I don't think this currently
15424 matters, but someday it might). */
15425 o->map_head.link_order = NULL;
15429 /* Invoke the regular ELF backend linker to do all the work. */
15430 if (!bfd_elf_final_link (abfd, info))
15431 return false;
15433 /* Now write out the computed sections. */
15435 if (abiflags_sec != NULL)
15437 Elf_External_ABIFlags_v0 ext;
15438 Elf_Internal_ABIFlags_v0 *abiflags;
15440 abiflags = &mips_elf_tdata (abfd)->abiflags;
15442 /* Set up the abiflags if no valid input sections were found. */
15443 if (!mips_elf_tdata (abfd)->abiflags_valid)
15445 infer_mips_abiflags (abfd, abiflags);
15446 mips_elf_tdata (abfd)->abiflags_valid = true;
15448 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15449 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15450 return false;
15453 if (reginfo_sec != NULL)
15455 Elf32_External_RegInfo ext;
15457 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15458 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15459 return false;
15462 if (mdebug_sec != NULL)
15464 BFD_ASSERT (abfd->output_has_begun);
15465 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15466 swap, info,
15467 mdebug_sec->filepos))
15468 return false;
15470 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15473 if (gptab_data_sec != NULL)
15475 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15476 gptab_data_sec->contents,
15477 0, gptab_data_sec->size))
15478 return false;
15481 if (gptab_bss_sec != NULL)
15483 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15484 gptab_bss_sec->contents,
15485 0, gptab_bss_sec->size))
15486 return false;
15489 if (SGI_COMPAT (abfd))
15491 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15492 if (rtproc_sec != NULL)
15494 if (! bfd_set_section_contents (abfd, rtproc_sec,
15495 rtproc_sec->contents,
15496 0, rtproc_sec->size))
15497 return false;
15501 return true;
15504 /* Merge object file header flags from IBFD into OBFD. Raise an error
15505 if there are conflicting settings. */
15507 static bool
15508 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15510 bfd *obfd = info->output_bfd;
15511 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15512 flagword old_flags;
15513 flagword new_flags;
15514 bool ok;
15516 new_flags = elf_elfheader (ibfd)->e_flags;
15517 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15518 old_flags = elf_elfheader (obfd)->e_flags;
15520 /* Check flag compatibility. */
15522 new_flags &= ~EF_MIPS_NOREORDER;
15523 old_flags &= ~EF_MIPS_NOREORDER;
15525 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15526 doesn't seem to matter. */
15527 new_flags &= ~EF_MIPS_XGOT;
15528 old_flags &= ~EF_MIPS_XGOT;
15530 /* MIPSpro generates ucode info in n64 objects. Again, we should
15531 just be able to ignore this. */
15532 new_flags &= ~EF_MIPS_UCODE;
15533 old_flags &= ~EF_MIPS_UCODE;
15535 /* DSOs should only be linked with CPIC code. */
15536 if ((ibfd->flags & DYNAMIC) != 0)
15537 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15539 if (new_flags == old_flags)
15540 return true;
15542 ok = true;
15544 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15545 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15547 _bfd_error_handler
15548 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15549 ibfd);
15550 ok = true;
15553 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15554 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15555 if (! (new_flags & EF_MIPS_PIC))
15556 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15558 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15559 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15561 /* Compare the ISAs. */
15562 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15564 _bfd_error_handler
15565 (_("%pB: linking 32-bit code with 64-bit code"),
15566 ibfd);
15567 ok = false;
15569 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15571 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15572 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15574 /* Copy the architecture info from IBFD to OBFD. Also copy
15575 the 32-bit flag (if set) so that we continue to recognise
15576 OBFD as a 32-bit binary. */
15577 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15578 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15579 elf_elfheader (obfd)->e_flags
15580 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15582 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15583 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15585 /* Copy across the ABI flags if OBFD doesn't use them
15586 and if that was what caused us to treat IBFD as 32-bit. */
15587 if ((old_flags & EF_MIPS_ABI) == 0
15588 && mips_32bit_flags_p (new_flags)
15589 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15590 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15592 else
15594 /* The ISAs aren't compatible. */
15595 _bfd_error_handler
15596 /* xgettext:c-format */
15597 (_("%pB: linking %s module with previous %s modules"),
15598 ibfd,
15599 bfd_printable_name (ibfd),
15600 bfd_printable_name (obfd));
15601 ok = false;
15605 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15606 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15608 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15609 does set EI_CLASS differently from any 32-bit ABI. */
15610 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15611 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15612 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15614 /* Only error if both are set (to different values). */
15615 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15616 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15617 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15619 _bfd_error_handler
15620 /* xgettext:c-format */
15621 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15622 ibfd,
15623 elf_mips_abi_name (ibfd),
15624 elf_mips_abi_name (obfd));
15625 ok = false;
15627 new_flags &= ~EF_MIPS_ABI;
15628 old_flags &= ~EF_MIPS_ABI;
15631 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15632 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15633 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15635 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15636 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15637 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15638 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15639 int micro_mis = old_m16 && new_micro;
15640 int m16_mis = old_micro && new_m16;
15642 if (m16_mis || micro_mis)
15644 _bfd_error_handler
15645 /* xgettext:c-format */
15646 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15647 ibfd,
15648 m16_mis ? "MIPS16" : "microMIPS",
15649 m16_mis ? "microMIPS" : "MIPS16");
15650 ok = false;
15653 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15655 new_flags &= ~ EF_MIPS_ARCH_ASE;
15656 old_flags &= ~ EF_MIPS_ARCH_ASE;
15659 /* Compare NaN encodings. */
15660 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15662 /* xgettext:c-format */
15663 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15664 ibfd,
15665 (new_flags & EF_MIPS_NAN2008
15666 ? "-mnan=2008" : "-mnan=legacy"),
15667 (old_flags & EF_MIPS_NAN2008
15668 ? "-mnan=2008" : "-mnan=legacy"));
15669 ok = false;
15670 new_flags &= ~EF_MIPS_NAN2008;
15671 old_flags &= ~EF_MIPS_NAN2008;
15674 /* Compare FP64 state. */
15675 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15677 /* xgettext:c-format */
15678 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15679 ibfd,
15680 (new_flags & EF_MIPS_FP64
15681 ? "-mfp64" : "-mfp32"),
15682 (old_flags & EF_MIPS_FP64
15683 ? "-mfp64" : "-mfp32"));
15684 ok = false;
15685 new_flags &= ~EF_MIPS_FP64;
15686 old_flags &= ~EF_MIPS_FP64;
15689 /* Warn about any other mismatches */
15690 if (new_flags != old_flags)
15692 /* xgettext:c-format */
15693 _bfd_error_handler
15694 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15695 "(%#x)"),
15696 ibfd, new_flags, old_flags);
15697 ok = false;
15700 return ok;
15703 /* Merge object attributes from IBFD into OBFD. Raise an error if
15704 there are conflicting attributes. */
15705 static bool
15706 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15708 bfd *obfd = info->output_bfd;
15709 obj_attribute *in_attr;
15710 obj_attribute *out_attr;
15711 bfd *abi_fp_bfd;
15712 bfd *abi_msa_bfd;
15714 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15715 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15716 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15717 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15719 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15720 if (!abi_msa_bfd
15721 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15722 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15724 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15726 /* This is the first object. Copy the attributes. */
15727 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15729 /* Use the Tag_null value to indicate the attributes have been
15730 initialized. */
15731 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15733 return true;
15736 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15737 non-conflicting ones. */
15738 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15739 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15741 int out_fp, in_fp;
15743 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15744 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15745 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15746 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15747 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15748 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15749 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15750 || in_fp == Val_GNU_MIPS_ABI_FP_64
15751 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
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_XX
15757 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15758 || out_fp == Val_GNU_MIPS_ABI_FP_64
15759 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15760 /* Keep the current setting. */;
15761 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15762 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15764 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15765 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15767 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15768 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15769 /* Keep the current setting. */;
15770 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15772 const char *out_string, *in_string;
15774 out_string = _bfd_mips_fp_abi_string (out_fp);
15775 in_string = _bfd_mips_fp_abi_string (in_fp);
15776 /* First warn about cases involving unrecognised ABIs. */
15777 if (!out_string && !in_string)
15778 /* xgettext:c-format */
15779 _bfd_error_handler
15780 (_("warning: %pB uses unknown floating point ABI %d "
15781 "(set by %pB), %pB uses unknown floating point ABI %d"),
15782 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15783 else if (!out_string)
15784 _bfd_error_handler
15785 /* xgettext:c-format */
15786 (_("warning: %pB uses unknown floating point ABI %d "
15787 "(set by %pB), %pB uses %s"),
15788 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15789 else if (!in_string)
15790 _bfd_error_handler
15791 /* xgettext:c-format */
15792 (_("warning: %pB uses %s (set by %pB), "
15793 "%pB uses unknown floating point ABI %d"),
15794 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15795 else
15797 /* If one of the bfds is soft-float, the other must be
15798 hard-float. The exact choice of hard-float ABI isn't
15799 really relevant to the error message. */
15800 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15801 out_string = "-mhard-float";
15802 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15803 in_string = "-mhard-float";
15804 _bfd_error_handler
15805 /* xgettext:c-format */
15806 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15807 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15812 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15813 non-conflicting ones. */
15814 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15816 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15817 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15818 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15819 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15820 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15822 case Val_GNU_MIPS_ABI_MSA_128:
15823 _bfd_error_handler
15824 /* xgettext:c-format */
15825 (_("warning: %pB uses %s (set by %pB), "
15826 "%pB uses unknown MSA ABI %d"),
15827 obfd, "-mmsa", abi_msa_bfd,
15828 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15829 break;
15831 default:
15832 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15834 case Val_GNU_MIPS_ABI_MSA_128:
15835 _bfd_error_handler
15836 /* xgettext:c-format */
15837 (_("warning: %pB uses unknown MSA ABI %d "
15838 "(set by %pB), %pB uses %s"),
15839 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15840 abi_msa_bfd, ibfd, "-mmsa");
15841 break;
15843 default:
15844 _bfd_error_handler
15845 /* xgettext:c-format */
15846 (_("warning: %pB uses unknown MSA ABI %d "
15847 "(set by %pB), %pB uses unknown MSA ABI %d"),
15848 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15849 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15850 break;
15855 /* Merge Tag_compatibility attributes and any common GNU ones. */
15856 return _bfd_elf_merge_object_attributes (ibfd, info);
15859 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15860 there are conflicting settings. */
15862 static bool
15863 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15865 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15866 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15867 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15869 /* Update the output abiflags fp_abi using the computed fp_abi. */
15870 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15872 #define max(a, b) ((a) > (b) ? (a) : (b))
15873 /* Merge abiflags. */
15874 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15875 in_tdata->abiflags.isa_level);
15876 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15877 in_tdata->abiflags.isa_rev);
15878 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15879 in_tdata->abiflags.gpr_size);
15880 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15881 in_tdata->abiflags.cpr1_size);
15882 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15883 in_tdata->abiflags.cpr2_size);
15884 #undef max
15885 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15886 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15888 return true;
15891 /* Merge backend specific data from an object file to the output
15892 object file when linking. */
15894 bool
15895 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15897 bfd *obfd = info->output_bfd;
15898 struct mips_elf_obj_tdata *out_tdata;
15899 struct mips_elf_obj_tdata *in_tdata;
15900 bool null_input_bfd = true;
15901 asection *sec;
15902 bool ok;
15904 /* Check if we have the same endianness. */
15905 if (! _bfd_generic_verify_endian_match (ibfd, info))
15907 _bfd_error_handler
15908 (_("%pB: endianness incompatible with that of the selected emulation"),
15909 ibfd);
15910 return false;
15913 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15914 return true;
15916 in_tdata = mips_elf_tdata (ibfd);
15917 out_tdata = mips_elf_tdata (obfd);
15919 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15921 _bfd_error_handler
15922 (_("%pB: ABI is incompatible with that of the selected emulation"),
15923 ibfd);
15924 return false;
15927 /* Check to see if the input BFD actually contains any sections. If not,
15928 then it has no attributes, and its flags may not have been initialized
15929 either, but it cannot actually cause any incompatibility. */
15930 /* FIXME: This excludes any input shared library from consideration. */
15931 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15933 /* Ignore synthetic sections and empty .text, .data and .bss sections
15934 which are automatically generated by gas. Also ignore fake
15935 (s)common sections, since merely defining a common symbol does
15936 not affect compatibility. */
15937 if ((sec->flags & SEC_IS_COMMON) == 0
15938 && strcmp (sec->name, ".reginfo")
15939 && strcmp (sec->name, ".mdebug")
15940 && (sec->size != 0
15941 || (strcmp (sec->name, ".text")
15942 && strcmp (sec->name, ".data")
15943 && strcmp (sec->name, ".bss"))))
15945 null_input_bfd = false;
15946 break;
15949 if (null_input_bfd)
15950 return true;
15952 /* Populate abiflags using existing information. */
15953 if (in_tdata->abiflags_valid)
15955 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15956 Elf_Internal_ABIFlags_v0 in_abiflags;
15957 Elf_Internal_ABIFlags_v0 abiflags;
15959 /* Set up the FP ABI attribute from the abiflags if it is not already
15960 set. */
15961 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15962 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15964 infer_mips_abiflags (ibfd, &abiflags);
15965 in_abiflags = in_tdata->abiflags;
15967 /* It is not possible to infer the correct ISA revision
15968 for R3 or R5 so drop down to R2 for the checks. */
15969 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15970 in_abiflags.isa_rev = 2;
15972 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15973 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15974 _bfd_error_handler
15975 (_("%pB: warning: inconsistent ISA between e_flags and "
15976 ".MIPS.abiflags"), ibfd);
15977 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15978 && in_abiflags.fp_abi != abiflags.fp_abi)
15979 _bfd_error_handler
15980 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15981 ".MIPS.abiflags"), ibfd);
15982 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15983 _bfd_error_handler
15984 (_("%pB: warning: inconsistent ASEs between e_flags and "
15985 ".MIPS.abiflags"), ibfd);
15986 /* The isa_ext is allowed to be an extension of what can be inferred
15987 from e_flags. */
15988 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15989 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15990 _bfd_error_handler
15991 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15992 ".MIPS.abiflags"), ibfd);
15993 if (in_abiflags.flags2 != 0)
15994 _bfd_error_handler
15995 (_("%pB: warning: unexpected flag in the flags2 field of "
15996 ".MIPS.abiflags (0x%lx)"), ibfd,
15997 in_abiflags.flags2);
15999 else
16001 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
16002 in_tdata->abiflags_valid = true;
16005 if (!out_tdata->abiflags_valid)
16007 /* Copy input abiflags if output abiflags are not already valid. */
16008 out_tdata->abiflags = in_tdata->abiflags;
16009 out_tdata->abiflags_valid = true;
16012 if (! elf_flags_init (obfd))
16014 elf_flags_init (obfd) = true;
16015 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16016 elf_elfheader (obfd)->e_ident[EI_CLASS]
16017 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16019 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16020 && (bfd_get_arch_info (obfd)->the_default
16021 || mips_mach_extends_p (bfd_get_mach (obfd),
16022 bfd_get_mach (ibfd))))
16024 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16025 bfd_get_mach (ibfd)))
16026 return false;
16028 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
16029 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16032 ok = true;
16034 else
16035 ok = mips_elf_merge_obj_e_flags (ibfd, info);
16037 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16039 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16041 if (!ok)
16043 bfd_set_error (bfd_error_bad_value);
16044 return false;
16047 return true;
16050 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
16052 bool
16053 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16055 BFD_ASSERT (!elf_flags_init (abfd)
16056 || elf_elfheader (abfd)->e_flags == flags);
16058 elf_elfheader (abfd)->e_flags = flags;
16059 elf_flags_init (abfd) = true;
16060 return true;
16063 char *
16064 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16066 switch (dtag)
16068 default: return "";
16069 case DT_MIPS_RLD_VERSION:
16070 return "MIPS_RLD_VERSION";
16071 case DT_MIPS_TIME_STAMP:
16072 return "MIPS_TIME_STAMP";
16073 case DT_MIPS_ICHECKSUM:
16074 return "MIPS_ICHECKSUM";
16075 case DT_MIPS_IVERSION:
16076 return "MIPS_IVERSION";
16077 case DT_MIPS_FLAGS:
16078 return "MIPS_FLAGS";
16079 case DT_MIPS_BASE_ADDRESS:
16080 return "MIPS_BASE_ADDRESS";
16081 case DT_MIPS_MSYM:
16082 return "MIPS_MSYM";
16083 case DT_MIPS_CONFLICT:
16084 return "MIPS_CONFLICT";
16085 case DT_MIPS_LIBLIST:
16086 return "MIPS_LIBLIST";
16087 case DT_MIPS_LOCAL_GOTNO:
16088 return "MIPS_LOCAL_GOTNO";
16089 case DT_MIPS_CONFLICTNO:
16090 return "MIPS_CONFLICTNO";
16091 case DT_MIPS_LIBLISTNO:
16092 return "MIPS_LIBLISTNO";
16093 case DT_MIPS_SYMTABNO:
16094 return "MIPS_SYMTABNO";
16095 case DT_MIPS_UNREFEXTNO:
16096 return "MIPS_UNREFEXTNO";
16097 case DT_MIPS_GOTSYM:
16098 return "MIPS_GOTSYM";
16099 case DT_MIPS_HIPAGENO:
16100 return "MIPS_HIPAGENO";
16101 case DT_MIPS_RLD_MAP:
16102 return "MIPS_RLD_MAP";
16103 case DT_MIPS_RLD_MAP_REL:
16104 return "MIPS_RLD_MAP_REL";
16105 case DT_MIPS_DELTA_CLASS:
16106 return "MIPS_DELTA_CLASS";
16107 case DT_MIPS_DELTA_CLASS_NO:
16108 return "MIPS_DELTA_CLASS_NO";
16109 case DT_MIPS_DELTA_INSTANCE:
16110 return "MIPS_DELTA_INSTANCE";
16111 case DT_MIPS_DELTA_INSTANCE_NO:
16112 return "MIPS_DELTA_INSTANCE_NO";
16113 case DT_MIPS_DELTA_RELOC:
16114 return "MIPS_DELTA_RELOC";
16115 case DT_MIPS_DELTA_RELOC_NO:
16116 return "MIPS_DELTA_RELOC_NO";
16117 case DT_MIPS_DELTA_SYM:
16118 return "MIPS_DELTA_SYM";
16119 case DT_MIPS_DELTA_SYM_NO:
16120 return "MIPS_DELTA_SYM_NO";
16121 case DT_MIPS_DELTA_CLASSSYM:
16122 return "MIPS_DELTA_CLASSSYM";
16123 case DT_MIPS_DELTA_CLASSSYM_NO:
16124 return "MIPS_DELTA_CLASSSYM_NO";
16125 case DT_MIPS_CXX_FLAGS:
16126 return "MIPS_CXX_FLAGS";
16127 case DT_MIPS_PIXIE_INIT:
16128 return "MIPS_PIXIE_INIT";
16129 case DT_MIPS_SYMBOL_LIB:
16130 return "MIPS_SYMBOL_LIB";
16131 case DT_MIPS_LOCALPAGE_GOTIDX:
16132 return "MIPS_LOCALPAGE_GOTIDX";
16133 case DT_MIPS_LOCAL_GOTIDX:
16134 return "MIPS_LOCAL_GOTIDX";
16135 case DT_MIPS_HIDDEN_GOTIDX:
16136 return "MIPS_HIDDEN_GOTIDX";
16137 case DT_MIPS_PROTECTED_GOTIDX:
16138 return "MIPS_PROTECTED_GOT_IDX";
16139 case DT_MIPS_OPTIONS:
16140 return "MIPS_OPTIONS";
16141 case DT_MIPS_INTERFACE:
16142 return "MIPS_INTERFACE";
16143 case DT_MIPS_DYNSTR_ALIGN:
16144 return "DT_MIPS_DYNSTR_ALIGN";
16145 case DT_MIPS_INTERFACE_SIZE:
16146 return "DT_MIPS_INTERFACE_SIZE";
16147 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16148 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16149 case DT_MIPS_PERF_SUFFIX:
16150 return "DT_MIPS_PERF_SUFFIX";
16151 case DT_MIPS_COMPACT_SIZE:
16152 return "DT_MIPS_COMPACT_SIZE";
16153 case DT_MIPS_GP_VALUE:
16154 return "DT_MIPS_GP_VALUE";
16155 case DT_MIPS_AUX_DYNAMIC:
16156 return "DT_MIPS_AUX_DYNAMIC";
16157 case DT_MIPS_PLTGOT:
16158 return "DT_MIPS_PLTGOT";
16159 case DT_MIPS_RWPLT:
16160 return "DT_MIPS_RWPLT";
16161 case DT_MIPS_XHASH:
16162 return "DT_MIPS_XHASH";
16166 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16167 not known. */
16169 const char *
16170 _bfd_mips_fp_abi_string (int fp)
16172 switch (fp)
16174 /* These strings aren't translated because they're simply
16175 option lists. */
16176 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16177 return "-mdouble-float";
16179 case Val_GNU_MIPS_ABI_FP_SINGLE:
16180 return "-msingle-float";
16182 case Val_GNU_MIPS_ABI_FP_SOFT:
16183 return "-msoft-float";
16185 case Val_GNU_MIPS_ABI_FP_OLD_64:
16186 return _("-mips32r2 -mfp64 (12 callee-saved)");
16188 case Val_GNU_MIPS_ABI_FP_XX:
16189 return "-mfpxx";
16191 case Val_GNU_MIPS_ABI_FP_64:
16192 return "-mgp32 -mfp64";
16194 case Val_GNU_MIPS_ABI_FP_64A:
16195 return "-mgp32 -mfp64 -mno-odd-spreg";
16197 default:
16198 return 0;
16202 static void
16203 print_mips_ases (FILE *file, unsigned int mask)
16205 if (mask & AFL_ASE_DSP)
16206 fputs ("\n\tDSP ASE", file);
16207 if (mask & AFL_ASE_DSPR2)
16208 fputs ("\n\tDSP R2 ASE", file);
16209 if (mask & AFL_ASE_DSPR3)
16210 fputs ("\n\tDSP R3 ASE", file);
16211 if (mask & AFL_ASE_EVA)
16212 fputs ("\n\tEnhanced VA Scheme", file);
16213 if (mask & AFL_ASE_MCU)
16214 fputs ("\n\tMCU (MicroController) ASE", file);
16215 if (mask & AFL_ASE_MDMX)
16216 fputs ("\n\tMDMX ASE", file);
16217 if (mask & AFL_ASE_MIPS3D)
16218 fputs ("\n\tMIPS-3D ASE", file);
16219 if (mask & AFL_ASE_MT)
16220 fputs ("\n\tMT ASE", file);
16221 if (mask & AFL_ASE_SMARTMIPS)
16222 fputs ("\n\tSmartMIPS ASE", file);
16223 if (mask & AFL_ASE_VIRT)
16224 fputs ("\n\tVZ ASE", file);
16225 if (mask & AFL_ASE_MSA)
16226 fputs ("\n\tMSA ASE", file);
16227 if (mask & AFL_ASE_MIPS16)
16228 fputs ("\n\tMIPS16 ASE", file);
16229 if (mask & AFL_ASE_MICROMIPS)
16230 fputs ("\n\tMICROMIPS ASE", file);
16231 if (mask & AFL_ASE_XPA)
16232 fputs ("\n\tXPA ASE", file);
16233 if (mask & AFL_ASE_MIPS16E2)
16234 fputs ("\n\tMIPS16e2 ASE", file);
16235 if (mask & AFL_ASE_CRC)
16236 fputs ("\n\tCRC ASE", file);
16237 if (mask & AFL_ASE_GINV)
16238 fputs ("\n\tGINV ASE", file);
16239 if (mask & AFL_ASE_LOONGSON_MMI)
16240 fputs ("\n\tLoongson MMI ASE", file);
16241 if (mask & AFL_ASE_LOONGSON_CAM)
16242 fputs ("\n\tLoongson CAM ASE", file);
16243 if (mask & AFL_ASE_LOONGSON_EXT)
16244 fputs ("\n\tLoongson EXT ASE", file);
16245 if (mask & AFL_ASE_LOONGSON_EXT2)
16246 fputs ("\n\tLoongson EXT2 ASE", file);
16247 if (mask == 0)
16248 fprintf (file, "\n\t%s", _("None"));
16249 else if ((mask & ~AFL_ASE_MASK) != 0)
16250 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16253 static void
16254 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16256 switch (isa_ext)
16258 case 0:
16259 fputs (_("None"), file);
16260 break;
16261 case AFL_EXT_XLR:
16262 fputs ("RMI XLR", file);
16263 break;
16264 case AFL_EXT_OCTEON3:
16265 fputs ("Cavium Networks Octeon3", file);
16266 break;
16267 case AFL_EXT_OCTEON2:
16268 fputs ("Cavium Networks Octeon2", file);
16269 break;
16270 case AFL_EXT_OCTEONP:
16271 fputs ("Cavium Networks OcteonP", file);
16272 break;
16273 case AFL_EXT_OCTEON:
16274 fputs ("Cavium Networks Octeon", file);
16275 break;
16276 case AFL_EXT_5900:
16277 fputs ("Toshiba R5900", file);
16278 break;
16279 case AFL_EXT_4650:
16280 fputs ("MIPS R4650", file);
16281 break;
16282 case AFL_EXT_4010:
16283 fputs ("LSI R4010", file);
16284 break;
16285 case AFL_EXT_4100:
16286 fputs ("NEC VR4100", file);
16287 break;
16288 case AFL_EXT_3900:
16289 fputs ("Toshiba R3900", file);
16290 break;
16291 case AFL_EXT_10000:
16292 fputs ("MIPS R10000", file);
16293 break;
16294 case AFL_EXT_SB1:
16295 fputs ("Broadcom SB-1", file);
16296 break;
16297 case AFL_EXT_4111:
16298 fputs ("NEC VR4111/VR4181", file);
16299 break;
16300 case AFL_EXT_4120:
16301 fputs ("NEC VR4120", file);
16302 break;
16303 case AFL_EXT_5400:
16304 fputs ("NEC VR5400", file);
16305 break;
16306 case AFL_EXT_5500:
16307 fputs ("NEC VR5500", file);
16308 break;
16309 case AFL_EXT_LOONGSON_2E:
16310 fputs ("ST Microelectronics Loongson 2E", file);
16311 break;
16312 case AFL_EXT_LOONGSON_2F:
16313 fputs ("ST Microelectronics Loongson 2F", file);
16314 break;
16315 case AFL_EXT_INTERAPTIV_MR2:
16316 fputs ("Imagination interAptiv MR2", file);
16317 break;
16318 default:
16319 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16320 break;
16324 static void
16325 print_mips_fp_abi_value (FILE *file, int val)
16327 switch (val)
16329 case Val_GNU_MIPS_ABI_FP_ANY:
16330 fprintf (file, _("Hard or soft float\n"));
16331 break;
16332 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16333 fprintf (file, _("Hard float (double precision)\n"));
16334 break;
16335 case Val_GNU_MIPS_ABI_FP_SINGLE:
16336 fprintf (file, _("Hard float (single precision)\n"));
16337 break;
16338 case Val_GNU_MIPS_ABI_FP_SOFT:
16339 fprintf (file, _("Soft float\n"));
16340 break;
16341 case Val_GNU_MIPS_ABI_FP_OLD_64:
16342 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16343 break;
16344 case Val_GNU_MIPS_ABI_FP_XX:
16345 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16346 break;
16347 case Val_GNU_MIPS_ABI_FP_64:
16348 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16349 break;
16350 case Val_GNU_MIPS_ABI_FP_64A:
16351 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16352 break;
16353 default:
16354 fprintf (file, "??? (%d)\n", val);
16355 break;
16359 static int
16360 get_mips_reg_size (int reg_size)
16362 return (reg_size == AFL_REG_NONE) ? 0
16363 : (reg_size == AFL_REG_32) ? 32
16364 : (reg_size == AFL_REG_64) ? 64
16365 : (reg_size == AFL_REG_128) ? 128
16366 : -1;
16369 bool
16370 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16372 FILE *file = ptr;
16374 BFD_ASSERT (abfd != NULL && ptr != NULL);
16376 /* Print normal ELF private data. */
16377 _bfd_elf_print_private_bfd_data (abfd, ptr);
16379 /* xgettext:c-format */
16380 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16382 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
16383 fprintf (file, _(" [abi=O32]"));
16384 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
16385 fprintf (file, _(" [abi=O64]"));
16386 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
16387 fprintf (file, _(" [abi=EABI32]"));
16388 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
16389 fprintf (file, _(" [abi=EABI64]"));
16390 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16391 fprintf (file, _(" [abi unknown]"));
16392 else if (ABI_N32_P (abfd))
16393 fprintf (file, _(" [abi=N32]"));
16394 else if (ABI_64_P (abfd))
16395 fprintf (file, _(" [abi=64]"));
16396 else
16397 fprintf (file, _(" [no abi set]"));
16399 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
16400 fprintf (file, " [mips1]");
16401 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
16402 fprintf (file, " [mips2]");
16403 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
16404 fprintf (file, " [mips3]");
16405 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
16406 fprintf (file, " [mips4]");
16407 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
16408 fprintf (file, " [mips5]");
16409 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
16410 fprintf (file, " [mips32]");
16411 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
16412 fprintf (file, " [mips64]");
16413 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
16414 fprintf (file, " [mips32r2]");
16415 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
16416 fprintf (file, " [mips64r2]");
16417 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
16418 fprintf (file, " [mips32r6]");
16419 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
16420 fprintf (file, " [mips64r6]");
16421 else
16422 fprintf (file, _(" [unknown ISA]"));
16424 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16425 fprintf (file, " [mdmx]");
16427 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16428 fprintf (file, " [mips16]");
16430 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16431 fprintf (file, " [micromips]");
16433 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16434 fprintf (file, " [nan2008]");
16436 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16437 fprintf (file, " [old fp64]");
16439 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16440 fprintf (file, " [32bitmode]");
16441 else
16442 fprintf (file, _(" [not 32bitmode]"));
16444 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16445 fprintf (file, " [noreorder]");
16447 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16448 fprintf (file, " [PIC]");
16450 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16451 fprintf (file, " [CPIC]");
16453 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16454 fprintf (file, " [XGOT]");
16456 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16457 fprintf (file, " [UCODE]");
16459 fputc ('\n', file);
16461 if (mips_elf_tdata (abfd)->abiflags_valid)
16463 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16464 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16465 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16466 if (abiflags->isa_rev > 1)
16467 fprintf (file, "r%d", abiflags->isa_rev);
16468 fprintf (file, "\nGPR size: %d",
16469 get_mips_reg_size (abiflags->gpr_size));
16470 fprintf (file, "\nCPR1 size: %d",
16471 get_mips_reg_size (abiflags->cpr1_size));
16472 fprintf (file, "\nCPR2 size: %d",
16473 get_mips_reg_size (abiflags->cpr2_size));
16474 fputs ("\nFP ABI: ", file);
16475 print_mips_fp_abi_value (file, abiflags->fp_abi);
16476 fputs ("ISA Extension: ", file);
16477 print_mips_isa_ext (file, abiflags->isa_ext);
16478 fputs ("\nASEs:", file);
16479 print_mips_ases (file, abiflags->ases);
16480 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16481 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16482 fputc ('\n', file);
16485 return true;
16488 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16490 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16491 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16492 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16493 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16494 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16495 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16496 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
16497 { NULL, 0, 0, 0, 0 }
16500 /* Merge non visibility st_other attributes. Ensure that the
16501 STO_OPTIONAL flag is copied into h->other, even if this is not a
16502 definiton of the symbol. */
16503 void
16504 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16505 unsigned int st_other,
16506 bool definition,
16507 bool dynamic ATTRIBUTE_UNUSED)
16509 if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16511 unsigned char other;
16513 other = (definition ? st_other : h->other);
16514 other &= ~ELF_ST_VISIBILITY (-1);
16515 h->other = other | ELF_ST_VISIBILITY (h->other);
16518 if (!definition
16519 && ELF_MIPS_IS_OPTIONAL (st_other))
16520 h->other |= STO_OPTIONAL;
16523 /* Decide whether an undefined symbol is special and can be ignored.
16524 This is the case for OPTIONAL symbols on IRIX. */
16525 bool
16526 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16528 return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16531 bool
16532 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16534 return (sym->st_shndx == SHN_COMMON
16535 || sym->st_shndx == SHN_MIPS_ACOMMON
16536 || sym->st_shndx == SHN_MIPS_SCOMMON);
16539 /* Return address for Ith PLT stub in section PLT, for relocation REL
16540 or (bfd_vma) -1 if it should not be included. */
16542 bfd_vma
16543 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16544 const arelent *rel ATTRIBUTE_UNUSED)
16546 return (plt->vma
16547 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16548 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16551 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16552 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16553 and .got.plt and also the slots may be of a different size each we walk
16554 the PLT manually fetching instructions and matching them against known
16555 patterns. To make things easier standard MIPS slots, if any, always come
16556 first. As we don't create proper ELF symbols we use the UDATA.I member
16557 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16558 with the ST_OTHER member of the ELF symbol. */
16560 long
16561 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16562 long symcount ATTRIBUTE_UNUSED,
16563 asymbol **syms ATTRIBUTE_UNUSED,
16564 long dynsymcount, asymbol **dynsyms,
16565 asymbol **ret)
16567 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16568 static const char microsuffix[] = "@micromipsplt";
16569 static const char m16suffix[] = "@mips16plt";
16570 static const char mipssuffix[] = "@plt";
16572 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16573 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16574 bool micromips_p = MICROMIPS_P (abfd);
16575 Elf_Internal_Shdr *hdr;
16576 bfd_byte *plt_data;
16577 bfd_vma plt_offset;
16578 unsigned int other;
16579 bfd_vma entry_size;
16580 bfd_vma plt0_size;
16581 asection *relplt;
16582 bfd_vma opcode;
16583 asection *plt;
16584 asymbol *send;
16585 size_t size;
16586 char *names;
16587 long counti;
16588 arelent *p;
16589 asymbol *s;
16590 char *nend;
16591 long count;
16592 long pi;
16593 long i;
16594 long n;
16596 *ret = NULL;
16598 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16599 return 0;
16601 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16602 if (relplt == NULL)
16603 return 0;
16605 hdr = &elf_section_data (relplt)->this_hdr;
16606 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16607 return 0;
16609 plt = bfd_get_section_by_name (abfd, ".plt");
16610 if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16611 return 0;
16613 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16614 if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16615 return -1;
16616 p = relplt->relocation;
16618 /* Calculating the exact amount of space required for symbols would
16619 require two passes over the PLT, so just pessimise assuming two
16620 PLT slots per relocation. */
16621 count = NUM_SHDR_ENTRIES (hdr);
16622 counti = count * bed->s->int_rels_per_ext_rel;
16623 size = 2 * count * sizeof (asymbol);
16624 size += count * (sizeof (mipssuffix) +
16625 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16626 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16627 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16629 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16630 size += sizeof (asymbol) + sizeof (pltname);
16632 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16633 return -1;
16635 if (plt->size < 16)
16636 return -1;
16638 s = *ret = bfd_malloc (size);
16639 if (s == NULL)
16640 return -1;
16641 send = s + 2 * count + 1;
16643 names = (char *) send;
16644 nend = (char *) s + size;
16645 n = 0;
16647 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16648 if (opcode == 0x3302fffe)
16650 if (!micromips_p)
16651 return -1;
16652 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16653 other = STO_MICROMIPS;
16655 else if (opcode == 0x0398c1d0)
16657 if (!micromips_p)
16658 return -1;
16659 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16660 other = STO_MICROMIPS;
16662 else
16664 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16665 other = 0;
16668 s->the_bfd = abfd;
16669 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16670 s->section = plt;
16671 s->value = 0;
16672 s->name = names;
16673 s->udata.i = other;
16674 memcpy (names, pltname, sizeof (pltname));
16675 names += sizeof (pltname);
16676 ++s, ++n;
16678 pi = 0;
16679 for (plt_offset = plt0_size;
16680 plt_offset + 8 <= plt->size && s < send;
16681 plt_offset += entry_size)
16683 bfd_vma gotplt_addr;
16684 const char *suffix;
16685 bfd_vma gotplt_hi;
16686 bfd_vma gotplt_lo;
16687 size_t suffixlen;
16689 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16691 /* Check if the second word matches the expected MIPS16 instruction. */
16692 if (opcode == 0x651aeb00)
16694 if (micromips_p)
16695 return -1;
16696 /* Truncated table??? */
16697 if (plt_offset + 16 > plt->size)
16698 break;
16699 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16700 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16701 suffixlen = sizeof (m16suffix);
16702 suffix = m16suffix;
16703 other = STO_MIPS16;
16705 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16706 else if (opcode == 0xff220000)
16708 if (!micromips_p)
16709 return -1;
16710 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16711 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16712 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16713 gotplt_lo <<= 2;
16714 gotplt_addr = gotplt_hi + gotplt_lo;
16715 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16716 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16717 suffixlen = sizeof (microsuffix);
16718 suffix = microsuffix;
16719 other = STO_MICROMIPS;
16721 /* Likewise the expected microMIPS instruction (insn32 mode). */
16722 else if ((opcode & 0xffff0000) == 0xff2f0000)
16724 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16725 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16726 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16727 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16728 gotplt_addr = gotplt_hi + gotplt_lo;
16729 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16730 suffixlen = sizeof (microsuffix);
16731 suffix = microsuffix;
16732 other = STO_MICROMIPS;
16734 /* Otherwise assume standard MIPS code. */
16735 else
16737 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16738 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16739 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16740 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16741 gotplt_addr = gotplt_hi + gotplt_lo;
16742 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16743 suffixlen = sizeof (mipssuffix);
16744 suffix = mipssuffix;
16745 other = 0;
16747 /* Truncated table??? */
16748 if (plt_offset + entry_size > plt->size)
16749 break;
16751 for (i = 0;
16752 i < count && p[pi].address != gotplt_addr;
16753 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16755 if (i < count)
16757 size_t namelen;
16758 size_t len;
16760 *s = **p[pi].sym_ptr_ptr;
16761 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16762 we are defining a symbol, ensure one of them is set. */
16763 if ((s->flags & BSF_LOCAL) == 0)
16764 s->flags |= BSF_GLOBAL;
16765 s->flags |= BSF_SYNTHETIC;
16766 s->section = plt;
16767 s->value = plt_offset;
16768 s->name = names;
16769 s->udata.i = other;
16771 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16772 namelen = len + suffixlen;
16773 if (names + namelen > nend)
16774 break;
16776 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16777 names += len;
16778 memcpy (names, suffix, suffixlen);
16779 names += suffixlen;
16781 ++s, ++n;
16782 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16786 free (plt_data);
16788 return n;
16791 /* Return the ABI flags associated with ABFD if available. */
16793 Elf_Internal_ABIFlags_v0 *
16794 bfd_mips_elf_get_abiflags (bfd *abfd)
16796 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16798 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16801 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16802 field. Taken from `libc-abis.h' generated at GNU libc build time.
16803 Using a MIPS_ prefix as other libc targets use different values. */
16804 enum
16806 MIPS_LIBC_ABI_DEFAULT = 0,
16807 MIPS_LIBC_ABI_MIPS_PLT,
16808 MIPS_LIBC_ABI_UNIQUE,
16809 MIPS_LIBC_ABI_MIPS_O32_FP64,
16810 MIPS_LIBC_ABI_ABSOLUTE,
16811 MIPS_LIBC_ABI_XHASH,
16812 MIPS_LIBC_ABI_MAX
16815 bool
16816 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16818 struct mips_elf_link_hash_table *htab = NULL;
16819 Elf_Internal_Ehdr *i_ehdrp;
16821 if (!_bfd_elf_init_file_header (abfd, link_info))
16822 return false;
16824 i_ehdrp = elf_elfheader (abfd);
16825 if (link_info)
16827 htab = mips_elf_hash_table (link_info);
16828 BFD_ASSERT (htab != NULL);
16831 if (htab != NULL
16832 && htab->use_plts_and_copy_relocs
16833 && htab->root.target_os != is_vxworks)
16834 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16836 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16837 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16838 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16840 /* Mark that we need support for absolute symbols in the dynamic loader. */
16841 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16842 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16844 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16845 if it is the only hash section that will be created. */
16846 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16847 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16848 return true;
16852 _bfd_mips_elf_compact_eh_encoding
16853 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16855 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16858 /* Return the opcode for can't unwind. */
16861 _bfd_mips_elf_cant_unwind_opcode
16862 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16864 return COMPACT_EH_CANT_UNWIND_OPCODE;
16867 /* Record a position XLAT_LOC in the xlat translation table, associated with
16868 the hash entry H. The entry in the translation table will later be
16869 populated with the real symbol dynindx. */
16871 void
16872 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16873 bfd_vma xlat_loc)
16875 struct mips_elf_link_hash_entry *hmips;
16877 hmips = (struct mips_elf_link_hash_entry *) h;
16878 hmips->mipsxhash_loc = xlat_loc;