1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11 This file is part of BFD, the Binary File Descriptor library.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
29 /* This file handles functionality common to the different MIPS ABI's. */
34 #include "libiberty.h"
36 #include "elfxx-mips.h"
38 #include "elf-vxworks.h"
40 /* Get the ECOFF swapping routines. */
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "coff/mips.h"
48 /* Types of TLS GOT entry. */
49 enum mips_got_tls_type {
56 /* This structure is used to hold information about one GOT entry.
57 There are four types of entry:
59 (1) an absolute address
60 requires: abfd == NULL
63 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
64 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
65 fields: abfd, symndx, d.addend, tls_type
67 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
68 requires: abfd != NULL, symndx == -1
72 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
73 fields: none; there's only one of these per GOT. */
76 /* One input bfd that needs the GOT entry. */
78 /* The index of the symbol, as stored in the relocation r_info, if
79 we have a local symbol; -1 otherwise. */
83 /* If abfd == NULL, an address that must be stored in the got. */
85 /* If abfd != NULL && symndx != -1, the addend of the relocation
86 that should be added to the symbol value. */
88 /* If abfd != NULL && symndx == -1, the hash table entry
89 corresponding to a symbol in the GOT. The symbol's entry
90 is in the local area if h->global_got_area is GGA_NONE,
91 otherwise it is in the global area. */
92 struct mips_elf_link_hash_entry *h;
95 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
96 symbol entry with r_symndx == 0. */
97 unsigned char tls_type;
99 /* True if we have filled in the GOT contents for a TLS entry,
100 and created the associated relocations. */
101 unsigned char tls_initialized;
103 /* The offset from the beginning of the .got section to the entry
104 corresponding to this symbol+addend. If it's a global symbol
105 whose offset is yet to be decided, it's going to be -1. */
109 /* This structure represents a GOT page reference from an input bfd.
110 Each instance represents a symbol + ADDEND, where the representation
111 of the symbol depends on whether it is local to the input bfd.
112 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
113 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
115 Page references with SYMNDX >= 0 always become page references
116 in the output. Page references with SYMNDX < 0 only become page
117 references if the symbol binds locally; in other cases, the page
118 reference decays to a global GOT reference. */
119 struct mips_got_page_ref
124 struct mips_elf_link_hash_entry *h;
130 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
131 The structures form a non-overlapping list that is sorted by increasing
133 struct mips_got_page_range
135 struct mips_got_page_range *next;
136 bfd_signed_vma min_addend;
137 bfd_signed_vma max_addend;
140 /* This structure describes the range of addends that are applied to page
141 relocations against a given section. */
142 struct mips_got_page_entry
144 /* The section that these entries are based on. */
146 /* The ranges for this page entry. */
147 struct mips_got_page_range *ranges;
148 /* The maximum number of page entries needed for RANGES. */
152 /* This structure is used to hold .got information when linking. */
156 /* The number of global .got entries. */
157 unsigned int global_gotno;
158 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
159 unsigned int reloc_only_gotno;
160 /* The number of .got slots used for TLS. */
161 unsigned int tls_gotno;
162 /* The first unused TLS .got entry. Used only during
163 mips_elf_initialize_tls_index. */
164 unsigned int tls_assigned_gotno;
165 /* The number of local .got entries, eventually including page entries. */
166 unsigned int local_gotno;
167 /* The maximum number of page entries needed. */
168 unsigned int page_gotno;
169 /* The number of relocations needed for the GOT entries. */
171 /* The first unused local .got entry. */
172 unsigned int assigned_low_gotno;
173 /* The last unused local .got entry. */
174 unsigned int assigned_high_gotno;
175 /* A hash table holding members of the got. */
176 struct htab *got_entries;
177 /* A hash table holding mips_got_page_ref structures. */
178 struct htab *got_page_refs;
179 /* A hash table of mips_got_page_entry structures. */
180 struct htab *got_page_entries;
181 /* In multi-got links, a pointer to the next got (err, rather, most
182 of the time, it points to the previous got). */
183 struct mips_got_info *next;
186 /* Structure passed when merging bfds' gots. */
188 struct mips_elf_got_per_bfd_arg
190 /* The output bfd. */
192 /* The link information. */
193 struct bfd_link_info *info;
194 /* A pointer to the primary got, i.e., the one that's going to get
195 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
197 struct mips_got_info *primary;
198 /* A non-primary got we're trying to merge with other input bfd's
200 struct mips_got_info *current;
201 /* The maximum number of got entries that can be addressed with a
203 unsigned int max_count;
204 /* The maximum number of page entries needed by each got. */
205 unsigned int max_pages;
206 /* The total number of global entries which will live in the
207 primary got and be automatically relocated. This includes
208 those not referenced by the primary GOT but included in
210 unsigned int global_count;
213 /* A structure used to pass information to htab_traverse callbacks
214 when laying out the GOT. */
216 struct mips_elf_traverse_got_arg
218 struct bfd_link_info *info;
219 struct mips_got_info *g;
223 struct _mips_elf_section_data
225 struct bfd_elf_section_data elf;
232 #define mips_elf_section_data(sec) \
233 ((struct _mips_elf_section_data *) elf_section_data (sec))
235 #define is_mips_elf(bfd) \
236 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
237 && elf_tdata (bfd) != NULL \
238 && elf_object_id (bfd) == MIPS_ELF_DATA)
240 /* The ABI says that every symbol used by dynamic relocations must have
241 a global GOT entry. Among other things, this provides the dynamic
242 linker with a free, directly-indexed cache. The GOT can therefore
243 contain symbols that are not referenced by GOT relocations themselves
244 (in other words, it may have symbols that are not referenced by things
245 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
247 GOT relocations are less likely to overflow if we put the associated
248 GOT entries towards the beginning. We therefore divide the global
249 GOT entries into two areas: "normal" and "reloc-only". Entries in
250 the first area can be used for both dynamic relocations and GP-relative
251 accesses, while those in the "reloc-only" area are for dynamic
254 These GGA_* ("Global GOT Area") values are organised so that lower
255 values are more general than higher values. Also, non-GGA_NONE
256 values are ordered by the position of the area in the GOT. */
258 #define GGA_RELOC_ONLY 1
261 /* Information about a non-PIC interface to a PIC function. There are
262 two ways of creating these interfaces. The first is to add:
265 addiu $25,$25,%lo(func)
267 immediately before a PIC function "func". The second is to add:
271 addiu $25,$25,%lo(func)
273 to a separate trampoline section.
275 Stubs of the first kind go in a new section immediately before the
276 target function. Stubs of the second kind go in a single section
277 pointed to by the hash table's "strampoline" field. */
278 struct mips_elf_la25_stub {
279 /* The generated section that contains this stub. */
280 asection *stub_section;
282 /* The offset of the stub from the start of STUB_SECTION. */
285 /* One symbol for the original function. Its location is available
286 in H->root.root.u.def. */
287 struct mips_elf_link_hash_entry *h;
290 /* Macros for populating a mips_elf_la25_stub. */
292 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
293 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
294 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
295 #define LA25_LUI_MICROMIPS(VAL) \
296 (0x41b90000 | (VAL)) /* lui t9,VAL */
297 #define LA25_J_MICROMIPS(VAL) \
298 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
299 #define LA25_ADDIU_MICROMIPS(VAL) \
300 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
302 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
303 the dynamic symbols. */
305 struct mips_elf_hash_sort_data
307 /* The symbol in the global GOT with the lowest dynamic symbol table
309 struct elf_link_hash_entry *low;
310 /* The least dynamic symbol table index corresponding to a non-TLS
311 symbol with a GOT entry. */
312 long min_got_dynindx;
313 /* The greatest dynamic symbol table index corresponding to a symbol
314 with a GOT entry that is not referenced (e.g., a dynamic symbol
315 with dynamic relocations pointing to it from non-primary GOTs). */
316 long max_unref_got_dynindx;
317 /* The greatest dynamic symbol table index not corresponding to a
318 symbol without a GOT entry. */
319 long max_non_got_dynindx;
322 /* We make up to two PLT entries if needed, one for standard MIPS code
323 and one for compressed code, either a MIPS16 or microMIPS one. We
324 keep a separate record of traditional lazy-binding stubs, for easier
329 /* Traditional SVR4 stub offset, or -1 if none. */
332 /* Standard PLT entry offset, or -1 if none. */
335 /* Compressed PLT entry offset, or -1 if none. */
338 /* The corresponding .got.plt index, or -1 if none. */
339 bfd_vma gotplt_index;
341 /* Whether we need a standard PLT entry. */
342 unsigned int need_mips : 1;
344 /* Whether we need a compressed PLT entry. */
345 unsigned int need_comp : 1;
348 /* The MIPS ELF linker needs additional information for each symbol in
349 the global hash table. */
351 struct mips_elf_link_hash_entry
353 struct elf_link_hash_entry root;
355 /* External symbol information. */
358 /* The la25 stub we have created for ths symbol, if any. */
359 struct mips_elf_la25_stub *la25_stub;
361 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
363 unsigned int possibly_dynamic_relocs;
365 /* If there is a stub that 32 bit functions should use to call this
366 16 bit function, this points to the section containing the stub. */
369 /* If there is a stub that 16 bit functions should use to call this
370 32 bit function, this points to the section containing the stub. */
373 /* This is like the call_stub field, but it is used if the function
374 being called returns a floating point value. */
375 asection *call_fp_stub;
377 /* The highest GGA_* value that satisfies all references to this symbol. */
378 unsigned int global_got_area : 2;
380 /* True if all GOT relocations against this symbol are for calls. This is
381 a looser condition than no_fn_stub below, because there may be other
382 non-call non-GOT relocations against the symbol. */
383 unsigned int got_only_for_calls : 1;
385 /* True if one of the relocations described by possibly_dynamic_relocs
386 is against a readonly section. */
387 unsigned int readonly_reloc : 1;
389 /* True if there is a relocation against this symbol that must be
390 resolved by the static linker (in other words, if the relocation
391 cannot possibly be made dynamic). */
392 unsigned int has_static_relocs : 1;
394 /* True if we must not create a .MIPS.stubs entry for this symbol.
395 This is set, for example, if there are relocations related to
396 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
397 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
398 unsigned int no_fn_stub : 1;
400 /* Whether we need the fn_stub; this is true if this symbol appears
401 in any relocs other than a 16 bit call. */
402 unsigned int need_fn_stub : 1;
404 /* True if this symbol is referenced by branch relocations from
405 any non-PIC input file. This is used to determine whether an
406 la25 stub is required. */
407 unsigned int has_nonpic_branches : 1;
409 /* Does this symbol need a traditional MIPS lazy-binding stub
410 (as opposed to a PLT entry)? */
411 unsigned int needs_lazy_stub : 1;
413 /* Does this symbol resolve to a PLT entry? */
414 unsigned int use_plt_entry : 1;
417 /* MIPS ELF linker hash table. */
419 struct mips_elf_link_hash_table
421 struct elf_link_hash_table root;
423 /* The number of .rtproc entries. */
424 bfd_size_type procedure_count;
426 /* The size of the .compact_rel section (if SGI_COMPAT). */
427 bfd_size_type compact_rel_size;
429 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
430 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
431 bfd_boolean use_rld_obj_head;
433 /* The __rld_map or __rld_obj_head symbol. */
434 struct elf_link_hash_entry *rld_symbol;
436 /* This is set if we see any mips16 stub sections. */
437 bfd_boolean mips16_stubs_seen;
439 /* True if we can generate copy relocs and PLTs. */
440 bfd_boolean use_plts_and_copy_relocs;
442 /* True if we can only use 32-bit microMIPS instructions. */
445 /* True if we're generating code for VxWorks. */
446 bfd_boolean is_vxworks;
448 /* True if we already reported the small-data section overflow. */
449 bfd_boolean small_data_overflow_reported;
451 /* Shortcuts to some dynamic sections, or NULL if they are not
462 /* The master GOT information. */
463 struct mips_got_info *got_info;
465 /* The global symbol in the GOT with the lowest index in the dynamic
467 struct elf_link_hash_entry *global_gotsym;
469 /* The size of the PLT header in bytes. */
470 bfd_vma plt_header_size;
472 /* The size of a standard PLT entry in bytes. */
473 bfd_vma plt_mips_entry_size;
475 /* The size of a compressed PLT entry in bytes. */
476 bfd_vma plt_comp_entry_size;
478 /* The offset of the next standard PLT entry to create. */
479 bfd_vma plt_mips_offset;
481 /* The offset of the next compressed PLT entry to create. */
482 bfd_vma plt_comp_offset;
484 /* The index of the next .got.plt entry to create. */
485 bfd_vma plt_got_index;
487 /* The number of functions that need a lazy-binding stub. */
488 bfd_vma lazy_stub_count;
490 /* The size of a function stub entry in bytes. */
491 bfd_vma function_stub_size;
493 /* The number of reserved entries at the beginning of the GOT. */
494 unsigned int reserved_gotno;
496 /* The section used for mips_elf_la25_stub trampolines.
497 See the comment above that structure for details. */
498 asection *strampoline;
500 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
504 /* A function FN (NAME, IS, OS) that creates a new input section
505 called NAME and links it to output section OS. If IS is nonnull,
506 the new section should go immediately before it, otherwise it
507 should go at the (current) beginning of OS.
509 The function returns the new section on success, otherwise it
511 asection *(*add_stub_section) (const char *, asection *, asection *);
513 /* Small local sym cache. */
514 struct sym_cache sym_cache;
516 /* Is the PLT header compressed? */
517 unsigned int plt_header_is_comp : 1;
520 /* Get the MIPS ELF linker hash table from a link_info structure. */
522 #define mips_elf_hash_table(p) \
523 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
524 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
526 /* A structure used to communicate with htab_traverse callbacks. */
527 struct mips_htab_traverse_info
529 /* The usual link-wide information. */
530 struct bfd_link_info *info;
533 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
537 /* MIPS ELF private object data. */
539 struct mips_elf_obj_tdata
541 /* Generic ELF private object data. */
542 struct elf_obj_tdata root;
544 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
547 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
550 /* The GOT requirements of input bfds. */
551 struct mips_got_info *got;
553 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
554 included directly in this one, but there's no point to wasting
555 the memory just for the infrequently called find_nearest_line. */
556 struct mips_elf_find_line *find_line_info;
558 /* An array of stub sections indexed by symbol number. */
559 asection **local_stubs;
560 asection **local_call_stubs;
562 /* The Irix 5 support uses two virtual sections, which represent
563 text/data symbols defined in dynamic objects. */
564 asymbol *elf_data_symbol;
565 asymbol *elf_text_symbol;
566 asection *elf_data_section;
567 asection *elf_text_section;
570 /* Get MIPS ELF private object data from BFD's tdata. */
572 #define mips_elf_tdata(bfd) \
573 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
575 #define TLS_RELOC_P(r_type) \
576 (r_type == R_MIPS_TLS_DTPMOD32 \
577 || r_type == R_MIPS_TLS_DTPMOD64 \
578 || r_type == R_MIPS_TLS_DTPREL32 \
579 || r_type == R_MIPS_TLS_DTPREL64 \
580 || r_type == R_MIPS_TLS_GD \
581 || r_type == R_MIPS_TLS_LDM \
582 || r_type == R_MIPS_TLS_DTPREL_HI16 \
583 || r_type == R_MIPS_TLS_DTPREL_LO16 \
584 || r_type == R_MIPS_TLS_GOTTPREL \
585 || r_type == R_MIPS_TLS_TPREL32 \
586 || r_type == R_MIPS_TLS_TPREL64 \
587 || r_type == R_MIPS_TLS_TPREL_HI16 \
588 || r_type == R_MIPS_TLS_TPREL_LO16 \
589 || r_type == R_MIPS16_TLS_GD \
590 || r_type == R_MIPS16_TLS_LDM \
591 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
592 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
593 || r_type == R_MIPS16_TLS_GOTTPREL \
594 || r_type == R_MIPS16_TLS_TPREL_HI16 \
595 || r_type == R_MIPS16_TLS_TPREL_LO16 \
596 || r_type == R_MICROMIPS_TLS_GD \
597 || r_type == R_MICROMIPS_TLS_LDM \
598 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
599 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
600 || r_type == R_MICROMIPS_TLS_GOTTPREL \
601 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
602 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
604 /* Structure used to pass information to mips_elf_output_extsym. */
609 struct bfd_link_info *info;
610 struct ecoff_debug_info *debug;
611 const struct ecoff_debug_swap *swap;
615 /* The names of the runtime procedure table symbols used on IRIX5. */
617 static const char * const mips_elf_dynsym_rtproc_names[] =
620 "_procedure_string_table",
621 "_procedure_table_size",
625 /* These structures are used to generate the .compact_rel section on
630 unsigned long id1; /* Always one? */
631 unsigned long num; /* Number of compact relocation entries. */
632 unsigned long id2; /* Always two? */
633 unsigned long offset; /* The file offset of the first relocation. */
634 unsigned long reserved0; /* Zero? */
635 unsigned long reserved1; /* Zero? */
644 bfd_byte reserved0[4];
645 bfd_byte reserved1[4];
646 } Elf32_External_compact_rel;
650 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
651 unsigned int rtype : 4; /* Relocation types. See below. */
652 unsigned int dist2to : 8;
653 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
654 unsigned long konst; /* KONST field. See below. */
655 unsigned long vaddr; /* VADDR to be relocated. */
660 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
661 unsigned int rtype : 4; /* Relocation types. See below. */
662 unsigned int dist2to : 8;
663 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
664 unsigned long konst; /* KONST field. See below. */
672 } Elf32_External_crinfo;
678 } Elf32_External_crinfo2;
680 /* These are the constants used to swap the bitfields in a crinfo. */
682 #define CRINFO_CTYPE (0x1)
683 #define CRINFO_CTYPE_SH (31)
684 #define CRINFO_RTYPE (0xf)
685 #define CRINFO_RTYPE_SH (27)
686 #define CRINFO_DIST2TO (0xff)
687 #define CRINFO_DIST2TO_SH (19)
688 #define CRINFO_RELVADDR (0x7ffff)
689 #define CRINFO_RELVADDR_SH (0)
691 /* A compact relocation info has long (3 words) or short (2 words)
692 formats. A short format doesn't have VADDR field and relvaddr
693 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
694 #define CRF_MIPS_LONG 1
695 #define CRF_MIPS_SHORT 0
697 /* There are 4 types of compact relocation at least. The value KONST
698 has different meaning for each type:
701 CT_MIPS_REL32 Address in data
702 CT_MIPS_WORD Address in word (XXX)
703 CT_MIPS_GPHI_LO GP - vaddr
704 CT_MIPS_JMPAD Address to jump
707 #define CRT_MIPS_REL32 0xa
708 #define CRT_MIPS_WORD 0xb
709 #define CRT_MIPS_GPHI_LO 0xc
710 #define CRT_MIPS_JMPAD 0xd
712 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
713 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
714 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
715 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
717 /* The structure of the runtime procedure descriptor created by the
718 loader for use by the static exception system. */
720 typedef struct runtime_pdr {
721 bfd_vma adr; /* Memory address of start of procedure. */
722 long regmask; /* Save register mask. */
723 long regoffset; /* Save register offset. */
724 long fregmask; /* Save floating point register mask. */
725 long fregoffset; /* Save floating point register offset. */
726 long frameoffset; /* Frame size. */
727 short framereg; /* Frame pointer register. */
728 short pcreg; /* Offset or reg of return pc. */
729 long irpss; /* Index into the runtime string table. */
731 struct exception_info *exception_info;/* Pointer to exception array. */
733 #define cbRPDR sizeof (RPDR)
734 #define rpdNil ((pRPDR) 0)
736 static struct mips_got_entry *mips_elf_create_local_got_entry
737 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
738 struct mips_elf_link_hash_entry *, int);
739 static bfd_boolean mips_elf_sort_hash_table_f
740 (struct mips_elf_link_hash_entry *, void *);
741 static bfd_vma mips_elf_high
743 static bfd_boolean mips_elf_create_dynamic_relocation
744 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
745 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
746 bfd_vma *, asection *);
747 static bfd_vma mips_elf_adjust_gp
748 (bfd *, struct mips_got_info *, bfd *);
750 /* This will be used when we sort the dynamic relocation records. */
751 static bfd *reldyn_sorting_bfd;
753 /* True if ABFD is for CPUs with load interlocking that include
754 non-MIPS1 CPUs and R3900. */
755 #define LOAD_INTERLOCKS_P(abfd) \
756 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
757 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
759 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
760 This should be safe for all architectures. We enable this predicate
761 for RM9000 for now. */
762 #define JAL_TO_BAL_P(abfd) \
763 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
765 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
766 This should be safe for all architectures. We enable this predicate for
768 #define JALR_TO_BAL_P(abfd) 1
770 /* True if ABFD is for CPUs that are faster if JR is converted to B.
771 This should be safe for all architectures. We enable this predicate for
773 #define JR_TO_B_P(abfd) 1
775 /* True if ABFD is a PIC object. */
776 #define PIC_OBJECT_P(abfd) \
777 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
779 /* Nonzero if ABFD is using the N32 ABI. */
780 #define ABI_N32_P(abfd) \
781 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
783 /* Nonzero if ABFD is using the N64 ABI. */
784 #define ABI_64_P(abfd) \
785 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
787 /* Nonzero if ABFD is using NewABI conventions. */
788 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
790 /* Nonzero if ABFD has microMIPS code. */
791 #define MICROMIPS_P(abfd) \
792 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
794 /* The IRIX compatibility level we are striving for. */
795 #define IRIX_COMPAT(abfd) \
796 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
798 /* Whether we are trying to be compatible with IRIX at all. */
799 #define SGI_COMPAT(abfd) \
800 (IRIX_COMPAT (abfd) != ict_none)
802 /* The name of the options section. */
803 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
804 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
806 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
807 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
808 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
809 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
811 /* Whether the section is readonly. */
812 #define MIPS_ELF_READONLY_SECTION(sec) \
813 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
814 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
816 /* The name of the stub section. */
817 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
819 /* The size of an external REL relocation. */
820 #define MIPS_ELF_REL_SIZE(abfd) \
821 (get_elf_backend_data (abfd)->s->sizeof_rel)
823 /* The size of an external RELA relocation. */
824 #define MIPS_ELF_RELA_SIZE(abfd) \
825 (get_elf_backend_data (abfd)->s->sizeof_rela)
827 /* The size of an external dynamic table entry. */
828 #define MIPS_ELF_DYN_SIZE(abfd) \
829 (get_elf_backend_data (abfd)->s->sizeof_dyn)
831 /* The size of a GOT entry. */
832 #define MIPS_ELF_GOT_SIZE(abfd) \
833 (get_elf_backend_data (abfd)->s->arch_size / 8)
835 /* The size of the .rld_map section. */
836 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
837 (get_elf_backend_data (abfd)->s->arch_size / 8)
839 /* The size of a symbol-table entry. */
840 #define MIPS_ELF_SYM_SIZE(abfd) \
841 (get_elf_backend_data (abfd)->s->sizeof_sym)
843 /* The default alignment for sections, as a power of two. */
844 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
845 (get_elf_backend_data (abfd)->s->log_file_align)
847 /* Get word-sized data. */
848 #define MIPS_ELF_GET_WORD(abfd, ptr) \
849 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
851 /* Put out word-sized data. */
852 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
854 ? bfd_put_64 (abfd, val, ptr) \
855 : bfd_put_32 (abfd, val, ptr))
857 /* The opcode for word-sized loads (LW or LD). */
858 #define MIPS_ELF_LOAD_WORD(abfd) \
859 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
861 /* Add a dynamic symbol table-entry. */
862 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
863 _bfd_elf_add_dynamic_entry (info, tag, val)
865 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
866 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
868 /* The name of the dynamic relocation section. */
869 #define MIPS_ELF_REL_DYN_NAME(INFO) \
870 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
872 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
873 from smaller values. Start with zero, widen, *then* decrement. */
874 #define MINUS_ONE (((bfd_vma)0) - 1)
875 #define MINUS_TWO (((bfd_vma)0) - 2)
877 /* The value to write into got[1] for SVR4 targets, to identify it is
878 a GNU object. The dynamic linker can then use got[1] to store the
880 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
881 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
883 /* The offset of $gp from the beginning of the .got section. */
884 #define ELF_MIPS_GP_OFFSET(INFO) \
885 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
887 /* The maximum size of the GOT for it to be addressable using 16-bit
889 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
891 /* Instructions which appear in a stub. */
892 #define STUB_LW(abfd) \
894 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
895 : 0x8f998010)) /* lw t9,0x8010(gp) */
896 #define STUB_MOVE(abfd) \
898 ? 0x03e0782d /* daddu t7,ra */ \
899 : 0x03e07821)) /* addu t7,ra */
900 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
901 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
902 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
903 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
904 #define STUB_LI16S(abfd, VAL) \
906 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
907 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
909 /* Likewise for the microMIPS ASE. */
910 #define STUB_LW_MICROMIPS(abfd) \
912 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
913 : 0xff3c8010) /* lw t9,0x8010(gp) */
914 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
915 #define STUB_MOVE32_MICROMIPS(abfd) \
917 ? 0x581f7950 /* daddu t7,ra,zero */ \
918 : 0x001f7950) /* addu t7,ra,zero */
919 #define STUB_LUI_MICROMIPS(VAL) \
920 (0x41b80000 + (VAL)) /* lui t8,VAL */
921 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
922 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
923 #define STUB_ORI_MICROMIPS(VAL) \
924 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
925 #define STUB_LI16U_MICROMIPS(VAL) \
926 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
927 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
929 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
930 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
932 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
933 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
934 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
935 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
936 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
937 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
939 /* The name of the dynamic interpreter. This is put in the .interp
942 #define ELF_DYNAMIC_INTERPRETER(abfd) \
943 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
944 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
945 : "/usr/lib/libc.so.1")
948 #define MNAME(bfd,pre,pos) \
949 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
950 #define ELF_R_SYM(bfd, i) \
951 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
952 #define ELF_R_TYPE(bfd, i) \
953 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
954 #define ELF_R_INFO(bfd, s, t) \
955 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
957 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
958 #define ELF_R_SYM(bfd, i) \
960 #define ELF_R_TYPE(bfd, i) \
962 #define ELF_R_INFO(bfd, s, t) \
963 (ELF32_R_INFO (s, t))
966 /* The mips16 compiler uses a couple of special sections to handle
967 floating point arguments.
969 Section names that look like .mips16.fn.FNNAME contain stubs that
970 copy floating point arguments from the fp regs to the gp regs and
971 then jump to FNNAME. If any 32 bit function calls FNNAME, the
972 call should be redirected to the stub instead. If no 32 bit
973 function calls FNNAME, the stub should be discarded. We need to
974 consider any reference to the function, not just a call, because
975 if the address of the function is taken we will need the stub,
976 since the address might be passed to a 32 bit function.
978 Section names that look like .mips16.call.FNNAME contain stubs
979 that copy floating point arguments from the gp regs to the fp
980 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
981 then any 16 bit function that calls FNNAME should be redirected
982 to the stub instead. If FNNAME is not a 32 bit function, the
983 stub should be discarded.
985 .mips16.call.fp.FNNAME sections are similar, but contain stubs
986 which call FNNAME and then copy the return value from the fp regs
987 to the gp regs. These stubs store the return value in $18 while
988 calling FNNAME; any function which might call one of these stubs
989 must arrange to save $18 around the call. (This case is not
990 needed for 32 bit functions that call 16 bit functions, because
991 16 bit functions always return floating point values in both
994 Note that in all cases FNNAME might be defined statically.
995 Therefore, FNNAME is not used literally. Instead, the relocation
996 information will indicate which symbol the section is for.
998 We record any stubs that we find in the symbol table. */
1000 #define FN_STUB ".mips16.fn."
1001 #define CALL_STUB ".mips16.call."
1002 #define CALL_FP_STUB ".mips16.call.fp."
1004 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1005 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1006 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1008 /* The format of the first PLT entry in an O32 executable. */
1009 static const bfd_vma mips_o32_exec_plt0_entry[] =
1011 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1012 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1013 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1014 0x031cc023, /* subu $24, $24, $28 */
1015 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1016 0x0018c082, /* srl $24, $24, 2 */
1017 0x0320f809, /* jalr $25 */
1018 0x2718fffe /* subu $24, $24, 2 */
1021 /* The format of the first PLT entry in an N32 executable. Different
1022 because gp ($28) is not available; we use t2 ($14) instead. */
1023 static const bfd_vma mips_n32_exec_plt0_entry[] =
1025 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1026 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1027 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1028 0x030ec023, /* subu $24, $24, $14 */
1029 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1030 0x0018c082, /* srl $24, $24, 2 */
1031 0x0320f809, /* jalr $25 */
1032 0x2718fffe /* subu $24, $24, 2 */
1035 /* The format of the first PLT entry in an N64 executable. Different
1036 from N32 because of the increased size of GOT entries. */
1037 static const bfd_vma mips_n64_exec_plt0_entry[] =
1039 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1040 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1041 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1042 0x030ec023, /* subu $24, $24, $14 */
1043 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1044 0x0018c0c2, /* srl $24, $24, 3 */
1045 0x0320f809, /* jalr $25 */
1046 0x2718fffe /* subu $24, $24, 2 */
1049 /* The format of the microMIPS first PLT entry in an O32 executable.
1050 We rely on v0 ($2) rather than t8 ($24) to contain the address
1051 of the GOTPLT entry handled, so this stub may only be used when
1052 all the subsequent PLT entries are microMIPS code too.
1054 The trailing NOP is for alignment and correct disassembly only. */
1055 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1057 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1058 0xff23, 0x0000, /* lw $25, 0($3) */
1059 0x0535, /* subu $2, $2, $3 */
1060 0x2525, /* srl $2, $2, 2 */
1061 0x3302, 0xfffe, /* subu $24, $2, 2 */
1062 0x0dff, /* move $15, $31 */
1063 0x45f9, /* jalrs $25 */
1064 0x0f83, /* move $28, $3 */
1068 /* The format of the microMIPS first PLT entry in an O32 executable
1069 in the insn32 mode. */
1070 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1072 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1073 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1074 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1075 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1076 0x001f, 0x7950, /* move $15, $31 */
1077 0x0318, 0x1040, /* srl $24, $24, 2 */
1078 0x03f9, 0x0f3c, /* jalr $25 */
1079 0x3318, 0xfffe /* subu $24, $24, 2 */
1082 /* The format of subsequent standard PLT entries. */
1083 static const bfd_vma mips_exec_plt_entry[] =
1085 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1086 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1087 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1088 0x03200008 /* jr $25 */
1091 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1092 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1093 directly addressable. */
1094 static const bfd_vma mips16_o32_exec_plt_entry[] =
1096 0xb203, /* lw $2, 12($pc) */
1097 0x9a60, /* lw $3, 0($2) */
1098 0x651a, /* move $24, $2 */
1100 0x653b, /* move $25, $3 */
1102 0x0000, 0x0000 /* .word (.got.plt entry) */
1105 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1106 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1107 static const bfd_vma micromips_o32_exec_plt_entry[] =
1109 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1110 0xff22, 0x0000, /* lw $25, 0($2) */
1111 0x4599, /* jr $25 */
1112 0x0f02 /* move $24, $2 */
1115 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1116 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1118 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1119 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1120 0x0019, 0x0f3c, /* jr $25 */
1121 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1124 /* The format of the first PLT entry in a VxWorks executable. */
1125 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1127 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1128 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1129 0x8f390008, /* lw t9, 8(t9) */
1130 0x00000000, /* nop */
1131 0x03200008, /* jr t9 */
1132 0x00000000 /* nop */
1135 /* The format of subsequent PLT entries. */
1136 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1138 0x10000000, /* b .PLT_resolver */
1139 0x24180000, /* li t8, <pltindex> */
1140 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1141 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1142 0x8f390000, /* lw t9, 0(t9) */
1143 0x00000000, /* nop */
1144 0x03200008, /* jr t9 */
1145 0x00000000 /* nop */
1148 /* The format of the first PLT entry in a VxWorks shared object. */
1149 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1151 0x8f990008, /* lw t9, 8(gp) */
1152 0x00000000, /* nop */
1153 0x03200008, /* jr t9 */
1154 0x00000000, /* nop */
1155 0x00000000, /* nop */
1156 0x00000000 /* nop */
1159 /* The format of subsequent PLT entries. */
1160 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1162 0x10000000, /* b .PLT_resolver */
1163 0x24180000 /* li t8, <pltindex> */
1166 /* microMIPS 32-bit opcode helper installer. */
1169 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1171 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1172 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1175 /* microMIPS 32-bit opcode helper retriever. */
1178 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1180 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1183 /* Look up an entry in a MIPS ELF linker hash table. */
1185 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1186 ((struct mips_elf_link_hash_entry *) \
1187 elf_link_hash_lookup (&(table)->root, (string), (create), \
1190 /* Traverse a MIPS ELF linker hash table. */
1192 #define mips_elf_link_hash_traverse(table, func, info) \
1193 (elf_link_hash_traverse \
1195 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1198 /* Find the base offsets for thread-local storage in this object,
1199 for GD/LD and IE/LE respectively. */
1201 #define TP_OFFSET 0x7000
1202 #define DTP_OFFSET 0x8000
1205 dtprel_base (struct bfd_link_info *info)
1207 /* If tls_sec is NULL, we should have signalled an error already. */
1208 if (elf_hash_table (info)->tls_sec == NULL)
1210 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1214 tprel_base (struct bfd_link_info *info)
1216 /* If tls_sec is NULL, we should have signalled an error already. */
1217 if (elf_hash_table (info)->tls_sec == NULL)
1219 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1222 /* Create an entry in a MIPS ELF linker hash table. */
1224 static struct bfd_hash_entry *
1225 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1226 struct bfd_hash_table *table, const char *string)
1228 struct mips_elf_link_hash_entry *ret =
1229 (struct mips_elf_link_hash_entry *) entry;
1231 /* Allocate the structure if it has not already been allocated by a
1234 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1236 return (struct bfd_hash_entry *) ret;
1238 /* Call the allocation method of the superclass. */
1239 ret = ((struct mips_elf_link_hash_entry *)
1240 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1244 /* Set local fields. */
1245 memset (&ret->esym, 0, sizeof (EXTR));
1246 /* We use -2 as a marker to indicate that the information has
1247 not been set. -1 means there is no associated ifd. */
1250 ret->possibly_dynamic_relocs = 0;
1251 ret->fn_stub = NULL;
1252 ret->call_stub = NULL;
1253 ret->call_fp_stub = NULL;
1254 ret->global_got_area = GGA_NONE;
1255 ret->got_only_for_calls = TRUE;
1256 ret->readonly_reloc = FALSE;
1257 ret->has_static_relocs = FALSE;
1258 ret->no_fn_stub = FALSE;
1259 ret->need_fn_stub = FALSE;
1260 ret->has_nonpic_branches = FALSE;
1261 ret->needs_lazy_stub = FALSE;
1262 ret->use_plt_entry = FALSE;
1265 return (struct bfd_hash_entry *) ret;
1268 /* Allocate MIPS ELF private object data. */
1271 _bfd_mips_elf_mkobject (bfd *abfd)
1273 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1278 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1280 if (!sec->used_by_bfd)
1282 struct _mips_elf_section_data *sdata;
1283 bfd_size_type amt = sizeof (*sdata);
1285 sdata = bfd_zalloc (abfd, amt);
1288 sec->used_by_bfd = sdata;
1291 return _bfd_elf_new_section_hook (abfd, sec);
1294 /* Read ECOFF debugging information from a .mdebug section into a
1295 ecoff_debug_info structure. */
1298 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1299 struct ecoff_debug_info *debug)
1302 const struct ecoff_debug_swap *swap;
1305 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1306 memset (debug, 0, sizeof (*debug));
1308 ext_hdr = bfd_malloc (swap->external_hdr_size);
1309 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1312 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1313 swap->external_hdr_size))
1316 symhdr = &debug->symbolic_header;
1317 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1319 /* The symbolic header contains absolute file offsets and sizes to
1321 #define READ(ptr, offset, count, size, type) \
1322 if (symhdr->count == 0) \
1323 debug->ptr = NULL; \
1326 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1327 debug->ptr = bfd_malloc (amt); \
1328 if (debug->ptr == NULL) \
1329 goto error_return; \
1330 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1331 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1332 goto error_return; \
1335 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1336 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1337 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1338 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1339 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1340 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1342 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1343 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1344 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1345 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1346 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1354 if (ext_hdr != NULL)
1356 if (debug->line != NULL)
1358 if (debug->external_dnr != NULL)
1359 free (debug->external_dnr);
1360 if (debug->external_pdr != NULL)
1361 free (debug->external_pdr);
1362 if (debug->external_sym != NULL)
1363 free (debug->external_sym);
1364 if (debug->external_opt != NULL)
1365 free (debug->external_opt);
1366 if (debug->external_aux != NULL)
1367 free (debug->external_aux);
1368 if (debug->ss != NULL)
1370 if (debug->ssext != NULL)
1371 free (debug->ssext);
1372 if (debug->external_fdr != NULL)
1373 free (debug->external_fdr);
1374 if (debug->external_rfd != NULL)
1375 free (debug->external_rfd);
1376 if (debug->external_ext != NULL)
1377 free (debug->external_ext);
1381 /* Swap RPDR (runtime procedure table entry) for output. */
1384 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1386 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1387 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1388 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1389 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1390 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1391 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1393 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1394 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1396 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1399 /* Create a runtime procedure table from the .mdebug section. */
1402 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1403 struct bfd_link_info *info, asection *s,
1404 struct ecoff_debug_info *debug)
1406 const struct ecoff_debug_swap *swap;
1407 HDRR *hdr = &debug->symbolic_header;
1409 struct rpdr_ext *erp;
1411 struct pdr_ext *epdr;
1412 struct sym_ext *esym;
1416 bfd_size_type count;
1417 unsigned long sindex;
1421 const char *no_name_func = _("static procedure (no name)");
1429 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1431 sindex = strlen (no_name_func) + 1;
1432 count = hdr->ipdMax;
1435 size = swap->external_pdr_size;
1437 epdr = bfd_malloc (size * count);
1441 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1444 size = sizeof (RPDR);
1445 rp = rpdr = bfd_malloc (size * count);
1449 size = sizeof (char *);
1450 sv = bfd_malloc (size * count);
1454 count = hdr->isymMax;
1455 size = swap->external_sym_size;
1456 esym = bfd_malloc (size * count);
1460 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1463 count = hdr->issMax;
1464 ss = bfd_malloc (count);
1467 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1470 count = hdr->ipdMax;
1471 for (i = 0; i < (unsigned long) count; i++, rp++)
1473 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1474 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1475 rp->adr = sym.value;
1476 rp->regmask = pdr.regmask;
1477 rp->regoffset = pdr.regoffset;
1478 rp->fregmask = pdr.fregmask;
1479 rp->fregoffset = pdr.fregoffset;
1480 rp->frameoffset = pdr.frameoffset;
1481 rp->framereg = pdr.framereg;
1482 rp->pcreg = pdr.pcreg;
1484 sv[i] = ss + sym.iss;
1485 sindex += strlen (sv[i]) + 1;
1489 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1490 size = BFD_ALIGN (size, 16);
1491 rtproc = bfd_alloc (abfd, size);
1494 mips_elf_hash_table (info)->procedure_count = 0;
1498 mips_elf_hash_table (info)->procedure_count = count + 2;
1501 memset (erp, 0, sizeof (struct rpdr_ext));
1503 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1504 strcpy (str, no_name_func);
1505 str += strlen (no_name_func) + 1;
1506 for (i = 0; i < count; i++)
1508 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1509 strcpy (str, sv[i]);
1510 str += strlen (sv[i]) + 1;
1512 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1514 /* Set the size and contents of .rtproc section. */
1516 s->contents = rtproc;
1518 /* Skip this section later on (I don't think this currently
1519 matters, but someday it might). */
1520 s->map_head.link_order = NULL;
1549 /* We're going to create a stub for H. Create a symbol for the stub's
1550 value and size, to help make the disassembly easier to read. */
1553 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1554 struct mips_elf_link_hash_entry *h,
1555 const char *prefix, asection *s, bfd_vma value,
1558 struct bfd_link_hash_entry *bh;
1559 struct elf_link_hash_entry *elfh;
1562 if (ELF_ST_IS_MICROMIPS (h->root.other))
1565 /* Create a new symbol. */
1566 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1568 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1569 BSF_LOCAL, s, value, NULL,
1573 /* Make it a local function. */
1574 elfh = (struct elf_link_hash_entry *) bh;
1575 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1577 elfh->forced_local = 1;
1581 /* We're about to redefine H. Create a symbol to represent H's
1582 current value and size, to help make the disassembly easier
1586 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1587 struct mips_elf_link_hash_entry *h,
1590 struct bfd_link_hash_entry *bh;
1591 struct elf_link_hash_entry *elfh;
1596 /* Read the symbol's value. */
1597 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1598 || h->root.root.type == bfd_link_hash_defweak);
1599 s = h->root.root.u.def.section;
1600 value = h->root.root.u.def.value;
1602 /* Create a new symbol. */
1603 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1605 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1606 BSF_LOCAL, s, value, NULL,
1610 /* Make it local and copy the other attributes from H. */
1611 elfh = (struct elf_link_hash_entry *) bh;
1612 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1613 elfh->other = h->root.other;
1614 elfh->size = h->root.size;
1615 elfh->forced_local = 1;
1619 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1620 function rather than to a hard-float stub. */
1623 section_allows_mips16_refs_p (asection *section)
1627 name = bfd_get_section_name (section->owner, section);
1628 return (FN_STUB_P (name)
1629 || CALL_STUB_P (name)
1630 || CALL_FP_STUB_P (name)
1631 || strcmp (name, ".pdr") == 0);
1634 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1635 stub section of some kind. Return the R_SYMNDX of the target
1636 function, or 0 if we can't decide which function that is. */
1638 static unsigned long
1639 mips16_stub_symndx (const struct elf_backend_data *bed,
1640 asection *sec ATTRIBUTE_UNUSED,
1641 const Elf_Internal_Rela *relocs,
1642 const Elf_Internal_Rela *relend)
1644 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1645 const Elf_Internal_Rela *rel;
1647 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1648 one in a compound relocation. */
1649 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1650 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1651 return ELF_R_SYM (sec->owner, rel->r_info);
1653 /* Otherwise trust the first relocation, whatever its kind. This is
1654 the traditional behavior. */
1655 if (relocs < relend)
1656 return ELF_R_SYM (sec->owner, relocs->r_info);
1661 /* Check the mips16 stubs for a particular symbol, and see if we can
1665 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1666 struct mips_elf_link_hash_entry *h)
1668 /* Dynamic symbols must use the standard call interface, in case other
1669 objects try to call them. */
1670 if (h->fn_stub != NULL
1671 && h->root.dynindx != -1)
1673 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1674 h->need_fn_stub = TRUE;
1677 if (h->fn_stub != NULL
1678 && ! h->need_fn_stub)
1680 /* We don't need the fn_stub; the only references to this symbol
1681 are 16 bit calls. Clobber the size to 0 to prevent it from
1682 being included in the link. */
1683 h->fn_stub->size = 0;
1684 h->fn_stub->flags &= ~SEC_RELOC;
1685 h->fn_stub->reloc_count = 0;
1686 h->fn_stub->flags |= SEC_EXCLUDE;
1689 if (h->call_stub != NULL
1690 && ELF_ST_IS_MIPS16 (h->root.other))
1692 /* We don't need the call_stub; this is a 16 bit function, so
1693 calls from other 16 bit functions are OK. Clobber the size
1694 to 0 to prevent it from being included in the link. */
1695 h->call_stub->size = 0;
1696 h->call_stub->flags &= ~SEC_RELOC;
1697 h->call_stub->reloc_count = 0;
1698 h->call_stub->flags |= SEC_EXCLUDE;
1701 if (h->call_fp_stub != NULL
1702 && ELF_ST_IS_MIPS16 (h->root.other))
1704 /* We don't need the call_stub; this is a 16 bit function, so
1705 calls from other 16 bit functions are OK. Clobber the size
1706 to 0 to prevent it from being included in the link. */
1707 h->call_fp_stub->size = 0;
1708 h->call_fp_stub->flags &= ~SEC_RELOC;
1709 h->call_fp_stub->reloc_count = 0;
1710 h->call_fp_stub->flags |= SEC_EXCLUDE;
1714 /* Hashtable callbacks for mips_elf_la25_stubs. */
1717 mips_elf_la25_stub_hash (const void *entry_)
1719 const struct mips_elf_la25_stub *entry;
1721 entry = (struct mips_elf_la25_stub *) entry_;
1722 return entry->h->root.root.u.def.section->id
1723 + entry->h->root.root.u.def.value;
1727 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1729 const struct mips_elf_la25_stub *entry1, *entry2;
1731 entry1 = (struct mips_elf_la25_stub *) entry1_;
1732 entry2 = (struct mips_elf_la25_stub *) entry2_;
1733 return ((entry1->h->root.root.u.def.section
1734 == entry2->h->root.root.u.def.section)
1735 && (entry1->h->root.root.u.def.value
1736 == entry2->h->root.root.u.def.value));
1739 /* Called by the linker to set up the la25 stub-creation code. FN is
1740 the linker's implementation of add_stub_function. Return true on
1744 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1745 asection *(*fn) (const char *, asection *,
1748 struct mips_elf_link_hash_table *htab;
1750 htab = mips_elf_hash_table (info);
1754 htab->add_stub_section = fn;
1755 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1756 mips_elf_la25_stub_eq, NULL);
1757 if (htab->la25_stubs == NULL)
1763 /* Return true if H is a locally-defined PIC function, in the sense
1764 that it or its fn_stub might need $25 to be valid on entry.
1765 Note that MIPS16 functions set up $gp using PC-relative instructions,
1766 so they themselves never need $25 to be valid. Only non-MIPS16
1767 entry points are of interest here. */
1770 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1772 return ((h->root.root.type == bfd_link_hash_defined
1773 || h->root.root.type == bfd_link_hash_defweak)
1774 && h->root.def_regular
1775 && !bfd_is_abs_section (h->root.root.u.def.section)
1776 && (!ELF_ST_IS_MIPS16 (h->root.other)
1777 || (h->fn_stub && h->need_fn_stub))
1778 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1779 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1782 /* Set *SEC to the input section that contains the target of STUB.
1783 Return the offset of the target from the start of that section. */
1786 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1789 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1791 BFD_ASSERT (stub->h->need_fn_stub);
1792 *sec = stub->h->fn_stub;
1797 *sec = stub->h->root.root.u.def.section;
1798 return stub->h->root.root.u.def.value;
1802 /* STUB describes an la25 stub that we have decided to implement
1803 by inserting an LUI/ADDIU pair before the target function.
1804 Create the section and redirect the function symbol to it. */
1807 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1808 struct bfd_link_info *info)
1810 struct mips_elf_link_hash_table *htab;
1812 asection *s, *input_section;
1815 htab = mips_elf_hash_table (info);
1819 /* Create a unique name for the new section. */
1820 name = bfd_malloc (11 + sizeof (".text.stub."));
1823 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1825 /* Create the section. */
1826 mips_elf_get_la25_target (stub, &input_section);
1827 s = htab->add_stub_section (name, input_section,
1828 input_section->output_section);
1832 /* Make sure that any padding goes before the stub. */
1833 align = input_section->alignment_power;
1834 if (!bfd_set_section_alignment (s->owner, s, align))
1837 s->size = (1 << align) - 8;
1839 /* Create a symbol for the stub. */
1840 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1841 stub->stub_section = s;
1842 stub->offset = s->size;
1844 /* Allocate room for it. */
1849 /* STUB describes an la25 stub that we have decided to implement
1850 with a separate trampoline. Allocate room for it and redirect
1851 the function symbol to it. */
1854 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1855 struct bfd_link_info *info)
1857 struct mips_elf_link_hash_table *htab;
1860 htab = mips_elf_hash_table (info);
1864 /* Create a trampoline section, if we haven't already. */
1865 s = htab->strampoline;
1868 asection *input_section = stub->h->root.root.u.def.section;
1869 s = htab->add_stub_section (".text", NULL,
1870 input_section->output_section);
1871 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1873 htab->strampoline = s;
1876 /* Create a symbol for the stub. */
1877 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1878 stub->stub_section = s;
1879 stub->offset = s->size;
1881 /* Allocate room for it. */
1886 /* H describes a symbol that needs an la25 stub. Make sure that an
1887 appropriate stub exists and point H at it. */
1890 mips_elf_add_la25_stub (struct bfd_link_info *info,
1891 struct mips_elf_link_hash_entry *h)
1893 struct mips_elf_link_hash_table *htab;
1894 struct mips_elf_la25_stub search, *stub;
1895 bfd_boolean use_trampoline_p;
1900 /* Describe the stub we want. */
1901 search.stub_section = NULL;
1905 /* See if we've already created an equivalent stub. */
1906 htab = mips_elf_hash_table (info);
1910 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1914 stub = (struct mips_elf_la25_stub *) *slot;
1917 /* We can reuse the existing stub. */
1918 h->la25_stub = stub;
1922 /* Create a permanent copy of ENTRY and add it to the hash table. */
1923 stub = bfd_malloc (sizeof (search));
1929 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1930 of the section and if we would need no more than 2 nops. */
1931 value = mips_elf_get_la25_target (stub, &s);
1932 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1934 h->la25_stub = stub;
1935 return (use_trampoline_p
1936 ? mips_elf_add_la25_trampoline (stub, info)
1937 : mips_elf_add_la25_intro (stub, info));
1940 /* A mips_elf_link_hash_traverse callback that is called before sizing
1941 sections. DATA points to a mips_htab_traverse_info structure. */
1944 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1946 struct mips_htab_traverse_info *hti;
1948 hti = (struct mips_htab_traverse_info *) data;
1949 if (!hti->info->relocatable)
1950 mips_elf_check_mips16_stubs (hti->info, h);
1952 if (mips_elf_local_pic_function_p (h))
1954 /* PR 12845: If H is in a section that has been garbage
1955 collected it will have its output section set to *ABS*. */
1956 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1959 /* H is a function that might need $25 to be valid on entry.
1960 If we're creating a non-PIC relocatable object, mark H as
1961 being PIC. If we're creating a non-relocatable object with
1962 non-PIC branches and jumps to H, make sure that H has an la25
1964 if (hti->info->relocatable)
1966 if (!PIC_OBJECT_P (hti->output_bfd))
1967 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1969 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1978 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1979 Most mips16 instructions are 16 bits, but these instructions
1982 The format of these instructions is:
1984 +--------------+--------------------------------+
1985 | JALX | X| Imm 20:16 | Imm 25:21 |
1986 +--------------+--------------------------------+
1988 +-----------------------------------------------+
1990 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1991 Note that the immediate value in the first word is swapped.
1993 When producing a relocatable object file, R_MIPS16_26 is
1994 handled mostly like R_MIPS_26. In particular, the addend is
1995 stored as a straight 26-bit value in a 32-bit instruction.
1996 (gas makes life simpler for itself by never adjusting a
1997 R_MIPS16_26 reloc to be against a section, so the addend is
1998 always zero). However, the 32 bit instruction is stored as 2
1999 16-bit values, rather than a single 32-bit value. In a
2000 big-endian file, the result is the same; in a little-endian
2001 file, the two 16-bit halves of the 32 bit value are swapped.
2002 This is so that a disassembler can recognize the jal
2005 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2006 instruction stored as two 16-bit values. The addend A is the
2007 contents of the targ26 field. The calculation is the same as
2008 R_MIPS_26. When storing the calculated value, reorder the
2009 immediate value as shown above, and don't forget to store the
2010 value as two 16-bit values.
2012 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2016 +--------+----------------------+
2020 +--------+----------------------+
2023 +----------+------+-------------+
2027 +----------+--------------------+
2028 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2029 ((sub1 << 16) | sub2)).
2031 When producing a relocatable object file, the calculation is
2032 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2033 When producing a fully linked file, the calculation is
2034 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2035 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2037 The table below lists the other MIPS16 instruction relocations.
2038 Each one is calculated in the same way as the non-MIPS16 relocation
2039 given on the right, but using the extended MIPS16 layout of 16-bit
2042 R_MIPS16_GPREL R_MIPS_GPREL16
2043 R_MIPS16_GOT16 R_MIPS_GOT16
2044 R_MIPS16_CALL16 R_MIPS_CALL16
2045 R_MIPS16_HI16 R_MIPS_HI16
2046 R_MIPS16_LO16 R_MIPS_LO16
2048 A typical instruction will have a format like this:
2050 +--------------+--------------------------------+
2051 | EXTEND | Imm 10:5 | Imm 15:11 |
2052 +--------------+--------------------------------+
2053 | Major | rx | ry | Imm 4:0 |
2054 +--------------+--------------------------------+
2056 EXTEND is the five bit value 11110. Major is the instruction
2059 All we need to do here is shuffle the bits appropriately.
2060 As above, the two 16-bit halves must be swapped on a
2061 little-endian system. */
2063 static inline bfd_boolean
2064 mips16_reloc_p (int r_type)
2069 case R_MIPS16_GPREL:
2070 case R_MIPS16_GOT16:
2071 case R_MIPS16_CALL16:
2074 case R_MIPS16_TLS_GD:
2075 case R_MIPS16_TLS_LDM:
2076 case R_MIPS16_TLS_DTPREL_HI16:
2077 case R_MIPS16_TLS_DTPREL_LO16:
2078 case R_MIPS16_TLS_GOTTPREL:
2079 case R_MIPS16_TLS_TPREL_HI16:
2080 case R_MIPS16_TLS_TPREL_LO16:
2088 /* Check if a microMIPS reloc. */
2090 static inline bfd_boolean
2091 micromips_reloc_p (unsigned int r_type)
2093 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2096 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2097 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2098 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2100 static inline bfd_boolean
2101 micromips_reloc_shuffle_p (unsigned int r_type)
2103 return (micromips_reloc_p (r_type)
2104 && r_type != R_MICROMIPS_PC7_S1
2105 && r_type != R_MICROMIPS_PC10_S1);
2108 static inline bfd_boolean
2109 got16_reloc_p (int r_type)
2111 return (r_type == R_MIPS_GOT16
2112 || r_type == R_MIPS16_GOT16
2113 || r_type == R_MICROMIPS_GOT16);
2116 static inline bfd_boolean
2117 call16_reloc_p (int r_type)
2119 return (r_type == R_MIPS_CALL16
2120 || r_type == R_MIPS16_CALL16
2121 || r_type == R_MICROMIPS_CALL16);
2124 static inline bfd_boolean
2125 got_disp_reloc_p (unsigned int r_type)
2127 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2130 static inline bfd_boolean
2131 got_page_reloc_p (unsigned int r_type)
2133 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2136 static inline bfd_boolean
2137 got_ofst_reloc_p (unsigned int r_type)
2139 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2142 static inline bfd_boolean
2143 got_hi16_reloc_p (unsigned int r_type)
2145 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2148 static inline bfd_boolean
2149 got_lo16_reloc_p (unsigned int r_type)
2151 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2154 static inline bfd_boolean
2155 call_hi16_reloc_p (unsigned int r_type)
2157 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2160 static inline bfd_boolean
2161 call_lo16_reloc_p (unsigned int r_type)
2163 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2166 static inline bfd_boolean
2167 hi16_reloc_p (int r_type)
2169 return (r_type == R_MIPS_HI16
2170 || r_type == R_MIPS16_HI16
2171 || r_type == R_MICROMIPS_HI16);
2174 static inline bfd_boolean
2175 lo16_reloc_p (int r_type)
2177 return (r_type == R_MIPS_LO16
2178 || r_type == R_MIPS16_LO16
2179 || r_type == R_MICROMIPS_LO16);
2182 static inline bfd_boolean
2183 mips16_call_reloc_p (int r_type)
2185 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2188 static inline bfd_boolean
2189 jal_reloc_p (int r_type)
2191 return (r_type == R_MIPS_26
2192 || r_type == R_MIPS16_26
2193 || r_type == R_MICROMIPS_26_S1);
2196 static inline bfd_boolean
2197 micromips_branch_reloc_p (int r_type)
2199 return (r_type == R_MICROMIPS_26_S1
2200 || r_type == R_MICROMIPS_PC16_S1
2201 || r_type == R_MICROMIPS_PC10_S1
2202 || r_type == R_MICROMIPS_PC7_S1);
2205 static inline bfd_boolean
2206 tls_gd_reloc_p (unsigned int r_type)
2208 return (r_type == R_MIPS_TLS_GD
2209 || r_type == R_MIPS16_TLS_GD
2210 || r_type == R_MICROMIPS_TLS_GD);
2213 static inline bfd_boolean
2214 tls_ldm_reloc_p (unsigned int r_type)
2216 return (r_type == R_MIPS_TLS_LDM
2217 || r_type == R_MIPS16_TLS_LDM
2218 || r_type == R_MICROMIPS_TLS_LDM);
2221 static inline bfd_boolean
2222 tls_gottprel_reloc_p (unsigned int r_type)
2224 return (r_type == R_MIPS_TLS_GOTTPREL
2225 || r_type == R_MIPS16_TLS_GOTTPREL
2226 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2230 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2231 bfd_boolean jal_shuffle, bfd_byte *data)
2233 bfd_vma first, second, val;
2235 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2238 /* Pick up the first and second halfwords of the instruction. */
2239 first = bfd_get_16 (abfd, data);
2240 second = bfd_get_16 (abfd, data + 2);
2241 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2242 val = first << 16 | second;
2243 else if (r_type != R_MIPS16_26)
2244 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2245 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2247 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2248 | ((first & 0x1f) << 21) | second);
2249 bfd_put_32 (abfd, val, data);
2253 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2254 bfd_boolean jal_shuffle, bfd_byte *data)
2256 bfd_vma first, second, val;
2258 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2261 val = bfd_get_32 (abfd, data);
2262 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2264 second = val & 0xffff;
2267 else if (r_type != R_MIPS16_26)
2269 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2270 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2274 second = val & 0xffff;
2275 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2276 | ((val >> 21) & 0x1f);
2278 bfd_put_16 (abfd, second, data + 2);
2279 bfd_put_16 (abfd, first, data);
2282 bfd_reloc_status_type
2283 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2284 arelent *reloc_entry, asection *input_section,
2285 bfd_boolean relocatable, void *data, bfd_vma gp)
2289 bfd_reloc_status_type status;
2291 if (bfd_is_com_section (symbol->section))
2294 relocation = symbol->value;
2296 relocation += symbol->section->output_section->vma;
2297 relocation += symbol->section->output_offset;
2299 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2300 return bfd_reloc_outofrange;
2302 /* Set val to the offset into the section or symbol. */
2303 val = reloc_entry->addend;
2305 _bfd_mips_elf_sign_extend (val, 16);
2307 /* Adjust val for the final section location and GP value. If we
2308 are producing relocatable output, we don't want to do this for
2309 an external symbol. */
2311 || (symbol->flags & BSF_SECTION_SYM) != 0)
2312 val += relocation - gp;
2314 if (reloc_entry->howto->partial_inplace)
2316 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2318 + reloc_entry->address);
2319 if (status != bfd_reloc_ok)
2323 reloc_entry->addend = val;
2326 reloc_entry->address += input_section->output_offset;
2328 return bfd_reloc_ok;
2331 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2332 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2333 that contains the relocation field and DATA points to the start of
2338 struct mips_hi16 *next;
2340 asection *input_section;
2344 /* FIXME: This should not be a static variable. */
2346 static struct mips_hi16 *mips_hi16_list;
2348 /* A howto special_function for REL *HI16 relocations. We can only
2349 calculate the correct value once we've seen the partnering
2350 *LO16 relocation, so just save the information for later.
2352 The ABI requires that the *LO16 immediately follow the *HI16.
2353 However, as a GNU extension, we permit an arbitrary number of
2354 *HI16s to be associated with a single *LO16. This significantly
2355 simplies the relocation handling in gcc. */
2357 bfd_reloc_status_type
2358 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2359 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2360 asection *input_section, bfd *output_bfd,
2361 char **error_message ATTRIBUTE_UNUSED)
2363 struct mips_hi16 *n;
2365 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2366 return bfd_reloc_outofrange;
2368 n = bfd_malloc (sizeof *n);
2370 return bfd_reloc_outofrange;
2372 n->next = mips_hi16_list;
2374 n->input_section = input_section;
2375 n->rel = *reloc_entry;
2378 if (output_bfd != NULL)
2379 reloc_entry->address += input_section->output_offset;
2381 return bfd_reloc_ok;
2384 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2385 like any other 16-bit relocation when applied to global symbols, but is
2386 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2388 bfd_reloc_status_type
2389 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2390 void *data, asection *input_section,
2391 bfd *output_bfd, char **error_message)
2393 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2394 || bfd_is_und_section (bfd_get_section (symbol))
2395 || bfd_is_com_section (bfd_get_section (symbol)))
2396 /* The relocation is against a global symbol. */
2397 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2398 input_section, output_bfd,
2401 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2402 input_section, output_bfd, error_message);
2405 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2406 is a straightforward 16 bit inplace relocation, but we must deal with
2407 any partnering high-part relocations as well. */
2409 bfd_reloc_status_type
2410 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2411 void *data, asection *input_section,
2412 bfd *output_bfd, char **error_message)
2415 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2417 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2418 return bfd_reloc_outofrange;
2420 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2422 vallo = bfd_get_32 (abfd, location);
2423 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2426 while (mips_hi16_list != NULL)
2428 bfd_reloc_status_type ret;
2429 struct mips_hi16 *hi;
2431 hi = mips_hi16_list;
2433 /* R_MIPS*_GOT16 relocations are something of a special case. We
2434 want to install the addend in the same way as for a R_MIPS*_HI16
2435 relocation (with a rightshift of 16). However, since GOT16
2436 relocations can also be used with global symbols, their howto
2437 has a rightshift of 0. */
2438 if (hi->rel.howto->type == R_MIPS_GOT16)
2439 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2440 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2441 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2442 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2443 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2445 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2446 carry or borrow will induce a change of +1 or -1 in the high part. */
2447 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2449 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2450 hi->input_section, output_bfd,
2452 if (ret != bfd_reloc_ok)
2455 mips_hi16_list = hi->next;
2459 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2460 input_section, output_bfd,
2464 /* A generic howto special_function. This calculates and installs the
2465 relocation itself, thus avoiding the oft-discussed problems in
2466 bfd_perform_relocation and bfd_install_relocation. */
2468 bfd_reloc_status_type
2469 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2470 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2471 asection *input_section, bfd *output_bfd,
2472 char **error_message ATTRIBUTE_UNUSED)
2475 bfd_reloc_status_type status;
2476 bfd_boolean relocatable;
2478 relocatable = (output_bfd != NULL);
2480 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2481 return bfd_reloc_outofrange;
2483 /* Build up the field adjustment in VAL. */
2485 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2487 /* Either we're calculating the final field value or we have a
2488 relocation against a section symbol. Add in the section's
2489 offset or address. */
2490 val += symbol->section->output_section->vma;
2491 val += symbol->section->output_offset;
2496 /* We're calculating the final field value. Add in the symbol's value
2497 and, if pc-relative, subtract the address of the field itself. */
2498 val += symbol->value;
2499 if (reloc_entry->howto->pc_relative)
2501 val -= input_section->output_section->vma;
2502 val -= input_section->output_offset;
2503 val -= reloc_entry->address;
2507 /* VAL is now the final adjustment. If we're keeping this relocation
2508 in the output file, and if the relocation uses a separate addend,
2509 we just need to add VAL to that addend. Otherwise we need to add
2510 VAL to the relocation field itself. */
2511 if (relocatable && !reloc_entry->howto->partial_inplace)
2512 reloc_entry->addend += val;
2515 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2517 /* Add in the separate addend, if any. */
2518 val += reloc_entry->addend;
2520 /* Add VAL to the relocation field. */
2521 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2523 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2525 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2528 if (status != bfd_reloc_ok)
2533 reloc_entry->address += input_section->output_offset;
2535 return bfd_reloc_ok;
2538 /* Swap an entry in a .gptab section. Note that these routines rely
2539 on the equivalence of the two elements of the union. */
2542 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2545 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2546 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2550 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2551 Elf32_External_gptab *ex)
2553 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2554 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2558 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2559 Elf32_External_compact_rel *ex)
2561 H_PUT_32 (abfd, in->id1, ex->id1);
2562 H_PUT_32 (abfd, in->num, ex->num);
2563 H_PUT_32 (abfd, in->id2, ex->id2);
2564 H_PUT_32 (abfd, in->offset, ex->offset);
2565 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2566 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2570 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2571 Elf32_External_crinfo *ex)
2575 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2576 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2577 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2578 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2579 H_PUT_32 (abfd, l, ex->info);
2580 H_PUT_32 (abfd, in->konst, ex->konst);
2581 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2584 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2585 routines swap this structure in and out. They are used outside of
2586 BFD, so they are globally visible. */
2589 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2592 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2593 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2594 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2595 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2596 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2597 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2601 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2602 Elf32_External_RegInfo *ex)
2604 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2605 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2606 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2607 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2608 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2609 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2612 /* In the 64 bit ABI, the .MIPS.options section holds register
2613 information in an Elf64_Reginfo structure. These routines swap
2614 them in and out. They are globally visible because they are used
2615 outside of BFD. These routines are here so that gas can call them
2616 without worrying about whether the 64 bit ABI has been included. */
2619 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2620 Elf64_Internal_RegInfo *in)
2622 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2623 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2624 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2625 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2626 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2627 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2628 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2632 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2633 Elf64_External_RegInfo *ex)
2635 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2636 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2637 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2638 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2639 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2640 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2641 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2644 /* Swap in an options header. */
2647 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2648 Elf_Internal_Options *in)
2650 in->kind = H_GET_8 (abfd, ex->kind);
2651 in->size = H_GET_8 (abfd, ex->size);
2652 in->section = H_GET_16 (abfd, ex->section);
2653 in->info = H_GET_32 (abfd, ex->info);
2656 /* Swap out an options header. */
2659 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2660 Elf_External_Options *ex)
2662 H_PUT_8 (abfd, in->kind, ex->kind);
2663 H_PUT_8 (abfd, in->size, ex->size);
2664 H_PUT_16 (abfd, in->section, ex->section);
2665 H_PUT_32 (abfd, in->info, ex->info);
2668 /* This function is called via qsort() to sort the dynamic relocation
2669 entries by increasing r_symndx value. */
2672 sort_dynamic_relocs (const void *arg1, const void *arg2)
2674 Elf_Internal_Rela int_reloc1;
2675 Elf_Internal_Rela int_reloc2;
2678 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2679 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2681 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2685 if (int_reloc1.r_offset < int_reloc2.r_offset)
2687 if (int_reloc1.r_offset > int_reloc2.r_offset)
2692 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2695 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2696 const void *arg2 ATTRIBUTE_UNUSED)
2699 Elf_Internal_Rela int_reloc1[3];
2700 Elf_Internal_Rela int_reloc2[3];
2702 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2703 (reldyn_sorting_bfd, arg1, int_reloc1);
2704 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2705 (reldyn_sorting_bfd, arg2, int_reloc2);
2707 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2709 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2712 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2714 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2723 /* This routine is used to write out ECOFF debugging external symbol
2724 information. It is called via mips_elf_link_hash_traverse. The
2725 ECOFF external symbol information must match the ELF external
2726 symbol information. Unfortunately, at this point we don't know
2727 whether a symbol is required by reloc information, so the two
2728 tables may wind up being different. We must sort out the external
2729 symbol information before we can set the final size of the .mdebug
2730 section, and we must set the size of the .mdebug section before we
2731 can relocate any sections, and we can't know which symbols are
2732 required by relocation until we relocate the sections.
2733 Fortunately, it is relatively unlikely that any symbol will be
2734 stripped but required by a reloc. In particular, it can not happen
2735 when generating a final executable. */
2738 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2740 struct extsym_info *einfo = data;
2742 asection *sec, *output_section;
2744 if (h->root.indx == -2)
2746 else if ((h->root.def_dynamic
2747 || h->root.ref_dynamic
2748 || h->root.type == bfd_link_hash_new)
2749 && !h->root.def_regular
2750 && !h->root.ref_regular)
2752 else if (einfo->info->strip == strip_all
2753 || (einfo->info->strip == strip_some
2754 && bfd_hash_lookup (einfo->info->keep_hash,
2755 h->root.root.root.string,
2756 FALSE, FALSE) == NULL))
2764 if (h->esym.ifd == -2)
2767 h->esym.cobol_main = 0;
2768 h->esym.weakext = 0;
2769 h->esym.reserved = 0;
2770 h->esym.ifd = ifdNil;
2771 h->esym.asym.value = 0;
2772 h->esym.asym.st = stGlobal;
2774 if (h->root.root.type == bfd_link_hash_undefined
2775 || h->root.root.type == bfd_link_hash_undefweak)
2779 /* Use undefined class. Also, set class and type for some
2781 name = h->root.root.root.string;
2782 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2783 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2785 h->esym.asym.sc = scData;
2786 h->esym.asym.st = stLabel;
2787 h->esym.asym.value = 0;
2789 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2791 h->esym.asym.sc = scAbs;
2792 h->esym.asym.st = stLabel;
2793 h->esym.asym.value =
2794 mips_elf_hash_table (einfo->info)->procedure_count;
2796 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2798 h->esym.asym.sc = scAbs;
2799 h->esym.asym.st = stLabel;
2800 h->esym.asym.value = elf_gp (einfo->abfd);
2803 h->esym.asym.sc = scUndefined;
2805 else if (h->root.root.type != bfd_link_hash_defined
2806 && h->root.root.type != bfd_link_hash_defweak)
2807 h->esym.asym.sc = scAbs;
2812 sec = h->root.root.u.def.section;
2813 output_section = sec->output_section;
2815 /* When making a shared library and symbol h is the one from
2816 the another shared library, OUTPUT_SECTION may be null. */
2817 if (output_section == NULL)
2818 h->esym.asym.sc = scUndefined;
2821 name = bfd_section_name (output_section->owner, output_section);
2823 if (strcmp (name, ".text") == 0)
2824 h->esym.asym.sc = scText;
2825 else if (strcmp (name, ".data") == 0)
2826 h->esym.asym.sc = scData;
2827 else if (strcmp (name, ".sdata") == 0)
2828 h->esym.asym.sc = scSData;
2829 else if (strcmp (name, ".rodata") == 0
2830 || strcmp (name, ".rdata") == 0)
2831 h->esym.asym.sc = scRData;
2832 else if (strcmp (name, ".bss") == 0)
2833 h->esym.asym.sc = scBss;
2834 else if (strcmp (name, ".sbss") == 0)
2835 h->esym.asym.sc = scSBss;
2836 else if (strcmp (name, ".init") == 0)
2837 h->esym.asym.sc = scInit;
2838 else if (strcmp (name, ".fini") == 0)
2839 h->esym.asym.sc = scFini;
2841 h->esym.asym.sc = scAbs;
2845 h->esym.asym.reserved = 0;
2846 h->esym.asym.index = indexNil;
2849 if (h->root.root.type == bfd_link_hash_common)
2850 h->esym.asym.value = h->root.root.u.c.size;
2851 else if (h->root.root.type == bfd_link_hash_defined
2852 || h->root.root.type == bfd_link_hash_defweak)
2854 if (h->esym.asym.sc == scCommon)
2855 h->esym.asym.sc = scBss;
2856 else if (h->esym.asym.sc == scSCommon)
2857 h->esym.asym.sc = scSBss;
2859 sec = h->root.root.u.def.section;
2860 output_section = sec->output_section;
2861 if (output_section != NULL)
2862 h->esym.asym.value = (h->root.root.u.def.value
2863 + sec->output_offset
2864 + output_section->vma);
2866 h->esym.asym.value = 0;
2870 struct mips_elf_link_hash_entry *hd = h;
2872 while (hd->root.root.type == bfd_link_hash_indirect)
2873 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2875 if (hd->needs_lazy_stub)
2877 BFD_ASSERT (hd->root.plt.plist != NULL);
2878 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
2879 /* Set type and value for a symbol with a function stub. */
2880 h->esym.asym.st = stProc;
2881 sec = hd->root.root.u.def.section;
2883 h->esym.asym.value = 0;
2886 output_section = sec->output_section;
2887 if (output_section != NULL)
2888 h->esym.asym.value = (hd->root.plt.plist->stub_offset
2889 + sec->output_offset
2890 + output_section->vma);
2892 h->esym.asym.value = 0;
2897 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2898 h->root.root.root.string,
2901 einfo->failed = TRUE;
2908 /* A comparison routine used to sort .gptab entries. */
2911 gptab_compare (const void *p1, const void *p2)
2913 const Elf32_gptab *a1 = p1;
2914 const Elf32_gptab *a2 = p2;
2916 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2919 /* Functions to manage the got entry hash table. */
2921 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2924 static INLINE hashval_t
2925 mips_elf_hash_bfd_vma (bfd_vma addr)
2928 return addr + (addr >> 32);
2935 mips_elf_got_entry_hash (const void *entry_)
2937 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2939 return (entry->symndx
2940 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2941 + (entry->tls_type == GOT_TLS_LDM ? 0
2942 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2943 : entry->symndx >= 0 ? (entry->abfd->id
2944 + mips_elf_hash_bfd_vma (entry->d.addend))
2945 : entry->d.h->root.root.root.hash));
2949 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2951 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2952 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2954 return (e1->symndx == e2->symndx
2955 && e1->tls_type == e2->tls_type
2956 && (e1->tls_type == GOT_TLS_LDM ? TRUE
2957 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2958 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2959 && e1->d.addend == e2->d.addend)
2960 : e2->abfd && e1->d.h == e2->d.h));
2964 mips_got_page_ref_hash (const void *ref_)
2966 const struct mips_got_page_ref *ref;
2968 ref = (const struct mips_got_page_ref *) ref_;
2969 return ((ref->symndx >= 0
2970 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2971 : ref->u.h->root.root.root.hash)
2972 + mips_elf_hash_bfd_vma (ref->addend));
2976 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2978 const struct mips_got_page_ref *ref1, *ref2;
2980 ref1 = (const struct mips_got_page_ref *) ref1_;
2981 ref2 = (const struct mips_got_page_ref *) ref2_;
2982 return (ref1->symndx == ref2->symndx
2983 && (ref1->symndx < 0
2984 ? ref1->u.h == ref2->u.h
2985 : ref1->u.abfd == ref2->u.abfd)
2986 && ref1->addend == ref2->addend);
2990 mips_got_page_entry_hash (const void *entry_)
2992 const struct mips_got_page_entry *entry;
2994 entry = (const struct mips_got_page_entry *) entry_;
2995 return entry->sec->id;
2999 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3001 const struct mips_got_page_entry *entry1, *entry2;
3003 entry1 = (const struct mips_got_page_entry *) entry1_;
3004 entry2 = (const struct mips_got_page_entry *) entry2_;
3005 return entry1->sec == entry2->sec;
3008 /* Create and return a new mips_got_info structure. */
3010 static struct mips_got_info *
3011 mips_elf_create_got_info (bfd *abfd)
3013 struct mips_got_info *g;
3015 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3019 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3020 mips_elf_got_entry_eq, NULL);
3021 if (g->got_entries == NULL)
3024 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3025 mips_got_page_ref_eq, NULL);
3026 if (g->got_page_refs == NULL)
3032 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3033 CREATE_P and if ABFD doesn't already have a GOT. */
3035 static struct mips_got_info *
3036 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3038 struct mips_elf_obj_tdata *tdata;
3040 if (!is_mips_elf (abfd))
3043 tdata = mips_elf_tdata (abfd);
3044 if (!tdata->got && create_p)
3045 tdata->got = mips_elf_create_got_info (abfd);
3049 /* Record that ABFD should use output GOT G. */
3052 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3054 struct mips_elf_obj_tdata *tdata;
3056 BFD_ASSERT (is_mips_elf (abfd));
3057 tdata = mips_elf_tdata (abfd);
3060 /* The GOT structure itself and the hash table entries are
3061 allocated to a bfd, but the hash tables aren't. */
3062 htab_delete (tdata->got->got_entries);
3063 htab_delete (tdata->got->got_page_refs);
3064 if (tdata->got->got_page_entries)
3065 htab_delete (tdata->got->got_page_entries);
3070 /* Return the dynamic relocation section. If it doesn't exist, try to
3071 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3072 if creation fails. */
3075 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3081 dname = MIPS_ELF_REL_DYN_NAME (info);
3082 dynobj = elf_hash_table (info)->dynobj;
3083 sreloc = bfd_get_linker_section (dynobj, dname);
3084 if (sreloc == NULL && create_p)
3086 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3091 | SEC_LINKER_CREATED
3094 || ! bfd_set_section_alignment (dynobj, sreloc,
3095 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3101 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3104 mips_elf_reloc_tls_type (unsigned int r_type)
3106 if (tls_gd_reloc_p (r_type))
3109 if (tls_ldm_reloc_p (r_type))
3112 if (tls_gottprel_reloc_p (r_type))
3115 return GOT_TLS_NONE;
3118 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3121 mips_tls_got_entries (unsigned int type)
3138 /* Count the number of relocations needed for a TLS GOT entry, with
3139 access types from TLS_TYPE, and symbol H (or a local symbol if H
3143 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3144 struct elf_link_hash_entry *h)
3147 bfd_boolean need_relocs = FALSE;
3148 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3150 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3151 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3154 if ((info->shared || indx != 0)
3156 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3157 || h->root.type != bfd_link_hash_undefweak))
3166 return indx != 0 ? 2 : 1;
3172 return info->shared ? 1 : 0;
3179 /* Add the number of GOT entries and TLS relocations required by ENTRY
3183 mips_elf_count_got_entry (struct bfd_link_info *info,
3184 struct mips_got_info *g,
3185 struct mips_got_entry *entry)
3187 if (entry->tls_type)
3189 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3190 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3192 ? &entry->d.h->root : NULL);
3194 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3195 g->local_gotno += 1;
3197 g->global_gotno += 1;
3200 /* Output a simple dynamic relocation into SRELOC. */
3203 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3205 unsigned long reloc_index,
3210 Elf_Internal_Rela rel[3];
3212 memset (rel, 0, sizeof (rel));
3214 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3215 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3217 if (ABI_64_P (output_bfd))
3219 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3220 (output_bfd, &rel[0],
3222 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3225 bfd_elf32_swap_reloc_out
3226 (output_bfd, &rel[0],
3228 + reloc_index * sizeof (Elf32_External_Rel)));
3231 /* Initialize a set of TLS GOT entries for one symbol. */
3234 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3235 struct mips_got_entry *entry,
3236 struct mips_elf_link_hash_entry *h,
3239 struct mips_elf_link_hash_table *htab;
3241 asection *sreloc, *sgot;
3242 bfd_vma got_offset, got_offset2;
3243 bfd_boolean need_relocs = FALSE;
3245 htab = mips_elf_hash_table (info);
3254 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3256 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3257 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3258 indx = h->root.dynindx;
3261 if (entry->tls_initialized)
3264 if ((info->shared || indx != 0)
3266 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3267 || h->root.type != bfd_link_hash_undefweak))
3270 /* MINUS_ONE means the symbol is not defined in this object. It may not
3271 be defined at all; assume that the value doesn't matter in that
3272 case. Otherwise complain if we would use the value. */
3273 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3274 || h->root.root.type == bfd_link_hash_undefweak);
3276 /* Emit necessary relocations. */
3277 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3278 got_offset = entry->gotidx;
3280 switch (entry->tls_type)
3283 /* General Dynamic. */
3284 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3288 mips_elf_output_dynamic_relocation
3289 (abfd, sreloc, sreloc->reloc_count++, indx,
3290 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3291 sgot->output_offset + sgot->output_section->vma + got_offset);
3294 mips_elf_output_dynamic_relocation
3295 (abfd, sreloc, sreloc->reloc_count++, indx,
3296 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3297 sgot->output_offset + sgot->output_section->vma + got_offset2);
3299 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3300 sgot->contents + got_offset2);
3304 MIPS_ELF_PUT_WORD (abfd, 1,
3305 sgot->contents + got_offset);
3306 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3307 sgot->contents + got_offset2);
3312 /* Initial Exec model. */
3316 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3317 sgot->contents + got_offset);
3319 MIPS_ELF_PUT_WORD (abfd, 0,
3320 sgot->contents + got_offset);
3322 mips_elf_output_dynamic_relocation
3323 (abfd, sreloc, sreloc->reloc_count++, indx,
3324 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3325 sgot->output_offset + sgot->output_section->vma + got_offset);
3328 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3329 sgot->contents + got_offset);
3333 /* The initial offset is zero, and the LD offsets will include the
3334 bias by DTP_OFFSET. */
3335 MIPS_ELF_PUT_WORD (abfd, 0,
3336 sgot->contents + got_offset
3337 + MIPS_ELF_GOT_SIZE (abfd));
3340 MIPS_ELF_PUT_WORD (abfd, 1,
3341 sgot->contents + got_offset);
3343 mips_elf_output_dynamic_relocation
3344 (abfd, sreloc, sreloc->reloc_count++, indx,
3345 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3346 sgot->output_offset + sgot->output_section->vma + got_offset);
3353 entry->tls_initialized = TRUE;
3356 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3357 for global symbol H. .got.plt comes before the GOT, so the offset
3358 will be negative. */
3361 mips_elf_gotplt_index (struct bfd_link_info *info,
3362 struct elf_link_hash_entry *h)
3364 bfd_vma got_address, got_value;
3365 struct mips_elf_link_hash_table *htab;
3367 htab = mips_elf_hash_table (info);
3368 BFD_ASSERT (htab != NULL);
3370 BFD_ASSERT (h->plt.plist != NULL);
3371 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3373 /* Calculate the address of the associated .got.plt entry. */
3374 got_address = (htab->sgotplt->output_section->vma
3375 + htab->sgotplt->output_offset
3376 + (h->plt.plist->gotplt_index
3377 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3379 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3380 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3381 + htab->root.hgot->root.u.def.section->output_offset
3382 + htab->root.hgot->root.u.def.value);
3384 return got_address - got_value;
3387 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3388 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3389 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3390 offset can be found. */
3393 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3394 bfd_vma value, unsigned long r_symndx,
3395 struct mips_elf_link_hash_entry *h, int r_type)
3397 struct mips_elf_link_hash_table *htab;
3398 struct mips_got_entry *entry;
3400 htab = mips_elf_hash_table (info);
3401 BFD_ASSERT (htab != NULL);
3403 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3404 r_symndx, h, r_type);
3408 if (entry->tls_type)
3409 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3410 return entry->gotidx;
3413 /* Return the GOT index of global symbol H in the primary GOT. */
3416 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3417 struct elf_link_hash_entry *h)
3419 struct mips_elf_link_hash_table *htab;
3420 long global_got_dynindx;
3421 struct mips_got_info *g;
3424 htab = mips_elf_hash_table (info);
3425 BFD_ASSERT (htab != NULL);
3427 global_got_dynindx = 0;
3428 if (htab->global_gotsym != NULL)
3429 global_got_dynindx = htab->global_gotsym->dynindx;
3431 /* Once we determine the global GOT entry with the lowest dynamic
3432 symbol table index, we must put all dynamic symbols with greater
3433 indices into the primary GOT. That makes it easy to calculate the
3435 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3436 g = mips_elf_bfd_got (obfd, FALSE);
3437 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3438 * MIPS_ELF_GOT_SIZE (obfd));
3439 BFD_ASSERT (got_index < htab->sgot->size);
3444 /* Return the GOT index for the global symbol indicated by H, which is
3445 referenced by a relocation of type R_TYPE in IBFD. */
3448 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3449 struct elf_link_hash_entry *h, int r_type)
3451 struct mips_elf_link_hash_table *htab;
3452 struct mips_got_info *g;
3453 struct mips_got_entry lookup, *entry;
3456 htab = mips_elf_hash_table (info);
3457 BFD_ASSERT (htab != NULL);
3459 g = mips_elf_bfd_got (ibfd, FALSE);
3462 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3463 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3464 return mips_elf_primary_global_got_index (obfd, info, h);
3468 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3469 entry = htab_find (g->got_entries, &lookup);
3472 gotidx = entry->gotidx;
3473 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3475 if (lookup.tls_type)
3477 bfd_vma value = MINUS_ONE;
3479 if ((h->root.type == bfd_link_hash_defined
3480 || h->root.type == bfd_link_hash_defweak)
3481 && h->root.u.def.section->output_section)
3482 value = (h->root.u.def.value
3483 + h->root.u.def.section->output_offset
3484 + h->root.u.def.section->output_section->vma);
3486 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3491 /* Find a GOT page entry that points to within 32KB of VALUE. These
3492 entries are supposed to be placed at small offsets in the GOT, i.e.,
3493 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3494 entry could be created. If OFFSETP is nonnull, use it to return the
3495 offset of the GOT entry from VALUE. */
3498 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3499 bfd_vma value, bfd_vma *offsetp)
3501 bfd_vma page, got_index;
3502 struct mips_got_entry *entry;
3504 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3505 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3506 NULL, R_MIPS_GOT_PAGE);
3511 got_index = entry->gotidx;
3514 *offsetp = value - entry->d.address;
3519 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3520 EXTERNAL is true if the relocation was originally against a global
3521 symbol that binds locally. */
3524 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3525 bfd_vma value, bfd_boolean external)
3527 struct mips_got_entry *entry;
3529 /* GOT16 relocations against local symbols are followed by a LO16
3530 relocation; those against global symbols are not. Thus if the
3531 symbol was originally local, the GOT16 relocation should load the
3532 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3534 value = mips_elf_high (value) << 16;
3536 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3537 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3538 same in all cases. */
3539 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3540 NULL, R_MIPS_GOT16);
3542 return entry->gotidx;
3547 /* Returns the offset for the entry at the INDEXth position
3551 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3552 bfd *input_bfd, bfd_vma got_index)
3554 struct mips_elf_link_hash_table *htab;
3558 htab = mips_elf_hash_table (info);
3559 BFD_ASSERT (htab != NULL);
3562 gp = _bfd_get_gp_value (output_bfd)
3563 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3565 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3568 /* Create and return a local GOT entry for VALUE, which was calculated
3569 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3570 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3573 static struct mips_got_entry *
3574 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3575 bfd *ibfd, bfd_vma value,
3576 unsigned long r_symndx,
3577 struct mips_elf_link_hash_entry *h,
3580 struct mips_got_entry lookup, *entry;
3582 struct mips_got_info *g;
3583 struct mips_elf_link_hash_table *htab;
3586 htab = mips_elf_hash_table (info);
3587 BFD_ASSERT (htab != NULL);
3589 g = mips_elf_bfd_got (ibfd, FALSE);
3592 g = mips_elf_bfd_got (abfd, FALSE);
3593 BFD_ASSERT (g != NULL);
3596 /* This function shouldn't be called for symbols that live in the global
3598 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3600 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3601 if (lookup.tls_type)
3604 if (tls_ldm_reloc_p (r_type))
3607 lookup.d.addend = 0;
3611 lookup.symndx = r_symndx;
3612 lookup.d.addend = 0;
3620 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3623 gotidx = entry->gotidx;
3624 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3631 lookup.d.address = value;
3632 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3636 entry = (struct mips_got_entry *) *loc;
3640 if (g->assigned_low_gotno > g->assigned_high_gotno)
3642 /* We didn't allocate enough space in the GOT. */
3643 (*_bfd_error_handler)
3644 (_("not enough GOT space for local GOT entries"));
3645 bfd_set_error (bfd_error_bad_value);
3649 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3653 if (got16_reloc_p (r_type)
3654 || call16_reloc_p (r_type)
3655 || got_page_reloc_p (r_type)
3656 || got_disp_reloc_p (r_type))
3657 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3659 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3664 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
3666 /* These GOT entries need a dynamic relocation on VxWorks. */
3667 if (htab->is_vxworks)
3669 Elf_Internal_Rela outrel;
3672 bfd_vma got_address;
3674 s = mips_elf_rel_dyn_section (info, FALSE);
3675 got_address = (htab->sgot->output_section->vma
3676 + htab->sgot->output_offset
3679 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3680 outrel.r_offset = got_address;
3681 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3682 outrel.r_addend = value;
3683 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3689 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3690 The number might be exact or a worst-case estimate, depending on how
3691 much information is available to elf_backend_omit_section_dynsym at
3692 the current linking stage. */
3694 static bfd_size_type
3695 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3697 bfd_size_type count;
3700 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3703 const struct elf_backend_data *bed;
3705 bed = get_elf_backend_data (output_bfd);
3706 for (p = output_bfd->sections; p ; p = p->next)
3707 if ((p->flags & SEC_EXCLUDE) == 0
3708 && (p->flags & SEC_ALLOC) != 0
3709 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3715 /* Sort the dynamic symbol table so that symbols that need GOT entries
3716 appear towards the end. */
3719 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3721 struct mips_elf_link_hash_table *htab;
3722 struct mips_elf_hash_sort_data hsd;
3723 struct mips_got_info *g;
3725 if (elf_hash_table (info)->dynsymcount == 0)
3728 htab = mips_elf_hash_table (info);
3729 BFD_ASSERT (htab != NULL);
3736 hsd.max_unref_got_dynindx
3737 = hsd.min_got_dynindx
3738 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3739 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3740 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3741 elf_hash_table (info)),
3742 mips_elf_sort_hash_table_f,
3745 /* There should have been enough room in the symbol table to
3746 accommodate both the GOT and non-GOT symbols. */
3747 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3748 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3749 == elf_hash_table (info)->dynsymcount);
3750 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3751 == g->global_gotno);
3753 /* Now we know which dynamic symbol has the lowest dynamic symbol
3754 table index in the GOT. */
3755 htab->global_gotsym = hsd.low;
3760 /* If H needs a GOT entry, assign it the highest available dynamic
3761 index. Otherwise, assign it the lowest available dynamic
3765 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3767 struct mips_elf_hash_sort_data *hsd = data;
3769 /* Symbols without dynamic symbol table entries aren't interesting
3771 if (h->root.dynindx == -1)
3774 switch (h->global_got_area)
3777 h->root.dynindx = hsd->max_non_got_dynindx++;
3781 h->root.dynindx = --hsd->min_got_dynindx;
3782 hsd->low = (struct elf_link_hash_entry *) h;
3785 case GGA_RELOC_ONLY:
3786 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3787 hsd->low = (struct elf_link_hash_entry *) h;
3788 h->root.dynindx = hsd->max_unref_got_dynindx++;
3795 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3796 (which is owned by the caller and shouldn't be added to the
3797 hash table directly). */
3800 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3801 struct mips_got_entry *lookup)
3803 struct mips_elf_link_hash_table *htab;
3804 struct mips_got_entry *entry;
3805 struct mips_got_info *g;
3806 void **loc, **bfd_loc;
3808 /* Make sure there's a slot for this entry in the master GOT. */
3809 htab = mips_elf_hash_table (info);
3811 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3815 /* Populate the entry if it isn't already. */
3816 entry = (struct mips_got_entry *) *loc;
3819 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3823 lookup->tls_initialized = FALSE;
3824 lookup->gotidx = -1;
3829 /* Reuse the same GOT entry for the BFD's GOT. */
3830 g = mips_elf_bfd_got (abfd, TRUE);
3834 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3843 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3844 entry for it. FOR_CALL is true if the caller is only interested in
3845 using the GOT entry for calls. */
3848 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3849 bfd *abfd, struct bfd_link_info *info,
3850 bfd_boolean for_call, int r_type)
3852 struct mips_elf_link_hash_table *htab;
3853 struct mips_elf_link_hash_entry *hmips;
3854 struct mips_got_entry entry;
3855 unsigned char tls_type;
3857 htab = mips_elf_hash_table (info);
3858 BFD_ASSERT (htab != NULL);
3860 hmips = (struct mips_elf_link_hash_entry *) h;
3862 hmips->got_only_for_calls = FALSE;
3864 /* A global symbol in the GOT must also be in the dynamic symbol
3866 if (h->dynindx == -1)
3868 switch (ELF_ST_VISIBILITY (h->other))
3872 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3875 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3879 tls_type = mips_elf_reloc_tls_type (r_type);
3880 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
3881 hmips->global_got_area = GGA_NORMAL;
3885 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3886 entry.tls_type = tls_type;
3887 return mips_elf_record_got_entry (info, abfd, &entry);
3890 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3891 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3894 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3895 struct bfd_link_info *info, int r_type)
3897 struct mips_elf_link_hash_table *htab;
3898 struct mips_got_info *g;
3899 struct mips_got_entry entry;
3901 htab = mips_elf_hash_table (info);
3902 BFD_ASSERT (htab != NULL);
3905 BFD_ASSERT (g != NULL);
3908 entry.symndx = symndx;
3909 entry.d.addend = addend;
3910 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3911 return mips_elf_record_got_entry (info, abfd, &entry);
3914 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3915 H is the symbol's hash table entry, or null if SYMNDX is local
3919 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3920 long symndx, struct elf_link_hash_entry *h,
3921 bfd_signed_vma addend)
3923 struct mips_elf_link_hash_table *htab;
3924 struct mips_got_info *g1, *g2;
3925 struct mips_got_page_ref lookup, *entry;
3926 void **loc, **bfd_loc;
3928 htab = mips_elf_hash_table (info);
3929 BFD_ASSERT (htab != NULL);
3931 g1 = htab->got_info;
3932 BFD_ASSERT (g1 != NULL);
3937 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3941 lookup.symndx = symndx;
3942 lookup.u.abfd = abfd;
3944 lookup.addend = addend;
3945 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
3949 entry = (struct mips_got_page_ref *) *loc;
3952 entry = bfd_alloc (abfd, sizeof (*entry));
3960 /* Add the same entry to the BFD's GOT. */
3961 g2 = mips_elf_bfd_got (abfd, TRUE);
3965 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
3975 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3978 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3982 struct mips_elf_link_hash_table *htab;
3984 htab = mips_elf_hash_table (info);
3985 BFD_ASSERT (htab != NULL);
3987 s = mips_elf_rel_dyn_section (info, FALSE);
3988 BFD_ASSERT (s != NULL);
3990 if (htab->is_vxworks)
3991 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3996 /* Make room for a null element. */
3997 s->size += MIPS_ELF_REL_SIZE (abfd);
4000 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4004 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4005 mips_elf_traverse_got_arg structure. Count the number of GOT
4006 entries and TLS relocs. Set DATA->value to true if we need
4007 to resolve indirect or warning symbols and then recreate the GOT. */
4010 mips_elf_check_recreate_got (void **entryp, void *data)
4012 struct mips_got_entry *entry;
4013 struct mips_elf_traverse_got_arg *arg;
4015 entry = (struct mips_got_entry *) *entryp;
4016 arg = (struct mips_elf_traverse_got_arg *) data;
4017 if (entry->abfd != NULL && entry->symndx == -1)
4019 struct mips_elf_link_hash_entry *h;
4022 if (h->root.root.type == bfd_link_hash_indirect
4023 || h->root.root.type == bfd_link_hash_warning)
4029 mips_elf_count_got_entry (arg->info, arg->g, entry);
4033 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4034 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4035 converting entries for indirect and warning symbols into entries
4036 for the target symbol. Set DATA->g to null on error. */
4039 mips_elf_recreate_got (void **entryp, void *data)
4041 struct mips_got_entry new_entry, *entry;
4042 struct mips_elf_traverse_got_arg *arg;
4045 entry = (struct mips_got_entry *) *entryp;
4046 arg = (struct mips_elf_traverse_got_arg *) data;
4047 if (entry->abfd != NULL
4048 && entry->symndx == -1
4049 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4050 || entry->d.h->root.root.type == bfd_link_hash_warning))
4052 struct mips_elf_link_hash_entry *h;
4059 BFD_ASSERT (h->global_got_area == GGA_NONE);
4060 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4062 while (h->root.root.type == bfd_link_hash_indirect
4063 || h->root.root.type == bfd_link_hash_warning);
4066 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4074 if (entry == &new_entry)
4076 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4085 mips_elf_count_got_entry (arg->info, arg->g, entry);
4090 /* Return the maximum number of GOT page entries required for RANGE. */
4093 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4095 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4098 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4101 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4102 asection *sec, bfd_signed_vma addend)
4104 struct mips_got_info *g = arg->g;
4105 struct mips_got_page_entry lookup, *entry;
4106 struct mips_got_page_range **range_ptr, *range;
4107 bfd_vma old_pages, new_pages;
4110 /* Find the mips_got_page_entry hash table entry for this section. */
4112 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4116 /* Create a mips_got_page_entry if this is the first time we've
4117 seen the section. */
4118 entry = (struct mips_got_page_entry *) *loc;
4121 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4129 /* Skip over ranges whose maximum extent cannot share a page entry
4131 range_ptr = &entry->ranges;
4132 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4133 range_ptr = &(*range_ptr)->next;
4135 /* If we scanned to the end of the list, or found a range whose
4136 minimum extent cannot share a page entry with ADDEND, create
4137 a new singleton range. */
4139 if (!range || addend < range->min_addend - 0xffff)
4141 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4145 range->next = *range_ptr;
4146 range->min_addend = addend;
4147 range->max_addend = addend;
4155 /* Remember how many pages the old range contributed. */
4156 old_pages = mips_elf_pages_for_range (range);
4158 /* Update the ranges. */
4159 if (addend < range->min_addend)
4160 range->min_addend = addend;
4161 else if (addend > range->max_addend)
4163 if (range->next && addend >= range->next->min_addend - 0xffff)
4165 old_pages += mips_elf_pages_for_range (range->next);
4166 range->max_addend = range->next->max_addend;
4167 range->next = range->next->next;
4170 range->max_addend = addend;
4173 /* Record any change in the total estimate. */
4174 new_pages = mips_elf_pages_for_range (range);
4175 if (old_pages != new_pages)
4177 entry->num_pages += new_pages - old_pages;
4178 g->page_gotno += new_pages - old_pages;
4184 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4185 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4186 whether the page reference described by *REFP needs a GOT page entry,
4187 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4190 mips_elf_resolve_got_page_ref (void **refp, void *data)
4192 struct mips_got_page_ref *ref;
4193 struct mips_elf_traverse_got_arg *arg;
4194 struct mips_elf_link_hash_table *htab;
4198 ref = (struct mips_got_page_ref *) *refp;
4199 arg = (struct mips_elf_traverse_got_arg *) data;
4200 htab = mips_elf_hash_table (arg->info);
4202 if (ref->symndx < 0)
4204 struct mips_elf_link_hash_entry *h;
4206 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4208 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4211 /* Ignore undefined symbols; we'll issue an error later if
4213 if (!((h->root.root.type == bfd_link_hash_defined
4214 || h->root.root.type == bfd_link_hash_defweak)
4215 && h->root.root.u.def.section))
4218 sec = h->root.root.u.def.section;
4219 addend = h->root.root.u.def.value + ref->addend;
4223 Elf_Internal_Sym *isym;
4225 /* Read in the symbol. */
4226 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4234 /* Get the associated input section. */
4235 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4242 /* If this is a mergable section, work out the section and offset
4243 of the merged data. For section symbols, the addend specifies
4244 of the offset _of_ the first byte in the data, otherwise it
4245 specifies the offset _from_ the first byte. */
4246 if (sec->flags & SEC_MERGE)
4250 secinfo = elf_section_data (sec)->sec_info;
4251 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4252 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4253 isym->st_value + ref->addend);
4255 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4256 isym->st_value) + ref->addend;
4259 addend = isym->st_value + ref->addend;
4261 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4269 /* If any entries in G->got_entries are for indirect or warning symbols,
4270 replace them with entries for the target symbol. Convert g->got_page_refs
4271 into got_page_entry structures and estimate the number of page entries
4272 that they require. */
4275 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4276 struct mips_got_info *g)
4278 struct mips_elf_traverse_got_arg tga;
4279 struct mips_got_info oldg;
4286 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4290 g->got_entries = htab_create (htab_size (oldg.got_entries),
4291 mips_elf_got_entry_hash,
4292 mips_elf_got_entry_eq, NULL);
4293 if (!g->got_entries)
4296 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4300 htab_delete (oldg.got_entries);
4303 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4304 mips_got_page_entry_eq, NULL);
4305 if (g->got_page_entries == NULL)
4310 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4315 /* Return true if a GOT entry for H should live in the local rather than
4319 mips_use_local_got_p (struct bfd_link_info *info,
4320 struct mips_elf_link_hash_entry *h)
4322 /* Symbols that aren't in the dynamic symbol table must live in the
4323 local GOT. This includes symbols that are completely undefined
4324 and which therefore don't bind locally. We'll report undefined
4325 symbols later if appropriate. */
4326 if (h->root.dynindx == -1)
4329 /* Symbols that bind locally can (and in the case of forced-local
4330 symbols, must) live in the local GOT. */
4331 if (h->got_only_for_calls
4332 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4333 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4336 /* If this is an executable that must provide a definition of the symbol,
4337 either though PLTs or copy relocations, then that address should go in
4338 the local rather than global GOT. */
4339 if (info->executable && h->has_static_relocs)
4345 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4346 link_info structure. Decide whether the hash entry needs an entry in
4347 the global part of the primary GOT, setting global_got_area accordingly.
4348 Count the number of global symbols that are in the primary GOT only
4349 because they have relocations against them (reloc_only_gotno). */
4352 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4354 struct bfd_link_info *info;
4355 struct mips_elf_link_hash_table *htab;
4356 struct mips_got_info *g;
4358 info = (struct bfd_link_info *) data;
4359 htab = mips_elf_hash_table (info);
4361 if (h->global_got_area != GGA_NONE)
4363 /* Make a final decision about whether the symbol belongs in the
4364 local or global GOT. */
4365 if (mips_use_local_got_p (info, h))
4366 /* The symbol belongs in the local GOT. We no longer need this
4367 entry if it was only used for relocations; those relocations
4368 will be against the null or section symbol instead of H. */
4369 h->global_got_area = GGA_NONE;
4370 else if (htab->is_vxworks
4371 && h->got_only_for_calls
4372 && h->root.plt.plist->mips_offset != MINUS_ONE)
4373 /* On VxWorks, calls can refer directly to the .got.plt entry;
4374 they don't need entries in the regular GOT. .got.plt entries
4375 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4376 h->global_got_area = GGA_NONE;
4377 else if (h->global_got_area == GGA_RELOC_ONLY)
4379 g->reloc_only_gotno++;
4386 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4387 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4390 mips_elf_add_got_entry (void **entryp, void *data)
4392 struct mips_got_entry *entry;
4393 struct mips_elf_traverse_got_arg *arg;
4396 entry = (struct mips_got_entry *) *entryp;
4397 arg = (struct mips_elf_traverse_got_arg *) data;
4398 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4407 mips_elf_count_got_entry (arg->info, arg->g, entry);
4412 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4413 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4416 mips_elf_add_got_page_entry (void **entryp, void *data)
4418 struct mips_got_page_entry *entry;
4419 struct mips_elf_traverse_got_arg *arg;
4422 entry = (struct mips_got_page_entry *) *entryp;
4423 arg = (struct mips_elf_traverse_got_arg *) data;
4424 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4433 arg->g->page_gotno += entry->num_pages;
4438 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4439 this would lead to overflow, 1 if they were merged successfully,
4440 and 0 if a merge failed due to lack of memory. (These values are chosen
4441 so that nonnegative return values can be returned by a htab_traverse
4445 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4446 struct mips_got_info *to,
4447 struct mips_elf_got_per_bfd_arg *arg)
4449 struct mips_elf_traverse_got_arg tga;
4450 unsigned int estimate;
4452 /* Work out how many page entries we would need for the combined GOT. */
4453 estimate = arg->max_pages;
4454 if (estimate >= from->page_gotno + to->page_gotno)
4455 estimate = from->page_gotno + to->page_gotno;
4457 /* And conservatively estimate how many local and TLS entries
4459 estimate += from->local_gotno + to->local_gotno;
4460 estimate += from->tls_gotno + to->tls_gotno;
4462 /* If we're merging with the primary got, any TLS relocations will
4463 come after the full set of global entries. Otherwise estimate those
4464 conservatively as well. */
4465 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4466 estimate += arg->global_count;
4468 estimate += from->global_gotno + to->global_gotno;
4470 /* Bail out if the combined GOT might be too big. */
4471 if (estimate > arg->max_count)
4474 /* Transfer the bfd's got information from FROM to TO. */
4475 tga.info = arg->info;
4477 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4481 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4485 mips_elf_replace_bfd_got (abfd, to);
4489 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4490 as possible of the primary got, since it doesn't require explicit
4491 dynamic relocations, but don't use bfds that would reference global
4492 symbols out of the addressable range. Failing the primary got,
4493 attempt to merge with the current got, or finish the current got
4494 and then make make the new got current. */
4497 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4498 struct mips_elf_got_per_bfd_arg *arg)
4500 unsigned int estimate;
4503 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4506 /* Work out the number of page, local and TLS entries. */
4507 estimate = arg->max_pages;
4508 if (estimate > g->page_gotno)
4509 estimate = g->page_gotno;
4510 estimate += g->local_gotno + g->tls_gotno;
4512 /* We place TLS GOT entries after both locals and globals. The globals
4513 for the primary GOT may overflow the normal GOT size limit, so be
4514 sure not to merge a GOT which requires TLS with the primary GOT in that
4515 case. This doesn't affect non-primary GOTs. */
4516 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4518 if (estimate <= arg->max_count)
4520 /* If we don't have a primary GOT, use it as
4521 a starting point for the primary GOT. */
4528 /* Try merging with the primary GOT. */
4529 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4534 /* If we can merge with the last-created got, do it. */
4537 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4542 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4543 fits; if it turns out that it doesn't, we'll get relocation
4544 overflows anyway. */
4545 g->next = arg->current;
4551 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4552 to GOTIDX, duplicating the entry if it has already been assigned
4553 an index in a different GOT. */
4556 mips_elf_set_gotidx (void **entryp, long gotidx)
4558 struct mips_got_entry *entry;
4560 entry = (struct mips_got_entry *) *entryp;
4561 if (entry->gotidx > 0)
4563 struct mips_got_entry *new_entry;
4565 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4569 *new_entry = *entry;
4570 *entryp = new_entry;
4573 entry->gotidx = gotidx;
4577 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4578 mips_elf_traverse_got_arg in which DATA->value is the size of one
4579 GOT entry. Set DATA->g to null on failure. */
4582 mips_elf_initialize_tls_index (void **entryp, void *data)
4584 struct mips_got_entry *entry;
4585 struct mips_elf_traverse_got_arg *arg;
4587 /* We're only interested in TLS symbols. */
4588 entry = (struct mips_got_entry *) *entryp;
4589 if (entry->tls_type == GOT_TLS_NONE)
4592 arg = (struct mips_elf_traverse_got_arg *) data;
4593 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4599 /* Account for the entries we've just allocated. */
4600 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4604 /* A htab_traverse callback for GOT entries, where DATA points to a
4605 mips_elf_traverse_got_arg. Set the global_got_area of each global
4606 symbol to DATA->value. */
4609 mips_elf_set_global_got_area (void **entryp, void *data)
4611 struct mips_got_entry *entry;
4612 struct mips_elf_traverse_got_arg *arg;
4614 entry = (struct mips_got_entry *) *entryp;
4615 arg = (struct mips_elf_traverse_got_arg *) data;
4616 if (entry->abfd != NULL
4617 && entry->symndx == -1
4618 && entry->d.h->global_got_area != GGA_NONE)
4619 entry->d.h->global_got_area = arg->value;
4623 /* A htab_traverse callback for secondary GOT entries, where DATA points
4624 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4625 and record the number of relocations they require. DATA->value is
4626 the size of one GOT entry. Set DATA->g to null on failure. */
4629 mips_elf_set_global_gotidx (void **entryp, void *data)
4631 struct mips_got_entry *entry;
4632 struct mips_elf_traverse_got_arg *arg;
4634 entry = (struct mips_got_entry *) *entryp;
4635 arg = (struct mips_elf_traverse_got_arg *) data;
4636 if (entry->abfd != NULL
4637 && entry->symndx == -1
4638 && entry->d.h->global_got_area != GGA_NONE)
4640 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4645 arg->g->assigned_low_gotno += 1;
4647 if (arg->info->shared
4648 || (elf_hash_table (arg->info)->dynamic_sections_created
4649 && entry->d.h->root.def_dynamic
4650 && !entry->d.h->root.def_regular))
4651 arg->g->relocs += 1;
4657 /* A htab_traverse callback for GOT entries for which DATA is the
4658 bfd_link_info. Forbid any global symbols from having traditional
4659 lazy-binding stubs. */
4662 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4664 struct bfd_link_info *info;
4665 struct mips_elf_link_hash_table *htab;
4666 struct mips_got_entry *entry;
4668 entry = (struct mips_got_entry *) *entryp;
4669 info = (struct bfd_link_info *) data;
4670 htab = mips_elf_hash_table (info);
4671 BFD_ASSERT (htab != NULL);
4673 if (entry->abfd != NULL
4674 && entry->symndx == -1
4675 && entry->d.h->needs_lazy_stub)
4677 entry->d.h->needs_lazy_stub = FALSE;
4678 htab->lazy_stub_count--;
4684 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4687 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4692 g = mips_elf_bfd_got (ibfd, FALSE);
4696 BFD_ASSERT (g->next);
4700 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4701 * MIPS_ELF_GOT_SIZE (abfd);
4704 /* Turn a single GOT that is too big for 16-bit addressing into
4705 a sequence of GOTs, each one 16-bit addressable. */
4708 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4709 asection *got, bfd_size_type pages)
4711 struct mips_elf_link_hash_table *htab;
4712 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4713 struct mips_elf_traverse_got_arg tga;
4714 struct mips_got_info *g, *gg;
4715 unsigned int assign, needed_relocs;
4718 dynobj = elf_hash_table (info)->dynobj;
4719 htab = mips_elf_hash_table (info);
4720 BFD_ASSERT (htab != NULL);
4724 got_per_bfd_arg.obfd = abfd;
4725 got_per_bfd_arg.info = info;
4726 got_per_bfd_arg.current = NULL;
4727 got_per_bfd_arg.primary = NULL;
4728 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4729 / MIPS_ELF_GOT_SIZE (abfd))
4730 - htab->reserved_gotno);
4731 got_per_bfd_arg.max_pages = pages;
4732 /* The number of globals that will be included in the primary GOT.
4733 See the calls to mips_elf_set_global_got_area below for more
4735 got_per_bfd_arg.global_count = g->global_gotno;
4737 /* Try to merge the GOTs of input bfds together, as long as they
4738 don't seem to exceed the maximum GOT size, choosing one of them
4739 to be the primary GOT. */
4740 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4742 gg = mips_elf_bfd_got (ibfd, FALSE);
4743 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4747 /* If we do not find any suitable primary GOT, create an empty one. */
4748 if (got_per_bfd_arg.primary == NULL)
4749 g->next = mips_elf_create_got_info (abfd);
4751 g->next = got_per_bfd_arg.primary;
4752 g->next->next = got_per_bfd_arg.current;
4754 /* GG is now the master GOT, and G is the primary GOT. */
4758 /* Map the output bfd to the primary got. That's what we're going
4759 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4760 didn't mark in check_relocs, and we want a quick way to find it.
4761 We can't just use gg->next because we're going to reverse the
4763 mips_elf_replace_bfd_got (abfd, g);
4765 /* Every symbol that is referenced in a dynamic relocation must be
4766 present in the primary GOT, so arrange for them to appear after
4767 those that are actually referenced. */
4768 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4769 g->global_gotno = gg->global_gotno;
4772 tga.value = GGA_RELOC_ONLY;
4773 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4774 tga.value = GGA_NORMAL;
4775 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4777 /* Now go through the GOTs assigning them offset ranges.
4778 [assigned_low_gotno, local_gotno[ will be set to the range of local
4779 entries in each GOT. We can then compute the end of a GOT by
4780 adding local_gotno to global_gotno. We reverse the list and make
4781 it circular since then we'll be able to quickly compute the
4782 beginning of a GOT, by computing the end of its predecessor. To
4783 avoid special cases for the primary GOT, while still preserving
4784 assertions that are valid for both single- and multi-got links,
4785 we arrange for the main got struct to have the right number of
4786 global entries, but set its local_gotno such that the initial
4787 offset of the primary GOT is zero. Remember that the primary GOT
4788 will become the last item in the circular linked list, so it
4789 points back to the master GOT. */
4790 gg->local_gotno = -g->global_gotno;
4791 gg->global_gotno = g->global_gotno;
4798 struct mips_got_info *gn;
4800 assign += htab->reserved_gotno;
4801 g->assigned_low_gotno = assign;
4802 g->local_gotno += assign;
4803 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4804 g->assigned_high_gotno = g->local_gotno - 1;
4805 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4807 /* Take g out of the direct list, and push it onto the reversed
4808 list that gg points to. g->next is guaranteed to be nonnull after
4809 this operation, as required by mips_elf_initialize_tls_index. */
4814 /* Set up any TLS entries. We always place the TLS entries after
4815 all non-TLS entries. */
4816 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4818 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4819 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4822 BFD_ASSERT (g->tls_assigned_gotno == assign);
4824 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4827 /* Forbid global symbols in every non-primary GOT from having
4828 lazy-binding stubs. */
4830 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4834 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4837 for (g = gg->next; g && g->next != gg; g = g->next)
4839 unsigned int save_assign;
4841 /* Assign offsets to global GOT entries and count how many
4842 relocations they need. */
4843 save_assign = g->assigned_low_gotno;
4844 g->assigned_low_gotno = g->local_gotno;
4846 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4848 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4851 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4852 g->assigned_low_gotno = save_assign;
4856 g->relocs += g->local_gotno - g->assigned_low_gotno;
4857 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
4858 + g->next->global_gotno
4859 + g->next->tls_gotno
4860 + htab->reserved_gotno);
4862 needed_relocs += g->relocs;
4864 needed_relocs += g->relocs;
4867 mips_elf_allocate_dynamic_relocations (dynobj, info,
4874 /* Returns the first relocation of type r_type found, beginning with
4875 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4877 static const Elf_Internal_Rela *
4878 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4879 const Elf_Internal_Rela *relocation,
4880 const Elf_Internal_Rela *relend)
4882 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4884 while (relocation < relend)
4886 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4887 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4893 /* We didn't find it. */
4897 /* Return whether an input relocation is against a local symbol. */
4900 mips_elf_local_relocation_p (bfd *input_bfd,
4901 const Elf_Internal_Rela *relocation,
4902 asection **local_sections)
4904 unsigned long r_symndx;
4905 Elf_Internal_Shdr *symtab_hdr;
4908 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4909 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4910 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4912 if (r_symndx < extsymoff)
4914 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4920 /* Sign-extend VALUE, which has the indicated number of BITS. */
4923 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4925 if (value & ((bfd_vma) 1 << (bits - 1)))
4926 /* VALUE is negative. */
4927 value |= ((bfd_vma) - 1) << bits;
4932 /* Return non-zero if the indicated VALUE has overflowed the maximum
4933 range expressible by a signed number with the indicated number of
4937 mips_elf_overflow_p (bfd_vma value, int bits)
4939 bfd_signed_vma svalue = (bfd_signed_vma) value;
4941 if (svalue > (1 << (bits - 1)) - 1)
4942 /* The value is too big. */
4944 else if (svalue < -(1 << (bits - 1)))
4945 /* The value is too small. */
4952 /* Calculate the %high function. */
4955 mips_elf_high (bfd_vma value)
4957 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4960 /* Calculate the %higher function. */
4963 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4966 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4973 /* Calculate the %highest function. */
4976 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4979 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4986 /* Create the .compact_rel section. */
4989 mips_elf_create_compact_rel_section
4990 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4993 register asection *s;
4995 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
4997 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5000 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5002 || ! bfd_set_section_alignment (abfd, s,
5003 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5006 s->size = sizeof (Elf32_External_compact_rel);
5012 /* Create the .got section to hold the global offset table. */
5015 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5018 register asection *s;
5019 struct elf_link_hash_entry *h;
5020 struct bfd_link_hash_entry *bh;
5021 struct mips_elf_link_hash_table *htab;
5023 htab = mips_elf_hash_table (info);
5024 BFD_ASSERT (htab != NULL);
5026 /* This function may be called more than once. */
5030 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5031 | SEC_LINKER_CREATED);
5033 /* We have to use an alignment of 2**4 here because this is hardcoded
5034 in the function stub generation and in the linker script. */
5035 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5037 || ! bfd_set_section_alignment (abfd, s, 4))
5041 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5042 linker script because we don't want to define the symbol if we
5043 are not creating a global offset table. */
5045 if (! (_bfd_generic_link_add_one_symbol
5046 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5047 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5050 h = (struct elf_link_hash_entry *) bh;
5053 h->type = STT_OBJECT;
5054 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5055 elf_hash_table (info)->hgot = h;
5058 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5061 htab->got_info = mips_elf_create_got_info (abfd);
5062 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5063 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5065 /* We also need a .got.plt section when generating PLTs. */
5066 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5067 SEC_ALLOC | SEC_LOAD
5070 | SEC_LINKER_CREATED);
5078 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5079 __GOTT_INDEX__ symbols. These symbols are only special for
5080 shared objects; they are not used in executables. */
5083 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5085 return (mips_elf_hash_table (info)->is_vxworks
5087 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5088 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5091 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5092 require an la25 stub. See also mips_elf_local_pic_function_p,
5093 which determines whether the destination function ever requires a
5097 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5098 bfd_boolean target_is_16_bit_code_p)
5100 /* We specifically ignore branches and jumps from EF_PIC objects,
5101 where the onus is on the compiler or programmer to perform any
5102 necessary initialization of $25. Sometimes such initialization
5103 is unnecessary; for example, -mno-shared functions do not use
5104 the incoming value of $25, and may therefore be called directly. */
5105 if (PIC_OBJECT_P (input_bfd))
5112 case R_MICROMIPS_26_S1:
5113 case R_MICROMIPS_PC7_S1:
5114 case R_MICROMIPS_PC10_S1:
5115 case R_MICROMIPS_PC16_S1:
5116 case R_MICROMIPS_PC23_S2:
5120 return !target_is_16_bit_code_p;
5127 /* Calculate the value produced by the RELOCATION (which comes from
5128 the INPUT_BFD). The ADDEND is the addend to use for this
5129 RELOCATION; RELOCATION->R_ADDEND is ignored.
5131 The result of the relocation calculation is stored in VALUEP.
5132 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5133 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5135 This function returns bfd_reloc_continue if the caller need take no
5136 further action regarding this relocation, bfd_reloc_notsupported if
5137 something goes dramatically wrong, bfd_reloc_overflow if an
5138 overflow occurs, and bfd_reloc_ok to indicate success. */
5140 static bfd_reloc_status_type
5141 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5142 asection *input_section,
5143 struct bfd_link_info *info,
5144 const Elf_Internal_Rela *relocation,
5145 bfd_vma addend, reloc_howto_type *howto,
5146 Elf_Internal_Sym *local_syms,
5147 asection **local_sections, bfd_vma *valuep,
5149 bfd_boolean *cross_mode_jump_p,
5150 bfd_boolean save_addend)
5152 /* The eventual value we will return. */
5154 /* The address of the symbol against which the relocation is
5157 /* The final GP value to be used for the relocatable, executable, or
5158 shared object file being produced. */
5160 /* The place (section offset or address) of the storage unit being
5163 /* The value of GP used to create the relocatable object. */
5165 /* The offset into the global offset table at which the address of
5166 the relocation entry symbol, adjusted by the addend, resides
5167 during execution. */
5168 bfd_vma g = MINUS_ONE;
5169 /* The section in which the symbol referenced by the relocation is
5171 asection *sec = NULL;
5172 struct mips_elf_link_hash_entry *h = NULL;
5173 /* TRUE if the symbol referred to by this relocation is a local
5175 bfd_boolean local_p, was_local_p;
5176 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5177 bfd_boolean gp_disp_p = FALSE;
5178 /* TRUE if the symbol referred to by this relocation is
5179 "__gnu_local_gp". */
5180 bfd_boolean gnu_local_gp_p = FALSE;
5181 Elf_Internal_Shdr *symtab_hdr;
5183 unsigned long r_symndx;
5185 /* TRUE if overflow occurred during the calculation of the
5186 relocation value. */
5187 bfd_boolean overflowed_p;
5188 /* TRUE if this relocation refers to a MIPS16 function. */
5189 bfd_boolean target_is_16_bit_code_p = FALSE;
5190 bfd_boolean target_is_micromips_code_p = FALSE;
5191 struct mips_elf_link_hash_table *htab;
5194 dynobj = elf_hash_table (info)->dynobj;
5195 htab = mips_elf_hash_table (info);
5196 BFD_ASSERT (htab != NULL);
5198 /* Parse the relocation. */
5199 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5200 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5201 p = (input_section->output_section->vma
5202 + input_section->output_offset
5203 + relocation->r_offset);
5205 /* Assume that there will be no overflow. */
5206 overflowed_p = FALSE;
5208 /* Figure out whether or not the symbol is local, and get the offset
5209 used in the array of hash table entries. */
5210 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5211 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5213 was_local_p = local_p;
5214 if (! elf_bad_symtab (input_bfd))
5215 extsymoff = symtab_hdr->sh_info;
5218 /* The symbol table does not follow the rule that local symbols
5219 must come before globals. */
5223 /* Figure out the value of the symbol. */
5226 Elf_Internal_Sym *sym;
5228 sym = local_syms + r_symndx;
5229 sec = local_sections[r_symndx];
5231 symbol = sec->output_section->vma + sec->output_offset;
5232 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5233 || (sec->flags & SEC_MERGE))
5234 symbol += sym->st_value;
5235 if ((sec->flags & SEC_MERGE)
5236 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5238 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5240 addend += sec->output_section->vma + sec->output_offset;
5243 /* MIPS16/microMIPS text labels should be treated as odd. */
5244 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5247 /* Record the name of this symbol, for our caller. */
5248 *namep = bfd_elf_string_from_elf_section (input_bfd,
5249 symtab_hdr->sh_link,
5252 *namep = bfd_section_name (input_bfd, sec);
5254 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5255 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5259 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5261 /* For global symbols we look up the symbol in the hash-table. */
5262 h = ((struct mips_elf_link_hash_entry *)
5263 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5264 /* Find the real hash-table entry for this symbol. */
5265 while (h->root.root.type == bfd_link_hash_indirect
5266 || h->root.root.type == bfd_link_hash_warning)
5267 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5269 /* Record the name of this symbol, for our caller. */
5270 *namep = h->root.root.root.string;
5272 /* See if this is the special _gp_disp symbol. Note that such a
5273 symbol must always be a global symbol. */
5274 if (strcmp (*namep, "_gp_disp") == 0
5275 && ! NEWABI_P (input_bfd))
5277 /* Relocations against _gp_disp are permitted only with
5278 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5279 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5280 return bfd_reloc_notsupported;
5284 /* See if this is the special _gp symbol. Note that such a
5285 symbol must always be a global symbol. */
5286 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5287 gnu_local_gp_p = TRUE;
5290 /* If this symbol is defined, calculate its address. Note that
5291 _gp_disp is a magic symbol, always implicitly defined by the
5292 linker, so it's inappropriate to check to see whether or not
5294 else if ((h->root.root.type == bfd_link_hash_defined
5295 || h->root.root.type == bfd_link_hash_defweak)
5296 && h->root.root.u.def.section)
5298 sec = h->root.root.u.def.section;
5299 if (sec->output_section)
5300 symbol = (h->root.root.u.def.value
5301 + sec->output_section->vma
5302 + sec->output_offset);
5304 symbol = h->root.root.u.def.value;
5306 else if (h->root.root.type == bfd_link_hash_undefweak)
5307 /* We allow relocations against undefined weak symbols, giving
5308 it the value zero, so that you can undefined weak functions
5309 and check to see if they exist by looking at their
5312 else if (info->unresolved_syms_in_objects == RM_IGNORE
5313 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5315 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5316 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5318 /* If this is a dynamic link, we should have created a
5319 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5320 in in _bfd_mips_elf_create_dynamic_sections.
5321 Otherwise, we should define the symbol with a value of 0.
5322 FIXME: It should probably get into the symbol table
5324 BFD_ASSERT (! info->shared);
5325 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5328 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5330 /* This is an optional symbol - an Irix specific extension to the
5331 ELF spec. Ignore it for now.
5332 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5333 than simply ignoring them, but we do not handle this for now.
5334 For information see the "64-bit ELF Object File Specification"
5335 which is available from here:
5336 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5339 else if ((*info->callbacks->undefined_symbol)
5340 (info, h->root.root.root.string, input_bfd,
5341 input_section, relocation->r_offset,
5342 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5343 || ELF_ST_VISIBILITY (h->root.other)))
5345 return bfd_reloc_undefined;
5349 return bfd_reloc_notsupported;
5352 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5353 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5356 /* If this is a reference to a 16-bit function with a stub, we need
5357 to redirect the relocation to the stub unless:
5359 (a) the relocation is for a MIPS16 JAL;
5361 (b) the relocation is for a MIPS16 PIC call, and there are no
5362 non-MIPS16 uses of the GOT slot; or
5364 (c) the section allows direct references to MIPS16 functions. */
5365 if (r_type != R_MIPS16_26
5366 && !info->relocatable
5368 && h->fn_stub != NULL
5369 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5371 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5372 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5373 && !section_allows_mips16_refs_p (input_section))
5375 /* This is a 32- or 64-bit call to a 16-bit function. We should
5376 have already noticed that we were going to need the
5380 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5385 BFD_ASSERT (h->need_fn_stub);
5388 /* If a LA25 header for the stub itself exists, point to the
5389 prepended LUI/ADDIU sequence. */
5390 sec = h->la25_stub->stub_section;
5391 value = h->la25_stub->offset;
5400 symbol = sec->output_section->vma + sec->output_offset + value;
5401 /* The target is 16-bit, but the stub isn't. */
5402 target_is_16_bit_code_p = FALSE;
5404 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5405 to a standard MIPS function, we need to redirect the call to the stub.
5406 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5407 indirect calls should use an indirect stub instead. */
5408 else if (r_type == R_MIPS16_26 && !info->relocatable
5409 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5411 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5412 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5413 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5416 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5419 /* If both call_stub and call_fp_stub are defined, we can figure
5420 out which one to use by checking which one appears in the input
5422 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5427 for (o = input_bfd->sections; o != NULL; o = o->next)
5429 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5431 sec = h->call_fp_stub;
5438 else if (h->call_stub != NULL)
5441 sec = h->call_fp_stub;
5444 BFD_ASSERT (sec->size > 0);
5445 symbol = sec->output_section->vma + sec->output_offset;
5447 /* If this is a direct call to a PIC function, redirect to the
5449 else if (h != NULL && h->la25_stub
5450 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5451 target_is_16_bit_code_p))
5452 symbol = (h->la25_stub->stub_section->output_section->vma
5453 + h->la25_stub->stub_section->output_offset
5454 + h->la25_stub->offset);
5455 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5456 entry is used if a standard PLT entry has also been made. In this
5457 case the symbol will have been set by mips_elf_set_plt_sym_value
5458 to point to the standard PLT entry, so redirect to the compressed
5460 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5461 && !info->relocatable
5464 && h->root.plt.plist->comp_offset != MINUS_ONE
5465 && h->root.plt.plist->mips_offset != MINUS_ONE)
5467 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5470 symbol = (sec->output_section->vma
5471 + sec->output_offset
5472 + htab->plt_header_size
5473 + htab->plt_mips_offset
5474 + h->root.plt.plist->comp_offset
5477 target_is_16_bit_code_p = !micromips_p;
5478 target_is_micromips_code_p = micromips_p;
5481 /* Make sure MIPS16 and microMIPS are not used together. */
5482 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5483 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5485 (*_bfd_error_handler)
5486 (_("MIPS16 and microMIPS functions cannot call each other"));
5487 return bfd_reloc_notsupported;
5490 /* Calls from 16-bit code to 32-bit code and vice versa require the
5491 mode change. However, we can ignore calls to undefined weak symbols,
5492 which should never be executed at runtime. This exception is important
5493 because the assembly writer may have "known" that any definition of the
5494 symbol would be 16-bit code, and that direct jumps were therefore
5496 *cross_mode_jump_p = (!info->relocatable
5497 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5498 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5499 || (r_type == R_MICROMIPS_26_S1
5500 && !target_is_micromips_code_p)
5501 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5502 && (target_is_16_bit_code_p
5503 || target_is_micromips_code_p))));
5505 local_p = (h == NULL || mips_use_local_got_p (info, h));
5507 gp0 = _bfd_get_gp_value (input_bfd);
5508 gp = _bfd_get_gp_value (abfd);
5510 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5515 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5516 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5517 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5518 if (got_page_reloc_p (r_type) && !local_p)
5520 r_type = (micromips_reloc_p (r_type)
5521 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5525 /* If we haven't already determined the GOT offset, and we're going
5526 to need it, get it now. */
5529 case R_MIPS16_CALL16:
5530 case R_MIPS16_GOT16:
5533 case R_MIPS_GOT_DISP:
5534 case R_MIPS_GOT_HI16:
5535 case R_MIPS_CALL_HI16:
5536 case R_MIPS_GOT_LO16:
5537 case R_MIPS_CALL_LO16:
5538 case R_MICROMIPS_CALL16:
5539 case R_MICROMIPS_GOT16:
5540 case R_MICROMIPS_GOT_DISP:
5541 case R_MICROMIPS_GOT_HI16:
5542 case R_MICROMIPS_CALL_HI16:
5543 case R_MICROMIPS_GOT_LO16:
5544 case R_MICROMIPS_CALL_LO16:
5546 case R_MIPS_TLS_GOTTPREL:
5547 case R_MIPS_TLS_LDM:
5548 case R_MIPS16_TLS_GD:
5549 case R_MIPS16_TLS_GOTTPREL:
5550 case R_MIPS16_TLS_LDM:
5551 case R_MICROMIPS_TLS_GD:
5552 case R_MICROMIPS_TLS_GOTTPREL:
5553 case R_MICROMIPS_TLS_LDM:
5554 /* Find the index into the GOT where this value is located. */
5555 if (tls_ldm_reloc_p (r_type))
5557 g = mips_elf_local_got_index (abfd, input_bfd, info,
5558 0, 0, NULL, r_type);
5560 return bfd_reloc_outofrange;
5564 /* On VxWorks, CALL relocations should refer to the .got.plt
5565 entry, which is initialized to point at the PLT stub. */
5566 if (htab->is_vxworks
5567 && (call_hi16_reloc_p (r_type)
5568 || call_lo16_reloc_p (r_type)
5569 || call16_reloc_p (r_type)))
5571 BFD_ASSERT (addend == 0);
5572 BFD_ASSERT (h->root.needs_plt);
5573 g = mips_elf_gotplt_index (info, &h->root);
5577 BFD_ASSERT (addend == 0);
5578 g = mips_elf_global_got_index (abfd, info, input_bfd,
5580 if (!TLS_RELOC_P (r_type)
5581 && !elf_hash_table (info)->dynamic_sections_created)
5582 /* This is a static link. We must initialize the GOT entry. */
5583 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5586 else if (!htab->is_vxworks
5587 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5588 /* The calculation below does not involve "g". */
5592 g = mips_elf_local_got_index (abfd, input_bfd, info,
5593 symbol + addend, r_symndx, h, r_type);
5595 return bfd_reloc_outofrange;
5598 /* Convert GOT indices to actual offsets. */
5599 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5603 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5604 symbols are resolved by the loader. Add them to .rela.dyn. */
5605 if (h != NULL && is_gott_symbol (info, &h->root))
5607 Elf_Internal_Rela outrel;
5611 s = mips_elf_rel_dyn_section (info, FALSE);
5612 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5614 outrel.r_offset = (input_section->output_section->vma
5615 + input_section->output_offset
5616 + relocation->r_offset);
5617 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5618 outrel.r_addend = addend;
5619 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5621 /* If we've written this relocation for a readonly section,
5622 we need to set DF_TEXTREL again, so that we do not delete the
5624 if (MIPS_ELF_READONLY_SECTION (input_section))
5625 info->flags |= DF_TEXTREL;
5628 return bfd_reloc_ok;
5631 /* Figure out what kind of relocation is being performed. */
5635 return bfd_reloc_continue;
5638 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5639 overflowed_p = mips_elf_overflow_p (value, 16);
5646 || (htab->root.dynamic_sections_created
5648 && h->root.def_dynamic
5649 && !h->root.def_regular
5650 && !h->has_static_relocs))
5651 && r_symndx != STN_UNDEF
5653 || h->root.root.type != bfd_link_hash_undefweak
5654 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5655 && (input_section->flags & SEC_ALLOC) != 0)
5657 /* If we're creating a shared library, then we can't know
5658 where the symbol will end up. So, we create a relocation
5659 record in the output, and leave the job up to the dynamic
5660 linker. We must do the same for executable references to
5661 shared library symbols, unless we've decided to use copy
5662 relocs or PLTs instead. */
5664 if (!mips_elf_create_dynamic_relocation (abfd,
5672 return bfd_reloc_undefined;
5676 if (r_type != R_MIPS_REL32)
5677 value = symbol + addend;
5681 value &= howto->dst_mask;
5685 value = symbol + addend - p;
5686 value &= howto->dst_mask;
5690 /* The calculation for R_MIPS16_26 is just the same as for an
5691 R_MIPS_26. It's only the storage of the relocated field into
5692 the output file that's different. That's handled in
5693 mips_elf_perform_relocation. So, we just fall through to the
5694 R_MIPS_26 case here. */
5696 case R_MICROMIPS_26_S1:
5700 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5701 the correct ISA mode selector and bit 1 must be 0. */
5702 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5703 return bfd_reloc_outofrange;
5705 /* Shift is 2, unusually, for microMIPS JALX. */
5706 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5709 value = addend | ((p + 4) & (0xfc000000 << shift));
5711 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5712 value = (value + symbol) >> shift;
5713 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5714 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5715 value &= howto->dst_mask;
5719 case R_MIPS_TLS_DTPREL_HI16:
5720 case R_MIPS16_TLS_DTPREL_HI16:
5721 case R_MICROMIPS_TLS_DTPREL_HI16:
5722 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5726 case R_MIPS_TLS_DTPREL_LO16:
5727 case R_MIPS_TLS_DTPREL32:
5728 case R_MIPS_TLS_DTPREL64:
5729 case R_MIPS16_TLS_DTPREL_LO16:
5730 case R_MICROMIPS_TLS_DTPREL_LO16:
5731 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5734 case R_MIPS_TLS_TPREL_HI16:
5735 case R_MIPS16_TLS_TPREL_HI16:
5736 case R_MICROMIPS_TLS_TPREL_HI16:
5737 value = (mips_elf_high (addend + symbol - tprel_base (info))
5741 case R_MIPS_TLS_TPREL_LO16:
5742 case R_MIPS_TLS_TPREL32:
5743 case R_MIPS_TLS_TPREL64:
5744 case R_MIPS16_TLS_TPREL_LO16:
5745 case R_MICROMIPS_TLS_TPREL_LO16:
5746 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5751 case R_MICROMIPS_HI16:
5754 value = mips_elf_high (addend + symbol);
5755 value &= howto->dst_mask;
5759 /* For MIPS16 ABI code we generate this sequence
5760 0: li $v0,%hi(_gp_disp)
5761 4: addiupc $v1,%lo(_gp_disp)
5765 So the offsets of hi and lo relocs are the same, but the
5766 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5767 ADDIUPC clears the low two bits of the instruction address,
5768 so the base is ($t9 + 4) & ~3. */
5769 if (r_type == R_MIPS16_HI16)
5770 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5771 /* The microMIPS .cpload sequence uses the same assembly
5772 instructions as the traditional psABI version, but the
5773 incoming $t9 has the low bit set. */
5774 else if (r_type == R_MICROMIPS_HI16)
5775 value = mips_elf_high (addend + gp - p - 1);
5777 value = mips_elf_high (addend + gp - p);
5778 overflowed_p = mips_elf_overflow_p (value, 16);
5784 case R_MICROMIPS_LO16:
5785 case R_MICROMIPS_HI0_LO16:
5787 value = (symbol + addend) & howto->dst_mask;
5790 /* See the comment for R_MIPS16_HI16 above for the reason
5791 for this conditional. */
5792 if (r_type == R_MIPS16_LO16)
5793 value = addend + gp - (p & ~(bfd_vma) 0x3);
5794 else if (r_type == R_MICROMIPS_LO16
5795 || r_type == R_MICROMIPS_HI0_LO16)
5796 value = addend + gp - p + 3;
5798 value = addend + gp - p + 4;
5799 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5800 for overflow. But, on, say, IRIX5, relocations against
5801 _gp_disp are normally generated from the .cpload
5802 pseudo-op. It generates code that normally looks like
5805 lui $gp,%hi(_gp_disp)
5806 addiu $gp,$gp,%lo(_gp_disp)
5809 Here $t9 holds the address of the function being called,
5810 as required by the MIPS ELF ABI. The R_MIPS_LO16
5811 relocation can easily overflow in this situation, but the
5812 R_MIPS_HI16 relocation will handle the overflow.
5813 Therefore, we consider this a bug in the MIPS ABI, and do
5814 not check for overflow here. */
5818 case R_MIPS_LITERAL:
5819 case R_MICROMIPS_LITERAL:
5820 /* Because we don't merge literal sections, we can handle this
5821 just like R_MIPS_GPREL16. In the long run, we should merge
5822 shared literals, and then we will need to additional work
5827 case R_MIPS16_GPREL:
5828 /* The R_MIPS16_GPREL performs the same calculation as
5829 R_MIPS_GPREL16, but stores the relocated bits in a different
5830 order. We don't need to do anything special here; the
5831 differences are handled in mips_elf_perform_relocation. */
5832 case R_MIPS_GPREL16:
5833 case R_MICROMIPS_GPREL7_S2:
5834 case R_MICROMIPS_GPREL16:
5835 /* Only sign-extend the addend if it was extracted from the
5836 instruction. If the addend was separate, leave it alone,
5837 otherwise we may lose significant bits. */
5838 if (howto->partial_inplace)
5839 addend = _bfd_mips_elf_sign_extend (addend, 16);
5840 value = symbol + addend - gp;
5841 /* If the symbol was local, any earlier relocatable links will
5842 have adjusted its addend with the gp offset, so compensate
5843 for that now. Don't do it for symbols forced local in this
5844 link, though, since they won't have had the gp offset applied
5848 overflowed_p = mips_elf_overflow_p (value, 16);
5851 case R_MIPS16_GOT16:
5852 case R_MIPS16_CALL16:
5855 case R_MICROMIPS_GOT16:
5856 case R_MICROMIPS_CALL16:
5857 /* VxWorks does not have separate local and global semantics for
5858 R_MIPS*_GOT16; every relocation evaluates to "G". */
5859 if (!htab->is_vxworks && local_p)
5861 value = mips_elf_got16_entry (abfd, input_bfd, info,
5862 symbol + addend, !was_local_p);
5863 if (value == MINUS_ONE)
5864 return bfd_reloc_outofrange;
5866 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5867 overflowed_p = mips_elf_overflow_p (value, 16);
5874 case R_MIPS_TLS_GOTTPREL:
5875 case R_MIPS_TLS_LDM:
5876 case R_MIPS_GOT_DISP:
5877 case R_MIPS16_TLS_GD:
5878 case R_MIPS16_TLS_GOTTPREL:
5879 case R_MIPS16_TLS_LDM:
5880 case R_MICROMIPS_TLS_GD:
5881 case R_MICROMIPS_TLS_GOTTPREL:
5882 case R_MICROMIPS_TLS_LDM:
5883 case R_MICROMIPS_GOT_DISP:
5885 overflowed_p = mips_elf_overflow_p (value, 16);
5888 case R_MIPS_GPREL32:
5889 value = (addend + symbol + gp0 - gp);
5891 value &= howto->dst_mask;
5895 case R_MIPS_GNU_REL16_S2:
5896 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5897 overflowed_p = mips_elf_overflow_p (value, 18);
5898 value >>= howto->rightshift;
5899 value &= howto->dst_mask;
5902 case R_MICROMIPS_PC7_S1:
5903 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5904 overflowed_p = mips_elf_overflow_p (value, 8);
5905 value >>= howto->rightshift;
5906 value &= howto->dst_mask;
5909 case R_MICROMIPS_PC10_S1:
5910 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5911 overflowed_p = mips_elf_overflow_p (value, 11);
5912 value >>= howto->rightshift;
5913 value &= howto->dst_mask;
5916 case R_MICROMIPS_PC16_S1:
5917 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5918 overflowed_p = mips_elf_overflow_p (value, 17);
5919 value >>= howto->rightshift;
5920 value &= howto->dst_mask;
5923 case R_MICROMIPS_PC23_S2:
5924 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5925 overflowed_p = mips_elf_overflow_p (value, 25);
5926 value >>= howto->rightshift;
5927 value &= howto->dst_mask;
5930 case R_MIPS_GOT_HI16:
5931 case R_MIPS_CALL_HI16:
5932 case R_MICROMIPS_GOT_HI16:
5933 case R_MICROMIPS_CALL_HI16:
5934 /* We're allowed to handle these two relocations identically.
5935 The dynamic linker is allowed to handle the CALL relocations
5936 differently by creating a lazy evaluation stub. */
5938 value = mips_elf_high (value);
5939 value &= howto->dst_mask;
5942 case R_MIPS_GOT_LO16:
5943 case R_MIPS_CALL_LO16:
5944 case R_MICROMIPS_GOT_LO16:
5945 case R_MICROMIPS_CALL_LO16:
5946 value = g & howto->dst_mask;
5949 case R_MIPS_GOT_PAGE:
5950 case R_MICROMIPS_GOT_PAGE:
5951 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5952 if (value == MINUS_ONE)
5953 return bfd_reloc_outofrange;
5954 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5955 overflowed_p = mips_elf_overflow_p (value, 16);
5958 case R_MIPS_GOT_OFST:
5959 case R_MICROMIPS_GOT_OFST:
5961 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5964 overflowed_p = mips_elf_overflow_p (value, 16);
5968 case R_MICROMIPS_SUB:
5969 value = symbol - addend;
5970 value &= howto->dst_mask;
5974 case R_MICROMIPS_HIGHER:
5975 value = mips_elf_higher (addend + symbol);
5976 value &= howto->dst_mask;
5979 case R_MIPS_HIGHEST:
5980 case R_MICROMIPS_HIGHEST:
5981 value = mips_elf_highest (addend + symbol);
5982 value &= howto->dst_mask;
5985 case R_MIPS_SCN_DISP:
5986 case R_MICROMIPS_SCN_DISP:
5987 value = symbol + addend - sec->output_offset;
5988 value &= howto->dst_mask;
5992 case R_MICROMIPS_JALR:
5993 /* This relocation is only a hint. In some cases, we optimize
5994 it into a bal instruction. But we don't try to optimize
5995 when the symbol does not resolve locally. */
5996 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5997 return bfd_reloc_continue;
5998 value = symbol + addend;
6002 case R_MIPS_GNU_VTINHERIT:
6003 case R_MIPS_GNU_VTENTRY:
6004 /* We don't do anything with these at present. */
6005 return bfd_reloc_continue;
6008 /* An unrecognized relocation type. */
6009 return bfd_reloc_notsupported;
6012 /* Store the VALUE for our caller. */
6014 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6017 /* Obtain the field relocated by RELOCATION. */
6020 mips_elf_obtain_contents (reloc_howto_type *howto,
6021 const Elf_Internal_Rela *relocation,
6022 bfd *input_bfd, bfd_byte *contents)
6025 bfd_byte *location = contents + relocation->r_offset;
6027 /* Obtain the bytes. */
6028 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6033 /* It has been determined that the result of the RELOCATION is the
6034 VALUE. Use HOWTO to place VALUE into the output file at the
6035 appropriate position. The SECTION is the section to which the
6037 CROSS_MODE_JUMP_P is true if the relocation field
6038 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6040 Returns FALSE if anything goes wrong. */
6043 mips_elf_perform_relocation (struct bfd_link_info *info,
6044 reloc_howto_type *howto,
6045 const Elf_Internal_Rela *relocation,
6046 bfd_vma value, bfd *input_bfd,
6047 asection *input_section, bfd_byte *contents,
6048 bfd_boolean cross_mode_jump_p)
6052 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6054 /* Figure out where the relocation is occurring. */
6055 location = contents + relocation->r_offset;
6057 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6059 /* Obtain the current value. */
6060 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6062 /* Clear the field we are setting. */
6063 x &= ~howto->dst_mask;
6065 /* Set the field. */
6066 x |= (value & howto->dst_mask);
6068 /* If required, turn JAL into JALX. */
6069 if (cross_mode_jump_p && jal_reloc_p (r_type))
6072 bfd_vma opcode = x >> 26;
6073 bfd_vma jalx_opcode;
6075 /* Check to see if the opcode is already JAL or JALX. */
6076 if (r_type == R_MIPS16_26)
6078 ok = ((opcode == 0x6) || (opcode == 0x7));
6081 else if (r_type == R_MICROMIPS_26_S1)
6083 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6088 ok = ((opcode == 0x3) || (opcode == 0x1d));
6092 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6093 convert J or JALS to JALX. */
6096 (*_bfd_error_handler)
6097 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
6100 (unsigned long) relocation->r_offset);
6101 bfd_set_error (bfd_error_bad_value);
6105 /* Make this the JALX opcode. */
6106 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6109 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6111 if (!info->relocatable
6112 && !cross_mode_jump_p
6113 && ((JAL_TO_BAL_P (input_bfd)
6114 && r_type == R_MIPS_26
6115 && (x >> 26) == 0x3) /* jal addr */
6116 || (JALR_TO_BAL_P (input_bfd)
6117 && r_type == R_MIPS_JALR
6118 && x == 0x0320f809) /* jalr t9 */
6119 || (JR_TO_B_P (input_bfd)
6120 && r_type == R_MIPS_JALR
6121 && x == 0x03200008))) /* jr t9 */
6127 addr = (input_section->output_section->vma
6128 + input_section->output_offset
6129 + relocation->r_offset
6131 if (r_type == R_MIPS_26)
6132 dest = (value << 2) | ((addr >> 28) << 28);
6136 if (off <= 0x1ffff && off >= -0x20000)
6138 if (x == 0x03200008) /* jr t9 */
6139 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6141 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6145 /* Put the value into the output. */
6146 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6148 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6154 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6155 is the original relocation, which is now being transformed into a
6156 dynamic relocation. The ADDENDP is adjusted if necessary; the
6157 caller should store the result in place of the original addend. */
6160 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6161 struct bfd_link_info *info,
6162 const Elf_Internal_Rela *rel,
6163 struct mips_elf_link_hash_entry *h,
6164 asection *sec, bfd_vma symbol,
6165 bfd_vma *addendp, asection *input_section)
6167 Elf_Internal_Rela outrel[3];
6172 bfd_boolean defined_p;
6173 struct mips_elf_link_hash_table *htab;
6175 htab = mips_elf_hash_table (info);
6176 BFD_ASSERT (htab != NULL);
6178 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6179 dynobj = elf_hash_table (info)->dynobj;
6180 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6181 BFD_ASSERT (sreloc != NULL);
6182 BFD_ASSERT (sreloc->contents != NULL);
6183 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6186 outrel[0].r_offset =
6187 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6188 if (ABI_64_P (output_bfd))
6190 outrel[1].r_offset =
6191 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6192 outrel[2].r_offset =
6193 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6196 if (outrel[0].r_offset == MINUS_ONE)
6197 /* The relocation field has been deleted. */
6200 if (outrel[0].r_offset == MINUS_TWO)
6202 /* The relocation field has been converted into a relative value of
6203 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6204 the field to be fully relocated, so add in the symbol's value. */
6209 /* We must now calculate the dynamic symbol table index to use
6210 in the relocation. */
6211 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6213 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6214 indx = h->root.dynindx;
6215 if (SGI_COMPAT (output_bfd))
6216 defined_p = h->root.def_regular;
6218 /* ??? glibc's ld.so just adds the final GOT entry to the
6219 relocation field. It therefore treats relocs against
6220 defined symbols in the same way as relocs against
6221 undefined symbols. */
6226 if (sec != NULL && bfd_is_abs_section (sec))
6228 else if (sec == NULL || sec->owner == NULL)
6230 bfd_set_error (bfd_error_bad_value);
6235 indx = elf_section_data (sec->output_section)->dynindx;
6238 asection *osec = htab->root.text_index_section;
6239 indx = elf_section_data (osec)->dynindx;
6245 /* Instead of generating a relocation using the section
6246 symbol, we may as well make it a fully relative
6247 relocation. We want to avoid generating relocations to
6248 local symbols because we used to generate them
6249 incorrectly, without adding the original symbol value,
6250 which is mandated by the ABI for section symbols. In
6251 order to give dynamic loaders and applications time to
6252 phase out the incorrect use, we refrain from emitting
6253 section-relative relocations. It's not like they're
6254 useful, after all. This should be a bit more efficient
6256 /* ??? Although this behavior is compatible with glibc's ld.so,
6257 the ABI says that relocations against STN_UNDEF should have
6258 a symbol value of 0. Irix rld honors this, so relocations
6259 against STN_UNDEF have no effect. */
6260 if (!SGI_COMPAT (output_bfd))
6265 /* If the relocation was previously an absolute relocation and
6266 this symbol will not be referred to by the relocation, we must
6267 adjust it by the value we give it in the dynamic symbol table.
6268 Otherwise leave the job up to the dynamic linker. */
6269 if (defined_p && r_type != R_MIPS_REL32)
6272 if (htab->is_vxworks)
6273 /* VxWorks uses non-relative relocations for this. */
6274 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6276 /* The relocation is always an REL32 relocation because we don't
6277 know where the shared library will wind up at load-time. */
6278 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6281 /* For strict adherence to the ABI specification, we should
6282 generate a R_MIPS_64 relocation record by itself before the
6283 _REL32/_64 record as well, such that the addend is read in as
6284 a 64-bit value (REL32 is a 32-bit relocation, after all).
6285 However, since none of the existing ELF64 MIPS dynamic
6286 loaders seems to care, we don't waste space with these
6287 artificial relocations. If this turns out to not be true,
6288 mips_elf_allocate_dynamic_relocation() should be tweaked so
6289 as to make room for a pair of dynamic relocations per
6290 invocation if ABI_64_P, and here we should generate an
6291 additional relocation record with R_MIPS_64 by itself for a
6292 NULL symbol before this relocation record. */
6293 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6294 ABI_64_P (output_bfd)
6297 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6299 /* Adjust the output offset of the relocation to reference the
6300 correct location in the output file. */
6301 outrel[0].r_offset += (input_section->output_section->vma
6302 + input_section->output_offset);
6303 outrel[1].r_offset += (input_section->output_section->vma
6304 + input_section->output_offset);
6305 outrel[2].r_offset += (input_section->output_section->vma
6306 + input_section->output_offset);
6308 /* Put the relocation back out. We have to use the special
6309 relocation outputter in the 64-bit case since the 64-bit
6310 relocation format is non-standard. */
6311 if (ABI_64_P (output_bfd))
6313 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6314 (output_bfd, &outrel[0],
6316 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6318 else if (htab->is_vxworks)
6320 /* VxWorks uses RELA rather than REL dynamic relocations. */
6321 outrel[0].r_addend = *addendp;
6322 bfd_elf32_swap_reloca_out
6323 (output_bfd, &outrel[0],
6325 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6328 bfd_elf32_swap_reloc_out
6329 (output_bfd, &outrel[0],
6330 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6332 /* We've now added another relocation. */
6333 ++sreloc->reloc_count;
6335 /* Make sure the output section is writable. The dynamic linker
6336 will be writing to it. */
6337 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6340 /* On IRIX5, make an entry of compact relocation info. */
6341 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6343 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6348 Elf32_crinfo cptrel;
6350 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6351 cptrel.vaddr = (rel->r_offset
6352 + input_section->output_section->vma
6353 + input_section->output_offset);
6354 if (r_type == R_MIPS_REL32)
6355 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6357 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6358 mips_elf_set_cr_dist2to (cptrel, 0);
6359 cptrel.konst = *addendp;
6361 cr = (scpt->contents
6362 + sizeof (Elf32_External_compact_rel));
6363 mips_elf_set_cr_relvaddr (cptrel, 0);
6364 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6365 ((Elf32_External_crinfo *) cr
6366 + scpt->reloc_count));
6367 ++scpt->reloc_count;
6371 /* If we've written this relocation for a readonly section,
6372 we need to set DF_TEXTREL again, so that we do not delete the
6374 if (MIPS_ELF_READONLY_SECTION (input_section))
6375 info->flags |= DF_TEXTREL;
6380 /* Return the MACH for a MIPS e_flags value. */
6383 _bfd_elf_mips_mach (flagword flags)
6385 switch (flags & EF_MIPS_MACH)
6387 case E_MIPS_MACH_3900:
6388 return bfd_mach_mips3900;
6390 case E_MIPS_MACH_4010:
6391 return bfd_mach_mips4010;
6393 case E_MIPS_MACH_4100:
6394 return bfd_mach_mips4100;
6396 case E_MIPS_MACH_4111:
6397 return bfd_mach_mips4111;
6399 case E_MIPS_MACH_4120:
6400 return bfd_mach_mips4120;
6402 case E_MIPS_MACH_4650:
6403 return bfd_mach_mips4650;
6405 case E_MIPS_MACH_5400:
6406 return bfd_mach_mips5400;
6408 case E_MIPS_MACH_5500:
6409 return bfd_mach_mips5500;
6411 case E_MIPS_MACH_5900:
6412 return bfd_mach_mips5900;
6414 case E_MIPS_MACH_9000:
6415 return bfd_mach_mips9000;
6417 case E_MIPS_MACH_SB1:
6418 return bfd_mach_mips_sb1;
6420 case E_MIPS_MACH_LS2E:
6421 return bfd_mach_mips_loongson_2e;
6423 case E_MIPS_MACH_LS2F:
6424 return bfd_mach_mips_loongson_2f;
6426 case E_MIPS_MACH_LS3A:
6427 return bfd_mach_mips_loongson_3a;
6429 case E_MIPS_MACH_OCTEON2:
6430 return bfd_mach_mips_octeon2;
6432 case E_MIPS_MACH_OCTEON:
6433 return bfd_mach_mips_octeon;
6435 case E_MIPS_MACH_XLR:
6436 return bfd_mach_mips_xlr;
6439 switch (flags & EF_MIPS_ARCH)
6443 return bfd_mach_mips3000;
6446 return bfd_mach_mips6000;
6449 return bfd_mach_mips4000;
6452 return bfd_mach_mips8000;
6455 return bfd_mach_mips5;
6457 case E_MIPS_ARCH_32:
6458 return bfd_mach_mipsisa32;
6460 case E_MIPS_ARCH_64:
6461 return bfd_mach_mipsisa64;
6463 case E_MIPS_ARCH_32R2:
6464 return bfd_mach_mipsisa32r2;
6466 case E_MIPS_ARCH_64R2:
6467 return bfd_mach_mipsisa64r2;
6474 /* Return printable name for ABI. */
6476 static INLINE char *
6477 elf_mips_abi_name (bfd *abfd)
6481 flags = elf_elfheader (abfd)->e_flags;
6482 switch (flags & EF_MIPS_ABI)
6485 if (ABI_N32_P (abfd))
6487 else if (ABI_64_P (abfd))
6491 case E_MIPS_ABI_O32:
6493 case E_MIPS_ABI_O64:
6495 case E_MIPS_ABI_EABI32:
6497 case E_MIPS_ABI_EABI64:
6500 return "unknown abi";
6504 /* MIPS ELF uses two common sections. One is the usual one, and the
6505 other is for small objects. All the small objects are kept
6506 together, and then referenced via the gp pointer, which yields
6507 faster assembler code. This is what we use for the small common
6508 section. This approach is copied from ecoff.c. */
6509 static asection mips_elf_scom_section;
6510 static asymbol mips_elf_scom_symbol;
6511 static asymbol *mips_elf_scom_symbol_ptr;
6513 /* MIPS ELF also uses an acommon section, which represents an
6514 allocated common symbol which may be overridden by a
6515 definition in a shared library. */
6516 static asection mips_elf_acom_section;
6517 static asymbol mips_elf_acom_symbol;
6518 static asymbol *mips_elf_acom_symbol_ptr;
6520 /* This is used for both the 32-bit and the 64-bit ABI. */
6523 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6525 elf_symbol_type *elfsym;
6527 /* Handle the special MIPS section numbers that a symbol may use. */
6528 elfsym = (elf_symbol_type *) asym;
6529 switch (elfsym->internal_elf_sym.st_shndx)
6531 case SHN_MIPS_ACOMMON:
6532 /* This section is used in a dynamically linked executable file.
6533 It is an allocated common section. The dynamic linker can
6534 either resolve these symbols to something in a shared
6535 library, or it can just leave them here. For our purposes,
6536 we can consider these symbols to be in a new section. */
6537 if (mips_elf_acom_section.name == NULL)
6539 /* Initialize the acommon section. */
6540 mips_elf_acom_section.name = ".acommon";
6541 mips_elf_acom_section.flags = SEC_ALLOC;
6542 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6543 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6544 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6545 mips_elf_acom_symbol.name = ".acommon";
6546 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6547 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6548 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6550 asym->section = &mips_elf_acom_section;
6554 /* Common symbols less than the GP size are automatically
6555 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6556 if (asym->value > elf_gp_size (abfd)
6557 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6558 || IRIX_COMPAT (abfd) == ict_irix6)
6561 case SHN_MIPS_SCOMMON:
6562 if (mips_elf_scom_section.name == NULL)
6564 /* Initialize the small common section. */
6565 mips_elf_scom_section.name = ".scommon";
6566 mips_elf_scom_section.flags = SEC_IS_COMMON;
6567 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6568 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6569 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6570 mips_elf_scom_symbol.name = ".scommon";
6571 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6572 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6573 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6575 asym->section = &mips_elf_scom_section;
6576 asym->value = elfsym->internal_elf_sym.st_size;
6579 case SHN_MIPS_SUNDEFINED:
6580 asym->section = bfd_und_section_ptr;
6585 asection *section = bfd_get_section_by_name (abfd, ".text");
6587 if (section != NULL)
6589 asym->section = section;
6590 /* MIPS_TEXT is a bit special, the address is not an offset
6591 to the base of the .text section. So substract the section
6592 base address to make it an offset. */
6593 asym->value -= section->vma;
6600 asection *section = bfd_get_section_by_name (abfd, ".data");
6602 if (section != NULL)
6604 asym->section = section;
6605 /* MIPS_DATA is a bit special, the address is not an offset
6606 to the base of the .data section. So substract the section
6607 base address to make it an offset. */
6608 asym->value -= section->vma;
6614 /* If this is an odd-valued function symbol, assume it's a MIPS16
6615 or microMIPS one. */
6616 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6617 && (asym->value & 1) != 0)
6620 if (MICROMIPS_P (abfd))
6621 elfsym->internal_elf_sym.st_other
6622 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6624 elfsym->internal_elf_sym.st_other
6625 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6629 /* Implement elf_backend_eh_frame_address_size. This differs from
6630 the default in the way it handles EABI64.
6632 EABI64 was originally specified as an LP64 ABI, and that is what
6633 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6634 historically accepted the combination of -mabi=eabi and -mlong32,
6635 and this ILP32 variation has become semi-official over time.
6636 Both forms use elf32 and have pointer-sized FDE addresses.
6638 If an EABI object was generated by GCC 4.0 or above, it will have
6639 an empty .gcc_compiled_longXX section, where XX is the size of longs
6640 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6641 have no special marking to distinguish them from LP64 objects.
6643 We don't want users of the official LP64 ABI to be punished for the
6644 existence of the ILP32 variant, but at the same time, we don't want
6645 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6646 We therefore take the following approach:
6648 - If ABFD contains a .gcc_compiled_longXX section, use it to
6649 determine the pointer size.
6651 - Otherwise check the type of the first relocation. Assume that
6652 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6656 The second check is enough to detect LP64 objects generated by pre-4.0
6657 compilers because, in the kind of output generated by those compilers,
6658 the first relocation will be associated with either a CIE personality
6659 routine or an FDE start address. Furthermore, the compilers never
6660 used a special (non-pointer) encoding for this ABI.
6662 Checking the relocation type should also be safe because there is no
6663 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6667 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6669 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6671 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6673 bfd_boolean long32_p, long64_p;
6675 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6676 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6677 if (long32_p && long64_p)
6684 if (sec->reloc_count > 0
6685 && elf_section_data (sec)->relocs != NULL
6686 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6695 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6696 relocations against two unnamed section symbols to resolve to the
6697 same address. For example, if we have code like:
6699 lw $4,%got_disp(.data)($gp)
6700 lw $25,%got_disp(.text)($gp)
6703 then the linker will resolve both relocations to .data and the program
6704 will jump there rather than to .text.
6706 We can work around this problem by giving names to local section symbols.
6707 This is also what the MIPSpro tools do. */
6710 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6712 return SGI_COMPAT (abfd);
6715 /* Work over a section just before writing it out. This routine is
6716 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6717 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6721 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6723 if (hdr->sh_type == SHT_MIPS_REGINFO
6724 && hdr->sh_size > 0)
6728 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6729 BFD_ASSERT (hdr->contents == NULL);
6732 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6735 H_PUT_32 (abfd, elf_gp (abfd), buf);
6736 if (bfd_bwrite (buf, 4, abfd) != 4)
6740 if (hdr->sh_type == SHT_MIPS_OPTIONS
6741 && hdr->bfd_section != NULL
6742 && mips_elf_section_data (hdr->bfd_section) != NULL
6743 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6745 bfd_byte *contents, *l, *lend;
6747 /* We stored the section contents in the tdata field in the
6748 set_section_contents routine. We save the section contents
6749 so that we don't have to read them again.
6750 At this point we know that elf_gp is set, so we can look
6751 through the section contents to see if there is an
6752 ODK_REGINFO structure. */
6754 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6756 lend = contents + hdr->sh_size;
6757 while (l + sizeof (Elf_External_Options) <= lend)
6759 Elf_Internal_Options intopt;
6761 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6763 if (intopt.size < sizeof (Elf_External_Options))
6765 (*_bfd_error_handler)
6766 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6767 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6770 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6777 + sizeof (Elf_External_Options)
6778 + (sizeof (Elf64_External_RegInfo) - 8)),
6781 H_PUT_64 (abfd, elf_gp (abfd), buf);
6782 if (bfd_bwrite (buf, 8, abfd) != 8)
6785 else if (intopt.kind == ODK_REGINFO)
6792 + sizeof (Elf_External_Options)
6793 + (sizeof (Elf32_External_RegInfo) - 4)),
6796 H_PUT_32 (abfd, elf_gp (abfd), buf);
6797 if (bfd_bwrite (buf, 4, abfd) != 4)
6804 if (hdr->bfd_section != NULL)
6806 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6808 /* .sbss is not handled specially here because the GNU/Linux
6809 prelinker can convert .sbss from NOBITS to PROGBITS and
6810 changing it back to NOBITS breaks the binary. The entry in
6811 _bfd_mips_elf_special_sections will ensure the correct flags
6812 are set on .sbss if BFD creates it without reading it from an
6813 input file, and without special handling here the flags set
6814 on it in an input file will be followed. */
6815 if (strcmp (name, ".sdata") == 0
6816 || strcmp (name, ".lit8") == 0
6817 || strcmp (name, ".lit4") == 0)
6819 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6820 hdr->sh_type = SHT_PROGBITS;
6822 else if (strcmp (name, ".srdata") == 0)
6824 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6825 hdr->sh_type = SHT_PROGBITS;
6827 else if (strcmp (name, ".compact_rel") == 0)
6830 hdr->sh_type = SHT_PROGBITS;
6832 else if (strcmp (name, ".rtproc") == 0)
6834 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6836 unsigned int adjust;
6838 adjust = hdr->sh_size % hdr->sh_addralign;
6840 hdr->sh_size += hdr->sh_addralign - adjust;
6848 /* Handle a MIPS specific section when reading an object file. This
6849 is called when elfcode.h finds a section with an unknown type.
6850 This routine supports both the 32-bit and 64-bit ELF ABI.
6852 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6856 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6857 Elf_Internal_Shdr *hdr,
6863 /* There ought to be a place to keep ELF backend specific flags, but
6864 at the moment there isn't one. We just keep track of the
6865 sections by their name, instead. Fortunately, the ABI gives
6866 suggested names for all the MIPS specific sections, so we will
6867 probably get away with this. */
6868 switch (hdr->sh_type)
6870 case SHT_MIPS_LIBLIST:
6871 if (strcmp (name, ".liblist") != 0)
6875 if (strcmp (name, ".msym") != 0)
6878 case SHT_MIPS_CONFLICT:
6879 if (strcmp (name, ".conflict") != 0)
6882 case SHT_MIPS_GPTAB:
6883 if (! CONST_STRNEQ (name, ".gptab."))
6886 case SHT_MIPS_UCODE:
6887 if (strcmp (name, ".ucode") != 0)
6890 case SHT_MIPS_DEBUG:
6891 if (strcmp (name, ".mdebug") != 0)
6893 flags = SEC_DEBUGGING;
6895 case SHT_MIPS_REGINFO:
6896 if (strcmp (name, ".reginfo") != 0
6897 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6899 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6901 case SHT_MIPS_IFACE:
6902 if (strcmp (name, ".MIPS.interfaces") != 0)
6905 case SHT_MIPS_CONTENT:
6906 if (! CONST_STRNEQ (name, ".MIPS.content"))
6909 case SHT_MIPS_OPTIONS:
6910 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6913 case SHT_MIPS_DWARF:
6914 if (! CONST_STRNEQ (name, ".debug_")
6915 && ! CONST_STRNEQ (name, ".zdebug_"))
6918 case SHT_MIPS_SYMBOL_LIB:
6919 if (strcmp (name, ".MIPS.symlib") != 0)
6922 case SHT_MIPS_EVENTS:
6923 if (! CONST_STRNEQ (name, ".MIPS.events")
6924 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6931 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6936 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6937 (bfd_get_section_flags (abfd,
6943 /* FIXME: We should record sh_info for a .gptab section. */
6945 /* For a .reginfo section, set the gp value in the tdata information
6946 from the contents of this section. We need the gp value while
6947 processing relocs, so we just get it now. The .reginfo section
6948 is not used in the 64-bit MIPS ELF ABI. */
6949 if (hdr->sh_type == SHT_MIPS_REGINFO)
6951 Elf32_External_RegInfo ext;
6954 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6955 &ext, 0, sizeof ext))
6957 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6958 elf_gp (abfd) = s.ri_gp_value;
6961 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6962 set the gp value based on what we find. We may see both
6963 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6964 they should agree. */
6965 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6967 bfd_byte *contents, *l, *lend;
6969 contents = bfd_malloc (hdr->sh_size);
6970 if (contents == NULL)
6972 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6979 lend = contents + hdr->sh_size;
6980 while (l + sizeof (Elf_External_Options) <= lend)
6982 Elf_Internal_Options intopt;
6984 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6986 if (intopt.size < sizeof (Elf_External_Options))
6988 (*_bfd_error_handler)
6989 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6990 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6993 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6995 Elf64_Internal_RegInfo intreg;
6997 bfd_mips_elf64_swap_reginfo_in
6999 ((Elf64_External_RegInfo *)
7000 (l + sizeof (Elf_External_Options))),
7002 elf_gp (abfd) = intreg.ri_gp_value;
7004 else if (intopt.kind == ODK_REGINFO)
7006 Elf32_RegInfo intreg;
7008 bfd_mips_elf32_swap_reginfo_in
7010 ((Elf32_External_RegInfo *)
7011 (l + sizeof (Elf_External_Options))),
7013 elf_gp (abfd) = intreg.ri_gp_value;
7023 /* Set the correct type for a MIPS ELF section. We do this by the
7024 section name, which is a hack, but ought to work. This routine is
7025 used by both the 32-bit and the 64-bit ABI. */
7028 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7030 const char *name = bfd_get_section_name (abfd, sec);
7032 if (strcmp (name, ".liblist") == 0)
7034 hdr->sh_type = SHT_MIPS_LIBLIST;
7035 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7036 /* The sh_link field is set in final_write_processing. */
7038 else if (strcmp (name, ".conflict") == 0)
7039 hdr->sh_type = SHT_MIPS_CONFLICT;
7040 else if (CONST_STRNEQ (name, ".gptab."))
7042 hdr->sh_type = SHT_MIPS_GPTAB;
7043 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7044 /* The sh_info field is set in final_write_processing. */
7046 else if (strcmp (name, ".ucode") == 0)
7047 hdr->sh_type = SHT_MIPS_UCODE;
7048 else if (strcmp (name, ".mdebug") == 0)
7050 hdr->sh_type = SHT_MIPS_DEBUG;
7051 /* In a shared object on IRIX 5.3, the .mdebug section has an
7052 entsize of 0. FIXME: Does this matter? */
7053 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7054 hdr->sh_entsize = 0;
7056 hdr->sh_entsize = 1;
7058 else if (strcmp (name, ".reginfo") == 0)
7060 hdr->sh_type = SHT_MIPS_REGINFO;
7061 /* In a shared object on IRIX 5.3, the .reginfo section has an
7062 entsize of 0x18. FIXME: Does this matter? */
7063 if (SGI_COMPAT (abfd))
7065 if ((abfd->flags & DYNAMIC) != 0)
7066 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7068 hdr->sh_entsize = 1;
7071 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7073 else if (SGI_COMPAT (abfd)
7074 && (strcmp (name, ".hash") == 0
7075 || strcmp (name, ".dynamic") == 0
7076 || strcmp (name, ".dynstr") == 0))
7078 if (SGI_COMPAT (abfd))
7079 hdr->sh_entsize = 0;
7081 /* This isn't how the IRIX6 linker behaves. */
7082 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7085 else if (strcmp (name, ".got") == 0
7086 || strcmp (name, ".srdata") == 0
7087 || strcmp (name, ".sdata") == 0
7088 || strcmp (name, ".sbss") == 0
7089 || strcmp (name, ".lit4") == 0
7090 || strcmp (name, ".lit8") == 0)
7091 hdr->sh_flags |= SHF_MIPS_GPREL;
7092 else if (strcmp (name, ".MIPS.interfaces") == 0)
7094 hdr->sh_type = SHT_MIPS_IFACE;
7095 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7097 else if (CONST_STRNEQ (name, ".MIPS.content"))
7099 hdr->sh_type = SHT_MIPS_CONTENT;
7100 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7101 /* The sh_info field is set in final_write_processing. */
7103 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7105 hdr->sh_type = SHT_MIPS_OPTIONS;
7106 hdr->sh_entsize = 1;
7107 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7109 else if (CONST_STRNEQ (name, ".debug_")
7110 || CONST_STRNEQ (name, ".zdebug_"))
7112 hdr->sh_type = SHT_MIPS_DWARF;
7114 /* Irix facilities such as libexc expect a single .debug_frame
7115 per executable, the system ones have NOSTRIP set and the linker
7116 doesn't merge sections with different flags so ... */
7117 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7118 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7120 else if (strcmp (name, ".MIPS.symlib") == 0)
7122 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7123 /* The sh_link and sh_info fields are set in
7124 final_write_processing. */
7126 else if (CONST_STRNEQ (name, ".MIPS.events")
7127 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7129 hdr->sh_type = SHT_MIPS_EVENTS;
7130 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7131 /* The sh_link field is set in final_write_processing. */
7133 else if (strcmp (name, ".msym") == 0)
7135 hdr->sh_type = SHT_MIPS_MSYM;
7136 hdr->sh_flags |= SHF_ALLOC;
7137 hdr->sh_entsize = 8;
7140 /* The generic elf_fake_sections will set up REL_HDR using the default
7141 kind of relocations. We used to set up a second header for the
7142 non-default kind of relocations here, but only NewABI would use
7143 these, and the IRIX ld doesn't like resulting empty RELA sections.
7144 Thus we create those header only on demand now. */
7149 /* Given a BFD section, try to locate the corresponding ELF section
7150 index. This is used by both the 32-bit and the 64-bit ABI.
7151 Actually, it's not clear to me that the 64-bit ABI supports these,
7152 but for non-PIC objects we will certainly want support for at least
7153 the .scommon section. */
7156 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7157 asection *sec, int *retval)
7159 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7161 *retval = SHN_MIPS_SCOMMON;
7164 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7166 *retval = SHN_MIPS_ACOMMON;
7172 /* Hook called by the linker routine which adds symbols from an object
7173 file. We must handle the special MIPS section numbers here. */
7176 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7177 Elf_Internal_Sym *sym, const char **namep,
7178 flagword *flagsp ATTRIBUTE_UNUSED,
7179 asection **secp, bfd_vma *valp)
7181 if (SGI_COMPAT (abfd)
7182 && (abfd->flags & DYNAMIC) != 0
7183 && strcmp (*namep, "_rld_new_interface") == 0)
7185 /* Skip IRIX5 rld entry name. */
7190 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7191 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7192 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7193 a magic symbol resolved by the linker, we ignore this bogus definition
7194 of _gp_disp. New ABI objects do not suffer from this problem so this
7195 is not done for them. */
7197 && (sym->st_shndx == SHN_ABS)
7198 && (strcmp (*namep, "_gp_disp") == 0))
7204 switch (sym->st_shndx)
7207 /* Common symbols less than the GP size are automatically
7208 treated as SHN_MIPS_SCOMMON symbols. */
7209 if (sym->st_size > elf_gp_size (abfd)
7210 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7211 || IRIX_COMPAT (abfd) == ict_irix6)
7214 case SHN_MIPS_SCOMMON:
7215 *secp = bfd_make_section_old_way (abfd, ".scommon");
7216 (*secp)->flags |= SEC_IS_COMMON;
7217 *valp = sym->st_size;
7221 /* This section is used in a shared object. */
7222 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7224 asymbol *elf_text_symbol;
7225 asection *elf_text_section;
7226 bfd_size_type amt = sizeof (asection);
7228 elf_text_section = bfd_zalloc (abfd, amt);
7229 if (elf_text_section == NULL)
7232 amt = sizeof (asymbol);
7233 elf_text_symbol = bfd_zalloc (abfd, amt);
7234 if (elf_text_symbol == NULL)
7237 /* Initialize the section. */
7239 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7240 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7242 elf_text_section->symbol = elf_text_symbol;
7243 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7245 elf_text_section->name = ".text";
7246 elf_text_section->flags = SEC_NO_FLAGS;
7247 elf_text_section->output_section = NULL;
7248 elf_text_section->owner = abfd;
7249 elf_text_symbol->name = ".text";
7250 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7251 elf_text_symbol->section = elf_text_section;
7253 /* This code used to do *secp = bfd_und_section_ptr if
7254 info->shared. I don't know why, and that doesn't make sense,
7255 so I took it out. */
7256 *secp = mips_elf_tdata (abfd)->elf_text_section;
7259 case SHN_MIPS_ACOMMON:
7260 /* Fall through. XXX Can we treat this as allocated data? */
7262 /* This section is used in a shared object. */
7263 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7265 asymbol *elf_data_symbol;
7266 asection *elf_data_section;
7267 bfd_size_type amt = sizeof (asection);
7269 elf_data_section = bfd_zalloc (abfd, amt);
7270 if (elf_data_section == NULL)
7273 amt = sizeof (asymbol);
7274 elf_data_symbol = bfd_zalloc (abfd, amt);
7275 if (elf_data_symbol == NULL)
7278 /* Initialize the section. */
7280 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7281 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7283 elf_data_section->symbol = elf_data_symbol;
7284 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7286 elf_data_section->name = ".data";
7287 elf_data_section->flags = SEC_NO_FLAGS;
7288 elf_data_section->output_section = NULL;
7289 elf_data_section->owner = abfd;
7290 elf_data_symbol->name = ".data";
7291 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7292 elf_data_symbol->section = elf_data_section;
7294 /* This code used to do *secp = bfd_und_section_ptr if
7295 info->shared. I don't know why, and that doesn't make sense,
7296 so I took it out. */
7297 *secp = mips_elf_tdata (abfd)->elf_data_section;
7300 case SHN_MIPS_SUNDEFINED:
7301 *secp = bfd_und_section_ptr;
7305 if (SGI_COMPAT (abfd)
7307 && info->output_bfd->xvec == abfd->xvec
7308 && strcmp (*namep, "__rld_obj_head") == 0)
7310 struct elf_link_hash_entry *h;
7311 struct bfd_link_hash_entry *bh;
7313 /* Mark __rld_obj_head as dynamic. */
7315 if (! (_bfd_generic_link_add_one_symbol
7316 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7317 get_elf_backend_data (abfd)->collect, &bh)))
7320 h = (struct elf_link_hash_entry *) bh;
7323 h->type = STT_OBJECT;
7325 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7328 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7329 mips_elf_hash_table (info)->rld_symbol = h;
7332 /* If this is a mips16 text symbol, add 1 to the value to make it
7333 odd. This will cause something like .word SYM to come up with
7334 the right value when it is loaded into the PC. */
7335 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7341 /* This hook function is called before the linker writes out a global
7342 symbol. We mark symbols as small common if appropriate. This is
7343 also where we undo the increment of the value for a mips16 symbol. */
7346 _bfd_mips_elf_link_output_symbol_hook
7347 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7348 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7349 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7351 /* If we see a common symbol, which implies a relocatable link, then
7352 if a symbol was small common in an input file, mark it as small
7353 common in the output file. */
7354 if (sym->st_shndx == SHN_COMMON
7355 && strcmp (input_sec->name, ".scommon") == 0)
7356 sym->st_shndx = SHN_MIPS_SCOMMON;
7358 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7359 sym->st_value &= ~1;
7364 /* Functions for the dynamic linker. */
7366 /* Create dynamic sections when linking against a dynamic object. */
7369 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7371 struct elf_link_hash_entry *h;
7372 struct bfd_link_hash_entry *bh;
7374 register asection *s;
7375 const char * const *namep;
7376 struct mips_elf_link_hash_table *htab;
7378 htab = mips_elf_hash_table (info);
7379 BFD_ASSERT (htab != NULL);
7381 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7382 | SEC_LINKER_CREATED | SEC_READONLY);
7384 /* The psABI requires a read-only .dynamic section, but the VxWorks
7386 if (!htab->is_vxworks)
7388 s = bfd_get_linker_section (abfd, ".dynamic");
7391 if (! bfd_set_section_flags (abfd, s, flags))
7396 /* We need to create .got section. */
7397 if (!mips_elf_create_got_section (abfd, info))
7400 if (! mips_elf_rel_dyn_section (info, TRUE))
7403 /* Create .stub section. */
7404 s = bfd_make_section_anyway_with_flags (abfd,
7405 MIPS_ELF_STUB_SECTION_NAME (abfd),
7408 || ! bfd_set_section_alignment (abfd, s,
7409 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7413 if (!mips_elf_hash_table (info)->use_rld_obj_head
7415 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7417 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7418 flags &~ (flagword) SEC_READONLY);
7420 || ! bfd_set_section_alignment (abfd, s,
7421 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7425 /* On IRIX5, we adjust add some additional symbols and change the
7426 alignments of several sections. There is no ABI documentation
7427 indicating that this is necessary on IRIX6, nor any evidence that
7428 the linker takes such action. */
7429 if (IRIX_COMPAT (abfd) == ict_irix5)
7431 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7434 if (! (_bfd_generic_link_add_one_symbol
7435 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7436 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7439 h = (struct elf_link_hash_entry *) bh;
7442 h->type = STT_SECTION;
7444 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7448 /* We need to create a .compact_rel section. */
7449 if (SGI_COMPAT (abfd))
7451 if (!mips_elf_create_compact_rel_section (abfd, info))
7455 /* Change alignments of some sections. */
7456 s = bfd_get_linker_section (abfd, ".hash");
7458 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7460 s = bfd_get_linker_section (abfd, ".dynsym");
7462 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7464 s = bfd_get_linker_section (abfd, ".dynstr");
7466 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7469 s = bfd_get_section_by_name (abfd, ".reginfo");
7471 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7473 s = bfd_get_linker_section (abfd, ".dynamic");
7475 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7482 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7484 if (!(_bfd_generic_link_add_one_symbol
7485 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7486 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7489 h = (struct elf_link_hash_entry *) bh;
7492 h->type = STT_SECTION;
7494 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7497 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7499 /* __rld_map is a four byte word located in the .data section
7500 and is filled in by the rtld to contain a pointer to
7501 the _r_debug structure. Its symbol value will be set in
7502 _bfd_mips_elf_finish_dynamic_symbol. */
7503 s = bfd_get_linker_section (abfd, ".rld_map");
7504 BFD_ASSERT (s != NULL);
7506 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7508 if (!(_bfd_generic_link_add_one_symbol
7509 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7510 get_elf_backend_data (abfd)->collect, &bh)))
7513 h = (struct elf_link_hash_entry *) bh;
7516 h->type = STT_OBJECT;
7518 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7520 mips_elf_hash_table (info)->rld_symbol = h;
7524 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7525 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
7526 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7529 /* Cache the sections created above. */
7530 htab->splt = bfd_get_linker_section (abfd, ".plt");
7531 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7532 if (htab->is_vxworks)
7534 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7535 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7538 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7540 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7545 /* Do the usual VxWorks handling. */
7546 if (htab->is_vxworks
7547 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7553 /* Return true if relocation REL against section SEC is a REL rather than
7554 RELA relocation. RELOCS is the first relocation in the section and
7555 ABFD is the bfd that contains SEC. */
7558 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7559 const Elf_Internal_Rela *relocs,
7560 const Elf_Internal_Rela *rel)
7562 Elf_Internal_Shdr *rel_hdr;
7563 const struct elf_backend_data *bed;
7565 /* To determine which flavor of relocation this is, we depend on the
7566 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7567 rel_hdr = elf_section_data (sec)->rel.hdr;
7568 if (rel_hdr == NULL)
7570 bed = get_elf_backend_data (abfd);
7571 return ((size_t) (rel - relocs)
7572 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7575 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7576 HOWTO is the relocation's howto and CONTENTS points to the contents
7577 of the section that REL is against. */
7580 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7581 reloc_howto_type *howto, bfd_byte *contents)
7584 unsigned int r_type;
7587 r_type = ELF_R_TYPE (abfd, rel->r_info);
7588 location = contents + rel->r_offset;
7590 /* Get the addend, which is stored in the input file. */
7591 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7592 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7593 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7595 return addend & howto->src_mask;
7598 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7599 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7600 and update *ADDEND with the final addend. Return true on success
7601 or false if the LO16 could not be found. RELEND is the exclusive
7602 upper bound on the relocations for REL's section. */
7605 mips_elf_add_lo16_rel_addend (bfd *abfd,
7606 const Elf_Internal_Rela *rel,
7607 const Elf_Internal_Rela *relend,
7608 bfd_byte *contents, bfd_vma *addend)
7610 unsigned int r_type, lo16_type;
7611 const Elf_Internal_Rela *lo16_relocation;
7612 reloc_howto_type *lo16_howto;
7615 r_type = ELF_R_TYPE (abfd, rel->r_info);
7616 if (mips16_reloc_p (r_type))
7617 lo16_type = R_MIPS16_LO16;
7618 else if (micromips_reloc_p (r_type))
7619 lo16_type = R_MICROMIPS_LO16;
7621 lo16_type = R_MIPS_LO16;
7623 /* The combined value is the sum of the HI16 addend, left-shifted by
7624 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7625 code does a `lui' of the HI16 value, and then an `addiu' of the
7628 Scan ahead to find a matching LO16 relocation.
7630 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7631 be immediately following. However, for the IRIX6 ABI, the next
7632 relocation may be a composed relocation consisting of several
7633 relocations for the same address. In that case, the R_MIPS_LO16
7634 relocation may occur as one of these. We permit a similar
7635 extension in general, as that is useful for GCC.
7637 In some cases GCC dead code elimination removes the LO16 but keeps
7638 the corresponding HI16. This is strictly speaking a violation of
7639 the ABI but not immediately harmful. */
7640 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7641 if (lo16_relocation == NULL)
7644 /* Obtain the addend kept there. */
7645 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7646 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7648 l <<= lo16_howto->rightshift;
7649 l = _bfd_mips_elf_sign_extend (l, 16);
7656 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7657 store the contents in *CONTENTS on success. Assume that *CONTENTS
7658 already holds the contents if it is nonull on entry. */
7661 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7666 /* Get cached copy if it exists. */
7667 if (elf_section_data (sec)->this_hdr.contents != NULL)
7669 *contents = elf_section_data (sec)->this_hdr.contents;
7673 return bfd_malloc_and_get_section (abfd, sec, contents);
7676 /* Make a new PLT record to keep internal data. */
7678 static struct plt_entry *
7679 mips_elf_make_plt_record (bfd *abfd)
7681 struct plt_entry *entry;
7683 entry = bfd_zalloc (abfd, sizeof (*entry));
7687 entry->stub_offset = MINUS_ONE;
7688 entry->mips_offset = MINUS_ONE;
7689 entry->comp_offset = MINUS_ONE;
7690 entry->gotplt_index = MINUS_ONE;
7694 /* Look through the relocs for a section during the first phase, and
7695 allocate space in the global offset table and record the need for
7696 standard MIPS and compressed procedure linkage table entries. */
7699 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7700 asection *sec, const Elf_Internal_Rela *relocs)
7704 Elf_Internal_Shdr *symtab_hdr;
7705 struct elf_link_hash_entry **sym_hashes;
7707 const Elf_Internal_Rela *rel;
7708 const Elf_Internal_Rela *rel_end;
7710 const struct elf_backend_data *bed;
7711 struct mips_elf_link_hash_table *htab;
7714 reloc_howto_type *howto;
7716 if (info->relocatable)
7719 htab = mips_elf_hash_table (info);
7720 BFD_ASSERT (htab != NULL);
7722 dynobj = elf_hash_table (info)->dynobj;
7723 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7724 sym_hashes = elf_sym_hashes (abfd);
7725 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7727 bed = get_elf_backend_data (abfd);
7728 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7730 /* Check for the mips16 stub sections. */
7732 name = bfd_get_section_name (abfd, sec);
7733 if (FN_STUB_P (name))
7735 unsigned long r_symndx;
7737 /* Look at the relocation information to figure out which symbol
7740 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7743 (*_bfd_error_handler)
7744 (_("%B: Warning: cannot determine the target function for"
7745 " stub section `%s'"),
7747 bfd_set_error (bfd_error_bad_value);
7751 if (r_symndx < extsymoff
7752 || sym_hashes[r_symndx - extsymoff] == NULL)
7756 /* This stub is for a local symbol. This stub will only be
7757 needed if there is some relocation in this BFD, other
7758 than a 16 bit function call, which refers to this symbol. */
7759 for (o = abfd->sections; o != NULL; o = o->next)
7761 Elf_Internal_Rela *sec_relocs;
7762 const Elf_Internal_Rela *r, *rend;
7764 /* We can ignore stub sections when looking for relocs. */
7765 if ((o->flags & SEC_RELOC) == 0
7766 || o->reloc_count == 0
7767 || section_allows_mips16_refs_p (o))
7771 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7773 if (sec_relocs == NULL)
7776 rend = sec_relocs + o->reloc_count;
7777 for (r = sec_relocs; r < rend; r++)
7778 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7779 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7782 if (elf_section_data (o)->relocs != sec_relocs)
7791 /* There is no non-call reloc for this stub, so we do
7792 not need it. Since this function is called before
7793 the linker maps input sections to output sections, we
7794 can easily discard it by setting the SEC_EXCLUDE
7796 sec->flags |= SEC_EXCLUDE;
7800 /* Record this stub in an array of local symbol stubs for
7802 if (mips_elf_tdata (abfd)->local_stubs == NULL)
7804 unsigned long symcount;
7808 if (elf_bad_symtab (abfd))
7809 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7811 symcount = symtab_hdr->sh_info;
7812 amt = symcount * sizeof (asection *);
7813 n = bfd_zalloc (abfd, amt);
7816 mips_elf_tdata (abfd)->local_stubs = n;
7819 sec->flags |= SEC_KEEP;
7820 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7822 /* We don't need to set mips16_stubs_seen in this case.
7823 That flag is used to see whether we need to look through
7824 the global symbol table for stubs. We don't need to set
7825 it here, because we just have a local stub. */
7829 struct mips_elf_link_hash_entry *h;
7831 h = ((struct mips_elf_link_hash_entry *)
7832 sym_hashes[r_symndx - extsymoff]);
7834 while (h->root.root.type == bfd_link_hash_indirect
7835 || h->root.root.type == bfd_link_hash_warning)
7836 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7838 /* H is the symbol this stub is for. */
7840 /* If we already have an appropriate stub for this function, we
7841 don't need another one, so we can discard this one. Since
7842 this function is called before the linker maps input sections
7843 to output sections, we can easily discard it by setting the
7844 SEC_EXCLUDE flag. */
7845 if (h->fn_stub != NULL)
7847 sec->flags |= SEC_EXCLUDE;
7851 sec->flags |= SEC_KEEP;
7853 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7856 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7858 unsigned long r_symndx;
7859 struct mips_elf_link_hash_entry *h;
7862 /* Look at the relocation information to figure out which symbol
7865 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7868 (*_bfd_error_handler)
7869 (_("%B: Warning: cannot determine the target function for"
7870 " stub section `%s'"),
7872 bfd_set_error (bfd_error_bad_value);
7876 if (r_symndx < extsymoff
7877 || sym_hashes[r_symndx - extsymoff] == NULL)
7881 /* This stub is for a local symbol. This stub will only be
7882 needed if there is some relocation (R_MIPS16_26) in this BFD
7883 that refers to this symbol. */
7884 for (o = abfd->sections; o != NULL; o = o->next)
7886 Elf_Internal_Rela *sec_relocs;
7887 const Elf_Internal_Rela *r, *rend;
7889 /* We can ignore stub sections when looking for relocs. */
7890 if ((o->flags & SEC_RELOC) == 0
7891 || o->reloc_count == 0
7892 || section_allows_mips16_refs_p (o))
7896 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7898 if (sec_relocs == NULL)
7901 rend = sec_relocs + o->reloc_count;
7902 for (r = sec_relocs; r < rend; r++)
7903 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7904 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7907 if (elf_section_data (o)->relocs != sec_relocs)
7916 /* There is no non-call reloc for this stub, so we do
7917 not need it. Since this function is called before
7918 the linker maps input sections to output sections, we
7919 can easily discard it by setting the SEC_EXCLUDE
7921 sec->flags |= SEC_EXCLUDE;
7925 /* Record this stub in an array of local symbol call_stubs for
7927 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
7929 unsigned long symcount;
7933 if (elf_bad_symtab (abfd))
7934 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7936 symcount = symtab_hdr->sh_info;
7937 amt = symcount * sizeof (asection *);
7938 n = bfd_zalloc (abfd, amt);
7941 mips_elf_tdata (abfd)->local_call_stubs = n;
7944 sec->flags |= SEC_KEEP;
7945 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7947 /* We don't need to set mips16_stubs_seen in this case.
7948 That flag is used to see whether we need to look through
7949 the global symbol table for stubs. We don't need to set
7950 it here, because we just have a local stub. */
7954 h = ((struct mips_elf_link_hash_entry *)
7955 sym_hashes[r_symndx - extsymoff]);
7957 /* H is the symbol this stub is for. */
7959 if (CALL_FP_STUB_P (name))
7960 loc = &h->call_fp_stub;
7962 loc = &h->call_stub;
7964 /* If we already have an appropriate stub for this function, we
7965 don't need another one, so we can discard this one. Since
7966 this function is called before the linker maps input sections
7967 to output sections, we can easily discard it by setting the
7968 SEC_EXCLUDE flag. */
7971 sec->flags |= SEC_EXCLUDE;
7975 sec->flags |= SEC_KEEP;
7977 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7983 for (rel = relocs; rel < rel_end; ++rel)
7985 unsigned long r_symndx;
7986 unsigned int r_type;
7987 struct elf_link_hash_entry *h;
7988 bfd_boolean can_make_dynamic_p;
7989 bfd_boolean call_reloc_p;
7990 bfd_boolean constrain_symbol_p;
7992 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7993 r_type = ELF_R_TYPE (abfd, rel->r_info);
7995 if (r_symndx < extsymoff)
7997 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7999 (*_bfd_error_handler)
8000 (_("%B: Malformed reloc detected for section %s"),
8002 bfd_set_error (bfd_error_bad_value);
8007 h = sym_hashes[r_symndx - extsymoff];
8010 while (h->root.type == bfd_link_hash_indirect
8011 || h->root.type == bfd_link_hash_warning)
8012 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8014 /* PR15323, ref flags aren't set for references in the
8016 h->root.non_ir_ref = 1;
8020 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8021 relocation into a dynamic one. */
8022 can_make_dynamic_p = FALSE;
8024 /* Set CALL_RELOC_P to true if the relocation is for a call,
8025 and if pointer equality therefore doesn't matter. */
8026 call_reloc_p = FALSE;
8028 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8029 into account when deciding how to define the symbol.
8030 Relocations in nonallocatable sections such as .pdr and
8031 .debug* should have no effect. */
8032 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8037 case R_MIPS_CALL_HI16:
8038 case R_MIPS_CALL_LO16:
8039 case R_MIPS16_CALL16:
8040 case R_MICROMIPS_CALL16:
8041 case R_MICROMIPS_CALL_HI16:
8042 case R_MICROMIPS_CALL_LO16:
8043 call_reloc_p = TRUE;
8047 case R_MIPS_GOT_HI16:
8048 case R_MIPS_GOT_LO16:
8049 case R_MIPS_GOT_PAGE:
8050 case R_MIPS_GOT_OFST:
8051 case R_MIPS_GOT_DISP:
8052 case R_MIPS_TLS_GOTTPREL:
8054 case R_MIPS_TLS_LDM:
8055 case R_MIPS16_GOT16:
8056 case R_MIPS16_TLS_GOTTPREL:
8057 case R_MIPS16_TLS_GD:
8058 case R_MIPS16_TLS_LDM:
8059 case R_MICROMIPS_GOT16:
8060 case R_MICROMIPS_GOT_HI16:
8061 case R_MICROMIPS_GOT_LO16:
8062 case R_MICROMIPS_GOT_PAGE:
8063 case R_MICROMIPS_GOT_OFST:
8064 case R_MICROMIPS_GOT_DISP:
8065 case R_MICROMIPS_TLS_GOTTPREL:
8066 case R_MICROMIPS_TLS_GD:
8067 case R_MICROMIPS_TLS_LDM:
8069 elf_hash_table (info)->dynobj = dynobj = abfd;
8070 if (!mips_elf_create_got_section (dynobj, info))
8072 if (htab->is_vxworks && !info->shared)
8074 (*_bfd_error_handler)
8075 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8076 abfd, (unsigned long) rel->r_offset);
8077 bfd_set_error (bfd_error_bad_value);
8080 can_make_dynamic_p = TRUE;
8085 case R_MICROMIPS_JALR:
8086 /* These relocations have empty fields and are purely there to
8087 provide link information. The symbol value doesn't matter. */
8088 constrain_symbol_p = FALSE;
8091 case R_MIPS_GPREL16:
8092 case R_MIPS_GPREL32:
8093 case R_MIPS16_GPREL:
8094 case R_MICROMIPS_GPREL16:
8095 /* GP-relative relocations always resolve to a definition in a
8096 regular input file, ignoring the one-definition rule. This is
8097 important for the GP setup sequence in NewABI code, which
8098 always resolves to a local function even if other relocations
8099 against the symbol wouldn't. */
8100 constrain_symbol_p = FALSE;
8106 /* In VxWorks executables, references to external symbols
8107 must be handled using copy relocs or PLT entries; it is not
8108 possible to convert this relocation into a dynamic one.
8110 For executables that use PLTs and copy-relocs, we have a
8111 choice between converting the relocation into a dynamic
8112 one or using copy relocations or PLT entries. It is
8113 usually better to do the former, unless the relocation is
8114 against a read-only section. */
8117 && !htab->is_vxworks
8118 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8119 && !(!info->nocopyreloc
8120 && !PIC_OBJECT_P (abfd)
8121 && MIPS_ELF_READONLY_SECTION (sec))))
8122 && (sec->flags & SEC_ALLOC) != 0)
8124 can_make_dynamic_p = TRUE;
8126 elf_hash_table (info)->dynobj = dynobj = abfd;
8133 case R_MICROMIPS_26_S1:
8134 case R_MICROMIPS_PC7_S1:
8135 case R_MICROMIPS_PC10_S1:
8136 case R_MICROMIPS_PC16_S1:
8137 case R_MICROMIPS_PC23_S2:
8138 call_reloc_p = TRUE;
8144 if (constrain_symbol_p)
8146 if (!can_make_dynamic_p)
8147 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8150 h->pointer_equality_needed = 1;
8152 /* We must not create a stub for a symbol that has
8153 relocations related to taking the function's address.
8154 This doesn't apply to VxWorks, where CALL relocs refer
8155 to a .got.plt entry instead of a normal .got entry. */
8156 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8157 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8160 /* Relocations against the special VxWorks __GOTT_BASE__ and
8161 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8162 room for them in .rela.dyn. */
8163 if (is_gott_symbol (info, h))
8167 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8171 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8172 if (MIPS_ELF_READONLY_SECTION (sec))
8173 /* We tell the dynamic linker that there are
8174 relocations against the text segment. */
8175 info->flags |= DF_TEXTREL;
8178 else if (call_lo16_reloc_p (r_type)
8179 || got_lo16_reloc_p (r_type)
8180 || got_disp_reloc_p (r_type)
8181 || (got16_reloc_p (r_type) && htab->is_vxworks))
8183 /* We may need a local GOT entry for this relocation. We
8184 don't count R_MIPS_GOT_PAGE because we can estimate the
8185 maximum number of pages needed by looking at the size of
8186 the segment. Similar comments apply to R_MIPS*_GOT16 and
8187 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8188 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8189 R_MIPS_CALL_HI16 because these are always followed by an
8190 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8191 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8192 rel->r_addend, info, r_type))
8197 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8198 ELF_ST_IS_MIPS16 (h->other)))
8199 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8204 case R_MIPS16_CALL16:
8205 case R_MICROMIPS_CALL16:
8208 (*_bfd_error_handler)
8209 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8210 abfd, (unsigned long) rel->r_offset);
8211 bfd_set_error (bfd_error_bad_value);
8216 case R_MIPS_CALL_HI16:
8217 case R_MIPS_CALL_LO16:
8218 case R_MICROMIPS_CALL_HI16:
8219 case R_MICROMIPS_CALL_LO16:
8222 /* Make sure there is room in the regular GOT to hold the
8223 function's address. We may eliminate it in favour of
8224 a .got.plt entry later; see mips_elf_count_got_symbols. */
8225 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8229 /* We need a stub, not a plt entry for the undefined
8230 function. But we record it as if it needs plt. See
8231 _bfd_elf_adjust_dynamic_symbol. */
8237 case R_MIPS_GOT_PAGE:
8238 case R_MICROMIPS_GOT_PAGE:
8239 case R_MIPS16_GOT16:
8241 case R_MIPS_GOT_HI16:
8242 case R_MIPS_GOT_LO16:
8243 case R_MICROMIPS_GOT16:
8244 case R_MICROMIPS_GOT_HI16:
8245 case R_MICROMIPS_GOT_LO16:
8246 if (!h || got_page_reloc_p (r_type))
8248 /* This relocation needs (or may need, if h != NULL) a
8249 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8250 know for sure until we know whether the symbol is
8252 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8254 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8256 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8257 addend = mips_elf_read_rel_addend (abfd, rel,
8259 if (got16_reloc_p (r_type))
8260 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8263 addend <<= howto->rightshift;
8266 addend = rel->r_addend;
8267 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8273 struct mips_elf_link_hash_entry *hmips =
8274 (struct mips_elf_link_hash_entry *) h;
8276 /* This symbol is definitely not overridable. */
8277 if (hmips->root.def_regular
8278 && ! (info->shared && ! info->symbolic
8279 && ! hmips->root.forced_local))
8283 /* If this is a global, overridable symbol, GOT_PAGE will
8284 decay to GOT_DISP, so we'll need a GOT entry for it. */
8287 case R_MIPS_GOT_DISP:
8288 case R_MICROMIPS_GOT_DISP:
8289 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8294 case R_MIPS_TLS_GOTTPREL:
8295 case R_MIPS16_TLS_GOTTPREL:
8296 case R_MICROMIPS_TLS_GOTTPREL:
8298 info->flags |= DF_STATIC_TLS;
8301 case R_MIPS_TLS_LDM:
8302 case R_MIPS16_TLS_LDM:
8303 case R_MICROMIPS_TLS_LDM:
8304 if (tls_ldm_reloc_p (r_type))
8306 r_symndx = STN_UNDEF;
8312 case R_MIPS16_TLS_GD:
8313 case R_MICROMIPS_TLS_GD:
8314 /* This symbol requires a global offset table entry, or two
8315 for TLS GD relocations. */
8318 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8324 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8334 /* In VxWorks executables, references to external symbols
8335 are handled using copy relocs or PLT stubs, so there's
8336 no need to add a .rela.dyn entry for this relocation. */
8337 if (can_make_dynamic_p)
8341 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8345 if (info->shared && h == NULL)
8347 /* When creating a shared object, we must copy these
8348 reloc types into the output file as R_MIPS_REL32
8349 relocs. Make room for this reloc in .rel(a).dyn. */
8350 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8351 if (MIPS_ELF_READONLY_SECTION (sec))
8352 /* We tell the dynamic linker that there are
8353 relocations against the text segment. */
8354 info->flags |= DF_TEXTREL;
8358 struct mips_elf_link_hash_entry *hmips;
8360 /* For a shared object, we must copy this relocation
8361 unless the symbol turns out to be undefined and
8362 weak with non-default visibility, in which case
8363 it will be left as zero.
8365 We could elide R_MIPS_REL32 for locally binding symbols
8366 in shared libraries, but do not yet do so.
8368 For an executable, we only need to copy this
8369 reloc if the symbol is defined in a dynamic
8371 hmips = (struct mips_elf_link_hash_entry *) h;
8372 ++hmips->possibly_dynamic_relocs;
8373 if (MIPS_ELF_READONLY_SECTION (sec))
8374 /* We need it to tell the dynamic linker if there
8375 are relocations against the text segment. */
8376 hmips->readonly_reloc = TRUE;
8380 if (SGI_COMPAT (abfd))
8381 mips_elf_hash_table (info)->compact_rel_size +=
8382 sizeof (Elf32_External_crinfo);
8386 case R_MIPS_GPREL16:
8387 case R_MIPS_LITERAL:
8388 case R_MIPS_GPREL32:
8389 case R_MICROMIPS_26_S1:
8390 case R_MICROMIPS_GPREL16:
8391 case R_MICROMIPS_LITERAL:
8392 case R_MICROMIPS_GPREL7_S2:
8393 if (SGI_COMPAT (abfd))
8394 mips_elf_hash_table (info)->compact_rel_size +=
8395 sizeof (Elf32_External_crinfo);
8398 /* This relocation describes the C++ object vtable hierarchy.
8399 Reconstruct it for later use during GC. */
8400 case R_MIPS_GNU_VTINHERIT:
8401 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8405 /* This relocation describes which C++ vtable entries are actually
8406 used. Record for later use during GC. */
8407 case R_MIPS_GNU_VTENTRY:
8408 BFD_ASSERT (h != NULL);
8410 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8418 /* Record the need for a PLT entry. At this point we don't know
8419 yet if we are going to create a PLT in the first place, but
8420 we only record whether the relocation requires a standard MIPS
8421 or a compressed code entry anyway. If we don't make a PLT after
8422 all, then we'll just ignore these arrangements. Likewise if
8423 a PLT entry is not created because the symbol is satisfied
8426 && jal_reloc_p (r_type)
8427 && !SYMBOL_CALLS_LOCAL (info, h))
8429 if (h->plt.plist == NULL)
8430 h->plt.plist = mips_elf_make_plt_record (abfd);
8431 if (h->plt.plist == NULL)
8434 if (r_type == R_MIPS_26)
8435 h->plt.plist->need_mips = TRUE;
8437 h->plt.plist->need_comp = TRUE;
8440 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8441 if there is one. We only need to handle global symbols here;
8442 we decide whether to keep or delete stubs for local symbols
8443 when processing the stub's relocations. */
8445 && !mips16_call_reloc_p (r_type)
8446 && !section_allows_mips16_refs_p (sec))
8448 struct mips_elf_link_hash_entry *mh;
8450 mh = (struct mips_elf_link_hash_entry *) h;
8451 mh->need_fn_stub = TRUE;
8454 /* Refuse some position-dependent relocations when creating a
8455 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8456 not PIC, but we can create dynamic relocations and the result
8457 will be fine. Also do not refuse R_MIPS_LO16, which can be
8458 combined with R_MIPS_GOT16. */
8466 case R_MIPS_HIGHEST:
8467 case R_MICROMIPS_HI16:
8468 case R_MICROMIPS_HIGHER:
8469 case R_MICROMIPS_HIGHEST:
8470 /* Don't refuse a high part relocation if it's against
8471 no symbol (e.g. part of a compound relocation). */
8472 if (r_symndx == STN_UNDEF)
8475 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8476 and has a special meaning. */
8477 if (!NEWABI_P (abfd) && h != NULL
8478 && strcmp (h->root.root.string, "_gp_disp") == 0)
8481 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8482 if (is_gott_symbol (info, h))
8489 case R_MICROMIPS_26_S1:
8490 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8491 (*_bfd_error_handler)
8492 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8494 (h) ? h->root.root.string : "a local symbol");
8495 bfd_set_error (bfd_error_bad_value);
8507 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8508 struct bfd_link_info *link_info,
8511 Elf_Internal_Rela *internal_relocs;
8512 Elf_Internal_Rela *irel, *irelend;
8513 Elf_Internal_Shdr *symtab_hdr;
8514 bfd_byte *contents = NULL;
8516 bfd_boolean changed_contents = FALSE;
8517 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8518 Elf_Internal_Sym *isymbuf = NULL;
8520 /* We are not currently changing any sizes, so only one pass. */
8523 if (link_info->relocatable)
8526 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8527 link_info->keep_memory);
8528 if (internal_relocs == NULL)
8531 irelend = internal_relocs + sec->reloc_count
8532 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8533 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8534 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8536 for (irel = internal_relocs; irel < irelend; irel++)
8539 bfd_signed_vma sym_offset;
8540 unsigned int r_type;
8541 unsigned long r_symndx;
8543 unsigned long instruction;
8545 /* Turn jalr into bgezal, and jr into beq, if they're marked
8546 with a JALR relocation, that indicate where they jump to.
8547 This saves some pipeline bubbles. */
8548 r_type = ELF_R_TYPE (abfd, irel->r_info);
8549 if (r_type != R_MIPS_JALR)
8552 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8553 /* Compute the address of the jump target. */
8554 if (r_symndx >= extsymoff)
8556 struct mips_elf_link_hash_entry *h
8557 = ((struct mips_elf_link_hash_entry *)
8558 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8560 while (h->root.root.type == bfd_link_hash_indirect
8561 || h->root.root.type == bfd_link_hash_warning)
8562 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8564 /* If a symbol is undefined, or if it may be overridden,
8566 if (! ((h->root.root.type == bfd_link_hash_defined
8567 || h->root.root.type == bfd_link_hash_defweak)
8568 && h->root.root.u.def.section)
8569 || (link_info->shared && ! link_info->symbolic
8570 && !h->root.forced_local))
8573 sym_sec = h->root.root.u.def.section;
8574 if (sym_sec->output_section)
8575 symval = (h->root.root.u.def.value
8576 + sym_sec->output_section->vma
8577 + sym_sec->output_offset);
8579 symval = h->root.root.u.def.value;
8583 Elf_Internal_Sym *isym;
8585 /* Read this BFD's symbols if we haven't done so already. */
8586 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8588 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8589 if (isymbuf == NULL)
8590 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8591 symtab_hdr->sh_info, 0,
8593 if (isymbuf == NULL)
8597 isym = isymbuf + r_symndx;
8598 if (isym->st_shndx == SHN_UNDEF)
8600 else if (isym->st_shndx == SHN_ABS)
8601 sym_sec = bfd_abs_section_ptr;
8602 else if (isym->st_shndx == SHN_COMMON)
8603 sym_sec = bfd_com_section_ptr;
8606 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8607 symval = isym->st_value
8608 + sym_sec->output_section->vma
8609 + sym_sec->output_offset;
8612 /* Compute branch offset, from delay slot of the jump to the
8614 sym_offset = (symval + irel->r_addend)
8615 - (sec_start + irel->r_offset + 4);
8617 /* Branch offset must be properly aligned. */
8618 if ((sym_offset & 3) != 0)
8623 /* Check that it's in range. */
8624 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8627 /* Get the section contents if we haven't done so already. */
8628 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8631 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8633 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8634 if ((instruction & 0xfc1fffff) == 0x0000f809)
8635 instruction = 0x04110000;
8636 /* If it was jr <reg>, turn it into b <target>. */
8637 else if ((instruction & 0xfc1fffff) == 0x00000008)
8638 instruction = 0x10000000;
8642 instruction |= (sym_offset & 0xffff);
8643 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8644 changed_contents = TRUE;
8647 if (contents != NULL
8648 && elf_section_data (sec)->this_hdr.contents != contents)
8650 if (!changed_contents && !link_info->keep_memory)
8654 /* Cache the section contents for elf_link_input_bfd. */
8655 elf_section_data (sec)->this_hdr.contents = contents;
8661 if (contents != NULL
8662 && elf_section_data (sec)->this_hdr.contents != contents)
8667 /* Allocate space for global sym dynamic relocs. */
8670 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8672 struct bfd_link_info *info = inf;
8674 struct mips_elf_link_hash_entry *hmips;
8675 struct mips_elf_link_hash_table *htab;
8677 htab = mips_elf_hash_table (info);
8678 BFD_ASSERT (htab != NULL);
8680 dynobj = elf_hash_table (info)->dynobj;
8681 hmips = (struct mips_elf_link_hash_entry *) h;
8683 /* VxWorks executables are handled elsewhere; we only need to
8684 allocate relocations in shared objects. */
8685 if (htab->is_vxworks && !info->shared)
8688 /* Ignore indirect symbols. All relocations against such symbols
8689 will be redirected to the target symbol. */
8690 if (h->root.type == bfd_link_hash_indirect)
8693 /* If this symbol is defined in a dynamic object, or we are creating
8694 a shared library, we will need to copy any R_MIPS_32 or
8695 R_MIPS_REL32 relocs against it into the output file. */
8696 if (! info->relocatable
8697 && hmips->possibly_dynamic_relocs != 0
8698 && (h->root.type == bfd_link_hash_defweak
8699 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8702 bfd_boolean do_copy = TRUE;
8704 if (h->root.type == bfd_link_hash_undefweak)
8706 /* Do not copy relocations for undefined weak symbols with
8707 non-default visibility. */
8708 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8711 /* Make sure undefined weak symbols are output as a dynamic
8713 else if (h->dynindx == -1 && !h->forced_local)
8715 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8722 /* Even though we don't directly need a GOT entry for this symbol,
8723 the SVR4 psABI requires it to have a dynamic symbol table
8724 index greater that DT_MIPS_GOTSYM if there are dynamic
8725 relocations against it.
8727 VxWorks does not enforce the same mapping between the GOT
8728 and the symbol table, so the same requirement does not
8730 if (!htab->is_vxworks)
8732 if (hmips->global_got_area > GGA_RELOC_ONLY)
8733 hmips->global_got_area = GGA_RELOC_ONLY;
8734 hmips->got_only_for_calls = FALSE;
8737 mips_elf_allocate_dynamic_relocations
8738 (dynobj, info, hmips->possibly_dynamic_relocs);
8739 if (hmips->readonly_reloc)
8740 /* We tell the dynamic linker that there are relocations
8741 against the text segment. */
8742 info->flags |= DF_TEXTREL;
8749 /* Adjust a symbol defined by a dynamic object and referenced by a
8750 regular object. The current definition is in some section of the
8751 dynamic object, but we're not including those sections. We have to
8752 change the definition to something the rest of the link can
8756 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8757 struct elf_link_hash_entry *h)
8760 struct mips_elf_link_hash_entry *hmips;
8761 struct mips_elf_link_hash_table *htab;
8763 htab = mips_elf_hash_table (info);
8764 BFD_ASSERT (htab != NULL);
8766 dynobj = elf_hash_table (info)->dynobj;
8767 hmips = (struct mips_elf_link_hash_entry *) h;
8769 /* Make sure we know what is going on here. */
8770 BFD_ASSERT (dynobj != NULL
8772 || h->u.weakdef != NULL
8775 && !h->def_regular)));
8777 hmips = (struct mips_elf_link_hash_entry *) h;
8779 /* If there are call relocations against an externally-defined symbol,
8780 see whether we can create a MIPS lazy-binding stub for it. We can
8781 only do this if all references to the function are through call
8782 relocations, and in that case, the traditional lazy-binding stubs
8783 are much more efficient than PLT entries.
8785 Traditional stubs are only available on SVR4 psABI-based systems;
8786 VxWorks always uses PLTs instead. */
8787 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8789 if (! elf_hash_table (info)->dynamic_sections_created)
8792 /* If this symbol is not defined in a regular file, then set
8793 the symbol to the stub location. This is required to make
8794 function pointers compare as equal between the normal
8795 executable and the shared library. */
8796 if (!h->def_regular)
8798 hmips->needs_lazy_stub = TRUE;
8799 htab->lazy_stub_count++;
8803 /* As above, VxWorks requires PLT entries for externally-defined
8804 functions that are only accessed through call relocations.
8806 Both VxWorks and non-VxWorks targets also need PLT entries if there
8807 are static-only relocations against an externally-defined function.
8808 This can technically occur for shared libraries if there are
8809 branches to the symbol, although it is unlikely that this will be
8810 used in practice due to the short ranges involved. It can occur
8811 for any relative or absolute relocation in executables; in that
8812 case, the PLT entry becomes the function's canonical address. */
8813 else if (((h->needs_plt && !hmips->no_fn_stub)
8814 || (h->type == STT_FUNC && hmips->has_static_relocs))
8815 && htab->use_plts_and_copy_relocs
8816 && !SYMBOL_CALLS_LOCAL (info, h)
8817 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8818 && h->root.type == bfd_link_hash_undefweak))
8820 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8821 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8823 /* If this is the first symbol to need a PLT entry, then make some
8824 basic setup. Also work out PLT entry sizes. We'll need them
8825 for PLT offset calculations. */
8826 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
8828 BFD_ASSERT (htab->sgotplt->size == 0);
8829 BFD_ASSERT (htab->plt_got_index == 0);
8831 /* If we're using the PLT additions to the psABI, each PLT
8832 entry is 16 bytes and the PLT0 entry is 32 bytes.
8833 Encourage better cache usage by aligning. We do this
8834 lazily to avoid pessimizing traditional objects. */
8835 if (!htab->is_vxworks
8836 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8839 /* Make sure that .got.plt is word-aligned. We do this lazily
8840 for the same reason as above. */
8841 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8842 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8845 /* On non-VxWorks targets, the first two entries in .got.plt
8847 if (!htab->is_vxworks)
8849 += (get_elf_backend_data (dynobj)->got_header_size
8850 / MIPS_ELF_GOT_SIZE (dynobj));
8852 /* On VxWorks, also allocate room for the header's
8853 .rela.plt.unloaded entries. */
8854 if (htab->is_vxworks && !info->shared)
8855 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8857 /* Now work out the sizes of individual PLT entries. */
8858 if (htab->is_vxworks && info->shared)
8859 htab->plt_mips_entry_size
8860 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8861 else if (htab->is_vxworks)
8862 htab->plt_mips_entry_size
8863 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8865 htab->plt_mips_entry_size
8866 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8867 else if (!micromips_p)
8869 htab->plt_mips_entry_size
8870 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8871 htab->plt_comp_entry_size
8872 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8874 else if (htab->insn32)
8876 htab->plt_mips_entry_size
8877 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8878 htab->plt_comp_entry_size
8879 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
8883 htab->plt_mips_entry_size
8884 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8885 htab->plt_comp_entry_size
8886 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
8890 if (h->plt.plist == NULL)
8891 h->plt.plist = mips_elf_make_plt_record (dynobj);
8892 if (h->plt.plist == NULL)
8895 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8896 n32 or n64, so always use a standard entry there.
8898 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8899 all MIPS16 calls will go via that stub, and there is no benefit
8900 to having a MIPS16 entry. And in the case of call_stub a
8901 standard entry actually has to be used as the stub ends with a J
8906 || hmips->call_fp_stub)
8908 h->plt.plist->need_mips = TRUE;
8909 h->plt.plist->need_comp = FALSE;
8912 /* Otherwise, if there are no direct calls to the function, we
8913 have a free choice of whether to use standard or compressed
8914 entries. Prefer microMIPS entries if the object is known to
8915 contain microMIPS code, so that it becomes possible to create
8916 pure microMIPS binaries. Prefer standard entries otherwise,
8917 because MIPS16 ones are no smaller and are usually slower. */
8918 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8921 h->plt.plist->need_comp = TRUE;
8923 h->plt.plist->need_mips = TRUE;
8926 if (h->plt.plist->need_mips)
8928 h->plt.plist->mips_offset = htab->plt_mips_offset;
8929 htab->plt_mips_offset += htab->plt_mips_entry_size;
8931 if (h->plt.plist->need_comp)
8933 h->plt.plist->comp_offset = htab->plt_comp_offset;
8934 htab->plt_comp_offset += htab->plt_comp_entry_size;
8937 /* Reserve the corresponding .got.plt entry now too. */
8938 h->plt.plist->gotplt_index = htab->plt_got_index++;
8940 /* If the output file has no definition of the symbol, set the
8941 symbol's value to the address of the stub. */
8942 if (!info->shared && !h->def_regular)
8943 hmips->use_plt_entry = TRUE;
8945 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
8946 htab->srelplt->size += (htab->is_vxworks
8947 ? MIPS_ELF_RELA_SIZE (dynobj)
8948 : MIPS_ELF_REL_SIZE (dynobj));
8950 /* Make room for the .rela.plt.unloaded relocations. */
8951 if (htab->is_vxworks && !info->shared)
8952 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8954 /* All relocations against this symbol that could have been made
8955 dynamic will now refer to the PLT entry instead. */
8956 hmips->possibly_dynamic_relocs = 0;
8961 /* If this is a weak symbol, and there is a real definition, the
8962 processor independent code will have arranged for us to see the
8963 real definition first, and we can just use the same value. */
8964 if (h->u.weakdef != NULL)
8966 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8967 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8968 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8969 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8973 /* Otherwise, there is nothing further to do for symbols defined
8974 in regular objects. */
8978 /* There's also nothing more to do if we'll convert all relocations
8979 against this symbol into dynamic relocations. */
8980 if (!hmips->has_static_relocs)
8983 /* We're now relying on copy relocations. Complain if we have
8984 some that we can't convert. */
8985 if (!htab->use_plts_and_copy_relocs || info->shared)
8987 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8988 "dynamic symbol %s"),
8989 h->root.root.string);
8990 bfd_set_error (bfd_error_bad_value);
8994 /* We must allocate the symbol in our .dynbss section, which will
8995 become part of the .bss section of the executable. There will be
8996 an entry for this symbol in the .dynsym section. The dynamic
8997 object will contain position independent code, so all references
8998 from the dynamic object to this symbol will go through the global
8999 offset table. The dynamic linker will use the .dynsym entry to
9000 determine the address it must put in the global offset table, so
9001 both the dynamic object and the regular object will refer to the
9002 same memory location for the variable. */
9004 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9006 if (htab->is_vxworks)
9007 htab->srelbss->size += sizeof (Elf32_External_Rela);
9009 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9013 /* All relocations against this symbol that could have been made
9014 dynamic will now refer to the local copy instead. */
9015 hmips->possibly_dynamic_relocs = 0;
9017 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
9020 /* This function is called after all the input files have been read,
9021 and the input sections have been assigned to output sections. We
9022 check for any mips16 stub sections that we can discard. */
9025 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9026 struct bfd_link_info *info)
9029 struct mips_elf_link_hash_table *htab;
9030 struct mips_htab_traverse_info hti;
9032 htab = mips_elf_hash_table (info);
9033 BFD_ASSERT (htab != NULL);
9035 /* The .reginfo section has a fixed size. */
9036 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
9038 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
9041 hti.output_bfd = output_bfd;
9043 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9044 mips_elf_check_symbols, &hti);
9051 /* If the link uses a GOT, lay it out and work out its size. */
9054 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9058 struct mips_got_info *g;
9059 bfd_size_type loadable_size = 0;
9060 bfd_size_type page_gotno;
9062 struct mips_elf_traverse_got_arg tga;
9063 struct mips_elf_link_hash_table *htab;
9065 htab = mips_elf_hash_table (info);
9066 BFD_ASSERT (htab != NULL);
9072 dynobj = elf_hash_table (info)->dynobj;
9075 /* Allocate room for the reserved entries. VxWorks always reserves
9076 3 entries; other objects only reserve 2 entries. */
9077 BFD_ASSERT (g->assigned_low_gotno == 0);
9078 if (htab->is_vxworks)
9079 htab->reserved_gotno = 3;
9081 htab->reserved_gotno = 2;
9082 g->local_gotno += htab->reserved_gotno;
9083 g->assigned_low_gotno = htab->reserved_gotno;
9085 /* Decide which symbols need to go in the global part of the GOT and
9086 count the number of reloc-only GOT symbols. */
9087 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9089 if (!mips_elf_resolve_final_got_entries (info, g))
9092 /* Calculate the total loadable size of the output. That
9093 will give us the maximum number of GOT_PAGE entries
9095 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9097 asection *subsection;
9099 for (subsection = ibfd->sections;
9101 subsection = subsection->next)
9103 if ((subsection->flags & SEC_ALLOC) == 0)
9105 loadable_size += ((subsection->size + 0xf)
9106 &~ (bfd_size_type) 0xf);
9110 if (htab->is_vxworks)
9111 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9112 relocations against local symbols evaluate to "G", and the EABI does
9113 not include R_MIPS_GOT_PAGE. */
9116 /* Assume there are two loadable segments consisting of contiguous
9117 sections. Is 5 enough? */
9118 page_gotno = (loadable_size >> 16) + 5;
9120 /* Choose the smaller of the two page estimates; both are intended to be
9122 if (page_gotno > g->page_gotno)
9123 page_gotno = g->page_gotno;
9125 g->local_gotno += page_gotno;
9126 g->assigned_high_gotno = g->local_gotno - 1;
9128 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9129 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9130 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9132 /* VxWorks does not support multiple GOTs. It initializes $gp to
9133 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9135 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9137 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9142 /* Record that all bfds use G. This also has the effect of freeing
9143 the per-bfd GOTs, which we no longer need. */
9144 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9145 if (mips_elf_bfd_got (ibfd, FALSE))
9146 mips_elf_replace_bfd_got (ibfd, g);
9147 mips_elf_replace_bfd_got (output_bfd, g);
9149 /* Set up TLS entries. */
9150 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9153 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9154 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9157 BFD_ASSERT (g->tls_assigned_gotno
9158 == g->global_gotno + g->local_gotno + g->tls_gotno);
9160 /* Each VxWorks GOT entry needs an explicit relocation. */
9161 if (htab->is_vxworks && info->shared)
9162 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9164 /* Allocate room for the TLS relocations. */
9166 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9172 /* Estimate the size of the .MIPS.stubs section. */
9175 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9177 struct mips_elf_link_hash_table *htab;
9178 bfd_size_type dynsymcount;
9180 htab = mips_elf_hash_table (info);
9181 BFD_ASSERT (htab != NULL);
9183 if (htab->lazy_stub_count == 0)
9186 /* IRIX rld assumes that a function stub isn't at the end of the .text
9187 section, so add a dummy entry to the end. */
9188 htab->lazy_stub_count++;
9190 /* Get a worst-case estimate of the number of dynamic symbols needed.
9191 At this point, dynsymcount does not account for section symbols
9192 and count_section_dynsyms may overestimate the number that will
9194 dynsymcount = (elf_hash_table (info)->dynsymcount
9195 + count_section_dynsyms (output_bfd, info));
9197 /* Determine the size of one stub entry. There's no disadvantage
9198 from using microMIPS code here, so for the sake of pure-microMIPS
9199 binaries we prefer it whenever there's any microMIPS code in
9200 output produced at all. This has a benefit of stubs being
9201 shorter by 4 bytes each too, unless in the insn32 mode. */
9202 if (!MICROMIPS_P (output_bfd))
9203 htab->function_stub_size = (dynsymcount > 0x10000
9204 ? MIPS_FUNCTION_STUB_BIG_SIZE
9205 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9206 else if (htab->insn32)
9207 htab->function_stub_size = (dynsymcount > 0x10000
9208 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9209 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9211 htab->function_stub_size = (dynsymcount > 0x10000
9212 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9213 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9215 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9218 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9219 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9220 stub, allocate an entry in the stubs section. */
9223 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9225 struct mips_htab_traverse_info *hti = data;
9226 struct mips_elf_link_hash_table *htab;
9227 struct bfd_link_info *info;
9231 output_bfd = hti->output_bfd;
9232 htab = mips_elf_hash_table (info);
9233 BFD_ASSERT (htab != NULL);
9235 if (h->needs_lazy_stub)
9237 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9238 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9239 bfd_vma isa_bit = micromips_p;
9241 BFD_ASSERT (htab->root.dynobj != NULL);
9242 if (h->root.plt.plist == NULL)
9243 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9244 if (h->root.plt.plist == NULL)
9249 h->root.root.u.def.section = htab->sstubs;
9250 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9251 h->root.plt.plist->stub_offset = htab->sstubs->size;
9252 h->root.other = other;
9253 htab->sstubs->size += htab->function_stub_size;
9258 /* Allocate offsets in the stubs section to each symbol that needs one.
9259 Set the final size of the .MIPS.stub section. */
9262 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9264 bfd *output_bfd = info->output_bfd;
9265 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9266 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9267 bfd_vma isa_bit = micromips_p;
9268 struct mips_elf_link_hash_table *htab;
9269 struct mips_htab_traverse_info hti;
9270 struct elf_link_hash_entry *h;
9273 htab = mips_elf_hash_table (info);
9274 BFD_ASSERT (htab != NULL);
9276 if (htab->lazy_stub_count == 0)
9279 htab->sstubs->size = 0;
9281 hti.output_bfd = output_bfd;
9283 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9286 htab->sstubs->size += htab->function_stub_size;
9287 BFD_ASSERT (htab->sstubs->size
9288 == htab->lazy_stub_count * htab->function_stub_size);
9290 dynobj = elf_hash_table (info)->dynobj;
9291 BFD_ASSERT (dynobj != NULL);
9292 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9295 h->root.u.def.value = isa_bit;
9302 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9303 bfd_link_info. If H uses the address of a PLT entry as the value
9304 of the symbol, then set the entry in the symbol table now. Prefer
9305 a standard MIPS PLT entry. */
9308 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9310 struct bfd_link_info *info = data;
9311 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9312 struct mips_elf_link_hash_table *htab;
9317 htab = mips_elf_hash_table (info);
9318 BFD_ASSERT (htab != NULL);
9320 if (h->use_plt_entry)
9322 BFD_ASSERT (h->root.plt.plist != NULL);
9323 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9324 || h->root.plt.plist->comp_offset != MINUS_ONE);
9326 val = htab->plt_header_size;
9327 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9330 val += h->root.plt.plist->mips_offset;
9336 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9337 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9340 /* For VxWorks, point at the PLT load stub rather than the lazy
9341 resolution stub; this stub will become the canonical function
9343 if (htab->is_vxworks)
9346 h->root.root.u.def.section = htab->splt;
9347 h->root.root.u.def.value = val;
9348 h->root.other = other;
9354 /* Set the sizes of the dynamic sections. */
9357 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9358 struct bfd_link_info *info)
9361 asection *s, *sreldyn;
9362 bfd_boolean reltext;
9363 struct mips_elf_link_hash_table *htab;
9365 htab = mips_elf_hash_table (info);
9366 BFD_ASSERT (htab != NULL);
9367 dynobj = elf_hash_table (info)->dynobj;
9368 BFD_ASSERT (dynobj != NULL);
9370 if (elf_hash_table (info)->dynamic_sections_created)
9372 /* Set the contents of the .interp section to the interpreter. */
9373 if (info->executable)
9375 s = bfd_get_linker_section (dynobj, ".interp");
9376 BFD_ASSERT (s != NULL);
9378 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9380 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9383 /* Figure out the size of the PLT header if we know that we
9384 are using it. For the sake of cache alignment always use
9385 a standard header whenever any standard entries are present
9386 even if microMIPS entries are present as well. This also
9387 lets the microMIPS header rely on the value of $v0 only set
9388 by microMIPS entries, for a small size reduction.
9390 Set symbol table entry values for symbols that use the
9391 address of their PLT entry now that we can calculate it.
9393 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9394 haven't already in _bfd_elf_create_dynamic_sections. */
9395 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9397 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9398 && !htab->plt_mips_offset);
9399 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9400 bfd_vma isa_bit = micromips_p;
9401 struct elf_link_hash_entry *h;
9404 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9405 BFD_ASSERT (htab->sgotplt->size == 0);
9406 BFD_ASSERT (htab->splt->size == 0);
9408 if (htab->is_vxworks && info->shared)
9409 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9410 else if (htab->is_vxworks)
9411 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9412 else if (ABI_64_P (output_bfd))
9413 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9414 else if (ABI_N32_P (output_bfd))
9415 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9416 else if (!micromips_p)
9417 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9418 else if (htab->insn32)
9419 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9421 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9423 htab->plt_header_is_comp = micromips_p;
9424 htab->plt_header_size = size;
9425 htab->splt->size = (size
9426 + htab->plt_mips_offset
9427 + htab->plt_comp_offset);
9428 htab->sgotplt->size = (htab->plt_got_index
9429 * MIPS_ELF_GOT_SIZE (dynobj));
9431 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9433 if (htab->root.hplt == NULL)
9435 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9436 "_PROCEDURE_LINKAGE_TABLE_");
9437 htab->root.hplt = h;
9442 h = htab->root.hplt;
9443 h->root.u.def.value = isa_bit;
9449 /* Allocate space for global sym dynamic relocs. */
9450 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9452 mips_elf_estimate_stub_size (output_bfd, info);
9454 if (!mips_elf_lay_out_got (output_bfd, info))
9457 mips_elf_lay_out_lazy_stubs (info);
9459 /* The check_relocs and adjust_dynamic_symbol entry points have
9460 determined the sizes of the various dynamic sections. Allocate
9463 for (s = dynobj->sections; s != NULL; s = s->next)
9467 /* It's OK to base decisions on the section name, because none
9468 of the dynobj section names depend upon the input files. */
9469 name = bfd_get_section_name (dynobj, s);
9471 if ((s->flags & SEC_LINKER_CREATED) == 0)
9474 if (CONST_STRNEQ (name, ".rel"))
9478 const char *outname;
9481 /* If this relocation section applies to a read only
9482 section, then we probably need a DT_TEXTREL entry.
9483 If the relocation section is .rel(a).dyn, we always
9484 assert a DT_TEXTREL entry rather than testing whether
9485 there exists a relocation to a read only section or
9487 outname = bfd_get_section_name (output_bfd,
9489 target = bfd_get_section_by_name (output_bfd, outname + 4);
9491 && (target->flags & SEC_READONLY) != 0
9492 && (target->flags & SEC_ALLOC) != 0)
9493 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9496 /* We use the reloc_count field as a counter if we need
9497 to copy relocs into the output file. */
9498 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9501 /* If combreloc is enabled, elf_link_sort_relocs() will
9502 sort relocations, but in a different way than we do,
9503 and before we're done creating relocations. Also, it
9504 will move them around between input sections'
9505 relocation's contents, so our sorting would be
9506 broken, so don't let it run. */
9507 info->combreloc = 0;
9510 else if (! info->shared
9511 && ! mips_elf_hash_table (info)->use_rld_obj_head
9512 && CONST_STRNEQ (name, ".rld_map"))
9514 /* We add a room for __rld_map. It will be filled in by the
9515 rtld to contain a pointer to the _r_debug structure. */
9516 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9518 else if (SGI_COMPAT (output_bfd)
9519 && CONST_STRNEQ (name, ".compact_rel"))
9520 s->size += mips_elf_hash_table (info)->compact_rel_size;
9521 else if (s == htab->splt)
9523 /* If the last PLT entry has a branch delay slot, allocate
9524 room for an extra nop to fill the delay slot. This is
9525 for CPUs without load interlocking. */
9526 if (! LOAD_INTERLOCKS_P (output_bfd)
9527 && ! htab->is_vxworks && s->size > 0)
9530 else if (! CONST_STRNEQ (name, ".init")
9532 && s != htab->sgotplt
9533 && s != htab->sstubs
9534 && s != htab->sdynbss)
9536 /* It's not one of our sections, so don't allocate space. */
9542 s->flags |= SEC_EXCLUDE;
9546 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9549 /* Allocate memory for the section contents. */
9550 s->contents = bfd_zalloc (dynobj, s->size);
9551 if (s->contents == NULL)
9553 bfd_set_error (bfd_error_no_memory);
9558 if (elf_hash_table (info)->dynamic_sections_created)
9560 /* Add some entries to the .dynamic section. We fill in the
9561 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9562 must add the entries now so that we get the correct size for
9563 the .dynamic section. */
9565 /* SGI object has the equivalence of DT_DEBUG in the
9566 DT_MIPS_RLD_MAP entry. This must come first because glibc
9567 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9568 may only look at the first one they see. */
9570 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9573 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9574 used by the debugger. */
9575 if (info->executable
9576 && !SGI_COMPAT (output_bfd)
9577 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9580 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9581 info->flags |= DF_TEXTREL;
9583 if ((info->flags & DF_TEXTREL) != 0)
9585 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9588 /* Clear the DF_TEXTREL flag. It will be set again if we
9589 write out an actual text relocation; we may not, because
9590 at this point we do not know whether e.g. any .eh_frame
9591 absolute relocations have been converted to PC-relative. */
9592 info->flags &= ~DF_TEXTREL;
9595 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9598 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9599 if (htab->is_vxworks)
9601 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9602 use any of the DT_MIPS_* tags. */
9603 if (sreldyn && sreldyn->size > 0)
9605 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9608 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9611 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9617 if (sreldyn && sreldyn->size > 0)
9619 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9622 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9625 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9629 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9632 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9635 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9638 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9641 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9644 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9647 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9650 if (IRIX_COMPAT (dynobj) == ict_irix5
9651 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9654 if (IRIX_COMPAT (dynobj) == ict_irix6
9655 && (bfd_get_section_by_name
9656 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9657 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9660 if (htab->splt->size > 0)
9662 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9665 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9668 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9671 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9674 if (htab->is_vxworks
9675 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9682 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9683 Adjust its R_ADDEND field so that it is correct for the output file.
9684 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9685 and sections respectively; both use symbol indexes. */
9688 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9689 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9690 asection **local_sections, Elf_Internal_Rela *rel)
9692 unsigned int r_type, r_symndx;
9693 Elf_Internal_Sym *sym;
9696 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9698 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9699 if (gprel16_reloc_p (r_type)
9700 || r_type == R_MIPS_GPREL32
9701 || literal_reloc_p (r_type))
9703 rel->r_addend += _bfd_get_gp_value (input_bfd);
9704 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9707 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9708 sym = local_syms + r_symndx;
9710 /* Adjust REL's addend to account for section merging. */
9711 if (!info->relocatable)
9713 sec = local_sections[r_symndx];
9714 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9717 /* This would normally be done by the rela_normal code in elflink.c. */
9718 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9719 rel->r_addend += local_sections[r_symndx]->output_offset;
9723 /* Handle relocations against symbols from removed linkonce sections,
9724 or sections discarded by a linker script. We use this wrapper around
9725 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9726 on 64-bit ELF targets. In this case for any relocation handled, which
9727 always be the first in a triplet, the remaining two have to be processed
9728 together with the first, even if they are R_MIPS_NONE. It is the symbol
9729 index referred by the first reloc that applies to all the three and the
9730 remaining two never refer to an object symbol. And it is the final
9731 relocation (the last non-null one) that determines the output field of
9732 the whole relocation so retrieve the corresponding howto structure for
9733 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9735 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9736 and therefore requires to be pasted in a loop. It also defines a block
9737 and does not protect any of its arguments, hence the extra brackets. */
9740 mips_reloc_against_discarded_section (bfd *output_bfd,
9741 struct bfd_link_info *info,
9742 bfd *input_bfd, asection *input_section,
9743 Elf_Internal_Rela **rel,
9744 const Elf_Internal_Rela **relend,
9745 bfd_boolean rel_reloc,
9746 reloc_howto_type *howto,
9749 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9750 int count = bed->s->int_rels_per_ext_rel;
9751 unsigned int r_type;
9754 for (i = count - 1; i > 0; i--)
9756 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9757 if (r_type != R_MIPS_NONE)
9759 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9765 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9766 (*rel), count, (*relend),
9767 howto, i, contents);
9772 /* Relocate a MIPS ELF section. */
9775 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9776 bfd *input_bfd, asection *input_section,
9777 bfd_byte *contents, Elf_Internal_Rela *relocs,
9778 Elf_Internal_Sym *local_syms,
9779 asection **local_sections)
9781 Elf_Internal_Rela *rel;
9782 const Elf_Internal_Rela *relend;
9784 bfd_boolean use_saved_addend_p = FALSE;
9785 const struct elf_backend_data *bed;
9787 bed = get_elf_backend_data (output_bfd);
9788 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9789 for (rel = relocs; rel < relend; ++rel)
9793 reloc_howto_type *howto;
9794 bfd_boolean cross_mode_jump_p = FALSE;
9795 /* TRUE if the relocation is a RELA relocation, rather than a
9797 bfd_boolean rela_relocation_p = TRUE;
9798 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9800 unsigned long r_symndx;
9802 Elf_Internal_Shdr *symtab_hdr;
9803 struct elf_link_hash_entry *h;
9804 bfd_boolean rel_reloc;
9806 rel_reloc = (NEWABI_P (input_bfd)
9807 && mips_elf_rel_relocation_p (input_bfd, input_section,
9809 /* Find the relocation howto for this relocation. */
9810 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9812 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9813 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9814 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9816 sec = local_sections[r_symndx];
9821 unsigned long extsymoff;
9824 if (!elf_bad_symtab (input_bfd))
9825 extsymoff = symtab_hdr->sh_info;
9826 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9827 while (h->root.type == bfd_link_hash_indirect
9828 || h->root.type == bfd_link_hash_warning)
9829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9832 if (h->root.type == bfd_link_hash_defined
9833 || h->root.type == bfd_link_hash_defweak)
9834 sec = h->root.u.def.section;
9837 if (sec != NULL && discarded_section (sec))
9839 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9840 input_section, &rel, &relend,
9841 rel_reloc, howto, contents);
9845 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9847 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9848 64-bit code, but make sure all their addresses are in the
9849 lowermost or uppermost 32-bit section of the 64-bit address
9850 space. Thus, when they use an R_MIPS_64 they mean what is
9851 usually meant by R_MIPS_32, with the exception that the
9852 stored value is sign-extended to 64 bits. */
9853 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9855 /* On big-endian systems, we need to lie about the position
9857 if (bfd_big_endian (input_bfd))
9861 if (!use_saved_addend_p)
9863 /* If these relocations were originally of the REL variety,
9864 we must pull the addend out of the field that will be
9865 relocated. Otherwise, we simply use the contents of the
9867 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9870 rela_relocation_p = FALSE;
9871 addend = mips_elf_read_rel_addend (input_bfd, rel,
9873 if (hi16_reloc_p (r_type)
9874 || (got16_reloc_p (r_type)
9875 && mips_elf_local_relocation_p (input_bfd, rel,
9878 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9882 name = h->root.root.string;
9884 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9885 local_syms + r_symndx,
9887 (*_bfd_error_handler)
9888 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9889 input_bfd, input_section, name, howto->name,
9894 addend <<= howto->rightshift;
9897 addend = rel->r_addend;
9898 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9899 local_syms, local_sections, rel);
9902 if (info->relocatable)
9904 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9905 && bfd_big_endian (input_bfd))
9908 if (!rela_relocation_p && rel->r_addend)
9910 addend += rel->r_addend;
9911 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9912 addend = mips_elf_high (addend);
9913 else if (r_type == R_MIPS_HIGHER)
9914 addend = mips_elf_higher (addend);
9915 else if (r_type == R_MIPS_HIGHEST)
9916 addend = mips_elf_highest (addend);
9918 addend >>= howto->rightshift;
9920 /* We use the source mask, rather than the destination
9921 mask because the place to which we are writing will be
9922 source of the addend in the final link. */
9923 addend &= howto->src_mask;
9925 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9926 /* See the comment above about using R_MIPS_64 in the 32-bit
9927 ABI. Here, we need to update the addend. It would be
9928 possible to get away with just using the R_MIPS_32 reloc
9929 but for endianness. */
9935 if (addend & ((bfd_vma) 1 << 31))
9937 sign_bits = ((bfd_vma) 1 << 32) - 1;
9944 /* If we don't know that we have a 64-bit type,
9945 do two separate stores. */
9946 if (bfd_big_endian (input_bfd))
9948 /* Store the sign-bits (which are most significant)
9950 low_bits = sign_bits;
9956 high_bits = sign_bits;
9958 bfd_put_32 (input_bfd, low_bits,
9959 contents + rel->r_offset);
9960 bfd_put_32 (input_bfd, high_bits,
9961 contents + rel->r_offset + 4);
9965 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9966 input_bfd, input_section,
9971 /* Go on to the next relocation. */
9975 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9976 relocations for the same offset. In that case we are
9977 supposed to treat the output of each relocation as the addend
9979 if (rel + 1 < relend
9980 && rel->r_offset == rel[1].r_offset
9981 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9982 use_saved_addend_p = TRUE;
9984 use_saved_addend_p = FALSE;
9986 /* Figure out what value we are supposed to relocate. */
9987 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9988 input_section, info, rel,
9989 addend, howto, local_syms,
9990 local_sections, &value,
9991 &name, &cross_mode_jump_p,
9992 use_saved_addend_p))
9994 case bfd_reloc_continue:
9995 /* There's nothing to do. */
9998 case bfd_reloc_undefined:
9999 /* mips_elf_calculate_relocation already called the
10000 undefined_symbol callback. There's no real point in
10001 trying to perform the relocation at this point, so we
10002 just skip ahead to the next relocation. */
10005 case bfd_reloc_notsupported:
10006 msg = _("internal error: unsupported relocation error");
10007 info->callbacks->warning
10008 (info, msg, name, input_bfd, input_section, rel->r_offset);
10011 case bfd_reloc_overflow:
10012 if (use_saved_addend_p)
10013 /* Ignore overflow until we reach the last relocation for
10014 a given location. */
10018 struct mips_elf_link_hash_table *htab;
10020 htab = mips_elf_hash_table (info);
10021 BFD_ASSERT (htab != NULL);
10022 BFD_ASSERT (name != NULL);
10023 if (!htab->small_data_overflow_reported
10024 && (gprel16_reloc_p (howto->type)
10025 || literal_reloc_p (howto->type)))
10027 msg = _("small-data section exceeds 64KB;"
10028 " lower small-data size limit (see option -G)");
10030 htab->small_data_overflow_reported = TRUE;
10031 (*info->callbacks->einfo) ("%P: %s\n", msg);
10033 if (! ((*info->callbacks->reloc_overflow)
10034 (info, NULL, name, howto->name, (bfd_vma) 0,
10035 input_bfd, input_section, rel->r_offset)))
10043 case bfd_reloc_outofrange:
10044 if (jal_reloc_p (howto->type))
10046 msg = _("JALX to a non-word-aligned address");
10047 info->callbacks->warning
10048 (info, msg, name, input_bfd, input_section, rel->r_offset);
10051 /* Fall through. */
10058 /* If we've got another relocation for the address, keep going
10059 until we reach the last one. */
10060 if (use_saved_addend_p)
10066 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10067 /* See the comment above about using R_MIPS_64 in the 32-bit
10068 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10069 that calculated the right value. Now, however, we
10070 sign-extend the 32-bit result to 64-bits, and store it as a
10071 64-bit value. We are especially generous here in that we
10072 go to extreme lengths to support this usage on systems with
10073 only a 32-bit VMA. */
10079 if (value & ((bfd_vma) 1 << 31))
10081 sign_bits = ((bfd_vma) 1 << 32) - 1;
10088 /* If we don't know that we have a 64-bit type,
10089 do two separate stores. */
10090 if (bfd_big_endian (input_bfd))
10092 /* Undo what we did above. */
10093 rel->r_offset -= 4;
10094 /* Store the sign-bits (which are most significant)
10096 low_bits = sign_bits;
10102 high_bits = sign_bits;
10104 bfd_put_32 (input_bfd, low_bits,
10105 contents + rel->r_offset);
10106 bfd_put_32 (input_bfd, high_bits,
10107 contents + rel->r_offset + 4);
10111 /* Actually perform the relocation. */
10112 if (! mips_elf_perform_relocation (info, howto, rel, value,
10113 input_bfd, input_section,
10114 contents, cross_mode_jump_p))
10121 /* A function that iterates over each entry in la25_stubs and fills
10122 in the code for each one. DATA points to a mips_htab_traverse_info. */
10125 mips_elf_create_la25_stub (void **slot, void *data)
10127 struct mips_htab_traverse_info *hti;
10128 struct mips_elf_link_hash_table *htab;
10129 struct mips_elf_la25_stub *stub;
10132 bfd_vma offset, target, target_high, target_low;
10134 stub = (struct mips_elf_la25_stub *) *slot;
10135 hti = (struct mips_htab_traverse_info *) data;
10136 htab = mips_elf_hash_table (hti->info);
10137 BFD_ASSERT (htab != NULL);
10139 /* Create the section contents, if we haven't already. */
10140 s = stub->stub_section;
10144 loc = bfd_malloc (s->size);
10153 /* Work out where in the section this stub should go. */
10154 offset = stub->offset;
10156 /* Work out the target address. */
10157 target = mips_elf_get_la25_target (stub, &s);
10158 target += s->output_section->vma + s->output_offset;
10160 target_high = ((target + 0x8000) >> 16) & 0xffff;
10161 target_low = (target & 0xffff);
10163 if (stub->stub_section != htab->strampoline)
10165 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10166 of the section and write the two instructions at the end. */
10167 memset (loc, 0, offset);
10169 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10171 bfd_put_micromips_32 (hti->output_bfd,
10172 LA25_LUI_MICROMIPS (target_high),
10174 bfd_put_micromips_32 (hti->output_bfd,
10175 LA25_ADDIU_MICROMIPS (target_low),
10180 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10181 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10186 /* This is trampoline. */
10188 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10190 bfd_put_micromips_32 (hti->output_bfd,
10191 LA25_LUI_MICROMIPS (target_high), loc);
10192 bfd_put_micromips_32 (hti->output_bfd,
10193 LA25_J_MICROMIPS (target), loc + 4);
10194 bfd_put_micromips_32 (hti->output_bfd,
10195 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10196 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10200 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10201 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10202 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10203 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10209 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10210 adjust it appropriately now. */
10213 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10214 const char *name, Elf_Internal_Sym *sym)
10216 /* The linker script takes care of providing names and values for
10217 these, but we must place them into the right sections. */
10218 static const char* const text_section_symbols[] = {
10221 "__dso_displacement",
10223 "__program_header_table",
10227 static const char* const data_section_symbols[] = {
10235 const char* const *p;
10238 for (i = 0; i < 2; ++i)
10239 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10242 if (strcmp (*p, name) == 0)
10244 /* All of these symbols are given type STT_SECTION by the
10246 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10247 sym->st_other = STO_PROTECTED;
10249 /* The IRIX linker puts these symbols in special sections. */
10251 sym->st_shndx = SHN_MIPS_TEXT;
10253 sym->st_shndx = SHN_MIPS_DATA;
10259 /* Finish up dynamic symbol handling. We set the contents of various
10260 dynamic sections here. */
10263 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10264 struct bfd_link_info *info,
10265 struct elf_link_hash_entry *h,
10266 Elf_Internal_Sym *sym)
10270 struct mips_got_info *g, *gg;
10273 struct mips_elf_link_hash_table *htab;
10274 struct mips_elf_link_hash_entry *hmips;
10276 htab = mips_elf_hash_table (info);
10277 BFD_ASSERT (htab != NULL);
10278 dynobj = elf_hash_table (info)->dynobj;
10279 hmips = (struct mips_elf_link_hash_entry *) h;
10281 BFD_ASSERT (!htab->is_vxworks);
10283 if (h->plt.plist != NULL
10284 && (h->plt.plist->mips_offset != MINUS_ONE
10285 || h->plt.plist->comp_offset != MINUS_ONE))
10287 /* We've decided to create a PLT entry for this symbol. */
10289 bfd_vma header_address, got_address;
10290 bfd_vma got_address_high, got_address_low, load;
10294 got_index = h->plt.plist->gotplt_index;
10296 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10297 BFD_ASSERT (h->dynindx != -1);
10298 BFD_ASSERT (htab->splt != NULL);
10299 BFD_ASSERT (got_index != MINUS_ONE);
10300 BFD_ASSERT (!h->def_regular);
10302 /* Calculate the address of the PLT header. */
10303 isa_bit = htab->plt_header_is_comp;
10304 header_address = (htab->splt->output_section->vma
10305 + htab->splt->output_offset + isa_bit);
10307 /* Calculate the address of the .got.plt entry. */
10308 got_address = (htab->sgotplt->output_section->vma
10309 + htab->sgotplt->output_offset
10310 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10312 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10313 got_address_low = got_address & 0xffff;
10315 /* Initially point the .got.plt entry at the PLT header. */
10316 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10317 if (ABI_64_P (output_bfd))
10318 bfd_put_64 (output_bfd, header_address, loc);
10320 bfd_put_32 (output_bfd, header_address, loc);
10322 /* Now handle the PLT itself. First the standard entry (the order
10323 does not matter, we just have to pick one). */
10324 if (h->plt.plist->mips_offset != MINUS_ONE)
10326 const bfd_vma *plt_entry;
10327 bfd_vma plt_offset;
10329 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10331 BFD_ASSERT (plt_offset <= htab->splt->size);
10333 /* Find out where the .plt entry should go. */
10334 loc = htab->splt->contents + plt_offset;
10336 /* Pick the load opcode. */
10337 load = MIPS_ELF_LOAD_WORD (output_bfd);
10339 /* Fill in the PLT entry itself. */
10340 plt_entry = mips_exec_plt_entry;
10341 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10342 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10345 if (! LOAD_INTERLOCKS_P (output_bfd))
10347 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10348 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10352 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10353 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10358 /* Now the compressed entry. They come after any standard ones. */
10359 if (h->plt.plist->comp_offset != MINUS_ONE)
10361 bfd_vma plt_offset;
10363 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10364 + h->plt.plist->comp_offset);
10366 BFD_ASSERT (plt_offset <= htab->splt->size);
10368 /* Find out where the .plt entry should go. */
10369 loc = htab->splt->contents + plt_offset;
10371 /* Fill in the PLT entry itself. */
10372 if (!MICROMIPS_P (output_bfd))
10374 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10376 bfd_put_16 (output_bfd, plt_entry[0], loc);
10377 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10378 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10379 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10380 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10381 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10382 bfd_put_32 (output_bfd, got_address, loc + 12);
10384 else if (htab->insn32)
10386 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10388 bfd_put_16 (output_bfd, plt_entry[0], loc);
10389 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10390 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10391 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10392 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10393 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10394 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10395 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10399 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10400 bfd_signed_vma gotpc_offset;
10401 bfd_vma loc_address;
10403 BFD_ASSERT (got_address % 4 == 0);
10405 loc_address = (htab->splt->output_section->vma
10406 + htab->splt->output_offset + plt_offset);
10407 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10409 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10410 if (gotpc_offset + 0x1000000 >= 0x2000000)
10412 (*_bfd_error_handler)
10413 (_("%B: `%A' offset of %ld from `%A' "
10414 "beyond the range of ADDIUPC"),
10416 htab->sgotplt->output_section,
10417 htab->splt->output_section,
10418 (long) gotpc_offset);
10419 bfd_set_error (bfd_error_no_error);
10422 bfd_put_16 (output_bfd,
10423 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10424 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10425 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10426 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10427 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10428 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10432 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10433 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
10434 got_index - 2, h->dynindx,
10435 R_MIPS_JUMP_SLOT, got_address);
10437 /* We distinguish between PLT entries and lazy-binding stubs by
10438 giving the former an st_other value of STO_MIPS_PLT. Set the
10439 flag and leave the value if there are any relocations in the
10440 binary where pointer equality matters. */
10441 sym->st_shndx = SHN_UNDEF;
10442 if (h->pointer_equality_needed)
10443 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
10451 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
10453 /* We've decided to create a lazy-binding stub. */
10454 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10455 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10456 bfd_vma stub_size = htab->function_stub_size;
10457 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
10458 bfd_vma isa_bit = micromips_p;
10459 bfd_vma stub_big_size;
10462 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
10463 else if (htab->insn32)
10464 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10466 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
10468 /* This symbol has a stub. Set it up. */
10470 BFD_ASSERT (h->dynindx != -1);
10472 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
10474 /* Values up to 2^31 - 1 are allowed. Larger values would cause
10475 sign extension at runtime in the stub, resulting in a negative
10477 if (h->dynindx & ~0x7fffffff)
10480 /* Fill the stub. */
10484 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10489 bfd_put_micromips_32 (output_bfd,
10490 STUB_MOVE32_MICROMIPS (output_bfd),
10496 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10499 if (stub_size == stub_big_size)
10501 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10503 bfd_put_micromips_32 (output_bfd,
10504 STUB_LUI_MICROMIPS (dynindx_hi),
10510 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10516 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10520 /* If a large stub is not required and sign extension is not a
10521 problem, then use legacy code in the stub. */
10522 if (stub_size == stub_big_size)
10523 bfd_put_micromips_32 (output_bfd,
10524 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10526 else if (h->dynindx & ~0x7fff)
10527 bfd_put_micromips_32 (output_bfd,
10528 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10531 bfd_put_micromips_32 (output_bfd,
10532 STUB_LI16S_MICROMIPS (output_bfd,
10539 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10541 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10543 if (stub_size == stub_big_size)
10545 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10549 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10552 /* If a large stub is not required and sign extension is not a
10553 problem, then use legacy code in the stub. */
10554 if (stub_size == stub_big_size)
10555 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10557 else if (h->dynindx & ~0x7fff)
10558 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10561 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10565 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10566 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10569 /* Mark the symbol as undefined. stub_offset != -1 occurs
10570 only for the referenced symbol. */
10571 sym->st_shndx = SHN_UNDEF;
10573 /* The run-time linker uses the st_value field of the symbol
10574 to reset the global offset table entry for this external
10575 to its stub address when unlinking a shared object. */
10576 sym->st_value = (htab->sstubs->output_section->vma
10577 + htab->sstubs->output_offset
10578 + h->plt.plist->stub_offset
10580 sym->st_other = other;
10583 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10584 refer to the stub, since only the stub uses the standard calling
10586 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10588 BFD_ASSERT (hmips->need_fn_stub);
10589 sym->st_value = (hmips->fn_stub->output_section->vma
10590 + hmips->fn_stub->output_offset);
10591 sym->st_size = hmips->fn_stub->size;
10592 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10595 BFD_ASSERT (h->dynindx != -1
10596 || h->forced_local);
10599 g = htab->got_info;
10600 BFD_ASSERT (g != NULL);
10602 /* Run through the global symbol table, creating GOT entries for all
10603 the symbols that need them. */
10604 if (hmips->global_got_area != GGA_NONE)
10609 value = sym->st_value;
10610 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10611 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10614 if (hmips->global_got_area != GGA_NONE && g->next)
10616 struct mips_got_entry e, *p;
10622 e.abfd = output_bfd;
10625 e.tls_type = GOT_TLS_NONE;
10627 for (g = g->next; g->next != gg; g = g->next)
10630 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10633 offset = p->gotidx;
10634 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
10636 || (elf_hash_table (info)->dynamic_sections_created
10638 && p->d.h->root.def_dynamic
10639 && !p->d.h->root.def_regular))
10641 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10642 the various compatibility problems, it's easier to mock
10643 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10644 mips_elf_create_dynamic_relocation to calculate the
10645 appropriate addend. */
10646 Elf_Internal_Rela rel[3];
10648 memset (rel, 0, sizeof (rel));
10649 if (ABI_64_P (output_bfd))
10650 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10652 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10653 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10656 if (! (mips_elf_create_dynamic_relocation
10657 (output_bfd, info, rel,
10658 e.d.h, NULL, sym->st_value, &entry, sgot)))
10662 entry = sym->st_value;
10663 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10668 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10669 name = h->root.root.string;
10670 if (h == elf_hash_table (info)->hdynamic
10671 || h == elf_hash_table (info)->hgot)
10672 sym->st_shndx = SHN_ABS;
10673 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10674 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10676 sym->st_shndx = SHN_ABS;
10677 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10680 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10682 sym->st_shndx = SHN_ABS;
10683 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10684 sym->st_value = elf_gp (output_bfd);
10686 else if (SGI_COMPAT (output_bfd))
10688 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10689 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10691 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10692 sym->st_other = STO_PROTECTED;
10694 sym->st_shndx = SHN_MIPS_DATA;
10696 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10698 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10699 sym->st_other = STO_PROTECTED;
10700 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10701 sym->st_shndx = SHN_ABS;
10703 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10705 if (h->type == STT_FUNC)
10706 sym->st_shndx = SHN_MIPS_TEXT;
10707 else if (h->type == STT_OBJECT)
10708 sym->st_shndx = SHN_MIPS_DATA;
10712 /* Emit a copy reloc, if needed. */
10718 BFD_ASSERT (h->dynindx != -1);
10719 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10721 s = mips_elf_rel_dyn_section (info, FALSE);
10722 symval = (h->root.u.def.section->output_section->vma
10723 + h->root.u.def.section->output_offset
10724 + h->root.u.def.value);
10725 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10726 h->dynindx, R_MIPS_COPY, symval);
10729 /* Handle the IRIX6-specific symbols. */
10730 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10731 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10733 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10734 to treat compressed symbols like any other. */
10735 if (ELF_ST_IS_MIPS16 (sym->st_other))
10737 BFD_ASSERT (sym->st_value & 1);
10738 sym->st_other -= STO_MIPS16;
10740 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10742 BFD_ASSERT (sym->st_value & 1);
10743 sym->st_other -= STO_MICROMIPS;
10749 /* Likewise, for VxWorks. */
10752 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10753 struct bfd_link_info *info,
10754 struct elf_link_hash_entry *h,
10755 Elf_Internal_Sym *sym)
10759 struct mips_got_info *g;
10760 struct mips_elf_link_hash_table *htab;
10761 struct mips_elf_link_hash_entry *hmips;
10763 htab = mips_elf_hash_table (info);
10764 BFD_ASSERT (htab != NULL);
10765 dynobj = elf_hash_table (info)->dynobj;
10766 hmips = (struct mips_elf_link_hash_entry *) h;
10768 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
10771 bfd_vma plt_address, got_address, got_offset, branch_offset;
10772 Elf_Internal_Rela rel;
10773 static const bfd_vma *plt_entry;
10774 bfd_vma gotplt_index;
10775 bfd_vma plt_offset;
10777 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10778 gotplt_index = h->plt.plist->gotplt_index;
10780 BFD_ASSERT (h->dynindx != -1);
10781 BFD_ASSERT (htab->splt != NULL);
10782 BFD_ASSERT (gotplt_index != MINUS_ONE);
10783 BFD_ASSERT (plt_offset <= htab->splt->size);
10785 /* Calculate the address of the .plt entry. */
10786 plt_address = (htab->splt->output_section->vma
10787 + htab->splt->output_offset
10790 /* Calculate the address of the .got.plt entry. */
10791 got_address = (htab->sgotplt->output_section->vma
10792 + htab->sgotplt->output_offset
10793 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
10795 /* Calculate the offset of the .got.plt entry from
10796 _GLOBAL_OFFSET_TABLE_. */
10797 got_offset = mips_elf_gotplt_index (info, h);
10799 /* Calculate the offset for the branch at the start of the PLT
10800 entry. The branch jumps to the beginning of .plt. */
10801 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
10803 /* Fill in the initial value of the .got.plt entry. */
10804 bfd_put_32 (output_bfd, plt_address,
10805 (htab->sgotplt->contents
10806 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
10808 /* Find out where the .plt entry should go. */
10809 loc = htab->splt->contents + plt_offset;
10813 plt_entry = mips_vxworks_shared_plt_entry;
10814 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10815 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10819 bfd_vma got_address_high, got_address_low;
10821 plt_entry = mips_vxworks_exec_plt_entry;
10822 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10823 got_address_low = got_address & 0xffff;
10825 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10826 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10827 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10828 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10829 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10830 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10831 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10832 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10834 loc = (htab->srelplt2->contents
10835 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10837 /* Emit a relocation for the .got.plt entry. */
10838 rel.r_offset = got_address;
10839 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10840 rel.r_addend = plt_offset;
10841 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10843 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10844 loc += sizeof (Elf32_External_Rela);
10845 rel.r_offset = plt_address + 8;
10846 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10847 rel.r_addend = got_offset;
10848 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10850 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10851 loc += sizeof (Elf32_External_Rela);
10853 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10854 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10857 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10858 loc = (htab->srelplt->contents
10859 + gotplt_index * sizeof (Elf32_External_Rela));
10860 rel.r_offset = got_address;
10861 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10863 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10865 if (!h->def_regular)
10866 sym->st_shndx = SHN_UNDEF;
10869 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10872 g = htab->got_info;
10873 BFD_ASSERT (g != NULL);
10875 /* See if this symbol has an entry in the GOT. */
10876 if (hmips->global_got_area != GGA_NONE)
10879 Elf_Internal_Rela outrel;
10883 /* Install the symbol value in the GOT. */
10884 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10885 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10887 /* Add a dynamic relocation for it. */
10888 s = mips_elf_rel_dyn_section (info, FALSE);
10889 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10890 outrel.r_offset = (sgot->output_section->vma
10891 + sgot->output_offset
10893 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10894 outrel.r_addend = 0;
10895 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10898 /* Emit a copy reloc, if needed. */
10901 Elf_Internal_Rela rel;
10903 BFD_ASSERT (h->dynindx != -1);
10905 rel.r_offset = (h->root.u.def.section->output_section->vma
10906 + h->root.u.def.section->output_offset
10907 + h->root.u.def.value);
10908 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10910 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10911 htab->srelbss->contents
10912 + (htab->srelbss->reloc_count
10913 * sizeof (Elf32_External_Rela)));
10914 ++htab->srelbss->reloc_count;
10917 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10918 if (ELF_ST_IS_COMPRESSED (sym->st_other))
10919 sym->st_value &= ~1;
10924 /* Write out a plt0 entry to the beginning of .plt. */
10927 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10930 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10931 static const bfd_vma *plt_entry;
10932 struct mips_elf_link_hash_table *htab;
10934 htab = mips_elf_hash_table (info);
10935 BFD_ASSERT (htab != NULL);
10937 if (ABI_64_P (output_bfd))
10938 plt_entry = mips_n64_exec_plt0_entry;
10939 else if (ABI_N32_P (output_bfd))
10940 plt_entry = mips_n32_exec_plt0_entry;
10941 else if (!htab->plt_header_is_comp)
10942 plt_entry = mips_o32_exec_plt0_entry;
10943 else if (htab->insn32)
10944 plt_entry = micromips_insn32_o32_exec_plt0_entry;
10946 plt_entry = micromips_o32_exec_plt0_entry;
10948 /* Calculate the value of .got.plt. */
10949 gotplt_value = (htab->sgotplt->output_section->vma
10950 + htab->sgotplt->output_offset);
10951 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10952 gotplt_value_low = gotplt_value & 0xffff;
10954 /* The PLT sequence is not safe for N64 if .got.plt's address can
10955 not be loaded in two instructions. */
10956 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10957 || ~(gotplt_value | 0x7fffffff) == 0);
10959 /* Install the PLT header. */
10960 loc = htab->splt->contents;
10961 if (plt_entry == micromips_o32_exec_plt0_entry)
10963 bfd_vma gotpc_offset;
10964 bfd_vma loc_address;
10967 BFD_ASSERT (gotplt_value % 4 == 0);
10969 loc_address = (htab->splt->output_section->vma
10970 + htab->splt->output_offset);
10971 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
10973 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10974 if (gotpc_offset + 0x1000000 >= 0x2000000)
10976 (*_bfd_error_handler)
10977 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
10979 htab->sgotplt->output_section,
10980 htab->splt->output_section,
10981 (long) gotpc_offset);
10982 bfd_set_error (bfd_error_no_error);
10985 bfd_put_16 (output_bfd,
10986 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10987 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10988 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
10989 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10991 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
10995 bfd_put_16 (output_bfd, plt_entry[0], loc);
10996 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
10997 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10998 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
10999 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11000 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11001 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11002 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11006 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11007 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11008 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11009 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11010 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11011 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11012 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11013 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11019 /* Install the PLT header for a VxWorks executable and finalize the
11020 contents of .rela.plt.unloaded. */
11023 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11025 Elf_Internal_Rela rela;
11027 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11028 static const bfd_vma *plt_entry;
11029 struct mips_elf_link_hash_table *htab;
11031 htab = mips_elf_hash_table (info);
11032 BFD_ASSERT (htab != NULL);
11034 plt_entry = mips_vxworks_exec_plt0_entry;
11036 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11037 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11038 + htab->root.hgot->root.u.def.section->output_offset
11039 + htab->root.hgot->root.u.def.value);
11041 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11042 got_value_low = got_value & 0xffff;
11044 /* Calculate the address of the PLT header. */
11045 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11047 /* Install the PLT header. */
11048 loc = htab->splt->contents;
11049 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11050 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11051 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11052 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11053 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11054 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11056 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11057 loc = htab->srelplt2->contents;
11058 rela.r_offset = plt_address;
11059 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11061 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11062 loc += sizeof (Elf32_External_Rela);
11064 /* Output the relocation for the following addiu of
11065 %lo(_GLOBAL_OFFSET_TABLE_). */
11066 rela.r_offset += 4;
11067 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11068 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11069 loc += sizeof (Elf32_External_Rela);
11071 /* Fix up the remaining relocations. They may have the wrong
11072 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11073 in which symbols were output. */
11074 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11076 Elf_Internal_Rela rel;
11078 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11079 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11080 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11081 loc += sizeof (Elf32_External_Rela);
11083 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11084 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11085 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11086 loc += sizeof (Elf32_External_Rela);
11088 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11089 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11090 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11091 loc += sizeof (Elf32_External_Rela);
11095 /* Install the PLT header for a VxWorks shared library. */
11098 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11101 struct mips_elf_link_hash_table *htab;
11103 htab = mips_elf_hash_table (info);
11104 BFD_ASSERT (htab != NULL);
11106 /* We just need to copy the entry byte-by-byte. */
11107 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11108 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11109 htab->splt->contents + i * 4);
11112 /* Finish up the dynamic sections. */
11115 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11116 struct bfd_link_info *info)
11121 struct mips_got_info *gg, *g;
11122 struct mips_elf_link_hash_table *htab;
11124 htab = mips_elf_hash_table (info);
11125 BFD_ASSERT (htab != NULL);
11127 dynobj = elf_hash_table (info)->dynobj;
11129 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11132 gg = htab->got_info;
11134 if (elf_hash_table (info)->dynamic_sections_created)
11137 int dyn_to_skip = 0, dyn_skipped = 0;
11139 BFD_ASSERT (sdyn != NULL);
11140 BFD_ASSERT (gg != NULL);
11142 g = mips_elf_bfd_got (output_bfd, FALSE);
11143 BFD_ASSERT (g != NULL);
11145 for (b = sdyn->contents;
11146 b < sdyn->contents + sdyn->size;
11147 b += MIPS_ELF_DYN_SIZE (dynobj))
11149 Elf_Internal_Dyn dyn;
11153 bfd_boolean swap_out_p;
11155 /* Read in the current dynamic entry. */
11156 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11158 /* Assume that we're going to modify it and write it out. */
11164 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11168 BFD_ASSERT (htab->is_vxworks);
11169 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11173 /* Rewrite DT_STRSZ. */
11175 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11180 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11183 case DT_MIPS_PLTGOT:
11185 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11188 case DT_MIPS_RLD_VERSION:
11189 dyn.d_un.d_val = 1; /* XXX */
11192 case DT_MIPS_FLAGS:
11193 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11196 case DT_MIPS_TIME_STAMP:
11200 dyn.d_un.d_val = t;
11204 case DT_MIPS_ICHECKSUM:
11206 swap_out_p = FALSE;
11209 case DT_MIPS_IVERSION:
11211 swap_out_p = FALSE;
11214 case DT_MIPS_BASE_ADDRESS:
11215 s = output_bfd->sections;
11216 BFD_ASSERT (s != NULL);
11217 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11220 case DT_MIPS_LOCAL_GOTNO:
11221 dyn.d_un.d_val = g->local_gotno;
11224 case DT_MIPS_UNREFEXTNO:
11225 /* The index into the dynamic symbol table which is the
11226 entry of the first external symbol that is not
11227 referenced within the same object. */
11228 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11231 case DT_MIPS_GOTSYM:
11232 if (htab->global_gotsym)
11234 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11237 /* In case if we don't have global got symbols we default
11238 to setting DT_MIPS_GOTSYM to the same value as
11239 DT_MIPS_SYMTABNO, so we just fall through. */
11241 case DT_MIPS_SYMTABNO:
11243 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11244 s = bfd_get_section_by_name (output_bfd, name);
11245 BFD_ASSERT (s != NULL);
11247 dyn.d_un.d_val = s->size / elemsize;
11250 case DT_MIPS_HIPAGENO:
11251 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11254 case DT_MIPS_RLD_MAP:
11256 struct elf_link_hash_entry *h;
11257 h = mips_elf_hash_table (info)->rld_symbol;
11260 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11261 swap_out_p = FALSE;
11264 s = h->root.u.def.section;
11265 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11266 + h->root.u.def.value);
11270 case DT_MIPS_OPTIONS:
11271 s = (bfd_get_section_by_name
11272 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11273 dyn.d_un.d_ptr = s->vma;
11277 BFD_ASSERT (htab->is_vxworks);
11278 /* The count does not include the JUMP_SLOT relocations. */
11280 dyn.d_un.d_val -= htab->srelplt->size;
11284 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11285 if (htab->is_vxworks)
11286 dyn.d_un.d_val = DT_RELA;
11288 dyn.d_un.d_val = DT_REL;
11292 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11293 dyn.d_un.d_val = htab->srelplt->size;
11297 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11298 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
11299 + htab->srelplt->output_offset);
11303 /* If we didn't need any text relocations after all, delete
11304 the dynamic tag. */
11305 if (!(info->flags & DF_TEXTREL))
11307 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11308 swap_out_p = FALSE;
11313 /* If we didn't need any text relocations after all, clear
11314 DF_TEXTREL from DT_FLAGS. */
11315 if (!(info->flags & DF_TEXTREL))
11316 dyn.d_un.d_val &= ~DF_TEXTREL;
11318 swap_out_p = FALSE;
11322 swap_out_p = FALSE;
11323 if (htab->is_vxworks
11324 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11329 if (swap_out_p || dyn_skipped)
11330 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11331 (dynobj, &dyn, b - dyn_skipped);
11335 dyn_skipped += dyn_to_skip;
11340 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11341 if (dyn_skipped > 0)
11342 memset (b - dyn_skipped, 0, dyn_skipped);
11345 if (sgot != NULL && sgot->size > 0
11346 && !bfd_is_abs_section (sgot->output_section))
11348 if (htab->is_vxworks)
11350 /* The first entry of the global offset table points to the
11351 ".dynamic" section. The second is initialized by the
11352 loader and contains the shared library identifier.
11353 The third is also initialized by the loader and points
11354 to the lazy resolution stub. */
11355 MIPS_ELF_PUT_WORD (output_bfd,
11356 sdyn->output_offset + sdyn->output_section->vma,
11358 MIPS_ELF_PUT_WORD (output_bfd, 0,
11359 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11360 MIPS_ELF_PUT_WORD (output_bfd, 0,
11362 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11366 /* The first entry of the global offset table will be filled at
11367 runtime. The second entry will be used by some runtime loaders.
11368 This isn't the case of IRIX rld. */
11369 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
11370 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11371 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11374 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11375 = MIPS_ELF_GOT_SIZE (output_bfd);
11378 /* Generate dynamic relocations for the non-primary gots. */
11379 if (gg != NULL && gg->next)
11381 Elf_Internal_Rela rel[3];
11382 bfd_vma addend = 0;
11384 memset (rel, 0, sizeof (rel));
11385 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11387 for (g = gg->next; g->next != gg; g = g->next)
11389 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
11390 + g->next->tls_gotno;
11392 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
11393 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11394 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11396 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11398 if (! info->shared)
11401 for (; got_index < g->local_gotno; got_index++)
11403 if (got_index >= g->assigned_low_gotno
11404 && got_index <= g->assigned_high_gotno)
11407 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
11408 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
11409 if (!(mips_elf_create_dynamic_relocation
11410 (output_bfd, info, rel, NULL,
11411 bfd_abs_section_ptr,
11412 0, &addend, sgot)))
11414 BFD_ASSERT (addend == 0);
11419 /* The generation of dynamic relocations for the non-primary gots
11420 adds more dynamic relocations. We cannot count them until
11423 if (elf_hash_table (info)->dynamic_sections_created)
11426 bfd_boolean swap_out_p;
11428 BFD_ASSERT (sdyn != NULL);
11430 for (b = sdyn->contents;
11431 b < sdyn->contents + sdyn->size;
11432 b += MIPS_ELF_DYN_SIZE (dynobj))
11434 Elf_Internal_Dyn dyn;
11437 /* Read in the current dynamic entry. */
11438 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11440 /* Assume that we're going to modify it and write it out. */
11446 /* Reduce DT_RELSZ to account for any relocations we
11447 decided not to make. This is for the n64 irix rld,
11448 which doesn't seem to apply any relocations if there
11449 are trailing null entries. */
11450 s = mips_elf_rel_dyn_section (info, FALSE);
11451 dyn.d_un.d_val = (s->reloc_count
11452 * (ABI_64_P (output_bfd)
11453 ? sizeof (Elf64_Mips_External_Rel)
11454 : sizeof (Elf32_External_Rel)));
11455 /* Adjust the section size too. Tools like the prelinker
11456 can reasonably expect the values to the same. */
11457 elf_section_data (s->output_section)->this_hdr.sh_size
11462 swap_out_p = FALSE;
11467 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11474 Elf32_compact_rel cpt;
11476 if (SGI_COMPAT (output_bfd))
11478 /* Write .compact_rel section out. */
11479 s = bfd_get_linker_section (dynobj, ".compact_rel");
11483 cpt.num = s->reloc_count;
11485 cpt.offset = (s->output_section->filepos
11486 + sizeof (Elf32_External_compact_rel));
11489 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11490 ((Elf32_External_compact_rel *)
11493 /* Clean up a dummy stub function entry in .text. */
11494 if (htab->sstubs != NULL)
11496 file_ptr dummy_offset;
11498 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11499 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11500 memset (htab->sstubs->contents + dummy_offset, 0,
11501 htab->function_stub_size);
11506 /* The psABI says that the dynamic relocations must be sorted in
11507 increasing order of r_symndx. The VxWorks EABI doesn't require
11508 this, and because the code below handles REL rather than RELA
11509 relocations, using it for VxWorks would be outright harmful. */
11510 if (!htab->is_vxworks)
11512 s = mips_elf_rel_dyn_section (info, FALSE);
11514 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11516 reldyn_sorting_bfd = output_bfd;
11518 if (ABI_64_P (output_bfd))
11519 qsort ((Elf64_External_Rel *) s->contents + 1,
11520 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11521 sort_dynamic_relocs_64);
11523 qsort ((Elf32_External_Rel *) s->contents + 1,
11524 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11525 sort_dynamic_relocs);
11530 if (htab->splt && htab->splt->size > 0)
11532 if (htab->is_vxworks)
11535 mips_vxworks_finish_shared_plt (output_bfd, info);
11537 mips_vxworks_finish_exec_plt (output_bfd, info);
11541 BFD_ASSERT (!info->shared);
11542 if (!mips_finish_exec_plt (output_bfd, info))
11550 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11553 mips_set_isa_flags (bfd *abfd)
11557 switch (bfd_get_mach (abfd))
11560 case bfd_mach_mips3000:
11561 val = E_MIPS_ARCH_1;
11564 case bfd_mach_mips3900:
11565 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11568 case bfd_mach_mips6000:
11569 val = E_MIPS_ARCH_2;
11572 case bfd_mach_mips4000:
11573 case bfd_mach_mips4300:
11574 case bfd_mach_mips4400:
11575 case bfd_mach_mips4600:
11576 val = E_MIPS_ARCH_3;
11579 case bfd_mach_mips4010:
11580 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11583 case bfd_mach_mips4100:
11584 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11587 case bfd_mach_mips4111:
11588 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11591 case bfd_mach_mips4120:
11592 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11595 case bfd_mach_mips4650:
11596 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11599 case bfd_mach_mips5400:
11600 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11603 case bfd_mach_mips5500:
11604 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11607 case bfd_mach_mips5900:
11608 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11611 case bfd_mach_mips9000:
11612 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11615 case bfd_mach_mips5000:
11616 case bfd_mach_mips7000:
11617 case bfd_mach_mips8000:
11618 case bfd_mach_mips10000:
11619 case bfd_mach_mips12000:
11620 case bfd_mach_mips14000:
11621 case bfd_mach_mips16000:
11622 val = E_MIPS_ARCH_4;
11625 case bfd_mach_mips5:
11626 val = E_MIPS_ARCH_5;
11629 case bfd_mach_mips_loongson_2e:
11630 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11633 case bfd_mach_mips_loongson_2f:
11634 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11637 case bfd_mach_mips_sb1:
11638 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11641 case bfd_mach_mips_loongson_3a:
11642 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
11645 case bfd_mach_mips_octeon:
11646 case bfd_mach_mips_octeonp:
11647 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11650 case bfd_mach_mips_xlr:
11651 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11654 case bfd_mach_mips_octeon2:
11655 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11658 case bfd_mach_mipsisa32:
11659 val = E_MIPS_ARCH_32;
11662 case bfd_mach_mipsisa64:
11663 val = E_MIPS_ARCH_64;
11666 case bfd_mach_mipsisa32r2:
11667 case bfd_mach_mipsisa32r3:
11668 case bfd_mach_mipsisa32r5:
11669 val = E_MIPS_ARCH_32R2;
11672 case bfd_mach_mipsisa64r2:
11673 case bfd_mach_mipsisa64r3:
11674 case bfd_mach_mipsisa64r5:
11675 val = E_MIPS_ARCH_64R2;
11678 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11679 elf_elfheader (abfd)->e_flags |= val;
11684 /* The final processing done just before writing out a MIPS ELF object
11685 file. This gets the MIPS architecture right based on the machine
11686 number. This is used by both the 32-bit and the 64-bit ABI. */
11689 _bfd_mips_elf_final_write_processing (bfd *abfd,
11690 bfd_boolean linker ATTRIBUTE_UNUSED)
11693 Elf_Internal_Shdr **hdrpp;
11697 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11698 is nonzero. This is for compatibility with old objects, which used
11699 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11700 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11701 mips_set_isa_flags (abfd);
11703 /* Set the sh_info field for .gptab sections and other appropriate
11704 info for each special section. */
11705 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11706 i < elf_numsections (abfd);
11709 switch ((*hdrpp)->sh_type)
11711 case SHT_MIPS_MSYM:
11712 case SHT_MIPS_LIBLIST:
11713 sec = bfd_get_section_by_name (abfd, ".dynstr");
11715 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11718 case SHT_MIPS_GPTAB:
11719 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11720 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11721 BFD_ASSERT (name != NULL
11722 && CONST_STRNEQ (name, ".gptab."));
11723 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11724 BFD_ASSERT (sec != NULL);
11725 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11728 case SHT_MIPS_CONTENT:
11729 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11730 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11731 BFD_ASSERT (name != NULL
11732 && CONST_STRNEQ (name, ".MIPS.content"));
11733 sec = bfd_get_section_by_name (abfd,
11734 name + sizeof ".MIPS.content" - 1);
11735 BFD_ASSERT (sec != NULL);
11736 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11739 case SHT_MIPS_SYMBOL_LIB:
11740 sec = bfd_get_section_by_name (abfd, ".dynsym");
11742 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11743 sec = bfd_get_section_by_name (abfd, ".liblist");
11745 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11748 case SHT_MIPS_EVENTS:
11749 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11750 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11751 BFD_ASSERT (name != NULL);
11752 if (CONST_STRNEQ (name, ".MIPS.events"))
11753 sec = bfd_get_section_by_name (abfd,
11754 name + sizeof ".MIPS.events" - 1);
11757 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
11758 sec = bfd_get_section_by_name (abfd,
11760 + sizeof ".MIPS.post_rel" - 1));
11762 BFD_ASSERT (sec != NULL);
11763 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11770 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11774 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11775 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11780 /* See if we need a PT_MIPS_REGINFO segment. */
11781 s = bfd_get_section_by_name (abfd, ".reginfo");
11782 if (s && (s->flags & SEC_LOAD))
11785 /* See if we need a PT_MIPS_OPTIONS segment. */
11786 if (IRIX_COMPAT (abfd) == ict_irix6
11787 && bfd_get_section_by_name (abfd,
11788 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11791 /* See if we need a PT_MIPS_RTPROC segment. */
11792 if (IRIX_COMPAT (abfd) == ict_irix5
11793 && bfd_get_section_by_name (abfd, ".dynamic")
11794 && bfd_get_section_by_name (abfd, ".mdebug"))
11797 /* Allocate a PT_NULL header in dynamic objects. See
11798 _bfd_mips_elf_modify_segment_map for details. */
11799 if (!SGI_COMPAT (abfd)
11800 && bfd_get_section_by_name (abfd, ".dynamic"))
11806 /* Modify the segment map for an IRIX5 executable. */
11809 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11810 struct bfd_link_info *info)
11813 struct elf_segment_map *m, **pm;
11816 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11818 s = bfd_get_section_by_name (abfd, ".reginfo");
11819 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11821 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11822 if (m->p_type == PT_MIPS_REGINFO)
11827 m = bfd_zalloc (abfd, amt);
11831 m->p_type = PT_MIPS_REGINFO;
11833 m->sections[0] = s;
11835 /* We want to put it after the PHDR and INTERP segments. */
11836 pm = &elf_seg_map (abfd);
11838 && ((*pm)->p_type == PT_PHDR
11839 || (*pm)->p_type == PT_INTERP))
11847 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11848 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11849 PT_MIPS_OPTIONS segment immediately following the program header
11851 if (NEWABI_P (abfd)
11852 /* On non-IRIX6 new abi, we'll have already created a segment
11853 for this section, so don't create another. I'm not sure this
11854 is not also the case for IRIX 6, but I can't test it right
11856 && IRIX_COMPAT (abfd) == ict_irix6)
11858 for (s = abfd->sections; s; s = s->next)
11859 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11864 struct elf_segment_map *options_segment;
11866 pm = &elf_seg_map (abfd);
11868 && ((*pm)->p_type == PT_PHDR
11869 || (*pm)->p_type == PT_INTERP))
11872 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11874 amt = sizeof (struct elf_segment_map);
11875 options_segment = bfd_zalloc (abfd, amt);
11876 options_segment->next = *pm;
11877 options_segment->p_type = PT_MIPS_OPTIONS;
11878 options_segment->p_flags = PF_R;
11879 options_segment->p_flags_valid = TRUE;
11880 options_segment->count = 1;
11881 options_segment->sections[0] = s;
11882 *pm = options_segment;
11888 if (IRIX_COMPAT (abfd) == ict_irix5)
11890 /* If there are .dynamic and .mdebug sections, we make a room
11891 for the RTPROC header. FIXME: Rewrite without section names. */
11892 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11893 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11894 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11896 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11897 if (m->p_type == PT_MIPS_RTPROC)
11902 m = bfd_zalloc (abfd, amt);
11906 m->p_type = PT_MIPS_RTPROC;
11908 s = bfd_get_section_by_name (abfd, ".rtproc");
11913 m->p_flags_valid = 1;
11918 m->sections[0] = s;
11921 /* We want to put it after the DYNAMIC segment. */
11922 pm = &elf_seg_map (abfd);
11923 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11933 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
11934 .dynstr, .dynsym, and .hash sections, and everything in
11936 for (pm = &elf_seg_map (abfd); *pm != NULL;
11938 if ((*pm)->p_type == PT_DYNAMIC)
11941 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11942 glibc's dynamic linker has traditionally derived the number of
11943 tags from the p_filesz field, and sometimes allocates stack
11944 arrays of that size. An overly-big PT_DYNAMIC segment can
11945 be actively harmful in such cases. Making PT_DYNAMIC contain
11946 other sections can also make life hard for the prelinker,
11947 which might move one of the other sections to a different
11948 PT_LOAD segment. */
11949 if (SGI_COMPAT (abfd)
11952 && strcmp (m->sections[0]->name, ".dynamic") == 0)
11954 static const char *sec_names[] =
11956 ".dynamic", ".dynstr", ".dynsym", ".hash"
11960 struct elf_segment_map *n;
11962 low = ~(bfd_vma) 0;
11964 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11966 s = bfd_get_section_by_name (abfd, sec_names[i]);
11967 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11974 if (high < s->vma + sz)
11975 high = s->vma + sz;
11980 for (s = abfd->sections; s != NULL; s = s->next)
11981 if ((s->flags & SEC_LOAD) != 0
11983 && s->vma + s->size <= high)
11986 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
11987 n = bfd_zalloc (abfd, amt);
11994 for (s = abfd->sections; s != NULL; s = s->next)
11996 if ((s->flags & SEC_LOAD) != 0
11998 && s->vma + s->size <= high)
12000 n->sections[i] = s;
12009 /* Allocate a spare program header in dynamic objects so that tools
12010 like the prelinker can add an extra PT_LOAD entry.
12012 If the prelinker needs to make room for a new PT_LOAD entry, its
12013 standard procedure is to move the first (read-only) sections into
12014 the new (writable) segment. However, the MIPS ABI requires
12015 .dynamic to be in a read-only segment, and the section will often
12016 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12018 Although the prelinker could in principle move .dynamic to a
12019 writable segment, it seems better to allocate a spare program
12020 header instead, and avoid the need to move any sections.
12021 There is a long tradition of allocating spare dynamic tags,
12022 so allocating a spare program header seems like a natural
12025 If INFO is NULL, we may be copying an already prelinked binary
12026 with objcopy or strip, so do not add this header. */
12028 && !SGI_COMPAT (abfd)
12029 && bfd_get_section_by_name (abfd, ".dynamic"))
12031 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12032 if ((*pm)->p_type == PT_NULL)
12036 m = bfd_zalloc (abfd, sizeof (*m));
12040 m->p_type = PT_NULL;
12048 /* Return the section that should be marked against GC for a given
12052 _bfd_mips_elf_gc_mark_hook (asection *sec,
12053 struct bfd_link_info *info,
12054 Elf_Internal_Rela *rel,
12055 struct elf_link_hash_entry *h,
12056 Elf_Internal_Sym *sym)
12058 /* ??? Do mips16 stub sections need to be handled special? */
12061 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12063 case R_MIPS_GNU_VTINHERIT:
12064 case R_MIPS_GNU_VTENTRY:
12068 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12071 /* Update the got entry reference counts for the section being removed. */
12074 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12075 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12076 asection *sec ATTRIBUTE_UNUSED,
12077 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
12080 Elf_Internal_Shdr *symtab_hdr;
12081 struct elf_link_hash_entry **sym_hashes;
12082 bfd_signed_vma *local_got_refcounts;
12083 const Elf_Internal_Rela *rel, *relend;
12084 unsigned long r_symndx;
12085 struct elf_link_hash_entry *h;
12087 if (info->relocatable)
12090 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12091 sym_hashes = elf_sym_hashes (abfd);
12092 local_got_refcounts = elf_local_got_refcounts (abfd);
12094 relend = relocs + sec->reloc_count;
12095 for (rel = relocs; rel < relend; rel++)
12096 switch (ELF_R_TYPE (abfd, rel->r_info))
12098 case R_MIPS16_GOT16:
12099 case R_MIPS16_CALL16:
12101 case R_MIPS_CALL16:
12102 case R_MIPS_CALL_HI16:
12103 case R_MIPS_CALL_LO16:
12104 case R_MIPS_GOT_HI16:
12105 case R_MIPS_GOT_LO16:
12106 case R_MIPS_GOT_DISP:
12107 case R_MIPS_GOT_PAGE:
12108 case R_MIPS_GOT_OFST:
12109 case R_MICROMIPS_GOT16:
12110 case R_MICROMIPS_CALL16:
12111 case R_MICROMIPS_CALL_HI16:
12112 case R_MICROMIPS_CALL_LO16:
12113 case R_MICROMIPS_GOT_HI16:
12114 case R_MICROMIPS_GOT_LO16:
12115 case R_MICROMIPS_GOT_DISP:
12116 case R_MICROMIPS_GOT_PAGE:
12117 case R_MICROMIPS_GOT_OFST:
12118 /* ??? It would seem that the existing MIPS code does no sort
12119 of reference counting or whatnot on its GOT and PLT entries,
12120 so it is not possible to garbage collect them at this time. */
12131 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12132 hiding the old indirect symbol. Process additional relocation
12133 information. Also called for weakdefs, in which case we just let
12134 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12137 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12138 struct elf_link_hash_entry *dir,
12139 struct elf_link_hash_entry *ind)
12141 struct mips_elf_link_hash_entry *dirmips, *indmips;
12143 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12145 dirmips = (struct mips_elf_link_hash_entry *) dir;
12146 indmips = (struct mips_elf_link_hash_entry *) ind;
12147 /* Any absolute non-dynamic relocations against an indirect or weak
12148 definition will be against the target symbol. */
12149 if (indmips->has_static_relocs)
12150 dirmips->has_static_relocs = TRUE;
12152 if (ind->root.type != bfd_link_hash_indirect)
12155 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12156 if (indmips->readonly_reloc)
12157 dirmips->readonly_reloc = TRUE;
12158 if (indmips->no_fn_stub)
12159 dirmips->no_fn_stub = TRUE;
12160 if (indmips->fn_stub)
12162 dirmips->fn_stub = indmips->fn_stub;
12163 indmips->fn_stub = NULL;
12165 if (indmips->need_fn_stub)
12167 dirmips->need_fn_stub = TRUE;
12168 indmips->need_fn_stub = FALSE;
12170 if (indmips->call_stub)
12172 dirmips->call_stub = indmips->call_stub;
12173 indmips->call_stub = NULL;
12175 if (indmips->call_fp_stub)
12177 dirmips->call_fp_stub = indmips->call_fp_stub;
12178 indmips->call_fp_stub = NULL;
12180 if (indmips->global_got_area < dirmips->global_got_area)
12181 dirmips->global_got_area = indmips->global_got_area;
12182 if (indmips->global_got_area < GGA_NONE)
12183 indmips->global_got_area = GGA_NONE;
12184 if (indmips->has_nonpic_branches)
12185 dirmips->has_nonpic_branches = TRUE;
12188 #define PDR_SIZE 32
12191 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12192 struct bfd_link_info *info)
12195 bfd_boolean ret = FALSE;
12196 unsigned char *tdata;
12199 o = bfd_get_section_by_name (abfd, ".pdr");
12204 if (o->size % PDR_SIZE != 0)
12206 if (o->output_section != NULL
12207 && bfd_is_abs_section (o->output_section))
12210 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12214 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12215 info->keep_memory);
12222 cookie->rel = cookie->rels;
12223 cookie->relend = cookie->rels + o->reloc_count;
12225 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12227 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12236 mips_elf_section_data (o)->u.tdata = tdata;
12237 o->size -= skip * PDR_SIZE;
12243 if (! info->keep_memory)
12244 free (cookie->rels);
12250 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
12252 if (strcmp (sec->name, ".pdr") == 0)
12258 _bfd_mips_elf_write_section (bfd *output_bfd,
12259 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12260 asection *sec, bfd_byte *contents)
12262 bfd_byte *to, *from, *end;
12265 if (strcmp (sec->name, ".pdr") != 0)
12268 if (mips_elf_section_data (sec)->u.tdata == NULL)
12272 end = contents + sec->size;
12273 for (from = contents, i = 0;
12275 from += PDR_SIZE, i++)
12277 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
12280 memcpy (to, from, PDR_SIZE);
12283 bfd_set_section_contents (output_bfd, sec->output_section, contents,
12284 sec->output_offset, sec->size);
12288 /* microMIPS code retains local labels for linker relaxation. Omit them
12289 from output by default for clarity. */
12292 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12294 return _bfd_elf_is_local_label_name (abfd, sym->name);
12297 /* MIPS ELF uses a special find_nearest_line routine in order the
12298 handle the ECOFF debugging information. */
12300 struct mips_elf_find_line
12302 struct ecoff_debug_info d;
12303 struct ecoff_find_line i;
12307 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12308 asymbol **symbols, bfd_vma offset,
12309 const char **filename_ptr,
12310 const char **functionname_ptr,
12311 unsigned int *line_ptr)
12315 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12316 filename_ptr, functionname_ptr,
12320 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12321 section, symbols, offset,
12322 filename_ptr, functionname_ptr,
12323 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
12324 &elf_tdata (abfd)->dwarf2_find_line_info))
12327 msec = bfd_get_section_by_name (abfd, ".mdebug");
12330 flagword origflags;
12331 struct mips_elf_find_line *fi;
12332 const struct ecoff_debug_swap * const swap =
12333 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12335 /* If we are called during a link, mips_elf_final_link may have
12336 cleared the SEC_HAS_CONTENTS field. We force it back on here
12337 if appropriate (which it normally will be). */
12338 origflags = msec->flags;
12339 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12340 msec->flags |= SEC_HAS_CONTENTS;
12342 fi = mips_elf_tdata (abfd)->find_line_info;
12345 bfd_size_type external_fdr_size;
12348 struct fdr *fdr_ptr;
12349 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12351 fi = bfd_zalloc (abfd, amt);
12354 msec->flags = origflags;
12358 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12360 msec->flags = origflags;
12364 /* Swap in the FDR information. */
12365 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
12366 fi->d.fdr = bfd_alloc (abfd, amt);
12367 if (fi->d.fdr == NULL)
12369 msec->flags = origflags;
12372 external_fdr_size = swap->external_fdr_size;
12373 fdr_ptr = fi->d.fdr;
12374 fraw_src = (char *) fi->d.external_fdr;
12375 fraw_end = (fraw_src
12376 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12377 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
12378 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
12380 mips_elf_tdata (abfd)->find_line_info = fi;
12382 /* Note that we don't bother to ever free this information.
12383 find_nearest_line is either called all the time, as in
12384 objdump -l, so the information should be saved, or it is
12385 rarely called, as in ld error messages, so the memory
12386 wasted is unimportant. Still, it would probably be a
12387 good idea for free_cached_info to throw it away. */
12390 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12391 &fi->i, filename_ptr, functionname_ptr,
12394 msec->flags = origflags;
12398 msec->flags = origflags;
12401 /* Fall back on the generic ELF find_nearest_line routine. */
12403 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12404 filename_ptr, functionname_ptr,
12409 _bfd_mips_elf_find_inliner_info (bfd *abfd,
12410 const char **filename_ptr,
12411 const char **functionname_ptr,
12412 unsigned int *line_ptr)
12415 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12416 functionname_ptr, line_ptr,
12417 & elf_tdata (abfd)->dwarf2_find_line_info);
12422 /* When are writing out the .options or .MIPS.options section,
12423 remember the bytes we are writing out, so that we can install the
12424 GP value in the section_processing routine. */
12427 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12428 const void *location,
12429 file_ptr offset, bfd_size_type count)
12431 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
12435 if (elf_section_data (section) == NULL)
12437 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
12438 section->used_by_bfd = bfd_zalloc (abfd, amt);
12439 if (elf_section_data (section) == NULL)
12442 c = mips_elf_section_data (section)->u.tdata;
12445 c = bfd_zalloc (abfd, section->size);
12448 mips_elf_section_data (section)->u.tdata = c;
12451 memcpy (c + offset, location, count);
12454 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12458 /* This is almost identical to bfd_generic_get_... except that some
12459 MIPS relocations need to be handled specially. Sigh. */
12462 _bfd_elf_mips_get_relocated_section_contents
12464 struct bfd_link_info *link_info,
12465 struct bfd_link_order *link_order,
12467 bfd_boolean relocatable,
12470 /* Get enough memory to hold the stuff */
12471 bfd *input_bfd = link_order->u.indirect.section->owner;
12472 asection *input_section = link_order->u.indirect.section;
12475 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12476 arelent **reloc_vector = NULL;
12479 if (reloc_size < 0)
12482 reloc_vector = bfd_malloc (reloc_size);
12483 if (reloc_vector == NULL && reloc_size != 0)
12486 /* read in the section */
12487 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12488 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
12491 reloc_count = bfd_canonicalize_reloc (input_bfd,
12495 if (reloc_count < 0)
12498 if (reloc_count > 0)
12503 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12506 struct bfd_hash_entry *h;
12507 struct bfd_link_hash_entry *lh;
12508 /* Skip all this stuff if we aren't mixing formats. */
12509 if (abfd && input_bfd
12510 && abfd->xvec == input_bfd->xvec)
12514 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
12515 lh = (struct bfd_link_hash_entry *) h;
12522 case bfd_link_hash_undefined:
12523 case bfd_link_hash_undefweak:
12524 case bfd_link_hash_common:
12527 case bfd_link_hash_defined:
12528 case bfd_link_hash_defweak:
12530 gp = lh->u.def.value;
12532 case bfd_link_hash_indirect:
12533 case bfd_link_hash_warning:
12535 /* @@FIXME ignoring warning for now */
12537 case bfd_link_hash_new:
12546 for (parent = reloc_vector; *parent != NULL; parent++)
12548 char *error_message = NULL;
12549 bfd_reloc_status_type r;
12551 /* Specific to MIPS: Deal with relocation types that require
12552 knowing the gp of the output bfd. */
12553 asymbol *sym = *(*parent)->sym_ptr_ptr;
12555 /* If we've managed to find the gp and have a special
12556 function for the relocation then go ahead, else default
12557 to the generic handling. */
12559 && (*parent)->howto->special_function
12560 == _bfd_mips_elf32_gprel16_reloc)
12561 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12562 input_section, relocatable,
12565 r = bfd_perform_relocation (input_bfd, *parent, data,
12567 relocatable ? abfd : NULL,
12572 asection *os = input_section->output_section;
12574 /* A partial link, so keep the relocs */
12575 os->orelocation[os->reloc_count] = *parent;
12579 if (r != bfd_reloc_ok)
12583 case bfd_reloc_undefined:
12584 if (!((*link_info->callbacks->undefined_symbol)
12585 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12586 input_bfd, input_section, (*parent)->address, TRUE)))
12589 case bfd_reloc_dangerous:
12590 BFD_ASSERT (error_message != NULL);
12591 if (!((*link_info->callbacks->reloc_dangerous)
12592 (link_info, error_message, input_bfd, input_section,
12593 (*parent)->address)))
12596 case bfd_reloc_overflow:
12597 if (!((*link_info->callbacks->reloc_overflow)
12599 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12600 (*parent)->howto->name, (*parent)->addend,
12601 input_bfd, input_section, (*parent)->address)))
12604 case bfd_reloc_outofrange:
12613 if (reloc_vector != NULL)
12614 free (reloc_vector);
12618 if (reloc_vector != NULL)
12619 free (reloc_vector);
12624 mips_elf_relax_delete_bytes (bfd *abfd,
12625 asection *sec, bfd_vma addr, int count)
12627 Elf_Internal_Shdr *symtab_hdr;
12628 unsigned int sec_shndx;
12629 bfd_byte *contents;
12630 Elf_Internal_Rela *irel, *irelend;
12631 Elf_Internal_Sym *isym;
12632 Elf_Internal_Sym *isymend;
12633 struct elf_link_hash_entry **sym_hashes;
12634 struct elf_link_hash_entry **end_hashes;
12635 struct elf_link_hash_entry **start_hashes;
12636 unsigned int symcount;
12638 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12639 contents = elf_section_data (sec)->this_hdr.contents;
12641 irel = elf_section_data (sec)->relocs;
12642 irelend = irel + sec->reloc_count;
12644 /* Actually delete the bytes. */
12645 memmove (contents + addr, contents + addr + count,
12646 (size_t) (sec->size - addr - count));
12647 sec->size -= count;
12649 /* Adjust all the relocs. */
12650 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12652 /* Get the new reloc address. */
12653 if (irel->r_offset > addr)
12654 irel->r_offset -= count;
12657 BFD_ASSERT (addr % 2 == 0);
12658 BFD_ASSERT (count % 2 == 0);
12660 /* Adjust the local symbols defined in this section. */
12661 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12662 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12663 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
12664 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
12665 isym->st_value -= count;
12667 /* Now adjust the global symbols defined in this section. */
12668 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12669 - symtab_hdr->sh_info);
12670 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12671 end_hashes = sym_hashes + symcount;
12673 for (; sym_hashes < end_hashes; sym_hashes++)
12675 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12677 if ((sym_hash->root.type == bfd_link_hash_defined
12678 || sym_hash->root.type == bfd_link_hash_defweak)
12679 && sym_hash->root.u.def.section == sec)
12681 bfd_vma value = sym_hash->root.u.def.value;
12683 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12684 value &= MINUS_TWO;
12686 sym_hash->root.u.def.value -= count;
12694 /* Opcodes needed for microMIPS relaxation as found in
12695 opcodes/micromips-opc.c. */
12697 struct opcode_descriptor {
12698 unsigned long match;
12699 unsigned long mask;
12702 /* The $ra register aka $31. */
12706 /* 32-bit instruction format register fields. */
12708 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12709 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12711 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
12713 #define OP16_VALID_REG(r) \
12714 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12717 /* 32-bit and 16-bit branches. */
12719 static const struct opcode_descriptor b_insns_32[] = {
12720 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12721 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12722 { 0, 0 } /* End marker for find_match(). */
12725 static const struct opcode_descriptor bc_insn_32 =
12726 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12728 static const struct opcode_descriptor bz_insn_32 =
12729 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12731 static const struct opcode_descriptor bzal_insn_32 =
12732 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12734 static const struct opcode_descriptor beq_insn_32 =
12735 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12737 static const struct opcode_descriptor b_insn_16 =
12738 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12740 static const struct opcode_descriptor bz_insn_16 =
12741 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
12744 /* 32-bit and 16-bit branch EQ and NE zero. */
12746 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12747 eq and second the ne. This convention is used when replacing a
12748 32-bit BEQ/BNE with the 16-bit version. */
12750 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12752 static const struct opcode_descriptor bz_rs_insns_32[] = {
12753 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12754 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12755 { 0, 0 } /* End marker for find_match(). */
12758 static const struct opcode_descriptor bz_rt_insns_32[] = {
12759 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12760 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12761 { 0, 0 } /* End marker for find_match(). */
12764 static const struct opcode_descriptor bzc_insns_32[] = {
12765 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12766 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12767 { 0, 0 } /* End marker for find_match(). */
12770 static const struct opcode_descriptor bz_insns_16[] = {
12771 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12772 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12773 { 0, 0 } /* End marker for find_match(). */
12776 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12778 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12779 #define BZ16_REG_FIELD(r) \
12780 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12783 /* 32-bit instructions with a delay slot. */
12785 static const struct opcode_descriptor jal_insn_32_bd16 =
12786 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12788 static const struct opcode_descriptor jal_insn_32_bd32 =
12789 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12791 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12792 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12794 static const struct opcode_descriptor j_insn_32 =
12795 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12797 static const struct opcode_descriptor jalr_insn_32 =
12798 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12800 /* This table can be compacted, because no opcode replacement is made. */
12802 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12803 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12805 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12806 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12808 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12809 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12810 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12811 { 0, 0 } /* End marker for find_match(). */
12814 /* This table can be compacted, because no opcode replacement is made. */
12816 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12817 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12819 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12820 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12821 { 0, 0 } /* End marker for find_match(). */
12825 /* 16-bit instructions with a delay slot. */
12827 static const struct opcode_descriptor jalr_insn_16_bd16 =
12828 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12830 static const struct opcode_descriptor jalr_insn_16_bd32 =
12831 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12833 static const struct opcode_descriptor jr_insn_16 =
12834 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12836 #define JR16_REG(opcode) ((opcode) & 0x1f)
12838 /* This table can be compacted, because no opcode replacement is made. */
12840 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12841 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12843 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12844 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12845 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12846 { 0, 0 } /* End marker for find_match(). */
12850 /* LUI instruction. */
12852 static const struct opcode_descriptor lui_insn =
12853 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12856 /* ADDIU instruction. */
12858 static const struct opcode_descriptor addiu_insn =
12859 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12861 static const struct opcode_descriptor addiupc_insn =
12862 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12864 #define ADDIUPC_REG_FIELD(r) \
12865 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12868 /* Relaxable instructions in a JAL delay slot: MOVE. */
12870 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12871 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12872 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12873 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12875 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12876 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12878 static const struct opcode_descriptor move_insns_32[] = {
12879 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12880 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12881 { 0, 0 } /* End marker for find_match(). */
12884 static const struct opcode_descriptor move_insn_16 =
12885 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12888 /* NOP instructions. */
12890 static const struct opcode_descriptor nop_insn_32 =
12891 { /* "nop", "", */ 0x00000000, 0xffffffff };
12893 static const struct opcode_descriptor nop_insn_16 =
12894 { /* "nop", "", */ 0x0c00, 0xffff };
12897 /* Instruction match support. */
12899 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12902 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12904 unsigned long indx;
12906 for (indx = 0; insn[indx].mask != 0; indx++)
12907 if (MATCH (opcode, insn[indx]))
12914 /* Branch and delay slot decoding support. */
12916 /* If PTR points to what *might* be a 16-bit branch or jump, then
12917 return the minimum length of its delay slot, otherwise return 0.
12918 Non-zero results are not definitive as we might be checking against
12919 the second half of another instruction. */
12922 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12924 unsigned long opcode;
12927 opcode = bfd_get_16 (abfd, ptr);
12928 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12929 /* 16-bit branch/jump with a 32-bit delay slot. */
12931 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12932 || find_match (opcode, ds_insns_16_bd16) >= 0)
12933 /* 16-bit branch/jump with a 16-bit delay slot. */
12936 /* No delay slot. */
12942 /* If PTR points to what *might* be a 32-bit branch or jump, then
12943 return the minimum length of its delay slot, otherwise return 0.
12944 Non-zero results are not definitive as we might be checking against
12945 the second half of another instruction. */
12948 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12950 unsigned long opcode;
12953 opcode = bfd_get_micromips_32 (abfd, ptr);
12954 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12955 /* 32-bit branch/jump with a 32-bit delay slot. */
12957 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12958 /* 32-bit branch/jump with a 16-bit delay slot. */
12961 /* No delay slot. */
12967 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12968 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12971 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12973 unsigned long opcode;
12975 opcode = bfd_get_16 (abfd, ptr);
12976 if (MATCH (opcode, b_insn_16)
12978 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12980 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12981 /* BEQZ16, BNEZ16 */
12982 || (MATCH (opcode, jalr_insn_16_bd32)
12984 && reg != JR16_REG (opcode) && reg != RA))
12990 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12991 then return TRUE, otherwise FALSE. */
12994 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12996 unsigned long opcode;
12998 opcode = bfd_get_micromips_32 (abfd, ptr);
12999 if (MATCH (opcode, j_insn_32)
13001 || MATCH (opcode, bc_insn_32)
13002 /* BC1F, BC1T, BC2F, BC2T */
13003 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13005 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13006 /* BGEZ, BGTZ, BLEZ, BLTZ */
13007 || (MATCH (opcode, bzal_insn_32)
13008 /* BGEZAL, BLTZAL */
13009 && reg != OP32_SREG (opcode) && reg != RA)
13010 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13011 /* JALR, JALR.HB, BEQ, BNE */
13012 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13018 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13019 IRELEND) at OFFSET indicate that there must be a compact branch there,
13020 then return TRUE, otherwise FALSE. */
13023 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13024 const Elf_Internal_Rela *internal_relocs,
13025 const Elf_Internal_Rela *irelend)
13027 const Elf_Internal_Rela *irel;
13028 unsigned long opcode;
13030 opcode = bfd_get_micromips_32 (abfd, ptr);
13031 if (find_match (opcode, bzc_insns_32) < 0)
13034 for (irel = internal_relocs; irel < irelend; irel++)
13035 if (irel->r_offset == offset
13036 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13042 /* Bitsize checking. */
13043 #define IS_BITSIZE(val, N) \
13044 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13045 - (1ULL << ((N) - 1))) == (val))
13049 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13050 struct bfd_link_info *link_info,
13051 bfd_boolean *again)
13053 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13054 Elf_Internal_Shdr *symtab_hdr;
13055 Elf_Internal_Rela *internal_relocs;
13056 Elf_Internal_Rela *irel, *irelend;
13057 bfd_byte *contents = NULL;
13058 Elf_Internal_Sym *isymbuf = NULL;
13060 /* Assume nothing changes. */
13063 /* We don't have to do anything for a relocatable link, if
13064 this section does not have relocs, or if this is not a
13067 if (link_info->relocatable
13068 || (sec->flags & SEC_RELOC) == 0
13069 || sec->reloc_count == 0
13070 || (sec->flags & SEC_CODE) == 0)
13073 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13075 /* Get a copy of the native relocations. */
13076 internal_relocs = (_bfd_elf_link_read_relocs
13077 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13078 link_info->keep_memory));
13079 if (internal_relocs == NULL)
13082 /* Walk through them looking for relaxing opportunities. */
13083 irelend = internal_relocs + sec->reloc_count;
13084 for (irel = internal_relocs; irel < irelend; irel++)
13086 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13087 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13088 bfd_boolean target_is_micromips_code_p;
13089 unsigned long opcode;
13095 /* The number of bytes to delete for relaxation and from where
13096 to delete these bytes starting at irel->r_offset. */
13100 /* If this isn't something that can be relaxed, then ignore
13102 if (r_type != R_MICROMIPS_HI16
13103 && r_type != R_MICROMIPS_PC16_S1
13104 && r_type != R_MICROMIPS_26_S1)
13107 /* Get the section contents if we haven't done so already. */
13108 if (contents == NULL)
13110 /* Get cached copy if it exists. */
13111 if (elf_section_data (sec)->this_hdr.contents != NULL)
13112 contents = elf_section_data (sec)->this_hdr.contents;
13113 /* Go get them off disk. */
13114 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13117 ptr = contents + irel->r_offset;
13119 /* Read this BFD's local symbols if we haven't done so already. */
13120 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13122 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13123 if (isymbuf == NULL)
13124 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13125 symtab_hdr->sh_info, 0,
13127 if (isymbuf == NULL)
13131 /* Get the value of the symbol referred to by the reloc. */
13132 if (r_symndx < symtab_hdr->sh_info)
13134 /* A local symbol. */
13135 Elf_Internal_Sym *isym;
13138 isym = isymbuf + r_symndx;
13139 if (isym->st_shndx == SHN_UNDEF)
13140 sym_sec = bfd_und_section_ptr;
13141 else if (isym->st_shndx == SHN_ABS)
13142 sym_sec = bfd_abs_section_ptr;
13143 else if (isym->st_shndx == SHN_COMMON)
13144 sym_sec = bfd_com_section_ptr;
13146 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13147 symval = (isym->st_value
13148 + sym_sec->output_section->vma
13149 + sym_sec->output_offset);
13150 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13154 unsigned long indx;
13155 struct elf_link_hash_entry *h;
13157 /* An external symbol. */
13158 indx = r_symndx - symtab_hdr->sh_info;
13159 h = elf_sym_hashes (abfd)[indx];
13160 BFD_ASSERT (h != NULL);
13162 if (h->root.type != bfd_link_hash_defined
13163 && h->root.type != bfd_link_hash_defweak)
13164 /* This appears to be a reference to an undefined
13165 symbol. Just ignore it -- it will be caught by the
13166 regular reloc processing. */
13169 symval = (h->root.u.def.value
13170 + h->root.u.def.section->output_section->vma
13171 + h->root.u.def.section->output_offset);
13172 target_is_micromips_code_p = (!h->needs_plt
13173 && ELF_ST_IS_MICROMIPS (h->other));
13177 /* For simplicity of coding, we are going to modify the
13178 section contents, the section relocs, and the BFD symbol
13179 table. We must tell the rest of the code not to free up this
13180 information. It would be possible to instead create a table
13181 of changes which have to be made, as is done in coff-mips.c;
13182 that would be more work, but would require less memory when
13183 the linker is run. */
13185 /* Only 32-bit instructions relaxed. */
13186 if (irel->r_offset + 4 > sec->size)
13189 opcode = bfd_get_micromips_32 (abfd, ptr);
13191 /* This is the pc-relative distance from the instruction the
13192 relocation is applied to, to the symbol referred. */
13194 - (sec->output_section->vma + sec->output_offset)
13197 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13198 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13199 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13201 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13203 where pcrval has first to be adjusted to apply against the LO16
13204 location (we make the adjustment later on, when we have figured
13205 out the offset). */
13206 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13208 bfd_boolean bzc = FALSE;
13209 unsigned long nextopc;
13213 /* Give up if the previous reloc was a HI16 against this symbol
13215 if (irel > internal_relocs
13216 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13217 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13220 /* Or if the next reloc is not a LO16 against this symbol. */
13221 if (irel + 1 >= irelend
13222 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13223 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13226 /* Or if the second next reloc is a LO16 against this symbol too. */
13227 if (irel + 2 >= irelend
13228 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13229 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13232 /* See if the LUI instruction *might* be in a branch delay slot.
13233 We check whether what looks like a 16-bit branch or jump is
13234 actually an immediate argument to a compact branch, and let
13235 it through if so. */
13236 if (irel->r_offset >= 2
13237 && check_br16_dslot (abfd, ptr - 2)
13238 && !(irel->r_offset >= 4
13239 && (bzc = check_relocated_bzc (abfd,
13240 ptr - 4, irel->r_offset - 4,
13241 internal_relocs, irelend))))
13243 if (irel->r_offset >= 4
13245 && check_br32_dslot (abfd, ptr - 4))
13248 reg = OP32_SREG (opcode);
13250 /* We only relax adjacent instructions or ones separated with
13251 a branch or jump that has a delay slot. The branch or jump
13252 must not fiddle with the register used to hold the address.
13253 Subtract 4 for the LUI itself. */
13254 offset = irel[1].r_offset - irel[0].r_offset;
13255 switch (offset - 4)
13260 if (check_br16 (abfd, ptr + 4, reg))
13264 if (check_br32 (abfd, ptr + 4, reg))
13271 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
13273 /* Give up unless the same register is used with both
13275 if (OP32_SREG (nextopc) != reg)
13278 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13279 and rounding up to take masking of the two LSBs into account. */
13280 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13282 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13283 if (IS_BITSIZE (symval, 16))
13285 /* Fix the relocation's type. */
13286 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13288 /* Instructions using R_MICROMIPS_LO16 have the base or
13289 source register in bits 20:16. This register becomes $0
13290 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13291 nextopc &= ~0x001f0000;
13292 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13293 contents + irel[1].r_offset);
13296 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13297 We add 4 to take LUI deletion into account while checking
13298 the PC-relative distance. */
13299 else if (symval % 4 == 0
13300 && IS_BITSIZE (pcrval + 4, 25)
13301 && MATCH (nextopc, addiu_insn)
13302 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13303 && OP16_VALID_REG (OP32_TREG (nextopc)))
13305 /* Fix the relocation's type. */
13306 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13308 /* Replace ADDIU with the ADDIUPC version. */
13309 nextopc = (addiupc_insn.match
13310 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13312 bfd_put_micromips_32 (abfd, nextopc,
13313 contents + irel[1].r_offset);
13316 /* Can't do anything, give up, sigh... */
13320 /* Fix the relocation's type. */
13321 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13323 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13328 /* Compact branch relaxation -- due to the multitude of macros
13329 employed by the compiler/assembler, compact branches are not
13330 always generated. Obviously, this can/will be fixed elsewhere,
13331 but there is no drawback in double checking it here. */
13332 else if (r_type == R_MICROMIPS_PC16_S1
13333 && irel->r_offset + 5 < sec->size
13334 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13335 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
13337 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13338 nop_insn_16) ? 2 : 0))
13339 || (irel->r_offset + 7 < sec->size
13340 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13342 nop_insn_32) ? 4 : 0))))
13346 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13348 /* Replace BEQZ/BNEZ with the compact version. */
13349 opcode = (bzc_insns_32[fndopc].match
13350 | BZC32_REG_FIELD (reg)
13351 | (opcode & 0xffff)); /* Addend value. */
13353 bfd_put_micromips_32 (abfd, opcode, ptr);
13355 /* Delete the delay slot NOP: two or four bytes from
13356 irel->offset + 4; delcnt has already been set above. */
13360 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13361 to check the distance from the next instruction, so subtract 2. */
13363 && r_type == R_MICROMIPS_PC16_S1
13364 && IS_BITSIZE (pcrval - 2, 11)
13365 && find_match (opcode, b_insns_32) >= 0)
13367 /* Fix the relocation's type. */
13368 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13370 /* Replace the 32-bit opcode with a 16-bit opcode. */
13373 | (opcode & 0x3ff)), /* Addend value. */
13376 /* Delete 2 bytes from irel->r_offset + 2. */
13381 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13382 to check the distance from the next instruction, so subtract 2. */
13384 && r_type == R_MICROMIPS_PC16_S1
13385 && IS_BITSIZE (pcrval - 2, 8)
13386 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13387 && OP16_VALID_REG (OP32_SREG (opcode)))
13388 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13389 && OP16_VALID_REG (OP32_TREG (opcode)))))
13393 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13395 /* Fix the relocation's type. */
13396 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13398 /* Replace the 32-bit opcode with a 16-bit opcode. */
13400 (bz_insns_16[fndopc].match
13401 | BZ16_REG_FIELD (reg)
13402 | (opcode & 0x7f)), /* Addend value. */
13405 /* Delete 2 bytes from irel->r_offset + 2. */
13410 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
13412 && r_type == R_MICROMIPS_26_S1
13413 && target_is_micromips_code_p
13414 && irel->r_offset + 7 < sec->size
13415 && MATCH (opcode, jal_insn_32_bd32))
13417 unsigned long n32opc;
13418 bfd_boolean relaxed = FALSE;
13420 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
13422 if (MATCH (n32opc, nop_insn_32))
13424 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
13425 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
13429 else if (find_match (n32opc, move_insns_32) >= 0)
13431 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13433 (move_insn_16.match
13434 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13435 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
13440 /* Other 32-bit instructions relaxable to 16-bit
13441 instructions will be handled here later. */
13445 /* JAL with 32-bit delay slot that is changed to a JALS
13446 with 16-bit delay slot. */
13447 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
13449 /* Delete 2 bytes from irel->r_offset + 6. */
13457 /* Note that we've changed the relocs, section contents, etc. */
13458 elf_section_data (sec)->relocs = internal_relocs;
13459 elf_section_data (sec)->this_hdr.contents = contents;
13460 symtab_hdr->contents = (unsigned char *) isymbuf;
13462 /* Delete bytes depending on the delcnt and deloff. */
13463 if (!mips_elf_relax_delete_bytes (abfd, sec,
13464 irel->r_offset + deloff, delcnt))
13467 /* That will change things, so we should relax again.
13468 Note that this is not required, and it may be slow. */
13473 if (isymbuf != NULL
13474 && symtab_hdr->contents != (unsigned char *) isymbuf)
13476 if (! link_info->keep_memory)
13480 /* Cache the symbols for elf_link_input_bfd. */
13481 symtab_hdr->contents = (unsigned char *) isymbuf;
13485 if (contents != NULL
13486 && elf_section_data (sec)->this_hdr.contents != contents)
13488 if (! link_info->keep_memory)
13492 /* Cache the section contents for elf_link_input_bfd. */
13493 elf_section_data (sec)->this_hdr.contents = contents;
13497 if (internal_relocs != NULL
13498 && elf_section_data (sec)->relocs != internal_relocs)
13499 free (internal_relocs);
13504 if (isymbuf != NULL
13505 && symtab_hdr->contents != (unsigned char *) isymbuf)
13507 if (contents != NULL
13508 && elf_section_data (sec)->this_hdr.contents != contents)
13510 if (internal_relocs != NULL
13511 && elf_section_data (sec)->relocs != internal_relocs)
13512 free (internal_relocs);
13517 /* Create a MIPS ELF linker hash table. */
13519 struct bfd_link_hash_table *
13520 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
13522 struct mips_elf_link_hash_table *ret;
13523 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13525 ret = bfd_zmalloc (amt);
13529 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13530 mips_elf_link_hash_newfunc,
13531 sizeof (struct mips_elf_link_hash_entry),
13537 ret->root.init_plt_refcount.plist = NULL;
13538 ret->root.init_plt_offset.plist = NULL;
13540 return &ret->root.root;
13543 /* Likewise, but indicate that the target is VxWorks. */
13545 struct bfd_link_hash_table *
13546 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13548 struct bfd_link_hash_table *ret;
13550 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13553 struct mips_elf_link_hash_table *htab;
13555 htab = (struct mips_elf_link_hash_table *) ret;
13556 htab->use_plts_and_copy_relocs = TRUE;
13557 htab->is_vxworks = TRUE;
13562 /* A function that the linker calls if we are allowed to use PLTs
13563 and copy relocs. */
13566 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13568 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13571 /* A function that the linker calls to select between all or only
13572 32-bit microMIPS instructions. */
13575 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13577 mips_elf_hash_table (info)->insn32 = on;
13580 /* We need to use a special link routine to handle the .reginfo and
13581 the .mdebug sections. We need to merge all instances of these
13582 sections together, not write them all out sequentially. */
13585 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
13588 struct bfd_link_order *p;
13589 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13590 asection *rtproc_sec;
13591 Elf32_RegInfo reginfo;
13592 struct ecoff_debug_info debug;
13593 struct mips_htab_traverse_info hti;
13594 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13595 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
13596 HDRR *symhdr = &debug.symbolic_header;
13597 void *mdebug_handle = NULL;
13602 struct mips_elf_link_hash_table *htab;
13604 static const char * const secname[] =
13606 ".text", ".init", ".fini", ".data",
13607 ".rodata", ".sdata", ".sbss", ".bss"
13609 static const int sc[] =
13611 scText, scInit, scFini, scData,
13612 scRData, scSData, scSBss, scBss
13615 /* Sort the dynamic symbols so that those with GOT entries come after
13617 htab = mips_elf_hash_table (info);
13618 BFD_ASSERT (htab != NULL);
13620 if (!mips_elf_sort_hash_table (abfd, info))
13623 /* Create any scheduled LA25 stubs. */
13625 hti.output_bfd = abfd;
13627 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13631 /* Get a value for the GP register. */
13632 if (elf_gp (abfd) == 0)
13634 struct bfd_link_hash_entry *h;
13636 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
13637 if (h != NULL && h->type == bfd_link_hash_defined)
13638 elf_gp (abfd) = (h->u.def.value
13639 + h->u.def.section->output_section->vma
13640 + h->u.def.section->output_offset);
13641 else if (htab->is_vxworks
13642 && (h = bfd_link_hash_lookup (info->hash,
13643 "_GLOBAL_OFFSET_TABLE_",
13644 FALSE, FALSE, TRUE))
13645 && h->type == bfd_link_hash_defined)
13646 elf_gp (abfd) = (h->u.def.section->output_section->vma
13647 + h->u.def.section->output_offset
13649 else if (info->relocatable)
13651 bfd_vma lo = MINUS_ONE;
13653 /* Find the GP-relative section with the lowest offset. */
13654 for (o = abfd->sections; o != NULL; o = o->next)
13656 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13659 /* And calculate GP relative to that. */
13660 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
13664 /* If the relocate_section function needs to do a reloc
13665 involving the GP value, it should make a reloc_dangerous
13666 callback to warn that GP is not defined. */
13670 /* Go through the sections and collect the .reginfo and .mdebug
13672 reginfo_sec = NULL;
13674 gptab_data_sec = NULL;
13675 gptab_bss_sec = NULL;
13676 for (o = abfd->sections; o != NULL; o = o->next)
13678 if (strcmp (o->name, ".reginfo") == 0)
13680 memset (®info, 0, sizeof reginfo);
13682 /* We have found the .reginfo section in the output file.
13683 Look through all the link_orders comprising it and merge
13684 the information together. */
13685 for (p = o->map_head.link_order; p != NULL; p = p->next)
13687 asection *input_section;
13689 Elf32_External_RegInfo ext;
13692 if (p->type != bfd_indirect_link_order)
13694 if (p->type == bfd_data_link_order)
13699 input_section = p->u.indirect.section;
13700 input_bfd = input_section->owner;
13702 if (! bfd_get_section_contents (input_bfd, input_section,
13703 &ext, 0, sizeof ext))
13706 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13708 reginfo.ri_gprmask |= sub.ri_gprmask;
13709 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13710 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13711 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13712 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13714 /* ri_gp_value is set by the function
13715 mips_elf32_section_processing when the section is
13716 finally written out. */
13718 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13719 elf_link_input_bfd ignores this section. */
13720 input_section->flags &= ~SEC_HAS_CONTENTS;
13723 /* Size has been set in _bfd_mips_elf_always_size_sections. */
13724 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
13726 /* Skip this section later on (I don't think this currently
13727 matters, but someday it might). */
13728 o->map_head.link_order = NULL;
13733 if (strcmp (o->name, ".mdebug") == 0)
13735 struct extsym_info einfo;
13738 /* We have found the .mdebug section in the output file.
13739 Look through all the link_orders comprising it and merge
13740 the information together. */
13741 symhdr->magic = swap->sym_magic;
13742 /* FIXME: What should the version stamp be? */
13743 symhdr->vstamp = 0;
13744 symhdr->ilineMax = 0;
13745 symhdr->cbLine = 0;
13746 symhdr->idnMax = 0;
13747 symhdr->ipdMax = 0;
13748 symhdr->isymMax = 0;
13749 symhdr->ioptMax = 0;
13750 symhdr->iauxMax = 0;
13751 symhdr->issMax = 0;
13752 symhdr->issExtMax = 0;
13753 symhdr->ifdMax = 0;
13755 symhdr->iextMax = 0;
13757 /* We accumulate the debugging information itself in the
13758 debug_info structure. */
13760 debug.external_dnr = NULL;
13761 debug.external_pdr = NULL;
13762 debug.external_sym = NULL;
13763 debug.external_opt = NULL;
13764 debug.external_aux = NULL;
13766 debug.ssext = debug.ssext_end = NULL;
13767 debug.external_fdr = NULL;
13768 debug.external_rfd = NULL;
13769 debug.external_ext = debug.external_ext_end = NULL;
13771 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
13772 if (mdebug_handle == NULL)
13776 esym.cobol_main = 0;
13780 esym.asym.iss = issNil;
13781 esym.asym.st = stLocal;
13782 esym.asym.reserved = 0;
13783 esym.asym.index = indexNil;
13785 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13787 esym.asym.sc = sc[i];
13788 s = bfd_get_section_by_name (abfd, secname[i]);
13791 esym.asym.value = s->vma;
13792 last = s->vma + s->size;
13795 esym.asym.value = last;
13796 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13797 secname[i], &esym))
13801 for (p = o->map_head.link_order; p != NULL; p = p->next)
13803 asection *input_section;
13805 const struct ecoff_debug_swap *input_swap;
13806 struct ecoff_debug_info input_debug;
13810 if (p->type != bfd_indirect_link_order)
13812 if (p->type == bfd_data_link_order)
13817 input_section = p->u.indirect.section;
13818 input_bfd = input_section->owner;
13820 if (!is_mips_elf (input_bfd))
13822 /* I don't know what a non MIPS ELF bfd would be
13823 doing with a .mdebug section, but I don't really
13824 want to deal with it. */
13828 input_swap = (get_elf_backend_data (input_bfd)
13829 ->elf_backend_ecoff_debug_swap);
13831 BFD_ASSERT (p->size == input_section->size);
13833 /* The ECOFF linking code expects that we have already
13834 read in the debugging information and set up an
13835 ecoff_debug_info structure, so we do that now. */
13836 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13840 if (! (bfd_ecoff_debug_accumulate
13841 (mdebug_handle, abfd, &debug, swap, input_bfd,
13842 &input_debug, input_swap, info)))
13845 /* Loop through the external symbols. For each one with
13846 interesting information, try to find the symbol in
13847 the linker global hash table and save the information
13848 for the output external symbols. */
13849 eraw_src = input_debug.external_ext;
13850 eraw_end = (eraw_src
13851 + (input_debug.symbolic_header.iextMax
13852 * input_swap->external_ext_size));
13854 eraw_src < eraw_end;
13855 eraw_src += input_swap->external_ext_size)
13859 struct mips_elf_link_hash_entry *h;
13861 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
13862 if (ext.asym.sc == scNil
13863 || ext.asym.sc == scUndefined
13864 || ext.asym.sc == scSUndefined)
13867 name = input_debug.ssext + ext.asym.iss;
13868 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
13869 name, FALSE, FALSE, TRUE);
13870 if (h == NULL || h->esym.ifd != -2)
13875 BFD_ASSERT (ext.ifd
13876 < input_debug.symbolic_header.ifdMax);
13877 ext.ifd = input_debug.ifdmap[ext.ifd];
13883 /* Free up the information we just read. */
13884 free (input_debug.line);
13885 free (input_debug.external_dnr);
13886 free (input_debug.external_pdr);
13887 free (input_debug.external_sym);
13888 free (input_debug.external_opt);
13889 free (input_debug.external_aux);
13890 free (input_debug.ss);
13891 free (input_debug.ssext);
13892 free (input_debug.external_fdr);
13893 free (input_debug.external_rfd);
13894 free (input_debug.external_ext);
13896 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13897 elf_link_input_bfd ignores this section. */
13898 input_section->flags &= ~SEC_HAS_CONTENTS;
13901 if (SGI_COMPAT (abfd) && info->shared)
13903 /* Create .rtproc section. */
13904 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
13905 if (rtproc_sec == NULL)
13907 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13908 | SEC_LINKER_CREATED | SEC_READONLY);
13910 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13913 if (rtproc_sec == NULL
13914 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
13918 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13924 /* Build the external symbol information. */
13927 einfo.debug = &debug;
13929 einfo.failed = FALSE;
13930 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
13931 mips_elf_output_extsym, &einfo);
13935 /* Set the size of the .mdebug section. */
13936 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
13938 /* Skip this section later on (I don't think this currently
13939 matters, but someday it might). */
13940 o->map_head.link_order = NULL;
13945 if (CONST_STRNEQ (o->name, ".gptab."))
13947 const char *subname;
13950 Elf32_External_gptab *ext_tab;
13953 /* The .gptab.sdata and .gptab.sbss sections hold
13954 information describing how the small data area would
13955 change depending upon the -G switch. These sections
13956 not used in executables files. */
13957 if (! info->relocatable)
13959 for (p = o->map_head.link_order; p != NULL; p = p->next)
13961 asection *input_section;
13963 if (p->type != bfd_indirect_link_order)
13965 if (p->type == bfd_data_link_order)
13970 input_section = p->u.indirect.section;
13972 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13973 elf_link_input_bfd ignores this section. */
13974 input_section->flags &= ~SEC_HAS_CONTENTS;
13977 /* Skip this section later on (I don't think this
13978 currently matters, but someday it might). */
13979 o->map_head.link_order = NULL;
13981 /* Really remove the section. */
13982 bfd_section_list_remove (abfd, o);
13983 --abfd->section_count;
13988 /* There is one gptab for initialized data, and one for
13989 uninitialized data. */
13990 if (strcmp (o->name, ".gptab.sdata") == 0)
13991 gptab_data_sec = o;
13992 else if (strcmp (o->name, ".gptab.sbss") == 0)
13996 (*_bfd_error_handler)
13997 (_("%s: illegal section name `%s'"),
13998 bfd_get_filename (abfd), o->name);
13999 bfd_set_error (bfd_error_nonrepresentable_section);
14003 /* The linker script always combines .gptab.data and
14004 .gptab.sdata into .gptab.sdata, and likewise for
14005 .gptab.bss and .gptab.sbss. It is possible that there is
14006 no .sdata or .sbss section in the output file, in which
14007 case we must change the name of the output section. */
14008 subname = o->name + sizeof ".gptab" - 1;
14009 if (bfd_get_section_by_name (abfd, subname) == NULL)
14011 if (o == gptab_data_sec)
14012 o->name = ".gptab.data";
14014 o->name = ".gptab.bss";
14015 subname = o->name + sizeof ".gptab" - 1;
14016 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14019 /* Set up the first entry. */
14021 amt = c * sizeof (Elf32_gptab);
14022 tab = bfd_malloc (amt);
14025 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14026 tab[0].gt_header.gt_unused = 0;
14028 /* Combine the input sections. */
14029 for (p = o->map_head.link_order; p != NULL; p = p->next)
14031 asection *input_section;
14033 bfd_size_type size;
14034 unsigned long last;
14035 bfd_size_type gpentry;
14037 if (p->type != bfd_indirect_link_order)
14039 if (p->type == bfd_data_link_order)
14044 input_section = p->u.indirect.section;
14045 input_bfd = input_section->owner;
14047 /* Combine the gptab entries for this input section one
14048 by one. We know that the input gptab entries are
14049 sorted by ascending -G value. */
14050 size = input_section->size;
14052 for (gpentry = sizeof (Elf32_External_gptab);
14054 gpentry += sizeof (Elf32_External_gptab))
14056 Elf32_External_gptab ext_gptab;
14057 Elf32_gptab int_gptab;
14063 if (! (bfd_get_section_contents
14064 (input_bfd, input_section, &ext_gptab, gpentry,
14065 sizeof (Elf32_External_gptab))))
14071 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14073 val = int_gptab.gt_entry.gt_g_value;
14074 add = int_gptab.gt_entry.gt_bytes - last;
14077 for (look = 1; look < c; look++)
14079 if (tab[look].gt_entry.gt_g_value >= val)
14080 tab[look].gt_entry.gt_bytes += add;
14082 if (tab[look].gt_entry.gt_g_value == val)
14088 Elf32_gptab *new_tab;
14091 /* We need a new table entry. */
14092 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
14093 new_tab = bfd_realloc (tab, amt);
14094 if (new_tab == NULL)
14100 tab[c].gt_entry.gt_g_value = val;
14101 tab[c].gt_entry.gt_bytes = add;
14103 /* Merge in the size for the next smallest -G
14104 value, since that will be implied by this new
14107 for (look = 1; look < c; look++)
14109 if (tab[look].gt_entry.gt_g_value < val
14111 || (tab[look].gt_entry.gt_g_value
14112 > tab[max].gt_entry.gt_g_value)))
14116 tab[c].gt_entry.gt_bytes +=
14117 tab[max].gt_entry.gt_bytes;
14122 last = int_gptab.gt_entry.gt_bytes;
14125 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14126 elf_link_input_bfd ignores this section. */
14127 input_section->flags &= ~SEC_HAS_CONTENTS;
14130 /* The table must be sorted by -G value. */
14132 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14134 /* Swap out the table. */
14135 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
14136 ext_tab = bfd_alloc (abfd, amt);
14137 if (ext_tab == NULL)
14143 for (j = 0; j < c; j++)
14144 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14147 o->size = c * sizeof (Elf32_External_gptab);
14148 o->contents = (bfd_byte *) ext_tab;
14150 /* Skip this section later on (I don't think this currently
14151 matters, but someday it might). */
14152 o->map_head.link_order = NULL;
14156 /* Invoke the regular ELF backend linker to do all the work. */
14157 if (!bfd_elf_final_link (abfd, info))
14160 /* Now write out the computed sections. */
14162 if (reginfo_sec != NULL)
14164 Elf32_External_RegInfo ext;
14166 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
14167 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
14171 if (mdebug_sec != NULL)
14173 BFD_ASSERT (abfd->output_has_begun);
14174 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14176 mdebug_sec->filepos))
14179 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14182 if (gptab_data_sec != NULL)
14184 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14185 gptab_data_sec->contents,
14186 0, gptab_data_sec->size))
14190 if (gptab_bss_sec != NULL)
14192 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14193 gptab_bss_sec->contents,
14194 0, gptab_bss_sec->size))
14198 if (SGI_COMPAT (abfd))
14200 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14201 if (rtproc_sec != NULL)
14203 if (! bfd_set_section_contents (abfd, rtproc_sec,
14204 rtproc_sec->contents,
14205 0, rtproc_sec->size))
14213 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14215 struct mips_mach_extension
14217 unsigned long extension, base;
14221 /* An array describing how BFD machines relate to one another. The entries
14222 are ordered topologically with MIPS I extensions listed last. */
14224 static const struct mips_mach_extension mips_mach_extensions[] =
14226 /* MIPS64r2 extensions. */
14227 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14228 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14229 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14230 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
14232 /* MIPS64 extensions. */
14233 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14234 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14235 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14237 /* MIPS V extensions. */
14238 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14240 /* R10000 extensions. */
14241 { bfd_mach_mips12000, bfd_mach_mips10000 },
14242 { bfd_mach_mips14000, bfd_mach_mips10000 },
14243 { bfd_mach_mips16000, bfd_mach_mips10000 },
14245 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14246 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14247 better to allow vr5400 and vr5500 code to be merged anyway, since
14248 many libraries will just use the core ISA. Perhaps we could add
14249 some sort of ASE flag if this ever proves a problem. */
14250 { bfd_mach_mips5500, bfd_mach_mips5400 },
14251 { bfd_mach_mips5400, bfd_mach_mips5000 },
14253 /* MIPS IV extensions. */
14254 { bfd_mach_mips5, bfd_mach_mips8000 },
14255 { bfd_mach_mips10000, bfd_mach_mips8000 },
14256 { bfd_mach_mips5000, bfd_mach_mips8000 },
14257 { bfd_mach_mips7000, bfd_mach_mips8000 },
14258 { bfd_mach_mips9000, bfd_mach_mips8000 },
14260 /* VR4100 extensions. */
14261 { bfd_mach_mips4120, bfd_mach_mips4100 },
14262 { bfd_mach_mips4111, bfd_mach_mips4100 },
14264 /* MIPS III extensions. */
14265 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14266 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14267 { bfd_mach_mips8000, bfd_mach_mips4000 },
14268 { bfd_mach_mips4650, bfd_mach_mips4000 },
14269 { bfd_mach_mips4600, bfd_mach_mips4000 },
14270 { bfd_mach_mips4400, bfd_mach_mips4000 },
14271 { bfd_mach_mips4300, bfd_mach_mips4000 },
14272 { bfd_mach_mips4100, bfd_mach_mips4000 },
14273 { bfd_mach_mips4010, bfd_mach_mips4000 },
14274 { bfd_mach_mips5900, bfd_mach_mips4000 },
14276 /* MIPS32 extensions. */
14277 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14279 /* MIPS II extensions. */
14280 { bfd_mach_mips4000, bfd_mach_mips6000 },
14281 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14283 /* MIPS I extensions. */
14284 { bfd_mach_mips6000, bfd_mach_mips3000 },
14285 { bfd_mach_mips3900, bfd_mach_mips3000 }
14289 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14292 mips_mach_extends_p (unsigned long base, unsigned long extension)
14296 if (extension == base)
14299 if (base == bfd_mach_mipsisa32
14300 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14303 if (base == bfd_mach_mipsisa32r2
14304 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14307 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14308 if (extension == mips_mach_extensions[i].extension)
14310 extension = mips_mach_extensions[i].base;
14311 if (extension == base)
14319 /* Return true if the given ELF header flags describe a 32-bit binary. */
14322 mips_32bit_flags_p (flagword flags)
14324 return ((flags & EF_MIPS_32BITMODE) != 0
14325 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14326 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14327 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14328 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14329 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14330 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
14334 /* Merge object attributes from IBFD into OBFD. Raise an error if
14335 there are conflicting attributes. */
14337 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14339 obj_attribute *in_attr;
14340 obj_attribute *out_attr;
14344 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14345 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
14346 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14347 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14349 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14351 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14352 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14354 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14356 /* This is the first object. Copy the attributes. */
14357 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14359 /* Use the Tag_null value to indicate the attributes have been
14361 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14366 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14367 non-conflicting ones. */
14368 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14369 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14373 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
14374 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14375 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
14376 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
14377 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
14378 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
14380 const char *out_string, *in_string;
14382 out_string = _bfd_mips_fp_abi_string (out_fp);
14383 in_string = _bfd_mips_fp_abi_string (in_fp);
14384 /* First warn about cases involving unrecognised ABIs. */
14385 if (!out_string && !in_string)
14387 (_("Warning: %B uses unknown floating point ABI %d "
14388 "(set by %B), %B uses unknown floating point ABI %d"),
14389 obfd, abi_fp_bfd, ibfd, out_fp, in_fp);
14390 else if (!out_string)
14392 (_("Warning: %B uses unknown floating point ABI %d "
14393 "(set by %B), %B uses %s"),
14394 obfd, abi_fp_bfd, ibfd, out_fp, in_string);
14395 else if (!in_string)
14397 (_("Warning: %B uses %s (set by %B), "
14398 "%B uses unknown floating point ABI %d"),
14399 obfd, abi_fp_bfd, ibfd, out_string, in_fp);
14402 /* If one of the bfds is soft-float, the other must be
14403 hard-float. The exact choice of hard-float ABI isn't
14404 really relevant to the error message. */
14405 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14406 out_string = "-mhard-float";
14407 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14408 in_string = "-mhard-float";
14410 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14411 obfd, abi_fp_bfd, ibfd, out_string, in_string);
14416 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14417 non-conflicting ones. */
14418 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14420 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14421 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
14422 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
14423 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14424 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14426 case Val_GNU_MIPS_ABI_MSA_128:
14428 (_("Warning: %B uses %s (set by %B), "
14429 "%B uses unknown MSA ABI %d"),
14430 obfd, abi_msa_bfd, ibfd,
14431 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14435 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
14437 case Val_GNU_MIPS_ABI_MSA_128:
14439 (_("Warning: %B uses unknown MSA ABI %d "
14440 "(set by %B), %B uses %s"),
14441 obfd, abi_msa_bfd, ibfd,
14442 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
14447 (_("Warning: %B uses unknown MSA ABI %d "
14448 "(set by %B), %B uses unknown MSA ABI %d"),
14449 obfd, abi_msa_bfd, ibfd,
14450 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
14451 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14457 /* Merge Tag_compatibility attributes and any common GNU ones. */
14458 _bfd_elf_merge_object_attributes (ibfd, obfd);
14463 /* Merge backend specific data from an object file to the output
14464 object file when linking. */
14467 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
14469 flagword old_flags;
14470 flagword new_flags;
14472 bfd_boolean null_input_bfd = TRUE;
14475 /* Check if we have the same endianness. */
14476 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
14478 (*_bfd_error_handler)
14479 (_("%B: endianness incompatible with that of the selected emulation"),
14484 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
14487 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14489 (*_bfd_error_handler)
14490 (_("%B: ABI is incompatible with that of the selected emulation"),
14495 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14498 new_flags = elf_elfheader (ibfd)->e_flags;
14499 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
14500 old_flags = elf_elfheader (obfd)->e_flags;
14502 if (! elf_flags_init (obfd))
14504 elf_flags_init (obfd) = TRUE;
14505 elf_elfheader (obfd)->e_flags = new_flags;
14506 elf_elfheader (obfd)->e_ident[EI_CLASS]
14507 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14509 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
14510 && (bfd_get_arch_info (obfd)->the_default
14511 || mips_mach_extends_p (bfd_get_mach (obfd),
14512 bfd_get_mach (ibfd))))
14514 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14515 bfd_get_mach (ibfd)))
14522 /* Check flag compatibility. */
14524 new_flags &= ~EF_MIPS_NOREORDER;
14525 old_flags &= ~EF_MIPS_NOREORDER;
14527 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
14528 doesn't seem to matter. */
14529 new_flags &= ~EF_MIPS_XGOT;
14530 old_flags &= ~EF_MIPS_XGOT;
14532 /* MIPSpro generates ucode info in n64 objects. Again, we should
14533 just be able to ignore this. */
14534 new_flags &= ~EF_MIPS_UCODE;
14535 old_flags &= ~EF_MIPS_UCODE;
14537 /* DSOs should only be linked with CPIC code. */
14538 if ((ibfd->flags & DYNAMIC) != 0)
14539 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
14541 if (new_flags == old_flags)
14544 /* Check to see if the input BFD actually contains any sections.
14545 If not, its flags may not have been initialised either, but it cannot
14546 actually cause any incompatibility. */
14547 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14549 /* Ignore synthetic sections and empty .text, .data and .bss sections
14550 which are automatically generated by gas. Also ignore fake
14551 (s)common sections, since merely defining a common symbol does
14552 not affect compatibility. */
14553 if ((sec->flags & SEC_IS_COMMON) == 0
14554 && strcmp (sec->name, ".reginfo")
14555 && strcmp (sec->name, ".mdebug")
14557 || (strcmp (sec->name, ".text")
14558 && strcmp (sec->name, ".data")
14559 && strcmp (sec->name, ".bss"))))
14561 null_input_bfd = FALSE;
14565 if (null_input_bfd)
14570 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
14571 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
14573 (*_bfd_error_handler)
14574 (_("%B: warning: linking abicalls files with non-abicalls files"),
14579 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
14580 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
14581 if (! (new_flags & EF_MIPS_PIC))
14582 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
14584 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14585 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14587 /* Compare the ISAs. */
14588 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
14590 (*_bfd_error_handler)
14591 (_("%B: linking 32-bit code with 64-bit code"),
14595 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14597 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14598 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
14600 /* Copy the architecture info from IBFD to OBFD. Also copy
14601 the 32-bit flag (if set) so that we continue to recognise
14602 OBFD as a 32-bit binary. */
14603 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14604 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14605 elf_elfheader (obfd)->e_flags
14606 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14608 /* Copy across the ABI flags if OBFD doesn't use them
14609 and if that was what caused us to treat IBFD as 32-bit. */
14610 if ((old_flags & EF_MIPS_ABI) == 0
14611 && mips_32bit_flags_p (new_flags)
14612 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14613 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
14617 /* The ISAs aren't compatible. */
14618 (*_bfd_error_handler)
14619 (_("%B: linking %s module with previous %s modules"),
14621 bfd_printable_name (ibfd),
14622 bfd_printable_name (obfd));
14627 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14628 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14630 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
14631 does set EI_CLASS differently from any 32-bit ABI. */
14632 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14633 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14634 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14636 /* Only error if both are set (to different values). */
14637 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14638 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14639 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14641 (*_bfd_error_handler)
14642 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14644 elf_mips_abi_name (ibfd),
14645 elf_mips_abi_name (obfd));
14648 new_flags &= ~EF_MIPS_ABI;
14649 old_flags &= ~EF_MIPS_ABI;
14652 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14653 and allow arbitrary mixing of the remaining ASEs (retain the union). */
14654 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14656 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14657 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14658 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14659 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14660 int micro_mis = old_m16 && new_micro;
14661 int m16_mis = old_micro && new_m16;
14663 if (m16_mis || micro_mis)
14665 (*_bfd_error_handler)
14666 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14668 m16_mis ? "MIPS16" : "microMIPS",
14669 m16_mis ? "microMIPS" : "MIPS16");
14673 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14675 new_flags &= ~ EF_MIPS_ARCH_ASE;
14676 old_flags &= ~ EF_MIPS_ARCH_ASE;
14679 /* Compare NaN encodings. */
14680 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
14682 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
14684 (new_flags & EF_MIPS_NAN2008
14685 ? "-mnan=2008" : "-mnan=legacy"),
14686 (old_flags & EF_MIPS_NAN2008
14687 ? "-mnan=2008" : "-mnan=legacy"));
14689 new_flags &= ~EF_MIPS_NAN2008;
14690 old_flags &= ~EF_MIPS_NAN2008;
14693 /* Warn about any other mismatches */
14694 if (new_flags != old_flags)
14696 (*_bfd_error_handler)
14697 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14698 ibfd, (unsigned long) new_flags,
14699 (unsigned long) old_flags);
14705 bfd_set_error (bfd_error_bad_value);
14712 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14715 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
14717 BFD_ASSERT (!elf_flags_init (abfd)
14718 || elf_elfheader (abfd)->e_flags == flags);
14720 elf_elfheader (abfd)->e_flags = flags;
14721 elf_flags_init (abfd) = TRUE;
14726 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14730 default: return "";
14731 case DT_MIPS_RLD_VERSION:
14732 return "MIPS_RLD_VERSION";
14733 case DT_MIPS_TIME_STAMP:
14734 return "MIPS_TIME_STAMP";
14735 case DT_MIPS_ICHECKSUM:
14736 return "MIPS_ICHECKSUM";
14737 case DT_MIPS_IVERSION:
14738 return "MIPS_IVERSION";
14739 case DT_MIPS_FLAGS:
14740 return "MIPS_FLAGS";
14741 case DT_MIPS_BASE_ADDRESS:
14742 return "MIPS_BASE_ADDRESS";
14744 return "MIPS_MSYM";
14745 case DT_MIPS_CONFLICT:
14746 return "MIPS_CONFLICT";
14747 case DT_MIPS_LIBLIST:
14748 return "MIPS_LIBLIST";
14749 case DT_MIPS_LOCAL_GOTNO:
14750 return "MIPS_LOCAL_GOTNO";
14751 case DT_MIPS_CONFLICTNO:
14752 return "MIPS_CONFLICTNO";
14753 case DT_MIPS_LIBLISTNO:
14754 return "MIPS_LIBLISTNO";
14755 case DT_MIPS_SYMTABNO:
14756 return "MIPS_SYMTABNO";
14757 case DT_MIPS_UNREFEXTNO:
14758 return "MIPS_UNREFEXTNO";
14759 case DT_MIPS_GOTSYM:
14760 return "MIPS_GOTSYM";
14761 case DT_MIPS_HIPAGENO:
14762 return "MIPS_HIPAGENO";
14763 case DT_MIPS_RLD_MAP:
14764 return "MIPS_RLD_MAP";
14765 case DT_MIPS_DELTA_CLASS:
14766 return "MIPS_DELTA_CLASS";
14767 case DT_MIPS_DELTA_CLASS_NO:
14768 return "MIPS_DELTA_CLASS_NO";
14769 case DT_MIPS_DELTA_INSTANCE:
14770 return "MIPS_DELTA_INSTANCE";
14771 case DT_MIPS_DELTA_INSTANCE_NO:
14772 return "MIPS_DELTA_INSTANCE_NO";
14773 case DT_MIPS_DELTA_RELOC:
14774 return "MIPS_DELTA_RELOC";
14775 case DT_MIPS_DELTA_RELOC_NO:
14776 return "MIPS_DELTA_RELOC_NO";
14777 case DT_MIPS_DELTA_SYM:
14778 return "MIPS_DELTA_SYM";
14779 case DT_MIPS_DELTA_SYM_NO:
14780 return "MIPS_DELTA_SYM_NO";
14781 case DT_MIPS_DELTA_CLASSSYM:
14782 return "MIPS_DELTA_CLASSSYM";
14783 case DT_MIPS_DELTA_CLASSSYM_NO:
14784 return "MIPS_DELTA_CLASSSYM_NO";
14785 case DT_MIPS_CXX_FLAGS:
14786 return "MIPS_CXX_FLAGS";
14787 case DT_MIPS_PIXIE_INIT:
14788 return "MIPS_PIXIE_INIT";
14789 case DT_MIPS_SYMBOL_LIB:
14790 return "MIPS_SYMBOL_LIB";
14791 case DT_MIPS_LOCALPAGE_GOTIDX:
14792 return "MIPS_LOCALPAGE_GOTIDX";
14793 case DT_MIPS_LOCAL_GOTIDX:
14794 return "MIPS_LOCAL_GOTIDX";
14795 case DT_MIPS_HIDDEN_GOTIDX:
14796 return "MIPS_HIDDEN_GOTIDX";
14797 case DT_MIPS_PROTECTED_GOTIDX:
14798 return "MIPS_PROTECTED_GOT_IDX";
14799 case DT_MIPS_OPTIONS:
14800 return "MIPS_OPTIONS";
14801 case DT_MIPS_INTERFACE:
14802 return "MIPS_INTERFACE";
14803 case DT_MIPS_DYNSTR_ALIGN:
14804 return "DT_MIPS_DYNSTR_ALIGN";
14805 case DT_MIPS_INTERFACE_SIZE:
14806 return "DT_MIPS_INTERFACE_SIZE";
14807 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14808 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14809 case DT_MIPS_PERF_SUFFIX:
14810 return "DT_MIPS_PERF_SUFFIX";
14811 case DT_MIPS_COMPACT_SIZE:
14812 return "DT_MIPS_COMPACT_SIZE";
14813 case DT_MIPS_GP_VALUE:
14814 return "DT_MIPS_GP_VALUE";
14815 case DT_MIPS_AUX_DYNAMIC:
14816 return "DT_MIPS_AUX_DYNAMIC";
14817 case DT_MIPS_PLTGOT:
14818 return "DT_MIPS_PLTGOT";
14819 case DT_MIPS_RWPLT:
14820 return "DT_MIPS_RWPLT";
14824 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
14828 _bfd_mips_fp_abi_string (int fp)
14832 /* These strings aren't translated because they're simply
14834 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14835 return "-mdouble-float";
14837 case Val_GNU_MIPS_ABI_FP_SINGLE:
14838 return "-msingle-float";
14840 case Val_GNU_MIPS_ABI_FP_SOFT:
14841 return "-msoft-float";
14843 case Val_GNU_MIPS_ABI_FP_64:
14844 return "-mips32r2 -mfp64";
14852 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
14856 BFD_ASSERT (abfd != NULL && ptr != NULL);
14858 /* Print normal ELF private data. */
14859 _bfd_elf_print_private_bfd_data (abfd, ptr);
14861 /* xgettext:c-format */
14862 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14864 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14865 fprintf (file, _(" [abi=O32]"));
14866 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14867 fprintf (file, _(" [abi=O64]"));
14868 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14869 fprintf (file, _(" [abi=EABI32]"));
14870 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14871 fprintf (file, _(" [abi=EABI64]"));
14872 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14873 fprintf (file, _(" [abi unknown]"));
14874 else if (ABI_N32_P (abfd))
14875 fprintf (file, _(" [abi=N32]"));
14876 else if (ABI_64_P (abfd))
14877 fprintf (file, _(" [abi=64]"));
14879 fprintf (file, _(" [no abi set]"));
14881 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
14882 fprintf (file, " [mips1]");
14883 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
14884 fprintf (file, " [mips2]");
14885 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
14886 fprintf (file, " [mips3]");
14887 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
14888 fprintf (file, " [mips4]");
14889 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
14890 fprintf (file, " [mips5]");
14891 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
14892 fprintf (file, " [mips32]");
14893 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
14894 fprintf (file, " [mips64]");
14895 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
14896 fprintf (file, " [mips32r2]");
14897 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
14898 fprintf (file, " [mips64r2]");
14900 fprintf (file, _(" [unknown ISA]"));
14902 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14903 fprintf (file, " [mdmx]");
14905 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14906 fprintf (file, " [mips16]");
14908 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14909 fprintf (file, " [micromips]");
14911 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
14912 fprintf (file, " [nan2008]");
14914 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
14915 fprintf (file, " [fp64]");
14917 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
14918 fprintf (file, " [32bitmode]");
14920 fprintf (file, _(" [not 32bitmode]"));
14922 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
14923 fprintf (file, " [noreorder]");
14925 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
14926 fprintf (file, " [PIC]");
14928 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
14929 fprintf (file, " [CPIC]");
14931 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
14932 fprintf (file, " [XGOT]");
14934 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
14935 fprintf (file, " [UCODE]");
14937 fputc ('\n', file);
14942 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
14944 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14945 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14946 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14947 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14948 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14949 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14950 { NULL, 0, 0, 0, 0 }
14953 /* Merge non visibility st_other attributes. Ensure that the
14954 STO_OPTIONAL flag is copied into h->other, even if this is not a
14955 definiton of the symbol. */
14957 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
14958 const Elf_Internal_Sym *isym,
14959 bfd_boolean definition,
14960 bfd_boolean dynamic ATTRIBUTE_UNUSED)
14962 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
14964 unsigned char other;
14966 other = (definition ? isym->st_other : h->other);
14967 other &= ~ELF_ST_VISIBILITY (-1);
14968 h->other = other | ELF_ST_VISIBILITY (h->other);
14972 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
14973 h->other |= STO_OPTIONAL;
14976 /* Decide whether an undefined symbol is special and can be ignored.
14977 This is the case for OPTIONAL symbols on IRIX. */
14979 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
14981 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
14985 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
14987 return (sym->st_shndx == SHN_COMMON
14988 || sym->st_shndx == SHN_MIPS_ACOMMON
14989 || sym->st_shndx == SHN_MIPS_SCOMMON);
14992 /* Return address for Ith PLT stub in section PLT, for relocation REL
14993 or (bfd_vma) -1 if it should not be included. */
14996 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
14997 const arelent *rel ATTRIBUTE_UNUSED)
15000 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15001 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15004 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15005 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15006 and .got.plt and also the slots may be of a different size each we walk
15007 the PLT manually fetching instructions and matching them against known
15008 patterns. To make things easier standard MIPS slots, if any, always come
15009 first. As we don't create proper ELF symbols we use the UDATA.I member
15010 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15011 with the ST_OTHER member of the ELF symbol. */
15014 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15015 long symcount ATTRIBUTE_UNUSED,
15016 asymbol **syms ATTRIBUTE_UNUSED,
15017 long dynsymcount, asymbol **dynsyms,
15020 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15021 static const char microsuffix[] = "@micromipsplt";
15022 static const char m16suffix[] = "@mips16plt";
15023 static const char mipssuffix[] = "@plt";
15025 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15026 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15027 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15028 Elf_Internal_Shdr *hdr;
15029 bfd_byte *plt_data;
15030 bfd_vma plt_offset;
15031 unsigned int other;
15032 bfd_vma entry_size;
15051 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15054 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15055 if (relplt == NULL)
15058 hdr = &elf_section_data (relplt)->this_hdr;
15059 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15062 plt = bfd_get_section_by_name (abfd, ".plt");
15066 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15067 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15069 p = relplt->relocation;
15071 /* Calculating the exact amount of space required for symbols would
15072 require two passes over the PLT, so just pessimise assuming two
15073 PLT slots per relocation. */
15074 count = relplt->size / hdr->sh_entsize;
15075 counti = count * bed->s->int_rels_per_ext_rel;
15076 size = 2 * count * sizeof (asymbol);
15077 size += count * (sizeof (mipssuffix) +
15078 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15079 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15080 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15082 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15083 size += sizeof (asymbol) + sizeof (pltname);
15085 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15088 if (plt->size < 16)
15091 s = *ret = bfd_malloc (size);
15094 send = s + 2 * count + 1;
15096 names = (char *) send;
15097 nend = (char *) s + size;
15100 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15101 if (opcode == 0x3302fffe)
15105 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15106 other = STO_MICROMIPS;
15108 else if (opcode == 0x0398c1d0)
15112 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15113 other = STO_MICROMIPS;
15117 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15122 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15126 s->udata.i = other;
15127 memcpy (names, pltname, sizeof (pltname));
15128 names += sizeof (pltname);
15132 for (plt_offset = plt0_size;
15133 plt_offset + 8 <= plt->size && s < send;
15134 plt_offset += entry_size)
15136 bfd_vma gotplt_addr;
15137 const char *suffix;
15142 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15144 /* Check if the second word matches the expected MIPS16 instruction. */
15145 if (opcode == 0x651aeb00)
15149 /* Truncated table??? */
15150 if (plt_offset + 16 > plt->size)
15152 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15153 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15154 suffixlen = sizeof (m16suffix);
15155 suffix = m16suffix;
15156 other = STO_MIPS16;
15158 /* Likewise the expected microMIPS instruction (no insn32 mode). */
15159 else if (opcode == 0xff220000)
15163 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15164 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15165 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15167 gotplt_addr = gotplt_hi + gotplt_lo;
15168 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15169 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15170 suffixlen = sizeof (microsuffix);
15171 suffix = microsuffix;
15172 other = STO_MICROMIPS;
15174 /* Likewise the expected microMIPS instruction (insn32 mode). */
15175 else if ((opcode & 0xffff0000) == 0xff2f0000)
15177 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15178 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15179 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15180 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15181 gotplt_addr = gotplt_hi + gotplt_lo;
15182 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15183 suffixlen = sizeof (microsuffix);
15184 suffix = microsuffix;
15185 other = STO_MICROMIPS;
15187 /* Otherwise assume standard MIPS code. */
15190 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15191 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15192 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15193 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15194 gotplt_addr = gotplt_hi + gotplt_lo;
15195 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15196 suffixlen = sizeof (mipssuffix);
15197 suffix = mipssuffix;
15200 /* Truncated table??? */
15201 if (plt_offset + entry_size > plt->size)
15205 i < count && p[pi].address != gotplt_addr;
15206 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15213 *s = **p[pi].sym_ptr_ptr;
15214 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15215 we are defining a symbol, ensure one of them is set. */
15216 if ((s->flags & BSF_LOCAL) == 0)
15217 s->flags |= BSF_GLOBAL;
15218 s->flags |= BSF_SYNTHETIC;
15220 s->value = plt_offset;
15222 s->udata.i = other;
15224 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15225 namelen = len + suffixlen;
15226 if (names + namelen > nend)
15229 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15231 memcpy (names, suffix, suffixlen);
15232 names += suffixlen;
15235 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15245 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15247 struct mips_elf_link_hash_table *htab;
15248 Elf_Internal_Ehdr *i_ehdrp;
15250 i_ehdrp = elf_elfheader (abfd);
15253 htab = mips_elf_hash_table (link_info);
15254 BFD_ASSERT (htab != NULL);
15256 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15257 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
15260 _bfd_elf_post_process_headers (abfd, link_info);