1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 Free Software Foundation, Inc.
6 Most of the information added by Ian Lance Taylor, Cygnus Support,
8 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 <mark@codesourcery.com>
10 Traditional MIPS targets support added by Koundinya.K, Dansk Data
11 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
13 This file is part of BFD, the Binary File Descriptor library.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28 MA 02110-1301, USA. */
31 /* This file handles functionality common to the different MIPS ABI's. */
36 #include "libiberty.h"
38 #include "elfxx-mips.h"
40 #include "elf-vxworks.h"
42 /* Get the ECOFF swapping routines. */
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
50 /* This structure is used to hold information about one GOT entry.
51 There are four types of entry:
53 (1) an absolute address
54 requires: abfd == NULL
57 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
58 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
59 fields: abfd, symndx, d.addend, tls_type
61 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
62 requires: abfd != NULL, symndx == -1
66 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
67 fields: none; there's only one of these per GOT. */
70 /* One input bfd that needs the GOT entry. */
72 /* The index of the symbol, as stored in the relocation r_info, if
73 we have a local symbol; -1 otherwise. */
77 /* If abfd == NULL, an address that must be stored in the got. */
79 /* If abfd != NULL && symndx != -1, the addend of the relocation
80 that should be added to the symbol value. */
82 /* If abfd != NULL && symndx == -1, the hash table entry
83 corresponding to a symbol in the GOT. The symbol's entry
84 is in the local area if h->global_got_area is GGA_NONE,
85 otherwise it is in the global area. */
86 struct mips_elf_link_hash_entry *h;
89 /* The TLS type of this GOT entry: GOT_NORMAL, GOT_TLS_IE, GOT_TLS_GD
90 or GOT_TLS_LDM. An LDM GOT entry will be a local symbol entry with
92 unsigned char tls_type;
94 /* The offset from the beginning of the .got section to the entry
95 corresponding to this symbol+addend. If it's a global symbol
96 whose offset is yet to be decided, it's going to be -1. */
100 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
101 The structures form a non-overlapping list that is sorted by increasing
103 struct mips_got_page_range
105 struct mips_got_page_range *next;
106 bfd_signed_vma min_addend;
107 bfd_signed_vma max_addend;
110 /* This structure describes the range of addends that are applied to page
111 relocations against a given symbol. */
112 struct mips_got_page_entry
114 /* The input bfd in which the symbol is defined. */
116 /* The index of the symbol, as stored in the relocation r_info. */
118 /* The ranges for this page entry. */
119 struct mips_got_page_range *ranges;
120 /* The maximum number of page entries needed for RANGES. */
124 /* This structure is used to hold .got information when linking. */
128 /* The number of global .got entries. */
129 unsigned int global_gotno;
130 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
131 unsigned int reloc_only_gotno;
132 /* The number of .got slots used for TLS. */
133 unsigned int tls_gotno;
134 /* The first unused TLS .got entry. Used only during
135 mips_elf_initialize_tls_index. */
136 unsigned int tls_assigned_gotno;
137 /* The number of local .got entries, eventually including page entries. */
138 unsigned int local_gotno;
139 /* The maximum number of page entries needed. */
140 unsigned int page_gotno;
141 /* The number of relocations needed for the GOT entries. */
143 /* The number of local .got entries we have used. */
144 unsigned int assigned_gotno;
145 /* A hash table holding members of the got. */
146 struct htab *got_entries;
147 /* A hash table of mips_got_page_entry structures. */
148 struct htab *got_page_entries;
149 /* In multi-got links, a pointer to the next got (err, rather, most
150 of the time, it points to the previous got). */
151 struct mips_got_info *next;
152 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
153 for none, or MINUS_TWO for not yet assigned. This is needed
154 because a single-GOT link may have multiple hash table entries
155 for the LDM. It does not get initialized in multi-GOT mode. */
156 bfd_vma tls_ldm_offset;
159 /* Structure passed when merging bfds' gots. */
161 struct mips_elf_got_per_bfd_arg
163 /* The output bfd. */
165 /* The link information. */
166 struct bfd_link_info *info;
167 /* A pointer to the primary got, i.e., the one that's going to get
168 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
170 struct mips_got_info *primary;
171 /* A non-primary got we're trying to merge with other input bfd's
173 struct mips_got_info *current;
174 /* The maximum number of got entries that can be addressed with a
176 unsigned int max_count;
177 /* The maximum number of page entries needed by each got. */
178 unsigned int max_pages;
179 /* The total number of global entries which will live in the
180 primary got and be automatically relocated. This includes
181 those not referenced by the primary GOT but included in
183 unsigned int global_count;
186 /* A structure used to pass information to htab_traverse callbacks
187 when laying out the GOT. */
189 struct mips_elf_traverse_got_arg
191 struct bfd_link_info *info;
192 struct mips_got_info *g;
196 struct _mips_elf_section_data
198 struct bfd_elf_section_data elf;
205 #define mips_elf_section_data(sec) \
206 ((struct _mips_elf_section_data *) elf_section_data (sec))
208 #define is_mips_elf(bfd) \
209 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
210 && elf_tdata (bfd) != NULL \
211 && elf_object_id (bfd) == MIPS_ELF_DATA)
213 /* The ABI says that every symbol used by dynamic relocations must have
214 a global GOT entry. Among other things, this provides the dynamic
215 linker with a free, directly-indexed cache. The GOT can therefore
216 contain symbols that are not referenced by GOT relocations themselves
217 (in other words, it may have symbols that are not referenced by things
218 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
220 GOT relocations are less likely to overflow if we put the associated
221 GOT entries towards the beginning. We therefore divide the global
222 GOT entries into two areas: "normal" and "reloc-only". Entries in
223 the first area can be used for both dynamic relocations and GP-relative
224 accesses, while those in the "reloc-only" area are for dynamic
227 These GGA_* ("Global GOT Area") values are organised so that lower
228 values are more general than higher values. Also, non-GGA_NONE
229 values are ordered by the position of the area in the GOT. */
231 #define GGA_RELOC_ONLY 1
234 /* Information about a non-PIC interface to a PIC function. There are
235 two ways of creating these interfaces. The first is to add:
238 addiu $25,$25,%lo(func)
240 immediately before a PIC function "func". The second is to add:
244 addiu $25,$25,%lo(func)
246 to a separate trampoline section.
248 Stubs of the first kind go in a new section immediately before the
249 target function. Stubs of the second kind go in a single section
250 pointed to by the hash table's "strampoline" field. */
251 struct mips_elf_la25_stub {
252 /* The generated section that contains this stub. */
253 asection *stub_section;
255 /* The offset of the stub from the start of STUB_SECTION. */
258 /* One symbol for the original function. Its location is available
259 in H->root.root.u.def. */
260 struct mips_elf_link_hash_entry *h;
263 /* Macros for populating a mips_elf_la25_stub. */
265 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
266 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
267 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
268 #define LA25_LUI_MICROMIPS(VAL) \
269 (0x41b90000 | (VAL)) /* lui t9,VAL */
270 #define LA25_J_MICROMIPS(VAL) \
271 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
272 #define LA25_ADDIU_MICROMIPS(VAL) \
273 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
275 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
276 the dynamic symbols. */
278 struct mips_elf_hash_sort_data
280 /* The symbol in the global GOT with the lowest dynamic symbol table
282 struct elf_link_hash_entry *low;
283 /* The least dynamic symbol table index corresponding to a non-TLS
284 symbol with a GOT entry. */
285 long min_got_dynindx;
286 /* The greatest dynamic symbol table index corresponding to a symbol
287 with a GOT entry that is not referenced (e.g., a dynamic symbol
288 with dynamic relocations pointing to it from non-primary GOTs). */
289 long max_unref_got_dynindx;
290 /* The greatest dynamic symbol table index not corresponding to a
291 symbol without a GOT entry. */
292 long max_non_got_dynindx;
295 /* The MIPS ELF linker needs additional information for each symbol in
296 the global hash table. */
298 struct mips_elf_link_hash_entry
300 struct elf_link_hash_entry root;
302 /* External symbol information. */
305 /* The la25 stub we have created for ths symbol, if any. */
306 struct mips_elf_la25_stub *la25_stub;
308 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
310 unsigned int possibly_dynamic_relocs;
312 /* If there is a stub that 32 bit functions should use to call this
313 16 bit function, this points to the section containing the stub. */
316 /* If there is a stub that 16 bit functions should use to call this
317 32 bit function, this points to the section containing the stub. */
320 /* This is like the call_stub field, but it is used if the function
321 being called returns a floating point value. */
322 asection *call_fp_stub;
326 #define GOT_TLS_LDM 2
328 #define GOT_TLS_TYPE 7
329 #define GOT_TLS_OFFSET_DONE 0x40
330 #define GOT_TLS_DONE 0x80
331 unsigned char tls_ie_type;
332 unsigned char tls_gd_type;
334 /* These fields are only used in single-GOT mode; in multi-GOT mode there
335 is one mips_got_entry per GOT entry, so the offset is stored
336 there. In single-GOT mode there may be many mips_got_entry
337 structures all referring to the same GOT slot. */
338 bfd_vma tls_ie_got_offset;
339 bfd_vma tls_gd_got_offset;
341 /* The highest GGA_* value that satisfies all references to this symbol. */
342 unsigned int global_got_area : 2;
344 /* True if all GOT relocations against this symbol are for calls. This is
345 a looser condition than no_fn_stub below, because there may be other
346 non-call non-GOT relocations against the symbol. */
347 unsigned int got_only_for_calls : 1;
349 /* True if one of the relocations described by possibly_dynamic_relocs
350 is against a readonly section. */
351 unsigned int readonly_reloc : 1;
353 /* True if there is a relocation against this symbol that must be
354 resolved by the static linker (in other words, if the relocation
355 cannot possibly be made dynamic). */
356 unsigned int has_static_relocs : 1;
358 /* True if we must not create a .MIPS.stubs entry for this symbol.
359 This is set, for example, if there are relocations related to
360 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
361 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
362 unsigned int no_fn_stub : 1;
364 /* Whether we need the fn_stub; this is true if this symbol appears
365 in any relocs other than a 16 bit call. */
366 unsigned int need_fn_stub : 1;
368 /* True if this symbol is referenced by branch relocations from
369 any non-PIC input file. This is used to determine whether an
370 la25 stub is required. */
371 unsigned int has_nonpic_branches : 1;
373 /* Does this symbol need a traditional MIPS lazy-binding stub
374 (as opposed to a PLT entry)? */
375 unsigned int needs_lazy_stub : 1;
378 /* MIPS ELF linker hash table. */
380 struct mips_elf_link_hash_table
382 struct elf_link_hash_table root;
384 /* The number of .rtproc entries. */
385 bfd_size_type procedure_count;
387 /* The size of the .compact_rel section (if SGI_COMPAT). */
388 bfd_size_type compact_rel_size;
390 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
391 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
392 bfd_boolean use_rld_obj_head;
394 /* The __rld_map or __rld_obj_head symbol. */
395 struct elf_link_hash_entry *rld_symbol;
397 /* This is set if we see any mips16 stub sections. */
398 bfd_boolean mips16_stubs_seen;
400 /* True if we can generate copy relocs and PLTs. */
401 bfd_boolean use_plts_and_copy_relocs;
403 /* True if we're generating code for VxWorks. */
404 bfd_boolean is_vxworks;
406 /* True if we already reported the small-data section overflow. */
407 bfd_boolean small_data_overflow_reported;
409 /* Shortcuts to some dynamic sections, or NULL if they are not
420 /* The master GOT information. */
421 struct mips_got_info *got_info;
423 /* The global symbol in the GOT with the lowest index in the dynamic
425 struct elf_link_hash_entry *global_gotsym;
427 /* The size of the PLT header in bytes. */
428 bfd_vma plt_header_size;
430 /* The size of a PLT entry in bytes. */
431 bfd_vma plt_entry_size;
433 /* The number of functions that need a lazy-binding stub. */
434 bfd_vma lazy_stub_count;
436 /* The size of a function stub entry in bytes. */
437 bfd_vma function_stub_size;
439 /* The number of reserved entries at the beginning of the GOT. */
440 unsigned int reserved_gotno;
442 /* The section used for mips_elf_la25_stub trampolines.
443 See the comment above that structure for details. */
444 asection *strampoline;
446 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
450 /* A function FN (NAME, IS, OS) that creates a new input section
451 called NAME and links it to output section OS. If IS is nonnull,
452 the new section should go immediately before it, otherwise it
453 should go at the (current) beginning of OS.
455 The function returns the new section on success, otherwise it
457 asection *(*add_stub_section) (const char *, asection *, asection *);
460 /* Get the MIPS ELF linker hash table from a link_info structure. */
462 #define mips_elf_hash_table(p) \
463 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
464 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
466 /* A structure used to communicate with htab_traverse callbacks. */
467 struct mips_htab_traverse_info
469 /* The usual link-wide information. */
470 struct bfd_link_info *info;
473 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
477 /* MIPS ELF private object data. */
479 struct mips_elf_obj_tdata
481 /* Generic ELF private object data. */
482 struct elf_obj_tdata root;
484 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
487 /* The GOT requirements of input bfds. */
488 struct mips_got_info *got;
491 /* Get MIPS ELF private object data from BFD's tdata. */
493 #define mips_elf_tdata(bfd) \
494 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
496 #define TLS_RELOC_P(r_type) \
497 (r_type == R_MIPS_TLS_DTPMOD32 \
498 || r_type == R_MIPS_TLS_DTPMOD64 \
499 || r_type == R_MIPS_TLS_DTPREL32 \
500 || r_type == R_MIPS_TLS_DTPREL64 \
501 || r_type == R_MIPS_TLS_GD \
502 || r_type == R_MIPS_TLS_LDM \
503 || r_type == R_MIPS_TLS_DTPREL_HI16 \
504 || r_type == R_MIPS_TLS_DTPREL_LO16 \
505 || r_type == R_MIPS_TLS_GOTTPREL \
506 || r_type == R_MIPS_TLS_TPREL32 \
507 || r_type == R_MIPS_TLS_TPREL64 \
508 || r_type == R_MIPS_TLS_TPREL_HI16 \
509 || r_type == R_MIPS_TLS_TPREL_LO16 \
510 || r_type == R_MIPS16_TLS_GD \
511 || r_type == R_MIPS16_TLS_LDM \
512 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
513 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
514 || r_type == R_MIPS16_TLS_GOTTPREL \
515 || r_type == R_MIPS16_TLS_TPREL_HI16 \
516 || r_type == R_MIPS16_TLS_TPREL_LO16 \
517 || r_type == R_MICROMIPS_TLS_GD \
518 || r_type == R_MICROMIPS_TLS_LDM \
519 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
520 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
521 || r_type == R_MICROMIPS_TLS_GOTTPREL \
522 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
523 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
525 /* Structure used to pass information to mips_elf_output_extsym. */
530 struct bfd_link_info *info;
531 struct ecoff_debug_info *debug;
532 const struct ecoff_debug_swap *swap;
536 /* The names of the runtime procedure table symbols used on IRIX5. */
538 static const char * const mips_elf_dynsym_rtproc_names[] =
541 "_procedure_string_table",
542 "_procedure_table_size",
546 /* These structures are used to generate the .compact_rel section on
551 unsigned long id1; /* Always one? */
552 unsigned long num; /* Number of compact relocation entries. */
553 unsigned long id2; /* Always two? */
554 unsigned long offset; /* The file offset of the first relocation. */
555 unsigned long reserved0; /* Zero? */
556 unsigned long reserved1; /* Zero? */
565 bfd_byte reserved0[4];
566 bfd_byte reserved1[4];
567 } Elf32_External_compact_rel;
571 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
572 unsigned int rtype : 4; /* Relocation types. See below. */
573 unsigned int dist2to : 8;
574 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
575 unsigned long konst; /* KONST field. See below. */
576 unsigned long vaddr; /* VADDR to be relocated. */
581 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
582 unsigned int rtype : 4; /* Relocation types. See below. */
583 unsigned int dist2to : 8;
584 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
585 unsigned long konst; /* KONST field. See below. */
593 } Elf32_External_crinfo;
599 } Elf32_External_crinfo2;
601 /* These are the constants used to swap the bitfields in a crinfo. */
603 #define CRINFO_CTYPE (0x1)
604 #define CRINFO_CTYPE_SH (31)
605 #define CRINFO_RTYPE (0xf)
606 #define CRINFO_RTYPE_SH (27)
607 #define CRINFO_DIST2TO (0xff)
608 #define CRINFO_DIST2TO_SH (19)
609 #define CRINFO_RELVADDR (0x7ffff)
610 #define CRINFO_RELVADDR_SH (0)
612 /* A compact relocation info has long (3 words) or short (2 words)
613 formats. A short format doesn't have VADDR field and relvaddr
614 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
615 #define CRF_MIPS_LONG 1
616 #define CRF_MIPS_SHORT 0
618 /* There are 4 types of compact relocation at least. The value KONST
619 has different meaning for each type:
622 CT_MIPS_REL32 Address in data
623 CT_MIPS_WORD Address in word (XXX)
624 CT_MIPS_GPHI_LO GP - vaddr
625 CT_MIPS_JMPAD Address to jump
628 #define CRT_MIPS_REL32 0xa
629 #define CRT_MIPS_WORD 0xb
630 #define CRT_MIPS_GPHI_LO 0xc
631 #define CRT_MIPS_JMPAD 0xd
633 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
634 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
635 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
636 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
638 /* The structure of the runtime procedure descriptor created by the
639 loader for use by the static exception system. */
641 typedef struct runtime_pdr {
642 bfd_vma adr; /* Memory address of start of procedure. */
643 long regmask; /* Save register mask. */
644 long regoffset; /* Save register offset. */
645 long fregmask; /* Save floating point register mask. */
646 long fregoffset; /* Save floating point register offset. */
647 long frameoffset; /* Frame size. */
648 short framereg; /* Frame pointer register. */
649 short pcreg; /* Offset or reg of return pc. */
650 long irpss; /* Index into the runtime string table. */
652 struct exception_info *exception_info;/* Pointer to exception array. */
654 #define cbRPDR sizeof (RPDR)
655 #define rpdNil ((pRPDR) 0)
657 static struct mips_got_entry *mips_elf_create_local_got_entry
658 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
659 struct mips_elf_link_hash_entry *, int);
660 static bfd_boolean mips_elf_sort_hash_table_f
661 (struct mips_elf_link_hash_entry *, void *);
662 static bfd_vma mips_elf_high
664 static bfd_boolean mips_elf_create_dynamic_relocation
665 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
666 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
667 bfd_vma *, asection *);
668 static bfd_vma mips_elf_adjust_gp
669 (bfd *, struct mips_got_info *, bfd *);
671 /* This will be used when we sort the dynamic relocation records. */
672 static bfd *reldyn_sorting_bfd;
674 /* True if ABFD is for CPUs with load interlocking that include
675 non-MIPS1 CPUs and R3900. */
676 #define LOAD_INTERLOCKS_P(abfd) \
677 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
678 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
680 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
681 This should be safe for all architectures. We enable this predicate
682 for RM9000 for now. */
683 #define JAL_TO_BAL_P(abfd) \
684 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
686 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
687 This should be safe for all architectures. We enable this predicate for
689 #define JALR_TO_BAL_P(abfd) 1
691 /* True if ABFD is for CPUs that are faster if JR is converted to B.
692 This should be safe for all architectures. We enable this predicate for
694 #define JR_TO_B_P(abfd) 1
696 /* True if ABFD is a PIC object. */
697 #define PIC_OBJECT_P(abfd) \
698 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
700 /* Nonzero if ABFD is using the N32 ABI. */
701 #define ABI_N32_P(abfd) \
702 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
704 /* Nonzero if ABFD is using the N64 ABI. */
705 #define ABI_64_P(abfd) \
706 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
708 /* Nonzero if ABFD is using NewABI conventions. */
709 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
711 /* The IRIX compatibility level we are striving for. */
712 #define IRIX_COMPAT(abfd) \
713 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
715 /* Whether we are trying to be compatible with IRIX at all. */
716 #define SGI_COMPAT(abfd) \
717 (IRIX_COMPAT (abfd) != ict_none)
719 /* The name of the options section. */
720 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
721 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
723 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
724 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
725 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
726 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
728 /* Whether the section is readonly. */
729 #define MIPS_ELF_READONLY_SECTION(sec) \
730 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
731 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
733 /* The name of the stub section. */
734 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
736 /* The size of an external REL relocation. */
737 #define MIPS_ELF_REL_SIZE(abfd) \
738 (get_elf_backend_data (abfd)->s->sizeof_rel)
740 /* The size of an external RELA relocation. */
741 #define MIPS_ELF_RELA_SIZE(abfd) \
742 (get_elf_backend_data (abfd)->s->sizeof_rela)
744 /* The size of an external dynamic table entry. */
745 #define MIPS_ELF_DYN_SIZE(abfd) \
746 (get_elf_backend_data (abfd)->s->sizeof_dyn)
748 /* The size of a GOT entry. */
749 #define MIPS_ELF_GOT_SIZE(abfd) \
750 (get_elf_backend_data (abfd)->s->arch_size / 8)
752 /* The size of the .rld_map section. */
753 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
754 (get_elf_backend_data (abfd)->s->arch_size / 8)
756 /* The size of a symbol-table entry. */
757 #define MIPS_ELF_SYM_SIZE(abfd) \
758 (get_elf_backend_data (abfd)->s->sizeof_sym)
760 /* The default alignment for sections, as a power of two. */
761 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
762 (get_elf_backend_data (abfd)->s->log_file_align)
764 /* Get word-sized data. */
765 #define MIPS_ELF_GET_WORD(abfd, ptr) \
766 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
768 /* Put out word-sized data. */
769 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
771 ? bfd_put_64 (abfd, val, ptr) \
772 : bfd_put_32 (abfd, val, ptr))
774 /* The opcode for word-sized loads (LW or LD). */
775 #define MIPS_ELF_LOAD_WORD(abfd) \
776 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
778 /* Add a dynamic symbol table-entry. */
779 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
780 _bfd_elf_add_dynamic_entry (info, tag, val)
782 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
783 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
785 /* The name of the dynamic relocation section. */
786 #define MIPS_ELF_REL_DYN_NAME(INFO) \
787 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
789 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
790 from smaller values. Start with zero, widen, *then* decrement. */
791 #define MINUS_ONE (((bfd_vma)0) - 1)
792 #define MINUS_TWO (((bfd_vma)0) - 2)
794 /* The value to write into got[1] for SVR4 targets, to identify it is
795 a GNU object. The dynamic linker can then use got[1] to store the
797 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
798 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
800 /* The offset of $gp from the beginning of the .got section. */
801 #define ELF_MIPS_GP_OFFSET(INFO) \
802 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
804 /* The maximum size of the GOT for it to be addressable using 16-bit
806 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
808 /* Instructions which appear in a stub. */
809 #define STUB_LW(abfd) \
811 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
812 : 0x8f998010)) /* lw t9,0x8010(gp) */
813 #define STUB_MOVE(abfd) \
815 ? 0x03e0782d /* daddu t7,ra */ \
816 : 0x03e07821)) /* addu t7,ra */
817 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
818 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
819 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
820 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
821 #define STUB_LI16S(abfd, VAL) \
823 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
824 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
826 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
827 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
829 /* The name of the dynamic interpreter. This is put in the .interp
832 #define ELF_DYNAMIC_INTERPRETER(abfd) \
833 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
834 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
835 : "/usr/lib/libc.so.1")
838 #define MNAME(bfd,pre,pos) \
839 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
840 #define ELF_R_SYM(bfd, i) \
841 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
842 #define ELF_R_TYPE(bfd, i) \
843 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
844 #define ELF_R_INFO(bfd, s, t) \
845 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
847 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
848 #define ELF_R_SYM(bfd, i) \
850 #define ELF_R_TYPE(bfd, i) \
852 #define ELF_R_INFO(bfd, s, t) \
853 (ELF32_R_INFO (s, t))
856 /* The mips16 compiler uses a couple of special sections to handle
857 floating point arguments.
859 Section names that look like .mips16.fn.FNNAME contain stubs that
860 copy floating point arguments from the fp regs to the gp regs and
861 then jump to FNNAME. If any 32 bit function calls FNNAME, the
862 call should be redirected to the stub instead. If no 32 bit
863 function calls FNNAME, the stub should be discarded. We need to
864 consider any reference to the function, not just a call, because
865 if the address of the function is taken we will need the stub,
866 since the address might be passed to a 32 bit function.
868 Section names that look like .mips16.call.FNNAME contain stubs
869 that copy floating point arguments from the gp regs to the fp
870 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
871 then any 16 bit function that calls FNNAME should be redirected
872 to the stub instead. If FNNAME is not a 32 bit function, the
873 stub should be discarded.
875 .mips16.call.fp.FNNAME sections are similar, but contain stubs
876 which call FNNAME and then copy the return value from the fp regs
877 to the gp regs. These stubs store the return value in $18 while
878 calling FNNAME; any function which might call one of these stubs
879 must arrange to save $18 around the call. (This case is not
880 needed for 32 bit functions that call 16 bit functions, because
881 16 bit functions always return floating point values in both
884 Note that in all cases FNNAME might be defined statically.
885 Therefore, FNNAME is not used literally. Instead, the relocation
886 information will indicate which symbol the section is for.
888 We record any stubs that we find in the symbol table. */
890 #define FN_STUB ".mips16.fn."
891 #define CALL_STUB ".mips16.call."
892 #define CALL_FP_STUB ".mips16.call.fp."
894 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
895 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
896 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
898 /* The format of the first PLT entry in an O32 executable. */
899 static const bfd_vma mips_o32_exec_plt0_entry[] =
901 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
902 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
903 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
904 0x031cc023, /* subu $24, $24, $28 */
905 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
906 0x0018c082, /* srl $24, $24, 2 */
907 0x0320f809, /* jalr $25 */
908 0x2718fffe /* subu $24, $24, 2 */
911 /* The format of the first PLT entry in an N32 executable. Different
912 because gp ($28) is not available; we use t2 ($14) instead. */
913 static const bfd_vma mips_n32_exec_plt0_entry[] =
915 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
916 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
917 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
918 0x030ec023, /* subu $24, $24, $14 */
919 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
920 0x0018c082, /* srl $24, $24, 2 */
921 0x0320f809, /* jalr $25 */
922 0x2718fffe /* subu $24, $24, 2 */
925 /* The format of the first PLT entry in an N64 executable. Different
926 from N32 because of the increased size of GOT entries. */
927 static const bfd_vma mips_n64_exec_plt0_entry[] =
929 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
930 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
931 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
932 0x030ec023, /* subu $24, $24, $14 */
933 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
934 0x0018c0c2, /* srl $24, $24, 3 */
935 0x0320f809, /* jalr $25 */
936 0x2718fffe /* subu $24, $24, 2 */
939 /* The format of subsequent PLT entries. */
940 static const bfd_vma mips_exec_plt_entry[] =
942 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
943 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
944 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
945 0x03200008 /* jr $25 */
948 /* The format of the first PLT entry in a VxWorks executable. */
949 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
951 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
952 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
953 0x8f390008, /* lw t9, 8(t9) */
954 0x00000000, /* nop */
955 0x03200008, /* jr t9 */
959 /* The format of subsequent PLT entries. */
960 static const bfd_vma mips_vxworks_exec_plt_entry[] =
962 0x10000000, /* b .PLT_resolver */
963 0x24180000, /* li t8, <pltindex> */
964 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
965 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
966 0x8f390000, /* lw t9, 0(t9) */
967 0x00000000, /* nop */
968 0x03200008, /* jr t9 */
972 /* The format of the first PLT entry in a VxWorks shared object. */
973 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
975 0x8f990008, /* lw t9, 8(gp) */
976 0x00000000, /* nop */
977 0x03200008, /* jr t9 */
978 0x00000000, /* nop */
979 0x00000000, /* nop */
983 /* The format of subsequent PLT entries. */
984 static const bfd_vma mips_vxworks_shared_plt_entry[] =
986 0x10000000, /* b .PLT_resolver */
987 0x24180000 /* li t8, <pltindex> */
990 /* microMIPS 32-bit opcode helper installer. */
993 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
995 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
996 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
999 /* microMIPS 32-bit opcode helper retriever. */
1002 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1004 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1007 /* Look up an entry in a MIPS ELF linker hash table. */
1009 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1010 ((struct mips_elf_link_hash_entry *) \
1011 elf_link_hash_lookup (&(table)->root, (string), (create), \
1014 /* Traverse a MIPS ELF linker hash table. */
1016 #define mips_elf_link_hash_traverse(table, func, info) \
1017 (elf_link_hash_traverse \
1019 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1022 /* Find the base offsets for thread-local storage in this object,
1023 for GD/LD and IE/LE respectively. */
1025 #define TP_OFFSET 0x7000
1026 #define DTP_OFFSET 0x8000
1029 dtprel_base (struct bfd_link_info *info)
1031 /* If tls_sec is NULL, we should have signalled an error already. */
1032 if (elf_hash_table (info)->tls_sec == NULL)
1034 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1038 tprel_base (struct bfd_link_info *info)
1040 /* If tls_sec is NULL, we should have signalled an error already. */
1041 if (elf_hash_table (info)->tls_sec == NULL)
1043 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1046 /* Create an entry in a MIPS ELF linker hash table. */
1048 static struct bfd_hash_entry *
1049 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1050 struct bfd_hash_table *table, const char *string)
1052 struct mips_elf_link_hash_entry *ret =
1053 (struct mips_elf_link_hash_entry *) entry;
1055 /* Allocate the structure if it has not already been allocated by a
1058 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1060 return (struct bfd_hash_entry *) ret;
1062 /* Call the allocation method of the superclass. */
1063 ret = ((struct mips_elf_link_hash_entry *)
1064 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1068 /* Set local fields. */
1069 memset (&ret->esym, 0, sizeof (EXTR));
1070 /* We use -2 as a marker to indicate that the information has
1071 not been set. -1 means there is no associated ifd. */
1074 ret->possibly_dynamic_relocs = 0;
1075 ret->fn_stub = NULL;
1076 ret->call_stub = NULL;
1077 ret->call_fp_stub = NULL;
1078 ret->tls_ie_type = GOT_NORMAL;
1079 ret->tls_gd_type = GOT_NORMAL;
1080 ret->global_got_area = GGA_NONE;
1081 ret->got_only_for_calls = TRUE;
1082 ret->readonly_reloc = FALSE;
1083 ret->has_static_relocs = FALSE;
1084 ret->no_fn_stub = FALSE;
1085 ret->need_fn_stub = FALSE;
1086 ret->has_nonpic_branches = FALSE;
1087 ret->needs_lazy_stub = FALSE;
1090 return (struct bfd_hash_entry *) ret;
1093 /* Allocate MIPS ELF private object data. */
1096 _bfd_mips_elf_mkobject (bfd *abfd)
1098 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1103 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1105 if (!sec->used_by_bfd)
1107 struct _mips_elf_section_data *sdata;
1108 bfd_size_type amt = sizeof (*sdata);
1110 sdata = bfd_zalloc (abfd, amt);
1113 sec->used_by_bfd = sdata;
1116 return _bfd_elf_new_section_hook (abfd, sec);
1119 /* Read ECOFF debugging information from a .mdebug section into a
1120 ecoff_debug_info structure. */
1123 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1124 struct ecoff_debug_info *debug)
1127 const struct ecoff_debug_swap *swap;
1130 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1131 memset (debug, 0, sizeof (*debug));
1133 ext_hdr = bfd_malloc (swap->external_hdr_size);
1134 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1137 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1138 swap->external_hdr_size))
1141 symhdr = &debug->symbolic_header;
1142 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1144 /* The symbolic header contains absolute file offsets and sizes to
1146 #define READ(ptr, offset, count, size, type) \
1147 if (symhdr->count == 0) \
1148 debug->ptr = NULL; \
1151 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1152 debug->ptr = bfd_malloc (amt); \
1153 if (debug->ptr == NULL) \
1154 goto error_return; \
1155 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1156 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1157 goto error_return; \
1160 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1161 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1162 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1163 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1164 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1165 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1167 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1168 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1169 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1170 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1171 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1179 if (ext_hdr != NULL)
1181 if (debug->line != NULL)
1183 if (debug->external_dnr != NULL)
1184 free (debug->external_dnr);
1185 if (debug->external_pdr != NULL)
1186 free (debug->external_pdr);
1187 if (debug->external_sym != NULL)
1188 free (debug->external_sym);
1189 if (debug->external_opt != NULL)
1190 free (debug->external_opt);
1191 if (debug->external_aux != NULL)
1192 free (debug->external_aux);
1193 if (debug->ss != NULL)
1195 if (debug->ssext != NULL)
1196 free (debug->ssext);
1197 if (debug->external_fdr != NULL)
1198 free (debug->external_fdr);
1199 if (debug->external_rfd != NULL)
1200 free (debug->external_rfd);
1201 if (debug->external_ext != NULL)
1202 free (debug->external_ext);
1206 /* Swap RPDR (runtime procedure table entry) for output. */
1209 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1211 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1212 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1213 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1214 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1215 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1216 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1218 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1219 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1221 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1224 /* Create a runtime procedure table from the .mdebug section. */
1227 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1228 struct bfd_link_info *info, asection *s,
1229 struct ecoff_debug_info *debug)
1231 const struct ecoff_debug_swap *swap;
1232 HDRR *hdr = &debug->symbolic_header;
1234 struct rpdr_ext *erp;
1236 struct pdr_ext *epdr;
1237 struct sym_ext *esym;
1241 bfd_size_type count;
1242 unsigned long sindex;
1246 const char *no_name_func = _("static procedure (no name)");
1254 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1256 sindex = strlen (no_name_func) + 1;
1257 count = hdr->ipdMax;
1260 size = swap->external_pdr_size;
1262 epdr = bfd_malloc (size * count);
1266 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1269 size = sizeof (RPDR);
1270 rp = rpdr = bfd_malloc (size * count);
1274 size = sizeof (char *);
1275 sv = bfd_malloc (size * count);
1279 count = hdr->isymMax;
1280 size = swap->external_sym_size;
1281 esym = bfd_malloc (size * count);
1285 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1288 count = hdr->issMax;
1289 ss = bfd_malloc (count);
1292 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1295 count = hdr->ipdMax;
1296 for (i = 0; i < (unsigned long) count; i++, rp++)
1298 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1299 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1300 rp->adr = sym.value;
1301 rp->regmask = pdr.regmask;
1302 rp->regoffset = pdr.regoffset;
1303 rp->fregmask = pdr.fregmask;
1304 rp->fregoffset = pdr.fregoffset;
1305 rp->frameoffset = pdr.frameoffset;
1306 rp->framereg = pdr.framereg;
1307 rp->pcreg = pdr.pcreg;
1309 sv[i] = ss + sym.iss;
1310 sindex += strlen (sv[i]) + 1;
1314 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1315 size = BFD_ALIGN (size, 16);
1316 rtproc = bfd_alloc (abfd, size);
1319 mips_elf_hash_table (info)->procedure_count = 0;
1323 mips_elf_hash_table (info)->procedure_count = count + 2;
1326 memset (erp, 0, sizeof (struct rpdr_ext));
1328 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1329 strcpy (str, no_name_func);
1330 str += strlen (no_name_func) + 1;
1331 for (i = 0; i < count; i++)
1333 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1334 strcpy (str, sv[i]);
1335 str += strlen (sv[i]) + 1;
1337 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1339 /* Set the size and contents of .rtproc section. */
1341 s->contents = rtproc;
1343 /* Skip this section later on (I don't think this currently
1344 matters, but someday it might). */
1345 s->map_head.link_order = NULL;
1374 /* We're going to create a stub for H. Create a symbol for the stub's
1375 value and size, to help make the disassembly easier to read. */
1378 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1379 struct mips_elf_link_hash_entry *h,
1380 const char *prefix, asection *s, bfd_vma value,
1383 struct bfd_link_hash_entry *bh;
1384 struct elf_link_hash_entry *elfh;
1387 if (ELF_ST_IS_MICROMIPS (h->root.other))
1390 /* Create a new symbol. */
1391 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1393 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1394 BSF_LOCAL, s, value, NULL,
1398 /* Make it a local function. */
1399 elfh = (struct elf_link_hash_entry *) bh;
1400 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1402 elfh->forced_local = 1;
1406 /* We're about to redefine H. Create a symbol to represent H's
1407 current value and size, to help make the disassembly easier
1411 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1412 struct mips_elf_link_hash_entry *h,
1415 struct bfd_link_hash_entry *bh;
1416 struct elf_link_hash_entry *elfh;
1421 /* Read the symbol's value. */
1422 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1423 || h->root.root.type == bfd_link_hash_defweak);
1424 s = h->root.root.u.def.section;
1425 value = h->root.root.u.def.value;
1427 /* Create a new symbol. */
1428 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1430 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1431 BSF_LOCAL, s, value, NULL,
1435 /* Make it local and copy the other attributes from H. */
1436 elfh = (struct elf_link_hash_entry *) bh;
1437 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1438 elfh->other = h->root.other;
1439 elfh->size = h->root.size;
1440 elfh->forced_local = 1;
1444 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1445 function rather than to a hard-float stub. */
1448 section_allows_mips16_refs_p (asection *section)
1452 name = bfd_get_section_name (section->owner, section);
1453 return (FN_STUB_P (name)
1454 || CALL_STUB_P (name)
1455 || CALL_FP_STUB_P (name)
1456 || strcmp (name, ".pdr") == 0);
1459 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1460 stub section of some kind. Return the R_SYMNDX of the target
1461 function, or 0 if we can't decide which function that is. */
1463 static unsigned long
1464 mips16_stub_symndx (const struct elf_backend_data *bed,
1465 asection *sec ATTRIBUTE_UNUSED,
1466 const Elf_Internal_Rela *relocs,
1467 const Elf_Internal_Rela *relend)
1469 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1470 const Elf_Internal_Rela *rel;
1472 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1473 one in a compound relocation. */
1474 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1475 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1476 return ELF_R_SYM (sec->owner, rel->r_info);
1478 /* Otherwise trust the first relocation, whatever its kind. This is
1479 the traditional behavior. */
1480 if (relocs < relend)
1481 return ELF_R_SYM (sec->owner, relocs->r_info);
1486 /* Check the mips16 stubs for a particular symbol, and see if we can
1490 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1491 struct mips_elf_link_hash_entry *h)
1493 /* Dynamic symbols must use the standard call interface, in case other
1494 objects try to call them. */
1495 if (h->fn_stub != NULL
1496 && h->root.dynindx != -1)
1498 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1499 h->need_fn_stub = TRUE;
1502 if (h->fn_stub != NULL
1503 && ! h->need_fn_stub)
1505 /* We don't need the fn_stub; the only references to this symbol
1506 are 16 bit calls. Clobber the size to 0 to prevent it from
1507 being included in the link. */
1508 h->fn_stub->size = 0;
1509 h->fn_stub->flags &= ~SEC_RELOC;
1510 h->fn_stub->reloc_count = 0;
1511 h->fn_stub->flags |= SEC_EXCLUDE;
1514 if (h->call_stub != NULL
1515 && ELF_ST_IS_MIPS16 (h->root.other))
1517 /* We don't need the call_stub; this is a 16 bit function, so
1518 calls from other 16 bit functions are OK. Clobber the size
1519 to 0 to prevent it from being included in the link. */
1520 h->call_stub->size = 0;
1521 h->call_stub->flags &= ~SEC_RELOC;
1522 h->call_stub->reloc_count = 0;
1523 h->call_stub->flags |= SEC_EXCLUDE;
1526 if (h->call_fp_stub != NULL
1527 && ELF_ST_IS_MIPS16 (h->root.other))
1529 /* We don't need the call_stub; this is a 16 bit function, so
1530 calls from other 16 bit functions are OK. Clobber the size
1531 to 0 to prevent it from being included in the link. */
1532 h->call_fp_stub->size = 0;
1533 h->call_fp_stub->flags &= ~SEC_RELOC;
1534 h->call_fp_stub->reloc_count = 0;
1535 h->call_fp_stub->flags |= SEC_EXCLUDE;
1539 /* Hashtable callbacks for mips_elf_la25_stubs. */
1542 mips_elf_la25_stub_hash (const void *entry_)
1544 const struct mips_elf_la25_stub *entry;
1546 entry = (struct mips_elf_la25_stub *) entry_;
1547 return entry->h->root.root.u.def.section->id
1548 + entry->h->root.root.u.def.value;
1552 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1554 const struct mips_elf_la25_stub *entry1, *entry2;
1556 entry1 = (struct mips_elf_la25_stub *) entry1_;
1557 entry2 = (struct mips_elf_la25_stub *) entry2_;
1558 return ((entry1->h->root.root.u.def.section
1559 == entry2->h->root.root.u.def.section)
1560 && (entry1->h->root.root.u.def.value
1561 == entry2->h->root.root.u.def.value));
1564 /* Called by the linker to set up the la25 stub-creation code. FN is
1565 the linker's implementation of add_stub_function. Return true on
1569 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1570 asection *(*fn) (const char *, asection *,
1573 struct mips_elf_link_hash_table *htab;
1575 htab = mips_elf_hash_table (info);
1579 htab->add_stub_section = fn;
1580 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1581 mips_elf_la25_stub_eq, NULL);
1582 if (htab->la25_stubs == NULL)
1588 /* Return true if H is a locally-defined PIC function, in the sense
1589 that it or its fn_stub might need $25 to be valid on entry.
1590 Note that MIPS16 functions set up $gp using PC-relative instructions,
1591 so they themselves never need $25 to be valid. Only non-MIPS16
1592 entry points are of interest here. */
1595 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1597 return ((h->root.root.type == bfd_link_hash_defined
1598 || h->root.root.type == bfd_link_hash_defweak)
1599 && h->root.def_regular
1600 && !bfd_is_abs_section (h->root.root.u.def.section)
1601 && (!ELF_ST_IS_MIPS16 (h->root.other)
1602 || (h->fn_stub && h->need_fn_stub))
1603 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1604 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1607 /* Set *SEC to the input section that contains the target of STUB.
1608 Return the offset of the target from the start of that section. */
1611 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1614 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1616 BFD_ASSERT (stub->h->need_fn_stub);
1617 *sec = stub->h->fn_stub;
1622 *sec = stub->h->root.root.u.def.section;
1623 return stub->h->root.root.u.def.value;
1627 /* STUB describes an la25 stub that we have decided to implement
1628 by inserting an LUI/ADDIU pair before the target function.
1629 Create the section and redirect the function symbol to it. */
1632 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1633 struct bfd_link_info *info)
1635 struct mips_elf_link_hash_table *htab;
1637 asection *s, *input_section;
1640 htab = mips_elf_hash_table (info);
1644 /* Create a unique name for the new section. */
1645 name = bfd_malloc (11 + sizeof (".text.stub."));
1648 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1650 /* Create the section. */
1651 mips_elf_get_la25_target (stub, &input_section);
1652 s = htab->add_stub_section (name, input_section,
1653 input_section->output_section);
1657 /* Make sure that any padding goes before the stub. */
1658 align = input_section->alignment_power;
1659 if (!bfd_set_section_alignment (s->owner, s, align))
1662 s->size = (1 << align) - 8;
1664 /* Create a symbol for the stub. */
1665 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1666 stub->stub_section = s;
1667 stub->offset = s->size;
1669 /* Allocate room for it. */
1674 /* STUB describes an la25 stub that we have decided to implement
1675 with a separate trampoline. Allocate room for it and redirect
1676 the function symbol to it. */
1679 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1680 struct bfd_link_info *info)
1682 struct mips_elf_link_hash_table *htab;
1685 htab = mips_elf_hash_table (info);
1689 /* Create a trampoline section, if we haven't already. */
1690 s = htab->strampoline;
1693 asection *input_section = stub->h->root.root.u.def.section;
1694 s = htab->add_stub_section (".text", NULL,
1695 input_section->output_section);
1696 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1698 htab->strampoline = s;
1701 /* Create a symbol for the stub. */
1702 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1703 stub->stub_section = s;
1704 stub->offset = s->size;
1706 /* Allocate room for it. */
1711 /* H describes a symbol that needs an la25 stub. Make sure that an
1712 appropriate stub exists and point H at it. */
1715 mips_elf_add_la25_stub (struct bfd_link_info *info,
1716 struct mips_elf_link_hash_entry *h)
1718 struct mips_elf_link_hash_table *htab;
1719 struct mips_elf_la25_stub search, *stub;
1720 bfd_boolean use_trampoline_p;
1725 /* Describe the stub we want. */
1726 search.stub_section = NULL;
1730 /* See if we've already created an equivalent stub. */
1731 htab = mips_elf_hash_table (info);
1735 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1739 stub = (struct mips_elf_la25_stub *) *slot;
1742 /* We can reuse the existing stub. */
1743 h->la25_stub = stub;
1747 /* Create a permanent copy of ENTRY and add it to the hash table. */
1748 stub = bfd_malloc (sizeof (search));
1754 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1755 of the section and if we would need no more than 2 nops. */
1756 value = mips_elf_get_la25_target (stub, &s);
1757 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1759 h->la25_stub = stub;
1760 return (use_trampoline_p
1761 ? mips_elf_add_la25_trampoline (stub, info)
1762 : mips_elf_add_la25_intro (stub, info));
1765 /* A mips_elf_link_hash_traverse callback that is called before sizing
1766 sections. DATA points to a mips_htab_traverse_info structure. */
1769 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1771 struct mips_htab_traverse_info *hti;
1773 hti = (struct mips_htab_traverse_info *) data;
1774 if (!hti->info->relocatable)
1775 mips_elf_check_mips16_stubs (hti->info, h);
1777 if (mips_elf_local_pic_function_p (h))
1779 /* PR 12845: If H is in a section that has been garbage
1780 collected it will have its output section set to *ABS*. */
1781 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1784 /* H is a function that might need $25 to be valid on entry.
1785 If we're creating a non-PIC relocatable object, mark H as
1786 being PIC. If we're creating a non-relocatable object with
1787 non-PIC branches and jumps to H, make sure that H has an la25
1789 if (hti->info->relocatable)
1791 if (!PIC_OBJECT_P (hti->output_bfd))
1792 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1794 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1803 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1804 Most mips16 instructions are 16 bits, but these instructions
1807 The format of these instructions is:
1809 +--------------+--------------------------------+
1810 | JALX | X| Imm 20:16 | Imm 25:21 |
1811 +--------------+--------------------------------+
1813 +-----------------------------------------------+
1815 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1816 Note that the immediate value in the first word is swapped.
1818 When producing a relocatable object file, R_MIPS16_26 is
1819 handled mostly like R_MIPS_26. In particular, the addend is
1820 stored as a straight 26-bit value in a 32-bit instruction.
1821 (gas makes life simpler for itself by never adjusting a
1822 R_MIPS16_26 reloc to be against a section, so the addend is
1823 always zero). However, the 32 bit instruction is stored as 2
1824 16-bit values, rather than a single 32-bit value. In a
1825 big-endian file, the result is the same; in a little-endian
1826 file, the two 16-bit halves of the 32 bit value are swapped.
1827 This is so that a disassembler can recognize the jal
1830 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1831 instruction stored as two 16-bit values. The addend A is the
1832 contents of the targ26 field. The calculation is the same as
1833 R_MIPS_26. When storing the calculated value, reorder the
1834 immediate value as shown above, and don't forget to store the
1835 value as two 16-bit values.
1837 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1841 +--------+----------------------+
1845 +--------+----------------------+
1848 +----------+------+-------------+
1852 +----------+--------------------+
1853 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1854 ((sub1 << 16) | sub2)).
1856 When producing a relocatable object file, the calculation is
1857 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1858 When producing a fully linked file, the calculation is
1859 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1860 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1862 The table below lists the other MIPS16 instruction relocations.
1863 Each one is calculated in the same way as the non-MIPS16 relocation
1864 given on the right, but using the extended MIPS16 layout of 16-bit
1867 R_MIPS16_GPREL R_MIPS_GPREL16
1868 R_MIPS16_GOT16 R_MIPS_GOT16
1869 R_MIPS16_CALL16 R_MIPS_CALL16
1870 R_MIPS16_HI16 R_MIPS_HI16
1871 R_MIPS16_LO16 R_MIPS_LO16
1873 A typical instruction will have a format like this:
1875 +--------------+--------------------------------+
1876 | EXTEND | Imm 10:5 | Imm 15:11 |
1877 +--------------+--------------------------------+
1878 | Major | rx | ry | Imm 4:0 |
1879 +--------------+--------------------------------+
1881 EXTEND is the five bit value 11110. Major is the instruction
1884 All we need to do here is shuffle the bits appropriately.
1885 As above, the two 16-bit halves must be swapped on a
1886 little-endian system. */
1888 static inline bfd_boolean
1889 mips16_reloc_p (int r_type)
1894 case R_MIPS16_GPREL:
1895 case R_MIPS16_GOT16:
1896 case R_MIPS16_CALL16:
1899 case R_MIPS16_TLS_GD:
1900 case R_MIPS16_TLS_LDM:
1901 case R_MIPS16_TLS_DTPREL_HI16:
1902 case R_MIPS16_TLS_DTPREL_LO16:
1903 case R_MIPS16_TLS_GOTTPREL:
1904 case R_MIPS16_TLS_TPREL_HI16:
1905 case R_MIPS16_TLS_TPREL_LO16:
1913 /* Check if a microMIPS reloc. */
1915 static inline bfd_boolean
1916 micromips_reloc_p (unsigned int r_type)
1918 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
1921 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
1922 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
1923 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
1925 static inline bfd_boolean
1926 micromips_reloc_shuffle_p (unsigned int r_type)
1928 return (micromips_reloc_p (r_type)
1929 && r_type != R_MICROMIPS_PC7_S1
1930 && r_type != R_MICROMIPS_PC10_S1);
1933 static inline bfd_boolean
1934 got16_reloc_p (int r_type)
1936 return (r_type == R_MIPS_GOT16
1937 || r_type == R_MIPS16_GOT16
1938 || r_type == R_MICROMIPS_GOT16);
1941 static inline bfd_boolean
1942 call16_reloc_p (int r_type)
1944 return (r_type == R_MIPS_CALL16
1945 || r_type == R_MIPS16_CALL16
1946 || r_type == R_MICROMIPS_CALL16);
1949 static inline bfd_boolean
1950 got_disp_reloc_p (unsigned int r_type)
1952 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
1955 static inline bfd_boolean
1956 got_page_reloc_p (unsigned int r_type)
1958 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
1961 static inline bfd_boolean
1962 got_ofst_reloc_p (unsigned int r_type)
1964 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
1967 static inline bfd_boolean
1968 got_hi16_reloc_p (unsigned int r_type)
1970 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
1973 static inline bfd_boolean
1974 got_lo16_reloc_p (unsigned int r_type)
1976 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
1979 static inline bfd_boolean
1980 call_hi16_reloc_p (unsigned int r_type)
1982 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
1985 static inline bfd_boolean
1986 call_lo16_reloc_p (unsigned int r_type)
1988 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
1991 static inline bfd_boolean
1992 hi16_reloc_p (int r_type)
1994 return (r_type == R_MIPS_HI16
1995 || r_type == R_MIPS16_HI16
1996 || r_type == R_MICROMIPS_HI16);
1999 static inline bfd_boolean
2000 lo16_reloc_p (int r_type)
2002 return (r_type == R_MIPS_LO16
2003 || r_type == R_MIPS16_LO16
2004 || r_type == R_MICROMIPS_LO16);
2007 static inline bfd_boolean
2008 mips16_call_reloc_p (int r_type)
2010 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2013 static inline bfd_boolean
2014 jal_reloc_p (int r_type)
2016 return (r_type == R_MIPS_26
2017 || r_type == R_MIPS16_26
2018 || r_type == R_MICROMIPS_26_S1);
2021 static inline bfd_boolean
2022 micromips_branch_reloc_p (int r_type)
2024 return (r_type == R_MICROMIPS_26_S1
2025 || r_type == R_MICROMIPS_PC16_S1
2026 || r_type == R_MICROMIPS_PC10_S1
2027 || r_type == R_MICROMIPS_PC7_S1);
2030 static inline bfd_boolean
2031 tls_gd_reloc_p (unsigned int r_type)
2033 return (r_type == R_MIPS_TLS_GD
2034 || r_type == R_MIPS16_TLS_GD
2035 || r_type == R_MICROMIPS_TLS_GD);
2038 static inline bfd_boolean
2039 tls_ldm_reloc_p (unsigned int r_type)
2041 return (r_type == R_MIPS_TLS_LDM
2042 || r_type == R_MIPS16_TLS_LDM
2043 || r_type == R_MICROMIPS_TLS_LDM);
2046 static inline bfd_boolean
2047 tls_gottprel_reloc_p (unsigned int r_type)
2049 return (r_type == R_MIPS_TLS_GOTTPREL
2050 || r_type == R_MIPS16_TLS_GOTTPREL
2051 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2055 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2056 bfd_boolean jal_shuffle, bfd_byte *data)
2058 bfd_vma first, second, val;
2060 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2063 /* Pick up the first and second halfwords of the instruction. */
2064 first = bfd_get_16 (abfd, data);
2065 second = bfd_get_16 (abfd, data + 2);
2066 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2067 val = first << 16 | second;
2068 else if (r_type != R_MIPS16_26)
2069 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2070 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2072 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2073 | ((first & 0x1f) << 21) | second);
2074 bfd_put_32 (abfd, val, data);
2078 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2079 bfd_boolean jal_shuffle, bfd_byte *data)
2081 bfd_vma first, second, val;
2083 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2086 val = bfd_get_32 (abfd, data);
2087 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2089 second = val & 0xffff;
2092 else if (r_type != R_MIPS16_26)
2094 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2095 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2099 second = val & 0xffff;
2100 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2101 | ((val >> 21) & 0x1f);
2103 bfd_put_16 (abfd, second, data + 2);
2104 bfd_put_16 (abfd, first, data);
2107 bfd_reloc_status_type
2108 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2109 arelent *reloc_entry, asection *input_section,
2110 bfd_boolean relocatable, void *data, bfd_vma gp)
2114 bfd_reloc_status_type status;
2116 if (bfd_is_com_section (symbol->section))
2119 relocation = symbol->value;
2121 relocation += symbol->section->output_section->vma;
2122 relocation += symbol->section->output_offset;
2124 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2125 return bfd_reloc_outofrange;
2127 /* Set val to the offset into the section or symbol. */
2128 val = reloc_entry->addend;
2130 _bfd_mips_elf_sign_extend (val, 16);
2132 /* Adjust val for the final section location and GP value. If we
2133 are producing relocatable output, we don't want to do this for
2134 an external symbol. */
2136 || (symbol->flags & BSF_SECTION_SYM) != 0)
2137 val += relocation - gp;
2139 if (reloc_entry->howto->partial_inplace)
2141 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2143 + reloc_entry->address);
2144 if (status != bfd_reloc_ok)
2148 reloc_entry->addend = val;
2151 reloc_entry->address += input_section->output_offset;
2153 return bfd_reloc_ok;
2156 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2157 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2158 that contains the relocation field and DATA points to the start of
2163 struct mips_hi16 *next;
2165 asection *input_section;
2169 /* FIXME: This should not be a static variable. */
2171 static struct mips_hi16 *mips_hi16_list;
2173 /* A howto special_function for REL *HI16 relocations. We can only
2174 calculate the correct value once we've seen the partnering
2175 *LO16 relocation, so just save the information for later.
2177 The ABI requires that the *LO16 immediately follow the *HI16.
2178 However, as a GNU extension, we permit an arbitrary number of
2179 *HI16s to be associated with a single *LO16. This significantly
2180 simplies the relocation handling in gcc. */
2182 bfd_reloc_status_type
2183 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2184 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2185 asection *input_section, bfd *output_bfd,
2186 char **error_message ATTRIBUTE_UNUSED)
2188 struct mips_hi16 *n;
2190 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2191 return bfd_reloc_outofrange;
2193 n = bfd_malloc (sizeof *n);
2195 return bfd_reloc_outofrange;
2197 n->next = mips_hi16_list;
2199 n->input_section = input_section;
2200 n->rel = *reloc_entry;
2203 if (output_bfd != NULL)
2204 reloc_entry->address += input_section->output_offset;
2206 return bfd_reloc_ok;
2209 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2210 like any other 16-bit relocation when applied to global symbols, but is
2211 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2213 bfd_reloc_status_type
2214 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2215 void *data, asection *input_section,
2216 bfd *output_bfd, char **error_message)
2218 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2219 || bfd_is_und_section (bfd_get_section (symbol))
2220 || bfd_is_com_section (bfd_get_section (symbol)))
2221 /* The relocation is against a global symbol. */
2222 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2223 input_section, output_bfd,
2226 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2227 input_section, output_bfd, error_message);
2230 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2231 is a straightforward 16 bit inplace relocation, but we must deal with
2232 any partnering high-part relocations as well. */
2234 bfd_reloc_status_type
2235 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2236 void *data, asection *input_section,
2237 bfd *output_bfd, char **error_message)
2240 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2242 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2243 return bfd_reloc_outofrange;
2245 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2247 vallo = bfd_get_32 (abfd, location);
2248 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2251 while (mips_hi16_list != NULL)
2253 bfd_reloc_status_type ret;
2254 struct mips_hi16 *hi;
2256 hi = mips_hi16_list;
2258 /* R_MIPS*_GOT16 relocations are something of a special case. We
2259 want to install the addend in the same way as for a R_MIPS*_HI16
2260 relocation (with a rightshift of 16). However, since GOT16
2261 relocations can also be used with global symbols, their howto
2262 has a rightshift of 0. */
2263 if (hi->rel.howto->type == R_MIPS_GOT16)
2264 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2265 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2266 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2267 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2268 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2270 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2271 carry or borrow will induce a change of +1 or -1 in the high part. */
2272 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2274 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2275 hi->input_section, output_bfd,
2277 if (ret != bfd_reloc_ok)
2280 mips_hi16_list = hi->next;
2284 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2285 input_section, output_bfd,
2289 /* A generic howto special_function. This calculates and installs the
2290 relocation itself, thus avoiding the oft-discussed problems in
2291 bfd_perform_relocation and bfd_install_relocation. */
2293 bfd_reloc_status_type
2294 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2295 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2296 asection *input_section, bfd *output_bfd,
2297 char **error_message ATTRIBUTE_UNUSED)
2300 bfd_reloc_status_type status;
2301 bfd_boolean relocatable;
2303 relocatable = (output_bfd != NULL);
2305 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2306 return bfd_reloc_outofrange;
2308 /* Build up the field adjustment in VAL. */
2310 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2312 /* Either we're calculating the final field value or we have a
2313 relocation against a section symbol. Add in the section's
2314 offset or address. */
2315 val += symbol->section->output_section->vma;
2316 val += symbol->section->output_offset;
2321 /* We're calculating the final field value. Add in the symbol's value
2322 and, if pc-relative, subtract the address of the field itself. */
2323 val += symbol->value;
2324 if (reloc_entry->howto->pc_relative)
2326 val -= input_section->output_section->vma;
2327 val -= input_section->output_offset;
2328 val -= reloc_entry->address;
2332 /* VAL is now the final adjustment. If we're keeping this relocation
2333 in the output file, and if the relocation uses a separate addend,
2334 we just need to add VAL to that addend. Otherwise we need to add
2335 VAL to the relocation field itself. */
2336 if (relocatable && !reloc_entry->howto->partial_inplace)
2337 reloc_entry->addend += val;
2340 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2342 /* Add in the separate addend, if any. */
2343 val += reloc_entry->addend;
2345 /* Add VAL to the relocation field. */
2346 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2348 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2350 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2353 if (status != bfd_reloc_ok)
2358 reloc_entry->address += input_section->output_offset;
2360 return bfd_reloc_ok;
2363 /* Swap an entry in a .gptab section. Note that these routines rely
2364 on the equivalence of the two elements of the union. */
2367 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2370 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2371 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2375 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2376 Elf32_External_gptab *ex)
2378 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2379 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2383 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2384 Elf32_External_compact_rel *ex)
2386 H_PUT_32 (abfd, in->id1, ex->id1);
2387 H_PUT_32 (abfd, in->num, ex->num);
2388 H_PUT_32 (abfd, in->id2, ex->id2);
2389 H_PUT_32 (abfd, in->offset, ex->offset);
2390 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2391 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2395 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2396 Elf32_External_crinfo *ex)
2400 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2401 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2402 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2403 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2404 H_PUT_32 (abfd, l, ex->info);
2405 H_PUT_32 (abfd, in->konst, ex->konst);
2406 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2409 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2410 routines swap this structure in and out. They are used outside of
2411 BFD, so they are globally visible. */
2414 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2417 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2418 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2419 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2420 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2421 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2422 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2426 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2427 Elf32_External_RegInfo *ex)
2429 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2430 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2431 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2432 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2433 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2434 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2437 /* In the 64 bit ABI, the .MIPS.options section holds register
2438 information in an Elf64_Reginfo structure. These routines swap
2439 them in and out. They are globally visible because they are used
2440 outside of BFD. These routines are here so that gas can call them
2441 without worrying about whether the 64 bit ABI has been included. */
2444 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2445 Elf64_Internal_RegInfo *in)
2447 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2448 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2449 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2450 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2451 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2452 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2453 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2457 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2458 Elf64_External_RegInfo *ex)
2460 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2461 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2462 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2463 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2464 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2465 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2466 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2469 /* Swap in an options header. */
2472 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2473 Elf_Internal_Options *in)
2475 in->kind = H_GET_8 (abfd, ex->kind);
2476 in->size = H_GET_8 (abfd, ex->size);
2477 in->section = H_GET_16 (abfd, ex->section);
2478 in->info = H_GET_32 (abfd, ex->info);
2481 /* Swap out an options header. */
2484 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2485 Elf_External_Options *ex)
2487 H_PUT_8 (abfd, in->kind, ex->kind);
2488 H_PUT_8 (abfd, in->size, ex->size);
2489 H_PUT_16 (abfd, in->section, ex->section);
2490 H_PUT_32 (abfd, in->info, ex->info);
2493 /* This function is called via qsort() to sort the dynamic relocation
2494 entries by increasing r_symndx value. */
2497 sort_dynamic_relocs (const void *arg1, const void *arg2)
2499 Elf_Internal_Rela int_reloc1;
2500 Elf_Internal_Rela int_reloc2;
2503 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2504 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2506 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2510 if (int_reloc1.r_offset < int_reloc2.r_offset)
2512 if (int_reloc1.r_offset > int_reloc2.r_offset)
2517 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2520 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2521 const void *arg2 ATTRIBUTE_UNUSED)
2524 Elf_Internal_Rela int_reloc1[3];
2525 Elf_Internal_Rela int_reloc2[3];
2527 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2528 (reldyn_sorting_bfd, arg1, int_reloc1);
2529 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2530 (reldyn_sorting_bfd, arg2, int_reloc2);
2532 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2534 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2537 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2539 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2548 /* This routine is used to write out ECOFF debugging external symbol
2549 information. It is called via mips_elf_link_hash_traverse. The
2550 ECOFF external symbol information must match the ELF external
2551 symbol information. Unfortunately, at this point we don't know
2552 whether a symbol is required by reloc information, so the two
2553 tables may wind up being different. We must sort out the external
2554 symbol information before we can set the final size of the .mdebug
2555 section, and we must set the size of the .mdebug section before we
2556 can relocate any sections, and we can't know which symbols are
2557 required by relocation until we relocate the sections.
2558 Fortunately, it is relatively unlikely that any symbol will be
2559 stripped but required by a reloc. In particular, it can not happen
2560 when generating a final executable. */
2563 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2565 struct extsym_info *einfo = data;
2567 asection *sec, *output_section;
2569 if (h->root.indx == -2)
2571 else if ((h->root.def_dynamic
2572 || h->root.ref_dynamic
2573 || h->root.type == bfd_link_hash_new)
2574 && !h->root.def_regular
2575 && !h->root.ref_regular)
2577 else if (einfo->info->strip == strip_all
2578 || (einfo->info->strip == strip_some
2579 && bfd_hash_lookup (einfo->info->keep_hash,
2580 h->root.root.root.string,
2581 FALSE, FALSE) == NULL))
2589 if (h->esym.ifd == -2)
2592 h->esym.cobol_main = 0;
2593 h->esym.weakext = 0;
2594 h->esym.reserved = 0;
2595 h->esym.ifd = ifdNil;
2596 h->esym.asym.value = 0;
2597 h->esym.asym.st = stGlobal;
2599 if (h->root.root.type == bfd_link_hash_undefined
2600 || h->root.root.type == bfd_link_hash_undefweak)
2604 /* Use undefined class. Also, set class and type for some
2606 name = h->root.root.root.string;
2607 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2608 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2610 h->esym.asym.sc = scData;
2611 h->esym.asym.st = stLabel;
2612 h->esym.asym.value = 0;
2614 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2616 h->esym.asym.sc = scAbs;
2617 h->esym.asym.st = stLabel;
2618 h->esym.asym.value =
2619 mips_elf_hash_table (einfo->info)->procedure_count;
2621 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2623 h->esym.asym.sc = scAbs;
2624 h->esym.asym.st = stLabel;
2625 h->esym.asym.value = elf_gp (einfo->abfd);
2628 h->esym.asym.sc = scUndefined;
2630 else if (h->root.root.type != bfd_link_hash_defined
2631 && h->root.root.type != bfd_link_hash_defweak)
2632 h->esym.asym.sc = scAbs;
2637 sec = h->root.root.u.def.section;
2638 output_section = sec->output_section;
2640 /* When making a shared library and symbol h is the one from
2641 the another shared library, OUTPUT_SECTION may be null. */
2642 if (output_section == NULL)
2643 h->esym.asym.sc = scUndefined;
2646 name = bfd_section_name (output_section->owner, output_section);
2648 if (strcmp (name, ".text") == 0)
2649 h->esym.asym.sc = scText;
2650 else if (strcmp (name, ".data") == 0)
2651 h->esym.asym.sc = scData;
2652 else if (strcmp (name, ".sdata") == 0)
2653 h->esym.asym.sc = scSData;
2654 else if (strcmp (name, ".rodata") == 0
2655 || strcmp (name, ".rdata") == 0)
2656 h->esym.asym.sc = scRData;
2657 else if (strcmp (name, ".bss") == 0)
2658 h->esym.asym.sc = scBss;
2659 else if (strcmp (name, ".sbss") == 0)
2660 h->esym.asym.sc = scSBss;
2661 else if (strcmp (name, ".init") == 0)
2662 h->esym.asym.sc = scInit;
2663 else if (strcmp (name, ".fini") == 0)
2664 h->esym.asym.sc = scFini;
2666 h->esym.asym.sc = scAbs;
2670 h->esym.asym.reserved = 0;
2671 h->esym.asym.index = indexNil;
2674 if (h->root.root.type == bfd_link_hash_common)
2675 h->esym.asym.value = h->root.root.u.c.size;
2676 else if (h->root.root.type == bfd_link_hash_defined
2677 || h->root.root.type == bfd_link_hash_defweak)
2679 if (h->esym.asym.sc == scCommon)
2680 h->esym.asym.sc = scBss;
2681 else if (h->esym.asym.sc == scSCommon)
2682 h->esym.asym.sc = scSBss;
2684 sec = h->root.root.u.def.section;
2685 output_section = sec->output_section;
2686 if (output_section != NULL)
2687 h->esym.asym.value = (h->root.root.u.def.value
2688 + sec->output_offset
2689 + output_section->vma);
2691 h->esym.asym.value = 0;
2695 struct mips_elf_link_hash_entry *hd = h;
2697 while (hd->root.root.type == bfd_link_hash_indirect)
2698 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2700 if (hd->needs_lazy_stub)
2702 /* Set type and value for a symbol with a function stub. */
2703 h->esym.asym.st = stProc;
2704 sec = hd->root.root.u.def.section;
2706 h->esym.asym.value = 0;
2709 output_section = sec->output_section;
2710 if (output_section != NULL)
2711 h->esym.asym.value = (hd->root.plt.offset
2712 + sec->output_offset
2713 + output_section->vma);
2715 h->esym.asym.value = 0;
2720 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2721 h->root.root.root.string,
2724 einfo->failed = TRUE;
2731 /* A comparison routine used to sort .gptab entries. */
2734 gptab_compare (const void *p1, const void *p2)
2736 const Elf32_gptab *a1 = p1;
2737 const Elf32_gptab *a2 = p2;
2739 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2742 /* Functions to manage the got entry hash table. */
2744 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2747 static INLINE hashval_t
2748 mips_elf_hash_bfd_vma (bfd_vma addr)
2751 return addr + (addr >> 32);
2758 mips_elf_got_entry_hash (const void *entry_)
2760 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2762 return (entry->symndx
2763 + (((entry->tls_type & GOT_TLS_TYPE) == GOT_TLS_LDM) << 18)
2764 + ((entry->tls_type & GOT_TLS_TYPE) == GOT_TLS_LDM ? 0
2765 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2766 : entry->symndx >= 0 ? (entry->abfd->id
2767 + mips_elf_hash_bfd_vma (entry->d.addend))
2768 : entry->d.h->root.root.root.hash));
2772 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2774 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2775 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2777 return (e1->symndx == e2->symndx
2778 && (e1->tls_type & GOT_TLS_TYPE) == (e2->tls_type & GOT_TLS_TYPE)
2779 && ((e1->tls_type & GOT_TLS_TYPE) == GOT_TLS_LDM ? TRUE
2780 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2781 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2782 && e1->d.addend == e2->d.addend)
2783 : e2->abfd && e1->d.h == e2->d.h));
2787 mips_got_page_entry_hash (const void *entry_)
2789 const struct mips_got_page_entry *entry;
2791 entry = (const struct mips_got_page_entry *) entry_;
2792 return entry->abfd->id + entry->symndx;
2796 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2798 const struct mips_got_page_entry *entry1, *entry2;
2800 entry1 = (const struct mips_got_page_entry *) entry1_;
2801 entry2 = (const struct mips_got_page_entry *) entry2_;
2802 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2805 /* Create and return a new mips_got_info structure. */
2807 static struct mips_got_info *
2808 mips_elf_create_got_info (bfd *abfd)
2810 struct mips_got_info *g;
2812 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
2816 g->tls_ldm_offset = MINUS_ONE;
2817 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
2818 mips_elf_got_entry_eq, NULL);
2819 if (g->got_entries == NULL)
2822 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
2823 mips_got_page_entry_eq, NULL);
2824 if (g->got_page_entries == NULL)
2830 /* Return the GOT info for input bfd ABFD, trying to create a new one if
2831 CREATE_P and if ABFD doesn't already have a GOT. */
2833 static struct mips_got_info *
2834 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
2836 struct mips_elf_obj_tdata *tdata;
2838 if (!is_mips_elf (abfd))
2841 tdata = mips_elf_tdata (abfd);
2842 if (!tdata->got && create_p)
2843 tdata->got = mips_elf_create_got_info (abfd);
2847 /* Record that ABFD should use output GOT G. */
2850 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
2852 struct mips_elf_obj_tdata *tdata;
2854 BFD_ASSERT (is_mips_elf (abfd));
2855 tdata = mips_elf_tdata (abfd);
2858 /* The GOT structure itself and the hash table entries are
2859 allocated to a bfd, but the hash tables aren't. */
2860 htab_delete (tdata->got->got_entries);
2861 htab_delete (tdata->got->got_page_entries);
2866 /* Return the dynamic relocation section. If it doesn't exist, try to
2867 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2868 if creation fails. */
2871 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2877 dname = MIPS_ELF_REL_DYN_NAME (info);
2878 dynobj = elf_hash_table (info)->dynobj;
2879 sreloc = bfd_get_linker_section (dynobj, dname);
2880 if (sreloc == NULL && create_p)
2882 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
2887 | SEC_LINKER_CREATED
2890 || ! bfd_set_section_alignment (dynobj, sreloc,
2891 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2897 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
2900 mips_elf_reloc_tls_type (unsigned int r_type)
2902 if (tls_gd_reloc_p (r_type))
2905 if (tls_ldm_reloc_p (r_type))
2908 if (tls_gottprel_reloc_p (r_type))
2914 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
2917 mips_tls_got_entries (unsigned int type)
2934 /* Count the number of relocations needed for a TLS GOT entry, with
2935 access types from TLS_TYPE, and symbol H (or a local symbol if H
2939 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2940 struct elf_link_hash_entry *h)
2943 bfd_boolean need_relocs = FALSE;
2944 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2946 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2947 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2950 if ((info->shared || indx != 0)
2952 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2953 || h->root.type != bfd_link_hash_undefweak))
2959 switch (tls_type & GOT_TLS_TYPE)
2962 return indx != 0 ? 2 : 1;
2968 return info->shared ? 1 : 0;
2975 /* Add the number of GOT entries and TLS relocations required by ENTRY
2979 mips_elf_count_got_entry (struct bfd_link_info *info,
2980 struct mips_got_info *g,
2981 struct mips_got_entry *entry)
2983 unsigned char tls_type;
2985 tls_type = entry->tls_type & GOT_TLS_TYPE;
2988 g->tls_gotno += mips_tls_got_entries (tls_type);
2989 g->relocs += mips_tls_got_relocs (info, tls_type,
2991 ? &entry->d.h->root : NULL);
2993 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
2994 g->local_gotno += 1;
2996 g->global_gotno += 1;
2999 /* A htab_traverse callback. Count the number of GOT entries and
3000 TLS relocations required for the GOT entry in *ENTRYP. DATA points
3001 to a mips_elf_traverse_got_arg structure. */
3004 mips_elf_count_got_entries (void **entryp, void *data)
3006 struct mips_got_entry *entry;
3007 struct mips_elf_traverse_got_arg *arg;
3009 entry = (struct mips_got_entry *) *entryp;
3010 arg = (struct mips_elf_traverse_got_arg *) data;
3011 mips_elf_count_got_entry (arg->info, arg->g, entry);
3016 /* A htab_traverse callback. If *SLOT describes a GOT entry for a local
3017 symbol, count the number of GOT entries and TLS relocations that it
3018 requires. DATA points to a mips_elf_traverse_got_arg structure. */
3021 mips_elf_count_local_got_entries (void **entryp, void *data)
3023 struct mips_got_entry *entry;
3024 struct mips_elf_traverse_got_arg *arg;
3026 entry = (struct mips_got_entry *) *entryp;
3027 arg = (struct mips_elf_traverse_got_arg *) data;
3028 if (entry->abfd != NULL && entry->symndx != -1)
3030 if ((entry->tls_type & GOT_TLS_TYPE) == GOT_TLS_LDM)
3032 if (arg->g->tls_ldm_offset == MINUS_TWO)
3034 arg->g->tls_ldm_offset = MINUS_TWO;
3036 mips_elf_count_got_entry (arg->info, arg->g, entry);
3042 /* Count the number of TLS GOT entries and relocationss required for the
3043 global (or forced-local) symbol in ARG1. */
3046 mips_elf_count_global_tls_entries (void *entry, void *data)
3048 struct mips_elf_link_hash_entry *hm;
3049 struct mips_elf_traverse_got_arg *arg;
3051 hm = (struct mips_elf_link_hash_entry *) entry;
3052 if (hm->root.root.type == bfd_link_hash_indirect
3053 || hm->root.root.type == bfd_link_hash_warning)
3056 arg = (struct mips_elf_traverse_got_arg *) data;
3057 if (hm->tls_gd_type)
3059 arg->g->tls_gotno += 2;
3060 arg->g->relocs += mips_tls_got_relocs (arg->info, hm->tls_gd_type,
3063 if (hm->tls_ie_type)
3065 arg->g->tls_gotno += 1;
3066 arg->g->relocs += mips_tls_got_relocs (arg->info, hm->tls_ie_type,
3073 /* Output a simple dynamic relocation into SRELOC. */
3076 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3078 unsigned long reloc_index,
3083 Elf_Internal_Rela rel[3];
3085 memset (rel, 0, sizeof (rel));
3087 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3088 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3090 if (ABI_64_P (output_bfd))
3092 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3093 (output_bfd, &rel[0],
3095 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3098 bfd_elf32_swap_reloc_out
3099 (output_bfd, &rel[0],
3101 + reloc_index * sizeof (Elf32_External_Rel)));
3104 /* Initialize a set of TLS GOT entries for one symbol. */
3107 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
3108 unsigned char *tls_type_p,
3109 struct bfd_link_info *info,
3110 struct mips_elf_link_hash_entry *h,
3113 struct mips_elf_link_hash_table *htab;
3115 asection *sreloc, *sgot;
3116 bfd_vma got_offset2;
3117 bfd_boolean need_relocs = FALSE;
3119 htab = mips_elf_hash_table (info);
3128 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3130 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3131 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3132 indx = h->root.dynindx;
3135 if (*tls_type_p & GOT_TLS_DONE)
3138 if ((info->shared || indx != 0)
3140 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3141 || h->root.type != bfd_link_hash_undefweak))
3144 /* MINUS_ONE means the symbol is not defined in this object. It may not
3145 be defined at all; assume that the value doesn't matter in that
3146 case. Otherwise complain if we would use the value. */
3147 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3148 || h->root.root.type == bfd_link_hash_undefweak);
3150 /* Emit necessary relocations. */
3151 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3153 switch (*tls_type_p & GOT_TLS_TYPE)
3156 /* General Dynamic. */
3157 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3161 mips_elf_output_dynamic_relocation
3162 (abfd, sreloc, sreloc->reloc_count++, indx,
3163 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3164 sgot->output_offset + sgot->output_section->vma + got_offset);
3167 mips_elf_output_dynamic_relocation
3168 (abfd, sreloc, sreloc->reloc_count++, indx,
3169 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3170 sgot->output_offset + sgot->output_section->vma + got_offset2);
3172 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3173 sgot->contents + got_offset2);
3177 MIPS_ELF_PUT_WORD (abfd, 1,
3178 sgot->contents + got_offset);
3179 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3180 sgot->contents + got_offset2);
3185 /* Initial Exec model. */
3189 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3190 sgot->contents + got_offset);
3192 MIPS_ELF_PUT_WORD (abfd, 0,
3193 sgot->contents + got_offset);
3195 mips_elf_output_dynamic_relocation
3196 (abfd, sreloc, sreloc->reloc_count++, indx,
3197 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3198 sgot->output_offset + sgot->output_section->vma + got_offset);
3201 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3202 sgot->contents + got_offset);
3206 /* The initial offset is zero, and the LD offsets will include the
3207 bias by DTP_OFFSET. */
3208 MIPS_ELF_PUT_WORD (abfd, 0,
3209 sgot->contents + got_offset
3210 + MIPS_ELF_GOT_SIZE (abfd));
3213 MIPS_ELF_PUT_WORD (abfd, 1,
3214 sgot->contents + got_offset);
3216 mips_elf_output_dynamic_relocation
3217 (abfd, sreloc, sreloc->reloc_count++, indx,
3218 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3219 sgot->output_offset + sgot->output_section->vma + got_offset);
3226 *tls_type_p |= GOT_TLS_DONE;
3229 /* Return the GOT index to use for a relocation against H using the
3230 TLS model in *TLS_TYPE. The GOT entries for this symbol/model
3231 combination start at GOT_INDEX into ABFD's GOT. This function
3232 initializes the GOT entries and corresponding relocations. */
3235 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
3236 struct bfd_link_info *info,
3237 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
3239 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
3243 /* Return the GOT index to use for a relocation of type R_TYPE against H
3247 mips_tls_single_got_index (bfd *abfd, int r_type, struct bfd_link_info *info,
3248 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
3250 if (tls_gottprel_reloc_p (r_type))
3251 return mips_tls_got_index (abfd, h->tls_ie_got_offset, &h->tls_ie_type,
3253 if (tls_gd_reloc_p (r_type))
3254 return mips_tls_got_index (abfd, h->tls_gd_got_offset, &h->tls_gd_type,
3259 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3260 for global symbol H. .got.plt comes before the GOT, so the offset
3261 will be negative. */
3264 mips_elf_gotplt_index (struct bfd_link_info *info,
3265 struct elf_link_hash_entry *h)
3267 bfd_vma plt_index, got_address, got_value;
3268 struct mips_elf_link_hash_table *htab;
3270 htab = mips_elf_hash_table (info);
3271 BFD_ASSERT (htab != NULL);
3273 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3275 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3276 section starts with reserved entries. */
3277 BFD_ASSERT (htab->is_vxworks);
3279 /* Calculate the index of the symbol's PLT entry. */
3280 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3282 /* Calculate the address of the associated .got.plt entry. */
3283 got_address = (htab->sgotplt->output_section->vma
3284 + htab->sgotplt->output_offset
3287 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3288 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3289 + htab->root.hgot->root.u.def.section->output_offset
3290 + htab->root.hgot->root.u.def.value);
3292 return got_address - got_value;
3295 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3296 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3297 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3298 offset can be found. */
3301 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3302 bfd_vma value, unsigned long r_symndx,
3303 struct mips_elf_link_hash_entry *h, int r_type)
3305 struct mips_elf_link_hash_table *htab;
3306 struct mips_got_entry *entry;
3308 htab = mips_elf_hash_table (info);
3309 BFD_ASSERT (htab != NULL);
3311 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3312 r_symndx, h, r_type);
3316 if (entry->tls_type)
3318 if (entry->symndx == -1 && htab->got_info->next == NULL)
3319 /* A type (3) entry in the single-GOT case. We use the symbol's
3320 hash table entry to track the index. */
3321 return mips_tls_single_got_index (abfd, r_type, info, h, value);
3323 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3327 return entry->gotidx;
3330 /* Return the GOT index of global symbol H in the primary GOT. */
3333 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3334 struct elf_link_hash_entry *h)
3336 struct mips_elf_link_hash_table *htab;
3337 long global_got_dynindx;
3338 struct mips_got_info *g;
3341 htab = mips_elf_hash_table (info);
3342 BFD_ASSERT (htab != NULL);
3344 global_got_dynindx = 0;
3345 if (htab->global_gotsym != NULL)
3346 global_got_dynindx = htab->global_gotsym->dynindx;
3348 /* Once we determine the global GOT entry with the lowest dynamic
3349 symbol table index, we must put all dynamic symbols with greater
3350 indices into the primary GOT. That makes it easy to calculate the
3352 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3353 g = mips_elf_bfd_got (obfd, FALSE);
3354 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3355 * MIPS_ELF_GOT_SIZE (obfd));
3356 BFD_ASSERT (got_index < htab->sgot->size);
3361 /* Return the GOT index for the global symbol indicated by H, which is
3362 referenced by a relocation of type R_TYPE in IBFD. */
3365 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3366 struct elf_link_hash_entry *h, int r_type)
3368 struct mips_elf_link_hash_table *htab;
3370 struct mips_got_info *g, *gg;
3372 htab = mips_elf_hash_table (info);
3373 BFD_ASSERT (htab != NULL);
3375 gg = g = htab->got_info;
3376 if (g->next && ibfd)
3378 struct mips_got_entry e, *p;
3380 BFD_ASSERT (h->dynindx >= 0);
3382 g = mips_elf_bfd_got (ibfd, FALSE);
3384 if (g->next != gg || TLS_RELOC_P (r_type))
3388 e.d.h = (struct mips_elf_link_hash_entry *)h;
3389 e.tls_type = mips_elf_reloc_tls_type (r_type);
3391 p = htab_find (g->got_entries, &e);
3393 BFD_ASSERT (p && p->gotidx > 0);
3397 bfd_vma value = MINUS_ONE;
3398 if ((h->root.type == bfd_link_hash_defined
3399 || h->root.type == bfd_link_hash_defweak)
3400 && h->root.u.def.section->output_section)
3401 value = (h->root.u.def.value
3402 + h->root.u.def.section->output_offset
3403 + h->root.u.def.section->output_section->vma);
3405 return mips_tls_got_index (obfd, p->gotidx, &p->tls_type,
3406 info, e.d.h, value);
3413 if (TLS_RELOC_P (r_type))
3415 struct mips_elf_link_hash_entry *hm
3416 = (struct mips_elf_link_hash_entry *) h;
3417 bfd_vma value = MINUS_ONE;
3419 if ((h->root.type == bfd_link_hash_defined
3420 || h->root.type == bfd_link_hash_defweak)
3421 && h->root.u.def.section->output_section)
3422 value = (h->root.u.def.value
3423 + h->root.u.def.section->output_offset
3424 + h->root.u.def.section->output_section->vma);
3426 got_index = mips_tls_single_got_index (obfd, r_type, info, hm, value);
3429 got_index = mips_elf_primary_global_got_index (obfd, info, h);
3430 BFD_ASSERT (got_index < htab->sgot->size);
3435 /* Find a GOT page entry that points to within 32KB of VALUE. These
3436 entries are supposed to be placed at small offsets in the GOT, i.e.,
3437 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3438 entry could be created. If OFFSETP is nonnull, use it to return the
3439 offset of the GOT entry from VALUE. */
3442 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3443 bfd_vma value, bfd_vma *offsetp)
3445 bfd_vma page, got_index;
3446 struct mips_got_entry *entry;
3448 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3449 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3450 NULL, R_MIPS_GOT_PAGE);
3455 got_index = entry->gotidx;
3458 *offsetp = value - entry->d.address;
3463 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3464 EXTERNAL is true if the relocation was originally against a global
3465 symbol that binds locally. */
3468 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3469 bfd_vma value, bfd_boolean external)
3471 struct mips_got_entry *entry;
3473 /* GOT16 relocations against local symbols are followed by a LO16
3474 relocation; those against global symbols are not. Thus if the
3475 symbol was originally local, the GOT16 relocation should load the
3476 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3478 value = mips_elf_high (value) << 16;
3480 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3481 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3482 same in all cases. */
3483 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3484 NULL, R_MIPS_GOT16);
3486 return entry->gotidx;
3491 /* Returns the offset for the entry at the INDEXth position
3495 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3496 bfd *input_bfd, bfd_vma got_index)
3498 struct mips_elf_link_hash_table *htab;
3502 htab = mips_elf_hash_table (info);
3503 BFD_ASSERT (htab != NULL);
3506 gp = _bfd_get_gp_value (output_bfd)
3507 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3509 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3512 /* Create and return a local GOT entry for VALUE, which was calculated
3513 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3514 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3517 static struct mips_got_entry *
3518 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3519 bfd *ibfd, bfd_vma value,
3520 unsigned long r_symndx,
3521 struct mips_elf_link_hash_entry *h,
3524 struct mips_got_entry entry, **loc;
3525 struct mips_got_info *g;
3526 struct mips_elf_link_hash_table *htab;
3528 htab = mips_elf_hash_table (info);
3529 BFD_ASSERT (htab != NULL);
3533 entry.d.address = value;
3534 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3536 g = mips_elf_bfd_got (ibfd, FALSE);
3539 g = mips_elf_bfd_got (abfd, FALSE);
3540 BFD_ASSERT (g != NULL);
3543 /* This function shouldn't be called for symbols that live in the global
3545 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3548 struct mips_got_entry *p;
3551 if (tls_ldm_reloc_p (r_type))
3558 entry.symndx = r_symndx;
3564 p = (struct mips_got_entry *)
3565 htab_find (g->got_entries, &entry);
3571 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3576 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3578 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3583 memcpy (*loc, &entry, sizeof entry);
3585 if (g->assigned_gotno > g->local_gotno)
3587 (*loc)->gotidx = -1;
3588 /* We didn't allocate enough space in the GOT. */
3589 (*_bfd_error_handler)
3590 (_("not enough GOT space for local GOT entries"));
3591 bfd_set_error (bfd_error_bad_value);
3595 MIPS_ELF_PUT_WORD (abfd, value,
3596 (htab->sgot->contents + entry.gotidx));
3598 /* These GOT entries need a dynamic relocation on VxWorks. */
3599 if (htab->is_vxworks)
3601 Elf_Internal_Rela outrel;
3604 bfd_vma got_address;
3606 s = mips_elf_rel_dyn_section (info, FALSE);
3607 got_address = (htab->sgot->output_section->vma
3608 + htab->sgot->output_offset
3611 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3612 outrel.r_offset = got_address;
3613 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3614 outrel.r_addend = value;
3615 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3621 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3622 The number might be exact or a worst-case estimate, depending on how
3623 much information is available to elf_backend_omit_section_dynsym at
3624 the current linking stage. */
3626 static bfd_size_type
3627 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3629 bfd_size_type count;
3632 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3635 const struct elf_backend_data *bed;
3637 bed = get_elf_backend_data (output_bfd);
3638 for (p = output_bfd->sections; p ; p = p->next)
3639 if ((p->flags & SEC_EXCLUDE) == 0
3640 && (p->flags & SEC_ALLOC) != 0
3641 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3647 /* Sort the dynamic symbol table so that symbols that need GOT entries
3648 appear towards the end. */
3651 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3653 struct mips_elf_link_hash_table *htab;
3654 struct mips_elf_hash_sort_data hsd;
3655 struct mips_got_info *g;
3657 if (elf_hash_table (info)->dynsymcount == 0)
3660 htab = mips_elf_hash_table (info);
3661 BFD_ASSERT (htab != NULL);
3668 hsd.max_unref_got_dynindx
3669 = hsd.min_got_dynindx
3670 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3671 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3672 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3673 elf_hash_table (info)),
3674 mips_elf_sort_hash_table_f,
3677 /* There should have been enough room in the symbol table to
3678 accommodate both the GOT and non-GOT symbols. */
3679 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3680 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3681 == elf_hash_table (info)->dynsymcount);
3682 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3683 == g->global_gotno);
3685 /* Now we know which dynamic symbol has the lowest dynamic symbol
3686 table index in the GOT. */
3687 htab->global_gotsym = hsd.low;
3692 /* If H needs a GOT entry, assign it the highest available dynamic
3693 index. Otherwise, assign it the lowest available dynamic
3697 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3699 struct mips_elf_hash_sort_data *hsd = data;
3701 /* Symbols without dynamic symbol table entries aren't interesting
3703 if (h->root.dynindx == -1)
3706 switch (h->global_got_area)
3709 h->root.dynindx = hsd->max_non_got_dynindx++;
3713 h->root.dynindx = --hsd->min_got_dynindx;
3714 hsd->low = (struct elf_link_hash_entry *) h;
3717 case GGA_RELOC_ONLY:
3718 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3719 hsd->low = (struct elf_link_hash_entry *) h;
3720 h->root.dynindx = hsd->max_unref_got_dynindx++;
3727 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3728 (which is owned by the caller and shouldn't be added to the
3729 hash table directly). */
3732 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3733 struct mips_got_entry *lookup)
3735 struct mips_elf_link_hash_table *htab;
3736 struct mips_got_entry *entry;
3737 struct mips_got_info *g;
3738 void **loc, **bfd_loc;
3740 /* Make sure there's a slot for this entry in the master GOT. */
3741 htab = mips_elf_hash_table (info);
3743 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3747 /* Populate the entry if it isn't already. */
3748 entry = (struct mips_got_entry *) *loc;
3751 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3755 lookup->gotidx = -1;
3760 /* Reuse the same GOT entry for the BFD's GOT. */
3761 g = mips_elf_bfd_got (abfd, TRUE);
3765 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3774 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3775 entry for it. FOR_CALL is true if the caller is only interested in
3776 using the GOT entry for calls. */
3779 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3780 bfd *abfd, struct bfd_link_info *info,
3781 bfd_boolean for_call, int r_type)
3783 struct mips_elf_link_hash_table *htab;
3784 struct mips_elf_link_hash_entry *hmips;
3785 struct mips_got_entry entry;
3786 unsigned char tls_type;
3788 htab = mips_elf_hash_table (info);
3789 BFD_ASSERT (htab != NULL);
3791 hmips = (struct mips_elf_link_hash_entry *) h;
3793 hmips->got_only_for_calls = FALSE;
3795 /* A global symbol in the GOT must also be in the dynamic symbol
3797 if (h->dynindx == -1)
3799 switch (ELF_ST_VISIBILITY (h->other))
3803 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3806 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3810 tls_type = mips_elf_reloc_tls_type (r_type);
3811 if (tls_type == GOT_NORMAL && hmips->global_got_area > GGA_NORMAL)
3812 hmips->global_got_area = GGA_NORMAL;
3813 else if (tls_type == GOT_TLS_IE && hmips->tls_ie_type == 0)
3814 hmips->tls_ie_type = tls_type;
3815 else if (tls_type == GOT_TLS_GD && hmips->tls_gd_type == 0)
3816 hmips->tls_gd_type = tls_type;
3820 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3821 entry.tls_type = tls_type;
3822 return mips_elf_record_got_entry (info, abfd, &entry);
3825 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3826 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3829 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3830 struct bfd_link_info *info, int r_type)
3832 struct mips_elf_link_hash_table *htab;
3833 struct mips_got_info *g;
3834 struct mips_got_entry entry;
3836 htab = mips_elf_hash_table (info);
3837 BFD_ASSERT (htab != NULL);
3840 BFD_ASSERT (g != NULL);
3843 entry.symndx = symndx;
3844 entry.d.addend = addend;
3845 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3846 return mips_elf_record_got_entry (info, abfd, &entry);
3849 /* Return the maximum number of GOT page entries required for RANGE. */
3852 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3854 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3857 /* Record that ABFD has a page relocation against symbol SYMNDX and
3858 that ADDEND is the addend for that relocation.
3860 This function creates an upper bound on the number of GOT slots
3861 required; no attempt is made to combine references to non-overridable
3862 global symbols across multiple input files. */
3865 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3866 long symndx, bfd_signed_vma addend)
3868 struct mips_elf_link_hash_table *htab;
3869 struct mips_got_info *g1, *g2;
3870 struct mips_got_page_entry lookup, *entry;
3871 struct mips_got_page_range **range_ptr, *range;
3872 bfd_vma old_pages, new_pages;
3873 void **loc, **bfd_loc;
3875 htab = mips_elf_hash_table (info);
3876 BFD_ASSERT (htab != NULL);
3878 g1 = htab->got_info;
3879 BFD_ASSERT (g1 != NULL);
3881 /* Find the mips_got_page_entry hash table entry for this symbol. */
3883 lookup.symndx = symndx;
3884 loc = htab_find_slot (g1->got_page_entries, &lookup, INSERT);
3888 /* Create a mips_got_page_entry if this is the first time we've
3890 entry = (struct mips_got_page_entry *) *loc;
3893 entry = bfd_alloc (abfd, sizeof (*entry));
3898 entry->symndx = symndx;
3899 entry->ranges = NULL;
3900 entry->num_pages = 0;
3904 /* Add the same entry to the BFD's GOT. */
3905 g2 = mips_elf_bfd_got (abfd, TRUE);
3909 bfd_loc = htab_find_slot (g2->got_page_entries, &lookup, INSERT);
3916 /* Skip over ranges whose maximum extent cannot share a page entry
3918 range_ptr = &entry->ranges;
3919 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3920 range_ptr = &(*range_ptr)->next;
3922 /* If we scanned to the end of the list, or found a range whose
3923 minimum extent cannot share a page entry with ADDEND, create
3924 a new singleton range. */
3926 if (!range || addend < range->min_addend - 0xffff)
3928 range = bfd_alloc (abfd, sizeof (*range));
3932 range->next = *range_ptr;
3933 range->min_addend = addend;
3934 range->max_addend = addend;
3943 /* Remember how many pages the old range contributed. */
3944 old_pages = mips_elf_pages_for_range (range);
3946 /* Update the ranges. */
3947 if (addend < range->min_addend)
3948 range->min_addend = addend;
3949 else if (addend > range->max_addend)
3951 if (range->next && addend >= range->next->min_addend - 0xffff)
3953 old_pages += mips_elf_pages_for_range (range->next);
3954 range->max_addend = range->next->max_addend;
3955 range->next = range->next->next;
3958 range->max_addend = addend;
3961 /* Record any change in the total estimate. */
3962 new_pages = mips_elf_pages_for_range (range);
3963 if (old_pages != new_pages)
3965 entry->num_pages += new_pages - old_pages;
3966 g1->page_gotno += new_pages - old_pages;
3967 g2->page_gotno += new_pages - old_pages;
3973 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3976 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3980 struct mips_elf_link_hash_table *htab;
3982 htab = mips_elf_hash_table (info);
3983 BFD_ASSERT (htab != NULL);
3985 s = mips_elf_rel_dyn_section (info, FALSE);
3986 BFD_ASSERT (s != NULL);
3988 if (htab->is_vxworks)
3989 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3994 /* Make room for a null element. */
3995 s->size += MIPS_ELF_REL_SIZE (abfd);
3998 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4002 /* A htab_traverse callback for GOT entries. Set boolean *DATA to true
4003 if the GOT entry is for an indirect or warning symbol. */
4006 mips_elf_check_recreate_got (void **entryp, void *data)
4008 struct mips_got_entry *entry;
4009 bfd_boolean *must_recreate;
4011 entry = (struct mips_got_entry *) *entryp;
4012 must_recreate = (bfd_boolean *) data;
4013 if (entry->abfd != NULL && entry->symndx == -1)
4015 struct mips_elf_link_hash_entry *h;
4018 if (h->root.root.type == bfd_link_hash_indirect
4019 || h->root.root.type == bfd_link_hash_warning)
4021 *must_recreate = TRUE;
4028 /* A htab_traverse callback for GOT entries. Add all entries to
4029 hash table *DATA, converting entries for indirect and warning
4030 symbols into entries for the target symbol. Set *DATA to null
4034 mips_elf_recreate_got (void **entryp, void *data)
4037 struct mips_got_entry new_entry, *entry;
4040 new_got = (htab_t *) data;
4041 entry = (struct mips_got_entry *) *entryp;
4042 if (entry->abfd != NULL
4043 && entry->symndx == -1
4044 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4045 || entry->d.h->root.root.type == bfd_link_hash_warning))
4047 struct mips_elf_link_hash_entry *h;
4054 BFD_ASSERT (h->global_got_area == GGA_NONE);
4055 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4057 while (h->root.root.type == bfd_link_hash_indirect
4058 || h->root.root.type == bfd_link_hash_warning);
4061 slot = htab_find_slot (*new_got, entry, INSERT);
4069 if (entry == &new_entry)
4071 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4084 /* If any entries in G->got_entries are for indirect or warning symbols,
4085 replace them with entries for the target symbol. */
4088 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
4090 bfd_boolean must_recreate;
4093 must_recreate = FALSE;
4094 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
4097 new_got = htab_create (htab_size (g->got_entries),
4098 mips_elf_got_entry_hash,
4099 mips_elf_got_entry_eq, NULL);
4100 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
4101 if (new_got == NULL)
4104 htab_delete (g->got_entries);
4105 g->got_entries = new_got;
4110 /* A mips_elf_link_hash_traverse callback for which DATA points
4111 to the link_info structure. Count the number of type (3) entries
4112 in the master GOT. */
4115 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4117 struct bfd_link_info *info;
4118 struct mips_elf_link_hash_table *htab;
4119 struct mips_got_info *g;
4121 info = (struct bfd_link_info *) data;
4122 htab = mips_elf_hash_table (info);
4124 if (h->global_got_area != GGA_NONE)
4126 /* Make a final decision about whether the symbol belongs in the
4127 local or global GOT. Symbols that bind locally can (and in the
4128 case of forced-local symbols, must) live in the local GOT.
4129 Those that are aren't in the dynamic symbol table must also
4130 live in the local GOT.
4132 Note that the former condition does not always imply the
4133 latter: symbols do not bind locally if they are completely
4134 undefined. We'll report undefined symbols later if appropriate. */
4135 if (h->root.dynindx == -1
4136 || (h->got_only_for_calls
4137 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4138 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
4140 /* The symbol belongs in the local GOT. We no longer need this
4141 entry if it was only used for relocations; those relocations
4142 will be against the null or section symbol instead of H. */
4143 if (h->global_got_area != GGA_RELOC_ONLY)
4145 h->global_got_area = GGA_NONE;
4147 else if (htab->is_vxworks
4148 && h->got_only_for_calls
4149 && h->root.plt.offset != MINUS_ONE)
4150 /* On VxWorks, calls can refer directly to the .got.plt entry;
4151 they don't need entries in the regular GOT. .got.plt entries
4152 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4153 h->global_got_area = GGA_NONE;
4157 if (h->global_got_area == GGA_RELOC_ONLY)
4158 g->reloc_only_gotno++;
4164 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4165 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4168 mips_elf_add_got_entry (void **entryp, void *data)
4170 struct mips_got_entry *entry;
4171 struct mips_elf_traverse_got_arg *arg;
4174 entry = (struct mips_got_entry *) *entryp;
4175 arg = (struct mips_elf_traverse_got_arg *) data;
4176 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4185 mips_elf_count_got_entry (arg->info, arg->g, entry);
4190 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4191 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4194 mips_elf_add_got_page_entry (void **entryp, void *data)
4196 struct mips_got_page_entry *entry;
4197 struct mips_elf_traverse_got_arg *arg;
4200 entry = (struct mips_got_page_entry *) *entryp;
4201 arg = (struct mips_elf_traverse_got_arg *) data;
4202 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4211 arg->g->page_gotno += entry->num_pages;
4216 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4217 this would lead to overflow, 1 if they were merged successfully,
4218 and 0 if a merge failed due to lack of memory. (These values are chosen
4219 so that nonnegative return values can be returned by a htab_traverse
4223 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4224 struct mips_got_info *to,
4225 struct mips_elf_got_per_bfd_arg *arg)
4227 struct mips_elf_traverse_got_arg tga;
4228 unsigned int estimate;
4230 /* Work out how many page entries we would need for the combined GOT. */
4231 estimate = arg->max_pages;
4232 if (estimate >= from->page_gotno + to->page_gotno)
4233 estimate = from->page_gotno + to->page_gotno;
4235 /* And conservatively estimate how many local and TLS entries
4237 estimate += from->local_gotno + to->local_gotno;
4238 estimate += from->tls_gotno + to->tls_gotno;
4240 /* If we're merging with the primary got, any TLS relocations will
4241 come after the full set of global entries. Otherwise estimate those
4242 conservatively as well. */
4243 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4244 estimate += arg->global_count;
4246 estimate += from->global_gotno + to->global_gotno;
4248 /* Bail out if the combined GOT might be too big. */
4249 if (estimate > arg->max_count)
4252 /* Transfer the bfd's got information from FROM to TO. */
4253 tga.info = arg->info;
4255 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4259 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4263 mips_elf_replace_bfd_got (abfd, to);
4267 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4268 as possible of the primary got, since it doesn't require explicit
4269 dynamic relocations, but don't use bfds that would reference global
4270 symbols out of the addressable range. Failing the primary got,
4271 attempt to merge with the current got, or finish the current got
4272 and then make make the new got current. */
4275 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4276 struct mips_elf_got_per_bfd_arg *arg)
4278 struct mips_elf_traverse_got_arg tga;
4279 unsigned int estimate;
4282 if (!mips_elf_resolve_final_got_entries (g))
4285 tga.info = arg->info;
4287 htab_traverse (g->got_entries, mips_elf_count_got_entries, &tga);
4289 /* Work out the number of page, local and TLS entries. */
4290 estimate = arg->max_pages;
4291 if (estimate > g->page_gotno)
4292 estimate = g->page_gotno;
4293 estimate += g->local_gotno + g->tls_gotno;
4295 /* We place TLS GOT entries after both locals and globals. The globals
4296 for the primary GOT may overflow the normal GOT size limit, so be
4297 sure not to merge a GOT which requires TLS with the primary GOT in that
4298 case. This doesn't affect non-primary GOTs. */
4299 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4301 if (estimate <= arg->max_count)
4303 /* If we don't have a primary GOT, use it as
4304 a starting point for the primary GOT. */
4311 /* Try merging with the primary GOT. */
4312 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4317 /* If we can merge with the last-created got, do it. */
4320 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4325 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4326 fits; if it turns out that it doesn't, we'll get relocation
4327 overflows anyway. */
4328 g->next = arg->current;
4334 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4335 to GOTIDX, duplicating the entry if it has already been assigned
4336 an index in a different GOT. */
4339 mips_elf_set_gotidx (void **entryp, long gotidx)
4341 struct mips_got_entry *entry;
4343 entry = (struct mips_got_entry *) *entryp;
4344 if (entry->gotidx > 0)
4346 struct mips_got_entry *new_entry;
4348 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4352 *new_entry = *entry;
4353 *entryp = new_entry;
4356 entry->gotidx = gotidx;
4360 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4361 mips_elf_traverse_got_arg in which DATA->value is the size of one
4362 GOT entry. Set DATA->g to null on failure. */
4365 mips_elf_initialize_tls_index (void **entryp, void *data)
4367 struct mips_got_entry *entry;
4368 struct mips_elf_traverse_got_arg *arg;
4369 struct mips_got_info *g;
4371 unsigned char tls_type;
4373 /* We're only interested in TLS symbols. */
4374 entry = (struct mips_got_entry *) *entryp;
4375 tls_type = (entry->tls_type & GOT_TLS_TYPE);
4379 arg = (struct mips_elf_traverse_got_arg *) data;
4381 next_index = arg->value * g->tls_assigned_gotno;
4383 if (entry->symndx == -1 && g->next == NULL)
4385 /* A type (3) got entry in the single-GOT case. We use the symbol's
4386 hash table entry to track its index. */
4387 if (tls_type == GOT_TLS_IE)
4389 if (entry->d.h->tls_ie_type & GOT_TLS_OFFSET_DONE)
4391 entry->d.h->tls_ie_type |= GOT_TLS_OFFSET_DONE;
4392 entry->d.h->tls_ie_got_offset = next_index;
4396 BFD_ASSERT (tls_type == GOT_TLS_GD);
4397 if (entry->d.h->tls_gd_type & GOT_TLS_OFFSET_DONE)
4399 entry->d.h->tls_gd_type |= GOT_TLS_OFFSET_DONE;
4400 entry->d.h->tls_gd_got_offset = next_index;
4405 if (tls_type == GOT_TLS_LDM)
4407 /* There are separate mips_got_entry objects for each input bfd
4408 that requires an LDM entry. Make sure that all LDM entries in
4409 a GOT resolve to the same index. */
4410 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4412 entry->gotidx = g->tls_ldm_offset;
4415 g->tls_ldm_offset = next_index;
4417 if (!mips_elf_set_gotidx (entryp, next_index))
4424 /* Account for the entries we've just allocated. */
4425 g->tls_assigned_gotno += mips_tls_got_entries (tls_type);
4429 /* A htab_traverse callback for GOT entries, where DATA points to a
4430 mips_elf_traverse_got_arg. Set the global_got_area of each global
4431 symbol to DATA->value. */
4434 mips_elf_set_global_got_area (void **entryp, void *data)
4436 struct mips_got_entry *entry;
4437 struct mips_elf_traverse_got_arg *arg;
4439 entry = (struct mips_got_entry *) *entryp;
4440 arg = (struct mips_elf_traverse_got_arg *) data;
4441 if (entry->abfd != NULL
4442 && entry->symndx == -1
4443 && entry->d.h->global_got_area != GGA_NONE)
4444 entry->d.h->global_got_area = arg->value;
4448 /* A htab_traverse callback for secondary GOT entries, where DATA points
4449 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4450 and record the number of relocations they require. DATA->value is
4451 the size of one GOT entry. Set DATA->g to null on failure. */
4454 mips_elf_set_global_gotidx (void **entryp, void *data)
4456 struct mips_got_entry *entry;
4457 struct mips_elf_traverse_got_arg *arg;
4459 entry = (struct mips_got_entry *) *entryp;
4460 arg = (struct mips_elf_traverse_got_arg *) data;
4461 if (entry->abfd != NULL
4462 && entry->symndx == -1
4463 && entry->d.h->global_got_area != GGA_NONE)
4465 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_gotno))
4470 arg->g->assigned_gotno += 1;
4472 if (arg->info->shared
4473 || (elf_hash_table (arg->info)->dynamic_sections_created
4474 && entry->d.h->root.def_dynamic
4475 && !entry->d.h->root.def_regular))
4476 arg->g->relocs += 1;
4482 /* A htab_traverse callback for GOT entries for which DATA is the
4483 bfd_link_info. Forbid any global symbols from having traditional
4484 lazy-binding stubs. */
4487 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4489 struct bfd_link_info *info;
4490 struct mips_elf_link_hash_table *htab;
4491 struct mips_got_entry *entry;
4493 entry = (struct mips_got_entry *) *entryp;
4494 info = (struct bfd_link_info *) data;
4495 htab = mips_elf_hash_table (info);
4496 BFD_ASSERT (htab != NULL);
4498 if (entry->abfd != NULL
4499 && entry->symndx == -1
4500 && entry->d.h->needs_lazy_stub)
4502 entry->d.h->needs_lazy_stub = FALSE;
4503 htab->lazy_stub_count--;
4509 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4512 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4517 g = mips_elf_bfd_got (ibfd, FALSE);
4521 BFD_ASSERT (g->next);
4525 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4526 * MIPS_ELF_GOT_SIZE (abfd);
4529 /* Turn a single GOT that is too big for 16-bit addressing into
4530 a sequence of GOTs, each one 16-bit addressable. */
4533 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4534 asection *got, bfd_size_type pages)
4536 struct mips_elf_link_hash_table *htab;
4537 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4538 struct mips_elf_traverse_got_arg tga;
4539 struct mips_got_info *g, *gg;
4540 unsigned int assign, needed_relocs;
4543 dynobj = elf_hash_table (info)->dynobj;
4544 htab = mips_elf_hash_table (info);
4545 BFD_ASSERT (htab != NULL);
4549 got_per_bfd_arg.obfd = abfd;
4550 got_per_bfd_arg.info = info;
4551 got_per_bfd_arg.current = NULL;
4552 got_per_bfd_arg.primary = NULL;
4553 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4554 / MIPS_ELF_GOT_SIZE (abfd))
4555 - htab->reserved_gotno);
4556 got_per_bfd_arg.max_pages = pages;
4557 /* The number of globals that will be included in the primary GOT.
4558 See the calls to mips_elf_set_global_got_area below for more
4560 got_per_bfd_arg.global_count = g->global_gotno;
4562 /* Try to merge the GOTs of input bfds together, as long as they
4563 don't seem to exceed the maximum GOT size, choosing one of them
4564 to be the primary GOT. */
4565 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4567 gg = mips_elf_bfd_got (ibfd, FALSE);
4568 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4572 /* If we do not find any suitable primary GOT, create an empty one. */
4573 if (got_per_bfd_arg.primary == NULL)
4574 g->next = mips_elf_create_got_info (abfd);
4576 g->next = got_per_bfd_arg.primary;
4577 g->next->next = got_per_bfd_arg.current;
4579 /* GG is now the master GOT, and G is the primary GOT. */
4583 /* Map the output bfd to the primary got. That's what we're going
4584 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4585 didn't mark in check_relocs, and we want a quick way to find it.
4586 We can't just use gg->next because we're going to reverse the
4588 mips_elf_replace_bfd_got (abfd, g);
4590 /* Every symbol that is referenced in a dynamic relocation must be
4591 present in the primary GOT, so arrange for them to appear after
4592 those that are actually referenced. */
4593 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4594 g->global_gotno = gg->global_gotno;
4597 tga.value = GGA_RELOC_ONLY;
4598 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4599 tga.value = GGA_NORMAL;
4600 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4602 /* Now go through the GOTs assigning them offset ranges.
4603 [assigned_gotno, local_gotno[ will be set to the range of local
4604 entries in each GOT. We can then compute the end of a GOT by
4605 adding local_gotno to global_gotno. We reverse the list and make
4606 it circular since then we'll be able to quickly compute the
4607 beginning of a GOT, by computing the end of its predecessor. To
4608 avoid special cases for the primary GOT, while still preserving
4609 assertions that are valid for both single- and multi-got links,
4610 we arrange for the main got struct to have the right number of
4611 global entries, but set its local_gotno such that the initial
4612 offset of the primary GOT is zero. Remember that the primary GOT
4613 will become the last item in the circular linked list, so it
4614 points back to the master GOT. */
4615 gg->local_gotno = -g->global_gotno;
4616 gg->global_gotno = g->global_gotno;
4623 struct mips_got_info *gn;
4625 assign += htab->reserved_gotno;
4626 g->assigned_gotno = assign;
4627 g->local_gotno += assign;
4628 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4629 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4631 /* Take g out of the direct list, and push it onto the reversed
4632 list that gg points to. g->next is guaranteed to be nonnull after
4633 this operation, as required by mips_elf_initialize_tls_index. */
4638 /* Set up any TLS entries. We always place the TLS entries after
4639 all non-TLS entries. */
4640 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4642 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4643 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4646 BFD_ASSERT (g->tls_assigned_gotno == assign);
4648 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4651 /* Forbid global symbols in every non-primary GOT from having
4652 lazy-binding stubs. */
4654 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4658 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4661 for (g = gg->next; g && g->next != gg; g = g->next)
4663 unsigned int save_assign;
4665 /* Assign offsets to global GOT entries and count how many
4666 relocations they need. */
4667 save_assign = g->assigned_gotno;
4668 g->assigned_gotno = g->local_gotno;
4670 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4672 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4675 BFD_ASSERT (g->assigned_gotno == g->local_gotno + g->global_gotno);
4676 g->assigned_gotno = save_assign;
4680 g->relocs += g->local_gotno - g->assigned_gotno;
4681 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4682 + g->next->global_gotno
4683 + g->next->tls_gotno
4684 + htab->reserved_gotno);
4686 needed_relocs += g->relocs;
4688 needed_relocs += g->relocs;
4691 mips_elf_allocate_dynamic_relocations (dynobj, info,
4698 /* Returns the first relocation of type r_type found, beginning with
4699 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4701 static const Elf_Internal_Rela *
4702 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4703 const Elf_Internal_Rela *relocation,
4704 const Elf_Internal_Rela *relend)
4706 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4708 while (relocation < relend)
4710 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4711 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4717 /* We didn't find it. */
4721 /* Return whether an input relocation is against a local symbol. */
4724 mips_elf_local_relocation_p (bfd *input_bfd,
4725 const Elf_Internal_Rela *relocation,
4726 asection **local_sections)
4728 unsigned long r_symndx;
4729 Elf_Internal_Shdr *symtab_hdr;
4732 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4733 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4734 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4736 if (r_symndx < extsymoff)
4738 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4744 /* Sign-extend VALUE, which has the indicated number of BITS. */
4747 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4749 if (value & ((bfd_vma) 1 << (bits - 1)))
4750 /* VALUE is negative. */
4751 value |= ((bfd_vma) - 1) << bits;
4756 /* Return non-zero if the indicated VALUE has overflowed the maximum
4757 range expressible by a signed number with the indicated number of
4761 mips_elf_overflow_p (bfd_vma value, int bits)
4763 bfd_signed_vma svalue = (bfd_signed_vma) value;
4765 if (svalue > (1 << (bits - 1)) - 1)
4766 /* The value is too big. */
4768 else if (svalue < -(1 << (bits - 1)))
4769 /* The value is too small. */
4776 /* Calculate the %high function. */
4779 mips_elf_high (bfd_vma value)
4781 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4784 /* Calculate the %higher function. */
4787 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4790 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4797 /* Calculate the %highest function. */
4800 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4803 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4810 /* Create the .compact_rel section. */
4813 mips_elf_create_compact_rel_section
4814 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4817 register asection *s;
4819 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
4821 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4824 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
4826 || ! bfd_set_section_alignment (abfd, s,
4827 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4830 s->size = sizeof (Elf32_External_compact_rel);
4836 /* Create the .got section to hold the global offset table. */
4839 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4842 register asection *s;
4843 struct elf_link_hash_entry *h;
4844 struct bfd_link_hash_entry *bh;
4845 struct mips_elf_link_hash_table *htab;
4847 htab = mips_elf_hash_table (info);
4848 BFD_ASSERT (htab != NULL);
4850 /* This function may be called more than once. */
4854 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4855 | SEC_LINKER_CREATED);
4857 /* We have to use an alignment of 2**4 here because this is hardcoded
4858 in the function stub generation and in the linker script. */
4859 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4861 || ! bfd_set_section_alignment (abfd, s, 4))
4865 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4866 linker script because we don't want to define the symbol if we
4867 are not creating a global offset table. */
4869 if (! (_bfd_generic_link_add_one_symbol
4870 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4871 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4874 h = (struct elf_link_hash_entry *) bh;
4877 h->type = STT_OBJECT;
4878 elf_hash_table (info)->hgot = h;
4881 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4884 htab->got_info = mips_elf_create_got_info (abfd);
4885 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4886 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4888 /* We also need a .got.plt section when generating PLTs. */
4889 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
4890 SEC_ALLOC | SEC_LOAD
4893 | SEC_LINKER_CREATED);
4901 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4902 __GOTT_INDEX__ symbols. These symbols are only special for
4903 shared objects; they are not used in executables. */
4906 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4908 return (mips_elf_hash_table (info)->is_vxworks
4910 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4911 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4914 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4915 require an la25 stub. See also mips_elf_local_pic_function_p,
4916 which determines whether the destination function ever requires a
4920 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
4921 bfd_boolean target_is_16_bit_code_p)
4923 /* We specifically ignore branches and jumps from EF_PIC objects,
4924 where the onus is on the compiler or programmer to perform any
4925 necessary initialization of $25. Sometimes such initialization
4926 is unnecessary; for example, -mno-shared functions do not use
4927 the incoming value of $25, and may therefore be called directly. */
4928 if (PIC_OBJECT_P (input_bfd))
4935 case R_MICROMIPS_26_S1:
4936 case R_MICROMIPS_PC7_S1:
4937 case R_MICROMIPS_PC10_S1:
4938 case R_MICROMIPS_PC16_S1:
4939 case R_MICROMIPS_PC23_S2:
4943 return !target_is_16_bit_code_p;
4950 /* Calculate the value produced by the RELOCATION (which comes from
4951 the INPUT_BFD). The ADDEND is the addend to use for this
4952 RELOCATION; RELOCATION->R_ADDEND is ignored.
4954 The result of the relocation calculation is stored in VALUEP.
4955 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4956 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
4958 This function returns bfd_reloc_continue if the caller need take no
4959 further action regarding this relocation, bfd_reloc_notsupported if
4960 something goes dramatically wrong, bfd_reloc_overflow if an
4961 overflow occurs, and bfd_reloc_ok to indicate success. */
4963 static bfd_reloc_status_type
4964 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4965 asection *input_section,
4966 struct bfd_link_info *info,
4967 const Elf_Internal_Rela *relocation,
4968 bfd_vma addend, reloc_howto_type *howto,
4969 Elf_Internal_Sym *local_syms,
4970 asection **local_sections, bfd_vma *valuep,
4972 bfd_boolean *cross_mode_jump_p,
4973 bfd_boolean save_addend)
4975 /* The eventual value we will return. */
4977 /* The address of the symbol against which the relocation is
4980 /* The final GP value to be used for the relocatable, executable, or
4981 shared object file being produced. */
4983 /* The place (section offset or address) of the storage unit being
4986 /* The value of GP used to create the relocatable object. */
4988 /* The offset into the global offset table at which the address of
4989 the relocation entry symbol, adjusted by the addend, resides
4990 during execution. */
4991 bfd_vma g = MINUS_ONE;
4992 /* The section in which the symbol referenced by the relocation is
4994 asection *sec = NULL;
4995 struct mips_elf_link_hash_entry *h = NULL;
4996 /* TRUE if the symbol referred to by this relocation is a local
4998 bfd_boolean local_p, was_local_p;
4999 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5000 bfd_boolean gp_disp_p = FALSE;
5001 /* TRUE if the symbol referred to by this relocation is
5002 "__gnu_local_gp". */
5003 bfd_boolean gnu_local_gp_p = FALSE;
5004 Elf_Internal_Shdr *symtab_hdr;
5006 unsigned long r_symndx;
5008 /* TRUE if overflow occurred during the calculation of the
5009 relocation value. */
5010 bfd_boolean overflowed_p;
5011 /* TRUE if this relocation refers to a MIPS16 function. */
5012 bfd_boolean target_is_16_bit_code_p = FALSE;
5013 bfd_boolean target_is_micromips_code_p = FALSE;
5014 struct mips_elf_link_hash_table *htab;
5017 dynobj = elf_hash_table (info)->dynobj;
5018 htab = mips_elf_hash_table (info);
5019 BFD_ASSERT (htab != NULL);
5021 /* Parse the relocation. */
5022 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5023 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5024 p = (input_section->output_section->vma
5025 + input_section->output_offset
5026 + relocation->r_offset);
5028 /* Assume that there will be no overflow. */
5029 overflowed_p = FALSE;
5031 /* Figure out whether or not the symbol is local, and get the offset
5032 used in the array of hash table entries. */
5033 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5034 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5036 was_local_p = local_p;
5037 if (! elf_bad_symtab (input_bfd))
5038 extsymoff = symtab_hdr->sh_info;
5041 /* The symbol table does not follow the rule that local symbols
5042 must come before globals. */
5046 /* Figure out the value of the symbol. */
5049 Elf_Internal_Sym *sym;
5051 sym = local_syms + r_symndx;
5052 sec = local_sections[r_symndx];
5054 symbol = sec->output_section->vma + sec->output_offset;
5055 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5056 || (sec->flags & SEC_MERGE))
5057 symbol += sym->st_value;
5058 if ((sec->flags & SEC_MERGE)
5059 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5061 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5063 addend += sec->output_section->vma + sec->output_offset;
5066 /* MIPS16/microMIPS text labels should be treated as odd. */
5067 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5070 /* Record the name of this symbol, for our caller. */
5071 *namep = bfd_elf_string_from_elf_section (input_bfd,
5072 symtab_hdr->sh_link,
5075 *namep = bfd_section_name (input_bfd, sec);
5077 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5078 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5082 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5084 /* For global symbols we look up the symbol in the hash-table. */
5085 h = ((struct mips_elf_link_hash_entry *)
5086 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5087 /* Find the real hash-table entry for this symbol. */
5088 while (h->root.root.type == bfd_link_hash_indirect
5089 || h->root.root.type == bfd_link_hash_warning)
5090 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5092 /* Record the name of this symbol, for our caller. */
5093 *namep = h->root.root.root.string;
5095 /* See if this is the special _gp_disp symbol. Note that such a
5096 symbol must always be a global symbol. */
5097 if (strcmp (*namep, "_gp_disp") == 0
5098 && ! NEWABI_P (input_bfd))
5100 /* Relocations against _gp_disp are permitted only with
5101 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5102 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5103 return bfd_reloc_notsupported;
5107 /* See if this is the special _gp symbol. Note that such a
5108 symbol must always be a global symbol. */
5109 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5110 gnu_local_gp_p = TRUE;
5113 /* If this symbol is defined, calculate its address. Note that
5114 _gp_disp is a magic symbol, always implicitly defined by the
5115 linker, so it's inappropriate to check to see whether or not
5117 else if ((h->root.root.type == bfd_link_hash_defined
5118 || h->root.root.type == bfd_link_hash_defweak)
5119 && h->root.root.u.def.section)
5121 sec = h->root.root.u.def.section;
5122 if (sec->output_section)
5123 symbol = (h->root.root.u.def.value
5124 + sec->output_section->vma
5125 + sec->output_offset);
5127 symbol = h->root.root.u.def.value;
5129 else if (h->root.root.type == bfd_link_hash_undefweak)
5130 /* We allow relocations against undefined weak symbols, giving
5131 it the value zero, so that you can undefined weak functions
5132 and check to see if they exist by looking at their
5135 else if (info->unresolved_syms_in_objects == RM_IGNORE
5136 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5138 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5139 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5141 /* If this is a dynamic link, we should have created a
5142 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5143 in in _bfd_mips_elf_create_dynamic_sections.
5144 Otherwise, we should define the symbol with a value of 0.
5145 FIXME: It should probably get into the symbol table
5147 BFD_ASSERT (! info->shared);
5148 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5151 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5153 /* This is an optional symbol - an Irix specific extension to the
5154 ELF spec. Ignore it for now.
5155 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5156 than simply ignoring them, but we do not handle this for now.
5157 For information see the "64-bit ELF Object File Specification"
5158 which is available from here:
5159 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5162 else if ((*info->callbacks->undefined_symbol)
5163 (info, h->root.root.root.string, input_bfd,
5164 input_section, relocation->r_offset,
5165 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5166 || ELF_ST_VISIBILITY (h->root.other)))
5168 return bfd_reloc_undefined;
5172 return bfd_reloc_notsupported;
5175 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5176 /* If the output section is the PLT section,
5177 then the target is not microMIPS. */
5178 target_is_micromips_code_p = (htab->splt != sec
5179 && ELF_ST_IS_MICROMIPS (h->root.other));
5182 /* If this is a reference to a 16-bit function with a stub, we need
5183 to redirect the relocation to the stub unless:
5185 (a) the relocation is for a MIPS16 JAL;
5187 (b) the relocation is for a MIPS16 PIC call, and there are no
5188 non-MIPS16 uses of the GOT slot; or
5190 (c) the section allows direct references to MIPS16 functions. */
5191 if (r_type != R_MIPS16_26
5192 && !info->relocatable
5194 && h->fn_stub != NULL
5195 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5197 && elf_tdata (input_bfd)->local_stubs != NULL
5198 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5199 && !section_allows_mips16_refs_p (input_section))
5201 /* This is a 32- or 64-bit call to a 16-bit function. We should
5202 have already noticed that we were going to need the
5206 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5211 BFD_ASSERT (h->need_fn_stub);
5214 /* If a LA25 header for the stub itself exists, point to the
5215 prepended LUI/ADDIU sequence. */
5216 sec = h->la25_stub->stub_section;
5217 value = h->la25_stub->offset;
5226 symbol = sec->output_section->vma + sec->output_offset + value;
5227 /* The target is 16-bit, but the stub isn't. */
5228 target_is_16_bit_code_p = FALSE;
5230 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5231 need to redirect the call to the stub. Note that we specifically
5232 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5233 use an indirect stub instead. */
5234 else if (r_type == R_MIPS16_26 && !info->relocatable
5235 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5237 && elf_tdata (input_bfd)->local_call_stubs != NULL
5238 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5239 && !target_is_16_bit_code_p)
5242 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5245 /* If both call_stub and call_fp_stub are defined, we can figure
5246 out which one to use by checking which one appears in the input
5248 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5253 for (o = input_bfd->sections; o != NULL; o = o->next)
5255 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5257 sec = h->call_fp_stub;
5264 else if (h->call_stub != NULL)
5267 sec = h->call_fp_stub;
5270 BFD_ASSERT (sec->size > 0);
5271 symbol = sec->output_section->vma + sec->output_offset;
5273 /* If this is a direct call to a PIC function, redirect to the
5275 else if (h != NULL && h->la25_stub
5276 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5277 target_is_16_bit_code_p))
5278 symbol = (h->la25_stub->stub_section->output_section->vma
5279 + h->la25_stub->stub_section->output_offset
5280 + h->la25_stub->offset);
5282 /* Make sure MIPS16 and microMIPS are not used together. */
5283 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5284 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5286 (*_bfd_error_handler)
5287 (_("MIPS16 and microMIPS functions cannot call each other"));
5288 return bfd_reloc_notsupported;
5291 /* Calls from 16-bit code to 32-bit code and vice versa require the
5292 mode change. However, we can ignore calls to undefined weak symbols,
5293 which should never be executed at runtime. This exception is important
5294 because the assembly writer may have "known" that any definition of the
5295 symbol would be 16-bit code, and that direct jumps were therefore
5297 *cross_mode_jump_p = (!info->relocatable
5298 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5299 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5300 || (r_type == R_MICROMIPS_26_S1
5301 && !target_is_micromips_code_p)
5302 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5303 && (target_is_16_bit_code_p
5304 || target_is_micromips_code_p))));
5306 local_p = (h == NULL
5307 || (h->got_only_for_calls
5308 ? SYMBOL_CALLS_LOCAL (info, &h->root)
5309 : SYMBOL_REFERENCES_LOCAL (info, &h->root)));
5311 gp0 = _bfd_get_gp_value (input_bfd);
5312 gp = _bfd_get_gp_value (abfd);
5314 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5319 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5320 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5321 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5322 if (got_page_reloc_p (r_type) && !local_p)
5324 r_type = (micromips_reloc_p (r_type)
5325 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5329 /* If we haven't already determined the GOT offset, and we're going
5330 to need it, get it now. */
5333 case R_MIPS16_CALL16:
5334 case R_MIPS16_GOT16:
5337 case R_MIPS_GOT_DISP:
5338 case R_MIPS_GOT_HI16:
5339 case R_MIPS_CALL_HI16:
5340 case R_MIPS_GOT_LO16:
5341 case R_MIPS_CALL_LO16:
5342 case R_MICROMIPS_CALL16:
5343 case R_MICROMIPS_GOT16:
5344 case R_MICROMIPS_GOT_DISP:
5345 case R_MICROMIPS_GOT_HI16:
5346 case R_MICROMIPS_CALL_HI16:
5347 case R_MICROMIPS_GOT_LO16:
5348 case R_MICROMIPS_CALL_LO16:
5350 case R_MIPS_TLS_GOTTPREL:
5351 case R_MIPS_TLS_LDM:
5352 case R_MIPS16_TLS_GD:
5353 case R_MIPS16_TLS_GOTTPREL:
5354 case R_MIPS16_TLS_LDM:
5355 case R_MICROMIPS_TLS_GD:
5356 case R_MICROMIPS_TLS_GOTTPREL:
5357 case R_MICROMIPS_TLS_LDM:
5358 /* Find the index into the GOT where this value is located. */
5359 if (tls_ldm_reloc_p (r_type))
5361 g = mips_elf_local_got_index (abfd, input_bfd, info,
5362 0, 0, NULL, r_type);
5364 return bfd_reloc_outofrange;
5368 /* On VxWorks, CALL relocations should refer to the .got.plt
5369 entry, which is initialized to point at the PLT stub. */
5370 if (htab->is_vxworks
5371 && (call_hi16_reloc_p (r_type)
5372 || call_lo16_reloc_p (r_type)
5373 || call16_reloc_p (r_type)))
5375 BFD_ASSERT (addend == 0);
5376 BFD_ASSERT (h->root.needs_plt);
5377 g = mips_elf_gotplt_index (info, &h->root);
5381 BFD_ASSERT (addend == 0);
5382 g = mips_elf_global_got_index (abfd, info, input_bfd,
5384 if (!TLS_RELOC_P (r_type)
5385 && !elf_hash_table (info)->dynamic_sections_created)
5386 /* This is a static link. We must initialize the GOT entry. */
5387 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5390 else if (!htab->is_vxworks
5391 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5392 /* The calculation below does not involve "g". */
5396 g = mips_elf_local_got_index (abfd, input_bfd, info,
5397 symbol + addend, r_symndx, h, r_type);
5399 return bfd_reloc_outofrange;
5402 /* Convert GOT indices to actual offsets. */
5403 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5407 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5408 symbols are resolved by the loader. Add them to .rela.dyn. */
5409 if (h != NULL && is_gott_symbol (info, &h->root))
5411 Elf_Internal_Rela outrel;
5415 s = mips_elf_rel_dyn_section (info, FALSE);
5416 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5418 outrel.r_offset = (input_section->output_section->vma
5419 + input_section->output_offset
5420 + relocation->r_offset);
5421 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5422 outrel.r_addend = addend;
5423 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5425 /* If we've written this relocation for a readonly section,
5426 we need to set DF_TEXTREL again, so that we do not delete the
5428 if (MIPS_ELF_READONLY_SECTION (input_section))
5429 info->flags |= DF_TEXTREL;
5432 return bfd_reloc_ok;
5435 /* Figure out what kind of relocation is being performed. */
5439 return bfd_reloc_continue;
5442 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5443 overflowed_p = mips_elf_overflow_p (value, 16);
5450 || (htab->root.dynamic_sections_created
5452 && h->root.def_dynamic
5453 && !h->root.def_regular
5454 && !h->has_static_relocs))
5455 && r_symndx != STN_UNDEF
5457 || h->root.root.type != bfd_link_hash_undefweak
5458 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5459 && (input_section->flags & SEC_ALLOC) != 0)
5461 /* If we're creating a shared library, then we can't know
5462 where the symbol will end up. So, we create a relocation
5463 record in the output, and leave the job up to the dynamic
5464 linker. We must do the same for executable references to
5465 shared library symbols, unless we've decided to use copy
5466 relocs or PLTs instead. */
5468 if (!mips_elf_create_dynamic_relocation (abfd,
5476 return bfd_reloc_undefined;
5480 if (r_type != R_MIPS_REL32)
5481 value = symbol + addend;
5485 value &= howto->dst_mask;
5489 value = symbol + addend - p;
5490 value &= howto->dst_mask;
5494 /* The calculation for R_MIPS16_26 is just the same as for an
5495 R_MIPS_26. It's only the storage of the relocated field into
5496 the output file that's different. That's handled in
5497 mips_elf_perform_relocation. So, we just fall through to the
5498 R_MIPS_26 case here. */
5500 case R_MICROMIPS_26_S1:
5504 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5505 the correct ISA mode selector and bit 1 must be 0. */
5506 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5507 return bfd_reloc_outofrange;
5509 /* Shift is 2, unusually, for microMIPS JALX. */
5510 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5513 value = addend | ((p + 4) & (0xfc000000 << shift));
5515 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5516 value = (value + symbol) >> shift;
5517 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5518 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5519 value &= howto->dst_mask;
5523 case R_MIPS_TLS_DTPREL_HI16:
5524 case R_MIPS16_TLS_DTPREL_HI16:
5525 case R_MICROMIPS_TLS_DTPREL_HI16:
5526 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5530 case R_MIPS_TLS_DTPREL_LO16:
5531 case R_MIPS_TLS_DTPREL32:
5532 case R_MIPS_TLS_DTPREL64:
5533 case R_MIPS16_TLS_DTPREL_LO16:
5534 case R_MICROMIPS_TLS_DTPREL_LO16:
5535 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5538 case R_MIPS_TLS_TPREL_HI16:
5539 case R_MIPS16_TLS_TPREL_HI16:
5540 case R_MICROMIPS_TLS_TPREL_HI16:
5541 value = (mips_elf_high (addend + symbol - tprel_base (info))
5545 case R_MIPS_TLS_TPREL_LO16:
5546 case R_MIPS_TLS_TPREL32:
5547 case R_MIPS_TLS_TPREL64:
5548 case R_MIPS16_TLS_TPREL_LO16:
5549 case R_MICROMIPS_TLS_TPREL_LO16:
5550 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5555 case R_MICROMIPS_HI16:
5558 value = mips_elf_high (addend + symbol);
5559 value &= howto->dst_mask;
5563 /* For MIPS16 ABI code we generate this sequence
5564 0: li $v0,%hi(_gp_disp)
5565 4: addiupc $v1,%lo(_gp_disp)
5569 So the offsets of hi and lo relocs are the same, but the
5570 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5571 ADDIUPC clears the low two bits of the instruction address,
5572 so the base is ($t9 + 4) & ~3. */
5573 if (r_type == R_MIPS16_HI16)
5574 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5575 /* The microMIPS .cpload sequence uses the same assembly
5576 instructions as the traditional psABI version, but the
5577 incoming $t9 has the low bit set. */
5578 else if (r_type == R_MICROMIPS_HI16)
5579 value = mips_elf_high (addend + gp - p - 1);
5581 value = mips_elf_high (addend + gp - p);
5582 overflowed_p = mips_elf_overflow_p (value, 16);
5588 case R_MICROMIPS_LO16:
5589 case R_MICROMIPS_HI0_LO16:
5591 value = (symbol + addend) & howto->dst_mask;
5594 /* See the comment for R_MIPS16_HI16 above for the reason
5595 for this conditional. */
5596 if (r_type == R_MIPS16_LO16)
5597 value = addend + gp - (p & ~(bfd_vma) 0x3);
5598 else if (r_type == R_MICROMIPS_LO16
5599 || r_type == R_MICROMIPS_HI0_LO16)
5600 value = addend + gp - p + 3;
5602 value = addend + gp - p + 4;
5603 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5604 for overflow. But, on, say, IRIX5, relocations against
5605 _gp_disp are normally generated from the .cpload
5606 pseudo-op. It generates code that normally looks like
5609 lui $gp,%hi(_gp_disp)
5610 addiu $gp,$gp,%lo(_gp_disp)
5613 Here $t9 holds the address of the function being called,
5614 as required by the MIPS ELF ABI. The R_MIPS_LO16
5615 relocation can easily overflow in this situation, but the
5616 R_MIPS_HI16 relocation will handle the overflow.
5617 Therefore, we consider this a bug in the MIPS ABI, and do
5618 not check for overflow here. */
5622 case R_MIPS_LITERAL:
5623 case R_MICROMIPS_LITERAL:
5624 /* Because we don't merge literal sections, we can handle this
5625 just like R_MIPS_GPREL16. In the long run, we should merge
5626 shared literals, and then we will need to additional work
5631 case R_MIPS16_GPREL:
5632 /* The R_MIPS16_GPREL performs the same calculation as
5633 R_MIPS_GPREL16, but stores the relocated bits in a different
5634 order. We don't need to do anything special here; the
5635 differences are handled in mips_elf_perform_relocation. */
5636 case R_MIPS_GPREL16:
5637 case R_MICROMIPS_GPREL7_S2:
5638 case R_MICROMIPS_GPREL16:
5639 /* Only sign-extend the addend if it was extracted from the
5640 instruction. If the addend was separate, leave it alone,
5641 otherwise we may lose significant bits. */
5642 if (howto->partial_inplace)
5643 addend = _bfd_mips_elf_sign_extend (addend, 16);
5644 value = symbol + addend - gp;
5645 /* If the symbol was local, any earlier relocatable links will
5646 have adjusted its addend with the gp offset, so compensate
5647 for that now. Don't do it for symbols forced local in this
5648 link, though, since they won't have had the gp offset applied
5652 overflowed_p = mips_elf_overflow_p (value, 16);
5655 case R_MIPS16_GOT16:
5656 case R_MIPS16_CALL16:
5659 case R_MICROMIPS_GOT16:
5660 case R_MICROMIPS_CALL16:
5661 /* VxWorks does not have separate local and global semantics for
5662 R_MIPS*_GOT16; every relocation evaluates to "G". */
5663 if (!htab->is_vxworks && local_p)
5665 value = mips_elf_got16_entry (abfd, input_bfd, info,
5666 symbol + addend, !was_local_p);
5667 if (value == MINUS_ONE)
5668 return bfd_reloc_outofrange;
5670 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5671 overflowed_p = mips_elf_overflow_p (value, 16);
5678 case R_MIPS_TLS_GOTTPREL:
5679 case R_MIPS_TLS_LDM:
5680 case R_MIPS_GOT_DISP:
5681 case R_MIPS16_TLS_GD:
5682 case R_MIPS16_TLS_GOTTPREL:
5683 case R_MIPS16_TLS_LDM:
5684 case R_MICROMIPS_TLS_GD:
5685 case R_MICROMIPS_TLS_GOTTPREL:
5686 case R_MICROMIPS_TLS_LDM:
5687 case R_MICROMIPS_GOT_DISP:
5689 overflowed_p = mips_elf_overflow_p (value, 16);
5692 case R_MIPS_GPREL32:
5693 value = (addend + symbol + gp0 - gp);
5695 value &= howto->dst_mask;
5699 case R_MIPS_GNU_REL16_S2:
5700 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5701 overflowed_p = mips_elf_overflow_p (value, 18);
5702 value >>= howto->rightshift;
5703 value &= howto->dst_mask;
5706 case R_MICROMIPS_PC7_S1:
5707 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5708 overflowed_p = mips_elf_overflow_p (value, 8);
5709 value >>= howto->rightshift;
5710 value &= howto->dst_mask;
5713 case R_MICROMIPS_PC10_S1:
5714 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5715 overflowed_p = mips_elf_overflow_p (value, 11);
5716 value >>= howto->rightshift;
5717 value &= howto->dst_mask;
5720 case R_MICROMIPS_PC16_S1:
5721 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5722 overflowed_p = mips_elf_overflow_p (value, 17);
5723 value >>= howto->rightshift;
5724 value &= howto->dst_mask;
5727 case R_MICROMIPS_PC23_S2:
5728 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5729 overflowed_p = mips_elf_overflow_p (value, 25);
5730 value >>= howto->rightshift;
5731 value &= howto->dst_mask;
5734 case R_MIPS_GOT_HI16:
5735 case R_MIPS_CALL_HI16:
5736 case R_MICROMIPS_GOT_HI16:
5737 case R_MICROMIPS_CALL_HI16:
5738 /* We're allowed to handle these two relocations identically.
5739 The dynamic linker is allowed to handle the CALL relocations
5740 differently by creating a lazy evaluation stub. */
5742 value = mips_elf_high (value);
5743 value &= howto->dst_mask;
5746 case R_MIPS_GOT_LO16:
5747 case R_MIPS_CALL_LO16:
5748 case R_MICROMIPS_GOT_LO16:
5749 case R_MICROMIPS_CALL_LO16:
5750 value = g & howto->dst_mask;
5753 case R_MIPS_GOT_PAGE:
5754 case R_MICROMIPS_GOT_PAGE:
5755 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5756 if (value == MINUS_ONE)
5757 return bfd_reloc_outofrange;
5758 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5759 overflowed_p = mips_elf_overflow_p (value, 16);
5762 case R_MIPS_GOT_OFST:
5763 case R_MICROMIPS_GOT_OFST:
5765 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5768 overflowed_p = mips_elf_overflow_p (value, 16);
5772 case R_MICROMIPS_SUB:
5773 value = symbol - addend;
5774 value &= howto->dst_mask;
5778 case R_MICROMIPS_HIGHER:
5779 value = mips_elf_higher (addend + symbol);
5780 value &= howto->dst_mask;
5783 case R_MIPS_HIGHEST:
5784 case R_MICROMIPS_HIGHEST:
5785 value = mips_elf_highest (addend + symbol);
5786 value &= howto->dst_mask;
5789 case R_MIPS_SCN_DISP:
5790 case R_MICROMIPS_SCN_DISP:
5791 value = symbol + addend - sec->output_offset;
5792 value &= howto->dst_mask;
5796 case R_MICROMIPS_JALR:
5797 /* This relocation is only a hint. In some cases, we optimize
5798 it into a bal instruction. But we don't try to optimize
5799 when the symbol does not resolve locally. */
5800 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5801 return bfd_reloc_continue;
5802 value = symbol + addend;
5806 case R_MIPS_GNU_VTINHERIT:
5807 case R_MIPS_GNU_VTENTRY:
5808 /* We don't do anything with these at present. */
5809 return bfd_reloc_continue;
5812 /* An unrecognized relocation type. */
5813 return bfd_reloc_notsupported;
5816 /* Store the VALUE for our caller. */
5818 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5821 /* Obtain the field relocated by RELOCATION. */
5824 mips_elf_obtain_contents (reloc_howto_type *howto,
5825 const Elf_Internal_Rela *relocation,
5826 bfd *input_bfd, bfd_byte *contents)
5829 bfd_byte *location = contents + relocation->r_offset;
5831 /* Obtain the bytes. */
5832 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5837 /* It has been determined that the result of the RELOCATION is the
5838 VALUE. Use HOWTO to place VALUE into the output file at the
5839 appropriate position. The SECTION is the section to which the
5841 CROSS_MODE_JUMP_P is true if the relocation field
5842 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5844 Returns FALSE if anything goes wrong. */
5847 mips_elf_perform_relocation (struct bfd_link_info *info,
5848 reloc_howto_type *howto,
5849 const Elf_Internal_Rela *relocation,
5850 bfd_vma value, bfd *input_bfd,
5851 asection *input_section, bfd_byte *contents,
5852 bfd_boolean cross_mode_jump_p)
5856 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5858 /* Figure out where the relocation is occurring. */
5859 location = contents + relocation->r_offset;
5861 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5863 /* Obtain the current value. */
5864 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5866 /* Clear the field we are setting. */
5867 x &= ~howto->dst_mask;
5869 /* Set the field. */
5870 x |= (value & howto->dst_mask);
5872 /* If required, turn JAL into JALX. */
5873 if (cross_mode_jump_p && jal_reloc_p (r_type))
5876 bfd_vma opcode = x >> 26;
5877 bfd_vma jalx_opcode;
5879 /* Check to see if the opcode is already JAL or JALX. */
5880 if (r_type == R_MIPS16_26)
5882 ok = ((opcode == 0x6) || (opcode == 0x7));
5885 else if (r_type == R_MICROMIPS_26_S1)
5887 ok = ((opcode == 0x3d) || (opcode == 0x3c));
5892 ok = ((opcode == 0x3) || (opcode == 0x1d));
5896 /* If the opcode is not JAL or JALX, there's a problem. We cannot
5897 convert J or JALS to JALX. */
5900 (*_bfd_error_handler)
5901 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
5904 (unsigned long) relocation->r_offset);
5905 bfd_set_error (bfd_error_bad_value);
5909 /* Make this the JALX opcode. */
5910 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5913 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5915 if (!info->relocatable
5916 && !cross_mode_jump_p
5917 && ((JAL_TO_BAL_P (input_bfd)
5918 && r_type == R_MIPS_26
5919 && (x >> 26) == 0x3) /* jal addr */
5920 || (JALR_TO_BAL_P (input_bfd)
5921 && r_type == R_MIPS_JALR
5922 && x == 0x0320f809) /* jalr t9 */
5923 || (JR_TO_B_P (input_bfd)
5924 && r_type == R_MIPS_JALR
5925 && x == 0x03200008))) /* jr t9 */
5931 addr = (input_section->output_section->vma
5932 + input_section->output_offset
5933 + relocation->r_offset
5935 if (r_type == R_MIPS_26)
5936 dest = (value << 2) | ((addr >> 28) << 28);
5940 if (off <= 0x1ffff && off >= -0x20000)
5942 if (x == 0x03200008) /* jr t9 */
5943 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5945 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5949 /* Put the value into the output. */
5950 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5952 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
5958 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5959 is the original relocation, which is now being transformed into a
5960 dynamic relocation. The ADDENDP is adjusted if necessary; the
5961 caller should store the result in place of the original addend. */
5964 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5965 struct bfd_link_info *info,
5966 const Elf_Internal_Rela *rel,
5967 struct mips_elf_link_hash_entry *h,
5968 asection *sec, bfd_vma symbol,
5969 bfd_vma *addendp, asection *input_section)
5971 Elf_Internal_Rela outrel[3];
5976 bfd_boolean defined_p;
5977 struct mips_elf_link_hash_table *htab;
5979 htab = mips_elf_hash_table (info);
5980 BFD_ASSERT (htab != NULL);
5982 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5983 dynobj = elf_hash_table (info)->dynobj;
5984 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5985 BFD_ASSERT (sreloc != NULL);
5986 BFD_ASSERT (sreloc->contents != NULL);
5987 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5990 outrel[0].r_offset =
5991 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5992 if (ABI_64_P (output_bfd))
5994 outrel[1].r_offset =
5995 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5996 outrel[2].r_offset =
5997 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6000 if (outrel[0].r_offset == MINUS_ONE)
6001 /* The relocation field has been deleted. */
6004 if (outrel[0].r_offset == MINUS_TWO)
6006 /* The relocation field has been converted into a relative value of
6007 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6008 the field to be fully relocated, so add in the symbol's value. */
6013 /* We must now calculate the dynamic symbol table index to use
6014 in the relocation. */
6015 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6017 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6018 indx = h->root.dynindx;
6019 if (SGI_COMPAT (output_bfd))
6020 defined_p = h->root.def_regular;
6022 /* ??? glibc's ld.so just adds the final GOT entry to the
6023 relocation field. It therefore treats relocs against
6024 defined symbols in the same way as relocs against
6025 undefined symbols. */
6030 if (sec != NULL && bfd_is_abs_section (sec))
6032 else if (sec == NULL || sec->owner == NULL)
6034 bfd_set_error (bfd_error_bad_value);
6039 indx = elf_section_data (sec->output_section)->dynindx;
6042 asection *osec = htab->root.text_index_section;
6043 indx = elf_section_data (osec)->dynindx;
6049 /* Instead of generating a relocation using the section
6050 symbol, we may as well make it a fully relative
6051 relocation. We want to avoid generating relocations to
6052 local symbols because we used to generate them
6053 incorrectly, without adding the original symbol value,
6054 which is mandated by the ABI for section symbols. In
6055 order to give dynamic loaders and applications time to
6056 phase out the incorrect use, we refrain from emitting
6057 section-relative relocations. It's not like they're
6058 useful, after all. This should be a bit more efficient
6060 /* ??? Although this behavior is compatible with glibc's ld.so,
6061 the ABI says that relocations against STN_UNDEF should have
6062 a symbol value of 0. Irix rld honors this, so relocations
6063 against STN_UNDEF have no effect. */
6064 if (!SGI_COMPAT (output_bfd))
6069 /* If the relocation was previously an absolute relocation and
6070 this symbol will not be referred to by the relocation, we must
6071 adjust it by the value we give it in the dynamic symbol table.
6072 Otherwise leave the job up to the dynamic linker. */
6073 if (defined_p && r_type != R_MIPS_REL32)
6076 if (htab->is_vxworks)
6077 /* VxWorks uses non-relative relocations for this. */
6078 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6080 /* The relocation is always an REL32 relocation because we don't
6081 know where the shared library will wind up at load-time. */
6082 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6085 /* For strict adherence to the ABI specification, we should
6086 generate a R_MIPS_64 relocation record by itself before the
6087 _REL32/_64 record as well, such that the addend is read in as
6088 a 64-bit value (REL32 is a 32-bit relocation, after all).
6089 However, since none of the existing ELF64 MIPS dynamic
6090 loaders seems to care, we don't waste space with these
6091 artificial relocations. If this turns out to not be true,
6092 mips_elf_allocate_dynamic_relocation() should be tweaked so
6093 as to make room for a pair of dynamic relocations per
6094 invocation if ABI_64_P, and here we should generate an
6095 additional relocation record with R_MIPS_64 by itself for a
6096 NULL symbol before this relocation record. */
6097 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6098 ABI_64_P (output_bfd)
6101 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6103 /* Adjust the output offset of the relocation to reference the
6104 correct location in the output file. */
6105 outrel[0].r_offset += (input_section->output_section->vma
6106 + input_section->output_offset);
6107 outrel[1].r_offset += (input_section->output_section->vma
6108 + input_section->output_offset);
6109 outrel[2].r_offset += (input_section->output_section->vma
6110 + input_section->output_offset);
6112 /* Put the relocation back out. We have to use the special
6113 relocation outputter in the 64-bit case since the 64-bit
6114 relocation format is non-standard. */
6115 if (ABI_64_P (output_bfd))
6117 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6118 (output_bfd, &outrel[0],
6120 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6122 else if (htab->is_vxworks)
6124 /* VxWorks uses RELA rather than REL dynamic relocations. */
6125 outrel[0].r_addend = *addendp;
6126 bfd_elf32_swap_reloca_out
6127 (output_bfd, &outrel[0],
6129 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6132 bfd_elf32_swap_reloc_out
6133 (output_bfd, &outrel[0],
6134 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6136 /* We've now added another relocation. */
6137 ++sreloc->reloc_count;
6139 /* Make sure the output section is writable. The dynamic linker
6140 will be writing to it. */
6141 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6144 /* On IRIX5, make an entry of compact relocation info. */
6145 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6147 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6152 Elf32_crinfo cptrel;
6154 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6155 cptrel.vaddr = (rel->r_offset
6156 + input_section->output_section->vma
6157 + input_section->output_offset);
6158 if (r_type == R_MIPS_REL32)
6159 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6161 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6162 mips_elf_set_cr_dist2to (cptrel, 0);
6163 cptrel.konst = *addendp;
6165 cr = (scpt->contents
6166 + sizeof (Elf32_External_compact_rel));
6167 mips_elf_set_cr_relvaddr (cptrel, 0);
6168 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6169 ((Elf32_External_crinfo *) cr
6170 + scpt->reloc_count));
6171 ++scpt->reloc_count;
6175 /* If we've written this relocation for a readonly section,
6176 we need to set DF_TEXTREL again, so that we do not delete the
6178 if (MIPS_ELF_READONLY_SECTION (input_section))
6179 info->flags |= DF_TEXTREL;
6184 /* Return the MACH for a MIPS e_flags value. */
6187 _bfd_elf_mips_mach (flagword flags)
6189 switch (flags & EF_MIPS_MACH)
6191 case E_MIPS_MACH_3900:
6192 return bfd_mach_mips3900;
6194 case E_MIPS_MACH_4010:
6195 return bfd_mach_mips4010;
6197 case E_MIPS_MACH_4100:
6198 return bfd_mach_mips4100;
6200 case E_MIPS_MACH_4111:
6201 return bfd_mach_mips4111;
6203 case E_MIPS_MACH_4120:
6204 return bfd_mach_mips4120;
6206 case E_MIPS_MACH_4650:
6207 return bfd_mach_mips4650;
6209 case E_MIPS_MACH_5400:
6210 return bfd_mach_mips5400;
6212 case E_MIPS_MACH_5500:
6213 return bfd_mach_mips5500;
6215 case E_MIPS_MACH_5900:
6216 return bfd_mach_mips5900;
6218 case E_MIPS_MACH_9000:
6219 return bfd_mach_mips9000;
6221 case E_MIPS_MACH_SB1:
6222 return bfd_mach_mips_sb1;
6224 case E_MIPS_MACH_LS2E:
6225 return bfd_mach_mips_loongson_2e;
6227 case E_MIPS_MACH_LS2F:
6228 return bfd_mach_mips_loongson_2f;
6230 case E_MIPS_MACH_LS3A:
6231 return bfd_mach_mips_loongson_3a;
6233 case E_MIPS_MACH_OCTEON2:
6234 return bfd_mach_mips_octeon2;
6236 case E_MIPS_MACH_OCTEON:
6237 return bfd_mach_mips_octeon;
6239 case E_MIPS_MACH_XLR:
6240 return bfd_mach_mips_xlr;
6243 switch (flags & EF_MIPS_ARCH)
6247 return bfd_mach_mips3000;
6250 return bfd_mach_mips6000;
6253 return bfd_mach_mips4000;
6256 return bfd_mach_mips8000;
6259 return bfd_mach_mips5;
6261 case E_MIPS_ARCH_32:
6262 return bfd_mach_mipsisa32;
6264 case E_MIPS_ARCH_64:
6265 return bfd_mach_mipsisa64;
6267 case E_MIPS_ARCH_32R2:
6268 return bfd_mach_mipsisa32r2;
6270 case E_MIPS_ARCH_64R2:
6271 return bfd_mach_mipsisa64r2;
6278 /* Return printable name for ABI. */
6280 static INLINE char *
6281 elf_mips_abi_name (bfd *abfd)
6285 flags = elf_elfheader (abfd)->e_flags;
6286 switch (flags & EF_MIPS_ABI)
6289 if (ABI_N32_P (abfd))
6291 else if (ABI_64_P (abfd))
6295 case E_MIPS_ABI_O32:
6297 case E_MIPS_ABI_O64:
6299 case E_MIPS_ABI_EABI32:
6301 case E_MIPS_ABI_EABI64:
6304 return "unknown abi";
6308 /* MIPS ELF uses two common sections. One is the usual one, and the
6309 other is for small objects. All the small objects are kept
6310 together, and then referenced via the gp pointer, which yields
6311 faster assembler code. This is what we use for the small common
6312 section. This approach is copied from ecoff.c. */
6313 static asection mips_elf_scom_section;
6314 static asymbol mips_elf_scom_symbol;
6315 static asymbol *mips_elf_scom_symbol_ptr;
6317 /* MIPS ELF also uses an acommon section, which represents an
6318 allocated common symbol which may be overridden by a
6319 definition in a shared library. */
6320 static asection mips_elf_acom_section;
6321 static asymbol mips_elf_acom_symbol;
6322 static asymbol *mips_elf_acom_symbol_ptr;
6324 /* This is used for both the 32-bit and the 64-bit ABI. */
6327 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6329 elf_symbol_type *elfsym;
6331 /* Handle the special MIPS section numbers that a symbol may use. */
6332 elfsym = (elf_symbol_type *) asym;
6333 switch (elfsym->internal_elf_sym.st_shndx)
6335 case SHN_MIPS_ACOMMON:
6336 /* This section is used in a dynamically linked executable file.
6337 It is an allocated common section. The dynamic linker can
6338 either resolve these symbols to something in a shared
6339 library, or it can just leave them here. For our purposes,
6340 we can consider these symbols to be in a new section. */
6341 if (mips_elf_acom_section.name == NULL)
6343 /* Initialize the acommon section. */
6344 mips_elf_acom_section.name = ".acommon";
6345 mips_elf_acom_section.flags = SEC_ALLOC;
6346 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6347 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6348 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6349 mips_elf_acom_symbol.name = ".acommon";
6350 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6351 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6352 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6354 asym->section = &mips_elf_acom_section;
6358 /* Common symbols less than the GP size are automatically
6359 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6360 if (asym->value > elf_gp_size (abfd)
6361 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6362 || IRIX_COMPAT (abfd) == ict_irix6)
6365 case SHN_MIPS_SCOMMON:
6366 if (mips_elf_scom_section.name == NULL)
6368 /* Initialize the small common section. */
6369 mips_elf_scom_section.name = ".scommon";
6370 mips_elf_scom_section.flags = SEC_IS_COMMON;
6371 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6372 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6373 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6374 mips_elf_scom_symbol.name = ".scommon";
6375 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6376 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6377 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6379 asym->section = &mips_elf_scom_section;
6380 asym->value = elfsym->internal_elf_sym.st_size;
6383 case SHN_MIPS_SUNDEFINED:
6384 asym->section = bfd_und_section_ptr;
6389 asection *section = bfd_get_section_by_name (abfd, ".text");
6391 if (section != NULL)
6393 asym->section = section;
6394 /* MIPS_TEXT is a bit special, the address is not an offset
6395 to the base of the .text section. So substract the section
6396 base address to make it an offset. */
6397 asym->value -= section->vma;
6404 asection *section = bfd_get_section_by_name (abfd, ".data");
6406 if (section != NULL)
6408 asym->section = section;
6409 /* MIPS_DATA is a bit special, the address is not an offset
6410 to the base of the .data section. So substract the section
6411 base address to make it an offset. */
6412 asym->value -= section->vma;
6418 /* If this is an odd-valued function symbol, assume it's a MIPS16
6419 or microMIPS one. */
6420 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6421 && (asym->value & 1) != 0)
6424 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
6425 elfsym->internal_elf_sym.st_other
6426 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6428 elfsym->internal_elf_sym.st_other
6429 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6433 /* Implement elf_backend_eh_frame_address_size. This differs from
6434 the default in the way it handles EABI64.
6436 EABI64 was originally specified as an LP64 ABI, and that is what
6437 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6438 historically accepted the combination of -mabi=eabi and -mlong32,
6439 and this ILP32 variation has become semi-official over time.
6440 Both forms use elf32 and have pointer-sized FDE addresses.
6442 If an EABI object was generated by GCC 4.0 or above, it will have
6443 an empty .gcc_compiled_longXX section, where XX is the size of longs
6444 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6445 have no special marking to distinguish them from LP64 objects.
6447 We don't want users of the official LP64 ABI to be punished for the
6448 existence of the ILP32 variant, but at the same time, we don't want
6449 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6450 We therefore take the following approach:
6452 - If ABFD contains a .gcc_compiled_longXX section, use it to
6453 determine the pointer size.
6455 - Otherwise check the type of the first relocation. Assume that
6456 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6460 The second check is enough to detect LP64 objects generated by pre-4.0
6461 compilers because, in the kind of output generated by those compilers,
6462 the first relocation will be associated with either a CIE personality
6463 routine or an FDE start address. Furthermore, the compilers never
6464 used a special (non-pointer) encoding for this ABI.
6466 Checking the relocation type should also be safe because there is no
6467 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6471 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6473 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6475 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6477 bfd_boolean long32_p, long64_p;
6479 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6480 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6481 if (long32_p && long64_p)
6488 if (sec->reloc_count > 0
6489 && elf_section_data (sec)->relocs != NULL
6490 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6499 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6500 relocations against two unnamed section symbols to resolve to the
6501 same address. For example, if we have code like:
6503 lw $4,%got_disp(.data)($gp)
6504 lw $25,%got_disp(.text)($gp)
6507 then the linker will resolve both relocations to .data and the program
6508 will jump there rather than to .text.
6510 We can work around this problem by giving names to local section symbols.
6511 This is also what the MIPSpro tools do. */
6514 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6516 return SGI_COMPAT (abfd);
6519 /* Work over a section just before writing it out. This routine is
6520 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6521 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6525 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6527 if (hdr->sh_type == SHT_MIPS_REGINFO
6528 && hdr->sh_size > 0)
6532 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6533 BFD_ASSERT (hdr->contents == NULL);
6536 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6539 H_PUT_32 (abfd, elf_gp (abfd), buf);
6540 if (bfd_bwrite (buf, 4, abfd) != 4)
6544 if (hdr->sh_type == SHT_MIPS_OPTIONS
6545 && hdr->bfd_section != NULL
6546 && mips_elf_section_data (hdr->bfd_section) != NULL
6547 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6549 bfd_byte *contents, *l, *lend;
6551 /* We stored the section contents in the tdata field in the
6552 set_section_contents routine. We save the section contents
6553 so that we don't have to read them again.
6554 At this point we know that elf_gp is set, so we can look
6555 through the section contents to see if there is an
6556 ODK_REGINFO structure. */
6558 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6560 lend = contents + hdr->sh_size;
6561 while (l + sizeof (Elf_External_Options) <= lend)
6563 Elf_Internal_Options intopt;
6565 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6567 if (intopt.size < sizeof (Elf_External_Options))
6569 (*_bfd_error_handler)
6570 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6571 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6574 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6581 + sizeof (Elf_External_Options)
6582 + (sizeof (Elf64_External_RegInfo) - 8)),
6585 H_PUT_64 (abfd, elf_gp (abfd), buf);
6586 if (bfd_bwrite (buf, 8, abfd) != 8)
6589 else if (intopt.kind == ODK_REGINFO)
6596 + sizeof (Elf_External_Options)
6597 + (sizeof (Elf32_External_RegInfo) - 4)),
6600 H_PUT_32 (abfd, elf_gp (abfd), buf);
6601 if (bfd_bwrite (buf, 4, abfd) != 4)
6608 if (hdr->bfd_section != NULL)
6610 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6612 /* .sbss is not handled specially here because the GNU/Linux
6613 prelinker can convert .sbss from NOBITS to PROGBITS and
6614 changing it back to NOBITS breaks the binary. The entry in
6615 _bfd_mips_elf_special_sections will ensure the correct flags
6616 are set on .sbss if BFD creates it without reading it from an
6617 input file, and without special handling here the flags set
6618 on it in an input file will be followed. */
6619 if (strcmp (name, ".sdata") == 0
6620 || strcmp (name, ".lit8") == 0
6621 || strcmp (name, ".lit4") == 0)
6623 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6624 hdr->sh_type = SHT_PROGBITS;
6626 else if (strcmp (name, ".srdata") == 0)
6628 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6629 hdr->sh_type = SHT_PROGBITS;
6631 else if (strcmp (name, ".compact_rel") == 0)
6634 hdr->sh_type = SHT_PROGBITS;
6636 else if (strcmp (name, ".rtproc") == 0)
6638 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6640 unsigned int adjust;
6642 adjust = hdr->sh_size % hdr->sh_addralign;
6644 hdr->sh_size += hdr->sh_addralign - adjust;
6652 /* Handle a MIPS specific section when reading an object file. This
6653 is called when elfcode.h finds a section with an unknown type.
6654 This routine supports both the 32-bit and 64-bit ELF ABI.
6656 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6660 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6661 Elf_Internal_Shdr *hdr,
6667 /* There ought to be a place to keep ELF backend specific flags, but
6668 at the moment there isn't one. We just keep track of the
6669 sections by their name, instead. Fortunately, the ABI gives
6670 suggested names for all the MIPS specific sections, so we will
6671 probably get away with this. */
6672 switch (hdr->sh_type)
6674 case SHT_MIPS_LIBLIST:
6675 if (strcmp (name, ".liblist") != 0)
6679 if (strcmp (name, ".msym") != 0)
6682 case SHT_MIPS_CONFLICT:
6683 if (strcmp (name, ".conflict") != 0)
6686 case SHT_MIPS_GPTAB:
6687 if (! CONST_STRNEQ (name, ".gptab."))
6690 case SHT_MIPS_UCODE:
6691 if (strcmp (name, ".ucode") != 0)
6694 case SHT_MIPS_DEBUG:
6695 if (strcmp (name, ".mdebug") != 0)
6697 flags = SEC_DEBUGGING;
6699 case SHT_MIPS_REGINFO:
6700 if (strcmp (name, ".reginfo") != 0
6701 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6703 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6705 case SHT_MIPS_IFACE:
6706 if (strcmp (name, ".MIPS.interfaces") != 0)
6709 case SHT_MIPS_CONTENT:
6710 if (! CONST_STRNEQ (name, ".MIPS.content"))
6713 case SHT_MIPS_OPTIONS:
6714 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6717 case SHT_MIPS_DWARF:
6718 if (! CONST_STRNEQ (name, ".debug_")
6719 && ! CONST_STRNEQ (name, ".zdebug_"))
6722 case SHT_MIPS_SYMBOL_LIB:
6723 if (strcmp (name, ".MIPS.symlib") != 0)
6726 case SHT_MIPS_EVENTS:
6727 if (! CONST_STRNEQ (name, ".MIPS.events")
6728 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6735 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6740 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6741 (bfd_get_section_flags (abfd,
6747 /* FIXME: We should record sh_info for a .gptab section. */
6749 /* For a .reginfo section, set the gp value in the tdata information
6750 from the contents of this section. We need the gp value while
6751 processing relocs, so we just get it now. The .reginfo section
6752 is not used in the 64-bit MIPS ELF ABI. */
6753 if (hdr->sh_type == SHT_MIPS_REGINFO)
6755 Elf32_External_RegInfo ext;
6758 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6759 &ext, 0, sizeof ext))
6761 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6762 elf_gp (abfd) = s.ri_gp_value;
6765 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6766 set the gp value based on what we find. We may see both
6767 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6768 they should agree. */
6769 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6771 bfd_byte *contents, *l, *lend;
6773 contents = bfd_malloc (hdr->sh_size);
6774 if (contents == NULL)
6776 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6783 lend = contents + hdr->sh_size;
6784 while (l + sizeof (Elf_External_Options) <= lend)
6786 Elf_Internal_Options intopt;
6788 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6790 if (intopt.size < sizeof (Elf_External_Options))
6792 (*_bfd_error_handler)
6793 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6794 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6797 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6799 Elf64_Internal_RegInfo intreg;
6801 bfd_mips_elf64_swap_reginfo_in
6803 ((Elf64_External_RegInfo *)
6804 (l + sizeof (Elf_External_Options))),
6806 elf_gp (abfd) = intreg.ri_gp_value;
6808 else if (intopt.kind == ODK_REGINFO)
6810 Elf32_RegInfo intreg;
6812 bfd_mips_elf32_swap_reginfo_in
6814 ((Elf32_External_RegInfo *)
6815 (l + sizeof (Elf_External_Options))),
6817 elf_gp (abfd) = intreg.ri_gp_value;
6827 /* Set the correct type for a MIPS ELF section. We do this by the
6828 section name, which is a hack, but ought to work. This routine is
6829 used by both the 32-bit and the 64-bit ABI. */
6832 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6834 const char *name = bfd_get_section_name (abfd, sec);
6836 if (strcmp (name, ".liblist") == 0)
6838 hdr->sh_type = SHT_MIPS_LIBLIST;
6839 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6840 /* The sh_link field is set in final_write_processing. */
6842 else if (strcmp (name, ".conflict") == 0)
6843 hdr->sh_type = SHT_MIPS_CONFLICT;
6844 else if (CONST_STRNEQ (name, ".gptab."))
6846 hdr->sh_type = SHT_MIPS_GPTAB;
6847 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6848 /* The sh_info field is set in final_write_processing. */
6850 else if (strcmp (name, ".ucode") == 0)
6851 hdr->sh_type = SHT_MIPS_UCODE;
6852 else if (strcmp (name, ".mdebug") == 0)
6854 hdr->sh_type = SHT_MIPS_DEBUG;
6855 /* In a shared object on IRIX 5.3, the .mdebug section has an
6856 entsize of 0. FIXME: Does this matter? */
6857 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6858 hdr->sh_entsize = 0;
6860 hdr->sh_entsize = 1;
6862 else if (strcmp (name, ".reginfo") == 0)
6864 hdr->sh_type = SHT_MIPS_REGINFO;
6865 /* In a shared object on IRIX 5.3, the .reginfo section has an
6866 entsize of 0x18. FIXME: Does this matter? */
6867 if (SGI_COMPAT (abfd))
6869 if ((abfd->flags & DYNAMIC) != 0)
6870 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6872 hdr->sh_entsize = 1;
6875 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6877 else if (SGI_COMPAT (abfd)
6878 && (strcmp (name, ".hash") == 0
6879 || strcmp (name, ".dynamic") == 0
6880 || strcmp (name, ".dynstr") == 0))
6882 if (SGI_COMPAT (abfd))
6883 hdr->sh_entsize = 0;
6885 /* This isn't how the IRIX6 linker behaves. */
6886 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6889 else if (strcmp (name, ".got") == 0
6890 || strcmp (name, ".srdata") == 0
6891 || strcmp (name, ".sdata") == 0
6892 || strcmp (name, ".sbss") == 0
6893 || strcmp (name, ".lit4") == 0
6894 || strcmp (name, ".lit8") == 0)
6895 hdr->sh_flags |= SHF_MIPS_GPREL;
6896 else if (strcmp (name, ".MIPS.interfaces") == 0)
6898 hdr->sh_type = SHT_MIPS_IFACE;
6899 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6901 else if (CONST_STRNEQ (name, ".MIPS.content"))
6903 hdr->sh_type = SHT_MIPS_CONTENT;
6904 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6905 /* The sh_info field is set in final_write_processing. */
6907 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6909 hdr->sh_type = SHT_MIPS_OPTIONS;
6910 hdr->sh_entsize = 1;
6911 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6913 else if (CONST_STRNEQ (name, ".debug_")
6914 || CONST_STRNEQ (name, ".zdebug_"))
6916 hdr->sh_type = SHT_MIPS_DWARF;
6918 /* Irix facilities such as libexc expect a single .debug_frame
6919 per executable, the system ones have NOSTRIP set and the linker
6920 doesn't merge sections with different flags so ... */
6921 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6922 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6924 else if (strcmp (name, ".MIPS.symlib") == 0)
6926 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6927 /* The sh_link and sh_info fields are set in
6928 final_write_processing. */
6930 else if (CONST_STRNEQ (name, ".MIPS.events")
6931 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6933 hdr->sh_type = SHT_MIPS_EVENTS;
6934 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6935 /* The sh_link field is set in final_write_processing. */
6937 else if (strcmp (name, ".msym") == 0)
6939 hdr->sh_type = SHT_MIPS_MSYM;
6940 hdr->sh_flags |= SHF_ALLOC;
6941 hdr->sh_entsize = 8;
6944 /* The generic elf_fake_sections will set up REL_HDR using the default
6945 kind of relocations. We used to set up a second header for the
6946 non-default kind of relocations here, but only NewABI would use
6947 these, and the IRIX ld doesn't like resulting empty RELA sections.
6948 Thus we create those header only on demand now. */
6953 /* Given a BFD section, try to locate the corresponding ELF section
6954 index. This is used by both the 32-bit and the 64-bit ABI.
6955 Actually, it's not clear to me that the 64-bit ABI supports these,
6956 but for non-PIC objects we will certainly want support for at least
6957 the .scommon section. */
6960 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6961 asection *sec, int *retval)
6963 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6965 *retval = SHN_MIPS_SCOMMON;
6968 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6970 *retval = SHN_MIPS_ACOMMON;
6976 /* Hook called by the linker routine which adds symbols from an object
6977 file. We must handle the special MIPS section numbers here. */
6980 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6981 Elf_Internal_Sym *sym, const char **namep,
6982 flagword *flagsp ATTRIBUTE_UNUSED,
6983 asection **secp, bfd_vma *valp)
6985 if (SGI_COMPAT (abfd)
6986 && (abfd->flags & DYNAMIC) != 0
6987 && strcmp (*namep, "_rld_new_interface") == 0)
6989 /* Skip IRIX5 rld entry name. */
6994 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6995 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6996 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6997 a magic symbol resolved by the linker, we ignore this bogus definition
6998 of _gp_disp. New ABI objects do not suffer from this problem so this
6999 is not done for them. */
7001 && (sym->st_shndx == SHN_ABS)
7002 && (strcmp (*namep, "_gp_disp") == 0))
7008 switch (sym->st_shndx)
7011 /* Common symbols less than the GP size are automatically
7012 treated as SHN_MIPS_SCOMMON symbols. */
7013 if (sym->st_size > elf_gp_size (abfd)
7014 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7015 || IRIX_COMPAT (abfd) == ict_irix6)
7018 case SHN_MIPS_SCOMMON:
7019 *secp = bfd_make_section_old_way (abfd, ".scommon");
7020 (*secp)->flags |= SEC_IS_COMMON;
7021 *valp = sym->st_size;
7025 /* This section is used in a shared object. */
7026 if (elf_tdata (abfd)->elf_text_section == NULL)
7028 asymbol *elf_text_symbol;
7029 asection *elf_text_section;
7030 bfd_size_type amt = sizeof (asection);
7032 elf_text_section = bfd_zalloc (abfd, amt);
7033 if (elf_text_section == NULL)
7036 amt = sizeof (asymbol);
7037 elf_text_symbol = bfd_zalloc (abfd, amt);
7038 if (elf_text_symbol == NULL)
7041 /* Initialize the section. */
7043 elf_tdata (abfd)->elf_text_section = elf_text_section;
7044 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7046 elf_text_section->symbol = elf_text_symbol;
7047 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
7049 elf_text_section->name = ".text";
7050 elf_text_section->flags = SEC_NO_FLAGS;
7051 elf_text_section->output_section = NULL;
7052 elf_text_section->owner = abfd;
7053 elf_text_symbol->name = ".text";
7054 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7055 elf_text_symbol->section = elf_text_section;
7057 /* This code used to do *secp = bfd_und_section_ptr if
7058 info->shared. I don't know why, and that doesn't make sense,
7059 so I took it out. */
7060 *secp = elf_tdata (abfd)->elf_text_section;
7063 case SHN_MIPS_ACOMMON:
7064 /* Fall through. XXX Can we treat this as allocated data? */
7066 /* This section is used in a shared object. */
7067 if (elf_tdata (abfd)->elf_data_section == NULL)
7069 asymbol *elf_data_symbol;
7070 asection *elf_data_section;
7071 bfd_size_type amt = sizeof (asection);
7073 elf_data_section = bfd_zalloc (abfd, amt);
7074 if (elf_data_section == NULL)
7077 amt = sizeof (asymbol);
7078 elf_data_symbol = bfd_zalloc (abfd, amt);
7079 if (elf_data_symbol == NULL)
7082 /* Initialize the section. */
7084 elf_tdata (abfd)->elf_data_section = elf_data_section;
7085 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7087 elf_data_section->symbol = elf_data_symbol;
7088 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
7090 elf_data_section->name = ".data";
7091 elf_data_section->flags = SEC_NO_FLAGS;
7092 elf_data_section->output_section = NULL;
7093 elf_data_section->owner = abfd;
7094 elf_data_symbol->name = ".data";
7095 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7096 elf_data_symbol->section = elf_data_section;
7098 /* This code used to do *secp = bfd_und_section_ptr if
7099 info->shared. I don't know why, and that doesn't make sense,
7100 so I took it out. */
7101 *secp = elf_tdata (abfd)->elf_data_section;
7104 case SHN_MIPS_SUNDEFINED:
7105 *secp = bfd_und_section_ptr;
7109 if (SGI_COMPAT (abfd)
7111 && info->output_bfd->xvec == abfd->xvec
7112 && strcmp (*namep, "__rld_obj_head") == 0)
7114 struct elf_link_hash_entry *h;
7115 struct bfd_link_hash_entry *bh;
7117 /* Mark __rld_obj_head as dynamic. */
7119 if (! (_bfd_generic_link_add_one_symbol
7120 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7121 get_elf_backend_data (abfd)->collect, &bh)))
7124 h = (struct elf_link_hash_entry *) bh;
7127 h->type = STT_OBJECT;
7129 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7132 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7133 mips_elf_hash_table (info)->rld_symbol = h;
7136 /* If this is a mips16 text symbol, add 1 to the value to make it
7137 odd. This will cause something like .word SYM to come up with
7138 the right value when it is loaded into the PC. */
7139 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7145 /* This hook function is called before the linker writes out a global
7146 symbol. We mark symbols as small common if appropriate. This is
7147 also where we undo the increment of the value for a mips16 symbol. */
7150 _bfd_mips_elf_link_output_symbol_hook
7151 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7152 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7153 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7155 /* If we see a common symbol, which implies a relocatable link, then
7156 if a symbol was small common in an input file, mark it as small
7157 common in the output file. */
7158 if (sym->st_shndx == SHN_COMMON
7159 && strcmp (input_sec->name, ".scommon") == 0)
7160 sym->st_shndx = SHN_MIPS_SCOMMON;
7162 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7163 sym->st_value &= ~1;
7168 /* Functions for the dynamic linker. */
7170 /* Create dynamic sections when linking against a dynamic object. */
7173 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7175 struct elf_link_hash_entry *h;
7176 struct bfd_link_hash_entry *bh;
7178 register asection *s;
7179 const char * const *namep;
7180 struct mips_elf_link_hash_table *htab;
7182 htab = mips_elf_hash_table (info);
7183 BFD_ASSERT (htab != NULL);
7185 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7186 | SEC_LINKER_CREATED | SEC_READONLY);
7188 /* The psABI requires a read-only .dynamic section, but the VxWorks
7190 if (!htab->is_vxworks)
7192 s = bfd_get_linker_section (abfd, ".dynamic");
7195 if (! bfd_set_section_flags (abfd, s, flags))
7200 /* We need to create .got section. */
7201 if (!mips_elf_create_got_section (abfd, info))
7204 if (! mips_elf_rel_dyn_section (info, TRUE))
7207 /* Create .stub section. */
7208 s = bfd_make_section_anyway_with_flags (abfd,
7209 MIPS_ELF_STUB_SECTION_NAME (abfd),
7212 || ! bfd_set_section_alignment (abfd, s,
7213 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7217 if (!mips_elf_hash_table (info)->use_rld_obj_head
7219 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7221 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7222 flags &~ (flagword) SEC_READONLY);
7224 || ! bfd_set_section_alignment (abfd, s,
7225 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7229 /* On IRIX5, we adjust add some additional symbols and change the
7230 alignments of several sections. There is no ABI documentation
7231 indicating that this is necessary on IRIX6, nor any evidence that
7232 the linker takes such action. */
7233 if (IRIX_COMPAT (abfd) == ict_irix5)
7235 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7238 if (! (_bfd_generic_link_add_one_symbol
7239 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7240 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7243 h = (struct elf_link_hash_entry *) bh;
7246 h->type = STT_SECTION;
7248 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7252 /* We need to create a .compact_rel section. */
7253 if (SGI_COMPAT (abfd))
7255 if (!mips_elf_create_compact_rel_section (abfd, info))
7259 /* Change alignments of some sections. */
7260 s = bfd_get_linker_section (abfd, ".hash");
7262 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7263 s = bfd_get_linker_section (abfd, ".dynsym");
7265 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7266 s = bfd_get_linker_section (abfd, ".dynstr");
7268 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7270 s = bfd_get_section_by_name (abfd, ".reginfo");
7272 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7273 s = bfd_get_linker_section (abfd, ".dynamic");
7275 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7282 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7284 if (!(_bfd_generic_link_add_one_symbol
7285 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7286 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7289 h = (struct elf_link_hash_entry *) bh;
7292 h->type = STT_SECTION;
7294 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7297 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7299 /* __rld_map is a four byte word located in the .data section
7300 and is filled in by the rtld to contain a pointer to
7301 the _r_debug structure. Its symbol value will be set in
7302 _bfd_mips_elf_finish_dynamic_symbol. */
7303 s = bfd_get_linker_section (abfd, ".rld_map");
7304 BFD_ASSERT (s != NULL);
7306 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7308 if (!(_bfd_generic_link_add_one_symbol
7309 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7310 get_elf_backend_data (abfd)->collect, &bh)))
7313 h = (struct elf_link_hash_entry *) bh;
7316 h->type = STT_OBJECT;
7318 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7320 mips_elf_hash_table (info)->rld_symbol = h;
7324 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7325 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7326 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7329 /* Cache the sections created above. */
7330 htab->splt = bfd_get_linker_section (abfd, ".plt");
7331 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7332 if (htab->is_vxworks)
7334 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7335 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7338 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7340 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7345 if (htab->is_vxworks)
7347 /* Do the usual VxWorks handling. */
7348 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7351 /* Work out the PLT sizes. */
7354 htab->plt_header_size
7355 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7356 htab->plt_entry_size
7357 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7361 htab->plt_header_size
7362 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7363 htab->plt_entry_size
7364 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7367 else if (!info->shared)
7369 /* All variants of the plt0 entry are the same size. */
7370 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7371 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7377 /* Return true if relocation REL against section SEC is a REL rather than
7378 RELA relocation. RELOCS is the first relocation in the section and
7379 ABFD is the bfd that contains SEC. */
7382 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7383 const Elf_Internal_Rela *relocs,
7384 const Elf_Internal_Rela *rel)
7386 Elf_Internal_Shdr *rel_hdr;
7387 const struct elf_backend_data *bed;
7389 /* To determine which flavor of relocation this is, we depend on the
7390 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7391 rel_hdr = elf_section_data (sec)->rel.hdr;
7392 if (rel_hdr == NULL)
7394 bed = get_elf_backend_data (abfd);
7395 return ((size_t) (rel - relocs)
7396 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7399 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7400 HOWTO is the relocation's howto and CONTENTS points to the contents
7401 of the section that REL is against. */
7404 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7405 reloc_howto_type *howto, bfd_byte *contents)
7408 unsigned int r_type;
7411 r_type = ELF_R_TYPE (abfd, rel->r_info);
7412 location = contents + rel->r_offset;
7414 /* Get the addend, which is stored in the input file. */
7415 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7416 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7417 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7419 return addend & howto->src_mask;
7422 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7423 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7424 and update *ADDEND with the final addend. Return true on success
7425 or false if the LO16 could not be found. RELEND is the exclusive
7426 upper bound on the relocations for REL's section. */
7429 mips_elf_add_lo16_rel_addend (bfd *abfd,
7430 const Elf_Internal_Rela *rel,
7431 const Elf_Internal_Rela *relend,
7432 bfd_byte *contents, bfd_vma *addend)
7434 unsigned int r_type, lo16_type;
7435 const Elf_Internal_Rela *lo16_relocation;
7436 reloc_howto_type *lo16_howto;
7439 r_type = ELF_R_TYPE (abfd, rel->r_info);
7440 if (mips16_reloc_p (r_type))
7441 lo16_type = R_MIPS16_LO16;
7442 else if (micromips_reloc_p (r_type))
7443 lo16_type = R_MICROMIPS_LO16;
7445 lo16_type = R_MIPS_LO16;
7447 /* The combined value is the sum of the HI16 addend, left-shifted by
7448 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7449 code does a `lui' of the HI16 value, and then an `addiu' of the
7452 Scan ahead to find a matching LO16 relocation.
7454 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7455 be immediately following. However, for the IRIX6 ABI, the next
7456 relocation may be a composed relocation consisting of several
7457 relocations for the same address. In that case, the R_MIPS_LO16
7458 relocation may occur as one of these. We permit a similar
7459 extension in general, as that is useful for GCC.
7461 In some cases GCC dead code elimination removes the LO16 but keeps
7462 the corresponding HI16. This is strictly speaking a violation of
7463 the ABI but not immediately harmful. */
7464 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7465 if (lo16_relocation == NULL)
7468 /* Obtain the addend kept there. */
7469 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7470 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7472 l <<= lo16_howto->rightshift;
7473 l = _bfd_mips_elf_sign_extend (l, 16);
7480 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7481 store the contents in *CONTENTS on success. Assume that *CONTENTS
7482 already holds the contents if it is nonull on entry. */
7485 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7490 /* Get cached copy if it exists. */
7491 if (elf_section_data (sec)->this_hdr.contents != NULL)
7493 *contents = elf_section_data (sec)->this_hdr.contents;
7497 return bfd_malloc_and_get_section (abfd, sec, contents);
7500 /* Look through the relocs for a section during the first phase, and
7501 allocate space in the global offset table. */
7504 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7505 asection *sec, const Elf_Internal_Rela *relocs)
7509 Elf_Internal_Shdr *symtab_hdr;
7510 struct elf_link_hash_entry **sym_hashes;
7512 const Elf_Internal_Rela *rel;
7513 const Elf_Internal_Rela *rel_end;
7515 const struct elf_backend_data *bed;
7516 struct mips_elf_link_hash_table *htab;
7519 reloc_howto_type *howto;
7521 if (info->relocatable)
7524 htab = mips_elf_hash_table (info);
7525 BFD_ASSERT (htab != NULL);
7527 dynobj = elf_hash_table (info)->dynobj;
7528 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7529 sym_hashes = elf_sym_hashes (abfd);
7530 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7532 bed = get_elf_backend_data (abfd);
7533 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7535 /* Check for the mips16 stub sections. */
7537 name = bfd_get_section_name (abfd, sec);
7538 if (FN_STUB_P (name))
7540 unsigned long r_symndx;
7542 /* Look at the relocation information to figure out which symbol
7545 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7548 (*_bfd_error_handler)
7549 (_("%B: Warning: cannot determine the target function for"
7550 " stub section `%s'"),
7552 bfd_set_error (bfd_error_bad_value);
7556 if (r_symndx < extsymoff
7557 || sym_hashes[r_symndx - extsymoff] == NULL)
7561 /* This stub is for a local symbol. This stub will only be
7562 needed if there is some relocation in this BFD, other
7563 than a 16 bit function call, which refers to this symbol. */
7564 for (o = abfd->sections; o != NULL; o = o->next)
7566 Elf_Internal_Rela *sec_relocs;
7567 const Elf_Internal_Rela *r, *rend;
7569 /* We can ignore stub sections when looking for relocs. */
7570 if ((o->flags & SEC_RELOC) == 0
7571 || o->reloc_count == 0
7572 || section_allows_mips16_refs_p (o))
7576 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7578 if (sec_relocs == NULL)
7581 rend = sec_relocs + o->reloc_count;
7582 for (r = sec_relocs; r < rend; r++)
7583 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7584 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7587 if (elf_section_data (o)->relocs != sec_relocs)
7596 /* There is no non-call reloc for this stub, so we do
7597 not need it. Since this function is called before
7598 the linker maps input sections to output sections, we
7599 can easily discard it by setting the SEC_EXCLUDE
7601 sec->flags |= SEC_EXCLUDE;
7605 /* Record this stub in an array of local symbol stubs for
7607 if (elf_tdata (abfd)->local_stubs == NULL)
7609 unsigned long symcount;
7613 if (elf_bad_symtab (abfd))
7614 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7616 symcount = symtab_hdr->sh_info;
7617 amt = symcount * sizeof (asection *);
7618 n = bfd_zalloc (abfd, amt);
7621 elf_tdata (abfd)->local_stubs = n;
7624 sec->flags |= SEC_KEEP;
7625 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7627 /* We don't need to set mips16_stubs_seen in this case.
7628 That flag is used to see whether we need to look through
7629 the global symbol table for stubs. We don't need to set
7630 it here, because we just have a local stub. */
7634 struct mips_elf_link_hash_entry *h;
7636 h = ((struct mips_elf_link_hash_entry *)
7637 sym_hashes[r_symndx - extsymoff]);
7639 while (h->root.root.type == bfd_link_hash_indirect
7640 || h->root.root.type == bfd_link_hash_warning)
7641 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7643 /* H is the symbol this stub is for. */
7645 /* If we already have an appropriate stub for this function, we
7646 don't need another one, so we can discard this one. Since
7647 this function is called before the linker maps input sections
7648 to output sections, we can easily discard it by setting the
7649 SEC_EXCLUDE flag. */
7650 if (h->fn_stub != NULL)
7652 sec->flags |= SEC_EXCLUDE;
7656 sec->flags |= SEC_KEEP;
7658 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7661 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7663 unsigned long r_symndx;
7664 struct mips_elf_link_hash_entry *h;
7667 /* Look at the relocation information to figure out which symbol
7670 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7673 (*_bfd_error_handler)
7674 (_("%B: Warning: cannot determine the target function for"
7675 " stub section `%s'"),
7677 bfd_set_error (bfd_error_bad_value);
7681 if (r_symndx < extsymoff
7682 || sym_hashes[r_symndx - extsymoff] == NULL)
7686 /* This stub is for a local symbol. This stub will only be
7687 needed if there is some relocation (R_MIPS16_26) in this BFD
7688 that refers to this symbol. */
7689 for (o = abfd->sections; o != NULL; o = o->next)
7691 Elf_Internal_Rela *sec_relocs;
7692 const Elf_Internal_Rela *r, *rend;
7694 /* We can ignore stub sections when looking for relocs. */
7695 if ((o->flags & SEC_RELOC) == 0
7696 || o->reloc_count == 0
7697 || section_allows_mips16_refs_p (o))
7701 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7703 if (sec_relocs == NULL)
7706 rend = sec_relocs + o->reloc_count;
7707 for (r = sec_relocs; r < rend; r++)
7708 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7709 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7712 if (elf_section_data (o)->relocs != sec_relocs)
7721 /* There is no non-call reloc for this stub, so we do
7722 not need it. Since this function is called before
7723 the linker maps input sections to output sections, we
7724 can easily discard it by setting the SEC_EXCLUDE
7726 sec->flags |= SEC_EXCLUDE;
7730 /* Record this stub in an array of local symbol call_stubs for
7732 if (elf_tdata (abfd)->local_call_stubs == NULL)
7734 unsigned long symcount;
7738 if (elf_bad_symtab (abfd))
7739 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7741 symcount = symtab_hdr->sh_info;
7742 amt = symcount * sizeof (asection *);
7743 n = bfd_zalloc (abfd, amt);
7746 elf_tdata (abfd)->local_call_stubs = n;
7749 sec->flags |= SEC_KEEP;
7750 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7752 /* We don't need to set mips16_stubs_seen in this case.
7753 That flag is used to see whether we need to look through
7754 the global symbol table for stubs. We don't need to set
7755 it here, because we just have a local stub. */
7759 h = ((struct mips_elf_link_hash_entry *)
7760 sym_hashes[r_symndx - extsymoff]);
7762 /* H is the symbol this stub is for. */
7764 if (CALL_FP_STUB_P (name))
7765 loc = &h->call_fp_stub;
7767 loc = &h->call_stub;
7769 /* If we already have an appropriate stub for this function, we
7770 don't need another one, so we can discard this one. Since
7771 this function is called before the linker maps input sections
7772 to output sections, we can easily discard it by setting the
7773 SEC_EXCLUDE flag. */
7776 sec->flags |= SEC_EXCLUDE;
7780 sec->flags |= SEC_KEEP;
7782 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7788 for (rel = relocs; rel < rel_end; ++rel)
7790 unsigned long r_symndx;
7791 unsigned int r_type;
7792 struct elf_link_hash_entry *h;
7793 bfd_boolean can_make_dynamic_p;
7795 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7796 r_type = ELF_R_TYPE (abfd, rel->r_info);
7798 if (r_symndx < extsymoff)
7800 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7802 (*_bfd_error_handler)
7803 (_("%B: Malformed reloc detected for section %s"),
7805 bfd_set_error (bfd_error_bad_value);
7810 h = sym_hashes[r_symndx - extsymoff];
7812 && (h->root.type == bfd_link_hash_indirect
7813 || h->root.type == bfd_link_hash_warning))
7814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7817 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7818 relocation into a dynamic one. */
7819 can_make_dynamic_p = FALSE;
7824 case R_MIPS_CALL_HI16:
7825 case R_MIPS_CALL_LO16:
7826 case R_MIPS_GOT_HI16:
7827 case R_MIPS_GOT_LO16:
7828 case R_MIPS_GOT_PAGE:
7829 case R_MIPS_GOT_OFST:
7830 case R_MIPS_GOT_DISP:
7831 case R_MIPS_TLS_GOTTPREL:
7833 case R_MIPS_TLS_LDM:
7834 case R_MIPS16_GOT16:
7835 case R_MIPS16_CALL16:
7836 case R_MIPS16_TLS_GOTTPREL:
7837 case R_MIPS16_TLS_GD:
7838 case R_MIPS16_TLS_LDM:
7839 case R_MICROMIPS_GOT16:
7840 case R_MICROMIPS_CALL16:
7841 case R_MICROMIPS_CALL_HI16:
7842 case R_MICROMIPS_CALL_LO16:
7843 case R_MICROMIPS_GOT_HI16:
7844 case R_MICROMIPS_GOT_LO16:
7845 case R_MICROMIPS_GOT_PAGE:
7846 case R_MICROMIPS_GOT_OFST:
7847 case R_MICROMIPS_GOT_DISP:
7848 case R_MICROMIPS_TLS_GOTTPREL:
7849 case R_MICROMIPS_TLS_GD:
7850 case R_MICROMIPS_TLS_LDM:
7852 elf_hash_table (info)->dynobj = dynobj = abfd;
7853 if (!mips_elf_create_got_section (dynobj, info))
7855 if (htab->is_vxworks && !info->shared)
7857 (*_bfd_error_handler)
7858 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7859 abfd, (unsigned long) rel->r_offset);
7860 bfd_set_error (bfd_error_bad_value);
7865 /* This is just a hint; it can safely be ignored. Don't set
7866 has_static_relocs for the corresponding symbol. */
7868 case R_MICROMIPS_JALR:
7874 /* In VxWorks executables, references to external symbols
7875 must be handled using copy relocs or PLT entries; it is not
7876 possible to convert this relocation into a dynamic one.
7878 For executables that use PLTs and copy-relocs, we have a
7879 choice between converting the relocation into a dynamic
7880 one or using copy relocations or PLT entries. It is
7881 usually better to do the former, unless the relocation is
7882 against a read-only section. */
7885 && !htab->is_vxworks
7886 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7887 && !(!info->nocopyreloc
7888 && !PIC_OBJECT_P (abfd)
7889 && MIPS_ELF_READONLY_SECTION (sec))))
7890 && (sec->flags & SEC_ALLOC) != 0)
7892 can_make_dynamic_p = TRUE;
7894 elf_hash_table (info)->dynobj = dynobj = abfd;
7897 /* For sections that are not SEC_ALLOC a copy reloc would be
7898 output if possible (implying questionable semantics for
7899 read-only data objects) or otherwise the final link would
7900 fail as ld.so will not process them and could not therefore
7901 handle any outstanding dynamic relocations.
7903 For such sections that are also SEC_DEBUGGING, we can avoid
7904 these problems by simply ignoring any relocs as these
7905 sections have a predefined use and we know it is safe to do
7908 This is needed in cases such as a global symbol definition
7909 in a shared library causing a common symbol from an object
7910 file to be converted to an undefined reference. If that
7911 happens, then all the relocations against this symbol from
7912 SEC_DEBUGGING sections in the object file will resolve to
7914 if ((sec->flags & SEC_DEBUGGING) != 0)
7919 /* Most static relocations require pointer equality, except
7922 h->pointer_equality_needed = TRUE;
7928 case R_MICROMIPS_26_S1:
7929 case R_MICROMIPS_PC7_S1:
7930 case R_MICROMIPS_PC10_S1:
7931 case R_MICROMIPS_PC16_S1:
7932 case R_MICROMIPS_PC23_S2:
7934 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7940 /* Relocations against the special VxWorks __GOTT_BASE__ and
7941 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7942 room for them in .rela.dyn. */
7943 if (is_gott_symbol (info, h))
7947 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7951 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7952 if (MIPS_ELF_READONLY_SECTION (sec))
7953 /* We tell the dynamic linker that there are
7954 relocations against the text segment. */
7955 info->flags |= DF_TEXTREL;
7958 else if (call_lo16_reloc_p (r_type)
7959 || got_lo16_reloc_p (r_type)
7960 || got_disp_reloc_p (r_type)
7961 || (got16_reloc_p (r_type) && htab->is_vxworks))
7963 /* We may need a local GOT entry for this relocation. We
7964 don't count R_MIPS_GOT_PAGE because we can estimate the
7965 maximum number of pages needed by looking at the size of
7966 the segment. Similar comments apply to R_MIPS*_GOT16 and
7967 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7968 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7969 R_MIPS_CALL_HI16 because these are always followed by an
7970 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7971 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7972 rel->r_addend, info, r_type))
7977 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
7978 ELF_ST_IS_MIPS16 (h->other)))
7979 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7984 case R_MIPS16_CALL16:
7985 case R_MICROMIPS_CALL16:
7988 (*_bfd_error_handler)
7989 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7990 abfd, (unsigned long) rel->r_offset);
7991 bfd_set_error (bfd_error_bad_value);
7996 case R_MIPS_CALL_HI16:
7997 case R_MIPS_CALL_LO16:
7998 case R_MICROMIPS_CALL_HI16:
7999 case R_MICROMIPS_CALL_LO16:
8002 /* Make sure there is room in the regular GOT to hold the
8003 function's address. We may eliminate it in favour of
8004 a .got.plt entry later; see mips_elf_count_got_symbols. */
8005 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8009 /* We need a stub, not a plt entry for the undefined
8010 function. But we record it as if it needs plt. See
8011 _bfd_elf_adjust_dynamic_symbol. */
8017 case R_MIPS_GOT_PAGE:
8018 case R_MICROMIPS_GOT_PAGE:
8019 /* If this is a global, overridable symbol, GOT_PAGE will
8020 decay to GOT_DISP, so we'll need a GOT entry for it. */
8023 struct mips_elf_link_hash_entry *hmips =
8024 (struct mips_elf_link_hash_entry *) h;
8026 /* This symbol is definitely not overridable. */
8027 if (hmips->root.def_regular
8028 && ! (info->shared && ! info->symbolic
8029 && ! hmips->root.forced_local))
8034 case R_MIPS16_GOT16:
8036 case R_MIPS_GOT_HI16:
8037 case R_MIPS_GOT_LO16:
8038 case R_MICROMIPS_GOT16:
8039 case R_MICROMIPS_GOT_HI16:
8040 case R_MICROMIPS_GOT_LO16:
8041 if (!h || got_page_reloc_p (r_type))
8043 /* This relocation needs (or may need, if h != NULL) a
8044 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8045 know for sure until we know whether the symbol is
8047 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8049 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8051 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8052 addend = mips_elf_read_rel_addend (abfd, rel,
8054 if (got16_reloc_p (r_type))
8055 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8058 addend <<= howto->rightshift;
8061 addend = rel->r_addend;
8062 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
8068 case R_MIPS_GOT_DISP:
8069 case R_MICROMIPS_GOT_DISP:
8070 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8075 case R_MIPS_TLS_GOTTPREL:
8076 case R_MIPS16_TLS_GOTTPREL:
8077 case R_MICROMIPS_TLS_GOTTPREL:
8079 info->flags |= DF_STATIC_TLS;
8082 case R_MIPS_TLS_LDM:
8083 case R_MIPS16_TLS_LDM:
8084 case R_MICROMIPS_TLS_LDM:
8085 if (tls_ldm_reloc_p (r_type))
8087 r_symndx = STN_UNDEF;
8093 case R_MIPS16_TLS_GD:
8094 case R_MICROMIPS_TLS_GD:
8095 /* This symbol requires a global offset table entry, or two
8096 for TLS GD relocations. */
8099 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8105 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8115 /* In VxWorks executables, references to external symbols
8116 are handled using copy relocs or PLT stubs, so there's
8117 no need to add a .rela.dyn entry for this relocation. */
8118 if (can_make_dynamic_p)
8122 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8126 if (info->shared && h == NULL)
8128 /* When creating a shared object, we must copy these
8129 reloc types into the output file as R_MIPS_REL32
8130 relocs. Make room for this reloc in .rel(a).dyn. */
8131 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8132 if (MIPS_ELF_READONLY_SECTION (sec))
8133 /* We tell the dynamic linker that there are
8134 relocations against the text segment. */
8135 info->flags |= DF_TEXTREL;
8139 struct mips_elf_link_hash_entry *hmips;
8141 /* For a shared object, we must copy this relocation
8142 unless the symbol turns out to be undefined and
8143 weak with non-default visibility, in which case
8144 it will be left as zero.
8146 We could elide R_MIPS_REL32 for locally binding symbols
8147 in shared libraries, but do not yet do so.
8149 For an executable, we only need to copy this
8150 reloc if the symbol is defined in a dynamic
8152 hmips = (struct mips_elf_link_hash_entry *) h;
8153 ++hmips->possibly_dynamic_relocs;
8154 if (MIPS_ELF_READONLY_SECTION (sec))
8155 /* We need it to tell the dynamic linker if there
8156 are relocations against the text segment. */
8157 hmips->readonly_reloc = TRUE;
8161 if (SGI_COMPAT (abfd))
8162 mips_elf_hash_table (info)->compact_rel_size +=
8163 sizeof (Elf32_External_crinfo);
8167 case R_MIPS_GPREL16:
8168 case R_MIPS_LITERAL:
8169 case R_MIPS_GPREL32:
8170 case R_MICROMIPS_26_S1:
8171 case R_MICROMIPS_GPREL16:
8172 case R_MICROMIPS_LITERAL:
8173 case R_MICROMIPS_GPREL7_S2:
8174 if (SGI_COMPAT (abfd))
8175 mips_elf_hash_table (info)->compact_rel_size +=
8176 sizeof (Elf32_External_crinfo);
8179 /* This relocation describes the C++ object vtable hierarchy.
8180 Reconstruct it for later use during GC. */
8181 case R_MIPS_GNU_VTINHERIT:
8182 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8186 /* This relocation describes which C++ vtable entries are actually
8187 used. Record for later use during GC. */
8188 case R_MIPS_GNU_VTENTRY:
8189 BFD_ASSERT (h != NULL);
8191 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8199 /* We must not create a stub for a symbol that has relocations
8200 related to taking the function's address. This doesn't apply to
8201 VxWorks, where CALL relocs refer to a .got.plt entry instead of
8202 a normal .got entry. */
8203 if (!htab->is_vxworks && h != NULL)
8207 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8209 case R_MIPS16_CALL16:
8211 case R_MIPS_CALL_HI16:
8212 case R_MIPS_CALL_LO16:
8214 case R_MICROMIPS_CALL16:
8215 case R_MICROMIPS_CALL_HI16:
8216 case R_MICROMIPS_CALL_LO16:
8217 case R_MICROMIPS_JALR:
8221 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8222 if there is one. We only need to handle global symbols here;
8223 we decide whether to keep or delete stubs for local symbols
8224 when processing the stub's relocations. */
8226 && !mips16_call_reloc_p (r_type)
8227 && !section_allows_mips16_refs_p (sec))
8229 struct mips_elf_link_hash_entry *mh;
8231 mh = (struct mips_elf_link_hash_entry *) h;
8232 mh->need_fn_stub = TRUE;
8235 /* Refuse some position-dependent relocations when creating a
8236 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8237 not PIC, but we can create dynamic relocations and the result
8238 will be fine. Also do not refuse R_MIPS_LO16, which can be
8239 combined with R_MIPS_GOT16. */
8247 case R_MIPS_HIGHEST:
8248 case R_MICROMIPS_HI16:
8249 case R_MICROMIPS_HIGHER:
8250 case R_MICROMIPS_HIGHEST:
8251 /* Don't refuse a high part relocation if it's against
8252 no symbol (e.g. part of a compound relocation). */
8253 if (r_symndx == STN_UNDEF)
8256 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8257 and has a special meaning. */
8258 if (!NEWABI_P (abfd) && h != NULL
8259 && strcmp (h->root.root.string, "_gp_disp") == 0)
8262 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8263 if (is_gott_symbol (info, h))
8270 case R_MICROMIPS_26_S1:
8271 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8272 (*_bfd_error_handler)
8273 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8275 (h) ? h->root.root.string : "a local symbol");
8276 bfd_set_error (bfd_error_bad_value);
8288 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8289 struct bfd_link_info *link_info,
8292 Elf_Internal_Rela *internal_relocs;
8293 Elf_Internal_Rela *irel, *irelend;
8294 Elf_Internal_Shdr *symtab_hdr;
8295 bfd_byte *contents = NULL;
8297 bfd_boolean changed_contents = FALSE;
8298 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8299 Elf_Internal_Sym *isymbuf = NULL;
8301 /* We are not currently changing any sizes, so only one pass. */
8304 if (link_info->relocatable)
8307 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8308 link_info->keep_memory);
8309 if (internal_relocs == NULL)
8312 irelend = internal_relocs + sec->reloc_count
8313 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8314 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8315 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8317 for (irel = internal_relocs; irel < irelend; irel++)
8320 bfd_signed_vma sym_offset;
8321 unsigned int r_type;
8322 unsigned long r_symndx;
8324 unsigned long instruction;
8326 /* Turn jalr into bgezal, and jr into beq, if they're marked
8327 with a JALR relocation, that indicate where they jump to.
8328 This saves some pipeline bubbles. */
8329 r_type = ELF_R_TYPE (abfd, irel->r_info);
8330 if (r_type != R_MIPS_JALR)
8333 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8334 /* Compute the address of the jump target. */
8335 if (r_symndx >= extsymoff)
8337 struct mips_elf_link_hash_entry *h
8338 = ((struct mips_elf_link_hash_entry *)
8339 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8341 while (h->root.root.type == bfd_link_hash_indirect
8342 || h->root.root.type == bfd_link_hash_warning)
8343 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8345 /* If a symbol is undefined, or if it may be overridden,
8347 if (! ((h->root.root.type == bfd_link_hash_defined
8348 || h->root.root.type == bfd_link_hash_defweak)
8349 && h->root.root.u.def.section)
8350 || (link_info->shared && ! link_info->symbolic
8351 && !h->root.forced_local))
8354 sym_sec = h->root.root.u.def.section;
8355 if (sym_sec->output_section)
8356 symval = (h->root.root.u.def.value
8357 + sym_sec->output_section->vma
8358 + sym_sec->output_offset);
8360 symval = h->root.root.u.def.value;
8364 Elf_Internal_Sym *isym;
8366 /* Read this BFD's symbols if we haven't done so already. */
8367 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8369 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8370 if (isymbuf == NULL)
8371 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8372 symtab_hdr->sh_info, 0,
8374 if (isymbuf == NULL)
8378 isym = isymbuf + r_symndx;
8379 if (isym->st_shndx == SHN_UNDEF)
8381 else if (isym->st_shndx == SHN_ABS)
8382 sym_sec = bfd_abs_section_ptr;
8383 else if (isym->st_shndx == SHN_COMMON)
8384 sym_sec = bfd_com_section_ptr;
8387 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8388 symval = isym->st_value
8389 + sym_sec->output_section->vma
8390 + sym_sec->output_offset;
8393 /* Compute branch offset, from delay slot of the jump to the
8395 sym_offset = (symval + irel->r_addend)
8396 - (sec_start + irel->r_offset + 4);
8398 /* Branch offset must be properly aligned. */
8399 if ((sym_offset & 3) != 0)
8404 /* Check that it's in range. */
8405 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8408 /* Get the section contents if we haven't done so already. */
8409 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8412 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8414 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8415 if ((instruction & 0xfc1fffff) == 0x0000f809)
8416 instruction = 0x04110000;
8417 /* If it was jr <reg>, turn it into b <target>. */
8418 else if ((instruction & 0xfc1fffff) == 0x00000008)
8419 instruction = 0x10000000;
8423 instruction |= (sym_offset & 0xffff);
8424 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8425 changed_contents = TRUE;
8428 if (contents != NULL
8429 && elf_section_data (sec)->this_hdr.contents != contents)
8431 if (!changed_contents && !link_info->keep_memory)
8435 /* Cache the section contents for elf_link_input_bfd. */
8436 elf_section_data (sec)->this_hdr.contents = contents;
8442 if (contents != NULL
8443 && elf_section_data (sec)->this_hdr.contents != contents)
8448 /* Allocate space for global sym dynamic relocs. */
8451 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8453 struct bfd_link_info *info = inf;
8455 struct mips_elf_link_hash_entry *hmips;
8456 struct mips_elf_link_hash_table *htab;
8458 htab = mips_elf_hash_table (info);
8459 BFD_ASSERT (htab != NULL);
8461 dynobj = elf_hash_table (info)->dynobj;
8462 hmips = (struct mips_elf_link_hash_entry *) h;
8464 /* VxWorks executables are handled elsewhere; we only need to
8465 allocate relocations in shared objects. */
8466 if (htab->is_vxworks && !info->shared)
8469 /* Ignore indirect symbols. All relocations against such symbols
8470 will be redirected to the target symbol. */
8471 if (h->root.type == bfd_link_hash_indirect)
8474 /* If this symbol is defined in a dynamic object, or we are creating
8475 a shared library, we will need to copy any R_MIPS_32 or
8476 R_MIPS_REL32 relocs against it into the output file. */
8477 if (! info->relocatable
8478 && hmips->possibly_dynamic_relocs != 0
8479 && (h->root.type == bfd_link_hash_defweak
8480 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8483 bfd_boolean do_copy = TRUE;
8485 if (h->root.type == bfd_link_hash_undefweak)
8487 /* Do not copy relocations for undefined weak symbols with
8488 non-default visibility. */
8489 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8492 /* Make sure undefined weak symbols are output as a dynamic
8494 else if (h->dynindx == -1 && !h->forced_local)
8496 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8503 /* Even though we don't directly need a GOT entry for this symbol,
8504 the SVR4 psABI requires it to have a dynamic symbol table
8505 index greater that DT_MIPS_GOTSYM if there are dynamic
8506 relocations against it.
8508 VxWorks does not enforce the same mapping between the GOT
8509 and the symbol table, so the same requirement does not
8511 if (!htab->is_vxworks)
8513 if (hmips->global_got_area > GGA_RELOC_ONLY)
8514 hmips->global_got_area = GGA_RELOC_ONLY;
8515 hmips->got_only_for_calls = FALSE;
8518 mips_elf_allocate_dynamic_relocations
8519 (dynobj, info, hmips->possibly_dynamic_relocs);
8520 if (hmips->readonly_reloc)
8521 /* We tell the dynamic linker that there are relocations
8522 against the text segment. */
8523 info->flags |= DF_TEXTREL;
8530 /* Adjust a symbol defined by a dynamic object and referenced by a
8531 regular object. The current definition is in some section of the
8532 dynamic object, but we're not including those sections. We have to
8533 change the definition to something the rest of the link can
8537 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8538 struct elf_link_hash_entry *h)
8541 struct mips_elf_link_hash_entry *hmips;
8542 struct mips_elf_link_hash_table *htab;
8544 htab = mips_elf_hash_table (info);
8545 BFD_ASSERT (htab != NULL);
8547 dynobj = elf_hash_table (info)->dynobj;
8548 hmips = (struct mips_elf_link_hash_entry *) h;
8550 /* Make sure we know what is going on here. */
8551 BFD_ASSERT (dynobj != NULL
8553 || h->u.weakdef != NULL
8556 && !h->def_regular)));
8558 hmips = (struct mips_elf_link_hash_entry *) h;
8560 /* If there are call relocations against an externally-defined symbol,
8561 see whether we can create a MIPS lazy-binding stub for it. We can
8562 only do this if all references to the function are through call
8563 relocations, and in that case, the traditional lazy-binding stubs
8564 are much more efficient than PLT entries.
8566 Traditional stubs are only available on SVR4 psABI-based systems;
8567 VxWorks always uses PLTs instead. */
8568 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8570 if (! elf_hash_table (info)->dynamic_sections_created)
8573 /* If this symbol is not defined in a regular file, then set
8574 the symbol to the stub location. This is required to make
8575 function pointers compare as equal between the normal
8576 executable and the shared library. */
8577 if (!h->def_regular)
8579 hmips->needs_lazy_stub = TRUE;
8580 htab->lazy_stub_count++;
8584 /* As above, VxWorks requires PLT entries for externally-defined
8585 functions that are only accessed through call relocations.
8587 Both VxWorks and non-VxWorks targets also need PLT entries if there
8588 are static-only relocations against an externally-defined function.
8589 This can technically occur for shared libraries if there are
8590 branches to the symbol, although it is unlikely that this will be
8591 used in practice due to the short ranges involved. It can occur
8592 for any relative or absolute relocation in executables; in that
8593 case, the PLT entry becomes the function's canonical address. */
8594 else if (((h->needs_plt && !hmips->no_fn_stub)
8595 || (h->type == STT_FUNC && hmips->has_static_relocs))
8596 && htab->use_plts_and_copy_relocs
8597 && !SYMBOL_CALLS_LOCAL (info, h)
8598 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8599 && h->root.type == bfd_link_hash_undefweak))
8601 /* If this is the first symbol to need a PLT entry, allocate room
8603 if (htab->splt->size == 0)
8605 BFD_ASSERT (htab->sgotplt->size == 0);
8607 /* If we're using the PLT additions to the psABI, each PLT
8608 entry is 16 bytes and the PLT0 entry is 32 bytes.
8609 Encourage better cache usage by aligning. We do this
8610 lazily to avoid pessimizing traditional objects. */
8611 if (!htab->is_vxworks
8612 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8615 /* Make sure that .got.plt is word-aligned. We do this lazily
8616 for the same reason as above. */
8617 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8618 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8621 htab->splt->size += htab->plt_header_size;
8623 /* On non-VxWorks targets, the first two entries in .got.plt
8625 if (!htab->is_vxworks)
8627 += get_elf_backend_data (dynobj)->got_header_size;
8629 /* On VxWorks, also allocate room for the header's
8630 .rela.plt.unloaded entries. */
8631 if (htab->is_vxworks && !info->shared)
8632 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8635 /* Assign the next .plt entry to this symbol. */
8636 h->plt.offset = htab->splt->size;
8637 htab->splt->size += htab->plt_entry_size;
8639 /* If the output file has no definition of the symbol, set the
8640 symbol's value to the address of the stub. */
8641 if (!info->shared && !h->def_regular)
8643 h->root.u.def.section = htab->splt;
8644 h->root.u.def.value = h->plt.offset;
8645 /* For VxWorks, point at the PLT load stub rather than the
8646 lazy resolution stub; this stub will become the canonical
8647 function address. */
8648 if (htab->is_vxworks)
8649 h->root.u.def.value += 8;
8652 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8654 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8655 htab->srelplt->size += (htab->is_vxworks
8656 ? MIPS_ELF_RELA_SIZE (dynobj)
8657 : MIPS_ELF_REL_SIZE (dynobj));
8659 /* Make room for the .rela.plt.unloaded relocations. */
8660 if (htab->is_vxworks && !info->shared)
8661 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8663 /* All relocations against this symbol that could have been made
8664 dynamic will now refer to the PLT entry instead. */
8665 hmips->possibly_dynamic_relocs = 0;
8670 /* If this is a weak symbol, and there is a real definition, the
8671 processor independent code will have arranged for us to see the
8672 real definition first, and we can just use the same value. */
8673 if (h->u.weakdef != NULL)
8675 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8676 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8677 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8678 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8682 /* Otherwise, there is nothing further to do for symbols defined
8683 in regular objects. */
8687 /* There's also nothing more to do if we'll convert all relocations
8688 against this symbol into dynamic relocations. */
8689 if (!hmips->has_static_relocs)
8692 /* We're now relying on copy relocations. Complain if we have
8693 some that we can't convert. */
8694 if (!htab->use_plts_and_copy_relocs || info->shared)
8696 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8697 "dynamic symbol %s"),
8698 h->root.root.string);
8699 bfd_set_error (bfd_error_bad_value);
8703 /* We must allocate the symbol in our .dynbss section, which will
8704 become part of the .bss section of the executable. There will be
8705 an entry for this symbol in the .dynsym section. The dynamic
8706 object will contain position independent code, so all references
8707 from the dynamic object to this symbol will go through the global
8708 offset table. The dynamic linker will use the .dynsym entry to
8709 determine the address it must put in the global offset table, so
8710 both the dynamic object and the regular object will refer to the
8711 same memory location for the variable. */
8713 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8715 if (htab->is_vxworks)
8716 htab->srelbss->size += sizeof (Elf32_External_Rela);
8718 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8722 /* All relocations against this symbol that could have been made
8723 dynamic will now refer to the local copy instead. */
8724 hmips->possibly_dynamic_relocs = 0;
8726 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8729 /* This function is called after all the input files have been read,
8730 and the input sections have been assigned to output sections. We
8731 check for any mips16 stub sections that we can discard. */
8734 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8735 struct bfd_link_info *info)
8738 struct mips_elf_link_hash_table *htab;
8739 struct mips_htab_traverse_info hti;
8741 htab = mips_elf_hash_table (info);
8742 BFD_ASSERT (htab != NULL);
8744 /* The .reginfo section has a fixed size. */
8745 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8747 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8750 hti.output_bfd = output_bfd;
8752 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8753 mips_elf_check_symbols, &hti);
8760 /* If the link uses a GOT, lay it out and work out its size. */
8763 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8767 struct mips_got_info *g;
8768 bfd_size_type loadable_size = 0;
8769 bfd_size_type page_gotno;
8771 struct mips_elf_traverse_got_arg tga;
8772 struct mips_elf_link_hash_table *htab;
8774 htab = mips_elf_hash_table (info);
8775 BFD_ASSERT (htab != NULL);
8781 dynobj = elf_hash_table (info)->dynobj;
8784 /* Allocate room for the reserved entries. VxWorks always reserves
8785 3 entries; other objects only reserve 2 entries. */
8786 BFD_ASSERT (g->assigned_gotno == 0);
8787 if (htab->is_vxworks)
8788 htab->reserved_gotno = 3;
8790 htab->reserved_gotno = 2;
8791 g->local_gotno += htab->reserved_gotno;
8792 g->assigned_gotno = htab->reserved_gotno;
8794 /* Replace entries for indirect and warning symbols with entries for
8795 the target symbol. */
8796 if (!mips_elf_resolve_final_got_entries (g))
8799 /* Count the number of GOT symbols. */
8800 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
8802 /* Calculate the total loadable size of the output. That
8803 will give us the maximum number of GOT_PAGE entries
8805 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
8807 asection *subsection;
8809 for (subsection = ibfd->sections;
8811 subsection = subsection->next)
8813 if ((subsection->flags & SEC_ALLOC) == 0)
8815 loadable_size += ((subsection->size + 0xf)
8816 &~ (bfd_size_type) 0xf);
8820 if (htab->is_vxworks)
8821 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8822 relocations against local symbols evaluate to "G", and the EABI does
8823 not include R_MIPS_GOT_PAGE. */
8826 /* Assume there are two loadable segments consisting of contiguous
8827 sections. Is 5 enough? */
8828 page_gotno = (loadable_size >> 16) + 5;
8830 /* Choose the smaller of the two estimates; both are intended to be
8832 if (page_gotno > g->page_gotno)
8833 page_gotno = g->page_gotno;
8835 g->local_gotno += page_gotno;
8837 /* Count the number of local GOT entries and TLS relocs. */
8840 htab_traverse (g->got_entries, mips_elf_count_local_got_entries, &tga);
8842 /* We need to calculate tls_gotno for global symbols at this point
8843 instead of building it up earlier, to avoid doublecounting
8844 entries for one global symbol from multiple input files. */
8845 elf_link_hash_traverse (elf_hash_table (info),
8846 mips_elf_count_global_tls_entries,
8849 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8850 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8851 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8853 /* VxWorks does not support multiple GOTs. It initializes $gp to
8854 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8856 if (htab->is_vxworks)
8858 /* VxWorks executables do not need a GOT. */
8861 /* Each VxWorks GOT entry needs an explicit relocation. */
8864 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8866 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8869 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8871 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8876 /* Record that all bfds use G. This also has the effect of freeing
8877 the per-bfd GOTs, which we no longer need. */
8878 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
8879 if (mips_elf_bfd_got (ibfd, FALSE))
8880 mips_elf_replace_bfd_got (ibfd, g);
8881 mips_elf_replace_bfd_got (output_bfd, g);
8883 /* Set up TLS entries. */
8884 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8887 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
8888 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
8891 BFD_ASSERT (g->tls_assigned_gotno
8892 == g->global_gotno + g->local_gotno + g->tls_gotno);
8894 /* Allocate room for the TLS relocations. */
8896 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
8902 /* Estimate the size of the .MIPS.stubs section. */
8905 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8907 struct mips_elf_link_hash_table *htab;
8908 bfd_size_type dynsymcount;
8910 htab = mips_elf_hash_table (info);
8911 BFD_ASSERT (htab != NULL);
8913 if (htab->lazy_stub_count == 0)
8916 /* IRIX rld assumes that a function stub isn't at the end of the .text
8917 section, so add a dummy entry to the end. */
8918 htab->lazy_stub_count++;
8920 /* Get a worst-case estimate of the number of dynamic symbols needed.
8921 At this point, dynsymcount does not account for section symbols
8922 and count_section_dynsyms may overestimate the number that will
8924 dynsymcount = (elf_hash_table (info)->dynsymcount
8925 + count_section_dynsyms (output_bfd, info));
8927 /* Determine the size of one stub entry. */
8928 htab->function_stub_size = (dynsymcount > 0x10000
8929 ? MIPS_FUNCTION_STUB_BIG_SIZE
8930 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8932 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8935 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8936 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8937 allocate an entry in the stubs section. */
8940 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8942 struct mips_elf_link_hash_table *htab;
8944 htab = (struct mips_elf_link_hash_table *) data;
8945 if (h->needs_lazy_stub)
8947 h->root.root.u.def.section = htab->sstubs;
8948 h->root.root.u.def.value = htab->sstubs->size;
8949 h->root.plt.offset = htab->sstubs->size;
8950 htab->sstubs->size += htab->function_stub_size;
8955 /* Allocate offsets in the stubs section to each symbol that needs one.
8956 Set the final size of the .MIPS.stub section. */
8959 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8961 struct mips_elf_link_hash_table *htab;
8963 htab = mips_elf_hash_table (info);
8964 BFD_ASSERT (htab != NULL);
8966 if (htab->lazy_stub_count == 0)
8969 htab->sstubs->size = 0;
8970 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
8971 htab->sstubs->size += htab->function_stub_size;
8972 BFD_ASSERT (htab->sstubs->size
8973 == htab->lazy_stub_count * htab->function_stub_size);
8976 /* Set the sizes of the dynamic sections. */
8979 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8980 struct bfd_link_info *info)
8983 asection *s, *sreldyn;
8984 bfd_boolean reltext;
8985 struct mips_elf_link_hash_table *htab;
8987 htab = mips_elf_hash_table (info);
8988 BFD_ASSERT (htab != NULL);
8989 dynobj = elf_hash_table (info)->dynobj;
8990 BFD_ASSERT (dynobj != NULL);
8992 if (elf_hash_table (info)->dynamic_sections_created)
8994 /* Set the contents of the .interp section to the interpreter. */
8995 if (info->executable)
8997 s = bfd_get_linker_section (dynobj, ".interp");
8998 BFD_ASSERT (s != NULL);
9000 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9002 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9005 /* Create a symbol for the PLT, if we know that we are using it. */
9006 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
9008 struct elf_link_hash_entry *h;
9010 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9012 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9013 "_PROCEDURE_LINKAGE_TABLE_");
9014 htab->root.hplt = h;
9021 /* Allocate space for global sym dynamic relocs. */
9022 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9024 mips_elf_estimate_stub_size (output_bfd, info);
9026 if (!mips_elf_lay_out_got (output_bfd, info))
9029 mips_elf_lay_out_lazy_stubs (info);
9031 /* The check_relocs and adjust_dynamic_symbol entry points have
9032 determined the sizes of the various dynamic sections. Allocate
9035 for (s = dynobj->sections; s != NULL; s = s->next)
9039 /* It's OK to base decisions on the section name, because none
9040 of the dynobj section names depend upon the input files. */
9041 name = bfd_get_section_name (dynobj, s);
9043 if ((s->flags & SEC_LINKER_CREATED) == 0)
9046 if (CONST_STRNEQ (name, ".rel"))
9050 const char *outname;
9053 /* If this relocation section applies to a read only
9054 section, then we probably need a DT_TEXTREL entry.
9055 If the relocation section is .rel(a).dyn, we always
9056 assert a DT_TEXTREL entry rather than testing whether
9057 there exists a relocation to a read only section or
9059 outname = bfd_get_section_name (output_bfd,
9061 target = bfd_get_section_by_name (output_bfd, outname + 4);
9063 && (target->flags & SEC_READONLY) != 0
9064 && (target->flags & SEC_ALLOC) != 0)
9065 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9068 /* We use the reloc_count field as a counter if we need
9069 to copy relocs into the output file. */
9070 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9073 /* If combreloc is enabled, elf_link_sort_relocs() will
9074 sort relocations, but in a different way than we do,
9075 and before we're done creating relocations. Also, it
9076 will move them around between input sections'
9077 relocation's contents, so our sorting would be
9078 broken, so don't let it run. */
9079 info->combreloc = 0;
9082 else if (! info->shared
9083 && ! mips_elf_hash_table (info)->use_rld_obj_head
9084 && CONST_STRNEQ (name, ".rld_map"))
9086 /* We add a room for __rld_map. It will be filled in by the
9087 rtld to contain a pointer to the _r_debug structure. */
9088 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9090 else if (SGI_COMPAT (output_bfd)
9091 && CONST_STRNEQ (name, ".compact_rel"))
9092 s->size += mips_elf_hash_table (info)->compact_rel_size;
9093 else if (s == htab->splt)
9095 /* If the last PLT entry has a branch delay slot, allocate
9096 room for an extra nop to fill the delay slot. This is
9097 for CPUs without load interlocking. */
9098 if (! LOAD_INTERLOCKS_P (output_bfd)
9099 && ! htab->is_vxworks && s->size > 0)
9102 else if (! CONST_STRNEQ (name, ".init")
9104 && s != htab->sgotplt
9105 && s != htab->sstubs
9106 && s != htab->sdynbss)
9108 /* It's not one of our sections, so don't allocate space. */
9114 s->flags |= SEC_EXCLUDE;
9118 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9121 /* Allocate memory for the section contents. */
9122 s->contents = bfd_zalloc (dynobj, s->size);
9123 if (s->contents == NULL)
9125 bfd_set_error (bfd_error_no_memory);
9130 if (elf_hash_table (info)->dynamic_sections_created)
9132 /* Add some entries to the .dynamic section. We fill in the
9133 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9134 must add the entries now so that we get the correct size for
9135 the .dynamic section. */
9137 /* SGI object has the equivalence of DT_DEBUG in the
9138 DT_MIPS_RLD_MAP entry. This must come first because glibc
9139 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9140 may only look at the first one they see. */
9142 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9145 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9146 used by the debugger. */
9147 if (info->executable
9148 && !SGI_COMPAT (output_bfd)
9149 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9152 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9153 info->flags |= DF_TEXTREL;
9155 if ((info->flags & DF_TEXTREL) != 0)
9157 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9160 /* Clear the DF_TEXTREL flag. It will be set again if we
9161 write out an actual text relocation; we may not, because
9162 at this point we do not know whether e.g. any .eh_frame
9163 absolute relocations have been converted to PC-relative. */
9164 info->flags &= ~DF_TEXTREL;
9167 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9170 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9171 if (htab->is_vxworks)
9173 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9174 use any of the DT_MIPS_* tags. */
9175 if (sreldyn && sreldyn->size > 0)
9177 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9180 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9183 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9189 if (sreldyn && sreldyn->size > 0)
9191 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9194 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9197 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9201 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9204 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9207 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9210 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9213 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9216 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9219 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9222 if (IRIX_COMPAT (dynobj) == ict_irix5
9223 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9226 if (IRIX_COMPAT (dynobj) == ict_irix6
9227 && (bfd_get_section_by_name
9228 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9229 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9232 if (htab->splt->size > 0)
9234 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9237 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9240 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9243 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9246 if (htab->is_vxworks
9247 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9254 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9255 Adjust its R_ADDEND field so that it is correct for the output file.
9256 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9257 and sections respectively; both use symbol indexes. */
9260 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9261 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9262 asection **local_sections, Elf_Internal_Rela *rel)
9264 unsigned int r_type, r_symndx;
9265 Elf_Internal_Sym *sym;
9268 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9270 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9271 if (gprel16_reloc_p (r_type)
9272 || r_type == R_MIPS_GPREL32
9273 || literal_reloc_p (r_type))
9275 rel->r_addend += _bfd_get_gp_value (input_bfd);
9276 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9279 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9280 sym = local_syms + r_symndx;
9282 /* Adjust REL's addend to account for section merging. */
9283 if (!info->relocatable)
9285 sec = local_sections[r_symndx];
9286 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9289 /* This would normally be done by the rela_normal code in elflink.c. */
9290 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9291 rel->r_addend += local_sections[r_symndx]->output_offset;
9295 /* Handle relocations against symbols from removed linkonce sections,
9296 or sections discarded by a linker script. We use this wrapper around
9297 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9298 on 64-bit ELF targets. In this case for any relocation handled, which
9299 always be the first in a triplet, the remaining two have to be processed
9300 together with the first, even if they are R_MIPS_NONE. It is the symbol
9301 index referred by the first reloc that applies to all the three and the
9302 remaining two never refer to an object symbol. And it is the final
9303 relocation (the last non-null one) that determines the output field of
9304 the whole relocation so retrieve the corresponding howto structure for
9305 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9307 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9308 and therefore requires to be pasted in a loop. It also defines a block
9309 and does not protect any of its arguments, hence the extra brackets. */
9312 mips_reloc_against_discarded_section (bfd *output_bfd,
9313 struct bfd_link_info *info,
9314 bfd *input_bfd, asection *input_section,
9315 Elf_Internal_Rela **rel,
9316 const Elf_Internal_Rela **relend,
9317 bfd_boolean rel_reloc,
9318 reloc_howto_type *howto,
9321 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9322 int count = bed->s->int_rels_per_ext_rel;
9323 unsigned int r_type;
9326 for (i = count - 1; i > 0; i--)
9328 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9329 if (r_type != R_MIPS_NONE)
9331 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9337 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9338 (*rel), count, (*relend),
9339 howto, i, contents);
9344 /* Relocate a MIPS ELF section. */
9347 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9348 bfd *input_bfd, asection *input_section,
9349 bfd_byte *contents, Elf_Internal_Rela *relocs,
9350 Elf_Internal_Sym *local_syms,
9351 asection **local_sections)
9353 Elf_Internal_Rela *rel;
9354 const Elf_Internal_Rela *relend;
9356 bfd_boolean use_saved_addend_p = FALSE;
9357 const struct elf_backend_data *bed;
9359 bed = get_elf_backend_data (output_bfd);
9360 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9361 for (rel = relocs; rel < relend; ++rel)
9365 reloc_howto_type *howto;
9366 bfd_boolean cross_mode_jump_p;
9367 /* TRUE if the relocation is a RELA relocation, rather than a
9369 bfd_boolean rela_relocation_p = TRUE;
9370 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9372 unsigned long r_symndx;
9374 Elf_Internal_Shdr *symtab_hdr;
9375 struct elf_link_hash_entry *h;
9376 bfd_boolean rel_reloc;
9378 rel_reloc = (NEWABI_P (input_bfd)
9379 && mips_elf_rel_relocation_p (input_bfd, input_section,
9381 /* Find the relocation howto for this relocation. */
9382 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9384 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9385 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9386 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9388 sec = local_sections[r_symndx];
9393 unsigned long extsymoff;
9396 if (!elf_bad_symtab (input_bfd))
9397 extsymoff = symtab_hdr->sh_info;
9398 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9399 while (h->root.type == bfd_link_hash_indirect
9400 || h->root.type == bfd_link_hash_warning)
9401 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9404 if (h->root.type == bfd_link_hash_defined
9405 || h->root.type == bfd_link_hash_defweak)
9406 sec = h->root.u.def.section;
9409 if (sec != NULL && discarded_section (sec))
9411 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9412 input_section, &rel, &relend,
9413 rel_reloc, howto, contents);
9417 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9419 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9420 64-bit code, but make sure all their addresses are in the
9421 lowermost or uppermost 32-bit section of the 64-bit address
9422 space. Thus, when they use an R_MIPS_64 they mean what is
9423 usually meant by R_MIPS_32, with the exception that the
9424 stored value is sign-extended to 64 bits. */
9425 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9427 /* On big-endian systems, we need to lie about the position
9429 if (bfd_big_endian (input_bfd))
9433 if (!use_saved_addend_p)
9435 /* If these relocations were originally of the REL variety,
9436 we must pull the addend out of the field that will be
9437 relocated. Otherwise, we simply use the contents of the
9439 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9442 rela_relocation_p = FALSE;
9443 addend = mips_elf_read_rel_addend (input_bfd, rel,
9445 if (hi16_reloc_p (r_type)
9446 || (got16_reloc_p (r_type)
9447 && mips_elf_local_relocation_p (input_bfd, rel,
9450 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9454 name = h->root.root.string;
9456 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9457 local_syms + r_symndx,
9459 (*_bfd_error_handler)
9460 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9461 input_bfd, input_section, name, howto->name,
9466 addend <<= howto->rightshift;
9469 addend = rel->r_addend;
9470 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9471 local_syms, local_sections, rel);
9474 if (info->relocatable)
9476 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9477 && bfd_big_endian (input_bfd))
9480 if (!rela_relocation_p && rel->r_addend)
9482 addend += rel->r_addend;
9483 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9484 addend = mips_elf_high (addend);
9485 else if (r_type == R_MIPS_HIGHER)
9486 addend = mips_elf_higher (addend);
9487 else if (r_type == R_MIPS_HIGHEST)
9488 addend = mips_elf_highest (addend);
9490 addend >>= howto->rightshift;
9492 /* We use the source mask, rather than the destination
9493 mask because the place to which we are writing will be
9494 source of the addend in the final link. */
9495 addend &= howto->src_mask;
9497 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9498 /* See the comment above about using R_MIPS_64 in the 32-bit
9499 ABI. Here, we need to update the addend. It would be
9500 possible to get away with just using the R_MIPS_32 reloc
9501 but for endianness. */
9507 if (addend & ((bfd_vma) 1 << 31))
9509 sign_bits = ((bfd_vma) 1 << 32) - 1;
9516 /* If we don't know that we have a 64-bit type,
9517 do two separate stores. */
9518 if (bfd_big_endian (input_bfd))
9520 /* Store the sign-bits (which are most significant)
9522 low_bits = sign_bits;
9528 high_bits = sign_bits;
9530 bfd_put_32 (input_bfd, low_bits,
9531 contents + rel->r_offset);
9532 bfd_put_32 (input_bfd, high_bits,
9533 contents + rel->r_offset + 4);
9537 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9538 input_bfd, input_section,
9543 /* Go on to the next relocation. */
9547 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9548 relocations for the same offset. In that case we are
9549 supposed to treat the output of each relocation as the addend
9551 if (rel + 1 < relend
9552 && rel->r_offset == rel[1].r_offset
9553 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9554 use_saved_addend_p = TRUE;
9556 use_saved_addend_p = FALSE;
9558 /* Figure out what value we are supposed to relocate. */
9559 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9560 input_section, info, rel,
9561 addend, howto, local_syms,
9562 local_sections, &value,
9563 &name, &cross_mode_jump_p,
9564 use_saved_addend_p))
9566 case bfd_reloc_continue:
9567 /* There's nothing to do. */
9570 case bfd_reloc_undefined:
9571 /* mips_elf_calculate_relocation already called the
9572 undefined_symbol callback. There's no real point in
9573 trying to perform the relocation at this point, so we
9574 just skip ahead to the next relocation. */
9577 case bfd_reloc_notsupported:
9578 msg = _("internal error: unsupported relocation error");
9579 info->callbacks->warning
9580 (info, msg, name, input_bfd, input_section, rel->r_offset);
9583 case bfd_reloc_overflow:
9584 if (use_saved_addend_p)
9585 /* Ignore overflow until we reach the last relocation for
9586 a given location. */
9590 struct mips_elf_link_hash_table *htab;
9592 htab = mips_elf_hash_table (info);
9593 BFD_ASSERT (htab != NULL);
9594 BFD_ASSERT (name != NULL);
9595 if (!htab->small_data_overflow_reported
9596 && (gprel16_reloc_p (howto->type)
9597 || literal_reloc_p (howto->type)))
9599 msg = _("small-data section exceeds 64KB;"
9600 " lower small-data size limit (see option -G)");
9602 htab->small_data_overflow_reported = TRUE;
9603 (*info->callbacks->einfo) ("%P: %s\n", msg);
9605 if (! ((*info->callbacks->reloc_overflow)
9606 (info, NULL, name, howto->name, (bfd_vma) 0,
9607 input_bfd, input_section, rel->r_offset)))
9615 case bfd_reloc_outofrange:
9616 if (jal_reloc_p (howto->type))
9618 msg = _("JALX to a non-word-aligned address");
9619 info->callbacks->warning
9620 (info, msg, name, input_bfd, input_section, rel->r_offset);
9630 /* If we've got another relocation for the address, keep going
9631 until we reach the last one. */
9632 if (use_saved_addend_p)
9638 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9639 /* See the comment above about using R_MIPS_64 in the 32-bit
9640 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9641 that calculated the right value. Now, however, we
9642 sign-extend the 32-bit result to 64-bits, and store it as a
9643 64-bit value. We are especially generous here in that we
9644 go to extreme lengths to support this usage on systems with
9645 only a 32-bit VMA. */
9651 if (value & ((bfd_vma) 1 << 31))
9653 sign_bits = ((bfd_vma) 1 << 32) - 1;
9660 /* If we don't know that we have a 64-bit type,
9661 do two separate stores. */
9662 if (bfd_big_endian (input_bfd))
9664 /* Undo what we did above. */
9666 /* Store the sign-bits (which are most significant)
9668 low_bits = sign_bits;
9674 high_bits = sign_bits;
9676 bfd_put_32 (input_bfd, low_bits,
9677 contents + rel->r_offset);
9678 bfd_put_32 (input_bfd, high_bits,
9679 contents + rel->r_offset + 4);
9683 /* Actually perform the relocation. */
9684 if (! mips_elf_perform_relocation (info, howto, rel, value,
9685 input_bfd, input_section,
9686 contents, cross_mode_jump_p))
9693 /* A function that iterates over each entry in la25_stubs and fills
9694 in the code for each one. DATA points to a mips_htab_traverse_info. */
9697 mips_elf_create_la25_stub (void **slot, void *data)
9699 struct mips_htab_traverse_info *hti;
9700 struct mips_elf_link_hash_table *htab;
9701 struct mips_elf_la25_stub *stub;
9704 bfd_vma offset, target, target_high, target_low;
9706 stub = (struct mips_elf_la25_stub *) *slot;
9707 hti = (struct mips_htab_traverse_info *) data;
9708 htab = mips_elf_hash_table (hti->info);
9709 BFD_ASSERT (htab != NULL);
9711 /* Create the section contents, if we haven't already. */
9712 s = stub->stub_section;
9716 loc = bfd_malloc (s->size);
9725 /* Work out where in the section this stub should go. */
9726 offset = stub->offset;
9728 /* Work out the target address. */
9729 target = mips_elf_get_la25_target (stub, &s);
9730 target += s->output_section->vma + s->output_offset;
9732 target_high = ((target + 0x8000) >> 16) & 0xffff;
9733 target_low = (target & 0xffff);
9735 if (stub->stub_section != htab->strampoline)
9737 /* This is a simple LUI/ADDIU stub. Zero out the beginning
9738 of the section and write the two instructions at the end. */
9739 memset (loc, 0, offset);
9741 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9743 bfd_put_micromips_32 (hti->output_bfd,
9744 LA25_LUI_MICROMIPS (target_high),
9746 bfd_put_micromips_32 (hti->output_bfd,
9747 LA25_ADDIU_MICROMIPS (target_low),
9752 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9753 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9758 /* This is trampoline. */
9760 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9762 bfd_put_micromips_32 (hti->output_bfd,
9763 LA25_LUI_MICROMIPS (target_high), loc);
9764 bfd_put_micromips_32 (hti->output_bfd,
9765 LA25_J_MICROMIPS (target), loc + 4);
9766 bfd_put_micromips_32 (hti->output_bfd,
9767 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
9768 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9772 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9773 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9774 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9775 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9781 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9782 adjust it appropriately now. */
9785 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9786 const char *name, Elf_Internal_Sym *sym)
9788 /* The linker script takes care of providing names and values for
9789 these, but we must place them into the right sections. */
9790 static const char* const text_section_symbols[] = {
9793 "__dso_displacement",
9795 "__program_header_table",
9799 static const char* const data_section_symbols[] = {
9807 const char* const *p;
9810 for (i = 0; i < 2; ++i)
9811 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9814 if (strcmp (*p, name) == 0)
9816 /* All of these symbols are given type STT_SECTION by the
9818 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9819 sym->st_other = STO_PROTECTED;
9821 /* The IRIX linker puts these symbols in special sections. */
9823 sym->st_shndx = SHN_MIPS_TEXT;
9825 sym->st_shndx = SHN_MIPS_DATA;
9831 /* Finish up dynamic symbol handling. We set the contents of various
9832 dynamic sections here. */
9835 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9836 struct bfd_link_info *info,
9837 struct elf_link_hash_entry *h,
9838 Elf_Internal_Sym *sym)
9842 struct mips_got_info *g, *gg;
9845 struct mips_elf_link_hash_table *htab;
9846 struct mips_elf_link_hash_entry *hmips;
9848 htab = mips_elf_hash_table (info);
9849 BFD_ASSERT (htab != NULL);
9850 dynobj = elf_hash_table (info)->dynobj;
9851 hmips = (struct mips_elf_link_hash_entry *) h;
9853 BFD_ASSERT (!htab->is_vxworks);
9855 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9857 /* We've decided to create a PLT entry for this symbol. */
9859 bfd_vma header_address, plt_index, got_address;
9860 bfd_vma got_address_high, got_address_low, load;
9861 const bfd_vma *plt_entry;
9863 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9864 BFD_ASSERT (h->dynindx != -1);
9865 BFD_ASSERT (htab->splt != NULL);
9866 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9867 BFD_ASSERT (!h->def_regular);
9869 /* Calculate the address of the PLT header. */
9870 header_address = (htab->splt->output_section->vma
9871 + htab->splt->output_offset);
9873 /* Calculate the index of the entry. */
9874 plt_index = ((h->plt.offset - htab->plt_header_size)
9875 / htab->plt_entry_size);
9877 /* Calculate the address of the .got.plt entry. */
9878 got_address = (htab->sgotplt->output_section->vma
9879 + htab->sgotplt->output_offset
9880 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9881 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9882 got_address_low = got_address & 0xffff;
9884 /* Initially point the .got.plt entry at the PLT header. */
9885 loc = (htab->sgotplt->contents
9886 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9887 if (ABI_64_P (output_bfd))
9888 bfd_put_64 (output_bfd, header_address, loc);
9890 bfd_put_32 (output_bfd, header_address, loc);
9892 /* Find out where the .plt entry should go. */
9893 loc = htab->splt->contents + h->plt.offset;
9895 /* Pick the load opcode. */
9896 load = MIPS_ELF_LOAD_WORD (output_bfd);
9898 /* Fill in the PLT entry itself. */
9899 plt_entry = mips_exec_plt_entry;
9900 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9901 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9903 if (! LOAD_INTERLOCKS_P (output_bfd))
9905 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9906 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9910 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9911 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9914 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9915 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9916 plt_index, h->dynindx,
9917 R_MIPS_JUMP_SLOT, got_address);
9919 /* We distinguish between PLT entries and lazy-binding stubs by
9920 giving the former an st_other value of STO_MIPS_PLT. Set the
9921 flag and leave the value if there are any relocations in the
9922 binary where pointer equality matters. */
9923 sym->st_shndx = SHN_UNDEF;
9924 if (h->pointer_equality_needed)
9925 sym->st_other = STO_MIPS_PLT;
9929 else if (h->plt.offset != MINUS_ONE)
9931 /* We've decided to create a lazy-binding stub. */
9932 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9934 /* This symbol has a stub. Set it up. */
9936 BFD_ASSERT (h->dynindx != -1);
9938 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9939 || (h->dynindx <= 0xffff));
9941 /* Values up to 2^31 - 1 are allowed. Larger values would cause
9942 sign extension at runtime in the stub, resulting in a negative
9944 if (h->dynindx & ~0x7fffffff)
9947 /* Fill the stub. */
9949 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9951 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9953 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9955 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9959 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9962 /* If a large stub is not required and sign extension is not a
9963 problem, then use legacy code in the stub. */
9964 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9965 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9966 else if (h->dynindx & ~0x7fff)
9967 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9969 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9972 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9973 memcpy (htab->sstubs->contents + h->plt.offset,
9974 stub, htab->function_stub_size);
9976 /* Mark the symbol as undefined. plt.offset != -1 occurs
9977 only for the referenced symbol. */
9978 sym->st_shndx = SHN_UNDEF;
9980 /* The run-time linker uses the st_value field of the symbol
9981 to reset the global offset table entry for this external
9982 to its stub address when unlinking a shared object. */
9983 sym->st_value = (htab->sstubs->output_section->vma
9984 + htab->sstubs->output_offset
9988 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9989 refer to the stub, since only the stub uses the standard calling
9991 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9993 BFD_ASSERT (hmips->need_fn_stub);
9994 sym->st_value = (hmips->fn_stub->output_section->vma
9995 + hmips->fn_stub->output_offset);
9996 sym->st_size = hmips->fn_stub->size;
9997 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10000 BFD_ASSERT (h->dynindx != -1
10001 || h->forced_local);
10004 g = htab->got_info;
10005 BFD_ASSERT (g != NULL);
10007 /* Run through the global symbol table, creating GOT entries for all
10008 the symbols that need them. */
10009 if (hmips->global_got_area != GGA_NONE)
10014 value = sym->st_value;
10015 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10016 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10019 if (hmips->global_got_area != GGA_NONE && g->next)
10021 struct mips_got_entry e, *p;
10027 e.abfd = output_bfd;
10032 for (g = g->next; g->next != gg; g = g->next)
10035 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10038 offset = p->gotidx;
10040 || (elf_hash_table (info)->dynamic_sections_created
10042 && p->d.h->root.def_dynamic
10043 && !p->d.h->root.def_regular))
10045 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10046 the various compatibility problems, it's easier to mock
10047 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10048 mips_elf_create_dynamic_relocation to calculate the
10049 appropriate addend. */
10050 Elf_Internal_Rela rel[3];
10052 memset (rel, 0, sizeof (rel));
10053 if (ABI_64_P (output_bfd))
10054 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10056 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10057 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10060 if (! (mips_elf_create_dynamic_relocation
10061 (output_bfd, info, rel,
10062 e.d.h, NULL, sym->st_value, &entry, sgot)))
10066 entry = sym->st_value;
10067 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10072 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10073 name = h->root.root.string;
10074 if (h == elf_hash_table (info)->hdynamic
10075 || h == elf_hash_table (info)->hgot)
10076 sym->st_shndx = SHN_ABS;
10077 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10078 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10080 sym->st_shndx = SHN_ABS;
10081 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10084 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10086 sym->st_shndx = SHN_ABS;
10087 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10088 sym->st_value = elf_gp (output_bfd);
10090 else if (SGI_COMPAT (output_bfd))
10092 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10093 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10095 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10096 sym->st_other = STO_PROTECTED;
10098 sym->st_shndx = SHN_MIPS_DATA;
10100 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10102 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10103 sym->st_other = STO_PROTECTED;
10104 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10105 sym->st_shndx = SHN_ABS;
10107 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10109 if (h->type == STT_FUNC)
10110 sym->st_shndx = SHN_MIPS_TEXT;
10111 else if (h->type == STT_OBJECT)
10112 sym->st_shndx = SHN_MIPS_DATA;
10116 /* Emit a copy reloc, if needed. */
10122 BFD_ASSERT (h->dynindx != -1);
10123 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10125 s = mips_elf_rel_dyn_section (info, FALSE);
10126 symval = (h->root.u.def.section->output_section->vma
10127 + h->root.u.def.section->output_offset
10128 + h->root.u.def.value);
10129 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10130 h->dynindx, R_MIPS_COPY, symval);
10133 /* Handle the IRIX6-specific symbols. */
10134 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10135 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10137 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
10138 treat MIPS16 symbols like any other. */
10139 if (ELF_ST_IS_MIPS16 (sym->st_other))
10141 BFD_ASSERT (sym->st_value & 1);
10142 sym->st_other -= STO_MIPS16;
10148 /* Likewise, for VxWorks. */
10151 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10152 struct bfd_link_info *info,
10153 struct elf_link_hash_entry *h,
10154 Elf_Internal_Sym *sym)
10158 struct mips_got_info *g;
10159 struct mips_elf_link_hash_table *htab;
10160 struct mips_elf_link_hash_entry *hmips;
10162 htab = mips_elf_hash_table (info);
10163 BFD_ASSERT (htab != NULL);
10164 dynobj = elf_hash_table (info)->dynobj;
10165 hmips = (struct mips_elf_link_hash_entry *) h;
10167 if (h->plt.offset != (bfd_vma) -1)
10170 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
10171 Elf_Internal_Rela rel;
10172 static const bfd_vma *plt_entry;
10174 BFD_ASSERT (h->dynindx != -1);
10175 BFD_ASSERT (htab->splt != NULL);
10176 BFD_ASSERT (h->plt.offset <= htab->splt->size);
10178 /* Calculate the address of the .plt entry. */
10179 plt_address = (htab->splt->output_section->vma
10180 + htab->splt->output_offset
10183 /* Calculate the index of the entry. */
10184 plt_index = ((h->plt.offset - htab->plt_header_size)
10185 / htab->plt_entry_size);
10187 /* Calculate the address of the .got.plt entry. */
10188 got_address = (htab->sgotplt->output_section->vma
10189 + htab->sgotplt->output_offset
10192 /* Calculate the offset of the .got.plt entry from
10193 _GLOBAL_OFFSET_TABLE_. */
10194 got_offset = mips_elf_gotplt_index (info, h);
10196 /* Calculate the offset for the branch at the start of the PLT
10197 entry. The branch jumps to the beginning of .plt. */
10198 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
10200 /* Fill in the initial value of the .got.plt entry. */
10201 bfd_put_32 (output_bfd, plt_address,
10202 htab->sgotplt->contents + plt_index * 4);
10204 /* Find out where the .plt entry should go. */
10205 loc = htab->splt->contents + h->plt.offset;
10209 plt_entry = mips_vxworks_shared_plt_entry;
10210 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10211 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10215 bfd_vma got_address_high, got_address_low;
10217 plt_entry = mips_vxworks_exec_plt_entry;
10218 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10219 got_address_low = got_address & 0xffff;
10221 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10222 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10223 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10224 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10225 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10226 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10227 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10228 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10230 loc = (htab->srelplt2->contents
10231 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10233 /* Emit a relocation for the .got.plt entry. */
10234 rel.r_offset = got_address;
10235 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10236 rel.r_addend = h->plt.offset;
10237 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10239 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10240 loc += sizeof (Elf32_External_Rela);
10241 rel.r_offset = plt_address + 8;
10242 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10243 rel.r_addend = got_offset;
10244 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10246 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10247 loc += sizeof (Elf32_External_Rela);
10249 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10250 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10253 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10254 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
10255 rel.r_offset = got_address;
10256 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10258 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10260 if (!h->def_regular)
10261 sym->st_shndx = SHN_UNDEF;
10264 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10267 g = htab->got_info;
10268 BFD_ASSERT (g != NULL);
10270 /* See if this symbol has an entry in the GOT. */
10271 if (hmips->global_got_area != GGA_NONE)
10274 Elf_Internal_Rela outrel;
10278 /* Install the symbol value in the GOT. */
10279 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10280 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10282 /* Add a dynamic relocation for it. */
10283 s = mips_elf_rel_dyn_section (info, FALSE);
10284 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10285 outrel.r_offset = (sgot->output_section->vma
10286 + sgot->output_offset
10288 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10289 outrel.r_addend = 0;
10290 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10293 /* Emit a copy reloc, if needed. */
10296 Elf_Internal_Rela rel;
10298 BFD_ASSERT (h->dynindx != -1);
10300 rel.r_offset = (h->root.u.def.section->output_section->vma
10301 + h->root.u.def.section->output_offset
10302 + h->root.u.def.value);
10303 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10305 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10306 htab->srelbss->contents
10307 + (htab->srelbss->reloc_count
10308 * sizeof (Elf32_External_Rela)));
10309 ++htab->srelbss->reloc_count;
10312 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10313 if (ELF_ST_IS_COMPRESSED (sym->st_other))
10314 sym->st_value &= ~1;
10319 /* Write out a plt0 entry to the beginning of .plt. */
10322 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10325 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10326 static const bfd_vma *plt_entry;
10327 struct mips_elf_link_hash_table *htab;
10329 htab = mips_elf_hash_table (info);
10330 BFD_ASSERT (htab != NULL);
10332 if (ABI_64_P (output_bfd))
10333 plt_entry = mips_n64_exec_plt0_entry;
10334 else if (ABI_N32_P (output_bfd))
10335 plt_entry = mips_n32_exec_plt0_entry;
10337 plt_entry = mips_o32_exec_plt0_entry;
10339 /* Calculate the value of .got.plt. */
10340 gotplt_value = (htab->sgotplt->output_section->vma
10341 + htab->sgotplt->output_offset);
10342 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10343 gotplt_value_low = gotplt_value & 0xffff;
10345 /* The PLT sequence is not safe for N64 if .got.plt's address can
10346 not be loaded in two instructions. */
10347 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10348 || ~(gotplt_value | 0x7fffffff) == 0);
10350 /* Install the PLT header. */
10351 loc = htab->splt->contents;
10352 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10353 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10354 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10355 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10356 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10357 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10358 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10359 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10362 /* Install the PLT header for a VxWorks executable and finalize the
10363 contents of .rela.plt.unloaded. */
10366 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10368 Elf_Internal_Rela rela;
10370 bfd_vma got_value, got_value_high, got_value_low, plt_address;
10371 static const bfd_vma *plt_entry;
10372 struct mips_elf_link_hash_table *htab;
10374 htab = mips_elf_hash_table (info);
10375 BFD_ASSERT (htab != NULL);
10377 plt_entry = mips_vxworks_exec_plt0_entry;
10379 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
10380 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
10381 + htab->root.hgot->root.u.def.section->output_offset
10382 + htab->root.hgot->root.u.def.value);
10384 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
10385 got_value_low = got_value & 0xffff;
10387 /* Calculate the address of the PLT header. */
10388 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
10390 /* Install the PLT header. */
10391 loc = htab->splt->contents;
10392 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
10393 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
10394 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10395 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10396 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10397 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10399 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
10400 loc = htab->srelplt2->contents;
10401 rela.r_offset = plt_address;
10402 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10404 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10405 loc += sizeof (Elf32_External_Rela);
10407 /* Output the relocation for the following addiu of
10408 %lo(_GLOBAL_OFFSET_TABLE_). */
10409 rela.r_offset += 4;
10410 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10411 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10412 loc += sizeof (Elf32_External_Rela);
10414 /* Fix up the remaining relocations. They may have the wrong
10415 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10416 in which symbols were output. */
10417 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10419 Elf_Internal_Rela rel;
10421 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10422 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10423 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10424 loc += sizeof (Elf32_External_Rela);
10426 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10427 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10428 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10429 loc += sizeof (Elf32_External_Rela);
10431 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10432 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10433 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10434 loc += sizeof (Elf32_External_Rela);
10438 /* Install the PLT header for a VxWorks shared library. */
10441 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10444 struct mips_elf_link_hash_table *htab;
10446 htab = mips_elf_hash_table (info);
10447 BFD_ASSERT (htab != NULL);
10449 /* We just need to copy the entry byte-by-byte. */
10450 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10451 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10452 htab->splt->contents + i * 4);
10455 /* Finish up the dynamic sections. */
10458 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10459 struct bfd_link_info *info)
10464 struct mips_got_info *gg, *g;
10465 struct mips_elf_link_hash_table *htab;
10467 htab = mips_elf_hash_table (info);
10468 BFD_ASSERT (htab != NULL);
10470 dynobj = elf_hash_table (info)->dynobj;
10472 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
10475 gg = htab->got_info;
10477 if (elf_hash_table (info)->dynamic_sections_created)
10480 int dyn_to_skip = 0, dyn_skipped = 0;
10482 BFD_ASSERT (sdyn != NULL);
10483 BFD_ASSERT (gg != NULL);
10485 g = mips_elf_bfd_got (output_bfd, FALSE);
10486 BFD_ASSERT (g != NULL);
10488 for (b = sdyn->contents;
10489 b < sdyn->contents + sdyn->size;
10490 b += MIPS_ELF_DYN_SIZE (dynobj))
10492 Elf_Internal_Dyn dyn;
10496 bfd_boolean swap_out_p;
10498 /* Read in the current dynamic entry. */
10499 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10501 /* Assume that we're going to modify it and write it out. */
10507 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10511 BFD_ASSERT (htab->is_vxworks);
10512 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10516 /* Rewrite DT_STRSZ. */
10518 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10523 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10526 case DT_MIPS_PLTGOT:
10528 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10531 case DT_MIPS_RLD_VERSION:
10532 dyn.d_un.d_val = 1; /* XXX */
10535 case DT_MIPS_FLAGS:
10536 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10539 case DT_MIPS_TIME_STAMP:
10543 dyn.d_un.d_val = t;
10547 case DT_MIPS_ICHECKSUM:
10549 swap_out_p = FALSE;
10552 case DT_MIPS_IVERSION:
10554 swap_out_p = FALSE;
10557 case DT_MIPS_BASE_ADDRESS:
10558 s = output_bfd->sections;
10559 BFD_ASSERT (s != NULL);
10560 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10563 case DT_MIPS_LOCAL_GOTNO:
10564 dyn.d_un.d_val = g->local_gotno;
10567 case DT_MIPS_UNREFEXTNO:
10568 /* The index into the dynamic symbol table which is the
10569 entry of the first external symbol that is not
10570 referenced within the same object. */
10571 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10574 case DT_MIPS_GOTSYM:
10575 if (htab->global_gotsym)
10577 dyn.d_un.d_val = htab->global_gotsym->dynindx;
10580 /* In case if we don't have global got symbols we default
10581 to setting DT_MIPS_GOTSYM to the same value as
10582 DT_MIPS_SYMTABNO, so we just fall through. */
10584 case DT_MIPS_SYMTABNO:
10586 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10587 s = bfd_get_section_by_name (output_bfd, name);
10588 BFD_ASSERT (s != NULL);
10590 dyn.d_un.d_val = s->size / elemsize;
10593 case DT_MIPS_HIPAGENO:
10594 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10597 case DT_MIPS_RLD_MAP:
10599 struct elf_link_hash_entry *h;
10600 h = mips_elf_hash_table (info)->rld_symbol;
10603 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10604 swap_out_p = FALSE;
10607 s = h->root.u.def.section;
10608 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
10609 + h->root.u.def.value);
10613 case DT_MIPS_OPTIONS:
10614 s = (bfd_get_section_by_name
10615 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10616 dyn.d_un.d_ptr = s->vma;
10620 BFD_ASSERT (htab->is_vxworks);
10621 /* The count does not include the JUMP_SLOT relocations. */
10623 dyn.d_un.d_val -= htab->srelplt->size;
10627 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10628 if (htab->is_vxworks)
10629 dyn.d_un.d_val = DT_RELA;
10631 dyn.d_un.d_val = DT_REL;
10635 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10636 dyn.d_un.d_val = htab->srelplt->size;
10640 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10641 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10642 + htab->srelplt->output_offset);
10646 /* If we didn't need any text relocations after all, delete
10647 the dynamic tag. */
10648 if (!(info->flags & DF_TEXTREL))
10650 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10651 swap_out_p = FALSE;
10656 /* If we didn't need any text relocations after all, clear
10657 DF_TEXTREL from DT_FLAGS. */
10658 if (!(info->flags & DF_TEXTREL))
10659 dyn.d_un.d_val &= ~DF_TEXTREL;
10661 swap_out_p = FALSE;
10665 swap_out_p = FALSE;
10666 if (htab->is_vxworks
10667 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10672 if (swap_out_p || dyn_skipped)
10673 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10674 (dynobj, &dyn, b - dyn_skipped);
10678 dyn_skipped += dyn_to_skip;
10683 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10684 if (dyn_skipped > 0)
10685 memset (b - dyn_skipped, 0, dyn_skipped);
10688 if (sgot != NULL && sgot->size > 0
10689 && !bfd_is_abs_section (sgot->output_section))
10691 if (htab->is_vxworks)
10693 /* The first entry of the global offset table points to the
10694 ".dynamic" section. The second is initialized by the
10695 loader and contains the shared library identifier.
10696 The third is also initialized by the loader and points
10697 to the lazy resolution stub. */
10698 MIPS_ELF_PUT_WORD (output_bfd,
10699 sdyn->output_offset + sdyn->output_section->vma,
10701 MIPS_ELF_PUT_WORD (output_bfd, 0,
10702 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10703 MIPS_ELF_PUT_WORD (output_bfd, 0,
10705 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10709 /* The first entry of the global offset table will be filled at
10710 runtime. The second entry will be used by some runtime loaders.
10711 This isn't the case of IRIX rld. */
10712 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10713 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10714 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10717 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10718 = MIPS_ELF_GOT_SIZE (output_bfd);
10721 /* Generate dynamic relocations for the non-primary gots. */
10722 if (gg != NULL && gg->next)
10724 Elf_Internal_Rela rel[3];
10725 bfd_vma addend = 0;
10727 memset (rel, 0, sizeof (rel));
10728 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10730 for (g = gg->next; g->next != gg; g = g->next)
10732 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
10733 + g->next->tls_gotno;
10735 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10736 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10737 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10739 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10741 if (! info->shared)
10744 while (got_index < g->assigned_gotno)
10746 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10747 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10748 if (!(mips_elf_create_dynamic_relocation
10749 (output_bfd, info, rel, NULL,
10750 bfd_abs_section_ptr,
10751 0, &addend, sgot)))
10753 BFD_ASSERT (addend == 0);
10758 /* The generation of dynamic relocations for the non-primary gots
10759 adds more dynamic relocations. We cannot count them until
10762 if (elf_hash_table (info)->dynamic_sections_created)
10765 bfd_boolean swap_out_p;
10767 BFD_ASSERT (sdyn != NULL);
10769 for (b = sdyn->contents;
10770 b < sdyn->contents + sdyn->size;
10771 b += MIPS_ELF_DYN_SIZE (dynobj))
10773 Elf_Internal_Dyn dyn;
10776 /* Read in the current dynamic entry. */
10777 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10779 /* Assume that we're going to modify it and write it out. */
10785 /* Reduce DT_RELSZ to account for any relocations we
10786 decided not to make. This is for the n64 irix rld,
10787 which doesn't seem to apply any relocations if there
10788 are trailing null entries. */
10789 s = mips_elf_rel_dyn_section (info, FALSE);
10790 dyn.d_un.d_val = (s->reloc_count
10791 * (ABI_64_P (output_bfd)
10792 ? sizeof (Elf64_Mips_External_Rel)
10793 : sizeof (Elf32_External_Rel)));
10794 /* Adjust the section size too. Tools like the prelinker
10795 can reasonably expect the values to the same. */
10796 elf_section_data (s->output_section)->this_hdr.sh_size
10801 swap_out_p = FALSE;
10806 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10813 Elf32_compact_rel cpt;
10815 if (SGI_COMPAT (output_bfd))
10817 /* Write .compact_rel section out. */
10818 s = bfd_get_linker_section (dynobj, ".compact_rel");
10822 cpt.num = s->reloc_count;
10824 cpt.offset = (s->output_section->filepos
10825 + sizeof (Elf32_External_compact_rel));
10828 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10829 ((Elf32_External_compact_rel *)
10832 /* Clean up a dummy stub function entry in .text. */
10833 if (htab->sstubs != NULL)
10835 file_ptr dummy_offset;
10837 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10838 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10839 memset (htab->sstubs->contents + dummy_offset, 0,
10840 htab->function_stub_size);
10845 /* The psABI says that the dynamic relocations must be sorted in
10846 increasing order of r_symndx. The VxWorks EABI doesn't require
10847 this, and because the code below handles REL rather than RELA
10848 relocations, using it for VxWorks would be outright harmful. */
10849 if (!htab->is_vxworks)
10851 s = mips_elf_rel_dyn_section (info, FALSE);
10853 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10855 reldyn_sorting_bfd = output_bfd;
10857 if (ABI_64_P (output_bfd))
10858 qsort ((Elf64_External_Rel *) s->contents + 1,
10859 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10860 sort_dynamic_relocs_64);
10862 qsort ((Elf32_External_Rel *) s->contents + 1,
10863 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10864 sort_dynamic_relocs);
10869 if (htab->splt && htab->splt->size > 0)
10871 if (htab->is_vxworks)
10874 mips_vxworks_finish_shared_plt (output_bfd, info);
10876 mips_vxworks_finish_exec_plt (output_bfd, info);
10880 BFD_ASSERT (!info->shared);
10881 mips_finish_exec_plt (output_bfd, info);
10888 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10891 mips_set_isa_flags (bfd *abfd)
10895 switch (bfd_get_mach (abfd))
10898 case bfd_mach_mips3000:
10899 val = E_MIPS_ARCH_1;
10902 case bfd_mach_mips3900:
10903 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10906 case bfd_mach_mips6000:
10907 val = E_MIPS_ARCH_2;
10910 case bfd_mach_mips4000:
10911 case bfd_mach_mips4300:
10912 case bfd_mach_mips4400:
10913 case bfd_mach_mips4600:
10914 val = E_MIPS_ARCH_3;
10917 case bfd_mach_mips4010:
10918 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10921 case bfd_mach_mips4100:
10922 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10925 case bfd_mach_mips4111:
10926 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10929 case bfd_mach_mips4120:
10930 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10933 case bfd_mach_mips4650:
10934 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10937 case bfd_mach_mips5400:
10938 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10941 case bfd_mach_mips5500:
10942 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10945 case bfd_mach_mips5900:
10946 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
10949 case bfd_mach_mips9000:
10950 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10953 case bfd_mach_mips5000:
10954 case bfd_mach_mips7000:
10955 case bfd_mach_mips8000:
10956 case bfd_mach_mips10000:
10957 case bfd_mach_mips12000:
10958 case bfd_mach_mips14000:
10959 case bfd_mach_mips16000:
10960 val = E_MIPS_ARCH_4;
10963 case bfd_mach_mips5:
10964 val = E_MIPS_ARCH_5;
10967 case bfd_mach_mips_loongson_2e:
10968 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10971 case bfd_mach_mips_loongson_2f:
10972 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10975 case bfd_mach_mips_sb1:
10976 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10979 case bfd_mach_mips_loongson_3a:
10980 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
10983 case bfd_mach_mips_octeon:
10984 case bfd_mach_mips_octeonp:
10985 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10988 case bfd_mach_mips_xlr:
10989 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10992 case bfd_mach_mips_octeon2:
10993 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
10996 case bfd_mach_mipsisa32:
10997 val = E_MIPS_ARCH_32;
11000 case bfd_mach_mipsisa64:
11001 val = E_MIPS_ARCH_64;
11004 case bfd_mach_mipsisa32r2:
11005 val = E_MIPS_ARCH_32R2;
11008 case bfd_mach_mipsisa64r2:
11009 val = E_MIPS_ARCH_64R2;
11012 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11013 elf_elfheader (abfd)->e_flags |= val;
11018 /* The final processing done just before writing out a MIPS ELF object
11019 file. This gets the MIPS architecture right based on the machine
11020 number. This is used by both the 32-bit and the 64-bit ABI. */
11023 _bfd_mips_elf_final_write_processing (bfd *abfd,
11024 bfd_boolean linker ATTRIBUTE_UNUSED)
11027 Elf_Internal_Shdr **hdrpp;
11031 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11032 is nonzero. This is for compatibility with old objects, which used
11033 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11034 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11035 mips_set_isa_flags (abfd);
11037 /* Set the sh_info field for .gptab sections and other appropriate
11038 info for each special section. */
11039 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11040 i < elf_numsections (abfd);
11043 switch ((*hdrpp)->sh_type)
11045 case SHT_MIPS_MSYM:
11046 case SHT_MIPS_LIBLIST:
11047 sec = bfd_get_section_by_name (abfd, ".dynstr");
11049 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11052 case SHT_MIPS_GPTAB:
11053 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11054 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11055 BFD_ASSERT (name != NULL
11056 && CONST_STRNEQ (name, ".gptab."));
11057 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11058 BFD_ASSERT (sec != NULL);
11059 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11062 case SHT_MIPS_CONTENT:
11063 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11064 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11065 BFD_ASSERT (name != NULL
11066 && CONST_STRNEQ (name, ".MIPS.content"));
11067 sec = bfd_get_section_by_name (abfd,
11068 name + sizeof ".MIPS.content" - 1);
11069 BFD_ASSERT (sec != NULL);
11070 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11073 case SHT_MIPS_SYMBOL_LIB:
11074 sec = bfd_get_section_by_name (abfd, ".dynsym");
11076 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11077 sec = bfd_get_section_by_name (abfd, ".liblist");
11079 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11082 case SHT_MIPS_EVENTS:
11083 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11084 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11085 BFD_ASSERT (name != NULL);
11086 if (CONST_STRNEQ (name, ".MIPS.events"))
11087 sec = bfd_get_section_by_name (abfd,
11088 name + sizeof ".MIPS.events" - 1);
11091 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
11092 sec = bfd_get_section_by_name (abfd,
11094 + sizeof ".MIPS.post_rel" - 1));
11096 BFD_ASSERT (sec != NULL);
11097 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11104 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11108 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11109 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11114 /* See if we need a PT_MIPS_REGINFO segment. */
11115 s = bfd_get_section_by_name (abfd, ".reginfo");
11116 if (s && (s->flags & SEC_LOAD))
11119 /* See if we need a PT_MIPS_OPTIONS segment. */
11120 if (IRIX_COMPAT (abfd) == ict_irix6
11121 && bfd_get_section_by_name (abfd,
11122 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11125 /* See if we need a PT_MIPS_RTPROC segment. */
11126 if (IRIX_COMPAT (abfd) == ict_irix5
11127 && bfd_get_section_by_name (abfd, ".dynamic")
11128 && bfd_get_section_by_name (abfd, ".mdebug"))
11131 /* Allocate a PT_NULL header in dynamic objects. See
11132 _bfd_mips_elf_modify_segment_map for details. */
11133 if (!SGI_COMPAT (abfd)
11134 && bfd_get_section_by_name (abfd, ".dynamic"))
11140 /* Modify the segment map for an IRIX5 executable. */
11143 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11144 struct bfd_link_info *info)
11147 struct elf_segment_map *m, **pm;
11150 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11152 s = bfd_get_section_by_name (abfd, ".reginfo");
11153 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11155 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11156 if (m->p_type == PT_MIPS_REGINFO)
11161 m = bfd_zalloc (abfd, amt);
11165 m->p_type = PT_MIPS_REGINFO;
11167 m->sections[0] = s;
11169 /* We want to put it after the PHDR and INTERP segments. */
11170 pm = &elf_tdata (abfd)->segment_map;
11172 && ((*pm)->p_type == PT_PHDR
11173 || (*pm)->p_type == PT_INTERP))
11181 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11182 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11183 PT_MIPS_OPTIONS segment immediately following the program header
11185 if (NEWABI_P (abfd)
11186 /* On non-IRIX6 new abi, we'll have already created a segment
11187 for this section, so don't create another. I'm not sure this
11188 is not also the case for IRIX 6, but I can't test it right
11190 && IRIX_COMPAT (abfd) == ict_irix6)
11192 for (s = abfd->sections; s; s = s->next)
11193 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11198 struct elf_segment_map *options_segment;
11200 pm = &elf_tdata (abfd)->segment_map;
11202 && ((*pm)->p_type == PT_PHDR
11203 || (*pm)->p_type == PT_INTERP))
11206 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11208 amt = sizeof (struct elf_segment_map);
11209 options_segment = bfd_zalloc (abfd, amt);
11210 options_segment->next = *pm;
11211 options_segment->p_type = PT_MIPS_OPTIONS;
11212 options_segment->p_flags = PF_R;
11213 options_segment->p_flags_valid = TRUE;
11214 options_segment->count = 1;
11215 options_segment->sections[0] = s;
11216 *pm = options_segment;
11222 if (IRIX_COMPAT (abfd) == ict_irix5)
11224 /* If there are .dynamic and .mdebug sections, we make a room
11225 for the RTPROC header. FIXME: Rewrite without section names. */
11226 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11227 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11228 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11230 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11231 if (m->p_type == PT_MIPS_RTPROC)
11236 m = bfd_zalloc (abfd, amt);
11240 m->p_type = PT_MIPS_RTPROC;
11242 s = bfd_get_section_by_name (abfd, ".rtproc");
11247 m->p_flags_valid = 1;
11252 m->sections[0] = s;
11255 /* We want to put it after the DYNAMIC segment. */
11256 pm = &elf_tdata (abfd)->segment_map;
11257 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11267 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
11268 .dynstr, .dynsym, and .hash sections, and everything in
11270 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
11272 if ((*pm)->p_type == PT_DYNAMIC)
11275 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
11277 /* For a normal mips executable the permissions for the PT_DYNAMIC
11278 segment are read, write and execute. We do that here since
11279 the code in elf.c sets only the read permission. This matters
11280 sometimes for the dynamic linker. */
11281 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
11283 m->p_flags = PF_R | PF_W | PF_X;
11284 m->p_flags_valid = 1;
11287 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11288 glibc's dynamic linker has traditionally derived the number of
11289 tags from the p_filesz field, and sometimes allocates stack
11290 arrays of that size. An overly-big PT_DYNAMIC segment can
11291 be actively harmful in such cases. Making PT_DYNAMIC contain
11292 other sections can also make life hard for the prelinker,
11293 which might move one of the other sections to a different
11294 PT_LOAD segment. */
11295 if (SGI_COMPAT (abfd)
11298 && strcmp (m->sections[0]->name, ".dynamic") == 0)
11300 static const char *sec_names[] =
11302 ".dynamic", ".dynstr", ".dynsym", ".hash"
11306 struct elf_segment_map *n;
11308 low = ~(bfd_vma) 0;
11310 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11312 s = bfd_get_section_by_name (abfd, sec_names[i]);
11313 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11320 if (high < s->vma + sz)
11321 high = s->vma + sz;
11326 for (s = abfd->sections; s != NULL; s = s->next)
11327 if ((s->flags & SEC_LOAD) != 0
11329 && s->vma + s->size <= high)
11332 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
11333 n = bfd_zalloc (abfd, amt);
11340 for (s = abfd->sections; s != NULL; s = s->next)
11342 if ((s->flags & SEC_LOAD) != 0
11344 && s->vma + s->size <= high)
11346 n->sections[i] = s;
11355 /* Allocate a spare program header in dynamic objects so that tools
11356 like the prelinker can add an extra PT_LOAD entry.
11358 If the prelinker needs to make room for a new PT_LOAD entry, its
11359 standard procedure is to move the first (read-only) sections into
11360 the new (writable) segment. However, the MIPS ABI requires
11361 .dynamic to be in a read-only segment, and the section will often
11362 start within sizeof (ElfNN_Phdr) bytes of the last program header.
11364 Although the prelinker could in principle move .dynamic to a
11365 writable segment, it seems better to allocate a spare program
11366 header instead, and avoid the need to move any sections.
11367 There is a long tradition of allocating spare dynamic tags,
11368 so allocating a spare program header seems like a natural
11371 If INFO is NULL, we may be copying an already prelinked binary
11372 with objcopy or strip, so do not add this header. */
11374 && !SGI_COMPAT (abfd)
11375 && bfd_get_section_by_name (abfd, ".dynamic"))
11377 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
11378 if ((*pm)->p_type == PT_NULL)
11382 m = bfd_zalloc (abfd, sizeof (*m));
11386 m->p_type = PT_NULL;
11394 /* Return the section that should be marked against GC for a given
11398 _bfd_mips_elf_gc_mark_hook (asection *sec,
11399 struct bfd_link_info *info,
11400 Elf_Internal_Rela *rel,
11401 struct elf_link_hash_entry *h,
11402 Elf_Internal_Sym *sym)
11404 /* ??? Do mips16 stub sections need to be handled special? */
11407 switch (ELF_R_TYPE (sec->owner, rel->r_info))
11409 case R_MIPS_GNU_VTINHERIT:
11410 case R_MIPS_GNU_VTENTRY:
11414 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
11417 /* Update the got entry reference counts for the section being removed. */
11420 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11421 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11422 asection *sec ATTRIBUTE_UNUSED,
11423 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
11426 Elf_Internal_Shdr *symtab_hdr;
11427 struct elf_link_hash_entry **sym_hashes;
11428 bfd_signed_vma *local_got_refcounts;
11429 const Elf_Internal_Rela *rel, *relend;
11430 unsigned long r_symndx;
11431 struct elf_link_hash_entry *h;
11433 if (info->relocatable)
11436 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11437 sym_hashes = elf_sym_hashes (abfd);
11438 local_got_refcounts = elf_local_got_refcounts (abfd);
11440 relend = relocs + sec->reloc_count;
11441 for (rel = relocs; rel < relend; rel++)
11442 switch (ELF_R_TYPE (abfd, rel->r_info))
11444 case R_MIPS16_GOT16:
11445 case R_MIPS16_CALL16:
11447 case R_MIPS_CALL16:
11448 case R_MIPS_CALL_HI16:
11449 case R_MIPS_CALL_LO16:
11450 case R_MIPS_GOT_HI16:
11451 case R_MIPS_GOT_LO16:
11452 case R_MIPS_GOT_DISP:
11453 case R_MIPS_GOT_PAGE:
11454 case R_MIPS_GOT_OFST:
11455 case R_MICROMIPS_GOT16:
11456 case R_MICROMIPS_CALL16:
11457 case R_MICROMIPS_CALL_HI16:
11458 case R_MICROMIPS_CALL_LO16:
11459 case R_MICROMIPS_GOT_HI16:
11460 case R_MICROMIPS_GOT_LO16:
11461 case R_MICROMIPS_GOT_DISP:
11462 case R_MICROMIPS_GOT_PAGE:
11463 case R_MICROMIPS_GOT_OFST:
11464 /* ??? It would seem that the existing MIPS code does no sort
11465 of reference counting or whatnot on its GOT and PLT entries,
11466 so it is not possible to garbage collect them at this time. */
11477 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11478 hiding the old indirect symbol. Process additional relocation
11479 information. Also called for weakdefs, in which case we just let
11480 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11483 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
11484 struct elf_link_hash_entry *dir,
11485 struct elf_link_hash_entry *ind)
11487 struct mips_elf_link_hash_entry *dirmips, *indmips;
11489 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
11491 dirmips = (struct mips_elf_link_hash_entry *) dir;
11492 indmips = (struct mips_elf_link_hash_entry *) ind;
11493 /* Any absolute non-dynamic relocations against an indirect or weak
11494 definition will be against the target symbol. */
11495 if (indmips->has_static_relocs)
11496 dirmips->has_static_relocs = TRUE;
11498 if (ind->root.type != bfd_link_hash_indirect)
11501 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11502 if (indmips->readonly_reloc)
11503 dirmips->readonly_reloc = TRUE;
11504 if (indmips->no_fn_stub)
11505 dirmips->no_fn_stub = TRUE;
11506 if (indmips->fn_stub)
11508 dirmips->fn_stub = indmips->fn_stub;
11509 indmips->fn_stub = NULL;
11511 if (indmips->need_fn_stub)
11513 dirmips->need_fn_stub = TRUE;
11514 indmips->need_fn_stub = FALSE;
11516 if (indmips->call_stub)
11518 dirmips->call_stub = indmips->call_stub;
11519 indmips->call_stub = NULL;
11521 if (indmips->call_fp_stub)
11523 dirmips->call_fp_stub = indmips->call_fp_stub;
11524 indmips->call_fp_stub = NULL;
11526 if (indmips->global_got_area < dirmips->global_got_area)
11527 dirmips->global_got_area = indmips->global_got_area;
11528 if (indmips->global_got_area < GGA_NONE)
11529 indmips->global_got_area = GGA_NONE;
11530 if (indmips->has_nonpic_branches)
11531 dirmips->has_nonpic_branches = TRUE;
11533 if (dirmips->tls_ie_type == 0)
11534 dirmips->tls_ie_type = indmips->tls_ie_type;
11535 if (dirmips->tls_gd_type == 0)
11536 dirmips->tls_gd_type = indmips->tls_gd_type;
11539 #define PDR_SIZE 32
11542 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11543 struct bfd_link_info *info)
11546 bfd_boolean ret = FALSE;
11547 unsigned char *tdata;
11550 o = bfd_get_section_by_name (abfd, ".pdr");
11555 if (o->size % PDR_SIZE != 0)
11557 if (o->output_section != NULL
11558 && bfd_is_abs_section (o->output_section))
11561 tdata = bfd_zmalloc (o->size / PDR_SIZE);
11565 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11566 info->keep_memory);
11573 cookie->rel = cookie->rels;
11574 cookie->relend = cookie->rels + o->reloc_count;
11576 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11578 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11587 mips_elf_section_data (o)->u.tdata = tdata;
11588 o->size -= skip * PDR_SIZE;
11594 if (! info->keep_memory)
11595 free (cookie->rels);
11601 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11603 if (strcmp (sec->name, ".pdr") == 0)
11609 _bfd_mips_elf_write_section (bfd *output_bfd,
11610 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11611 asection *sec, bfd_byte *contents)
11613 bfd_byte *to, *from, *end;
11616 if (strcmp (sec->name, ".pdr") != 0)
11619 if (mips_elf_section_data (sec)->u.tdata == NULL)
11623 end = contents + sec->size;
11624 for (from = contents, i = 0;
11626 from += PDR_SIZE, i++)
11628 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11631 memcpy (to, from, PDR_SIZE);
11634 bfd_set_section_contents (output_bfd, sec->output_section, contents,
11635 sec->output_offset, sec->size);
11639 /* microMIPS code retains local labels for linker relaxation. Omit them
11640 from output by default for clarity. */
11643 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
11645 return _bfd_elf_is_local_label_name (abfd, sym->name);
11648 /* MIPS ELF uses a special find_nearest_line routine in order the
11649 handle the ECOFF debugging information. */
11651 struct mips_elf_find_line
11653 struct ecoff_debug_info d;
11654 struct ecoff_find_line i;
11658 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11659 asymbol **symbols, bfd_vma offset,
11660 const char **filename_ptr,
11661 const char **functionname_ptr,
11662 unsigned int *line_ptr)
11666 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11667 filename_ptr, functionname_ptr,
11671 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
11672 section, symbols, offset,
11673 filename_ptr, functionname_ptr,
11674 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
11675 &elf_tdata (abfd)->dwarf2_find_line_info))
11678 msec = bfd_get_section_by_name (abfd, ".mdebug");
11681 flagword origflags;
11682 struct mips_elf_find_line *fi;
11683 const struct ecoff_debug_swap * const swap =
11684 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11686 /* If we are called during a link, mips_elf_final_link may have
11687 cleared the SEC_HAS_CONTENTS field. We force it back on here
11688 if appropriate (which it normally will be). */
11689 origflags = msec->flags;
11690 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11691 msec->flags |= SEC_HAS_CONTENTS;
11693 fi = elf_tdata (abfd)->find_line_info;
11696 bfd_size_type external_fdr_size;
11699 struct fdr *fdr_ptr;
11700 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11702 fi = bfd_zalloc (abfd, amt);
11705 msec->flags = origflags;
11709 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11711 msec->flags = origflags;
11715 /* Swap in the FDR information. */
11716 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11717 fi->d.fdr = bfd_alloc (abfd, amt);
11718 if (fi->d.fdr == NULL)
11720 msec->flags = origflags;
11723 external_fdr_size = swap->external_fdr_size;
11724 fdr_ptr = fi->d.fdr;
11725 fraw_src = (char *) fi->d.external_fdr;
11726 fraw_end = (fraw_src
11727 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11728 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11729 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11731 elf_tdata (abfd)->find_line_info = fi;
11733 /* Note that we don't bother to ever free this information.
11734 find_nearest_line is either called all the time, as in
11735 objdump -l, so the information should be saved, or it is
11736 rarely called, as in ld error messages, so the memory
11737 wasted is unimportant. Still, it would probably be a
11738 good idea for free_cached_info to throw it away. */
11741 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11742 &fi->i, filename_ptr, functionname_ptr,
11745 msec->flags = origflags;
11749 msec->flags = origflags;
11752 /* Fall back on the generic ELF find_nearest_line routine. */
11754 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11755 filename_ptr, functionname_ptr,
11760 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11761 const char **filename_ptr,
11762 const char **functionname_ptr,
11763 unsigned int *line_ptr)
11766 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11767 functionname_ptr, line_ptr,
11768 & elf_tdata (abfd)->dwarf2_find_line_info);
11773 /* When are writing out the .options or .MIPS.options section,
11774 remember the bytes we are writing out, so that we can install the
11775 GP value in the section_processing routine. */
11778 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11779 const void *location,
11780 file_ptr offset, bfd_size_type count)
11782 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11786 if (elf_section_data (section) == NULL)
11788 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11789 section->used_by_bfd = bfd_zalloc (abfd, amt);
11790 if (elf_section_data (section) == NULL)
11793 c = mips_elf_section_data (section)->u.tdata;
11796 c = bfd_zalloc (abfd, section->size);
11799 mips_elf_section_data (section)->u.tdata = c;
11802 memcpy (c + offset, location, count);
11805 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11809 /* This is almost identical to bfd_generic_get_... except that some
11810 MIPS relocations need to be handled specially. Sigh. */
11813 _bfd_elf_mips_get_relocated_section_contents
11815 struct bfd_link_info *link_info,
11816 struct bfd_link_order *link_order,
11818 bfd_boolean relocatable,
11821 /* Get enough memory to hold the stuff */
11822 bfd *input_bfd = link_order->u.indirect.section->owner;
11823 asection *input_section = link_order->u.indirect.section;
11826 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11827 arelent **reloc_vector = NULL;
11830 if (reloc_size < 0)
11833 reloc_vector = bfd_malloc (reloc_size);
11834 if (reloc_vector == NULL && reloc_size != 0)
11837 /* read in the section */
11838 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11839 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11842 reloc_count = bfd_canonicalize_reloc (input_bfd,
11846 if (reloc_count < 0)
11849 if (reloc_count > 0)
11854 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11857 struct bfd_hash_entry *h;
11858 struct bfd_link_hash_entry *lh;
11859 /* Skip all this stuff if we aren't mixing formats. */
11860 if (abfd && input_bfd
11861 && abfd->xvec == input_bfd->xvec)
11865 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11866 lh = (struct bfd_link_hash_entry *) h;
11873 case bfd_link_hash_undefined:
11874 case bfd_link_hash_undefweak:
11875 case bfd_link_hash_common:
11878 case bfd_link_hash_defined:
11879 case bfd_link_hash_defweak:
11881 gp = lh->u.def.value;
11883 case bfd_link_hash_indirect:
11884 case bfd_link_hash_warning:
11886 /* @@FIXME ignoring warning for now */
11888 case bfd_link_hash_new:
11897 for (parent = reloc_vector; *parent != NULL; parent++)
11899 char *error_message = NULL;
11900 bfd_reloc_status_type r;
11902 /* Specific to MIPS: Deal with relocation types that require
11903 knowing the gp of the output bfd. */
11904 asymbol *sym = *(*parent)->sym_ptr_ptr;
11906 /* If we've managed to find the gp and have a special
11907 function for the relocation then go ahead, else default
11908 to the generic handling. */
11910 && (*parent)->howto->special_function
11911 == _bfd_mips_elf32_gprel16_reloc)
11912 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11913 input_section, relocatable,
11916 r = bfd_perform_relocation (input_bfd, *parent, data,
11918 relocatable ? abfd : NULL,
11923 asection *os = input_section->output_section;
11925 /* A partial link, so keep the relocs */
11926 os->orelocation[os->reloc_count] = *parent;
11930 if (r != bfd_reloc_ok)
11934 case bfd_reloc_undefined:
11935 if (!((*link_info->callbacks->undefined_symbol)
11936 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11937 input_bfd, input_section, (*parent)->address, TRUE)))
11940 case bfd_reloc_dangerous:
11941 BFD_ASSERT (error_message != NULL);
11942 if (!((*link_info->callbacks->reloc_dangerous)
11943 (link_info, error_message, input_bfd, input_section,
11944 (*parent)->address)))
11947 case bfd_reloc_overflow:
11948 if (!((*link_info->callbacks->reloc_overflow)
11950 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11951 (*parent)->howto->name, (*parent)->addend,
11952 input_bfd, input_section, (*parent)->address)))
11955 case bfd_reloc_outofrange:
11964 if (reloc_vector != NULL)
11965 free (reloc_vector);
11969 if (reloc_vector != NULL)
11970 free (reloc_vector);
11975 mips_elf_relax_delete_bytes (bfd *abfd,
11976 asection *sec, bfd_vma addr, int count)
11978 Elf_Internal_Shdr *symtab_hdr;
11979 unsigned int sec_shndx;
11980 bfd_byte *contents;
11981 Elf_Internal_Rela *irel, *irelend;
11982 Elf_Internal_Sym *isym;
11983 Elf_Internal_Sym *isymend;
11984 struct elf_link_hash_entry **sym_hashes;
11985 struct elf_link_hash_entry **end_hashes;
11986 struct elf_link_hash_entry **start_hashes;
11987 unsigned int symcount;
11989 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
11990 contents = elf_section_data (sec)->this_hdr.contents;
11992 irel = elf_section_data (sec)->relocs;
11993 irelend = irel + sec->reloc_count;
11995 /* Actually delete the bytes. */
11996 memmove (contents + addr, contents + addr + count,
11997 (size_t) (sec->size - addr - count));
11998 sec->size -= count;
12000 /* Adjust all the relocs. */
12001 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12003 /* Get the new reloc address. */
12004 if (irel->r_offset > addr)
12005 irel->r_offset -= count;
12008 BFD_ASSERT (addr % 2 == 0);
12009 BFD_ASSERT (count % 2 == 0);
12011 /* Adjust the local symbols defined in this section. */
12012 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12013 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12014 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
12015 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
12016 isym->st_value -= count;
12018 /* Now adjust the global symbols defined in this section. */
12019 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12020 - symtab_hdr->sh_info);
12021 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12022 end_hashes = sym_hashes + symcount;
12024 for (; sym_hashes < end_hashes; sym_hashes++)
12026 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12028 if ((sym_hash->root.type == bfd_link_hash_defined
12029 || sym_hash->root.type == bfd_link_hash_defweak)
12030 && sym_hash->root.u.def.section == sec)
12032 bfd_vma value = sym_hash->root.u.def.value;
12034 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12035 value &= MINUS_TWO;
12037 sym_hash->root.u.def.value -= count;
12045 /* Opcodes needed for microMIPS relaxation as found in
12046 opcodes/micromips-opc.c. */
12048 struct opcode_descriptor {
12049 unsigned long match;
12050 unsigned long mask;
12053 /* The $ra register aka $31. */
12057 /* 32-bit instruction format register fields. */
12059 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12060 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12062 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
12064 #define OP16_VALID_REG(r) \
12065 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12068 /* 32-bit and 16-bit branches. */
12070 static const struct opcode_descriptor b_insns_32[] = {
12071 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12072 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12073 { 0, 0 } /* End marker for find_match(). */
12076 static const struct opcode_descriptor bc_insn_32 =
12077 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12079 static const struct opcode_descriptor bz_insn_32 =
12080 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12082 static const struct opcode_descriptor bzal_insn_32 =
12083 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12085 static const struct opcode_descriptor beq_insn_32 =
12086 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12088 static const struct opcode_descriptor b_insn_16 =
12089 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12091 static const struct opcode_descriptor bz_insn_16 =
12092 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
12095 /* 32-bit and 16-bit branch EQ and NE zero. */
12097 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12098 eq and second the ne. This convention is used when replacing a
12099 32-bit BEQ/BNE with the 16-bit version. */
12101 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12103 static const struct opcode_descriptor bz_rs_insns_32[] = {
12104 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12105 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12106 { 0, 0 } /* End marker for find_match(). */
12109 static const struct opcode_descriptor bz_rt_insns_32[] = {
12110 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12111 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12112 { 0, 0 } /* End marker for find_match(). */
12115 static const struct opcode_descriptor bzc_insns_32[] = {
12116 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12117 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12118 { 0, 0 } /* End marker for find_match(). */
12121 static const struct opcode_descriptor bz_insns_16[] = {
12122 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12123 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12124 { 0, 0 } /* End marker for find_match(). */
12127 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12129 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12130 #define BZ16_REG_FIELD(r) \
12131 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12134 /* 32-bit instructions with a delay slot. */
12136 static const struct opcode_descriptor jal_insn_32_bd16 =
12137 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12139 static const struct opcode_descriptor jal_insn_32_bd32 =
12140 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12142 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12143 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12145 static const struct opcode_descriptor j_insn_32 =
12146 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12148 static const struct opcode_descriptor jalr_insn_32 =
12149 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12151 /* This table can be compacted, because no opcode replacement is made. */
12153 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12154 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12156 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12157 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12159 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12160 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12161 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12162 { 0, 0 } /* End marker for find_match(). */
12165 /* This table can be compacted, because no opcode replacement is made. */
12167 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12168 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12170 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12171 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12172 { 0, 0 } /* End marker for find_match(). */
12176 /* 16-bit instructions with a delay slot. */
12178 static const struct opcode_descriptor jalr_insn_16_bd16 =
12179 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12181 static const struct opcode_descriptor jalr_insn_16_bd32 =
12182 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12184 static const struct opcode_descriptor jr_insn_16 =
12185 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12187 #define JR16_REG(opcode) ((opcode) & 0x1f)
12189 /* This table can be compacted, because no opcode replacement is made. */
12191 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12192 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12194 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12195 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12196 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12197 { 0, 0 } /* End marker for find_match(). */
12201 /* LUI instruction. */
12203 static const struct opcode_descriptor lui_insn =
12204 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12207 /* ADDIU instruction. */
12209 static const struct opcode_descriptor addiu_insn =
12210 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12212 static const struct opcode_descriptor addiupc_insn =
12213 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12215 #define ADDIUPC_REG_FIELD(r) \
12216 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12219 /* Relaxable instructions in a JAL delay slot: MOVE. */
12221 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12222 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12223 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12224 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12226 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12227 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12229 static const struct opcode_descriptor move_insns_32[] = {
12230 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12231 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12232 { 0, 0 } /* End marker for find_match(). */
12235 static const struct opcode_descriptor move_insn_16 =
12236 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12239 /* NOP instructions. */
12241 static const struct opcode_descriptor nop_insn_32 =
12242 { /* "nop", "", */ 0x00000000, 0xffffffff };
12244 static const struct opcode_descriptor nop_insn_16 =
12245 { /* "nop", "", */ 0x0c00, 0xffff };
12248 /* Instruction match support. */
12250 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12253 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12255 unsigned long indx;
12257 for (indx = 0; insn[indx].mask != 0; indx++)
12258 if (MATCH (opcode, insn[indx]))
12265 /* Branch and delay slot decoding support. */
12267 /* If PTR points to what *might* be a 16-bit branch or jump, then
12268 return the minimum length of its delay slot, otherwise return 0.
12269 Non-zero results are not definitive as we might be checking against
12270 the second half of another instruction. */
12273 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12275 unsigned long opcode;
12278 opcode = bfd_get_16 (abfd, ptr);
12279 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12280 /* 16-bit branch/jump with a 32-bit delay slot. */
12282 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12283 || find_match (opcode, ds_insns_16_bd16) >= 0)
12284 /* 16-bit branch/jump with a 16-bit delay slot. */
12287 /* No delay slot. */
12293 /* If PTR points to what *might* be a 32-bit branch or jump, then
12294 return the minimum length of its delay slot, otherwise return 0.
12295 Non-zero results are not definitive as we might be checking against
12296 the second half of another instruction. */
12299 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12301 unsigned long opcode;
12304 opcode = bfd_get_micromips_32 (abfd, ptr);
12305 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12306 /* 32-bit branch/jump with a 32-bit delay slot. */
12308 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12309 /* 32-bit branch/jump with a 16-bit delay slot. */
12312 /* No delay slot. */
12318 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12319 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12322 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12324 unsigned long opcode;
12326 opcode = bfd_get_16 (abfd, ptr);
12327 if (MATCH (opcode, b_insn_16)
12329 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12331 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12332 /* BEQZ16, BNEZ16 */
12333 || (MATCH (opcode, jalr_insn_16_bd32)
12335 && reg != JR16_REG (opcode) && reg != RA))
12341 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12342 then return TRUE, otherwise FALSE. */
12345 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12347 unsigned long opcode;
12349 opcode = bfd_get_micromips_32 (abfd, ptr);
12350 if (MATCH (opcode, j_insn_32)
12352 || MATCH (opcode, bc_insn_32)
12353 /* BC1F, BC1T, BC2F, BC2T */
12354 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12356 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12357 /* BGEZ, BGTZ, BLEZ, BLTZ */
12358 || (MATCH (opcode, bzal_insn_32)
12359 /* BGEZAL, BLTZAL */
12360 && reg != OP32_SREG (opcode) && reg != RA)
12361 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
12362 /* JALR, JALR.HB, BEQ, BNE */
12363 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
12369 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
12370 IRELEND) at OFFSET indicate that there must be a compact branch there,
12371 then return TRUE, otherwise FALSE. */
12374 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
12375 const Elf_Internal_Rela *internal_relocs,
12376 const Elf_Internal_Rela *irelend)
12378 const Elf_Internal_Rela *irel;
12379 unsigned long opcode;
12381 opcode = bfd_get_micromips_32 (abfd, ptr);
12382 if (find_match (opcode, bzc_insns_32) < 0)
12385 for (irel = internal_relocs; irel < irelend; irel++)
12386 if (irel->r_offset == offset
12387 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
12393 /* Bitsize checking. */
12394 #define IS_BITSIZE(val, N) \
12395 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
12396 - (1ULL << ((N) - 1))) == (val))
12400 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
12401 struct bfd_link_info *link_info,
12402 bfd_boolean *again)
12404 Elf_Internal_Shdr *symtab_hdr;
12405 Elf_Internal_Rela *internal_relocs;
12406 Elf_Internal_Rela *irel, *irelend;
12407 bfd_byte *contents = NULL;
12408 Elf_Internal_Sym *isymbuf = NULL;
12410 /* Assume nothing changes. */
12413 /* We don't have to do anything for a relocatable link, if
12414 this section does not have relocs, or if this is not a
12417 if (link_info->relocatable
12418 || (sec->flags & SEC_RELOC) == 0
12419 || sec->reloc_count == 0
12420 || (sec->flags & SEC_CODE) == 0)
12423 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12425 /* Get a copy of the native relocations. */
12426 internal_relocs = (_bfd_elf_link_read_relocs
12427 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
12428 link_info->keep_memory));
12429 if (internal_relocs == NULL)
12432 /* Walk through them looking for relaxing opportunities. */
12433 irelend = internal_relocs + sec->reloc_count;
12434 for (irel = internal_relocs; irel < irelend; irel++)
12436 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
12437 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
12438 bfd_boolean target_is_micromips_code_p;
12439 unsigned long opcode;
12445 /* The number of bytes to delete for relaxation and from where
12446 to delete these bytes starting at irel->r_offset. */
12450 /* If this isn't something that can be relaxed, then ignore
12452 if (r_type != R_MICROMIPS_HI16
12453 && r_type != R_MICROMIPS_PC16_S1
12454 && r_type != R_MICROMIPS_26_S1)
12457 /* Get the section contents if we haven't done so already. */
12458 if (contents == NULL)
12460 /* Get cached copy if it exists. */
12461 if (elf_section_data (sec)->this_hdr.contents != NULL)
12462 contents = elf_section_data (sec)->this_hdr.contents;
12463 /* Go get them off disk. */
12464 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
12467 ptr = contents + irel->r_offset;
12469 /* Read this BFD's local symbols if we haven't done so already. */
12470 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
12472 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
12473 if (isymbuf == NULL)
12474 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12475 symtab_hdr->sh_info, 0,
12477 if (isymbuf == NULL)
12481 /* Get the value of the symbol referred to by the reloc. */
12482 if (r_symndx < symtab_hdr->sh_info)
12484 /* A local symbol. */
12485 Elf_Internal_Sym *isym;
12488 isym = isymbuf + r_symndx;
12489 if (isym->st_shndx == SHN_UNDEF)
12490 sym_sec = bfd_und_section_ptr;
12491 else if (isym->st_shndx == SHN_ABS)
12492 sym_sec = bfd_abs_section_ptr;
12493 else if (isym->st_shndx == SHN_COMMON)
12494 sym_sec = bfd_com_section_ptr;
12496 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
12497 symval = (isym->st_value
12498 + sym_sec->output_section->vma
12499 + sym_sec->output_offset);
12500 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
12504 unsigned long indx;
12505 struct elf_link_hash_entry *h;
12507 /* An external symbol. */
12508 indx = r_symndx - symtab_hdr->sh_info;
12509 h = elf_sym_hashes (abfd)[indx];
12510 BFD_ASSERT (h != NULL);
12512 if (h->root.type != bfd_link_hash_defined
12513 && h->root.type != bfd_link_hash_defweak)
12514 /* This appears to be a reference to an undefined
12515 symbol. Just ignore it -- it will be caught by the
12516 regular reloc processing. */
12519 symval = (h->root.u.def.value
12520 + h->root.u.def.section->output_section->vma
12521 + h->root.u.def.section->output_offset);
12522 target_is_micromips_code_p = (!h->needs_plt
12523 && ELF_ST_IS_MICROMIPS (h->other));
12527 /* For simplicity of coding, we are going to modify the
12528 section contents, the section relocs, and the BFD symbol
12529 table. We must tell the rest of the code not to free up this
12530 information. It would be possible to instead create a table
12531 of changes which have to be made, as is done in coff-mips.c;
12532 that would be more work, but would require less memory when
12533 the linker is run. */
12535 /* Only 32-bit instructions relaxed. */
12536 if (irel->r_offset + 4 > sec->size)
12539 opcode = bfd_get_micromips_32 (abfd, ptr);
12541 /* This is the pc-relative distance from the instruction the
12542 relocation is applied to, to the symbol referred. */
12544 - (sec->output_section->vma + sec->output_offset)
12547 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
12548 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
12549 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
12551 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
12553 where pcrval has first to be adjusted to apply against the LO16
12554 location (we make the adjustment later on, when we have figured
12555 out the offset). */
12556 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
12558 bfd_boolean bzc = FALSE;
12559 unsigned long nextopc;
12563 /* Give up if the previous reloc was a HI16 against this symbol
12565 if (irel > internal_relocs
12566 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
12567 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
12570 /* Or if the next reloc is not a LO16 against this symbol. */
12571 if (irel + 1 >= irelend
12572 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
12573 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
12576 /* Or if the second next reloc is a LO16 against this symbol too. */
12577 if (irel + 2 >= irelend
12578 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
12579 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
12582 /* See if the LUI instruction *might* be in a branch delay slot.
12583 We check whether what looks like a 16-bit branch or jump is
12584 actually an immediate argument to a compact branch, and let
12585 it through if so. */
12586 if (irel->r_offset >= 2
12587 && check_br16_dslot (abfd, ptr - 2)
12588 && !(irel->r_offset >= 4
12589 && (bzc = check_relocated_bzc (abfd,
12590 ptr - 4, irel->r_offset - 4,
12591 internal_relocs, irelend))))
12593 if (irel->r_offset >= 4
12595 && check_br32_dslot (abfd, ptr - 4))
12598 reg = OP32_SREG (opcode);
12600 /* We only relax adjacent instructions or ones separated with
12601 a branch or jump that has a delay slot. The branch or jump
12602 must not fiddle with the register used to hold the address.
12603 Subtract 4 for the LUI itself. */
12604 offset = irel[1].r_offset - irel[0].r_offset;
12605 switch (offset - 4)
12610 if (check_br16 (abfd, ptr + 4, reg))
12614 if (check_br32 (abfd, ptr + 4, reg))
12621 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
12623 /* Give up unless the same register is used with both
12625 if (OP32_SREG (nextopc) != reg)
12628 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
12629 and rounding up to take masking of the two LSBs into account. */
12630 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
12632 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
12633 if (IS_BITSIZE (symval, 16))
12635 /* Fix the relocation's type. */
12636 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
12638 /* Instructions using R_MICROMIPS_LO16 have the base or
12639 source register in bits 20:16. This register becomes $0
12640 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
12641 nextopc &= ~0x001f0000;
12642 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
12643 contents + irel[1].r_offset);
12646 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
12647 We add 4 to take LUI deletion into account while checking
12648 the PC-relative distance. */
12649 else if (symval % 4 == 0
12650 && IS_BITSIZE (pcrval + 4, 25)
12651 && MATCH (nextopc, addiu_insn)
12652 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
12653 && OP16_VALID_REG (OP32_TREG (nextopc)))
12655 /* Fix the relocation's type. */
12656 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
12658 /* Replace ADDIU with the ADDIUPC version. */
12659 nextopc = (addiupc_insn.match
12660 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
12662 bfd_put_micromips_32 (abfd, nextopc,
12663 contents + irel[1].r_offset);
12666 /* Can't do anything, give up, sigh... */
12670 /* Fix the relocation's type. */
12671 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
12673 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
12678 /* Compact branch relaxation -- due to the multitude of macros
12679 employed by the compiler/assembler, compact branches are not
12680 always generated. Obviously, this can/will be fixed elsewhere,
12681 but there is no drawback in double checking it here. */
12682 else if (r_type == R_MICROMIPS_PC16_S1
12683 && irel->r_offset + 5 < sec->size
12684 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12685 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
12686 && MATCH (bfd_get_16 (abfd, ptr + 4), nop_insn_16))
12690 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12692 /* Replace BEQZ/BNEZ with the compact version. */
12693 opcode = (bzc_insns_32[fndopc].match
12694 | BZC32_REG_FIELD (reg)
12695 | (opcode & 0xffff)); /* Addend value. */
12697 bfd_put_micromips_32 (abfd, opcode, ptr);
12699 /* Delete the 16-bit delay slot NOP: two bytes from
12700 irel->offset + 4. */
12705 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
12706 to check the distance from the next instruction, so subtract 2. */
12707 else if (r_type == R_MICROMIPS_PC16_S1
12708 && IS_BITSIZE (pcrval - 2, 11)
12709 && find_match (opcode, b_insns_32) >= 0)
12711 /* Fix the relocation's type. */
12712 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
12714 /* Replace the 32-bit opcode with a 16-bit opcode. */
12717 | (opcode & 0x3ff)), /* Addend value. */
12720 /* Delete 2 bytes from irel->r_offset + 2. */
12725 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
12726 to check the distance from the next instruction, so subtract 2. */
12727 else if (r_type == R_MICROMIPS_PC16_S1
12728 && IS_BITSIZE (pcrval - 2, 8)
12729 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12730 && OP16_VALID_REG (OP32_SREG (opcode)))
12731 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
12732 && OP16_VALID_REG (OP32_TREG (opcode)))))
12736 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12738 /* Fix the relocation's type. */
12739 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
12741 /* Replace the 32-bit opcode with a 16-bit opcode. */
12743 (bz_insns_16[fndopc].match
12744 | BZ16_REG_FIELD (reg)
12745 | (opcode & 0x7f)), /* Addend value. */
12748 /* Delete 2 bytes from irel->r_offset + 2. */
12753 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
12754 else if (r_type == R_MICROMIPS_26_S1
12755 && target_is_micromips_code_p
12756 && irel->r_offset + 7 < sec->size
12757 && MATCH (opcode, jal_insn_32_bd32))
12759 unsigned long n32opc;
12760 bfd_boolean relaxed = FALSE;
12762 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
12764 if (MATCH (n32opc, nop_insn_32))
12766 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
12767 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
12771 else if (find_match (n32opc, move_insns_32) >= 0)
12773 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
12775 (move_insn_16.match
12776 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
12777 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
12782 /* Other 32-bit instructions relaxable to 16-bit
12783 instructions will be handled here later. */
12787 /* JAL with 32-bit delay slot that is changed to a JALS
12788 with 16-bit delay slot. */
12789 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
12791 /* Delete 2 bytes from irel->r_offset + 6. */
12799 /* Note that we've changed the relocs, section contents, etc. */
12800 elf_section_data (sec)->relocs = internal_relocs;
12801 elf_section_data (sec)->this_hdr.contents = contents;
12802 symtab_hdr->contents = (unsigned char *) isymbuf;
12804 /* Delete bytes depending on the delcnt and deloff. */
12805 if (!mips_elf_relax_delete_bytes (abfd, sec,
12806 irel->r_offset + deloff, delcnt))
12809 /* That will change things, so we should relax again.
12810 Note that this is not required, and it may be slow. */
12815 if (isymbuf != NULL
12816 && symtab_hdr->contents != (unsigned char *) isymbuf)
12818 if (! link_info->keep_memory)
12822 /* Cache the symbols for elf_link_input_bfd. */
12823 symtab_hdr->contents = (unsigned char *) isymbuf;
12827 if (contents != NULL
12828 && elf_section_data (sec)->this_hdr.contents != contents)
12830 if (! link_info->keep_memory)
12834 /* Cache the section contents for elf_link_input_bfd. */
12835 elf_section_data (sec)->this_hdr.contents = contents;
12839 if (internal_relocs != NULL
12840 && elf_section_data (sec)->relocs != internal_relocs)
12841 free (internal_relocs);
12846 if (isymbuf != NULL
12847 && symtab_hdr->contents != (unsigned char *) isymbuf)
12849 if (contents != NULL
12850 && elf_section_data (sec)->this_hdr.contents != contents)
12852 if (internal_relocs != NULL
12853 && elf_section_data (sec)->relocs != internal_relocs)
12854 free (internal_relocs);
12859 /* Create a MIPS ELF linker hash table. */
12861 struct bfd_link_hash_table *
12862 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
12864 struct mips_elf_link_hash_table *ret;
12865 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
12867 ret = bfd_zmalloc (amt);
12871 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
12872 mips_elf_link_hash_newfunc,
12873 sizeof (struct mips_elf_link_hash_entry),
12880 return &ret->root.root;
12883 /* Likewise, but indicate that the target is VxWorks. */
12885 struct bfd_link_hash_table *
12886 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
12888 struct bfd_link_hash_table *ret;
12890 ret = _bfd_mips_elf_link_hash_table_create (abfd);
12893 struct mips_elf_link_hash_table *htab;
12895 htab = (struct mips_elf_link_hash_table *) ret;
12896 htab->use_plts_and_copy_relocs = TRUE;
12897 htab->is_vxworks = TRUE;
12902 /* A function that the linker calls if we are allowed to use PLTs
12903 and copy relocs. */
12906 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
12908 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
12911 /* We need to use a special link routine to handle the .reginfo and
12912 the .mdebug sections. We need to merge all instances of these
12913 sections together, not write them all out sequentially. */
12916 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12919 struct bfd_link_order *p;
12920 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
12921 asection *rtproc_sec;
12922 Elf32_RegInfo reginfo;
12923 struct ecoff_debug_info debug;
12924 struct mips_htab_traverse_info hti;
12925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12926 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
12927 HDRR *symhdr = &debug.symbolic_header;
12928 void *mdebug_handle = NULL;
12933 struct mips_elf_link_hash_table *htab;
12935 static const char * const secname[] =
12937 ".text", ".init", ".fini", ".data",
12938 ".rodata", ".sdata", ".sbss", ".bss"
12940 static const int sc[] =
12942 scText, scInit, scFini, scData,
12943 scRData, scSData, scSBss, scBss
12946 /* Sort the dynamic symbols so that those with GOT entries come after
12948 htab = mips_elf_hash_table (info);
12949 BFD_ASSERT (htab != NULL);
12951 if (!mips_elf_sort_hash_table (abfd, info))
12954 /* Create any scheduled LA25 stubs. */
12956 hti.output_bfd = abfd;
12958 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
12962 /* Get a value for the GP register. */
12963 if (elf_gp (abfd) == 0)
12965 struct bfd_link_hash_entry *h;
12967 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
12968 if (h != NULL && h->type == bfd_link_hash_defined)
12969 elf_gp (abfd) = (h->u.def.value
12970 + h->u.def.section->output_section->vma
12971 + h->u.def.section->output_offset);
12972 else if (htab->is_vxworks
12973 && (h = bfd_link_hash_lookup (info->hash,
12974 "_GLOBAL_OFFSET_TABLE_",
12975 FALSE, FALSE, TRUE))
12976 && h->type == bfd_link_hash_defined)
12977 elf_gp (abfd) = (h->u.def.section->output_section->vma
12978 + h->u.def.section->output_offset
12980 else if (info->relocatable)
12982 bfd_vma lo = MINUS_ONE;
12984 /* Find the GP-relative section with the lowest offset. */
12985 for (o = abfd->sections; o != NULL; o = o->next)
12987 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
12990 /* And calculate GP relative to that. */
12991 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
12995 /* If the relocate_section function needs to do a reloc
12996 involving the GP value, it should make a reloc_dangerous
12997 callback to warn that GP is not defined. */
13001 /* Go through the sections and collect the .reginfo and .mdebug
13003 reginfo_sec = NULL;
13005 gptab_data_sec = NULL;
13006 gptab_bss_sec = NULL;
13007 for (o = abfd->sections; o != NULL; o = o->next)
13009 if (strcmp (o->name, ".reginfo") == 0)
13011 memset (®info, 0, sizeof reginfo);
13013 /* We have found the .reginfo section in the output file.
13014 Look through all the link_orders comprising it and merge
13015 the information together. */
13016 for (p = o->map_head.link_order; p != NULL; p = p->next)
13018 asection *input_section;
13020 Elf32_External_RegInfo ext;
13023 if (p->type != bfd_indirect_link_order)
13025 if (p->type == bfd_data_link_order)
13030 input_section = p->u.indirect.section;
13031 input_bfd = input_section->owner;
13033 if (! bfd_get_section_contents (input_bfd, input_section,
13034 &ext, 0, sizeof ext))
13037 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13039 reginfo.ri_gprmask |= sub.ri_gprmask;
13040 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13041 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13042 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13043 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13045 /* ri_gp_value is set by the function
13046 mips_elf32_section_processing when the section is
13047 finally written out. */
13049 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13050 elf_link_input_bfd ignores this section. */
13051 input_section->flags &= ~SEC_HAS_CONTENTS;
13054 /* Size has been set in _bfd_mips_elf_always_size_sections. */
13055 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
13057 /* Skip this section later on (I don't think this currently
13058 matters, but someday it might). */
13059 o->map_head.link_order = NULL;
13064 if (strcmp (o->name, ".mdebug") == 0)
13066 struct extsym_info einfo;
13069 /* We have found the .mdebug section in the output file.
13070 Look through all the link_orders comprising it and merge
13071 the information together. */
13072 symhdr->magic = swap->sym_magic;
13073 /* FIXME: What should the version stamp be? */
13074 symhdr->vstamp = 0;
13075 symhdr->ilineMax = 0;
13076 symhdr->cbLine = 0;
13077 symhdr->idnMax = 0;
13078 symhdr->ipdMax = 0;
13079 symhdr->isymMax = 0;
13080 symhdr->ioptMax = 0;
13081 symhdr->iauxMax = 0;
13082 symhdr->issMax = 0;
13083 symhdr->issExtMax = 0;
13084 symhdr->ifdMax = 0;
13086 symhdr->iextMax = 0;
13088 /* We accumulate the debugging information itself in the
13089 debug_info structure. */
13091 debug.external_dnr = NULL;
13092 debug.external_pdr = NULL;
13093 debug.external_sym = NULL;
13094 debug.external_opt = NULL;
13095 debug.external_aux = NULL;
13097 debug.ssext = debug.ssext_end = NULL;
13098 debug.external_fdr = NULL;
13099 debug.external_rfd = NULL;
13100 debug.external_ext = debug.external_ext_end = NULL;
13102 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
13103 if (mdebug_handle == NULL)
13107 esym.cobol_main = 0;
13111 esym.asym.iss = issNil;
13112 esym.asym.st = stLocal;
13113 esym.asym.reserved = 0;
13114 esym.asym.index = indexNil;
13116 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13118 esym.asym.sc = sc[i];
13119 s = bfd_get_section_by_name (abfd, secname[i]);
13122 esym.asym.value = s->vma;
13123 last = s->vma + s->size;
13126 esym.asym.value = last;
13127 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13128 secname[i], &esym))
13132 for (p = o->map_head.link_order; p != NULL; p = p->next)
13134 asection *input_section;
13136 const struct ecoff_debug_swap *input_swap;
13137 struct ecoff_debug_info input_debug;
13141 if (p->type != bfd_indirect_link_order)
13143 if (p->type == bfd_data_link_order)
13148 input_section = p->u.indirect.section;
13149 input_bfd = input_section->owner;
13151 if (!is_mips_elf (input_bfd))
13153 /* I don't know what a non MIPS ELF bfd would be
13154 doing with a .mdebug section, but I don't really
13155 want to deal with it. */
13159 input_swap = (get_elf_backend_data (input_bfd)
13160 ->elf_backend_ecoff_debug_swap);
13162 BFD_ASSERT (p->size == input_section->size);
13164 /* The ECOFF linking code expects that we have already
13165 read in the debugging information and set up an
13166 ecoff_debug_info structure, so we do that now. */
13167 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13171 if (! (bfd_ecoff_debug_accumulate
13172 (mdebug_handle, abfd, &debug, swap, input_bfd,
13173 &input_debug, input_swap, info)))
13176 /* Loop through the external symbols. For each one with
13177 interesting information, try to find the symbol in
13178 the linker global hash table and save the information
13179 for the output external symbols. */
13180 eraw_src = input_debug.external_ext;
13181 eraw_end = (eraw_src
13182 + (input_debug.symbolic_header.iextMax
13183 * input_swap->external_ext_size));
13185 eraw_src < eraw_end;
13186 eraw_src += input_swap->external_ext_size)
13190 struct mips_elf_link_hash_entry *h;
13192 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
13193 if (ext.asym.sc == scNil
13194 || ext.asym.sc == scUndefined
13195 || ext.asym.sc == scSUndefined)
13198 name = input_debug.ssext + ext.asym.iss;
13199 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
13200 name, FALSE, FALSE, TRUE);
13201 if (h == NULL || h->esym.ifd != -2)
13206 BFD_ASSERT (ext.ifd
13207 < input_debug.symbolic_header.ifdMax);
13208 ext.ifd = input_debug.ifdmap[ext.ifd];
13214 /* Free up the information we just read. */
13215 free (input_debug.line);
13216 free (input_debug.external_dnr);
13217 free (input_debug.external_pdr);
13218 free (input_debug.external_sym);
13219 free (input_debug.external_opt);
13220 free (input_debug.external_aux);
13221 free (input_debug.ss);
13222 free (input_debug.ssext);
13223 free (input_debug.external_fdr);
13224 free (input_debug.external_rfd);
13225 free (input_debug.external_ext);
13227 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13228 elf_link_input_bfd ignores this section. */
13229 input_section->flags &= ~SEC_HAS_CONTENTS;
13232 if (SGI_COMPAT (abfd) && info->shared)
13234 /* Create .rtproc section. */
13235 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
13236 if (rtproc_sec == NULL)
13238 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13239 | SEC_LINKER_CREATED | SEC_READONLY);
13241 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13244 if (rtproc_sec == NULL
13245 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
13249 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13255 /* Build the external symbol information. */
13258 einfo.debug = &debug;
13260 einfo.failed = FALSE;
13261 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
13262 mips_elf_output_extsym, &einfo);
13266 /* Set the size of the .mdebug section. */
13267 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
13269 /* Skip this section later on (I don't think this currently
13270 matters, but someday it might). */
13271 o->map_head.link_order = NULL;
13276 if (CONST_STRNEQ (o->name, ".gptab."))
13278 const char *subname;
13281 Elf32_External_gptab *ext_tab;
13284 /* The .gptab.sdata and .gptab.sbss sections hold
13285 information describing how the small data area would
13286 change depending upon the -G switch. These sections
13287 not used in executables files. */
13288 if (! info->relocatable)
13290 for (p = o->map_head.link_order; p != NULL; p = p->next)
13292 asection *input_section;
13294 if (p->type != bfd_indirect_link_order)
13296 if (p->type == bfd_data_link_order)
13301 input_section = p->u.indirect.section;
13303 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13304 elf_link_input_bfd ignores this section. */
13305 input_section->flags &= ~SEC_HAS_CONTENTS;
13308 /* Skip this section later on (I don't think this
13309 currently matters, but someday it might). */
13310 o->map_head.link_order = NULL;
13312 /* Really remove the section. */
13313 bfd_section_list_remove (abfd, o);
13314 --abfd->section_count;
13319 /* There is one gptab for initialized data, and one for
13320 uninitialized data. */
13321 if (strcmp (o->name, ".gptab.sdata") == 0)
13322 gptab_data_sec = o;
13323 else if (strcmp (o->name, ".gptab.sbss") == 0)
13327 (*_bfd_error_handler)
13328 (_("%s: illegal section name `%s'"),
13329 bfd_get_filename (abfd), o->name);
13330 bfd_set_error (bfd_error_nonrepresentable_section);
13334 /* The linker script always combines .gptab.data and
13335 .gptab.sdata into .gptab.sdata, and likewise for
13336 .gptab.bss and .gptab.sbss. It is possible that there is
13337 no .sdata or .sbss section in the output file, in which
13338 case we must change the name of the output section. */
13339 subname = o->name + sizeof ".gptab" - 1;
13340 if (bfd_get_section_by_name (abfd, subname) == NULL)
13342 if (o == gptab_data_sec)
13343 o->name = ".gptab.data";
13345 o->name = ".gptab.bss";
13346 subname = o->name + sizeof ".gptab" - 1;
13347 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
13350 /* Set up the first entry. */
13352 amt = c * sizeof (Elf32_gptab);
13353 tab = bfd_malloc (amt);
13356 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
13357 tab[0].gt_header.gt_unused = 0;
13359 /* Combine the input sections. */
13360 for (p = o->map_head.link_order; p != NULL; p = p->next)
13362 asection *input_section;
13364 bfd_size_type size;
13365 unsigned long last;
13366 bfd_size_type gpentry;
13368 if (p->type != bfd_indirect_link_order)
13370 if (p->type == bfd_data_link_order)
13375 input_section = p->u.indirect.section;
13376 input_bfd = input_section->owner;
13378 /* Combine the gptab entries for this input section one
13379 by one. We know that the input gptab entries are
13380 sorted by ascending -G value. */
13381 size = input_section->size;
13383 for (gpentry = sizeof (Elf32_External_gptab);
13385 gpentry += sizeof (Elf32_External_gptab))
13387 Elf32_External_gptab ext_gptab;
13388 Elf32_gptab int_gptab;
13394 if (! (bfd_get_section_contents
13395 (input_bfd, input_section, &ext_gptab, gpentry,
13396 sizeof (Elf32_External_gptab))))
13402 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
13404 val = int_gptab.gt_entry.gt_g_value;
13405 add = int_gptab.gt_entry.gt_bytes - last;
13408 for (look = 1; look < c; look++)
13410 if (tab[look].gt_entry.gt_g_value >= val)
13411 tab[look].gt_entry.gt_bytes += add;
13413 if (tab[look].gt_entry.gt_g_value == val)
13419 Elf32_gptab *new_tab;
13422 /* We need a new table entry. */
13423 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
13424 new_tab = bfd_realloc (tab, amt);
13425 if (new_tab == NULL)
13431 tab[c].gt_entry.gt_g_value = val;
13432 tab[c].gt_entry.gt_bytes = add;
13434 /* Merge in the size for the next smallest -G
13435 value, since that will be implied by this new
13438 for (look = 1; look < c; look++)
13440 if (tab[look].gt_entry.gt_g_value < val
13442 || (tab[look].gt_entry.gt_g_value
13443 > tab[max].gt_entry.gt_g_value)))
13447 tab[c].gt_entry.gt_bytes +=
13448 tab[max].gt_entry.gt_bytes;
13453 last = int_gptab.gt_entry.gt_bytes;
13456 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13457 elf_link_input_bfd ignores this section. */
13458 input_section->flags &= ~SEC_HAS_CONTENTS;
13461 /* The table must be sorted by -G value. */
13463 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
13465 /* Swap out the table. */
13466 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
13467 ext_tab = bfd_alloc (abfd, amt);
13468 if (ext_tab == NULL)
13474 for (j = 0; j < c; j++)
13475 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
13478 o->size = c * sizeof (Elf32_External_gptab);
13479 o->contents = (bfd_byte *) ext_tab;
13481 /* Skip this section later on (I don't think this currently
13482 matters, but someday it might). */
13483 o->map_head.link_order = NULL;
13487 /* Invoke the regular ELF backend linker to do all the work. */
13488 if (!bfd_elf_final_link (abfd, info))
13491 /* Now write out the computed sections. */
13493 if (reginfo_sec != NULL)
13495 Elf32_External_RegInfo ext;
13497 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
13498 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
13502 if (mdebug_sec != NULL)
13504 BFD_ASSERT (abfd->output_has_begun);
13505 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
13507 mdebug_sec->filepos))
13510 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
13513 if (gptab_data_sec != NULL)
13515 if (! bfd_set_section_contents (abfd, gptab_data_sec,
13516 gptab_data_sec->contents,
13517 0, gptab_data_sec->size))
13521 if (gptab_bss_sec != NULL)
13523 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
13524 gptab_bss_sec->contents,
13525 0, gptab_bss_sec->size))
13529 if (SGI_COMPAT (abfd))
13531 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
13532 if (rtproc_sec != NULL)
13534 if (! bfd_set_section_contents (abfd, rtproc_sec,
13535 rtproc_sec->contents,
13536 0, rtproc_sec->size))
13544 /* Structure for saying that BFD machine EXTENSION extends BASE. */
13546 struct mips_mach_extension {
13547 unsigned long extension, base;
13551 /* An array describing how BFD machines relate to one another. The entries
13552 are ordered topologically with MIPS I extensions listed last. */
13554 static const struct mips_mach_extension mips_mach_extensions[] = {
13555 /* MIPS64r2 extensions. */
13556 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
13557 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
13558 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
13560 /* MIPS64 extensions. */
13561 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
13562 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
13563 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
13564 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
13566 /* MIPS V extensions. */
13567 { bfd_mach_mipsisa64, bfd_mach_mips5 },
13569 /* R10000 extensions. */
13570 { bfd_mach_mips12000, bfd_mach_mips10000 },
13571 { bfd_mach_mips14000, bfd_mach_mips10000 },
13572 { bfd_mach_mips16000, bfd_mach_mips10000 },
13574 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
13575 vr5400 ISA, but doesn't include the multimedia stuff. It seems
13576 better to allow vr5400 and vr5500 code to be merged anyway, since
13577 many libraries will just use the core ISA. Perhaps we could add
13578 some sort of ASE flag if this ever proves a problem. */
13579 { bfd_mach_mips5500, bfd_mach_mips5400 },
13580 { bfd_mach_mips5400, bfd_mach_mips5000 },
13582 /* MIPS IV extensions. */
13583 { bfd_mach_mips5, bfd_mach_mips8000 },
13584 { bfd_mach_mips10000, bfd_mach_mips8000 },
13585 { bfd_mach_mips5000, bfd_mach_mips8000 },
13586 { bfd_mach_mips7000, bfd_mach_mips8000 },
13587 { bfd_mach_mips9000, bfd_mach_mips8000 },
13589 /* VR4100 extensions. */
13590 { bfd_mach_mips4120, bfd_mach_mips4100 },
13591 { bfd_mach_mips4111, bfd_mach_mips4100 },
13593 /* MIPS III extensions. */
13594 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
13595 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
13596 { bfd_mach_mips8000, bfd_mach_mips4000 },
13597 { bfd_mach_mips4650, bfd_mach_mips4000 },
13598 { bfd_mach_mips4600, bfd_mach_mips4000 },
13599 { bfd_mach_mips4400, bfd_mach_mips4000 },
13600 { bfd_mach_mips4300, bfd_mach_mips4000 },
13601 { bfd_mach_mips4100, bfd_mach_mips4000 },
13602 { bfd_mach_mips4010, bfd_mach_mips4000 },
13603 { bfd_mach_mips5900, bfd_mach_mips4000 },
13605 /* MIPS32 extensions. */
13606 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
13608 /* MIPS II extensions. */
13609 { bfd_mach_mips4000, bfd_mach_mips6000 },
13610 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
13612 /* MIPS I extensions. */
13613 { bfd_mach_mips6000, bfd_mach_mips3000 },
13614 { bfd_mach_mips3900, bfd_mach_mips3000 }
13618 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
13621 mips_mach_extends_p (unsigned long base, unsigned long extension)
13625 if (extension == base)
13628 if (base == bfd_mach_mipsisa32
13629 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
13632 if (base == bfd_mach_mipsisa32r2
13633 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
13636 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
13637 if (extension == mips_mach_extensions[i].extension)
13639 extension = mips_mach_extensions[i].base;
13640 if (extension == base)
13648 /* Return true if the given ELF header flags describe a 32-bit binary. */
13651 mips_32bit_flags_p (flagword flags)
13653 return ((flags & EF_MIPS_32BITMODE) != 0
13654 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
13655 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
13656 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
13657 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
13658 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
13659 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
13663 /* Merge object attributes from IBFD into OBFD. Raise an error if
13664 there are conflicting attributes. */
13666 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
13668 obj_attribute *in_attr;
13669 obj_attribute *out_attr;
13672 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
13673 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
13674 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != 0)
13675 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
13677 if (!elf_known_obj_attributes_proc (obfd)[0].i)
13679 /* This is the first object. Copy the attributes. */
13680 _bfd_elf_copy_obj_attributes (ibfd, obfd);
13682 /* Use the Tag_null value to indicate the attributes have been
13684 elf_known_obj_attributes_proc (obfd)[0].i = 1;
13689 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
13690 non-conflicting ones. */
13691 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
13692 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
13694 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
13695 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
13696 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
13697 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != 0)
13698 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
13701 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13705 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13706 obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
13711 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13712 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
13717 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13718 obfd, abi_fp_bfd, ibfd,
13719 "-mdouble-float", "-mips32r2 -mfp64");
13724 (_("Warning: %B uses %s (set by %B), "
13725 "%B uses unknown floating point ABI %d"),
13726 obfd, abi_fp_bfd, ibfd,
13727 "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13733 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13737 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13738 obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
13743 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13744 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
13749 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13750 obfd, abi_fp_bfd, ibfd,
13751 "-msingle-float", "-mips32r2 -mfp64");
13756 (_("Warning: %B uses %s (set by %B), "
13757 "%B uses unknown floating point ABI %d"),
13758 obfd, abi_fp_bfd, ibfd,
13759 "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13765 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13771 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13772 obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
13777 (_("Warning: %B uses %s (set by %B), "
13778 "%B uses unknown floating point ABI %d"),
13779 obfd, abi_fp_bfd, ibfd,
13780 "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13786 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13790 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13791 obfd, abi_fp_bfd, ibfd,
13792 "-mips32r2 -mfp64", "-mdouble-float");
13797 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13798 obfd, abi_fp_bfd, ibfd,
13799 "-mips32r2 -mfp64", "-msingle-float");
13804 (_("Warning: %B uses %s (set by %B), %B uses %s"),
13805 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
13810 (_("Warning: %B uses %s (set by %B), "
13811 "%B uses unknown floating point ABI %d"),
13812 obfd, abi_fp_bfd, ibfd,
13813 "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
13819 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13823 (_("Warning: %B uses unknown floating point ABI %d "
13824 "(set by %B), %B uses %s"),
13825 obfd, abi_fp_bfd, ibfd,
13826 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
13831 (_("Warning: %B uses unknown floating point ABI %d "
13832 "(set by %B), %B uses %s"),
13833 obfd, abi_fp_bfd, ibfd,
13834 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
13839 (_("Warning: %B uses unknown floating point ABI %d "
13840 "(set by %B), %B uses %s"),
13841 obfd, abi_fp_bfd, ibfd,
13842 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
13847 (_("Warning: %B uses unknown floating point ABI %d "
13848 "(set by %B), %B uses %s"),
13849 obfd, abi_fp_bfd, ibfd,
13850 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
13855 (_("Warning: %B uses unknown floating point ABI %d "
13856 "(set by %B), %B uses unknown floating point ABI %d"),
13857 obfd, abi_fp_bfd, ibfd,
13858 out_attr[Tag_GNU_MIPS_ABI_FP].i,
13859 in_attr[Tag_GNU_MIPS_ABI_FP].i);
13866 /* Merge Tag_compatibility attributes and any common GNU ones. */
13867 _bfd_elf_merge_object_attributes (ibfd, obfd);
13872 /* Merge backend specific data from an object file to the output
13873 object file when linking. */
13876 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
13878 flagword old_flags;
13879 flagword new_flags;
13881 bfd_boolean null_input_bfd = TRUE;
13884 /* Check if we have the same endianness. */
13885 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
13887 (*_bfd_error_handler)
13888 (_("%B: endianness incompatible with that of the selected emulation"),
13893 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
13896 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
13898 (*_bfd_error_handler)
13899 (_("%B: ABI is incompatible with that of the selected emulation"),
13904 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
13907 new_flags = elf_elfheader (ibfd)->e_flags;
13908 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
13909 old_flags = elf_elfheader (obfd)->e_flags;
13911 if (! elf_flags_init (obfd))
13913 elf_flags_init (obfd) = TRUE;
13914 elf_elfheader (obfd)->e_flags = new_flags;
13915 elf_elfheader (obfd)->e_ident[EI_CLASS]
13916 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
13918 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
13919 && (bfd_get_arch_info (obfd)->the_default
13920 || mips_mach_extends_p (bfd_get_mach (obfd),
13921 bfd_get_mach (ibfd))))
13923 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
13924 bfd_get_mach (ibfd)))
13931 /* Check flag compatibility. */
13933 new_flags &= ~EF_MIPS_NOREORDER;
13934 old_flags &= ~EF_MIPS_NOREORDER;
13936 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
13937 doesn't seem to matter. */
13938 new_flags &= ~EF_MIPS_XGOT;
13939 old_flags &= ~EF_MIPS_XGOT;
13941 /* MIPSpro generates ucode info in n64 objects. Again, we should
13942 just be able to ignore this. */
13943 new_flags &= ~EF_MIPS_UCODE;
13944 old_flags &= ~EF_MIPS_UCODE;
13946 /* DSOs should only be linked with CPIC code. */
13947 if ((ibfd->flags & DYNAMIC) != 0)
13948 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
13950 if (new_flags == old_flags)
13953 /* Check to see if the input BFD actually contains any sections.
13954 If not, its flags may not have been initialised either, but it cannot
13955 actually cause any incompatibility. */
13956 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
13958 /* Ignore synthetic sections and empty .text, .data and .bss sections
13959 which are automatically generated by gas. Also ignore fake
13960 (s)common sections, since merely defining a common symbol does
13961 not affect compatibility. */
13962 if ((sec->flags & SEC_IS_COMMON) == 0
13963 && strcmp (sec->name, ".reginfo")
13964 && strcmp (sec->name, ".mdebug")
13966 || (strcmp (sec->name, ".text")
13967 && strcmp (sec->name, ".data")
13968 && strcmp (sec->name, ".bss"))))
13970 null_input_bfd = FALSE;
13974 if (null_input_bfd)
13979 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
13980 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
13982 (*_bfd_error_handler)
13983 (_("%B: warning: linking abicalls files with non-abicalls files"),
13988 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
13989 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
13990 if (! (new_flags & EF_MIPS_PIC))
13991 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
13993 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13994 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13996 /* Compare the ISAs. */
13997 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
13999 (*_bfd_error_handler)
14000 (_("%B: linking 32-bit code with 64-bit code"),
14004 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14006 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14007 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
14009 /* Copy the architecture info from IBFD to OBFD. Also copy
14010 the 32-bit flag (if set) so that we continue to recognise
14011 OBFD as a 32-bit binary. */
14012 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14013 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14014 elf_elfheader (obfd)->e_flags
14015 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14017 /* Copy across the ABI flags if OBFD doesn't use them
14018 and if that was what caused us to treat IBFD as 32-bit. */
14019 if ((old_flags & EF_MIPS_ABI) == 0
14020 && mips_32bit_flags_p (new_flags)
14021 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14022 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
14026 /* The ISAs aren't compatible. */
14027 (*_bfd_error_handler)
14028 (_("%B: linking %s module with previous %s modules"),
14030 bfd_printable_name (ibfd),
14031 bfd_printable_name (obfd));
14036 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14037 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14039 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
14040 does set EI_CLASS differently from any 32-bit ABI. */
14041 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14042 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14043 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14045 /* Only error if both are set (to different values). */
14046 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14047 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14048 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14050 (*_bfd_error_handler)
14051 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14053 elf_mips_abi_name (ibfd),
14054 elf_mips_abi_name (obfd));
14057 new_flags &= ~EF_MIPS_ABI;
14058 old_flags &= ~EF_MIPS_ABI;
14061 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14062 and allow arbitrary mixing of the remaining ASEs (retain the union). */
14063 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14065 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14066 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14067 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14068 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14069 int micro_mis = old_m16 && new_micro;
14070 int m16_mis = old_micro && new_m16;
14072 if (m16_mis || micro_mis)
14074 (*_bfd_error_handler)
14075 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14077 m16_mis ? "MIPS16" : "microMIPS",
14078 m16_mis ? "microMIPS" : "MIPS16");
14082 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14084 new_flags &= ~ EF_MIPS_ARCH_ASE;
14085 old_flags &= ~ EF_MIPS_ARCH_ASE;
14088 /* Warn about any other mismatches */
14089 if (new_flags != old_flags)
14091 (*_bfd_error_handler)
14092 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14093 ibfd, (unsigned long) new_flags,
14094 (unsigned long) old_flags);
14100 bfd_set_error (bfd_error_bad_value);
14107 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14110 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
14112 BFD_ASSERT (!elf_flags_init (abfd)
14113 || elf_elfheader (abfd)->e_flags == flags);
14115 elf_elfheader (abfd)->e_flags = flags;
14116 elf_flags_init (abfd) = TRUE;
14121 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14125 default: return "";
14126 case DT_MIPS_RLD_VERSION:
14127 return "MIPS_RLD_VERSION";
14128 case DT_MIPS_TIME_STAMP:
14129 return "MIPS_TIME_STAMP";
14130 case DT_MIPS_ICHECKSUM:
14131 return "MIPS_ICHECKSUM";
14132 case DT_MIPS_IVERSION:
14133 return "MIPS_IVERSION";
14134 case DT_MIPS_FLAGS:
14135 return "MIPS_FLAGS";
14136 case DT_MIPS_BASE_ADDRESS:
14137 return "MIPS_BASE_ADDRESS";
14139 return "MIPS_MSYM";
14140 case DT_MIPS_CONFLICT:
14141 return "MIPS_CONFLICT";
14142 case DT_MIPS_LIBLIST:
14143 return "MIPS_LIBLIST";
14144 case DT_MIPS_LOCAL_GOTNO:
14145 return "MIPS_LOCAL_GOTNO";
14146 case DT_MIPS_CONFLICTNO:
14147 return "MIPS_CONFLICTNO";
14148 case DT_MIPS_LIBLISTNO:
14149 return "MIPS_LIBLISTNO";
14150 case DT_MIPS_SYMTABNO:
14151 return "MIPS_SYMTABNO";
14152 case DT_MIPS_UNREFEXTNO:
14153 return "MIPS_UNREFEXTNO";
14154 case DT_MIPS_GOTSYM:
14155 return "MIPS_GOTSYM";
14156 case DT_MIPS_HIPAGENO:
14157 return "MIPS_HIPAGENO";
14158 case DT_MIPS_RLD_MAP:
14159 return "MIPS_RLD_MAP";
14160 case DT_MIPS_DELTA_CLASS:
14161 return "MIPS_DELTA_CLASS";
14162 case DT_MIPS_DELTA_CLASS_NO:
14163 return "MIPS_DELTA_CLASS_NO";
14164 case DT_MIPS_DELTA_INSTANCE:
14165 return "MIPS_DELTA_INSTANCE";
14166 case DT_MIPS_DELTA_INSTANCE_NO:
14167 return "MIPS_DELTA_INSTANCE_NO";
14168 case DT_MIPS_DELTA_RELOC:
14169 return "MIPS_DELTA_RELOC";
14170 case DT_MIPS_DELTA_RELOC_NO:
14171 return "MIPS_DELTA_RELOC_NO";
14172 case DT_MIPS_DELTA_SYM:
14173 return "MIPS_DELTA_SYM";
14174 case DT_MIPS_DELTA_SYM_NO:
14175 return "MIPS_DELTA_SYM_NO";
14176 case DT_MIPS_DELTA_CLASSSYM:
14177 return "MIPS_DELTA_CLASSSYM";
14178 case DT_MIPS_DELTA_CLASSSYM_NO:
14179 return "MIPS_DELTA_CLASSSYM_NO";
14180 case DT_MIPS_CXX_FLAGS:
14181 return "MIPS_CXX_FLAGS";
14182 case DT_MIPS_PIXIE_INIT:
14183 return "MIPS_PIXIE_INIT";
14184 case DT_MIPS_SYMBOL_LIB:
14185 return "MIPS_SYMBOL_LIB";
14186 case DT_MIPS_LOCALPAGE_GOTIDX:
14187 return "MIPS_LOCALPAGE_GOTIDX";
14188 case DT_MIPS_LOCAL_GOTIDX:
14189 return "MIPS_LOCAL_GOTIDX";
14190 case DT_MIPS_HIDDEN_GOTIDX:
14191 return "MIPS_HIDDEN_GOTIDX";
14192 case DT_MIPS_PROTECTED_GOTIDX:
14193 return "MIPS_PROTECTED_GOT_IDX";
14194 case DT_MIPS_OPTIONS:
14195 return "MIPS_OPTIONS";
14196 case DT_MIPS_INTERFACE:
14197 return "MIPS_INTERFACE";
14198 case DT_MIPS_DYNSTR_ALIGN:
14199 return "DT_MIPS_DYNSTR_ALIGN";
14200 case DT_MIPS_INTERFACE_SIZE:
14201 return "DT_MIPS_INTERFACE_SIZE";
14202 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14203 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14204 case DT_MIPS_PERF_SUFFIX:
14205 return "DT_MIPS_PERF_SUFFIX";
14206 case DT_MIPS_COMPACT_SIZE:
14207 return "DT_MIPS_COMPACT_SIZE";
14208 case DT_MIPS_GP_VALUE:
14209 return "DT_MIPS_GP_VALUE";
14210 case DT_MIPS_AUX_DYNAMIC:
14211 return "DT_MIPS_AUX_DYNAMIC";
14212 case DT_MIPS_PLTGOT:
14213 return "DT_MIPS_PLTGOT";
14214 case DT_MIPS_RWPLT:
14215 return "DT_MIPS_RWPLT";
14220 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
14224 BFD_ASSERT (abfd != NULL && ptr != NULL);
14226 /* Print normal ELF private data. */
14227 _bfd_elf_print_private_bfd_data (abfd, ptr);
14229 /* xgettext:c-format */
14230 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14232 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14233 fprintf (file, _(" [abi=O32]"));
14234 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14235 fprintf (file, _(" [abi=O64]"));
14236 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14237 fprintf (file, _(" [abi=EABI32]"));
14238 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14239 fprintf (file, _(" [abi=EABI64]"));
14240 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14241 fprintf (file, _(" [abi unknown]"));
14242 else if (ABI_N32_P (abfd))
14243 fprintf (file, _(" [abi=N32]"));
14244 else if (ABI_64_P (abfd))
14245 fprintf (file, _(" [abi=64]"));
14247 fprintf (file, _(" [no abi set]"));
14249 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
14250 fprintf (file, " [mips1]");
14251 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
14252 fprintf (file, " [mips2]");
14253 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
14254 fprintf (file, " [mips3]");
14255 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
14256 fprintf (file, " [mips4]");
14257 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
14258 fprintf (file, " [mips5]");
14259 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
14260 fprintf (file, " [mips32]");
14261 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
14262 fprintf (file, " [mips64]");
14263 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
14264 fprintf (file, " [mips32r2]");
14265 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
14266 fprintf (file, " [mips64r2]");
14268 fprintf (file, _(" [unknown ISA]"));
14270 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14271 fprintf (file, " [mdmx]");
14273 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14274 fprintf (file, " [mips16]");
14276 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14277 fprintf (file, " [micromips]");
14279 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
14280 fprintf (file, " [32bitmode]");
14282 fprintf (file, _(" [not 32bitmode]"));
14284 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
14285 fprintf (file, " [noreorder]");
14287 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
14288 fprintf (file, " [PIC]");
14290 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
14291 fprintf (file, " [CPIC]");
14293 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
14294 fprintf (file, " [XGOT]");
14296 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
14297 fprintf (file, " [UCODE]");
14299 fputc ('\n', file);
14304 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
14306 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14307 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14308 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14309 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14310 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14311 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14312 { NULL, 0, 0, 0, 0 }
14315 /* Merge non visibility st_other attributes. Ensure that the
14316 STO_OPTIONAL flag is copied into h->other, even if this is not a
14317 definiton of the symbol. */
14319 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
14320 const Elf_Internal_Sym *isym,
14321 bfd_boolean definition,
14322 bfd_boolean dynamic ATTRIBUTE_UNUSED)
14324 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
14326 unsigned char other;
14328 other = (definition ? isym->st_other : h->other);
14329 other &= ~ELF_ST_VISIBILITY (-1);
14330 h->other = other | ELF_ST_VISIBILITY (h->other);
14334 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
14335 h->other |= STO_OPTIONAL;
14338 /* Decide whether an undefined symbol is special and can be ignored.
14339 This is the case for OPTIONAL symbols on IRIX. */
14341 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
14343 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
14347 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
14349 return (sym->st_shndx == SHN_COMMON
14350 || sym->st_shndx == SHN_MIPS_ACOMMON
14351 || sym->st_shndx == SHN_MIPS_SCOMMON);
14354 /* Return address for Ith PLT stub in section PLT, for relocation REL
14355 or (bfd_vma) -1 if it should not be included. */
14358 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
14359 const arelent *rel ATTRIBUTE_UNUSED)
14362 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
14363 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
14367 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
14369 struct mips_elf_link_hash_table *htab;
14370 Elf_Internal_Ehdr *i_ehdrp;
14372 i_ehdrp = elf_elfheader (abfd);
14375 htab = mips_elf_hash_table (link_info);
14376 BFD_ASSERT (htab != NULL);
14378 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
14379 i_ehdrp->e_ident[EI_ABIVERSION] = 1;