1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 Free Software Foundation, Inc.
6 Most of the information added by Ian Lance Taylor, Cygnus Support,
8 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 <mark@codesourcery.com>
10 Traditional MIPS targets support added by Koundinya.K, Dansk Data
11 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
13 This file is part of BFD, the Binary File Descriptor library.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28 MA 02110-1301, USA. */
31 /* This file handles functionality common to the different MIPS ABI's. */
36 #include "libiberty.h"
38 #include "elfxx-mips.h"
40 #include "elf-vxworks.h"
42 /* Get the ECOFF swapping routines. */
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
50 /* Types of TLS GOT entry. */
51 enum mips_got_tls_type {
58 /* This structure is used to hold information about one GOT entry.
59 There are four types of entry:
61 (1) an absolute address
62 requires: abfd == NULL
65 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67 fields: abfd, symndx, d.addend, tls_type
69 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70 requires: abfd != NULL, symndx == -1
74 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75 fields: none; there's only one of these per GOT. */
78 /* One input bfd that needs the GOT entry. */
80 /* The index of the symbol, as stored in the relocation r_info, if
81 we have a local symbol; -1 otherwise. */
85 /* If abfd == NULL, an address that must be stored in the got. */
87 /* If abfd != NULL && symndx != -1, the addend of the relocation
88 that should be added to the symbol value. */
90 /* If abfd != NULL && symndx == -1, the hash table entry
91 corresponding to a symbol in the GOT. The symbol's entry
92 is in the local area if h->global_got_area is GGA_NONE,
93 otherwise it is in the global area. */
94 struct mips_elf_link_hash_entry *h;
97 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
98 symbol entry with r_symndx == 0. */
99 unsigned char tls_type;
101 /* True if we have filled in the GOT contents for a TLS entry,
102 and created the associated relocations. */
103 unsigned char tls_initialized;
105 /* The offset from the beginning of the .got section to the entry
106 corresponding to this symbol+addend. If it's a global symbol
107 whose offset is yet to be decided, it's going to be -1. */
111 /* This structure represents a GOT page reference from an input bfd.
112 Each instance represents a symbol + ADDEND, where the representation
113 of the symbol depends on whether it is local to the input bfd.
114 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
117 Page references with SYMNDX >= 0 always become page references
118 in the output. Page references with SYMNDX < 0 only become page
119 references if the symbol binds locally; in other cases, the page
120 reference decays to a global GOT reference. */
121 struct mips_got_page_ref
126 struct mips_elf_link_hash_entry *h;
132 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133 The structures form a non-overlapping list that is sorted by increasing
135 struct mips_got_page_range
137 struct mips_got_page_range *next;
138 bfd_signed_vma min_addend;
139 bfd_signed_vma max_addend;
142 /* This structure describes the range of addends that are applied to page
143 relocations against a given section. */
144 struct mips_got_page_entry
146 /* The section that these entries are based on. */
148 /* The ranges for this page entry. */
149 struct mips_got_page_range *ranges;
150 /* The maximum number of page entries needed for RANGES. */
154 /* This structure is used to hold .got information when linking. */
158 /* The number of global .got entries. */
159 unsigned int global_gotno;
160 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
161 unsigned int reloc_only_gotno;
162 /* The number of .got slots used for TLS. */
163 unsigned int tls_gotno;
164 /* The first unused TLS .got entry. Used only during
165 mips_elf_initialize_tls_index. */
166 unsigned int tls_assigned_gotno;
167 /* The number of local .got entries, eventually including page entries. */
168 unsigned int local_gotno;
169 /* The maximum number of page entries needed. */
170 unsigned int page_gotno;
171 /* The number of relocations needed for the GOT entries. */
173 /* The number of local .got entries we have used. */
174 unsigned int assigned_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 /* The MIPS ELF linker needs additional information for each symbol in
323 the global hash table. */
325 struct mips_elf_link_hash_entry
327 struct elf_link_hash_entry root;
329 /* External symbol information. */
332 /* The la25 stub we have created for ths symbol, if any. */
333 struct mips_elf_la25_stub *la25_stub;
335 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
337 unsigned int possibly_dynamic_relocs;
339 /* If there is a stub that 32 bit functions should use to call this
340 16 bit function, this points to the section containing the stub. */
343 /* If there is a stub that 16 bit functions should use to call this
344 32 bit function, this points to the section containing the stub. */
347 /* This is like the call_stub field, but it is used if the function
348 being called returns a floating point value. */
349 asection *call_fp_stub;
351 /* The highest GGA_* value that satisfies all references to this symbol. */
352 unsigned int global_got_area : 2;
354 /* True if all GOT relocations against this symbol are for calls. This is
355 a looser condition than no_fn_stub below, because there may be other
356 non-call non-GOT relocations against the symbol. */
357 unsigned int got_only_for_calls : 1;
359 /* True if one of the relocations described by possibly_dynamic_relocs
360 is against a readonly section. */
361 unsigned int readonly_reloc : 1;
363 /* True if there is a relocation against this symbol that must be
364 resolved by the static linker (in other words, if the relocation
365 cannot possibly be made dynamic). */
366 unsigned int has_static_relocs : 1;
368 /* True if we must not create a .MIPS.stubs entry for this symbol.
369 This is set, for example, if there are relocations related to
370 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
371 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
372 unsigned int no_fn_stub : 1;
374 /* Whether we need the fn_stub; this is true if this symbol appears
375 in any relocs other than a 16 bit call. */
376 unsigned int need_fn_stub : 1;
378 /* True if this symbol is referenced by branch relocations from
379 any non-PIC input file. This is used to determine whether an
380 la25 stub is required. */
381 unsigned int has_nonpic_branches : 1;
383 /* Does this symbol need a traditional MIPS lazy-binding stub
384 (as opposed to a PLT entry)? */
385 unsigned int needs_lazy_stub : 1;
388 /* MIPS ELF linker hash table. */
390 struct mips_elf_link_hash_table
392 struct elf_link_hash_table root;
394 /* The number of .rtproc entries. */
395 bfd_size_type procedure_count;
397 /* The size of the .compact_rel section (if SGI_COMPAT). */
398 bfd_size_type compact_rel_size;
400 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
401 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
402 bfd_boolean use_rld_obj_head;
404 /* The __rld_map or __rld_obj_head symbol. */
405 struct elf_link_hash_entry *rld_symbol;
407 /* This is set if we see any mips16 stub sections. */
408 bfd_boolean mips16_stubs_seen;
410 /* True if we can generate copy relocs and PLTs. */
411 bfd_boolean use_plts_and_copy_relocs;
413 /* True if we're generating code for VxWorks. */
414 bfd_boolean is_vxworks;
416 /* True if we already reported the small-data section overflow. */
417 bfd_boolean small_data_overflow_reported;
419 /* Shortcuts to some dynamic sections, or NULL if they are not
430 /* The master GOT information. */
431 struct mips_got_info *got_info;
433 /* The global symbol in the GOT with the lowest index in the dynamic
435 struct elf_link_hash_entry *global_gotsym;
437 /* The size of the PLT header in bytes. */
438 bfd_vma plt_header_size;
440 /* The size of a PLT entry in bytes. */
441 bfd_vma plt_entry_size;
443 /* The number of functions that need a lazy-binding stub. */
444 bfd_vma lazy_stub_count;
446 /* The size of a function stub entry in bytes. */
447 bfd_vma function_stub_size;
449 /* The number of reserved entries at the beginning of the GOT. */
450 unsigned int reserved_gotno;
452 /* The section used for mips_elf_la25_stub trampolines.
453 See the comment above that structure for details. */
454 asection *strampoline;
456 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
460 /* A function FN (NAME, IS, OS) that creates a new input section
461 called NAME and links it to output section OS. If IS is nonnull,
462 the new section should go immediately before it, otherwise it
463 should go at the (current) beginning of OS.
465 The function returns the new section on success, otherwise it
467 asection *(*add_stub_section) (const char *, asection *, asection *);
469 /* Small local sym cache. */
470 struct sym_cache sym_cache;
473 /* Get the MIPS ELF linker hash table from a link_info structure. */
475 #define mips_elf_hash_table(p) \
476 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
477 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
479 /* A structure used to communicate with htab_traverse callbacks. */
480 struct mips_htab_traverse_info
482 /* The usual link-wide information. */
483 struct bfd_link_info *info;
486 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
490 /* MIPS ELF private object data. */
492 struct mips_elf_obj_tdata
494 /* Generic ELF private object data. */
495 struct elf_obj_tdata root;
497 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
500 /* The GOT requirements of input bfds. */
501 struct mips_got_info *got;
504 /* Get MIPS ELF private object data from BFD's tdata. */
506 #define mips_elf_tdata(bfd) \
507 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
509 #define TLS_RELOC_P(r_type) \
510 (r_type == R_MIPS_TLS_DTPMOD32 \
511 || r_type == R_MIPS_TLS_DTPMOD64 \
512 || r_type == R_MIPS_TLS_DTPREL32 \
513 || r_type == R_MIPS_TLS_DTPREL64 \
514 || r_type == R_MIPS_TLS_GD \
515 || r_type == R_MIPS_TLS_LDM \
516 || r_type == R_MIPS_TLS_DTPREL_HI16 \
517 || r_type == R_MIPS_TLS_DTPREL_LO16 \
518 || r_type == R_MIPS_TLS_GOTTPREL \
519 || r_type == R_MIPS_TLS_TPREL32 \
520 || r_type == R_MIPS_TLS_TPREL64 \
521 || r_type == R_MIPS_TLS_TPREL_HI16 \
522 || r_type == R_MIPS_TLS_TPREL_LO16 \
523 || r_type == R_MIPS16_TLS_GD \
524 || r_type == R_MIPS16_TLS_LDM \
525 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
526 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
527 || r_type == R_MIPS16_TLS_GOTTPREL \
528 || r_type == R_MIPS16_TLS_TPREL_HI16 \
529 || r_type == R_MIPS16_TLS_TPREL_LO16 \
530 || r_type == R_MICROMIPS_TLS_GD \
531 || r_type == R_MICROMIPS_TLS_LDM \
532 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
533 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
534 || r_type == R_MICROMIPS_TLS_GOTTPREL \
535 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
536 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
538 /* Structure used to pass information to mips_elf_output_extsym. */
543 struct bfd_link_info *info;
544 struct ecoff_debug_info *debug;
545 const struct ecoff_debug_swap *swap;
549 /* The names of the runtime procedure table symbols used on IRIX5. */
551 static const char * const mips_elf_dynsym_rtproc_names[] =
554 "_procedure_string_table",
555 "_procedure_table_size",
559 /* These structures are used to generate the .compact_rel section on
564 unsigned long id1; /* Always one? */
565 unsigned long num; /* Number of compact relocation entries. */
566 unsigned long id2; /* Always two? */
567 unsigned long offset; /* The file offset of the first relocation. */
568 unsigned long reserved0; /* Zero? */
569 unsigned long reserved1; /* Zero? */
578 bfd_byte reserved0[4];
579 bfd_byte reserved1[4];
580 } Elf32_External_compact_rel;
584 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
585 unsigned int rtype : 4; /* Relocation types. See below. */
586 unsigned int dist2to : 8;
587 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
588 unsigned long konst; /* KONST field. See below. */
589 unsigned long vaddr; /* VADDR to be relocated. */
594 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
595 unsigned int rtype : 4; /* Relocation types. See below. */
596 unsigned int dist2to : 8;
597 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
598 unsigned long konst; /* KONST field. See below. */
606 } Elf32_External_crinfo;
612 } Elf32_External_crinfo2;
614 /* These are the constants used to swap the bitfields in a crinfo. */
616 #define CRINFO_CTYPE (0x1)
617 #define CRINFO_CTYPE_SH (31)
618 #define CRINFO_RTYPE (0xf)
619 #define CRINFO_RTYPE_SH (27)
620 #define CRINFO_DIST2TO (0xff)
621 #define CRINFO_DIST2TO_SH (19)
622 #define CRINFO_RELVADDR (0x7ffff)
623 #define CRINFO_RELVADDR_SH (0)
625 /* A compact relocation info has long (3 words) or short (2 words)
626 formats. A short format doesn't have VADDR field and relvaddr
627 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
628 #define CRF_MIPS_LONG 1
629 #define CRF_MIPS_SHORT 0
631 /* There are 4 types of compact relocation at least. The value KONST
632 has different meaning for each type:
635 CT_MIPS_REL32 Address in data
636 CT_MIPS_WORD Address in word (XXX)
637 CT_MIPS_GPHI_LO GP - vaddr
638 CT_MIPS_JMPAD Address to jump
641 #define CRT_MIPS_REL32 0xa
642 #define CRT_MIPS_WORD 0xb
643 #define CRT_MIPS_GPHI_LO 0xc
644 #define CRT_MIPS_JMPAD 0xd
646 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
647 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
648 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
649 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
651 /* The structure of the runtime procedure descriptor created by the
652 loader for use by the static exception system. */
654 typedef struct runtime_pdr {
655 bfd_vma adr; /* Memory address of start of procedure. */
656 long regmask; /* Save register mask. */
657 long regoffset; /* Save register offset. */
658 long fregmask; /* Save floating point register mask. */
659 long fregoffset; /* Save floating point register offset. */
660 long frameoffset; /* Frame size. */
661 short framereg; /* Frame pointer register. */
662 short pcreg; /* Offset or reg of return pc. */
663 long irpss; /* Index into the runtime string table. */
665 struct exception_info *exception_info;/* Pointer to exception array. */
667 #define cbRPDR sizeof (RPDR)
668 #define rpdNil ((pRPDR) 0)
670 static struct mips_got_entry *mips_elf_create_local_got_entry
671 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
672 struct mips_elf_link_hash_entry *, int);
673 static bfd_boolean mips_elf_sort_hash_table_f
674 (struct mips_elf_link_hash_entry *, void *);
675 static bfd_vma mips_elf_high
677 static bfd_boolean mips_elf_create_dynamic_relocation
678 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
679 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
680 bfd_vma *, asection *);
681 static bfd_vma mips_elf_adjust_gp
682 (bfd *, struct mips_got_info *, bfd *);
684 /* This will be used when we sort the dynamic relocation records. */
685 static bfd *reldyn_sorting_bfd;
687 /* True if ABFD is for CPUs with load interlocking that include
688 non-MIPS1 CPUs and R3900. */
689 #define LOAD_INTERLOCKS_P(abfd) \
690 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
691 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
693 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
694 This should be safe for all architectures. We enable this predicate
695 for RM9000 for now. */
696 #define JAL_TO_BAL_P(abfd) \
697 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
699 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
700 This should be safe for all architectures. We enable this predicate for
702 #define JALR_TO_BAL_P(abfd) 1
704 /* True if ABFD is for CPUs that are faster if JR is converted to B.
705 This should be safe for all architectures. We enable this predicate for
707 #define JR_TO_B_P(abfd) 1
709 /* True if ABFD is a PIC object. */
710 #define PIC_OBJECT_P(abfd) \
711 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
713 /* Nonzero if ABFD is using the N32 ABI. */
714 #define ABI_N32_P(abfd) \
715 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
717 /* Nonzero if ABFD is using the N64 ABI. */
718 #define ABI_64_P(abfd) \
719 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
721 /* Nonzero if ABFD is using NewABI conventions. */
722 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
724 /* The IRIX compatibility level we are striving for. */
725 #define IRIX_COMPAT(abfd) \
726 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
728 /* Whether we are trying to be compatible with IRIX at all. */
729 #define SGI_COMPAT(abfd) \
730 (IRIX_COMPAT (abfd) != ict_none)
732 /* The name of the options section. */
733 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
734 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
736 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
737 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
738 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
739 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
741 /* Whether the section is readonly. */
742 #define MIPS_ELF_READONLY_SECTION(sec) \
743 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
744 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
746 /* The name of the stub section. */
747 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
749 /* The size of an external REL relocation. */
750 #define MIPS_ELF_REL_SIZE(abfd) \
751 (get_elf_backend_data (abfd)->s->sizeof_rel)
753 /* The size of an external RELA relocation. */
754 #define MIPS_ELF_RELA_SIZE(abfd) \
755 (get_elf_backend_data (abfd)->s->sizeof_rela)
757 /* The size of an external dynamic table entry. */
758 #define MIPS_ELF_DYN_SIZE(abfd) \
759 (get_elf_backend_data (abfd)->s->sizeof_dyn)
761 /* The size of a GOT entry. */
762 #define MIPS_ELF_GOT_SIZE(abfd) \
763 (get_elf_backend_data (abfd)->s->arch_size / 8)
765 /* The size of the .rld_map section. */
766 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
767 (get_elf_backend_data (abfd)->s->arch_size / 8)
769 /* The size of a symbol-table entry. */
770 #define MIPS_ELF_SYM_SIZE(abfd) \
771 (get_elf_backend_data (abfd)->s->sizeof_sym)
773 /* The default alignment for sections, as a power of two. */
774 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
775 (get_elf_backend_data (abfd)->s->log_file_align)
777 /* Get word-sized data. */
778 #define MIPS_ELF_GET_WORD(abfd, ptr) \
779 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
781 /* Put out word-sized data. */
782 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
784 ? bfd_put_64 (abfd, val, ptr) \
785 : bfd_put_32 (abfd, val, ptr))
787 /* The opcode for word-sized loads (LW or LD). */
788 #define MIPS_ELF_LOAD_WORD(abfd) \
789 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
791 /* Add a dynamic symbol table-entry. */
792 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
793 _bfd_elf_add_dynamic_entry (info, tag, val)
795 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
796 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
798 /* The name of the dynamic relocation section. */
799 #define MIPS_ELF_REL_DYN_NAME(INFO) \
800 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
802 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
803 from smaller values. Start with zero, widen, *then* decrement. */
804 #define MINUS_ONE (((bfd_vma)0) - 1)
805 #define MINUS_TWO (((bfd_vma)0) - 2)
807 /* The value to write into got[1] for SVR4 targets, to identify it is
808 a GNU object. The dynamic linker can then use got[1] to store the
810 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
811 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
813 /* The offset of $gp from the beginning of the .got section. */
814 #define ELF_MIPS_GP_OFFSET(INFO) \
815 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
817 /* The maximum size of the GOT for it to be addressable using 16-bit
819 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
821 /* Instructions which appear in a stub. */
822 #define STUB_LW(abfd) \
824 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
825 : 0x8f998010)) /* lw t9,0x8010(gp) */
826 #define STUB_MOVE(abfd) \
828 ? 0x03e0782d /* daddu t7,ra */ \
829 : 0x03e07821)) /* addu t7,ra */
830 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
831 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
832 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
833 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
834 #define STUB_LI16S(abfd, VAL) \
836 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
837 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
839 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
840 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
842 /* The name of the dynamic interpreter. This is put in the .interp
845 #define ELF_DYNAMIC_INTERPRETER(abfd) \
846 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
847 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
848 : "/usr/lib/libc.so.1")
851 #define MNAME(bfd,pre,pos) \
852 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
853 #define ELF_R_SYM(bfd, i) \
854 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
855 #define ELF_R_TYPE(bfd, i) \
856 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
857 #define ELF_R_INFO(bfd, s, t) \
858 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
860 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
861 #define ELF_R_SYM(bfd, i) \
863 #define ELF_R_TYPE(bfd, i) \
865 #define ELF_R_INFO(bfd, s, t) \
866 (ELF32_R_INFO (s, t))
869 /* The mips16 compiler uses a couple of special sections to handle
870 floating point arguments.
872 Section names that look like .mips16.fn.FNNAME contain stubs that
873 copy floating point arguments from the fp regs to the gp regs and
874 then jump to FNNAME. If any 32 bit function calls FNNAME, the
875 call should be redirected to the stub instead. If no 32 bit
876 function calls FNNAME, the stub should be discarded. We need to
877 consider any reference to the function, not just a call, because
878 if the address of the function is taken we will need the stub,
879 since the address might be passed to a 32 bit function.
881 Section names that look like .mips16.call.FNNAME contain stubs
882 that copy floating point arguments from the gp regs to the fp
883 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
884 then any 16 bit function that calls FNNAME should be redirected
885 to the stub instead. If FNNAME is not a 32 bit function, the
886 stub should be discarded.
888 .mips16.call.fp.FNNAME sections are similar, but contain stubs
889 which call FNNAME and then copy the return value from the fp regs
890 to the gp regs. These stubs store the return value in $18 while
891 calling FNNAME; any function which might call one of these stubs
892 must arrange to save $18 around the call. (This case is not
893 needed for 32 bit functions that call 16 bit functions, because
894 16 bit functions always return floating point values in both
897 Note that in all cases FNNAME might be defined statically.
898 Therefore, FNNAME is not used literally. Instead, the relocation
899 information will indicate which symbol the section is for.
901 We record any stubs that we find in the symbol table. */
903 #define FN_STUB ".mips16.fn."
904 #define CALL_STUB ".mips16.call."
905 #define CALL_FP_STUB ".mips16.call.fp."
907 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
908 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
909 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
911 /* The format of the first PLT entry in an O32 executable. */
912 static const bfd_vma mips_o32_exec_plt0_entry[] =
914 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
915 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
916 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
917 0x031cc023, /* subu $24, $24, $28 */
918 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
919 0x0018c082, /* srl $24, $24, 2 */
920 0x0320f809, /* jalr $25 */
921 0x2718fffe /* subu $24, $24, 2 */
924 /* The format of the first PLT entry in an N32 executable. Different
925 because gp ($28) is not available; we use t2 ($14) instead. */
926 static const bfd_vma mips_n32_exec_plt0_entry[] =
928 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
929 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
930 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
931 0x030ec023, /* subu $24, $24, $14 */
932 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
933 0x0018c082, /* srl $24, $24, 2 */
934 0x0320f809, /* jalr $25 */
935 0x2718fffe /* subu $24, $24, 2 */
938 /* The format of the first PLT entry in an N64 executable. Different
939 from N32 because of the increased size of GOT entries. */
940 static const bfd_vma mips_n64_exec_plt0_entry[] =
942 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
943 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
944 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
945 0x030ec023, /* subu $24, $24, $14 */
946 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
947 0x0018c0c2, /* srl $24, $24, 3 */
948 0x0320f809, /* jalr $25 */
949 0x2718fffe /* subu $24, $24, 2 */
952 /* The format of subsequent PLT entries. */
953 static const bfd_vma mips_exec_plt_entry[] =
955 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
956 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
957 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
958 0x03200008 /* jr $25 */
961 /* The format of the first PLT entry in a VxWorks executable. */
962 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
964 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
965 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
966 0x8f390008, /* lw t9, 8(t9) */
967 0x00000000, /* nop */
968 0x03200008, /* jr t9 */
972 /* The format of subsequent PLT entries. */
973 static const bfd_vma mips_vxworks_exec_plt_entry[] =
975 0x10000000, /* b .PLT_resolver */
976 0x24180000, /* li t8, <pltindex> */
977 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
978 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
979 0x8f390000, /* lw t9, 0(t9) */
980 0x00000000, /* nop */
981 0x03200008, /* jr t9 */
985 /* The format of the first PLT entry in a VxWorks shared object. */
986 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
988 0x8f990008, /* lw t9, 8(gp) */
989 0x00000000, /* nop */
990 0x03200008, /* jr t9 */
991 0x00000000, /* nop */
992 0x00000000, /* nop */
996 /* The format of subsequent PLT entries. */
997 static const bfd_vma mips_vxworks_shared_plt_entry[] =
999 0x10000000, /* b .PLT_resolver */
1000 0x24180000 /* li t8, <pltindex> */
1003 /* microMIPS 32-bit opcode helper installer. */
1006 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1008 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1009 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1012 /* microMIPS 32-bit opcode helper retriever. */
1015 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1017 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1020 /* Look up an entry in a MIPS ELF linker hash table. */
1022 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1023 ((struct mips_elf_link_hash_entry *) \
1024 elf_link_hash_lookup (&(table)->root, (string), (create), \
1027 /* Traverse a MIPS ELF linker hash table. */
1029 #define mips_elf_link_hash_traverse(table, func, info) \
1030 (elf_link_hash_traverse \
1032 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1035 /* Find the base offsets for thread-local storage in this object,
1036 for GD/LD and IE/LE respectively. */
1038 #define TP_OFFSET 0x7000
1039 #define DTP_OFFSET 0x8000
1042 dtprel_base (struct bfd_link_info *info)
1044 /* If tls_sec is NULL, we should have signalled an error already. */
1045 if (elf_hash_table (info)->tls_sec == NULL)
1047 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1051 tprel_base (struct bfd_link_info *info)
1053 /* If tls_sec is NULL, we should have signalled an error already. */
1054 if (elf_hash_table (info)->tls_sec == NULL)
1056 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1059 /* Create an entry in a MIPS ELF linker hash table. */
1061 static struct bfd_hash_entry *
1062 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1063 struct bfd_hash_table *table, const char *string)
1065 struct mips_elf_link_hash_entry *ret =
1066 (struct mips_elf_link_hash_entry *) entry;
1068 /* Allocate the structure if it has not already been allocated by a
1071 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1073 return (struct bfd_hash_entry *) ret;
1075 /* Call the allocation method of the superclass. */
1076 ret = ((struct mips_elf_link_hash_entry *)
1077 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1081 /* Set local fields. */
1082 memset (&ret->esym, 0, sizeof (EXTR));
1083 /* We use -2 as a marker to indicate that the information has
1084 not been set. -1 means there is no associated ifd. */
1087 ret->possibly_dynamic_relocs = 0;
1088 ret->fn_stub = NULL;
1089 ret->call_stub = NULL;
1090 ret->call_fp_stub = NULL;
1091 ret->global_got_area = GGA_NONE;
1092 ret->got_only_for_calls = TRUE;
1093 ret->readonly_reloc = FALSE;
1094 ret->has_static_relocs = FALSE;
1095 ret->no_fn_stub = FALSE;
1096 ret->need_fn_stub = FALSE;
1097 ret->has_nonpic_branches = FALSE;
1098 ret->needs_lazy_stub = FALSE;
1101 return (struct bfd_hash_entry *) ret;
1104 /* Allocate MIPS ELF private object data. */
1107 _bfd_mips_elf_mkobject (bfd *abfd)
1109 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1114 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1116 if (!sec->used_by_bfd)
1118 struct _mips_elf_section_data *sdata;
1119 bfd_size_type amt = sizeof (*sdata);
1121 sdata = bfd_zalloc (abfd, amt);
1124 sec->used_by_bfd = sdata;
1127 return _bfd_elf_new_section_hook (abfd, sec);
1130 /* Read ECOFF debugging information from a .mdebug section into a
1131 ecoff_debug_info structure. */
1134 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1135 struct ecoff_debug_info *debug)
1138 const struct ecoff_debug_swap *swap;
1141 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1142 memset (debug, 0, sizeof (*debug));
1144 ext_hdr = bfd_malloc (swap->external_hdr_size);
1145 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1148 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1149 swap->external_hdr_size))
1152 symhdr = &debug->symbolic_header;
1153 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1155 /* The symbolic header contains absolute file offsets and sizes to
1157 #define READ(ptr, offset, count, size, type) \
1158 if (symhdr->count == 0) \
1159 debug->ptr = NULL; \
1162 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1163 debug->ptr = bfd_malloc (amt); \
1164 if (debug->ptr == NULL) \
1165 goto error_return; \
1166 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1167 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1168 goto error_return; \
1171 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1172 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1173 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1174 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1175 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1176 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1178 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1179 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1180 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1181 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1182 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1190 if (ext_hdr != NULL)
1192 if (debug->line != NULL)
1194 if (debug->external_dnr != NULL)
1195 free (debug->external_dnr);
1196 if (debug->external_pdr != NULL)
1197 free (debug->external_pdr);
1198 if (debug->external_sym != NULL)
1199 free (debug->external_sym);
1200 if (debug->external_opt != NULL)
1201 free (debug->external_opt);
1202 if (debug->external_aux != NULL)
1203 free (debug->external_aux);
1204 if (debug->ss != NULL)
1206 if (debug->ssext != NULL)
1207 free (debug->ssext);
1208 if (debug->external_fdr != NULL)
1209 free (debug->external_fdr);
1210 if (debug->external_rfd != NULL)
1211 free (debug->external_rfd);
1212 if (debug->external_ext != NULL)
1213 free (debug->external_ext);
1217 /* Swap RPDR (runtime procedure table entry) for output. */
1220 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1222 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1223 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1224 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1225 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1226 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1227 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1229 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1230 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1232 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1235 /* Create a runtime procedure table from the .mdebug section. */
1238 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1239 struct bfd_link_info *info, asection *s,
1240 struct ecoff_debug_info *debug)
1242 const struct ecoff_debug_swap *swap;
1243 HDRR *hdr = &debug->symbolic_header;
1245 struct rpdr_ext *erp;
1247 struct pdr_ext *epdr;
1248 struct sym_ext *esym;
1252 bfd_size_type count;
1253 unsigned long sindex;
1257 const char *no_name_func = _("static procedure (no name)");
1265 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1267 sindex = strlen (no_name_func) + 1;
1268 count = hdr->ipdMax;
1271 size = swap->external_pdr_size;
1273 epdr = bfd_malloc (size * count);
1277 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1280 size = sizeof (RPDR);
1281 rp = rpdr = bfd_malloc (size * count);
1285 size = sizeof (char *);
1286 sv = bfd_malloc (size * count);
1290 count = hdr->isymMax;
1291 size = swap->external_sym_size;
1292 esym = bfd_malloc (size * count);
1296 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1299 count = hdr->issMax;
1300 ss = bfd_malloc (count);
1303 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1306 count = hdr->ipdMax;
1307 for (i = 0; i < (unsigned long) count; i++, rp++)
1309 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1310 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1311 rp->adr = sym.value;
1312 rp->regmask = pdr.regmask;
1313 rp->regoffset = pdr.regoffset;
1314 rp->fregmask = pdr.fregmask;
1315 rp->fregoffset = pdr.fregoffset;
1316 rp->frameoffset = pdr.frameoffset;
1317 rp->framereg = pdr.framereg;
1318 rp->pcreg = pdr.pcreg;
1320 sv[i] = ss + sym.iss;
1321 sindex += strlen (sv[i]) + 1;
1325 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1326 size = BFD_ALIGN (size, 16);
1327 rtproc = bfd_alloc (abfd, size);
1330 mips_elf_hash_table (info)->procedure_count = 0;
1334 mips_elf_hash_table (info)->procedure_count = count + 2;
1337 memset (erp, 0, sizeof (struct rpdr_ext));
1339 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1340 strcpy (str, no_name_func);
1341 str += strlen (no_name_func) + 1;
1342 for (i = 0; i < count; i++)
1344 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1345 strcpy (str, sv[i]);
1346 str += strlen (sv[i]) + 1;
1348 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1350 /* Set the size and contents of .rtproc section. */
1352 s->contents = rtproc;
1354 /* Skip this section later on (I don't think this currently
1355 matters, but someday it might). */
1356 s->map_head.link_order = NULL;
1385 /* We're going to create a stub for H. Create a symbol for the stub's
1386 value and size, to help make the disassembly easier to read. */
1389 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1390 struct mips_elf_link_hash_entry *h,
1391 const char *prefix, asection *s, bfd_vma value,
1394 struct bfd_link_hash_entry *bh;
1395 struct elf_link_hash_entry *elfh;
1398 if (ELF_ST_IS_MICROMIPS (h->root.other))
1401 /* Create a new symbol. */
1402 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1404 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1405 BSF_LOCAL, s, value, NULL,
1409 /* Make it a local function. */
1410 elfh = (struct elf_link_hash_entry *) bh;
1411 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1413 elfh->forced_local = 1;
1417 /* We're about to redefine H. Create a symbol to represent H's
1418 current value and size, to help make the disassembly easier
1422 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1423 struct mips_elf_link_hash_entry *h,
1426 struct bfd_link_hash_entry *bh;
1427 struct elf_link_hash_entry *elfh;
1432 /* Read the symbol's value. */
1433 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1434 || h->root.root.type == bfd_link_hash_defweak);
1435 s = h->root.root.u.def.section;
1436 value = h->root.root.u.def.value;
1438 /* Create a new symbol. */
1439 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1441 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1442 BSF_LOCAL, s, value, NULL,
1446 /* Make it local and copy the other attributes from H. */
1447 elfh = (struct elf_link_hash_entry *) bh;
1448 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1449 elfh->other = h->root.other;
1450 elfh->size = h->root.size;
1451 elfh->forced_local = 1;
1455 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1456 function rather than to a hard-float stub. */
1459 section_allows_mips16_refs_p (asection *section)
1463 name = bfd_get_section_name (section->owner, section);
1464 return (FN_STUB_P (name)
1465 || CALL_STUB_P (name)
1466 || CALL_FP_STUB_P (name)
1467 || strcmp (name, ".pdr") == 0);
1470 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1471 stub section of some kind. Return the R_SYMNDX of the target
1472 function, or 0 if we can't decide which function that is. */
1474 static unsigned long
1475 mips16_stub_symndx (const struct elf_backend_data *bed,
1476 asection *sec ATTRIBUTE_UNUSED,
1477 const Elf_Internal_Rela *relocs,
1478 const Elf_Internal_Rela *relend)
1480 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1481 const Elf_Internal_Rela *rel;
1483 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1484 one in a compound relocation. */
1485 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1486 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1487 return ELF_R_SYM (sec->owner, rel->r_info);
1489 /* Otherwise trust the first relocation, whatever its kind. This is
1490 the traditional behavior. */
1491 if (relocs < relend)
1492 return ELF_R_SYM (sec->owner, relocs->r_info);
1497 /* Check the mips16 stubs for a particular symbol, and see if we can
1501 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1502 struct mips_elf_link_hash_entry *h)
1504 /* Dynamic symbols must use the standard call interface, in case other
1505 objects try to call them. */
1506 if (h->fn_stub != NULL
1507 && h->root.dynindx != -1)
1509 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1510 h->need_fn_stub = TRUE;
1513 if (h->fn_stub != NULL
1514 && ! h->need_fn_stub)
1516 /* We don't need the fn_stub; the only references to this symbol
1517 are 16 bit calls. Clobber the size to 0 to prevent it from
1518 being included in the link. */
1519 h->fn_stub->size = 0;
1520 h->fn_stub->flags &= ~SEC_RELOC;
1521 h->fn_stub->reloc_count = 0;
1522 h->fn_stub->flags |= SEC_EXCLUDE;
1525 if (h->call_stub != NULL
1526 && ELF_ST_IS_MIPS16 (h->root.other))
1528 /* We don't need the call_stub; this is a 16 bit function, so
1529 calls from other 16 bit functions are OK. Clobber the size
1530 to 0 to prevent it from being included in the link. */
1531 h->call_stub->size = 0;
1532 h->call_stub->flags &= ~SEC_RELOC;
1533 h->call_stub->reloc_count = 0;
1534 h->call_stub->flags |= SEC_EXCLUDE;
1537 if (h->call_fp_stub != NULL
1538 && ELF_ST_IS_MIPS16 (h->root.other))
1540 /* We don't need the call_stub; this is a 16 bit function, so
1541 calls from other 16 bit functions are OK. Clobber the size
1542 to 0 to prevent it from being included in the link. */
1543 h->call_fp_stub->size = 0;
1544 h->call_fp_stub->flags &= ~SEC_RELOC;
1545 h->call_fp_stub->reloc_count = 0;
1546 h->call_fp_stub->flags |= SEC_EXCLUDE;
1550 /* Hashtable callbacks for mips_elf_la25_stubs. */
1553 mips_elf_la25_stub_hash (const void *entry_)
1555 const struct mips_elf_la25_stub *entry;
1557 entry = (struct mips_elf_la25_stub *) entry_;
1558 return entry->h->root.root.u.def.section->id
1559 + entry->h->root.root.u.def.value;
1563 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1565 const struct mips_elf_la25_stub *entry1, *entry2;
1567 entry1 = (struct mips_elf_la25_stub *) entry1_;
1568 entry2 = (struct mips_elf_la25_stub *) entry2_;
1569 return ((entry1->h->root.root.u.def.section
1570 == entry2->h->root.root.u.def.section)
1571 && (entry1->h->root.root.u.def.value
1572 == entry2->h->root.root.u.def.value));
1575 /* Called by the linker to set up the la25 stub-creation code. FN is
1576 the linker's implementation of add_stub_function. Return true on
1580 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1581 asection *(*fn) (const char *, asection *,
1584 struct mips_elf_link_hash_table *htab;
1586 htab = mips_elf_hash_table (info);
1590 htab->add_stub_section = fn;
1591 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1592 mips_elf_la25_stub_eq, NULL);
1593 if (htab->la25_stubs == NULL)
1599 /* Return true if H is a locally-defined PIC function, in the sense
1600 that it or its fn_stub might need $25 to be valid on entry.
1601 Note that MIPS16 functions set up $gp using PC-relative instructions,
1602 so they themselves never need $25 to be valid. Only non-MIPS16
1603 entry points are of interest here. */
1606 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1608 return ((h->root.root.type == bfd_link_hash_defined
1609 || h->root.root.type == bfd_link_hash_defweak)
1610 && h->root.def_regular
1611 && !bfd_is_abs_section (h->root.root.u.def.section)
1612 && (!ELF_ST_IS_MIPS16 (h->root.other)
1613 || (h->fn_stub && h->need_fn_stub))
1614 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1615 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1618 /* Set *SEC to the input section that contains the target of STUB.
1619 Return the offset of the target from the start of that section. */
1622 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1625 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1627 BFD_ASSERT (stub->h->need_fn_stub);
1628 *sec = stub->h->fn_stub;
1633 *sec = stub->h->root.root.u.def.section;
1634 return stub->h->root.root.u.def.value;
1638 /* STUB describes an la25 stub that we have decided to implement
1639 by inserting an LUI/ADDIU pair before the target function.
1640 Create the section and redirect the function symbol to it. */
1643 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1644 struct bfd_link_info *info)
1646 struct mips_elf_link_hash_table *htab;
1648 asection *s, *input_section;
1651 htab = mips_elf_hash_table (info);
1655 /* Create a unique name for the new section. */
1656 name = bfd_malloc (11 + sizeof (".text.stub."));
1659 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1661 /* Create the section. */
1662 mips_elf_get_la25_target (stub, &input_section);
1663 s = htab->add_stub_section (name, input_section,
1664 input_section->output_section);
1668 /* Make sure that any padding goes before the stub. */
1669 align = input_section->alignment_power;
1670 if (!bfd_set_section_alignment (s->owner, s, align))
1673 s->size = (1 << align) - 8;
1675 /* Create a symbol for the stub. */
1676 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1677 stub->stub_section = s;
1678 stub->offset = s->size;
1680 /* Allocate room for it. */
1685 /* STUB describes an la25 stub that we have decided to implement
1686 with a separate trampoline. Allocate room for it and redirect
1687 the function symbol to it. */
1690 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1691 struct bfd_link_info *info)
1693 struct mips_elf_link_hash_table *htab;
1696 htab = mips_elf_hash_table (info);
1700 /* Create a trampoline section, if we haven't already. */
1701 s = htab->strampoline;
1704 asection *input_section = stub->h->root.root.u.def.section;
1705 s = htab->add_stub_section (".text", NULL,
1706 input_section->output_section);
1707 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1709 htab->strampoline = s;
1712 /* Create a symbol for the stub. */
1713 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1714 stub->stub_section = s;
1715 stub->offset = s->size;
1717 /* Allocate room for it. */
1722 /* H describes a symbol that needs an la25 stub. Make sure that an
1723 appropriate stub exists and point H at it. */
1726 mips_elf_add_la25_stub (struct bfd_link_info *info,
1727 struct mips_elf_link_hash_entry *h)
1729 struct mips_elf_link_hash_table *htab;
1730 struct mips_elf_la25_stub search, *stub;
1731 bfd_boolean use_trampoline_p;
1736 /* Describe the stub we want. */
1737 search.stub_section = NULL;
1741 /* See if we've already created an equivalent stub. */
1742 htab = mips_elf_hash_table (info);
1746 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1750 stub = (struct mips_elf_la25_stub *) *slot;
1753 /* We can reuse the existing stub. */
1754 h->la25_stub = stub;
1758 /* Create a permanent copy of ENTRY and add it to the hash table. */
1759 stub = bfd_malloc (sizeof (search));
1765 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1766 of the section and if we would need no more than 2 nops. */
1767 value = mips_elf_get_la25_target (stub, &s);
1768 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1770 h->la25_stub = stub;
1771 return (use_trampoline_p
1772 ? mips_elf_add_la25_trampoline (stub, info)
1773 : mips_elf_add_la25_intro (stub, info));
1776 /* A mips_elf_link_hash_traverse callback that is called before sizing
1777 sections. DATA points to a mips_htab_traverse_info structure. */
1780 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1782 struct mips_htab_traverse_info *hti;
1784 hti = (struct mips_htab_traverse_info *) data;
1785 if (!hti->info->relocatable)
1786 mips_elf_check_mips16_stubs (hti->info, h);
1788 if (mips_elf_local_pic_function_p (h))
1790 /* PR 12845: If H is in a section that has been garbage
1791 collected it will have its output section set to *ABS*. */
1792 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1795 /* H is a function that might need $25 to be valid on entry.
1796 If we're creating a non-PIC relocatable object, mark H as
1797 being PIC. If we're creating a non-relocatable object with
1798 non-PIC branches and jumps to H, make sure that H has an la25
1800 if (hti->info->relocatable)
1802 if (!PIC_OBJECT_P (hti->output_bfd))
1803 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1805 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1814 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1815 Most mips16 instructions are 16 bits, but these instructions
1818 The format of these instructions is:
1820 +--------------+--------------------------------+
1821 | JALX | X| Imm 20:16 | Imm 25:21 |
1822 +--------------+--------------------------------+
1824 +-----------------------------------------------+
1826 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1827 Note that the immediate value in the first word is swapped.
1829 When producing a relocatable object file, R_MIPS16_26 is
1830 handled mostly like R_MIPS_26. In particular, the addend is
1831 stored as a straight 26-bit value in a 32-bit instruction.
1832 (gas makes life simpler for itself by never adjusting a
1833 R_MIPS16_26 reloc to be against a section, so the addend is
1834 always zero). However, the 32 bit instruction is stored as 2
1835 16-bit values, rather than a single 32-bit value. In a
1836 big-endian file, the result is the same; in a little-endian
1837 file, the two 16-bit halves of the 32 bit value are swapped.
1838 This is so that a disassembler can recognize the jal
1841 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1842 instruction stored as two 16-bit values. The addend A is the
1843 contents of the targ26 field. The calculation is the same as
1844 R_MIPS_26. When storing the calculated value, reorder the
1845 immediate value as shown above, and don't forget to store the
1846 value as two 16-bit values.
1848 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1852 +--------+----------------------+
1856 +--------+----------------------+
1859 +----------+------+-------------+
1863 +----------+--------------------+
1864 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1865 ((sub1 << 16) | sub2)).
1867 When producing a relocatable object file, the calculation is
1868 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1869 When producing a fully linked file, the calculation is
1870 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1871 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1873 The table below lists the other MIPS16 instruction relocations.
1874 Each one is calculated in the same way as the non-MIPS16 relocation
1875 given on the right, but using the extended MIPS16 layout of 16-bit
1878 R_MIPS16_GPREL R_MIPS_GPREL16
1879 R_MIPS16_GOT16 R_MIPS_GOT16
1880 R_MIPS16_CALL16 R_MIPS_CALL16
1881 R_MIPS16_HI16 R_MIPS_HI16
1882 R_MIPS16_LO16 R_MIPS_LO16
1884 A typical instruction will have a format like this:
1886 +--------------+--------------------------------+
1887 | EXTEND | Imm 10:5 | Imm 15:11 |
1888 +--------------+--------------------------------+
1889 | Major | rx | ry | Imm 4:0 |
1890 +--------------+--------------------------------+
1892 EXTEND is the five bit value 11110. Major is the instruction
1895 All we need to do here is shuffle the bits appropriately.
1896 As above, the two 16-bit halves must be swapped on a
1897 little-endian system. */
1899 static inline bfd_boolean
1900 mips16_reloc_p (int r_type)
1905 case R_MIPS16_GPREL:
1906 case R_MIPS16_GOT16:
1907 case R_MIPS16_CALL16:
1910 case R_MIPS16_TLS_GD:
1911 case R_MIPS16_TLS_LDM:
1912 case R_MIPS16_TLS_DTPREL_HI16:
1913 case R_MIPS16_TLS_DTPREL_LO16:
1914 case R_MIPS16_TLS_GOTTPREL:
1915 case R_MIPS16_TLS_TPREL_HI16:
1916 case R_MIPS16_TLS_TPREL_LO16:
1924 /* Check if a microMIPS reloc. */
1926 static inline bfd_boolean
1927 micromips_reloc_p (unsigned int r_type)
1929 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
1932 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
1933 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
1934 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
1936 static inline bfd_boolean
1937 micromips_reloc_shuffle_p (unsigned int r_type)
1939 return (micromips_reloc_p (r_type)
1940 && r_type != R_MICROMIPS_PC7_S1
1941 && r_type != R_MICROMIPS_PC10_S1);
1944 static inline bfd_boolean
1945 got16_reloc_p (int r_type)
1947 return (r_type == R_MIPS_GOT16
1948 || r_type == R_MIPS16_GOT16
1949 || r_type == R_MICROMIPS_GOT16);
1952 static inline bfd_boolean
1953 call16_reloc_p (int r_type)
1955 return (r_type == R_MIPS_CALL16
1956 || r_type == R_MIPS16_CALL16
1957 || r_type == R_MICROMIPS_CALL16);
1960 static inline bfd_boolean
1961 got_disp_reloc_p (unsigned int r_type)
1963 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
1966 static inline bfd_boolean
1967 got_page_reloc_p (unsigned int r_type)
1969 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
1972 static inline bfd_boolean
1973 got_ofst_reloc_p (unsigned int r_type)
1975 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
1978 static inline bfd_boolean
1979 got_hi16_reloc_p (unsigned int r_type)
1981 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
1984 static inline bfd_boolean
1985 got_lo16_reloc_p (unsigned int r_type)
1987 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
1990 static inline bfd_boolean
1991 call_hi16_reloc_p (unsigned int r_type)
1993 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
1996 static inline bfd_boolean
1997 call_lo16_reloc_p (unsigned int r_type)
1999 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2002 static inline bfd_boolean
2003 hi16_reloc_p (int r_type)
2005 return (r_type == R_MIPS_HI16
2006 || r_type == R_MIPS16_HI16
2007 || r_type == R_MICROMIPS_HI16);
2010 static inline bfd_boolean
2011 lo16_reloc_p (int r_type)
2013 return (r_type == R_MIPS_LO16
2014 || r_type == R_MIPS16_LO16
2015 || r_type == R_MICROMIPS_LO16);
2018 static inline bfd_boolean
2019 mips16_call_reloc_p (int r_type)
2021 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2024 static inline bfd_boolean
2025 jal_reloc_p (int r_type)
2027 return (r_type == R_MIPS_26
2028 || r_type == R_MIPS16_26
2029 || r_type == R_MICROMIPS_26_S1);
2032 static inline bfd_boolean
2033 micromips_branch_reloc_p (int r_type)
2035 return (r_type == R_MICROMIPS_26_S1
2036 || r_type == R_MICROMIPS_PC16_S1
2037 || r_type == R_MICROMIPS_PC10_S1
2038 || r_type == R_MICROMIPS_PC7_S1);
2041 static inline bfd_boolean
2042 tls_gd_reloc_p (unsigned int r_type)
2044 return (r_type == R_MIPS_TLS_GD
2045 || r_type == R_MIPS16_TLS_GD
2046 || r_type == R_MICROMIPS_TLS_GD);
2049 static inline bfd_boolean
2050 tls_ldm_reloc_p (unsigned int r_type)
2052 return (r_type == R_MIPS_TLS_LDM
2053 || r_type == R_MIPS16_TLS_LDM
2054 || r_type == R_MICROMIPS_TLS_LDM);
2057 static inline bfd_boolean
2058 tls_gottprel_reloc_p (unsigned int r_type)
2060 return (r_type == R_MIPS_TLS_GOTTPREL
2061 || r_type == R_MIPS16_TLS_GOTTPREL
2062 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2066 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2067 bfd_boolean jal_shuffle, bfd_byte *data)
2069 bfd_vma first, second, val;
2071 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2074 /* Pick up the first and second halfwords of the instruction. */
2075 first = bfd_get_16 (abfd, data);
2076 second = bfd_get_16 (abfd, data + 2);
2077 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2078 val = first << 16 | second;
2079 else if (r_type != R_MIPS16_26)
2080 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2081 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2083 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2084 | ((first & 0x1f) << 21) | second);
2085 bfd_put_32 (abfd, val, data);
2089 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2090 bfd_boolean jal_shuffle, bfd_byte *data)
2092 bfd_vma first, second, val;
2094 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2097 val = bfd_get_32 (abfd, data);
2098 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2100 second = val & 0xffff;
2103 else if (r_type != R_MIPS16_26)
2105 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2106 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2110 second = val & 0xffff;
2111 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2112 | ((val >> 21) & 0x1f);
2114 bfd_put_16 (abfd, second, data + 2);
2115 bfd_put_16 (abfd, first, data);
2118 bfd_reloc_status_type
2119 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2120 arelent *reloc_entry, asection *input_section,
2121 bfd_boolean relocatable, void *data, bfd_vma gp)
2125 bfd_reloc_status_type status;
2127 if (bfd_is_com_section (symbol->section))
2130 relocation = symbol->value;
2132 relocation += symbol->section->output_section->vma;
2133 relocation += symbol->section->output_offset;
2135 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2136 return bfd_reloc_outofrange;
2138 /* Set val to the offset into the section or symbol. */
2139 val = reloc_entry->addend;
2141 _bfd_mips_elf_sign_extend (val, 16);
2143 /* Adjust val for the final section location and GP value. If we
2144 are producing relocatable output, we don't want to do this for
2145 an external symbol. */
2147 || (symbol->flags & BSF_SECTION_SYM) != 0)
2148 val += relocation - gp;
2150 if (reloc_entry->howto->partial_inplace)
2152 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2154 + reloc_entry->address);
2155 if (status != bfd_reloc_ok)
2159 reloc_entry->addend = val;
2162 reloc_entry->address += input_section->output_offset;
2164 return bfd_reloc_ok;
2167 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2168 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2169 that contains the relocation field and DATA points to the start of
2174 struct mips_hi16 *next;
2176 asection *input_section;
2180 /* FIXME: This should not be a static variable. */
2182 static struct mips_hi16 *mips_hi16_list;
2184 /* A howto special_function for REL *HI16 relocations. We can only
2185 calculate the correct value once we've seen the partnering
2186 *LO16 relocation, so just save the information for later.
2188 The ABI requires that the *LO16 immediately follow the *HI16.
2189 However, as a GNU extension, we permit an arbitrary number of
2190 *HI16s to be associated with a single *LO16. This significantly
2191 simplies the relocation handling in gcc. */
2193 bfd_reloc_status_type
2194 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2195 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2196 asection *input_section, bfd *output_bfd,
2197 char **error_message ATTRIBUTE_UNUSED)
2199 struct mips_hi16 *n;
2201 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2202 return bfd_reloc_outofrange;
2204 n = bfd_malloc (sizeof *n);
2206 return bfd_reloc_outofrange;
2208 n->next = mips_hi16_list;
2210 n->input_section = input_section;
2211 n->rel = *reloc_entry;
2214 if (output_bfd != NULL)
2215 reloc_entry->address += input_section->output_offset;
2217 return bfd_reloc_ok;
2220 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2221 like any other 16-bit relocation when applied to global symbols, but is
2222 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2224 bfd_reloc_status_type
2225 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2226 void *data, asection *input_section,
2227 bfd *output_bfd, char **error_message)
2229 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2230 || bfd_is_und_section (bfd_get_section (symbol))
2231 || bfd_is_com_section (bfd_get_section (symbol)))
2232 /* The relocation is against a global symbol. */
2233 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2234 input_section, output_bfd,
2237 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2238 input_section, output_bfd, error_message);
2241 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2242 is a straightforward 16 bit inplace relocation, but we must deal with
2243 any partnering high-part relocations as well. */
2245 bfd_reloc_status_type
2246 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2247 void *data, asection *input_section,
2248 bfd *output_bfd, char **error_message)
2251 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2253 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2254 return bfd_reloc_outofrange;
2256 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2258 vallo = bfd_get_32 (abfd, location);
2259 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2262 while (mips_hi16_list != NULL)
2264 bfd_reloc_status_type ret;
2265 struct mips_hi16 *hi;
2267 hi = mips_hi16_list;
2269 /* R_MIPS*_GOT16 relocations are something of a special case. We
2270 want to install the addend in the same way as for a R_MIPS*_HI16
2271 relocation (with a rightshift of 16). However, since GOT16
2272 relocations can also be used with global symbols, their howto
2273 has a rightshift of 0. */
2274 if (hi->rel.howto->type == R_MIPS_GOT16)
2275 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2276 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2277 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2278 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2279 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2281 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2282 carry or borrow will induce a change of +1 or -1 in the high part. */
2283 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2285 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2286 hi->input_section, output_bfd,
2288 if (ret != bfd_reloc_ok)
2291 mips_hi16_list = hi->next;
2295 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2296 input_section, output_bfd,
2300 /* A generic howto special_function. This calculates and installs the
2301 relocation itself, thus avoiding the oft-discussed problems in
2302 bfd_perform_relocation and bfd_install_relocation. */
2304 bfd_reloc_status_type
2305 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2306 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2307 asection *input_section, bfd *output_bfd,
2308 char **error_message ATTRIBUTE_UNUSED)
2311 bfd_reloc_status_type status;
2312 bfd_boolean relocatable;
2314 relocatable = (output_bfd != NULL);
2316 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2317 return bfd_reloc_outofrange;
2319 /* Build up the field adjustment in VAL. */
2321 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2323 /* Either we're calculating the final field value or we have a
2324 relocation against a section symbol. Add in the section's
2325 offset or address. */
2326 val += symbol->section->output_section->vma;
2327 val += symbol->section->output_offset;
2332 /* We're calculating the final field value. Add in the symbol's value
2333 and, if pc-relative, subtract the address of the field itself. */
2334 val += symbol->value;
2335 if (reloc_entry->howto->pc_relative)
2337 val -= input_section->output_section->vma;
2338 val -= input_section->output_offset;
2339 val -= reloc_entry->address;
2343 /* VAL is now the final adjustment. If we're keeping this relocation
2344 in the output file, and if the relocation uses a separate addend,
2345 we just need to add VAL to that addend. Otherwise we need to add
2346 VAL to the relocation field itself. */
2347 if (relocatable && !reloc_entry->howto->partial_inplace)
2348 reloc_entry->addend += val;
2351 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2353 /* Add in the separate addend, if any. */
2354 val += reloc_entry->addend;
2356 /* Add VAL to the relocation field. */
2357 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2359 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2361 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2364 if (status != bfd_reloc_ok)
2369 reloc_entry->address += input_section->output_offset;
2371 return bfd_reloc_ok;
2374 /* Swap an entry in a .gptab section. Note that these routines rely
2375 on the equivalence of the two elements of the union. */
2378 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2381 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2382 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2386 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2387 Elf32_External_gptab *ex)
2389 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2390 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2394 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2395 Elf32_External_compact_rel *ex)
2397 H_PUT_32 (abfd, in->id1, ex->id1);
2398 H_PUT_32 (abfd, in->num, ex->num);
2399 H_PUT_32 (abfd, in->id2, ex->id2);
2400 H_PUT_32 (abfd, in->offset, ex->offset);
2401 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2402 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2406 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2407 Elf32_External_crinfo *ex)
2411 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2412 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2413 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2414 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2415 H_PUT_32 (abfd, l, ex->info);
2416 H_PUT_32 (abfd, in->konst, ex->konst);
2417 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2420 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2421 routines swap this structure in and out. They are used outside of
2422 BFD, so they are globally visible. */
2425 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2428 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2429 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2430 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2431 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2432 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2433 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2437 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2438 Elf32_External_RegInfo *ex)
2440 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2441 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2442 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2443 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2444 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2445 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2448 /* In the 64 bit ABI, the .MIPS.options section holds register
2449 information in an Elf64_Reginfo structure. These routines swap
2450 them in and out. They are globally visible because they are used
2451 outside of BFD. These routines are here so that gas can call them
2452 without worrying about whether the 64 bit ABI has been included. */
2455 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2456 Elf64_Internal_RegInfo *in)
2458 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2459 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2460 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2461 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2462 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2463 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2464 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2468 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2469 Elf64_External_RegInfo *ex)
2471 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2472 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2473 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2474 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2475 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2476 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2477 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2480 /* Swap in an options header. */
2483 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2484 Elf_Internal_Options *in)
2486 in->kind = H_GET_8 (abfd, ex->kind);
2487 in->size = H_GET_8 (abfd, ex->size);
2488 in->section = H_GET_16 (abfd, ex->section);
2489 in->info = H_GET_32 (abfd, ex->info);
2492 /* Swap out an options header. */
2495 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2496 Elf_External_Options *ex)
2498 H_PUT_8 (abfd, in->kind, ex->kind);
2499 H_PUT_8 (abfd, in->size, ex->size);
2500 H_PUT_16 (abfd, in->section, ex->section);
2501 H_PUT_32 (abfd, in->info, ex->info);
2504 /* This function is called via qsort() to sort the dynamic relocation
2505 entries by increasing r_symndx value. */
2508 sort_dynamic_relocs (const void *arg1, const void *arg2)
2510 Elf_Internal_Rela int_reloc1;
2511 Elf_Internal_Rela int_reloc2;
2514 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2515 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2517 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2521 if (int_reloc1.r_offset < int_reloc2.r_offset)
2523 if (int_reloc1.r_offset > int_reloc2.r_offset)
2528 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2531 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2532 const void *arg2 ATTRIBUTE_UNUSED)
2535 Elf_Internal_Rela int_reloc1[3];
2536 Elf_Internal_Rela int_reloc2[3];
2538 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2539 (reldyn_sorting_bfd, arg1, int_reloc1);
2540 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2541 (reldyn_sorting_bfd, arg2, int_reloc2);
2543 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2545 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2548 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2550 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2559 /* This routine is used to write out ECOFF debugging external symbol
2560 information. It is called via mips_elf_link_hash_traverse. The
2561 ECOFF external symbol information must match the ELF external
2562 symbol information. Unfortunately, at this point we don't know
2563 whether a symbol is required by reloc information, so the two
2564 tables may wind up being different. We must sort out the external
2565 symbol information before we can set the final size of the .mdebug
2566 section, and we must set the size of the .mdebug section before we
2567 can relocate any sections, and we can't know which symbols are
2568 required by relocation until we relocate the sections.
2569 Fortunately, it is relatively unlikely that any symbol will be
2570 stripped but required by a reloc. In particular, it can not happen
2571 when generating a final executable. */
2574 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2576 struct extsym_info *einfo = data;
2578 asection *sec, *output_section;
2580 if (h->root.indx == -2)
2582 else if ((h->root.def_dynamic
2583 || h->root.ref_dynamic
2584 || h->root.type == bfd_link_hash_new)
2585 && !h->root.def_regular
2586 && !h->root.ref_regular)
2588 else if (einfo->info->strip == strip_all
2589 || (einfo->info->strip == strip_some
2590 && bfd_hash_lookup (einfo->info->keep_hash,
2591 h->root.root.root.string,
2592 FALSE, FALSE) == NULL))
2600 if (h->esym.ifd == -2)
2603 h->esym.cobol_main = 0;
2604 h->esym.weakext = 0;
2605 h->esym.reserved = 0;
2606 h->esym.ifd = ifdNil;
2607 h->esym.asym.value = 0;
2608 h->esym.asym.st = stGlobal;
2610 if (h->root.root.type == bfd_link_hash_undefined
2611 || h->root.root.type == bfd_link_hash_undefweak)
2615 /* Use undefined class. Also, set class and type for some
2617 name = h->root.root.root.string;
2618 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2619 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2621 h->esym.asym.sc = scData;
2622 h->esym.asym.st = stLabel;
2623 h->esym.asym.value = 0;
2625 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2627 h->esym.asym.sc = scAbs;
2628 h->esym.asym.st = stLabel;
2629 h->esym.asym.value =
2630 mips_elf_hash_table (einfo->info)->procedure_count;
2632 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2634 h->esym.asym.sc = scAbs;
2635 h->esym.asym.st = stLabel;
2636 h->esym.asym.value = elf_gp (einfo->abfd);
2639 h->esym.asym.sc = scUndefined;
2641 else if (h->root.root.type != bfd_link_hash_defined
2642 && h->root.root.type != bfd_link_hash_defweak)
2643 h->esym.asym.sc = scAbs;
2648 sec = h->root.root.u.def.section;
2649 output_section = sec->output_section;
2651 /* When making a shared library and symbol h is the one from
2652 the another shared library, OUTPUT_SECTION may be null. */
2653 if (output_section == NULL)
2654 h->esym.asym.sc = scUndefined;
2657 name = bfd_section_name (output_section->owner, output_section);
2659 if (strcmp (name, ".text") == 0)
2660 h->esym.asym.sc = scText;
2661 else if (strcmp (name, ".data") == 0)
2662 h->esym.asym.sc = scData;
2663 else if (strcmp (name, ".sdata") == 0)
2664 h->esym.asym.sc = scSData;
2665 else if (strcmp (name, ".rodata") == 0
2666 || strcmp (name, ".rdata") == 0)
2667 h->esym.asym.sc = scRData;
2668 else if (strcmp (name, ".bss") == 0)
2669 h->esym.asym.sc = scBss;
2670 else if (strcmp (name, ".sbss") == 0)
2671 h->esym.asym.sc = scSBss;
2672 else if (strcmp (name, ".init") == 0)
2673 h->esym.asym.sc = scInit;
2674 else if (strcmp (name, ".fini") == 0)
2675 h->esym.asym.sc = scFini;
2677 h->esym.asym.sc = scAbs;
2681 h->esym.asym.reserved = 0;
2682 h->esym.asym.index = indexNil;
2685 if (h->root.root.type == bfd_link_hash_common)
2686 h->esym.asym.value = h->root.root.u.c.size;
2687 else if (h->root.root.type == bfd_link_hash_defined
2688 || h->root.root.type == bfd_link_hash_defweak)
2690 if (h->esym.asym.sc == scCommon)
2691 h->esym.asym.sc = scBss;
2692 else if (h->esym.asym.sc == scSCommon)
2693 h->esym.asym.sc = scSBss;
2695 sec = h->root.root.u.def.section;
2696 output_section = sec->output_section;
2697 if (output_section != NULL)
2698 h->esym.asym.value = (h->root.root.u.def.value
2699 + sec->output_offset
2700 + output_section->vma);
2702 h->esym.asym.value = 0;
2706 struct mips_elf_link_hash_entry *hd = h;
2708 while (hd->root.root.type == bfd_link_hash_indirect)
2709 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2711 if (hd->needs_lazy_stub)
2713 /* Set type and value for a symbol with a function stub. */
2714 h->esym.asym.st = stProc;
2715 sec = hd->root.root.u.def.section;
2717 h->esym.asym.value = 0;
2720 output_section = sec->output_section;
2721 if (output_section != NULL)
2722 h->esym.asym.value = (hd->root.plt.offset
2723 + sec->output_offset
2724 + output_section->vma);
2726 h->esym.asym.value = 0;
2731 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2732 h->root.root.root.string,
2735 einfo->failed = TRUE;
2742 /* A comparison routine used to sort .gptab entries. */
2745 gptab_compare (const void *p1, const void *p2)
2747 const Elf32_gptab *a1 = p1;
2748 const Elf32_gptab *a2 = p2;
2750 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2753 /* Functions to manage the got entry hash table. */
2755 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2758 static INLINE hashval_t
2759 mips_elf_hash_bfd_vma (bfd_vma addr)
2762 return addr + (addr >> 32);
2769 mips_elf_got_entry_hash (const void *entry_)
2771 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2773 return (entry->symndx
2774 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2775 + (entry->tls_type == GOT_TLS_LDM ? 0
2776 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2777 : entry->symndx >= 0 ? (entry->abfd->id
2778 + mips_elf_hash_bfd_vma (entry->d.addend))
2779 : entry->d.h->root.root.root.hash));
2783 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2785 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2786 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2788 return (e1->symndx == e2->symndx
2789 && e1->tls_type == e2->tls_type
2790 && (e1->tls_type == GOT_TLS_LDM ? TRUE
2791 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2792 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2793 && e1->d.addend == e2->d.addend)
2794 : e2->abfd && e1->d.h == e2->d.h));
2798 mips_got_page_ref_hash (const void *ref_)
2800 const struct mips_got_page_ref *ref;
2802 ref = (const struct mips_got_page_ref *) ref_;
2803 return ((ref->symndx >= 0
2804 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2805 : ref->u.h->root.root.root.hash)
2806 + mips_elf_hash_bfd_vma (ref->addend));
2810 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2812 const struct mips_got_page_ref *ref1, *ref2;
2814 ref1 = (const struct mips_got_page_ref *) ref1_;
2815 ref2 = (const struct mips_got_page_ref *) ref2_;
2816 return (ref1->symndx == ref2->symndx
2817 && (ref1->symndx < 0
2818 ? ref1->u.h == ref2->u.h
2819 : ref1->u.abfd == ref2->u.abfd)
2820 && ref1->addend == ref2->addend);
2824 mips_got_page_entry_hash (const void *entry_)
2826 const struct mips_got_page_entry *entry;
2828 entry = (const struct mips_got_page_entry *) entry_;
2829 return entry->sec->id;
2833 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2835 const struct mips_got_page_entry *entry1, *entry2;
2837 entry1 = (const struct mips_got_page_entry *) entry1_;
2838 entry2 = (const struct mips_got_page_entry *) entry2_;
2839 return entry1->sec == entry2->sec;
2842 /* Create and return a new mips_got_info structure. */
2844 static struct mips_got_info *
2845 mips_elf_create_got_info (bfd *abfd)
2847 struct mips_got_info *g;
2849 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
2853 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
2854 mips_elf_got_entry_eq, NULL);
2855 if (g->got_entries == NULL)
2858 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
2859 mips_got_page_ref_eq, NULL);
2860 if (g->got_page_refs == NULL)
2866 /* Return the GOT info for input bfd ABFD, trying to create a new one if
2867 CREATE_P and if ABFD doesn't already have a GOT. */
2869 static struct mips_got_info *
2870 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
2872 struct mips_elf_obj_tdata *tdata;
2874 if (!is_mips_elf (abfd))
2877 tdata = mips_elf_tdata (abfd);
2878 if (!tdata->got && create_p)
2879 tdata->got = mips_elf_create_got_info (abfd);
2883 /* Record that ABFD should use output GOT G. */
2886 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
2888 struct mips_elf_obj_tdata *tdata;
2890 BFD_ASSERT (is_mips_elf (abfd));
2891 tdata = mips_elf_tdata (abfd);
2894 /* The GOT structure itself and the hash table entries are
2895 allocated to a bfd, but the hash tables aren't. */
2896 htab_delete (tdata->got->got_entries);
2897 htab_delete (tdata->got->got_page_refs);
2898 if (tdata->got->got_page_entries)
2899 htab_delete (tdata->got->got_page_entries);
2904 /* Return the dynamic relocation section. If it doesn't exist, try to
2905 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2906 if creation fails. */
2909 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2915 dname = MIPS_ELF_REL_DYN_NAME (info);
2916 dynobj = elf_hash_table (info)->dynobj;
2917 sreloc = bfd_get_linker_section (dynobj, dname);
2918 if (sreloc == NULL && create_p)
2920 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
2925 | SEC_LINKER_CREATED
2928 || ! bfd_set_section_alignment (dynobj, sreloc,
2929 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2935 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
2938 mips_elf_reloc_tls_type (unsigned int r_type)
2940 if (tls_gd_reloc_p (r_type))
2943 if (tls_ldm_reloc_p (r_type))
2946 if (tls_gottprel_reloc_p (r_type))
2949 return GOT_TLS_NONE;
2952 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
2955 mips_tls_got_entries (unsigned int type)
2972 /* Count the number of relocations needed for a TLS GOT entry, with
2973 access types from TLS_TYPE, and symbol H (or a local symbol if H
2977 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2978 struct elf_link_hash_entry *h)
2981 bfd_boolean need_relocs = FALSE;
2982 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2984 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2985 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2988 if ((info->shared || indx != 0)
2990 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2991 || h->root.type != bfd_link_hash_undefweak))
3000 return indx != 0 ? 2 : 1;
3006 return info->shared ? 1 : 0;
3013 /* Add the number of GOT entries and TLS relocations required by ENTRY
3017 mips_elf_count_got_entry (struct bfd_link_info *info,
3018 struct mips_got_info *g,
3019 struct mips_got_entry *entry)
3021 if (entry->tls_type)
3023 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3024 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3026 ? &entry->d.h->root : NULL);
3028 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3029 g->local_gotno += 1;
3031 g->global_gotno += 1;
3034 /* Output a simple dynamic relocation into SRELOC. */
3037 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3039 unsigned long reloc_index,
3044 Elf_Internal_Rela rel[3];
3046 memset (rel, 0, sizeof (rel));
3048 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3049 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3051 if (ABI_64_P (output_bfd))
3053 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3054 (output_bfd, &rel[0],
3056 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3059 bfd_elf32_swap_reloc_out
3060 (output_bfd, &rel[0],
3062 + reloc_index * sizeof (Elf32_External_Rel)));
3065 /* Initialize a set of TLS GOT entries for one symbol. */
3068 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3069 struct mips_got_entry *entry,
3070 struct mips_elf_link_hash_entry *h,
3073 struct mips_elf_link_hash_table *htab;
3075 asection *sreloc, *sgot;
3076 bfd_vma got_offset, got_offset2;
3077 bfd_boolean need_relocs = FALSE;
3079 htab = mips_elf_hash_table (info);
3088 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3090 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3091 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3092 indx = h->root.dynindx;
3095 if (entry->tls_initialized)
3098 if ((info->shared || indx != 0)
3100 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3101 || h->root.type != bfd_link_hash_undefweak))
3104 /* MINUS_ONE means the symbol is not defined in this object. It may not
3105 be defined at all; assume that the value doesn't matter in that
3106 case. Otherwise complain if we would use the value. */
3107 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3108 || h->root.root.type == bfd_link_hash_undefweak);
3110 /* Emit necessary relocations. */
3111 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3112 got_offset = entry->gotidx;
3114 switch (entry->tls_type)
3117 /* General Dynamic. */
3118 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3122 mips_elf_output_dynamic_relocation
3123 (abfd, sreloc, sreloc->reloc_count++, indx,
3124 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3125 sgot->output_offset + sgot->output_section->vma + got_offset);
3128 mips_elf_output_dynamic_relocation
3129 (abfd, sreloc, sreloc->reloc_count++, indx,
3130 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3131 sgot->output_offset + sgot->output_section->vma + got_offset2);
3133 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3134 sgot->contents + got_offset2);
3138 MIPS_ELF_PUT_WORD (abfd, 1,
3139 sgot->contents + got_offset);
3140 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3141 sgot->contents + got_offset2);
3146 /* Initial Exec model. */
3150 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3151 sgot->contents + got_offset);
3153 MIPS_ELF_PUT_WORD (abfd, 0,
3154 sgot->contents + got_offset);
3156 mips_elf_output_dynamic_relocation
3157 (abfd, sreloc, sreloc->reloc_count++, indx,
3158 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3159 sgot->output_offset + sgot->output_section->vma + got_offset);
3162 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3163 sgot->contents + got_offset);
3167 /* The initial offset is zero, and the LD offsets will include the
3168 bias by DTP_OFFSET. */
3169 MIPS_ELF_PUT_WORD (abfd, 0,
3170 sgot->contents + got_offset
3171 + MIPS_ELF_GOT_SIZE (abfd));
3174 MIPS_ELF_PUT_WORD (abfd, 1,
3175 sgot->contents + got_offset);
3177 mips_elf_output_dynamic_relocation
3178 (abfd, sreloc, sreloc->reloc_count++, indx,
3179 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3180 sgot->output_offset + sgot->output_section->vma + got_offset);
3187 entry->tls_initialized = TRUE;
3190 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3191 for global symbol H. .got.plt comes before the GOT, so the offset
3192 will be negative. */
3195 mips_elf_gotplt_index (struct bfd_link_info *info,
3196 struct elf_link_hash_entry *h)
3198 bfd_vma plt_index, got_address, got_value;
3199 struct mips_elf_link_hash_table *htab;
3201 htab = mips_elf_hash_table (info);
3202 BFD_ASSERT (htab != NULL);
3204 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3206 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3207 section starts with reserved entries. */
3208 BFD_ASSERT (htab->is_vxworks);
3210 /* Calculate the index of the symbol's PLT entry. */
3211 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3213 /* Calculate the address of the associated .got.plt entry. */
3214 got_address = (htab->sgotplt->output_section->vma
3215 + htab->sgotplt->output_offset
3218 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3219 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3220 + htab->root.hgot->root.u.def.section->output_offset
3221 + htab->root.hgot->root.u.def.value);
3223 return got_address - got_value;
3226 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3227 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3228 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3229 offset can be found. */
3232 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3233 bfd_vma value, unsigned long r_symndx,
3234 struct mips_elf_link_hash_entry *h, int r_type)
3236 struct mips_elf_link_hash_table *htab;
3237 struct mips_got_entry *entry;
3239 htab = mips_elf_hash_table (info);
3240 BFD_ASSERT (htab != NULL);
3242 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3243 r_symndx, h, r_type);
3247 if (entry->tls_type)
3248 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3249 return entry->gotidx;
3252 /* Return the GOT index of global symbol H in the primary GOT. */
3255 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3256 struct elf_link_hash_entry *h)
3258 struct mips_elf_link_hash_table *htab;
3259 long global_got_dynindx;
3260 struct mips_got_info *g;
3263 htab = mips_elf_hash_table (info);
3264 BFD_ASSERT (htab != NULL);
3266 global_got_dynindx = 0;
3267 if (htab->global_gotsym != NULL)
3268 global_got_dynindx = htab->global_gotsym->dynindx;
3270 /* Once we determine the global GOT entry with the lowest dynamic
3271 symbol table index, we must put all dynamic symbols with greater
3272 indices into the primary GOT. That makes it easy to calculate the
3274 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3275 g = mips_elf_bfd_got (obfd, FALSE);
3276 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3277 * MIPS_ELF_GOT_SIZE (obfd));
3278 BFD_ASSERT (got_index < htab->sgot->size);
3283 /* Return the GOT index for the global symbol indicated by H, which is
3284 referenced by a relocation of type R_TYPE in IBFD. */
3287 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3288 struct elf_link_hash_entry *h, int r_type)
3290 struct mips_elf_link_hash_table *htab;
3291 struct mips_got_info *g;
3292 struct mips_got_entry lookup, *entry;
3295 htab = mips_elf_hash_table (info);
3296 BFD_ASSERT (htab != NULL);
3298 g = mips_elf_bfd_got (ibfd, FALSE);
3301 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3302 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3303 return mips_elf_primary_global_got_index (obfd, info, h);
3307 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3308 entry = htab_find (g->got_entries, &lookup);
3311 gotidx = entry->gotidx;
3312 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3314 if (lookup.tls_type)
3316 bfd_vma value = MINUS_ONE;
3318 if ((h->root.type == bfd_link_hash_defined
3319 || h->root.type == bfd_link_hash_defweak)
3320 && h->root.u.def.section->output_section)
3321 value = (h->root.u.def.value
3322 + h->root.u.def.section->output_offset
3323 + h->root.u.def.section->output_section->vma);
3325 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3330 /* Find a GOT page entry that points to within 32KB of VALUE. These
3331 entries are supposed to be placed at small offsets in the GOT, i.e.,
3332 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3333 entry could be created. If OFFSETP is nonnull, use it to return the
3334 offset of the GOT entry from VALUE. */
3337 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3338 bfd_vma value, bfd_vma *offsetp)
3340 bfd_vma page, got_index;
3341 struct mips_got_entry *entry;
3343 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3344 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3345 NULL, R_MIPS_GOT_PAGE);
3350 got_index = entry->gotidx;
3353 *offsetp = value - entry->d.address;
3358 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3359 EXTERNAL is true if the relocation was originally against a global
3360 symbol that binds locally. */
3363 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3364 bfd_vma value, bfd_boolean external)
3366 struct mips_got_entry *entry;
3368 /* GOT16 relocations against local symbols are followed by a LO16
3369 relocation; those against global symbols are not. Thus if the
3370 symbol was originally local, the GOT16 relocation should load the
3371 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3373 value = mips_elf_high (value) << 16;
3375 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3376 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3377 same in all cases. */
3378 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3379 NULL, R_MIPS_GOT16);
3381 return entry->gotidx;
3386 /* Returns the offset for the entry at the INDEXth position
3390 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3391 bfd *input_bfd, bfd_vma got_index)
3393 struct mips_elf_link_hash_table *htab;
3397 htab = mips_elf_hash_table (info);
3398 BFD_ASSERT (htab != NULL);
3401 gp = _bfd_get_gp_value (output_bfd)
3402 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3404 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3407 /* Create and return a local GOT entry for VALUE, which was calculated
3408 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3409 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3412 static struct mips_got_entry *
3413 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3414 bfd *ibfd, bfd_vma value,
3415 unsigned long r_symndx,
3416 struct mips_elf_link_hash_entry *h,
3419 struct mips_got_entry lookup, *entry;
3421 struct mips_got_info *g;
3422 struct mips_elf_link_hash_table *htab;
3425 htab = mips_elf_hash_table (info);
3426 BFD_ASSERT (htab != NULL);
3428 g = mips_elf_bfd_got (ibfd, FALSE);
3431 g = mips_elf_bfd_got (abfd, FALSE);
3432 BFD_ASSERT (g != NULL);
3435 /* This function shouldn't be called for symbols that live in the global
3437 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3439 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3440 if (lookup.tls_type)
3443 if (tls_ldm_reloc_p (r_type))
3446 lookup.d.addend = 0;
3450 lookup.symndx = r_symndx;
3451 lookup.d.addend = 0;
3459 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3462 gotidx = entry->gotidx;
3463 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3470 lookup.d.address = value;
3471 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3475 entry = (struct mips_got_entry *) *loc;
3479 if (g->assigned_gotno >= g->local_gotno)
3481 /* We didn't allocate enough space in the GOT. */
3482 (*_bfd_error_handler)
3483 (_("not enough GOT space for local GOT entries"));
3484 bfd_set_error (bfd_error_bad_value);
3488 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3492 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3496 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
3498 /* These GOT entries need a dynamic relocation on VxWorks. */
3499 if (htab->is_vxworks)
3501 Elf_Internal_Rela outrel;
3504 bfd_vma got_address;
3506 s = mips_elf_rel_dyn_section (info, FALSE);
3507 got_address = (htab->sgot->output_section->vma
3508 + htab->sgot->output_offset
3511 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3512 outrel.r_offset = got_address;
3513 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3514 outrel.r_addend = value;
3515 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3521 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3522 The number might be exact or a worst-case estimate, depending on how
3523 much information is available to elf_backend_omit_section_dynsym at
3524 the current linking stage. */
3526 static bfd_size_type
3527 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3529 bfd_size_type count;
3532 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3535 const struct elf_backend_data *bed;
3537 bed = get_elf_backend_data (output_bfd);
3538 for (p = output_bfd->sections; p ; p = p->next)
3539 if ((p->flags & SEC_EXCLUDE) == 0
3540 && (p->flags & SEC_ALLOC) != 0
3541 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3547 /* Sort the dynamic symbol table so that symbols that need GOT entries
3548 appear towards the end. */
3551 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3553 struct mips_elf_link_hash_table *htab;
3554 struct mips_elf_hash_sort_data hsd;
3555 struct mips_got_info *g;
3557 if (elf_hash_table (info)->dynsymcount == 0)
3560 htab = mips_elf_hash_table (info);
3561 BFD_ASSERT (htab != NULL);
3568 hsd.max_unref_got_dynindx
3569 = hsd.min_got_dynindx
3570 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3571 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3572 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3573 elf_hash_table (info)),
3574 mips_elf_sort_hash_table_f,
3577 /* There should have been enough room in the symbol table to
3578 accommodate both the GOT and non-GOT symbols. */
3579 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3580 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3581 == elf_hash_table (info)->dynsymcount);
3582 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3583 == g->global_gotno);
3585 /* Now we know which dynamic symbol has the lowest dynamic symbol
3586 table index in the GOT. */
3587 htab->global_gotsym = hsd.low;
3592 /* If H needs a GOT entry, assign it the highest available dynamic
3593 index. Otherwise, assign it the lowest available dynamic
3597 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3599 struct mips_elf_hash_sort_data *hsd = data;
3601 /* Symbols without dynamic symbol table entries aren't interesting
3603 if (h->root.dynindx == -1)
3606 switch (h->global_got_area)
3609 h->root.dynindx = hsd->max_non_got_dynindx++;
3613 h->root.dynindx = --hsd->min_got_dynindx;
3614 hsd->low = (struct elf_link_hash_entry *) h;
3617 case GGA_RELOC_ONLY:
3618 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3619 hsd->low = (struct elf_link_hash_entry *) h;
3620 h->root.dynindx = hsd->max_unref_got_dynindx++;
3627 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3628 (which is owned by the caller and shouldn't be added to the
3629 hash table directly). */
3632 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3633 struct mips_got_entry *lookup)
3635 struct mips_elf_link_hash_table *htab;
3636 struct mips_got_entry *entry;
3637 struct mips_got_info *g;
3638 void **loc, **bfd_loc;
3640 /* Make sure there's a slot for this entry in the master GOT. */
3641 htab = mips_elf_hash_table (info);
3643 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3647 /* Populate the entry if it isn't already. */
3648 entry = (struct mips_got_entry *) *loc;
3651 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3655 lookup->tls_initialized = FALSE;
3656 lookup->gotidx = -1;
3661 /* Reuse the same GOT entry for the BFD's GOT. */
3662 g = mips_elf_bfd_got (abfd, TRUE);
3666 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3675 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3676 entry for it. FOR_CALL is true if the caller is only interested in
3677 using the GOT entry for calls. */
3680 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3681 bfd *abfd, struct bfd_link_info *info,
3682 bfd_boolean for_call, int r_type)
3684 struct mips_elf_link_hash_table *htab;
3685 struct mips_elf_link_hash_entry *hmips;
3686 struct mips_got_entry entry;
3687 unsigned char tls_type;
3689 htab = mips_elf_hash_table (info);
3690 BFD_ASSERT (htab != NULL);
3692 hmips = (struct mips_elf_link_hash_entry *) h;
3694 hmips->got_only_for_calls = FALSE;
3696 /* A global symbol in the GOT must also be in the dynamic symbol
3698 if (h->dynindx == -1)
3700 switch (ELF_ST_VISIBILITY (h->other))
3704 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3707 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3711 tls_type = mips_elf_reloc_tls_type (r_type);
3712 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
3713 hmips->global_got_area = GGA_NORMAL;
3717 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3718 entry.tls_type = tls_type;
3719 return mips_elf_record_got_entry (info, abfd, &entry);
3722 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3723 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3726 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3727 struct bfd_link_info *info, int r_type)
3729 struct mips_elf_link_hash_table *htab;
3730 struct mips_got_info *g;
3731 struct mips_got_entry entry;
3733 htab = mips_elf_hash_table (info);
3734 BFD_ASSERT (htab != NULL);
3737 BFD_ASSERT (g != NULL);
3740 entry.symndx = symndx;
3741 entry.d.addend = addend;
3742 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3743 return mips_elf_record_got_entry (info, abfd, &entry);
3746 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3747 H is the symbol's hash table entry, or null if SYMNDX is local
3751 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3752 long symndx, struct elf_link_hash_entry *h,
3753 bfd_signed_vma addend)
3755 struct mips_elf_link_hash_table *htab;
3756 struct mips_got_info *g1, *g2;
3757 struct mips_got_page_ref lookup, *entry;
3758 void **loc, **bfd_loc;
3760 htab = mips_elf_hash_table (info);
3761 BFD_ASSERT (htab != NULL);
3763 g1 = htab->got_info;
3764 BFD_ASSERT (g1 != NULL);
3769 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3773 lookup.symndx = symndx;
3774 lookup.u.abfd = abfd;
3776 lookup.addend = addend;
3777 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
3781 entry = (struct mips_got_page_ref *) *loc;
3784 entry = bfd_alloc (abfd, sizeof (*entry));
3792 /* Add the same entry to the BFD's GOT. */
3793 g2 = mips_elf_bfd_got (abfd, TRUE);
3797 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
3807 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3810 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3814 struct mips_elf_link_hash_table *htab;
3816 htab = mips_elf_hash_table (info);
3817 BFD_ASSERT (htab != NULL);
3819 s = mips_elf_rel_dyn_section (info, FALSE);
3820 BFD_ASSERT (s != NULL);
3822 if (htab->is_vxworks)
3823 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3828 /* Make room for a null element. */
3829 s->size += MIPS_ELF_REL_SIZE (abfd);
3832 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3836 /* A htab_traverse callback for GOT entries, with DATA pointing to a
3837 mips_elf_traverse_got_arg structure. Count the number of GOT
3838 entries and TLS relocs. Set DATA->value to true if we need
3839 to resolve indirect or warning symbols and then recreate the GOT. */
3842 mips_elf_check_recreate_got (void **entryp, void *data)
3844 struct mips_got_entry *entry;
3845 struct mips_elf_traverse_got_arg *arg;
3847 entry = (struct mips_got_entry *) *entryp;
3848 arg = (struct mips_elf_traverse_got_arg *) data;
3849 if (entry->abfd != NULL && entry->symndx == -1)
3851 struct mips_elf_link_hash_entry *h;
3854 if (h->root.root.type == bfd_link_hash_indirect
3855 || h->root.root.type == bfd_link_hash_warning)
3861 mips_elf_count_got_entry (arg->info, arg->g, entry);
3865 /* A htab_traverse callback for GOT entries, with DATA pointing to a
3866 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
3867 converting entries for indirect and warning symbols into entries
3868 for the target symbol. Set DATA->g to null on error. */
3871 mips_elf_recreate_got (void **entryp, void *data)
3873 struct mips_got_entry new_entry, *entry;
3874 struct mips_elf_traverse_got_arg *arg;
3877 entry = (struct mips_got_entry *) *entryp;
3878 arg = (struct mips_elf_traverse_got_arg *) data;
3879 if (entry->abfd != NULL
3880 && entry->symndx == -1
3881 && (entry->d.h->root.root.type == bfd_link_hash_indirect
3882 || entry->d.h->root.root.type == bfd_link_hash_warning))
3884 struct mips_elf_link_hash_entry *h;
3891 BFD_ASSERT (h->global_got_area == GGA_NONE);
3892 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3894 while (h->root.root.type == bfd_link_hash_indirect
3895 || h->root.root.type == bfd_link_hash_warning);
3898 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
3906 if (entry == &new_entry)
3908 entry = bfd_alloc (entry->abfd, sizeof (*entry));
3917 mips_elf_count_got_entry (arg->info, arg->g, entry);
3922 /* Return the maximum number of GOT page entries required for RANGE. */
3925 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3927 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3930 /* Record that G requires a page entry that can reach SEC + ADDEND. */
3933 mips_elf_record_got_page_entry (struct mips_got_info *g,
3934 asection *sec, bfd_signed_vma addend)
3936 struct mips_got_page_entry lookup, *entry;
3937 struct mips_got_page_range **range_ptr, *range;
3938 bfd_vma old_pages, new_pages;
3941 /* Find the mips_got_page_entry hash table entry for this section. */
3943 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3947 /* Create a mips_got_page_entry if this is the first time we've
3948 seen the section. */
3949 entry = (struct mips_got_page_entry *) *loc;
3952 entry = bfd_zalloc (sec->owner, sizeof (*entry));
3960 /* Skip over ranges whose maximum extent cannot share a page entry
3962 range_ptr = &entry->ranges;
3963 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3964 range_ptr = &(*range_ptr)->next;
3966 /* If we scanned to the end of the list, or found a range whose
3967 minimum extent cannot share a page entry with ADDEND, create
3968 a new singleton range. */
3970 if (!range || addend < range->min_addend - 0xffff)
3972 range = bfd_zalloc (sec->owner, sizeof (*range));
3976 range->next = *range_ptr;
3977 range->min_addend = addend;
3978 range->max_addend = addend;
3986 /* Remember how many pages the old range contributed. */
3987 old_pages = mips_elf_pages_for_range (range);
3989 /* Update the ranges. */
3990 if (addend < range->min_addend)
3991 range->min_addend = addend;
3992 else if (addend > range->max_addend)
3994 if (range->next && addend >= range->next->min_addend - 0xffff)
3996 old_pages += mips_elf_pages_for_range (range->next);
3997 range->max_addend = range->next->max_addend;
3998 range->next = range->next->next;
4001 range->max_addend = addend;
4004 /* Record any change in the total estimate. */
4005 new_pages = mips_elf_pages_for_range (range);
4006 if (old_pages != new_pages)
4008 entry->num_pages += new_pages - old_pages;
4009 g->page_gotno += new_pages - old_pages;
4015 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4016 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4017 whether the page reference described by *REFP needs a GOT page entry,
4018 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4021 mips_elf_resolve_got_page_ref (void **refp, void *data)
4023 struct mips_got_page_ref *ref;
4024 struct mips_elf_traverse_got_arg *arg;
4025 struct mips_elf_link_hash_table *htab;
4029 ref = (struct mips_got_page_ref *) *refp;
4030 arg = (struct mips_elf_traverse_got_arg *) data;
4031 htab = mips_elf_hash_table (arg->info);
4033 if (ref->symndx < 0)
4035 struct mips_elf_link_hash_entry *h;
4037 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4039 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4042 /* Ignore undefined symbols; we'll issue an error later if
4044 if (!((h->root.root.type == bfd_link_hash_defined
4045 || h->root.root.type == bfd_link_hash_defweak)
4046 && h->root.root.u.def.section))
4049 sec = h->root.root.u.def.section;
4050 addend = h->root.root.u.def.value + ref->addend;
4054 Elf_Internal_Sym *isym;
4056 /* Read in the symbol. */
4057 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4065 /* Get the associated input section. */
4066 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4073 /* If this is a mergable section, work out the section and offset
4074 of the merged data. For section symbols, the addend specifies
4075 of the offset _of_ the first byte in the data, otherwise it
4076 specifies the offset _from_ the first byte. */
4077 if (sec->flags & SEC_MERGE)
4081 secinfo = elf_section_data (sec)->sec_info;
4082 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4083 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4084 isym->st_value + ref->addend);
4086 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4087 isym->st_value) + ref->addend;
4090 addend = isym->st_value + ref->addend;
4092 if (!mips_elf_record_got_page_entry (arg->g, sec, addend))
4100 /* If any entries in G->got_entries are for indirect or warning symbols,
4101 replace them with entries for the target symbol. Convert g->got_page_refs
4102 into got_page_entry structures and estimate the number of page entries
4103 that they require. */
4106 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4107 struct mips_got_info *g)
4109 struct mips_elf_traverse_got_arg tga;
4110 struct mips_got_info oldg;
4117 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4121 g->got_entries = htab_create (htab_size (oldg.got_entries),
4122 mips_elf_got_entry_hash,
4123 mips_elf_got_entry_eq, NULL);
4124 if (!g->got_entries)
4127 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4131 htab_delete (oldg.got_entries);
4134 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4135 mips_got_page_entry_eq, NULL);
4136 if (g->got_page_entries == NULL)
4141 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4146 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4147 link_info structure. Decide whether the hash entry needs an entry in
4148 the global part of the primary GOT, setting global_got_area accordingly.
4149 Count the number of global symbols that are in the primary GOT only
4150 because they have relocations against them (reloc_only_gotno). */
4153 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4155 struct bfd_link_info *info;
4156 struct mips_elf_link_hash_table *htab;
4157 struct mips_got_info *g;
4159 info = (struct bfd_link_info *) data;
4160 htab = mips_elf_hash_table (info);
4162 if (h->global_got_area != GGA_NONE)
4164 /* Make a final decision about whether the symbol belongs in the
4165 local or global GOT. Symbols that bind locally can (and in the
4166 case of forced-local symbols, must) live in the local GOT.
4167 Those that are aren't in the dynamic symbol table must also
4168 live in the local GOT.
4170 Note that the former condition does not always imply the
4171 latter: symbols do not bind locally if they are completely
4172 undefined. We'll report undefined symbols later if appropriate. */
4173 if (h->root.dynindx == -1
4174 || (h->got_only_for_calls
4175 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4176 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
4177 /* The symbol belongs in the local GOT. We no longer need this
4178 entry if it was only used for relocations; those relocations
4179 will be against the null or section symbol instead of H. */
4180 h->global_got_area = GGA_NONE;
4181 else if (htab->is_vxworks
4182 && h->got_only_for_calls
4183 && h->root.plt.offset != MINUS_ONE)
4184 /* On VxWorks, calls can refer directly to the .got.plt entry;
4185 they don't need entries in the regular GOT. .got.plt entries
4186 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4187 h->global_got_area = GGA_NONE;
4188 else if (h->global_got_area == GGA_RELOC_ONLY)
4190 g->reloc_only_gotno++;
4197 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4198 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4201 mips_elf_add_got_entry (void **entryp, void *data)
4203 struct mips_got_entry *entry;
4204 struct mips_elf_traverse_got_arg *arg;
4207 entry = (struct mips_got_entry *) *entryp;
4208 arg = (struct mips_elf_traverse_got_arg *) data;
4209 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4218 mips_elf_count_got_entry (arg->info, arg->g, entry);
4223 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4224 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4227 mips_elf_add_got_page_entry (void **entryp, void *data)
4229 struct mips_got_page_entry *entry;
4230 struct mips_elf_traverse_got_arg *arg;
4233 entry = (struct mips_got_page_entry *) *entryp;
4234 arg = (struct mips_elf_traverse_got_arg *) data;
4235 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4244 arg->g->page_gotno += entry->num_pages;
4249 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4250 this would lead to overflow, 1 if they were merged successfully,
4251 and 0 if a merge failed due to lack of memory. (These values are chosen
4252 so that nonnegative return values can be returned by a htab_traverse
4256 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4257 struct mips_got_info *to,
4258 struct mips_elf_got_per_bfd_arg *arg)
4260 struct mips_elf_traverse_got_arg tga;
4261 unsigned int estimate;
4263 /* Work out how many page entries we would need for the combined GOT. */
4264 estimate = arg->max_pages;
4265 if (estimate >= from->page_gotno + to->page_gotno)
4266 estimate = from->page_gotno + to->page_gotno;
4268 /* And conservatively estimate how many local and TLS entries
4270 estimate += from->local_gotno + to->local_gotno;
4271 estimate += from->tls_gotno + to->tls_gotno;
4273 /* If we're merging with the primary got, any TLS relocations will
4274 come after the full set of global entries. Otherwise estimate those
4275 conservatively as well. */
4276 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4277 estimate += arg->global_count;
4279 estimate += from->global_gotno + to->global_gotno;
4281 /* Bail out if the combined GOT might be too big. */
4282 if (estimate > arg->max_count)
4285 /* Transfer the bfd's got information from FROM to TO. */
4286 tga.info = arg->info;
4288 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4292 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4296 mips_elf_replace_bfd_got (abfd, to);
4300 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4301 as possible of the primary got, since it doesn't require explicit
4302 dynamic relocations, but don't use bfds that would reference global
4303 symbols out of the addressable range. Failing the primary got,
4304 attempt to merge with the current got, or finish the current got
4305 and then make make the new got current. */
4308 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4309 struct mips_elf_got_per_bfd_arg *arg)
4311 unsigned int estimate;
4314 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4317 /* Work out the number of page, local and TLS entries. */
4318 estimate = arg->max_pages;
4319 if (estimate > g->page_gotno)
4320 estimate = g->page_gotno;
4321 estimate += g->local_gotno + g->tls_gotno;
4323 /* We place TLS GOT entries after both locals and globals. The globals
4324 for the primary GOT may overflow the normal GOT size limit, so be
4325 sure not to merge a GOT which requires TLS with the primary GOT in that
4326 case. This doesn't affect non-primary GOTs. */
4327 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4329 if (estimate <= arg->max_count)
4331 /* If we don't have a primary GOT, use it as
4332 a starting point for the primary GOT. */
4339 /* Try merging with the primary GOT. */
4340 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4345 /* If we can merge with the last-created got, do it. */
4348 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4353 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4354 fits; if it turns out that it doesn't, we'll get relocation
4355 overflows anyway. */
4356 g->next = arg->current;
4362 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4363 to GOTIDX, duplicating the entry if it has already been assigned
4364 an index in a different GOT. */
4367 mips_elf_set_gotidx (void **entryp, long gotidx)
4369 struct mips_got_entry *entry;
4371 entry = (struct mips_got_entry *) *entryp;
4372 if (entry->gotidx > 0)
4374 struct mips_got_entry *new_entry;
4376 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4380 *new_entry = *entry;
4381 *entryp = new_entry;
4384 entry->gotidx = gotidx;
4388 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4389 mips_elf_traverse_got_arg in which DATA->value is the size of one
4390 GOT entry. Set DATA->g to null on failure. */
4393 mips_elf_initialize_tls_index (void **entryp, void *data)
4395 struct mips_got_entry *entry;
4396 struct mips_elf_traverse_got_arg *arg;
4398 /* We're only interested in TLS symbols. */
4399 entry = (struct mips_got_entry *) *entryp;
4400 if (entry->tls_type == GOT_TLS_NONE)
4403 arg = (struct mips_elf_traverse_got_arg *) data;
4404 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4410 /* Account for the entries we've just allocated. */
4411 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4415 /* A htab_traverse callback for GOT entries, where DATA points to a
4416 mips_elf_traverse_got_arg. Set the global_got_area of each global
4417 symbol to DATA->value. */
4420 mips_elf_set_global_got_area (void **entryp, void *data)
4422 struct mips_got_entry *entry;
4423 struct mips_elf_traverse_got_arg *arg;
4425 entry = (struct mips_got_entry *) *entryp;
4426 arg = (struct mips_elf_traverse_got_arg *) data;
4427 if (entry->abfd != NULL
4428 && entry->symndx == -1
4429 && entry->d.h->global_got_area != GGA_NONE)
4430 entry->d.h->global_got_area = arg->value;
4434 /* A htab_traverse callback for secondary GOT entries, where DATA points
4435 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4436 and record the number of relocations they require. DATA->value is
4437 the size of one GOT entry. Set DATA->g to null on failure. */
4440 mips_elf_set_global_gotidx (void **entryp, void *data)
4442 struct mips_got_entry *entry;
4443 struct mips_elf_traverse_got_arg *arg;
4445 entry = (struct mips_got_entry *) *entryp;
4446 arg = (struct mips_elf_traverse_got_arg *) data;
4447 if (entry->abfd != NULL
4448 && entry->symndx == -1
4449 && entry->d.h->global_got_area != GGA_NONE)
4451 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_gotno))
4456 arg->g->assigned_gotno += 1;
4458 if (arg->info->shared
4459 || (elf_hash_table (arg->info)->dynamic_sections_created
4460 && entry->d.h->root.def_dynamic
4461 && !entry->d.h->root.def_regular))
4462 arg->g->relocs += 1;
4468 /* A htab_traverse callback for GOT entries for which DATA is the
4469 bfd_link_info. Forbid any global symbols from having traditional
4470 lazy-binding stubs. */
4473 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4475 struct bfd_link_info *info;
4476 struct mips_elf_link_hash_table *htab;
4477 struct mips_got_entry *entry;
4479 entry = (struct mips_got_entry *) *entryp;
4480 info = (struct bfd_link_info *) data;
4481 htab = mips_elf_hash_table (info);
4482 BFD_ASSERT (htab != NULL);
4484 if (entry->abfd != NULL
4485 && entry->symndx == -1
4486 && entry->d.h->needs_lazy_stub)
4488 entry->d.h->needs_lazy_stub = FALSE;
4489 htab->lazy_stub_count--;
4495 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4498 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4503 g = mips_elf_bfd_got (ibfd, FALSE);
4507 BFD_ASSERT (g->next);
4511 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4512 * MIPS_ELF_GOT_SIZE (abfd);
4515 /* Turn a single GOT that is too big for 16-bit addressing into
4516 a sequence of GOTs, each one 16-bit addressable. */
4519 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4520 asection *got, bfd_size_type pages)
4522 struct mips_elf_link_hash_table *htab;
4523 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4524 struct mips_elf_traverse_got_arg tga;
4525 struct mips_got_info *g, *gg;
4526 unsigned int assign, needed_relocs;
4529 dynobj = elf_hash_table (info)->dynobj;
4530 htab = mips_elf_hash_table (info);
4531 BFD_ASSERT (htab != NULL);
4535 got_per_bfd_arg.obfd = abfd;
4536 got_per_bfd_arg.info = info;
4537 got_per_bfd_arg.current = NULL;
4538 got_per_bfd_arg.primary = NULL;
4539 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4540 / MIPS_ELF_GOT_SIZE (abfd))
4541 - htab->reserved_gotno);
4542 got_per_bfd_arg.max_pages = pages;
4543 /* The number of globals that will be included in the primary GOT.
4544 See the calls to mips_elf_set_global_got_area below for more
4546 got_per_bfd_arg.global_count = g->global_gotno;
4548 /* Try to merge the GOTs of input bfds together, as long as they
4549 don't seem to exceed the maximum GOT size, choosing one of them
4550 to be the primary GOT. */
4551 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4553 gg = mips_elf_bfd_got (ibfd, FALSE);
4554 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4558 /* If we do not find any suitable primary GOT, create an empty one. */
4559 if (got_per_bfd_arg.primary == NULL)
4560 g->next = mips_elf_create_got_info (abfd);
4562 g->next = got_per_bfd_arg.primary;
4563 g->next->next = got_per_bfd_arg.current;
4565 /* GG is now the master GOT, and G is the primary GOT. */
4569 /* Map the output bfd to the primary got. That's what we're going
4570 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4571 didn't mark in check_relocs, and we want a quick way to find it.
4572 We can't just use gg->next because we're going to reverse the
4574 mips_elf_replace_bfd_got (abfd, g);
4576 /* Every symbol that is referenced in a dynamic relocation must be
4577 present in the primary GOT, so arrange for them to appear after
4578 those that are actually referenced. */
4579 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4580 g->global_gotno = gg->global_gotno;
4583 tga.value = GGA_RELOC_ONLY;
4584 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4585 tga.value = GGA_NORMAL;
4586 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4588 /* Now go through the GOTs assigning them offset ranges.
4589 [assigned_gotno, local_gotno[ will be set to the range of local
4590 entries in each GOT. We can then compute the end of a GOT by
4591 adding local_gotno to global_gotno. We reverse the list and make
4592 it circular since then we'll be able to quickly compute the
4593 beginning of a GOT, by computing the end of its predecessor. To
4594 avoid special cases for the primary GOT, while still preserving
4595 assertions that are valid for both single- and multi-got links,
4596 we arrange for the main got struct to have the right number of
4597 global entries, but set its local_gotno such that the initial
4598 offset of the primary GOT is zero. Remember that the primary GOT
4599 will become the last item in the circular linked list, so it
4600 points back to the master GOT. */
4601 gg->local_gotno = -g->global_gotno;
4602 gg->global_gotno = g->global_gotno;
4609 struct mips_got_info *gn;
4611 assign += htab->reserved_gotno;
4612 g->assigned_gotno = assign;
4613 g->local_gotno += assign;
4614 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4615 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4617 /* Take g out of the direct list, and push it onto the reversed
4618 list that gg points to. g->next is guaranteed to be nonnull after
4619 this operation, as required by mips_elf_initialize_tls_index. */
4624 /* Set up any TLS entries. We always place the TLS entries after
4625 all non-TLS entries. */
4626 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4628 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4629 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4632 BFD_ASSERT (g->tls_assigned_gotno == assign);
4634 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4637 /* Forbid global symbols in every non-primary GOT from having
4638 lazy-binding stubs. */
4640 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4644 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4647 for (g = gg->next; g && g->next != gg; g = g->next)
4649 unsigned int save_assign;
4651 /* Assign offsets to global GOT entries and count how many
4652 relocations they need. */
4653 save_assign = g->assigned_gotno;
4654 g->assigned_gotno = g->local_gotno;
4656 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4658 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4661 BFD_ASSERT (g->assigned_gotno == g->local_gotno + g->global_gotno);
4662 g->assigned_gotno = save_assign;
4666 g->relocs += g->local_gotno - g->assigned_gotno;
4667 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4668 + g->next->global_gotno
4669 + g->next->tls_gotno
4670 + htab->reserved_gotno);
4672 needed_relocs += g->relocs;
4674 needed_relocs += g->relocs;
4677 mips_elf_allocate_dynamic_relocations (dynobj, info,
4684 /* Returns the first relocation of type r_type found, beginning with
4685 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4687 static const Elf_Internal_Rela *
4688 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4689 const Elf_Internal_Rela *relocation,
4690 const Elf_Internal_Rela *relend)
4692 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4694 while (relocation < relend)
4696 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4697 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4703 /* We didn't find it. */
4707 /* Return whether an input relocation is against a local symbol. */
4710 mips_elf_local_relocation_p (bfd *input_bfd,
4711 const Elf_Internal_Rela *relocation,
4712 asection **local_sections)
4714 unsigned long r_symndx;
4715 Elf_Internal_Shdr *symtab_hdr;
4718 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4719 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4720 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4722 if (r_symndx < extsymoff)
4724 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4730 /* Sign-extend VALUE, which has the indicated number of BITS. */
4733 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4735 if (value & ((bfd_vma) 1 << (bits - 1)))
4736 /* VALUE is negative. */
4737 value |= ((bfd_vma) - 1) << bits;
4742 /* Return non-zero if the indicated VALUE has overflowed the maximum
4743 range expressible by a signed number with the indicated number of
4747 mips_elf_overflow_p (bfd_vma value, int bits)
4749 bfd_signed_vma svalue = (bfd_signed_vma) value;
4751 if (svalue > (1 << (bits - 1)) - 1)
4752 /* The value is too big. */
4754 else if (svalue < -(1 << (bits - 1)))
4755 /* The value is too small. */
4762 /* Calculate the %high function. */
4765 mips_elf_high (bfd_vma value)
4767 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4770 /* Calculate the %higher function. */
4773 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4776 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4783 /* Calculate the %highest function. */
4786 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4789 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4796 /* Create the .compact_rel section. */
4799 mips_elf_create_compact_rel_section
4800 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4803 register asection *s;
4805 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
4807 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4810 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
4812 || ! bfd_set_section_alignment (abfd, s,
4813 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4816 s->size = sizeof (Elf32_External_compact_rel);
4822 /* Create the .got section to hold the global offset table. */
4825 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4828 register asection *s;
4829 struct elf_link_hash_entry *h;
4830 struct bfd_link_hash_entry *bh;
4831 struct mips_elf_link_hash_table *htab;
4833 htab = mips_elf_hash_table (info);
4834 BFD_ASSERT (htab != NULL);
4836 /* This function may be called more than once. */
4840 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4841 | SEC_LINKER_CREATED);
4843 /* We have to use an alignment of 2**4 here because this is hardcoded
4844 in the function stub generation and in the linker script. */
4845 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4847 || ! bfd_set_section_alignment (abfd, s, 4))
4851 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4852 linker script because we don't want to define the symbol if we
4853 are not creating a global offset table. */
4855 if (! (_bfd_generic_link_add_one_symbol
4856 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4857 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4860 h = (struct elf_link_hash_entry *) bh;
4863 h->type = STT_OBJECT;
4864 elf_hash_table (info)->hgot = h;
4867 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4870 htab->got_info = mips_elf_create_got_info (abfd);
4871 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4872 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4874 /* We also need a .got.plt section when generating PLTs. */
4875 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
4876 SEC_ALLOC | SEC_LOAD
4879 | SEC_LINKER_CREATED);
4887 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4888 __GOTT_INDEX__ symbols. These symbols are only special for
4889 shared objects; they are not used in executables. */
4892 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4894 return (mips_elf_hash_table (info)->is_vxworks
4896 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4897 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4900 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4901 require an la25 stub. See also mips_elf_local_pic_function_p,
4902 which determines whether the destination function ever requires a
4906 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
4907 bfd_boolean target_is_16_bit_code_p)
4909 /* We specifically ignore branches and jumps from EF_PIC objects,
4910 where the onus is on the compiler or programmer to perform any
4911 necessary initialization of $25. Sometimes such initialization
4912 is unnecessary; for example, -mno-shared functions do not use
4913 the incoming value of $25, and may therefore be called directly. */
4914 if (PIC_OBJECT_P (input_bfd))
4921 case R_MICROMIPS_26_S1:
4922 case R_MICROMIPS_PC7_S1:
4923 case R_MICROMIPS_PC10_S1:
4924 case R_MICROMIPS_PC16_S1:
4925 case R_MICROMIPS_PC23_S2:
4929 return !target_is_16_bit_code_p;
4936 /* Calculate the value produced by the RELOCATION (which comes from
4937 the INPUT_BFD). The ADDEND is the addend to use for this
4938 RELOCATION; RELOCATION->R_ADDEND is ignored.
4940 The result of the relocation calculation is stored in VALUEP.
4941 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4942 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
4944 This function returns bfd_reloc_continue if the caller need take no
4945 further action regarding this relocation, bfd_reloc_notsupported if
4946 something goes dramatically wrong, bfd_reloc_overflow if an
4947 overflow occurs, and bfd_reloc_ok to indicate success. */
4949 static bfd_reloc_status_type
4950 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4951 asection *input_section,
4952 struct bfd_link_info *info,
4953 const Elf_Internal_Rela *relocation,
4954 bfd_vma addend, reloc_howto_type *howto,
4955 Elf_Internal_Sym *local_syms,
4956 asection **local_sections, bfd_vma *valuep,
4958 bfd_boolean *cross_mode_jump_p,
4959 bfd_boolean save_addend)
4961 /* The eventual value we will return. */
4963 /* The address of the symbol against which the relocation is
4966 /* The final GP value to be used for the relocatable, executable, or
4967 shared object file being produced. */
4969 /* The place (section offset or address) of the storage unit being
4972 /* The value of GP used to create the relocatable object. */
4974 /* The offset into the global offset table at which the address of
4975 the relocation entry symbol, adjusted by the addend, resides
4976 during execution. */
4977 bfd_vma g = MINUS_ONE;
4978 /* The section in which the symbol referenced by the relocation is
4980 asection *sec = NULL;
4981 struct mips_elf_link_hash_entry *h = NULL;
4982 /* TRUE if the symbol referred to by this relocation is a local
4984 bfd_boolean local_p, was_local_p;
4985 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4986 bfd_boolean gp_disp_p = FALSE;
4987 /* TRUE if the symbol referred to by this relocation is
4988 "__gnu_local_gp". */
4989 bfd_boolean gnu_local_gp_p = FALSE;
4990 Elf_Internal_Shdr *symtab_hdr;
4992 unsigned long r_symndx;
4994 /* TRUE if overflow occurred during the calculation of the
4995 relocation value. */
4996 bfd_boolean overflowed_p;
4997 /* TRUE if this relocation refers to a MIPS16 function. */
4998 bfd_boolean target_is_16_bit_code_p = FALSE;
4999 bfd_boolean target_is_micromips_code_p = FALSE;
5000 struct mips_elf_link_hash_table *htab;
5003 dynobj = elf_hash_table (info)->dynobj;
5004 htab = mips_elf_hash_table (info);
5005 BFD_ASSERT (htab != NULL);
5007 /* Parse the relocation. */
5008 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5009 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5010 p = (input_section->output_section->vma
5011 + input_section->output_offset
5012 + relocation->r_offset);
5014 /* Assume that there will be no overflow. */
5015 overflowed_p = FALSE;
5017 /* Figure out whether or not the symbol is local, and get the offset
5018 used in the array of hash table entries. */
5019 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5020 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5022 was_local_p = local_p;
5023 if (! elf_bad_symtab (input_bfd))
5024 extsymoff = symtab_hdr->sh_info;
5027 /* The symbol table does not follow the rule that local symbols
5028 must come before globals. */
5032 /* Figure out the value of the symbol. */
5035 Elf_Internal_Sym *sym;
5037 sym = local_syms + r_symndx;
5038 sec = local_sections[r_symndx];
5040 symbol = sec->output_section->vma + sec->output_offset;
5041 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5042 || (sec->flags & SEC_MERGE))
5043 symbol += sym->st_value;
5044 if ((sec->flags & SEC_MERGE)
5045 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5047 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5049 addend += sec->output_section->vma + sec->output_offset;
5052 /* MIPS16/microMIPS text labels should be treated as odd. */
5053 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5056 /* Record the name of this symbol, for our caller. */
5057 *namep = bfd_elf_string_from_elf_section (input_bfd,
5058 symtab_hdr->sh_link,
5061 *namep = bfd_section_name (input_bfd, sec);
5063 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5064 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5068 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5070 /* For global symbols we look up the symbol in the hash-table. */
5071 h = ((struct mips_elf_link_hash_entry *)
5072 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5073 /* Find the real hash-table entry for this symbol. */
5074 while (h->root.root.type == bfd_link_hash_indirect
5075 || h->root.root.type == bfd_link_hash_warning)
5076 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5078 /* Record the name of this symbol, for our caller. */
5079 *namep = h->root.root.root.string;
5081 /* See if this is the special _gp_disp symbol. Note that such a
5082 symbol must always be a global symbol. */
5083 if (strcmp (*namep, "_gp_disp") == 0
5084 && ! NEWABI_P (input_bfd))
5086 /* Relocations against _gp_disp are permitted only with
5087 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5088 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5089 return bfd_reloc_notsupported;
5093 /* See if this is the special _gp symbol. Note that such a
5094 symbol must always be a global symbol. */
5095 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5096 gnu_local_gp_p = TRUE;
5099 /* If this symbol is defined, calculate its address. Note that
5100 _gp_disp is a magic symbol, always implicitly defined by the
5101 linker, so it's inappropriate to check to see whether or not
5103 else if ((h->root.root.type == bfd_link_hash_defined
5104 || h->root.root.type == bfd_link_hash_defweak)
5105 && h->root.root.u.def.section)
5107 sec = h->root.root.u.def.section;
5108 if (sec->output_section)
5109 symbol = (h->root.root.u.def.value
5110 + sec->output_section->vma
5111 + sec->output_offset);
5113 symbol = h->root.root.u.def.value;
5115 else if (h->root.root.type == bfd_link_hash_undefweak)
5116 /* We allow relocations against undefined weak symbols, giving
5117 it the value zero, so that you can undefined weak functions
5118 and check to see if they exist by looking at their
5121 else if (info->unresolved_syms_in_objects == RM_IGNORE
5122 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5124 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5125 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5127 /* If this is a dynamic link, we should have created a
5128 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5129 in in _bfd_mips_elf_create_dynamic_sections.
5130 Otherwise, we should define the symbol with a value of 0.
5131 FIXME: It should probably get into the symbol table
5133 BFD_ASSERT (! info->shared);
5134 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5137 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5139 /* This is an optional symbol - an Irix specific extension to the
5140 ELF spec. Ignore it for now.
5141 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5142 than simply ignoring them, but we do not handle this for now.
5143 For information see the "64-bit ELF Object File Specification"
5144 which is available from here:
5145 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5148 else if ((*info->callbacks->undefined_symbol)
5149 (info, h->root.root.root.string, input_bfd,
5150 input_section, relocation->r_offset,
5151 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5152 || ELF_ST_VISIBILITY (h->root.other)))
5154 return bfd_reloc_undefined;
5158 return bfd_reloc_notsupported;
5161 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5162 /* If the output section is the PLT section,
5163 then the target is not microMIPS. */
5164 target_is_micromips_code_p = (htab->splt != sec
5165 && ELF_ST_IS_MICROMIPS (h->root.other));
5168 /* If this is a reference to a 16-bit function with a stub, we need
5169 to redirect the relocation to the stub unless:
5171 (a) the relocation is for a MIPS16 JAL;
5173 (b) the relocation is for a MIPS16 PIC call, and there are no
5174 non-MIPS16 uses of the GOT slot; or
5176 (c) the section allows direct references to MIPS16 functions. */
5177 if (r_type != R_MIPS16_26
5178 && !info->relocatable
5180 && h->fn_stub != NULL
5181 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5183 && elf_tdata (input_bfd)->local_stubs != NULL
5184 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5185 && !section_allows_mips16_refs_p (input_section))
5187 /* This is a 32- or 64-bit call to a 16-bit function. We should
5188 have already noticed that we were going to need the
5192 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5197 BFD_ASSERT (h->need_fn_stub);
5200 /* If a LA25 header for the stub itself exists, point to the
5201 prepended LUI/ADDIU sequence. */
5202 sec = h->la25_stub->stub_section;
5203 value = h->la25_stub->offset;
5212 symbol = sec->output_section->vma + sec->output_offset + value;
5213 /* The target is 16-bit, but the stub isn't. */
5214 target_is_16_bit_code_p = FALSE;
5216 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5217 need to redirect the call to the stub. Note that we specifically
5218 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5219 use an indirect stub instead. */
5220 else if (r_type == R_MIPS16_26 && !info->relocatable
5221 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5223 && elf_tdata (input_bfd)->local_call_stubs != NULL
5224 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5225 && !target_is_16_bit_code_p)
5228 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5231 /* If both call_stub and call_fp_stub are defined, we can figure
5232 out which one to use by checking which one appears in the input
5234 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5239 for (o = input_bfd->sections; o != NULL; o = o->next)
5241 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5243 sec = h->call_fp_stub;
5250 else if (h->call_stub != NULL)
5253 sec = h->call_fp_stub;
5256 BFD_ASSERT (sec->size > 0);
5257 symbol = sec->output_section->vma + sec->output_offset;
5259 /* If this is a direct call to a PIC function, redirect to the
5261 else if (h != NULL && h->la25_stub
5262 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5263 target_is_16_bit_code_p))
5264 symbol = (h->la25_stub->stub_section->output_section->vma
5265 + h->la25_stub->stub_section->output_offset
5266 + h->la25_stub->offset);
5268 /* Make sure MIPS16 and microMIPS are not used together. */
5269 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5270 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5272 (*_bfd_error_handler)
5273 (_("MIPS16 and microMIPS functions cannot call each other"));
5274 return bfd_reloc_notsupported;
5277 /* Calls from 16-bit code to 32-bit code and vice versa require the
5278 mode change. However, we can ignore calls to undefined weak symbols,
5279 which should never be executed at runtime. This exception is important
5280 because the assembly writer may have "known" that any definition of the
5281 symbol would be 16-bit code, and that direct jumps were therefore
5283 *cross_mode_jump_p = (!info->relocatable
5284 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5285 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5286 || (r_type == R_MICROMIPS_26_S1
5287 && !target_is_micromips_code_p)
5288 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5289 && (target_is_16_bit_code_p
5290 || target_is_micromips_code_p))));
5292 local_p = (h == NULL
5293 || (h->got_only_for_calls
5294 ? SYMBOL_CALLS_LOCAL (info, &h->root)
5295 : SYMBOL_REFERENCES_LOCAL (info, &h->root)));
5297 gp0 = _bfd_get_gp_value (input_bfd);
5298 gp = _bfd_get_gp_value (abfd);
5300 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5305 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5306 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5307 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5308 if (got_page_reloc_p (r_type) && !local_p)
5310 r_type = (micromips_reloc_p (r_type)
5311 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5315 /* If we haven't already determined the GOT offset, and we're going
5316 to need it, get it now. */
5319 case R_MIPS16_CALL16:
5320 case R_MIPS16_GOT16:
5323 case R_MIPS_GOT_DISP:
5324 case R_MIPS_GOT_HI16:
5325 case R_MIPS_CALL_HI16:
5326 case R_MIPS_GOT_LO16:
5327 case R_MIPS_CALL_LO16:
5328 case R_MICROMIPS_CALL16:
5329 case R_MICROMIPS_GOT16:
5330 case R_MICROMIPS_GOT_DISP:
5331 case R_MICROMIPS_GOT_HI16:
5332 case R_MICROMIPS_CALL_HI16:
5333 case R_MICROMIPS_GOT_LO16:
5334 case R_MICROMIPS_CALL_LO16:
5336 case R_MIPS_TLS_GOTTPREL:
5337 case R_MIPS_TLS_LDM:
5338 case R_MIPS16_TLS_GD:
5339 case R_MIPS16_TLS_GOTTPREL:
5340 case R_MIPS16_TLS_LDM:
5341 case R_MICROMIPS_TLS_GD:
5342 case R_MICROMIPS_TLS_GOTTPREL:
5343 case R_MICROMIPS_TLS_LDM:
5344 /* Find the index into the GOT where this value is located. */
5345 if (tls_ldm_reloc_p (r_type))
5347 g = mips_elf_local_got_index (abfd, input_bfd, info,
5348 0, 0, NULL, r_type);
5350 return bfd_reloc_outofrange;
5354 /* On VxWorks, CALL relocations should refer to the .got.plt
5355 entry, which is initialized to point at the PLT stub. */
5356 if (htab->is_vxworks
5357 && (call_hi16_reloc_p (r_type)
5358 || call_lo16_reloc_p (r_type)
5359 || call16_reloc_p (r_type)))
5361 BFD_ASSERT (addend == 0);
5362 BFD_ASSERT (h->root.needs_plt);
5363 g = mips_elf_gotplt_index (info, &h->root);
5367 BFD_ASSERT (addend == 0);
5368 g = mips_elf_global_got_index (abfd, info, input_bfd,
5370 if (!TLS_RELOC_P (r_type)
5371 && !elf_hash_table (info)->dynamic_sections_created)
5372 /* This is a static link. We must initialize the GOT entry. */
5373 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5376 else if (!htab->is_vxworks
5377 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5378 /* The calculation below does not involve "g". */
5382 g = mips_elf_local_got_index (abfd, input_bfd, info,
5383 symbol + addend, r_symndx, h, r_type);
5385 return bfd_reloc_outofrange;
5388 /* Convert GOT indices to actual offsets. */
5389 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5393 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5394 symbols are resolved by the loader. Add them to .rela.dyn. */
5395 if (h != NULL && is_gott_symbol (info, &h->root))
5397 Elf_Internal_Rela outrel;
5401 s = mips_elf_rel_dyn_section (info, FALSE);
5402 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5404 outrel.r_offset = (input_section->output_section->vma
5405 + input_section->output_offset
5406 + relocation->r_offset);
5407 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5408 outrel.r_addend = addend;
5409 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5411 /* If we've written this relocation for a readonly section,
5412 we need to set DF_TEXTREL again, so that we do not delete the
5414 if (MIPS_ELF_READONLY_SECTION (input_section))
5415 info->flags |= DF_TEXTREL;
5418 return bfd_reloc_ok;
5421 /* Figure out what kind of relocation is being performed. */
5425 return bfd_reloc_continue;
5428 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5429 overflowed_p = mips_elf_overflow_p (value, 16);
5436 || (htab->root.dynamic_sections_created
5438 && h->root.def_dynamic
5439 && !h->root.def_regular
5440 && !h->has_static_relocs))
5441 && r_symndx != STN_UNDEF
5443 || h->root.root.type != bfd_link_hash_undefweak
5444 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5445 && (input_section->flags & SEC_ALLOC) != 0)
5447 /* If we're creating a shared library, then we can't know
5448 where the symbol will end up. So, we create a relocation
5449 record in the output, and leave the job up to the dynamic
5450 linker. We must do the same for executable references to
5451 shared library symbols, unless we've decided to use copy
5452 relocs or PLTs instead. */
5454 if (!mips_elf_create_dynamic_relocation (abfd,
5462 return bfd_reloc_undefined;
5466 if (r_type != R_MIPS_REL32)
5467 value = symbol + addend;
5471 value &= howto->dst_mask;
5475 value = symbol + addend - p;
5476 value &= howto->dst_mask;
5480 /* The calculation for R_MIPS16_26 is just the same as for an
5481 R_MIPS_26. It's only the storage of the relocated field into
5482 the output file that's different. That's handled in
5483 mips_elf_perform_relocation. So, we just fall through to the
5484 R_MIPS_26 case here. */
5486 case R_MICROMIPS_26_S1:
5490 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5491 the correct ISA mode selector and bit 1 must be 0. */
5492 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5493 return bfd_reloc_outofrange;
5495 /* Shift is 2, unusually, for microMIPS JALX. */
5496 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5499 value = addend | ((p + 4) & (0xfc000000 << shift));
5501 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5502 value = (value + symbol) >> shift;
5503 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5504 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5505 value &= howto->dst_mask;
5509 case R_MIPS_TLS_DTPREL_HI16:
5510 case R_MIPS16_TLS_DTPREL_HI16:
5511 case R_MICROMIPS_TLS_DTPREL_HI16:
5512 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5516 case R_MIPS_TLS_DTPREL_LO16:
5517 case R_MIPS_TLS_DTPREL32:
5518 case R_MIPS_TLS_DTPREL64:
5519 case R_MIPS16_TLS_DTPREL_LO16:
5520 case R_MICROMIPS_TLS_DTPREL_LO16:
5521 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5524 case R_MIPS_TLS_TPREL_HI16:
5525 case R_MIPS16_TLS_TPREL_HI16:
5526 case R_MICROMIPS_TLS_TPREL_HI16:
5527 value = (mips_elf_high (addend + symbol - tprel_base (info))
5531 case R_MIPS_TLS_TPREL_LO16:
5532 case R_MIPS_TLS_TPREL32:
5533 case R_MIPS_TLS_TPREL64:
5534 case R_MIPS16_TLS_TPREL_LO16:
5535 case R_MICROMIPS_TLS_TPREL_LO16:
5536 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5541 case R_MICROMIPS_HI16:
5544 value = mips_elf_high (addend + symbol);
5545 value &= howto->dst_mask;
5549 /* For MIPS16 ABI code we generate this sequence
5550 0: li $v0,%hi(_gp_disp)
5551 4: addiupc $v1,%lo(_gp_disp)
5555 So the offsets of hi and lo relocs are the same, but the
5556 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5557 ADDIUPC clears the low two bits of the instruction address,
5558 so the base is ($t9 + 4) & ~3. */
5559 if (r_type == R_MIPS16_HI16)
5560 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5561 /* The microMIPS .cpload sequence uses the same assembly
5562 instructions as the traditional psABI version, but the
5563 incoming $t9 has the low bit set. */
5564 else if (r_type == R_MICROMIPS_HI16)
5565 value = mips_elf_high (addend + gp - p - 1);
5567 value = mips_elf_high (addend + gp - p);
5568 overflowed_p = mips_elf_overflow_p (value, 16);
5574 case R_MICROMIPS_LO16:
5575 case R_MICROMIPS_HI0_LO16:
5577 value = (symbol + addend) & howto->dst_mask;
5580 /* See the comment for R_MIPS16_HI16 above for the reason
5581 for this conditional. */
5582 if (r_type == R_MIPS16_LO16)
5583 value = addend + gp - (p & ~(bfd_vma) 0x3);
5584 else if (r_type == R_MICROMIPS_LO16
5585 || r_type == R_MICROMIPS_HI0_LO16)
5586 value = addend + gp - p + 3;
5588 value = addend + gp - p + 4;
5589 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5590 for overflow. But, on, say, IRIX5, relocations against
5591 _gp_disp are normally generated from the .cpload
5592 pseudo-op. It generates code that normally looks like
5595 lui $gp,%hi(_gp_disp)
5596 addiu $gp,$gp,%lo(_gp_disp)
5599 Here $t9 holds the address of the function being called,
5600 as required by the MIPS ELF ABI. The R_MIPS_LO16
5601 relocation can easily overflow in this situation, but the
5602 R_MIPS_HI16 relocation will handle the overflow.
5603 Therefore, we consider this a bug in the MIPS ABI, and do
5604 not check for overflow here. */
5608 case R_MIPS_LITERAL:
5609 case R_MICROMIPS_LITERAL:
5610 /* Because we don't merge literal sections, we can handle this
5611 just like R_MIPS_GPREL16. In the long run, we should merge
5612 shared literals, and then we will need to additional work
5617 case R_MIPS16_GPREL:
5618 /* The R_MIPS16_GPREL performs the same calculation as
5619 R_MIPS_GPREL16, but stores the relocated bits in a different
5620 order. We don't need to do anything special here; the
5621 differences are handled in mips_elf_perform_relocation. */
5622 case R_MIPS_GPREL16:
5623 case R_MICROMIPS_GPREL7_S2:
5624 case R_MICROMIPS_GPREL16:
5625 /* Only sign-extend the addend if it was extracted from the
5626 instruction. If the addend was separate, leave it alone,
5627 otherwise we may lose significant bits. */
5628 if (howto->partial_inplace)
5629 addend = _bfd_mips_elf_sign_extend (addend, 16);
5630 value = symbol + addend - gp;
5631 /* If the symbol was local, any earlier relocatable links will
5632 have adjusted its addend with the gp offset, so compensate
5633 for that now. Don't do it for symbols forced local in this
5634 link, though, since they won't have had the gp offset applied
5638 overflowed_p = mips_elf_overflow_p (value, 16);
5641 case R_MIPS16_GOT16:
5642 case R_MIPS16_CALL16:
5645 case R_MICROMIPS_GOT16:
5646 case R_MICROMIPS_CALL16:
5647 /* VxWorks does not have separate local and global semantics for
5648 R_MIPS*_GOT16; every relocation evaluates to "G". */
5649 if (!htab->is_vxworks && local_p)
5651 value = mips_elf_got16_entry (abfd, input_bfd, info,
5652 symbol + addend, !was_local_p);
5653 if (value == MINUS_ONE)
5654 return bfd_reloc_outofrange;
5656 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5657 overflowed_p = mips_elf_overflow_p (value, 16);
5664 case R_MIPS_TLS_GOTTPREL:
5665 case R_MIPS_TLS_LDM:
5666 case R_MIPS_GOT_DISP:
5667 case R_MIPS16_TLS_GD:
5668 case R_MIPS16_TLS_GOTTPREL:
5669 case R_MIPS16_TLS_LDM:
5670 case R_MICROMIPS_TLS_GD:
5671 case R_MICROMIPS_TLS_GOTTPREL:
5672 case R_MICROMIPS_TLS_LDM:
5673 case R_MICROMIPS_GOT_DISP:
5675 overflowed_p = mips_elf_overflow_p (value, 16);
5678 case R_MIPS_GPREL32:
5679 value = (addend + symbol + gp0 - gp);
5681 value &= howto->dst_mask;
5685 case R_MIPS_GNU_REL16_S2:
5686 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5687 overflowed_p = mips_elf_overflow_p (value, 18);
5688 value >>= howto->rightshift;
5689 value &= howto->dst_mask;
5692 case R_MICROMIPS_PC7_S1:
5693 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5694 overflowed_p = mips_elf_overflow_p (value, 8);
5695 value >>= howto->rightshift;
5696 value &= howto->dst_mask;
5699 case R_MICROMIPS_PC10_S1:
5700 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5701 overflowed_p = mips_elf_overflow_p (value, 11);
5702 value >>= howto->rightshift;
5703 value &= howto->dst_mask;
5706 case R_MICROMIPS_PC16_S1:
5707 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5708 overflowed_p = mips_elf_overflow_p (value, 17);
5709 value >>= howto->rightshift;
5710 value &= howto->dst_mask;
5713 case R_MICROMIPS_PC23_S2:
5714 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5715 overflowed_p = mips_elf_overflow_p (value, 25);
5716 value >>= howto->rightshift;
5717 value &= howto->dst_mask;
5720 case R_MIPS_GOT_HI16:
5721 case R_MIPS_CALL_HI16:
5722 case R_MICROMIPS_GOT_HI16:
5723 case R_MICROMIPS_CALL_HI16:
5724 /* We're allowed to handle these two relocations identically.
5725 The dynamic linker is allowed to handle the CALL relocations
5726 differently by creating a lazy evaluation stub. */
5728 value = mips_elf_high (value);
5729 value &= howto->dst_mask;
5732 case R_MIPS_GOT_LO16:
5733 case R_MIPS_CALL_LO16:
5734 case R_MICROMIPS_GOT_LO16:
5735 case R_MICROMIPS_CALL_LO16:
5736 value = g & howto->dst_mask;
5739 case R_MIPS_GOT_PAGE:
5740 case R_MICROMIPS_GOT_PAGE:
5741 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5742 if (value == MINUS_ONE)
5743 return bfd_reloc_outofrange;
5744 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5745 overflowed_p = mips_elf_overflow_p (value, 16);
5748 case R_MIPS_GOT_OFST:
5749 case R_MICROMIPS_GOT_OFST:
5751 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5754 overflowed_p = mips_elf_overflow_p (value, 16);
5758 case R_MICROMIPS_SUB:
5759 value = symbol - addend;
5760 value &= howto->dst_mask;
5764 case R_MICROMIPS_HIGHER:
5765 value = mips_elf_higher (addend + symbol);
5766 value &= howto->dst_mask;
5769 case R_MIPS_HIGHEST:
5770 case R_MICROMIPS_HIGHEST:
5771 value = mips_elf_highest (addend + symbol);
5772 value &= howto->dst_mask;
5775 case R_MIPS_SCN_DISP:
5776 case R_MICROMIPS_SCN_DISP:
5777 value = symbol + addend - sec->output_offset;
5778 value &= howto->dst_mask;
5782 case R_MICROMIPS_JALR:
5783 /* This relocation is only a hint. In some cases, we optimize
5784 it into a bal instruction. But we don't try to optimize
5785 when the symbol does not resolve locally. */
5786 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5787 return bfd_reloc_continue;
5788 value = symbol + addend;
5792 case R_MIPS_GNU_VTINHERIT:
5793 case R_MIPS_GNU_VTENTRY:
5794 /* We don't do anything with these at present. */
5795 return bfd_reloc_continue;
5798 /* An unrecognized relocation type. */
5799 return bfd_reloc_notsupported;
5802 /* Store the VALUE for our caller. */
5804 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5807 /* Obtain the field relocated by RELOCATION. */
5810 mips_elf_obtain_contents (reloc_howto_type *howto,
5811 const Elf_Internal_Rela *relocation,
5812 bfd *input_bfd, bfd_byte *contents)
5815 bfd_byte *location = contents + relocation->r_offset;
5817 /* Obtain the bytes. */
5818 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5823 /* It has been determined that the result of the RELOCATION is the
5824 VALUE. Use HOWTO to place VALUE into the output file at the
5825 appropriate position. The SECTION is the section to which the
5827 CROSS_MODE_JUMP_P is true if the relocation field
5828 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5830 Returns FALSE if anything goes wrong. */
5833 mips_elf_perform_relocation (struct bfd_link_info *info,
5834 reloc_howto_type *howto,
5835 const Elf_Internal_Rela *relocation,
5836 bfd_vma value, bfd *input_bfd,
5837 asection *input_section, bfd_byte *contents,
5838 bfd_boolean cross_mode_jump_p)
5842 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5844 /* Figure out where the relocation is occurring. */
5845 location = contents + relocation->r_offset;
5847 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5849 /* Obtain the current value. */
5850 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5852 /* Clear the field we are setting. */
5853 x &= ~howto->dst_mask;
5855 /* Set the field. */
5856 x |= (value & howto->dst_mask);
5858 /* If required, turn JAL into JALX. */
5859 if (cross_mode_jump_p && jal_reloc_p (r_type))
5862 bfd_vma opcode = x >> 26;
5863 bfd_vma jalx_opcode;
5865 /* Check to see if the opcode is already JAL or JALX. */
5866 if (r_type == R_MIPS16_26)
5868 ok = ((opcode == 0x6) || (opcode == 0x7));
5871 else if (r_type == R_MICROMIPS_26_S1)
5873 ok = ((opcode == 0x3d) || (opcode == 0x3c));
5878 ok = ((opcode == 0x3) || (opcode == 0x1d));
5882 /* If the opcode is not JAL or JALX, there's a problem. We cannot
5883 convert J or JALS to JALX. */
5886 (*_bfd_error_handler)
5887 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
5890 (unsigned long) relocation->r_offset);
5891 bfd_set_error (bfd_error_bad_value);
5895 /* Make this the JALX opcode. */
5896 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5899 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5901 if (!info->relocatable
5902 && !cross_mode_jump_p
5903 && ((JAL_TO_BAL_P (input_bfd)
5904 && r_type == R_MIPS_26
5905 && (x >> 26) == 0x3) /* jal addr */
5906 || (JALR_TO_BAL_P (input_bfd)
5907 && r_type == R_MIPS_JALR
5908 && x == 0x0320f809) /* jalr t9 */
5909 || (JR_TO_B_P (input_bfd)
5910 && r_type == R_MIPS_JALR
5911 && x == 0x03200008))) /* jr t9 */
5917 addr = (input_section->output_section->vma
5918 + input_section->output_offset
5919 + relocation->r_offset
5921 if (r_type == R_MIPS_26)
5922 dest = (value << 2) | ((addr >> 28) << 28);
5926 if (off <= 0x1ffff && off >= -0x20000)
5928 if (x == 0x03200008) /* jr t9 */
5929 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5931 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5935 /* Put the value into the output. */
5936 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5938 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
5944 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5945 is the original relocation, which is now being transformed into a
5946 dynamic relocation. The ADDENDP is adjusted if necessary; the
5947 caller should store the result in place of the original addend. */
5950 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5951 struct bfd_link_info *info,
5952 const Elf_Internal_Rela *rel,
5953 struct mips_elf_link_hash_entry *h,
5954 asection *sec, bfd_vma symbol,
5955 bfd_vma *addendp, asection *input_section)
5957 Elf_Internal_Rela outrel[3];
5962 bfd_boolean defined_p;
5963 struct mips_elf_link_hash_table *htab;
5965 htab = mips_elf_hash_table (info);
5966 BFD_ASSERT (htab != NULL);
5968 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5969 dynobj = elf_hash_table (info)->dynobj;
5970 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5971 BFD_ASSERT (sreloc != NULL);
5972 BFD_ASSERT (sreloc->contents != NULL);
5973 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5976 outrel[0].r_offset =
5977 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5978 if (ABI_64_P (output_bfd))
5980 outrel[1].r_offset =
5981 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5982 outrel[2].r_offset =
5983 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5986 if (outrel[0].r_offset == MINUS_ONE)
5987 /* The relocation field has been deleted. */
5990 if (outrel[0].r_offset == MINUS_TWO)
5992 /* The relocation field has been converted into a relative value of
5993 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5994 the field to be fully relocated, so add in the symbol's value. */
5999 /* We must now calculate the dynamic symbol table index to use
6000 in the relocation. */
6001 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6003 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6004 indx = h->root.dynindx;
6005 if (SGI_COMPAT (output_bfd))
6006 defined_p = h->root.def_regular;
6008 /* ??? glibc's ld.so just adds the final GOT entry to the
6009 relocation field. It therefore treats relocs against
6010 defined symbols in the same way as relocs against
6011 undefined symbols. */
6016 if (sec != NULL && bfd_is_abs_section (sec))
6018 else if (sec == NULL || sec->owner == NULL)
6020 bfd_set_error (bfd_error_bad_value);
6025 indx = elf_section_data (sec->output_section)->dynindx;
6028 asection *osec = htab->root.text_index_section;
6029 indx = elf_section_data (osec)->dynindx;
6035 /* Instead of generating a relocation using the section
6036 symbol, we may as well make it a fully relative
6037 relocation. We want to avoid generating relocations to
6038 local symbols because we used to generate them
6039 incorrectly, without adding the original symbol value,
6040 which is mandated by the ABI for section symbols. In
6041 order to give dynamic loaders and applications time to
6042 phase out the incorrect use, we refrain from emitting
6043 section-relative relocations. It's not like they're
6044 useful, after all. This should be a bit more efficient
6046 /* ??? Although this behavior is compatible with glibc's ld.so,
6047 the ABI says that relocations against STN_UNDEF should have
6048 a symbol value of 0. Irix rld honors this, so relocations
6049 against STN_UNDEF have no effect. */
6050 if (!SGI_COMPAT (output_bfd))
6055 /* If the relocation was previously an absolute relocation and
6056 this symbol will not be referred to by the relocation, we must
6057 adjust it by the value we give it in the dynamic symbol table.
6058 Otherwise leave the job up to the dynamic linker. */
6059 if (defined_p && r_type != R_MIPS_REL32)
6062 if (htab->is_vxworks)
6063 /* VxWorks uses non-relative relocations for this. */
6064 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6066 /* The relocation is always an REL32 relocation because we don't
6067 know where the shared library will wind up at load-time. */
6068 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6071 /* For strict adherence to the ABI specification, we should
6072 generate a R_MIPS_64 relocation record by itself before the
6073 _REL32/_64 record as well, such that the addend is read in as
6074 a 64-bit value (REL32 is a 32-bit relocation, after all).
6075 However, since none of the existing ELF64 MIPS dynamic
6076 loaders seems to care, we don't waste space with these
6077 artificial relocations. If this turns out to not be true,
6078 mips_elf_allocate_dynamic_relocation() should be tweaked so
6079 as to make room for a pair of dynamic relocations per
6080 invocation if ABI_64_P, and here we should generate an
6081 additional relocation record with R_MIPS_64 by itself for a
6082 NULL symbol before this relocation record. */
6083 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6084 ABI_64_P (output_bfd)
6087 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6089 /* Adjust the output offset of the relocation to reference the
6090 correct location in the output file. */
6091 outrel[0].r_offset += (input_section->output_section->vma
6092 + input_section->output_offset);
6093 outrel[1].r_offset += (input_section->output_section->vma
6094 + input_section->output_offset);
6095 outrel[2].r_offset += (input_section->output_section->vma
6096 + input_section->output_offset);
6098 /* Put the relocation back out. We have to use the special
6099 relocation outputter in the 64-bit case since the 64-bit
6100 relocation format is non-standard. */
6101 if (ABI_64_P (output_bfd))
6103 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6104 (output_bfd, &outrel[0],
6106 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6108 else if (htab->is_vxworks)
6110 /* VxWorks uses RELA rather than REL dynamic relocations. */
6111 outrel[0].r_addend = *addendp;
6112 bfd_elf32_swap_reloca_out
6113 (output_bfd, &outrel[0],
6115 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6118 bfd_elf32_swap_reloc_out
6119 (output_bfd, &outrel[0],
6120 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6122 /* We've now added another relocation. */
6123 ++sreloc->reloc_count;
6125 /* Make sure the output section is writable. The dynamic linker
6126 will be writing to it. */
6127 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6130 /* On IRIX5, make an entry of compact relocation info. */
6131 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6133 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6138 Elf32_crinfo cptrel;
6140 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6141 cptrel.vaddr = (rel->r_offset
6142 + input_section->output_section->vma
6143 + input_section->output_offset);
6144 if (r_type == R_MIPS_REL32)
6145 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6147 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6148 mips_elf_set_cr_dist2to (cptrel, 0);
6149 cptrel.konst = *addendp;
6151 cr = (scpt->contents
6152 + sizeof (Elf32_External_compact_rel));
6153 mips_elf_set_cr_relvaddr (cptrel, 0);
6154 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6155 ((Elf32_External_crinfo *) cr
6156 + scpt->reloc_count));
6157 ++scpt->reloc_count;
6161 /* If we've written this relocation for a readonly section,
6162 we need to set DF_TEXTREL again, so that we do not delete the
6164 if (MIPS_ELF_READONLY_SECTION (input_section))
6165 info->flags |= DF_TEXTREL;
6170 /* Return the MACH for a MIPS e_flags value. */
6173 _bfd_elf_mips_mach (flagword flags)
6175 switch (flags & EF_MIPS_MACH)
6177 case E_MIPS_MACH_3900:
6178 return bfd_mach_mips3900;
6180 case E_MIPS_MACH_4010:
6181 return bfd_mach_mips4010;
6183 case E_MIPS_MACH_4100:
6184 return bfd_mach_mips4100;
6186 case E_MIPS_MACH_4111:
6187 return bfd_mach_mips4111;
6189 case E_MIPS_MACH_4120:
6190 return bfd_mach_mips4120;
6192 case E_MIPS_MACH_4650:
6193 return bfd_mach_mips4650;
6195 case E_MIPS_MACH_5400:
6196 return bfd_mach_mips5400;
6198 case E_MIPS_MACH_5500:
6199 return bfd_mach_mips5500;
6201 case E_MIPS_MACH_5900:
6202 return bfd_mach_mips5900;
6204 case E_MIPS_MACH_9000:
6205 return bfd_mach_mips9000;
6207 case E_MIPS_MACH_SB1:
6208 return bfd_mach_mips_sb1;
6210 case E_MIPS_MACH_LS2E:
6211 return bfd_mach_mips_loongson_2e;
6213 case E_MIPS_MACH_LS2F:
6214 return bfd_mach_mips_loongson_2f;
6216 case E_MIPS_MACH_LS3A:
6217 return bfd_mach_mips_loongson_3a;
6219 case E_MIPS_MACH_OCTEON2:
6220 return bfd_mach_mips_octeon2;
6222 case E_MIPS_MACH_OCTEON:
6223 return bfd_mach_mips_octeon;
6225 case E_MIPS_MACH_XLR:
6226 return bfd_mach_mips_xlr;
6229 switch (flags & EF_MIPS_ARCH)
6233 return bfd_mach_mips3000;
6236 return bfd_mach_mips6000;
6239 return bfd_mach_mips4000;
6242 return bfd_mach_mips8000;
6245 return bfd_mach_mips5;
6247 case E_MIPS_ARCH_32:
6248 return bfd_mach_mipsisa32;
6250 case E_MIPS_ARCH_64:
6251 return bfd_mach_mipsisa64;
6253 case E_MIPS_ARCH_32R2:
6254 return bfd_mach_mipsisa32r2;
6256 case E_MIPS_ARCH_64R2:
6257 return bfd_mach_mipsisa64r2;
6264 /* Return printable name for ABI. */
6266 static INLINE char *
6267 elf_mips_abi_name (bfd *abfd)
6271 flags = elf_elfheader (abfd)->e_flags;
6272 switch (flags & EF_MIPS_ABI)
6275 if (ABI_N32_P (abfd))
6277 else if (ABI_64_P (abfd))
6281 case E_MIPS_ABI_O32:
6283 case E_MIPS_ABI_O64:
6285 case E_MIPS_ABI_EABI32:
6287 case E_MIPS_ABI_EABI64:
6290 return "unknown abi";
6294 /* MIPS ELF uses two common sections. One is the usual one, and the
6295 other is for small objects. All the small objects are kept
6296 together, and then referenced via the gp pointer, which yields
6297 faster assembler code. This is what we use for the small common
6298 section. This approach is copied from ecoff.c. */
6299 static asection mips_elf_scom_section;
6300 static asymbol mips_elf_scom_symbol;
6301 static asymbol *mips_elf_scom_symbol_ptr;
6303 /* MIPS ELF also uses an acommon section, which represents an
6304 allocated common symbol which may be overridden by a
6305 definition in a shared library. */
6306 static asection mips_elf_acom_section;
6307 static asymbol mips_elf_acom_symbol;
6308 static asymbol *mips_elf_acom_symbol_ptr;
6310 /* This is used for both the 32-bit and the 64-bit ABI. */
6313 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6315 elf_symbol_type *elfsym;
6317 /* Handle the special MIPS section numbers that a symbol may use. */
6318 elfsym = (elf_symbol_type *) asym;
6319 switch (elfsym->internal_elf_sym.st_shndx)
6321 case SHN_MIPS_ACOMMON:
6322 /* This section is used in a dynamically linked executable file.
6323 It is an allocated common section. The dynamic linker can
6324 either resolve these symbols to something in a shared
6325 library, or it can just leave them here. For our purposes,
6326 we can consider these symbols to be in a new section. */
6327 if (mips_elf_acom_section.name == NULL)
6329 /* Initialize the acommon section. */
6330 mips_elf_acom_section.name = ".acommon";
6331 mips_elf_acom_section.flags = SEC_ALLOC;
6332 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6333 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6334 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6335 mips_elf_acom_symbol.name = ".acommon";
6336 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6337 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6338 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6340 asym->section = &mips_elf_acom_section;
6344 /* Common symbols less than the GP size are automatically
6345 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6346 if (asym->value > elf_gp_size (abfd)
6347 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6348 || IRIX_COMPAT (abfd) == ict_irix6)
6351 case SHN_MIPS_SCOMMON:
6352 if (mips_elf_scom_section.name == NULL)
6354 /* Initialize the small common section. */
6355 mips_elf_scom_section.name = ".scommon";
6356 mips_elf_scom_section.flags = SEC_IS_COMMON;
6357 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6358 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6359 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6360 mips_elf_scom_symbol.name = ".scommon";
6361 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6362 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6363 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6365 asym->section = &mips_elf_scom_section;
6366 asym->value = elfsym->internal_elf_sym.st_size;
6369 case SHN_MIPS_SUNDEFINED:
6370 asym->section = bfd_und_section_ptr;
6375 asection *section = bfd_get_section_by_name (abfd, ".text");
6377 if (section != NULL)
6379 asym->section = section;
6380 /* MIPS_TEXT is a bit special, the address is not an offset
6381 to the base of the .text section. So substract the section
6382 base address to make it an offset. */
6383 asym->value -= section->vma;
6390 asection *section = bfd_get_section_by_name (abfd, ".data");
6392 if (section != NULL)
6394 asym->section = section;
6395 /* MIPS_DATA is a bit special, the address is not an offset
6396 to the base of the .data section. So substract the section
6397 base address to make it an offset. */
6398 asym->value -= section->vma;
6404 /* If this is an odd-valued function symbol, assume it's a MIPS16
6405 or microMIPS one. */
6406 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6407 && (asym->value & 1) != 0)
6410 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
6411 elfsym->internal_elf_sym.st_other
6412 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6414 elfsym->internal_elf_sym.st_other
6415 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6419 /* Implement elf_backend_eh_frame_address_size. This differs from
6420 the default in the way it handles EABI64.
6422 EABI64 was originally specified as an LP64 ABI, and that is what
6423 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6424 historically accepted the combination of -mabi=eabi and -mlong32,
6425 and this ILP32 variation has become semi-official over time.
6426 Both forms use elf32 and have pointer-sized FDE addresses.
6428 If an EABI object was generated by GCC 4.0 or above, it will have
6429 an empty .gcc_compiled_longXX section, where XX is the size of longs
6430 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6431 have no special marking to distinguish them from LP64 objects.
6433 We don't want users of the official LP64 ABI to be punished for the
6434 existence of the ILP32 variant, but at the same time, we don't want
6435 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6436 We therefore take the following approach:
6438 - If ABFD contains a .gcc_compiled_longXX section, use it to
6439 determine the pointer size.
6441 - Otherwise check the type of the first relocation. Assume that
6442 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6446 The second check is enough to detect LP64 objects generated by pre-4.0
6447 compilers because, in the kind of output generated by those compilers,
6448 the first relocation will be associated with either a CIE personality
6449 routine or an FDE start address. Furthermore, the compilers never
6450 used a special (non-pointer) encoding for this ABI.
6452 Checking the relocation type should also be safe because there is no
6453 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6457 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6459 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6461 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6463 bfd_boolean long32_p, long64_p;
6465 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6466 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6467 if (long32_p && long64_p)
6474 if (sec->reloc_count > 0
6475 && elf_section_data (sec)->relocs != NULL
6476 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6485 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6486 relocations against two unnamed section symbols to resolve to the
6487 same address. For example, if we have code like:
6489 lw $4,%got_disp(.data)($gp)
6490 lw $25,%got_disp(.text)($gp)
6493 then the linker will resolve both relocations to .data and the program
6494 will jump there rather than to .text.
6496 We can work around this problem by giving names to local section symbols.
6497 This is also what the MIPSpro tools do. */
6500 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6502 return SGI_COMPAT (abfd);
6505 /* Work over a section just before writing it out. This routine is
6506 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6507 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6511 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6513 if (hdr->sh_type == SHT_MIPS_REGINFO
6514 && hdr->sh_size > 0)
6518 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6519 BFD_ASSERT (hdr->contents == NULL);
6522 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6525 H_PUT_32 (abfd, elf_gp (abfd), buf);
6526 if (bfd_bwrite (buf, 4, abfd) != 4)
6530 if (hdr->sh_type == SHT_MIPS_OPTIONS
6531 && hdr->bfd_section != NULL
6532 && mips_elf_section_data (hdr->bfd_section) != NULL
6533 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6535 bfd_byte *contents, *l, *lend;
6537 /* We stored the section contents in the tdata field in the
6538 set_section_contents routine. We save the section contents
6539 so that we don't have to read them again.
6540 At this point we know that elf_gp is set, so we can look
6541 through the section contents to see if there is an
6542 ODK_REGINFO structure. */
6544 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6546 lend = contents + hdr->sh_size;
6547 while (l + sizeof (Elf_External_Options) <= lend)
6549 Elf_Internal_Options intopt;
6551 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6553 if (intopt.size < sizeof (Elf_External_Options))
6555 (*_bfd_error_handler)
6556 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6557 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6560 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6567 + sizeof (Elf_External_Options)
6568 + (sizeof (Elf64_External_RegInfo) - 8)),
6571 H_PUT_64 (abfd, elf_gp (abfd), buf);
6572 if (bfd_bwrite (buf, 8, abfd) != 8)
6575 else if (intopt.kind == ODK_REGINFO)
6582 + sizeof (Elf_External_Options)
6583 + (sizeof (Elf32_External_RegInfo) - 4)),
6586 H_PUT_32 (abfd, elf_gp (abfd), buf);
6587 if (bfd_bwrite (buf, 4, abfd) != 4)
6594 if (hdr->bfd_section != NULL)
6596 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6598 /* .sbss is not handled specially here because the GNU/Linux
6599 prelinker can convert .sbss from NOBITS to PROGBITS and
6600 changing it back to NOBITS breaks the binary. The entry in
6601 _bfd_mips_elf_special_sections will ensure the correct flags
6602 are set on .sbss if BFD creates it without reading it from an
6603 input file, and without special handling here the flags set
6604 on it in an input file will be followed. */
6605 if (strcmp (name, ".sdata") == 0
6606 || strcmp (name, ".lit8") == 0
6607 || strcmp (name, ".lit4") == 0)
6609 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6610 hdr->sh_type = SHT_PROGBITS;
6612 else if (strcmp (name, ".srdata") == 0)
6614 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6615 hdr->sh_type = SHT_PROGBITS;
6617 else if (strcmp (name, ".compact_rel") == 0)
6620 hdr->sh_type = SHT_PROGBITS;
6622 else if (strcmp (name, ".rtproc") == 0)
6624 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6626 unsigned int adjust;
6628 adjust = hdr->sh_size % hdr->sh_addralign;
6630 hdr->sh_size += hdr->sh_addralign - adjust;
6638 /* Handle a MIPS specific section when reading an object file. This
6639 is called when elfcode.h finds a section with an unknown type.
6640 This routine supports both the 32-bit and 64-bit ELF ABI.
6642 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6646 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6647 Elf_Internal_Shdr *hdr,
6653 /* There ought to be a place to keep ELF backend specific flags, but
6654 at the moment there isn't one. We just keep track of the
6655 sections by their name, instead. Fortunately, the ABI gives
6656 suggested names for all the MIPS specific sections, so we will
6657 probably get away with this. */
6658 switch (hdr->sh_type)
6660 case SHT_MIPS_LIBLIST:
6661 if (strcmp (name, ".liblist") != 0)
6665 if (strcmp (name, ".msym") != 0)
6668 case SHT_MIPS_CONFLICT:
6669 if (strcmp (name, ".conflict") != 0)
6672 case SHT_MIPS_GPTAB:
6673 if (! CONST_STRNEQ (name, ".gptab."))
6676 case SHT_MIPS_UCODE:
6677 if (strcmp (name, ".ucode") != 0)
6680 case SHT_MIPS_DEBUG:
6681 if (strcmp (name, ".mdebug") != 0)
6683 flags = SEC_DEBUGGING;
6685 case SHT_MIPS_REGINFO:
6686 if (strcmp (name, ".reginfo") != 0
6687 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6689 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6691 case SHT_MIPS_IFACE:
6692 if (strcmp (name, ".MIPS.interfaces") != 0)
6695 case SHT_MIPS_CONTENT:
6696 if (! CONST_STRNEQ (name, ".MIPS.content"))
6699 case SHT_MIPS_OPTIONS:
6700 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6703 case SHT_MIPS_DWARF:
6704 if (! CONST_STRNEQ (name, ".debug_")
6705 && ! CONST_STRNEQ (name, ".zdebug_"))
6708 case SHT_MIPS_SYMBOL_LIB:
6709 if (strcmp (name, ".MIPS.symlib") != 0)
6712 case SHT_MIPS_EVENTS:
6713 if (! CONST_STRNEQ (name, ".MIPS.events")
6714 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6721 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6726 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6727 (bfd_get_section_flags (abfd,
6733 /* FIXME: We should record sh_info for a .gptab section. */
6735 /* For a .reginfo section, set the gp value in the tdata information
6736 from the contents of this section. We need the gp value while
6737 processing relocs, so we just get it now. The .reginfo section
6738 is not used in the 64-bit MIPS ELF ABI. */
6739 if (hdr->sh_type == SHT_MIPS_REGINFO)
6741 Elf32_External_RegInfo ext;
6744 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6745 &ext, 0, sizeof ext))
6747 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6748 elf_gp (abfd) = s.ri_gp_value;
6751 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6752 set the gp value based on what we find. We may see both
6753 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6754 they should agree. */
6755 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6757 bfd_byte *contents, *l, *lend;
6759 contents = bfd_malloc (hdr->sh_size);
6760 if (contents == NULL)
6762 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6769 lend = contents + hdr->sh_size;
6770 while (l + sizeof (Elf_External_Options) <= lend)
6772 Elf_Internal_Options intopt;
6774 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6776 if (intopt.size < sizeof (Elf_External_Options))
6778 (*_bfd_error_handler)
6779 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6780 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6783 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6785 Elf64_Internal_RegInfo intreg;
6787 bfd_mips_elf64_swap_reginfo_in
6789 ((Elf64_External_RegInfo *)
6790 (l + sizeof (Elf_External_Options))),
6792 elf_gp (abfd) = intreg.ri_gp_value;
6794 else if (intopt.kind == ODK_REGINFO)
6796 Elf32_RegInfo intreg;
6798 bfd_mips_elf32_swap_reginfo_in
6800 ((Elf32_External_RegInfo *)
6801 (l + sizeof (Elf_External_Options))),
6803 elf_gp (abfd) = intreg.ri_gp_value;
6813 /* Set the correct type for a MIPS ELF section. We do this by the
6814 section name, which is a hack, but ought to work. This routine is
6815 used by both the 32-bit and the 64-bit ABI. */
6818 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6820 const char *name = bfd_get_section_name (abfd, sec);
6822 if (strcmp (name, ".liblist") == 0)
6824 hdr->sh_type = SHT_MIPS_LIBLIST;
6825 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6826 /* The sh_link field is set in final_write_processing. */
6828 else if (strcmp (name, ".conflict") == 0)
6829 hdr->sh_type = SHT_MIPS_CONFLICT;
6830 else if (CONST_STRNEQ (name, ".gptab."))
6832 hdr->sh_type = SHT_MIPS_GPTAB;
6833 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6834 /* The sh_info field is set in final_write_processing. */
6836 else if (strcmp (name, ".ucode") == 0)
6837 hdr->sh_type = SHT_MIPS_UCODE;
6838 else if (strcmp (name, ".mdebug") == 0)
6840 hdr->sh_type = SHT_MIPS_DEBUG;
6841 /* In a shared object on IRIX 5.3, the .mdebug section has an
6842 entsize of 0. FIXME: Does this matter? */
6843 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6844 hdr->sh_entsize = 0;
6846 hdr->sh_entsize = 1;
6848 else if (strcmp (name, ".reginfo") == 0)
6850 hdr->sh_type = SHT_MIPS_REGINFO;
6851 /* In a shared object on IRIX 5.3, the .reginfo section has an
6852 entsize of 0x18. FIXME: Does this matter? */
6853 if (SGI_COMPAT (abfd))
6855 if ((abfd->flags & DYNAMIC) != 0)
6856 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6858 hdr->sh_entsize = 1;
6861 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6863 else if (SGI_COMPAT (abfd)
6864 && (strcmp (name, ".hash") == 0
6865 || strcmp (name, ".dynamic") == 0
6866 || strcmp (name, ".dynstr") == 0))
6868 if (SGI_COMPAT (abfd))
6869 hdr->sh_entsize = 0;
6871 /* This isn't how the IRIX6 linker behaves. */
6872 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6875 else if (strcmp (name, ".got") == 0
6876 || strcmp (name, ".srdata") == 0
6877 || strcmp (name, ".sdata") == 0
6878 || strcmp (name, ".sbss") == 0
6879 || strcmp (name, ".lit4") == 0
6880 || strcmp (name, ".lit8") == 0)
6881 hdr->sh_flags |= SHF_MIPS_GPREL;
6882 else if (strcmp (name, ".MIPS.interfaces") == 0)
6884 hdr->sh_type = SHT_MIPS_IFACE;
6885 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6887 else if (CONST_STRNEQ (name, ".MIPS.content"))
6889 hdr->sh_type = SHT_MIPS_CONTENT;
6890 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6891 /* The sh_info field is set in final_write_processing. */
6893 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6895 hdr->sh_type = SHT_MIPS_OPTIONS;
6896 hdr->sh_entsize = 1;
6897 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6899 else if (CONST_STRNEQ (name, ".debug_")
6900 || CONST_STRNEQ (name, ".zdebug_"))
6902 hdr->sh_type = SHT_MIPS_DWARF;
6904 /* Irix facilities such as libexc expect a single .debug_frame
6905 per executable, the system ones have NOSTRIP set and the linker
6906 doesn't merge sections with different flags so ... */
6907 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6908 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6910 else if (strcmp (name, ".MIPS.symlib") == 0)
6912 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6913 /* The sh_link and sh_info fields are set in
6914 final_write_processing. */
6916 else if (CONST_STRNEQ (name, ".MIPS.events")
6917 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6919 hdr->sh_type = SHT_MIPS_EVENTS;
6920 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6921 /* The sh_link field is set in final_write_processing. */
6923 else if (strcmp (name, ".msym") == 0)
6925 hdr->sh_type = SHT_MIPS_MSYM;
6926 hdr->sh_flags |= SHF_ALLOC;
6927 hdr->sh_entsize = 8;
6930 /* The generic elf_fake_sections will set up REL_HDR using the default
6931 kind of relocations. We used to set up a second header for the
6932 non-default kind of relocations here, but only NewABI would use
6933 these, and the IRIX ld doesn't like resulting empty RELA sections.
6934 Thus we create those header only on demand now. */
6939 /* Given a BFD section, try to locate the corresponding ELF section
6940 index. This is used by both the 32-bit and the 64-bit ABI.
6941 Actually, it's not clear to me that the 64-bit ABI supports these,
6942 but for non-PIC objects we will certainly want support for at least
6943 the .scommon section. */
6946 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6947 asection *sec, int *retval)
6949 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6951 *retval = SHN_MIPS_SCOMMON;
6954 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6956 *retval = SHN_MIPS_ACOMMON;
6962 /* Hook called by the linker routine which adds symbols from an object
6963 file. We must handle the special MIPS section numbers here. */
6966 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6967 Elf_Internal_Sym *sym, const char **namep,
6968 flagword *flagsp ATTRIBUTE_UNUSED,
6969 asection **secp, bfd_vma *valp)
6971 if (SGI_COMPAT (abfd)
6972 && (abfd->flags & DYNAMIC) != 0
6973 && strcmp (*namep, "_rld_new_interface") == 0)
6975 /* Skip IRIX5 rld entry name. */
6980 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6981 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6982 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6983 a magic symbol resolved by the linker, we ignore this bogus definition
6984 of _gp_disp. New ABI objects do not suffer from this problem so this
6985 is not done for them. */
6987 && (sym->st_shndx == SHN_ABS)
6988 && (strcmp (*namep, "_gp_disp") == 0))
6994 switch (sym->st_shndx)
6997 /* Common symbols less than the GP size are automatically
6998 treated as SHN_MIPS_SCOMMON symbols. */
6999 if (sym->st_size > elf_gp_size (abfd)
7000 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7001 || IRIX_COMPAT (abfd) == ict_irix6)
7004 case SHN_MIPS_SCOMMON:
7005 *secp = bfd_make_section_old_way (abfd, ".scommon");
7006 (*secp)->flags |= SEC_IS_COMMON;
7007 *valp = sym->st_size;
7011 /* This section is used in a shared object. */
7012 if (elf_tdata (abfd)->elf_text_section == NULL)
7014 asymbol *elf_text_symbol;
7015 asection *elf_text_section;
7016 bfd_size_type amt = sizeof (asection);
7018 elf_text_section = bfd_zalloc (abfd, amt);
7019 if (elf_text_section == NULL)
7022 amt = sizeof (asymbol);
7023 elf_text_symbol = bfd_zalloc (abfd, amt);
7024 if (elf_text_symbol == NULL)
7027 /* Initialize the section. */
7029 elf_tdata (abfd)->elf_text_section = elf_text_section;
7030 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7032 elf_text_section->symbol = elf_text_symbol;
7033 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
7035 elf_text_section->name = ".text";
7036 elf_text_section->flags = SEC_NO_FLAGS;
7037 elf_text_section->output_section = NULL;
7038 elf_text_section->owner = abfd;
7039 elf_text_symbol->name = ".text";
7040 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7041 elf_text_symbol->section = elf_text_section;
7043 /* This code used to do *secp = bfd_und_section_ptr if
7044 info->shared. I don't know why, and that doesn't make sense,
7045 so I took it out. */
7046 *secp = elf_tdata (abfd)->elf_text_section;
7049 case SHN_MIPS_ACOMMON:
7050 /* Fall through. XXX Can we treat this as allocated data? */
7052 /* This section is used in a shared object. */
7053 if (elf_tdata (abfd)->elf_data_section == NULL)
7055 asymbol *elf_data_symbol;
7056 asection *elf_data_section;
7057 bfd_size_type amt = sizeof (asection);
7059 elf_data_section = bfd_zalloc (abfd, amt);
7060 if (elf_data_section == NULL)
7063 amt = sizeof (asymbol);
7064 elf_data_symbol = bfd_zalloc (abfd, amt);
7065 if (elf_data_symbol == NULL)
7068 /* Initialize the section. */
7070 elf_tdata (abfd)->elf_data_section = elf_data_section;
7071 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7073 elf_data_section->symbol = elf_data_symbol;
7074 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
7076 elf_data_section->name = ".data";
7077 elf_data_section->flags = SEC_NO_FLAGS;
7078 elf_data_section->output_section = NULL;
7079 elf_data_section->owner = abfd;
7080 elf_data_symbol->name = ".data";
7081 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7082 elf_data_symbol->section = elf_data_section;
7084 /* This code used to do *secp = bfd_und_section_ptr if
7085 info->shared. I don't know why, and that doesn't make sense,
7086 so I took it out. */
7087 *secp = elf_tdata (abfd)->elf_data_section;
7090 case SHN_MIPS_SUNDEFINED:
7091 *secp = bfd_und_section_ptr;
7095 if (SGI_COMPAT (abfd)
7097 && info->output_bfd->xvec == abfd->xvec
7098 && strcmp (*namep, "__rld_obj_head") == 0)
7100 struct elf_link_hash_entry *h;
7101 struct bfd_link_hash_entry *bh;
7103 /* Mark __rld_obj_head as dynamic. */
7105 if (! (_bfd_generic_link_add_one_symbol
7106 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7107 get_elf_backend_data (abfd)->collect, &bh)))
7110 h = (struct elf_link_hash_entry *) bh;
7113 h->type = STT_OBJECT;
7115 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7118 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7119 mips_elf_hash_table (info)->rld_symbol = h;
7122 /* If this is a mips16 text symbol, add 1 to the value to make it
7123 odd. This will cause something like .word SYM to come up with
7124 the right value when it is loaded into the PC. */
7125 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7131 /* This hook function is called before the linker writes out a global
7132 symbol. We mark symbols as small common if appropriate. This is
7133 also where we undo the increment of the value for a mips16 symbol. */
7136 _bfd_mips_elf_link_output_symbol_hook
7137 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7138 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7139 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7141 /* If we see a common symbol, which implies a relocatable link, then
7142 if a symbol was small common in an input file, mark it as small
7143 common in the output file. */
7144 if (sym->st_shndx == SHN_COMMON
7145 && strcmp (input_sec->name, ".scommon") == 0)
7146 sym->st_shndx = SHN_MIPS_SCOMMON;
7148 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7149 sym->st_value &= ~1;
7154 /* Functions for the dynamic linker. */
7156 /* Create dynamic sections when linking against a dynamic object. */
7159 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7161 struct elf_link_hash_entry *h;
7162 struct bfd_link_hash_entry *bh;
7164 register asection *s;
7165 const char * const *namep;
7166 struct mips_elf_link_hash_table *htab;
7168 htab = mips_elf_hash_table (info);
7169 BFD_ASSERT (htab != NULL);
7171 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7172 | SEC_LINKER_CREATED | SEC_READONLY);
7174 /* The psABI requires a read-only .dynamic section, but the VxWorks
7176 if (!htab->is_vxworks)
7178 s = bfd_get_linker_section (abfd, ".dynamic");
7181 if (! bfd_set_section_flags (abfd, s, flags))
7186 /* We need to create .got section. */
7187 if (!mips_elf_create_got_section (abfd, info))
7190 if (! mips_elf_rel_dyn_section (info, TRUE))
7193 /* Create .stub section. */
7194 s = bfd_make_section_anyway_with_flags (abfd,
7195 MIPS_ELF_STUB_SECTION_NAME (abfd),
7198 || ! bfd_set_section_alignment (abfd, s,
7199 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7203 if (!mips_elf_hash_table (info)->use_rld_obj_head
7205 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7207 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7208 flags &~ (flagword) SEC_READONLY);
7210 || ! bfd_set_section_alignment (abfd, s,
7211 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7215 /* On IRIX5, we adjust add some additional symbols and change the
7216 alignments of several sections. There is no ABI documentation
7217 indicating that this is necessary on IRIX6, nor any evidence that
7218 the linker takes such action. */
7219 if (IRIX_COMPAT (abfd) == ict_irix5)
7221 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7224 if (! (_bfd_generic_link_add_one_symbol
7225 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7226 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7229 h = (struct elf_link_hash_entry *) bh;
7232 h->type = STT_SECTION;
7234 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7238 /* We need to create a .compact_rel section. */
7239 if (SGI_COMPAT (abfd))
7241 if (!mips_elf_create_compact_rel_section (abfd, info))
7245 /* Change alignments of some sections. */
7246 s = bfd_get_linker_section (abfd, ".hash");
7248 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7249 s = bfd_get_linker_section (abfd, ".dynsym");
7251 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7252 s = bfd_get_linker_section (abfd, ".dynstr");
7254 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7256 s = bfd_get_section_by_name (abfd, ".reginfo");
7258 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7259 s = bfd_get_linker_section (abfd, ".dynamic");
7261 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7268 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7270 if (!(_bfd_generic_link_add_one_symbol
7271 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7272 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7275 h = (struct elf_link_hash_entry *) bh;
7278 h->type = STT_SECTION;
7280 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7283 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7285 /* __rld_map is a four byte word located in the .data section
7286 and is filled in by the rtld to contain a pointer to
7287 the _r_debug structure. Its symbol value will be set in
7288 _bfd_mips_elf_finish_dynamic_symbol. */
7289 s = bfd_get_linker_section (abfd, ".rld_map");
7290 BFD_ASSERT (s != NULL);
7292 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7294 if (!(_bfd_generic_link_add_one_symbol
7295 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7296 get_elf_backend_data (abfd)->collect, &bh)))
7299 h = (struct elf_link_hash_entry *) bh;
7302 h->type = STT_OBJECT;
7304 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7306 mips_elf_hash_table (info)->rld_symbol = h;
7310 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7311 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7312 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7315 /* Cache the sections created above. */
7316 htab->splt = bfd_get_linker_section (abfd, ".plt");
7317 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7318 if (htab->is_vxworks)
7320 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7321 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7324 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7326 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7331 if (htab->is_vxworks)
7333 /* Do the usual VxWorks handling. */
7334 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7337 /* Work out the PLT sizes. */
7340 htab->plt_header_size
7341 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7342 htab->plt_entry_size
7343 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7347 htab->plt_header_size
7348 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7349 htab->plt_entry_size
7350 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7353 else if (!info->shared)
7355 /* All variants of the plt0 entry are the same size. */
7356 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7357 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7363 /* Return true if relocation REL against section SEC is a REL rather than
7364 RELA relocation. RELOCS is the first relocation in the section and
7365 ABFD is the bfd that contains SEC. */
7368 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7369 const Elf_Internal_Rela *relocs,
7370 const Elf_Internal_Rela *rel)
7372 Elf_Internal_Shdr *rel_hdr;
7373 const struct elf_backend_data *bed;
7375 /* To determine which flavor of relocation this is, we depend on the
7376 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7377 rel_hdr = elf_section_data (sec)->rel.hdr;
7378 if (rel_hdr == NULL)
7380 bed = get_elf_backend_data (abfd);
7381 return ((size_t) (rel - relocs)
7382 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7385 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7386 HOWTO is the relocation's howto and CONTENTS points to the contents
7387 of the section that REL is against. */
7390 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7391 reloc_howto_type *howto, bfd_byte *contents)
7394 unsigned int r_type;
7397 r_type = ELF_R_TYPE (abfd, rel->r_info);
7398 location = contents + rel->r_offset;
7400 /* Get the addend, which is stored in the input file. */
7401 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7402 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7403 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7405 return addend & howto->src_mask;
7408 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7409 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7410 and update *ADDEND with the final addend. Return true on success
7411 or false if the LO16 could not be found. RELEND is the exclusive
7412 upper bound on the relocations for REL's section. */
7415 mips_elf_add_lo16_rel_addend (bfd *abfd,
7416 const Elf_Internal_Rela *rel,
7417 const Elf_Internal_Rela *relend,
7418 bfd_byte *contents, bfd_vma *addend)
7420 unsigned int r_type, lo16_type;
7421 const Elf_Internal_Rela *lo16_relocation;
7422 reloc_howto_type *lo16_howto;
7425 r_type = ELF_R_TYPE (abfd, rel->r_info);
7426 if (mips16_reloc_p (r_type))
7427 lo16_type = R_MIPS16_LO16;
7428 else if (micromips_reloc_p (r_type))
7429 lo16_type = R_MICROMIPS_LO16;
7431 lo16_type = R_MIPS_LO16;
7433 /* The combined value is the sum of the HI16 addend, left-shifted by
7434 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7435 code does a `lui' of the HI16 value, and then an `addiu' of the
7438 Scan ahead to find a matching LO16 relocation.
7440 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7441 be immediately following. However, for the IRIX6 ABI, the next
7442 relocation may be a composed relocation consisting of several
7443 relocations for the same address. In that case, the R_MIPS_LO16
7444 relocation may occur as one of these. We permit a similar
7445 extension in general, as that is useful for GCC.
7447 In some cases GCC dead code elimination removes the LO16 but keeps
7448 the corresponding HI16. This is strictly speaking a violation of
7449 the ABI but not immediately harmful. */
7450 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7451 if (lo16_relocation == NULL)
7454 /* Obtain the addend kept there. */
7455 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7456 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7458 l <<= lo16_howto->rightshift;
7459 l = _bfd_mips_elf_sign_extend (l, 16);
7466 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7467 store the contents in *CONTENTS on success. Assume that *CONTENTS
7468 already holds the contents if it is nonull on entry. */
7471 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7476 /* Get cached copy if it exists. */
7477 if (elf_section_data (sec)->this_hdr.contents != NULL)
7479 *contents = elf_section_data (sec)->this_hdr.contents;
7483 return bfd_malloc_and_get_section (abfd, sec, contents);
7486 /* Look through the relocs for a section during the first phase, and
7487 allocate space in the global offset table. */
7490 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7491 asection *sec, const Elf_Internal_Rela *relocs)
7495 Elf_Internal_Shdr *symtab_hdr;
7496 struct elf_link_hash_entry **sym_hashes;
7498 const Elf_Internal_Rela *rel;
7499 const Elf_Internal_Rela *rel_end;
7501 const struct elf_backend_data *bed;
7502 struct mips_elf_link_hash_table *htab;
7505 reloc_howto_type *howto;
7507 if (info->relocatable)
7510 htab = mips_elf_hash_table (info);
7511 BFD_ASSERT (htab != NULL);
7513 dynobj = elf_hash_table (info)->dynobj;
7514 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7515 sym_hashes = elf_sym_hashes (abfd);
7516 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7518 bed = get_elf_backend_data (abfd);
7519 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7521 /* Check for the mips16 stub sections. */
7523 name = bfd_get_section_name (abfd, sec);
7524 if (FN_STUB_P (name))
7526 unsigned long r_symndx;
7528 /* Look at the relocation information to figure out which symbol
7531 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7534 (*_bfd_error_handler)
7535 (_("%B: Warning: cannot determine the target function for"
7536 " stub section `%s'"),
7538 bfd_set_error (bfd_error_bad_value);
7542 if (r_symndx < extsymoff
7543 || sym_hashes[r_symndx - extsymoff] == NULL)
7547 /* This stub is for a local symbol. This stub will only be
7548 needed if there is some relocation in this BFD, other
7549 than a 16 bit function call, which refers to this symbol. */
7550 for (o = abfd->sections; o != NULL; o = o->next)
7552 Elf_Internal_Rela *sec_relocs;
7553 const Elf_Internal_Rela *r, *rend;
7555 /* We can ignore stub sections when looking for relocs. */
7556 if ((o->flags & SEC_RELOC) == 0
7557 || o->reloc_count == 0
7558 || section_allows_mips16_refs_p (o))
7562 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7564 if (sec_relocs == NULL)
7567 rend = sec_relocs + o->reloc_count;
7568 for (r = sec_relocs; r < rend; r++)
7569 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7570 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7573 if (elf_section_data (o)->relocs != sec_relocs)
7582 /* There is no non-call reloc for this stub, so we do
7583 not need it. Since this function is called before
7584 the linker maps input sections to output sections, we
7585 can easily discard it by setting the SEC_EXCLUDE
7587 sec->flags |= SEC_EXCLUDE;
7591 /* Record this stub in an array of local symbol stubs for
7593 if (elf_tdata (abfd)->local_stubs == NULL)
7595 unsigned long symcount;
7599 if (elf_bad_symtab (abfd))
7600 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7602 symcount = symtab_hdr->sh_info;
7603 amt = symcount * sizeof (asection *);
7604 n = bfd_zalloc (abfd, amt);
7607 elf_tdata (abfd)->local_stubs = n;
7610 sec->flags |= SEC_KEEP;
7611 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7613 /* We don't need to set mips16_stubs_seen in this case.
7614 That flag is used to see whether we need to look through
7615 the global symbol table for stubs. We don't need to set
7616 it here, because we just have a local stub. */
7620 struct mips_elf_link_hash_entry *h;
7622 h = ((struct mips_elf_link_hash_entry *)
7623 sym_hashes[r_symndx - extsymoff]);
7625 while (h->root.root.type == bfd_link_hash_indirect
7626 || h->root.root.type == bfd_link_hash_warning)
7627 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7629 /* H is the symbol this stub is for. */
7631 /* If we already have an appropriate stub for this function, we
7632 don't need another one, so we can discard this one. Since
7633 this function is called before the linker maps input sections
7634 to output sections, we can easily discard it by setting the
7635 SEC_EXCLUDE flag. */
7636 if (h->fn_stub != NULL)
7638 sec->flags |= SEC_EXCLUDE;
7642 sec->flags |= SEC_KEEP;
7644 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7647 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7649 unsigned long r_symndx;
7650 struct mips_elf_link_hash_entry *h;
7653 /* Look at the relocation information to figure out which symbol
7656 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7659 (*_bfd_error_handler)
7660 (_("%B: Warning: cannot determine the target function for"
7661 " stub section `%s'"),
7663 bfd_set_error (bfd_error_bad_value);
7667 if (r_symndx < extsymoff
7668 || sym_hashes[r_symndx - extsymoff] == NULL)
7672 /* This stub is for a local symbol. This stub will only be
7673 needed if there is some relocation (R_MIPS16_26) in this BFD
7674 that refers to this symbol. */
7675 for (o = abfd->sections; o != NULL; o = o->next)
7677 Elf_Internal_Rela *sec_relocs;
7678 const Elf_Internal_Rela *r, *rend;
7680 /* We can ignore stub sections when looking for relocs. */
7681 if ((o->flags & SEC_RELOC) == 0
7682 || o->reloc_count == 0
7683 || section_allows_mips16_refs_p (o))
7687 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7689 if (sec_relocs == NULL)
7692 rend = sec_relocs + o->reloc_count;
7693 for (r = sec_relocs; r < rend; r++)
7694 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7695 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7698 if (elf_section_data (o)->relocs != sec_relocs)
7707 /* There is no non-call reloc for this stub, so we do
7708 not need it. Since this function is called before
7709 the linker maps input sections to output sections, we
7710 can easily discard it by setting the SEC_EXCLUDE
7712 sec->flags |= SEC_EXCLUDE;
7716 /* Record this stub in an array of local symbol call_stubs for
7718 if (elf_tdata (abfd)->local_call_stubs == NULL)
7720 unsigned long symcount;
7724 if (elf_bad_symtab (abfd))
7725 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7727 symcount = symtab_hdr->sh_info;
7728 amt = symcount * sizeof (asection *);
7729 n = bfd_zalloc (abfd, amt);
7732 elf_tdata (abfd)->local_call_stubs = n;
7735 sec->flags |= SEC_KEEP;
7736 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7738 /* We don't need to set mips16_stubs_seen in this case.
7739 That flag is used to see whether we need to look through
7740 the global symbol table for stubs. We don't need to set
7741 it here, because we just have a local stub. */
7745 h = ((struct mips_elf_link_hash_entry *)
7746 sym_hashes[r_symndx - extsymoff]);
7748 /* H is the symbol this stub is for. */
7750 if (CALL_FP_STUB_P (name))
7751 loc = &h->call_fp_stub;
7753 loc = &h->call_stub;
7755 /* If we already have an appropriate stub for this function, we
7756 don't need another one, so we can discard this one. Since
7757 this function is called before the linker maps input sections
7758 to output sections, we can easily discard it by setting the
7759 SEC_EXCLUDE flag. */
7762 sec->flags |= SEC_EXCLUDE;
7766 sec->flags |= SEC_KEEP;
7768 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7774 for (rel = relocs; rel < rel_end; ++rel)
7776 unsigned long r_symndx;
7777 unsigned int r_type;
7778 struct elf_link_hash_entry *h;
7779 bfd_boolean can_make_dynamic_p;
7781 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7782 r_type = ELF_R_TYPE (abfd, rel->r_info);
7784 if (r_symndx < extsymoff)
7786 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7788 (*_bfd_error_handler)
7789 (_("%B: Malformed reloc detected for section %s"),
7791 bfd_set_error (bfd_error_bad_value);
7796 h = sym_hashes[r_symndx - extsymoff];
7798 && (h->root.type == bfd_link_hash_indirect
7799 || h->root.type == bfd_link_hash_warning))
7800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7803 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7804 relocation into a dynamic one. */
7805 can_make_dynamic_p = FALSE;
7810 case R_MIPS_CALL_HI16:
7811 case R_MIPS_CALL_LO16:
7812 case R_MIPS_GOT_HI16:
7813 case R_MIPS_GOT_LO16:
7814 case R_MIPS_GOT_PAGE:
7815 case R_MIPS_GOT_OFST:
7816 case R_MIPS_GOT_DISP:
7817 case R_MIPS_TLS_GOTTPREL:
7819 case R_MIPS_TLS_LDM:
7820 case R_MIPS16_GOT16:
7821 case R_MIPS16_CALL16:
7822 case R_MIPS16_TLS_GOTTPREL:
7823 case R_MIPS16_TLS_GD:
7824 case R_MIPS16_TLS_LDM:
7825 case R_MICROMIPS_GOT16:
7826 case R_MICROMIPS_CALL16:
7827 case R_MICROMIPS_CALL_HI16:
7828 case R_MICROMIPS_CALL_LO16:
7829 case R_MICROMIPS_GOT_HI16:
7830 case R_MICROMIPS_GOT_LO16:
7831 case R_MICROMIPS_GOT_PAGE:
7832 case R_MICROMIPS_GOT_OFST:
7833 case R_MICROMIPS_GOT_DISP:
7834 case R_MICROMIPS_TLS_GOTTPREL:
7835 case R_MICROMIPS_TLS_GD:
7836 case R_MICROMIPS_TLS_LDM:
7838 elf_hash_table (info)->dynobj = dynobj = abfd;
7839 if (!mips_elf_create_got_section (dynobj, info))
7841 if (htab->is_vxworks && !info->shared)
7843 (*_bfd_error_handler)
7844 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7845 abfd, (unsigned long) rel->r_offset);
7846 bfd_set_error (bfd_error_bad_value);
7851 /* This is just a hint; it can safely be ignored. Don't set
7852 has_static_relocs for the corresponding symbol. */
7854 case R_MICROMIPS_JALR:
7860 /* In VxWorks executables, references to external symbols
7861 must be handled using copy relocs or PLT entries; it is not
7862 possible to convert this relocation into a dynamic one.
7864 For executables that use PLTs and copy-relocs, we have a
7865 choice between converting the relocation into a dynamic
7866 one or using copy relocations or PLT entries. It is
7867 usually better to do the former, unless the relocation is
7868 against a read-only section. */
7871 && !htab->is_vxworks
7872 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7873 && !(!info->nocopyreloc
7874 && !PIC_OBJECT_P (abfd)
7875 && MIPS_ELF_READONLY_SECTION (sec))))
7876 && (sec->flags & SEC_ALLOC) != 0)
7878 can_make_dynamic_p = TRUE;
7880 elf_hash_table (info)->dynobj = dynobj = abfd;
7883 /* For sections that are not SEC_ALLOC a copy reloc would be
7884 output if possible (implying questionable semantics for
7885 read-only data objects) or otherwise the final link would
7886 fail as ld.so will not process them and could not therefore
7887 handle any outstanding dynamic relocations.
7889 For such sections that are also SEC_DEBUGGING, we can avoid
7890 these problems by simply ignoring any relocs as these
7891 sections have a predefined use and we know it is safe to do
7894 This is needed in cases such as a global symbol definition
7895 in a shared library causing a common symbol from an object
7896 file to be converted to an undefined reference. If that
7897 happens, then all the relocations against this symbol from
7898 SEC_DEBUGGING sections in the object file will resolve to
7900 if ((sec->flags & SEC_DEBUGGING) != 0)
7905 /* Most static relocations require pointer equality, except
7908 h->pointer_equality_needed = TRUE;
7914 case R_MICROMIPS_26_S1:
7915 case R_MICROMIPS_PC7_S1:
7916 case R_MICROMIPS_PC10_S1:
7917 case R_MICROMIPS_PC16_S1:
7918 case R_MICROMIPS_PC23_S2:
7920 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7926 /* Relocations against the special VxWorks __GOTT_BASE__ and
7927 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7928 room for them in .rela.dyn. */
7929 if (is_gott_symbol (info, h))
7933 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7937 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7938 if (MIPS_ELF_READONLY_SECTION (sec))
7939 /* We tell the dynamic linker that there are
7940 relocations against the text segment. */
7941 info->flags |= DF_TEXTREL;
7944 else if (call_lo16_reloc_p (r_type)
7945 || got_lo16_reloc_p (r_type)
7946 || got_disp_reloc_p (r_type)
7947 || (got16_reloc_p (r_type) && htab->is_vxworks))
7949 /* We may need a local GOT entry for this relocation. We
7950 don't count R_MIPS_GOT_PAGE because we can estimate the
7951 maximum number of pages needed by looking at the size of
7952 the segment. Similar comments apply to R_MIPS*_GOT16 and
7953 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7954 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7955 R_MIPS_CALL_HI16 because these are always followed by an
7956 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7957 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7958 rel->r_addend, info, r_type))
7963 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
7964 ELF_ST_IS_MIPS16 (h->other)))
7965 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7970 case R_MIPS16_CALL16:
7971 case R_MICROMIPS_CALL16:
7974 (*_bfd_error_handler)
7975 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7976 abfd, (unsigned long) rel->r_offset);
7977 bfd_set_error (bfd_error_bad_value);
7982 case R_MIPS_CALL_HI16:
7983 case R_MIPS_CALL_LO16:
7984 case R_MICROMIPS_CALL_HI16:
7985 case R_MICROMIPS_CALL_LO16:
7988 /* Make sure there is room in the regular GOT to hold the
7989 function's address. We may eliminate it in favour of
7990 a .got.plt entry later; see mips_elf_count_got_symbols. */
7991 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
7995 /* We need a stub, not a plt entry for the undefined
7996 function. But we record it as if it needs plt. See
7997 _bfd_elf_adjust_dynamic_symbol. */
8003 case R_MIPS_GOT_PAGE:
8004 case R_MICROMIPS_GOT_PAGE:
8005 case R_MIPS16_GOT16:
8007 case R_MIPS_GOT_HI16:
8008 case R_MIPS_GOT_LO16:
8009 case R_MICROMIPS_GOT16:
8010 case R_MICROMIPS_GOT_HI16:
8011 case R_MICROMIPS_GOT_LO16:
8012 if (!h || got_page_reloc_p (r_type))
8014 /* This relocation needs (or may need, if h != NULL) a
8015 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8016 know for sure until we know whether the symbol is
8018 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8020 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8022 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8023 addend = mips_elf_read_rel_addend (abfd, rel,
8025 if (got16_reloc_p (r_type))
8026 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8029 addend <<= howto->rightshift;
8032 addend = rel->r_addend;
8033 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8039 struct mips_elf_link_hash_entry *hmips =
8040 (struct mips_elf_link_hash_entry *) h;
8042 /* This symbol is definitely not overridable. */
8043 if (hmips->root.def_regular
8044 && ! (info->shared && ! info->symbolic
8045 && ! hmips->root.forced_local))
8049 /* If this is a global, overridable symbol, GOT_PAGE will
8050 decay to GOT_DISP, so we'll need a GOT entry for it. */
8053 case R_MIPS_GOT_DISP:
8054 case R_MICROMIPS_GOT_DISP:
8055 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8060 case R_MIPS_TLS_GOTTPREL:
8061 case R_MIPS16_TLS_GOTTPREL:
8062 case R_MICROMIPS_TLS_GOTTPREL:
8064 info->flags |= DF_STATIC_TLS;
8067 case R_MIPS_TLS_LDM:
8068 case R_MIPS16_TLS_LDM:
8069 case R_MICROMIPS_TLS_LDM:
8070 if (tls_ldm_reloc_p (r_type))
8072 r_symndx = STN_UNDEF;
8078 case R_MIPS16_TLS_GD:
8079 case R_MICROMIPS_TLS_GD:
8080 /* This symbol requires a global offset table entry, or two
8081 for TLS GD relocations. */
8084 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8090 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8100 /* In VxWorks executables, references to external symbols
8101 are handled using copy relocs or PLT stubs, so there's
8102 no need to add a .rela.dyn entry for this relocation. */
8103 if (can_make_dynamic_p)
8107 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8111 if (info->shared && h == NULL)
8113 /* When creating a shared object, we must copy these
8114 reloc types into the output file as R_MIPS_REL32
8115 relocs. Make room for this reloc in .rel(a).dyn. */
8116 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8117 if (MIPS_ELF_READONLY_SECTION (sec))
8118 /* We tell the dynamic linker that there are
8119 relocations against the text segment. */
8120 info->flags |= DF_TEXTREL;
8124 struct mips_elf_link_hash_entry *hmips;
8126 /* For a shared object, we must copy this relocation
8127 unless the symbol turns out to be undefined and
8128 weak with non-default visibility, in which case
8129 it will be left as zero.
8131 We could elide R_MIPS_REL32 for locally binding symbols
8132 in shared libraries, but do not yet do so.
8134 For an executable, we only need to copy this
8135 reloc if the symbol is defined in a dynamic
8137 hmips = (struct mips_elf_link_hash_entry *) h;
8138 ++hmips->possibly_dynamic_relocs;
8139 if (MIPS_ELF_READONLY_SECTION (sec))
8140 /* We need it to tell the dynamic linker if there
8141 are relocations against the text segment. */
8142 hmips->readonly_reloc = TRUE;
8146 if (SGI_COMPAT (abfd))
8147 mips_elf_hash_table (info)->compact_rel_size +=
8148 sizeof (Elf32_External_crinfo);
8152 case R_MIPS_GPREL16:
8153 case R_MIPS_LITERAL:
8154 case R_MIPS_GPREL32:
8155 case R_MICROMIPS_26_S1:
8156 case R_MICROMIPS_GPREL16:
8157 case R_MICROMIPS_LITERAL:
8158 case R_MICROMIPS_GPREL7_S2:
8159 if (SGI_COMPAT (abfd))
8160 mips_elf_hash_table (info)->compact_rel_size +=
8161 sizeof (Elf32_External_crinfo);
8164 /* This relocation describes the C++ object vtable hierarchy.
8165 Reconstruct it for later use during GC. */
8166 case R_MIPS_GNU_VTINHERIT:
8167 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8171 /* This relocation describes which C++ vtable entries are actually
8172 used. Record for later use during GC. */
8173 case R_MIPS_GNU_VTENTRY:
8174 BFD_ASSERT (h != NULL);
8176 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8184 /* We must not create a stub for a symbol that has relocations
8185 related to taking the function's address. This doesn't apply to
8186 VxWorks, where CALL relocs refer to a .got.plt entry instead of
8187 a normal .got entry. */
8188 if (!htab->is_vxworks && h != NULL)
8192 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8194 case R_MIPS16_CALL16:
8196 case R_MIPS_CALL_HI16:
8197 case R_MIPS_CALL_LO16:
8199 case R_MICROMIPS_CALL16:
8200 case R_MICROMIPS_CALL_HI16:
8201 case R_MICROMIPS_CALL_LO16:
8202 case R_MICROMIPS_JALR:
8206 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8207 if there is one. We only need to handle global symbols here;
8208 we decide whether to keep or delete stubs for local symbols
8209 when processing the stub's relocations. */
8211 && !mips16_call_reloc_p (r_type)
8212 && !section_allows_mips16_refs_p (sec))
8214 struct mips_elf_link_hash_entry *mh;
8216 mh = (struct mips_elf_link_hash_entry *) h;
8217 mh->need_fn_stub = TRUE;
8220 /* Refuse some position-dependent relocations when creating a
8221 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8222 not PIC, but we can create dynamic relocations and the result
8223 will be fine. Also do not refuse R_MIPS_LO16, which can be
8224 combined with R_MIPS_GOT16. */
8232 case R_MIPS_HIGHEST:
8233 case R_MICROMIPS_HI16:
8234 case R_MICROMIPS_HIGHER:
8235 case R_MICROMIPS_HIGHEST:
8236 /* Don't refuse a high part relocation if it's against
8237 no symbol (e.g. part of a compound relocation). */
8238 if (r_symndx == STN_UNDEF)
8241 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8242 and has a special meaning. */
8243 if (!NEWABI_P (abfd) && h != NULL
8244 && strcmp (h->root.root.string, "_gp_disp") == 0)
8247 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8248 if (is_gott_symbol (info, h))
8255 case R_MICROMIPS_26_S1:
8256 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8257 (*_bfd_error_handler)
8258 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8260 (h) ? h->root.root.string : "a local symbol");
8261 bfd_set_error (bfd_error_bad_value);
8273 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8274 struct bfd_link_info *link_info,
8277 Elf_Internal_Rela *internal_relocs;
8278 Elf_Internal_Rela *irel, *irelend;
8279 Elf_Internal_Shdr *symtab_hdr;
8280 bfd_byte *contents = NULL;
8282 bfd_boolean changed_contents = FALSE;
8283 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8284 Elf_Internal_Sym *isymbuf = NULL;
8286 /* We are not currently changing any sizes, so only one pass. */
8289 if (link_info->relocatable)
8292 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8293 link_info->keep_memory);
8294 if (internal_relocs == NULL)
8297 irelend = internal_relocs + sec->reloc_count
8298 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8299 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8300 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8302 for (irel = internal_relocs; irel < irelend; irel++)
8305 bfd_signed_vma sym_offset;
8306 unsigned int r_type;
8307 unsigned long r_symndx;
8309 unsigned long instruction;
8311 /* Turn jalr into bgezal, and jr into beq, if they're marked
8312 with a JALR relocation, that indicate where they jump to.
8313 This saves some pipeline bubbles. */
8314 r_type = ELF_R_TYPE (abfd, irel->r_info);
8315 if (r_type != R_MIPS_JALR)
8318 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8319 /* Compute the address of the jump target. */
8320 if (r_symndx >= extsymoff)
8322 struct mips_elf_link_hash_entry *h
8323 = ((struct mips_elf_link_hash_entry *)
8324 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8326 while (h->root.root.type == bfd_link_hash_indirect
8327 || h->root.root.type == bfd_link_hash_warning)
8328 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8330 /* If a symbol is undefined, or if it may be overridden,
8332 if (! ((h->root.root.type == bfd_link_hash_defined
8333 || h->root.root.type == bfd_link_hash_defweak)
8334 && h->root.root.u.def.section)
8335 || (link_info->shared && ! link_info->symbolic
8336 && !h->root.forced_local))
8339 sym_sec = h->root.root.u.def.section;
8340 if (sym_sec->output_section)
8341 symval = (h->root.root.u.def.value
8342 + sym_sec->output_section->vma
8343 + sym_sec->output_offset);
8345 symval = h->root.root.u.def.value;
8349 Elf_Internal_Sym *isym;
8351 /* Read this BFD's symbols if we haven't done so already. */
8352 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8354 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8355 if (isymbuf == NULL)
8356 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8357 symtab_hdr->sh_info, 0,
8359 if (isymbuf == NULL)
8363 isym = isymbuf + r_symndx;
8364 if (isym->st_shndx == SHN_UNDEF)
8366 else if (isym->st_shndx == SHN_ABS)
8367 sym_sec = bfd_abs_section_ptr;
8368 else if (isym->st_shndx == SHN_COMMON)
8369 sym_sec = bfd_com_section_ptr;
8372 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8373 symval = isym->st_value
8374 + sym_sec->output_section->vma
8375 + sym_sec->output_offset;
8378 /* Compute branch offset, from delay slot of the jump to the
8380 sym_offset = (symval + irel->r_addend)
8381 - (sec_start + irel->r_offset + 4);
8383 /* Branch offset must be properly aligned. */
8384 if ((sym_offset & 3) != 0)
8389 /* Check that it's in range. */
8390 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8393 /* Get the section contents if we haven't done so already. */
8394 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8397 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8399 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8400 if ((instruction & 0xfc1fffff) == 0x0000f809)
8401 instruction = 0x04110000;
8402 /* If it was jr <reg>, turn it into b <target>. */
8403 else if ((instruction & 0xfc1fffff) == 0x00000008)
8404 instruction = 0x10000000;
8408 instruction |= (sym_offset & 0xffff);
8409 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8410 changed_contents = TRUE;
8413 if (contents != NULL
8414 && elf_section_data (sec)->this_hdr.contents != contents)
8416 if (!changed_contents && !link_info->keep_memory)
8420 /* Cache the section contents for elf_link_input_bfd. */
8421 elf_section_data (sec)->this_hdr.contents = contents;
8427 if (contents != NULL
8428 && elf_section_data (sec)->this_hdr.contents != contents)
8433 /* Allocate space for global sym dynamic relocs. */
8436 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8438 struct bfd_link_info *info = inf;
8440 struct mips_elf_link_hash_entry *hmips;
8441 struct mips_elf_link_hash_table *htab;
8443 htab = mips_elf_hash_table (info);
8444 BFD_ASSERT (htab != NULL);
8446 dynobj = elf_hash_table (info)->dynobj;
8447 hmips = (struct mips_elf_link_hash_entry *) h;
8449 /* VxWorks executables are handled elsewhere; we only need to
8450 allocate relocations in shared objects. */
8451 if (htab->is_vxworks && !info->shared)
8454 /* Ignore indirect symbols. All relocations against such symbols
8455 will be redirected to the target symbol. */
8456 if (h->root.type == bfd_link_hash_indirect)
8459 /* If this symbol is defined in a dynamic object, or we are creating
8460 a shared library, we will need to copy any R_MIPS_32 or
8461 R_MIPS_REL32 relocs against it into the output file. */
8462 if (! info->relocatable
8463 && hmips->possibly_dynamic_relocs != 0
8464 && (h->root.type == bfd_link_hash_defweak
8465 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8468 bfd_boolean do_copy = TRUE;
8470 if (h->root.type == bfd_link_hash_undefweak)
8472 /* Do not copy relocations for undefined weak symbols with
8473 non-default visibility. */
8474 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8477 /* Make sure undefined weak symbols are output as a dynamic
8479 else if (h->dynindx == -1 && !h->forced_local)
8481 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8488 /* Even though we don't directly need a GOT entry for this symbol,
8489 the SVR4 psABI requires it to have a dynamic symbol table
8490 index greater that DT_MIPS_GOTSYM if there are dynamic
8491 relocations against it.
8493 VxWorks does not enforce the same mapping between the GOT
8494 and the symbol table, so the same requirement does not
8496 if (!htab->is_vxworks)
8498 if (hmips->global_got_area > GGA_RELOC_ONLY)
8499 hmips->global_got_area = GGA_RELOC_ONLY;
8500 hmips->got_only_for_calls = FALSE;
8503 mips_elf_allocate_dynamic_relocations
8504 (dynobj, info, hmips->possibly_dynamic_relocs);
8505 if (hmips->readonly_reloc)
8506 /* We tell the dynamic linker that there are relocations
8507 against the text segment. */
8508 info->flags |= DF_TEXTREL;
8515 /* Adjust a symbol defined by a dynamic object and referenced by a
8516 regular object. The current definition is in some section of the
8517 dynamic object, but we're not including those sections. We have to
8518 change the definition to something the rest of the link can
8522 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8523 struct elf_link_hash_entry *h)
8526 struct mips_elf_link_hash_entry *hmips;
8527 struct mips_elf_link_hash_table *htab;
8529 htab = mips_elf_hash_table (info);
8530 BFD_ASSERT (htab != NULL);
8532 dynobj = elf_hash_table (info)->dynobj;
8533 hmips = (struct mips_elf_link_hash_entry *) h;
8535 /* Make sure we know what is going on here. */
8536 BFD_ASSERT (dynobj != NULL
8538 || h->u.weakdef != NULL
8541 && !h->def_regular)));
8543 hmips = (struct mips_elf_link_hash_entry *) h;
8545 /* If there are call relocations against an externally-defined symbol,
8546 see whether we can create a MIPS lazy-binding stub for it. We can
8547 only do this if all references to the function are through call
8548 relocations, and in that case, the traditional lazy-binding stubs
8549 are much more efficient than PLT entries.
8551 Traditional stubs are only available on SVR4 psABI-based systems;
8552 VxWorks always uses PLTs instead. */
8553 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8555 if (! elf_hash_table (info)->dynamic_sections_created)
8558 /* If this symbol is not defined in a regular file, then set
8559 the symbol to the stub location. This is required to make
8560 function pointers compare as equal between the normal
8561 executable and the shared library. */
8562 if (!h->def_regular)
8564 hmips->needs_lazy_stub = TRUE;
8565 htab->lazy_stub_count++;
8569 /* As above, VxWorks requires PLT entries for externally-defined
8570 functions that are only accessed through call relocations.
8572 Both VxWorks and non-VxWorks targets also need PLT entries if there
8573 are static-only relocations against an externally-defined function.
8574 This can technically occur for shared libraries if there are
8575 branches to the symbol, although it is unlikely that this will be
8576 used in practice due to the short ranges involved. It can occur
8577 for any relative or absolute relocation in executables; in that
8578 case, the PLT entry becomes the function's canonical address. */
8579 else if (((h->needs_plt && !hmips->no_fn_stub)
8580 || (h->type == STT_FUNC && hmips->has_static_relocs))
8581 && htab->use_plts_and_copy_relocs
8582 && !SYMBOL_CALLS_LOCAL (info, h)
8583 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8584 && h->root.type == bfd_link_hash_undefweak))
8586 /* If this is the first symbol to need a PLT entry, allocate room
8588 if (htab->splt->size == 0)
8590 BFD_ASSERT (htab->sgotplt->size == 0);
8592 /* If we're using the PLT additions to the psABI, each PLT
8593 entry is 16 bytes and the PLT0 entry is 32 bytes.
8594 Encourage better cache usage by aligning. We do this
8595 lazily to avoid pessimizing traditional objects. */
8596 if (!htab->is_vxworks
8597 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8600 /* Make sure that .got.plt is word-aligned. We do this lazily
8601 for the same reason as above. */
8602 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8603 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8606 htab->splt->size += htab->plt_header_size;
8608 /* On non-VxWorks targets, the first two entries in .got.plt
8610 if (!htab->is_vxworks)
8612 += get_elf_backend_data (dynobj)->got_header_size;
8614 /* On VxWorks, also allocate room for the header's
8615 .rela.plt.unloaded entries. */
8616 if (htab->is_vxworks && !info->shared)
8617 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8620 /* Assign the next .plt entry to this symbol. */
8621 h->plt.offset = htab->splt->size;
8622 htab->splt->size += htab->plt_entry_size;
8624 /* If the output file has no definition of the symbol, set the
8625 symbol's value to the address of the stub. */
8626 if (!info->shared && !h->def_regular)
8628 h->root.u.def.section = htab->splt;
8629 h->root.u.def.value = h->plt.offset;
8630 /* For VxWorks, point at the PLT load stub rather than the
8631 lazy resolution stub; this stub will become the canonical
8632 function address. */
8633 if (htab->is_vxworks)
8634 h->root.u.def.value += 8;
8637 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8639 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8640 htab->srelplt->size += (htab->is_vxworks
8641 ? MIPS_ELF_RELA_SIZE (dynobj)
8642 : MIPS_ELF_REL_SIZE (dynobj));
8644 /* Make room for the .rela.plt.unloaded relocations. */
8645 if (htab->is_vxworks && !info->shared)
8646 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8648 /* All relocations against this symbol that could have been made
8649 dynamic will now refer to the PLT entry instead. */
8650 hmips->possibly_dynamic_relocs = 0;
8655 /* If this is a weak symbol, and there is a real definition, the
8656 processor independent code will have arranged for us to see the
8657 real definition first, and we can just use the same value. */
8658 if (h->u.weakdef != NULL)
8660 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8661 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8662 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8663 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8667 /* Otherwise, there is nothing further to do for symbols defined
8668 in regular objects. */
8672 /* There's also nothing more to do if we'll convert all relocations
8673 against this symbol into dynamic relocations. */
8674 if (!hmips->has_static_relocs)
8677 /* We're now relying on copy relocations. Complain if we have
8678 some that we can't convert. */
8679 if (!htab->use_plts_and_copy_relocs || info->shared)
8681 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8682 "dynamic symbol %s"),
8683 h->root.root.string);
8684 bfd_set_error (bfd_error_bad_value);
8688 /* We must allocate the symbol in our .dynbss section, which will
8689 become part of the .bss section of the executable. There will be
8690 an entry for this symbol in the .dynsym section. The dynamic
8691 object will contain position independent code, so all references
8692 from the dynamic object to this symbol will go through the global
8693 offset table. The dynamic linker will use the .dynsym entry to
8694 determine the address it must put in the global offset table, so
8695 both the dynamic object and the regular object will refer to the
8696 same memory location for the variable. */
8698 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8700 if (htab->is_vxworks)
8701 htab->srelbss->size += sizeof (Elf32_External_Rela);
8703 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8707 /* All relocations against this symbol that could have been made
8708 dynamic will now refer to the local copy instead. */
8709 hmips->possibly_dynamic_relocs = 0;
8711 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8714 /* This function is called after all the input files have been read,
8715 and the input sections have been assigned to output sections. We
8716 check for any mips16 stub sections that we can discard. */
8719 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8720 struct bfd_link_info *info)
8723 struct mips_elf_link_hash_table *htab;
8724 struct mips_htab_traverse_info hti;
8726 htab = mips_elf_hash_table (info);
8727 BFD_ASSERT (htab != NULL);
8729 /* The .reginfo section has a fixed size. */
8730 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8732 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8735 hti.output_bfd = output_bfd;
8737 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8738 mips_elf_check_symbols, &hti);
8745 /* If the link uses a GOT, lay it out and work out its size. */
8748 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8752 struct mips_got_info *g;
8753 bfd_size_type loadable_size = 0;
8754 bfd_size_type page_gotno;
8756 struct mips_elf_traverse_got_arg tga;
8757 struct mips_elf_link_hash_table *htab;
8759 htab = mips_elf_hash_table (info);
8760 BFD_ASSERT (htab != NULL);
8766 dynobj = elf_hash_table (info)->dynobj;
8769 /* Allocate room for the reserved entries. VxWorks always reserves
8770 3 entries; other objects only reserve 2 entries. */
8771 BFD_ASSERT (g->assigned_gotno == 0);
8772 if (htab->is_vxworks)
8773 htab->reserved_gotno = 3;
8775 htab->reserved_gotno = 2;
8776 g->local_gotno += htab->reserved_gotno;
8777 g->assigned_gotno = htab->reserved_gotno;
8779 /* Decide which symbols need to go in the global part of the GOT and
8780 count the number of reloc-only GOT symbols. */
8781 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
8783 if (!mips_elf_resolve_final_got_entries (info, g))
8786 /* Calculate the total loadable size of the output. That
8787 will give us the maximum number of GOT_PAGE entries
8789 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
8791 asection *subsection;
8793 for (subsection = ibfd->sections;
8795 subsection = subsection->next)
8797 if ((subsection->flags & SEC_ALLOC) == 0)
8799 loadable_size += ((subsection->size + 0xf)
8800 &~ (bfd_size_type) 0xf);
8804 if (htab->is_vxworks)
8805 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8806 relocations against local symbols evaluate to "G", and the EABI does
8807 not include R_MIPS_GOT_PAGE. */
8810 /* Assume there are two loadable segments consisting of contiguous
8811 sections. Is 5 enough? */
8812 page_gotno = (loadable_size >> 16) + 5;
8814 /* Choose the smaller of the two page estimates; both are intended to be
8816 if (page_gotno > g->page_gotno)
8817 page_gotno = g->page_gotno;
8819 g->local_gotno += page_gotno;
8821 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8822 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8823 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8825 /* VxWorks does not support multiple GOTs. It initializes $gp to
8826 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8828 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8830 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8835 /* Record that all bfds use G. This also has the effect of freeing
8836 the per-bfd GOTs, which we no longer need. */
8837 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
8838 if (mips_elf_bfd_got (ibfd, FALSE))
8839 mips_elf_replace_bfd_got (ibfd, g);
8840 mips_elf_replace_bfd_got (output_bfd, g);
8842 /* Set up TLS entries. */
8843 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8846 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
8847 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
8850 BFD_ASSERT (g->tls_assigned_gotno
8851 == g->global_gotno + g->local_gotno + g->tls_gotno);
8853 /* Each VxWorks GOT entry needs an explicit relocation. */
8854 if (htab->is_vxworks && info->shared)
8855 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
8857 /* Allocate room for the TLS relocations. */
8859 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
8865 /* Estimate the size of the .MIPS.stubs section. */
8868 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8870 struct mips_elf_link_hash_table *htab;
8871 bfd_size_type dynsymcount;
8873 htab = mips_elf_hash_table (info);
8874 BFD_ASSERT (htab != NULL);
8876 if (htab->lazy_stub_count == 0)
8879 /* IRIX rld assumes that a function stub isn't at the end of the .text
8880 section, so add a dummy entry to the end. */
8881 htab->lazy_stub_count++;
8883 /* Get a worst-case estimate of the number of dynamic symbols needed.
8884 At this point, dynsymcount does not account for section symbols
8885 and count_section_dynsyms may overestimate the number that will
8887 dynsymcount = (elf_hash_table (info)->dynsymcount
8888 + count_section_dynsyms (output_bfd, info));
8890 /* Determine the size of one stub entry. */
8891 htab->function_stub_size = (dynsymcount > 0x10000
8892 ? MIPS_FUNCTION_STUB_BIG_SIZE
8893 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8895 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8898 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8899 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8900 allocate an entry in the stubs section. */
8903 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8905 struct mips_elf_link_hash_table *htab;
8907 htab = (struct mips_elf_link_hash_table *) data;
8908 if (h->needs_lazy_stub)
8910 h->root.root.u.def.section = htab->sstubs;
8911 h->root.root.u.def.value = htab->sstubs->size;
8912 h->root.plt.offset = htab->sstubs->size;
8913 htab->sstubs->size += htab->function_stub_size;
8918 /* Allocate offsets in the stubs section to each symbol that needs one.
8919 Set the final size of the .MIPS.stub section. */
8922 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8924 struct mips_elf_link_hash_table *htab;
8926 htab = mips_elf_hash_table (info);
8927 BFD_ASSERT (htab != NULL);
8929 if (htab->lazy_stub_count == 0)
8932 htab->sstubs->size = 0;
8933 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
8934 htab->sstubs->size += htab->function_stub_size;
8935 BFD_ASSERT (htab->sstubs->size
8936 == htab->lazy_stub_count * htab->function_stub_size);
8939 /* Set the sizes of the dynamic sections. */
8942 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8943 struct bfd_link_info *info)
8946 asection *s, *sreldyn;
8947 bfd_boolean reltext;
8948 struct mips_elf_link_hash_table *htab;
8950 htab = mips_elf_hash_table (info);
8951 BFD_ASSERT (htab != NULL);
8952 dynobj = elf_hash_table (info)->dynobj;
8953 BFD_ASSERT (dynobj != NULL);
8955 if (elf_hash_table (info)->dynamic_sections_created)
8957 /* Set the contents of the .interp section to the interpreter. */
8958 if (info->executable)
8960 s = bfd_get_linker_section (dynobj, ".interp");
8961 BFD_ASSERT (s != NULL);
8963 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8965 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8968 /* Create a symbol for the PLT, if we know that we are using it. */
8969 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8971 struct elf_link_hash_entry *h;
8973 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8975 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8976 "_PROCEDURE_LINKAGE_TABLE_");
8977 htab->root.hplt = h;
8984 /* Allocate space for global sym dynamic relocs. */
8985 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
8987 mips_elf_estimate_stub_size (output_bfd, info);
8989 if (!mips_elf_lay_out_got (output_bfd, info))
8992 mips_elf_lay_out_lazy_stubs (info);
8994 /* The check_relocs and adjust_dynamic_symbol entry points have
8995 determined the sizes of the various dynamic sections. Allocate
8998 for (s = dynobj->sections; s != NULL; s = s->next)
9002 /* It's OK to base decisions on the section name, because none
9003 of the dynobj section names depend upon the input files. */
9004 name = bfd_get_section_name (dynobj, s);
9006 if ((s->flags & SEC_LINKER_CREATED) == 0)
9009 if (CONST_STRNEQ (name, ".rel"))
9013 const char *outname;
9016 /* If this relocation section applies to a read only
9017 section, then we probably need a DT_TEXTREL entry.
9018 If the relocation section is .rel(a).dyn, we always
9019 assert a DT_TEXTREL entry rather than testing whether
9020 there exists a relocation to a read only section or
9022 outname = bfd_get_section_name (output_bfd,
9024 target = bfd_get_section_by_name (output_bfd, outname + 4);
9026 && (target->flags & SEC_READONLY) != 0
9027 && (target->flags & SEC_ALLOC) != 0)
9028 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9031 /* We use the reloc_count field as a counter if we need
9032 to copy relocs into the output file. */
9033 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9036 /* If combreloc is enabled, elf_link_sort_relocs() will
9037 sort relocations, but in a different way than we do,
9038 and before we're done creating relocations. Also, it
9039 will move them around between input sections'
9040 relocation's contents, so our sorting would be
9041 broken, so don't let it run. */
9042 info->combreloc = 0;
9045 else if (! info->shared
9046 && ! mips_elf_hash_table (info)->use_rld_obj_head
9047 && CONST_STRNEQ (name, ".rld_map"))
9049 /* We add a room for __rld_map. It will be filled in by the
9050 rtld to contain a pointer to the _r_debug structure. */
9051 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9053 else if (SGI_COMPAT (output_bfd)
9054 && CONST_STRNEQ (name, ".compact_rel"))
9055 s->size += mips_elf_hash_table (info)->compact_rel_size;
9056 else if (s == htab->splt)
9058 /* If the last PLT entry has a branch delay slot, allocate
9059 room for an extra nop to fill the delay slot. This is
9060 for CPUs without load interlocking. */
9061 if (! LOAD_INTERLOCKS_P (output_bfd)
9062 && ! htab->is_vxworks && s->size > 0)
9065 else if (! CONST_STRNEQ (name, ".init")
9067 && s != htab->sgotplt
9068 && s != htab->sstubs
9069 && s != htab->sdynbss)
9071 /* It's not one of our sections, so don't allocate space. */
9077 s->flags |= SEC_EXCLUDE;
9081 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9084 /* Allocate memory for the section contents. */
9085 s->contents = bfd_zalloc (dynobj, s->size);
9086 if (s->contents == NULL)
9088 bfd_set_error (bfd_error_no_memory);
9093 if (elf_hash_table (info)->dynamic_sections_created)
9095 /* Add some entries to the .dynamic section. We fill in the
9096 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9097 must add the entries now so that we get the correct size for
9098 the .dynamic section. */
9100 /* SGI object has the equivalence of DT_DEBUG in the
9101 DT_MIPS_RLD_MAP entry. This must come first because glibc
9102 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9103 may only look at the first one they see. */
9105 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9108 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9109 used by the debugger. */
9110 if (info->executable
9111 && !SGI_COMPAT (output_bfd)
9112 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9115 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9116 info->flags |= DF_TEXTREL;
9118 if ((info->flags & DF_TEXTREL) != 0)
9120 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9123 /* Clear the DF_TEXTREL flag. It will be set again if we
9124 write out an actual text relocation; we may not, because
9125 at this point we do not know whether e.g. any .eh_frame
9126 absolute relocations have been converted to PC-relative. */
9127 info->flags &= ~DF_TEXTREL;
9130 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9133 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9134 if (htab->is_vxworks)
9136 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9137 use any of the DT_MIPS_* tags. */
9138 if (sreldyn && sreldyn->size > 0)
9140 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9143 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9146 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9152 if (sreldyn && sreldyn->size > 0)
9154 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9157 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9160 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9164 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9167 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9170 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9173 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9176 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9179 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9182 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9185 if (IRIX_COMPAT (dynobj) == ict_irix5
9186 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9189 if (IRIX_COMPAT (dynobj) == ict_irix6
9190 && (bfd_get_section_by_name
9191 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9192 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9195 if (htab->splt->size > 0)
9197 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9200 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9203 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9206 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9209 if (htab->is_vxworks
9210 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9217 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9218 Adjust its R_ADDEND field so that it is correct for the output file.
9219 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9220 and sections respectively; both use symbol indexes. */
9223 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9224 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9225 asection **local_sections, Elf_Internal_Rela *rel)
9227 unsigned int r_type, r_symndx;
9228 Elf_Internal_Sym *sym;
9231 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9233 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9234 if (gprel16_reloc_p (r_type)
9235 || r_type == R_MIPS_GPREL32
9236 || literal_reloc_p (r_type))
9238 rel->r_addend += _bfd_get_gp_value (input_bfd);
9239 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9242 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9243 sym = local_syms + r_symndx;
9245 /* Adjust REL's addend to account for section merging. */
9246 if (!info->relocatable)
9248 sec = local_sections[r_symndx];
9249 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9252 /* This would normally be done by the rela_normal code in elflink.c. */
9253 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9254 rel->r_addend += local_sections[r_symndx]->output_offset;
9258 /* Handle relocations against symbols from removed linkonce sections,
9259 or sections discarded by a linker script. We use this wrapper around
9260 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9261 on 64-bit ELF targets. In this case for any relocation handled, which
9262 always be the first in a triplet, the remaining two have to be processed
9263 together with the first, even if they are R_MIPS_NONE. It is the symbol
9264 index referred by the first reloc that applies to all the three and the
9265 remaining two never refer to an object symbol. And it is the final
9266 relocation (the last non-null one) that determines the output field of
9267 the whole relocation so retrieve the corresponding howto structure for
9268 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9270 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9271 and therefore requires to be pasted in a loop. It also defines a block
9272 and does not protect any of its arguments, hence the extra brackets. */
9275 mips_reloc_against_discarded_section (bfd *output_bfd,
9276 struct bfd_link_info *info,
9277 bfd *input_bfd, asection *input_section,
9278 Elf_Internal_Rela **rel,
9279 const Elf_Internal_Rela **relend,
9280 bfd_boolean rel_reloc,
9281 reloc_howto_type *howto,
9284 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9285 int count = bed->s->int_rels_per_ext_rel;
9286 unsigned int r_type;
9289 for (i = count - 1; i > 0; i--)
9291 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9292 if (r_type != R_MIPS_NONE)
9294 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9300 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9301 (*rel), count, (*relend),
9302 howto, i, contents);
9307 /* Relocate a MIPS ELF section. */
9310 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9311 bfd *input_bfd, asection *input_section,
9312 bfd_byte *contents, Elf_Internal_Rela *relocs,
9313 Elf_Internal_Sym *local_syms,
9314 asection **local_sections)
9316 Elf_Internal_Rela *rel;
9317 const Elf_Internal_Rela *relend;
9319 bfd_boolean use_saved_addend_p = FALSE;
9320 const struct elf_backend_data *bed;
9322 bed = get_elf_backend_data (output_bfd);
9323 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9324 for (rel = relocs; rel < relend; ++rel)
9328 reloc_howto_type *howto;
9329 bfd_boolean cross_mode_jump_p;
9330 /* TRUE if the relocation is a RELA relocation, rather than a
9332 bfd_boolean rela_relocation_p = TRUE;
9333 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9335 unsigned long r_symndx;
9337 Elf_Internal_Shdr *symtab_hdr;
9338 struct elf_link_hash_entry *h;
9339 bfd_boolean rel_reloc;
9341 rel_reloc = (NEWABI_P (input_bfd)
9342 && mips_elf_rel_relocation_p (input_bfd, input_section,
9344 /* Find the relocation howto for this relocation. */
9345 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9347 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9348 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9349 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9351 sec = local_sections[r_symndx];
9356 unsigned long extsymoff;
9359 if (!elf_bad_symtab (input_bfd))
9360 extsymoff = symtab_hdr->sh_info;
9361 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9362 while (h->root.type == bfd_link_hash_indirect
9363 || h->root.type == bfd_link_hash_warning)
9364 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9367 if (h->root.type == bfd_link_hash_defined
9368 || h->root.type == bfd_link_hash_defweak)
9369 sec = h->root.u.def.section;
9372 if (sec != NULL && discarded_section (sec))
9374 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9375 input_section, &rel, &relend,
9376 rel_reloc, howto, contents);
9380 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9382 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9383 64-bit code, but make sure all their addresses are in the
9384 lowermost or uppermost 32-bit section of the 64-bit address
9385 space. Thus, when they use an R_MIPS_64 they mean what is
9386 usually meant by R_MIPS_32, with the exception that the
9387 stored value is sign-extended to 64 bits. */
9388 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9390 /* On big-endian systems, we need to lie about the position
9392 if (bfd_big_endian (input_bfd))
9396 if (!use_saved_addend_p)
9398 /* If these relocations were originally of the REL variety,
9399 we must pull the addend out of the field that will be
9400 relocated. Otherwise, we simply use the contents of the
9402 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9405 rela_relocation_p = FALSE;
9406 addend = mips_elf_read_rel_addend (input_bfd, rel,
9408 if (hi16_reloc_p (r_type)
9409 || (got16_reloc_p (r_type)
9410 && mips_elf_local_relocation_p (input_bfd, rel,
9413 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9417 name = h->root.root.string;
9419 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9420 local_syms + r_symndx,
9422 (*_bfd_error_handler)
9423 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9424 input_bfd, input_section, name, howto->name,
9429 addend <<= howto->rightshift;
9432 addend = rel->r_addend;
9433 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9434 local_syms, local_sections, rel);
9437 if (info->relocatable)
9439 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9440 && bfd_big_endian (input_bfd))
9443 if (!rela_relocation_p && rel->r_addend)
9445 addend += rel->r_addend;
9446 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9447 addend = mips_elf_high (addend);
9448 else if (r_type == R_MIPS_HIGHER)
9449 addend = mips_elf_higher (addend);
9450 else if (r_type == R_MIPS_HIGHEST)
9451 addend = mips_elf_highest (addend);
9453 addend >>= howto->rightshift;
9455 /* We use the source mask, rather than the destination
9456 mask because the place to which we are writing will be
9457 source of the addend in the final link. */
9458 addend &= howto->src_mask;
9460 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9461 /* See the comment above about using R_MIPS_64 in the 32-bit
9462 ABI. Here, we need to update the addend. It would be
9463 possible to get away with just using the R_MIPS_32 reloc
9464 but for endianness. */
9470 if (addend & ((bfd_vma) 1 << 31))
9472 sign_bits = ((bfd_vma) 1 << 32) - 1;
9479 /* If we don't know that we have a 64-bit type,
9480 do two separate stores. */
9481 if (bfd_big_endian (input_bfd))
9483 /* Store the sign-bits (which are most significant)
9485 low_bits = sign_bits;
9491 high_bits = sign_bits;
9493 bfd_put_32 (input_bfd, low_bits,
9494 contents + rel->r_offset);
9495 bfd_put_32 (input_bfd, high_bits,
9496 contents + rel->r_offset + 4);
9500 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9501 input_bfd, input_section,
9506 /* Go on to the next relocation. */
9510 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9511 relocations for the same offset. In that case we are
9512 supposed to treat the output of each relocation as the addend
9514 if (rel + 1 < relend
9515 && rel->r_offset == rel[1].r_offset
9516 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9517 use_saved_addend_p = TRUE;
9519 use_saved_addend_p = FALSE;
9521 /* Figure out what value we are supposed to relocate. */
9522 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9523 input_section, info, rel,
9524 addend, howto, local_syms,
9525 local_sections, &value,
9526 &name, &cross_mode_jump_p,
9527 use_saved_addend_p))
9529 case bfd_reloc_continue:
9530 /* There's nothing to do. */
9533 case bfd_reloc_undefined:
9534 /* mips_elf_calculate_relocation already called the
9535 undefined_symbol callback. There's no real point in
9536 trying to perform the relocation at this point, so we
9537 just skip ahead to the next relocation. */
9540 case bfd_reloc_notsupported:
9541 msg = _("internal error: unsupported relocation error");
9542 info->callbacks->warning
9543 (info, msg, name, input_bfd, input_section, rel->r_offset);
9546 case bfd_reloc_overflow:
9547 if (use_saved_addend_p)
9548 /* Ignore overflow until we reach the last relocation for
9549 a given location. */
9553 struct mips_elf_link_hash_table *htab;
9555 htab = mips_elf_hash_table (info);
9556 BFD_ASSERT (htab != NULL);
9557 BFD_ASSERT (name != NULL);
9558 if (!htab->small_data_overflow_reported
9559 && (gprel16_reloc_p (howto->type)
9560 || literal_reloc_p (howto->type)))
9562 msg = _("small-data section exceeds 64KB;"
9563 " lower small-data size limit (see option -G)");
9565 htab->small_data_overflow_reported = TRUE;
9566 (*info->callbacks->einfo) ("%P: %s\n", msg);
9568 if (! ((*info->callbacks->reloc_overflow)
9569 (info, NULL, name, howto->name, (bfd_vma) 0,
9570 input_bfd, input_section, rel->r_offset)))
9578 case bfd_reloc_outofrange:
9579 if (jal_reloc_p (howto->type))
9581 msg = _("JALX to a non-word-aligned address");
9582 info->callbacks->warning
9583 (info, msg, name, input_bfd, input_section, rel->r_offset);
9593 /* If we've got another relocation for the address, keep going
9594 until we reach the last one. */
9595 if (use_saved_addend_p)
9601 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9602 /* See the comment above about using R_MIPS_64 in the 32-bit
9603 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9604 that calculated the right value. Now, however, we
9605 sign-extend the 32-bit result to 64-bits, and store it as a
9606 64-bit value. We are especially generous here in that we
9607 go to extreme lengths to support this usage on systems with
9608 only a 32-bit VMA. */
9614 if (value & ((bfd_vma) 1 << 31))
9616 sign_bits = ((bfd_vma) 1 << 32) - 1;
9623 /* If we don't know that we have a 64-bit type,
9624 do two separate stores. */
9625 if (bfd_big_endian (input_bfd))
9627 /* Undo what we did above. */
9629 /* Store the sign-bits (which are most significant)
9631 low_bits = sign_bits;
9637 high_bits = sign_bits;
9639 bfd_put_32 (input_bfd, low_bits,
9640 contents + rel->r_offset);
9641 bfd_put_32 (input_bfd, high_bits,
9642 contents + rel->r_offset + 4);
9646 /* Actually perform the relocation. */
9647 if (! mips_elf_perform_relocation (info, howto, rel, value,
9648 input_bfd, input_section,
9649 contents, cross_mode_jump_p))
9656 /* A function that iterates over each entry in la25_stubs and fills
9657 in the code for each one. DATA points to a mips_htab_traverse_info. */
9660 mips_elf_create_la25_stub (void **slot, void *data)
9662 struct mips_htab_traverse_info *hti;
9663 struct mips_elf_link_hash_table *htab;
9664 struct mips_elf_la25_stub *stub;
9667 bfd_vma offset, target, target_high, target_low;
9669 stub = (struct mips_elf_la25_stub *) *slot;
9670 hti = (struct mips_htab_traverse_info *) data;
9671 htab = mips_elf_hash_table (hti->info);
9672 BFD_ASSERT (htab != NULL);
9674 /* Create the section contents, if we haven't already. */
9675 s = stub->stub_section;
9679 loc = bfd_malloc (s->size);
9688 /* Work out where in the section this stub should go. */
9689 offset = stub->offset;
9691 /* Work out the target address. */
9692 target = mips_elf_get_la25_target (stub, &s);
9693 target += s->output_section->vma + s->output_offset;
9695 target_high = ((target + 0x8000) >> 16) & 0xffff;
9696 target_low = (target & 0xffff);
9698 if (stub->stub_section != htab->strampoline)
9700 /* This is a simple LUI/ADDIU stub. Zero out the beginning
9701 of the section and write the two instructions at the end. */
9702 memset (loc, 0, offset);
9704 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9706 bfd_put_micromips_32 (hti->output_bfd,
9707 LA25_LUI_MICROMIPS (target_high),
9709 bfd_put_micromips_32 (hti->output_bfd,
9710 LA25_ADDIU_MICROMIPS (target_low),
9715 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9716 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9721 /* This is trampoline. */
9723 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9725 bfd_put_micromips_32 (hti->output_bfd,
9726 LA25_LUI_MICROMIPS (target_high), loc);
9727 bfd_put_micromips_32 (hti->output_bfd,
9728 LA25_J_MICROMIPS (target), loc + 4);
9729 bfd_put_micromips_32 (hti->output_bfd,
9730 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
9731 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9735 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9736 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9737 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9738 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9744 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9745 adjust it appropriately now. */
9748 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9749 const char *name, Elf_Internal_Sym *sym)
9751 /* The linker script takes care of providing names and values for
9752 these, but we must place them into the right sections. */
9753 static const char* const text_section_symbols[] = {
9756 "__dso_displacement",
9758 "__program_header_table",
9762 static const char* const data_section_symbols[] = {
9770 const char* const *p;
9773 for (i = 0; i < 2; ++i)
9774 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9777 if (strcmp (*p, name) == 0)
9779 /* All of these symbols are given type STT_SECTION by the
9781 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9782 sym->st_other = STO_PROTECTED;
9784 /* The IRIX linker puts these symbols in special sections. */
9786 sym->st_shndx = SHN_MIPS_TEXT;
9788 sym->st_shndx = SHN_MIPS_DATA;
9794 /* Finish up dynamic symbol handling. We set the contents of various
9795 dynamic sections here. */
9798 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9799 struct bfd_link_info *info,
9800 struct elf_link_hash_entry *h,
9801 Elf_Internal_Sym *sym)
9805 struct mips_got_info *g, *gg;
9808 struct mips_elf_link_hash_table *htab;
9809 struct mips_elf_link_hash_entry *hmips;
9811 htab = mips_elf_hash_table (info);
9812 BFD_ASSERT (htab != NULL);
9813 dynobj = elf_hash_table (info)->dynobj;
9814 hmips = (struct mips_elf_link_hash_entry *) h;
9816 BFD_ASSERT (!htab->is_vxworks);
9818 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9820 /* We've decided to create a PLT entry for this symbol. */
9822 bfd_vma header_address, plt_index, got_address;
9823 bfd_vma got_address_high, got_address_low, load;
9824 const bfd_vma *plt_entry;
9826 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9827 BFD_ASSERT (h->dynindx != -1);
9828 BFD_ASSERT (htab->splt != NULL);
9829 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9830 BFD_ASSERT (!h->def_regular);
9832 /* Calculate the address of the PLT header. */
9833 header_address = (htab->splt->output_section->vma
9834 + htab->splt->output_offset);
9836 /* Calculate the index of the entry. */
9837 plt_index = ((h->plt.offset - htab->plt_header_size)
9838 / htab->plt_entry_size);
9840 /* Calculate the address of the .got.plt entry. */
9841 got_address = (htab->sgotplt->output_section->vma
9842 + htab->sgotplt->output_offset
9843 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9844 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9845 got_address_low = got_address & 0xffff;
9847 /* Initially point the .got.plt entry at the PLT header. */
9848 loc = (htab->sgotplt->contents
9849 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9850 if (ABI_64_P (output_bfd))
9851 bfd_put_64 (output_bfd, header_address, loc);
9853 bfd_put_32 (output_bfd, header_address, loc);
9855 /* Find out where the .plt entry should go. */
9856 loc = htab->splt->contents + h->plt.offset;
9858 /* Pick the load opcode. */
9859 load = MIPS_ELF_LOAD_WORD (output_bfd);
9861 /* Fill in the PLT entry itself. */
9862 plt_entry = mips_exec_plt_entry;
9863 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9864 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9866 if (! LOAD_INTERLOCKS_P (output_bfd))
9868 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9869 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9873 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9874 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9877 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9878 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9879 plt_index, h->dynindx,
9880 R_MIPS_JUMP_SLOT, got_address);
9882 /* We distinguish between PLT entries and lazy-binding stubs by
9883 giving the former an st_other value of STO_MIPS_PLT. Set the
9884 flag and leave the value if there are any relocations in the
9885 binary where pointer equality matters. */
9886 sym->st_shndx = SHN_UNDEF;
9887 if (h->pointer_equality_needed)
9888 sym->st_other = STO_MIPS_PLT;
9892 else if (h->plt.offset != MINUS_ONE)
9894 /* We've decided to create a lazy-binding stub. */
9895 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9897 /* This symbol has a stub. Set it up. */
9899 BFD_ASSERT (h->dynindx != -1);
9901 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9902 || (h->dynindx <= 0xffff));
9904 /* Values up to 2^31 - 1 are allowed. Larger values would cause
9905 sign extension at runtime in the stub, resulting in a negative
9907 if (h->dynindx & ~0x7fffffff)
9910 /* Fill the stub. */
9912 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9914 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9916 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9918 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9922 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9925 /* If a large stub is not required and sign extension is not a
9926 problem, then use legacy code in the stub. */
9927 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9928 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9929 else if (h->dynindx & ~0x7fff)
9930 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9932 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9935 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9936 memcpy (htab->sstubs->contents + h->plt.offset,
9937 stub, htab->function_stub_size);
9939 /* Mark the symbol as undefined. plt.offset != -1 occurs
9940 only for the referenced symbol. */
9941 sym->st_shndx = SHN_UNDEF;
9943 /* The run-time linker uses the st_value field of the symbol
9944 to reset the global offset table entry for this external
9945 to its stub address when unlinking a shared object. */
9946 sym->st_value = (htab->sstubs->output_section->vma
9947 + htab->sstubs->output_offset
9951 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9952 refer to the stub, since only the stub uses the standard calling
9954 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9956 BFD_ASSERT (hmips->need_fn_stub);
9957 sym->st_value = (hmips->fn_stub->output_section->vma
9958 + hmips->fn_stub->output_offset);
9959 sym->st_size = hmips->fn_stub->size;
9960 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9963 BFD_ASSERT (h->dynindx != -1
9964 || h->forced_local);
9968 BFD_ASSERT (g != NULL);
9970 /* Run through the global symbol table, creating GOT entries for all
9971 the symbols that need them. */
9972 if (hmips->global_got_area != GGA_NONE)
9977 value = sym->st_value;
9978 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
9979 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9982 if (hmips->global_got_area != GGA_NONE && g->next)
9984 struct mips_got_entry e, *p;
9990 e.abfd = output_bfd;
9993 e.tls_type = GOT_TLS_NONE;
9995 for (g = g->next; g->next != gg; g = g->next)
9998 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10001 offset = p->gotidx;
10002 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
10004 || (elf_hash_table (info)->dynamic_sections_created
10006 && p->d.h->root.def_dynamic
10007 && !p->d.h->root.def_regular))
10009 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10010 the various compatibility problems, it's easier to mock
10011 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10012 mips_elf_create_dynamic_relocation to calculate the
10013 appropriate addend. */
10014 Elf_Internal_Rela rel[3];
10016 memset (rel, 0, sizeof (rel));
10017 if (ABI_64_P (output_bfd))
10018 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10020 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10021 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10024 if (! (mips_elf_create_dynamic_relocation
10025 (output_bfd, info, rel,
10026 e.d.h, NULL, sym->st_value, &entry, sgot)))
10030 entry = sym->st_value;
10031 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10036 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10037 name = h->root.root.string;
10038 if (h == elf_hash_table (info)->hdynamic
10039 || h == elf_hash_table (info)->hgot)
10040 sym->st_shndx = SHN_ABS;
10041 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10042 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10044 sym->st_shndx = SHN_ABS;
10045 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10048 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10050 sym->st_shndx = SHN_ABS;
10051 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10052 sym->st_value = elf_gp (output_bfd);
10054 else if (SGI_COMPAT (output_bfd))
10056 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10057 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10059 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10060 sym->st_other = STO_PROTECTED;
10062 sym->st_shndx = SHN_MIPS_DATA;
10064 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10066 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10067 sym->st_other = STO_PROTECTED;
10068 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10069 sym->st_shndx = SHN_ABS;
10071 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10073 if (h->type == STT_FUNC)
10074 sym->st_shndx = SHN_MIPS_TEXT;
10075 else if (h->type == STT_OBJECT)
10076 sym->st_shndx = SHN_MIPS_DATA;
10080 /* Emit a copy reloc, if needed. */
10086 BFD_ASSERT (h->dynindx != -1);
10087 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10089 s = mips_elf_rel_dyn_section (info, FALSE);
10090 symval = (h->root.u.def.section->output_section->vma
10091 + h->root.u.def.section->output_offset
10092 + h->root.u.def.value);
10093 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10094 h->dynindx, R_MIPS_COPY, symval);
10097 /* Handle the IRIX6-specific symbols. */
10098 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10099 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10101 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
10102 treat MIPS16 symbols like any other. */
10103 if (ELF_ST_IS_MIPS16 (sym->st_other))
10105 BFD_ASSERT (sym->st_value & 1);
10106 sym->st_other -= STO_MIPS16;
10112 /* Likewise, for VxWorks. */
10115 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10116 struct bfd_link_info *info,
10117 struct elf_link_hash_entry *h,
10118 Elf_Internal_Sym *sym)
10122 struct mips_got_info *g;
10123 struct mips_elf_link_hash_table *htab;
10124 struct mips_elf_link_hash_entry *hmips;
10126 htab = mips_elf_hash_table (info);
10127 BFD_ASSERT (htab != NULL);
10128 dynobj = elf_hash_table (info)->dynobj;
10129 hmips = (struct mips_elf_link_hash_entry *) h;
10131 if (h->plt.offset != (bfd_vma) -1)
10134 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
10135 Elf_Internal_Rela rel;
10136 static const bfd_vma *plt_entry;
10138 BFD_ASSERT (h->dynindx != -1);
10139 BFD_ASSERT (htab->splt != NULL);
10140 BFD_ASSERT (h->plt.offset <= htab->splt->size);
10142 /* Calculate the address of the .plt entry. */
10143 plt_address = (htab->splt->output_section->vma
10144 + htab->splt->output_offset
10147 /* Calculate the index of the entry. */
10148 plt_index = ((h->plt.offset - htab->plt_header_size)
10149 / htab->plt_entry_size);
10151 /* Calculate the address of the .got.plt entry. */
10152 got_address = (htab->sgotplt->output_section->vma
10153 + htab->sgotplt->output_offset
10156 /* Calculate the offset of the .got.plt entry from
10157 _GLOBAL_OFFSET_TABLE_. */
10158 got_offset = mips_elf_gotplt_index (info, h);
10160 /* Calculate the offset for the branch at the start of the PLT
10161 entry. The branch jumps to the beginning of .plt. */
10162 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
10164 /* Fill in the initial value of the .got.plt entry. */
10165 bfd_put_32 (output_bfd, plt_address,
10166 htab->sgotplt->contents + plt_index * 4);
10168 /* Find out where the .plt entry should go. */
10169 loc = htab->splt->contents + h->plt.offset;
10173 plt_entry = mips_vxworks_shared_plt_entry;
10174 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10175 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10179 bfd_vma got_address_high, got_address_low;
10181 plt_entry = mips_vxworks_exec_plt_entry;
10182 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10183 got_address_low = got_address & 0xffff;
10185 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10186 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10187 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10188 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10189 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10190 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10191 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10192 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10194 loc = (htab->srelplt2->contents
10195 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10197 /* Emit a relocation for the .got.plt entry. */
10198 rel.r_offset = got_address;
10199 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10200 rel.r_addend = h->plt.offset;
10201 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10203 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10204 loc += sizeof (Elf32_External_Rela);
10205 rel.r_offset = plt_address + 8;
10206 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10207 rel.r_addend = got_offset;
10208 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10210 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10211 loc += sizeof (Elf32_External_Rela);
10213 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10214 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10217 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10218 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
10219 rel.r_offset = got_address;
10220 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10222 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10224 if (!h->def_regular)
10225 sym->st_shndx = SHN_UNDEF;
10228 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10231 g = htab->got_info;
10232 BFD_ASSERT (g != NULL);
10234 /* See if this symbol has an entry in the GOT. */
10235 if (hmips->global_got_area != GGA_NONE)
10238 Elf_Internal_Rela outrel;
10242 /* Install the symbol value in the GOT. */
10243 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10244 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10246 /* Add a dynamic relocation for it. */
10247 s = mips_elf_rel_dyn_section (info, FALSE);
10248 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10249 outrel.r_offset = (sgot->output_section->vma
10250 + sgot->output_offset
10252 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10253 outrel.r_addend = 0;
10254 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10257 /* Emit a copy reloc, if needed. */
10260 Elf_Internal_Rela rel;
10262 BFD_ASSERT (h->dynindx != -1);
10264 rel.r_offset = (h->root.u.def.section->output_section->vma
10265 + h->root.u.def.section->output_offset
10266 + h->root.u.def.value);
10267 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10269 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10270 htab->srelbss->contents
10271 + (htab->srelbss->reloc_count
10272 * sizeof (Elf32_External_Rela)));
10273 ++htab->srelbss->reloc_count;
10276 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10277 if (ELF_ST_IS_COMPRESSED (sym->st_other))
10278 sym->st_value &= ~1;
10283 /* Write out a plt0 entry to the beginning of .plt. */
10286 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10289 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10290 static const bfd_vma *plt_entry;
10291 struct mips_elf_link_hash_table *htab;
10293 htab = mips_elf_hash_table (info);
10294 BFD_ASSERT (htab != NULL);
10296 if (ABI_64_P (output_bfd))
10297 plt_entry = mips_n64_exec_plt0_entry;
10298 else if (ABI_N32_P (output_bfd))
10299 plt_entry = mips_n32_exec_plt0_entry;
10301 plt_entry = mips_o32_exec_plt0_entry;
10303 /* Calculate the value of .got.plt. */
10304 gotplt_value = (htab->sgotplt->output_section->vma
10305 + htab->sgotplt->output_offset);
10306 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10307 gotplt_value_low = gotplt_value & 0xffff;
10309 /* The PLT sequence is not safe for N64 if .got.plt's address can
10310 not be loaded in two instructions. */
10311 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10312 || ~(gotplt_value | 0x7fffffff) == 0);
10314 /* Install the PLT header. */
10315 loc = htab->splt->contents;
10316 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10317 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10318 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10319 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10320 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10321 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10322 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10323 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10326 /* Install the PLT header for a VxWorks executable and finalize the
10327 contents of .rela.plt.unloaded. */
10330 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10332 Elf_Internal_Rela rela;
10334 bfd_vma got_value, got_value_high, got_value_low, plt_address;
10335 static const bfd_vma *plt_entry;
10336 struct mips_elf_link_hash_table *htab;
10338 htab = mips_elf_hash_table (info);
10339 BFD_ASSERT (htab != NULL);
10341 plt_entry = mips_vxworks_exec_plt0_entry;
10343 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
10344 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
10345 + htab->root.hgot->root.u.def.section->output_offset
10346 + htab->root.hgot->root.u.def.value);
10348 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
10349 got_value_low = got_value & 0xffff;
10351 /* Calculate the address of the PLT header. */
10352 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
10354 /* Install the PLT header. */
10355 loc = htab->splt->contents;
10356 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
10357 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
10358 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10359 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10360 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10361 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10363 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
10364 loc = htab->srelplt2->contents;
10365 rela.r_offset = plt_address;
10366 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10368 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10369 loc += sizeof (Elf32_External_Rela);
10371 /* Output the relocation for the following addiu of
10372 %lo(_GLOBAL_OFFSET_TABLE_). */
10373 rela.r_offset += 4;
10374 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10375 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10376 loc += sizeof (Elf32_External_Rela);
10378 /* Fix up the remaining relocations. They may have the wrong
10379 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10380 in which symbols were output. */
10381 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10383 Elf_Internal_Rela rel;
10385 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10386 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10387 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10388 loc += sizeof (Elf32_External_Rela);
10390 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10391 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10392 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10393 loc += sizeof (Elf32_External_Rela);
10395 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10396 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10397 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10398 loc += sizeof (Elf32_External_Rela);
10402 /* Install the PLT header for a VxWorks shared library. */
10405 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10408 struct mips_elf_link_hash_table *htab;
10410 htab = mips_elf_hash_table (info);
10411 BFD_ASSERT (htab != NULL);
10413 /* We just need to copy the entry byte-by-byte. */
10414 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10415 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10416 htab->splt->contents + i * 4);
10419 /* Finish up the dynamic sections. */
10422 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10423 struct bfd_link_info *info)
10428 struct mips_got_info *gg, *g;
10429 struct mips_elf_link_hash_table *htab;
10431 htab = mips_elf_hash_table (info);
10432 BFD_ASSERT (htab != NULL);
10434 dynobj = elf_hash_table (info)->dynobj;
10436 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
10439 gg = htab->got_info;
10441 if (elf_hash_table (info)->dynamic_sections_created)
10444 int dyn_to_skip = 0, dyn_skipped = 0;
10446 BFD_ASSERT (sdyn != NULL);
10447 BFD_ASSERT (gg != NULL);
10449 g = mips_elf_bfd_got (output_bfd, FALSE);
10450 BFD_ASSERT (g != NULL);
10452 for (b = sdyn->contents;
10453 b < sdyn->contents + sdyn->size;
10454 b += MIPS_ELF_DYN_SIZE (dynobj))
10456 Elf_Internal_Dyn dyn;
10460 bfd_boolean swap_out_p;
10462 /* Read in the current dynamic entry. */
10463 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10465 /* Assume that we're going to modify it and write it out. */
10471 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10475 BFD_ASSERT (htab->is_vxworks);
10476 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10480 /* Rewrite DT_STRSZ. */
10482 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10487 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10490 case DT_MIPS_PLTGOT:
10492 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10495 case DT_MIPS_RLD_VERSION:
10496 dyn.d_un.d_val = 1; /* XXX */
10499 case DT_MIPS_FLAGS:
10500 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10503 case DT_MIPS_TIME_STAMP:
10507 dyn.d_un.d_val = t;
10511 case DT_MIPS_ICHECKSUM:
10513 swap_out_p = FALSE;
10516 case DT_MIPS_IVERSION:
10518 swap_out_p = FALSE;
10521 case DT_MIPS_BASE_ADDRESS:
10522 s = output_bfd->sections;
10523 BFD_ASSERT (s != NULL);
10524 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10527 case DT_MIPS_LOCAL_GOTNO:
10528 dyn.d_un.d_val = g->local_gotno;
10531 case DT_MIPS_UNREFEXTNO:
10532 /* The index into the dynamic symbol table which is the
10533 entry of the first external symbol that is not
10534 referenced within the same object. */
10535 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10538 case DT_MIPS_GOTSYM:
10539 if (htab->global_gotsym)
10541 dyn.d_un.d_val = htab->global_gotsym->dynindx;
10544 /* In case if we don't have global got symbols we default
10545 to setting DT_MIPS_GOTSYM to the same value as
10546 DT_MIPS_SYMTABNO, so we just fall through. */
10548 case DT_MIPS_SYMTABNO:
10550 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10551 s = bfd_get_section_by_name (output_bfd, name);
10552 BFD_ASSERT (s != NULL);
10554 dyn.d_un.d_val = s->size / elemsize;
10557 case DT_MIPS_HIPAGENO:
10558 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10561 case DT_MIPS_RLD_MAP:
10563 struct elf_link_hash_entry *h;
10564 h = mips_elf_hash_table (info)->rld_symbol;
10567 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10568 swap_out_p = FALSE;
10571 s = h->root.u.def.section;
10572 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
10573 + h->root.u.def.value);
10577 case DT_MIPS_OPTIONS:
10578 s = (bfd_get_section_by_name
10579 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10580 dyn.d_un.d_ptr = s->vma;
10584 BFD_ASSERT (htab->is_vxworks);
10585 /* The count does not include the JUMP_SLOT relocations. */
10587 dyn.d_un.d_val -= htab->srelplt->size;
10591 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10592 if (htab->is_vxworks)
10593 dyn.d_un.d_val = DT_RELA;
10595 dyn.d_un.d_val = DT_REL;
10599 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10600 dyn.d_un.d_val = htab->srelplt->size;
10604 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10605 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10606 + htab->srelplt->output_offset);
10610 /* If we didn't need any text relocations after all, delete
10611 the dynamic tag. */
10612 if (!(info->flags & DF_TEXTREL))
10614 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10615 swap_out_p = FALSE;
10620 /* If we didn't need any text relocations after all, clear
10621 DF_TEXTREL from DT_FLAGS. */
10622 if (!(info->flags & DF_TEXTREL))
10623 dyn.d_un.d_val &= ~DF_TEXTREL;
10625 swap_out_p = FALSE;
10629 swap_out_p = FALSE;
10630 if (htab->is_vxworks
10631 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10636 if (swap_out_p || dyn_skipped)
10637 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10638 (dynobj, &dyn, b - dyn_skipped);
10642 dyn_skipped += dyn_to_skip;
10647 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10648 if (dyn_skipped > 0)
10649 memset (b - dyn_skipped, 0, dyn_skipped);
10652 if (sgot != NULL && sgot->size > 0
10653 && !bfd_is_abs_section (sgot->output_section))
10655 if (htab->is_vxworks)
10657 /* The first entry of the global offset table points to the
10658 ".dynamic" section. The second is initialized by the
10659 loader and contains the shared library identifier.
10660 The third is also initialized by the loader and points
10661 to the lazy resolution stub. */
10662 MIPS_ELF_PUT_WORD (output_bfd,
10663 sdyn->output_offset + sdyn->output_section->vma,
10665 MIPS_ELF_PUT_WORD (output_bfd, 0,
10666 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10667 MIPS_ELF_PUT_WORD (output_bfd, 0,
10669 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10673 /* The first entry of the global offset table will be filled at
10674 runtime. The second entry will be used by some runtime loaders.
10675 This isn't the case of IRIX rld. */
10676 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10677 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10678 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10681 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10682 = MIPS_ELF_GOT_SIZE (output_bfd);
10685 /* Generate dynamic relocations for the non-primary gots. */
10686 if (gg != NULL && gg->next)
10688 Elf_Internal_Rela rel[3];
10689 bfd_vma addend = 0;
10691 memset (rel, 0, sizeof (rel));
10692 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10694 for (g = gg->next; g->next != gg; g = g->next)
10696 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
10697 + g->next->tls_gotno;
10699 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10700 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10701 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10703 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10705 if (! info->shared)
10708 while (got_index < g->assigned_gotno)
10710 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10711 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10712 if (!(mips_elf_create_dynamic_relocation
10713 (output_bfd, info, rel, NULL,
10714 bfd_abs_section_ptr,
10715 0, &addend, sgot)))
10717 BFD_ASSERT (addend == 0);
10722 /* The generation of dynamic relocations for the non-primary gots
10723 adds more dynamic relocations. We cannot count them until
10726 if (elf_hash_table (info)->dynamic_sections_created)
10729 bfd_boolean swap_out_p;
10731 BFD_ASSERT (sdyn != NULL);
10733 for (b = sdyn->contents;
10734 b < sdyn->contents + sdyn->size;
10735 b += MIPS_ELF_DYN_SIZE (dynobj))
10737 Elf_Internal_Dyn dyn;
10740 /* Read in the current dynamic entry. */
10741 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10743 /* Assume that we're going to modify it and write it out. */
10749 /* Reduce DT_RELSZ to account for any relocations we
10750 decided not to make. This is for the n64 irix rld,
10751 which doesn't seem to apply any relocations if there
10752 are trailing null entries. */
10753 s = mips_elf_rel_dyn_section (info, FALSE);
10754 dyn.d_un.d_val = (s->reloc_count
10755 * (ABI_64_P (output_bfd)
10756 ? sizeof (Elf64_Mips_External_Rel)
10757 : sizeof (Elf32_External_Rel)));
10758 /* Adjust the section size too. Tools like the prelinker
10759 can reasonably expect the values to the same. */
10760 elf_section_data (s->output_section)->this_hdr.sh_size
10765 swap_out_p = FALSE;
10770 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10777 Elf32_compact_rel cpt;
10779 if (SGI_COMPAT (output_bfd))
10781 /* Write .compact_rel section out. */
10782 s = bfd_get_linker_section (dynobj, ".compact_rel");
10786 cpt.num = s->reloc_count;
10788 cpt.offset = (s->output_section->filepos
10789 + sizeof (Elf32_External_compact_rel));
10792 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10793 ((Elf32_External_compact_rel *)
10796 /* Clean up a dummy stub function entry in .text. */
10797 if (htab->sstubs != NULL)
10799 file_ptr dummy_offset;
10801 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10802 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10803 memset (htab->sstubs->contents + dummy_offset, 0,
10804 htab->function_stub_size);
10809 /* The psABI says that the dynamic relocations must be sorted in
10810 increasing order of r_symndx. The VxWorks EABI doesn't require
10811 this, and because the code below handles REL rather than RELA
10812 relocations, using it for VxWorks would be outright harmful. */
10813 if (!htab->is_vxworks)
10815 s = mips_elf_rel_dyn_section (info, FALSE);
10817 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10819 reldyn_sorting_bfd = output_bfd;
10821 if (ABI_64_P (output_bfd))
10822 qsort ((Elf64_External_Rel *) s->contents + 1,
10823 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10824 sort_dynamic_relocs_64);
10826 qsort ((Elf32_External_Rel *) s->contents + 1,
10827 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10828 sort_dynamic_relocs);
10833 if (htab->splt && htab->splt->size > 0)
10835 if (htab->is_vxworks)
10838 mips_vxworks_finish_shared_plt (output_bfd, info);
10840 mips_vxworks_finish_exec_plt (output_bfd, info);
10844 BFD_ASSERT (!info->shared);
10845 mips_finish_exec_plt (output_bfd, info);
10852 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10855 mips_set_isa_flags (bfd *abfd)
10859 switch (bfd_get_mach (abfd))
10862 case bfd_mach_mips3000:
10863 val = E_MIPS_ARCH_1;
10866 case bfd_mach_mips3900:
10867 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10870 case bfd_mach_mips6000:
10871 val = E_MIPS_ARCH_2;
10874 case bfd_mach_mips4000:
10875 case bfd_mach_mips4300:
10876 case bfd_mach_mips4400:
10877 case bfd_mach_mips4600:
10878 val = E_MIPS_ARCH_3;
10881 case bfd_mach_mips4010:
10882 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10885 case bfd_mach_mips4100:
10886 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10889 case bfd_mach_mips4111:
10890 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10893 case bfd_mach_mips4120:
10894 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10897 case bfd_mach_mips4650:
10898 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10901 case bfd_mach_mips5400:
10902 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10905 case bfd_mach_mips5500:
10906 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10909 case bfd_mach_mips5900:
10910 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
10913 case bfd_mach_mips9000:
10914 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10917 case bfd_mach_mips5000:
10918 case bfd_mach_mips7000:
10919 case bfd_mach_mips8000:
10920 case bfd_mach_mips10000:
10921 case bfd_mach_mips12000:
10922 case bfd_mach_mips14000:
10923 case bfd_mach_mips16000:
10924 val = E_MIPS_ARCH_4;
10927 case bfd_mach_mips5:
10928 val = E_MIPS_ARCH_5;
10931 case bfd_mach_mips_loongson_2e:
10932 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10935 case bfd_mach_mips_loongson_2f:
10936 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10939 case bfd_mach_mips_sb1:
10940 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10943 case bfd_mach_mips_loongson_3a:
10944 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
10947 case bfd_mach_mips_octeon:
10948 case bfd_mach_mips_octeonp:
10949 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10952 case bfd_mach_mips_xlr:
10953 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10956 case bfd_mach_mips_octeon2:
10957 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
10960 case bfd_mach_mipsisa32:
10961 val = E_MIPS_ARCH_32;
10964 case bfd_mach_mipsisa64:
10965 val = E_MIPS_ARCH_64;
10968 case bfd_mach_mipsisa32r2:
10969 val = E_MIPS_ARCH_32R2;
10972 case bfd_mach_mipsisa64r2:
10973 val = E_MIPS_ARCH_64R2;
10976 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10977 elf_elfheader (abfd)->e_flags |= val;
10982 /* The final processing done just before writing out a MIPS ELF object
10983 file. This gets the MIPS architecture right based on the machine
10984 number. This is used by both the 32-bit and the 64-bit ABI. */
10987 _bfd_mips_elf_final_write_processing (bfd *abfd,
10988 bfd_boolean linker ATTRIBUTE_UNUSED)
10991 Elf_Internal_Shdr **hdrpp;
10995 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10996 is nonzero. This is for compatibility with old objects, which used
10997 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10998 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10999 mips_set_isa_flags (abfd);
11001 /* Set the sh_info field for .gptab sections and other appropriate
11002 info for each special section. */
11003 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11004 i < elf_numsections (abfd);
11007 switch ((*hdrpp)->sh_type)
11009 case SHT_MIPS_MSYM:
11010 case SHT_MIPS_LIBLIST:
11011 sec = bfd_get_section_by_name (abfd, ".dynstr");
11013 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11016 case SHT_MIPS_GPTAB:
11017 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11018 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11019 BFD_ASSERT (name != NULL
11020 && CONST_STRNEQ (name, ".gptab."));
11021 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11022 BFD_ASSERT (sec != NULL);
11023 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11026 case SHT_MIPS_CONTENT:
11027 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11028 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11029 BFD_ASSERT (name != NULL
11030 && CONST_STRNEQ (name, ".MIPS.content"));
11031 sec = bfd_get_section_by_name (abfd,
11032 name + sizeof ".MIPS.content" - 1);
11033 BFD_ASSERT (sec != NULL);
11034 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11037 case SHT_MIPS_SYMBOL_LIB:
11038 sec = bfd_get_section_by_name (abfd, ".dynsym");
11040 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11041 sec = bfd_get_section_by_name (abfd, ".liblist");
11043 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11046 case SHT_MIPS_EVENTS:
11047 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11048 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11049 BFD_ASSERT (name != NULL);
11050 if (CONST_STRNEQ (name, ".MIPS.events"))
11051 sec = bfd_get_section_by_name (abfd,
11052 name + sizeof ".MIPS.events" - 1);
11055 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
11056 sec = bfd_get_section_by_name (abfd,
11058 + sizeof ".MIPS.post_rel" - 1));
11060 BFD_ASSERT (sec != NULL);
11061 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11068 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11072 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11073 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11078 /* See if we need a PT_MIPS_REGINFO segment. */
11079 s = bfd_get_section_by_name (abfd, ".reginfo");
11080 if (s && (s->flags & SEC_LOAD))
11083 /* See if we need a PT_MIPS_OPTIONS segment. */
11084 if (IRIX_COMPAT (abfd) == ict_irix6
11085 && bfd_get_section_by_name (abfd,
11086 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11089 /* See if we need a PT_MIPS_RTPROC segment. */
11090 if (IRIX_COMPAT (abfd) == ict_irix5
11091 && bfd_get_section_by_name (abfd, ".dynamic")
11092 && bfd_get_section_by_name (abfd, ".mdebug"))
11095 /* Allocate a PT_NULL header in dynamic objects. See
11096 _bfd_mips_elf_modify_segment_map for details. */
11097 if (!SGI_COMPAT (abfd)
11098 && bfd_get_section_by_name (abfd, ".dynamic"))
11104 /* Modify the segment map for an IRIX5 executable. */
11107 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11108 struct bfd_link_info *info)
11111 struct elf_segment_map *m, **pm;
11114 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11116 s = bfd_get_section_by_name (abfd, ".reginfo");
11117 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11119 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11120 if (m->p_type == PT_MIPS_REGINFO)
11125 m = bfd_zalloc (abfd, amt);
11129 m->p_type = PT_MIPS_REGINFO;
11131 m->sections[0] = s;
11133 /* We want to put it after the PHDR and INTERP segments. */
11134 pm = &elf_tdata (abfd)->segment_map;
11136 && ((*pm)->p_type == PT_PHDR
11137 || (*pm)->p_type == PT_INTERP))
11145 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11146 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11147 PT_MIPS_OPTIONS segment immediately following the program header
11149 if (NEWABI_P (abfd)
11150 /* On non-IRIX6 new abi, we'll have already created a segment
11151 for this section, so don't create another. I'm not sure this
11152 is not also the case for IRIX 6, but I can't test it right
11154 && IRIX_COMPAT (abfd) == ict_irix6)
11156 for (s = abfd->sections; s; s = s->next)
11157 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11162 struct elf_segment_map *options_segment;
11164 pm = &elf_tdata (abfd)->segment_map;
11166 && ((*pm)->p_type == PT_PHDR
11167 || (*pm)->p_type == PT_INTERP))
11170 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11172 amt = sizeof (struct elf_segment_map);
11173 options_segment = bfd_zalloc (abfd, amt);
11174 options_segment->next = *pm;
11175 options_segment->p_type = PT_MIPS_OPTIONS;
11176 options_segment->p_flags = PF_R;
11177 options_segment->p_flags_valid = TRUE;
11178 options_segment->count = 1;
11179 options_segment->sections[0] = s;
11180 *pm = options_segment;
11186 if (IRIX_COMPAT (abfd) == ict_irix5)
11188 /* If there are .dynamic and .mdebug sections, we make a room
11189 for the RTPROC header. FIXME: Rewrite without section names. */
11190 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11191 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11192 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11194 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11195 if (m->p_type == PT_MIPS_RTPROC)
11200 m = bfd_zalloc (abfd, amt);
11204 m->p_type = PT_MIPS_RTPROC;
11206 s = bfd_get_section_by_name (abfd, ".rtproc");
11211 m->p_flags_valid = 1;
11216 m->sections[0] = s;
11219 /* We want to put it after the DYNAMIC segment. */
11220 pm = &elf_tdata (abfd)->segment_map;
11221 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11231 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
11232 .dynstr, .dynsym, and .hash sections, and everything in
11234 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
11236 if ((*pm)->p_type == PT_DYNAMIC)
11239 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
11241 /* For a normal mips executable the permissions for the PT_DYNAMIC
11242 segment are read, write and execute. We do that here since
11243 the code in elf.c sets only the read permission. This matters
11244 sometimes for the dynamic linker. */
11245 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
11247 m->p_flags = PF_R | PF_W | PF_X;
11248 m->p_flags_valid = 1;
11251 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11252 glibc's dynamic linker has traditionally derived the number of
11253 tags from the p_filesz field, and sometimes allocates stack
11254 arrays of that size. An overly-big PT_DYNAMIC segment can
11255 be actively harmful in such cases. Making PT_DYNAMIC contain
11256 other sections can also make life hard for the prelinker,
11257 which might move one of the other sections to a different
11258 PT_LOAD segment. */
11259 if (SGI_COMPAT (abfd)
11262 && strcmp (m->sections[0]->name, ".dynamic") == 0)
11264 static const char *sec_names[] =
11266 ".dynamic", ".dynstr", ".dynsym", ".hash"
11270 struct elf_segment_map *n;
11272 low = ~(bfd_vma) 0;
11274 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11276 s = bfd_get_section_by_name (abfd, sec_names[i]);
11277 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11284 if (high < s->vma + sz)
11285 high = s->vma + sz;
11290 for (s = abfd->sections; s != NULL; s = s->next)
11291 if ((s->flags & SEC_LOAD) != 0
11293 && s->vma + s->size <= high)
11296 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
11297 n = bfd_zalloc (abfd, amt);
11304 for (s = abfd->sections; s != NULL; s = s->next)
11306 if ((s->flags & SEC_LOAD) != 0
11308 && s->vma + s->size <= high)
11310 n->sections[i] = s;
11319 /* Allocate a spare program header in dynamic objects so that tools
11320 like the prelinker can add an extra PT_LOAD entry.
11322 If the prelinker needs to make room for a new PT_LOAD entry, its
11323 standard procedure is to move the first (read-only) sections into
11324 the new (writable) segment. However, the MIPS ABI requires
11325 .dynamic to be in a read-only segment, and the section will often
11326 start within sizeof (ElfNN_Phdr) bytes of the last program header.
11328 Although the prelinker could in principle move .dynamic to a
11329 writable segment, it seems better to allocate a spare program
11330 header instead, and avoid the need to move any sections.
11331 There is a long tradition of allocating spare dynamic tags,
11332 so allocating a spare program header seems like a natural
11335 If INFO is NULL, we may be copying an already prelinked binary
11336 with objcopy or strip, so do not add this header. */
11338 && !SGI_COMPAT (abfd)
11339 && bfd_get_section_by_name (abfd, ".dynamic"))
11341 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
11342 if ((*pm)->p_type == PT_NULL)
11346 m = bfd_zalloc (abfd, sizeof (*m));
11350 m->p_type = PT_NULL;
11358 /* Return the section that should be marked against GC for a given
11362 _bfd_mips_elf_gc_mark_hook (asection *sec,
11363 struct bfd_link_info *info,
11364 Elf_Internal_Rela *rel,
11365 struct elf_link_hash_entry *h,
11366 Elf_Internal_Sym *sym)
11368 /* ??? Do mips16 stub sections need to be handled special? */
11371 switch (ELF_R_TYPE (sec->owner, rel->r_info))
11373 case R_MIPS_GNU_VTINHERIT:
11374 case R_MIPS_GNU_VTENTRY:
11378 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
11381 /* Update the got entry reference counts for the section being removed. */
11384 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11385 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11386 asection *sec ATTRIBUTE_UNUSED,
11387 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
11390 Elf_Internal_Shdr *symtab_hdr;
11391 struct elf_link_hash_entry **sym_hashes;
11392 bfd_signed_vma *local_got_refcounts;
11393 const Elf_Internal_Rela *rel, *relend;
11394 unsigned long r_symndx;
11395 struct elf_link_hash_entry *h;
11397 if (info->relocatable)
11400 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11401 sym_hashes = elf_sym_hashes (abfd);
11402 local_got_refcounts = elf_local_got_refcounts (abfd);
11404 relend = relocs + sec->reloc_count;
11405 for (rel = relocs; rel < relend; rel++)
11406 switch (ELF_R_TYPE (abfd, rel->r_info))
11408 case R_MIPS16_GOT16:
11409 case R_MIPS16_CALL16:
11411 case R_MIPS_CALL16:
11412 case R_MIPS_CALL_HI16:
11413 case R_MIPS_CALL_LO16:
11414 case R_MIPS_GOT_HI16:
11415 case R_MIPS_GOT_LO16:
11416 case R_MIPS_GOT_DISP:
11417 case R_MIPS_GOT_PAGE:
11418 case R_MIPS_GOT_OFST:
11419 case R_MICROMIPS_GOT16:
11420 case R_MICROMIPS_CALL16:
11421 case R_MICROMIPS_CALL_HI16:
11422 case R_MICROMIPS_CALL_LO16:
11423 case R_MICROMIPS_GOT_HI16:
11424 case R_MICROMIPS_GOT_LO16:
11425 case R_MICROMIPS_GOT_DISP:
11426 case R_MICROMIPS_GOT_PAGE:
11427 case R_MICROMIPS_GOT_OFST:
11428 /* ??? It would seem that the existing MIPS code does no sort
11429 of reference counting or whatnot on its GOT and PLT entries,
11430 so it is not possible to garbage collect them at this time. */
11441 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11442 hiding the old indirect symbol. Process additional relocation
11443 information. Also called for weakdefs, in which case we just let
11444 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11447 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
11448 struct elf_link_hash_entry *dir,
11449 struct elf_link_hash_entry *ind)
11451 struct mips_elf_link_hash_entry *dirmips, *indmips;
11453 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
11455 dirmips = (struct mips_elf_link_hash_entry *) dir;
11456 indmips = (struct mips_elf_link_hash_entry *) ind;
11457 /* Any absolute non-dynamic relocations against an indirect or weak
11458 definition will be against the target symbol. */
11459 if (indmips->has_static_relocs)
11460 dirmips->has_static_relocs = TRUE;
11462 if (ind->root.type != bfd_link_hash_indirect)
11465 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11466 if (indmips->readonly_reloc)
11467 dirmips->readonly_reloc = TRUE;
11468 if (indmips->no_fn_stub)
11469 dirmips->no_fn_stub = TRUE;
11470 if (indmips->fn_stub)
11472 dirmips->fn_stub = indmips->fn_stub;
11473 indmips->fn_stub = NULL;
11475 if (indmips->need_fn_stub)
11477 dirmips->need_fn_stub = TRUE;
11478 indmips->need_fn_stub = FALSE;
11480 if (indmips->call_stub)
11482 dirmips->call_stub = indmips->call_stub;
11483 indmips->call_stub = NULL;
11485 if (indmips->call_fp_stub)
11487 dirmips->call_fp_stub = indmips->call_fp_stub;
11488 indmips->call_fp_stub = NULL;
11490 if (indmips->global_got_area < dirmips->global_got_area)
11491 dirmips->global_got_area = indmips->global_got_area;
11492 if (indmips->global_got_area < GGA_NONE)
11493 indmips->global_got_area = GGA_NONE;
11494 if (indmips->has_nonpic_branches)
11495 dirmips->has_nonpic_branches = TRUE;
11498 #define PDR_SIZE 32
11501 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11502 struct bfd_link_info *info)
11505 bfd_boolean ret = FALSE;
11506 unsigned char *tdata;
11509 o = bfd_get_section_by_name (abfd, ".pdr");
11514 if (o->size % PDR_SIZE != 0)
11516 if (o->output_section != NULL
11517 && bfd_is_abs_section (o->output_section))
11520 tdata = bfd_zmalloc (o->size / PDR_SIZE);
11524 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11525 info->keep_memory);
11532 cookie->rel = cookie->rels;
11533 cookie->relend = cookie->rels + o->reloc_count;
11535 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11537 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11546 mips_elf_section_data (o)->u.tdata = tdata;
11547 o->size -= skip * PDR_SIZE;
11553 if (! info->keep_memory)
11554 free (cookie->rels);
11560 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11562 if (strcmp (sec->name, ".pdr") == 0)
11568 _bfd_mips_elf_write_section (bfd *output_bfd,
11569 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11570 asection *sec, bfd_byte *contents)
11572 bfd_byte *to, *from, *end;
11575 if (strcmp (sec->name, ".pdr") != 0)
11578 if (mips_elf_section_data (sec)->u.tdata == NULL)
11582 end = contents + sec->size;
11583 for (from = contents, i = 0;
11585 from += PDR_SIZE, i++)
11587 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11590 memcpy (to, from, PDR_SIZE);
11593 bfd_set_section_contents (output_bfd, sec->output_section, contents,
11594 sec->output_offset, sec->size);
11598 /* microMIPS code retains local labels for linker relaxation. Omit them
11599 from output by default for clarity. */
11602 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
11604 return _bfd_elf_is_local_label_name (abfd, sym->name);
11607 /* MIPS ELF uses a special find_nearest_line routine in order the
11608 handle the ECOFF debugging information. */
11610 struct mips_elf_find_line
11612 struct ecoff_debug_info d;
11613 struct ecoff_find_line i;
11617 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11618 asymbol **symbols, bfd_vma offset,
11619 const char **filename_ptr,
11620 const char **functionname_ptr,
11621 unsigned int *line_ptr)
11625 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11626 filename_ptr, functionname_ptr,
11630 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
11631 section, symbols, offset,
11632 filename_ptr, functionname_ptr,
11633 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
11634 &elf_tdata (abfd)->dwarf2_find_line_info))
11637 msec = bfd_get_section_by_name (abfd, ".mdebug");
11640 flagword origflags;
11641 struct mips_elf_find_line *fi;
11642 const struct ecoff_debug_swap * const swap =
11643 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11645 /* If we are called during a link, mips_elf_final_link may have
11646 cleared the SEC_HAS_CONTENTS field. We force it back on here
11647 if appropriate (which it normally will be). */
11648 origflags = msec->flags;
11649 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11650 msec->flags |= SEC_HAS_CONTENTS;
11652 fi = elf_tdata (abfd)->find_line_info;
11655 bfd_size_type external_fdr_size;
11658 struct fdr *fdr_ptr;
11659 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11661 fi = bfd_zalloc (abfd, amt);
11664 msec->flags = origflags;
11668 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11670 msec->flags = origflags;
11674 /* Swap in the FDR information. */
11675 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11676 fi->d.fdr = bfd_alloc (abfd, amt);
11677 if (fi->d.fdr == NULL)
11679 msec->flags = origflags;
11682 external_fdr_size = swap->external_fdr_size;
11683 fdr_ptr = fi->d.fdr;
11684 fraw_src = (char *) fi->d.external_fdr;
11685 fraw_end = (fraw_src
11686 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11687 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11688 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11690 elf_tdata (abfd)->find_line_info = fi;
11692 /* Note that we don't bother to ever free this information.
11693 find_nearest_line is either called all the time, as in
11694 objdump -l, so the information should be saved, or it is
11695 rarely called, as in ld error messages, so the memory
11696 wasted is unimportant. Still, it would probably be a
11697 good idea for free_cached_info to throw it away. */
11700 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11701 &fi->i, filename_ptr, functionname_ptr,
11704 msec->flags = origflags;
11708 msec->flags = origflags;
11711 /* Fall back on the generic ELF find_nearest_line routine. */
11713 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11714 filename_ptr, functionname_ptr,
11719 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11720 const char **filename_ptr,
11721 const char **functionname_ptr,
11722 unsigned int *line_ptr)
11725 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11726 functionname_ptr, line_ptr,
11727 & elf_tdata (abfd)->dwarf2_find_line_info);
11732 /* When are writing out the .options or .MIPS.options section,
11733 remember the bytes we are writing out, so that we can install the
11734 GP value in the section_processing routine. */
11737 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11738 const void *location,
11739 file_ptr offset, bfd_size_type count)
11741 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11745 if (elf_section_data (section) == NULL)
11747 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11748 section->used_by_bfd = bfd_zalloc (abfd, amt);
11749 if (elf_section_data (section) == NULL)
11752 c = mips_elf_section_data (section)->u.tdata;
11755 c = bfd_zalloc (abfd, section->size);
11758 mips_elf_section_data (section)->u.tdata = c;
11761 memcpy (c + offset, location, count);
11764 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11768 /* This is almost identical to bfd_generic_get_... except that some
11769 MIPS relocations need to be handled specially. Sigh. */
11772 _bfd_elf_mips_get_relocated_section_contents
11774 struct bfd_link_info *link_info,
11775 struct bfd_link_order *link_order,
11777 bfd_boolean relocatable,
11780 /* Get enough memory to hold the stuff */
11781 bfd *input_bfd = link_order->u.indirect.section->owner;
11782 asection *input_section = link_order->u.indirect.section;
11785 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11786 arelent **reloc_vector = NULL;
11789 if (reloc_size < 0)
11792 reloc_vector = bfd_malloc (reloc_size);
11793 if (reloc_vector == NULL && reloc_size != 0)
11796 /* read in the section */
11797 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11798 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11801 reloc_count = bfd_canonicalize_reloc (input_bfd,
11805 if (reloc_count < 0)
11808 if (reloc_count > 0)
11813 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11816 struct bfd_hash_entry *h;
11817 struct bfd_link_hash_entry *lh;
11818 /* Skip all this stuff if we aren't mixing formats. */
11819 if (abfd && input_bfd
11820 && abfd->xvec == input_bfd->xvec)
11824 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11825 lh = (struct bfd_link_hash_entry *) h;
11832 case bfd_link_hash_undefined:
11833 case bfd_link_hash_undefweak:
11834 case bfd_link_hash_common:
11837 case bfd_link_hash_defined:
11838 case bfd_link_hash_defweak:
11840 gp = lh->u.def.value;
11842 case bfd_link_hash_indirect:
11843 case bfd_link_hash_warning:
11845 /* @@FIXME ignoring warning for now */
11847 case bfd_link_hash_new:
11856 for (parent = reloc_vector; *parent != NULL; parent++)
11858 char *error_message = NULL;
11859 bfd_reloc_status_type r;
11861 /* Specific to MIPS: Deal with relocation types that require
11862 knowing the gp of the output bfd. */
11863 asymbol *sym = *(*parent)->sym_ptr_ptr;
11865 /* If we've managed to find the gp and have a special
11866 function for the relocation then go ahead, else default
11867 to the generic handling. */
11869 && (*parent)->howto->special_function
11870 == _bfd_mips_elf32_gprel16_reloc)
11871 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11872 input_section, relocatable,
11875 r = bfd_perform_relocation (input_bfd, *parent, data,
11877 relocatable ? abfd : NULL,
11882 asection *os = input_section->output_section;
11884 /* A partial link, so keep the relocs */
11885 os->orelocation[os->reloc_count] = *parent;
11889 if (r != bfd_reloc_ok)
11893 case bfd_reloc_undefined:
11894 if (!((*link_info->callbacks->undefined_symbol)
11895 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11896 input_bfd, input_section, (*parent)->address, TRUE)))
11899 case bfd_reloc_dangerous:
11900 BFD_ASSERT (error_message != NULL);
11901 if (!((*link_info->callbacks->reloc_dangerous)
11902 (link_info, error_message, input_bfd, input_section,
11903 (*parent)->address)))
11906 case bfd_reloc_overflow:
11907 if (!((*link_info->callbacks->reloc_overflow)
11909 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11910 (*parent)->howto->name, (*parent)->addend,
11911 input_bfd, input_section, (*parent)->address)))
11914 case bfd_reloc_outofrange:
11923 if (reloc_vector != NULL)
11924 free (reloc_vector);
11928 if (reloc_vector != NULL)
11929 free (reloc_vector);
11934 mips_elf_relax_delete_bytes (bfd *abfd,
11935 asection *sec, bfd_vma addr, int count)
11937 Elf_Internal_Shdr *symtab_hdr;
11938 unsigned int sec_shndx;
11939 bfd_byte *contents;
11940 Elf_Internal_Rela *irel, *irelend;
11941 Elf_Internal_Sym *isym;
11942 Elf_Internal_Sym *isymend;
11943 struct elf_link_hash_entry **sym_hashes;
11944 struct elf_link_hash_entry **end_hashes;
11945 struct elf_link_hash_entry **start_hashes;
11946 unsigned int symcount;
11948 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
11949 contents = elf_section_data (sec)->this_hdr.contents;
11951 irel = elf_section_data (sec)->relocs;
11952 irelend = irel + sec->reloc_count;
11954 /* Actually delete the bytes. */
11955 memmove (contents + addr, contents + addr + count,
11956 (size_t) (sec->size - addr - count));
11957 sec->size -= count;
11959 /* Adjust all the relocs. */
11960 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
11962 /* Get the new reloc address. */
11963 if (irel->r_offset > addr)
11964 irel->r_offset -= count;
11967 BFD_ASSERT (addr % 2 == 0);
11968 BFD_ASSERT (count % 2 == 0);
11970 /* Adjust the local symbols defined in this section. */
11971 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11972 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
11973 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
11974 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
11975 isym->st_value -= count;
11977 /* Now adjust the global symbols defined in this section. */
11978 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
11979 - symtab_hdr->sh_info);
11980 sym_hashes = start_hashes = elf_sym_hashes (abfd);
11981 end_hashes = sym_hashes + symcount;
11983 for (; sym_hashes < end_hashes; sym_hashes++)
11985 struct elf_link_hash_entry *sym_hash = *sym_hashes;
11987 if ((sym_hash->root.type == bfd_link_hash_defined
11988 || sym_hash->root.type == bfd_link_hash_defweak)
11989 && sym_hash->root.u.def.section == sec)
11991 bfd_vma value = sym_hash->root.u.def.value;
11993 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
11994 value &= MINUS_TWO;
11996 sym_hash->root.u.def.value -= count;
12004 /* Opcodes needed for microMIPS relaxation as found in
12005 opcodes/micromips-opc.c. */
12007 struct opcode_descriptor {
12008 unsigned long match;
12009 unsigned long mask;
12012 /* The $ra register aka $31. */
12016 /* 32-bit instruction format register fields. */
12018 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12019 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12021 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
12023 #define OP16_VALID_REG(r) \
12024 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12027 /* 32-bit and 16-bit branches. */
12029 static const struct opcode_descriptor b_insns_32[] = {
12030 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12031 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12032 { 0, 0 } /* End marker for find_match(). */
12035 static const struct opcode_descriptor bc_insn_32 =
12036 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12038 static const struct opcode_descriptor bz_insn_32 =
12039 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12041 static const struct opcode_descriptor bzal_insn_32 =
12042 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12044 static const struct opcode_descriptor beq_insn_32 =
12045 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12047 static const struct opcode_descriptor b_insn_16 =
12048 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12050 static const struct opcode_descriptor bz_insn_16 =
12051 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
12054 /* 32-bit and 16-bit branch EQ and NE zero. */
12056 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12057 eq and second the ne. This convention is used when replacing a
12058 32-bit BEQ/BNE with the 16-bit version. */
12060 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12062 static const struct opcode_descriptor bz_rs_insns_32[] = {
12063 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12064 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12065 { 0, 0 } /* End marker for find_match(). */
12068 static const struct opcode_descriptor bz_rt_insns_32[] = {
12069 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12070 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12071 { 0, 0 } /* End marker for find_match(). */
12074 static const struct opcode_descriptor bzc_insns_32[] = {
12075 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12076 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12077 { 0, 0 } /* End marker for find_match(). */
12080 static const struct opcode_descriptor bz_insns_16[] = {
12081 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12082 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12083 { 0, 0 } /* End marker for find_match(). */
12086 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12088 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12089 #define BZ16_REG_FIELD(r) \
12090 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12093 /* 32-bit instructions with a delay slot. */
12095 static const struct opcode_descriptor jal_insn_32_bd16 =
12096 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12098 static const struct opcode_descriptor jal_insn_32_bd32 =
12099 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12101 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12102 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12104 static const struct opcode_descriptor j_insn_32 =
12105 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12107 static const struct opcode_descriptor jalr_insn_32 =
12108 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12110 /* This table can be compacted, because no opcode replacement is made. */
12112 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12113 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12115 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12116 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12118 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12119 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12120 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12121 { 0, 0 } /* End marker for find_match(). */
12124 /* This table can be compacted, because no opcode replacement is made. */
12126 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12127 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12129 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12130 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12131 { 0, 0 } /* End marker for find_match(). */
12135 /* 16-bit instructions with a delay slot. */
12137 static const struct opcode_descriptor jalr_insn_16_bd16 =
12138 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12140 static const struct opcode_descriptor jalr_insn_16_bd32 =
12141 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12143 static const struct opcode_descriptor jr_insn_16 =
12144 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12146 #define JR16_REG(opcode) ((opcode) & 0x1f)
12148 /* This table can be compacted, because no opcode replacement is made. */
12150 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12151 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12153 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12154 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12155 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12156 { 0, 0 } /* End marker for find_match(). */
12160 /* LUI instruction. */
12162 static const struct opcode_descriptor lui_insn =
12163 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12166 /* ADDIU instruction. */
12168 static const struct opcode_descriptor addiu_insn =
12169 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12171 static const struct opcode_descriptor addiupc_insn =
12172 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12174 #define ADDIUPC_REG_FIELD(r) \
12175 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12178 /* Relaxable instructions in a JAL delay slot: MOVE. */
12180 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12181 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12182 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12183 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12185 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12186 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12188 static const struct opcode_descriptor move_insns_32[] = {
12189 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12190 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12191 { 0, 0 } /* End marker for find_match(). */
12194 static const struct opcode_descriptor move_insn_16 =
12195 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12198 /* NOP instructions. */
12200 static const struct opcode_descriptor nop_insn_32 =
12201 { /* "nop", "", */ 0x00000000, 0xffffffff };
12203 static const struct opcode_descriptor nop_insn_16 =
12204 { /* "nop", "", */ 0x0c00, 0xffff };
12207 /* Instruction match support. */
12209 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12212 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12214 unsigned long indx;
12216 for (indx = 0; insn[indx].mask != 0; indx++)
12217 if (MATCH (opcode, insn[indx]))
12224 /* Branch and delay slot decoding support. */
12226 /* If PTR points to what *might* be a 16-bit branch or jump, then
12227 return the minimum length of its delay slot, otherwise return 0.
12228 Non-zero results are not definitive as we might be checking against
12229 the second half of another instruction. */
12232 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12234 unsigned long opcode;
12237 opcode = bfd_get_16 (abfd, ptr);
12238 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12239 /* 16-bit branch/jump with a 32-bit delay slot. */
12241 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12242 || find_match (opcode, ds_insns_16_bd16) >= 0)
12243 /* 16-bit branch/jump with a 16-bit delay slot. */
12246 /* No delay slot. */
12252 /* If PTR points to what *might* be a 32-bit branch or jump, then
12253 return the minimum length of its delay slot, otherwise return 0.
12254 Non-zero results are not definitive as we might be checking against
12255 the second half of another instruction. */
12258 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12260 unsigned long opcode;
12263 opcode = bfd_get_micromips_32 (abfd, ptr);
12264 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12265 /* 32-bit branch/jump with a 32-bit delay slot. */
12267 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12268 /* 32-bit branch/jump with a 16-bit delay slot. */
12271 /* No delay slot. */
12277 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12278 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12281 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12283 unsigned long opcode;
12285 opcode = bfd_get_16 (abfd, ptr);
12286 if (MATCH (opcode, b_insn_16)
12288 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12290 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12291 /* BEQZ16, BNEZ16 */
12292 || (MATCH (opcode, jalr_insn_16_bd32)
12294 && reg != JR16_REG (opcode) && reg != RA))
12300 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12301 then return TRUE, otherwise FALSE. */
12304 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12306 unsigned long opcode;
12308 opcode = bfd_get_micromips_32 (abfd, ptr);
12309 if (MATCH (opcode, j_insn_32)
12311 || MATCH (opcode, bc_insn_32)
12312 /* BC1F, BC1T, BC2F, BC2T */
12313 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12315 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12316 /* BGEZ, BGTZ, BLEZ, BLTZ */
12317 || (MATCH (opcode, bzal_insn_32)
12318 /* BGEZAL, BLTZAL */
12319 && reg != OP32_SREG (opcode) && reg != RA)
12320 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
12321 /* JALR, JALR.HB, BEQ, BNE */
12322 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
12328 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
12329 IRELEND) at OFFSET indicate that there must be a compact branch there,
12330 then return TRUE, otherwise FALSE. */
12333 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
12334 const Elf_Internal_Rela *internal_relocs,
12335 const Elf_Internal_Rela *irelend)
12337 const Elf_Internal_Rela *irel;
12338 unsigned long opcode;
12340 opcode = bfd_get_micromips_32 (abfd, ptr);
12341 if (find_match (opcode, bzc_insns_32) < 0)
12344 for (irel = internal_relocs; irel < irelend; irel++)
12345 if (irel->r_offset == offset
12346 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
12352 /* Bitsize checking. */
12353 #define IS_BITSIZE(val, N) \
12354 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
12355 - (1ULL << ((N) - 1))) == (val))
12359 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
12360 struct bfd_link_info *link_info,
12361 bfd_boolean *again)
12363 Elf_Internal_Shdr *symtab_hdr;
12364 Elf_Internal_Rela *internal_relocs;
12365 Elf_Internal_Rela *irel, *irelend;
12366 bfd_byte *contents = NULL;
12367 Elf_Internal_Sym *isymbuf = NULL;
12369 /* Assume nothing changes. */
12372 /* We don't have to do anything for a relocatable link, if
12373 this section does not have relocs, or if this is not a
12376 if (link_info->relocatable
12377 || (sec->flags & SEC_RELOC) == 0
12378 || sec->reloc_count == 0
12379 || (sec->flags & SEC_CODE) == 0)
12382 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12384 /* Get a copy of the native relocations. */
12385 internal_relocs = (_bfd_elf_link_read_relocs
12386 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
12387 link_info->keep_memory));
12388 if (internal_relocs == NULL)
12391 /* Walk through them looking for relaxing opportunities. */
12392 irelend = internal_relocs + sec->reloc_count;
12393 for (irel = internal_relocs; irel < irelend; irel++)
12395 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
12396 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
12397 bfd_boolean target_is_micromips_code_p;
12398 unsigned long opcode;
12404 /* The number of bytes to delete for relaxation and from where
12405 to delete these bytes starting at irel->r_offset. */
12409 /* If this isn't something that can be relaxed, then ignore
12411 if (r_type != R_MICROMIPS_HI16
12412 && r_type != R_MICROMIPS_PC16_S1
12413 && r_type != R_MICROMIPS_26_S1)
12416 /* Get the section contents if we haven't done so already. */
12417 if (contents == NULL)
12419 /* Get cached copy if it exists. */
12420 if (elf_section_data (sec)->this_hdr.contents != NULL)
12421 contents = elf_section_data (sec)->this_hdr.contents;
12422 /* Go get them off disk. */
12423 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
12426 ptr = contents + irel->r_offset;
12428 /* Read this BFD's local symbols if we haven't done so already. */
12429 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
12431 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
12432 if (isymbuf == NULL)
12433 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12434 symtab_hdr->sh_info, 0,
12436 if (isymbuf == NULL)
12440 /* Get the value of the symbol referred to by the reloc. */
12441 if (r_symndx < symtab_hdr->sh_info)
12443 /* A local symbol. */
12444 Elf_Internal_Sym *isym;
12447 isym = isymbuf + r_symndx;
12448 if (isym->st_shndx == SHN_UNDEF)
12449 sym_sec = bfd_und_section_ptr;
12450 else if (isym->st_shndx == SHN_ABS)
12451 sym_sec = bfd_abs_section_ptr;
12452 else if (isym->st_shndx == SHN_COMMON)
12453 sym_sec = bfd_com_section_ptr;
12455 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
12456 symval = (isym->st_value
12457 + sym_sec->output_section->vma
12458 + sym_sec->output_offset);
12459 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
12463 unsigned long indx;
12464 struct elf_link_hash_entry *h;
12466 /* An external symbol. */
12467 indx = r_symndx - symtab_hdr->sh_info;
12468 h = elf_sym_hashes (abfd)[indx];
12469 BFD_ASSERT (h != NULL);
12471 if (h->root.type != bfd_link_hash_defined
12472 && h->root.type != bfd_link_hash_defweak)
12473 /* This appears to be a reference to an undefined
12474 symbol. Just ignore it -- it will be caught by the
12475 regular reloc processing. */
12478 symval = (h->root.u.def.value
12479 + h->root.u.def.section->output_section->vma
12480 + h->root.u.def.section->output_offset);
12481 target_is_micromips_code_p = (!h->needs_plt
12482 && ELF_ST_IS_MICROMIPS (h->other));
12486 /* For simplicity of coding, we are going to modify the
12487 section contents, the section relocs, and the BFD symbol
12488 table. We must tell the rest of the code not to free up this
12489 information. It would be possible to instead create a table
12490 of changes which have to be made, as is done in coff-mips.c;
12491 that would be more work, but would require less memory when
12492 the linker is run. */
12494 /* Only 32-bit instructions relaxed. */
12495 if (irel->r_offset + 4 > sec->size)
12498 opcode = bfd_get_micromips_32 (abfd, ptr);
12500 /* This is the pc-relative distance from the instruction the
12501 relocation is applied to, to the symbol referred. */
12503 - (sec->output_section->vma + sec->output_offset)
12506 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
12507 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
12508 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
12510 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
12512 where pcrval has first to be adjusted to apply against the LO16
12513 location (we make the adjustment later on, when we have figured
12514 out the offset). */
12515 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
12517 bfd_boolean bzc = FALSE;
12518 unsigned long nextopc;
12522 /* Give up if the previous reloc was a HI16 against this symbol
12524 if (irel > internal_relocs
12525 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
12526 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
12529 /* Or if the next reloc is not a LO16 against this symbol. */
12530 if (irel + 1 >= irelend
12531 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
12532 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
12535 /* Or if the second next reloc is a LO16 against this symbol too. */
12536 if (irel + 2 >= irelend
12537 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
12538 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
12541 /* See if the LUI instruction *might* be in a branch delay slot.
12542 We check whether what looks like a 16-bit branch or jump is
12543 actually an immediate argument to a compact branch, and let
12544 it through if so. */
12545 if (irel->r_offset >= 2
12546 && check_br16_dslot (abfd, ptr - 2)
12547 && !(irel->r_offset >= 4
12548 && (bzc = check_relocated_bzc (abfd,
12549 ptr - 4, irel->r_offset - 4,
12550 internal_relocs, irelend))))
12552 if (irel->r_offset >= 4
12554 && check_br32_dslot (abfd, ptr - 4))
12557 reg = OP32_SREG (opcode);
12559 /* We only relax adjacent instructions or ones separated with
12560 a branch or jump that has a delay slot. The branch or jump
12561 must not fiddle with the register used to hold the address.
12562 Subtract 4 for the LUI itself. */
12563 offset = irel[1].r_offset - irel[0].r_offset;
12564 switch (offset - 4)
12569 if (check_br16 (abfd, ptr + 4, reg))
12573 if (check_br32 (abfd, ptr + 4, reg))
12580 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
12582 /* Give up unless the same register is used with both
12584 if (OP32_SREG (nextopc) != reg)
12587 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
12588 and rounding up to take masking of the two LSBs into account. */
12589 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
12591 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
12592 if (IS_BITSIZE (symval, 16))
12594 /* Fix the relocation's type. */
12595 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
12597 /* Instructions using R_MICROMIPS_LO16 have the base or
12598 source register in bits 20:16. This register becomes $0
12599 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
12600 nextopc &= ~0x001f0000;
12601 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
12602 contents + irel[1].r_offset);
12605 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
12606 We add 4 to take LUI deletion into account while checking
12607 the PC-relative distance. */
12608 else if (symval % 4 == 0
12609 && IS_BITSIZE (pcrval + 4, 25)
12610 && MATCH (nextopc, addiu_insn)
12611 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
12612 && OP16_VALID_REG (OP32_TREG (nextopc)))
12614 /* Fix the relocation's type. */
12615 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
12617 /* Replace ADDIU with the ADDIUPC version. */
12618 nextopc = (addiupc_insn.match
12619 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
12621 bfd_put_micromips_32 (abfd, nextopc,
12622 contents + irel[1].r_offset);
12625 /* Can't do anything, give up, sigh... */
12629 /* Fix the relocation's type. */
12630 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
12632 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
12637 /* Compact branch relaxation -- due to the multitude of macros
12638 employed by the compiler/assembler, compact branches are not
12639 always generated. Obviously, this can/will be fixed elsewhere,
12640 but there is no drawback in double checking it here. */
12641 else if (r_type == R_MICROMIPS_PC16_S1
12642 && irel->r_offset + 5 < sec->size
12643 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12644 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
12645 && MATCH (bfd_get_16 (abfd, ptr + 4), nop_insn_16))
12649 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12651 /* Replace BEQZ/BNEZ with the compact version. */
12652 opcode = (bzc_insns_32[fndopc].match
12653 | BZC32_REG_FIELD (reg)
12654 | (opcode & 0xffff)); /* Addend value. */
12656 bfd_put_micromips_32 (abfd, opcode, ptr);
12658 /* Delete the 16-bit delay slot NOP: two bytes from
12659 irel->offset + 4. */
12664 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
12665 to check the distance from the next instruction, so subtract 2. */
12666 else if (r_type == R_MICROMIPS_PC16_S1
12667 && IS_BITSIZE (pcrval - 2, 11)
12668 && find_match (opcode, b_insns_32) >= 0)
12670 /* Fix the relocation's type. */
12671 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
12673 /* Replace the 32-bit opcode with a 16-bit opcode. */
12676 | (opcode & 0x3ff)), /* Addend value. */
12679 /* Delete 2 bytes from irel->r_offset + 2. */
12684 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
12685 to check the distance from the next instruction, so subtract 2. */
12686 else if (r_type == R_MICROMIPS_PC16_S1
12687 && IS_BITSIZE (pcrval - 2, 8)
12688 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12689 && OP16_VALID_REG (OP32_SREG (opcode)))
12690 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
12691 && OP16_VALID_REG (OP32_TREG (opcode)))))
12695 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12697 /* Fix the relocation's type. */
12698 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
12700 /* Replace the 32-bit opcode with a 16-bit opcode. */
12702 (bz_insns_16[fndopc].match
12703 | BZ16_REG_FIELD (reg)
12704 | (opcode & 0x7f)), /* Addend value. */
12707 /* Delete 2 bytes from irel->r_offset + 2. */
12712 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
12713 else if (r_type == R_MICROMIPS_26_S1
12714 && target_is_micromips_code_p
12715 && irel->r_offset + 7 < sec->size
12716 && MATCH (opcode, jal_insn_32_bd32))
12718 unsigned long n32opc;
12719 bfd_boolean relaxed = FALSE;
12721 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
12723 if (MATCH (n32opc, nop_insn_32))
12725 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
12726 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
12730 else if (find_match (n32opc, move_insns_32) >= 0)
12732 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
12734 (move_insn_16.match
12735 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
12736 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
12741 /* Other 32-bit instructions relaxable to 16-bit
12742 instructions will be handled here later. */
12746 /* JAL with 32-bit delay slot that is changed to a JALS
12747 with 16-bit delay slot. */
12748 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
12750 /* Delete 2 bytes from irel->r_offset + 6. */
12758 /* Note that we've changed the relocs, section contents, etc. */
12759 elf_section_data (sec)->relocs = internal_relocs;
12760 elf_section_data (sec)->this_hdr.contents = contents;
12761 symtab_hdr->contents = (unsigned char *) isymbuf;
12763 /* Delete bytes depending on the delcnt and deloff. */
12764 if (!mips_elf_relax_delete_bytes (abfd, sec,
12765 irel->r_offset + deloff, delcnt))
12768 /* That will change things, so we should relax again.
12769 Note that this is not required, and it may be slow. */
12774 if (isymbuf != NULL
12775 && symtab_hdr->contents != (unsigned char *) isymbuf)
12777 if (! link_info->keep_memory)
12781 /* Cache the symbols for elf_link_input_bfd. */
12782 symtab_hdr->contents = (unsigned char *) isymbuf;
12786 if (contents != NULL
12787 && elf_section_data (sec)->this_hdr.contents != contents)
12789 if (! link_info->keep_memory)
12793 /* Cache the section contents for elf_link_input_bfd. */
12794 elf_section_data (sec)->this_hdr.contents = contents;
12798 if (internal_relocs != NULL
12799 && elf_section_data (sec)->relocs != internal_relocs)
12800 free (internal_relocs);
12805 if (isymbuf != NULL
12806 && symtab_hdr->contents != (unsigned char *) isymbuf)
12808 if (contents != NULL
12809 && elf_section_data (sec)->this_hdr.contents != contents)
12811 if (internal_relocs != NULL
12812 && elf_section_data (sec)->relocs != internal_relocs)
12813 free (internal_relocs);
12818 /* Create a MIPS ELF linker hash table. */
12820 struct bfd_link_hash_table *
12821 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
12823 struct mips_elf_link_hash_table *ret;
12824 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
12826 ret = bfd_zmalloc (amt);
12830 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
12831 mips_elf_link_hash_newfunc,
12832 sizeof (struct mips_elf_link_hash_entry),
12839 return &ret->root.root;
12842 /* Likewise, but indicate that the target is VxWorks. */
12844 struct bfd_link_hash_table *
12845 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
12847 struct bfd_link_hash_table *ret;
12849 ret = _bfd_mips_elf_link_hash_table_create (abfd);
12852 struct mips_elf_link_hash_table *htab;
12854 htab = (struct mips_elf_link_hash_table *) ret;
12855 htab->use_plts_and_copy_relocs = TRUE;
12856 htab->is_vxworks = TRUE;
12861 /* A function that the linker calls if we are allowed to use PLTs
12862 and copy relocs. */
12865 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
12867 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
12870 /* We need to use a special link routine to handle the .reginfo and
12871 the .mdebug sections. We need to merge all instances of these
12872 sections together, not write them all out sequentially. */
12875 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12878 struct bfd_link_order *p;
12879 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
12880 asection *rtproc_sec;
12881 Elf32_RegInfo reginfo;
12882 struct ecoff_debug_info debug;
12883 struct mips_htab_traverse_info hti;
12884 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12885 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
12886 HDRR *symhdr = &debug.symbolic_header;
12887 void *mdebug_handle = NULL;
12892 struct mips_elf_link_hash_table *htab;
12894 static const char * const secname[] =
12896 ".text", ".init", ".fini", ".data",
12897 ".rodata", ".sdata", ".sbss", ".bss"
12899 static const int sc[] =
12901 scText, scInit, scFini, scData,
12902 scRData, scSData, scSBss, scBss
12905 /* Sort the dynamic symbols so that those with GOT entries come after
12907 htab = mips_elf_hash_table (info);
12908 BFD_ASSERT (htab != NULL);
12910 if (!mips_elf_sort_hash_table (abfd, info))
12913 /* Create any scheduled LA25 stubs. */
12915 hti.output_bfd = abfd;
12917 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
12921 /* Get a value for the GP register. */
12922 if (elf_gp (abfd) == 0)
12924 struct bfd_link_hash_entry *h;
12926 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
12927 if (h != NULL && h->type == bfd_link_hash_defined)
12928 elf_gp (abfd) = (h->u.def.value
12929 + h->u.def.section->output_section->vma
12930 + h->u.def.section->output_offset);
12931 else if (htab->is_vxworks
12932 && (h = bfd_link_hash_lookup (info->hash,
12933 "_GLOBAL_OFFSET_TABLE_",
12934 FALSE, FALSE, TRUE))
12935 && h->type == bfd_link_hash_defined)
12936 elf_gp (abfd) = (h->u.def.section->output_section->vma
12937 + h->u.def.section->output_offset
12939 else if (info->relocatable)
12941 bfd_vma lo = MINUS_ONE;
12943 /* Find the GP-relative section with the lowest offset. */
12944 for (o = abfd->sections; o != NULL; o = o->next)
12946 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
12949 /* And calculate GP relative to that. */
12950 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
12954 /* If the relocate_section function needs to do a reloc
12955 involving the GP value, it should make a reloc_dangerous
12956 callback to warn that GP is not defined. */
12960 /* Go through the sections and collect the .reginfo and .mdebug
12962 reginfo_sec = NULL;
12964 gptab_data_sec = NULL;
12965 gptab_bss_sec = NULL;
12966 for (o = abfd->sections; o != NULL; o = o->next)
12968 if (strcmp (o->name, ".reginfo") == 0)
12970 memset (®info, 0, sizeof reginfo);
12972 /* We have found the .reginfo section in the output file.
12973 Look through all the link_orders comprising it and merge
12974 the information together. */
12975 for (p = o->map_head.link_order; p != NULL; p = p->next)
12977 asection *input_section;
12979 Elf32_External_RegInfo ext;
12982 if (p->type != bfd_indirect_link_order)
12984 if (p->type == bfd_data_link_order)
12989 input_section = p->u.indirect.section;
12990 input_bfd = input_section->owner;
12992 if (! bfd_get_section_contents (input_bfd, input_section,
12993 &ext, 0, sizeof ext))
12996 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
12998 reginfo.ri_gprmask |= sub.ri_gprmask;
12999 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13000 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13001 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13002 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13004 /* ri_gp_value is set by the function
13005 mips_elf32_section_processing when the section is
13006 finally written out. */
13008 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13009 elf_link_input_bfd ignores this section. */
13010 input_section->flags &= ~SEC_HAS_CONTENTS;
13013 /* Size has been set in _bfd_mips_elf_always_size_sections. */
13014 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
13016 /* Skip this section later on (I don't think this currently
13017 matters, but someday it might). */
13018 o->map_head.link_order = NULL;
13023 if (strcmp (o->name, ".mdebug") == 0)
13025 struct extsym_info einfo;
13028 /* We have found the .mdebug section in the output file.
13029 Look through all the link_orders comprising it and merge
13030 the information together. */
13031 symhdr->magic = swap->sym_magic;
13032 /* FIXME: What should the version stamp be? */
13033 symhdr->vstamp = 0;
13034 symhdr->ilineMax = 0;
13035 symhdr->cbLine = 0;
13036 symhdr->idnMax = 0;
13037 symhdr->ipdMax = 0;
13038 symhdr->isymMax = 0;
13039 symhdr->ioptMax = 0;
13040 symhdr->iauxMax = 0;
13041 symhdr->issMax = 0;
13042 symhdr->issExtMax = 0;
13043 symhdr->ifdMax = 0;
13045 symhdr->iextMax = 0;
13047 /* We accumulate the debugging information itself in the
13048 debug_info structure. */
13050 debug.external_dnr = NULL;
13051 debug.external_pdr = NULL;
13052 debug.external_sym = NULL;
13053 debug.external_opt = NULL;
13054 debug.external_aux = NULL;
13056 debug.ssext = debug.ssext_end = NULL;
13057 debug.external_fdr = NULL;
13058 debug.external_rfd = NULL;
13059 debug.external_ext = debug.external_ext_end = NULL;
13061 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
13062 if (mdebug_handle == NULL)
13066 esym.cobol_main = 0;
13070 esym.asym.iss = issNil;
13071 esym.asym.st = stLocal;
13072 esym.asym.reserved = 0;
13073 esym.asym.index = indexNil;
13075 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13077 esym.asym.sc = sc[i];
13078 s = bfd_get_section_by_name (abfd, secname[i]);
13081 esym.asym.value = s->vma;
13082 last = s->vma + s->size;
13085 esym.asym.value = last;
13086 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13087 secname[i], &esym))
13091 for (p = o->map_head.link_order; p != NULL; p = p->next)
13093 asection *input_section;
13095 const struct ecoff_debug_swap *input_swap;
13096 struct ecoff_debug_info input_debug;
13100 if (p->type != bfd_indirect_link_order)
13102 if (p->type == bfd_data_link_order)
13107 input_section = p->u.indirect.section;
13108 input_bfd = input_section->owner;
13110 if (!is_mips_elf (input_bfd))
13112 /* I don't know what a non MIPS ELF bfd would be
13113 doing with a .mdebug section, but I don't really
13114 want to deal with it. */
13118 input_swap = (get_elf_backend_data (input_bfd)
13119 ->elf_backend_ecoff_debug_swap);
13121 BFD_ASSERT (p->size == input_section->size);
13123 /* The ECOFF linking code expects that we have already
13124 read in the debugging information and set up an
13125 ecoff_debug_info structure, so we do that now. */
13126 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13130 if (! (bfd_ecoff_debug_accumulate
13131 (mdebug_handle, abfd, &debug, swap, input_bfd,
13132 &input_debug, input_swap, info)))
13135 /* Loop through the external symbols. For each one with
13136 interesting information, try to find the symbol in
13137 the linker global hash table and save the information
13138 for the output external symbols. */
13139 eraw_src = input_debug.external_ext;
13140 eraw_end = (eraw_src
13141 + (input_debug.symbolic_header.iextMax
13142 * input_swap->external_ext_size));
13144 eraw_src < eraw_end;
13145 eraw_src += input_swap->external_ext_size)
13149 struct mips_elf_link_hash_entry *h;
13151 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
13152 if (ext.asym.sc == scNil
13153 || ext.asym.sc == scUndefined
13154 || ext.asym.sc == scSUndefined)
13157 name = input_debug.ssext + ext.asym.iss;
13158 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
13159 name, FALSE, FALSE, TRUE);
13160 if (h == NULL || h->esym.ifd != -2)
13165 BFD_ASSERT (ext.ifd
13166 < input_debug.symbolic_header.ifdMax);
13167 ext.ifd = input_debug.ifdmap[ext.ifd];
13173 /* Free up the information we just read. */
13174 free (input_debug.line);
13175 free (input_debug.external_dnr);
13176 free (input_debug.external_pdr);
13177 free (input_debug.external_sym);
13178 free (input_debug.external_opt);
13179 free (input_debug.external_aux);
13180 free (input_debug.ss);
13181 free (input_debug.ssext);
13182 free (input_debug.external_fdr);
13183 free (input_debug.external_rfd);
13184 free (input_debug.external_ext);
13186 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13187 elf_link_input_bfd ignores this section. */
13188 input_section->flags &= ~SEC_HAS_CONTENTS;
13191 if (SGI_COMPAT (abfd) && info->shared)
13193 /* Create .rtproc section. */
13194 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
13195 if (rtproc_sec == NULL)
13197 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13198 | SEC_LINKER_CREATED | SEC_READONLY);
13200 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13203 if (rtproc_sec == NULL
13204 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
13208 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13214 /* Build the external symbol information. */
13217 einfo.debug = &debug;
13219 einfo.failed = FALSE;
13220 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
13221 mips_elf_output_extsym, &einfo);
13225 /* Set the size of the .mdebug section. */
13226 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
13228 /* Skip this section later on (I don't think this currently
13229 matters, but someday it might). */
13230 o->map_head.link_order = NULL;
13235 if (CONST_STRNEQ (o->name, ".gptab."))
13237 const char *subname;
13240 Elf32_External_gptab *ext_tab;
13243 /* The .gptab.sdata and .gptab.sbss sections hold
13244 information describing how the small data area would
13245 change depending upon the -G switch. These sections
13246 not used in executables files. */
13247 if (! info->relocatable)
13249 for (p = o->map_head.link_order; p != NULL; p = p->next)
13251 asection *input_section;
13253 if (p->type != bfd_indirect_link_order)
13255 if (p->type == bfd_data_link_order)
13260 input_section = p->u.indirect.section;
13262 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13263 elf_link_input_bfd ignores this section. */
13264 input_section->flags &= ~SEC_HAS_CONTENTS;
13267 /* Skip this section later on (I don't think this
13268 currently matters, but someday it might). */
13269 o->map_head.link_order = NULL;
13271 /* Really remove the section. */
13272 bfd_section_list_remove (abfd, o);
13273 --abfd->section_count;
13278 /* There is one gptab for initialized data, and one for
13279 uninitialized data. */
13280 if (strcmp (o->name, ".gptab.sdata") == 0)
13281 gptab_data_sec = o;
13282 else if (strcmp (o->name, ".gptab.sbss") == 0)
13286 (*_bfd_error_handler)
13287 (_("%s: illegal section name `%s'"),
13288 bfd_get_filename (abfd), o->name);
13289 bfd_set_error (bfd_error_nonrepresentable_section);
13293 /* The linker script always combines .gptab.data and
13294 .gptab.sdata into .gptab.sdata, and likewise for
13295 .gptab.bss and .gptab.sbss. It is possible that there is
13296 no .sdata or .sbss section in the output file, in which
13297 case we must change the name of the output section. */
13298 subname = o->name + sizeof ".gptab" - 1;
13299 if (bfd_get_section_by_name (abfd, subname) == NULL)
13301 if (o == gptab_data_sec)
13302 o->name = ".gptab.data";
13304 o->name = ".gptab.bss";
13305 subname = o->name + sizeof ".gptab" - 1;
13306 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
13309 /* Set up the first entry. */
13311 amt = c * sizeof (Elf32_gptab);
13312 tab = bfd_malloc (amt);
13315 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
13316 tab[0].gt_header.gt_unused = 0;
13318 /* Combine the input sections. */
13319 for (p = o->map_head.link_order; p != NULL; p = p->next)
13321 asection *input_section;
13323 bfd_size_type size;
13324 unsigned long last;
13325 bfd_size_type gpentry;
13327 if (p->type != bfd_indirect_link_order)
13329 if (p->type == bfd_data_link_order)
13334 input_section = p->u.indirect.section;
13335 input_bfd = input_section->owner;
13337 /* Combine the gptab entries for this input section one
13338 by one. We know that the input gptab entries are
13339 sorted by ascending -G value. */
13340 size = input_section->size;
13342 for (gpentry = sizeof (Elf32_External_gptab);
13344 gpentry += sizeof (Elf32_External_gptab))
13346 Elf32_External_gptab ext_gptab;
13347 Elf32_gptab int_gptab;
13353 if (! (bfd_get_section_contents
13354 (input_bfd, input_section, &ext_gptab, gpentry,
13355 sizeof (Elf32_External_gptab))))
13361 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
13363 val = int_gptab.gt_entry.gt_g_value;
13364 add = int_gptab.gt_entry.gt_bytes - last;
13367 for (look = 1; look < c; look++)
13369 if (tab[look].gt_entry.gt_g_value >= val)
13370 tab[look].gt_entry.gt_bytes += add;
13372 if (tab[look].gt_entry.gt_g_value == val)
13378 Elf32_gptab *new_tab;
13381 /* We need a new table entry. */
13382 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
13383 new_tab = bfd_realloc (tab, amt);
13384 if (new_tab == NULL)
13390 tab[c].gt_entry.gt_g_value = val;
13391 tab[c].gt_entry.gt_bytes = add;
13393 /* Merge in the size for the next smallest -G
13394 value, since that will be implied by this new
13397 for (look = 1; look < c; look++)
13399 if (tab[look].gt_entry.gt_g_value < val
13401 || (tab[look].gt_entry.gt_g_value
13402 > tab[max].gt_entry.gt_g_value)))
13406 tab[c].gt_entry.gt_bytes +=
13407 tab[max].gt_entry.gt_bytes;
13412 last = int_gptab.gt_entry.gt_bytes;
13415 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13416 elf_link_input_bfd ignores this section. */
13417 input_section->flags &= ~SEC_HAS_CONTENTS;
13420 /* The table must be sorted by -G value. */
13422 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
13424 /* Swap out the table. */
13425 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
13426 ext_tab = bfd_alloc (abfd, amt);
13427 if (ext_tab == NULL)
13433 for (j = 0; j < c; j++)
13434 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
13437 o->size = c * sizeof (Elf32_External_gptab);
13438 o->contents = (bfd_byte *) ext_tab;
13440 /* Skip this section later on (I don't think this currently
13441 matters, but someday it might). */
13442 o->map_head.link_order = NULL;
13446 /* Invoke the regular ELF backend linker to do all the work. */
13447 if (!bfd_elf_final_link (abfd, info))
13450 /* Now write out the computed sections. */
13452 if (reginfo_sec != NULL)
13454 Elf32_External_RegInfo ext;
13456 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
13457 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
13461 if (mdebug_sec != NULL)
13463 BFD_ASSERT (abfd->output_has_begun);
13464 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
13466 mdebug_sec->filepos))
13469 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
13472 if (gptab_data_sec != NULL)
13474 if (! bfd_set_section_contents (abfd, gptab_data_sec,
13475 gptab_data_sec->contents,
13476 0, gptab_data_sec->size))
13480 if (gptab_bss_sec != NULL)
13482 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
13483 gptab_bss_sec->contents,
13484 0, gptab_bss_sec->size))
13488 if (SGI_COMPAT (abfd))
13490 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
13491 if (rtproc_sec != NULL)
13493 if (! bfd_set_section_contents (abfd, rtproc_sec,
13494 rtproc_sec->contents,
13495 0, rtproc_sec->size))
13503 /* Structure for saying that BFD machine EXTENSION extends BASE. */
13505 struct mips_mach_extension {
13506 unsigned long extension, base;
13510 /* An array describing how BFD machines relate to one another. The entries
13511 are ordered topologically with MIPS I extensions listed last. */
13513 static const struct mips_mach_extension mips_mach_extensions[] = {
13514 /* MIPS64r2 extensions. */
13515 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
13516 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
13517 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
13519 /* MIPS64 extensions. */
13520 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
13521 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
13522 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
13523 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
13525 /* MIPS V extensions. */
13526 { bfd_mach_mipsisa64, bfd_mach_mips5 },
13528 /* R10000 extensions. */
13529 { bfd_mach_mips12000, bfd_mach_mips10000 },
13530 { bfd_mach_mips14000, bfd_mach_mips10000 },
13531 { bfd_mach_mips16000, bfd_mach_mips10000 },
13533 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
13534 vr5400 ISA, but doesn't include the multimedia stuff. It seems
13535 better to allow vr5400 and vr5500 code to be merged anyway, since
13536 many libraries will just use the core ISA. Perhaps we could add
13537 some sort of ASE flag if this ever proves a problem. */
13538 { bfd_mach_mips5500, bfd_mach_mips5400 },
13539 { bfd_mach_mips5400, bfd_mach_mips5000 },
13541 /* MIPS IV extensions. */
13542 { bfd_mach_mips5, bfd_mach_mips8000 },
13543 { bfd_mach_mips10000, bfd_mach_mips8000 },
13544 { bfd_mach_mips5000, bfd_mach_mips8000 },
13545 { bfd_mach_mips7000, bfd_mach_mips8000 },
13546 { bfd_mach_mips9000, bfd_mach_mips8000 },
13548 /* VR4100 extensions. */
13549 { bfd_mach_mips4120, bfd_mach_mips4100 },
13550 { bfd_mach_mips4111, bfd_mach_mips4100 },
13552 /* MIPS III extensions. */
13553 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
13554 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
13555 { bfd_mach_mips8000, bfd_mach_mips4000 },
13556 { bfd_mach_mips4650, bfd_mach_mips4000 },
13557 { bfd_mach_mips4600, bfd_mach_mips4000 },
13558 { bfd_mach_mips4400, bfd_mach_mips4000 },
13559 { bfd_mach_mips4300, bfd_mach_mips4000 },
13560 { bfd_mach_mips4100, bfd_mach_mips4000 },
13561 { bfd_mach_mips4010, bfd_mach_mips4000 },
13562 { bfd_mach_mips5900, bfd_mach_mips4000 },
13564 /* MIPS32 extensions. */
13565 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
13567 /* MIPS II extensions. */
13568 { bfd_mach_mips4000, bfd_mach_mips6000 },
13569 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
13571 /* MIPS I extensions. */
13572 { bfd_mach_mips6000, bfd_mach_mips3000 },
13573 { bfd_mach_mips3900, bfd_mach_mips3000 }
13577 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
13580 mips_mach_extends_p (unsigned long base, unsigned long extension)
13584 if (extension == base)
13587 if (base == bfd_mach_mipsisa32
13588 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
13591 if (base == bfd_mach_mipsisa32r2
13592 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
13595 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
13596 if (extension == mips_mach_extensions[i].extension)
13598 extension = mips_mach_extensions[i].base;
13599 if (extension == base)
13607 /* Return true if the given ELF header flags describe a 32-bit binary. */
13610 mips_32bit_flags_p (flagword flags)
13612 return ((flags & EF_MIPS_32BITMODE) != 0
13613 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
13614 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
13615 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
13616 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
13617 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
13618 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
13622 /* Merge object attributes from IBFD into OBFD. Raise an error if
13623 there are conflicting attributes. */
13625 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
13627 obj_attribute *in_attr;
13628 obj_attribute *out_attr;
13631 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
13632 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
13633 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != 0)
13634 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
13636 if (!elf_known_obj_attributes_proc (obfd)[0].i)
13638 /* This is the first object. Copy the attributes. */
13639 _bfd_elf_copy_obj_attributes (ibfd, obfd);
13641 /* Use the Tag_null value to indicate the attributes have been
13643 elf_known_obj_attributes_proc (obfd)[0].i = 1;
13648 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
13649 non-conflicting ones. */
13650 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
13651 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
13653 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
13654 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
13655 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
13656 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != 0)
13657 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
13660 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13664 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13665 obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
13670 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13671 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
13676 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13677 obfd, abi_fp_bfd, ibfd,
13678 "-mdouble-float", "-mips32r2 -mfp64");
13683 (_("Warning: %B uses %s (set by %B), "
13684 "%B uses unknown floating point ABI %d"),
13685 obfd, abi_fp_bfd, ibfd,
13686 "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13692 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13696 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13697 obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
13702 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13703 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
13708 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13709 obfd, abi_fp_bfd, ibfd,
13710 "-msingle-float", "-mips32r2 -mfp64");
13715 (_("Warning: %B uses %s (set by %B), "
13716 "%B uses unknown floating point ABI %d"),
13717 obfd, abi_fp_bfd, ibfd,
13718 "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13724 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13730 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13731 obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
13736 (_("Warning: %B uses %s (set by %B), "
13737 "%B uses unknown floating point ABI %d"),
13738 obfd, abi_fp_bfd, ibfd,
13739 "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13745 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13749 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13750 obfd, abi_fp_bfd, ibfd,
13751 "-mips32r2 -mfp64", "-mdouble-float");
13756 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13757 obfd, abi_fp_bfd, ibfd,
13758 "-mips32r2 -mfp64", "-msingle-float");
13763 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13764 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
13769 (_("Warning: %B uses %s (set by %B), "
13770 "%B uses unknown floating point ABI %d"),
13771 obfd, abi_fp_bfd, ibfd,
13772 "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13778 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13782 (_("Warning: %B uses unknown floating point ABI %d "
13783 "(set by %B), %B uses %s"),
13784 obfd, abi_fp_bfd, ibfd,
13785 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
13790 (_("Warning: %B uses unknown floating point ABI %d "
13791 "(set by %B), %B uses %s"),
13792 obfd, abi_fp_bfd, ibfd,
13793 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
13798 (_("Warning: %B uses unknown floating point ABI %d "
13799 "(set by %B), %B uses %s"),
13800 obfd, abi_fp_bfd, ibfd,
13801 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
13806 (_("Warning: %B uses unknown floating point ABI %d "
13807 "(set by %B), %B uses %s"),
13808 obfd, abi_fp_bfd, ibfd,
13809 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
13814 (_("Warning: %B uses unknown floating point ABI %d "
13815 "(set by %B), %B uses unknown floating point ABI %d"),
13816 obfd, abi_fp_bfd, ibfd,
13817 out_attr[Tag_GNU_MIPS_ABI_FP].i,
13818 in_attr[Tag_GNU_MIPS_ABI_FP].i);
13825 /* Merge Tag_compatibility attributes and any common GNU ones. */
13826 _bfd_elf_merge_object_attributes (ibfd, obfd);
13831 /* Merge backend specific data from an object file to the output
13832 object file when linking. */
13835 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
13837 flagword old_flags;
13838 flagword new_flags;
13840 bfd_boolean null_input_bfd = TRUE;
13843 /* Check if we have the same endianness. */
13844 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
13846 (*_bfd_error_handler)
13847 (_("%B: endianness incompatible with that of the selected emulation"),
13852 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
13855 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
13857 (*_bfd_error_handler)
13858 (_("%B: ABI is incompatible with that of the selected emulation"),
13863 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
13866 new_flags = elf_elfheader (ibfd)->e_flags;
13867 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
13868 old_flags = elf_elfheader (obfd)->e_flags;
13870 if (! elf_flags_init (obfd))
13872 elf_flags_init (obfd) = TRUE;
13873 elf_elfheader (obfd)->e_flags = new_flags;
13874 elf_elfheader (obfd)->e_ident[EI_CLASS]
13875 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
13877 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
13878 && (bfd_get_arch_info (obfd)->the_default
13879 || mips_mach_extends_p (bfd_get_mach (obfd),
13880 bfd_get_mach (ibfd))))
13882 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
13883 bfd_get_mach (ibfd)))
13890 /* Check flag compatibility. */
13892 new_flags &= ~EF_MIPS_NOREORDER;
13893 old_flags &= ~EF_MIPS_NOREORDER;
13895 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
13896 doesn't seem to matter. */
13897 new_flags &= ~EF_MIPS_XGOT;
13898 old_flags &= ~EF_MIPS_XGOT;
13900 /* MIPSpro generates ucode info in n64 objects. Again, we should
13901 just be able to ignore this. */
13902 new_flags &= ~EF_MIPS_UCODE;
13903 old_flags &= ~EF_MIPS_UCODE;
13905 /* DSOs should only be linked with CPIC code. */
13906 if ((ibfd->flags & DYNAMIC) != 0)
13907 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
13909 if (new_flags == old_flags)
13912 /* Check to see if the input BFD actually contains any sections.
13913 If not, its flags may not have been initialised either, but it cannot
13914 actually cause any incompatibility. */
13915 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
13917 /* Ignore synthetic sections and empty .text, .data and .bss sections
13918 which are automatically generated by gas. Also ignore fake
13919 (s)common sections, since merely defining a common symbol does
13920 not affect compatibility. */
13921 if ((sec->flags & SEC_IS_COMMON) == 0
13922 && strcmp (sec->name, ".reginfo")
13923 && strcmp (sec->name, ".mdebug")
13925 || (strcmp (sec->name, ".text")
13926 && strcmp (sec->name, ".data")
13927 && strcmp (sec->name, ".bss"))))
13929 null_input_bfd = FALSE;
13933 if (null_input_bfd)
13938 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
13939 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
13941 (*_bfd_error_handler)
13942 (_("%B: warning: linking abicalls files with non-abicalls files"),
13947 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
13948 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
13949 if (! (new_flags & EF_MIPS_PIC))
13950 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
13952 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13953 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13955 /* Compare the ISAs. */
13956 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
13958 (*_bfd_error_handler)
13959 (_("%B: linking 32-bit code with 64-bit code"),
13963 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
13965 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
13966 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
13968 /* Copy the architecture info from IBFD to OBFD. Also copy
13969 the 32-bit flag (if set) so that we continue to recognise
13970 OBFD as a 32-bit binary. */
13971 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
13972 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
13973 elf_elfheader (obfd)->e_flags
13974 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13976 /* Copy across the ABI flags if OBFD doesn't use them
13977 and if that was what caused us to treat IBFD as 32-bit. */
13978 if ((old_flags & EF_MIPS_ABI) == 0
13979 && mips_32bit_flags_p (new_flags)
13980 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
13981 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
13985 /* The ISAs aren't compatible. */
13986 (*_bfd_error_handler)
13987 (_("%B: linking %s module with previous %s modules"),
13989 bfd_printable_name (ibfd),
13990 bfd_printable_name (obfd));
13995 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13996 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13998 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
13999 does set EI_CLASS differently from any 32-bit ABI. */
14000 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14001 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14002 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14004 /* Only error if both are set (to different values). */
14005 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14006 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14007 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14009 (*_bfd_error_handler)
14010 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14012 elf_mips_abi_name (ibfd),
14013 elf_mips_abi_name (obfd));
14016 new_flags &= ~EF_MIPS_ABI;
14017 old_flags &= ~EF_MIPS_ABI;
14020 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14021 and allow arbitrary mixing of the remaining ASEs (retain the union). */
14022 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14024 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14025 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14026 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14027 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14028 int micro_mis = old_m16 && new_micro;
14029 int m16_mis = old_micro && new_m16;
14031 if (m16_mis || micro_mis)
14033 (*_bfd_error_handler)
14034 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14036 m16_mis ? "MIPS16" : "microMIPS",
14037 m16_mis ? "microMIPS" : "MIPS16");
14041 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14043 new_flags &= ~ EF_MIPS_ARCH_ASE;
14044 old_flags &= ~ EF_MIPS_ARCH_ASE;
14047 /* Warn about any other mismatches */
14048 if (new_flags != old_flags)
14050 (*_bfd_error_handler)
14051 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14052 ibfd, (unsigned long) new_flags,
14053 (unsigned long) old_flags);
14059 bfd_set_error (bfd_error_bad_value);
14066 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14069 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
14071 BFD_ASSERT (!elf_flags_init (abfd)
14072 || elf_elfheader (abfd)->e_flags == flags);
14074 elf_elfheader (abfd)->e_flags = flags;
14075 elf_flags_init (abfd) = TRUE;
14080 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14084 default: return "";
14085 case DT_MIPS_RLD_VERSION:
14086 return "MIPS_RLD_VERSION";
14087 case DT_MIPS_TIME_STAMP:
14088 return "MIPS_TIME_STAMP";
14089 case DT_MIPS_ICHECKSUM:
14090 return "MIPS_ICHECKSUM";
14091 case DT_MIPS_IVERSION:
14092 return "MIPS_IVERSION";
14093 case DT_MIPS_FLAGS:
14094 return "MIPS_FLAGS";
14095 case DT_MIPS_BASE_ADDRESS:
14096 return "MIPS_BASE_ADDRESS";
14098 return "MIPS_MSYM";
14099 case DT_MIPS_CONFLICT:
14100 return "MIPS_CONFLICT";
14101 case DT_MIPS_LIBLIST:
14102 return "MIPS_LIBLIST";
14103 case DT_MIPS_LOCAL_GOTNO:
14104 return "MIPS_LOCAL_GOTNO";
14105 case DT_MIPS_CONFLICTNO:
14106 return "MIPS_CONFLICTNO";
14107 case DT_MIPS_LIBLISTNO:
14108 return "MIPS_LIBLISTNO";
14109 case DT_MIPS_SYMTABNO:
14110 return "MIPS_SYMTABNO";
14111 case DT_MIPS_UNREFEXTNO:
14112 return "MIPS_UNREFEXTNO";
14113 case DT_MIPS_GOTSYM:
14114 return "MIPS_GOTSYM";
14115 case DT_MIPS_HIPAGENO:
14116 return "MIPS_HIPAGENO";
14117 case DT_MIPS_RLD_MAP:
14118 return "MIPS_RLD_MAP";
14119 case DT_MIPS_DELTA_CLASS:
14120 return "MIPS_DELTA_CLASS";
14121 case DT_MIPS_DELTA_CLASS_NO:
14122 return "MIPS_DELTA_CLASS_NO";
14123 case DT_MIPS_DELTA_INSTANCE:
14124 return "MIPS_DELTA_INSTANCE";
14125 case DT_MIPS_DELTA_INSTANCE_NO:
14126 return "MIPS_DELTA_INSTANCE_NO";
14127 case DT_MIPS_DELTA_RELOC:
14128 return "MIPS_DELTA_RELOC";
14129 case DT_MIPS_DELTA_RELOC_NO:
14130 return "MIPS_DELTA_RELOC_NO";
14131 case DT_MIPS_DELTA_SYM:
14132 return "MIPS_DELTA_SYM";
14133 case DT_MIPS_DELTA_SYM_NO:
14134 return "MIPS_DELTA_SYM_NO";
14135 case DT_MIPS_DELTA_CLASSSYM:
14136 return "MIPS_DELTA_CLASSSYM";
14137 case DT_MIPS_DELTA_CLASSSYM_NO:
14138 return "MIPS_DELTA_CLASSSYM_NO";
14139 case DT_MIPS_CXX_FLAGS:
14140 return "MIPS_CXX_FLAGS";
14141 case DT_MIPS_PIXIE_INIT:
14142 return "MIPS_PIXIE_INIT";
14143 case DT_MIPS_SYMBOL_LIB:
14144 return "MIPS_SYMBOL_LIB";
14145 case DT_MIPS_LOCALPAGE_GOTIDX:
14146 return "MIPS_LOCALPAGE_GOTIDX";
14147 case DT_MIPS_LOCAL_GOTIDX:
14148 return "MIPS_LOCAL_GOTIDX";
14149 case DT_MIPS_HIDDEN_GOTIDX:
14150 return "MIPS_HIDDEN_GOTIDX";
14151 case DT_MIPS_PROTECTED_GOTIDX:
14152 return "MIPS_PROTECTED_GOT_IDX";
14153 case DT_MIPS_OPTIONS:
14154 return "MIPS_OPTIONS";
14155 case DT_MIPS_INTERFACE:
14156 return "MIPS_INTERFACE";
14157 case DT_MIPS_DYNSTR_ALIGN:
14158 return "DT_MIPS_DYNSTR_ALIGN";
14159 case DT_MIPS_INTERFACE_SIZE:
14160 return "DT_MIPS_INTERFACE_SIZE";
14161 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14162 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14163 case DT_MIPS_PERF_SUFFIX:
14164 return "DT_MIPS_PERF_SUFFIX";
14165 case DT_MIPS_COMPACT_SIZE:
14166 return "DT_MIPS_COMPACT_SIZE";
14167 case DT_MIPS_GP_VALUE:
14168 return "DT_MIPS_GP_VALUE";
14169 case DT_MIPS_AUX_DYNAMIC:
14170 return "DT_MIPS_AUX_DYNAMIC";
14171 case DT_MIPS_PLTGOT:
14172 return "DT_MIPS_PLTGOT";
14173 case DT_MIPS_RWPLT:
14174 return "DT_MIPS_RWPLT";
14179 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
14183 BFD_ASSERT (abfd != NULL && ptr != NULL);
14185 /* Print normal ELF private data. */
14186 _bfd_elf_print_private_bfd_data (abfd, ptr);
14188 /* xgettext:c-format */
14189 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14191 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14192 fprintf (file, _(" [abi=O32]"));
14193 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14194 fprintf (file, _(" [abi=O64]"));
14195 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14196 fprintf (file, _(" [abi=EABI32]"));
14197 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14198 fprintf (file, _(" [abi=EABI64]"));
14199 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14200 fprintf (file, _(" [abi unknown]"));
14201 else if (ABI_N32_P (abfd))
14202 fprintf (file, _(" [abi=N32]"));
14203 else if (ABI_64_P (abfd))
14204 fprintf (file, _(" [abi=64]"));
14206 fprintf (file, _(" [no abi set]"));
14208 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
14209 fprintf (file, " [mips1]");
14210 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
14211 fprintf (file, " [mips2]");
14212 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
14213 fprintf (file, " [mips3]");
14214 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
14215 fprintf (file, " [mips4]");
14216 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
14217 fprintf (file, " [mips5]");
14218 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
14219 fprintf (file, " [mips32]");
14220 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
14221 fprintf (file, " [mips64]");
14222 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
14223 fprintf (file, " [mips32r2]");
14224 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
14225 fprintf (file, " [mips64r2]");
14227 fprintf (file, _(" [unknown ISA]"));
14229 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14230 fprintf (file, " [mdmx]");
14232 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14233 fprintf (file, " [mips16]");
14235 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14236 fprintf (file, " [micromips]");
14238 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
14239 fprintf (file, " [32bitmode]");
14241 fprintf (file, _(" [not 32bitmode]"));
14243 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
14244 fprintf (file, " [noreorder]");
14246 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
14247 fprintf (file, " [PIC]");
14249 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
14250 fprintf (file, " [CPIC]");
14252 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
14253 fprintf (file, " [XGOT]");
14255 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
14256 fprintf (file, " [UCODE]");
14258 fputc ('\n', file);
14263 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
14265 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14266 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14267 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14268 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14269 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14270 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14271 { NULL, 0, 0, 0, 0 }
14274 /* Merge non visibility st_other attributes. Ensure that the
14275 STO_OPTIONAL flag is copied into h->other, even if this is not a
14276 definiton of the symbol. */
14278 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
14279 const Elf_Internal_Sym *isym,
14280 bfd_boolean definition,
14281 bfd_boolean dynamic ATTRIBUTE_UNUSED)
14283 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
14285 unsigned char other;
14287 other = (definition ? isym->st_other : h->other);
14288 other &= ~ELF_ST_VISIBILITY (-1);
14289 h->other = other | ELF_ST_VISIBILITY (h->other);
14293 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
14294 h->other |= STO_OPTIONAL;
14297 /* Decide whether an undefined symbol is special and can be ignored.
14298 This is the case for OPTIONAL symbols on IRIX. */
14300 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
14302 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
14306 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
14308 return (sym->st_shndx == SHN_COMMON
14309 || sym->st_shndx == SHN_MIPS_ACOMMON
14310 || sym->st_shndx == SHN_MIPS_SCOMMON);
14313 /* Return address for Ith PLT stub in section PLT, for relocation REL
14314 or (bfd_vma) -1 if it should not be included. */
14317 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
14318 const arelent *rel ATTRIBUTE_UNUSED)
14321 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
14322 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
14326 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
14328 struct mips_elf_link_hash_table *htab;
14329 Elf_Internal_Ehdr *i_ehdrp;
14331 i_ehdrp = elf_elfheader (abfd);
14334 htab = mips_elf_hash_table (link_info);
14335 BFD_ASSERT (htab != NULL);
14337 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
14338 i_ehdrp->e_ident[EI_ABIVERSION] = 1;