1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
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. */
34 #include "libiberty.h"
36 #include "elfxx-mips.h"
38 #include "elf-vxworks.h"
40 /* Get the ECOFF swapping routines. */
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "coff/mips.h"
48 /* Types of TLS GOT entry. */
49 enum mips_got_tls_type {
56 /* This structure is used to hold information about one GOT entry.
57 There are four types of entry:
59 (1) an absolute address
60 requires: abfd == NULL
63 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
64 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
65 fields: abfd, symndx, d.addend, tls_type
67 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
68 requires: abfd != NULL, symndx == -1
72 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
73 fields: none; there's only one of these per GOT. */
76 /* One input bfd that needs the GOT entry. */
78 /* The index of the symbol, as stored in the relocation r_info, if
79 we have a local symbol; -1 otherwise. */
83 /* If abfd == NULL, an address that must be stored in the got. */
85 /* If abfd != NULL && symndx != -1, the addend of the relocation
86 that should be added to the symbol value. */
88 /* If abfd != NULL && symndx == -1, the hash table entry
89 corresponding to a symbol in the GOT. The symbol's entry
90 is in the local area if h->global_got_area is GGA_NONE,
91 otherwise it is in the global area. */
92 struct mips_elf_link_hash_entry *h;
95 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
96 symbol entry with r_symndx == 0. */
97 unsigned char tls_type;
99 /* True if we have filled in the GOT contents for a TLS entry,
100 and created the associated relocations. */
101 unsigned char tls_initialized;
103 /* The offset from the beginning of the .got section to the entry
104 corresponding to this symbol+addend. If it's a global symbol
105 whose offset is yet to be decided, it's going to be -1. */
109 /* This structure represents a GOT page reference from an input bfd.
110 Each instance represents a symbol + ADDEND, where the representation
111 of the symbol depends on whether it is local to the input bfd.
112 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
113 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
115 Page references with SYMNDX >= 0 always become page references
116 in the output. Page references with SYMNDX < 0 only become page
117 references if the symbol binds locally; in other cases, the page
118 reference decays to a global GOT reference. */
119 struct mips_got_page_ref
124 struct mips_elf_link_hash_entry *h;
130 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
131 The structures form a non-overlapping list that is sorted by increasing
133 struct mips_got_page_range
135 struct mips_got_page_range *next;
136 bfd_signed_vma min_addend;
137 bfd_signed_vma max_addend;
140 /* This structure describes the range of addends that are applied to page
141 relocations against a given section. */
142 struct mips_got_page_entry
144 /* The section that these entries are based on. */
146 /* The ranges for this page entry. */
147 struct mips_got_page_range *ranges;
148 /* The maximum number of page entries needed for RANGES. */
152 /* This structure is used to hold .got information when linking. */
156 /* The number of global .got entries. */
157 unsigned int global_gotno;
158 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
159 unsigned int reloc_only_gotno;
160 /* The number of .got slots used for TLS. */
161 unsigned int tls_gotno;
162 /* The first unused TLS .got entry. Used only during
163 mips_elf_initialize_tls_index. */
164 unsigned int tls_assigned_gotno;
165 /* The number of local .got entries, eventually including page entries. */
166 unsigned int local_gotno;
167 /* The maximum number of page entries needed. */
168 unsigned int page_gotno;
169 /* The number of relocations needed for the GOT entries. */
171 /* The first unused local .got entry. */
172 unsigned int assigned_low_gotno;
173 /* The last unused local .got entry. */
174 unsigned int assigned_high_gotno;
175 /* A hash table holding members of the got. */
176 struct htab *got_entries;
177 /* A hash table holding mips_got_page_ref structures. */
178 struct htab *got_page_refs;
179 /* A hash table of mips_got_page_entry structures. */
180 struct htab *got_page_entries;
181 /* In multi-got links, a pointer to the next got (err, rather, most
182 of the time, it points to the previous got). */
183 struct mips_got_info *next;
186 /* Structure passed when merging bfds' gots. */
188 struct mips_elf_got_per_bfd_arg
190 /* The output bfd. */
192 /* The link information. */
193 struct bfd_link_info *info;
194 /* A pointer to the primary got, i.e., the one that's going to get
195 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
197 struct mips_got_info *primary;
198 /* A non-primary got we're trying to merge with other input bfd's
200 struct mips_got_info *current;
201 /* The maximum number of got entries that can be addressed with a
203 unsigned int max_count;
204 /* The maximum number of page entries needed by each got. */
205 unsigned int max_pages;
206 /* The total number of global entries which will live in the
207 primary got and be automatically relocated. This includes
208 those not referenced by the primary GOT but included in
210 unsigned int global_count;
213 /* A structure used to pass information to htab_traverse callbacks
214 when laying out the GOT. */
216 struct mips_elf_traverse_got_arg
218 struct bfd_link_info *info;
219 struct mips_got_info *g;
223 struct _mips_elf_section_data
225 struct bfd_elf_section_data elf;
232 #define mips_elf_section_data(sec) \
233 ((struct _mips_elf_section_data *) elf_section_data (sec))
235 #define is_mips_elf(bfd) \
236 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
237 && elf_tdata (bfd) != NULL \
238 && elf_object_id (bfd) == MIPS_ELF_DATA)
240 /* The ABI says that every symbol used by dynamic relocations must have
241 a global GOT entry. Among other things, this provides the dynamic
242 linker with a free, directly-indexed cache. The GOT can therefore
243 contain symbols that are not referenced by GOT relocations themselves
244 (in other words, it may have symbols that are not referenced by things
245 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
247 GOT relocations are less likely to overflow if we put the associated
248 GOT entries towards the beginning. We therefore divide the global
249 GOT entries into two areas: "normal" and "reloc-only". Entries in
250 the first area can be used for both dynamic relocations and GP-relative
251 accesses, while those in the "reloc-only" area are for dynamic
254 These GGA_* ("Global GOT Area") values are organised so that lower
255 values are more general than higher values. Also, non-GGA_NONE
256 values are ordered by the position of the area in the GOT. */
258 #define GGA_RELOC_ONLY 1
261 /* Information about a non-PIC interface to a PIC function. There are
262 two ways of creating these interfaces. The first is to add:
265 addiu $25,$25,%lo(func)
267 immediately before a PIC function "func". The second is to add:
271 addiu $25,$25,%lo(func)
273 to a separate trampoline section.
275 Stubs of the first kind go in a new section immediately before the
276 target function. Stubs of the second kind go in a single section
277 pointed to by the hash table's "strampoline" field. */
278 struct mips_elf_la25_stub {
279 /* The generated section that contains this stub. */
280 asection *stub_section;
282 /* The offset of the stub from the start of STUB_SECTION. */
285 /* One symbol for the original function. Its location is available
286 in H->root.root.u.def. */
287 struct mips_elf_link_hash_entry *h;
290 /* Macros for populating a mips_elf_la25_stub. */
292 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
293 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
294 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
295 #define LA25_LUI_MICROMIPS(VAL) \
296 (0x41b90000 | (VAL)) /* lui t9,VAL */
297 #define LA25_J_MICROMIPS(VAL) \
298 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
299 #define LA25_ADDIU_MICROMIPS(VAL) \
300 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
302 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
303 the dynamic symbols. */
305 struct mips_elf_hash_sort_data
307 /* The symbol in the global GOT with the lowest dynamic symbol table
309 struct elf_link_hash_entry *low;
310 /* The least dynamic symbol table index corresponding to a non-TLS
311 symbol with a GOT entry. */
312 long min_got_dynindx;
313 /* The greatest dynamic symbol table index corresponding to a symbol
314 with a GOT entry that is not referenced (e.g., a dynamic symbol
315 with dynamic relocations pointing to it from non-primary GOTs). */
316 long max_unref_got_dynindx;
317 /* The greatest dynamic symbol table index not corresponding to a
318 symbol without a GOT entry. */
319 long max_non_got_dynindx;
322 /* We make up to two PLT entries if needed, one for standard MIPS code
323 and one for compressed code, either a MIPS16 or microMIPS one. We
324 keep a separate record of traditional lazy-binding stubs, for easier
329 /* Traditional SVR4 stub offset, or -1 if none. */
332 /* Standard PLT entry offset, or -1 if none. */
335 /* Compressed PLT entry offset, or -1 if none. */
338 /* The corresponding .got.plt index, or -1 if none. */
339 bfd_vma gotplt_index;
341 /* Whether we need a standard PLT entry. */
342 unsigned int need_mips : 1;
344 /* Whether we need a compressed PLT entry. */
345 unsigned int need_comp : 1;
348 /* The MIPS ELF linker needs additional information for each symbol in
349 the global hash table. */
351 struct mips_elf_link_hash_entry
353 struct elf_link_hash_entry root;
355 /* External symbol information. */
358 /* The la25 stub we have created for ths symbol, if any. */
359 struct mips_elf_la25_stub *la25_stub;
361 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
363 unsigned int possibly_dynamic_relocs;
365 /* If there is a stub that 32 bit functions should use to call this
366 16 bit function, this points to the section containing the stub. */
369 /* If there is a stub that 16 bit functions should use to call this
370 32 bit function, this points to the section containing the stub. */
373 /* This is like the call_stub field, but it is used if the function
374 being called returns a floating point value. */
375 asection *call_fp_stub;
377 /* The highest GGA_* value that satisfies all references to this symbol. */
378 unsigned int global_got_area : 2;
380 /* True if all GOT relocations against this symbol are for calls. This is
381 a looser condition than no_fn_stub below, because there may be other
382 non-call non-GOT relocations against the symbol. */
383 unsigned int got_only_for_calls : 1;
385 /* True if one of the relocations described by possibly_dynamic_relocs
386 is against a readonly section. */
387 unsigned int readonly_reloc : 1;
389 /* True if there is a relocation against this symbol that must be
390 resolved by the static linker (in other words, if the relocation
391 cannot possibly be made dynamic). */
392 unsigned int has_static_relocs : 1;
394 /* True if we must not create a .MIPS.stubs entry for this symbol.
395 This is set, for example, if there are relocations related to
396 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
397 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
398 unsigned int no_fn_stub : 1;
400 /* Whether we need the fn_stub; this is true if this symbol appears
401 in any relocs other than a 16 bit call. */
402 unsigned int need_fn_stub : 1;
404 /* True if this symbol is referenced by branch relocations from
405 any non-PIC input file. This is used to determine whether an
406 la25 stub is required. */
407 unsigned int has_nonpic_branches : 1;
409 /* Does this symbol need a traditional MIPS lazy-binding stub
410 (as opposed to a PLT entry)? */
411 unsigned int needs_lazy_stub : 1;
413 /* Does this symbol resolve to a PLT entry? */
414 unsigned int use_plt_entry : 1;
417 /* MIPS ELF linker hash table. */
419 struct mips_elf_link_hash_table
421 struct elf_link_hash_table root;
423 /* The number of .rtproc entries. */
424 bfd_size_type procedure_count;
426 /* The size of the .compact_rel section (if SGI_COMPAT). */
427 bfd_size_type compact_rel_size;
429 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
430 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
431 bfd_boolean use_rld_obj_head;
433 /* The __rld_map or __rld_obj_head symbol. */
434 struct elf_link_hash_entry *rld_symbol;
436 /* This is set if we see any mips16 stub sections. */
437 bfd_boolean mips16_stubs_seen;
439 /* True if we can generate copy relocs and PLTs. */
440 bfd_boolean use_plts_and_copy_relocs;
442 /* True if we can only use 32-bit microMIPS instructions. */
445 /* True if we're generating code for VxWorks. */
446 bfd_boolean is_vxworks;
448 /* True if we already reported the small-data section overflow. */
449 bfd_boolean small_data_overflow_reported;
451 /* Shortcuts to some dynamic sections, or NULL if they are not
462 /* The master GOT information. */
463 struct mips_got_info *got_info;
465 /* The global symbol in the GOT with the lowest index in the dynamic
467 struct elf_link_hash_entry *global_gotsym;
469 /* The size of the PLT header in bytes. */
470 bfd_vma plt_header_size;
472 /* The size of a standard PLT entry in bytes. */
473 bfd_vma plt_mips_entry_size;
475 /* The size of a compressed PLT entry in bytes. */
476 bfd_vma plt_comp_entry_size;
478 /* The offset of the next standard PLT entry to create. */
479 bfd_vma plt_mips_offset;
481 /* The offset of the next compressed PLT entry to create. */
482 bfd_vma plt_comp_offset;
484 /* The index of the next .got.plt entry to create. */
485 bfd_vma plt_got_index;
487 /* The number of functions that need a lazy-binding stub. */
488 bfd_vma lazy_stub_count;
490 /* The size of a function stub entry in bytes. */
491 bfd_vma function_stub_size;
493 /* The number of reserved entries at the beginning of the GOT. */
494 unsigned int reserved_gotno;
496 /* The section used for mips_elf_la25_stub trampolines.
497 See the comment above that structure for details. */
498 asection *strampoline;
500 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
504 /* A function FN (NAME, IS, OS) that creates a new input section
505 called NAME and links it to output section OS. If IS is nonnull,
506 the new section should go immediately before it, otherwise it
507 should go at the (current) beginning of OS.
509 The function returns the new section on success, otherwise it
511 asection *(*add_stub_section) (const char *, asection *, asection *);
513 /* Small local sym cache. */
514 struct sym_cache sym_cache;
516 /* Is the PLT header compressed? */
517 unsigned int plt_header_is_comp : 1;
520 /* Get the MIPS ELF linker hash table from a link_info structure. */
522 #define mips_elf_hash_table(p) \
523 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
524 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
526 /* A structure used to communicate with htab_traverse callbacks. */
527 struct mips_htab_traverse_info
529 /* The usual link-wide information. */
530 struct bfd_link_info *info;
533 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
537 /* MIPS ELF private object data. */
539 struct mips_elf_obj_tdata
541 /* Generic ELF private object data. */
542 struct elf_obj_tdata root;
544 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
547 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
550 /* The abiflags for this object. */
551 Elf_Internal_ABIFlags_v0 abiflags;
552 bfd_boolean abiflags_valid;
554 /* The GOT requirements of input bfds. */
555 struct mips_got_info *got;
557 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
558 included directly in this one, but there's no point to wasting
559 the memory just for the infrequently called find_nearest_line. */
560 struct mips_elf_find_line *find_line_info;
562 /* An array of stub sections indexed by symbol number. */
563 asection **local_stubs;
564 asection **local_call_stubs;
566 /* The Irix 5 support uses two virtual sections, which represent
567 text/data symbols defined in dynamic objects. */
568 asymbol *elf_data_symbol;
569 asymbol *elf_text_symbol;
570 asection *elf_data_section;
571 asection *elf_text_section;
574 /* Get MIPS ELF private object data from BFD's tdata. */
576 #define mips_elf_tdata(bfd) \
577 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
579 #define TLS_RELOC_P(r_type) \
580 (r_type == R_MIPS_TLS_DTPMOD32 \
581 || r_type == R_MIPS_TLS_DTPMOD64 \
582 || r_type == R_MIPS_TLS_DTPREL32 \
583 || r_type == R_MIPS_TLS_DTPREL64 \
584 || r_type == R_MIPS_TLS_GD \
585 || r_type == R_MIPS_TLS_LDM \
586 || r_type == R_MIPS_TLS_DTPREL_HI16 \
587 || r_type == R_MIPS_TLS_DTPREL_LO16 \
588 || r_type == R_MIPS_TLS_GOTTPREL \
589 || r_type == R_MIPS_TLS_TPREL32 \
590 || r_type == R_MIPS_TLS_TPREL64 \
591 || r_type == R_MIPS_TLS_TPREL_HI16 \
592 || r_type == R_MIPS_TLS_TPREL_LO16 \
593 || r_type == R_MIPS16_TLS_GD \
594 || r_type == R_MIPS16_TLS_LDM \
595 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
596 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
597 || r_type == R_MIPS16_TLS_GOTTPREL \
598 || r_type == R_MIPS16_TLS_TPREL_HI16 \
599 || r_type == R_MIPS16_TLS_TPREL_LO16 \
600 || r_type == R_MICROMIPS_TLS_GD \
601 || r_type == R_MICROMIPS_TLS_LDM \
602 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
603 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
604 || r_type == R_MICROMIPS_TLS_GOTTPREL \
605 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
606 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
608 /* Structure used to pass information to mips_elf_output_extsym. */
613 struct bfd_link_info *info;
614 struct ecoff_debug_info *debug;
615 const struct ecoff_debug_swap *swap;
619 /* The names of the runtime procedure table symbols used on IRIX5. */
621 static const char * const mips_elf_dynsym_rtproc_names[] =
624 "_procedure_string_table",
625 "_procedure_table_size",
629 /* These structures are used to generate the .compact_rel section on
634 unsigned long id1; /* Always one? */
635 unsigned long num; /* Number of compact relocation entries. */
636 unsigned long id2; /* Always two? */
637 unsigned long offset; /* The file offset of the first relocation. */
638 unsigned long reserved0; /* Zero? */
639 unsigned long reserved1; /* Zero? */
648 bfd_byte reserved0[4];
649 bfd_byte reserved1[4];
650 } Elf32_External_compact_rel;
654 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
655 unsigned int rtype : 4; /* Relocation types. See below. */
656 unsigned int dist2to : 8;
657 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
658 unsigned long konst; /* KONST field. See below. */
659 unsigned long vaddr; /* VADDR to be relocated. */
664 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
665 unsigned int rtype : 4; /* Relocation types. See below. */
666 unsigned int dist2to : 8;
667 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
668 unsigned long konst; /* KONST field. See below. */
676 } Elf32_External_crinfo;
682 } Elf32_External_crinfo2;
684 /* These are the constants used to swap the bitfields in a crinfo. */
686 #define CRINFO_CTYPE (0x1)
687 #define CRINFO_CTYPE_SH (31)
688 #define CRINFO_RTYPE (0xf)
689 #define CRINFO_RTYPE_SH (27)
690 #define CRINFO_DIST2TO (0xff)
691 #define CRINFO_DIST2TO_SH (19)
692 #define CRINFO_RELVADDR (0x7ffff)
693 #define CRINFO_RELVADDR_SH (0)
695 /* A compact relocation info has long (3 words) or short (2 words)
696 formats. A short format doesn't have VADDR field and relvaddr
697 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
698 #define CRF_MIPS_LONG 1
699 #define CRF_MIPS_SHORT 0
701 /* There are 4 types of compact relocation at least. The value KONST
702 has different meaning for each type:
705 CT_MIPS_REL32 Address in data
706 CT_MIPS_WORD Address in word (XXX)
707 CT_MIPS_GPHI_LO GP - vaddr
708 CT_MIPS_JMPAD Address to jump
711 #define CRT_MIPS_REL32 0xa
712 #define CRT_MIPS_WORD 0xb
713 #define CRT_MIPS_GPHI_LO 0xc
714 #define CRT_MIPS_JMPAD 0xd
716 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
717 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
718 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
719 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
721 /* The structure of the runtime procedure descriptor created by the
722 loader for use by the static exception system. */
724 typedef struct runtime_pdr {
725 bfd_vma adr; /* Memory address of start of procedure. */
726 long regmask; /* Save register mask. */
727 long regoffset; /* Save register offset. */
728 long fregmask; /* Save floating point register mask. */
729 long fregoffset; /* Save floating point register offset. */
730 long frameoffset; /* Frame size. */
731 short framereg; /* Frame pointer register. */
732 short pcreg; /* Offset or reg of return pc. */
733 long irpss; /* Index into the runtime string table. */
735 struct exception_info *exception_info;/* Pointer to exception array. */
737 #define cbRPDR sizeof (RPDR)
738 #define rpdNil ((pRPDR) 0)
740 static struct mips_got_entry *mips_elf_create_local_got_entry
741 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
742 struct mips_elf_link_hash_entry *, int);
743 static bfd_boolean mips_elf_sort_hash_table_f
744 (struct mips_elf_link_hash_entry *, void *);
745 static bfd_vma mips_elf_high
747 static bfd_boolean mips_elf_create_dynamic_relocation
748 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
749 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
750 bfd_vma *, asection *);
751 static bfd_vma mips_elf_adjust_gp
752 (bfd *, struct mips_got_info *, bfd *);
754 /* This will be used when we sort the dynamic relocation records. */
755 static bfd *reldyn_sorting_bfd;
757 /* True if ABFD is for CPUs with load interlocking that include
758 non-MIPS1 CPUs and R3900. */
759 #define LOAD_INTERLOCKS_P(abfd) \
760 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
761 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
763 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
764 This should be safe for all architectures. We enable this predicate
765 for RM9000 for now. */
766 #define JAL_TO_BAL_P(abfd) \
767 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
769 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
770 This should be safe for all architectures. We enable this predicate for
772 #define JALR_TO_BAL_P(abfd) 1
774 /* True if ABFD is for CPUs that are faster if JR is converted to B.
775 This should be safe for all architectures. We enable this predicate for
777 #define JR_TO_B_P(abfd) 1
779 /* True if ABFD is a PIC object. */
780 #define PIC_OBJECT_P(abfd) \
781 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
783 /* Nonzero if ABFD is using the O32 ABI. */
784 #define ABI_O32_P(abfd) \
785 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
787 /* Nonzero if ABFD is using the N32 ABI. */
788 #define ABI_N32_P(abfd) \
789 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
791 /* Nonzero if ABFD is using the N64 ABI. */
792 #define ABI_64_P(abfd) \
793 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
795 /* Nonzero if ABFD is using NewABI conventions. */
796 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
798 /* Nonzero if ABFD has microMIPS code. */
799 #define MICROMIPS_P(abfd) \
800 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
802 /* The IRIX compatibility level we are striving for. */
803 #define IRIX_COMPAT(abfd) \
804 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
806 /* Whether we are trying to be compatible with IRIX at all. */
807 #define SGI_COMPAT(abfd) \
808 (IRIX_COMPAT (abfd) != ict_none)
810 /* The name of the options section. */
811 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
812 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
814 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
815 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
816 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
817 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
819 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
820 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
821 (strcmp (NAME, ".MIPS.abiflags") == 0)
823 /* Whether the section is readonly. */
824 #define MIPS_ELF_READONLY_SECTION(sec) \
825 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
826 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
828 /* The name of the stub section. */
829 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
831 /* The size of an external REL relocation. */
832 #define MIPS_ELF_REL_SIZE(abfd) \
833 (get_elf_backend_data (abfd)->s->sizeof_rel)
835 /* The size of an external RELA relocation. */
836 #define MIPS_ELF_RELA_SIZE(abfd) \
837 (get_elf_backend_data (abfd)->s->sizeof_rela)
839 /* The size of an external dynamic table entry. */
840 #define MIPS_ELF_DYN_SIZE(abfd) \
841 (get_elf_backend_data (abfd)->s->sizeof_dyn)
843 /* The size of a GOT entry. */
844 #define MIPS_ELF_GOT_SIZE(abfd) \
845 (get_elf_backend_data (abfd)->s->arch_size / 8)
847 /* The size of the .rld_map section. */
848 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
849 (get_elf_backend_data (abfd)->s->arch_size / 8)
851 /* The size of a symbol-table entry. */
852 #define MIPS_ELF_SYM_SIZE(abfd) \
853 (get_elf_backend_data (abfd)->s->sizeof_sym)
855 /* The default alignment for sections, as a power of two. */
856 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
857 (get_elf_backend_data (abfd)->s->log_file_align)
859 /* Get word-sized data. */
860 #define MIPS_ELF_GET_WORD(abfd, ptr) \
861 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
863 /* Put out word-sized data. */
864 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
866 ? bfd_put_64 (abfd, val, ptr) \
867 : bfd_put_32 (abfd, val, ptr))
869 /* The opcode for word-sized loads (LW or LD). */
870 #define MIPS_ELF_LOAD_WORD(abfd) \
871 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
873 /* Add a dynamic symbol table-entry. */
874 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
875 _bfd_elf_add_dynamic_entry (info, tag, val)
877 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
878 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
880 /* The name of the dynamic relocation section. */
881 #define MIPS_ELF_REL_DYN_NAME(INFO) \
882 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
884 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
885 from smaller values. Start with zero, widen, *then* decrement. */
886 #define MINUS_ONE (((bfd_vma)0) - 1)
887 #define MINUS_TWO (((bfd_vma)0) - 2)
889 /* The value to write into got[1] for SVR4 targets, to identify it is
890 a GNU object. The dynamic linker can then use got[1] to store the
892 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
893 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
895 /* The offset of $gp from the beginning of the .got section. */
896 #define ELF_MIPS_GP_OFFSET(INFO) \
897 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
899 /* The maximum size of the GOT for it to be addressable using 16-bit
901 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
903 /* Instructions which appear in a stub. */
904 #define STUB_LW(abfd) \
906 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
907 : 0x8f998010)) /* lw t9,0x8010(gp) */
908 #define STUB_MOVE(abfd) \
910 ? 0x03e0782d /* daddu t7,ra */ \
911 : 0x03e07821)) /* addu t7,ra */
912 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
913 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
914 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
915 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
916 #define STUB_LI16S(abfd, VAL) \
918 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
919 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
921 /* Likewise for the microMIPS ASE. */
922 #define STUB_LW_MICROMIPS(abfd) \
924 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
925 : 0xff3c8010) /* lw t9,0x8010(gp) */
926 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
927 #define STUB_MOVE32_MICROMIPS(abfd) \
929 ? 0x581f7950 /* daddu t7,ra,zero */ \
930 : 0x001f7950) /* addu t7,ra,zero */
931 #define STUB_LUI_MICROMIPS(VAL) \
932 (0x41b80000 + (VAL)) /* lui t8,VAL */
933 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
934 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
935 #define STUB_ORI_MICROMIPS(VAL) \
936 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
937 #define STUB_LI16U_MICROMIPS(VAL) \
938 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
939 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
941 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
942 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
944 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
945 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
946 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
947 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
948 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
949 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
951 /* The name of the dynamic interpreter. This is put in the .interp
954 #define ELF_DYNAMIC_INTERPRETER(abfd) \
955 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
956 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
957 : "/usr/lib/libc.so.1")
960 #define MNAME(bfd,pre,pos) \
961 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
962 #define ELF_R_SYM(bfd, i) \
963 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
964 #define ELF_R_TYPE(bfd, i) \
965 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
966 #define ELF_R_INFO(bfd, s, t) \
967 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
969 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
970 #define ELF_R_SYM(bfd, i) \
972 #define ELF_R_TYPE(bfd, i) \
974 #define ELF_R_INFO(bfd, s, t) \
975 (ELF32_R_INFO (s, t))
978 /* The mips16 compiler uses a couple of special sections to handle
979 floating point arguments.
981 Section names that look like .mips16.fn.FNNAME contain stubs that
982 copy floating point arguments from the fp regs to the gp regs and
983 then jump to FNNAME. If any 32 bit function calls FNNAME, the
984 call should be redirected to the stub instead. If no 32 bit
985 function calls FNNAME, the stub should be discarded. We need to
986 consider any reference to the function, not just a call, because
987 if the address of the function is taken we will need the stub,
988 since the address might be passed to a 32 bit function.
990 Section names that look like .mips16.call.FNNAME contain stubs
991 that copy floating point arguments from the gp regs to the fp
992 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
993 then any 16 bit function that calls FNNAME should be redirected
994 to the stub instead. If FNNAME is not a 32 bit function, the
995 stub should be discarded.
997 .mips16.call.fp.FNNAME sections are similar, but contain stubs
998 which call FNNAME and then copy the return value from the fp regs
999 to the gp regs. These stubs store the return value in $18 while
1000 calling FNNAME; any function which might call one of these stubs
1001 must arrange to save $18 around the call. (This case is not
1002 needed for 32 bit functions that call 16 bit functions, because
1003 16 bit functions always return floating point values in both
1006 Note that in all cases FNNAME might be defined statically.
1007 Therefore, FNNAME is not used literally. Instead, the relocation
1008 information will indicate which symbol the section is for.
1010 We record any stubs that we find in the symbol table. */
1012 #define FN_STUB ".mips16.fn."
1013 #define CALL_STUB ".mips16.call."
1014 #define CALL_FP_STUB ".mips16.call.fp."
1016 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1017 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1018 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1020 /* The format of the first PLT entry in an O32 executable. */
1021 static const bfd_vma mips_o32_exec_plt0_entry[] =
1023 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1024 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1025 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1026 0x031cc023, /* subu $24, $24, $28 */
1027 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1028 0x0018c082, /* srl $24, $24, 2 */
1029 0x0320f809, /* jalr $25 */
1030 0x2718fffe /* subu $24, $24, 2 */
1033 /* The format of the first PLT entry in an N32 executable. Different
1034 because gp ($28) is not available; we use t2 ($14) instead. */
1035 static const bfd_vma mips_n32_exec_plt0_entry[] =
1037 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1038 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1039 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1040 0x030ec023, /* subu $24, $24, $14 */
1041 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1042 0x0018c082, /* srl $24, $24, 2 */
1043 0x0320f809, /* jalr $25 */
1044 0x2718fffe /* subu $24, $24, 2 */
1047 /* The format of the first PLT entry in an N64 executable. Different
1048 from N32 because of the increased size of GOT entries. */
1049 static const bfd_vma mips_n64_exec_plt0_entry[] =
1051 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1052 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1053 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1054 0x030ec023, /* subu $24, $24, $14 */
1055 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1056 0x0018c0c2, /* srl $24, $24, 3 */
1057 0x0320f809, /* jalr $25 */
1058 0x2718fffe /* subu $24, $24, 2 */
1061 /* The format of the microMIPS first PLT entry in an O32 executable.
1062 We rely on v0 ($2) rather than t8 ($24) to contain the address
1063 of the GOTPLT entry handled, so this stub may only be used when
1064 all the subsequent PLT entries are microMIPS code too.
1066 The trailing NOP is for alignment and correct disassembly only. */
1067 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1069 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1070 0xff23, 0x0000, /* lw $25, 0($3) */
1071 0x0535, /* subu $2, $2, $3 */
1072 0x2525, /* srl $2, $2, 2 */
1073 0x3302, 0xfffe, /* subu $24, $2, 2 */
1074 0x0dff, /* move $15, $31 */
1075 0x45f9, /* jalrs $25 */
1076 0x0f83, /* move $28, $3 */
1080 /* The format of the microMIPS first PLT entry in an O32 executable
1081 in the insn32 mode. */
1082 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1084 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1085 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1086 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1087 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1088 0x001f, 0x7950, /* move $15, $31 */
1089 0x0318, 0x1040, /* srl $24, $24, 2 */
1090 0x03f9, 0x0f3c, /* jalr $25 */
1091 0x3318, 0xfffe /* subu $24, $24, 2 */
1094 /* The format of subsequent standard PLT entries. */
1095 static const bfd_vma mips_exec_plt_entry[] =
1097 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1098 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1099 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1100 0x03200008 /* jr $25 */
1103 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1104 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1105 directly addressable. */
1106 static const bfd_vma mips16_o32_exec_plt_entry[] =
1108 0xb203, /* lw $2, 12($pc) */
1109 0x9a60, /* lw $3, 0($2) */
1110 0x651a, /* move $24, $2 */
1112 0x653b, /* move $25, $3 */
1114 0x0000, 0x0000 /* .word (.got.plt entry) */
1117 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1118 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1119 static const bfd_vma micromips_o32_exec_plt_entry[] =
1121 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1122 0xff22, 0x0000, /* lw $25, 0($2) */
1123 0x4599, /* jr $25 */
1124 0x0f02 /* move $24, $2 */
1127 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1128 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1130 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1131 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1132 0x0019, 0x0f3c, /* jr $25 */
1133 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1136 /* The format of the first PLT entry in a VxWorks executable. */
1137 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1139 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1140 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1141 0x8f390008, /* lw t9, 8(t9) */
1142 0x00000000, /* nop */
1143 0x03200008, /* jr t9 */
1144 0x00000000 /* nop */
1147 /* The format of subsequent PLT entries. */
1148 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1150 0x10000000, /* b .PLT_resolver */
1151 0x24180000, /* li t8, <pltindex> */
1152 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1153 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1154 0x8f390000, /* lw t9, 0(t9) */
1155 0x00000000, /* nop */
1156 0x03200008, /* jr t9 */
1157 0x00000000 /* nop */
1160 /* The format of the first PLT entry in a VxWorks shared object. */
1161 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1163 0x8f990008, /* lw t9, 8(gp) */
1164 0x00000000, /* nop */
1165 0x03200008, /* jr t9 */
1166 0x00000000, /* nop */
1167 0x00000000, /* nop */
1168 0x00000000 /* nop */
1171 /* The format of subsequent PLT entries. */
1172 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1174 0x10000000, /* b .PLT_resolver */
1175 0x24180000 /* li t8, <pltindex> */
1178 /* microMIPS 32-bit opcode helper installer. */
1181 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1183 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1184 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1187 /* microMIPS 32-bit opcode helper retriever. */
1190 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1192 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1195 /* Look up an entry in a MIPS ELF linker hash table. */
1197 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1198 ((struct mips_elf_link_hash_entry *) \
1199 elf_link_hash_lookup (&(table)->root, (string), (create), \
1202 /* Traverse a MIPS ELF linker hash table. */
1204 #define mips_elf_link_hash_traverse(table, func, info) \
1205 (elf_link_hash_traverse \
1207 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1210 /* Find the base offsets for thread-local storage in this object,
1211 for GD/LD and IE/LE respectively. */
1213 #define TP_OFFSET 0x7000
1214 #define DTP_OFFSET 0x8000
1217 dtprel_base (struct bfd_link_info *info)
1219 /* If tls_sec is NULL, we should have signalled an error already. */
1220 if (elf_hash_table (info)->tls_sec == NULL)
1222 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1226 tprel_base (struct bfd_link_info *info)
1228 /* If tls_sec is NULL, we should have signalled an error already. */
1229 if (elf_hash_table (info)->tls_sec == NULL)
1231 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1234 /* Create an entry in a MIPS ELF linker hash table. */
1236 static struct bfd_hash_entry *
1237 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1238 struct bfd_hash_table *table, const char *string)
1240 struct mips_elf_link_hash_entry *ret =
1241 (struct mips_elf_link_hash_entry *) entry;
1243 /* Allocate the structure if it has not already been allocated by a
1246 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1248 return (struct bfd_hash_entry *) ret;
1250 /* Call the allocation method of the superclass. */
1251 ret = ((struct mips_elf_link_hash_entry *)
1252 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1256 /* Set local fields. */
1257 memset (&ret->esym, 0, sizeof (EXTR));
1258 /* We use -2 as a marker to indicate that the information has
1259 not been set. -1 means there is no associated ifd. */
1262 ret->possibly_dynamic_relocs = 0;
1263 ret->fn_stub = NULL;
1264 ret->call_stub = NULL;
1265 ret->call_fp_stub = NULL;
1266 ret->global_got_area = GGA_NONE;
1267 ret->got_only_for_calls = TRUE;
1268 ret->readonly_reloc = FALSE;
1269 ret->has_static_relocs = FALSE;
1270 ret->no_fn_stub = FALSE;
1271 ret->need_fn_stub = FALSE;
1272 ret->has_nonpic_branches = FALSE;
1273 ret->needs_lazy_stub = FALSE;
1274 ret->use_plt_entry = FALSE;
1277 return (struct bfd_hash_entry *) ret;
1280 /* Allocate MIPS ELF private object data. */
1283 _bfd_mips_elf_mkobject (bfd *abfd)
1285 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1290 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1292 if (!sec->used_by_bfd)
1294 struct _mips_elf_section_data *sdata;
1295 bfd_size_type amt = sizeof (*sdata);
1297 sdata = bfd_zalloc (abfd, amt);
1300 sec->used_by_bfd = sdata;
1303 return _bfd_elf_new_section_hook (abfd, sec);
1306 /* Read ECOFF debugging information from a .mdebug section into a
1307 ecoff_debug_info structure. */
1310 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1311 struct ecoff_debug_info *debug)
1314 const struct ecoff_debug_swap *swap;
1317 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1318 memset (debug, 0, sizeof (*debug));
1320 ext_hdr = bfd_malloc (swap->external_hdr_size);
1321 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1324 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1325 swap->external_hdr_size))
1328 symhdr = &debug->symbolic_header;
1329 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1331 /* The symbolic header contains absolute file offsets and sizes to
1333 #define READ(ptr, offset, count, size, type) \
1334 if (symhdr->count == 0) \
1335 debug->ptr = NULL; \
1338 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1339 debug->ptr = bfd_malloc (amt); \
1340 if (debug->ptr == NULL) \
1341 goto error_return; \
1342 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1343 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1344 goto error_return; \
1347 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1348 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1349 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1350 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1351 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1352 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1354 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1355 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1356 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1357 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1358 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1366 if (ext_hdr != NULL)
1368 if (debug->line != NULL)
1370 if (debug->external_dnr != NULL)
1371 free (debug->external_dnr);
1372 if (debug->external_pdr != NULL)
1373 free (debug->external_pdr);
1374 if (debug->external_sym != NULL)
1375 free (debug->external_sym);
1376 if (debug->external_opt != NULL)
1377 free (debug->external_opt);
1378 if (debug->external_aux != NULL)
1379 free (debug->external_aux);
1380 if (debug->ss != NULL)
1382 if (debug->ssext != NULL)
1383 free (debug->ssext);
1384 if (debug->external_fdr != NULL)
1385 free (debug->external_fdr);
1386 if (debug->external_rfd != NULL)
1387 free (debug->external_rfd);
1388 if (debug->external_ext != NULL)
1389 free (debug->external_ext);
1393 /* Swap RPDR (runtime procedure table entry) for output. */
1396 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1398 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1399 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1400 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1401 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1402 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1403 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1405 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1406 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1408 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1411 /* Create a runtime procedure table from the .mdebug section. */
1414 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1415 struct bfd_link_info *info, asection *s,
1416 struct ecoff_debug_info *debug)
1418 const struct ecoff_debug_swap *swap;
1419 HDRR *hdr = &debug->symbolic_header;
1421 struct rpdr_ext *erp;
1423 struct pdr_ext *epdr;
1424 struct sym_ext *esym;
1428 bfd_size_type count;
1429 unsigned long sindex;
1433 const char *no_name_func = _("static procedure (no name)");
1441 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443 sindex = strlen (no_name_func) + 1;
1444 count = hdr->ipdMax;
1447 size = swap->external_pdr_size;
1449 epdr = bfd_malloc (size * count);
1453 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1456 size = sizeof (RPDR);
1457 rp = rpdr = bfd_malloc (size * count);
1461 size = sizeof (char *);
1462 sv = bfd_malloc (size * count);
1466 count = hdr->isymMax;
1467 size = swap->external_sym_size;
1468 esym = bfd_malloc (size * count);
1472 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1475 count = hdr->issMax;
1476 ss = bfd_malloc (count);
1479 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1482 count = hdr->ipdMax;
1483 for (i = 0; i < (unsigned long) count; i++, rp++)
1485 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1486 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1487 rp->adr = sym.value;
1488 rp->regmask = pdr.regmask;
1489 rp->regoffset = pdr.regoffset;
1490 rp->fregmask = pdr.fregmask;
1491 rp->fregoffset = pdr.fregoffset;
1492 rp->frameoffset = pdr.frameoffset;
1493 rp->framereg = pdr.framereg;
1494 rp->pcreg = pdr.pcreg;
1496 sv[i] = ss + sym.iss;
1497 sindex += strlen (sv[i]) + 1;
1501 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1502 size = BFD_ALIGN (size, 16);
1503 rtproc = bfd_alloc (abfd, size);
1506 mips_elf_hash_table (info)->procedure_count = 0;
1510 mips_elf_hash_table (info)->procedure_count = count + 2;
1513 memset (erp, 0, sizeof (struct rpdr_ext));
1515 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1516 strcpy (str, no_name_func);
1517 str += strlen (no_name_func) + 1;
1518 for (i = 0; i < count; i++)
1520 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1521 strcpy (str, sv[i]);
1522 str += strlen (sv[i]) + 1;
1524 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1526 /* Set the size and contents of .rtproc section. */
1528 s->contents = rtproc;
1530 /* Skip this section later on (I don't think this currently
1531 matters, but someday it might). */
1532 s->map_head.link_order = NULL;
1561 /* We're going to create a stub for H. Create a symbol for the stub's
1562 value and size, to help make the disassembly easier to read. */
1565 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1566 struct mips_elf_link_hash_entry *h,
1567 const char *prefix, asection *s, bfd_vma value,
1570 struct bfd_link_hash_entry *bh;
1571 struct elf_link_hash_entry *elfh;
1574 if (ELF_ST_IS_MICROMIPS (h->root.other))
1577 /* Create a new symbol. */
1578 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1580 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1581 BSF_LOCAL, s, value, NULL,
1585 /* Make it a local function. */
1586 elfh = (struct elf_link_hash_entry *) bh;
1587 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1589 elfh->forced_local = 1;
1593 /* We're about to redefine H. Create a symbol to represent H's
1594 current value and size, to help make the disassembly easier
1598 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1599 struct mips_elf_link_hash_entry *h,
1602 struct bfd_link_hash_entry *bh;
1603 struct elf_link_hash_entry *elfh;
1608 /* Read the symbol's value. */
1609 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1610 || h->root.root.type == bfd_link_hash_defweak);
1611 s = h->root.root.u.def.section;
1612 value = h->root.root.u.def.value;
1614 /* Create a new symbol. */
1615 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1617 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1618 BSF_LOCAL, s, value, NULL,
1622 /* Make it local and copy the other attributes from H. */
1623 elfh = (struct elf_link_hash_entry *) bh;
1624 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1625 elfh->other = h->root.other;
1626 elfh->size = h->root.size;
1627 elfh->forced_local = 1;
1631 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1632 function rather than to a hard-float stub. */
1635 section_allows_mips16_refs_p (asection *section)
1639 name = bfd_get_section_name (section->owner, section);
1640 return (FN_STUB_P (name)
1641 || CALL_STUB_P (name)
1642 || CALL_FP_STUB_P (name)
1643 || strcmp (name, ".pdr") == 0);
1646 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1647 stub section of some kind. Return the R_SYMNDX of the target
1648 function, or 0 if we can't decide which function that is. */
1650 static unsigned long
1651 mips16_stub_symndx (const struct elf_backend_data *bed,
1652 asection *sec ATTRIBUTE_UNUSED,
1653 const Elf_Internal_Rela *relocs,
1654 const Elf_Internal_Rela *relend)
1656 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1657 const Elf_Internal_Rela *rel;
1659 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1660 one in a compound relocation. */
1661 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1662 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1663 return ELF_R_SYM (sec->owner, rel->r_info);
1665 /* Otherwise trust the first relocation, whatever its kind. This is
1666 the traditional behavior. */
1667 if (relocs < relend)
1668 return ELF_R_SYM (sec->owner, relocs->r_info);
1673 /* Check the mips16 stubs for a particular symbol, and see if we can
1677 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1678 struct mips_elf_link_hash_entry *h)
1680 /* Dynamic symbols must use the standard call interface, in case other
1681 objects try to call them. */
1682 if (h->fn_stub != NULL
1683 && h->root.dynindx != -1)
1685 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1686 h->need_fn_stub = TRUE;
1689 if (h->fn_stub != NULL
1690 && ! h->need_fn_stub)
1692 /* We don't need the fn_stub; the only references to this symbol
1693 are 16 bit calls. Clobber the size to 0 to prevent it from
1694 being included in the link. */
1695 h->fn_stub->size = 0;
1696 h->fn_stub->flags &= ~SEC_RELOC;
1697 h->fn_stub->reloc_count = 0;
1698 h->fn_stub->flags |= SEC_EXCLUDE;
1701 if (h->call_stub != NULL
1702 && ELF_ST_IS_MIPS16 (h->root.other))
1704 /* We don't need the call_stub; this is a 16 bit function, so
1705 calls from other 16 bit functions are OK. Clobber the size
1706 to 0 to prevent it from being included in the link. */
1707 h->call_stub->size = 0;
1708 h->call_stub->flags &= ~SEC_RELOC;
1709 h->call_stub->reloc_count = 0;
1710 h->call_stub->flags |= SEC_EXCLUDE;
1713 if (h->call_fp_stub != NULL
1714 && ELF_ST_IS_MIPS16 (h->root.other))
1716 /* We don't need the call_stub; this is a 16 bit function, so
1717 calls from other 16 bit functions are OK. Clobber the size
1718 to 0 to prevent it from being included in the link. */
1719 h->call_fp_stub->size = 0;
1720 h->call_fp_stub->flags &= ~SEC_RELOC;
1721 h->call_fp_stub->reloc_count = 0;
1722 h->call_fp_stub->flags |= SEC_EXCLUDE;
1726 /* Hashtable callbacks for mips_elf_la25_stubs. */
1729 mips_elf_la25_stub_hash (const void *entry_)
1731 const struct mips_elf_la25_stub *entry;
1733 entry = (struct mips_elf_la25_stub *) entry_;
1734 return entry->h->root.root.u.def.section->id
1735 + entry->h->root.root.u.def.value;
1739 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1741 const struct mips_elf_la25_stub *entry1, *entry2;
1743 entry1 = (struct mips_elf_la25_stub *) entry1_;
1744 entry2 = (struct mips_elf_la25_stub *) entry2_;
1745 return ((entry1->h->root.root.u.def.section
1746 == entry2->h->root.root.u.def.section)
1747 && (entry1->h->root.root.u.def.value
1748 == entry2->h->root.root.u.def.value));
1751 /* Called by the linker to set up the la25 stub-creation code. FN is
1752 the linker's implementation of add_stub_function. Return true on
1756 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1757 asection *(*fn) (const char *, asection *,
1760 struct mips_elf_link_hash_table *htab;
1762 htab = mips_elf_hash_table (info);
1766 htab->add_stub_section = fn;
1767 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1768 mips_elf_la25_stub_eq, NULL);
1769 if (htab->la25_stubs == NULL)
1775 /* Return true if H is a locally-defined PIC function, in the sense
1776 that it or its fn_stub might need $25 to be valid on entry.
1777 Note that MIPS16 functions set up $gp using PC-relative instructions,
1778 so they themselves never need $25 to be valid. Only non-MIPS16
1779 entry points are of interest here. */
1782 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1784 return ((h->root.root.type == bfd_link_hash_defined
1785 || h->root.root.type == bfd_link_hash_defweak)
1786 && h->root.def_regular
1787 && !bfd_is_abs_section (h->root.root.u.def.section)
1788 && (!ELF_ST_IS_MIPS16 (h->root.other)
1789 || (h->fn_stub && h->need_fn_stub))
1790 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1791 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1794 /* Set *SEC to the input section that contains the target of STUB.
1795 Return the offset of the target from the start of that section. */
1798 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1801 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1803 BFD_ASSERT (stub->h->need_fn_stub);
1804 *sec = stub->h->fn_stub;
1809 *sec = stub->h->root.root.u.def.section;
1810 return stub->h->root.root.u.def.value;
1814 /* STUB describes an la25 stub that we have decided to implement
1815 by inserting an LUI/ADDIU pair before the target function.
1816 Create the section and redirect the function symbol to it. */
1819 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1820 struct bfd_link_info *info)
1822 struct mips_elf_link_hash_table *htab;
1824 asection *s, *input_section;
1827 htab = mips_elf_hash_table (info);
1831 /* Create a unique name for the new section. */
1832 name = bfd_malloc (11 + sizeof (".text.stub."));
1835 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1837 /* Create the section. */
1838 mips_elf_get_la25_target (stub, &input_section);
1839 s = htab->add_stub_section (name, input_section,
1840 input_section->output_section);
1844 /* Make sure that any padding goes before the stub. */
1845 align = input_section->alignment_power;
1846 if (!bfd_set_section_alignment (s->owner, s, align))
1849 s->size = (1 << align) - 8;
1851 /* Create a symbol for the stub. */
1852 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1853 stub->stub_section = s;
1854 stub->offset = s->size;
1856 /* Allocate room for it. */
1861 /* STUB describes an la25 stub that we have decided to implement
1862 with a separate trampoline. Allocate room for it and redirect
1863 the function symbol to it. */
1866 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1867 struct bfd_link_info *info)
1869 struct mips_elf_link_hash_table *htab;
1872 htab = mips_elf_hash_table (info);
1876 /* Create a trampoline section, if we haven't already. */
1877 s = htab->strampoline;
1880 asection *input_section = stub->h->root.root.u.def.section;
1881 s = htab->add_stub_section (".text", NULL,
1882 input_section->output_section);
1883 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1885 htab->strampoline = s;
1888 /* Create a symbol for the stub. */
1889 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1890 stub->stub_section = s;
1891 stub->offset = s->size;
1893 /* Allocate room for it. */
1898 /* H describes a symbol that needs an la25 stub. Make sure that an
1899 appropriate stub exists and point H at it. */
1902 mips_elf_add_la25_stub (struct bfd_link_info *info,
1903 struct mips_elf_link_hash_entry *h)
1905 struct mips_elf_link_hash_table *htab;
1906 struct mips_elf_la25_stub search, *stub;
1907 bfd_boolean use_trampoline_p;
1912 /* Describe the stub we want. */
1913 search.stub_section = NULL;
1917 /* See if we've already created an equivalent stub. */
1918 htab = mips_elf_hash_table (info);
1922 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1926 stub = (struct mips_elf_la25_stub *) *slot;
1929 /* We can reuse the existing stub. */
1930 h->la25_stub = stub;
1934 /* Create a permanent copy of ENTRY and add it to the hash table. */
1935 stub = bfd_malloc (sizeof (search));
1941 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1942 of the section and if we would need no more than 2 nops. */
1943 value = mips_elf_get_la25_target (stub, &s);
1944 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1946 h->la25_stub = stub;
1947 return (use_trampoline_p
1948 ? mips_elf_add_la25_trampoline (stub, info)
1949 : mips_elf_add_la25_intro (stub, info));
1952 /* A mips_elf_link_hash_traverse callback that is called before sizing
1953 sections. DATA points to a mips_htab_traverse_info structure. */
1956 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1958 struct mips_htab_traverse_info *hti;
1960 hti = (struct mips_htab_traverse_info *) data;
1961 if (!hti->info->relocatable)
1962 mips_elf_check_mips16_stubs (hti->info, h);
1964 if (mips_elf_local_pic_function_p (h))
1966 /* PR 12845: If H is in a section that has been garbage
1967 collected it will have its output section set to *ABS*. */
1968 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1971 /* H is a function that might need $25 to be valid on entry.
1972 If we're creating a non-PIC relocatable object, mark H as
1973 being PIC. If we're creating a non-relocatable object with
1974 non-PIC branches and jumps to H, make sure that H has an la25
1976 if (hti->info->relocatable)
1978 if (!PIC_OBJECT_P (hti->output_bfd))
1979 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1981 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1990 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1991 Most mips16 instructions are 16 bits, but these instructions
1994 The format of these instructions is:
1996 +--------------+--------------------------------+
1997 | JALX | X| Imm 20:16 | Imm 25:21 |
1998 +--------------+--------------------------------+
2000 +-----------------------------------------------+
2002 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2003 Note that the immediate value in the first word is swapped.
2005 When producing a relocatable object file, R_MIPS16_26 is
2006 handled mostly like R_MIPS_26. In particular, the addend is
2007 stored as a straight 26-bit value in a 32-bit instruction.
2008 (gas makes life simpler for itself by never adjusting a
2009 R_MIPS16_26 reloc to be against a section, so the addend is
2010 always zero). However, the 32 bit instruction is stored as 2
2011 16-bit values, rather than a single 32-bit value. In a
2012 big-endian file, the result is the same; in a little-endian
2013 file, the two 16-bit halves of the 32 bit value are swapped.
2014 This is so that a disassembler can recognize the jal
2017 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2018 instruction stored as two 16-bit values. The addend A is the
2019 contents of the targ26 field. The calculation is the same as
2020 R_MIPS_26. When storing the calculated value, reorder the
2021 immediate value as shown above, and don't forget to store the
2022 value as two 16-bit values.
2024 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2028 +--------+----------------------+
2032 +--------+----------------------+
2035 +----------+------+-------------+
2039 +----------+--------------------+
2040 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2041 ((sub1 << 16) | sub2)).
2043 When producing a relocatable object file, the calculation is
2044 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2045 When producing a fully linked file, the calculation is
2046 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2047 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2049 The table below lists the other MIPS16 instruction relocations.
2050 Each one is calculated in the same way as the non-MIPS16 relocation
2051 given on the right, but using the extended MIPS16 layout of 16-bit
2054 R_MIPS16_GPREL R_MIPS_GPREL16
2055 R_MIPS16_GOT16 R_MIPS_GOT16
2056 R_MIPS16_CALL16 R_MIPS_CALL16
2057 R_MIPS16_HI16 R_MIPS_HI16
2058 R_MIPS16_LO16 R_MIPS_LO16
2060 A typical instruction will have a format like this:
2062 +--------------+--------------------------------+
2063 | EXTEND | Imm 10:5 | Imm 15:11 |
2064 +--------------+--------------------------------+
2065 | Major | rx | ry | Imm 4:0 |
2066 +--------------+--------------------------------+
2068 EXTEND is the five bit value 11110. Major is the instruction
2071 All we need to do here is shuffle the bits appropriately.
2072 As above, the two 16-bit halves must be swapped on a
2073 little-endian system. */
2075 static inline bfd_boolean
2076 mips16_reloc_p (int r_type)
2081 case R_MIPS16_GPREL:
2082 case R_MIPS16_GOT16:
2083 case R_MIPS16_CALL16:
2086 case R_MIPS16_TLS_GD:
2087 case R_MIPS16_TLS_LDM:
2088 case R_MIPS16_TLS_DTPREL_HI16:
2089 case R_MIPS16_TLS_DTPREL_LO16:
2090 case R_MIPS16_TLS_GOTTPREL:
2091 case R_MIPS16_TLS_TPREL_HI16:
2092 case R_MIPS16_TLS_TPREL_LO16:
2100 /* Check if a microMIPS reloc. */
2102 static inline bfd_boolean
2103 micromips_reloc_p (unsigned int r_type)
2105 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2108 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2109 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2110 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2112 static inline bfd_boolean
2113 micromips_reloc_shuffle_p (unsigned int r_type)
2115 return (micromips_reloc_p (r_type)
2116 && r_type != R_MICROMIPS_PC7_S1
2117 && r_type != R_MICROMIPS_PC10_S1);
2120 static inline bfd_boolean
2121 got16_reloc_p (int r_type)
2123 return (r_type == R_MIPS_GOT16
2124 || r_type == R_MIPS16_GOT16
2125 || r_type == R_MICROMIPS_GOT16);
2128 static inline bfd_boolean
2129 call16_reloc_p (int r_type)
2131 return (r_type == R_MIPS_CALL16
2132 || r_type == R_MIPS16_CALL16
2133 || r_type == R_MICROMIPS_CALL16);
2136 static inline bfd_boolean
2137 got_disp_reloc_p (unsigned int r_type)
2139 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2142 static inline bfd_boolean
2143 got_page_reloc_p (unsigned int r_type)
2145 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2148 static inline bfd_boolean
2149 got_ofst_reloc_p (unsigned int r_type)
2151 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2154 static inline bfd_boolean
2155 got_hi16_reloc_p (unsigned int r_type)
2157 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2160 static inline bfd_boolean
2161 got_lo16_reloc_p (unsigned int r_type)
2163 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2166 static inline bfd_boolean
2167 call_hi16_reloc_p (unsigned int r_type)
2169 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2172 static inline bfd_boolean
2173 call_lo16_reloc_p (unsigned int r_type)
2175 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2178 static inline bfd_boolean
2179 hi16_reloc_p (int r_type)
2181 return (r_type == R_MIPS_HI16
2182 || r_type == R_MIPS16_HI16
2183 || r_type == R_MICROMIPS_HI16);
2186 static inline bfd_boolean
2187 lo16_reloc_p (int r_type)
2189 return (r_type == R_MIPS_LO16
2190 || r_type == R_MIPS16_LO16
2191 || r_type == R_MICROMIPS_LO16);
2194 static inline bfd_boolean
2195 mips16_call_reloc_p (int r_type)
2197 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2200 static inline bfd_boolean
2201 jal_reloc_p (int r_type)
2203 return (r_type == R_MIPS_26
2204 || r_type == R_MIPS16_26
2205 || r_type == R_MICROMIPS_26_S1);
2208 static inline bfd_boolean
2209 micromips_branch_reloc_p (int r_type)
2211 return (r_type == R_MICROMIPS_26_S1
2212 || r_type == R_MICROMIPS_PC16_S1
2213 || r_type == R_MICROMIPS_PC10_S1
2214 || r_type == R_MICROMIPS_PC7_S1);
2217 static inline bfd_boolean
2218 tls_gd_reloc_p (unsigned int r_type)
2220 return (r_type == R_MIPS_TLS_GD
2221 || r_type == R_MIPS16_TLS_GD
2222 || r_type == R_MICROMIPS_TLS_GD);
2225 static inline bfd_boolean
2226 tls_ldm_reloc_p (unsigned int r_type)
2228 return (r_type == R_MIPS_TLS_LDM
2229 || r_type == R_MIPS16_TLS_LDM
2230 || r_type == R_MICROMIPS_TLS_LDM);
2233 static inline bfd_boolean
2234 tls_gottprel_reloc_p (unsigned int r_type)
2236 return (r_type == R_MIPS_TLS_GOTTPREL
2237 || r_type == R_MIPS16_TLS_GOTTPREL
2238 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2242 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2243 bfd_boolean jal_shuffle, bfd_byte *data)
2245 bfd_vma first, second, val;
2247 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2250 /* Pick up the first and second halfwords of the instruction. */
2251 first = bfd_get_16 (abfd, data);
2252 second = bfd_get_16 (abfd, data + 2);
2253 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2254 val = first << 16 | second;
2255 else if (r_type != R_MIPS16_26)
2256 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2257 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2259 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2260 | ((first & 0x1f) << 21) | second);
2261 bfd_put_32 (abfd, val, data);
2265 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2266 bfd_boolean jal_shuffle, bfd_byte *data)
2268 bfd_vma first, second, val;
2270 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2273 val = bfd_get_32 (abfd, data);
2274 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2276 second = val & 0xffff;
2279 else if (r_type != R_MIPS16_26)
2281 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2282 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2286 second = val & 0xffff;
2287 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2288 | ((val >> 21) & 0x1f);
2290 bfd_put_16 (abfd, second, data + 2);
2291 bfd_put_16 (abfd, first, data);
2294 bfd_reloc_status_type
2295 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2296 arelent *reloc_entry, asection *input_section,
2297 bfd_boolean relocatable, void *data, bfd_vma gp)
2301 bfd_reloc_status_type status;
2303 if (bfd_is_com_section (symbol->section))
2306 relocation = symbol->value;
2308 relocation += symbol->section->output_section->vma;
2309 relocation += symbol->section->output_offset;
2311 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2312 return bfd_reloc_outofrange;
2314 /* Set val to the offset into the section or symbol. */
2315 val = reloc_entry->addend;
2317 _bfd_mips_elf_sign_extend (val, 16);
2319 /* Adjust val for the final section location and GP value. If we
2320 are producing relocatable output, we don't want to do this for
2321 an external symbol. */
2323 || (symbol->flags & BSF_SECTION_SYM) != 0)
2324 val += relocation - gp;
2326 if (reloc_entry->howto->partial_inplace)
2328 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2330 + reloc_entry->address);
2331 if (status != bfd_reloc_ok)
2335 reloc_entry->addend = val;
2338 reloc_entry->address += input_section->output_offset;
2340 return bfd_reloc_ok;
2343 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2344 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2345 that contains the relocation field and DATA points to the start of
2350 struct mips_hi16 *next;
2352 asection *input_section;
2356 /* FIXME: This should not be a static variable. */
2358 static struct mips_hi16 *mips_hi16_list;
2360 /* A howto special_function for REL *HI16 relocations. We can only
2361 calculate the correct value once we've seen the partnering
2362 *LO16 relocation, so just save the information for later.
2364 The ABI requires that the *LO16 immediately follow the *HI16.
2365 However, as a GNU extension, we permit an arbitrary number of
2366 *HI16s to be associated with a single *LO16. This significantly
2367 simplies the relocation handling in gcc. */
2369 bfd_reloc_status_type
2370 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2371 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2372 asection *input_section, bfd *output_bfd,
2373 char **error_message ATTRIBUTE_UNUSED)
2375 struct mips_hi16 *n;
2377 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2378 return bfd_reloc_outofrange;
2380 n = bfd_malloc (sizeof *n);
2382 return bfd_reloc_outofrange;
2384 n->next = mips_hi16_list;
2386 n->input_section = input_section;
2387 n->rel = *reloc_entry;
2390 if (output_bfd != NULL)
2391 reloc_entry->address += input_section->output_offset;
2393 return bfd_reloc_ok;
2396 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2397 like any other 16-bit relocation when applied to global symbols, but is
2398 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2400 bfd_reloc_status_type
2401 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2402 void *data, asection *input_section,
2403 bfd *output_bfd, char **error_message)
2405 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2406 || bfd_is_und_section (bfd_get_section (symbol))
2407 || bfd_is_com_section (bfd_get_section (symbol)))
2408 /* The relocation is against a global symbol. */
2409 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2410 input_section, output_bfd,
2413 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2414 input_section, output_bfd, error_message);
2417 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2418 is a straightforward 16 bit inplace relocation, but we must deal with
2419 any partnering high-part relocations as well. */
2421 bfd_reloc_status_type
2422 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423 void *data, asection *input_section,
2424 bfd *output_bfd, char **error_message)
2427 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2429 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2430 return bfd_reloc_outofrange;
2432 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2434 vallo = bfd_get_32 (abfd, location);
2435 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2438 while (mips_hi16_list != NULL)
2440 bfd_reloc_status_type ret;
2441 struct mips_hi16 *hi;
2443 hi = mips_hi16_list;
2445 /* R_MIPS*_GOT16 relocations are something of a special case. We
2446 want to install the addend in the same way as for a R_MIPS*_HI16
2447 relocation (with a rightshift of 16). However, since GOT16
2448 relocations can also be used with global symbols, their howto
2449 has a rightshift of 0. */
2450 if (hi->rel.howto->type == R_MIPS_GOT16)
2451 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2452 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2453 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2454 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2455 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2457 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2458 carry or borrow will induce a change of +1 or -1 in the high part. */
2459 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2461 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2462 hi->input_section, output_bfd,
2464 if (ret != bfd_reloc_ok)
2467 mips_hi16_list = hi->next;
2471 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2472 input_section, output_bfd,
2476 /* A generic howto special_function. This calculates and installs the
2477 relocation itself, thus avoiding the oft-discussed problems in
2478 bfd_perform_relocation and bfd_install_relocation. */
2480 bfd_reloc_status_type
2481 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2482 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2483 asection *input_section, bfd *output_bfd,
2484 char **error_message ATTRIBUTE_UNUSED)
2487 bfd_reloc_status_type status;
2488 bfd_boolean relocatable;
2490 relocatable = (output_bfd != NULL);
2492 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2493 return bfd_reloc_outofrange;
2495 /* Build up the field adjustment in VAL. */
2497 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2499 /* Either we're calculating the final field value or we have a
2500 relocation against a section symbol. Add in the section's
2501 offset or address. */
2502 val += symbol->section->output_section->vma;
2503 val += symbol->section->output_offset;
2508 /* We're calculating the final field value. Add in the symbol's value
2509 and, if pc-relative, subtract the address of the field itself. */
2510 val += symbol->value;
2511 if (reloc_entry->howto->pc_relative)
2513 val -= input_section->output_section->vma;
2514 val -= input_section->output_offset;
2515 val -= reloc_entry->address;
2519 /* VAL is now the final adjustment. If we're keeping this relocation
2520 in the output file, and if the relocation uses a separate addend,
2521 we just need to add VAL to that addend. Otherwise we need to add
2522 VAL to the relocation field itself. */
2523 if (relocatable && !reloc_entry->howto->partial_inplace)
2524 reloc_entry->addend += val;
2527 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2529 /* Add in the separate addend, if any. */
2530 val += reloc_entry->addend;
2532 /* Add VAL to the relocation field. */
2533 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2535 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2537 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2540 if (status != bfd_reloc_ok)
2545 reloc_entry->address += input_section->output_offset;
2547 return bfd_reloc_ok;
2550 /* Swap an entry in a .gptab section. Note that these routines rely
2551 on the equivalence of the two elements of the union. */
2554 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2557 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2558 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2562 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2563 Elf32_External_gptab *ex)
2565 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2566 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2570 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2571 Elf32_External_compact_rel *ex)
2573 H_PUT_32 (abfd, in->id1, ex->id1);
2574 H_PUT_32 (abfd, in->num, ex->num);
2575 H_PUT_32 (abfd, in->id2, ex->id2);
2576 H_PUT_32 (abfd, in->offset, ex->offset);
2577 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2578 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2582 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2583 Elf32_External_crinfo *ex)
2587 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2588 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2589 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2590 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2591 H_PUT_32 (abfd, l, ex->info);
2592 H_PUT_32 (abfd, in->konst, ex->konst);
2593 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2596 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2597 routines swap this structure in and out. They are used outside of
2598 BFD, so they are globally visible. */
2601 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2604 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2605 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2606 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2607 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2608 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2609 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2613 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2614 Elf32_External_RegInfo *ex)
2616 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2617 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2618 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2619 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2620 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2621 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2624 /* In the 64 bit ABI, the .MIPS.options section holds register
2625 information in an Elf64_Reginfo structure. These routines swap
2626 them in and out. They are globally visible because they are used
2627 outside of BFD. These routines are here so that gas can call them
2628 without worrying about whether the 64 bit ABI has been included. */
2631 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2632 Elf64_Internal_RegInfo *in)
2634 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2635 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2636 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2637 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2638 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2639 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2640 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2644 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2645 Elf64_External_RegInfo *ex)
2647 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2648 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2649 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2650 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2651 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2652 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2653 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2656 /* Swap in an options header. */
2659 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2660 Elf_Internal_Options *in)
2662 in->kind = H_GET_8 (abfd, ex->kind);
2663 in->size = H_GET_8 (abfd, ex->size);
2664 in->section = H_GET_16 (abfd, ex->section);
2665 in->info = H_GET_32 (abfd, ex->info);
2668 /* Swap out an options header. */
2671 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2672 Elf_External_Options *ex)
2674 H_PUT_8 (abfd, in->kind, ex->kind);
2675 H_PUT_8 (abfd, in->size, ex->size);
2676 H_PUT_16 (abfd, in->section, ex->section);
2677 H_PUT_32 (abfd, in->info, ex->info);
2680 /* Swap in an abiflags structure. */
2683 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2684 const Elf_External_ABIFlags_v0 *ex,
2685 Elf_Internal_ABIFlags_v0 *in)
2687 in->version = H_GET_16 (abfd, ex->version);
2688 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2689 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2690 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2691 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2692 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2693 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2694 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2695 in->ases = H_GET_32 (abfd, ex->ases);
2696 in->flags1 = H_GET_32 (abfd, ex->flags1);
2697 in->flags2 = H_GET_32 (abfd, ex->flags2);
2700 /* Swap out an abiflags structure. */
2703 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2704 const Elf_Internal_ABIFlags_v0 *in,
2705 Elf_External_ABIFlags_v0 *ex)
2707 H_PUT_16 (abfd, in->version, ex->version);
2708 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2709 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2710 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2711 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2712 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2713 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2714 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2715 H_PUT_32 (abfd, in->ases, ex->ases);
2716 H_PUT_32 (abfd, in->flags1, ex->flags1);
2717 H_PUT_32 (abfd, in->flags2, ex->flags2);
2720 /* This function is called via qsort() to sort the dynamic relocation
2721 entries by increasing r_symndx value. */
2724 sort_dynamic_relocs (const void *arg1, const void *arg2)
2726 Elf_Internal_Rela int_reloc1;
2727 Elf_Internal_Rela int_reloc2;
2730 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2731 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2733 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2737 if (int_reloc1.r_offset < int_reloc2.r_offset)
2739 if (int_reloc1.r_offset > int_reloc2.r_offset)
2744 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2747 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2748 const void *arg2 ATTRIBUTE_UNUSED)
2751 Elf_Internal_Rela int_reloc1[3];
2752 Elf_Internal_Rela int_reloc2[3];
2754 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2755 (reldyn_sorting_bfd, arg1, int_reloc1);
2756 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2757 (reldyn_sorting_bfd, arg2, int_reloc2);
2759 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2761 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2764 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2766 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2775 /* This routine is used to write out ECOFF debugging external symbol
2776 information. It is called via mips_elf_link_hash_traverse. The
2777 ECOFF external symbol information must match the ELF external
2778 symbol information. Unfortunately, at this point we don't know
2779 whether a symbol is required by reloc information, so the two
2780 tables may wind up being different. We must sort out the external
2781 symbol information before we can set the final size of the .mdebug
2782 section, and we must set the size of the .mdebug section before we
2783 can relocate any sections, and we can't know which symbols are
2784 required by relocation until we relocate the sections.
2785 Fortunately, it is relatively unlikely that any symbol will be
2786 stripped but required by a reloc. In particular, it can not happen
2787 when generating a final executable. */
2790 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2792 struct extsym_info *einfo = data;
2794 asection *sec, *output_section;
2796 if (h->root.indx == -2)
2798 else if ((h->root.def_dynamic
2799 || h->root.ref_dynamic
2800 || h->root.type == bfd_link_hash_new)
2801 && !h->root.def_regular
2802 && !h->root.ref_regular)
2804 else if (einfo->info->strip == strip_all
2805 || (einfo->info->strip == strip_some
2806 && bfd_hash_lookup (einfo->info->keep_hash,
2807 h->root.root.root.string,
2808 FALSE, FALSE) == NULL))
2816 if (h->esym.ifd == -2)
2819 h->esym.cobol_main = 0;
2820 h->esym.weakext = 0;
2821 h->esym.reserved = 0;
2822 h->esym.ifd = ifdNil;
2823 h->esym.asym.value = 0;
2824 h->esym.asym.st = stGlobal;
2826 if (h->root.root.type == bfd_link_hash_undefined
2827 || h->root.root.type == bfd_link_hash_undefweak)
2831 /* Use undefined class. Also, set class and type for some
2833 name = h->root.root.root.string;
2834 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2835 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2837 h->esym.asym.sc = scData;
2838 h->esym.asym.st = stLabel;
2839 h->esym.asym.value = 0;
2841 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2843 h->esym.asym.sc = scAbs;
2844 h->esym.asym.st = stLabel;
2845 h->esym.asym.value =
2846 mips_elf_hash_table (einfo->info)->procedure_count;
2848 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2850 h->esym.asym.sc = scAbs;
2851 h->esym.asym.st = stLabel;
2852 h->esym.asym.value = elf_gp (einfo->abfd);
2855 h->esym.asym.sc = scUndefined;
2857 else if (h->root.root.type != bfd_link_hash_defined
2858 && h->root.root.type != bfd_link_hash_defweak)
2859 h->esym.asym.sc = scAbs;
2864 sec = h->root.root.u.def.section;
2865 output_section = sec->output_section;
2867 /* When making a shared library and symbol h is the one from
2868 the another shared library, OUTPUT_SECTION may be null. */
2869 if (output_section == NULL)
2870 h->esym.asym.sc = scUndefined;
2873 name = bfd_section_name (output_section->owner, output_section);
2875 if (strcmp (name, ".text") == 0)
2876 h->esym.asym.sc = scText;
2877 else if (strcmp (name, ".data") == 0)
2878 h->esym.asym.sc = scData;
2879 else if (strcmp (name, ".sdata") == 0)
2880 h->esym.asym.sc = scSData;
2881 else if (strcmp (name, ".rodata") == 0
2882 || strcmp (name, ".rdata") == 0)
2883 h->esym.asym.sc = scRData;
2884 else if (strcmp (name, ".bss") == 0)
2885 h->esym.asym.sc = scBss;
2886 else if (strcmp (name, ".sbss") == 0)
2887 h->esym.asym.sc = scSBss;
2888 else if (strcmp (name, ".init") == 0)
2889 h->esym.asym.sc = scInit;
2890 else if (strcmp (name, ".fini") == 0)
2891 h->esym.asym.sc = scFini;
2893 h->esym.asym.sc = scAbs;
2897 h->esym.asym.reserved = 0;
2898 h->esym.asym.index = indexNil;
2901 if (h->root.root.type == bfd_link_hash_common)
2902 h->esym.asym.value = h->root.root.u.c.size;
2903 else if (h->root.root.type == bfd_link_hash_defined
2904 || h->root.root.type == bfd_link_hash_defweak)
2906 if (h->esym.asym.sc == scCommon)
2907 h->esym.asym.sc = scBss;
2908 else if (h->esym.asym.sc == scSCommon)
2909 h->esym.asym.sc = scSBss;
2911 sec = h->root.root.u.def.section;
2912 output_section = sec->output_section;
2913 if (output_section != NULL)
2914 h->esym.asym.value = (h->root.root.u.def.value
2915 + sec->output_offset
2916 + output_section->vma);
2918 h->esym.asym.value = 0;
2922 struct mips_elf_link_hash_entry *hd = h;
2924 while (hd->root.root.type == bfd_link_hash_indirect)
2925 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2927 if (hd->needs_lazy_stub)
2929 BFD_ASSERT (hd->root.plt.plist != NULL);
2930 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
2931 /* Set type and value for a symbol with a function stub. */
2932 h->esym.asym.st = stProc;
2933 sec = hd->root.root.u.def.section;
2935 h->esym.asym.value = 0;
2938 output_section = sec->output_section;
2939 if (output_section != NULL)
2940 h->esym.asym.value = (hd->root.plt.plist->stub_offset
2941 + sec->output_offset
2942 + output_section->vma);
2944 h->esym.asym.value = 0;
2949 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2950 h->root.root.root.string,
2953 einfo->failed = TRUE;
2960 /* A comparison routine used to sort .gptab entries. */
2963 gptab_compare (const void *p1, const void *p2)
2965 const Elf32_gptab *a1 = p1;
2966 const Elf32_gptab *a2 = p2;
2968 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2971 /* Functions to manage the got entry hash table. */
2973 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2976 static INLINE hashval_t
2977 mips_elf_hash_bfd_vma (bfd_vma addr)
2980 return addr + (addr >> 32);
2987 mips_elf_got_entry_hash (const void *entry_)
2989 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2991 return (entry->symndx
2992 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2993 + (entry->tls_type == GOT_TLS_LDM ? 0
2994 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2995 : entry->symndx >= 0 ? (entry->abfd->id
2996 + mips_elf_hash_bfd_vma (entry->d.addend))
2997 : entry->d.h->root.root.root.hash));
3001 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3003 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3004 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3006 return (e1->symndx == e2->symndx
3007 && e1->tls_type == e2->tls_type
3008 && (e1->tls_type == GOT_TLS_LDM ? TRUE
3009 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3010 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3011 && e1->d.addend == e2->d.addend)
3012 : e2->abfd && e1->d.h == e2->d.h));
3016 mips_got_page_ref_hash (const void *ref_)
3018 const struct mips_got_page_ref *ref;
3020 ref = (const struct mips_got_page_ref *) ref_;
3021 return ((ref->symndx >= 0
3022 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3023 : ref->u.h->root.root.root.hash)
3024 + mips_elf_hash_bfd_vma (ref->addend));
3028 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3030 const struct mips_got_page_ref *ref1, *ref2;
3032 ref1 = (const struct mips_got_page_ref *) ref1_;
3033 ref2 = (const struct mips_got_page_ref *) ref2_;
3034 return (ref1->symndx == ref2->symndx
3035 && (ref1->symndx < 0
3036 ? ref1->u.h == ref2->u.h
3037 : ref1->u.abfd == ref2->u.abfd)
3038 && ref1->addend == ref2->addend);
3042 mips_got_page_entry_hash (const void *entry_)
3044 const struct mips_got_page_entry *entry;
3046 entry = (const struct mips_got_page_entry *) entry_;
3047 return entry->sec->id;
3051 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3053 const struct mips_got_page_entry *entry1, *entry2;
3055 entry1 = (const struct mips_got_page_entry *) entry1_;
3056 entry2 = (const struct mips_got_page_entry *) entry2_;
3057 return entry1->sec == entry2->sec;
3060 /* Create and return a new mips_got_info structure. */
3062 static struct mips_got_info *
3063 mips_elf_create_got_info (bfd *abfd)
3065 struct mips_got_info *g;
3067 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3071 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3072 mips_elf_got_entry_eq, NULL);
3073 if (g->got_entries == NULL)
3076 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3077 mips_got_page_ref_eq, NULL);
3078 if (g->got_page_refs == NULL)
3084 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3085 CREATE_P and if ABFD doesn't already have a GOT. */
3087 static struct mips_got_info *
3088 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3090 struct mips_elf_obj_tdata *tdata;
3092 if (!is_mips_elf (abfd))
3095 tdata = mips_elf_tdata (abfd);
3096 if (!tdata->got && create_p)
3097 tdata->got = mips_elf_create_got_info (abfd);
3101 /* Record that ABFD should use output GOT G. */
3104 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3106 struct mips_elf_obj_tdata *tdata;
3108 BFD_ASSERT (is_mips_elf (abfd));
3109 tdata = mips_elf_tdata (abfd);
3112 /* The GOT structure itself and the hash table entries are
3113 allocated to a bfd, but the hash tables aren't. */
3114 htab_delete (tdata->got->got_entries);
3115 htab_delete (tdata->got->got_page_refs);
3116 if (tdata->got->got_page_entries)
3117 htab_delete (tdata->got->got_page_entries);
3122 /* Return the dynamic relocation section. If it doesn't exist, try to
3123 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3124 if creation fails. */
3127 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3133 dname = MIPS_ELF_REL_DYN_NAME (info);
3134 dynobj = elf_hash_table (info)->dynobj;
3135 sreloc = bfd_get_linker_section (dynobj, dname);
3136 if (sreloc == NULL && create_p)
3138 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3143 | SEC_LINKER_CREATED
3146 || ! bfd_set_section_alignment (dynobj, sreloc,
3147 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3153 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3156 mips_elf_reloc_tls_type (unsigned int r_type)
3158 if (tls_gd_reloc_p (r_type))
3161 if (tls_ldm_reloc_p (r_type))
3164 if (tls_gottprel_reloc_p (r_type))
3167 return GOT_TLS_NONE;
3170 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3173 mips_tls_got_entries (unsigned int type)
3190 /* Count the number of relocations needed for a TLS GOT entry, with
3191 access types from TLS_TYPE, and symbol H (or a local symbol if H
3195 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3196 struct elf_link_hash_entry *h)
3199 bfd_boolean need_relocs = FALSE;
3200 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3202 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3203 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3206 if ((info->shared || indx != 0)
3208 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3209 || h->root.type != bfd_link_hash_undefweak))
3218 return indx != 0 ? 2 : 1;
3224 return info->shared ? 1 : 0;
3231 /* Add the number of GOT entries and TLS relocations required by ENTRY
3235 mips_elf_count_got_entry (struct bfd_link_info *info,
3236 struct mips_got_info *g,
3237 struct mips_got_entry *entry)
3239 if (entry->tls_type)
3241 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3242 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3244 ? &entry->d.h->root : NULL);
3246 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3247 g->local_gotno += 1;
3249 g->global_gotno += 1;
3252 /* Output a simple dynamic relocation into SRELOC. */
3255 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3257 unsigned long reloc_index,
3262 Elf_Internal_Rela rel[3];
3264 memset (rel, 0, sizeof (rel));
3266 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3267 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3269 if (ABI_64_P (output_bfd))
3271 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3272 (output_bfd, &rel[0],
3274 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3277 bfd_elf32_swap_reloc_out
3278 (output_bfd, &rel[0],
3280 + reloc_index * sizeof (Elf32_External_Rel)));
3283 /* Initialize a set of TLS GOT entries for one symbol. */
3286 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3287 struct mips_got_entry *entry,
3288 struct mips_elf_link_hash_entry *h,
3291 struct mips_elf_link_hash_table *htab;
3293 asection *sreloc, *sgot;
3294 bfd_vma got_offset, got_offset2;
3295 bfd_boolean need_relocs = FALSE;
3297 htab = mips_elf_hash_table (info);
3306 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3308 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3309 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3310 indx = h->root.dynindx;
3313 if (entry->tls_initialized)
3316 if ((info->shared || indx != 0)
3318 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3319 || h->root.type != bfd_link_hash_undefweak))
3322 /* MINUS_ONE means the symbol is not defined in this object. It may not
3323 be defined at all; assume that the value doesn't matter in that
3324 case. Otherwise complain if we would use the value. */
3325 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3326 || h->root.root.type == bfd_link_hash_undefweak);
3328 /* Emit necessary relocations. */
3329 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3330 got_offset = entry->gotidx;
3332 switch (entry->tls_type)
3335 /* General Dynamic. */
3336 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3340 mips_elf_output_dynamic_relocation
3341 (abfd, sreloc, sreloc->reloc_count++, indx,
3342 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3343 sgot->output_offset + sgot->output_section->vma + got_offset);
3346 mips_elf_output_dynamic_relocation
3347 (abfd, sreloc, sreloc->reloc_count++, indx,
3348 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3349 sgot->output_offset + sgot->output_section->vma + got_offset2);
3351 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3352 sgot->contents + got_offset2);
3356 MIPS_ELF_PUT_WORD (abfd, 1,
3357 sgot->contents + got_offset);
3358 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3359 sgot->contents + got_offset2);
3364 /* Initial Exec model. */
3368 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3369 sgot->contents + got_offset);
3371 MIPS_ELF_PUT_WORD (abfd, 0,
3372 sgot->contents + got_offset);
3374 mips_elf_output_dynamic_relocation
3375 (abfd, sreloc, sreloc->reloc_count++, indx,
3376 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3377 sgot->output_offset + sgot->output_section->vma + got_offset);
3380 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3381 sgot->contents + got_offset);
3385 /* The initial offset is zero, and the LD offsets will include the
3386 bias by DTP_OFFSET. */
3387 MIPS_ELF_PUT_WORD (abfd, 0,
3388 sgot->contents + got_offset
3389 + MIPS_ELF_GOT_SIZE (abfd));
3392 MIPS_ELF_PUT_WORD (abfd, 1,
3393 sgot->contents + got_offset);
3395 mips_elf_output_dynamic_relocation
3396 (abfd, sreloc, sreloc->reloc_count++, indx,
3397 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3398 sgot->output_offset + sgot->output_section->vma + got_offset);
3405 entry->tls_initialized = TRUE;
3408 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3409 for global symbol H. .got.plt comes before the GOT, so the offset
3410 will be negative. */
3413 mips_elf_gotplt_index (struct bfd_link_info *info,
3414 struct elf_link_hash_entry *h)
3416 bfd_vma got_address, got_value;
3417 struct mips_elf_link_hash_table *htab;
3419 htab = mips_elf_hash_table (info);
3420 BFD_ASSERT (htab != NULL);
3422 BFD_ASSERT (h->plt.plist != NULL);
3423 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3425 /* Calculate the address of the associated .got.plt entry. */
3426 got_address = (htab->sgotplt->output_section->vma
3427 + htab->sgotplt->output_offset
3428 + (h->plt.plist->gotplt_index
3429 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3431 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3432 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3433 + htab->root.hgot->root.u.def.section->output_offset
3434 + htab->root.hgot->root.u.def.value);
3436 return got_address - got_value;
3439 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3440 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3441 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3442 offset can be found. */
3445 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3446 bfd_vma value, unsigned long r_symndx,
3447 struct mips_elf_link_hash_entry *h, int r_type)
3449 struct mips_elf_link_hash_table *htab;
3450 struct mips_got_entry *entry;
3452 htab = mips_elf_hash_table (info);
3453 BFD_ASSERT (htab != NULL);
3455 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3456 r_symndx, h, r_type);
3460 if (entry->tls_type)
3461 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3462 return entry->gotidx;
3465 /* Return the GOT index of global symbol H in the primary GOT. */
3468 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3469 struct elf_link_hash_entry *h)
3471 struct mips_elf_link_hash_table *htab;
3472 long global_got_dynindx;
3473 struct mips_got_info *g;
3476 htab = mips_elf_hash_table (info);
3477 BFD_ASSERT (htab != NULL);
3479 global_got_dynindx = 0;
3480 if (htab->global_gotsym != NULL)
3481 global_got_dynindx = htab->global_gotsym->dynindx;
3483 /* Once we determine the global GOT entry with the lowest dynamic
3484 symbol table index, we must put all dynamic symbols with greater
3485 indices into the primary GOT. That makes it easy to calculate the
3487 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3488 g = mips_elf_bfd_got (obfd, FALSE);
3489 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3490 * MIPS_ELF_GOT_SIZE (obfd));
3491 BFD_ASSERT (got_index < htab->sgot->size);
3496 /* Return the GOT index for the global symbol indicated by H, which is
3497 referenced by a relocation of type R_TYPE in IBFD. */
3500 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3501 struct elf_link_hash_entry *h, int r_type)
3503 struct mips_elf_link_hash_table *htab;
3504 struct mips_got_info *g;
3505 struct mips_got_entry lookup, *entry;
3508 htab = mips_elf_hash_table (info);
3509 BFD_ASSERT (htab != NULL);
3511 g = mips_elf_bfd_got (ibfd, FALSE);
3514 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3515 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3516 return mips_elf_primary_global_got_index (obfd, info, h);
3520 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3521 entry = htab_find (g->got_entries, &lookup);
3524 gotidx = entry->gotidx;
3525 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3527 if (lookup.tls_type)
3529 bfd_vma value = MINUS_ONE;
3531 if ((h->root.type == bfd_link_hash_defined
3532 || h->root.type == bfd_link_hash_defweak)
3533 && h->root.u.def.section->output_section)
3534 value = (h->root.u.def.value
3535 + h->root.u.def.section->output_offset
3536 + h->root.u.def.section->output_section->vma);
3538 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3543 /* Find a GOT page entry that points to within 32KB of VALUE. These
3544 entries are supposed to be placed at small offsets in the GOT, i.e.,
3545 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3546 entry could be created. If OFFSETP is nonnull, use it to return the
3547 offset of the GOT entry from VALUE. */
3550 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3551 bfd_vma value, bfd_vma *offsetp)
3553 bfd_vma page, got_index;
3554 struct mips_got_entry *entry;
3556 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3557 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3558 NULL, R_MIPS_GOT_PAGE);
3563 got_index = entry->gotidx;
3566 *offsetp = value - entry->d.address;
3571 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3572 EXTERNAL is true if the relocation was originally against a global
3573 symbol that binds locally. */
3576 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3577 bfd_vma value, bfd_boolean external)
3579 struct mips_got_entry *entry;
3581 /* GOT16 relocations against local symbols are followed by a LO16
3582 relocation; those against global symbols are not. Thus if the
3583 symbol was originally local, the GOT16 relocation should load the
3584 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3586 value = mips_elf_high (value) << 16;
3588 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3589 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3590 same in all cases. */
3591 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3592 NULL, R_MIPS_GOT16);
3594 return entry->gotidx;
3599 /* Returns the offset for the entry at the INDEXth position
3603 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3604 bfd *input_bfd, bfd_vma got_index)
3606 struct mips_elf_link_hash_table *htab;
3610 htab = mips_elf_hash_table (info);
3611 BFD_ASSERT (htab != NULL);
3614 gp = _bfd_get_gp_value (output_bfd)
3615 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3617 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3620 /* Create and return a local GOT entry for VALUE, which was calculated
3621 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3622 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3625 static struct mips_got_entry *
3626 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3627 bfd *ibfd, bfd_vma value,
3628 unsigned long r_symndx,
3629 struct mips_elf_link_hash_entry *h,
3632 struct mips_got_entry lookup, *entry;
3634 struct mips_got_info *g;
3635 struct mips_elf_link_hash_table *htab;
3638 htab = mips_elf_hash_table (info);
3639 BFD_ASSERT (htab != NULL);
3641 g = mips_elf_bfd_got (ibfd, FALSE);
3644 g = mips_elf_bfd_got (abfd, FALSE);
3645 BFD_ASSERT (g != NULL);
3648 /* This function shouldn't be called for symbols that live in the global
3650 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3652 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3653 if (lookup.tls_type)
3656 if (tls_ldm_reloc_p (r_type))
3659 lookup.d.addend = 0;
3663 lookup.symndx = r_symndx;
3664 lookup.d.addend = 0;
3672 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3675 gotidx = entry->gotidx;
3676 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3683 lookup.d.address = value;
3684 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3688 entry = (struct mips_got_entry *) *loc;
3692 if (g->assigned_low_gotno > g->assigned_high_gotno)
3694 /* We didn't allocate enough space in the GOT. */
3695 (*_bfd_error_handler)
3696 (_("not enough GOT space for local GOT entries"));
3697 bfd_set_error (bfd_error_bad_value);
3701 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3705 if (got16_reloc_p (r_type)
3706 || call16_reloc_p (r_type)
3707 || got_page_reloc_p (r_type)
3708 || got_disp_reloc_p (r_type))
3709 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3711 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3716 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
3718 /* These GOT entries need a dynamic relocation on VxWorks. */
3719 if (htab->is_vxworks)
3721 Elf_Internal_Rela outrel;
3724 bfd_vma got_address;
3726 s = mips_elf_rel_dyn_section (info, FALSE);
3727 got_address = (htab->sgot->output_section->vma
3728 + htab->sgot->output_offset
3731 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3732 outrel.r_offset = got_address;
3733 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3734 outrel.r_addend = value;
3735 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3741 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3742 The number might be exact or a worst-case estimate, depending on how
3743 much information is available to elf_backend_omit_section_dynsym at
3744 the current linking stage. */
3746 static bfd_size_type
3747 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3749 bfd_size_type count;
3752 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3755 const struct elf_backend_data *bed;
3757 bed = get_elf_backend_data (output_bfd);
3758 for (p = output_bfd->sections; p ; p = p->next)
3759 if ((p->flags & SEC_EXCLUDE) == 0
3760 && (p->flags & SEC_ALLOC) != 0
3761 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3767 /* Sort the dynamic symbol table so that symbols that need GOT entries
3768 appear towards the end. */
3771 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3773 struct mips_elf_link_hash_table *htab;
3774 struct mips_elf_hash_sort_data hsd;
3775 struct mips_got_info *g;
3777 if (elf_hash_table (info)->dynsymcount == 0)
3780 htab = mips_elf_hash_table (info);
3781 BFD_ASSERT (htab != NULL);
3788 hsd.max_unref_got_dynindx
3789 = hsd.min_got_dynindx
3790 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3791 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3792 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3793 elf_hash_table (info)),
3794 mips_elf_sort_hash_table_f,
3797 /* There should have been enough room in the symbol table to
3798 accommodate both the GOT and non-GOT symbols. */
3799 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3800 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3801 == elf_hash_table (info)->dynsymcount);
3802 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3803 == g->global_gotno);
3805 /* Now we know which dynamic symbol has the lowest dynamic symbol
3806 table index in the GOT. */
3807 htab->global_gotsym = hsd.low;
3812 /* If H needs a GOT entry, assign it the highest available dynamic
3813 index. Otherwise, assign it the lowest available dynamic
3817 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3819 struct mips_elf_hash_sort_data *hsd = data;
3821 /* Symbols without dynamic symbol table entries aren't interesting
3823 if (h->root.dynindx == -1)
3826 switch (h->global_got_area)
3829 h->root.dynindx = hsd->max_non_got_dynindx++;
3833 h->root.dynindx = --hsd->min_got_dynindx;
3834 hsd->low = (struct elf_link_hash_entry *) h;
3837 case GGA_RELOC_ONLY:
3838 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3839 hsd->low = (struct elf_link_hash_entry *) h;
3840 h->root.dynindx = hsd->max_unref_got_dynindx++;
3847 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3848 (which is owned by the caller and shouldn't be added to the
3849 hash table directly). */
3852 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3853 struct mips_got_entry *lookup)
3855 struct mips_elf_link_hash_table *htab;
3856 struct mips_got_entry *entry;
3857 struct mips_got_info *g;
3858 void **loc, **bfd_loc;
3860 /* Make sure there's a slot for this entry in the master GOT. */
3861 htab = mips_elf_hash_table (info);
3863 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3867 /* Populate the entry if it isn't already. */
3868 entry = (struct mips_got_entry *) *loc;
3871 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3875 lookup->tls_initialized = FALSE;
3876 lookup->gotidx = -1;
3881 /* Reuse the same GOT entry for the BFD's GOT. */
3882 g = mips_elf_bfd_got (abfd, TRUE);
3886 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3895 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3896 entry for it. FOR_CALL is true if the caller is only interested in
3897 using the GOT entry for calls. */
3900 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3901 bfd *abfd, struct bfd_link_info *info,
3902 bfd_boolean for_call, int r_type)
3904 struct mips_elf_link_hash_table *htab;
3905 struct mips_elf_link_hash_entry *hmips;
3906 struct mips_got_entry entry;
3907 unsigned char tls_type;
3909 htab = mips_elf_hash_table (info);
3910 BFD_ASSERT (htab != NULL);
3912 hmips = (struct mips_elf_link_hash_entry *) h;
3914 hmips->got_only_for_calls = FALSE;
3916 /* A global symbol in the GOT must also be in the dynamic symbol
3918 if (h->dynindx == -1)
3920 switch (ELF_ST_VISIBILITY (h->other))
3924 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3927 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3931 tls_type = mips_elf_reloc_tls_type (r_type);
3932 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
3933 hmips->global_got_area = GGA_NORMAL;
3937 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3938 entry.tls_type = tls_type;
3939 return mips_elf_record_got_entry (info, abfd, &entry);
3942 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3943 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3946 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3947 struct bfd_link_info *info, int r_type)
3949 struct mips_elf_link_hash_table *htab;
3950 struct mips_got_info *g;
3951 struct mips_got_entry entry;
3953 htab = mips_elf_hash_table (info);
3954 BFD_ASSERT (htab != NULL);
3957 BFD_ASSERT (g != NULL);
3960 entry.symndx = symndx;
3961 entry.d.addend = addend;
3962 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3963 return mips_elf_record_got_entry (info, abfd, &entry);
3966 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3967 H is the symbol's hash table entry, or null if SYMNDX is local
3971 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3972 long symndx, struct elf_link_hash_entry *h,
3973 bfd_signed_vma addend)
3975 struct mips_elf_link_hash_table *htab;
3976 struct mips_got_info *g1, *g2;
3977 struct mips_got_page_ref lookup, *entry;
3978 void **loc, **bfd_loc;
3980 htab = mips_elf_hash_table (info);
3981 BFD_ASSERT (htab != NULL);
3983 g1 = htab->got_info;
3984 BFD_ASSERT (g1 != NULL);
3989 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3993 lookup.symndx = symndx;
3994 lookup.u.abfd = abfd;
3996 lookup.addend = addend;
3997 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4001 entry = (struct mips_got_page_ref *) *loc;
4004 entry = bfd_alloc (abfd, sizeof (*entry));
4012 /* Add the same entry to the BFD's GOT. */
4013 g2 = mips_elf_bfd_got (abfd, TRUE);
4017 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4027 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4030 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4034 struct mips_elf_link_hash_table *htab;
4036 htab = mips_elf_hash_table (info);
4037 BFD_ASSERT (htab != NULL);
4039 s = mips_elf_rel_dyn_section (info, FALSE);
4040 BFD_ASSERT (s != NULL);
4042 if (htab->is_vxworks)
4043 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4048 /* Make room for a null element. */
4049 s->size += MIPS_ELF_REL_SIZE (abfd);
4052 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4056 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4057 mips_elf_traverse_got_arg structure. Count the number of GOT
4058 entries and TLS relocs. Set DATA->value to true if we need
4059 to resolve indirect or warning symbols and then recreate the GOT. */
4062 mips_elf_check_recreate_got (void **entryp, void *data)
4064 struct mips_got_entry *entry;
4065 struct mips_elf_traverse_got_arg *arg;
4067 entry = (struct mips_got_entry *) *entryp;
4068 arg = (struct mips_elf_traverse_got_arg *) data;
4069 if (entry->abfd != NULL && entry->symndx == -1)
4071 struct mips_elf_link_hash_entry *h;
4074 if (h->root.root.type == bfd_link_hash_indirect
4075 || h->root.root.type == bfd_link_hash_warning)
4081 mips_elf_count_got_entry (arg->info, arg->g, entry);
4085 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4086 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4087 converting entries for indirect and warning symbols into entries
4088 for the target symbol. Set DATA->g to null on error. */
4091 mips_elf_recreate_got (void **entryp, void *data)
4093 struct mips_got_entry new_entry, *entry;
4094 struct mips_elf_traverse_got_arg *arg;
4097 entry = (struct mips_got_entry *) *entryp;
4098 arg = (struct mips_elf_traverse_got_arg *) data;
4099 if (entry->abfd != NULL
4100 && entry->symndx == -1
4101 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4102 || entry->d.h->root.root.type == bfd_link_hash_warning))
4104 struct mips_elf_link_hash_entry *h;
4111 BFD_ASSERT (h->global_got_area == GGA_NONE);
4112 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4114 while (h->root.root.type == bfd_link_hash_indirect
4115 || h->root.root.type == bfd_link_hash_warning);
4118 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4126 if (entry == &new_entry)
4128 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4137 mips_elf_count_got_entry (arg->info, arg->g, entry);
4142 /* Return the maximum number of GOT page entries required for RANGE. */
4145 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4147 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4150 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4153 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4154 asection *sec, bfd_signed_vma addend)
4156 struct mips_got_info *g = arg->g;
4157 struct mips_got_page_entry lookup, *entry;
4158 struct mips_got_page_range **range_ptr, *range;
4159 bfd_vma old_pages, new_pages;
4162 /* Find the mips_got_page_entry hash table entry for this section. */
4164 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4168 /* Create a mips_got_page_entry if this is the first time we've
4169 seen the section. */
4170 entry = (struct mips_got_page_entry *) *loc;
4173 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4181 /* Skip over ranges whose maximum extent cannot share a page entry
4183 range_ptr = &entry->ranges;
4184 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4185 range_ptr = &(*range_ptr)->next;
4187 /* If we scanned to the end of the list, or found a range whose
4188 minimum extent cannot share a page entry with ADDEND, create
4189 a new singleton range. */
4191 if (!range || addend < range->min_addend - 0xffff)
4193 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4197 range->next = *range_ptr;
4198 range->min_addend = addend;
4199 range->max_addend = addend;
4207 /* Remember how many pages the old range contributed. */
4208 old_pages = mips_elf_pages_for_range (range);
4210 /* Update the ranges. */
4211 if (addend < range->min_addend)
4212 range->min_addend = addend;
4213 else if (addend > range->max_addend)
4215 if (range->next && addend >= range->next->min_addend - 0xffff)
4217 old_pages += mips_elf_pages_for_range (range->next);
4218 range->max_addend = range->next->max_addend;
4219 range->next = range->next->next;
4222 range->max_addend = addend;
4225 /* Record any change in the total estimate. */
4226 new_pages = mips_elf_pages_for_range (range);
4227 if (old_pages != new_pages)
4229 entry->num_pages += new_pages - old_pages;
4230 g->page_gotno += new_pages - old_pages;
4236 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4237 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4238 whether the page reference described by *REFP needs a GOT page entry,
4239 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4242 mips_elf_resolve_got_page_ref (void **refp, void *data)
4244 struct mips_got_page_ref *ref;
4245 struct mips_elf_traverse_got_arg *arg;
4246 struct mips_elf_link_hash_table *htab;
4250 ref = (struct mips_got_page_ref *) *refp;
4251 arg = (struct mips_elf_traverse_got_arg *) data;
4252 htab = mips_elf_hash_table (arg->info);
4254 if (ref->symndx < 0)
4256 struct mips_elf_link_hash_entry *h;
4258 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4260 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4263 /* Ignore undefined symbols; we'll issue an error later if
4265 if (!((h->root.root.type == bfd_link_hash_defined
4266 || h->root.root.type == bfd_link_hash_defweak)
4267 && h->root.root.u.def.section))
4270 sec = h->root.root.u.def.section;
4271 addend = h->root.root.u.def.value + ref->addend;
4275 Elf_Internal_Sym *isym;
4277 /* Read in the symbol. */
4278 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4286 /* Get the associated input section. */
4287 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4294 /* If this is a mergable section, work out the section and offset
4295 of the merged data. For section symbols, the addend specifies
4296 of the offset _of_ the first byte in the data, otherwise it
4297 specifies the offset _from_ the first byte. */
4298 if (sec->flags & SEC_MERGE)
4302 secinfo = elf_section_data (sec)->sec_info;
4303 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4304 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4305 isym->st_value + ref->addend);
4307 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4308 isym->st_value) + ref->addend;
4311 addend = isym->st_value + ref->addend;
4313 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4321 /* If any entries in G->got_entries are for indirect or warning symbols,
4322 replace them with entries for the target symbol. Convert g->got_page_refs
4323 into got_page_entry structures and estimate the number of page entries
4324 that they require. */
4327 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4328 struct mips_got_info *g)
4330 struct mips_elf_traverse_got_arg tga;
4331 struct mips_got_info oldg;
4338 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4342 g->got_entries = htab_create (htab_size (oldg.got_entries),
4343 mips_elf_got_entry_hash,
4344 mips_elf_got_entry_eq, NULL);
4345 if (!g->got_entries)
4348 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4352 htab_delete (oldg.got_entries);
4355 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4356 mips_got_page_entry_eq, NULL);
4357 if (g->got_page_entries == NULL)
4362 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4367 /* Return true if a GOT entry for H should live in the local rather than
4371 mips_use_local_got_p (struct bfd_link_info *info,
4372 struct mips_elf_link_hash_entry *h)
4374 /* Symbols that aren't in the dynamic symbol table must live in the
4375 local GOT. This includes symbols that are completely undefined
4376 and which therefore don't bind locally. We'll report undefined
4377 symbols later if appropriate. */
4378 if (h->root.dynindx == -1)
4381 /* Symbols that bind locally can (and in the case of forced-local
4382 symbols, must) live in the local GOT. */
4383 if (h->got_only_for_calls
4384 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4385 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4388 /* If this is an executable that must provide a definition of the symbol,
4389 either though PLTs or copy relocations, then that address should go in
4390 the local rather than global GOT. */
4391 if (info->executable && h->has_static_relocs)
4397 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4398 link_info structure. Decide whether the hash entry needs an entry in
4399 the global part of the primary GOT, setting global_got_area accordingly.
4400 Count the number of global symbols that are in the primary GOT only
4401 because they have relocations against them (reloc_only_gotno). */
4404 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4406 struct bfd_link_info *info;
4407 struct mips_elf_link_hash_table *htab;
4408 struct mips_got_info *g;
4410 info = (struct bfd_link_info *) data;
4411 htab = mips_elf_hash_table (info);
4413 if (h->global_got_area != GGA_NONE)
4415 /* Make a final decision about whether the symbol belongs in the
4416 local or global GOT. */
4417 if (mips_use_local_got_p (info, h))
4418 /* The symbol belongs in the local GOT. We no longer need this
4419 entry if it was only used for relocations; those relocations
4420 will be against the null or section symbol instead of H. */
4421 h->global_got_area = GGA_NONE;
4422 else if (htab->is_vxworks
4423 && h->got_only_for_calls
4424 && h->root.plt.plist->mips_offset != MINUS_ONE)
4425 /* On VxWorks, calls can refer directly to the .got.plt entry;
4426 they don't need entries in the regular GOT. .got.plt entries
4427 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4428 h->global_got_area = GGA_NONE;
4429 else if (h->global_got_area == GGA_RELOC_ONLY)
4431 g->reloc_only_gotno++;
4438 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4439 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4442 mips_elf_add_got_entry (void **entryp, void *data)
4444 struct mips_got_entry *entry;
4445 struct mips_elf_traverse_got_arg *arg;
4448 entry = (struct mips_got_entry *) *entryp;
4449 arg = (struct mips_elf_traverse_got_arg *) data;
4450 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4459 mips_elf_count_got_entry (arg->info, arg->g, entry);
4464 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4465 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4468 mips_elf_add_got_page_entry (void **entryp, void *data)
4470 struct mips_got_page_entry *entry;
4471 struct mips_elf_traverse_got_arg *arg;
4474 entry = (struct mips_got_page_entry *) *entryp;
4475 arg = (struct mips_elf_traverse_got_arg *) data;
4476 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4485 arg->g->page_gotno += entry->num_pages;
4490 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4491 this would lead to overflow, 1 if they were merged successfully,
4492 and 0 if a merge failed due to lack of memory. (These values are chosen
4493 so that nonnegative return values can be returned by a htab_traverse
4497 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4498 struct mips_got_info *to,
4499 struct mips_elf_got_per_bfd_arg *arg)
4501 struct mips_elf_traverse_got_arg tga;
4502 unsigned int estimate;
4504 /* Work out how many page entries we would need for the combined GOT. */
4505 estimate = arg->max_pages;
4506 if (estimate >= from->page_gotno + to->page_gotno)
4507 estimate = from->page_gotno + to->page_gotno;
4509 /* And conservatively estimate how many local and TLS entries
4511 estimate += from->local_gotno + to->local_gotno;
4512 estimate += from->tls_gotno + to->tls_gotno;
4514 /* If we're merging with the primary got, any TLS relocations will
4515 come after the full set of global entries. Otherwise estimate those
4516 conservatively as well. */
4517 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4518 estimate += arg->global_count;
4520 estimate += from->global_gotno + to->global_gotno;
4522 /* Bail out if the combined GOT might be too big. */
4523 if (estimate > arg->max_count)
4526 /* Transfer the bfd's got information from FROM to TO. */
4527 tga.info = arg->info;
4529 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4533 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4537 mips_elf_replace_bfd_got (abfd, to);
4541 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4542 as possible of the primary got, since it doesn't require explicit
4543 dynamic relocations, but don't use bfds that would reference global
4544 symbols out of the addressable range. Failing the primary got,
4545 attempt to merge with the current got, or finish the current got
4546 and then make make the new got current. */
4549 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4550 struct mips_elf_got_per_bfd_arg *arg)
4552 unsigned int estimate;
4555 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4558 /* Work out the number of page, local and TLS entries. */
4559 estimate = arg->max_pages;
4560 if (estimate > g->page_gotno)
4561 estimate = g->page_gotno;
4562 estimate += g->local_gotno + g->tls_gotno;
4564 /* We place TLS GOT entries after both locals and globals. The globals
4565 for the primary GOT may overflow the normal GOT size limit, so be
4566 sure not to merge a GOT which requires TLS with the primary GOT in that
4567 case. This doesn't affect non-primary GOTs. */
4568 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4570 if (estimate <= arg->max_count)
4572 /* If we don't have a primary GOT, use it as
4573 a starting point for the primary GOT. */
4580 /* Try merging with the primary GOT. */
4581 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4586 /* If we can merge with the last-created got, do it. */
4589 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4594 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4595 fits; if it turns out that it doesn't, we'll get relocation
4596 overflows anyway. */
4597 g->next = arg->current;
4603 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4604 to GOTIDX, duplicating the entry if it has already been assigned
4605 an index in a different GOT. */
4608 mips_elf_set_gotidx (void **entryp, long gotidx)
4610 struct mips_got_entry *entry;
4612 entry = (struct mips_got_entry *) *entryp;
4613 if (entry->gotidx > 0)
4615 struct mips_got_entry *new_entry;
4617 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4621 *new_entry = *entry;
4622 *entryp = new_entry;
4625 entry->gotidx = gotidx;
4629 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4630 mips_elf_traverse_got_arg in which DATA->value is the size of one
4631 GOT entry. Set DATA->g to null on failure. */
4634 mips_elf_initialize_tls_index (void **entryp, void *data)
4636 struct mips_got_entry *entry;
4637 struct mips_elf_traverse_got_arg *arg;
4639 /* We're only interested in TLS symbols. */
4640 entry = (struct mips_got_entry *) *entryp;
4641 if (entry->tls_type == GOT_TLS_NONE)
4644 arg = (struct mips_elf_traverse_got_arg *) data;
4645 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4651 /* Account for the entries we've just allocated. */
4652 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4656 /* A htab_traverse callback for GOT entries, where DATA points to a
4657 mips_elf_traverse_got_arg. Set the global_got_area of each global
4658 symbol to DATA->value. */
4661 mips_elf_set_global_got_area (void **entryp, void *data)
4663 struct mips_got_entry *entry;
4664 struct mips_elf_traverse_got_arg *arg;
4666 entry = (struct mips_got_entry *) *entryp;
4667 arg = (struct mips_elf_traverse_got_arg *) data;
4668 if (entry->abfd != NULL
4669 && entry->symndx == -1
4670 && entry->d.h->global_got_area != GGA_NONE)
4671 entry->d.h->global_got_area = arg->value;
4675 /* A htab_traverse callback for secondary GOT entries, where DATA points
4676 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4677 and record the number of relocations they require. DATA->value is
4678 the size of one GOT entry. Set DATA->g to null on failure. */
4681 mips_elf_set_global_gotidx (void **entryp, void *data)
4683 struct mips_got_entry *entry;
4684 struct mips_elf_traverse_got_arg *arg;
4686 entry = (struct mips_got_entry *) *entryp;
4687 arg = (struct mips_elf_traverse_got_arg *) data;
4688 if (entry->abfd != NULL
4689 && entry->symndx == -1
4690 && entry->d.h->global_got_area != GGA_NONE)
4692 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4697 arg->g->assigned_low_gotno += 1;
4699 if (arg->info->shared
4700 || (elf_hash_table (arg->info)->dynamic_sections_created
4701 && entry->d.h->root.def_dynamic
4702 && !entry->d.h->root.def_regular))
4703 arg->g->relocs += 1;
4709 /* A htab_traverse callback for GOT entries for which DATA is the
4710 bfd_link_info. Forbid any global symbols from having traditional
4711 lazy-binding stubs. */
4714 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4716 struct bfd_link_info *info;
4717 struct mips_elf_link_hash_table *htab;
4718 struct mips_got_entry *entry;
4720 entry = (struct mips_got_entry *) *entryp;
4721 info = (struct bfd_link_info *) data;
4722 htab = mips_elf_hash_table (info);
4723 BFD_ASSERT (htab != NULL);
4725 if (entry->abfd != NULL
4726 && entry->symndx == -1
4727 && entry->d.h->needs_lazy_stub)
4729 entry->d.h->needs_lazy_stub = FALSE;
4730 htab->lazy_stub_count--;
4736 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4739 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4744 g = mips_elf_bfd_got (ibfd, FALSE);
4748 BFD_ASSERT (g->next);
4752 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4753 * MIPS_ELF_GOT_SIZE (abfd);
4756 /* Turn a single GOT that is too big for 16-bit addressing into
4757 a sequence of GOTs, each one 16-bit addressable. */
4760 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4761 asection *got, bfd_size_type pages)
4763 struct mips_elf_link_hash_table *htab;
4764 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4765 struct mips_elf_traverse_got_arg tga;
4766 struct mips_got_info *g, *gg;
4767 unsigned int assign, needed_relocs;
4770 dynobj = elf_hash_table (info)->dynobj;
4771 htab = mips_elf_hash_table (info);
4772 BFD_ASSERT (htab != NULL);
4776 got_per_bfd_arg.obfd = abfd;
4777 got_per_bfd_arg.info = info;
4778 got_per_bfd_arg.current = NULL;
4779 got_per_bfd_arg.primary = NULL;
4780 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4781 / MIPS_ELF_GOT_SIZE (abfd))
4782 - htab->reserved_gotno);
4783 got_per_bfd_arg.max_pages = pages;
4784 /* The number of globals that will be included in the primary GOT.
4785 See the calls to mips_elf_set_global_got_area below for more
4787 got_per_bfd_arg.global_count = g->global_gotno;
4789 /* Try to merge the GOTs of input bfds together, as long as they
4790 don't seem to exceed the maximum GOT size, choosing one of them
4791 to be the primary GOT. */
4792 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4794 gg = mips_elf_bfd_got (ibfd, FALSE);
4795 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4799 /* If we do not find any suitable primary GOT, create an empty one. */
4800 if (got_per_bfd_arg.primary == NULL)
4801 g->next = mips_elf_create_got_info (abfd);
4803 g->next = got_per_bfd_arg.primary;
4804 g->next->next = got_per_bfd_arg.current;
4806 /* GG is now the master GOT, and G is the primary GOT. */
4810 /* Map the output bfd to the primary got. That's what we're going
4811 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4812 didn't mark in check_relocs, and we want a quick way to find it.
4813 We can't just use gg->next because we're going to reverse the
4815 mips_elf_replace_bfd_got (abfd, g);
4817 /* Every symbol that is referenced in a dynamic relocation must be
4818 present in the primary GOT, so arrange for them to appear after
4819 those that are actually referenced. */
4820 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4821 g->global_gotno = gg->global_gotno;
4824 tga.value = GGA_RELOC_ONLY;
4825 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4826 tga.value = GGA_NORMAL;
4827 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4829 /* Now go through the GOTs assigning them offset ranges.
4830 [assigned_low_gotno, local_gotno[ will be set to the range of local
4831 entries in each GOT. We can then compute the end of a GOT by
4832 adding local_gotno to global_gotno. We reverse the list and make
4833 it circular since then we'll be able to quickly compute the
4834 beginning of a GOT, by computing the end of its predecessor. To
4835 avoid special cases for the primary GOT, while still preserving
4836 assertions that are valid for both single- and multi-got links,
4837 we arrange for the main got struct to have the right number of
4838 global entries, but set its local_gotno such that the initial
4839 offset of the primary GOT is zero. Remember that the primary GOT
4840 will become the last item in the circular linked list, so it
4841 points back to the master GOT. */
4842 gg->local_gotno = -g->global_gotno;
4843 gg->global_gotno = g->global_gotno;
4850 struct mips_got_info *gn;
4852 assign += htab->reserved_gotno;
4853 g->assigned_low_gotno = assign;
4854 g->local_gotno += assign;
4855 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4856 g->assigned_high_gotno = g->local_gotno - 1;
4857 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4859 /* Take g out of the direct list, and push it onto the reversed
4860 list that gg points to. g->next is guaranteed to be nonnull after
4861 this operation, as required by mips_elf_initialize_tls_index. */
4866 /* Set up any TLS entries. We always place the TLS entries after
4867 all non-TLS entries. */
4868 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4870 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4871 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4874 BFD_ASSERT (g->tls_assigned_gotno == assign);
4876 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4879 /* Forbid global symbols in every non-primary GOT from having
4880 lazy-binding stubs. */
4882 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4886 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4889 for (g = gg->next; g && g->next != gg; g = g->next)
4891 unsigned int save_assign;
4893 /* Assign offsets to global GOT entries and count how many
4894 relocations they need. */
4895 save_assign = g->assigned_low_gotno;
4896 g->assigned_low_gotno = g->local_gotno;
4898 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4900 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4903 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4904 g->assigned_low_gotno = save_assign;
4908 g->relocs += g->local_gotno - g->assigned_low_gotno;
4909 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
4910 + g->next->global_gotno
4911 + g->next->tls_gotno
4912 + htab->reserved_gotno);
4914 needed_relocs += g->relocs;
4916 needed_relocs += g->relocs;
4919 mips_elf_allocate_dynamic_relocations (dynobj, info,
4926 /* Returns the first relocation of type r_type found, beginning with
4927 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4929 static const Elf_Internal_Rela *
4930 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4931 const Elf_Internal_Rela *relocation,
4932 const Elf_Internal_Rela *relend)
4934 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4936 while (relocation < relend)
4938 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4939 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4945 /* We didn't find it. */
4949 /* Return whether an input relocation is against a local symbol. */
4952 mips_elf_local_relocation_p (bfd *input_bfd,
4953 const Elf_Internal_Rela *relocation,
4954 asection **local_sections)
4956 unsigned long r_symndx;
4957 Elf_Internal_Shdr *symtab_hdr;
4960 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4961 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4962 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4964 if (r_symndx < extsymoff)
4966 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4972 /* Sign-extend VALUE, which has the indicated number of BITS. */
4975 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4977 if (value & ((bfd_vma) 1 << (bits - 1)))
4978 /* VALUE is negative. */
4979 value |= ((bfd_vma) - 1) << bits;
4984 /* Return non-zero if the indicated VALUE has overflowed the maximum
4985 range expressible by a signed number with the indicated number of
4989 mips_elf_overflow_p (bfd_vma value, int bits)
4991 bfd_signed_vma svalue = (bfd_signed_vma) value;
4993 if (svalue > (1 << (bits - 1)) - 1)
4994 /* The value is too big. */
4996 else if (svalue < -(1 << (bits - 1)))
4997 /* The value is too small. */
5004 /* Calculate the %high function. */
5007 mips_elf_high (bfd_vma value)
5009 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5012 /* Calculate the %higher function. */
5015 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5018 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5025 /* Calculate the %highest function. */
5028 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5031 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5038 /* Create the .compact_rel section. */
5041 mips_elf_create_compact_rel_section
5042 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5045 register asection *s;
5047 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5049 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5052 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5054 || ! bfd_set_section_alignment (abfd, s,
5055 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5058 s->size = sizeof (Elf32_External_compact_rel);
5064 /* Create the .got section to hold the global offset table. */
5067 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5070 register asection *s;
5071 struct elf_link_hash_entry *h;
5072 struct bfd_link_hash_entry *bh;
5073 struct mips_elf_link_hash_table *htab;
5075 htab = mips_elf_hash_table (info);
5076 BFD_ASSERT (htab != NULL);
5078 /* This function may be called more than once. */
5082 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5083 | SEC_LINKER_CREATED);
5085 /* We have to use an alignment of 2**4 here because this is hardcoded
5086 in the function stub generation and in the linker script. */
5087 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5089 || ! bfd_set_section_alignment (abfd, s, 4))
5093 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5094 linker script because we don't want to define the symbol if we
5095 are not creating a global offset table. */
5097 if (! (_bfd_generic_link_add_one_symbol
5098 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5099 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5102 h = (struct elf_link_hash_entry *) bh;
5105 h->type = STT_OBJECT;
5106 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5107 elf_hash_table (info)->hgot = h;
5110 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5113 htab->got_info = mips_elf_create_got_info (abfd);
5114 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5115 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5117 /* We also need a .got.plt section when generating PLTs. */
5118 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5119 SEC_ALLOC | SEC_LOAD
5122 | SEC_LINKER_CREATED);
5130 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5131 __GOTT_INDEX__ symbols. These symbols are only special for
5132 shared objects; they are not used in executables. */
5135 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5137 return (mips_elf_hash_table (info)->is_vxworks
5139 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5140 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5143 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5144 require an la25 stub. See also mips_elf_local_pic_function_p,
5145 which determines whether the destination function ever requires a
5149 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5150 bfd_boolean target_is_16_bit_code_p)
5152 /* We specifically ignore branches and jumps from EF_PIC objects,
5153 where the onus is on the compiler or programmer to perform any
5154 necessary initialization of $25. Sometimes such initialization
5155 is unnecessary; for example, -mno-shared functions do not use
5156 the incoming value of $25, and may therefore be called directly. */
5157 if (PIC_OBJECT_P (input_bfd))
5164 case R_MICROMIPS_26_S1:
5165 case R_MICROMIPS_PC7_S1:
5166 case R_MICROMIPS_PC10_S1:
5167 case R_MICROMIPS_PC16_S1:
5168 case R_MICROMIPS_PC23_S2:
5172 return !target_is_16_bit_code_p;
5179 /* Calculate the value produced by the RELOCATION (which comes from
5180 the INPUT_BFD). The ADDEND is the addend to use for this
5181 RELOCATION; RELOCATION->R_ADDEND is ignored.
5183 The result of the relocation calculation is stored in VALUEP.
5184 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5185 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5187 This function returns bfd_reloc_continue if the caller need take no
5188 further action regarding this relocation, bfd_reloc_notsupported if
5189 something goes dramatically wrong, bfd_reloc_overflow if an
5190 overflow occurs, and bfd_reloc_ok to indicate success. */
5192 static bfd_reloc_status_type
5193 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5194 asection *input_section,
5195 struct bfd_link_info *info,
5196 const Elf_Internal_Rela *relocation,
5197 bfd_vma addend, reloc_howto_type *howto,
5198 Elf_Internal_Sym *local_syms,
5199 asection **local_sections, bfd_vma *valuep,
5201 bfd_boolean *cross_mode_jump_p,
5202 bfd_boolean save_addend)
5204 /* The eventual value we will return. */
5206 /* The address of the symbol against which the relocation is
5209 /* The final GP value to be used for the relocatable, executable, or
5210 shared object file being produced. */
5212 /* The place (section offset or address) of the storage unit being
5215 /* The value of GP used to create the relocatable object. */
5217 /* The offset into the global offset table at which the address of
5218 the relocation entry symbol, adjusted by the addend, resides
5219 during execution. */
5220 bfd_vma g = MINUS_ONE;
5221 /* The section in which the symbol referenced by the relocation is
5223 asection *sec = NULL;
5224 struct mips_elf_link_hash_entry *h = NULL;
5225 /* TRUE if the symbol referred to by this relocation is a local
5227 bfd_boolean local_p, was_local_p;
5228 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5229 bfd_boolean gp_disp_p = FALSE;
5230 /* TRUE if the symbol referred to by this relocation is
5231 "__gnu_local_gp". */
5232 bfd_boolean gnu_local_gp_p = FALSE;
5233 Elf_Internal_Shdr *symtab_hdr;
5235 unsigned long r_symndx;
5237 /* TRUE if overflow occurred during the calculation of the
5238 relocation value. */
5239 bfd_boolean overflowed_p;
5240 /* TRUE if this relocation refers to a MIPS16 function. */
5241 bfd_boolean target_is_16_bit_code_p = FALSE;
5242 bfd_boolean target_is_micromips_code_p = FALSE;
5243 struct mips_elf_link_hash_table *htab;
5246 dynobj = elf_hash_table (info)->dynobj;
5247 htab = mips_elf_hash_table (info);
5248 BFD_ASSERT (htab != NULL);
5250 /* Parse the relocation. */
5251 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5252 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5253 p = (input_section->output_section->vma
5254 + input_section->output_offset
5255 + relocation->r_offset);
5257 /* Assume that there will be no overflow. */
5258 overflowed_p = FALSE;
5260 /* Figure out whether or not the symbol is local, and get the offset
5261 used in the array of hash table entries. */
5262 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5263 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5265 was_local_p = local_p;
5266 if (! elf_bad_symtab (input_bfd))
5267 extsymoff = symtab_hdr->sh_info;
5270 /* The symbol table does not follow the rule that local symbols
5271 must come before globals. */
5275 /* Figure out the value of the symbol. */
5278 Elf_Internal_Sym *sym;
5280 sym = local_syms + r_symndx;
5281 sec = local_sections[r_symndx];
5283 symbol = sec->output_section->vma + sec->output_offset;
5284 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5285 || (sec->flags & SEC_MERGE))
5286 symbol += sym->st_value;
5287 if ((sec->flags & SEC_MERGE)
5288 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5290 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5292 addend += sec->output_section->vma + sec->output_offset;
5295 /* MIPS16/microMIPS text labels should be treated as odd. */
5296 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5299 /* Record the name of this symbol, for our caller. */
5300 *namep = bfd_elf_string_from_elf_section (input_bfd,
5301 symtab_hdr->sh_link,
5304 *namep = bfd_section_name (input_bfd, sec);
5306 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5307 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5311 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5313 /* For global symbols we look up the symbol in the hash-table. */
5314 h = ((struct mips_elf_link_hash_entry *)
5315 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5316 /* Find the real hash-table entry for this symbol. */
5317 while (h->root.root.type == bfd_link_hash_indirect
5318 || h->root.root.type == bfd_link_hash_warning)
5319 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5321 /* Record the name of this symbol, for our caller. */
5322 *namep = h->root.root.root.string;
5324 /* See if this is the special _gp_disp symbol. Note that such a
5325 symbol must always be a global symbol. */
5326 if (strcmp (*namep, "_gp_disp") == 0
5327 && ! NEWABI_P (input_bfd))
5329 /* Relocations against _gp_disp are permitted only with
5330 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5331 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5332 return bfd_reloc_notsupported;
5336 /* See if this is the special _gp symbol. Note that such a
5337 symbol must always be a global symbol. */
5338 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5339 gnu_local_gp_p = TRUE;
5342 /* If this symbol is defined, calculate its address. Note that
5343 _gp_disp is a magic symbol, always implicitly defined by the
5344 linker, so it's inappropriate to check to see whether or not
5346 else if ((h->root.root.type == bfd_link_hash_defined
5347 || h->root.root.type == bfd_link_hash_defweak)
5348 && h->root.root.u.def.section)
5350 sec = h->root.root.u.def.section;
5351 if (sec->output_section)
5352 symbol = (h->root.root.u.def.value
5353 + sec->output_section->vma
5354 + sec->output_offset);
5356 symbol = h->root.root.u.def.value;
5358 else if (h->root.root.type == bfd_link_hash_undefweak)
5359 /* We allow relocations against undefined weak symbols, giving
5360 it the value zero, so that you can undefined weak functions
5361 and check to see if they exist by looking at their
5364 else if (info->unresolved_syms_in_objects == RM_IGNORE
5365 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5367 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5368 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5370 /* If this is a dynamic link, we should have created a
5371 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5372 in in _bfd_mips_elf_create_dynamic_sections.
5373 Otherwise, we should define the symbol with a value of 0.
5374 FIXME: It should probably get into the symbol table
5376 BFD_ASSERT (! info->shared);
5377 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5380 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5382 /* This is an optional symbol - an Irix specific extension to the
5383 ELF spec. Ignore it for now.
5384 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5385 than simply ignoring them, but we do not handle this for now.
5386 For information see the "64-bit ELF Object File Specification"
5387 which is available from here:
5388 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5391 else if ((*info->callbacks->undefined_symbol)
5392 (info, h->root.root.root.string, input_bfd,
5393 input_section, relocation->r_offset,
5394 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5395 || ELF_ST_VISIBILITY (h->root.other)))
5397 return bfd_reloc_undefined;
5401 return bfd_reloc_notsupported;
5404 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5405 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5408 /* If this is a reference to a 16-bit function with a stub, we need
5409 to redirect the relocation to the stub unless:
5411 (a) the relocation is for a MIPS16 JAL;
5413 (b) the relocation is for a MIPS16 PIC call, and there are no
5414 non-MIPS16 uses of the GOT slot; or
5416 (c) the section allows direct references to MIPS16 functions. */
5417 if (r_type != R_MIPS16_26
5418 && !info->relocatable
5420 && h->fn_stub != NULL
5421 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5423 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5424 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5425 && !section_allows_mips16_refs_p (input_section))
5427 /* This is a 32- or 64-bit call to a 16-bit function. We should
5428 have already noticed that we were going to need the
5432 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5437 BFD_ASSERT (h->need_fn_stub);
5440 /* If a LA25 header for the stub itself exists, point to the
5441 prepended LUI/ADDIU sequence. */
5442 sec = h->la25_stub->stub_section;
5443 value = h->la25_stub->offset;
5452 symbol = sec->output_section->vma + sec->output_offset + value;
5453 /* The target is 16-bit, but the stub isn't. */
5454 target_is_16_bit_code_p = FALSE;
5456 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5457 to a standard MIPS function, we need to redirect the call to the stub.
5458 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5459 indirect calls should use an indirect stub instead. */
5460 else if (r_type == R_MIPS16_26 && !info->relocatable
5461 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5463 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5464 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5465 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5468 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5471 /* If both call_stub and call_fp_stub are defined, we can figure
5472 out which one to use by checking which one appears in the input
5474 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5479 for (o = input_bfd->sections; o != NULL; o = o->next)
5481 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5483 sec = h->call_fp_stub;
5490 else if (h->call_stub != NULL)
5493 sec = h->call_fp_stub;
5496 BFD_ASSERT (sec->size > 0);
5497 symbol = sec->output_section->vma + sec->output_offset;
5499 /* If this is a direct call to a PIC function, redirect to the
5501 else if (h != NULL && h->la25_stub
5502 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5503 target_is_16_bit_code_p))
5504 symbol = (h->la25_stub->stub_section->output_section->vma
5505 + h->la25_stub->stub_section->output_offset
5506 + h->la25_stub->offset);
5507 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5508 entry is used if a standard PLT entry has also been made. In this
5509 case the symbol will have been set by mips_elf_set_plt_sym_value
5510 to point to the standard PLT entry, so redirect to the compressed
5512 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5513 && !info->relocatable
5516 && h->root.plt.plist->comp_offset != MINUS_ONE
5517 && h->root.plt.plist->mips_offset != MINUS_ONE)
5519 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5522 symbol = (sec->output_section->vma
5523 + sec->output_offset
5524 + htab->plt_header_size
5525 + htab->plt_mips_offset
5526 + h->root.plt.plist->comp_offset
5529 target_is_16_bit_code_p = !micromips_p;
5530 target_is_micromips_code_p = micromips_p;
5533 /* Make sure MIPS16 and microMIPS are not used together. */
5534 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5535 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5537 (*_bfd_error_handler)
5538 (_("MIPS16 and microMIPS functions cannot call each other"));
5539 return bfd_reloc_notsupported;
5542 /* Calls from 16-bit code to 32-bit code and vice versa require the
5543 mode change. However, we can ignore calls to undefined weak symbols,
5544 which should never be executed at runtime. This exception is important
5545 because the assembly writer may have "known" that any definition of the
5546 symbol would be 16-bit code, and that direct jumps were therefore
5548 *cross_mode_jump_p = (!info->relocatable
5549 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5550 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5551 || (r_type == R_MICROMIPS_26_S1
5552 && !target_is_micromips_code_p)
5553 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5554 && (target_is_16_bit_code_p
5555 || target_is_micromips_code_p))));
5557 local_p = (h == NULL || mips_use_local_got_p (info, h));
5559 gp0 = _bfd_get_gp_value (input_bfd);
5560 gp = _bfd_get_gp_value (abfd);
5562 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5567 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5568 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5569 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5570 if (got_page_reloc_p (r_type) && !local_p)
5572 r_type = (micromips_reloc_p (r_type)
5573 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5577 /* If we haven't already determined the GOT offset, and we're going
5578 to need it, get it now. */
5581 case R_MIPS16_CALL16:
5582 case R_MIPS16_GOT16:
5585 case R_MIPS_GOT_DISP:
5586 case R_MIPS_GOT_HI16:
5587 case R_MIPS_CALL_HI16:
5588 case R_MIPS_GOT_LO16:
5589 case R_MIPS_CALL_LO16:
5590 case R_MICROMIPS_CALL16:
5591 case R_MICROMIPS_GOT16:
5592 case R_MICROMIPS_GOT_DISP:
5593 case R_MICROMIPS_GOT_HI16:
5594 case R_MICROMIPS_CALL_HI16:
5595 case R_MICROMIPS_GOT_LO16:
5596 case R_MICROMIPS_CALL_LO16:
5598 case R_MIPS_TLS_GOTTPREL:
5599 case R_MIPS_TLS_LDM:
5600 case R_MIPS16_TLS_GD:
5601 case R_MIPS16_TLS_GOTTPREL:
5602 case R_MIPS16_TLS_LDM:
5603 case R_MICROMIPS_TLS_GD:
5604 case R_MICROMIPS_TLS_GOTTPREL:
5605 case R_MICROMIPS_TLS_LDM:
5606 /* Find the index into the GOT where this value is located. */
5607 if (tls_ldm_reloc_p (r_type))
5609 g = mips_elf_local_got_index (abfd, input_bfd, info,
5610 0, 0, NULL, r_type);
5612 return bfd_reloc_outofrange;
5616 /* On VxWorks, CALL relocations should refer to the .got.plt
5617 entry, which is initialized to point at the PLT stub. */
5618 if (htab->is_vxworks
5619 && (call_hi16_reloc_p (r_type)
5620 || call_lo16_reloc_p (r_type)
5621 || call16_reloc_p (r_type)))
5623 BFD_ASSERT (addend == 0);
5624 BFD_ASSERT (h->root.needs_plt);
5625 g = mips_elf_gotplt_index (info, &h->root);
5629 BFD_ASSERT (addend == 0);
5630 g = mips_elf_global_got_index (abfd, info, input_bfd,
5632 if (!TLS_RELOC_P (r_type)
5633 && !elf_hash_table (info)->dynamic_sections_created)
5634 /* This is a static link. We must initialize the GOT entry. */
5635 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5638 else if (!htab->is_vxworks
5639 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5640 /* The calculation below does not involve "g". */
5644 g = mips_elf_local_got_index (abfd, input_bfd, info,
5645 symbol + addend, r_symndx, h, r_type);
5647 return bfd_reloc_outofrange;
5650 /* Convert GOT indices to actual offsets. */
5651 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5655 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5656 symbols are resolved by the loader. Add them to .rela.dyn. */
5657 if (h != NULL && is_gott_symbol (info, &h->root))
5659 Elf_Internal_Rela outrel;
5663 s = mips_elf_rel_dyn_section (info, FALSE);
5664 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5666 outrel.r_offset = (input_section->output_section->vma
5667 + input_section->output_offset
5668 + relocation->r_offset);
5669 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5670 outrel.r_addend = addend;
5671 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5673 /* If we've written this relocation for a readonly section,
5674 we need to set DF_TEXTREL again, so that we do not delete the
5676 if (MIPS_ELF_READONLY_SECTION (input_section))
5677 info->flags |= DF_TEXTREL;
5680 return bfd_reloc_ok;
5683 /* Figure out what kind of relocation is being performed. */
5687 return bfd_reloc_continue;
5690 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5691 overflowed_p = mips_elf_overflow_p (value, 16);
5698 || (htab->root.dynamic_sections_created
5700 && h->root.def_dynamic
5701 && !h->root.def_regular
5702 && !h->has_static_relocs))
5703 && r_symndx != STN_UNDEF
5705 || h->root.root.type != bfd_link_hash_undefweak
5706 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5707 && (input_section->flags & SEC_ALLOC) != 0)
5709 /* If we're creating a shared library, then we can't know
5710 where the symbol will end up. So, we create a relocation
5711 record in the output, and leave the job up to the dynamic
5712 linker. We must do the same for executable references to
5713 shared library symbols, unless we've decided to use copy
5714 relocs or PLTs instead. */
5716 if (!mips_elf_create_dynamic_relocation (abfd,
5724 return bfd_reloc_undefined;
5728 if (r_type != R_MIPS_REL32)
5729 value = symbol + addend;
5733 value &= howto->dst_mask;
5737 value = symbol + addend - p;
5738 value &= howto->dst_mask;
5742 /* The calculation for R_MIPS16_26 is just the same as for an
5743 R_MIPS_26. It's only the storage of the relocated field into
5744 the output file that's different. That's handled in
5745 mips_elf_perform_relocation. So, we just fall through to the
5746 R_MIPS_26 case here. */
5748 case R_MICROMIPS_26_S1:
5752 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5753 the correct ISA mode selector and bit 1 must be 0. */
5754 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5755 return bfd_reloc_outofrange;
5757 /* Shift is 2, unusually, for microMIPS JALX. */
5758 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5761 value = addend | ((p + 4) & (0xfc000000 << shift));
5763 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5764 value = (value + symbol) >> shift;
5765 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5766 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5767 value &= howto->dst_mask;
5771 case R_MIPS_TLS_DTPREL_HI16:
5772 case R_MIPS16_TLS_DTPREL_HI16:
5773 case R_MICROMIPS_TLS_DTPREL_HI16:
5774 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5778 case R_MIPS_TLS_DTPREL_LO16:
5779 case R_MIPS_TLS_DTPREL32:
5780 case R_MIPS_TLS_DTPREL64:
5781 case R_MIPS16_TLS_DTPREL_LO16:
5782 case R_MICROMIPS_TLS_DTPREL_LO16:
5783 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5786 case R_MIPS_TLS_TPREL_HI16:
5787 case R_MIPS16_TLS_TPREL_HI16:
5788 case R_MICROMIPS_TLS_TPREL_HI16:
5789 value = (mips_elf_high (addend + symbol - tprel_base (info))
5793 case R_MIPS_TLS_TPREL_LO16:
5794 case R_MIPS_TLS_TPREL32:
5795 case R_MIPS_TLS_TPREL64:
5796 case R_MIPS16_TLS_TPREL_LO16:
5797 case R_MICROMIPS_TLS_TPREL_LO16:
5798 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5803 case R_MICROMIPS_HI16:
5806 value = mips_elf_high (addend + symbol);
5807 value &= howto->dst_mask;
5811 /* For MIPS16 ABI code we generate this sequence
5812 0: li $v0,%hi(_gp_disp)
5813 4: addiupc $v1,%lo(_gp_disp)
5817 So the offsets of hi and lo relocs are the same, but the
5818 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5819 ADDIUPC clears the low two bits of the instruction address,
5820 so the base is ($t9 + 4) & ~3. */
5821 if (r_type == R_MIPS16_HI16)
5822 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5823 /* The microMIPS .cpload sequence uses the same assembly
5824 instructions as the traditional psABI version, but the
5825 incoming $t9 has the low bit set. */
5826 else if (r_type == R_MICROMIPS_HI16)
5827 value = mips_elf_high (addend + gp - p - 1);
5829 value = mips_elf_high (addend + gp - p);
5830 overflowed_p = mips_elf_overflow_p (value, 16);
5836 case R_MICROMIPS_LO16:
5837 case R_MICROMIPS_HI0_LO16:
5839 value = (symbol + addend) & howto->dst_mask;
5842 /* See the comment for R_MIPS16_HI16 above for the reason
5843 for this conditional. */
5844 if (r_type == R_MIPS16_LO16)
5845 value = addend + gp - (p & ~(bfd_vma) 0x3);
5846 else if (r_type == R_MICROMIPS_LO16
5847 || r_type == R_MICROMIPS_HI0_LO16)
5848 value = addend + gp - p + 3;
5850 value = addend + gp - p + 4;
5851 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5852 for overflow. But, on, say, IRIX5, relocations against
5853 _gp_disp are normally generated from the .cpload
5854 pseudo-op. It generates code that normally looks like
5857 lui $gp,%hi(_gp_disp)
5858 addiu $gp,$gp,%lo(_gp_disp)
5861 Here $t9 holds the address of the function being called,
5862 as required by the MIPS ELF ABI. The R_MIPS_LO16
5863 relocation can easily overflow in this situation, but the
5864 R_MIPS_HI16 relocation will handle the overflow.
5865 Therefore, we consider this a bug in the MIPS ABI, and do
5866 not check for overflow here. */
5870 case R_MIPS_LITERAL:
5871 case R_MICROMIPS_LITERAL:
5872 /* Because we don't merge literal sections, we can handle this
5873 just like R_MIPS_GPREL16. In the long run, we should merge
5874 shared literals, and then we will need to additional work
5879 case R_MIPS16_GPREL:
5880 /* The R_MIPS16_GPREL performs the same calculation as
5881 R_MIPS_GPREL16, but stores the relocated bits in a different
5882 order. We don't need to do anything special here; the
5883 differences are handled in mips_elf_perform_relocation. */
5884 case R_MIPS_GPREL16:
5885 case R_MICROMIPS_GPREL7_S2:
5886 case R_MICROMIPS_GPREL16:
5887 /* Only sign-extend the addend if it was extracted from the
5888 instruction. If the addend was separate, leave it alone,
5889 otherwise we may lose significant bits. */
5890 if (howto->partial_inplace)
5891 addend = _bfd_mips_elf_sign_extend (addend, 16);
5892 value = symbol + addend - gp;
5893 /* If the symbol was local, any earlier relocatable links will
5894 have adjusted its addend with the gp offset, so compensate
5895 for that now. Don't do it for symbols forced local in this
5896 link, though, since they won't have had the gp offset applied
5900 overflowed_p = mips_elf_overflow_p (value, 16);
5903 case R_MIPS16_GOT16:
5904 case R_MIPS16_CALL16:
5907 case R_MICROMIPS_GOT16:
5908 case R_MICROMIPS_CALL16:
5909 /* VxWorks does not have separate local and global semantics for
5910 R_MIPS*_GOT16; every relocation evaluates to "G". */
5911 if (!htab->is_vxworks && local_p)
5913 value = mips_elf_got16_entry (abfd, input_bfd, info,
5914 symbol + addend, !was_local_p);
5915 if (value == MINUS_ONE)
5916 return bfd_reloc_outofrange;
5918 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5919 overflowed_p = mips_elf_overflow_p (value, 16);
5926 case R_MIPS_TLS_GOTTPREL:
5927 case R_MIPS_TLS_LDM:
5928 case R_MIPS_GOT_DISP:
5929 case R_MIPS16_TLS_GD:
5930 case R_MIPS16_TLS_GOTTPREL:
5931 case R_MIPS16_TLS_LDM:
5932 case R_MICROMIPS_TLS_GD:
5933 case R_MICROMIPS_TLS_GOTTPREL:
5934 case R_MICROMIPS_TLS_LDM:
5935 case R_MICROMIPS_GOT_DISP:
5937 overflowed_p = mips_elf_overflow_p (value, 16);
5940 case R_MIPS_GPREL32:
5941 value = (addend + symbol + gp0 - gp);
5943 value &= howto->dst_mask;
5947 case R_MIPS_GNU_REL16_S2:
5948 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5949 overflowed_p = mips_elf_overflow_p (value, 18);
5950 value >>= howto->rightshift;
5951 value &= howto->dst_mask;
5954 case R_MICROMIPS_PC7_S1:
5955 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5956 overflowed_p = mips_elf_overflow_p (value, 8);
5957 value >>= howto->rightshift;
5958 value &= howto->dst_mask;
5961 case R_MICROMIPS_PC10_S1:
5962 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5963 overflowed_p = mips_elf_overflow_p (value, 11);
5964 value >>= howto->rightshift;
5965 value &= howto->dst_mask;
5968 case R_MICROMIPS_PC16_S1:
5969 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5970 overflowed_p = mips_elf_overflow_p (value, 17);
5971 value >>= howto->rightshift;
5972 value &= howto->dst_mask;
5975 case R_MICROMIPS_PC23_S2:
5976 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5977 overflowed_p = mips_elf_overflow_p (value, 25);
5978 value >>= howto->rightshift;
5979 value &= howto->dst_mask;
5982 case R_MIPS_GOT_HI16:
5983 case R_MIPS_CALL_HI16:
5984 case R_MICROMIPS_GOT_HI16:
5985 case R_MICROMIPS_CALL_HI16:
5986 /* We're allowed to handle these two relocations identically.
5987 The dynamic linker is allowed to handle the CALL relocations
5988 differently by creating a lazy evaluation stub. */
5990 value = mips_elf_high (value);
5991 value &= howto->dst_mask;
5994 case R_MIPS_GOT_LO16:
5995 case R_MIPS_CALL_LO16:
5996 case R_MICROMIPS_GOT_LO16:
5997 case R_MICROMIPS_CALL_LO16:
5998 value = g & howto->dst_mask;
6001 case R_MIPS_GOT_PAGE:
6002 case R_MICROMIPS_GOT_PAGE:
6003 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6004 if (value == MINUS_ONE)
6005 return bfd_reloc_outofrange;
6006 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6007 overflowed_p = mips_elf_overflow_p (value, 16);
6010 case R_MIPS_GOT_OFST:
6011 case R_MICROMIPS_GOT_OFST:
6013 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6016 overflowed_p = mips_elf_overflow_p (value, 16);
6020 case R_MICROMIPS_SUB:
6021 value = symbol - addend;
6022 value &= howto->dst_mask;
6026 case R_MICROMIPS_HIGHER:
6027 value = mips_elf_higher (addend + symbol);
6028 value &= howto->dst_mask;
6031 case R_MIPS_HIGHEST:
6032 case R_MICROMIPS_HIGHEST:
6033 value = mips_elf_highest (addend + symbol);
6034 value &= howto->dst_mask;
6037 case R_MIPS_SCN_DISP:
6038 case R_MICROMIPS_SCN_DISP:
6039 value = symbol + addend - sec->output_offset;
6040 value &= howto->dst_mask;
6044 case R_MICROMIPS_JALR:
6045 /* This relocation is only a hint. In some cases, we optimize
6046 it into a bal instruction. But we don't try to optimize
6047 when the symbol does not resolve locally. */
6048 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6049 return bfd_reloc_continue;
6050 value = symbol + addend;
6054 case R_MIPS_GNU_VTINHERIT:
6055 case R_MIPS_GNU_VTENTRY:
6056 /* We don't do anything with these at present. */
6057 return bfd_reloc_continue;
6060 /* An unrecognized relocation type. */
6061 return bfd_reloc_notsupported;
6064 /* Store the VALUE for our caller. */
6066 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6069 /* Obtain the field relocated by RELOCATION. */
6072 mips_elf_obtain_contents (reloc_howto_type *howto,
6073 const Elf_Internal_Rela *relocation,
6074 bfd *input_bfd, bfd_byte *contents)
6077 bfd_byte *location = contents + relocation->r_offset;
6079 /* Obtain the bytes. */
6080 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6085 /* It has been determined that the result of the RELOCATION is the
6086 VALUE. Use HOWTO to place VALUE into the output file at the
6087 appropriate position. The SECTION is the section to which the
6089 CROSS_MODE_JUMP_P is true if the relocation field
6090 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6092 Returns FALSE if anything goes wrong. */
6095 mips_elf_perform_relocation (struct bfd_link_info *info,
6096 reloc_howto_type *howto,
6097 const Elf_Internal_Rela *relocation,
6098 bfd_vma value, bfd *input_bfd,
6099 asection *input_section, bfd_byte *contents,
6100 bfd_boolean cross_mode_jump_p)
6104 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6106 /* Figure out where the relocation is occurring. */
6107 location = contents + relocation->r_offset;
6109 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6111 /* Obtain the current value. */
6112 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6114 /* Clear the field we are setting. */
6115 x &= ~howto->dst_mask;
6117 /* Set the field. */
6118 x |= (value & howto->dst_mask);
6120 /* If required, turn JAL into JALX. */
6121 if (cross_mode_jump_p && jal_reloc_p (r_type))
6124 bfd_vma opcode = x >> 26;
6125 bfd_vma jalx_opcode;
6127 /* Check to see if the opcode is already JAL or JALX. */
6128 if (r_type == R_MIPS16_26)
6130 ok = ((opcode == 0x6) || (opcode == 0x7));
6133 else if (r_type == R_MICROMIPS_26_S1)
6135 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6140 ok = ((opcode == 0x3) || (opcode == 0x1d));
6144 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6145 convert J or JALS to JALX. */
6148 (*_bfd_error_handler)
6149 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
6152 (unsigned long) relocation->r_offset);
6153 bfd_set_error (bfd_error_bad_value);
6157 /* Make this the JALX opcode. */
6158 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6161 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6163 if (!info->relocatable
6164 && !cross_mode_jump_p
6165 && ((JAL_TO_BAL_P (input_bfd)
6166 && r_type == R_MIPS_26
6167 && (x >> 26) == 0x3) /* jal addr */
6168 || (JALR_TO_BAL_P (input_bfd)
6169 && r_type == R_MIPS_JALR
6170 && x == 0x0320f809) /* jalr t9 */
6171 || (JR_TO_B_P (input_bfd)
6172 && r_type == R_MIPS_JALR
6173 && x == 0x03200008))) /* jr t9 */
6179 addr = (input_section->output_section->vma
6180 + input_section->output_offset
6181 + relocation->r_offset
6183 if (r_type == R_MIPS_26)
6184 dest = (value << 2) | ((addr >> 28) << 28);
6188 if (off <= 0x1ffff && off >= -0x20000)
6190 if (x == 0x03200008) /* jr t9 */
6191 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6193 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6197 /* Put the value into the output. */
6198 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6200 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6206 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6207 is the original relocation, which is now being transformed into a
6208 dynamic relocation. The ADDENDP is adjusted if necessary; the
6209 caller should store the result in place of the original addend. */
6212 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6213 struct bfd_link_info *info,
6214 const Elf_Internal_Rela *rel,
6215 struct mips_elf_link_hash_entry *h,
6216 asection *sec, bfd_vma symbol,
6217 bfd_vma *addendp, asection *input_section)
6219 Elf_Internal_Rela outrel[3];
6224 bfd_boolean defined_p;
6225 struct mips_elf_link_hash_table *htab;
6227 htab = mips_elf_hash_table (info);
6228 BFD_ASSERT (htab != NULL);
6230 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6231 dynobj = elf_hash_table (info)->dynobj;
6232 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6233 BFD_ASSERT (sreloc != NULL);
6234 BFD_ASSERT (sreloc->contents != NULL);
6235 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6238 outrel[0].r_offset =
6239 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6240 if (ABI_64_P (output_bfd))
6242 outrel[1].r_offset =
6243 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6244 outrel[2].r_offset =
6245 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6248 if (outrel[0].r_offset == MINUS_ONE)
6249 /* The relocation field has been deleted. */
6252 if (outrel[0].r_offset == MINUS_TWO)
6254 /* The relocation field has been converted into a relative value of
6255 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6256 the field to be fully relocated, so add in the symbol's value. */
6261 /* We must now calculate the dynamic symbol table index to use
6262 in the relocation. */
6263 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6265 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6266 indx = h->root.dynindx;
6267 if (SGI_COMPAT (output_bfd))
6268 defined_p = h->root.def_regular;
6270 /* ??? glibc's ld.so just adds the final GOT entry to the
6271 relocation field. It therefore treats relocs against
6272 defined symbols in the same way as relocs against
6273 undefined symbols. */
6278 if (sec != NULL && bfd_is_abs_section (sec))
6280 else if (sec == NULL || sec->owner == NULL)
6282 bfd_set_error (bfd_error_bad_value);
6287 indx = elf_section_data (sec->output_section)->dynindx;
6290 asection *osec = htab->root.text_index_section;
6291 indx = elf_section_data (osec)->dynindx;
6297 /* Instead of generating a relocation using the section
6298 symbol, we may as well make it a fully relative
6299 relocation. We want to avoid generating relocations to
6300 local symbols because we used to generate them
6301 incorrectly, without adding the original symbol value,
6302 which is mandated by the ABI for section symbols. In
6303 order to give dynamic loaders and applications time to
6304 phase out the incorrect use, we refrain from emitting
6305 section-relative relocations. It's not like they're
6306 useful, after all. This should be a bit more efficient
6308 /* ??? Although this behavior is compatible with glibc's ld.so,
6309 the ABI says that relocations against STN_UNDEF should have
6310 a symbol value of 0. Irix rld honors this, so relocations
6311 against STN_UNDEF have no effect. */
6312 if (!SGI_COMPAT (output_bfd))
6317 /* If the relocation was previously an absolute relocation and
6318 this symbol will not be referred to by the relocation, we must
6319 adjust it by the value we give it in the dynamic symbol table.
6320 Otherwise leave the job up to the dynamic linker. */
6321 if (defined_p && r_type != R_MIPS_REL32)
6324 if (htab->is_vxworks)
6325 /* VxWorks uses non-relative relocations for this. */
6326 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6328 /* The relocation is always an REL32 relocation because we don't
6329 know where the shared library will wind up at load-time. */
6330 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6333 /* For strict adherence to the ABI specification, we should
6334 generate a R_MIPS_64 relocation record by itself before the
6335 _REL32/_64 record as well, such that the addend is read in as
6336 a 64-bit value (REL32 is a 32-bit relocation, after all).
6337 However, since none of the existing ELF64 MIPS dynamic
6338 loaders seems to care, we don't waste space with these
6339 artificial relocations. If this turns out to not be true,
6340 mips_elf_allocate_dynamic_relocation() should be tweaked so
6341 as to make room for a pair of dynamic relocations per
6342 invocation if ABI_64_P, and here we should generate an
6343 additional relocation record with R_MIPS_64 by itself for a
6344 NULL symbol before this relocation record. */
6345 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6346 ABI_64_P (output_bfd)
6349 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6351 /* Adjust the output offset of the relocation to reference the
6352 correct location in the output file. */
6353 outrel[0].r_offset += (input_section->output_section->vma
6354 + input_section->output_offset);
6355 outrel[1].r_offset += (input_section->output_section->vma
6356 + input_section->output_offset);
6357 outrel[2].r_offset += (input_section->output_section->vma
6358 + input_section->output_offset);
6360 /* Put the relocation back out. We have to use the special
6361 relocation outputter in the 64-bit case since the 64-bit
6362 relocation format is non-standard. */
6363 if (ABI_64_P (output_bfd))
6365 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6366 (output_bfd, &outrel[0],
6368 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6370 else if (htab->is_vxworks)
6372 /* VxWorks uses RELA rather than REL dynamic relocations. */
6373 outrel[0].r_addend = *addendp;
6374 bfd_elf32_swap_reloca_out
6375 (output_bfd, &outrel[0],
6377 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6380 bfd_elf32_swap_reloc_out
6381 (output_bfd, &outrel[0],
6382 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6384 /* We've now added another relocation. */
6385 ++sreloc->reloc_count;
6387 /* Make sure the output section is writable. The dynamic linker
6388 will be writing to it. */
6389 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6392 /* On IRIX5, make an entry of compact relocation info. */
6393 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6395 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6400 Elf32_crinfo cptrel;
6402 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6403 cptrel.vaddr = (rel->r_offset
6404 + input_section->output_section->vma
6405 + input_section->output_offset);
6406 if (r_type == R_MIPS_REL32)
6407 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6409 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6410 mips_elf_set_cr_dist2to (cptrel, 0);
6411 cptrel.konst = *addendp;
6413 cr = (scpt->contents
6414 + sizeof (Elf32_External_compact_rel));
6415 mips_elf_set_cr_relvaddr (cptrel, 0);
6416 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6417 ((Elf32_External_crinfo *) cr
6418 + scpt->reloc_count));
6419 ++scpt->reloc_count;
6423 /* If we've written this relocation for a readonly section,
6424 we need to set DF_TEXTREL again, so that we do not delete the
6426 if (MIPS_ELF_READONLY_SECTION (input_section))
6427 info->flags |= DF_TEXTREL;
6432 /* Return the MACH for a MIPS e_flags value. */
6435 _bfd_elf_mips_mach (flagword flags)
6437 switch (flags & EF_MIPS_MACH)
6439 case E_MIPS_MACH_3900:
6440 return bfd_mach_mips3900;
6442 case E_MIPS_MACH_4010:
6443 return bfd_mach_mips4010;
6445 case E_MIPS_MACH_4100:
6446 return bfd_mach_mips4100;
6448 case E_MIPS_MACH_4111:
6449 return bfd_mach_mips4111;
6451 case E_MIPS_MACH_4120:
6452 return bfd_mach_mips4120;
6454 case E_MIPS_MACH_4650:
6455 return bfd_mach_mips4650;
6457 case E_MIPS_MACH_5400:
6458 return bfd_mach_mips5400;
6460 case E_MIPS_MACH_5500:
6461 return bfd_mach_mips5500;
6463 case E_MIPS_MACH_5900:
6464 return bfd_mach_mips5900;
6466 case E_MIPS_MACH_9000:
6467 return bfd_mach_mips9000;
6469 case E_MIPS_MACH_SB1:
6470 return bfd_mach_mips_sb1;
6472 case E_MIPS_MACH_LS2E:
6473 return bfd_mach_mips_loongson_2e;
6475 case E_MIPS_MACH_LS2F:
6476 return bfd_mach_mips_loongson_2f;
6478 case E_MIPS_MACH_LS3A:
6479 return bfd_mach_mips_loongson_3a;
6481 case E_MIPS_MACH_OCTEON2:
6482 return bfd_mach_mips_octeon2;
6484 case E_MIPS_MACH_OCTEON:
6485 return bfd_mach_mips_octeon;
6487 case E_MIPS_MACH_XLR:
6488 return bfd_mach_mips_xlr;
6491 switch (flags & EF_MIPS_ARCH)
6495 return bfd_mach_mips3000;
6498 return bfd_mach_mips6000;
6501 return bfd_mach_mips4000;
6504 return bfd_mach_mips8000;
6507 return bfd_mach_mips5;
6509 case E_MIPS_ARCH_32:
6510 return bfd_mach_mipsisa32;
6512 case E_MIPS_ARCH_64:
6513 return bfd_mach_mipsisa64;
6515 case E_MIPS_ARCH_32R2:
6516 return bfd_mach_mipsisa32r2;
6518 case E_MIPS_ARCH_64R2:
6519 return bfd_mach_mipsisa64r2;
6526 /* Return printable name for ABI. */
6528 static INLINE char *
6529 elf_mips_abi_name (bfd *abfd)
6533 flags = elf_elfheader (abfd)->e_flags;
6534 switch (flags & EF_MIPS_ABI)
6537 if (ABI_N32_P (abfd))
6539 else if (ABI_64_P (abfd))
6543 case E_MIPS_ABI_O32:
6545 case E_MIPS_ABI_O64:
6547 case E_MIPS_ABI_EABI32:
6549 case E_MIPS_ABI_EABI64:
6552 return "unknown abi";
6556 /* MIPS ELF uses two common sections. One is the usual one, and the
6557 other is for small objects. All the small objects are kept
6558 together, and then referenced via the gp pointer, which yields
6559 faster assembler code. This is what we use for the small common
6560 section. This approach is copied from ecoff.c. */
6561 static asection mips_elf_scom_section;
6562 static asymbol mips_elf_scom_symbol;
6563 static asymbol *mips_elf_scom_symbol_ptr;
6565 /* MIPS ELF also uses an acommon section, which represents an
6566 allocated common symbol which may be overridden by a
6567 definition in a shared library. */
6568 static asection mips_elf_acom_section;
6569 static asymbol mips_elf_acom_symbol;
6570 static asymbol *mips_elf_acom_symbol_ptr;
6572 /* This is used for both the 32-bit and the 64-bit ABI. */
6575 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6577 elf_symbol_type *elfsym;
6579 /* Handle the special MIPS section numbers that a symbol may use. */
6580 elfsym = (elf_symbol_type *) asym;
6581 switch (elfsym->internal_elf_sym.st_shndx)
6583 case SHN_MIPS_ACOMMON:
6584 /* This section is used in a dynamically linked executable file.
6585 It is an allocated common section. The dynamic linker can
6586 either resolve these symbols to something in a shared
6587 library, or it can just leave them here. For our purposes,
6588 we can consider these symbols to be in a new section. */
6589 if (mips_elf_acom_section.name == NULL)
6591 /* Initialize the acommon section. */
6592 mips_elf_acom_section.name = ".acommon";
6593 mips_elf_acom_section.flags = SEC_ALLOC;
6594 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6595 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6596 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6597 mips_elf_acom_symbol.name = ".acommon";
6598 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6599 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6600 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6602 asym->section = &mips_elf_acom_section;
6606 /* Common symbols less than the GP size are automatically
6607 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6608 if (asym->value > elf_gp_size (abfd)
6609 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6610 || IRIX_COMPAT (abfd) == ict_irix6)
6613 case SHN_MIPS_SCOMMON:
6614 if (mips_elf_scom_section.name == NULL)
6616 /* Initialize the small common section. */
6617 mips_elf_scom_section.name = ".scommon";
6618 mips_elf_scom_section.flags = SEC_IS_COMMON;
6619 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6620 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6621 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6622 mips_elf_scom_symbol.name = ".scommon";
6623 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6624 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6625 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6627 asym->section = &mips_elf_scom_section;
6628 asym->value = elfsym->internal_elf_sym.st_size;
6631 case SHN_MIPS_SUNDEFINED:
6632 asym->section = bfd_und_section_ptr;
6637 asection *section = bfd_get_section_by_name (abfd, ".text");
6639 if (section != NULL)
6641 asym->section = section;
6642 /* MIPS_TEXT is a bit special, the address is not an offset
6643 to the base of the .text section. So substract the section
6644 base address to make it an offset. */
6645 asym->value -= section->vma;
6652 asection *section = bfd_get_section_by_name (abfd, ".data");
6654 if (section != NULL)
6656 asym->section = section;
6657 /* MIPS_DATA is a bit special, the address is not an offset
6658 to the base of the .data section. So substract the section
6659 base address to make it an offset. */
6660 asym->value -= section->vma;
6666 /* If this is an odd-valued function symbol, assume it's a MIPS16
6667 or microMIPS one. */
6668 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6669 && (asym->value & 1) != 0)
6672 if (MICROMIPS_P (abfd))
6673 elfsym->internal_elf_sym.st_other
6674 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6676 elfsym->internal_elf_sym.st_other
6677 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6681 /* Implement elf_backend_eh_frame_address_size. This differs from
6682 the default in the way it handles EABI64.
6684 EABI64 was originally specified as an LP64 ABI, and that is what
6685 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6686 historically accepted the combination of -mabi=eabi and -mlong32,
6687 and this ILP32 variation has become semi-official over time.
6688 Both forms use elf32 and have pointer-sized FDE addresses.
6690 If an EABI object was generated by GCC 4.0 or above, it will have
6691 an empty .gcc_compiled_longXX section, where XX is the size of longs
6692 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6693 have no special marking to distinguish them from LP64 objects.
6695 We don't want users of the official LP64 ABI to be punished for the
6696 existence of the ILP32 variant, but at the same time, we don't want
6697 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6698 We therefore take the following approach:
6700 - If ABFD contains a .gcc_compiled_longXX section, use it to
6701 determine the pointer size.
6703 - Otherwise check the type of the first relocation. Assume that
6704 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6708 The second check is enough to detect LP64 objects generated by pre-4.0
6709 compilers because, in the kind of output generated by those compilers,
6710 the first relocation will be associated with either a CIE personality
6711 routine or an FDE start address. Furthermore, the compilers never
6712 used a special (non-pointer) encoding for this ABI.
6714 Checking the relocation type should also be safe because there is no
6715 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6719 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6721 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6723 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6725 bfd_boolean long32_p, long64_p;
6727 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6728 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6729 if (long32_p && long64_p)
6736 if (sec->reloc_count > 0
6737 && elf_section_data (sec)->relocs != NULL
6738 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6747 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6748 relocations against two unnamed section symbols to resolve to the
6749 same address. For example, if we have code like:
6751 lw $4,%got_disp(.data)($gp)
6752 lw $25,%got_disp(.text)($gp)
6755 then the linker will resolve both relocations to .data and the program
6756 will jump there rather than to .text.
6758 We can work around this problem by giving names to local section symbols.
6759 This is also what the MIPSpro tools do. */
6762 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6764 return SGI_COMPAT (abfd);
6767 /* Work over a section just before writing it out. This routine is
6768 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6769 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6773 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6775 if (hdr->sh_type == SHT_MIPS_REGINFO
6776 && hdr->sh_size > 0)
6780 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6781 BFD_ASSERT (hdr->contents == NULL);
6784 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6787 H_PUT_32 (abfd, elf_gp (abfd), buf);
6788 if (bfd_bwrite (buf, 4, abfd) != 4)
6792 if (hdr->sh_type == SHT_MIPS_OPTIONS
6793 && hdr->bfd_section != NULL
6794 && mips_elf_section_data (hdr->bfd_section) != NULL
6795 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6797 bfd_byte *contents, *l, *lend;
6799 /* We stored the section contents in the tdata field in the
6800 set_section_contents routine. We save the section contents
6801 so that we don't have to read them again.
6802 At this point we know that elf_gp is set, so we can look
6803 through the section contents to see if there is an
6804 ODK_REGINFO structure. */
6806 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6808 lend = contents + hdr->sh_size;
6809 while (l + sizeof (Elf_External_Options) <= lend)
6811 Elf_Internal_Options intopt;
6813 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6815 if (intopt.size < sizeof (Elf_External_Options))
6817 (*_bfd_error_handler)
6818 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6819 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6822 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6829 + sizeof (Elf_External_Options)
6830 + (sizeof (Elf64_External_RegInfo) - 8)),
6833 H_PUT_64 (abfd, elf_gp (abfd), buf);
6834 if (bfd_bwrite (buf, 8, abfd) != 8)
6837 else if (intopt.kind == ODK_REGINFO)
6844 + sizeof (Elf_External_Options)
6845 + (sizeof (Elf32_External_RegInfo) - 4)),
6848 H_PUT_32 (abfd, elf_gp (abfd), buf);
6849 if (bfd_bwrite (buf, 4, abfd) != 4)
6856 if (hdr->bfd_section != NULL)
6858 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6860 /* .sbss is not handled specially here because the GNU/Linux
6861 prelinker can convert .sbss from NOBITS to PROGBITS and
6862 changing it back to NOBITS breaks the binary. The entry in
6863 _bfd_mips_elf_special_sections will ensure the correct flags
6864 are set on .sbss if BFD creates it without reading it from an
6865 input file, and without special handling here the flags set
6866 on it in an input file will be followed. */
6867 if (strcmp (name, ".sdata") == 0
6868 || strcmp (name, ".lit8") == 0
6869 || strcmp (name, ".lit4") == 0)
6871 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6872 hdr->sh_type = SHT_PROGBITS;
6874 else if (strcmp (name, ".srdata") == 0)
6876 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6877 hdr->sh_type = SHT_PROGBITS;
6879 else if (strcmp (name, ".compact_rel") == 0)
6882 hdr->sh_type = SHT_PROGBITS;
6884 else if (strcmp (name, ".rtproc") == 0)
6886 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6888 unsigned int adjust;
6890 adjust = hdr->sh_size % hdr->sh_addralign;
6892 hdr->sh_size += hdr->sh_addralign - adjust;
6900 /* Handle a MIPS specific section when reading an object file. This
6901 is called when elfcode.h finds a section with an unknown type.
6902 This routine supports both the 32-bit and 64-bit ELF ABI.
6904 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6908 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6909 Elf_Internal_Shdr *hdr,
6915 /* There ought to be a place to keep ELF backend specific flags, but
6916 at the moment there isn't one. We just keep track of the
6917 sections by their name, instead. Fortunately, the ABI gives
6918 suggested names for all the MIPS specific sections, so we will
6919 probably get away with this. */
6920 switch (hdr->sh_type)
6922 case SHT_MIPS_LIBLIST:
6923 if (strcmp (name, ".liblist") != 0)
6927 if (strcmp (name, ".msym") != 0)
6930 case SHT_MIPS_CONFLICT:
6931 if (strcmp (name, ".conflict") != 0)
6934 case SHT_MIPS_GPTAB:
6935 if (! CONST_STRNEQ (name, ".gptab."))
6938 case SHT_MIPS_UCODE:
6939 if (strcmp (name, ".ucode") != 0)
6942 case SHT_MIPS_DEBUG:
6943 if (strcmp (name, ".mdebug") != 0)
6945 flags = SEC_DEBUGGING;
6947 case SHT_MIPS_REGINFO:
6948 if (strcmp (name, ".reginfo") != 0
6949 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6951 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6953 case SHT_MIPS_IFACE:
6954 if (strcmp (name, ".MIPS.interfaces") != 0)
6957 case SHT_MIPS_CONTENT:
6958 if (! CONST_STRNEQ (name, ".MIPS.content"))
6961 case SHT_MIPS_OPTIONS:
6962 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6965 case SHT_MIPS_ABIFLAGS:
6966 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
6968 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6970 case SHT_MIPS_DWARF:
6971 if (! CONST_STRNEQ (name, ".debug_")
6972 && ! CONST_STRNEQ (name, ".zdebug_"))
6975 case SHT_MIPS_SYMBOL_LIB:
6976 if (strcmp (name, ".MIPS.symlib") != 0)
6979 case SHT_MIPS_EVENTS:
6980 if (! CONST_STRNEQ (name, ".MIPS.events")
6981 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6988 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6993 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6994 (bfd_get_section_flags (abfd,
7000 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7002 Elf_External_ABIFlags_v0 ext;
7004 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7005 &ext, 0, sizeof ext))
7007 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7008 &mips_elf_tdata (abfd)->abiflags);
7009 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7011 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
7014 /* FIXME: We should record sh_info for a .gptab section. */
7016 /* For a .reginfo section, set the gp value in the tdata information
7017 from the contents of this section. We need the gp value while
7018 processing relocs, so we just get it now. The .reginfo section
7019 is not used in the 64-bit MIPS ELF ABI. */
7020 if (hdr->sh_type == SHT_MIPS_REGINFO)
7022 Elf32_External_RegInfo ext;
7025 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7026 &ext, 0, sizeof ext))
7028 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7029 elf_gp (abfd) = s.ri_gp_value;
7032 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7033 set the gp value based on what we find. We may see both
7034 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7035 they should agree. */
7036 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7038 bfd_byte *contents, *l, *lend;
7040 contents = bfd_malloc (hdr->sh_size);
7041 if (contents == NULL)
7043 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
7050 lend = contents + hdr->sh_size;
7051 while (l + sizeof (Elf_External_Options) <= lend)
7053 Elf_Internal_Options intopt;
7055 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7057 if (intopt.size < sizeof (Elf_External_Options))
7059 (*_bfd_error_handler)
7060 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
7061 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7064 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7066 Elf64_Internal_RegInfo intreg;
7068 bfd_mips_elf64_swap_reginfo_in
7070 ((Elf64_External_RegInfo *)
7071 (l + sizeof (Elf_External_Options))),
7073 elf_gp (abfd) = intreg.ri_gp_value;
7075 else if (intopt.kind == ODK_REGINFO)
7077 Elf32_RegInfo intreg;
7079 bfd_mips_elf32_swap_reginfo_in
7081 ((Elf32_External_RegInfo *)
7082 (l + sizeof (Elf_External_Options))),
7084 elf_gp (abfd) = intreg.ri_gp_value;
7094 /* Set the correct type for a MIPS ELF section. We do this by the
7095 section name, which is a hack, but ought to work. This routine is
7096 used by both the 32-bit and the 64-bit ABI. */
7099 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7101 const char *name = bfd_get_section_name (abfd, sec);
7103 if (strcmp (name, ".liblist") == 0)
7105 hdr->sh_type = SHT_MIPS_LIBLIST;
7106 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7107 /* The sh_link field is set in final_write_processing. */
7109 else if (strcmp (name, ".conflict") == 0)
7110 hdr->sh_type = SHT_MIPS_CONFLICT;
7111 else if (CONST_STRNEQ (name, ".gptab."))
7113 hdr->sh_type = SHT_MIPS_GPTAB;
7114 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7115 /* The sh_info field is set in final_write_processing. */
7117 else if (strcmp (name, ".ucode") == 0)
7118 hdr->sh_type = SHT_MIPS_UCODE;
7119 else if (strcmp (name, ".mdebug") == 0)
7121 hdr->sh_type = SHT_MIPS_DEBUG;
7122 /* In a shared object on IRIX 5.3, the .mdebug section has an
7123 entsize of 0. FIXME: Does this matter? */
7124 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7125 hdr->sh_entsize = 0;
7127 hdr->sh_entsize = 1;
7129 else if (strcmp (name, ".reginfo") == 0)
7131 hdr->sh_type = SHT_MIPS_REGINFO;
7132 /* In a shared object on IRIX 5.3, the .reginfo section has an
7133 entsize of 0x18. FIXME: Does this matter? */
7134 if (SGI_COMPAT (abfd))
7136 if ((abfd->flags & DYNAMIC) != 0)
7137 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7139 hdr->sh_entsize = 1;
7142 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7144 else if (SGI_COMPAT (abfd)
7145 && (strcmp (name, ".hash") == 0
7146 || strcmp (name, ".dynamic") == 0
7147 || strcmp (name, ".dynstr") == 0))
7149 if (SGI_COMPAT (abfd))
7150 hdr->sh_entsize = 0;
7152 /* This isn't how the IRIX6 linker behaves. */
7153 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7156 else if (strcmp (name, ".got") == 0
7157 || strcmp (name, ".srdata") == 0
7158 || strcmp (name, ".sdata") == 0
7159 || strcmp (name, ".sbss") == 0
7160 || strcmp (name, ".lit4") == 0
7161 || strcmp (name, ".lit8") == 0)
7162 hdr->sh_flags |= SHF_MIPS_GPREL;
7163 else if (strcmp (name, ".MIPS.interfaces") == 0)
7165 hdr->sh_type = SHT_MIPS_IFACE;
7166 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7168 else if (CONST_STRNEQ (name, ".MIPS.content"))
7170 hdr->sh_type = SHT_MIPS_CONTENT;
7171 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7172 /* The sh_info field is set in final_write_processing. */
7174 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7176 hdr->sh_type = SHT_MIPS_OPTIONS;
7177 hdr->sh_entsize = 1;
7178 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7180 else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
7182 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7183 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7185 else if (CONST_STRNEQ (name, ".debug_")
7186 || CONST_STRNEQ (name, ".zdebug_"))
7188 hdr->sh_type = SHT_MIPS_DWARF;
7190 /* Irix facilities such as libexc expect a single .debug_frame
7191 per executable, the system ones have NOSTRIP set and the linker
7192 doesn't merge sections with different flags so ... */
7193 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7194 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7196 else if (strcmp (name, ".MIPS.symlib") == 0)
7198 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7199 /* The sh_link and sh_info fields are set in
7200 final_write_processing. */
7202 else if (CONST_STRNEQ (name, ".MIPS.events")
7203 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7205 hdr->sh_type = SHT_MIPS_EVENTS;
7206 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7207 /* The sh_link field is set in final_write_processing. */
7209 else if (strcmp (name, ".msym") == 0)
7211 hdr->sh_type = SHT_MIPS_MSYM;
7212 hdr->sh_flags |= SHF_ALLOC;
7213 hdr->sh_entsize = 8;
7216 /* The generic elf_fake_sections will set up REL_HDR using the default
7217 kind of relocations. We used to set up a second header for the
7218 non-default kind of relocations here, but only NewABI would use
7219 these, and the IRIX ld doesn't like resulting empty RELA sections.
7220 Thus we create those header only on demand now. */
7225 /* Given a BFD section, try to locate the corresponding ELF section
7226 index. This is used by both the 32-bit and the 64-bit ABI.
7227 Actually, it's not clear to me that the 64-bit ABI supports these,
7228 but for non-PIC objects we will certainly want support for at least
7229 the .scommon section. */
7232 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7233 asection *sec, int *retval)
7235 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7237 *retval = SHN_MIPS_SCOMMON;
7240 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7242 *retval = SHN_MIPS_ACOMMON;
7248 /* Hook called by the linker routine which adds symbols from an object
7249 file. We must handle the special MIPS section numbers here. */
7252 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7253 Elf_Internal_Sym *sym, const char **namep,
7254 flagword *flagsp ATTRIBUTE_UNUSED,
7255 asection **secp, bfd_vma *valp)
7257 if (SGI_COMPAT (abfd)
7258 && (abfd->flags & DYNAMIC) != 0
7259 && strcmp (*namep, "_rld_new_interface") == 0)
7261 /* Skip IRIX5 rld entry name. */
7266 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7267 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7268 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7269 a magic symbol resolved by the linker, we ignore this bogus definition
7270 of _gp_disp. New ABI objects do not suffer from this problem so this
7271 is not done for them. */
7273 && (sym->st_shndx == SHN_ABS)
7274 && (strcmp (*namep, "_gp_disp") == 0))
7280 switch (sym->st_shndx)
7283 /* Common symbols less than the GP size are automatically
7284 treated as SHN_MIPS_SCOMMON symbols. */
7285 if (sym->st_size > elf_gp_size (abfd)
7286 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7287 || IRIX_COMPAT (abfd) == ict_irix6)
7290 case SHN_MIPS_SCOMMON:
7291 *secp = bfd_make_section_old_way (abfd, ".scommon");
7292 (*secp)->flags |= SEC_IS_COMMON;
7293 *valp = sym->st_size;
7297 /* This section is used in a shared object. */
7298 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7300 asymbol *elf_text_symbol;
7301 asection *elf_text_section;
7302 bfd_size_type amt = sizeof (asection);
7304 elf_text_section = bfd_zalloc (abfd, amt);
7305 if (elf_text_section == NULL)
7308 amt = sizeof (asymbol);
7309 elf_text_symbol = bfd_zalloc (abfd, amt);
7310 if (elf_text_symbol == NULL)
7313 /* Initialize the section. */
7315 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7316 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7318 elf_text_section->symbol = elf_text_symbol;
7319 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7321 elf_text_section->name = ".text";
7322 elf_text_section->flags = SEC_NO_FLAGS;
7323 elf_text_section->output_section = NULL;
7324 elf_text_section->owner = abfd;
7325 elf_text_symbol->name = ".text";
7326 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7327 elf_text_symbol->section = elf_text_section;
7329 /* This code used to do *secp = bfd_und_section_ptr if
7330 info->shared. I don't know why, and that doesn't make sense,
7331 so I took it out. */
7332 *secp = mips_elf_tdata (abfd)->elf_text_section;
7335 case SHN_MIPS_ACOMMON:
7336 /* Fall through. XXX Can we treat this as allocated data? */
7338 /* This section is used in a shared object. */
7339 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7341 asymbol *elf_data_symbol;
7342 asection *elf_data_section;
7343 bfd_size_type amt = sizeof (asection);
7345 elf_data_section = bfd_zalloc (abfd, amt);
7346 if (elf_data_section == NULL)
7349 amt = sizeof (asymbol);
7350 elf_data_symbol = bfd_zalloc (abfd, amt);
7351 if (elf_data_symbol == NULL)
7354 /* Initialize the section. */
7356 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7357 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7359 elf_data_section->symbol = elf_data_symbol;
7360 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7362 elf_data_section->name = ".data";
7363 elf_data_section->flags = SEC_NO_FLAGS;
7364 elf_data_section->output_section = NULL;
7365 elf_data_section->owner = abfd;
7366 elf_data_symbol->name = ".data";
7367 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7368 elf_data_symbol->section = elf_data_section;
7370 /* This code used to do *secp = bfd_und_section_ptr if
7371 info->shared. I don't know why, and that doesn't make sense,
7372 so I took it out. */
7373 *secp = mips_elf_tdata (abfd)->elf_data_section;
7376 case SHN_MIPS_SUNDEFINED:
7377 *secp = bfd_und_section_ptr;
7381 if (SGI_COMPAT (abfd)
7383 && info->output_bfd->xvec == abfd->xvec
7384 && strcmp (*namep, "__rld_obj_head") == 0)
7386 struct elf_link_hash_entry *h;
7387 struct bfd_link_hash_entry *bh;
7389 /* Mark __rld_obj_head as dynamic. */
7391 if (! (_bfd_generic_link_add_one_symbol
7392 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7393 get_elf_backend_data (abfd)->collect, &bh)))
7396 h = (struct elf_link_hash_entry *) bh;
7399 h->type = STT_OBJECT;
7401 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7404 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7405 mips_elf_hash_table (info)->rld_symbol = h;
7408 /* If this is a mips16 text symbol, add 1 to the value to make it
7409 odd. This will cause something like .word SYM to come up with
7410 the right value when it is loaded into the PC. */
7411 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7417 /* This hook function is called before the linker writes out a global
7418 symbol. We mark symbols as small common if appropriate. This is
7419 also where we undo the increment of the value for a mips16 symbol. */
7422 _bfd_mips_elf_link_output_symbol_hook
7423 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7424 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7425 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7427 /* If we see a common symbol, which implies a relocatable link, then
7428 if a symbol was small common in an input file, mark it as small
7429 common in the output file. */
7430 if (sym->st_shndx == SHN_COMMON
7431 && strcmp (input_sec->name, ".scommon") == 0)
7432 sym->st_shndx = SHN_MIPS_SCOMMON;
7434 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7435 sym->st_value &= ~1;
7440 /* Functions for the dynamic linker. */
7442 /* Create dynamic sections when linking against a dynamic object. */
7445 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7447 struct elf_link_hash_entry *h;
7448 struct bfd_link_hash_entry *bh;
7450 register asection *s;
7451 const char * const *namep;
7452 struct mips_elf_link_hash_table *htab;
7454 htab = mips_elf_hash_table (info);
7455 BFD_ASSERT (htab != NULL);
7457 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7458 | SEC_LINKER_CREATED | SEC_READONLY);
7460 /* The psABI requires a read-only .dynamic section, but the VxWorks
7462 if (!htab->is_vxworks)
7464 s = bfd_get_linker_section (abfd, ".dynamic");
7467 if (! bfd_set_section_flags (abfd, s, flags))
7472 /* We need to create .got section. */
7473 if (!mips_elf_create_got_section (abfd, info))
7476 if (! mips_elf_rel_dyn_section (info, TRUE))
7479 /* Create .stub section. */
7480 s = bfd_make_section_anyway_with_flags (abfd,
7481 MIPS_ELF_STUB_SECTION_NAME (abfd),
7484 || ! bfd_set_section_alignment (abfd, s,
7485 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7489 if (!mips_elf_hash_table (info)->use_rld_obj_head
7491 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7493 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7494 flags &~ (flagword) SEC_READONLY);
7496 || ! bfd_set_section_alignment (abfd, s,
7497 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7501 /* On IRIX5, we adjust add some additional symbols and change the
7502 alignments of several sections. There is no ABI documentation
7503 indicating that this is necessary on IRIX6, nor any evidence that
7504 the linker takes such action. */
7505 if (IRIX_COMPAT (abfd) == ict_irix5)
7507 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7510 if (! (_bfd_generic_link_add_one_symbol
7511 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7512 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7515 h = (struct elf_link_hash_entry *) bh;
7518 h->type = STT_SECTION;
7520 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7524 /* We need to create a .compact_rel section. */
7525 if (SGI_COMPAT (abfd))
7527 if (!mips_elf_create_compact_rel_section (abfd, info))
7531 /* Change alignments of some sections. */
7532 s = bfd_get_linker_section (abfd, ".hash");
7534 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7536 s = bfd_get_linker_section (abfd, ".dynsym");
7538 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7540 s = bfd_get_linker_section (abfd, ".dynstr");
7542 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7545 s = bfd_get_section_by_name (abfd, ".reginfo");
7547 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7549 s = bfd_get_linker_section (abfd, ".dynamic");
7551 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7558 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7560 if (!(_bfd_generic_link_add_one_symbol
7561 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7562 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7565 h = (struct elf_link_hash_entry *) bh;
7568 h->type = STT_SECTION;
7570 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7573 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7575 /* __rld_map is a four byte word located in the .data section
7576 and is filled in by the rtld to contain a pointer to
7577 the _r_debug structure. Its symbol value will be set in
7578 _bfd_mips_elf_finish_dynamic_symbol. */
7579 s = bfd_get_linker_section (abfd, ".rld_map");
7580 BFD_ASSERT (s != NULL);
7582 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7584 if (!(_bfd_generic_link_add_one_symbol
7585 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7586 get_elf_backend_data (abfd)->collect, &bh)))
7589 h = (struct elf_link_hash_entry *) bh;
7592 h->type = STT_OBJECT;
7594 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7596 mips_elf_hash_table (info)->rld_symbol = h;
7600 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7601 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
7602 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7605 /* Cache the sections created above. */
7606 htab->splt = bfd_get_linker_section (abfd, ".plt");
7607 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7608 if (htab->is_vxworks)
7610 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7611 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7614 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7616 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7621 /* Do the usual VxWorks handling. */
7622 if (htab->is_vxworks
7623 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7629 /* Return true if relocation REL against section SEC is a REL rather than
7630 RELA relocation. RELOCS is the first relocation in the section and
7631 ABFD is the bfd that contains SEC. */
7634 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7635 const Elf_Internal_Rela *relocs,
7636 const Elf_Internal_Rela *rel)
7638 Elf_Internal_Shdr *rel_hdr;
7639 const struct elf_backend_data *bed;
7641 /* To determine which flavor of relocation this is, we depend on the
7642 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7643 rel_hdr = elf_section_data (sec)->rel.hdr;
7644 if (rel_hdr == NULL)
7646 bed = get_elf_backend_data (abfd);
7647 return ((size_t) (rel - relocs)
7648 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7651 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7652 HOWTO is the relocation's howto and CONTENTS points to the contents
7653 of the section that REL is against. */
7656 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7657 reloc_howto_type *howto, bfd_byte *contents)
7660 unsigned int r_type;
7663 r_type = ELF_R_TYPE (abfd, rel->r_info);
7664 location = contents + rel->r_offset;
7666 /* Get the addend, which is stored in the input file. */
7667 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7668 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7669 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7671 return addend & howto->src_mask;
7674 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7675 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7676 and update *ADDEND with the final addend. Return true on success
7677 or false if the LO16 could not be found. RELEND is the exclusive
7678 upper bound on the relocations for REL's section. */
7681 mips_elf_add_lo16_rel_addend (bfd *abfd,
7682 const Elf_Internal_Rela *rel,
7683 const Elf_Internal_Rela *relend,
7684 bfd_byte *contents, bfd_vma *addend)
7686 unsigned int r_type, lo16_type;
7687 const Elf_Internal_Rela *lo16_relocation;
7688 reloc_howto_type *lo16_howto;
7691 r_type = ELF_R_TYPE (abfd, rel->r_info);
7692 if (mips16_reloc_p (r_type))
7693 lo16_type = R_MIPS16_LO16;
7694 else if (micromips_reloc_p (r_type))
7695 lo16_type = R_MICROMIPS_LO16;
7697 lo16_type = R_MIPS_LO16;
7699 /* The combined value is the sum of the HI16 addend, left-shifted by
7700 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7701 code does a `lui' of the HI16 value, and then an `addiu' of the
7704 Scan ahead to find a matching LO16 relocation.
7706 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7707 be immediately following. However, for the IRIX6 ABI, the next
7708 relocation may be a composed relocation consisting of several
7709 relocations for the same address. In that case, the R_MIPS_LO16
7710 relocation may occur as one of these. We permit a similar
7711 extension in general, as that is useful for GCC.
7713 In some cases GCC dead code elimination removes the LO16 but keeps
7714 the corresponding HI16. This is strictly speaking a violation of
7715 the ABI but not immediately harmful. */
7716 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7717 if (lo16_relocation == NULL)
7720 /* Obtain the addend kept there. */
7721 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7722 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7724 l <<= lo16_howto->rightshift;
7725 l = _bfd_mips_elf_sign_extend (l, 16);
7732 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7733 store the contents in *CONTENTS on success. Assume that *CONTENTS
7734 already holds the contents if it is nonull on entry. */
7737 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7742 /* Get cached copy if it exists. */
7743 if (elf_section_data (sec)->this_hdr.contents != NULL)
7745 *contents = elf_section_data (sec)->this_hdr.contents;
7749 return bfd_malloc_and_get_section (abfd, sec, contents);
7752 /* Make a new PLT record to keep internal data. */
7754 static struct plt_entry *
7755 mips_elf_make_plt_record (bfd *abfd)
7757 struct plt_entry *entry;
7759 entry = bfd_zalloc (abfd, sizeof (*entry));
7763 entry->stub_offset = MINUS_ONE;
7764 entry->mips_offset = MINUS_ONE;
7765 entry->comp_offset = MINUS_ONE;
7766 entry->gotplt_index = MINUS_ONE;
7770 /* Look through the relocs for a section during the first phase, and
7771 allocate space in the global offset table and record the need for
7772 standard MIPS and compressed procedure linkage table entries. */
7775 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7776 asection *sec, const Elf_Internal_Rela *relocs)
7780 Elf_Internal_Shdr *symtab_hdr;
7781 struct elf_link_hash_entry **sym_hashes;
7783 const Elf_Internal_Rela *rel;
7784 const Elf_Internal_Rela *rel_end;
7786 const struct elf_backend_data *bed;
7787 struct mips_elf_link_hash_table *htab;
7790 reloc_howto_type *howto;
7792 if (info->relocatable)
7795 htab = mips_elf_hash_table (info);
7796 BFD_ASSERT (htab != NULL);
7798 dynobj = elf_hash_table (info)->dynobj;
7799 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7800 sym_hashes = elf_sym_hashes (abfd);
7801 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7803 bed = get_elf_backend_data (abfd);
7804 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7806 /* Check for the mips16 stub sections. */
7808 name = bfd_get_section_name (abfd, sec);
7809 if (FN_STUB_P (name))
7811 unsigned long r_symndx;
7813 /* Look at the relocation information to figure out which symbol
7816 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7819 (*_bfd_error_handler)
7820 (_("%B: Warning: cannot determine the target function for"
7821 " stub section `%s'"),
7823 bfd_set_error (bfd_error_bad_value);
7827 if (r_symndx < extsymoff
7828 || sym_hashes[r_symndx - extsymoff] == NULL)
7832 /* This stub is for a local symbol. This stub will only be
7833 needed if there is some relocation in this BFD, other
7834 than a 16 bit function call, which refers to this symbol. */
7835 for (o = abfd->sections; o != NULL; o = o->next)
7837 Elf_Internal_Rela *sec_relocs;
7838 const Elf_Internal_Rela *r, *rend;
7840 /* We can ignore stub sections when looking for relocs. */
7841 if ((o->flags & SEC_RELOC) == 0
7842 || o->reloc_count == 0
7843 || section_allows_mips16_refs_p (o))
7847 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7849 if (sec_relocs == NULL)
7852 rend = sec_relocs + o->reloc_count;
7853 for (r = sec_relocs; r < rend; r++)
7854 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7855 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7858 if (elf_section_data (o)->relocs != sec_relocs)
7867 /* There is no non-call reloc for this stub, so we do
7868 not need it. Since this function is called before
7869 the linker maps input sections to output sections, we
7870 can easily discard it by setting the SEC_EXCLUDE
7872 sec->flags |= SEC_EXCLUDE;
7876 /* Record this stub in an array of local symbol stubs for
7878 if (mips_elf_tdata (abfd)->local_stubs == NULL)
7880 unsigned long symcount;
7884 if (elf_bad_symtab (abfd))
7885 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7887 symcount = symtab_hdr->sh_info;
7888 amt = symcount * sizeof (asection *);
7889 n = bfd_zalloc (abfd, amt);
7892 mips_elf_tdata (abfd)->local_stubs = n;
7895 sec->flags |= SEC_KEEP;
7896 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7898 /* We don't need to set mips16_stubs_seen in this case.
7899 That flag is used to see whether we need to look through
7900 the global symbol table for stubs. We don't need to set
7901 it here, because we just have a local stub. */
7905 struct mips_elf_link_hash_entry *h;
7907 h = ((struct mips_elf_link_hash_entry *)
7908 sym_hashes[r_symndx - extsymoff]);
7910 while (h->root.root.type == bfd_link_hash_indirect
7911 || h->root.root.type == bfd_link_hash_warning)
7912 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7914 /* H is the symbol this stub is for. */
7916 /* If we already have an appropriate stub for this function, we
7917 don't need another one, so we can discard this one. Since
7918 this function is called before the linker maps input sections
7919 to output sections, we can easily discard it by setting the
7920 SEC_EXCLUDE flag. */
7921 if (h->fn_stub != NULL)
7923 sec->flags |= SEC_EXCLUDE;
7927 sec->flags |= SEC_KEEP;
7929 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7932 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7934 unsigned long r_symndx;
7935 struct mips_elf_link_hash_entry *h;
7938 /* Look at the relocation information to figure out which symbol
7941 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7944 (*_bfd_error_handler)
7945 (_("%B: Warning: cannot determine the target function for"
7946 " stub section `%s'"),
7948 bfd_set_error (bfd_error_bad_value);
7952 if (r_symndx < extsymoff
7953 || sym_hashes[r_symndx - extsymoff] == NULL)
7957 /* This stub is for a local symbol. This stub will only be
7958 needed if there is some relocation (R_MIPS16_26) in this BFD
7959 that refers to this symbol. */
7960 for (o = abfd->sections; o != NULL; o = o->next)
7962 Elf_Internal_Rela *sec_relocs;
7963 const Elf_Internal_Rela *r, *rend;
7965 /* We can ignore stub sections when looking for relocs. */
7966 if ((o->flags & SEC_RELOC) == 0
7967 || o->reloc_count == 0
7968 || section_allows_mips16_refs_p (o))
7972 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7974 if (sec_relocs == NULL)
7977 rend = sec_relocs + o->reloc_count;
7978 for (r = sec_relocs; r < rend; r++)
7979 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7980 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7983 if (elf_section_data (o)->relocs != sec_relocs)
7992 /* There is no non-call reloc for this stub, so we do
7993 not need it. Since this function is called before
7994 the linker maps input sections to output sections, we
7995 can easily discard it by setting the SEC_EXCLUDE
7997 sec->flags |= SEC_EXCLUDE;
8001 /* Record this stub in an array of local symbol call_stubs for
8003 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8005 unsigned long symcount;
8009 if (elf_bad_symtab (abfd))
8010 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8012 symcount = symtab_hdr->sh_info;
8013 amt = symcount * sizeof (asection *);
8014 n = bfd_zalloc (abfd, amt);
8017 mips_elf_tdata (abfd)->local_call_stubs = n;
8020 sec->flags |= SEC_KEEP;
8021 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8023 /* We don't need to set mips16_stubs_seen in this case.
8024 That flag is used to see whether we need to look through
8025 the global symbol table for stubs. We don't need to set
8026 it here, because we just have a local stub. */
8030 h = ((struct mips_elf_link_hash_entry *)
8031 sym_hashes[r_symndx - extsymoff]);
8033 /* H is the symbol this stub is for. */
8035 if (CALL_FP_STUB_P (name))
8036 loc = &h->call_fp_stub;
8038 loc = &h->call_stub;
8040 /* If we already have an appropriate stub for this function, we
8041 don't need another one, so we can discard this one. Since
8042 this function is called before the linker maps input sections
8043 to output sections, we can easily discard it by setting the
8044 SEC_EXCLUDE flag. */
8047 sec->flags |= SEC_EXCLUDE;
8051 sec->flags |= SEC_KEEP;
8053 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8059 for (rel = relocs; rel < rel_end; ++rel)
8061 unsigned long r_symndx;
8062 unsigned int r_type;
8063 struct elf_link_hash_entry *h;
8064 bfd_boolean can_make_dynamic_p;
8065 bfd_boolean call_reloc_p;
8066 bfd_boolean constrain_symbol_p;
8068 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8069 r_type = ELF_R_TYPE (abfd, rel->r_info);
8071 if (r_symndx < extsymoff)
8073 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8075 (*_bfd_error_handler)
8076 (_("%B: Malformed reloc detected for section %s"),
8078 bfd_set_error (bfd_error_bad_value);
8083 h = sym_hashes[r_symndx - extsymoff];
8086 while (h->root.type == bfd_link_hash_indirect
8087 || h->root.type == bfd_link_hash_warning)
8088 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8090 /* PR15323, ref flags aren't set for references in the
8092 h->root.non_ir_ref = 1;
8096 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8097 relocation into a dynamic one. */
8098 can_make_dynamic_p = FALSE;
8100 /* Set CALL_RELOC_P to true if the relocation is for a call,
8101 and if pointer equality therefore doesn't matter. */
8102 call_reloc_p = FALSE;
8104 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8105 into account when deciding how to define the symbol.
8106 Relocations in nonallocatable sections such as .pdr and
8107 .debug* should have no effect. */
8108 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8113 case R_MIPS_CALL_HI16:
8114 case R_MIPS_CALL_LO16:
8115 case R_MIPS16_CALL16:
8116 case R_MICROMIPS_CALL16:
8117 case R_MICROMIPS_CALL_HI16:
8118 case R_MICROMIPS_CALL_LO16:
8119 call_reloc_p = TRUE;
8123 case R_MIPS_GOT_HI16:
8124 case R_MIPS_GOT_LO16:
8125 case R_MIPS_GOT_PAGE:
8126 case R_MIPS_GOT_OFST:
8127 case R_MIPS_GOT_DISP:
8128 case R_MIPS_TLS_GOTTPREL:
8130 case R_MIPS_TLS_LDM:
8131 case R_MIPS16_GOT16:
8132 case R_MIPS16_TLS_GOTTPREL:
8133 case R_MIPS16_TLS_GD:
8134 case R_MIPS16_TLS_LDM:
8135 case R_MICROMIPS_GOT16:
8136 case R_MICROMIPS_GOT_HI16:
8137 case R_MICROMIPS_GOT_LO16:
8138 case R_MICROMIPS_GOT_PAGE:
8139 case R_MICROMIPS_GOT_OFST:
8140 case R_MICROMIPS_GOT_DISP:
8141 case R_MICROMIPS_TLS_GOTTPREL:
8142 case R_MICROMIPS_TLS_GD:
8143 case R_MICROMIPS_TLS_LDM:
8145 elf_hash_table (info)->dynobj = dynobj = abfd;
8146 if (!mips_elf_create_got_section (dynobj, info))
8148 if (htab->is_vxworks && !info->shared)
8150 (*_bfd_error_handler)
8151 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8152 abfd, (unsigned long) rel->r_offset);
8153 bfd_set_error (bfd_error_bad_value);
8156 can_make_dynamic_p = TRUE;
8161 case R_MICROMIPS_JALR:
8162 /* These relocations have empty fields and are purely there to
8163 provide link information. The symbol value doesn't matter. */
8164 constrain_symbol_p = FALSE;
8167 case R_MIPS_GPREL16:
8168 case R_MIPS_GPREL32:
8169 case R_MIPS16_GPREL:
8170 case R_MICROMIPS_GPREL16:
8171 /* GP-relative relocations always resolve to a definition in a
8172 regular input file, ignoring the one-definition rule. This is
8173 important for the GP setup sequence in NewABI code, which
8174 always resolves to a local function even if other relocations
8175 against the symbol wouldn't. */
8176 constrain_symbol_p = FALSE;
8182 /* In VxWorks executables, references to external symbols
8183 must be handled using copy relocs or PLT entries; it is not
8184 possible to convert this relocation into a dynamic one.
8186 For executables that use PLTs and copy-relocs, we have a
8187 choice between converting the relocation into a dynamic
8188 one or using copy relocations or PLT entries. It is
8189 usually better to do the former, unless the relocation is
8190 against a read-only section. */
8193 && !htab->is_vxworks
8194 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8195 && !(!info->nocopyreloc
8196 && !PIC_OBJECT_P (abfd)
8197 && MIPS_ELF_READONLY_SECTION (sec))))
8198 && (sec->flags & SEC_ALLOC) != 0)
8200 can_make_dynamic_p = TRUE;
8202 elf_hash_table (info)->dynobj = dynobj = abfd;
8209 case R_MICROMIPS_26_S1:
8210 case R_MICROMIPS_PC7_S1:
8211 case R_MICROMIPS_PC10_S1:
8212 case R_MICROMIPS_PC16_S1:
8213 case R_MICROMIPS_PC23_S2:
8214 call_reloc_p = TRUE;
8220 if (constrain_symbol_p)
8222 if (!can_make_dynamic_p)
8223 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8226 h->pointer_equality_needed = 1;
8228 /* We must not create a stub for a symbol that has
8229 relocations related to taking the function's address.
8230 This doesn't apply to VxWorks, where CALL relocs refer
8231 to a .got.plt entry instead of a normal .got entry. */
8232 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8233 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8236 /* Relocations against the special VxWorks __GOTT_BASE__ and
8237 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8238 room for them in .rela.dyn. */
8239 if (is_gott_symbol (info, h))
8243 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8247 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8248 if (MIPS_ELF_READONLY_SECTION (sec))
8249 /* We tell the dynamic linker that there are
8250 relocations against the text segment. */
8251 info->flags |= DF_TEXTREL;
8254 else if (call_lo16_reloc_p (r_type)
8255 || got_lo16_reloc_p (r_type)
8256 || got_disp_reloc_p (r_type)
8257 || (got16_reloc_p (r_type) && htab->is_vxworks))
8259 /* We may need a local GOT entry for this relocation. We
8260 don't count R_MIPS_GOT_PAGE because we can estimate the
8261 maximum number of pages needed by looking at the size of
8262 the segment. Similar comments apply to R_MIPS*_GOT16 and
8263 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8264 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8265 R_MIPS_CALL_HI16 because these are always followed by an
8266 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8267 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8268 rel->r_addend, info, r_type))
8273 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8274 ELF_ST_IS_MIPS16 (h->other)))
8275 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8280 case R_MIPS16_CALL16:
8281 case R_MICROMIPS_CALL16:
8284 (*_bfd_error_handler)
8285 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8286 abfd, (unsigned long) rel->r_offset);
8287 bfd_set_error (bfd_error_bad_value);
8292 case R_MIPS_CALL_HI16:
8293 case R_MIPS_CALL_LO16:
8294 case R_MICROMIPS_CALL_HI16:
8295 case R_MICROMIPS_CALL_LO16:
8298 /* Make sure there is room in the regular GOT to hold the
8299 function's address. We may eliminate it in favour of
8300 a .got.plt entry later; see mips_elf_count_got_symbols. */
8301 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8305 /* We need a stub, not a plt entry for the undefined
8306 function. But we record it as if it needs plt. See
8307 _bfd_elf_adjust_dynamic_symbol. */
8313 case R_MIPS_GOT_PAGE:
8314 case R_MICROMIPS_GOT_PAGE:
8315 case R_MIPS16_GOT16:
8317 case R_MIPS_GOT_HI16:
8318 case R_MIPS_GOT_LO16:
8319 case R_MICROMIPS_GOT16:
8320 case R_MICROMIPS_GOT_HI16:
8321 case R_MICROMIPS_GOT_LO16:
8322 if (!h || got_page_reloc_p (r_type))
8324 /* This relocation needs (or may need, if h != NULL) a
8325 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8326 know for sure until we know whether the symbol is
8328 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8330 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8332 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8333 addend = mips_elf_read_rel_addend (abfd, rel,
8335 if (got16_reloc_p (r_type))
8336 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8339 addend <<= howto->rightshift;
8342 addend = rel->r_addend;
8343 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8349 struct mips_elf_link_hash_entry *hmips =
8350 (struct mips_elf_link_hash_entry *) h;
8352 /* This symbol is definitely not overridable. */
8353 if (hmips->root.def_regular
8354 && ! (info->shared && ! info->symbolic
8355 && ! hmips->root.forced_local))
8359 /* If this is a global, overridable symbol, GOT_PAGE will
8360 decay to GOT_DISP, so we'll need a GOT entry for it. */
8363 case R_MIPS_GOT_DISP:
8364 case R_MICROMIPS_GOT_DISP:
8365 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8370 case R_MIPS_TLS_GOTTPREL:
8371 case R_MIPS16_TLS_GOTTPREL:
8372 case R_MICROMIPS_TLS_GOTTPREL:
8374 info->flags |= DF_STATIC_TLS;
8377 case R_MIPS_TLS_LDM:
8378 case R_MIPS16_TLS_LDM:
8379 case R_MICROMIPS_TLS_LDM:
8380 if (tls_ldm_reloc_p (r_type))
8382 r_symndx = STN_UNDEF;
8388 case R_MIPS16_TLS_GD:
8389 case R_MICROMIPS_TLS_GD:
8390 /* This symbol requires a global offset table entry, or two
8391 for TLS GD relocations. */
8394 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8400 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8410 /* In VxWorks executables, references to external symbols
8411 are handled using copy relocs or PLT stubs, so there's
8412 no need to add a .rela.dyn entry for this relocation. */
8413 if (can_make_dynamic_p)
8417 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8421 if (info->shared && h == NULL)
8423 /* When creating a shared object, we must copy these
8424 reloc types into the output file as R_MIPS_REL32
8425 relocs. Make room for this reloc in .rel(a).dyn. */
8426 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8427 if (MIPS_ELF_READONLY_SECTION (sec))
8428 /* We tell the dynamic linker that there are
8429 relocations against the text segment. */
8430 info->flags |= DF_TEXTREL;
8434 struct mips_elf_link_hash_entry *hmips;
8436 /* For a shared object, we must copy this relocation
8437 unless the symbol turns out to be undefined and
8438 weak with non-default visibility, in which case
8439 it will be left as zero.
8441 We could elide R_MIPS_REL32 for locally binding symbols
8442 in shared libraries, but do not yet do so.
8444 For an executable, we only need to copy this
8445 reloc if the symbol is defined in a dynamic
8447 hmips = (struct mips_elf_link_hash_entry *) h;
8448 ++hmips->possibly_dynamic_relocs;
8449 if (MIPS_ELF_READONLY_SECTION (sec))
8450 /* We need it to tell the dynamic linker if there
8451 are relocations against the text segment. */
8452 hmips->readonly_reloc = TRUE;
8456 if (SGI_COMPAT (abfd))
8457 mips_elf_hash_table (info)->compact_rel_size +=
8458 sizeof (Elf32_External_crinfo);
8462 case R_MIPS_GPREL16:
8463 case R_MIPS_LITERAL:
8464 case R_MIPS_GPREL32:
8465 case R_MICROMIPS_26_S1:
8466 case R_MICROMIPS_GPREL16:
8467 case R_MICROMIPS_LITERAL:
8468 case R_MICROMIPS_GPREL7_S2:
8469 if (SGI_COMPAT (abfd))
8470 mips_elf_hash_table (info)->compact_rel_size +=
8471 sizeof (Elf32_External_crinfo);
8474 /* This relocation describes the C++ object vtable hierarchy.
8475 Reconstruct it for later use during GC. */
8476 case R_MIPS_GNU_VTINHERIT:
8477 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8481 /* This relocation describes which C++ vtable entries are actually
8482 used. Record for later use during GC. */
8483 case R_MIPS_GNU_VTENTRY:
8484 BFD_ASSERT (h != NULL);
8486 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8494 /* Record the need for a PLT entry. At this point we don't know
8495 yet if we are going to create a PLT in the first place, but
8496 we only record whether the relocation requires a standard MIPS
8497 or a compressed code entry anyway. If we don't make a PLT after
8498 all, then we'll just ignore these arrangements. Likewise if
8499 a PLT entry is not created because the symbol is satisfied
8502 && jal_reloc_p (r_type)
8503 && !SYMBOL_CALLS_LOCAL (info, h))
8505 if (h->plt.plist == NULL)
8506 h->plt.plist = mips_elf_make_plt_record (abfd);
8507 if (h->plt.plist == NULL)
8510 if (r_type == R_MIPS_26)
8511 h->plt.plist->need_mips = TRUE;
8513 h->plt.plist->need_comp = TRUE;
8516 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8517 if there is one. We only need to handle global symbols here;
8518 we decide whether to keep or delete stubs for local symbols
8519 when processing the stub's relocations. */
8521 && !mips16_call_reloc_p (r_type)
8522 && !section_allows_mips16_refs_p (sec))
8524 struct mips_elf_link_hash_entry *mh;
8526 mh = (struct mips_elf_link_hash_entry *) h;
8527 mh->need_fn_stub = TRUE;
8530 /* Refuse some position-dependent relocations when creating a
8531 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8532 not PIC, but we can create dynamic relocations and the result
8533 will be fine. Also do not refuse R_MIPS_LO16, which can be
8534 combined with R_MIPS_GOT16. */
8542 case R_MIPS_HIGHEST:
8543 case R_MICROMIPS_HI16:
8544 case R_MICROMIPS_HIGHER:
8545 case R_MICROMIPS_HIGHEST:
8546 /* Don't refuse a high part relocation if it's against
8547 no symbol (e.g. part of a compound relocation). */
8548 if (r_symndx == STN_UNDEF)
8551 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8552 and has a special meaning. */
8553 if (!NEWABI_P (abfd) && h != NULL
8554 && strcmp (h->root.root.string, "_gp_disp") == 0)
8557 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8558 if (is_gott_symbol (info, h))
8565 case R_MICROMIPS_26_S1:
8566 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8567 (*_bfd_error_handler)
8568 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8570 (h) ? h->root.root.string : "a local symbol");
8571 bfd_set_error (bfd_error_bad_value);
8583 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8584 struct bfd_link_info *link_info,
8587 Elf_Internal_Rela *internal_relocs;
8588 Elf_Internal_Rela *irel, *irelend;
8589 Elf_Internal_Shdr *symtab_hdr;
8590 bfd_byte *contents = NULL;
8592 bfd_boolean changed_contents = FALSE;
8593 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8594 Elf_Internal_Sym *isymbuf = NULL;
8596 /* We are not currently changing any sizes, so only one pass. */
8599 if (link_info->relocatable)
8602 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8603 link_info->keep_memory);
8604 if (internal_relocs == NULL)
8607 irelend = internal_relocs + sec->reloc_count
8608 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8609 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8610 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8612 for (irel = internal_relocs; irel < irelend; irel++)
8615 bfd_signed_vma sym_offset;
8616 unsigned int r_type;
8617 unsigned long r_symndx;
8619 unsigned long instruction;
8621 /* Turn jalr into bgezal, and jr into beq, if they're marked
8622 with a JALR relocation, that indicate where they jump to.
8623 This saves some pipeline bubbles. */
8624 r_type = ELF_R_TYPE (abfd, irel->r_info);
8625 if (r_type != R_MIPS_JALR)
8628 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8629 /* Compute the address of the jump target. */
8630 if (r_symndx >= extsymoff)
8632 struct mips_elf_link_hash_entry *h
8633 = ((struct mips_elf_link_hash_entry *)
8634 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8636 while (h->root.root.type == bfd_link_hash_indirect
8637 || h->root.root.type == bfd_link_hash_warning)
8638 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8640 /* If a symbol is undefined, or if it may be overridden,
8642 if (! ((h->root.root.type == bfd_link_hash_defined
8643 || h->root.root.type == bfd_link_hash_defweak)
8644 && h->root.root.u.def.section)
8645 || (link_info->shared && ! link_info->symbolic
8646 && !h->root.forced_local))
8649 sym_sec = h->root.root.u.def.section;
8650 if (sym_sec->output_section)
8651 symval = (h->root.root.u.def.value
8652 + sym_sec->output_section->vma
8653 + sym_sec->output_offset);
8655 symval = h->root.root.u.def.value;
8659 Elf_Internal_Sym *isym;
8661 /* Read this BFD's symbols if we haven't done so already. */
8662 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8664 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8665 if (isymbuf == NULL)
8666 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8667 symtab_hdr->sh_info, 0,
8669 if (isymbuf == NULL)
8673 isym = isymbuf + r_symndx;
8674 if (isym->st_shndx == SHN_UNDEF)
8676 else if (isym->st_shndx == SHN_ABS)
8677 sym_sec = bfd_abs_section_ptr;
8678 else if (isym->st_shndx == SHN_COMMON)
8679 sym_sec = bfd_com_section_ptr;
8682 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8683 symval = isym->st_value
8684 + sym_sec->output_section->vma
8685 + sym_sec->output_offset;
8688 /* Compute branch offset, from delay slot of the jump to the
8690 sym_offset = (symval + irel->r_addend)
8691 - (sec_start + irel->r_offset + 4);
8693 /* Branch offset must be properly aligned. */
8694 if ((sym_offset & 3) != 0)
8699 /* Check that it's in range. */
8700 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8703 /* Get the section contents if we haven't done so already. */
8704 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8707 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8709 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8710 if ((instruction & 0xfc1fffff) == 0x0000f809)
8711 instruction = 0x04110000;
8712 /* If it was jr <reg>, turn it into b <target>. */
8713 else if ((instruction & 0xfc1fffff) == 0x00000008)
8714 instruction = 0x10000000;
8718 instruction |= (sym_offset & 0xffff);
8719 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8720 changed_contents = TRUE;
8723 if (contents != NULL
8724 && elf_section_data (sec)->this_hdr.contents != contents)
8726 if (!changed_contents && !link_info->keep_memory)
8730 /* Cache the section contents for elf_link_input_bfd. */
8731 elf_section_data (sec)->this_hdr.contents = contents;
8737 if (contents != NULL
8738 && elf_section_data (sec)->this_hdr.contents != contents)
8743 /* Allocate space for global sym dynamic relocs. */
8746 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8748 struct bfd_link_info *info = inf;
8750 struct mips_elf_link_hash_entry *hmips;
8751 struct mips_elf_link_hash_table *htab;
8753 htab = mips_elf_hash_table (info);
8754 BFD_ASSERT (htab != NULL);
8756 dynobj = elf_hash_table (info)->dynobj;
8757 hmips = (struct mips_elf_link_hash_entry *) h;
8759 /* VxWorks executables are handled elsewhere; we only need to
8760 allocate relocations in shared objects. */
8761 if (htab->is_vxworks && !info->shared)
8764 /* Ignore indirect symbols. All relocations against such symbols
8765 will be redirected to the target symbol. */
8766 if (h->root.type == bfd_link_hash_indirect)
8769 /* If this symbol is defined in a dynamic object, or we are creating
8770 a shared library, we will need to copy any R_MIPS_32 or
8771 R_MIPS_REL32 relocs against it into the output file. */
8772 if (! info->relocatable
8773 && hmips->possibly_dynamic_relocs != 0
8774 && (h->root.type == bfd_link_hash_defweak
8775 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8778 bfd_boolean do_copy = TRUE;
8780 if (h->root.type == bfd_link_hash_undefweak)
8782 /* Do not copy relocations for undefined weak symbols with
8783 non-default visibility. */
8784 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8787 /* Make sure undefined weak symbols are output as a dynamic
8789 else if (h->dynindx == -1 && !h->forced_local)
8791 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8798 /* Even though we don't directly need a GOT entry for this symbol,
8799 the SVR4 psABI requires it to have a dynamic symbol table
8800 index greater that DT_MIPS_GOTSYM if there are dynamic
8801 relocations against it.
8803 VxWorks does not enforce the same mapping between the GOT
8804 and the symbol table, so the same requirement does not
8806 if (!htab->is_vxworks)
8808 if (hmips->global_got_area > GGA_RELOC_ONLY)
8809 hmips->global_got_area = GGA_RELOC_ONLY;
8810 hmips->got_only_for_calls = FALSE;
8813 mips_elf_allocate_dynamic_relocations
8814 (dynobj, info, hmips->possibly_dynamic_relocs);
8815 if (hmips->readonly_reloc)
8816 /* We tell the dynamic linker that there are relocations
8817 against the text segment. */
8818 info->flags |= DF_TEXTREL;
8825 /* Adjust a symbol defined by a dynamic object and referenced by a
8826 regular object. The current definition is in some section of the
8827 dynamic object, but we're not including those sections. We have to
8828 change the definition to something the rest of the link can
8832 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8833 struct elf_link_hash_entry *h)
8836 struct mips_elf_link_hash_entry *hmips;
8837 struct mips_elf_link_hash_table *htab;
8839 htab = mips_elf_hash_table (info);
8840 BFD_ASSERT (htab != NULL);
8842 dynobj = elf_hash_table (info)->dynobj;
8843 hmips = (struct mips_elf_link_hash_entry *) h;
8845 /* Make sure we know what is going on here. */
8846 BFD_ASSERT (dynobj != NULL
8848 || h->u.weakdef != NULL
8851 && !h->def_regular)));
8853 hmips = (struct mips_elf_link_hash_entry *) h;
8855 /* If there are call relocations against an externally-defined symbol,
8856 see whether we can create a MIPS lazy-binding stub for it. We can
8857 only do this if all references to the function are through call
8858 relocations, and in that case, the traditional lazy-binding stubs
8859 are much more efficient than PLT entries.
8861 Traditional stubs are only available on SVR4 psABI-based systems;
8862 VxWorks always uses PLTs instead. */
8863 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8865 if (! elf_hash_table (info)->dynamic_sections_created)
8868 /* If this symbol is not defined in a regular file, then set
8869 the symbol to the stub location. This is required to make
8870 function pointers compare as equal between the normal
8871 executable and the shared library. */
8872 if (!h->def_regular)
8874 hmips->needs_lazy_stub = TRUE;
8875 htab->lazy_stub_count++;
8879 /* As above, VxWorks requires PLT entries for externally-defined
8880 functions that are only accessed through call relocations.
8882 Both VxWorks and non-VxWorks targets also need PLT entries if there
8883 are static-only relocations against an externally-defined function.
8884 This can technically occur for shared libraries if there are
8885 branches to the symbol, although it is unlikely that this will be
8886 used in practice due to the short ranges involved. It can occur
8887 for any relative or absolute relocation in executables; in that
8888 case, the PLT entry becomes the function's canonical address. */
8889 else if (((h->needs_plt && !hmips->no_fn_stub)
8890 || (h->type == STT_FUNC && hmips->has_static_relocs))
8891 && htab->use_plts_and_copy_relocs
8892 && !SYMBOL_CALLS_LOCAL (info, h)
8893 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8894 && h->root.type == bfd_link_hash_undefweak))
8896 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8897 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8899 /* If this is the first symbol to need a PLT entry, then make some
8900 basic setup. Also work out PLT entry sizes. We'll need them
8901 for PLT offset calculations. */
8902 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
8904 BFD_ASSERT (htab->sgotplt->size == 0);
8905 BFD_ASSERT (htab->plt_got_index == 0);
8907 /* If we're using the PLT additions to the psABI, each PLT
8908 entry is 16 bytes and the PLT0 entry is 32 bytes.
8909 Encourage better cache usage by aligning. We do this
8910 lazily to avoid pessimizing traditional objects. */
8911 if (!htab->is_vxworks
8912 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8915 /* Make sure that .got.plt is word-aligned. We do this lazily
8916 for the same reason as above. */
8917 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8918 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8921 /* On non-VxWorks targets, the first two entries in .got.plt
8923 if (!htab->is_vxworks)
8925 += (get_elf_backend_data (dynobj)->got_header_size
8926 / MIPS_ELF_GOT_SIZE (dynobj));
8928 /* On VxWorks, also allocate room for the header's
8929 .rela.plt.unloaded entries. */
8930 if (htab->is_vxworks && !info->shared)
8931 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8933 /* Now work out the sizes of individual PLT entries. */
8934 if (htab->is_vxworks && info->shared)
8935 htab->plt_mips_entry_size
8936 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8937 else if (htab->is_vxworks)
8938 htab->plt_mips_entry_size
8939 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8941 htab->plt_mips_entry_size
8942 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8943 else if (!micromips_p)
8945 htab->plt_mips_entry_size
8946 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8947 htab->plt_comp_entry_size
8948 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8950 else if (htab->insn32)
8952 htab->plt_mips_entry_size
8953 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8954 htab->plt_comp_entry_size
8955 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
8959 htab->plt_mips_entry_size
8960 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8961 htab->plt_comp_entry_size
8962 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
8966 if (h->plt.plist == NULL)
8967 h->plt.plist = mips_elf_make_plt_record (dynobj);
8968 if (h->plt.plist == NULL)
8971 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8972 n32 or n64, so always use a standard entry there.
8974 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8975 all MIPS16 calls will go via that stub, and there is no benefit
8976 to having a MIPS16 entry. And in the case of call_stub a
8977 standard entry actually has to be used as the stub ends with a J
8982 || hmips->call_fp_stub)
8984 h->plt.plist->need_mips = TRUE;
8985 h->plt.plist->need_comp = FALSE;
8988 /* Otherwise, if there are no direct calls to the function, we
8989 have a free choice of whether to use standard or compressed
8990 entries. Prefer microMIPS entries if the object is known to
8991 contain microMIPS code, so that it becomes possible to create
8992 pure microMIPS binaries. Prefer standard entries otherwise,
8993 because MIPS16 ones are no smaller and are usually slower. */
8994 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8997 h->plt.plist->need_comp = TRUE;
8999 h->plt.plist->need_mips = TRUE;
9002 if (h->plt.plist->need_mips)
9004 h->plt.plist->mips_offset = htab->plt_mips_offset;
9005 htab->plt_mips_offset += htab->plt_mips_entry_size;
9007 if (h->plt.plist->need_comp)
9009 h->plt.plist->comp_offset = htab->plt_comp_offset;
9010 htab->plt_comp_offset += htab->plt_comp_entry_size;
9013 /* Reserve the corresponding .got.plt entry now too. */
9014 h->plt.plist->gotplt_index = htab->plt_got_index++;
9016 /* If the output file has no definition of the symbol, set the
9017 symbol's value to the address of the stub. */
9018 if (!info->shared && !h->def_regular)
9019 hmips->use_plt_entry = TRUE;
9021 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9022 htab->srelplt->size += (htab->is_vxworks
9023 ? MIPS_ELF_RELA_SIZE (dynobj)
9024 : MIPS_ELF_REL_SIZE (dynobj));
9026 /* Make room for the .rela.plt.unloaded relocations. */
9027 if (htab->is_vxworks && !info->shared)
9028 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9030 /* All relocations against this symbol that could have been made
9031 dynamic will now refer to the PLT entry instead. */
9032 hmips->possibly_dynamic_relocs = 0;
9037 /* If this is a weak symbol, and there is a real definition, the
9038 processor independent code will have arranged for us to see the
9039 real definition first, and we can just use the same value. */
9040 if (h->u.weakdef != NULL)
9042 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
9043 || h->u.weakdef->root.type == bfd_link_hash_defweak);
9044 h->root.u.def.section = h->u.weakdef->root.u.def.section;
9045 h->root.u.def.value = h->u.weakdef->root.u.def.value;
9049 /* Otherwise, there is nothing further to do for symbols defined
9050 in regular objects. */
9054 /* There's also nothing more to do if we'll convert all relocations
9055 against this symbol into dynamic relocations. */
9056 if (!hmips->has_static_relocs)
9059 /* We're now relying on copy relocations. Complain if we have
9060 some that we can't convert. */
9061 if (!htab->use_plts_and_copy_relocs || info->shared)
9063 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
9064 "dynamic symbol %s"),
9065 h->root.root.string);
9066 bfd_set_error (bfd_error_bad_value);
9070 /* We must allocate the symbol in our .dynbss section, which will
9071 become part of the .bss section of the executable. There will be
9072 an entry for this symbol in the .dynsym section. The dynamic
9073 object will contain position independent code, so all references
9074 from the dynamic object to this symbol will go through the global
9075 offset table. The dynamic linker will use the .dynsym entry to
9076 determine the address it must put in the global offset table, so
9077 both the dynamic object and the regular object will refer to the
9078 same memory location for the variable. */
9080 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9082 if (htab->is_vxworks)
9083 htab->srelbss->size += sizeof (Elf32_External_Rela);
9085 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9089 /* All relocations against this symbol that could have been made
9090 dynamic will now refer to the local copy instead. */
9091 hmips->possibly_dynamic_relocs = 0;
9093 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
9096 /* This function is called after all the input files have been read,
9097 and the input sections have been assigned to output sections. We
9098 check for any mips16 stub sections that we can discard. */
9101 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9102 struct bfd_link_info *info)
9105 struct mips_elf_link_hash_table *htab;
9106 struct mips_htab_traverse_info hti;
9108 htab = mips_elf_hash_table (info);
9109 BFD_ASSERT (htab != NULL);
9111 /* The .reginfo section has a fixed size. */
9112 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9114 bfd_set_section_size (output_bfd, sect, sizeof (Elf32_External_RegInfo));
9116 /* The .MIPS.abiflags section has a fixed size. */
9117 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9119 bfd_set_section_size (output_bfd, sect, sizeof (Elf_External_ABIFlags_v0));
9122 hti.output_bfd = output_bfd;
9124 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9125 mips_elf_check_symbols, &hti);
9132 /* If the link uses a GOT, lay it out and work out its size. */
9135 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9139 struct mips_got_info *g;
9140 bfd_size_type loadable_size = 0;
9141 bfd_size_type page_gotno;
9143 struct mips_elf_traverse_got_arg tga;
9144 struct mips_elf_link_hash_table *htab;
9146 htab = mips_elf_hash_table (info);
9147 BFD_ASSERT (htab != NULL);
9153 dynobj = elf_hash_table (info)->dynobj;
9156 /* Allocate room for the reserved entries. VxWorks always reserves
9157 3 entries; other objects only reserve 2 entries. */
9158 BFD_ASSERT (g->assigned_low_gotno == 0);
9159 if (htab->is_vxworks)
9160 htab->reserved_gotno = 3;
9162 htab->reserved_gotno = 2;
9163 g->local_gotno += htab->reserved_gotno;
9164 g->assigned_low_gotno = htab->reserved_gotno;
9166 /* Decide which symbols need to go in the global part of the GOT and
9167 count the number of reloc-only GOT symbols. */
9168 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9170 if (!mips_elf_resolve_final_got_entries (info, g))
9173 /* Calculate the total loadable size of the output. That
9174 will give us the maximum number of GOT_PAGE entries
9176 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9178 asection *subsection;
9180 for (subsection = ibfd->sections;
9182 subsection = subsection->next)
9184 if ((subsection->flags & SEC_ALLOC) == 0)
9186 loadable_size += ((subsection->size + 0xf)
9187 &~ (bfd_size_type) 0xf);
9191 if (htab->is_vxworks)
9192 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9193 relocations against local symbols evaluate to "G", and the EABI does
9194 not include R_MIPS_GOT_PAGE. */
9197 /* Assume there are two loadable segments consisting of contiguous
9198 sections. Is 5 enough? */
9199 page_gotno = (loadable_size >> 16) + 5;
9201 /* Choose the smaller of the two page estimates; both are intended to be
9203 if (page_gotno > g->page_gotno)
9204 page_gotno = g->page_gotno;
9206 g->local_gotno += page_gotno;
9207 g->assigned_high_gotno = g->local_gotno - 1;
9209 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9210 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9211 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9213 /* VxWorks does not support multiple GOTs. It initializes $gp to
9214 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9216 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9218 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9223 /* Record that all bfds use G. This also has the effect of freeing
9224 the per-bfd GOTs, which we no longer need. */
9225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9226 if (mips_elf_bfd_got (ibfd, FALSE))
9227 mips_elf_replace_bfd_got (ibfd, g);
9228 mips_elf_replace_bfd_got (output_bfd, g);
9230 /* Set up TLS entries. */
9231 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9234 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9235 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9238 BFD_ASSERT (g->tls_assigned_gotno
9239 == g->global_gotno + g->local_gotno + g->tls_gotno);
9241 /* Each VxWorks GOT entry needs an explicit relocation. */
9242 if (htab->is_vxworks && info->shared)
9243 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9245 /* Allocate room for the TLS relocations. */
9247 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9253 /* Estimate the size of the .MIPS.stubs section. */
9256 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9258 struct mips_elf_link_hash_table *htab;
9259 bfd_size_type dynsymcount;
9261 htab = mips_elf_hash_table (info);
9262 BFD_ASSERT (htab != NULL);
9264 if (htab->lazy_stub_count == 0)
9267 /* IRIX rld assumes that a function stub isn't at the end of the .text
9268 section, so add a dummy entry to the end. */
9269 htab->lazy_stub_count++;
9271 /* Get a worst-case estimate of the number of dynamic symbols needed.
9272 At this point, dynsymcount does not account for section symbols
9273 and count_section_dynsyms may overestimate the number that will
9275 dynsymcount = (elf_hash_table (info)->dynsymcount
9276 + count_section_dynsyms (output_bfd, info));
9278 /* Determine the size of one stub entry. There's no disadvantage
9279 from using microMIPS code here, so for the sake of pure-microMIPS
9280 binaries we prefer it whenever there's any microMIPS code in
9281 output produced at all. This has a benefit of stubs being
9282 shorter by 4 bytes each too, unless in the insn32 mode. */
9283 if (!MICROMIPS_P (output_bfd))
9284 htab->function_stub_size = (dynsymcount > 0x10000
9285 ? MIPS_FUNCTION_STUB_BIG_SIZE
9286 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9287 else if (htab->insn32)
9288 htab->function_stub_size = (dynsymcount > 0x10000
9289 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9290 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9292 htab->function_stub_size = (dynsymcount > 0x10000
9293 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9294 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9296 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9299 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9300 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9301 stub, allocate an entry in the stubs section. */
9304 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9306 struct mips_htab_traverse_info *hti = data;
9307 struct mips_elf_link_hash_table *htab;
9308 struct bfd_link_info *info;
9312 output_bfd = hti->output_bfd;
9313 htab = mips_elf_hash_table (info);
9314 BFD_ASSERT (htab != NULL);
9316 if (h->needs_lazy_stub)
9318 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9319 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9320 bfd_vma isa_bit = micromips_p;
9322 BFD_ASSERT (htab->root.dynobj != NULL);
9323 if (h->root.plt.plist == NULL)
9324 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9325 if (h->root.plt.plist == NULL)
9330 h->root.root.u.def.section = htab->sstubs;
9331 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9332 h->root.plt.plist->stub_offset = htab->sstubs->size;
9333 h->root.other = other;
9334 htab->sstubs->size += htab->function_stub_size;
9339 /* Allocate offsets in the stubs section to each symbol that needs one.
9340 Set the final size of the .MIPS.stub section. */
9343 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9345 bfd *output_bfd = info->output_bfd;
9346 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9347 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9348 bfd_vma isa_bit = micromips_p;
9349 struct mips_elf_link_hash_table *htab;
9350 struct mips_htab_traverse_info hti;
9351 struct elf_link_hash_entry *h;
9354 htab = mips_elf_hash_table (info);
9355 BFD_ASSERT (htab != NULL);
9357 if (htab->lazy_stub_count == 0)
9360 htab->sstubs->size = 0;
9362 hti.output_bfd = output_bfd;
9364 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9367 htab->sstubs->size += htab->function_stub_size;
9368 BFD_ASSERT (htab->sstubs->size
9369 == htab->lazy_stub_count * htab->function_stub_size);
9371 dynobj = elf_hash_table (info)->dynobj;
9372 BFD_ASSERT (dynobj != NULL);
9373 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9376 h->root.u.def.value = isa_bit;
9383 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9384 bfd_link_info. If H uses the address of a PLT entry as the value
9385 of the symbol, then set the entry in the symbol table now. Prefer
9386 a standard MIPS PLT entry. */
9389 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9391 struct bfd_link_info *info = data;
9392 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9393 struct mips_elf_link_hash_table *htab;
9398 htab = mips_elf_hash_table (info);
9399 BFD_ASSERT (htab != NULL);
9401 if (h->use_plt_entry)
9403 BFD_ASSERT (h->root.plt.plist != NULL);
9404 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9405 || h->root.plt.plist->comp_offset != MINUS_ONE);
9407 val = htab->plt_header_size;
9408 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9411 val += h->root.plt.plist->mips_offset;
9417 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9418 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9421 /* For VxWorks, point at the PLT load stub rather than the lazy
9422 resolution stub; this stub will become the canonical function
9424 if (htab->is_vxworks)
9427 h->root.root.u.def.section = htab->splt;
9428 h->root.root.u.def.value = val;
9429 h->root.other = other;
9435 /* Set the sizes of the dynamic sections. */
9438 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9439 struct bfd_link_info *info)
9442 asection *s, *sreldyn;
9443 bfd_boolean reltext;
9444 struct mips_elf_link_hash_table *htab;
9446 htab = mips_elf_hash_table (info);
9447 BFD_ASSERT (htab != NULL);
9448 dynobj = elf_hash_table (info)->dynobj;
9449 BFD_ASSERT (dynobj != NULL);
9451 if (elf_hash_table (info)->dynamic_sections_created)
9453 /* Set the contents of the .interp section to the interpreter. */
9454 if (info->executable)
9456 s = bfd_get_linker_section (dynobj, ".interp");
9457 BFD_ASSERT (s != NULL);
9459 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9461 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9464 /* Figure out the size of the PLT header if we know that we
9465 are using it. For the sake of cache alignment always use
9466 a standard header whenever any standard entries are present
9467 even if microMIPS entries are present as well. This also
9468 lets the microMIPS header rely on the value of $v0 only set
9469 by microMIPS entries, for a small size reduction.
9471 Set symbol table entry values for symbols that use the
9472 address of their PLT entry now that we can calculate it.
9474 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9475 haven't already in _bfd_elf_create_dynamic_sections. */
9476 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9478 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9479 && !htab->plt_mips_offset);
9480 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9481 bfd_vma isa_bit = micromips_p;
9482 struct elf_link_hash_entry *h;
9485 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9486 BFD_ASSERT (htab->sgotplt->size == 0);
9487 BFD_ASSERT (htab->splt->size == 0);
9489 if (htab->is_vxworks && info->shared)
9490 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9491 else if (htab->is_vxworks)
9492 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9493 else if (ABI_64_P (output_bfd))
9494 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9495 else if (ABI_N32_P (output_bfd))
9496 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9497 else if (!micromips_p)
9498 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9499 else if (htab->insn32)
9500 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9502 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9504 htab->plt_header_is_comp = micromips_p;
9505 htab->plt_header_size = size;
9506 htab->splt->size = (size
9507 + htab->plt_mips_offset
9508 + htab->plt_comp_offset);
9509 htab->sgotplt->size = (htab->plt_got_index
9510 * MIPS_ELF_GOT_SIZE (dynobj));
9512 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9514 if (htab->root.hplt == NULL)
9516 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9517 "_PROCEDURE_LINKAGE_TABLE_");
9518 htab->root.hplt = h;
9523 h = htab->root.hplt;
9524 h->root.u.def.value = isa_bit;
9530 /* Allocate space for global sym dynamic relocs. */
9531 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9533 mips_elf_estimate_stub_size (output_bfd, info);
9535 if (!mips_elf_lay_out_got (output_bfd, info))
9538 mips_elf_lay_out_lazy_stubs (info);
9540 /* The check_relocs and adjust_dynamic_symbol entry points have
9541 determined the sizes of the various dynamic sections. Allocate
9544 for (s = dynobj->sections; s != NULL; s = s->next)
9548 /* It's OK to base decisions on the section name, because none
9549 of the dynobj section names depend upon the input files. */
9550 name = bfd_get_section_name (dynobj, s);
9552 if ((s->flags & SEC_LINKER_CREATED) == 0)
9555 if (CONST_STRNEQ (name, ".rel"))
9559 const char *outname;
9562 /* If this relocation section applies to a read only
9563 section, then we probably need a DT_TEXTREL entry.
9564 If the relocation section is .rel(a).dyn, we always
9565 assert a DT_TEXTREL entry rather than testing whether
9566 there exists a relocation to a read only section or
9568 outname = bfd_get_section_name (output_bfd,
9570 target = bfd_get_section_by_name (output_bfd, outname + 4);
9572 && (target->flags & SEC_READONLY) != 0
9573 && (target->flags & SEC_ALLOC) != 0)
9574 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9577 /* We use the reloc_count field as a counter if we need
9578 to copy relocs into the output file. */
9579 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9582 /* If combreloc is enabled, elf_link_sort_relocs() will
9583 sort relocations, but in a different way than we do,
9584 and before we're done creating relocations. Also, it
9585 will move them around between input sections'
9586 relocation's contents, so our sorting would be
9587 broken, so don't let it run. */
9588 info->combreloc = 0;
9591 else if (! info->shared
9592 && ! mips_elf_hash_table (info)->use_rld_obj_head
9593 && CONST_STRNEQ (name, ".rld_map"))
9595 /* We add a room for __rld_map. It will be filled in by the
9596 rtld to contain a pointer to the _r_debug structure. */
9597 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9599 else if (SGI_COMPAT (output_bfd)
9600 && CONST_STRNEQ (name, ".compact_rel"))
9601 s->size += mips_elf_hash_table (info)->compact_rel_size;
9602 else if (s == htab->splt)
9604 /* If the last PLT entry has a branch delay slot, allocate
9605 room for an extra nop to fill the delay slot. This is
9606 for CPUs without load interlocking. */
9607 if (! LOAD_INTERLOCKS_P (output_bfd)
9608 && ! htab->is_vxworks && s->size > 0)
9611 else if (! CONST_STRNEQ (name, ".init")
9613 && s != htab->sgotplt
9614 && s != htab->sstubs
9615 && s != htab->sdynbss)
9617 /* It's not one of our sections, so don't allocate space. */
9623 s->flags |= SEC_EXCLUDE;
9627 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9630 /* Allocate memory for the section contents. */
9631 s->contents = bfd_zalloc (dynobj, s->size);
9632 if (s->contents == NULL)
9634 bfd_set_error (bfd_error_no_memory);
9639 if (elf_hash_table (info)->dynamic_sections_created)
9641 /* Add some entries to the .dynamic section. We fill in the
9642 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9643 must add the entries now so that we get the correct size for
9644 the .dynamic section. */
9646 /* SGI object has the equivalence of DT_DEBUG in the
9647 DT_MIPS_RLD_MAP entry. This must come first because glibc
9648 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9649 may only look at the first one they see. */
9651 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9654 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9655 used by the debugger. */
9656 if (info->executable
9657 && !SGI_COMPAT (output_bfd)
9658 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9661 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9662 info->flags |= DF_TEXTREL;
9664 if ((info->flags & DF_TEXTREL) != 0)
9666 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9669 /* Clear the DF_TEXTREL flag. It will be set again if we
9670 write out an actual text relocation; we may not, because
9671 at this point we do not know whether e.g. any .eh_frame
9672 absolute relocations have been converted to PC-relative. */
9673 info->flags &= ~DF_TEXTREL;
9676 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9679 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9680 if (htab->is_vxworks)
9682 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9683 use any of the DT_MIPS_* tags. */
9684 if (sreldyn && sreldyn->size > 0)
9686 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9689 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9692 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9698 if (sreldyn && sreldyn->size > 0)
9700 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9703 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9706 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9710 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9713 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9716 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9719 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9722 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9725 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9728 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9731 if (IRIX_COMPAT (dynobj) == ict_irix5
9732 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9735 if (IRIX_COMPAT (dynobj) == ict_irix6
9736 && (bfd_get_section_by_name
9737 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9738 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9741 if (htab->splt->size > 0)
9743 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9746 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9749 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9752 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9755 if (htab->is_vxworks
9756 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9763 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9764 Adjust its R_ADDEND field so that it is correct for the output file.
9765 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9766 and sections respectively; both use symbol indexes. */
9769 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9770 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9771 asection **local_sections, Elf_Internal_Rela *rel)
9773 unsigned int r_type, r_symndx;
9774 Elf_Internal_Sym *sym;
9777 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9779 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9780 if (gprel16_reloc_p (r_type)
9781 || r_type == R_MIPS_GPREL32
9782 || literal_reloc_p (r_type))
9784 rel->r_addend += _bfd_get_gp_value (input_bfd);
9785 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9788 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9789 sym = local_syms + r_symndx;
9791 /* Adjust REL's addend to account for section merging. */
9792 if (!info->relocatable)
9794 sec = local_sections[r_symndx];
9795 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9798 /* This would normally be done by the rela_normal code in elflink.c. */
9799 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9800 rel->r_addend += local_sections[r_symndx]->output_offset;
9804 /* Handle relocations against symbols from removed linkonce sections,
9805 or sections discarded by a linker script. We use this wrapper around
9806 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9807 on 64-bit ELF targets. In this case for any relocation handled, which
9808 always be the first in a triplet, the remaining two have to be processed
9809 together with the first, even if they are R_MIPS_NONE. It is the symbol
9810 index referred by the first reloc that applies to all the three and the
9811 remaining two never refer to an object symbol. And it is the final
9812 relocation (the last non-null one) that determines the output field of
9813 the whole relocation so retrieve the corresponding howto structure for
9814 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9816 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9817 and therefore requires to be pasted in a loop. It also defines a block
9818 and does not protect any of its arguments, hence the extra brackets. */
9821 mips_reloc_against_discarded_section (bfd *output_bfd,
9822 struct bfd_link_info *info,
9823 bfd *input_bfd, asection *input_section,
9824 Elf_Internal_Rela **rel,
9825 const Elf_Internal_Rela **relend,
9826 bfd_boolean rel_reloc,
9827 reloc_howto_type *howto,
9830 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9831 int count = bed->s->int_rels_per_ext_rel;
9832 unsigned int r_type;
9835 for (i = count - 1; i > 0; i--)
9837 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9838 if (r_type != R_MIPS_NONE)
9840 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9846 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9847 (*rel), count, (*relend),
9848 howto, i, contents);
9853 /* Relocate a MIPS ELF section. */
9856 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9857 bfd *input_bfd, asection *input_section,
9858 bfd_byte *contents, Elf_Internal_Rela *relocs,
9859 Elf_Internal_Sym *local_syms,
9860 asection **local_sections)
9862 Elf_Internal_Rela *rel;
9863 const Elf_Internal_Rela *relend;
9865 bfd_boolean use_saved_addend_p = FALSE;
9866 const struct elf_backend_data *bed;
9868 bed = get_elf_backend_data (output_bfd);
9869 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9870 for (rel = relocs; rel < relend; ++rel)
9874 reloc_howto_type *howto;
9875 bfd_boolean cross_mode_jump_p = FALSE;
9876 /* TRUE if the relocation is a RELA relocation, rather than a
9878 bfd_boolean rela_relocation_p = TRUE;
9879 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9881 unsigned long r_symndx;
9883 Elf_Internal_Shdr *symtab_hdr;
9884 struct elf_link_hash_entry *h;
9885 bfd_boolean rel_reloc;
9887 rel_reloc = (NEWABI_P (input_bfd)
9888 && mips_elf_rel_relocation_p (input_bfd, input_section,
9890 /* Find the relocation howto for this relocation. */
9891 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9893 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9894 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9895 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9897 sec = local_sections[r_symndx];
9902 unsigned long extsymoff;
9905 if (!elf_bad_symtab (input_bfd))
9906 extsymoff = symtab_hdr->sh_info;
9907 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9908 while (h->root.type == bfd_link_hash_indirect
9909 || h->root.type == bfd_link_hash_warning)
9910 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9913 if (h->root.type == bfd_link_hash_defined
9914 || h->root.type == bfd_link_hash_defweak)
9915 sec = h->root.u.def.section;
9918 if (sec != NULL && discarded_section (sec))
9920 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9921 input_section, &rel, &relend,
9922 rel_reloc, howto, contents);
9926 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9928 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9929 64-bit code, but make sure all their addresses are in the
9930 lowermost or uppermost 32-bit section of the 64-bit address
9931 space. Thus, when they use an R_MIPS_64 they mean what is
9932 usually meant by R_MIPS_32, with the exception that the
9933 stored value is sign-extended to 64 bits. */
9934 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9936 /* On big-endian systems, we need to lie about the position
9938 if (bfd_big_endian (input_bfd))
9942 if (!use_saved_addend_p)
9944 /* If these relocations were originally of the REL variety,
9945 we must pull the addend out of the field that will be
9946 relocated. Otherwise, we simply use the contents of the
9948 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9951 rela_relocation_p = FALSE;
9952 addend = mips_elf_read_rel_addend (input_bfd, rel,
9954 if (hi16_reloc_p (r_type)
9955 || (got16_reloc_p (r_type)
9956 && mips_elf_local_relocation_p (input_bfd, rel,
9959 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9963 name = h->root.root.string;
9965 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9966 local_syms + r_symndx,
9968 (*_bfd_error_handler)
9969 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9970 input_bfd, input_section, name, howto->name,
9975 addend <<= howto->rightshift;
9978 addend = rel->r_addend;
9979 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9980 local_syms, local_sections, rel);
9983 if (info->relocatable)
9985 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9986 && bfd_big_endian (input_bfd))
9989 if (!rela_relocation_p && rel->r_addend)
9991 addend += rel->r_addend;
9992 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9993 addend = mips_elf_high (addend);
9994 else if (r_type == R_MIPS_HIGHER)
9995 addend = mips_elf_higher (addend);
9996 else if (r_type == R_MIPS_HIGHEST)
9997 addend = mips_elf_highest (addend);
9999 addend >>= howto->rightshift;
10001 /* We use the source mask, rather than the destination
10002 mask because the place to which we are writing will be
10003 source of the addend in the final link. */
10004 addend &= howto->src_mask;
10006 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10007 /* See the comment above about using R_MIPS_64 in the 32-bit
10008 ABI. Here, we need to update the addend. It would be
10009 possible to get away with just using the R_MIPS_32 reloc
10010 but for endianness. */
10016 if (addend & ((bfd_vma) 1 << 31))
10018 sign_bits = ((bfd_vma) 1 << 32) - 1;
10025 /* If we don't know that we have a 64-bit type,
10026 do two separate stores. */
10027 if (bfd_big_endian (input_bfd))
10029 /* Store the sign-bits (which are most significant)
10031 low_bits = sign_bits;
10032 high_bits = addend;
10037 high_bits = sign_bits;
10039 bfd_put_32 (input_bfd, low_bits,
10040 contents + rel->r_offset);
10041 bfd_put_32 (input_bfd, high_bits,
10042 contents + rel->r_offset + 4);
10046 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10047 input_bfd, input_section,
10052 /* Go on to the next relocation. */
10056 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10057 relocations for the same offset. In that case we are
10058 supposed to treat the output of each relocation as the addend
10060 if (rel + 1 < relend
10061 && rel->r_offset == rel[1].r_offset
10062 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10063 use_saved_addend_p = TRUE;
10065 use_saved_addend_p = FALSE;
10067 /* Figure out what value we are supposed to relocate. */
10068 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10069 input_section, info, rel,
10070 addend, howto, local_syms,
10071 local_sections, &value,
10072 &name, &cross_mode_jump_p,
10073 use_saved_addend_p))
10075 case bfd_reloc_continue:
10076 /* There's nothing to do. */
10079 case bfd_reloc_undefined:
10080 /* mips_elf_calculate_relocation already called the
10081 undefined_symbol callback. There's no real point in
10082 trying to perform the relocation at this point, so we
10083 just skip ahead to the next relocation. */
10086 case bfd_reloc_notsupported:
10087 msg = _("internal error: unsupported relocation error");
10088 info->callbacks->warning
10089 (info, msg, name, input_bfd, input_section, rel->r_offset);
10092 case bfd_reloc_overflow:
10093 if (use_saved_addend_p)
10094 /* Ignore overflow until we reach the last relocation for
10095 a given location. */
10099 struct mips_elf_link_hash_table *htab;
10101 htab = mips_elf_hash_table (info);
10102 BFD_ASSERT (htab != NULL);
10103 BFD_ASSERT (name != NULL);
10104 if (!htab->small_data_overflow_reported
10105 && (gprel16_reloc_p (howto->type)
10106 || literal_reloc_p (howto->type)))
10108 msg = _("small-data section exceeds 64KB;"
10109 " lower small-data size limit (see option -G)");
10111 htab->small_data_overflow_reported = TRUE;
10112 (*info->callbacks->einfo) ("%P: %s\n", msg);
10114 if (! ((*info->callbacks->reloc_overflow)
10115 (info, NULL, name, howto->name, (bfd_vma) 0,
10116 input_bfd, input_section, rel->r_offset)))
10124 case bfd_reloc_outofrange:
10125 if (jal_reloc_p (howto->type))
10127 msg = _("JALX to a non-word-aligned address");
10128 info->callbacks->warning
10129 (info, msg, name, input_bfd, input_section, rel->r_offset);
10132 /* Fall through. */
10139 /* If we've got another relocation for the address, keep going
10140 until we reach the last one. */
10141 if (use_saved_addend_p)
10147 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10148 /* See the comment above about using R_MIPS_64 in the 32-bit
10149 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10150 that calculated the right value. Now, however, we
10151 sign-extend the 32-bit result to 64-bits, and store it as a
10152 64-bit value. We are especially generous here in that we
10153 go to extreme lengths to support this usage on systems with
10154 only a 32-bit VMA. */
10160 if (value & ((bfd_vma) 1 << 31))
10162 sign_bits = ((bfd_vma) 1 << 32) - 1;
10169 /* If we don't know that we have a 64-bit type,
10170 do two separate stores. */
10171 if (bfd_big_endian (input_bfd))
10173 /* Undo what we did above. */
10174 rel->r_offset -= 4;
10175 /* Store the sign-bits (which are most significant)
10177 low_bits = sign_bits;
10183 high_bits = sign_bits;
10185 bfd_put_32 (input_bfd, low_bits,
10186 contents + rel->r_offset);
10187 bfd_put_32 (input_bfd, high_bits,
10188 contents + rel->r_offset + 4);
10192 /* Actually perform the relocation. */
10193 if (! mips_elf_perform_relocation (info, howto, rel, value,
10194 input_bfd, input_section,
10195 contents, cross_mode_jump_p))
10202 /* A function that iterates over each entry in la25_stubs and fills
10203 in the code for each one. DATA points to a mips_htab_traverse_info. */
10206 mips_elf_create_la25_stub (void **slot, void *data)
10208 struct mips_htab_traverse_info *hti;
10209 struct mips_elf_link_hash_table *htab;
10210 struct mips_elf_la25_stub *stub;
10213 bfd_vma offset, target, target_high, target_low;
10215 stub = (struct mips_elf_la25_stub *) *slot;
10216 hti = (struct mips_htab_traverse_info *) data;
10217 htab = mips_elf_hash_table (hti->info);
10218 BFD_ASSERT (htab != NULL);
10220 /* Create the section contents, if we haven't already. */
10221 s = stub->stub_section;
10225 loc = bfd_malloc (s->size);
10234 /* Work out where in the section this stub should go. */
10235 offset = stub->offset;
10237 /* Work out the target address. */
10238 target = mips_elf_get_la25_target (stub, &s);
10239 target += s->output_section->vma + s->output_offset;
10241 target_high = ((target + 0x8000) >> 16) & 0xffff;
10242 target_low = (target & 0xffff);
10244 if (stub->stub_section != htab->strampoline)
10246 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10247 of the section and write the two instructions at the end. */
10248 memset (loc, 0, offset);
10250 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10252 bfd_put_micromips_32 (hti->output_bfd,
10253 LA25_LUI_MICROMIPS (target_high),
10255 bfd_put_micromips_32 (hti->output_bfd,
10256 LA25_ADDIU_MICROMIPS (target_low),
10261 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10262 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10267 /* This is trampoline. */
10269 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10271 bfd_put_micromips_32 (hti->output_bfd,
10272 LA25_LUI_MICROMIPS (target_high), loc);
10273 bfd_put_micromips_32 (hti->output_bfd,
10274 LA25_J_MICROMIPS (target), loc + 4);
10275 bfd_put_micromips_32 (hti->output_bfd,
10276 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10277 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10281 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10282 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10283 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10284 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10290 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10291 adjust it appropriately now. */
10294 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10295 const char *name, Elf_Internal_Sym *sym)
10297 /* The linker script takes care of providing names and values for
10298 these, but we must place them into the right sections. */
10299 static const char* const text_section_symbols[] = {
10302 "__dso_displacement",
10304 "__program_header_table",
10308 static const char* const data_section_symbols[] = {
10316 const char* const *p;
10319 for (i = 0; i < 2; ++i)
10320 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10323 if (strcmp (*p, name) == 0)
10325 /* All of these symbols are given type STT_SECTION by the
10327 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10328 sym->st_other = STO_PROTECTED;
10330 /* The IRIX linker puts these symbols in special sections. */
10332 sym->st_shndx = SHN_MIPS_TEXT;
10334 sym->st_shndx = SHN_MIPS_DATA;
10340 /* Finish up dynamic symbol handling. We set the contents of various
10341 dynamic sections here. */
10344 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10345 struct bfd_link_info *info,
10346 struct elf_link_hash_entry *h,
10347 Elf_Internal_Sym *sym)
10351 struct mips_got_info *g, *gg;
10354 struct mips_elf_link_hash_table *htab;
10355 struct mips_elf_link_hash_entry *hmips;
10357 htab = mips_elf_hash_table (info);
10358 BFD_ASSERT (htab != NULL);
10359 dynobj = elf_hash_table (info)->dynobj;
10360 hmips = (struct mips_elf_link_hash_entry *) h;
10362 BFD_ASSERT (!htab->is_vxworks);
10364 if (h->plt.plist != NULL
10365 && (h->plt.plist->mips_offset != MINUS_ONE
10366 || h->plt.plist->comp_offset != MINUS_ONE))
10368 /* We've decided to create a PLT entry for this symbol. */
10370 bfd_vma header_address, got_address;
10371 bfd_vma got_address_high, got_address_low, load;
10375 got_index = h->plt.plist->gotplt_index;
10377 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10378 BFD_ASSERT (h->dynindx != -1);
10379 BFD_ASSERT (htab->splt != NULL);
10380 BFD_ASSERT (got_index != MINUS_ONE);
10381 BFD_ASSERT (!h->def_regular);
10383 /* Calculate the address of the PLT header. */
10384 isa_bit = htab->plt_header_is_comp;
10385 header_address = (htab->splt->output_section->vma
10386 + htab->splt->output_offset + isa_bit);
10388 /* Calculate the address of the .got.plt entry. */
10389 got_address = (htab->sgotplt->output_section->vma
10390 + htab->sgotplt->output_offset
10391 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10393 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10394 got_address_low = got_address & 0xffff;
10396 /* Initially point the .got.plt entry at the PLT header. */
10397 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10398 if (ABI_64_P (output_bfd))
10399 bfd_put_64 (output_bfd, header_address, loc);
10401 bfd_put_32 (output_bfd, header_address, loc);
10403 /* Now handle the PLT itself. First the standard entry (the order
10404 does not matter, we just have to pick one). */
10405 if (h->plt.plist->mips_offset != MINUS_ONE)
10407 const bfd_vma *plt_entry;
10408 bfd_vma plt_offset;
10410 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10412 BFD_ASSERT (plt_offset <= htab->splt->size);
10414 /* Find out where the .plt entry should go. */
10415 loc = htab->splt->contents + plt_offset;
10417 /* Pick the load opcode. */
10418 load = MIPS_ELF_LOAD_WORD (output_bfd);
10420 /* Fill in the PLT entry itself. */
10421 plt_entry = mips_exec_plt_entry;
10422 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10423 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10426 if (! LOAD_INTERLOCKS_P (output_bfd))
10428 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10429 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10433 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10434 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10439 /* Now the compressed entry. They come after any standard ones. */
10440 if (h->plt.plist->comp_offset != MINUS_ONE)
10442 bfd_vma plt_offset;
10444 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10445 + h->plt.plist->comp_offset);
10447 BFD_ASSERT (plt_offset <= htab->splt->size);
10449 /* Find out where the .plt entry should go. */
10450 loc = htab->splt->contents + plt_offset;
10452 /* Fill in the PLT entry itself. */
10453 if (!MICROMIPS_P (output_bfd))
10455 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10457 bfd_put_16 (output_bfd, plt_entry[0], loc);
10458 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10459 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10460 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10461 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10462 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10463 bfd_put_32 (output_bfd, got_address, loc + 12);
10465 else if (htab->insn32)
10467 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10469 bfd_put_16 (output_bfd, plt_entry[0], loc);
10470 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10471 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10472 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10473 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10474 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10475 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10476 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10480 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10481 bfd_signed_vma gotpc_offset;
10482 bfd_vma loc_address;
10484 BFD_ASSERT (got_address % 4 == 0);
10486 loc_address = (htab->splt->output_section->vma
10487 + htab->splt->output_offset + plt_offset);
10488 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10490 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10491 if (gotpc_offset + 0x1000000 >= 0x2000000)
10493 (*_bfd_error_handler)
10494 (_("%B: `%A' offset of %ld from `%A' "
10495 "beyond the range of ADDIUPC"),
10497 htab->sgotplt->output_section,
10498 htab->splt->output_section,
10499 (long) gotpc_offset);
10500 bfd_set_error (bfd_error_no_error);
10503 bfd_put_16 (output_bfd,
10504 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10505 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10506 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10507 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10508 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10509 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10513 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10514 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
10515 got_index - 2, h->dynindx,
10516 R_MIPS_JUMP_SLOT, got_address);
10518 /* We distinguish between PLT entries and lazy-binding stubs by
10519 giving the former an st_other value of STO_MIPS_PLT. Set the
10520 flag and leave the value if there are any relocations in the
10521 binary where pointer equality matters. */
10522 sym->st_shndx = SHN_UNDEF;
10523 if (h->pointer_equality_needed)
10524 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
10532 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
10534 /* We've decided to create a lazy-binding stub. */
10535 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10536 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10537 bfd_vma stub_size = htab->function_stub_size;
10538 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
10539 bfd_vma isa_bit = micromips_p;
10540 bfd_vma stub_big_size;
10543 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
10544 else if (htab->insn32)
10545 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10547 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
10549 /* This symbol has a stub. Set it up. */
10551 BFD_ASSERT (h->dynindx != -1);
10553 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
10555 /* Values up to 2^31 - 1 are allowed. Larger values would cause
10556 sign extension at runtime in the stub, resulting in a negative
10558 if (h->dynindx & ~0x7fffffff)
10561 /* Fill the stub. */
10565 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10570 bfd_put_micromips_32 (output_bfd,
10571 STUB_MOVE32_MICROMIPS (output_bfd),
10577 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10580 if (stub_size == stub_big_size)
10582 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10584 bfd_put_micromips_32 (output_bfd,
10585 STUB_LUI_MICROMIPS (dynindx_hi),
10591 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10597 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10601 /* If a large stub is not required and sign extension is not a
10602 problem, then use legacy code in the stub. */
10603 if (stub_size == stub_big_size)
10604 bfd_put_micromips_32 (output_bfd,
10605 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10607 else if (h->dynindx & ~0x7fff)
10608 bfd_put_micromips_32 (output_bfd,
10609 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10612 bfd_put_micromips_32 (output_bfd,
10613 STUB_LI16S_MICROMIPS (output_bfd,
10620 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10622 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10624 if (stub_size == stub_big_size)
10626 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10630 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10633 /* If a large stub is not required and sign extension is not a
10634 problem, then use legacy code in the stub. */
10635 if (stub_size == stub_big_size)
10636 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10638 else if (h->dynindx & ~0x7fff)
10639 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10642 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10646 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10647 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10650 /* Mark the symbol as undefined. stub_offset != -1 occurs
10651 only for the referenced symbol. */
10652 sym->st_shndx = SHN_UNDEF;
10654 /* The run-time linker uses the st_value field of the symbol
10655 to reset the global offset table entry for this external
10656 to its stub address when unlinking a shared object. */
10657 sym->st_value = (htab->sstubs->output_section->vma
10658 + htab->sstubs->output_offset
10659 + h->plt.plist->stub_offset
10661 sym->st_other = other;
10664 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10665 refer to the stub, since only the stub uses the standard calling
10667 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10669 BFD_ASSERT (hmips->need_fn_stub);
10670 sym->st_value = (hmips->fn_stub->output_section->vma
10671 + hmips->fn_stub->output_offset);
10672 sym->st_size = hmips->fn_stub->size;
10673 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10676 BFD_ASSERT (h->dynindx != -1
10677 || h->forced_local);
10680 g = htab->got_info;
10681 BFD_ASSERT (g != NULL);
10683 /* Run through the global symbol table, creating GOT entries for all
10684 the symbols that need them. */
10685 if (hmips->global_got_area != GGA_NONE)
10690 value = sym->st_value;
10691 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10692 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10695 if (hmips->global_got_area != GGA_NONE && g->next)
10697 struct mips_got_entry e, *p;
10703 e.abfd = output_bfd;
10706 e.tls_type = GOT_TLS_NONE;
10708 for (g = g->next; g->next != gg; g = g->next)
10711 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10714 offset = p->gotidx;
10715 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
10717 || (elf_hash_table (info)->dynamic_sections_created
10719 && p->d.h->root.def_dynamic
10720 && !p->d.h->root.def_regular))
10722 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10723 the various compatibility problems, it's easier to mock
10724 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10725 mips_elf_create_dynamic_relocation to calculate the
10726 appropriate addend. */
10727 Elf_Internal_Rela rel[3];
10729 memset (rel, 0, sizeof (rel));
10730 if (ABI_64_P (output_bfd))
10731 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10733 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10734 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10737 if (! (mips_elf_create_dynamic_relocation
10738 (output_bfd, info, rel,
10739 e.d.h, NULL, sym->st_value, &entry, sgot)))
10743 entry = sym->st_value;
10744 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10749 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10750 name = h->root.root.string;
10751 if (h == elf_hash_table (info)->hdynamic
10752 || h == elf_hash_table (info)->hgot)
10753 sym->st_shndx = SHN_ABS;
10754 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10755 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10757 sym->st_shndx = SHN_ABS;
10758 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10761 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10763 sym->st_shndx = SHN_ABS;
10764 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10765 sym->st_value = elf_gp (output_bfd);
10767 else if (SGI_COMPAT (output_bfd))
10769 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10770 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10772 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10773 sym->st_other = STO_PROTECTED;
10775 sym->st_shndx = SHN_MIPS_DATA;
10777 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10779 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10780 sym->st_other = STO_PROTECTED;
10781 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10782 sym->st_shndx = SHN_ABS;
10784 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10786 if (h->type == STT_FUNC)
10787 sym->st_shndx = SHN_MIPS_TEXT;
10788 else if (h->type == STT_OBJECT)
10789 sym->st_shndx = SHN_MIPS_DATA;
10793 /* Emit a copy reloc, if needed. */
10799 BFD_ASSERT (h->dynindx != -1);
10800 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10802 s = mips_elf_rel_dyn_section (info, FALSE);
10803 symval = (h->root.u.def.section->output_section->vma
10804 + h->root.u.def.section->output_offset
10805 + h->root.u.def.value);
10806 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10807 h->dynindx, R_MIPS_COPY, symval);
10810 /* Handle the IRIX6-specific symbols. */
10811 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10812 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10814 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10815 to treat compressed symbols like any other. */
10816 if (ELF_ST_IS_MIPS16 (sym->st_other))
10818 BFD_ASSERT (sym->st_value & 1);
10819 sym->st_other -= STO_MIPS16;
10821 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10823 BFD_ASSERT (sym->st_value & 1);
10824 sym->st_other -= STO_MICROMIPS;
10830 /* Likewise, for VxWorks. */
10833 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10834 struct bfd_link_info *info,
10835 struct elf_link_hash_entry *h,
10836 Elf_Internal_Sym *sym)
10840 struct mips_got_info *g;
10841 struct mips_elf_link_hash_table *htab;
10842 struct mips_elf_link_hash_entry *hmips;
10844 htab = mips_elf_hash_table (info);
10845 BFD_ASSERT (htab != NULL);
10846 dynobj = elf_hash_table (info)->dynobj;
10847 hmips = (struct mips_elf_link_hash_entry *) h;
10849 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
10852 bfd_vma plt_address, got_address, got_offset, branch_offset;
10853 Elf_Internal_Rela rel;
10854 static const bfd_vma *plt_entry;
10855 bfd_vma gotplt_index;
10856 bfd_vma plt_offset;
10858 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10859 gotplt_index = h->plt.plist->gotplt_index;
10861 BFD_ASSERT (h->dynindx != -1);
10862 BFD_ASSERT (htab->splt != NULL);
10863 BFD_ASSERT (gotplt_index != MINUS_ONE);
10864 BFD_ASSERT (plt_offset <= htab->splt->size);
10866 /* Calculate the address of the .plt entry. */
10867 plt_address = (htab->splt->output_section->vma
10868 + htab->splt->output_offset
10871 /* Calculate the address of the .got.plt entry. */
10872 got_address = (htab->sgotplt->output_section->vma
10873 + htab->sgotplt->output_offset
10874 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
10876 /* Calculate the offset of the .got.plt entry from
10877 _GLOBAL_OFFSET_TABLE_. */
10878 got_offset = mips_elf_gotplt_index (info, h);
10880 /* Calculate the offset for the branch at the start of the PLT
10881 entry. The branch jumps to the beginning of .plt. */
10882 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
10884 /* Fill in the initial value of the .got.plt entry. */
10885 bfd_put_32 (output_bfd, plt_address,
10886 (htab->sgotplt->contents
10887 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
10889 /* Find out where the .plt entry should go. */
10890 loc = htab->splt->contents + plt_offset;
10894 plt_entry = mips_vxworks_shared_plt_entry;
10895 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10896 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10900 bfd_vma got_address_high, got_address_low;
10902 plt_entry = mips_vxworks_exec_plt_entry;
10903 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10904 got_address_low = got_address & 0xffff;
10906 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10907 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10908 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10909 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10910 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10911 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10912 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10913 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10915 loc = (htab->srelplt2->contents
10916 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10918 /* Emit a relocation for the .got.plt entry. */
10919 rel.r_offset = got_address;
10920 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10921 rel.r_addend = plt_offset;
10922 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10924 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10925 loc += sizeof (Elf32_External_Rela);
10926 rel.r_offset = plt_address + 8;
10927 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10928 rel.r_addend = got_offset;
10929 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10931 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10932 loc += sizeof (Elf32_External_Rela);
10934 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10935 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10938 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10939 loc = (htab->srelplt->contents
10940 + gotplt_index * sizeof (Elf32_External_Rela));
10941 rel.r_offset = got_address;
10942 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10944 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10946 if (!h->def_regular)
10947 sym->st_shndx = SHN_UNDEF;
10950 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10953 g = htab->got_info;
10954 BFD_ASSERT (g != NULL);
10956 /* See if this symbol has an entry in the GOT. */
10957 if (hmips->global_got_area != GGA_NONE)
10960 Elf_Internal_Rela outrel;
10964 /* Install the symbol value in the GOT. */
10965 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10966 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10968 /* Add a dynamic relocation for it. */
10969 s = mips_elf_rel_dyn_section (info, FALSE);
10970 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10971 outrel.r_offset = (sgot->output_section->vma
10972 + sgot->output_offset
10974 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10975 outrel.r_addend = 0;
10976 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10979 /* Emit a copy reloc, if needed. */
10982 Elf_Internal_Rela rel;
10984 BFD_ASSERT (h->dynindx != -1);
10986 rel.r_offset = (h->root.u.def.section->output_section->vma
10987 + h->root.u.def.section->output_offset
10988 + h->root.u.def.value);
10989 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10991 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10992 htab->srelbss->contents
10993 + (htab->srelbss->reloc_count
10994 * sizeof (Elf32_External_Rela)));
10995 ++htab->srelbss->reloc_count;
10998 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10999 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11000 sym->st_value &= ~1;
11005 /* Write out a plt0 entry to the beginning of .plt. */
11008 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11011 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11012 static const bfd_vma *plt_entry;
11013 struct mips_elf_link_hash_table *htab;
11015 htab = mips_elf_hash_table (info);
11016 BFD_ASSERT (htab != NULL);
11018 if (ABI_64_P (output_bfd))
11019 plt_entry = mips_n64_exec_plt0_entry;
11020 else if (ABI_N32_P (output_bfd))
11021 plt_entry = mips_n32_exec_plt0_entry;
11022 else if (!htab->plt_header_is_comp)
11023 plt_entry = mips_o32_exec_plt0_entry;
11024 else if (htab->insn32)
11025 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11027 plt_entry = micromips_o32_exec_plt0_entry;
11029 /* Calculate the value of .got.plt. */
11030 gotplt_value = (htab->sgotplt->output_section->vma
11031 + htab->sgotplt->output_offset);
11032 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11033 gotplt_value_low = gotplt_value & 0xffff;
11035 /* The PLT sequence is not safe for N64 if .got.plt's address can
11036 not be loaded in two instructions. */
11037 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
11038 || ~(gotplt_value | 0x7fffffff) == 0);
11040 /* Install the PLT header. */
11041 loc = htab->splt->contents;
11042 if (plt_entry == micromips_o32_exec_plt0_entry)
11044 bfd_vma gotpc_offset;
11045 bfd_vma loc_address;
11048 BFD_ASSERT (gotplt_value % 4 == 0);
11050 loc_address = (htab->splt->output_section->vma
11051 + htab->splt->output_offset);
11052 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11054 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11055 if (gotpc_offset + 0x1000000 >= 0x2000000)
11057 (*_bfd_error_handler)
11058 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
11060 htab->sgotplt->output_section,
11061 htab->splt->output_section,
11062 (long) gotpc_offset);
11063 bfd_set_error (bfd_error_no_error);
11066 bfd_put_16 (output_bfd,
11067 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11068 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11069 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11070 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11072 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11076 bfd_put_16 (output_bfd, plt_entry[0], loc);
11077 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11078 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11079 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11080 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11081 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11082 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11083 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11087 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11088 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11089 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11090 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11091 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11092 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11093 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11094 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11100 /* Install the PLT header for a VxWorks executable and finalize the
11101 contents of .rela.plt.unloaded. */
11104 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11106 Elf_Internal_Rela rela;
11108 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11109 static const bfd_vma *plt_entry;
11110 struct mips_elf_link_hash_table *htab;
11112 htab = mips_elf_hash_table (info);
11113 BFD_ASSERT (htab != NULL);
11115 plt_entry = mips_vxworks_exec_plt0_entry;
11117 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11118 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11119 + htab->root.hgot->root.u.def.section->output_offset
11120 + htab->root.hgot->root.u.def.value);
11122 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11123 got_value_low = got_value & 0xffff;
11125 /* Calculate the address of the PLT header. */
11126 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11128 /* Install the PLT header. */
11129 loc = htab->splt->contents;
11130 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11131 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11132 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11133 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11134 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11135 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11137 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11138 loc = htab->srelplt2->contents;
11139 rela.r_offset = plt_address;
11140 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11142 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11143 loc += sizeof (Elf32_External_Rela);
11145 /* Output the relocation for the following addiu of
11146 %lo(_GLOBAL_OFFSET_TABLE_). */
11147 rela.r_offset += 4;
11148 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11149 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11150 loc += sizeof (Elf32_External_Rela);
11152 /* Fix up the remaining relocations. They may have the wrong
11153 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11154 in which symbols were output. */
11155 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11157 Elf_Internal_Rela rel;
11159 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11160 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11161 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11162 loc += sizeof (Elf32_External_Rela);
11164 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11165 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11166 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11167 loc += sizeof (Elf32_External_Rela);
11169 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11170 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11171 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11172 loc += sizeof (Elf32_External_Rela);
11176 /* Install the PLT header for a VxWorks shared library. */
11179 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11182 struct mips_elf_link_hash_table *htab;
11184 htab = mips_elf_hash_table (info);
11185 BFD_ASSERT (htab != NULL);
11187 /* We just need to copy the entry byte-by-byte. */
11188 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11189 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11190 htab->splt->contents + i * 4);
11193 /* Finish up the dynamic sections. */
11196 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11197 struct bfd_link_info *info)
11202 struct mips_got_info *gg, *g;
11203 struct mips_elf_link_hash_table *htab;
11205 htab = mips_elf_hash_table (info);
11206 BFD_ASSERT (htab != NULL);
11208 dynobj = elf_hash_table (info)->dynobj;
11210 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11213 gg = htab->got_info;
11215 if (elf_hash_table (info)->dynamic_sections_created)
11218 int dyn_to_skip = 0, dyn_skipped = 0;
11220 BFD_ASSERT (sdyn != NULL);
11221 BFD_ASSERT (gg != NULL);
11223 g = mips_elf_bfd_got (output_bfd, FALSE);
11224 BFD_ASSERT (g != NULL);
11226 for (b = sdyn->contents;
11227 b < sdyn->contents + sdyn->size;
11228 b += MIPS_ELF_DYN_SIZE (dynobj))
11230 Elf_Internal_Dyn dyn;
11234 bfd_boolean swap_out_p;
11236 /* Read in the current dynamic entry. */
11237 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11239 /* Assume that we're going to modify it and write it out. */
11245 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11249 BFD_ASSERT (htab->is_vxworks);
11250 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11254 /* Rewrite DT_STRSZ. */
11256 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11261 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11264 case DT_MIPS_PLTGOT:
11266 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11269 case DT_MIPS_RLD_VERSION:
11270 dyn.d_un.d_val = 1; /* XXX */
11273 case DT_MIPS_FLAGS:
11274 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11277 case DT_MIPS_TIME_STAMP:
11281 dyn.d_un.d_val = t;
11285 case DT_MIPS_ICHECKSUM:
11287 swap_out_p = FALSE;
11290 case DT_MIPS_IVERSION:
11292 swap_out_p = FALSE;
11295 case DT_MIPS_BASE_ADDRESS:
11296 s = output_bfd->sections;
11297 BFD_ASSERT (s != NULL);
11298 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11301 case DT_MIPS_LOCAL_GOTNO:
11302 dyn.d_un.d_val = g->local_gotno;
11305 case DT_MIPS_UNREFEXTNO:
11306 /* The index into the dynamic symbol table which is the
11307 entry of the first external symbol that is not
11308 referenced within the same object. */
11309 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11312 case DT_MIPS_GOTSYM:
11313 if (htab->global_gotsym)
11315 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11318 /* In case if we don't have global got symbols we default
11319 to setting DT_MIPS_GOTSYM to the same value as
11320 DT_MIPS_SYMTABNO, so we just fall through. */
11322 case DT_MIPS_SYMTABNO:
11324 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11325 s = bfd_get_section_by_name (output_bfd, name);
11326 BFD_ASSERT (s != NULL);
11328 dyn.d_un.d_val = s->size / elemsize;
11331 case DT_MIPS_HIPAGENO:
11332 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11335 case DT_MIPS_RLD_MAP:
11337 struct elf_link_hash_entry *h;
11338 h = mips_elf_hash_table (info)->rld_symbol;
11341 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11342 swap_out_p = FALSE;
11345 s = h->root.u.def.section;
11346 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11347 + h->root.u.def.value);
11351 case DT_MIPS_OPTIONS:
11352 s = (bfd_get_section_by_name
11353 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11354 dyn.d_un.d_ptr = s->vma;
11358 BFD_ASSERT (htab->is_vxworks);
11359 /* The count does not include the JUMP_SLOT relocations. */
11361 dyn.d_un.d_val -= htab->srelplt->size;
11365 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11366 if (htab->is_vxworks)
11367 dyn.d_un.d_val = DT_RELA;
11369 dyn.d_un.d_val = DT_REL;
11373 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11374 dyn.d_un.d_val = htab->srelplt->size;
11378 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11379 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
11380 + htab->srelplt->output_offset);
11384 /* If we didn't need any text relocations after all, delete
11385 the dynamic tag. */
11386 if (!(info->flags & DF_TEXTREL))
11388 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11389 swap_out_p = FALSE;
11394 /* If we didn't need any text relocations after all, clear
11395 DF_TEXTREL from DT_FLAGS. */
11396 if (!(info->flags & DF_TEXTREL))
11397 dyn.d_un.d_val &= ~DF_TEXTREL;
11399 swap_out_p = FALSE;
11403 swap_out_p = FALSE;
11404 if (htab->is_vxworks
11405 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11410 if (swap_out_p || dyn_skipped)
11411 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11412 (dynobj, &dyn, b - dyn_skipped);
11416 dyn_skipped += dyn_to_skip;
11421 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11422 if (dyn_skipped > 0)
11423 memset (b - dyn_skipped, 0, dyn_skipped);
11426 if (sgot != NULL && sgot->size > 0
11427 && !bfd_is_abs_section (sgot->output_section))
11429 if (htab->is_vxworks)
11431 /* The first entry of the global offset table points to the
11432 ".dynamic" section. The second is initialized by the
11433 loader and contains the shared library identifier.
11434 The third is also initialized by the loader and points
11435 to the lazy resolution stub. */
11436 MIPS_ELF_PUT_WORD (output_bfd,
11437 sdyn->output_offset + sdyn->output_section->vma,
11439 MIPS_ELF_PUT_WORD (output_bfd, 0,
11440 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11441 MIPS_ELF_PUT_WORD (output_bfd, 0,
11443 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11447 /* The first entry of the global offset table will be filled at
11448 runtime. The second entry will be used by some runtime loaders.
11449 This isn't the case of IRIX rld. */
11450 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
11451 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11452 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11455 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11456 = MIPS_ELF_GOT_SIZE (output_bfd);
11459 /* Generate dynamic relocations for the non-primary gots. */
11460 if (gg != NULL && gg->next)
11462 Elf_Internal_Rela rel[3];
11463 bfd_vma addend = 0;
11465 memset (rel, 0, sizeof (rel));
11466 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11468 for (g = gg->next; g->next != gg; g = g->next)
11470 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
11471 + g->next->tls_gotno;
11473 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
11474 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11475 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11477 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11479 if (! info->shared)
11482 for (; got_index < g->local_gotno; got_index++)
11484 if (got_index >= g->assigned_low_gotno
11485 && got_index <= g->assigned_high_gotno)
11488 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
11489 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
11490 if (!(mips_elf_create_dynamic_relocation
11491 (output_bfd, info, rel, NULL,
11492 bfd_abs_section_ptr,
11493 0, &addend, sgot)))
11495 BFD_ASSERT (addend == 0);
11500 /* The generation of dynamic relocations for the non-primary gots
11501 adds more dynamic relocations. We cannot count them until
11504 if (elf_hash_table (info)->dynamic_sections_created)
11507 bfd_boolean swap_out_p;
11509 BFD_ASSERT (sdyn != NULL);
11511 for (b = sdyn->contents;
11512 b < sdyn->contents + sdyn->size;
11513 b += MIPS_ELF_DYN_SIZE (dynobj))
11515 Elf_Internal_Dyn dyn;
11518 /* Read in the current dynamic entry. */
11519 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11521 /* Assume that we're going to modify it and write it out. */
11527 /* Reduce DT_RELSZ to account for any relocations we
11528 decided not to make. This is for the n64 irix rld,
11529 which doesn't seem to apply any relocations if there
11530 are trailing null entries. */
11531 s = mips_elf_rel_dyn_section (info, FALSE);
11532 dyn.d_un.d_val = (s->reloc_count
11533 * (ABI_64_P (output_bfd)
11534 ? sizeof (Elf64_Mips_External_Rel)
11535 : sizeof (Elf32_External_Rel)));
11536 /* Adjust the section size too. Tools like the prelinker
11537 can reasonably expect the values to the same. */
11538 elf_section_data (s->output_section)->this_hdr.sh_size
11543 swap_out_p = FALSE;
11548 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11555 Elf32_compact_rel cpt;
11557 if (SGI_COMPAT (output_bfd))
11559 /* Write .compact_rel section out. */
11560 s = bfd_get_linker_section (dynobj, ".compact_rel");
11564 cpt.num = s->reloc_count;
11566 cpt.offset = (s->output_section->filepos
11567 + sizeof (Elf32_External_compact_rel));
11570 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11571 ((Elf32_External_compact_rel *)
11574 /* Clean up a dummy stub function entry in .text. */
11575 if (htab->sstubs != NULL)
11577 file_ptr dummy_offset;
11579 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11580 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11581 memset (htab->sstubs->contents + dummy_offset, 0,
11582 htab->function_stub_size);
11587 /* The psABI says that the dynamic relocations must be sorted in
11588 increasing order of r_symndx. The VxWorks EABI doesn't require
11589 this, and because the code below handles REL rather than RELA
11590 relocations, using it for VxWorks would be outright harmful. */
11591 if (!htab->is_vxworks)
11593 s = mips_elf_rel_dyn_section (info, FALSE);
11595 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11597 reldyn_sorting_bfd = output_bfd;
11599 if (ABI_64_P (output_bfd))
11600 qsort ((Elf64_External_Rel *) s->contents + 1,
11601 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11602 sort_dynamic_relocs_64);
11604 qsort ((Elf32_External_Rel *) s->contents + 1,
11605 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11606 sort_dynamic_relocs);
11611 if (htab->splt && htab->splt->size > 0)
11613 if (htab->is_vxworks)
11616 mips_vxworks_finish_shared_plt (output_bfd, info);
11618 mips_vxworks_finish_exec_plt (output_bfd, info);
11622 BFD_ASSERT (!info->shared);
11623 if (!mips_finish_exec_plt (output_bfd, info))
11631 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11634 mips_set_isa_flags (bfd *abfd)
11638 switch (bfd_get_mach (abfd))
11641 case bfd_mach_mips3000:
11642 val = E_MIPS_ARCH_1;
11645 case bfd_mach_mips3900:
11646 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11649 case bfd_mach_mips6000:
11650 val = E_MIPS_ARCH_2;
11653 case bfd_mach_mips4000:
11654 case bfd_mach_mips4300:
11655 case bfd_mach_mips4400:
11656 case bfd_mach_mips4600:
11657 val = E_MIPS_ARCH_3;
11660 case bfd_mach_mips4010:
11661 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11664 case bfd_mach_mips4100:
11665 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11668 case bfd_mach_mips4111:
11669 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11672 case bfd_mach_mips4120:
11673 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11676 case bfd_mach_mips4650:
11677 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11680 case bfd_mach_mips5400:
11681 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11684 case bfd_mach_mips5500:
11685 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11688 case bfd_mach_mips5900:
11689 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11692 case bfd_mach_mips9000:
11693 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11696 case bfd_mach_mips5000:
11697 case bfd_mach_mips7000:
11698 case bfd_mach_mips8000:
11699 case bfd_mach_mips10000:
11700 case bfd_mach_mips12000:
11701 case bfd_mach_mips14000:
11702 case bfd_mach_mips16000:
11703 val = E_MIPS_ARCH_4;
11706 case bfd_mach_mips5:
11707 val = E_MIPS_ARCH_5;
11710 case bfd_mach_mips_loongson_2e:
11711 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11714 case bfd_mach_mips_loongson_2f:
11715 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11718 case bfd_mach_mips_sb1:
11719 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11722 case bfd_mach_mips_loongson_3a:
11723 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
11726 case bfd_mach_mips_octeon:
11727 case bfd_mach_mips_octeonp:
11728 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11731 case bfd_mach_mips_xlr:
11732 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11735 case bfd_mach_mips_octeon2:
11736 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11739 case bfd_mach_mipsisa32:
11740 val = E_MIPS_ARCH_32;
11743 case bfd_mach_mipsisa64:
11744 val = E_MIPS_ARCH_64;
11747 case bfd_mach_mipsisa32r2:
11748 case bfd_mach_mipsisa32r3:
11749 case bfd_mach_mipsisa32r5:
11750 val = E_MIPS_ARCH_32R2;
11753 case bfd_mach_mipsisa64r2:
11754 case bfd_mach_mipsisa64r3:
11755 case bfd_mach_mipsisa64r5:
11756 val = E_MIPS_ARCH_64R2;
11759 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11760 elf_elfheader (abfd)->e_flags |= val;
11765 /* The final processing done just before writing out a MIPS ELF object
11766 file. This gets the MIPS architecture right based on the machine
11767 number. This is used by both the 32-bit and the 64-bit ABI. */
11770 _bfd_mips_elf_final_write_processing (bfd *abfd,
11771 bfd_boolean linker ATTRIBUTE_UNUSED)
11774 Elf_Internal_Shdr **hdrpp;
11778 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11779 is nonzero. This is for compatibility with old objects, which used
11780 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11781 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11782 mips_set_isa_flags (abfd);
11784 /* Set the sh_info field for .gptab sections and other appropriate
11785 info for each special section. */
11786 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11787 i < elf_numsections (abfd);
11790 switch ((*hdrpp)->sh_type)
11792 case SHT_MIPS_MSYM:
11793 case SHT_MIPS_LIBLIST:
11794 sec = bfd_get_section_by_name (abfd, ".dynstr");
11796 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11799 case SHT_MIPS_GPTAB:
11800 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11801 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11802 BFD_ASSERT (name != NULL
11803 && CONST_STRNEQ (name, ".gptab."));
11804 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11805 BFD_ASSERT (sec != NULL);
11806 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11809 case SHT_MIPS_CONTENT:
11810 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11811 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11812 BFD_ASSERT (name != NULL
11813 && CONST_STRNEQ (name, ".MIPS.content"));
11814 sec = bfd_get_section_by_name (abfd,
11815 name + sizeof ".MIPS.content" - 1);
11816 BFD_ASSERT (sec != NULL);
11817 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11820 case SHT_MIPS_SYMBOL_LIB:
11821 sec = bfd_get_section_by_name (abfd, ".dynsym");
11823 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11824 sec = bfd_get_section_by_name (abfd, ".liblist");
11826 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11829 case SHT_MIPS_EVENTS:
11830 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11831 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11832 BFD_ASSERT (name != NULL);
11833 if (CONST_STRNEQ (name, ".MIPS.events"))
11834 sec = bfd_get_section_by_name (abfd,
11835 name + sizeof ".MIPS.events" - 1);
11838 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
11839 sec = bfd_get_section_by_name (abfd,
11841 + sizeof ".MIPS.post_rel" - 1));
11843 BFD_ASSERT (sec != NULL);
11844 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11851 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11855 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11856 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11861 /* See if we need a PT_MIPS_REGINFO segment. */
11862 s = bfd_get_section_by_name (abfd, ".reginfo");
11863 if (s && (s->flags & SEC_LOAD))
11866 /* See if we need a PT_MIPS_ABIFLAGS segment. */
11867 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
11870 /* See if we need a PT_MIPS_OPTIONS segment. */
11871 if (IRIX_COMPAT (abfd) == ict_irix6
11872 && bfd_get_section_by_name (abfd,
11873 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11876 /* See if we need a PT_MIPS_RTPROC segment. */
11877 if (IRIX_COMPAT (abfd) == ict_irix5
11878 && bfd_get_section_by_name (abfd, ".dynamic")
11879 && bfd_get_section_by_name (abfd, ".mdebug"))
11882 /* Allocate a PT_NULL header in dynamic objects. See
11883 _bfd_mips_elf_modify_segment_map for details. */
11884 if (!SGI_COMPAT (abfd)
11885 && bfd_get_section_by_name (abfd, ".dynamic"))
11891 /* Modify the segment map for an IRIX5 executable. */
11894 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11895 struct bfd_link_info *info)
11898 struct elf_segment_map *m, **pm;
11901 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11903 s = bfd_get_section_by_name (abfd, ".reginfo");
11904 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11906 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11907 if (m->p_type == PT_MIPS_REGINFO)
11912 m = bfd_zalloc (abfd, amt);
11916 m->p_type = PT_MIPS_REGINFO;
11918 m->sections[0] = s;
11920 /* We want to put it after the PHDR and INTERP segments. */
11921 pm = &elf_seg_map (abfd);
11923 && ((*pm)->p_type == PT_PHDR
11924 || (*pm)->p_type == PT_INTERP))
11932 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
11934 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
11935 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11937 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11938 if (m->p_type == PT_MIPS_ABIFLAGS)
11943 m = bfd_zalloc (abfd, amt);
11947 m->p_type = PT_MIPS_ABIFLAGS;
11949 m->sections[0] = s;
11951 /* We want to put it after the PHDR and INTERP segments. */
11952 pm = &elf_seg_map (abfd);
11954 && ((*pm)->p_type == PT_PHDR
11955 || (*pm)->p_type == PT_INTERP))
11963 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11964 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11965 PT_MIPS_OPTIONS segment immediately following the program header
11967 if (NEWABI_P (abfd)
11968 /* On non-IRIX6 new abi, we'll have already created a segment
11969 for this section, so don't create another. I'm not sure this
11970 is not also the case for IRIX 6, but I can't test it right
11972 && IRIX_COMPAT (abfd) == ict_irix6)
11974 for (s = abfd->sections; s; s = s->next)
11975 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11980 struct elf_segment_map *options_segment;
11982 pm = &elf_seg_map (abfd);
11984 && ((*pm)->p_type == PT_PHDR
11985 || (*pm)->p_type == PT_INTERP))
11988 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11990 amt = sizeof (struct elf_segment_map);
11991 options_segment = bfd_zalloc (abfd, amt);
11992 options_segment->next = *pm;
11993 options_segment->p_type = PT_MIPS_OPTIONS;
11994 options_segment->p_flags = PF_R;
11995 options_segment->p_flags_valid = TRUE;
11996 options_segment->count = 1;
11997 options_segment->sections[0] = s;
11998 *pm = options_segment;
12004 if (IRIX_COMPAT (abfd) == ict_irix5)
12006 /* If there are .dynamic and .mdebug sections, we make a room
12007 for the RTPROC header. FIXME: Rewrite without section names. */
12008 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12009 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12010 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12012 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12013 if (m->p_type == PT_MIPS_RTPROC)
12018 m = bfd_zalloc (abfd, amt);
12022 m->p_type = PT_MIPS_RTPROC;
12024 s = bfd_get_section_by_name (abfd, ".rtproc");
12029 m->p_flags_valid = 1;
12034 m->sections[0] = s;
12037 /* We want to put it after the DYNAMIC segment. */
12038 pm = &elf_seg_map (abfd);
12039 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12049 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12050 .dynstr, .dynsym, and .hash sections, and everything in
12052 for (pm = &elf_seg_map (abfd); *pm != NULL;
12054 if ((*pm)->p_type == PT_DYNAMIC)
12057 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12058 glibc's dynamic linker has traditionally derived the number of
12059 tags from the p_filesz field, and sometimes allocates stack
12060 arrays of that size. An overly-big PT_DYNAMIC segment can
12061 be actively harmful in such cases. Making PT_DYNAMIC contain
12062 other sections can also make life hard for the prelinker,
12063 which might move one of the other sections to a different
12064 PT_LOAD segment. */
12065 if (SGI_COMPAT (abfd)
12068 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12070 static const char *sec_names[] =
12072 ".dynamic", ".dynstr", ".dynsym", ".hash"
12076 struct elf_segment_map *n;
12078 low = ~(bfd_vma) 0;
12080 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12082 s = bfd_get_section_by_name (abfd, sec_names[i]);
12083 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12090 if (high < s->vma + sz)
12091 high = s->vma + sz;
12096 for (s = abfd->sections; s != NULL; s = s->next)
12097 if ((s->flags & SEC_LOAD) != 0
12099 && s->vma + s->size <= high)
12102 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
12103 n = bfd_zalloc (abfd, amt);
12110 for (s = abfd->sections; s != NULL; s = s->next)
12112 if ((s->flags & SEC_LOAD) != 0
12114 && s->vma + s->size <= high)
12116 n->sections[i] = s;
12125 /* Allocate a spare program header in dynamic objects so that tools
12126 like the prelinker can add an extra PT_LOAD entry.
12128 If the prelinker needs to make room for a new PT_LOAD entry, its
12129 standard procedure is to move the first (read-only) sections into
12130 the new (writable) segment. However, the MIPS ABI requires
12131 .dynamic to be in a read-only segment, and the section will often
12132 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12134 Although the prelinker could in principle move .dynamic to a
12135 writable segment, it seems better to allocate a spare program
12136 header instead, and avoid the need to move any sections.
12137 There is a long tradition of allocating spare dynamic tags,
12138 so allocating a spare program header seems like a natural
12141 If INFO is NULL, we may be copying an already prelinked binary
12142 with objcopy or strip, so do not add this header. */
12144 && !SGI_COMPAT (abfd)
12145 && bfd_get_section_by_name (abfd, ".dynamic"))
12147 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12148 if ((*pm)->p_type == PT_NULL)
12152 m = bfd_zalloc (abfd, sizeof (*m));
12156 m->p_type = PT_NULL;
12164 /* Return the section that should be marked against GC for a given
12168 _bfd_mips_elf_gc_mark_hook (asection *sec,
12169 struct bfd_link_info *info,
12170 Elf_Internal_Rela *rel,
12171 struct elf_link_hash_entry *h,
12172 Elf_Internal_Sym *sym)
12174 /* ??? Do mips16 stub sections need to be handled special? */
12177 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12179 case R_MIPS_GNU_VTINHERIT:
12180 case R_MIPS_GNU_VTENTRY:
12184 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12187 /* Update the got entry reference counts for the section being removed. */
12190 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12191 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12192 asection *sec ATTRIBUTE_UNUSED,
12193 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
12196 Elf_Internal_Shdr *symtab_hdr;
12197 struct elf_link_hash_entry **sym_hashes;
12198 bfd_signed_vma *local_got_refcounts;
12199 const Elf_Internal_Rela *rel, *relend;
12200 unsigned long r_symndx;
12201 struct elf_link_hash_entry *h;
12203 if (info->relocatable)
12206 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12207 sym_hashes = elf_sym_hashes (abfd);
12208 local_got_refcounts = elf_local_got_refcounts (abfd);
12210 relend = relocs + sec->reloc_count;
12211 for (rel = relocs; rel < relend; rel++)
12212 switch (ELF_R_TYPE (abfd, rel->r_info))
12214 case R_MIPS16_GOT16:
12215 case R_MIPS16_CALL16:
12217 case R_MIPS_CALL16:
12218 case R_MIPS_CALL_HI16:
12219 case R_MIPS_CALL_LO16:
12220 case R_MIPS_GOT_HI16:
12221 case R_MIPS_GOT_LO16:
12222 case R_MIPS_GOT_DISP:
12223 case R_MIPS_GOT_PAGE:
12224 case R_MIPS_GOT_OFST:
12225 case R_MICROMIPS_GOT16:
12226 case R_MICROMIPS_CALL16:
12227 case R_MICROMIPS_CALL_HI16:
12228 case R_MICROMIPS_CALL_LO16:
12229 case R_MICROMIPS_GOT_HI16:
12230 case R_MICROMIPS_GOT_LO16:
12231 case R_MICROMIPS_GOT_DISP:
12232 case R_MICROMIPS_GOT_PAGE:
12233 case R_MICROMIPS_GOT_OFST:
12234 /* ??? It would seem that the existing MIPS code does no sort
12235 of reference counting or whatnot on its GOT and PLT entries,
12236 so it is not possible to garbage collect them at this time. */
12247 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12250 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12251 elf_gc_mark_hook_fn gc_mark_hook)
12255 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12257 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12261 if (! is_mips_elf (sub))
12264 for (o = sub->sections; o != NULL; o = o->next)
12266 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P
12267 (bfd_get_section_name (sub, o)))
12269 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12277 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12278 hiding the old indirect symbol. Process additional relocation
12279 information. Also called for weakdefs, in which case we just let
12280 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12283 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12284 struct elf_link_hash_entry *dir,
12285 struct elf_link_hash_entry *ind)
12287 struct mips_elf_link_hash_entry *dirmips, *indmips;
12289 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12291 dirmips = (struct mips_elf_link_hash_entry *) dir;
12292 indmips = (struct mips_elf_link_hash_entry *) ind;
12293 /* Any absolute non-dynamic relocations against an indirect or weak
12294 definition will be against the target symbol. */
12295 if (indmips->has_static_relocs)
12296 dirmips->has_static_relocs = TRUE;
12298 if (ind->root.type != bfd_link_hash_indirect)
12301 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12302 if (indmips->readonly_reloc)
12303 dirmips->readonly_reloc = TRUE;
12304 if (indmips->no_fn_stub)
12305 dirmips->no_fn_stub = TRUE;
12306 if (indmips->fn_stub)
12308 dirmips->fn_stub = indmips->fn_stub;
12309 indmips->fn_stub = NULL;
12311 if (indmips->need_fn_stub)
12313 dirmips->need_fn_stub = TRUE;
12314 indmips->need_fn_stub = FALSE;
12316 if (indmips->call_stub)
12318 dirmips->call_stub = indmips->call_stub;
12319 indmips->call_stub = NULL;
12321 if (indmips->call_fp_stub)
12323 dirmips->call_fp_stub = indmips->call_fp_stub;
12324 indmips->call_fp_stub = NULL;
12326 if (indmips->global_got_area < dirmips->global_got_area)
12327 dirmips->global_got_area = indmips->global_got_area;
12328 if (indmips->global_got_area < GGA_NONE)
12329 indmips->global_got_area = GGA_NONE;
12330 if (indmips->has_nonpic_branches)
12331 dirmips->has_nonpic_branches = TRUE;
12334 #define PDR_SIZE 32
12337 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12338 struct bfd_link_info *info)
12341 bfd_boolean ret = FALSE;
12342 unsigned char *tdata;
12345 o = bfd_get_section_by_name (abfd, ".pdr");
12350 if (o->size % PDR_SIZE != 0)
12352 if (o->output_section != NULL
12353 && bfd_is_abs_section (o->output_section))
12356 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12360 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12361 info->keep_memory);
12368 cookie->rel = cookie->rels;
12369 cookie->relend = cookie->rels + o->reloc_count;
12371 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12373 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12382 mips_elf_section_data (o)->u.tdata = tdata;
12383 o->size -= skip * PDR_SIZE;
12389 if (! info->keep_memory)
12390 free (cookie->rels);
12396 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
12398 if (strcmp (sec->name, ".pdr") == 0)
12404 _bfd_mips_elf_write_section (bfd *output_bfd,
12405 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12406 asection *sec, bfd_byte *contents)
12408 bfd_byte *to, *from, *end;
12411 if (strcmp (sec->name, ".pdr") != 0)
12414 if (mips_elf_section_data (sec)->u.tdata == NULL)
12418 end = contents + sec->size;
12419 for (from = contents, i = 0;
12421 from += PDR_SIZE, i++)
12423 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
12426 memcpy (to, from, PDR_SIZE);
12429 bfd_set_section_contents (output_bfd, sec->output_section, contents,
12430 sec->output_offset, sec->size);
12434 /* microMIPS code retains local labels for linker relaxation. Omit them
12435 from output by default for clarity. */
12438 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12440 return _bfd_elf_is_local_label_name (abfd, sym->name);
12443 /* MIPS ELF uses a special find_nearest_line routine in order the
12444 handle the ECOFF debugging information. */
12446 struct mips_elf_find_line
12448 struct ecoff_debug_info d;
12449 struct ecoff_find_line i;
12453 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12454 asymbol **symbols, bfd_vma offset,
12455 const char **filename_ptr,
12456 const char **functionname_ptr,
12457 unsigned int *line_ptr)
12461 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12462 filename_ptr, functionname_ptr,
12466 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12467 section, symbols, offset,
12468 filename_ptr, functionname_ptr,
12469 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
12470 &elf_tdata (abfd)->dwarf2_find_line_info))
12473 msec = bfd_get_section_by_name (abfd, ".mdebug");
12476 flagword origflags;
12477 struct mips_elf_find_line *fi;
12478 const struct ecoff_debug_swap * const swap =
12479 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12481 /* If we are called during a link, mips_elf_final_link may have
12482 cleared the SEC_HAS_CONTENTS field. We force it back on here
12483 if appropriate (which it normally will be). */
12484 origflags = msec->flags;
12485 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12486 msec->flags |= SEC_HAS_CONTENTS;
12488 fi = mips_elf_tdata (abfd)->find_line_info;
12491 bfd_size_type external_fdr_size;
12494 struct fdr *fdr_ptr;
12495 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12497 fi = bfd_zalloc (abfd, amt);
12500 msec->flags = origflags;
12504 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12506 msec->flags = origflags;
12510 /* Swap in the FDR information. */
12511 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
12512 fi->d.fdr = bfd_alloc (abfd, amt);
12513 if (fi->d.fdr == NULL)
12515 msec->flags = origflags;
12518 external_fdr_size = swap->external_fdr_size;
12519 fdr_ptr = fi->d.fdr;
12520 fraw_src = (char *) fi->d.external_fdr;
12521 fraw_end = (fraw_src
12522 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12523 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
12524 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
12526 mips_elf_tdata (abfd)->find_line_info = fi;
12528 /* Note that we don't bother to ever free this information.
12529 find_nearest_line is either called all the time, as in
12530 objdump -l, so the information should be saved, or it is
12531 rarely called, as in ld error messages, so the memory
12532 wasted is unimportant. Still, it would probably be a
12533 good idea for free_cached_info to throw it away. */
12536 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12537 &fi->i, filename_ptr, functionname_ptr,
12540 msec->flags = origflags;
12544 msec->flags = origflags;
12547 /* Fall back on the generic ELF find_nearest_line routine. */
12549 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12550 filename_ptr, functionname_ptr,
12555 _bfd_mips_elf_find_inliner_info (bfd *abfd,
12556 const char **filename_ptr,
12557 const char **functionname_ptr,
12558 unsigned int *line_ptr)
12561 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12562 functionname_ptr, line_ptr,
12563 & elf_tdata (abfd)->dwarf2_find_line_info);
12568 /* When are writing out the .options or .MIPS.options section,
12569 remember the bytes we are writing out, so that we can install the
12570 GP value in the section_processing routine. */
12573 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12574 const void *location,
12575 file_ptr offset, bfd_size_type count)
12577 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
12581 if (elf_section_data (section) == NULL)
12583 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
12584 section->used_by_bfd = bfd_zalloc (abfd, amt);
12585 if (elf_section_data (section) == NULL)
12588 c = mips_elf_section_data (section)->u.tdata;
12591 c = bfd_zalloc (abfd, section->size);
12594 mips_elf_section_data (section)->u.tdata = c;
12597 memcpy (c + offset, location, count);
12600 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12604 /* This is almost identical to bfd_generic_get_... except that some
12605 MIPS relocations need to be handled specially. Sigh. */
12608 _bfd_elf_mips_get_relocated_section_contents
12610 struct bfd_link_info *link_info,
12611 struct bfd_link_order *link_order,
12613 bfd_boolean relocatable,
12616 /* Get enough memory to hold the stuff */
12617 bfd *input_bfd = link_order->u.indirect.section->owner;
12618 asection *input_section = link_order->u.indirect.section;
12621 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12622 arelent **reloc_vector = NULL;
12625 if (reloc_size < 0)
12628 reloc_vector = bfd_malloc (reloc_size);
12629 if (reloc_vector == NULL && reloc_size != 0)
12632 /* read in the section */
12633 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12634 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
12637 reloc_count = bfd_canonicalize_reloc (input_bfd,
12641 if (reloc_count < 0)
12644 if (reloc_count > 0)
12649 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12652 struct bfd_hash_entry *h;
12653 struct bfd_link_hash_entry *lh;
12654 /* Skip all this stuff if we aren't mixing formats. */
12655 if (abfd && input_bfd
12656 && abfd->xvec == input_bfd->xvec)
12660 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
12661 lh = (struct bfd_link_hash_entry *) h;
12668 case bfd_link_hash_undefined:
12669 case bfd_link_hash_undefweak:
12670 case bfd_link_hash_common:
12673 case bfd_link_hash_defined:
12674 case bfd_link_hash_defweak:
12676 gp = lh->u.def.value;
12678 case bfd_link_hash_indirect:
12679 case bfd_link_hash_warning:
12681 /* @@FIXME ignoring warning for now */
12683 case bfd_link_hash_new:
12692 for (parent = reloc_vector; *parent != NULL; parent++)
12694 char *error_message = NULL;
12695 bfd_reloc_status_type r;
12697 /* Specific to MIPS: Deal with relocation types that require
12698 knowing the gp of the output bfd. */
12699 asymbol *sym = *(*parent)->sym_ptr_ptr;
12701 /* If we've managed to find the gp and have a special
12702 function for the relocation then go ahead, else default
12703 to the generic handling. */
12705 && (*parent)->howto->special_function
12706 == _bfd_mips_elf32_gprel16_reloc)
12707 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12708 input_section, relocatable,
12711 r = bfd_perform_relocation (input_bfd, *parent, data,
12713 relocatable ? abfd : NULL,
12718 asection *os = input_section->output_section;
12720 /* A partial link, so keep the relocs */
12721 os->orelocation[os->reloc_count] = *parent;
12725 if (r != bfd_reloc_ok)
12729 case bfd_reloc_undefined:
12730 if (!((*link_info->callbacks->undefined_symbol)
12731 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12732 input_bfd, input_section, (*parent)->address, TRUE)))
12735 case bfd_reloc_dangerous:
12736 BFD_ASSERT (error_message != NULL);
12737 if (!((*link_info->callbacks->reloc_dangerous)
12738 (link_info, error_message, input_bfd, input_section,
12739 (*parent)->address)))
12742 case bfd_reloc_overflow:
12743 if (!((*link_info->callbacks->reloc_overflow)
12745 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12746 (*parent)->howto->name, (*parent)->addend,
12747 input_bfd, input_section, (*parent)->address)))
12750 case bfd_reloc_outofrange:
12759 if (reloc_vector != NULL)
12760 free (reloc_vector);
12764 if (reloc_vector != NULL)
12765 free (reloc_vector);
12770 mips_elf_relax_delete_bytes (bfd *abfd,
12771 asection *sec, bfd_vma addr, int count)
12773 Elf_Internal_Shdr *symtab_hdr;
12774 unsigned int sec_shndx;
12775 bfd_byte *contents;
12776 Elf_Internal_Rela *irel, *irelend;
12777 Elf_Internal_Sym *isym;
12778 Elf_Internal_Sym *isymend;
12779 struct elf_link_hash_entry **sym_hashes;
12780 struct elf_link_hash_entry **end_hashes;
12781 struct elf_link_hash_entry **start_hashes;
12782 unsigned int symcount;
12784 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12785 contents = elf_section_data (sec)->this_hdr.contents;
12787 irel = elf_section_data (sec)->relocs;
12788 irelend = irel + sec->reloc_count;
12790 /* Actually delete the bytes. */
12791 memmove (contents + addr, contents + addr + count,
12792 (size_t) (sec->size - addr - count));
12793 sec->size -= count;
12795 /* Adjust all the relocs. */
12796 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12798 /* Get the new reloc address. */
12799 if (irel->r_offset > addr)
12800 irel->r_offset -= count;
12803 BFD_ASSERT (addr % 2 == 0);
12804 BFD_ASSERT (count % 2 == 0);
12806 /* Adjust the local symbols defined in this section. */
12807 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12808 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12809 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
12810 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
12811 isym->st_value -= count;
12813 /* Now adjust the global symbols defined in this section. */
12814 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12815 - symtab_hdr->sh_info);
12816 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12817 end_hashes = sym_hashes + symcount;
12819 for (; sym_hashes < end_hashes; sym_hashes++)
12821 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12823 if ((sym_hash->root.type == bfd_link_hash_defined
12824 || sym_hash->root.type == bfd_link_hash_defweak)
12825 && sym_hash->root.u.def.section == sec)
12827 bfd_vma value = sym_hash->root.u.def.value;
12829 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12830 value &= MINUS_TWO;
12832 sym_hash->root.u.def.value -= count;
12840 /* Opcodes needed for microMIPS relaxation as found in
12841 opcodes/micromips-opc.c. */
12843 struct opcode_descriptor {
12844 unsigned long match;
12845 unsigned long mask;
12848 /* The $ra register aka $31. */
12852 /* 32-bit instruction format register fields. */
12854 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12855 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12857 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
12859 #define OP16_VALID_REG(r) \
12860 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12863 /* 32-bit and 16-bit branches. */
12865 static const struct opcode_descriptor b_insns_32[] = {
12866 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12867 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12868 { 0, 0 } /* End marker for find_match(). */
12871 static const struct opcode_descriptor bc_insn_32 =
12872 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12874 static const struct opcode_descriptor bz_insn_32 =
12875 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12877 static const struct opcode_descriptor bzal_insn_32 =
12878 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12880 static const struct opcode_descriptor beq_insn_32 =
12881 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12883 static const struct opcode_descriptor b_insn_16 =
12884 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12886 static const struct opcode_descriptor bz_insn_16 =
12887 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
12890 /* 32-bit and 16-bit branch EQ and NE zero. */
12892 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12893 eq and second the ne. This convention is used when replacing a
12894 32-bit BEQ/BNE with the 16-bit version. */
12896 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12898 static const struct opcode_descriptor bz_rs_insns_32[] = {
12899 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12900 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12901 { 0, 0 } /* End marker for find_match(). */
12904 static const struct opcode_descriptor bz_rt_insns_32[] = {
12905 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12906 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12907 { 0, 0 } /* End marker for find_match(). */
12910 static const struct opcode_descriptor bzc_insns_32[] = {
12911 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12912 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12913 { 0, 0 } /* End marker for find_match(). */
12916 static const struct opcode_descriptor bz_insns_16[] = {
12917 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12918 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12919 { 0, 0 } /* End marker for find_match(). */
12922 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12924 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12925 #define BZ16_REG_FIELD(r) \
12926 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12929 /* 32-bit instructions with a delay slot. */
12931 static const struct opcode_descriptor jal_insn_32_bd16 =
12932 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12934 static const struct opcode_descriptor jal_insn_32_bd32 =
12935 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12937 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12938 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12940 static const struct opcode_descriptor j_insn_32 =
12941 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12943 static const struct opcode_descriptor jalr_insn_32 =
12944 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12946 /* This table can be compacted, because no opcode replacement is made. */
12948 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12949 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12951 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12952 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12954 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12955 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12956 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12957 { 0, 0 } /* End marker for find_match(). */
12960 /* This table can be compacted, because no opcode replacement is made. */
12962 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12963 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12965 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12966 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12967 { 0, 0 } /* End marker for find_match(). */
12971 /* 16-bit instructions with a delay slot. */
12973 static const struct opcode_descriptor jalr_insn_16_bd16 =
12974 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12976 static const struct opcode_descriptor jalr_insn_16_bd32 =
12977 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12979 static const struct opcode_descriptor jr_insn_16 =
12980 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12982 #define JR16_REG(opcode) ((opcode) & 0x1f)
12984 /* This table can be compacted, because no opcode replacement is made. */
12986 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12987 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12989 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12990 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12991 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12992 { 0, 0 } /* End marker for find_match(). */
12996 /* LUI instruction. */
12998 static const struct opcode_descriptor lui_insn =
12999 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13002 /* ADDIU instruction. */
13004 static const struct opcode_descriptor addiu_insn =
13005 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13007 static const struct opcode_descriptor addiupc_insn =
13008 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13010 #define ADDIUPC_REG_FIELD(r) \
13011 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13014 /* Relaxable instructions in a JAL delay slot: MOVE. */
13016 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13017 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13018 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13019 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13021 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13022 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13024 static const struct opcode_descriptor move_insns_32[] = {
13025 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13026 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13027 { 0, 0 } /* End marker for find_match(). */
13030 static const struct opcode_descriptor move_insn_16 =
13031 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13034 /* NOP instructions. */
13036 static const struct opcode_descriptor nop_insn_32 =
13037 { /* "nop", "", */ 0x00000000, 0xffffffff };
13039 static const struct opcode_descriptor nop_insn_16 =
13040 { /* "nop", "", */ 0x0c00, 0xffff };
13043 /* Instruction match support. */
13045 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13048 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13050 unsigned long indx;
13052 for (indx = 0; insn[indx].mask != 0; indx++)
13053 if (MATCH (opcode, insn[indx]))
13060 /* Branch and delay slot decoding support. */
13062 /* If PTR points to what *might* be a 16-bit branch or jump, then
13063 return the minimum length of its delay slot, otherwise return 0.
13064 Non-zero results are not definitive as we might be checking against
13065 the second half of another instruction. */
13068 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13070 unsigned long opcode;
13073 opcode = bfd_get_16 (abfd, ptr);
13074 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13075 /* 16-bit branch/jump with a 32-bit delay slot. */
13077 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13078 || find_match (opcode, ds_insns_16_bd16) >= 0)
13079 /* 16-bit branch/jump with a 16-bit delay slot. */
13082 /* No delay slot. */
13088 /* If PTR points to what *might* be a 32-bit branch or jump, then
13089 return the minimum length of its delay slot, otherwise return 0.
13090 Non-zero results are not definitive as we might be checking against
13091 the second half of another instruction. */
13094 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13096 unsigned long opcode;
13099 opcode = bfd_get_micromips_32 (abfd, ptr);
13100 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13101 /* 32-bit branch/jump with a 32-bit delay slot. */
13103 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13104 /* 32-bit branch/jump with a 16-bit delay slot. */
13107 /* No delay slot. */
13113 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13114 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13117 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13119 unsigned long opcode;
13121 opcode = bfd_get_16 (abfd, ptr);
13122 if (MATCH (opcode, b_insn_16)
13124 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13126 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13127 /* BEQZ16, BNEZ16 */
13128 || (MATCH (opcode, jalr_insn_16_bd32)
13130 && reg != JR16_REG (opcode) && reg != RA))
13136 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13137 then return TRUE, otherwise FALSE. */
13140 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13142 unsigned long opcode;
13144 opcode = bfd_get_micromips_32 (abfd, ptr);
13145 if (MATCH (opcode, j_insn_32)
13147 || MATCH (opcode, bc_insn_32)
13148 /* BC1F, BC1T, BC2F, BC2T */
13149 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13151 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13152 /* BGEZ, BGTZ, BLEZ, BLTZ */
13153 || (MATCH (opcode, bzal_insn_32)
13154 /* BGEZAL, BLTZAL */
13155 && reg != OP32_SREG (opcode) && reg != RA)
13156 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13157 /* JALR, JALR.HB, BEQ, BNE */
13158 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13164 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13165 IRELEND) at OFFSET indicate that there must be a compact branch there,
13166 then return TRUE, otherwise FALSE. */
13169 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13170 const Elf_Internal_Rela *internal_relocs,
13171 const Elf_Internal_Rela *irelend)
13173 const Elf_Internal_Rela *irel;
13174 unsigned long opcode;
13176 opcode = bfd_get_micromips_32 (abfd, ptr);
13177 if (find_match (opcode, bzc_insns_32) < 0)
13180 for (irel = internal_relocs; irel < irelend; irel++)
13181 if (irel->r_offset == offset
13182 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13188 /* Bitsize checking. */
13189 #define IS_BITSIZE(val, N) \
13190 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13191 - (1ULL << ((N) - 1))) == (val))
13195 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13196 struct bfd_link_info *link_info,
13197 bfd_boolean *again)
13199 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13200 Elf_Internal_Shdr *symtab_hdr;
13201 Elf_Internal_Rela *internal_relocs;
13202 Elf_Internal_Rela *irel, *irelend;
13203 bfd_byte *contents = NULL;
13204 Elf_Internal_Sym *isymbuf = NULL;
13206 /* Assume nothing changes. */
13209 /* We don't have to do anything for a relocatable link, if
13210 this section does not have relocs, or if this is not a
13213 if (link_info->relocatable
13214 || (sec->flags & SEC_RELOC) == 0
13215 || sec->reloc_count == 0
13216 || (sec->flags & SEC_CODE) == 0)
13219 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13221 /* Get a copy of the native relocations. */
13222 internal_relocs = (_bfd_elf_link_read_relocs
13223 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13224 link_info->keep_memory));
13225 if (internal_relocs == NULL)
13228 /* Walk through them looking for relaxing opportunities. */
13229 irelend = internal_relocs + sec->reloc_count;
13230 for (irel = internal_relocs; irel < irelend; irel++)
13232 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13233 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13234 bfd_boolean target_is_micromips_code_p;
13235 unsigned long opcode;
13241 /* The number of bytes to delete for relaxation and from where
13242 to delete these bytes starting at irel->r_offset. */
13246 /* If this isn't something that can be relaxed, then ignore
13248 if (r_type != R_MICROMIPS_HI16
13249 && r_type != R_MICROMIPS_PC16_S1
13250 && r_type != R_MICROMIPS_26_S1)
13253 /* Get the section contents if we haven't done so already. */
13254 if (contents == NULL)
13256 /* Get cached copy if it exists. */
13257 if (elf_section_data (sec)->this_hdr.contents != NULL)
13258 contents = elf_section_data (sec)->this_hdr.contents;
13259 /* Go get them off disk. */
13260 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13263 ptr = contents + irel->r_offset;
13265 /* Read this BFD's local symbols if we haven't done so already. */
13266 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13268 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13269 if (isymbuf == NULL)
13270 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13271 symtab_hdr->sh_info, 0,
13273 if (isymbuf == NULL)
13277 /* Get the value of the symbol referred to by the reloc. */
13278 if (r_symndx < symtab_hdr->sh_info)
13280 /* A local symbol. */
13281 Elf_Internal_Sym *isym;
13284 isym = isymbuf + r_symndx;
13285 if (isym->st_shndx == SHN_UNDEF)
13286 sym_sec = bfd_und_section_ptr;
13287 else if (isym->st_shndx == SHN_ABS)
13288 sym_sec = bfd_abs_section_ptr;
13289 else if (isym->st_shndx == SHN_COMMON)
13290 sym_sec = bfd_com_section_ptr;
13292 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13293 symval = (isym->st_value
13294 + sym_sec->output_section->vma
13295 + sym_sec->output_offset);
13296 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13300 unsigned long indx;
13301 struct elf_link_hash_entry *h;
13303 /* An external symbol. */
13304 indx = r_symndx - symtab_hdr->sh_info;
13305 h = elf_sym_hashes (abfd)[indx];
13306 BFD_ASSERT (h != NULL);
13308 if (h->root.type != bfd_link_hash_defined
13309 && h->root.type != bfd_link_hash_defweak)
13310 /* This appears to be a reference to an undefined
13311 symbol. Just ignore it -- it will be caught by the
13312 regular reloc processing. */
13315 symval = (h->root.u.def.value
13316 + h->root.u.def.section->output_section->vma
13317 + h->root.u.def.section->output_offset);
13318 target_is_micromips_code_p = (!h->needs_plt
13319 && ELF_ST_IS_MICROMIPS (h->other));
13323 /* For simplicity of coding, we are going to modify the
13324 section contents, the section relocs, and the BFD symbol
13325 table. We must tell the rest of the code not to free up this
13326 information. It would be possible to instead create a table
13327 of changes which have to be made, as is done in coff-mips.c;
13328 that would be more work, but would require less memory when
13329 the linker is run. */
13331 /* Only 32-bit instructions relaxed. */
13332 if (irel->r_offset + 4 > sec->size)
13335 opcode = bfd_get_micromips_32 (abfd, ptr);
13337 /* This is the pc-relative distance from the instruction the
13338 relocation is applied to, to the symbol referred. */
13340 - (sec->output_section->vma + sec->output_offset)
13343 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13344 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13345 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13347 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13349 where pcrval has first to be adjusted to apply against the LO16
13350 location (we make the adjustment later on, when we have figured
13351 out the offset). */
13352 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13354 bfd_boolean bzc = FALSE;
13355 unsigned long nextopc;
13359 /* Give up if the previous reloc was a HI16 against this symbol
13361 if (irel > internal_relocs
13362 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13363 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13366 /* Or if the next reloc is not a LO16 against this symbol. */
13367 if (irel + 1 >= irelend
13368 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13369 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13372 /* Or if the second next reloc is a LO16 against this symbol too. */
13373 if (irel + 2 >= irelend
13374 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13375 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13378 /* See if the LUI instruction *might* be in a branch delay slot.
13379 We check whether what looks like a 16-bit branch or jump is
13380 actually an immediate argument to a compact branch, and let
13381 it through if so. */
13382 if (irel->r_offset >= 2
13383 && check_br16_dslot (abfd, ptr - 2)
13384 && !(irel->r_offset >= 4
13385 && (bzc = check_relocated_bzc (abfd,
13386 ptr - 4, irel->r_offset - 4,
13387 internal_relocs, irelend))))
13389 if (irel->r_offset >= 4
13391 && check_br32_dslot (abfd, ptr - 4))
13394 reg = OP32_SREG (opcode);
13396 /* We only relax adjacent instructions or ones separated with
13397 a branch or jump that has a delay slot. The branch or jump
13398 must not fiddle with the register used to hold the address.
13399 Subtract 4 for the LUI itself. */
13400 offset = irel[1].r_offset - irel[0].r_offset;
13401 switch (offset - 4)
13406 if (check_br16 (abfd, ptr + 4, reg))
13410 if (check_br32 (abfd, ptr + 4, reg))
13417 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
13419 /* Give up unless the same register is used with both
13421 if (OP32_SREG (nextopc) != reg)
13424 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13425 and rounding up to take masking of the two LSBs into account. */
13426 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13428 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13429 if (IS_BITSIZE (symval, 16))
13431 /* Fix the relocation's type. */
13432 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13434 /* Instructions using R_MICROMIPS_LO16 have the base or
13435 source register in bits 20:16. This register becomes $0
13436 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13437 nextopc &= ~0x001f0000;
13438 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13439 contents + irel[1].r_offset);
13442 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13443 We add 4 to take LUI deletion into account while checking
13444 the PC-relative distance. */
13445 else if (symval % 4 == 0
13446 && IS_BITSIZE (pcrval + 4, 25)
13447 && MATCH (nextopc, addiu_insn)
13448 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13449 && OP16_VALID_REG (OP32_TREG (nextopc)))
13451 /* Fix the relocation's type. */
13452 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13454 /* Replace ADDIU with the ADDIUPC version. */
13455 nextopc = (addiupc_insn.match
13456 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13458 bfd_put_micromips_32 (abfd, nextopc,
13459 contents + irel[1].r_offset);
13462 /* Can't do anything, give up, sigh... */
13466 /* Fix the relocation's type. */
13467 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13469 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13474 /* Compact branch relaxation -- due to the multitude of macros
13475 employed by the compiler/assembler, compact branches are not
13476 always generated. Obviously, this can/will be fixed elsewhere,
13477 but there is no drawback in double checking it here. */
13478 else if (r_type == R_MICROMIPS_PC16_S1
13479 && irel->r_offset + 5 < sec->size
13480 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13481 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
13483 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13484 nop_insn_16) ? 2 : 0))
13485 || (irel->r_offset + 7 < sec->size
13486 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13488 nop_insn_32) ? 4 : 0))))
13492 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13494 /* Replace BEQZ/BNEZ with the compact version. */
13495 opcode = (bzc_insns_32[fndopc].match
13496 | BZC32_REG_FIELD (reg)
13497 | (opcode & 0xffff)); /* Addend value. */
13499 bfd_put_micromips_32 (abfd, opcode, ptr);
13501 /* Delete the delay slot NOP: two or four bytes from
13502 irel->offset + 4; delcnt has already been set above. */
13506 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13507 to check the distance from the next instruction, so subtract 2. */
13509 && r_type == R_MICROMIPS_PC16_S1
13510 && IS_BITSIZE (pcrval - 2, 11)
13511 && find_match (opcode, b_insns_32) >= 0)
13513 /* Fix the relocation's type. */
13514 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13516 /* Replace the 32-bit opcode with a 16-bit opcode. */
13519 | (opcode & 0x3ff)), /* Addend value. */
13522 /* Delete 2 bytes from irel->r_offset + 2. */
13527 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13528 to check the distance from the next instruction, so subtract 2. */
13530 && r_type == R_MICROMIPS_PC16_S1
13531 && IS_BITSIZE (pcrval - 2, 8)
13532 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13533 && OP16_VALID_REG (OP32_SREG (opcode)))
13534 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13535 && OP16_VALID_REG (OP32_TREG (opcode)))))
13539 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13541 /* Fix the relocation's type. */
13542 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13544 /* Replace the 32-bit opcode with a 16-bit opcode. */
13546 (bz_insns_16[fndopc].match
13547 | BZ16_REG_FIELD (reg)
13548 | (opcode & 0x7f)), /* Addend value. */
13551 /* Delete 2 bytes from irel->r_offset + 2. */
13556 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
13558 && r_type == R_MICROMIPS_26_S1
13559 && target_is_micromips_code_p
13560 && irel->r_offset + 7 < sec->size
13561 && MATCH (opcode, jal_insn_32_bd32))
13563 unsigned long n32opc;
13564 bfd_boolean relaxed = FALSE;
13566 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
13568 if (MATCH (n32opc, nop_insn_32))
13570 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
13571 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
13575 else if (find_match (n32opc, move_insns_32) >= 0)
13577 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13579 (move_insn_16.match
13580 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13581 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
13586 /* Other 32-bit instructions relaxable to 16-bit
13587 instructions will be handled here later. */
13591 /* JAL with 32-bit delay slot that is changed to a JALS
13592 with 16-bit delay slot. */
13593 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
13595 /* Delete 2 bytes from irel->r_offset + 6. */
13603 /* Note that we've changed the relocs, section contents, etc. */
13604 elf_section_data (sec)->relocs = internal_relocs;
13605 elf_section_data (sec)->this_hdr.contents = contents;
13606 symtab_hdr->contents = (unsigned char *) isymbuf;
13608 /* Delete bytes depending on the delcnt and deloff. */
13609 if (!mips_elf_relax_delete_bytes (abfd, sec,
13610 irel->r_offset + deloff, delcnt))
13613 /* That will change things, so we should relax again.
13614 Note that this is not required, and it may be slow. */
13619 if (isymbuf != NULL
13620 && symtab_hdr->contents != (unsigned char *) isymbuf)
13622 if (! link_info->keep_memory)
13626 /* Cache the symbols for elf_link_input_bfd. */
13627 symtab_hdr->contents = (unsigned char *) isymbuf;
13631 if (contents != NULL
13632 && elf_section_data (sec)->this_hdr.contents != contents)
13634 if (! link_info->keep_memory)
13638 /* Cache the section contents for elf_link_input_bfd. */
13639 elf_section_data (sec)->this_hdr.contents = contents;
13643 if (internal_relocs != NULL
13644 && elf_section_data (sec)->relocs != internal_relocs)
13645 free (internal_relocs);
13650 if (isymbuf != NULL
13651 && symtab_hdr->contents != (unsigned char *) isymbuf)
13653 if (contents != NULL
13654 && elf_section_data (sec)->this_hdr.contents != contents)
13656 if (internal_relocs != NULL
13657 && elf_section_data (sec)->relocs != internal_relocs)
13658 free (internal_relocs);
13663 /* Create a MIPS ELF linker hash table. */
13665 struct bfd_link_hash_table *
13666 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
13668 struct mips_elf_link_hash_table *ret;
13669 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13671 ret = bfd_zmalloc (amt);
13675 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13676 mips_elf_link_hash_newfunc,
13677 sizeof (struct mips_elf_link_hash_entry),
13683 ret->root.init_plt_refcount.plist = NULL;
13684 ret->root.init_plt_offset.plist = NULL;
13686 return &ret->root.root;
13689 /* Likewise, but indicate that the target is VxWorks. */
13691 struct bfd_link_hash_table *
13692 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13694 struct bfd_link_hash_table *ret;
13696 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13699 struct mips_elf_link_hash_table *htab;
13701 htab = (struct mips_elf_link_hash_table *) ret;
13702 htab->use_plts_and_copy_relocs = TRUE;
13703 htab->is_vxworks = TRUE;
13708 /* A function that the linker calls if we are allowed to use PLTs
13709 and copy relocs. */
13712 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13714 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13717 /* A function that the linker calls to select between all or only
13718 32-bit microMIPS instructions. */
13721 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13723 mips_elf_hash_table (info)->insn32 = on;
13726 /* Return the .MIPS.abiflags value representing each ISA Extension. */
13729 bfd_mips_isa_ext (bfd *abfd)
13731 switch (bfd_get_mach (abfd))
13733 case bfd_mach_mips3900:
13734 return AFL_EXT_3900;
13735 case bfd_mach_mips4010:
13736 return AFL_EXT_4010;
13737 case bfd_mach_mips4100:
13738 return AFL_EXT_4100;
13739 case bfd_mach_mips4111:
13740 return AFL_EXT_4111;
13741 case bfd_mach_mips4120:
13742 return AFL_EXT_4120;
13743 case bfd_mach_mips4650:
13744 return AFL_EXT_4650;
13745 case bfd_mach_mips5400:
13746 return AFL_EXT_5400;
13747 case bfd_mach_mips5500:
13748 return AFL_EXT_5500;
13749 case bfd_mach_mips5900:
13750 return AFL_EXT_5900;
13751 case bfd_mach_mips10000:
13752 return AFL_EXT_10000;
13753 case bfd_mach_mips_loongson_2e:
13754 return AFL_EXT_LOONGSON_2E;
13755 case bfd_mach_mips_loongson_2f:
13756 return AFL_EXT_LOONGSON_2F;
13757 case bfd_mach_mips_loongson_3a:
13758 return AFL_EXT_LOONGSON_3A;
13759 case bfd_mach_mips_sb1:
13760 return AFL_EXT_SB1;
13761 case bfd_mach_mips_octeon:
13762 return AFL_EXT_OCTEON;
13763 case bfd_mach_mips_octeonp:
13764 return AFL_EXT_OCTEONP;
13765 case bfd_mach_mips_octeon2:
13766 return AFL_EXT_OCTEON2;
13767 case bfd_mach_mips_xlr:
13768 return AFL_EXT_XLR;
13773 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
13776 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
13778 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
13780 case E_MIPS_ARCH_1:
13781 abiflags->isa_level = 1;
13782 abiflags->isa_rev = 0;
13784 case E_MIPS_ARCH_2:
13785 abiflags->isa_level = 2;
13786 abiflags->isa_rev = 0;
13788 case E_MIPS_ARCH_3:
13789 abiflags->isa_level = 3;
13790 abiflags->isa_rev = 0;
13792 case E_MIPS_ARCH_4:
13793 abiflags->isa_level = 4;
13794 abiflags->isa_rev = 0;
13796 case E_MIPS_ARCH_5:
13797 abiflags->isa_level = 5;
13798 abiflags->isa_rev = 0;
13800 case E_MIPS_ARCH_32:
13801 abiflags->isa_level = 32;
13802 abiflags->isa_rev = 1;
13804 case E_MIPS_ARCH_32R2:
13805 abiflags->isa_level = 32;
13806 /* Handle MIPS32r3 and MIPS32r5 which do not have a header flag. */
13807 if (abiflags->isa_rev < 2)
13808 abiflags->isa_rev = 2;
13810 case E_MIPS_ARCH_64:
13811 abiflags->isa_level = 64;
13812 abiflags->isa_rev = 1;
13814 case E_MIPS_ARCH_64R2:
13815 /* Handle MIPS64r3 and MIPS64r5 which do not have a header flag. */
13816 abiflags->isa_level = 64;
13817 if (abiflags->isa_rev < 2)
13818 abiflags->isa_rev = 2;
13821 (*_bfd_error_handler)
13822 (_("%B: Unknown architecture %s"),
13823 abfd, bfd_printable_name (abfd));
13826 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
13829 /* Return true if the given ELF header flags describe a 32-bit binary. */
13832 mips_32bit_flags_p (flagword flags)
13834 return ((flags & EF_MIPS_32BITMODE) != 0
13835 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
13836 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
13837 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
13838 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
13839 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
13840 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
13843 /* Infer the content of the ABI flags based on the elf header. */
13846 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
13848 obj_attribute *in_attr;
13850 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
13851 update_mips_abiflags_isa (abfd, abiflags);
13853 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
13854 abiflags->gpr_size = AFL_REG_32;
13856 abiflags->gpr_size = AFL_REG_64;
13858 abiflags->cpr1_size = AFL_REG_NONE;
13860 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
13861 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
13863 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
13864 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
13865 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
13866 && abiflags->gpr_size == AFL_REG_32))
13867 abiflags->cpr1_size = AFL_REG_32;
13868 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
13869 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
13870 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
13871 abiflags->cpr1_size = AFL_REG_64;
13873 abiflags->cpr2_size = AFL_REG_NONE;
13875 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
13876 abiflags->ases |= AFL_ASE_MDMX;
13877 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
13878 abiflags->ases |= AFL_ASE_MIPS16;
13879 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
13880 abiflags->ases |= AFL_ASE_MICROMIPS;
13882 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
13883 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
13884 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
13885 && abiflags->isa_level >= 32
13886 && abiflags->isa_ext != AFL_EXT_LOONGSON_3A)
13887 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
13890 /* We need to use a special link routine to handle the .reginfo and
13891 the .mdebug sections. We need to merge all instances of these
13892 sections together, not write them all out sequentially. */
13895 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
13898 struct bfd_link_order *p;
13899 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13900 asection *rtproc_sec, *abiflags_sec;
13901 Elf32_RegInfo reginfo;
13902 struct ecoff_debug_info debug;
13903 struct mips_htab_traverse_info hti;
13904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13905 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
13906 HDRR *symhdr = &debug.symbolic_header;
13907 void *mdebug_handle = NULL;
13912 struct mips_elf_link_hash_table *htab;
13914 static const char * const secname[] =
13916 ".text", ".init", ".fini", ".data",
13917 ".rodata", ".sdata", ".sbss", ".bss"
13919 static const int sc[] =
13921 scText, scInit, scFini, scData,
13922 scRData, scSData, scSBss, scBss
13925 /* Sort the dynamic symbols so that those with GOT entries come after
13927 htab = mips_elf_hash_table (info);
13928 BFD_ASSERT (htab != NULL);
13930 if (!mips_elf_sort_hash_table (abfd, info))
13933 /* Create any scheduled LA25 stubs. */
13935 hti.output_bfd = abfd;
13937 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13941 /* Get a value for the GP register. */
13942 if (elf_gp (abfd) == 0)
13944 struct bfd_link_hash_entry *h;
13946 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
13947 if (h != NULL && h->type == bfd_link_hash_defined)
13948 elf_gp (abfd) = (h->u.def.value
13949 + h->u.def.section->output_section->vma
13950 + h->u.def.section->output_offset);
13951 else if (htab->is_vxworks
13952 && (h = bfd_link_hash_lookup (info->hash,
13953 "_GLOBAL_OFFSET_TABLE_",
13954 FALSE, FALSE, TRUE))
13955 && h->type == bfd_link_hash_defined)
13956 elf_gp (abfd) = (h->u.def.section->output_section->vma
13957 + h->u.def.section->output_offset
13959 else if (info->relocatable)
13961 bfd_vma lo = MINUS_ONE;
13963 /* Find the GP-relative section with the lowest offset. */
13964 for (o = abfd->sections; o != NULL; o = o->next)
13966 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13969 /* And calculate GP relative to that. */
13970 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
13974 /* If the relocate_section function needs to do a reloc
13975 involving the GP value, it should make a reloc_dangerous
13976 callback to warn that GP is not defined. */
13980 /* Go through the sections and collect the .reginfo and .mdebug
13982 abiflags_sec = NULL;
13983 reginfo_sec = NULL;
13985 gptab_data_sec = NULL;
13986 gptab_bss_sec = NULL;
13987 for (o = abfd->sections; o != NULL; o = o->next)
13989 if (strcmp (o->name, ".MIPS.abiflags") == 0)
13991 /* We have found the .MIPS.abiflags section in the output file.
13992 Look through all the link_orders comprising it and remove them.
13993 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
13994 for (p = o->map_head.link_order; p != NULL; p = p->next)
13996 asection *input_section;
13998 if (p->type != bfd_indirect_link_order)
14000 if (p->type == bfd_data_link_order)
14005 input_section = p->u.indirect.section;
14007 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14008 elf_link_input_bfd ignores this section. */
14009 input_section->flags &= ~SEC_HAS_CONTENTS;
14012 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14013 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14015 /* Skip this section later on (I don't think this currently
14016 matters, but someday it might). */
14017 o->map_head.link_order = NULL;
14022 if (strcmp (o->name, ".reginfo") == 0)
14024 memset (®info, 0, sizeof reginfo);
14026 /* We have found the .reginfo section in the output file.
14027 Look through all the link_orders comprising it and merge
14028 the information together. */
14029 for (p = o->map_head.link_order; p != NULL; p = p->next)
14031 asection *input_section;
14033 Elf32_External_RegInfo ext;
14036 if (p->type != bfd_indirect_link_order)
14038 if (p->type == bfd_data_link_order)
14043 input_section = p->u.indirect.section;
14044 input_bfd = input_section->owner;
14046 if (! bfd_get_section_contents (input_bfd, input_section,
14047 &ext, 0, sizeof ext))
14050 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14052 reginfo.ri_gprmask |= sub.ri_gprmask;
14053 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14054 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14055 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14056 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14058 /* ri_gp_value is set by the function
14059 mips_elf32_section_processing when the section is
14060 finally written out. */
14062 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14063 elf_link_input_bfd ignores this section. */
14064 input_section->flags &= ~SEC_HAS_CONTENTS;
14067 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14068 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14070 /* Skip this section later on (I don't think this currently
14071 matters, but someday it might). */
14072 o->map_head.link_order = NULL;
14077 if (strcmp (o->name, ".mdebug") == 0)
14079 struct extsym_info einfo;
14082 /* We have found the .mdebug section in the output file.
14083 Look through all the link_orders comprising it and merge
14084 the information together. */
14085 symhdr->magic = swap->sym_magic;
14086 /* FIXME: What should the version stamp be? */
14087 symhdr->vstamp = 0;
14088 symhdr->ilineMax = 0;
14089 symhdr->cbLine = 0;
14090 symhdr->idnMax = 0;
14091 symhdr->ipdMax = 0;
14092 symhdr->isymMax = 0;
14093 symhdr->ioptMax = 0;
14094 symhdr->iauxMax = 0;
14095 symhdr->issMax = 0;
14096 symhdr->issExtMax = 0;
14097 symhdr->ifdMax = 0;
14099 symhdr->iextMax = 0;
14101 /* We accumulate the debugging information itself in the
14102 debug_info structure. */
14104 debug.external_dnr = NULL;
14105 debug.external_pdr = NULL;
14106 debug.external_sym = NULL;
14107 debug.external_opt = NULL;
14108 debug.external_aux = NULL;
14110 debug.ssext = debug.ssext_end = NULL;
14111 debug.external_fdr = NULL;
14112 debug.external_rfd = NULL;
14113 debug.external_ext = debug.external_ext_end = NULL;
14115 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
14116 if (mdebug_handle == NULL)
14120 esym.cobol_main = 0;
14124 esym.asym.iss = issNil;
14125 esym.asym.st = stLocal;
14126 esym.asym.reserved = 0;
14127 esym.asym.index = indexNil;
14129 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
14131 esym.asym.sc = sc[i];
14132 s = bfd_get_section_by_name (abfd, secname[i]);
14135 esym.asym.value = s->vma;
14136 last = s->vma + s->size;
14139 esym.asym.value = last;
14140 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
14141 secname[i], &esym))
14145 for (p = o->map_head.link_order; p != NULL; p = p->next)
14147 asection *input_section;
14149 const struct ecoff_debug_swap *input_swap;
14150 struct ecoff_debug_info input_debug;
14154 if (p->type != bfd_indirect_link_order)
14156 if (p->type == bfd_data_link_order)
14161 input_section = p->u.indirect.section;
14162 input_bfd = input_section->owner;
14164 if (!is_mips_elf (input_bfd))
14166 /* I don't know what a non MIPS ELF bfd would be
14167 doing with a .mdebug section, but I don't really
14168 want to deal with it. */
14172 input_swap = (get_elf_backend_data (input_bfd)
14173 ->elf_backend_ecoff_debug_swap);
14175 BFD_ASSERT (p->size == input_section->size);
14177 /* The ECOFF linking code expects that we have already
14178 read in the debugging information and set up an
14179 ecoff_debug_info structure, so we do that now. */
14180 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
14184 if (! (bfd_ecoff_debug_accumulate
14185 (mdebug_handle, abfd, &debug, swap, input_bfd,
14186 &input_debug, input_swap, info)))
14189 /* Loop through the external symbols. For each one with
14190 interesting information, try to find the symbol in
14191 the linker global hash table and save the information
14192 for the output external symbols. */
14193 eraw_src = input_debug.external_ext;
14194 eraw_end = (eraw_src
14195 + (input_debug.symbolic_header.iextMax
14196 * input_swap->external_ext_size));
14198 eraw_src < eraw_end;
14199 eraw_src += input_swap->external_ext_size)
14203 struct mips_elf_link_hash_entry *h;
14205 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
14206 if (ext.asym.sc == scNil
14207 || ext.asym.sc == scUndefined
14208 || ext.asym.sc == scSUndefined)
14211 name = input_debug.ssext + ext.asym.iss;
14212 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
14213 name, FALSE, FALSE, TRUE);
14214 if (h == NULL || h->esym.ifd != -2)
14219 BFD_ASSERT (ext.ifd
14220 < input_debug.symbolic_header.ifdMax);
14221 ext.ifd = input_debug.ifdmap[ext.ifd];
14227 /* Free up the information we just read. */
14228 free (input_debug.line);
14229 free (input_debug.external_dnr);
14230 free (input_debug.external_pdr);
14231 free (input_debug.external_sym);
14232 free (input_debug.external_opt);
14233 free (input_debug.external_aux);
14234 free (input_debug.ss);
14235 free (input_debug.ssext);
14236 free (input_debug.external_fdr);
14237 free (input_debug.external_rfd);
14238 free (input_debug.external_ext);
14240 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14241 elf_link_input_bfd ignores this section. */
14242 input_section->flags &= ~SEC_HAS_CONTENTS;
14245 if (SGI_COMPAT (abfd) && info->shared)
14247 /* Create .rtproc section. */
14248 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
14249 if (rtproc_sec == NULL)
14251 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
14252 | SEC_LINKER_CREATED | SEC_READONLY);
14254 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
14257 if (rtproc_sec == NULL
14258 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
14262 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
14268 /* Build the external symbol information. */
14271 einfo.debug = &debug;
14273 einfo.failed = FALSE;
14274 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
14275 mips_elf_output_extsym, &einfo);
14279 /* Set the size of the .mdebug section. */
14280 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
14282 /* Skip this section later on (I don't think this currently
14283 matters, but someday it might). */
14284 o->map_head.link_order = NULL;
14289 if (CONST_STRNEQ (o->name, ".gptab."))
14291 const char *subname;
14294 Elf32_External_gptab *ext_tab;
14297 /* The .gptab.sdata and .gptab.sbss sections hold
14298 information describing how the small data area would
14299 change depending upon the -G switch. These sections
14300 not used in executables files. */
14301 if (! info->relocatable)
14303 for (p = o->map_head.link_order; p != NULL; p = p->next)
14305 asection *input_section;
14307 if (p->type != bfd_indirect_link_order)
14309 if (p->type == bfd_data_link_order)
14314 input_section = p->u.indirect.section;
14316 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14317 elf_link_input_bfd ignores this section. */
14318 input_section->flags &= ~SEC_HAS_CONTENTS;
14321 /* Skip this section later on (I don't think this
14322 currently matters, but someday it might). */
14323 o->map_head.link_order = NULL;
14325 /* Really remove the section. */
14326 bfd_section_list_remove (abfd, o);
14327 --abfd->section_count;
14332 /* There is one gptab for initialized data, and one for
14333 uninitialized data. */
14334 if (strcmp (o->name, ".gptab.sdata") == 0)
14335 gptab_data_sec = o;
14336 else if (strcmp (o->name, ".gptab.sbss") == 0)
14340 (*_bfd_error_handler)
14341 (_("%s: illegal section name `%s'"),
14342 bfd_get_filename (abfd), o->name);
14343 bfd_set_error (bfd_error_nonrepresentable_section);
14347 /* The linker script always combines .gptab.data and
14348 .gptab.sdata into .gptab.sdata, and likewise for
14349 .gptab.bss and .gptab.sbss. It is possible that there is
14350 no .sdata or .sbss section in the output file, in which
14351 case we must change the name of the output section. */
14352 subname = o->name + sizeof ".gptab" - 1;
14353 if (bfd_get_section_by_name (abfd, subname) == NULL)
14355 if (o == gptab_data_sec)
14356 o->name = ".gptab.data";
14358 o->name = ".gptab.bss";
14359 subname = o->name + sizeof ".gptab" - 1;
14360 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14363 /* Set up the first entry. */
14365 amt = c * sizeof (Elf32_gptab);
14366 tab = bfd_malloc (amt);
14369 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14370 tab[0].gt_header.gt_unused = 0;
14372 /* Combine the input sections. */
14373 for (p = o->map_head.link_order; p != NULL; p = p->next)
14375 asection *input_section;
14377 bfd_size_type size;
14378 unsigned long last;
14379 bfd_size_type gpentry;
14381 if (p->type != bfd_indirect_link_order)
14383 if (p->type == bfd_data_link_order)
14388 input_section = p->u.indirect.section;
14389 input_bfd = input_section->owner;
14391 /* Combine the gptab entries for this input section one
14392 by one. We know that the input gptab entries are
14393 sorted by ascending -G value. */
14394 size = input_section->size;
14396 for (gpentry = sizeof (Elf32_External_gptab);
14398 gpentry += sizeof (Elf32_External_gptab))
14400 Elf32_External_gptab ext_gptab;
14401 Elf32_gptab int_gptab;
14407 if (! (bfd_get_section_contents
14408 (input_bfd, input_section, &ext_gptab, gpentry,
14409 sizeof (Elf32_External_gptab))))
14415 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14417 val = int_gptab.gt_entry.gt_g_value;
14418 add = int_gptab.gt_entry.gt_bytes - last;
14421 for (look = 1; look < c; look++)
14423 if (tab[look].gt_entry.gt_g_value >= val)
14424 tab[look].gt_entry.gt_bytes += add;
14426 if (tab[look].gt_entry.gt_g_value == val)
14432 Elf32_gptab *new_tab;
14435 /* We need a new table entry. */
14436 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
14437 new_tab = bfd_realloc (tab, amt);
14438 if (new_tab == NULL)
14444 tab[c].gt_entry.gt_g_value = val;
14445 tab[c].gt_entry.gt_bytes = add;
14447 /* Merge in the size for the next smallest -G
14448 value, since that will be implied by this new
14451 for (look = 1; look < c; look++)
14453 if (tab[look].gt_entry.gt_g_value < val
14455 || (tab[look].gt_entry.gt_g_value
14456 > tab[max].gt_entry.gt_g_value)))
14460 tab[c].gt_entry.gt_bytes +=
14461 tab[max].gt_entry.gt_bytes;
14466 last = int_gptab.gt_entry.gt_bytes;
14469 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14470 elf_link_input_bfd ignores this section. */
14471 input_section->flags &= ~SEC_HAS_CONTENTS;
14474 /* The table must be sorted by -G value. */
14476 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14478 /* Swap out the table. */
14479 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
14480 ext_tab = bfd_alloc (abfd, amt);
14481 if (ext_tab == NULL)
14487 for (j = 0; j < c; j++)
14488 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14491 o->size = c * sizeof (Elf32_External_gptab);
14492 o->contents = (bfd_byte *) ext_tab;
14494 /* Skip this section later on (I don't think this currently
14495 matters, but someday it might). */
14496 o->map_head.link_order = NULL;
14500 /* Invoke the regular ELF backend linker to do all the work. */
14501 if (!bfd_elf_final_link (abfd, info))
14504 /* Now write out the computed sections. */
14506 if (abiflags_sec != NULL)
14508 Elf_External_ABIFlags_v0 ext;
14509 Elf_Internal_ABIFlags_v0 *abiflags;
14511 abiflags = &mips_elf_tdata (abfd)->abiflags;
14513 /* Set up the abiflags if no valid input sections were found. */
14514 if (!mips_elf_tdata (abfd)->abiflags_valid)
14516 infer_mips_abiflags (abfd, abiflags);
14517 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
14519 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
14520 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
14524 if (reginfo_sec != NULL)
14526 Elf32_External_RegInfo ext;
14528 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
14529 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
14533 if (mdebug_sec != NULL)
14535 BFD_ASSERT (abfd->output_has_begun);
14536 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14538 mdebug_sec->filepos))
14541 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14544 if (gptab_data_sec != NULL)
14546 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14547 gptab_data_sec->contents,
14548 0, gptab_data_sec->size))
14552 if (gptab_bss_sec != NULL)
14554 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14555 gptab_bss_sec->contents,
14556 0, gptab_bss_sec->size))
14560 if (SGI_COMPAT (abfd))
14562 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14563 if (rtproc_sec != NULL)
14565 if (! bfd_set_section_contents (abfd, rtproc_sec,
14566 rtproc_sec->contents,
14567 0, rtproc_sec->size))
14575 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14577 struct mips_mach_extension
14579 unsigned long extension, base;
14583 /* An array describing how BFD machines relate to one another. The entries
14584 are ordered topologically with MIPS I extensions listed last. */
14586 static const struct mips_mach_extension mips_mach_extensions[] =
14588 /* MIPS64r2 extensions. */
14589 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14590 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14591 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14592 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
14594 /* MIPS64 extensions. */
14595 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14596 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14597 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14599 /* MIPS V extensions. */
14600 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14602 /* R10000 extensions. */
14603 { bfd_mach_mips12000, bfd_mach_mips10000 },
14604 { bfd_mach_mips14000, bfd_mach_mips10000 },
14605 { bfd_mach_mips16000, bfd_mach_mips10000 },
14607 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14608 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14609 better to allow vr5400 and vr5500 code to be merged anyway, since
14610 many libraries will just use the core ISA. Perhaps we could add
14611 some sort of ASE flag if this ever proves a problem. */
14612 { bfd_mach_mips5500, bfd_mach_mips5400 },
14613 { bfd_mach_mips5400, bfd_mach_mips5000 },
14615 /* MIPS IV extensions. */
14616 { bfd_mach_mips5, bfd_mach_mips8000 },
14617 { bfd_mach_mips10000, bfd_mach_mips8000 },
14618 { bfd_mach_mips5000, bfd_mach_mips8000 },
14619 { bfd_mach_mips7000, bfd_mach_mips8000 },
14620 { bfd_mach_mips9000, bfd_mach_mips8000 },
14622 /* VR4100 extensions. */
14623 { bfd_mach_mips4120, bfd_mach_mips4100 },
14624 { bfd_mach_mips4111, bfd_mach_mips4100 },
14626 /* MIPS III extensions. */
14627 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14628 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14629 { bfd_mach_mips8000, bfd_mach_mips4000 },
14630 { bfd_mach_mips4650, bfd_mach_mips4000 },
14631 { bfd_mach_mips4600, bfd_mach_mips4000 },
14632 { bfd_mach_mips4400, bfd_mach_mips4000 },
14633 { bfd_mach_mips4300, bfd_mach_mips4000 },
14634 { bfd_mach_mips4100, bfd_mach_mips4000 },
14635 { bfd_mach_mips4010, bfd_mach_mips4000 },
14636 { bfd_mach_mips5900, bfd_mach_mips4000 },
14638 /* MIPS32 extensions. */
14639 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14641 /* MIPS II extensions. */
14642 { bfd_mach_mips4000, bfd_mach_mips6000 },
14643 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14645 /* MIPS I extensions. */
14646 { bfd_mach_mips6000, bfd_mach_mips3000 },
14647 { bfd_mach_mips3900, bfd_mach_mips3000 }
14651 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14654 mips_mach_extends_p (unsigned long base, unsigned long extension)
14658 if (extension == base)
14661 if (base == bfd_mach_mipsisa32
14662 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14665 if (base == bfd_mach_mipsisa32r2
14666 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14669 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14670 if (extension == mips_mach_extensions[i].extension)
14672 extension = mips_mach_extensions[i].base;
14673 if (extension == base)
14681 /* Merge object attributes from IBFD into OBFD. Raise an error if
14682 there are conflicting attributes. */
14684 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14686 obj_attribute *in_attr;
14687 obj_attribute *out_attr;
14691 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14692 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
14693 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14694 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14696 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14698 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14699 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14701 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14703 /* This is the first object. Copy the attributes. */
14704 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14706 /* Use the Tag_null value to indicate the attributes have been
14708 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14713 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14714 non-conflicting ones. */
14715 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14716 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14720 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
14721 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14722 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
14723 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
14724 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
14725 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
14726 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
14727 || in_fp == Val_GNU_MIPS_ABI_FP_64
14728 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
14730 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14731 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14733 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
14734 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
14735 || out_fp == Val_GNU_MIPS_ABI_FP_64
14736 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
14737 /* Keep the current setting. */;
14738 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
14739 && in_fp == Val_GNU_MIPS_ABI_FP_64)
14741 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14742 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14744 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
14745 && out_fp == Val_GNU_MIPS_ABI_FP_64)
14746 /* Keep the current setting. */;
14747 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
14749 const char *out_string, *in_string;
14751 out_string = _bfd_mips_fp_abi_string (out_fp);
14752 in_string = _bfd_mips_fp_abi_string (in_fp);
14753 /* First warn about cases involving unrecognised ABIs. */
14754 if (!out_string && !in_string)
14756 (_("Warning: %B uses unknown floating point ABI %d "
14757 "(set by %B), %B uses unknown floating point ABI %d"),
14758 obfd, abi_fp_bfd, ibfd, out_fp, in_fp);
14759 else if (!out_string)
14761 (_("Warning: %B uses unknown floating point ABI %d "
14762 "(set by %B), %B uses %s"),
14763 obfd, abi_fp_bfd, ibfd, out_fp, in_string);
14764 else if (!in_string)
14766 (_("Warning: %B uses %s (set by %B), "
14767 "%B uses unknown floating point ABI %d"),
14768 obfd, abi_fp_bfd, ibfd, out_string, in_fp);
14771 /* If one of the bfds is soft-float, the other must be
14772 hard-float. The exact choice of hard-float ABI isn't
14773 really relevant to the error message. */
14774 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14775 out_string = "-mhard-float";
14776 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14777 in_string = "-mhard-float";
14779 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14780 obfd, abi_fp_bfd, ibfd, out_string, in_string);
14785 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14786 non-conflicting ones. */
14787 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14789 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14790 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
14791 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
14792 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14793 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14795 case Val_GNU_MIPS_ABI_MSA_128:
14797 (_("Warning: %B uses %s (set by %B), "
14798 "%B uses unknown MSA ABI %d"),
14799 obfd, abi_msa_bfd, ibfd,
14800 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14804 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
14806 case Val_GNU_MIPS_ABI_MSA_128:
14808 (_("Warning: %B uses unknown MSA ABI %d "
14809 "(set by %B), %B uses %s"),
14810 obfd, abi_msa_bfd, ibfd,
14811 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
14816 (_("Warning: %B uses unknown MSA ABI %d "
14817 "(set by %B), %B uses unknown MSA ABI %d"),
14818 obfd, abi_msa_bfd, ibfd,
14819 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
14820 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14826 /* Merge Tag_compatibility attributes and any common GNU ones. */
14827 _bfd_elf_merge_object_attributes (ibfd, obfd);
14832 /* Merge backend specific data from an object file to the output
14833 object file when linking. */
14836 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
14838 flagword old_flags;
14839 flagword new_flags;
14841 bfd_boolean null_input_bfd = TRUE;
14843 obj_attribute *out_attr;
14845 /* Check if we have the same endianness. */
14846 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
14848 (*_bfd_error_handler)
14849 (_("%B: endianness incompatible with that of the selected emulation"),
14854 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
14857 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14859 (*_bfd_error_handler)
14860 (_("%B: ABI is incompatible with that of the selected emulation"),
14865 /* Set up the FP ABI attribute from the abiflags if it is not already
14867 if (mips_elf_tdata (ibfd)->abiflags_valid)
14869 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
14870 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
14871 in_attr[Tag_GNU_MIPS_ABI_FP].i =
14872 mips_elf_tdata (ibfd)->abiflags.fp_abi;
14875 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14878 /* Check to see if the input BFD actually contains any sections.
14879 If not, its flags may not have been initialised either, but it cannot
14880 actually cause any incompatibility. */
14881 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14883 /* Ignore synthetic sections and empty .text, .data and .bss sections
14884 which are automatically generated by gas. Also ignore fake
14885 (s)common sections, since merely defining a common symbol does
14886 not affect compatibility. */
14887 if ((sec->flags & SEC_IS_COMMON) == 0
14888 && strcmp (sec->name, ".reginfo")
14889 && strcmp (sec->name, ".mdebug")
14891 || (strcmp (sec->name, ".text")
14892 && strcmp (sec->name, ".data")
14893 && strcmp (sec->name, ".bss"))))
14895 null_input_bfd = FALSE;
14899 if (null_input_bfd)
14902 /* Populate abiflags using existing information. */
14903 if (!mips_elf_tdata (ibfd)->abiflags_valid)
14905 infer_mips_abiflags (ibfd, &mips_elf_tdata (ibfd)->abiflags);
14906 mips_elf_tdata (ibfd)->abiflags_valid = TRUE;
14910 Elf_Internal_ABIFlags_v0 abiflags;
14911 Elf_Internal_ABIFlags_v0 in_abiflags;
14912 infer_mips_abiflags (ibfd, &abiflags);
14913 in_abiflags = mips_elf_tdata (ibfd)->abiflags;
14915 /* It is not possible to infer the correct ISA revision
14916 for R3 or R5 so drop down to R2 for the checks. */
14917 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
14918 in_abiflags.isa_rev = 2;
14920 if (in_abiflags.isa_level != abiflags.isa_level
14921 || in_abiflags.isa_rev != abiflags.isa_rev
14922 || in_abiflags.isa_ext != abiflags.isa_ext)
14923 (*_bfd_error_handler)
14924 (_("%B: warning: Inconsistent ISA between e_flags and "
14925 ".MIPS.abiflags"), ibfd);
14926 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14927 && in_abiflags.fp_abi != abiflags.fp_abi)
14928 (*_bfd_error_handler)
14929 (_("%B: warning: Inconsistent FP ABI between e_flags and "
14930 ".MIPS.abiflags"), ibfd);
14931 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
14932 (*_bfd_error_handler)
14933 (_("%B: warning: Inconsistent ASEs between e_flags and "
14934 ".MIPS.abiflags"), ibfd);
14935 if (in_abiflags.isa_ext != abiflags.isa_ext)
14936 (*_bfd_error_handler)
14937 (_("%B: warning: Inconsistent ISA extensions between e_flags and "
14938 ".MIPS.abiflags"), ibfd);
14939 if (in_abiflags.flags2 != 0)
14940 (*_bfd_error_handler)
14941 (_("%B: warning: Unexpected flag in the flags2 field of "
14942 ".MIPS.abiflags (0x%lx)"), ibfd,
14943 (unsigned long) in_abiflags.flags2);
14946 if (!mips_elf_tdata (obfd)->abiflags_valid)
14948 /* Copy input abiflags if output abiflags are not already valid. */
14949 mips_elf_tdata (obfd)->abiflags = mips_elf_tdata (ibfd)->abiflags;
14950 mips_elf_tdata (obfd)->abiflags_valid = TRUE;
14953 if (! elf_flags_init (obfd))
14955 elf_flags_init (obfd) = TRUE;
14956 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
14957 elf_elfheader (obfd)->e_ident[EI_CLASS]
14958 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14960 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
14961 && (bfd_get_arch_info (obfd)->the_default
14962 || mips_mach_extends_p (bfd_get_mach (obfd),
14963 bfd_get_mach (ibfd))))
14965 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14966 bfd_get_mach (ibfd)))
14969 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
14970 update_mips_abiflags_isa (obfd, &mips_elf_tdata (obfd)->abiflags);
14976 /* Update the output abiflags fp_abi using the computed fp_abi. */
14977 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14978 mips_elf_tdata (obfd)->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
14980 #define max(a,b) ((a) > (b) ? (a) : (b))
14981 /* Merge abiflags. */
14982 mips_elf_tdata (obfd)->abiflags.isa_rev
14983 = max (mips_elf_tdata (obfd)->abiflags.isa_rev,
14984 mips_elf_tdata (ibfd)->abiflags.isa_rev);
14985 mips_elf_tdata (obfd)->abiflags.gpr_size
14986 = max (mips_elf_tdata (obfd)->abiflags.gpr_size,
14987 mips_elf_tdata (ibfd)->abiflags.gpr_size);
14988 mips_elf_tdata (obfd)->abiflags.cpr1_size
14989 = max (mips_elf_tdata (obfd)->abiflags.cpr1_size,
14990 mips_elf_tdata (ibfd)->abiflags.cpr1_size);
14991 mips_elf_tdata (obfd)->abiflags.cpr2_size
14992 = max (mips_elf_tdata (obfd)->abiflags.cpr2_size,
14993 mips_elf_tdata (ibfd)->abiflags.cpr2_size);
14995 mips_elf_tdata (obfd)->abiflags.ases
14996 |= mips_elf_tdata (ibfd)->abiflags.ases;
14997 mips_elf_tdata (obfd)->abiflags.flags1
14998 |= mips_elf_tdata (ibfd)->abiflags.flags1;
15000 new_flags = elf_elfheader (ibfd)->e_flags;
15001 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15002 old_flags = elf_elfheader (obfd)->e_flags;
15004 /* Check flag compatibility. */
15006 new_flags &= ~EF_MIPS_NOREORDER;
15007 old_flags &= ~EF_MIPS_NOREORDER;
15009 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15010 doesn't seem to matter. */
15011 new_flags &= ~EF_MIPS_XGOT;
15012 old_flags &= ~EF_MIPS_XGOT;
15014 /* MIPSpro generates ucode info in n64 objects. Again, we should
15015 just be able to ignore this. */
15016 new_flags &= ~EF_MIPS_UCODE;
15017 old_flags &= ~EF_MIPS_UCODE;
15019 /* DSOs should only be linked with CPIC code. */
15020 if ((ibfd->flags & DYNAMIC) != 0)
15021 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15023 if (new_flags == old_flags)
15028 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15029 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15031 (*_bfd_error_handler)
15032 (_("%B: warning: linking abicalls files with non-abicalls files"),
15037 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15038 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15039 if (! (new_flags & EF_MIPS_PIC))
15040 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15042 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15043 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15045 /* Compare the ISAs. */
15046 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15048 (*_bfd_error_handler)
15049 (_("%B: linking 32-bit code with 64-bit code"),
15053 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15055 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15056 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15058 /* Copy the architecture info from IBFD to OBFD. Also copy
15059 the 32-bit flag (if set) so that we continue to recognise
15060 OBFD as a 32-bit binary. */
15061 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15062 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15063 elf_elfheader (obfd)->e_flags
15064 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15066 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15067 update_mips_abiflags_isa (obfd, &mips_elf_tdata (obfd)->abiflags);
15069 /* Copy across the ABI flags if OBFD doesn't use them
15070 and if that was what caused us to treat IBFD as 32-bit. */
15071 if ((old_flags & EF_MIPS_ABI) == 0
15072 && mips_32bit_flags_p (new_flags)
15073 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15074 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15078 /* The ISAs aren't compatible. */
15079 (*_bfd_error_handler)
15080 (_("%B: linking %s module with previous %s modules"),
15082 bfd_printable_name (ibfd),
15083 bfd_printable_name (obfd));
15088 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15089 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15091 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15092 does set EI_CLASS differently from any 32-bit ABI. */
15093 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15094 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15095 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15097 /* Only error if both are set (to different values). */
15098 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15099 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15100 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15102 (*_bfd_error_handler)
15103 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
15105 elf_mips_abi_name (ibfd),
15106 elf_mips_abi_name (obfd));
15109 new_flags &= ~EF_MIPS_ABI;
15110 old_flags &= ~EF_MIPS_ABI;
15113 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15114 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15115 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15117 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15118 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15119 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15120 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15121 int micro_mis = old_m16 && new_micro;
15122 int m16_mis = old_micro && new_m16;
15124 if (m16_mis || micro_mis)
15126 (*_bfd_error_handler)
15127 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
15129 m16_mis ? "MIPS16" : "microMIPS",
15130 m16_mis ? "microMIPS" : "MIPS16");
15134 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15136 new_flags &= ~ EF_MIPS_ARCH_ASE;
15137 old_flags &= ~ EF_MIPS_ARCH_ASE;
15140 /* Compare NaN encodings. */
15141 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15143 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
15145 (new_flags & EF_MIPS_NAN2008
15146 ? "-mnan=2008" : "-mnan=legacy"),
15147 (old_flags & EF_MIPS_NAN2008
15148 ? "-mnan=2008" : "-mnan=legacy"));
15150 new_flags &= ~EF_MIPS_NAN2008;
15151 old_flags &= ~EF_MIPS_NAN2008;
15154 /* Compare FP64 state. */
15155 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15157 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
15159 (new_flags & EF_MIPS_FP64
15160 ? "-mfp64" : "-mfp32"),
15161 (old_flags & EF_MIPS_FP64
15162 ? "-mfp64" : "-mfp32"));
15164 new_flags &= ~EF_MIPS_FP64;
15165 old_flags &= ~EF_MIPS_FP64;
15168 /* Warn about any other mismatches */
15169 if (new_flags != old_flags)
15171 (*_bfd_error_handler)
15172 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
15173 ibfd, (unsigned long) new_flags,
15174 (unsigned long) old_flags);
15180 bfd_set_error (bfd_error_bad_value);
15187 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
15190 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
15192 BFD_ASSERT (!elf_flags_init (abfd)
15193 || elf_elfheader (abfd)->e_flags == flags);
15195 elf_elfheader (abfd)->e_flags = flags;
15196 elf_flags_init (abfd) = TRUE;
15201 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
15205 default: return "";
15206 case DT_MIPS_RLD_VERSION:
15207 return "MIPS_RLD_VERSION";
15208 case DT_MIPS_TIME_STAMP:
15209 return "MIPS_TIME_STAMP";
15210 case DT_MIPS_ICHECKSUM:
15211 return "MIPS_ICHECKSUM";
15212 case DT_MIPS_IVERSION:
15213 return "MIPS_IVERSION";
15214 case DT_MIPS_FLAGS:
15215 return "MIPS_FLAGS";
15216 case DT_MIPS_BASE_ADDRESS:
15217 return "MIPS_BASE_ADDRESS";
15219 return "MIPS_MSYM";
15220 case DT_MIPS_CONFLICT:
15221 return "MIPS_CONFLICT";
15222 case DT_MIPS_LIBLIST:
15223 return "MIPS_LIBLIST";
15224 case DT_MIPS_LOCAL_GOTNO:
15225 return "MIPS_LOCAL_GOTNO";
15226 case DT_MIPS_CONFLICTNO:
15227 return "MIPS_CONFLICTNO";
15228 case DT_MIPS_LIBLISTNO:
15229 return "MIPS_LIBLISTNO";
15230 case DT_MIPS_SYMTABNO:
15231 return "MIPS_SYMTABNO";
15232 case DT_MIPS_UNREFEXTNO:
15233 return "MIPS_UNREFEXTNO";
15234 case DT_MIPS_GOTSYM:
15235 return "MIPS_GOTSYM";
15236 case DT_MIPS_HIPAGENO:
15237 return "MIPS_HIPAGENO";
15238 case DT_MIPS_RLD_MAP:
15239 return "MIPS_RLD_MAP";
15240 case DT_MIPS_DELTA_CLASS:
15241 return "MIPS_DELTA_CLASS";
15242 case DT_MIPS_DELTA_CLASS_NO:
15243 return "MIPS_DELTA_CLASS_NO";
15244 case DT_MIPS_DELTA_INSTANCE:
15245 return "MIPS_DELTA_INSTANCE";
15246 case DT_MIPS_DELTA_INSTANCE_NO:
15247 return "MIPS_DELTA_INSTANCE_NO";
15248 case DT_MIPS_DELTA_RELOC:
15249 return "MIPS_DELTA_RELOC";
15250 case DT_MIPS_DELTA_RELOC_NO:
15251 return "MIPS_DELTA_RELOC_NO";
15252 case DT_MIPS_DELTA_SYM:
15253 return "MIPS_DELTA_SYM";
15254 case DT_MIPS_DELTA_SYM_NO:
15255 return "MIPS_DELTA_SYM_NO";
15256 case DT_MIPS_DELTA_CLASSSYM:
15257 return "MIPS_DELTA_CLASSSYM";
15258 case DT_MIPS_DELTA_CLASSSYM_NO:
15259 return "MIPS_DELTA_CLASSSYM_NO";
15260 case DT_MIPS_CXX_FLAGS:
15261 return "MIPS_CXX_FLAGS";
15262 case DT_MIPS_PIXIE_INIT:
15263 return "MIPS_PIXIE_INIT";
15264 case DT_MIPS_SYMBOL_LIB:
15265 return "MIPS_SYMBOL_LIB";
15266 case DT_MIPS_LOCALPAGE_GOTIDX:
15267 return "MIPS_LOCALPAGE_GOTIDX";
15268 case DT_MIPS_LOCAL_GOTIDX:
15269 return "MIPS_LOCAL_GOTIDX";
15270 case DT_MIPS_HIDDEN_GOTIDX:
15271 return "MIPS_HIDDEN_GOTIDX";
15272 case DT_MIPS_PROTECTED_GOTIDX:
15273 return "MIPS_PROTECTED_GOT_IDX";
15274 case DT_MIPS_OPTIONS:
15275 return "MIPS_OPTIONS";
15276 case DT_MIPS_INTERFACE:
15277 return "MIPS_INTERFACE";
15278 case DT_MIPS_DYNSTR_ALIGN:
15279 return "DT_MIPS_DYNSTR_ALIGN";
15280 case DT_MIPS_INTERFACE_SIZE:
15281 return "DT_MIPS_INTERFACE_SIZE";
15282 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
15283 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
15284 case DT_MIPS_PERF_SUFFIX:
15285 return "DT_MIPS_PERF_SUFFIX";
15286 case DT_MIPS_COMPACT_SIZE:
15287 return "DT_MIPS_COMPACT_SIZE";
15288 case DT_MIPS_GP_VALUE:
15289 return "DT_MIPS_GP_VALUE";
15290 case DT_MIPS_AUX_DYNAMIC:
15291 return "DT_MIPS_AUX_DYNAMIC";
15292 case DT_MIPS_PLTGOT:
15293 return "DT_MIPS_PLTGOT";
15294 case DT_MIPS_RWPLT:
15295 return "DT_MIPS_RWPLT";
15299 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
15303 _bfd_mips_fp_abi_string (int fp)
15307 /* These strings aren't translated because they're simply
15309 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15310 return "-mdouble-float";
15312 case Val_GNU_MIPS_ABI_FP_SINGLE:
15313 return "-msingle-float";
15315 case Val_GNU_MIPS_ABI_FP_SOFT:
15316 return "-msoft-float";
15318 case Val_GNU_MIPS_ABI_FP_OLD_64:
15319 return _("-mips32r2 -mfp64 (12 callee-saved)");
15321 case Val_GNU_MIPS_ABI_FP_XX:
15324 case Val_GNU_MIPS_ABI_FP_64:
15325 return "-mgp32 -mfp64";
15327 case Val_GNU_MIPS_ABI_FP_64A:
15328 return "-mgp32 -mfp64 -mno-odd-spreg";
15336 print_mips_ases (FILE *file, unsigned int mask)
15338 if (mask & AFL_ASE_DSP)
15339 fputs ("\n\tDSP ASE", file);
15340 if (mask & AFL_ASE_DSPR2)
15341 fputs ("\n\tDSP R2 ASE", file);
15342 if (mask & AFL_ASE_EVA)
15343 fputs ("\n\tEnhanced VA Scheme", file);
15344 if (mask & AFL_ASE_MCU)
15345 fputs ("\n\tMCU (MicroController) ASE", file);
15346 if (mask & AFL_ASE_MDMX)
15347 fputs ("\n\tMDMX ASE", file);
15348 if (mask & AFL_ASE_MIPS3D)
15349 fputs ("\n\tMIPS-3D ASE", file);
15350 if (mask & AFL_ASE_MT)
15351 fputs ("\n\tMT ASE", file);
15352 if (mask & AFL_ASE_SMARTMIPS)
15353 fputs ("\n\tSmartMIPS ASE", file);
15354 if (mask & AFL_ASE_VIRT)
15355 fputs ("\n\tVZ ASE", file);
15356 if (mask & AFL_ASE_MSA)
15357 fputs ("\n\tMSA ASE", file);
15358 if (mask & AFL_ASE_MIPS16)
15359 fputs ("\n\tMIPS16 ASE", file);
15360 if (mask & AFL_ASE_MICROMIPS)
15361 fputs ("\n\tMICROMIPS ASE", file);
15362 if (mask & AFL_ASE_XPA)
15363 fputs ("\n\tXPA ASE", file);
15365 fprintf (file, "\n\t%s", _("None"));
15369 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
15374 fputs (_("None"), file);
15377 fputs ("RMI XLR", file);
15379 case AFL_EXT_OCTEON2:
15380 fputs ("Cavium Networks Octeon2", file);
15382 case AFL_EXT_OCTEONP:
15383 fputs ("Cavium Networks OcteonP", file);
15385 case AFL_EXT_LOONGSON_3A:
15386 fputs ("Loongson 3A", file);
15388 case AFL_EXT_OCTEON:
15389 fputs ("Cavium Networks Octeon", file);
15392 fputs ("Toshiba R5900", file);
15395 fputs ("MIPS R4650", file);
15398 fputs ("LSI R4010", file);
15401 fputs ("NEC VR4100", file);
15404 fputs ("Toshiba R3900", file);
15406 case AFL_EXT_10000:
15407 fputs ("MIPS R10000", file);
15410 fputs ("Broadcom SB-1", file);
15413 fputs ("NEC VR4111/VR4181", file);
15416 fputs ("NEC VR4120", file);
15419 fputs ("NEC VR5400", file);
15422 fputs ("NEC VR5500", file);
15424 case AFL_EXT_LOONGSON_2E:
15425 fputs ("ST Microelectronics Loongson 2E", file);
15427 case AFL_EXT_LOONGSON_2F:
15428 fputs ("ST Microelectronics Loongson 2F", file);
15431 fputs (_("Unknown"), file);
15437 print_mips_fp_abi_value (FILE *file, int val)
15441 case Val_GNU_MIPS_ABI_FP_ANY:
15442 fprintf (file, _("Hard or soft float\n"));
15444 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15445 fprintf (file, _("Hard float (double precision)\n"));
15447 case Val_GNU_MIPS_ABI_FP_SINGLE:
15448 fprintf (file, _("Hard float (single precision)\n"));
15450 case Val_GNU_MIPS_ABI_FP_SOFT:
15451 fprintf (file, _("Soft float\n"));
15453 case Val_GNU_MIPS_ABI_FP_OLD_64:
15454 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
15456 case Val_GNU_MIPS_ABI_FP_XX:
15457 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
15459 case Val_GNU_MIPS_ABI_FP_64:
15460 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
15462 case Val_GNU_MIPS_ABI_FP_64A:
15463 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
15466 fprintf (file, "??? (%d)\n", val);
15472 get_mips_reg_size (int reg_size)
15474 return (reg_size == AFL_REG_NONE) ? 0
15475 : (reg_size == AFL_REG_32) ? 32
15476 : (reg_size == AFL_REG_64) ? 64
15477 : (reg_size == AFL_REG_128) ? 128
15482 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
15486 BFD_ASSERT (abfd != NULL && ptr != NULL);
15488 /* Print normal ELF private data. */
15489 _bfd_elf_print_private_bfd_data (abfd, ptr);
15491 /* xgettext:c-format */
15492 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
15494 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
15495 fprintf (file, _(" [abi=O32]"));
15496 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
15497 fprintf (file, _(" [abi=O64]"));
15498 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
15499 fprintf (file, _(" [abi=EABI32]"));
15500 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
15501 fprintf (file, _(" [abi=EABI64]"));
15502 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
15503 fprintf (file, _(" [abi unknown]"));
15504 else if (ABI_N32_P (abfd))
15505 fprintf (file, _(" [abi=N32]"));
15506 else if (ABI_64_P (abfd))
15507 fprintf (file, _(" [abi=64]"));
15509 fprintf (file, _(" [no abi set]"));
15511 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
15512 fprintf (file, " [mips1]");
15513 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
15514 fprintf (file, " [mips2]");
15515 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
15516 fprintf (file, " [mips3]");
15517 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
15518 fprintf (file, " [mips4]");
15519 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
15520 fprintf (file, " [mips5]");
15521 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
15522 fprintf (file, " [mips32]");
15523 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
15524 fprintf (file, " [mips64]");
15525 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
15526 fprintf (file, " [mips32r2]");
15527 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
15528 fprintf (file, " [mips64r2]");
15530 fprintf (file, _(" [unknown ISA]"));
15532 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
15533 fprintf (file, " [mdmx]");
15535 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
15536 fprintf (file, " [mips16]");
15538 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
15539 fprintf (file, " [micromips]");
15541 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
15542 fprintf (file, " [nan2008]");
15544 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
15545 fprintf (file, " [old fp64]");
15547 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
15548 fprintf (file, " [32bitmode]");
15550 fprintf (file, _(" [not 32bitmode]"));
15552 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
15553 fprintf (file, " [noreorder]");
15555 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
15556 fprintf (file, " [PIC]");
15558 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
15559 fprintf (file, " [CPIC]");
15561 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
15562 fprintf (file, " [XGOT]");
15564 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
15565 fprintf (file, " [UCODE]");
15567 fputc ('\n', file);
15569 if (mips_elf_tdata (abfd)->abiflags_valid)
15571 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
15572 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
15573 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
15574 if (abiflags->isa_rev > 1)
15575 fprintf (file, "r%d", abiflags->isa_rev);
15576 fprintf (file, "\nGPR size: %d",
15577 get_mips_reg_size (abiflags->gpr_size));
15578 fprintf (file, "\nCPR1 size: %d",
15579 get_mips_reg_size (abiflags->cpr1_size));
15580 fprintf (file, "\nCPR2 size: %d",
15581 get_mips_reg_size (abiflags->cpr2_size));
15582 fputs ("\nFP ABI: ", file);
15583 print_mips_fp_abi_value (file, abiflags->fp_abi);
15584 fputs ("ISA Extension: ", file);
15585 print_mips_isa_ext (file, abiflags->isa_ext);
15586 fputs ("\nASEs:", file);
15587 print_mips_ases (file, abiflags->ases);
15588 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
15589 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
15590 fputc ('\n', file);
15596 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
15598 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15599 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15600 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
15601 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15602 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15603 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
15604 { NULL, 0, 0, 0, 0 }
15607 /* Merge non visibility st_other attributes. Ensure that the
15608 STO_OPTIONAL flag is copied into h->other, even if this is not a
15609 definiton of the symbol. */
15611 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
15612 const Elf_Internal_Sym *isym,
15613 bfd_boolean definition,
15614 bfd_boolean dynamic ATTRIBUTE_UNUSED)
15616 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
15618 unsigned char other;
15620 other = (definition ? isym->st_other : h->other);
15621 other &= ~ELF_ST_VISIBILITY (-1);
15622 h->other = other | ELF_ST_VISIBILITY (h->other);
15626 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
15627 h->other |= STO_OPTIONAL;
15630 /* Decide whether an undefined symbol is special and can be ignored.
15631 This is the case for OPTIONAL symbols on IRIX. */
15633 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
15635 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
15639 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
15641 return (sym->st_shndx == SHN_COMMON
15642 || sym->st_shndx == SHN_MIPS_ACOMMON
15643 || sym->st_shndx == SHN_MIPS_SCOMMON);
15646 /* Return address for Ith PLT stub in section PLT, for relocation REL
15647 or (bfd_vma) -1 if it should not be included. */
15650 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
15651 const arelent *rel ATTRIBUTE_UNUSED)
15654 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15655 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15658 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15659 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15660 and .got.plt and also the slots may be of a different size each we walk
15661 the PLT manually fetching instructions and matching them against known
15662 patterns. To make things easier standard MIPS slots, if any, always come
15663 first. As we don't create proper ELF symbols we use the UDATA.I member
15664 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15665 with the ST_OTHER member of the ELF symbol. */
15668 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15669 long symcount ATTRIBUTE_UNUSED,
15670 asymbol **syms ATTRIBUTE_UNUSED,
15671 long dynsymcount, asymbol **dynsyms,
15674 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15675 static const char microsuffix[] = "@micromipsplt";
15676 static const char m16suffix[] = "@mips16plt";
15677 static const char mipssuffix[] = "@plt";
15679 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15681 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15682 Elf_Internal_Shdr *hdr;
15683 bfd_byte *plt_data;
15684 bfd_vma plt_offset;
15685 unsigned int other;
15686 bfd_vma entry_size;
15705 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15708 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15709 if (relplt == NULL)
15712 hdr = &elf_section_data (relplt)->this_hdr;
15713 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15716 plt = bfd_get_section_by_name (abfd, ".plt");
15720 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15721 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15723 p = relplt->relocation;
15725 /* Calculating the exact amount of space required for symbols would
15726 require two passes over the PLT, so just pessimise assuming two
15727 PLT slots per relocation. */
15728 count = relplt->size / hdr->sh_entsize;
15729 counti = count * bed->s->int_rels_per_ext_rel;
15730 size = 2 * count * sizeof (asymbol);
15731 size += count * (sizeof (mipssuffix) +
15732 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15733 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15734 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15736 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15737 size += sizeof (asymbol) + sizeof (pltname);
15739 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15742 if (plt->size < 16)
15745 s = *ret = bfd_malloc (size);
15748 send = s + 2 * count + 1;
15750 names = (char *) send;
15751 nend = (char *) s + size;
15754 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15755 if (opcode == 0x3302fffe)
15759 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15760 other = STO_MICROMIPS;
15762 else if (opcode == 0x0398c1d0)
15766 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15767 other = STO_MICROMIPS;
15771 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15776 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15780 s->udata.i = other;
15781 memcpy (names, pltname, sizeof (pltname));
15782 names += sizeof (pltname);
15786 for (plt_offset = plt0_size;
15787 plt_offset + 8 <= plt->size && s < send;
15788 plt_offset += entry_size)
15790 bfd_vma gotplt_addr;
15791 const char *suffix;
15796 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15798 /* Check if the second word matches the expected MIPS16 instruction. */
15799 if (opcode == 0x651aeb00)
15803 /* Truncated table??? */
15804 if (plt_offset + 16 > plt->size)
15806 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15807 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15808 suffixlen = sizeof (m16suffix);
15809 suffix = m16suffix;
15810 other = STO_MIPS16;
15812 /* Likewise the expected microMIPS instruction (no insn32 mode). */
15813 else if (opcode == 0xff220000)
15817 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15818 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15819 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15821 gotplt_addr = gotplt_hi + gotplt_lo;
15822 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15823 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15824 suffixlen = sizeof (microsuffix);
15825 suffix = microsuffix;
15826 other = STO_MICROMIPS;
15828 /* Likewise the expected microMIPS instruction (insn32 mode). */
15829 else if ((opcode & 0xffff0000) == 0xff2f0000)
15831 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15832 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15833 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15834 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15835 gotplt_addr = gotplt_hi + gotplt_lo;
15836 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15837 suffixlen = sizeof (microsuffix);
15838 suffix = microsuffix;
15839 other = STO_MICROMIPS;
15841 /* Otherwise assume standard MIPS code. */
15844 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15845 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15846 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15847 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15848 gotplt_addr = gotplt_hi + gotplt_lo;
15849 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15850 suffixlen = sizeof (mipssuffix);
15851 suffix = mipssuffix;
15854 /* Truncated table??? */
15855 if (plt_offset + entry_size > plt->size)
15859 i < count && p[pi].address != gotplt_addr;
15860 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15867 *s = **p[pi].sym_ptr_ptr;
15868 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15869 we are defining a symbol, ensure one of them is set. */
15870 if ((s->flags & BSF_LOCAL) == 0)
15871 s->flags |= BSF_GLOBAL;
15872 s->flags |= BSF_SYNTHETIC;
15874 s->value = plt_offset;
15876 s->udata.i = other;
15878 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15879 namelen = len + suffixlen;
15880 if (names + namelen > nend)
15883 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15885 memcpy (names, suffix, suffixlen);
15886 names += suffixlen;
15889 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15899 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15901 struct mips_elf_link_hash_table *htab;
15902 Elf_Internal_Ehdr *i_ehdrp;
15904 i_ehdrp = elf_elfheader (abfd);
15907 htab = mips_elf_hash_table (link_info);
15908 BFD_ASSERT (htab != NULL);
15910 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15911 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
15914 _bfd_elf_post_process_headers (abfd, link_info);
15916 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
15917 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
15918 i_ehdrp->e_ident[EI_ABIVERSION] = 3;