1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
26 different MIPS ELF from other targets. This matters when linking.
27 This file supports both, switching at runtime. */
37 /* Get the ECOFF swapping routines. */
39 #include "coff/symconst.h"
40 #include "coff/internal.h"
41 #include "coff/ecoff.h"
42 #include "coff/mips.h"
44 #include "ecoffswap.h"
46 /* This structure is used to hold .got information when linking. It
47 is stored in the tdata field of the bfd_elf_section_data structure. */
51 /* The global symbol in the GOT with the lowest index in the dynamic
53 struct elf_link_hash_entry *global_gotsym;
54 /* The number of local .got entries. */
55 unsigned int local_gotno;
56 /* The number of local .got entries we have used. */
57 unsigned int assigned_gotno;
60 /* The MIPS ELF linker needs additional information for each symbol in
61 the global hash table. */
63 struct mips_elf_link_hash_entry
65 struct elf_link_hash_entry root;
67 /* External symbol information. */
70 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
71 unsigned int mips_32_relocs;
73 /* The index of the first dynamic relocation (in the .rel.dyn
74 section) against this symbol. */
75 unsigned int min_dyn_reloc_index;
77 /* If there is a stub that 32 bit functions should use to call this
78 16 bit function, this points to the section containing the stub. */
81 /* Whether we need the fn_stub; this is set if this symbol appears
82 in any relocs other than a 16 bit call. */
85 /* If there is a stub that 16 bit functions should use to call this
86 32 bit function, this points to the section containing the stub. */
89 /* This is like the call_stub field, but it is used if the function
90 being called returns a floating point value. */
91 asection *call_fp_stub;
94 static bfd_reloc_status_type mips32_64bit_reloc
95 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
96 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
97 PARAMS ((bfd *, bfd_reloc_code_real_type));
98 static void mips_info_to_howto_rel
99 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
100 static void mips_info_to_howto_rela
101 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
102 static void bfd_mips_elf32_swap_gptab_in
103 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
104 static void bfd_mips_elf32_swap_gptab_out
105 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
106 static void bfd_mips_elf_swap_msym_in
107 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
108 static void bfd_mips_elf_swap_msym_out
109 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
110 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
111 static boolean mips_elf_create_procedure_table
112 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
113 struct ecoff_debug_info *));
114 static INLINE int elf_mips_isa PARAMS ((flagword));
115 static INLINE int elf_mips_mach PARAMS ((flagword));
116 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
117 static boolean mips_elf_is_local_label_name
118 PARAMS ((bfd *, const char *));
119 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
120 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
121 static int gptab_compare PARAMS ((const void *, const void *));
122 static void mips_elf_relocate_hi16
123 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
125 static boolean mips_elf_relocate_got_local
126 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
127 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
128 static void mips_elf_relocate_global_got
129 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
130 static bfd_reloc_status_type mips16_jump_reloc
131 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
132 static bfd_reloc_status_type mips16_gprel_reloc
133 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
134 static boolean mips_elf_create_compact_rel_section
135 PARAMS ((bfd *, struct bfd_link_info *));
136 static boolean mips_elf_create_got_section
137 PARAMS ((bfd *, struct bfd_link_info *));
138 static bfd_reloc_status_type mips_elf_final_gp
139 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
140 static bfd_byte *elf32_mips_get_relocated_section_contents
141 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
142 bfd_byte *, boolean, asymbol **));
143 static asection *mips_elf_create_msym_section
145 static void mips_elf_irix6_finish_dynamic_symbol
146 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
147 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
148 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
149 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
150 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
151 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
152 static bfd_vma mips_elf_global_got_index
153 PARAMS ((bfd *, struct elf_link_hash_entry *));
154 static bfd_vma mips_elf_local_got_index
155 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
156 static bfd_vma mips_elf_got_offset_from_index
157 PARAMS ((bfd *, bfd *, bfd_vma));
158 static boolean mips_elf_record_global_got_symbol
159 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
160 struct mips_got_info *));
161 static bfd_vma mips_elf_got_page
162 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
163 static boolean mips_elf_next_lo16_addend
164 PARAMS ((const Elf_Internal_Rela *, const Elf_Internal_Rela *, bfd_vma *));
165 static bfd_reloc_status_type mips_elf_calculate_relocation
166 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
167 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
168 Elf_Internal_Sym *, asection **, bfd_vma *, const char **));
169 static bfd_vma mips_elf_obtain_contents
170 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
171 static void mips_elf_perform_relocation
172 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd_vma,
174 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
175 static boolean mips_elf_sort_hash_table_f
176 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
177 static boolean mips_elf_sort_hash_table
178 PARAMS ((struct bfd_link_info *));
179 static asection * mips_elf_got_section PARAMS ((bfd *));
180 static struct mips_got_info *mips_elf_got_info
181 PARAMS ((bfd *, asection **));
182 static bfd_vma mips_elf_create_local_got_entry
183 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
184 static bfd_vma mips_elf_got16_entry
185 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
186 static unsigned int mips_elf_create_dynamic_relocation
187 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
188 long, bfd_vma, asection *));
189 static void mips_elf_allocate_dynamic_relocations
190 PARAMS ((bfd *, unsigned int));
192 /* The level of IRIX compatibility we're striving for. */
200 /* Nonzero if ABFD is using the N32 ABI. */
202 #define ABI_N32_P(abfd) \
203 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
205 /* Nonzero if ABFD is using the 64-bit ABI. FIXME: This is never
207 #define ABI_64_P(abfd) \
208 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
210 /* What version of Irix we are trying to be compatible with. FIXME:
211 At the moment, we never generate "normal" MIPS ELF ABI executables;
212 we always use some version of Irix. */
214 #define IRIX_COMPAT(abfd) \
215 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5)
217 /* Whether we are trying to be compatible with IRIX at all. */
219 #define SGI_COMPAT(abfd) \
220 (IRIX_COMPAT (abfd) != ict_none)
222 /* The name of the msym section. */
223 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
225 /* The name of the srdata section. */
226 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
228 /* The name of the options section. */
229 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
230 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
232 /* The name of the stub section. */
233 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
234 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
236 /* The name of the dynamic relocation section. */
237 #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
239 /* The size of an external REL relocation. */
240 #define MIPS_ELF_REL_SIZE(abfd) \
241 (get_elf_backend_data (abfd)->s->sizeof_rel)
243 /* The size of an external dynamic table entry. */
244 #define MIPS_ELF_DYN_SIZE(abfd) \
245 (get_elf_backend_data (abfd)->s->sizeof_dyn)
247 /* The size of a GOT entry. */
248 #define MIPS_ELF_GOT_SIZE(abfd) \
249 (get_elf_backend_data (abfd)->s->arch_size / 8)
251 /* The size of a symbol-table entry. */
252 #define MIPS_ELF_SYM_SIZE(abfd) \
253 (get_elf_backend_data (abfd)->s->sizeof_sym)
255 /* The default alignment for sections, as a power of two. */
256 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
257 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
259 /* Get word-sized data. */
260 #define MIPS_ELF_GET_WORD(abfd, ptr) \
261 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
263 /* Put out word-sized data. */
264 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
266 ? bfd_put_64 (abfd, val, ptr) \
267 : bfd_put_32 (abfd, val, ptr))
269 /* Add a dynamic symbol table-entry. */
271 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
272 (ABI_64_P (elf_hash_table (info)->dynobj) \
273 ? bfd_elf64_add_dynamic_entry (info, tag, val) \
274 : bfd_elf32_add_dynamic_entry (info, tag, val))
276 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
277 (ABI_64_P (elf_hash_table (info)->dynobj) \
278 ? (abort (), false) \
279 : bfd_elf32_add_dynamic_entry (info, tag, val))
282 /* The number of local .got entries we reserve. */
283 #define MIPS_RESERVED_GOTNO (2)
285 /* Instructions which appear in a stub. For some reason the stub is
286 slightly different on an SGI system. */
287 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
288 #define STUB_LW(abfd) \
291 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
292 : 0x8f998010) /* lw t9,0x8010(gp) */ \
293 : 0x8f998000) /* lw t9,0x8000(gp) */
294 #define STUB_MOVE 0x03e07825 /* move t7,ra */
295 #define STUB_JALR 0x0320f809 /* jal t9 */
296 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
297 #define MIPS_FUNCTION_STUB_SIZE (16)
300 /* We no longer try to identify particular sections for the .dynsym
301 section. When we do, we wind up crashing if there are other random
302 sections with relocations. */
304 /* Names of sections which appear in the .dynsym section in an Irix 5
307 static const char * const mips_elf_dynsym_sec_names[] =
320 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
321 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
323 /* The number of entries in mips_elf_dynsym_sec_names which go in the
326 #define MIPS_TEXT_DYNSYM_SECNO (3)
330 /* The names of the runtime procedure table symbols used on Irix 5. */
332 static const char * const mips_elf_dynsym_rtproc_names[] =
335 "_procedure_string_table",
336 "_procedure_table_size",
340 /* These structures are used to generate the .compact_rel section on
345 unsigned long id1; /* Always one? */
346 unsigned long num; /* Number of compact relocation entries. */
347 unsigned long id2; /* Always two? */
348 unsigned long offset; /* The file offset of the first relocation. */
349 unsigned long reserved0; /* Zero? */
350 unsigned long reserved1; /* Zero? */
359 bfd_byte reserved0[4];
360 bfd_byte reserved1[4];
361 } Elf32_External_compact_rel;
365 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
366 unsigned int rtype : 4; /* Relocation types. See below. */
367 unsigned int dist2to : 8;
368 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
369 unsigned long konst; /* KONST field. See below. */
370 unsigned long vaddr; /* VADDR to be relocated. */
375 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
376 unsigned int rtype : 4; /* Relocation types. See below. */
377 unsigned int dist2to : 8;
378 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
379 unsigned long konst; /* KONST field. See below. */
387 } Elf32_External_crinfo;
393 } Elf32_External_crinfo2;
395 /* These are the constants used to swap the bitfields in a crinfo. */
397 #define CRINFO_CTYPE (0x1)
398 #define CRINFO_CTYPE_SH (31)
399 #define CRINFO_RTYPE (0xf)
400 #define CRINFO_RTYPE_SH (27)
401 #define CRINFO_DIST2TO (0xff)
402 #define CRINFO_DIST2TO_SH (19)
403 #define CRINFO_RELVADDR (0x7ffff)
404 #define CRINFO_RELVADDR_SH (0)
406 /* A compact relocation info has long (3 words) or short (2 words)
407 formats. A short format doesn't have VADDR field and relvaddr
408 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
409 #define CRF_MIPS_LONG 1
410 #define CRF_MIPS_SHORT 0
412 /* There are 4 types of compact relocation at least. The value KONST
413 has different meaning for each type:
416 CT_MIPS_REL32 Address in data
417 CT_MIPS_WORD Address in word (XXX)
418 CT_MIPS_GPHI_LO GP - vaddr
419 CT_MIPS_JMPAD Address to jump
422 #define CRT_MIPS_REL32 0xa
423 #define CRT_MIPS_WORD 0xb
424 #define CRT_MIPS_GPHI_LO 0xc
425 #define CRT_MIPS_JMPAD 0xd
427 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
428 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
429 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
430 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
432 static void bfd_elf32_swap_compact_rel_out
433 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
434 static void bfd_elf32_swap_crinfo_out
435 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
437 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
439 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
440 from smaller values. Start with zero, widen, *then* decrement. */
441 #define MINUS_ONE (((bfd_vma)0) - 1)
443 static reloc_howto_type elf_mips_howto_table[] =
446 HOWTO (R_MIPS_NONE, /* type */
448 0, /* size (0 = byte, 1 = short, 2 = long) */
450 false, /* pc_relative */
452 complain_overflow_dont, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_MIPS_NONE", /* name */
455 false, /* partial_inplace */
458 false), /* pcrel_offset */
460 /* 16 bit relocation. */
461 HOWTO (R_MIPS_16, /* type */
463 1, /* size (0 = byte, 1 = short, 2 = long) */
465 false, /* pc_relative */
467 complain_overflow_bitfield, /* complain_on_overflow */
468 bfd_elf_generic_reloc, /* special_function */
469 "R_MIPS_16", /* name */
470 true, /* partial_inplace */
471 0xffff, /* src_mask */
472 0xffff, /* dst_mask */
473 false), /* pcrel_offset */
475 /* 32 bit relocation. */
476 HOWTO (R_MIPS_32, /* type */
478 2, /* size (0 = byte, 1 = short, 2 = long) */
480 false, /* pc_relative */
482 complain_overflow_bitfield, /* complain_on_overflow */
483 bfd_elf_generic_reloc, /* special_function */
484 "R_MIPS_32", /* name */
485 true, /* partial_inplace */
486 0xffffffff, /* src_mask */
487 0xffffffff, /* dst_mask */
488 false), /* pcrel_offset */
490 /* 32 bit symbol relative relocation. */
491 HOWTO (R_MIPS_REL32, /* type */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
495 false, /* pc_relative */
497 complain_overflow_bitfield, /* complain_on_overflow */
498 bfd_elf_generic_reloc, /* special_function */
499 "R_MIPS_REL32", /* name */
500 true, /* partial_inplace */
501 0xffffffff, /* src_mask */
502 0xffffffff, /* dst_mask */
503 false), /* pcrel_offset */
505 /* 26 bit branch address. */
506 HOWTO (R_MIPS_26, /* type */
508 2, /* size (0 = byte, 1 = short, 2 = long) */
510 false, /* pc_relative */
512 complain_overflow_dont, /* complain_on_overflow */
513 /* This needs complex overflow
514 detection, because the upper four
515 bits must match the PC. */
516 bfd_elf_generic_reloc, /* special_function */
517 "R_MIPS_26", /* name */
518 true, /* partial_inplace */
519 0x3ffffff, /* src_mask */
520 0x3ffffff, /* dst_mask */
521 false), /* pcrel_offset */
523 /* High 16 bits of symbol value. */
524 HOWTO (R_MIPS_HI16, /* type */
526 2, /* size (0 = byte, 1 = short, 2 = long) */
528 false, /* pc_relative */
530 complain_overflow_dont, /* complain_on_overflow */
531 _bfd_mips_elf_hi16_reloc, /* special_function */
532 "R_MIPS_HI16", /* name */
533 true, /* partial_inplace */
534 0xffff, /* src_mask */
535 0xffff, /* dst_mask */
536 false), /* pcrel_offset */
538 /* Low 16 bits of symbol value. */
539 HOWTO (R_MIPS_LO16, /* type */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
543 false, /* pc_relative */
545 complain_overflow_dont, /* complain_on_overflow */
546 _bfd_mips_elf_lo16_reloc, /* special_function */
547 "R_MIPS_LO16", /* name */
548 true, /* partial_inplace */
549 0xffff, /* src_mask */
550 0xffff, /* dst_mask */
551 false), /* pcrel_offset */
553 /* GP relative reference. */
554 HOWTO (R_MIPS_GPREL16, /* type */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
558 false, /* pc_relative */
560 complain_overflow_signed, /* complain_on_overflow */
561 _bfd_mips_elf_gprel16_reloc, /* special_function */
562 "R_MIPS_GPREL16", /* name */
563 true, /* partial_inplace */
564 0xffff, /* src_mask */
565 0xffff, /* dst_mask */
566 false), /* pcrel_offset */
568 /* Reference to literal section. */
569 HOWTO (R_MIPS_LITERAL, /* type */
571 2, /* size (0 = byte, 1 = short, 2 = long) */
573 false, /* pc_relative */
575 complain_overflow_signed, /* complain_on_overflow */
576 _bfd_mips_elf_gprel16_reloc, /* special_function */
577 "R_MIPS_LITERAL", /* name */
578 true, /* partial_inplace */
579 0xffff, /* src_mask */
580 0xffff, /* dst_mask */
581 false), /* pcrel_offset */
583 /* Reference to global offset table. */
584 HOWTO (R_MIPS_GOT16, /* type */
586 2, /* size (0 = byte, 1 = short, 2 = long) */
588 false, /* pc_relative */
590 complain_overflow_signed, /* complain_on_overflow */
591 _bfd_mips_elf_got16_reloc, /* special_function */
592 "R_MIPS_GOT16", /* name */
593 false, /* partial_inplace */
595 0xffff, /* dst_mask */
596 false), /* pcrel_offset */
598 /* 16 bit PC relative reference. */
599 HOWTO (R_MIPS_PC16, /* type */
601 2, /* size (0 = byte, 1 = short, 2 = long) */
603 true, /* pc_relative */
605 complain_overflow_signed, /* complain_on_overflow */
606 bfd_elf_generic_reloc, /* special_function */
607 "R_MIPS_PC16", /* name */
608 true, /* partial_inplace */
609 0xffff, /* src_mask */
610 0xffff, /* dst_mask */
611 false), /* pcrel_offset */
613 /* 16 bit call through global offset table. */
614 HOWTO (R_MIPS_CALL16, /* type */
616 2, /* size (0 = byte, 1 = short, 2 = long) */
618 false, /* pc_relative */
620 complain_overflow_signed, /* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_MIPS_CALL16", /* name */
623 false, /* partial_inplace */
625 0xffff, /* dst_mask */
626 false), /* pcrel_offset */
628 /* 32 bit GP relative reference. */
629 HOWTO (R_MIPS_GPREL32, /* type */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
633 false, /* pc_relative */
635 complain_overflow_bitfield, /* complain_on_overflow */
636 _bfd_mips_elf_gprel32_reloc, /* special_function */
637 "R_MIPS_GPREL32", /* name */
638 true, /* partial_inplace */
639 0xffffffff, /* src_mask */
640 0xffffffff, /* dst_mask */
641 false), /* pcrel_offset */
643 /* The remaining relocs are defined on Irix 5, although they are
644 not defined by the ABI. */
649 /* A 5 bit shift field. */
650 HOWTO (R_MIPS_SHIFT5, /* type */
652 2, /* size (0 = byte, 1 = short, 2 = long) */
654 false, /* pc_relative */
656 complain_overflow_bitfield, /* complain_on_overflow */
657 bfd_elf_generic_reloc, /* special_function */
658 "R_MIPS_SHIFT5", /* name */
659 true, /* partial_inplace */
660 0x000007c0, /* src_mask */
661 0x000007c0, /* dst_mask */
662 false), /* pcrel_offset */
664 /* A 6 bit shift field. */
665 /* FIXME: This is not handled correctly; a special function is
666 needed to put the most significant bit in the right place. */
667 HOWTO (R_MIPS_SHIFT6, /* type */
669 2, /* size (0 = byte, 1 = short, 2 = long) */
671 false, /* pc_relative */
673 complain_overflow_bitfield, /* complain_on_overflow */
674 bfd_elf_generic_reloc, /* special_function */
675 "R_MIPS_SHIFT6", /* name */
676 true, /* partial_inplace */
677 0x000007c4, /* src_mask */
678 0x000007c4, /* dst_mask */
679 false), /* pcrel_offset */
681 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
682 are 64 bits long; the upper 32 bits are simply a sign extension.
683 The fields of the howto should be the same as for R_MIPS_32,
684 other than the type, name, and special_function. */
685 HOWTO (R_MIPS_64, /* type */
687 2, /* size (0 = byte, 1 = short, 2 = long) */
689 false, /* pc_relative */
691 complain_overflow_bitfield, /* complain_on_overflow */
692 mips32_64bit_reloc, /* special_function */
693 "R_MIPS_64", /* name */
694 true, /* partial_inplace */
695 0xffffffff, /* src_mask */
696 0xffffffff, /* dst_mask */
697 false), /* pcrel_offset */
699 /* Displacement in the global offset table. */
700 HOWTO (R_MIPS_GOT_DISP, /* type */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
704 false, /* pc_relative */
706 complain_overflow_bitfield, /* complain_on_overflow */
707 bfd_elf_generic_reloc, /* special_function */
708 "R_MIPS_GOT_DISP", /* name */
709 true, /* partial_inplace */
710 0x0000ffff, /* src_mask */
711 0x0000ffff, /* dst_mask */
712 false), /* pcrel_offset */
714 /* Displacement to page pointer in the global offset table. */
715 HOWTO (R_MIPS_GOT_PAGE, /* type */
717 2, /* size (0 = byte, 1 = short, 2 = long) */
719 false, /* pc_relative */
721 complain_overflow_bitfield, /* complain_on_overflow */
722 bfd_elf_generic_reloc, /* special_function */
723 "R_MIPS_GOT_PAGE", /* name */
724 true, /* partial_inplace */
725 0x0000ffff, /* src_mask */
726 0x0000ffff, /* dst_mask */
727 false), /* pcrel_offset */
729 /* Offset from page pointer in the global offset table. */
730 HOWTO (R_MIPS_GOT_OFST, /* type */
732 2, /* size (0 = byte, 1 = short, 2 = long) */
734 false, /* pc_relative */
736 complain_overflow_bitfield, /* complain_on_overflow */
737 bfd_elf_generic_reloc, /* special_function */
738 "R_MIPS_GOT_OFST", /* name */
739 true, /* partial_inplace */
740 0x0000ffff, /* src_mask */
741 0x0000ffff, /* dst_mask */
742 false), /* pcrel_offset */
744 /* High 16 bits of displacement in global offset table. */
745 HOWTO (R_MIPS_GOT_HI16, /* type */
747 2, /* size (0 = byte, 1 = short, 2 = long) */
749 false, /* pc_relative */
751 complain_overflow_dont, /* complain_on_overflow */
752 bfd_elf_generic_reloc, /* special_function */
753 "R_MIPS_GOT_HI16", /* name */
754 true, /* partial_inplace */
755 0x0000ffff, /* src_mask */
756 0x0000ffff, /* dst_mask */
757 false), /* pcrel_offset */
759 /* Low 16 bits of displacement in global offset table. */
760 HOWTO (R_MIPS_GOT_LO16, /* type */
762 2, /* size (0 = byte, 1 = short, 2 = long) */
764 false, /* pc_relative */
766 complain_overflow_dont, /* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
768 "R_MIPS_GOT_LO16", /* name */
769 true, /* partial_inplace */
770 0x0000ffff, /* src_mask */
771 0x0000ffff, /* dst_mask */
772 false), /* pcrel_offset */
774 /* 64 bit subtraction. Used in the N32 ABI. */
775 HOWTO (R_MIPS_SUB, /* type */
777 4, /* size (0 = byte, 1 = short, 2 = long) */
779 false, /* pc_relative */
781 complain_overflow_bitfield, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
783 "R_MIPS_SUB", /* name */
784 true, /* partial_inplace */
785 MINUS_ONE, /* src_mask */
786 MINUS_ONE, /* dst_mask */
787 false), /* pcrel_offset */
789 /* Used to cause the linker to insert and delete instructions? */
790 EMPTY_HOWTO (R_MIPS_INSERT_A),
791 EMPTY_HOWTO (R_MIPS_INSERT_B),
792 EMPTY_HOWTO (R_MIPS_DELETE),
794 /* Get the higher value of a 64 bit addend. */
795 HOWTO (R_MIPS_HIGHER, /* type */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
799 false, /* pc_relative */
801 complain_overflow_dont, /* complain_on_overflow */
802 bfd_elf_generic_reloc, /* special_function */
803 "R_MIPS_HIGHER", /* name */
804 true, /* partial_inplace */
806 0xffff, /* dst_mask */
807 false), /* pcrel_offset */
809 /* Get the highest value of a 64 bit addend. */
810 HOWTO (R_MIPS_HIGHEST, /* type */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
814 false, /* pc_relative */
816 complain_overflow_dont, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
818 "R_MIPS_HIGHEST", /* name */
819 true, /* partial_inplace */
821 0xffff, /* dst_mask */
822 false), /* pcrel_offset */
824 /* High 16 bits of displacement in global offset table. */
825 HOWTO (R_MIPS_CALL_HI16, /* type */
827 2, /* size (0 = byte, 1 = short, 2 = long) */
829 false, /* pc_relative */
831 complain_overflow_dont, /* complain_on_overflow */
832 bfd_elf_generic_reloc, /* special_function */
833 "R_MIPS_CALL_HI16", /* name */
834 true, /* partial_inplace */
835 0x0000ffff, /* src_mask */
836 0x0000ffff, /* dst_mask */
837 false), /* pcrel_offset */
839 /* Low 16 bits of displacement in global offset table. */
840 HOWTO (R_MIPS_CALL_LO16, /* type */
842 2, /* size (0 = byte, 1 = short, 2 = long) */
844 false, /* pc_relative */
846 complain_overflow_dont, /* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_MIPS_CALL_LO16", /* name */
849 true, /* partial_inplace */
850 0x0000ffff, /* src_mask */
851 0x0000ffff, /* dst_mask */
852 false), /* pcrel_offset */
854 /* Section displacement. */
855 HOWTO (R_MIPS_SCN_DISP, /* type */
857 2, /* size (0 = byte, 1 = short, 2 = long) */
859 false, /* pc_relative */
861 complain_overflow_dont, /* complain_on_overflow */
862 bfd_elf_generic_reloc, /* special_function */
863 "R_MIPS_SCN_DISP", /* name */
864 false, /* partial_inplace */
865 0xffffffff, /* src_mask */
866 0xffffffff, /* dst_mask */
867 false), /* pcrel_offset */
869 EMPTY_HOWTO (R_MIPS_REL16),
870 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
871 EMPTY_HOWTO (R_MIPS_PJUMP),
872 EMPTY_HOWTO (R_MIPS_RELGOT),
874 /* Protected jump conversion. This is an optimization hint. No
875 relocation is required for correctness. */
876 HOWTO (R_MIPS_JALR, /* type */
878 0, /* size (0 = byte, 1 = short, 2 = long) */
880 false, /* pc_relative */
882 complain_overflow_dont, /* complain_on_overflow */
883 bfd_elf_generic_reloc, /* special_function */
884 "R_MIPS_JALR", /* name */
885 false, /* partial_inplace */
886 0x00000000, /* src_mask */
887 0x00000000, /* dst_mask */
888 false), /* pcrel_offset */
891 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
892 is a hack to make the linker think that we need 64 bit values. */
893 static reloc_howto_type elf_mips_ctor64_howto =
894 HOWTO (R_MIPS_64, /* type */
896 4, /* size (0 = byte, 1 = short, 2 = long) */
898 false, /* pc_relative */
900 complain_overflow_signed, /* complain_on_overflow */
901 mips32_64bit_reloc, /* special_function */
902 "R_MIPS_64", /* name */
903 true, /* partial_inplace */
904 0xffffffff, /* src_mask */
905 0xffffffff, /* dst_mask */
906 false); /* pcrel_offset */
908 /* The reloc used for the mips16 jump instruction. */
909 static reloc_howto_type elf_mips16_jump_howto =
910 HOWTO (R_MIPS16_26, /* type */
912 2, /* size (0 = byte, 1 = short, 2 = long) */
914 false, /* pc_relative */
916 complain_overflow_dont, /* complain_on_overflow */
917 /* This needs complex overflow
918 detection, because the upper four
919 bits must match the PC. */
920 mips16_jump_reloc, /* special_function */
921 "R_MIPS16_26", /* name */
922 true, /* partial_inplace */
923 0x3ffffff, /* src_mask */
924 0x3ffffff, /* dst_mask */
925 false); /* pcrel_offset */
927 /* The reloc used for the mips16 gprel instruction. The src_mask and
928 dsk_mask for this howto do not reflect the actual instruction, in
929 which the value is not contiguous; the masks are for the
930 convenience of the relocate_section routine. */
931 static reloc_howto_type elf_mips16_gprel_howto =
932 HOWTO (R_MIPS16_GPREL, /* type */
934 2, /* size (0 = byte, 1 = short, 2 = long) */
936 false, /* pc_relative */
938 complain_overflow_signed, /* complain_on_overflow */
939 mips16_gprel_reloc, /* special_function */
940 "R_MIPS16_GPREL", /* name */
941 true, /* partial_inplace */
942 0xffff, /* src_mask */
943 0xffff, /* dst_mask */
944 false); /* pcrel_offset */
947 /* GNU extension to record C++ vtable hierarchy */
948 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
949 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
951 2, /* size (0 = byte, 1 = short, 2 = long) */
953 false, /* pc_relative */
955 complain_overflow_dont, /* complain_on_overflow */
956 NULL, /* special_function */
957 "R_MIPS_GNU_VTINHERIT", /* name */
958 false, /* partial_inplace */
961 false); /* pcrel_offset */
963 /* GNU extension to record C++ vtable member usage */
964 static reloc_howto_type elf_mips_gnu_vtentry_howto =
965 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
967 2, /* size (0 = byte, 1 = short, 2 = long) */
969 false, /* pc_relative */
971 complain_overflow_dont, /* complain_on_overflow */
972 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
973 "R_MIPS_GNU_VTENTRY", /* name */
974 false, /* partial_inplace */
977 false); /* pcrel_offset */
979 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
980 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
981 the HI16. Here we just save the information we need; we do the
982 actual relocation when we see the LO16. MIPS ELF requires that the
983 LO16 immediately follow the HI16. As a GNU extension, we permit an
984 arbitrary number of HI16 relocs to be associated with a single LO16
985 reloc. This extension permits gcc to output the HI and LO relocs
990 struct mips_hi16 *next;
995 /* FIXME: This should not be a static variable. */
997 static struct mips_hi16 *mips_hi16_list;
999 bfd_reloc_status_type
1000 _bfd_mips_elf_hi16_reloc (abfd,
1007 bfd *abfd ATTRIBUTE_UNUSED;
1008 arelent *reloc_entry;
1011 asection *input_section;
1013 char **error_message;
1015 bfd_reloc_status_type ret;
1017 struct mips_hi16 *n;
1019 /* If we're relocating, and this an external symbol, we don't want
1020 to change anything. */
1021 if (output_bfd != (bfd *) NULL
1022 && (symbol->flags & BSF_SECTION_SYM) == 0
1023 && reloc_entry->addend == 0)
1025 reloc_entry->address += input_section->output_offset;
1026 return bfd_reloc_ok;
1031 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1033 boolean relocateable;
1036 if (ret == bfd_reloc_undefined)
1039 if (output_bfd != NULL)
1040 relocateable = true;
1043 relocateable = false;
1044 output_bfd = symbol->section->output_section->owner;
1047 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1048 error_message, &gp);
1049 if (ret != bfd_reloc_ok)
1052 relocation = gp - reloc_entry->address;
1056 if (bfd_is_und_section (symbol->section)
1057 && output_bfd == (bfd *) NULL)
1058 ret = bfd_reloc_undefined;
1060 if (bfd_is_com_section (symbol->section))
1063 relocation = symbol->value;
1066 relocation += symbol->section->output_section->vma;
1067 relocation += symbol->section->output_offset;
1068 relocation += reloc_entry->addend;
1070 if (reloc_entry->address > input_section->_cooked_size)
1071 return bfd_reloc_outofrange;
1073 /* Save the information, and let LO16 do the actual relocation. */
1074 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
1076 return bfd_reloc_outofrange;
1077 n->addr = (bfd_byte *) data + reloc_entry->address;
1078 n->addend = relocation;
1079 n->next = mips_hi16_list;
1082 if (output_bfd != (bfd *) NULL)
1083 reloc_entry->address += input_section->output_offset;
1088 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1089 inplace relocation; this function exists in order to do the
1090 R_MIPS_HI16 relocation described above. */
1092 bfd_reloc_status_type
1093 _bfd_mips_elf_lo16_reloc (abfd,
1101 arelent *reloc_entry;
1104 asection *input_section;
1106 char **error_message;
1108 arelent gp_disp_relent;
1110 if (mips_hi16_list != NULL)
1112 struct mips_hi16 *l;
1119 unsigned long vallo;
1120 struct mips_hi16 *next;
1122 /* Do the HI16 relocation. Note that we actually don't need
1123 to know anything about the LO16 itself, except where to
1124 find the low 16 bits of the addend needed by the LO16. */
1125 insn = bfd_get_32 (abfd, l->addr);
1126 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1128 val = ((insn & 0xffff) << 16) + vallo;
1131 /* The low order 16 bits are always treated as a signed
1132 value. Therefore, a negative value in the low order bits
1133 requires an adjustment in the high order bits. We need
1134 to make this adjustment in two ways: once for the bits we
1135 took from the data, and once for the bits we are putting
1136 back in to the data. */
1137 if ((vallo & 0x8000) != 0)
1139 if ((val & 0x8000) != 0)
1142 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1143 bfd_put_32 (abfd, insn, l->addr);
1145 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1147 gp_disp_relent = *reloc_entry;
1148 reloc_entry = &gp_disp_relent;
1149 reloc_entry->addend = l->addend;
1157 mips_hi16_list = NULL;
1159 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1161 bfd_reloc_status_type ret;
1162 bfd_vma gp, relocation;
1164 /* FIXME: Does this case ever occur? */
1166 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1167 if (ret != bfd_reloc_ok)
1170 relocation = gp - reloc_entry->address;
1171 relocation += symbol->section->output_section->vma;
1172 relocation += symbol->section->output_offset;
1173 relocation += reloc_entry->addend;
1175 if (reloc_entry->address > input_section->_cooked_size)
1176 return bfd_reloc_outofrange;
1178 gp_disp_relent = *reloc_entry;
1179 reloc_entry = &gp_disp_relent;
1180 reloc_entry->addend = relocation - 4;
1183 /* Now do the LO16 reloc in the usual way. */
1184 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1185 input_section, output_bfd, error_message);
1188 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1189 table used for PIC code. If the symbol is an external symbol, the
1190 instruction is modified to contain the offset of the appropriate
1191 entry in the global offset table. If the symbol is a section
1192 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1193 addends are combined to form the real addend against the section
1194 symbol; the GOT16 is modified to contain the offset of an entry in
1195 the global offset table, and the LO16 is modified to offset it
1196 appropriately. Thus an offset larger than 16 bits requires a
1197 modified value in the global offset table.
1199 This implementation suffices for the assembler, but the linker does
1200 not yet know how to create global offset tables. */
1202 bfd_reloc_status_type
1203 _bfd_mips_elf_got16_reloc (abfd,
1211 arelent *reloc_entry;
1214 asection *input_section;
1216 char **error_message;
1218 /* If we're relocating, and this an external symbol, we don't want
1219 to change anything. */
1220 if (output_bfd != (bfd *) NULL
1221 && (symbol->flags & BSF_SECTION_SYM) == 0
1222 && reloc_entry->addend == 0)
1224 reloc_entry->address += input_section->output_offset;
1225 return bfd_reloc_ok;
1228 /* If we're relocating, and this is a local symbol, we can handle it
1230 if (output_bfd != (bfd *) NULL
1231 && (symbol->flags & BSF_SECTION_SYM) != 0)
1232 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1233 input_section, output_bfd, error_message);
1238 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1239 dangerous relocation. */
1242 mips_elf_assign_gp (output_bfd, pgp)
1250 /* If we've already figured out what GP will be, just return it. */
1251 *pgp = _bfd_get_gp_value (output_bfd);
1255 count = bfd_get_symcount (output_bfd);
1256 sym = bfd_get_outsymbols (output_bfd);
1258 /* The linker script will have created a symbol named `_gp' with the
1259 appropriate value. */
1260 if (sym == (asymbol **) NULL)
1264 for (i = 0; i < count; i++, sym++)
1266 register CONST char *name;
1268 name = bfd_asymbol_name (*sym);
1269 if (*name == '_' && strcmp (name, "_gp") == 0)
1271 *pgp = bfd_asymbol_value (*sym);
1272 _bfd_set_gp_value (output_bfd, *pgp);
1280 /* Only get the error once. */
1282 _bfd_set_gp_value (output_bfd, *pgp);
1289 /* We have to figure out the gp value, so that we can adjust the
1290 symbol value correctly. We look up the symbol _gp in the output
1291 BFD. If we can't find it, we're stuck. We cache it in the ELF
1292 target data. We don't need to adjust the symbol value for an
1293 external symbol if we are producing relocateable output. */
1295 static bfd_reloc_status_type
1296 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1299 boolean relocateable;
1300 char **error_message;
1303 if (bfd_is_und_section (symbol->section)
1307 return bfd_reloc_undefined;
1310 *pgp = _bfd_get_gp_value (output_bfd);
1313 || (symbol->flags & BSF_SECTION_SYM) != 0))
1317 /* Make up a value. */
1318 *pgp = symbol->section->output_section->vma + 0x4000;
1319 _bfd_set_gp_value (output_bfd, *pgp);
1321 else if (!mips_elf_assign_gp (output_bfd, pgp))
1324 (char *) _("GP relative relocation when _gp not defined");
1325 return bfd_reloc_dangerous;
1329 return bfd_reloc_ok;
1332 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1333 become the offset from the gp register. This function also handles
1334 R_MIPS_LITERAL relocations, although those can be handled more
1335 cleverly because the entries in the .lit8 and .lit4 sections can be
1338 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1339 arelent *, asection *,
1340 boolean, PTR, bfd_vma));
1342 bfd_reloc_status_type
1343 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1344 output_bfd, error_message)
1346 arelent *reloc_entry;
1349 asection *input_section;
1351 char **error_message;
1353 boolean relocateable;
1354 bfd_reloc_status_type ret;
1357 /* If we're relocating, and this is an external symbol with no
1358 addend, we don't want to change anything. We will only have an
1359 addend if this is a newly created reloc, not read from an ELF
1361 if (output_bfd != (bfd *) NULL
1362 && (symbol->flags & BSF_SECTION_SYM) == 0
1363 && reloc_entry->addend == 0)
1365 reloc_entry->address += input_section->output_offset;
1366 return bfd_reloc_ok;
1369 if (output_bfd != (bfd *) NULL)
1370 relocateable = true;
1373 relocateable = false;
1374 output_bfd = symbol->section->output_section->owner;
1377 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1379 if (ret != bfd_reloc_ok)
1382 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1383 relocateable, data, gp);
1386 static bfd_reloc_status_type
1387 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1391 arelent *reloc_entry;
1392 asection *input_section;
1393 boolean relocateable;
1401 if (bfd_is_com_section (symbol->section))
1404 relocation = symbol->value;
1406 relocation += symbol->section->output_section->vma;
1407 relocation += symbol->section->output_offset;
1409 if (reloc_entry->address > input_section->_cooked_size)
1410 return bfd_reloc_outofrange;
1412 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1414 /* Set val to the offset into the section or symbol. */
1415 if (reloc_entry->howto->src_mask == 0)
1417 /* This case occurs with the 64-bit MIPS ELF ABI. */
1418 val = reloc_entry->addend;
1422 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1427 /* Adjust val for the final section location and GP value. If we
1428 are producing relocateable output, we don't want to do this for
1429 an external symbol. */
1431 || (symbol->flags & BSF_SECTION_SYM) != 0)
1432 val += relocation - gp;
1434 insn = (insn &~ 0xffff) | (val & 0xffff);
1435 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1438 reloc_entry->address += input_section->output_offset;
1440 /* Make sure it fit in 16 bits. */
1441 if (val >= 0x8000 && val < 0xffff8000)
1442 return bfd_reloc_overflow;
1444 return bfd_reloc_ok;
1447 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1448 from the gp register? XXX */
1450 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1451 arelent *, asection *,
1452 boolean, PTR, bfd_vma));
1454 bfd_reloc_status_type
1455 _bfd_mips_elf_gprel32_reloc (abfd,
1463 arelent *reloc_entry;
1466 asection *input_section;
1468 char **error_message;
1470 boolean relocateable;
1471 bfd_reloc_status_type ret;
1474 /* If we're relocating, and this is an external symbol with no
1475 addend, we don't want to change anything. We will only have an
1476 addend if this is a newly created reloc, not read from an ELF
1478 if (output_bfd != (bfd *) NULL
1479 && (symbol->flags & BSF_SECTION_SYM) == 0
1480 && reloc_entry->addend == 0)
1482 *error_message = (char *)
1483 _("32bits gp relative relocation occurs for an external symbol");
1484 return bfd_reloc_outofrange;
1487 if (output_bfd != (bfd *) NULL)
1489 relocateable = true;
1490 gp = _bfd_get_gp_value (output_bfd);
1494 relocateable = false;
1495 output_bfd = symbol->section->output_section->owner;
1497 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1498 error_message, &gp);
1499 if (ret != bfd_reloc_ok)
1503 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1504 relocateable, data, gp);
1507 static bfd_reloc_status_type
1508 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1512 arelent *reloc_entry;
1513 asection *input_section;
1514 boolean relocateable;
1521 if (bfd_is_com_section (symbol->section))
1524 relocation = symbol->value;
1526 relocation += symbol->section->output_section->vma;
1527 relocation += symbol->section->output_offset;
1529 if (reloc_entry->address > input_section->_cooked_size)
1530 return bfd_reloc_outofrange;
1532 if (reloc_entry->howto->src_mask == 0)
1534 /* This case arises with the 64-bit MIPS ELF ABI. */
1538 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1540 /* Set val to the offset into the section or symbol. */
1541 val += reloc_entry->addend;
1543 /* Adjust val for the final section location and GP value. If we
1544 are producing relocateable output, we don't want to do this for
1545 an external symbol. */
1547 || (symbol->flags & BSF_SECTION_SYM) != 0)
1548 val += relocation - gp;
1550 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1553 reloc_entry->address += input_section->output_offset;
1555 return bfd_reloc_ok;
1558 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1559 generated when addreses are 64 bits. The upper 32 bits are a simle
1562 static bfd_reloc_status_type
1563 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1564 output_bfd, error_message)
1566 arelent *reloc_entry;
1569 asection *input_section;
1571 char **error_message;
1573 bfd_reloc_status_type r;
1578 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1579 input_section, output_bfd, error_message);
1580 if (r != bfd_reloc_continue)
1583 /* Do a normal 32 bit relocation on the lower 32 bits. */
1584 reloc32 = *reloc_entry;
1585 if (bfd_big_endian (abfd))
1586 reloc32.address += 4;
1587 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1588 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1589 output_bfd, error_message);
1591 /* Sign extend into the upper 32 bits. */
1592 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1593 if ((val & 0x80000000) != 0)
1597 addr = reloc_entry->address;
1598 if (bfd_little_endian (abfd))
1600 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1605 /* Handle a mips16 jump. */
1607 static bfd_reloc_status_type
1608 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1609 output_bfd, error_message)
1610 bfd *abfd ATTRIBUTE_UNUSED;
1611 arelent *reloc_entry;
1613 PTR data ATTRIBUTE_UNUSED;
1614 asection *input_section;
1616 char **error_message ATTRIBUTE_UNUSED;
1618 if (output_bfd != (bfd *) NULL
1619 && (symbol->flags & BSF_SECTION_SYM) == 0
1620 && reloc_entry->addend == 0)
1622 reloc_entry->address += input_section->output_offset;
1623 return bfd_reloc_ok;
1628 static boolean warned;
1631 (*_bfd_error_handler)
1632 (_("Linking mips16 objects into %s format is not supported"),
1633 bfd_get_target (input_section->output_section->owner));
1637 return bfd_reloc_undefined;
1640 /* Handle a mips16 GP relative reloc. */
1642 static bfd_reloc_status_type
1643 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1644 output_bfd, error_message)
1646 arelent *reloc_entry;
1649 asection *input_section;
1651 char **error_message;
1653 boolean relocateable;
1654 bfd_reloc_status_type ret;
1656 unsigned short extend, insn;
1657 unsigned long final;
1659 /* If we're relocating, and this is an external symbol with no
1660 addend, we don't want to change anything. We will only have an
1661 addend if this is a newly created reloc, not read from an ELF
1663 if (output_bfd != NULL
1664 && (symbol->flags & BSF_SECTION_SYM) == 0
1665 && reloc_entry->addend == 0)
1667 reloc_entry->address += input_section->output_offset;
1668 return bfd_reloc_ok;
1671 if (output_bfd != NULL)
1672 relocateable = true;
1675 relocateable = false;
1676 output_bfd = symbol->section->output_section->owner;
1679 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1681 if (ret != bfd_reloc_ok)
1684 if (reloc_entry->address > input_section->_cooked_size)
1685 return bfd_reloc_outofrange;
1687 /* Pick up the mips16 extend instruction and the real instruction. */
1688 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1689 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1691 /* Stuff the current addend back as a 32 bit value, do the usual
1692 relocation, and then clean up. */
1694 (((extend & 0x1f) << 11)
1697 (bfd_byte *) data + reloc_entry->address);
1699 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1700 relocateable, data, gp);
1702 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1705 | ((final >> 11) & 0x1f)
1707 (bfd_byte *) data + reloc_entry->address);
1711 (bfd_byte *) data + reloc_entry->address + 2);
1716 /* Return the ISA for a MIPS e_flags value. */
1719 elf_mips_isa (flags)
1722 switch (flags & EF_MIPS_ARCH)
1736 /* Return the MACH for a MIPS e_flags value. */
1739 elf_mips_mach (flags)
1742 switch (flags & EF_MIPS_MACH)
1744 case E_MIPS_MACH_3900:
1745 return bfd_mach_mips3900;
1747 case E_MIPS_MACH_4010:
1748 return bfd_mach_mips4010;
1750 case E_MIPS_MACH_4100:
1751 return bfd_mach_mips4100;
1753 case E_MIPS_MACH_4111:
1754 return bfd_mach_mips4111;
1756 case E_MIPS_MACH_4650:
1757 return bfd_mach_mips4650;
1760 switch (flags & EF_MIPS_ARCH)
1764 return bfd_mach_mips3000;
1768 return bfd_mach_mips6000;
1772 return bfd_mach_mips4000;
1776 return bfd_mach_mips8000;
1784 /* Return printable name for ABI. */
1787 elf_mips_abi_name (abfd)
1792 if (ABI_N32_P (abfd))
1794 else if (ABI_64_P (abfd))
1797 flags = elf_elfheader (abfd)->e_flags;
1798 switch (flags & EF_MIPS_ABI)
1802 case E_MIPS_ABI_O32:
1804 case E_MIPS_ABI_O64:
1806 case E_MIPS_ABI_EABI32:
1808 case E_MIPS_ABI_EABI64:
1811 return "unknown abi";
1815 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1817 struct elf_reloc_map {
1818 bfd_reloc_code_real_type bfd_reloc_val;
1819 enum elf_mips_reloc_type elf_reloc_val;
1822 static CONST struct elf_reloc_map mips_reloc_map[] =
1824 { BFD_RELOC_NONE, R_MIPS_NONE, },
1825 { BFD_RELOC_16, R_MIPS_16 },
1826 { BFD_RELOC_32, R_MIPS_32 },
1827 { BFD_RELOC_64, R_MIPS_64 },
1828 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1829 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1830 { BFD_RELOC_LO16, R_MIPS_LO16 },
1831 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1832 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1833 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1834 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1835 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1836 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1837 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1838 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1839 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1840 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1841 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1842 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1843 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1844 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1847 /* Given a BFD reloc type, return a howto structure. */
1849 static reloc_howto_type *
1850 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1852 bfd_reloc_code_real_type code;
1856 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1858 if (mips_reloc_map[i].bfd_reloc_val == code)
1859 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1865 bfd_set_error (bfd_error_bad_value);
1868 case BFD_RELOC_CTOR:
1869 /* We need to handle BFD_RELOC_CTOR specially.
1870 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1871 size of addresses on this architecture. */
1872 if (bfd_arch_bits_per_address (abfd) == 32)
1873 return &elf_mips_howto_table[(int) R_MIPS_32];
1875 return &elf_mips_ctor64_howto;
1877 case BFD_RELOC_MIPS16_JMP:
1878 return &elf_mips16_jump_howto;
1879 case BFD_RELOC_MIPS16_GPREL:
1880 return &elf_mips16_gprel_howto;
1881 case BFD_RELOC_VTABLE_INHERIT:
1882 return &elf_mips_gnu_vtinherit_howto;
1883 case BFD_RELOC_VTABLE_ENTRY:
1884 return &elf_mips_gnu_vtentry_howto;
1888 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1891 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1894 Elf32_Internal_Rel *dst;
1896 unsigned int r_type;
1898 r_type = ELF32_R_TYPE (dst->r_info);
1902 cache_ptr->howto = &elf_mips16_jump_howto;
1904 case R_MIPS16_GPREL:
1905 cache_ptr->howto = &elf_mips16_gprel_howto;
1907 case R_MIPS_GNU_VTINHERIT:
1908 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1910 case R_MIPS_GNU_VTENTRY:
1911 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1915 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1916 cache_ptr->howto = &elf_mips_howto_table[r_type];
1920 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1921 value for the object file. We get the addend now, rather than
1922 when we do the relocation, because the symbol manipulations done
1923 by the linker may cause us to lose track of the input BFD. */
1924 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1925 && (r_type == (unsigned int) R_MIPS_GPREL16
1926 || r_type == (unsigned int) R_MIPS_LITERAL))
1927 cache_ptr->addend = elf_gp (abfd);
1930 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1933 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1936 Elf32_Internal_Rela *dst;
1938 /* Since an Elf32_Internal_Rel is an initial prefix of an
1939 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1941 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1943 /* If we ever need to do any extra processing with dst->r_addend
1944 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1947 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1948 routines swap this structure in and out. They are used outside of
1949 BFD, so they are globally visible. */
1952 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1954 const Elf32_External_RegInfo *ex;
1957 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1958 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1959 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1960 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1961 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1962 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1966 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1968 const Elf32_RegInfo *in;
1969 Elf32_External_RegInfo *ex;
1971 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1972 (bfd_byte *) ex->ri_gprmask);
1973 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1974 (bfd_byte *) ex->ri_cprmask[0]);
1975 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1976 (bfd_byte *) ex->ri_cprmask[1]);
1977 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1978 (bfd_byte *) ex->ri_cprmask[2]);
1979 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1980 (bfd_byte *) ex->ri_cprmask[3]);
1981 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1982 (bfd_byte *) ex->ri_gp_value);
1985 /* In the 64 bit ABI, the .MIPS.options section holds register
1986 information in an Elf64_Reginfo structure. These routines swap
1987 them in and out. They are globally visible because they are used
1988 outside of BFD. These routines are here so that gas can call them
1989 without worrying about whether the 64 bit ABI has been included. */
1992 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1994 const Elf64_External_RegInfo *ex;
1995 Elf64_Internal_RegInfo *in;
1997 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1998 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1999 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
2000 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
2001 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
2002 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
2003 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
2007 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
2009 const Elf64_Internal_RegInfo *in;
2010 Elf64_External_RegInfo *ex;
2012 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
2013 (bfd_byte *) ex->ri_gprmask);
2014 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
2015 (bfd_byte *) ex->ri_pad);
2016 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
2017 (bfd_byte *) ex->ri_cprmask[0]);
2018 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
2019 (bfd_byte *) ex->ri_cprmask[1]);
2020 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
2021 (bfd_byte *) ex->ri_cprmask[2]);
2022 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
2023 (bfd_byte *) ex->ri_cprmask[3]);
2024 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
2025 (bfd_byte *) ex->ri_gp_value);
2028 /* Swap an entry in a .gptab section. Note that these routines rely
2029 on the equivalence of the two elements of the union. */
2032 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
2034 const Elf32_External_gptab *ex;
2037 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
2038 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
2042 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
2044 const Elf32_gptab *in;
2045 Elf32_External_gptab *ex;
2047 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
2048 ex->gt_entry.gt_g_value);
2049 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
2050 ex->gt_entry.gt_bytes);
2054 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2056 const Elf32_compact_rel *in;
2057 Elf32_External_compact_rel *ex;
2059 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
2060 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
2061 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
2062 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
2063 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
2064 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
2068 bfd_elf32_swap_crinfo_out (abfd, in, ex)
2070 const Elf32_crinfo *in;
2071 Elf32_External_crinfo *ex;
2075 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2076 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2077 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2078 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2079 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
2080 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
2081 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
2084 /* Swap in an options header. */
2087 bfd_mips_elf_swap_options_in (abfd, ex, in)
2089 const Elf_External_Options *ex;
2090 Elf_Internal_Options *in;
2092 in->kind = bfd_h_get_8 (abfd, ex->kind);
2093 in->size = bfd_h_get_8 (abfd, ex->size);
2094 in->section = bfd_h_get_16 (abfd, ex->section);
2095 in->info = bfd_h_get_32 (abfd, ex->info);
2098 /* Swap out an options header. */
2101 bfd_mips_elf_swap_options_out (abfd, in, ex)
2103 const Elf_Internal_Options *in;
2104 Elf_External_Options *ex;
2106 bfd_h_put_8 (abfd, in->kind, ex->kind);
2107 bfd_h_put_8 (abfd, in->size, ex->size);
2108 bfd_h_put_16 (abfd, in->section, ex->section);
2109 bfd_h_put_32 (abfd, in->info, ex->info);
2112 /* Swap in an MSYM entry. */
2115 bfd_mips_elf_swap_msym_in (abfd, ex, in)
2117 const Elf32_External_Msym *ex;
2118 Elf32_Internal_Msym *in;
2120 in->ms_hash_value = bfd_h_get_32 (abfd, ex->ms_hash_value);
2121 in->ms_info = bfd_h_get_32 (abfd, ex->ms_info);
2124 /* Swap out an MSYM entry. */
2127 bfd_mips_elf_swap_msym_out (abfd, in, ex)
2129 const Elf32_Internal_Msym *in;
2130 Elf32_External_Msym *ex;
2132 bfd_h_put_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2133 bfd_h_put_32 (abfd, in->ms_info, ex->ms_info);
2137 /* Determine whether a symbol is global for the purposes of splitting
2138 the symbol table into global symbols and local symbols. At least
2139 on Irix 5, this split must be between section symbols and all other
2140 symbols. On most ELF targets the split is between static symbols
2141 and externally visible symbols. */
2145 mips_elf_sym_is_global (abfd, sym)
2146 bfd *abfd ATTRIBUTE_UNUSED;
2149 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
2152 /* Set the right machine number for a MIPS ELF file. This is used for
2153 both the 32-bit and the 64-bit ABI. */
2156 _bfd_mips_elf_object_p (abfd)
2159 /* Irix 5 and 6 is broken. Object file symbol tables are not always
2160 sorted correctly such that local symbols precede global symbols,
2161 and the sh_info field in the symbol table is not always right. */
2162 elf_bad_symtab (abfd) = true;
2164 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2165 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2169 /* The final processing done just before writing out a MIPS ELF object
2170 file. This gets the MIPS architecture right based on the machine
2171 number. This is used by both the 32-bit and the 64-bit ABI. */
2175 _bfd_mips_elf_final_write_processing (abfd, linker)
2177 boolean linker ATTRIBUTE_UNUSED;
2181 Elf_Internal_Shdr **hdrpp;
2185 switch (bfd_get_mach (abfd))
2188 case bfd_mach_mips3000:
2189 val = E_MIPS_ARCH_1;
2192 case bfd_mach_mips3900:
2193 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2196 case bfd_mach_mips6000:
2197 val = E_MIPS_ARCH_2;
2200 case bfd_mach_mips4000:
2201 case bfd_mach_mips4300:
2202 val = E_MIPS_ARCH_3;
2205 case bfd_mach_mips4010:
2206 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2209 case bfd_mach_mips4100:
2210 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2213 case bfd_mach_mips4111:
2214 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2217 case bfd_mach_mips4650:
2218 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2221 case bfd_mach_mips8000:
2222 val = E_MIPS_ARCH_4;
2226 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2227 elf_elfheader (abfd)->e_flags |= val;
2229 /* Set the sh_info field for .gptab sections and other appropriate
2230 info for each special section. */
2231 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2232 i < elf_elfheader (abfd)->e_shnum;
2235 switch ((*hdrpp)->sh_type)
2238 case SHT_MIPS_LIBLIST:
2239 sec = bfd_get_section_by_name (abfd, ".dynstr");
2241 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2244 case SHT_MIPS_GPTAB:
2245 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2246 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2247 BFD_ASSERT (name != NULL
2248 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2249 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2250 BFD_ASSERT (sec != NULL);
2251 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2254 case SHT_MIPS_CONTENT:
2255 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2256 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2257 BFD_ASSERT (name != NULL
2258 && strncmp (name, ".MIPS.content",
2259 sizeof ".MIPS.content" - 1) == 0);
2260 sec = bfd_get_section_by_name (abfd,
2261 name + sizeof ".MIPS.content" - 1);
2262 BFD_ASSERT (sec != NULL);
2263 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2266 case SHT_MIPS_SYMBOL_LIB:
2267 sec = bfd_get_section_by_name (abfd, ".dynsym");
2269 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2270 sec = bfd_get_section_by_name (abfd, ".liblist");
2272 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2275 case SHT_MIPS_EVENTS:
2276 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2277 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2278 BFD_ASSERT (name != NULL);
2279 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2280 sec = bfd_get_section_by_name (abfd,
2281 name + sizeof ".MIPS.events" - 1);
2284 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2285 sizeof ".MIPS.post_rel" - 1) == 0);
2286 sec = bfd_get_section_by_name (abfd,
2288 + sizeof ".MIPS.post_rel" - 1));
2290 BFD_ASSERT (sec != NULL);
2291 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2298 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2301 _bfd_mips_elf_set_private_flags (abfd, flags)
2305 BFD_ASSERT (!elf_flags_init (abfd)
2306 || elf_elfheader (abfd)->e_flags == flags);
2308 elf_elfheader (abfd)->e_flags = flags;
2309 elf_flags_init (abfd) = true;
2313 /* Copy backend specific data from one object module to another */
2316 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2320 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2321 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2324 BFD_ASSERT (!elf_flags_init (obfd)
2325 || (elf_elfheader (obfd)->e_flags
2326 == elf_elfheader (ibfd)->e_flags));
2328 elf_gp (obfd) = elf_gp (ibfd);
2329 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2330 elf_flags_init (obfd) = true;
2334 /* Merge backend specific data from an object file to the output
2335 object file when linking. */
2338 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2346 /* Check if we have the same endianess */
2347 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2348 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2352 if (bfd_big_endian (ibfd))
2353 msg = _("%s: compiled for a big endian system and target is little endian");
2355 msg = _("%s: compiled for a little endian system and target is big endian");
2357 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2359 bfd_set_error (bfd_error_wrong_format);
2363 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2364 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2367 new_flags = elf_elfheader (ibfd)->e_flags;
2368 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2369 old_flags = elf_elfheader (obfd)->e_flags;
2371 if (! elf_flags_init (obfd))
2373 elf_flags_init (obfd) = true;
2374 elf_elfheader (obfd)->e_flags = new_flags;
2375 elf_elfheader (obfd)->e_ident[EI_CLASS]
2376 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
2378 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2379 && bfd_get_arch_info (obfd)->the_default)
2381 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2382 bfd_get_mach (ibfd)))
2389 /* Check flag compatibility. */
2391 new_flags &= ~EF_MIPS_NOREORDER;
2392 old_flags &= ~EF_MIPS_NOREORDER;
2394 if (new_flags == old_flags)
2399 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2401 new_flags &= ~EF_MIPS_PIC;
2402 old_flags &= ~EF_MIPS_PIC;
2403 (*_bfd_error_handler)
2404 (_("%s: linking PIC files with non-PIC files"),
2405 bfd_get_filename (ibfd));
2409 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2411 new_flags &= ~EF_MIPS_CPIC;
2412 old_flags &= ~EF_MIPS_CPIC;
2413 (*_bfd_error_handler)
2414 (_("%s: linking abicalls files with non-abicalls files"),
2415 bfd_get_filename (ibfd));
2419 /* Compare the ISA's. */
2420 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2421 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2423 int new_mach = new_flags & EF_MIPS_MACH;
2424 int old_mach = old_flags & EF_MIPS_MACH;
2425 int new_isa = elf_mips_isa (new_flags);
2426 int old_isa = elf_mips_isa (old_flags);
2428 /* If either has no machine specified, just compare the general isa's.
2429 Some combinations of machines are ok, if the isa's match. */
2432 || new_mach == old_mach
2435 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2436 and -mips4 code. They will normally use the same data sizes and
2437 calling conventions. */
2439 if ((new_isa == 1 || new_isa == 2)
2440 ? (old_isa != 1 && old_isa != 2)
2441 : (old_isa == 1 || old_isa == 2))
2443 (*_bfd_error_handler)
2444 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2445 bfd_get_filename (ibfd), new_isa, old_isa);
2452 (*_bfd_error_handler)
2453 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2454 bfd_get_filename (ibfd),
2455 elf_mips_mach (new_flags),
2456 elf_mips_mach (old_flags));
2460 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2461 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2464 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
2465 does set EI_CLASS differently from any 32-bit ABI. */
2466 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
2467 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2468 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2470 /* Only error if both are set (to different values). */
2471 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
2472 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2473 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2475 (*_bfd_error_handler)
2476 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2477 bfd_get_filename (ibfd),
2478 elf_mips_abi_name (ibfd),
2479 elf_mips_abi_name (obfd));
2482 new_flags &= ~EF_MIPS_ABI;
2483 old_flags &= ~EF_MIPS_ABI;
2486 /* Warn about any other mismatches */
2487 if (new_flags != old_flags)
2489 (*_bfd_error_handler)
2490 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2491 bfd_get_filename (ibfd), (unsigned long) new_flags,
2492 (unsigned long) old_flags);
2498 bfd_set_error (bfd_error_bad_value);
2506 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2510 FILE *file = (FILE *) ptr;
2512 BFD_ASSERT (abfd != NULL && ptr != NULL);
2514 /* Print normal ELF private data. */
2515 _bfd_elf_print_private_bfd_data (abfd, ptr);
2517 /* xgettext:c-format */
2518 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2520 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2521 fprintf (file, _ (" [abi=O32]"));
2522 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2523 fprintf (file, _ (" [abi=O64]"));
2524 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2525 fprintf (file, _ (" [abi=EABI32]"));
2526 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2527 fprintf (file, _ (" [abi=EABI64]"));
2528 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2529 fprintf (file, _ (" [abi unknown]"));
2530 else if (ABI_N32_P (abfd))
2531 fprintf (file, _ (" [abi=N32]"));
2532 else if (ABI_64_P (abfd))
2533 fprintf (file, _ (" [abi=64]"));
2535 fprintf (file, _ (" [no abi set]"));
2537 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2538 fprintf (file, _ (" [mips1]"));
2539 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2540 fprintf (file, _ (" [mips2]"));
2541 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2542 fprintf (file, _ (" [mips3]"));
2543 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2544 fprintf (file, _ (" [mips4]"));
2546 fprintf (file, _ (" [unknown ISA]"));
2548 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2549 fprintf (file, _ (" [32bitmode]"));
2551 fprintf (file, _ (" [not 32bitmode]"));
2558 /* Handle a MIPS specific section when reading an object file. This
2559 is called when elfcode.h finds a section with an unknown type.
2560 This routine supports both the 32-bit and 64-bit ELF ABI.
2562 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2566 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2568 Elf_Internal_Shdr *hdr;
2573 /* There ought to be a place to keep ELF backend specific flags, but
2574 at the moment there isn't one. We just keep track of the
2575 sections by their name, instead. Fortunately, the ABI gives
2576 suggested names for all the MIPS specific sections, so we will
2577 probably get away with this. */
2578 switch (hdr->sh_type)
2580 case SHT_MIPS_LIBLIST:
2581 if (strcmp (name, ".liblist") != 0)
2585 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2588 case SHT_MIPS_CONFLICT:
2589 if (strcmp (name, ".conflict") != 0)
2592 case SHT_MIPS_GPTAB:
2593 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2596 case SHT_MIPS_UCODE:
2597 if (strcmp (name, ".ucode") != 0)
2600 case SHT_MIPS_DEBUG:
2601 if (strcmp (name, ".mdebug") != 0)
2603 flags = SEC_DEBUGGING;
2605 case SHT_MIPS_REGINFO:
2606 if (strcmp (name, ".reginfo") != 0
2607 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2609 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2611 case SHT_MIPS_IFACE:
2612 if (strcmp (name, ".MIPS.interfaces") != 0)
2615 case SHT_MIPS_CONTENT:
2616 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2619 case SHT_MIPS_OPTIONS:
2620 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2623 case SHT_MIPS_DWARF:
2624 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2627 case SHT_MIPS_SYMBOL_LIB:
2628 if (strcmp (name, ".MIPS.symlib") != 0)
2631 case SHT_MIPS_EVENTS:
2632 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2633 && strncmp (name, ".MIPS.post_rel",
2634 sizeof ".MIPS.post_rel" - 1) != 0)
2641 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2646 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2647 (bfd_get_section_flags (abfd,
2653 /* FIXME: We should record sh_info for a .gptab section. */
2655 /* For a .reginfo section, set the gp value in the tdata information
2656 from the contents of this section. We need the gp value while
2657 processing relocs, so we just get it now. The .reginfo section
2658 is not used in the 64-bit MIPS ELF ABI. */
2659 if (hdr->sh_type == SHT_MIPS_REGINFO)
2661 Elf32_External_RegInfo ext;
2664 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2665 (file_ptr) 0, sizeof ext))
2667 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2668 elf_gp (abfd) = s.ri_gp_value;
2671 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2672 set the gp value based on what we find. We may see both
2673 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2674 they should agree. */
2675 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2677 bfd_byte *contents, *l, *lend;
2679 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2680 if (contents == NULL)
2682 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2683 (file_ptr) 0, hdr->sh_size))
2689 lend = contents + hdr->sh_size;
2690 while (l + sizeof (Elf_External_Options) <= lend)
2692 Elf_Internal_Options intopt;
2694 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2696 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2698 Elf64_Internal_RegInfo intreg;
2700 bfd_mips_elf64_swap_reginfo_in
2702 ((Elf64_External_RegInfo *)
2703 (l + sizeof (Elf_External_Options))),
2705 elf_gp (abfd) = intreg.ri_gp_value;
2707 else if (intopt.kind == ODK_REGINFO)
2709 Elf32_RegInfo intreg;
2711 bfd_mips_elf32_swap_reginfo_in
2713 ((Elf32_External_RegInfo *)
2714 (l + sizeof (Elf_External_Options))),
2716 elf_gp (abfd) = intreg.ri_gp_value;
2726 /* Set the correct type for a MIPS ELF section. We do this by the
2727 section name, which is a hack, but ought to work. This routine is
2728 used by both the 32-bit and the 64-bit ABI. */
2731 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2733 Elf32_Internal_Shdr *hdr;
2736 register const char *name;
2738 name = bfd_get_section_name (abfd, sec);
2740 if (strcmp (name, ".liblist") == 0)
2742 hdr->sh_type = SHT_MIPS_LIBLIST;
2743 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2744 /* The sh_link field is set in final_write_processing. */
2746 else if (strcmp (name, ".conflict") == 0)
2747 hdr->sh_type = SHT_MIPS_CONFLICT;
2748 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2750 hdr->sh_type = SHT_MIPS_GPTAB;
2751 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2752 /* The sh_info field is set in final_write_processing. */
2754 else if (strcmp (name, ".ucode") == 0)
2755 hdr->sh_type = SHT_MIPS_UCODE;
2756 else if (strcmp (name, ".mdebug") == 0)
2758 hdr->sh_type = SHT_MIPS_DEBUG;
2759 /* In a shared object on Irix 5.3, the .mdebug section has an
2760 entsize of 0. FIXME: Does this matter? */
2761 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2762 hdr->sh_entsize = 0;
2764 hdr->sh_entsize = 1;
2766 else if (strcmp (name, ".reginfo") == 0)
2768 hdr->sh_type = SHT_MIPS_REGINFO;
2769 /* In a shared object on Irix 5.3, the .reginfo section has an
2770 entsize of 0x18. FIXME: Does this matter? */
2771 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2772 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2774 hdr->sh_entsize = 1;
2776 else if (SGI_COMPAT (abfd)
2777 && (strcmp (name, ".hash") == 0
2778 || strcmp (name, ".dynamic") == 0
2779 || strcmp (name, ".dynstr") == 0))
2781 hdr->sh_entsize = 0;
2783 /* This isn't how the Irix 6 linker behaves. */
2784 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2787 else if (strcmp (name, ".got") == 0
2788 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2789 || strcmp (name, ".sdata") == 0
2790 || strcmp (name, ".sbss") == 0
2791 || strcmp (name, ".lit4") == 0
2792 || strcmp (name, ".lit8") == 0)
2793 hdr->sh_flags |= SHF_MIPS_GPREL;
2794 else if (strcmp (name, ".MIPS.interfaces") == 0)
2796 hdr->sh_type = SHT_MIPS_IFACE;
2797 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2799 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2801 hdr->sh_type = SHT_MIPS_CONTENT;
2802 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2803 /* The sh_info field is set in final_write_processing. */
2805 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2807 hdr->sh_type = SHT_MIPS_OPTIONS;
2808 hdr->sh_entsize = 1;
2809 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2811 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2812 hdr->sh_type = SHT_MIPS_DWARF;
2813 else if (strcmp (name, ".MIPS.symlib") == 0)
2815 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2816 /* The sh_link and sh_info fields are set in
2817 final_write_processing. */
2819 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2820 || strncmp (name, ".MIPS.post_rel",
2821 sizeof ".MIPS.post_rel" - 1) == 0)
2823 hdr->sh_type = SHT_MIPS_EVENTS;
2824 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2825 /* The sh_link field is set in final_write_processing. */
2827 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2829 hdr->sh_type = SHT_MIPS_MSYM;
2830 hdr->sh_flags |= SHF_ALLOC;
2831 hdr->sh_entsize = 8;
2834 /* The generic elf_fake_sections will set up REL_HDR using the
2835 default kind of relocations. But, we may actually need both
2836 kinds of relocations, so we set up the second header here. */
2837 if ((sec->flags & SEC_RELOC) != 0)
2839 struct bfd_elf_section_data *esd;
2841 esd = elf_section_data (sec);
2842 BFD_ASSERT (esd->rel_hdr2 == NULL);
2844 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2847 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
2848 !elf_section_data (sec)->use_rela_p);
2854 /* Given a BFD section, try to locate the corresponding ELF section
2855 index. This is used by both the 32-bit and the 64-bit ABI.
2856 Actually, it's not clear to me that the 64-bit ABI supports these,
2857 but for non-PIC objects we will certainly want support for at least
2858 the .scommon section. */
2861 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2862 bfd *abfd ATTRIBUTE_UNUSED;
2863 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
2867 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2869 *retval = SHN_MIPS_SCOMMON;
2872 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2874 *retval = SHN_MIPS_ACOMMON;
2880 /* When are writing out the .options or .MIPS.options section,
2881 remember the bytes we are writing out, so that we can install the
2882 GP value in the section_processing routine. */
2885 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2890 bfd_size_type count;
2892 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2896 if (elf_section_data (section) == NULL)
2898 section->used_by_bfd =
2899 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2900 if (elf_section_data (section) == NULL)
2903 c = (bfd_byte *) elf_section_data (section)->tdata;
2908 if (section->_cooked_size != 0)
2909 size = section->_cooked_size;
2911 size = section->_raw_size;
2912 c = (bfd_byte *) bfd_zalloc (abfd, size);
2915 elf_section_data (section)->tdata = (PTR) c;
2918 memcpy (c + offset, location, count);
2921 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2925 /* Work over a section just before writing it out. This routine is
2926 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2927 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2931 _bfd_mips_elf_section_processing (abfd, hdr)
2933 Elf_Internal_Shdr *hdr;
2935 if (hdr->sh_type == SHT_MIPS_REGINFO
2936 && hdr->sh_size > 0)
2940 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2941 BFD_ASSERT (hdr->contents == NULL);
2944 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2947 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2948 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2952 if (hdr->sh_type == SHT_MIPS_OPTIONS
2953 && hdr->bfd_section != NULL
2954 && elf_section_data (hdr->bfd_section) != NULL
2955 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2957 bfd_byte *contents, *l, *lend;
2959 /* We stored the section contents in the elf_section_data tdata
2960 field in the set_section_contents routine. We save the
2961 section contents so that we don't have to read them again.
2962 At this point we know that elf_gp is set, so we can look
2963 through the section contents to see if there is an
2964 ODK_REGINFO structure. */
2966 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2968 lend = contents + hdr->sh_size;
2969 while (l + sizeof (Elf_External_Options) <= lend)
2971 Elf_Internal_Options intopt;
2973 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2975 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2982 + sizeof (Elf_External_Options)
2983 + (sizeof (Elf64_External_RegInfo) - 8)),
2986 bfd_h_put_64 (abfd, elf_gp (abfd), buf);
2987 if (bfd_write (buf, 1, 8, abfd) != 8)
2990 else if (intopt.kind == ODK_REGINFO)
2997 + sizeof (Elf_External_Options)
2998 + (sizeof (Elf32_External_RegInfo) - 4)),
3001 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
3002 if (bfd_write (buf, 1, 4, abfd) != 4)
3009 if (hdr->bfd_section != NULL)
3011 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3013 if (strcmp (name, ".sdata") == 0
3014 || strcmp (name, ".lit8") == 0
3015 || strcmp (name, ".lit4") == 0)
3017 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3018 hdr->sh_type = SHT_PROGBITS;
3020 else if (strcmp (name, ".sbss") == 0)
3022 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3023 hdr->sh_type = SHT_NOBITS;
3025 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3027 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3028 hdr->sh_type = SHT_PROGBITS;
3030 else if (strcmp (name, ".compact_rel") == 0)
3033 hdr->sh_type = SHT_PROGBITS;
3035 else if (strcmp (name, ".rtproc") == 0)
3037 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3039 unsigned int adjust;
3041 adjust = hdr->sh_size % hdr->sh_addralign;
3043 hdr->sh_size += hdr->sh_addralign - adjust;
3052 /* MIPS ELF uses two common sections. One is the usual one, and the
3053 other is for small objects. All the small objects are kept
3054 together, and then referenced via the gp pointer, which yields
3055 faster assembler code. This is what we use for the small common
3056 section. This approach is copied from ecoff.c. */
3057 static asection mips_elf_scom_section;
3058 static asymbol mips_elf_scom_symbol;
3059 static asymbol *mips_elf_scom_symbol_ptr;
3061 /* MIPS ELF also uses an acommon section, which represents an
3062 allocated common symbol which may be overridden by a
3063 definition in a shared library. */
3064 static asection mips_elf_acom_section;
3065 static asymbol mips_elf_acom_symbol;
3066 static asymbol *mips_elf_acom_symbol_ptr;
3068 /* The Irix 5 support uses two virtual sections, which represent
3069 text/data symbols defined in dynamic objects. */
3070 static asection mips_elf_text_section;
3071 static asection *mips_elf_text_section_ptr;
3072 static asymbol mips_elf_text_symbol;
3073 static asymbol *mips_elf_text_symbol_ptr;
3075 static asection mips_elf_data_section;
3076 static asection *mips_elf_data_section_ptr;
3077 static asymbol mips_elf_data_symbol;
3078 static asymbol *mips_elf_data_symbol_ptr;
3080 /* Handle the special MIPS section numbers that a symbol may use.
3081 This is used for both the 32-bit and the 64-bit ABI. */
3084 _bfd_mips_elf_symbol_processing (abfd, asym)
3088 elf_symbol_type *elfsym;
3090 elfsym = (elf_symbol_type *) asym;
3091 switch (elfsym->internal_elf_sym.st_shndx)
3093 case SHN_MIPS_ACOMMON:
3094 /* This section is used in a dynamically linked executable file.
3095 It is an allocated common section. The dynamic linker can
3096 either resolve these symbols to something in a shared
3097 library, or it can just leave them here. For our purposes,
3098 we can consider these symbols to be in a new section. */
3099 if (mips_elf_acom_section.name == NULL)
3101 /* Initialize the acommon section. */
3102 mips_elf_acom_section.name = ".acommon";
3103 mips_elf_acom_section.flags = SEC_ALLOC;
3104 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3105 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3106 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3107 mips_elf_acom_symbol.name = ".acommon";
3108 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3109 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3110 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3112 asym->section = &mips_elf_acom_section;
3116 /* Common symbols less than the GP size are automatically
3117 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3118 if (asym->value > elf_gp_size (abfd)
3119 || IRIX_COMPAT (abfd) == ict_irix6)
3122 case SHN_MIPS_SCOMMON:
3123 if (mips_elf_scom_section.name == NULL)
3125 /* Initialize the small common section. */
3126 mips_elf_scom_section.name = ".scommon";
3127 mips_elf_scom_section.flags = SEC_IS_COMMON;
3128 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3129 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3130 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3131 mips_elf_scom_symbol.name = ".scommon";
3132 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3133 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3134 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3136 asym->section = &mips_elf_scom_section;
3137 asym->value = elfsym->internal_elf_sym.st_size;
3140 case SHN_MIPS_SUNDEFINED:
3141 asym->section = bfd_und_section_ptr;
3144 #if 0 /* for SGI_COMPAT */
3146 asym->section = mips_elf_text_section_ptr;
3150 asym->section = mips_elf_data_section_ptr;
3156 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3160 _bfd_mips_elf_additional_program_headers (abfd)
3166 if (!SGI_COMPAT (abfd))
3169 /* See if we need a PT_MIPS_REGINFO segment. */
3170 s = bfd_get_section_by_name (abfd, ".reginfo");
3171 if (s && (s->flags & SEC_LOAD))
3174 /* See if we need a PT_MIPS_OPTIONS segment. */
3175 if (IRIX_COMPAT (abfd) == ict_irix6
3176 && bfd_get_section_by_name (abfd,
3177 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3180 /* See if we need a PT_MIPS_RTPROC segment. */
3181 if (IRIX_COMPAT (abfd) == ict_irix5
3182 && bfd_get_section_by_name (abfd, ".dynamic")
3183 && bfd_get_section_by_name (abfd, ".mdebug"))
3189 /* Modify the segment map for an Irix 5 executable. */
3192 _bfd_mips_elf_modify_segment_map (abfd)
3196 struct elf_segment_map *m, **pm;
3198 if (! SGI_COMPAT (abfd))
3201 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3203 s = bfd_get_section_by_name (abfd, ".reginfo");
3204 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3206 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3207 if (m->p_type == PT_MIPS_REGINFO)
3211 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3215 m->p_type = PT_MIPS_REGINFO;
3219 /* We want to put it after the PHDR and INTERP segments. */
3220 pm = &elf_tdata (abfd)->segment_map;
3222 && ((*pm)->p_type == PT_PHDR
3223 || (*pm)->p_type == PT_INTERP))
3231 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3232 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3233 PT_OPTIONS segement immediately following the program header
3235 if (IRIX_COMPAT (abfd) == ict_irix6)
3239 for (s = abfd->sections; s; s = s->next)
3240 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3245 struct elf_segment_map *options_segment;
3247 for (m = elf_tdata (abfd)->segment_map; m; m = m->next)
3248 if (m->p_type == PT_PHDR)
3251 /* There should always be a program header table. */
3255 options_segment = bfd_zalloc (abfd,
3256 sizeof (struct elf_segment_map));
3257 options_segment->next = m->next;
3258 options_segment->p_type = PT_MIPS_OPTIONS;
3259 options_segment->p_flags = PF_R;
3260 options_segment->p_flags_valid = true;
3261 options_segment->count = 1;
3262 options_segment->sections[0] = s;
3263 m->next = options_segment;
3268 /* If there are .dynamic and .mdebug sections, we make a room
3269 for the RTPROC header. FIXME: Rewrite without section names. */
3270 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3271 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3272 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3274 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3275 if (m->p_type == PT_MIPS_RTPROC)
3279 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3283 m->p_type = PT_MIPS_RTPROC;
3285 s = bfd_get_section_by_name (abfd, ".rtproc");
3290 m->p_flags_valid = 1;
3298 /* We want to put it after the DYNAMIC segment. */
3299 pm = &elf_tdata (abfd)->segment_map;
3300 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3310 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3311 .dynstr, .dynsym, and .hash sections, and everything in
3313 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3314 if ((*pm)->p_type == PT_DYNAMIC)
3319 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3321 static const char *sec_names[] =
3322 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3325 struct elf_segment_map *n;
3329 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3331 s = bfd_get_section_by_name (abfd, sec_names[i]);
3332 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3338 sz = s->_cooked_size;
3341 if (high < s->vma + sz)
3347 for (s = abfd->sections; s != NULL; s = s->next)
3348 if ((s->flags & SEC_LOAD) != 0
3351 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3355 n = ((struct elf_segment_map *)
3356 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3363 for (s = abfd->sections; s != NULL; s = s->next)
3365 if ((s->flags & SEC_LOAD) != 0
3368 + (s->_cooked_size != 0 ?
3369 s->_cooked_size : s->_raw_size))
3384 /* The structure of the runtime procedure descriptor created by the
3385 loader for use by the static exception system. */
3387 typedef struct runtime_pdr {
3388 bfd_vma adr; /* memory address of start of procedure */
3389 long regmask; /* save register mask */
3390 long regoffset; /* save register offset */
3391 long fregmask; /* save floating point register mask */
3392 long fregoffset; /* save floating point register offset */
3393 long frameoffset; /* frame size */
3394 short framereg; /* frame pointer register */
3395 short pcreg; /* offset or reg of return pc */
3396 long irpss; /* index into the runtime string table */
3398 struct exception_info *exception_info;/* pointer to exception array */
3400 #define cbRPDR sizeof(RPDR)
3401 #define rpdNil ((pRPDR) 0)
3403 /* Swap RPDR (runtime procedure table entry) for output. */
3405 static void ecoff_swap_rpdr_out
3406 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3409 ecoff_swap_rpdr_out (abfd, in, ex)
3412 struct rpdr_ext *ex;
3414 /* ecoff_put_off was defined in ecoffswap.h. */
3415 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3416 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3417 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3418 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3419 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3420 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3422 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3423 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3425 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3427 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3431 /* Read ECOFF debugging information from a .mdebug section into a
3432 ecoff_debug_info structure. */
3435 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3438 struct ecoff_debug_info *debug;
3441 const struct ecoff_debug_swap *swap;
3442 char *ext_hdr = NULL;
3444 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3445 memset (debug, 0, sizeof(*debug));
3447 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3448 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3451 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3452 swap->external_hdr_size)
3456 symhdr = &debug->symbolic_header;
3457 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3459 /* The symbolic header contains absolute file offsets and sizes to
3461 #define READ(ptr, offset, count, size, type) \
3462 if (symhdr->count == 0) \
3463 debug->ptr = NULL; \
3466 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3467 if (debug->ptr == NULL) \
3468 goto error_return; \
3469 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3470 || (bfd_read (debug->ptr, size, symhdr->count, \
3471 abfd) != size * symhdr->count)) \
3472 goto error_return; \
3475 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3476 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3477 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3478 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3479 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3480 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3482 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3483 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3484 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3485 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3486 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3490 debug->adjust = NULL;
3495 if (ext_hdr != NULL)
3497 if (debug->line != NULL)
3499 if (debug->external_dnr != NULL)
3500 free (debug->external_dnr);
3501 if (debug->external_pdr != NULL)
3502 free (debug->external_pdr);
3503 if (debug->external_sym != NULL)
3504 free (debug->external_sym);
3505 if (debug->external_opt != NULL)
3506 free (debug->external_opt);
3507 if (debug->external_aux != NULL)
3508 free (debug->external_aux);
3509 if (debug->ss != NULL)
3511 if (debug->ssext != NULL)
3512 free (debug->ssext);
3513 if (debug->external_fdr != NULL)
3514 free (debug->external_fdr);
3515 if (debug->external_rfd != NULL)
3516 free (debug->external_rfd);
3517 if (debug->external_ext != NULL)
3518 free (debug->external_ext);
3522 /* MIPS ELF local labels start with '$', not 'L'. */
3526 mips_elf_is_local_label_name (abfd, name)
3533 /* On Irix 6, the labels go back to starting with '.', so we accept
3534 the generic ELF local label syntax as well. */
3535 return _bfd_elf_is_local_label_name (abfd, name);
3538 /* MIPS ELF uses a special find_nearest_line routine in order the
3539 handle the ECOFF debugging information. */
3541 struct mips_elf_find_line
3543 struct ecoff_debug_info d;
3544 struct ecoff_find_line i;
3548 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3549 functionname_ptr, line_ptr)
3554 const char **filename_ptr;
3555 const char **functionname_ptr;
3556 unsigned int *line_ptr;
3560 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3561 filename_ptr, functionname_ptr,
3565 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3566 filename_ptr, functionname_ptr,
3568 ABI_64_P (abfd) ? 8 : 0))
3571 msec = bfd_get_section_by_name (abfd, ".mdebug");
3575 struct mips_elf_find_line *fi;
3576 const struct ecoff_debug_swap * const swap =
3577 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3579 /* If we are called during a link, mips_elf_final_link may have
3580 cleared the SEC_HAS_CONTENTS field. We force it back on here
3581 if appropriate (which it normally will be). */
3582 origflags = msec->flags;
3583 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3584 msec->flags |= SEC_HAS_CONTENTS;
3586 fi = elf_tdata (abfd)->find_line_info;
3589 bfd_size_type external_fdr_size;
3592 struct fdr *fdr_ptr;
3594 fi = ((struct mips_elf_find_line *)
3595 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3598 msec->flags = origflags;
3602 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3604 msec->flags = origflags;
3608 /* Swap in the FDR information. */
3609 fi->d.fdr = ((struct fdr *)
3611 (fi->d.symbolic_header.ifdMax *
3612 sizeof (struct fdr))));
3613 if (fi->d.fdr == NULL)
3615 msec->flags = origflags;
3618 external_fdr_size = swap->external_fdr_size;
3619 fdr_ptr = fi->d.fdr;
3620 fraw_src = (char *) fi->d.external_fdr;
3621 fraw_end = (fraw_src
3622 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3623 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3624 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3626 elf_tdata (abfd)->find_line_info = fi;
3628 /* Note that we don't bother to ever free this information.
3629 find_nearest_line is either called all the time, as in
3630 objdump -l, so the information should be saved, or it is
3631 rarely called, as in ld error messages, so the memory
3632 wasted is unimportant. Still, it would probably be a
3633 good idea for free_cached_info to throw it away. */
3636 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3637 &fi->i, filename_ptr, functionname_ptr,
3640 msec->flags = origflags;
3644 msec->flags = origflags;
3647 /* Fall back on the generic ELF find_nearest_line routine. */
3649 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3650 filename_ptr, functionname_ptr,
3654 /* The mips16 compiler uses a couple of special sections to handle
3655 floating point arguments.
3657 Section names that look like .mips16.fn.FNNAME contain stubs that
3658 copy floating point arguments from the fp regs to the gp regs and
3659 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3660 call should be redirected to the stub instead. If no 32 bit
3661 function calls FNNAME, the stub should be discarded. We need to
3662 consider any reference to the function, not just a call, because
3663 if the address of the function is taken we will need the stub,
3664 since the address might be passed to a 32 bit function.
3666 Section names that look like .mips16.call.FNNAME contain stubs
3667 that copy floating point arguments from the gp regs to the fp
3668 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3669 then any 16 bit function that calls FNNAME should be redirected
3670 to the stub instead. If FNNAME is not a 32 bit function, the
3671 stub should be discarded.
3673 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3674 which call FNNAME and then copy the return value from the fp regs
3675 to the gp regs. These stubs store the return value in $18 while
3676 calling FNNAME; any function which might call one of these stubs
3677 must arrange to save $18 around the call. (This case is not
3678 needed for 32 bit functions that call 16 bit functions, because
3679 16 bit functions always return floating point values in both
3682 Note that in all cases FNNAME might be defined statically.
3683 Therefore, FNNAME is not used literally. Instead, the relocation
3684 information will indicate which symbol the section is for.
3686 We record any stubs that we find in the symbol table. */
3688 #define FN_STUB ".mips16.fn."
3689 #define CALL_STUB ".mips16.call."
3690 #define CALL_FP_STUB ".mips16.call.fp."
3692 /* MIPS ELF linker hash table. */
3694 struct mips_elf_link_hash_table
3696 struct elf_link_hash_table root;
3698 /* We no longer use this. */
3699 /* String section indices for the dynamic section symbols. */
3700 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3702 /* The number of .rtproc entries. */
3703 bfd_size_type procedure_count;
3704 /* The size of the .compact_rel section (if SGI_COMPAT). */
3705 bfd_size_type compact_rel_size;
3706 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3707 entry is set to the address of __rld_obj_head as in Irix 5. */
3708 boolean use_rld_obj_head;
3709 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3711 /* This is set if we see any mips16 stub sections. */
3712 boolean mips16_stubs_seen;
3715 /* Look up an entry in a MIPS ELF linker hash table. */
3717 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3718 ((struct mips_elf_link_hash_entry *) \
3719 elf_link_hash_lookup (&(table)->root, (string), (create), \
3722 /* Traverse a MIPS ELF linker hash table. */
3724 #define mips_elf_link_hash_traverse(table, func, info) \
3725 (elf_link_hash_traverse \
3727 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3730 /* Get the MIPS ELF linker hash table from a link_info structure. */
3732 #define mips_elf_hash_table(p) \
3733 ((struct mips_elf_link_hash_table *) ((p)->hash))
3735 static boolean mips_elf_output_extsym
3736 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3738 /* Create an entry in a MIPS ELF linker hash table. */
3740 static struct bfd_hash_entry *
3741 mips_elf_link_hash_newfunc (entry, table, string)
3742 struct bfd_hash_entry *entry;
3743 struct bfd_hash_table *table;
3746 struct mips_elf_link_hash_entry *ret =
3747 (struct mips_elf_link_hash_entry *) entry;
3749 /* Allocate the structure if it has not already been allocated by a
3751 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3752 ret = ((struct mips_elf_link_hash_entry *)
3753 bfd_hash_allocate (table,
3754 sizeof (struct mips_elf_link_hash_entry)));
3755 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3756 return (struct bfd_hash_entry *) ret;
3758 /* Call the allocation method of the superclass. */
3759 ret = ((struct mips_elf_link_hash_entry *)
3760 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3762 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3764 /* Set local fields. */
3765 memset (&ret->esym, 0, sizeof (EXTR));
3766 /* We use -2 as a marker to indicate that the information has
3767 not been set. -1 means there is no associated ifd. */
3769 ret->mips_32_relocs = 0;
3770 ret->min_dyn_reloc_index = 0;
3771 ret->fn_stub = NULL;
3772 ret->need_fn_stub = false;
3773 ret->call_stub = NULL;
3774 ret->call_fp_stub = NULL;
3777 return (struct bfd_hash_entry *) ret;
3780 /* Create a MIPS ELF linker hash table. */
3782 struct bfd_link_hash_table *
3783 _bfd_mips_elf_link_hash_table_create (abfd)
3786 struct mips_elf_link_hash_table *ret;
3788 ret = ((struct mips_elf_link_hash_table *)
3789 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3790 if (ret == (struct mips_elf_link_hash_table *) NULL)
3793 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3794 mips_elf_link_hash_newfunc))
3796 bfd_release (abfd, ret);
3801 /* We no longer use this. */
3802 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3803 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3805 ret->procedure_count = 0;
3806 ret->compact_rel_size = 0;
3807 ret->use_rld_obj_head = false;
3809 ret->mips16_stubs_seen = false;
3811 return &ret->root.root;
3814 /* Hook called by the linker routine which adds symbols from an object
3815 file. We must handle the special MIPS section numbers here. */
3819 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3821 struct bfd_link_info *info;
3822 const Elf_Internal_Sym *sym;
3824 flagword *flagsp ATTRIBUTE_UNUSED;
3828 if (SGI_COMPAT (abfd)
3829 && (abfd->flags & DYNAMIC) != 0
3830 && strcmp (*namep, "_rld_new_interface") == 0)
3832 /* Skip Irix 5 rld entry name. */
3837 switch (sym->st_shndx)
3840 /* Common symbols less than the GP size are automatically
3841 treated as SHN_MIPS_SCOMMON symbols. */
3842 if (sym->st_size > elf_gp_size (abfd)
3843 || IRIX_COMPAT (abfd) == ict_irix6)
3846 case SHN_MIPS_SCOMMON:
3847 *secp = bfd_make_section_old_way (abfd, ".scommon");
3848 (*secp)->flags |= SEC_IS_COMMON;
3849 *valp = sym->st_size;
3853 /* This section is used in a shared object. */
3854 if (mips_elf_text_section_ptr == NULL)
3856 /* Initialize the section. */
3857 mips_elf_text_section.name = ".text";
3858 mips_elf_text_section.flags = SEC_NO_FLAGS;
3859 mips_elf_text_section.output_section = NULL;
3860 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3861 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3862 mips_elf_text_symbol.name = ".text";
3863 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3864 mips_elf_text_symbol.section = &mips_elf_text_section;
3865 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3866 mips_elf_text_section_ptr = &mips_elf_text_section;
3868 /* This code used to do *secp = bfd_und_section_ptr if
3869 info->shared. I don't know why, and that doesn't make sense,
3870 so I took it out. */
3871 *secp = mips_elf_text_section_ptr;
3874 case SHN_MIPS_ACOMMON:
3875 /* Fall through. XXX Can we treat this as allocated data? */
3877 /* This section is used in a shared object. */
3878 if (mips_elf_data_section_ptr == NULL)
3880 /* Initialize the section. */
3881 mips_elf_data_section.name = ".data";
3882 mips_elf_data_section.flags = SEC_NO_FLAGS;
3883 mips_elf_data_section.output_section = NULL;
3884 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3885 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3886 mips_elf_data_symbol.name = ".data";
3887 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3888 mips_elf_data_symbol.section = &mips_elf_data_section;
3889 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3890 mips_elf_data_section_ptr = &mips_elf_data_section;
3892 /* This code used to do *secp = bfd_und_section_ptr if
3893 info->shared. I don't know why, and that doesn't make sense,
3894 so I took it out. */
3895 *secp = mips_elf_data_section_ptr;
3898 case SHN_MIPS_SUNDEFINED:
3899 *secp = bfd_und_section_ptr;
3903 if (SGI_COMPAT (abfd)
3905 && info->hash->creator == abfd->xvec
3906 && strcmp (*namep, "__rld_obj_head") == 0)
3908 struct elf_link_hash_entry *h;
3910 /* Mark __rld_obj_head as dynamic. */
3912 if (! (_bfd_generic_link_add_one_symbol
3913 (info, abfd, *namep, BSF_GLOBAL, *secp,
3914 (bfd_vma) *valp, (const char *) NULL, false,
3915 get_elf_backend_data (abfd)->collect,
3916 (struct bfd_link_hash_entry **) &h)))
3918 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3919 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3920 h->type = STT_OBJECT;
3922 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3925 mips_elf_hash_table (info)->use_rld_obj_head = true;
3928 /* If this is a mips16 text symbol, add 1 to the value to make it
3929 odd. This will cause something like .word SYM to come up with
3930 the right value when it is loaded into the PC. */
3931 if (sym->st_other == STO_MIPS16)
3937 /* Structure used to pass information to mips_elf_output_extsym. */
3942 struct bfd_link_info *info;
3943 struct ecoff_debug_info *debug;
3944 const struct ecoff_debug_swap *swap;
3948 /* This routine is used to write out ECOFF debugging external symbol
3949 information. It is called via mips_elf_link_hash_traverse. The
3950 ECOFF external symbol information must match the ELF external
3951 symbol information. Unfortunately, at this point we don't know
3952 whether a symbol is required by reloc information, so the two
3953 tables may wind up being different. We must sort out the external
3954 symbol information before we can set the final size of the .mdebug
3955 section, and we must set the size of the .mdebug section before we
3956 can relocate any sections, and we can't know which symbols are
3957 required by relocation until we relocate the sections.
3958 Fortunately, it is relatively unlikely that any symbol will be
3959 stripped but required by a reloc. In particular, it can not happen
3960 when generating a final executable. */
3963 mips_elf_output_extsym (h, data)
3964 struct mips_elf_link_hash_entry *h;
3967 struct extsym_info *einfo = (struct extsym_info *) data;
3969 asection *sec, *output_section;
3971 if (h->root.indx == -2)
3973 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3974 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3975 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3976 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3978 else if (einfo->info->strip == strip_all
3979 || (einfo->info->strip == strip_some
3980 && bfd_hash_lookup (einfo->info->keep_hash,
3981 h->root.root.root.string,
3982 false, false) == NULL))
3990 if (h->esym.ifd == -2)
3993 h->esym.cobol_main = 0;
3994 h->esym.weakext = 0;
3995 h->esym.reserved = 0;
3996 h->esym.ifd = ifdNil;
3997 h->esym.asym.value = 0;
3998 h->esym.asym.st = stGlobal;
4000 if (SGI_COMPAT (einfo->abfd)
4001 && (h->root.root.type == bfd_link_hash_undefined
4002 || h->root.root.type == bfd_link_hash_undefweak))
4006 /* Use undefined class. Also, set class and type for some
4008 name = h->root.root.root.string;
4009 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4010 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4012 h->esym.asym.sc = scData;
4013 h->esym.asym.st = stLabel;
4014 h->esym.asym.value = 0;
4016 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4018 h->esym.asym.sc = scAbs;
4019 h->esym.asym.st = stLabel;
4020 h->esym.asym.value =
4021 mips_elf_hash_table (einfo->info)->procedure_count;
4023 else if (strcmp (name, "_gp_disp") == 0)
4025 h->esym.asym.sc = scAbs;
4026 h->esym.asym.st = stLabel;
4027 h->esym.asym.value = elf_gp (einfo->abfd);
4030 h->esym.asym.sc = scUndefined;
4032 else if (h->root.root.type != bfd_link_hash_defined
4033 && h->root.root.type != bfd_link_hash_defweak)
4034 h->esym.asym.sc = scAbs;
4039 sec = h->root.root.u.def.section;
4040 output_section = sec->output_section;
4042 /* When making a shared library and symbol h is the one from
4043 the another shared library, OUTPUT_SECTION may be null. */
4044 if (output_section == NULL)
4045 h->esym.asym.sc = scUndefined;
4048 name = bfd_section_name (output_section->owner, output_section);
4050 if (strcmp (name, ".text") == 0)
4051 h->esym.asym.sc = scText;
4052 else if (strcmp (name, ".data") == 0)
4053 h->esym.asym.sc = scData;
4054 else if (strcmp (name, ".sdata") == 0)
4055 h->esym.asym.sc = scSData;
4056 else if (strcmp (name, ".rodata") == 0
4057 || strcmp (name, ".rdata") == 0)
4058 h->esym.asym.sc = scRData;
4059 else if (strcmp (name, ".bss") == 0)
4060 h->esym.asym.sc = scBss;
4061 else if (strcmp (name, ".sbss") == 0)
4062 h->esym.asym.sc = scSBss;
4063 else if (strcmp (name, ".init") == 0)
4064 h->esym.asym.sc = scInit;
4065 else if (strcmp (name, ".fini") == 0)
4066 h->esym.asym.sc = scFini;
4068 h->esym.asym.sc = scAbs;
4072 h->esym.asym.reserved = 0;
4073 h->esym.asym.index = indexNil;
4076 if (h->root.root.type == bfd_link_hash_common)
4077 h->esym.asym.value = h->root.root.u.c.size;
4078 else if (h->root.root.type == bfd_link_hash_defined
4079 || h->root.root.type == bfd_link_hash_defweak)
4081 if (h->esym.asym.sc == scCommon)
4082 h->esym.asym.sc = scBss;
4083 else if (h->esym.asym.sc == scSCommon)
4084 h->esym.asym.sc = scSBss;
4086 sec = h->root.root.u.def.section;
4087 output_section = sec->output_section;
4088 if (output_section != NULL)
4089 h->esym.asym.value = (h->root.root.u.def.value
4090 + sec->output_offset
4091 + output_section->vma);
4093 h->esym.asym.value = 0;
4095 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4097 /* Set type and value for a symbol with a function stub. */
4098 h->esym.asym.st = stProc;
4099 sec = h->root.root.u.def.section;
4101 h->esym.asym.value = 0;
4104 output_section = sec->output_section;
4105 if (output_section != NULL)
4106 h->esym.asym.value = (h->root.plt.offset
4107 + sec->output_offset
4108 + output_section->vma);
4110 h->esym.asym.value = 0;
4117 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4118 h->root.root.root.string,
4121 einfo->failed = true;
4128 /* Create a runtime procedure table from the .mdebug section. */
4131 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4134 struct bfd_link_info *info;
4136 struct ecoff_debug_info *debug;
4138 const struct ecoff_debug_swap *swap;
4139 HDRR *hdr = &debug->symbolic_header;
4141 struct rpdr_ext *erp;
4143 struct pdr_ext *epdr;
4144 struct sym_ext *esym;
4147 unsigned long size, count;
4148 unsigned long sindex;
4152 const char *no_name_func = _("static procedure (no name)");
4160 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4162 sindex = strlen (no_name_func) + 1;
4163 count = hdr->ipdMax;
4166 size = swap->external_pdr_size;
4168 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4172 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4175 size = sizeof (RPDR);
4176 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4180 sv = (char **) bfd_malloc (sizeof (char *) * count);
4184 count = hdr->isymMax;
4185 size = swap->external_sym_size;
4186 esym = (struct sym_ext *) bfd_malloc (size * count);
4190 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4193 count = hdr->issMax;
4194 ss = (char *) bfd_malloc (count);
4197 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4200 count = hdr->ipdMax;
4201 for (i = 0; i < count; i++, rp++)
4203 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4204 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4205 rp->adr = sym.value;
4206 rp->regmask = pdr.regmask;
4207 rp->regoffset = pdr.regoffset;
4208 rp->fregmask = pdr.fregmask;
4209 rp->fregoffset = pdr.fregoffset;
4210 rp->frameoffset = pdr.frameoffset;
4211 rp->framereg = pdr.framereg;
4212 rp->pcreg = pdr.pcreg;
4214 sv[i] = ss + sym.iss;
4215 sindex += strlen (sv[i]) + 1;
4219 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4220 size = BFD_ALIGN (size, 16);
4221 rtproc = (PTR) bfd_alloc (abfd, size);
4224 mips_elf_hash_table (info)->procedure_count = 0;
4228 mips_elf_hash_table (info)->procedure_count = count + 2;
4230 erp = (struct rpdr_ext *) rtproc;
4231 memset (erp, 0, sizeof (struct rpdr_ext));
4233 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4234 strcpy (str, no_name_func);
4235 str += strlen (no_name_func) + 1;
4236 for (i = 0; i < count; i++)
4238 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4239 strcpy (str, sv[i]);
4240 str += strlen (sv[i]) + 1;
4242 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
4244 /* Set the size and contents of .rtproc section. */
4245 s->_raw_size = size;
4246 s->contents = (bfd_byte *) rtproc;
4248 /* Skip this section later on (I don't think this currently
4249 matters, but someday it might). */
4250 s->link_order_head = (struct bfd_link_order *) NULL;
4279 /* A comparison routine used to sort .gptab entries. */
4282 gptab_compare (p1, p2)
4286 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4287 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4289 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4292 /* We need to use a special link routine to handle the .reginfo and
4293 the .mdebug sections. We need to merge all instances of these
4294 sections together, not write them all out sequentially. */
4297 _bfd_mips_elf_final_link (abfd, info)
4299 struct bfd_link_info *info;
4303 struct bfd_link_order *p;
4304 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4305 asection *rtproc_sec;
4306 Elf32_RegInfo reginfo;
4307 struct ecoff_debug_info debug;
4308 const struct ecoff_debug_swap *swap
4309 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4310 HDRR *symhdr = &debug.symbolic_header;
4311 PTR mdebug_handle = NULL;
4313 /* If all the things we linked together were PIC, but we're
4314 producing an executable (rather than a shared object), then the
4315 resulting file is CPIC (i.e., it calls PIC code.) */
4317 && !info->relocateable
4318 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4320 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4321 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4324 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4325 include it, even though we don't process it quite right. (Some
4326 entries are supposed to be merged.) Empirically, we seem to be
4327 better off including it then not. */
4328 if (IRIX_COMPAT (abfd) == ict_irix5)
4329 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4331 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4333 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4334 if (p->type == bfd_indirect_link_order)
4335 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4336 (*secpp)->link_order_head = NULL;
4337 *secpp = (*secpp)->next;
4338 --abfd->section_count;
4344 /* Get a value for the GP register. */
4345 if (elf_gp (abfd) == 0)
4347 struct bfd_link_hash_entry *h;
4349 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4350 if (h != (struct bfd_link_hash_entry *) NULL
4351 && h->type == bfd_link_hash_defined)
4352 elf_gp (abfd) = (h->u.def.value
4353 + h->u.def.section->output_section->vma
4354 + h->u.def.section->output_offset);
4355 else if (info->relocateable)
4359 /* Find the GP-relative section with the lowest offset. */
4361 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4363 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4366 /* And calculate GP relative to that. */
4367 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4371 /* If the relocate_section function needs to do a reloc
4372 involving the GP value, it should make a reloc_dangerous
4373 callback to warn that GP is not defined. */
4377 /* Go through the sections and collect the .reginfo and .mdebug
4381 gptab_data_sec = NULL;
4382 gptab_bss_sec = NULL;
4383 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4385 if (strcmp (o->name, ".reginfo") == 0)
4387 memset (®info, 0, sizeof reginfo);
4389 /* We have found the .reginfo section in the output file.
4390 Look through all the link_orders comprising it and merge
4391 the information together. */
4392 for (p = o->link_order_head;
4393 p != (struct bfd_link_order *) NULL;
4396 asection *input_section;
4398 Elf32_External_RegInfo ext;
4401 if (p->type != bfd_indirect_link_order)
4403 if (p->type == bfd_fill_link_order)
4408 input_section = p->u.indirect.section;
4409 input_bfd = input_section->owner;
4411 /* The linker emulation code has probably clobbered the
4412 size to be zero bytes. */
4413 if (input_section->_raw_size == 0)
4414 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4416 if (! bfd_get_section_contents (input_bfd, input_section,
4422 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4424 reginfo.ri_gprmask |= sub.ri_gprmask;
4425 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4426 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4427 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4428 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4430 /* ri_gp_value is set by the function
4431 mips_elf32_section_processing when the section is
4432 finally written out. */
4434 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4435 elf_link_input_bfd ignores this section. */
4436 input_section->flags &=~ SEC_HAS_CONTENTS;
4439 /* Size has been set in mips_elf_always_size_sections */
4440 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4442 /* Skip this section later on (I don't think this currently
4443 matters, but someday it might). */
4444 o->link_order_head = (struct bfd_link_order *) NULL;
4449 if (strcmp (o->name, ".mdebug") == 0)
4451 struct extsym_info einfo;
4453 /* We have found the .mdebug section in the output file.
4454 Look through all the link_orders comprising it and merge
4455 the information together. */
4456 symhdr->magic = swap->sym_magic;
4457 /* FIXME: What should the version stamp be? */
4459 symhdr->ilineMax = 0;
4463 symhdr->isymMax = 0;
4464 symhdr->ioptMax = 0;
4465 symhdr->iauxMax = 0;
4467 symhdr->issExtMax = 0;
4470 symhdr->iextMax = 0;
4472 /* We accumulate the debugging information itself in the
4473 debug_info structure. */
4475 debug.external_dnr = NULL;
4476 debug.external_pdr = NULL;
4477 debug.external_sym = NULL;
4478 debug.external_opt = NULL;
4479 debug.external_aux = NULL;
4481 debug.ssext = debug.ssext_end = NULL;
4482 debug.external_fdr = NULL;
4483 debug.external_rfd = NULL;
4484 debug.external_ext = debug.external_ext_end = NULL;
4486 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4487 if (mdebug_handle == (PTR) NULL)
4490 if (SGI_COMPAT (abfd))
4496 static const char * const name[] =
4497 { ".text", ".init", ".fini", ".data",
4498 ".rodata", ".sdata", ".sbss", ".bss" };
4499 static const int sc[] = { scText, scInit, scFini, scData,
4500 scRData, scSData, scSBss, scBss };
4503 esym.cobol_main = 0;
4507 esym.asym.iss = issNil;
4508 esym.asym.st = stLocal;
4509 esym.asym.reserved = 0;
4510 esym.asym.index = indexNil;
4512 for (i = 0; i < 8; i++)
4514 esym.asym.sc = sc[i];
4515 s = bfd_get_section_by_name (abfd, name[i]);
4518 esym.asym.value = s->vma;
4519 last = s->vma + s->_raw_size;
4522 esym.asym.value = last;
4524 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4530 for (p = o->link_order_head;
4531 p != (struct bfd_link_order *) NULL;
4534 asection *input_section;
4536 const struct ecoff_debug_swap *input_swap;
4537 struct ecoff_debug_info input_debug;
4541 if (p->type != bfd_indirect_link_order)
4543 if (p->type == bfd_fill_link_order)
4548 input_section = p->u.indirect.section;
4549 input_bfd = input_section->owner;
4551 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4552 || (get_elf_backend_data (input_bfd)
4553 ->elf_backend_ecoff_debug_swap) == NULL)
4555 /* I don't know what a non MIPS ELF bfd would be
4556 doing with a .mdebug section, but I don't really
4557 want to deal with it. */
4561 input_swap = (get_elf_backend_data (input_bfd)
4562 ->elf_backend_ecoff_debug_swap);
4564 BFD_ASSERT (p->size == input_section->_raw_size);
4566 /* The ECOFF linking code expects that we have already
4567 read in the debugging information and set up an
4568 ecoff_debug_info structure, so we do that now. */
4569 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4573 if (! (bfd_ecoff_debug_accumulate
4574 (mdebug_handle, abfd, &debug, swap, input_bfd,
4575 &input_debug, input_swap, info)))
4578 /* Loop through the external symbols. For each one with
4579 interesting information, try to find the symbol in
4580 the linker global hash table and save the information
4581 for the output external symbols. */
4582 eraw_src = input_debug.external_ext;
4583 eraw_end = (eraw_src
4584 + (input_debug.symbolic_header.iextMax
4585 * input_swap->external_ext_size));
4587 eraw_src < eraw_end;
4588 eraw_src += input_swap->external_ext_size)
4592 struct mips_elf_link_hash_entry *h;
4594 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4595 if (ext.asym.sc == scNil
4596 || ext.asym.sc == scUndefined
4597 || ext.asym.sc == scSUndefined)
4600 name = input_debug.ssext + ext.asym.iss;
4601 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4602 name, false, false, true);
4603 if (h == NULL || h->esym.ifd != -2)
4609 < input_debug.symbolic_header.ifdMax);
4610 ext.ifd = input_debug.ifdmap[ext.ifd];
4616 /* Free up the information we just read. */
4617 free (input_debug.line);
4618 free (input_debug.external_dnr);
4619 free (input_debug.external_pdr);
4620 free (input_debug.external_sym);
4621 free (input_debug.external_opt);
4622 free (input_debug.external_aux);
4623 free (input_debug.ss);
4624 free (input_debug.ssext);
4625 free (input_debug.external_fdr);
4626 free (input_debug.external_rfd);
4627 free (input_debug.external_ext);
4629 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4630 elf_link_input_bfd ignores this section. */
4631 input_section->flags &=~ SEC_HAS_CONTENTS;
4634 if (SGI_COMPAT (abfd) && info->shared)
4636 /* Create .rtproc section. */
4637 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4638 if (rtproc_sec == NULL)
4640 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4641 | SEC_LINKER_CREATED | SEC_READONLY);
4643 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4644 if (rtproc_sec == NULL
4645 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4646 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4650 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4651 info, rtproc_sec, &debug))
4655 /* Build the external symbol information. */
4658 einfo.debug = &debug;
4660 einfo.failed = false;
4661 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4662 mips_elf_output_extsym,
4667 /* Set the size of the .mdebug section. */
4668 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4670 /* Skip this section later on (I don't think this currently
4671 matters, but someday it might). */
4672 o->link_order_head = (struct bfd_link_order *) NULL;
4677 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4679 const char *subname;
4682 Elf32_External_gptab *ext_tab;
4685 /* The .gptab.sdata and .gptab.sbss sections hold
4686 information describing how the small data area would
4687 change depending upon the -G switch. These sections
4688 not used in executables files. */
4689 if (! info->relocateable)
4693 for (p = o->link_order_head;
4694 p != (struct bfd_link_order *) NULL;
4697 asection *input_section;
4699 if (p->type != bfd_indirect_link_order)
4701 if (p->type == bfd_fill_link_order)
4706 input_section = p->u.indirect.section;
4708 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4709 elf_link_input_bfd ignores this section. */
4710 input_section->flags &=~ SEC_HAS_CONTENTS;
4713 /* Skip this section later on (I don't think this
4714 currently matters, but someday it might). */
4715 o->link_order_head = (struct bfd_link_order *) NULL;
4717 /* Really remove the section. */
4718 for (secpp = &abfd->sections;
4720 secpp = &(*secpp)->next)
4722 *secpp = (*secpp)->next;
4723 --abfd->section_count;
4728 /* There is one gptab for initialized data, and one for
4729 uninitialized data. */
4730 if (strcmp (o->name, ".gptab.sdata") == 0)
4732 else if (strcmp (o->name, ".gptab.sbss") == 0)
4736 (*_bfd_error_handler)
4737 (_("%s: illegal section name `%s'"),
4738 bfd_get_filename (abfd), o->name);
4739 bfd_set_error (bfd_error_nonrepresentable_section);
4743 /* The linker script always combines .gptab.data and
4744 .gptab.sdata into .gptab.sdata, and likewise for
4745 .gptab.bss and .gptab.sbss. It is possible that there is
4746 no .sdata or .sbss section in the output file, in which
4747 case we must change the name of the output section. */
4748 subname = o->name + sizeof ".gptab" - 1;
4749 if (bfd_get_section_by_name (abfd, subname) == NULL)
4751 if (o == gptab_data_sec)
4752 o->name = ".gptab.data";
4754 o->name = ".gptab.bss";
4755 subname = o->name + sizeof ".gptab" - 1;
4756 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4759 /* Set up the first entry. */
4761 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4764 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4765 tab[0].gt_header.gt_unused = 0;
4767 /* Combine the input sections. */
4768 for (p = o->link_order_head;
4769 p != (struct bfd_link_order *) NULL;
4772 asection *input_section;
4776 bfd_size_type gpentry;
4778 if (p->type != bfd_indirect_link_order)
4780 if (p->type == bfd_fill_link_order)
4785 input_section = p->u.indirect.section;
4786 input_bfd = input_section->owner;
4788 /* Combine the gptab entries for this input section one
4789 by one. We know that the input gptab entries are
4790 sorted by ascending -G value. */
4791 size = bfd_section_size (input_bfd, input_section);
4793 for (gpentry = sizeof (Elf32_External_gptab);
4795 gpentry += sizeof (Elf32_External_gptab))
4797 Elf32_External_gptab ext_gptab;
4798 Elf32_gptab int_gptab;
4804 if (! (bfd_get_section_contents
4805 (input_bfd, input_section, (PTR) &ext_gptab,
4806 gpentry, sizeof (Elf32_External_gptab))))
4812 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4814 val = int_gptab.gt_entry.gt_g_value;
4815 add = int_gptab.gt_entry.gt_bytes - last;
4818 for (look = 1; look < c; look++)
4820 if (tab[look].gt_entry.gt_g_value >= val)
4821 tab[look].gt_entry.gt_bytes += add;
4823 if (tab[look].gt_entry.gt_g_value == val)
4829 Elf32_gptab *new_tab;
4832 /* We need a new table entry. */
4833 new_tab = ((Elf32_gptab *)
4834 bfd_realloc ((PTR) tab,
4835 (c + 1) * sizeof (Elf32_gptab)));
4836 if (new_tab == NULL)
4842 tab[c].gt_entry.gt_g_value = val;
4843 tab[c].gt_entry.gt_bytes = add;
4845 /* Merge in the size for the next smallest -G
4846 value, since that will be implied by this new
4849 for (look = 1; look < c; look++)
4851 if (tab[look].gt_entry.gt_g_value < val
4853 || (tab[look].gt_entry.gt_g_value
4854 > tab[max].gt_entry.gt_g_value)))
4858 tab[c].gt_entry.gt_bytes +=
4859 tab[max].gt_entry.gt_bytes;
4864 last = int_gptab.gt_entry.gt_bytes;
4867 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4868 elf_link_input_bfd ignores this section. */
4869 input_section->flags &=~ SEC_HAS_CONTENTS;
4872 /* The table must be sorted by -G value. */
4874 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4876 /* Swap out the table. */
4877 ext_tab = ((Elf32_External_gptab *)
4878 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4879 if (ext_tab == NULL)
4885 for (i = 0; i < c; i++)
4886 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4889 o->_raw_size = c * sizeof (Elf32_External_gptab);
4890 o->contents = (bfd_byte *) ext_tab;
4892 /* Skip this section later on (I don't think this currently
4893 matters, but someday it might). */
4894 o->link_order_head = (struct bfd_link_order *) NULL;
4898 /* Invoke the regular ELF backend linker to do all the work. */
4899 if (ABI_64_P (abfd))
4902 if (!bfd_elf64_bfd_final_link (abfd, info))
4909 else if (!bfd_elf32_bfd_final_link (abfd, info))
4912 /* Now write out the computed sections. */
4914 if (reginfo_sec != (asection *) NULL)
4916 Elf32_External_RegInfo ext;
4918 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
4919 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4920 (file_ptr) 0, sizeof ext))
4924 if (mdebug_sec != (asection *) NULL)
4926 BFD_ASSERT (abfd->output_has_begun);
4927 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4929 mdebug_sec->filepos))
4932 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4935 if (gptab_data_sec != (asection *) NULL)
4937 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4938 gptab_data_sec->contents,
4940 gptab_data_sec->_raw_size))
4944 if (gptab_bss_sec != (asection *) NULL)
4946 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4947 gptab_bss_sec->contents,
4949 gptab_bss_sec->_raw_size))
4953 if (SGI_COMPAT (abfd))
4955 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4956 if (rtproc_sec != NULL)
4958 if (! bfd_set_section_contents (abfd, rtproc_sec,
4959 rtproc_sec->contents,
4961 rtproc_sec->_raw_size))
4969 /* Handle a MIPS ELF HI16 reloc. */
4972 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4974 Elf_Internal_Rela *relhi;
4975 Elf_Internal_Rela *rello;
4982 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4984 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4987 addend += ((insn & 0xffff) << 16) + addlo;
4989 if ((addlo & 0x8000) != 0)
4991 if ((addend & 0x8000) != 0)
4994 bfd_put_32 (input_bfd,
4995 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4996 contents + relhi->r_offset);
4999 /* Handle a MIPS ELF local GOT16 reloc. */
5002 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
5007 Elf_Internal_Rela *relhi;
5008 Elf_Internal_Rela *rello;
5012 unsigned int assigned_gotno;
5018 bfd_byte *got_contents;
5019 struct mips_got_info *g;
5021 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5023 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5026 addend += ((insn & 0xffff) << 16) + addlo;
5028 if ((addlo & 0x8000) != 0)
5030 if ((addend & 0x8000) != 0)
5033 /* Get a got entry representing requested hipage. */
5034 BFD_ASSERT (elf_section_data (sgot) != NULL);
5035 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5036 BFD_ASSERT (g != NULL);
5038 assigned_gotno = g->assigned_gotno;
5039 got_contents = sgot->contents;
5040 hipage = addend & 0xffff0000;
5042 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
5044 address = bfd_get_32 (input_bfd, got_contents + i * 4);
5045 if (hipage == (address & 0xffff0000))
5049 if (i == assigned_gotno)
5051 if (assigned_gotno >= g->local_gotno)
5053 (*_bfd_error_handler)
5054 (_("more got entries are needed for hipage relocations"));
5055 bfd_set_error (bfd_error_bad_value);
5059 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
5060 ++g->assigned_gotno;
5063 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
5064 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
5065 contents + relhi->r_offset);
5070 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
5073 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
5075 Elf_Internal_Rela *rel;
5081 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5082 bfd_put_32 (input_bfd,
5083 (insn & 0xffff0000) | (offset & 0xffff),
5084 contents + rel->r_offset);
5087 /* Returns the GOT section for ABFD. */
5090 mips_elf_got_section (abfd)
5093 return bfd_get_section_by_name (abfd, ".got");
5096 /* Returns the GOT information associated with the link indicated by
5097 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5100 static struct mips_got_info *
5101 mips_elf_got_info (abfd, sgotp)
5106 struct mips_got_info *g;
5108 sgot = mips_elf_got_section (abfd);
5109 BFD_ASSERT (sgot != NULL);
5110 BFD_ASSERT (elf_section_data (sgot) != NULL);
5111 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5112 BFD_ASSERT (g != NULL);
5119 /* Sign-extend VALUE, which has the indicated number of BITS. */
5122 mips_elf_sign_extend (value, bits)
5126 if (value & (1 << (bits - 1)))
5127 /* VALUE is negative. */
5128 value |= ((bfd_vma) - 1) << bits;
5133 /* Return non-zero if the indicated VALUE has overflowed the maximum
5134 range expressable by a signed number with the indicated number of
5138 mips_elf_overflow_p (value, bits)
5142 bfd_signed_vma svalue = (bfd_signed_vma) value;
5144 if (svalue > (1 << (bits - 1)) - 1)
5145 /* The value is too big. */
5147 else if (svalue < -(1 << (bits - 1)))
5148 /* The value is too small. */
5155 /* Calculate the %high function. */
5158 mips_elf_high (value)
5161 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5164 /* Calculate the %higher function. */
5167 mips_elf_higher (value)
5168 bfd_vma value ATTRIBUTE_UNUSED;
5171 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5174 return (bfd_vma) -1;
5178 /* Calculate the %highest function. */
5181 mips_elf_highest (value)
5182 bfd_vma value ATTRIBUTE_UNUSED;
5185 return ((value + (bfd_vma) 0x800080008000) > 48) & 0xffff;
5188 return (bfd_vma) -1;
5192 /* Returns the GOT index for the global symbol indicated by H. */
5195 mips_elf_global_got_index (abfd, h)
5197 struct elf_link_hash_entry *h;
5201 struct mips_got_info *g;
5203 g = mips_elf_got_info (abfd, &sgot);
5205 /* Once we determine the global GOT entry with the lowest dynamic
5206 symbol table index, we must put all dynamic symbols with greater
5207 indices into the GOT. That makes it easy to calculate the GOT
5209 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5210 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5211 * MIPS_ELF_GOT_SIZE (abfd));
5212 BFD_ASSERT (index < sgot->_raw_size);
5217 /* Returns the offset for the entry at the INDEXth position
5221 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5229 sgot = mips_elf_got_section (dynobj);
5230 gp = _bfd_get_gp_value (output_bfd);
5231 return (sgot->output_section->vma + sgot->output_offset + index -
5235 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5236 symbol table index lower than any we've seen to date, record it for
5240 mips_elf_record_global_got_symbol (h, info, g)
5241 struct elf_link_hash_entry *h;
5242 struct bfd_link_info *info;
5243 struct mips_got_info *g ATTRIBUTE_UNUSED;
5245 /* A global symbol in the GOT must also be in the dynamic symbol
5247 if (h->dynindx == -1
5248 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5251 /* If we've already marked this entry as need GOT space, we don't
5252 need to do it again. */
5253 if (h->got.offset != (bfd_vma) - 1)
5256 /* By setting this to a value other than -1, we are indicating that
5257 there needs to be a GOT entry for H. */
5263 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5264 the dynamic symbols. */
5266 struct mips_elf_hash_sort_data
5268 /* The symbol in the global GOT with the lowest dynamic symbol table
5270 struct elf_link_hash_entry *low;
5271 /* The least dynamic symbol table index corresponding to a symbol
5272 with a GOT entry. */
5273 long min_got_dynindx;
5274 /* The greatest dynamic symbol table index not corresponding to a
5275 symbol without a GOT entry. */
5276 long max_non_got_dynindx;
5279 /* If H needs a GOT entry, assign it the highest available dynamic
5280 index. Otherwise, assign it the lowest available dynamic
5284 mips_elf_sort_hash_table_f (h, data)
5285 struct mips_elf_link_hash_entry *h;
5288 struct mips_elf_hash_sort_data *hsd
5289 = (struct mips_elf_hash_sort_data *) data;
5291 /* Symbols without dynamic symbol table entries aren't interesting
5293 if (h->root.dynindx == -1)
5296 if (h->root.got.offset != 0)
5297 h->root.dynindx = hsd->max_non_got_dynindx++;
5300 h->root.dynindx = --hsd->min_got_dynindx;
5301 hsd->low = (struct elf_link_hash_entry *) h;
5307 /* Sort the dynamic symbol table so that symbols that need GOT entries
5308 appear towards the end. This reduces the amount of GOT space
5312 mips_elf_sort_hash_table (info)
5313 struct bfd_link_info *info;
5315 struct mips_elf_hash_sort_data hsd;
5316 struct mips_got_info *g;
5319 dynobj = elf_hash_table (info)->dynobj;
5322 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5323 hsd.max_non_got_dynindx = 1;
5324 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5325 elf_hash_table (info)),
5326 mips_elf_sort_hash_table_f,
5329 /* There shoud have been enough room in the symbol table to
5330 accomodate both the GOT and non-GOT symbols. */
5331 BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5333 /* Now we know which dynamic symbol has the lowest dynamic symbol
5334 table index in the GOT. */
5335 g = mips_elf_got_info (dynobj, NULL);
5336 g->global_gotsym = hsd.low;
5341 /* Create a local GOT entry for VALUE. Return the index of the entry,
5342 or -1 if it could not be created. */
5345 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5347 struct mips_got_info *g;
5351 if (g->assigned_gotno >= g->local_gotno)
5353 /* We didn't allocate enough space in the GOT. */
5354 (*_bfd_error_handler)
5355 (_("not enough GOT space for local GOT entries"));
5356 bfd_set_error (bfd_error_bad_value);
5357 return (bfd_vma) -1;
5360 MIPS_ELF_PUT_WORD (abfd, value,
5362 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5363 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5366 /* Returns the GOT offset at which the indicated address can be found.
5367 If there is not yet a GOT entry for this value, create one. Returns
5368 -1 if no satisfactory GOT offset can be found. */
5371 mips_elf_local_got_index (abfd, info, value)
5373 struct bfd_link_info *info;
5377 struct mips_got_info *g;
5380 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5382 /* Look to see if we already have an appropriate entry. */
5383 for (entry = (sgot->contents
5384 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5385 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5386 entry += MIPS_ELF_GOT_SIZE (abfd))
5388 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5389 if (address == value)
5390 return entry - sgot->contents;
5393 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5396 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5397 are supposed to be placed at small offsets in the GOT, i.e.,
5398 within 32KB of GP. Return the index into the GOT for this page,
5399 and store the offset from this entry to the desired address in
5400 OFFSETP, if it is non-NULL. */
5403 mips_elf_got_page (abfd, info, value, offsetp)
5405 struct bfd_link_info *info;
5410 struct mips_got_info *g;
5412 bfd_byte *last_entry;
5416 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5418 /* Look to see if we aleady have an appropriate entry. */
5419 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5420 for (entry = (sgot->contents
5421 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5422 entry != last_entry;
5423 entry += MIPS_ELF_GOT_SIZE (abfd))
5425 address = MIPS_ELF_GET_WORD (abfd, entry);
5427 if (!mips_elf_overflow_p (value - address, 16))
5429 /* This entry will serve as the page pointer. We can add a
5430 16-bit number to it to get the actual address. */
5431 index = entry - sgot->contents;
5436 /* If we didn't have an appropriate entry, we create one now. */
5437 if (entry == last_entry)
5438 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5442 address = MIPS_ELF_GET_WORD (abfd, entry);
5443 *offsetp = value - address;
5449 /* Find a GOT entry whose higher-order 16 bits are the same as those
5450 for value. Return the index into the GOT for this entry. */
5453 mips_elf_got16_entry (abfd, info, value)
5455 struct bfd_link_info *info;
5459 struct mips_got_info *g;
5461 bfd_byte *last_entry;
5465 value &= 0xffff0000;
5466 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5468 /* Look to see if we already have an appropriate entry. */
5469 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5470 for (entry = (sgot->contents
5471 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5472 entry != last_entry;
5473 entry += MIPS_ELF_GOT_SIZE (abfd))
5475 address = MIPS_ELF_GET_WORD (abfd, entry);
5476 if ((address & 0xffff0000) == value)
5478 /* This entry has the right high-order 16 bits. */
5479 index = MIPS_ELF_GOT_SIZE (abfd) * (entry - sgot->contents);
5484 /* If we didn't have an appropriate entry, we create one now. */
5485 if (entry == last_entry)
5486 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5491 /* Sets *ADDENDP to the addend for the first R_MIPS_LO16 relocation
5492 found, beginning with RELOCATION. RELEND is one-past-the-end of
5493 the relocation table. */
5496 mips_elf_next_lo16_addend (relocation, relend, addendp)
5497 const Elf_Internal_Rela *relocation;
5498 const Elf_Internal_Rela *relend;
5501 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5502 immediately following. However, for the IRIX6 ABI, the next
5503 relocation may be a composed relocation consisting of several
5504 relocations for the same address. In that case, the R_MIPS_LO16
5505 relo!scation may occur as one of these. We permit a similar
5506 extension in general, as that is useful for GCC. */
5507 while (relocation < relend)
5509 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS_LO16)
5511 *addendp = relocation->r_addend;
5518 /* We didn't find it. */
5522 /* Create a rel.dyn relocation for the dynamic linker to resolve. The
5523 relocatin is against the symbol with the dynamic symbol table index
5524 DYNINDX. REL is the original relocation, which is now being made
5528 mips_elf_create_dynamic_relocation (output_bfd, info, rel, dynindx,
5529 addend, input_section)
5531 struct bfd_link_info *info;
5532 const Elf_Internal_Rela *rel;
5535 asection *input_section;
5537 Elf_Internal_Rel outrel;
5543 r_type = ELF32_R_TYPE (rel->r_info);
5544 dynobj = elf_hash_table (info)->dynobj;
5546 = bfd_get_section_by_name (dynobj,
5547 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5548 BFD_ASSERT (sreloc != NULL);
5552 /* The symbol for the relocation is the same as it was for the
5553 original relocation. */
5554 outrel.r_info = ELF32_R_INFO (dynindx, R_MIPS_REL32);
5556 /* The offset for the dynamic relocation is the same as for the
5557 original relocation, adjusted by the offset at which the original
5558 section is output. */
5559 if (elf_section_data (input_section)->stab_info == NULL)
5560 outrel.r_offset = rel->r_offset;
5565 off = (_bfd_stab_section_offset
5566 (output_bfd, &elf_hash_table (info)->stab_info,
5568 &elf_section_data (input_section)->stab_info,
5570 if (off == (bfd_vma) -1)
5572 outrel.r_offset = off;
5574 outrel.r_offset += (input_section->output_section->vma
5575 + input_section->output_offset);
5577 /* If we've decided to skip this relocation, just output an emtpy
5580 memset (&outrel, 0, sizeof (outrel));
5582 if (ABI_64_P (output_bfd))
5584 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5585 (output_bfd, &outrel,
5587 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5590 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5591 (((Elf32_External_Rel *)
5593 + sreloc->reloc_count));
5594 ++sreloc->reloc_count;
5596 /* Make sure the output section is writable. The dynamic linker
5597 will be writing to it. */
5598 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5601 /* On IRIX5, make an entry of compact relocation info. */
5602 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5604 asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5609 Elf32_crinfo cptrel;
5611 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5612 cptrel.vaddr = (rel->r_offset
5613 + input_section->output_section->vma
5614 + input_section->output_offset);
5615 if (r_type == R_MIPS_REL32)
5616 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5618 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5619 mips_elf_set_cr_dist2to (cptrel, 0);
5620 cptrel.konst = addend;
5622 cr = (scpt->contents
5623 + sizeof (Elf32_External_compact_rel));
5624 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5625 ((Elf32_External_crinfo *) cr
5626 + scpt->reloc_count));
5627 ++scpt->reloc_count;
5631 return sreloc->reloc_count - 1;
5634 /* Calculate the value produced by the RELOCATION (which comes from
5635 the INPUT_BFD). The ADDEND is the addend to use for this
5636 RELOCATION; RELOCATION->R_ADDEND is ignored.
5638 The result of the relocation calculation is stored in VALUEP.
5640 This function returns bfd_reloc_continue if the caller need take no
5641 further action regarding this relocation, bfd_reloc_notsupported if
5642 something goes dramatically wrong, bfd_reloc_overflow if an
5643 overflow occurs, and bfd_reloc_ok to indicate success. */
5645 static bfd_reloc_status_type
5646 mips_elf_calculate_relocation (abfd,
5659 asection *input_section;
5660 struct bfd_link_info *info;
5661 const Elf_Internal_Rela *relocation;
5663 reloc_howto_type *howto;
5664 Elf_Internal_Sym *local_syms;
5665 asection **local_sections;
5669 /* The eventual value we will return. */
5671 /* The address of the symbol against which the relocation is
5674 /* The final GP value to be used for the relocatable, executable, or
5675 shared object file being produced. */
5676 bfd_vma gp = (bfd_vma) - 1;
5677 /* The place (section offset or address) of the storage unit being
5680 /* The value of GP used to create the relocatable object. */
5681 bfd_vma gp0 = (bfd_vma) - 1;
5682 /* The offset into the global offset table at which the address of
5683 the relocation entry symbol, adjusted by the addend, resides
5684 during execution. */
5685 bfd_vma g = (bfd_vma) - 1;
5686 /* The section in which the symbol referenced by the relocation is
5688 asection *sec = NULL;
5689 struct mips_elf_link_hash_entry* h = NULL;
5690 /* True if the symbol referred to by this relocation is a local
5693 /* True if the symbol referred to by this relocation is "_gp_disp". */
5694 boolean gp_disp_p = false;
5695 Elf_Internal_Shdr *symtab_hdr;
5697 unsigned long r_symndx;
5699 /* True if overflow occurred during the calculation of the
5700 relocation value. */
5701 boolean overflowed_p;
5703 /* Parse the relocation. */
5704 r_symndx = ELF32_R_SYM (relocation->r_info);
5705 r_type = ELF32_R_TYPE (relocation->r_info);
5706 p = (input_section->output_section->vma
5707 + input_section->output_offset
5708 + relocation->r_offset);
5710 /* Assume that there will be no overflow. */
5711 overflowed_p = false;
5713 /* Figure out whether or not the symbol is local. */
5714 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5715 if (elf_bad_symtab (input_bfd))
5717 /* The symbol table does not follow the rule that local symbols
5718 must come before globals. */
5720 local_p = local_sections[r_symndx] != NULL;
5724 extsymoff = symtab_hdr->sh_info;
5725 local_p = r_symndx < extsymoff;
5728 /* Figure out the value of the symbol. */
5731 Elf_Internal_Sym *sym;
5733 sym = local_syms + r_symndx;
5734 sec = local_sections[r_symndx];
5736 symbol = sec->output_section->vma + sec->output_offset;
5737 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5738 symbol += sym->st_value;
5740 /* MIPS16 text labels should be treated as odd. */
5741 if (sym->st_other == STO_MIPS16)
5744 /* Record the name of this symbol, for our caller. */
5745 *namep = bfd_elf_string_from_elf_section (input_bfd,
5746 symtab_hdr->sh_link,
5749 *namep = bfd_section_name (input_bfd, sec);
5753 /* For global symbols we look up the symbol in the hash-table. */
5754 h = ((struct mips_elf_link_hash_entry *)
5755 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5756 /* Find the real hash-table entry for this symbol. */
5757 while (h->root.type == bfd_link_hash_indirect
5758 || h->root.type == bfd_link_hash_warning)
5759 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5761 /* Record the name of this symbol, for our caller. */
5762 *namep = h->root.root.root.string;
5764 /* See if this is the special _gp_disp symbol. Note that such a
5765 symbol must always be a global symbol. */
5766 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5768 /* Relocations against _gp_disp are permitted only with
5769 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5770 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5771 return bfd_reloc_notsupported;
5776 /* If this symbol is defined, calculate its address. */
5777 if ((h->root.root.type == bfd_link_hash_defined
5778 || h->root.root.type == bfd_link_hash_defweak)
5779 && h->root.root.u.def.section)
5781 sec = h->root.root.u.def.section;
5782 if (sec->output_section)
5783 symbol = (h->root.root.u.def.value
5784 + sec->output_section->vma
5785 + sec->output_offset);
5787 symbol = h->root.root.u.def.value;
5791 (*info->callbacks->undefined_symbol)
5792 (info, h->root.root.root.string, input_bfd,
5793 input_section, relocation->r_offset);
5794 return bfd_reloc_undefined;
5798 /* If we haven't already determined the GOT offset, or the GP value,
5799 and we're going to need it, get it now. */
5803 case R_MIPS_GOT_DISP:
5804 case R_MIPS_GOT_HI16:
5805 case R_MIPS_CALL_HI16:
5806 case R_MIPS_GOT_LO16:
5807 case R_MIPS_CALL_LO16:
5808 /* Find the index into the GOT where this value is located. */
5811 BFD_ASSERT (addend == 0);
5812 g = mips_elf_global_got_index
5813 (elf_hash_table (info)->dynobj,
5814 (struct elf_link_hash_entry*) h);
5818 g = mips_elf_local_got_index (abfd, info, symbol + addend);
5819 if (g == (bfd_vma) -1)
5823 /* Convert GOT indices to actual offsets. */
5824 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5830 case R_MIPS_GPREL16:
5831 case R_MIPS_GPREL32:
5832 gp0 = _bfd_get_gp_value (input_bfd);
5833 gp = _bfd_get_gp_value (abfd);
5840 /* Figure out what kind of relocation is being performed. */
5844 return bfd_reloc_continue;
5847 value = symbol + mips_elf_sign_extend (addend, 16);
5848 overflowed_p = mips_elf_overflow_p (value, 16);
5853 /* If we're creating a shared library, or this relocation is
5854 against a symbol in a shared library, then we can't know
5855 where the symbol will end up. So, we create a relocation
5856 record in the output, and leave the job up to the dynamic
5858 if (info->shared || !sec->output_section)
5860 unsigned int reloc_index;
5862 BFD_ASSERT (h != NULL);
5864 = mips_elf_create_dynamic_relocation (abfd,
5870 if (h->min_dyn_reloc_index == 0
5871 || reloc_index < h->min_dyn_reloc_index)
5872 h->min_dyn_reloc_index = reloc_index;
5873 value = symbol + addend;
5877 if (r_type == R_MIPS_32)
5878 value = symbol + addend;
5882 value &= howto->dst_mask;
5887 value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
5889 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
5890 value &= howto->dst_mask;
5896 value = mips_elf_high (addend + symbol);
5897 value &= howto->dst_mask;
5901 value = mips_elf_high (addend + gp - p);
5902 overflowed_p = mips_elf_overflow_p (value, 16);
5908 value = (symbol + addend) & howto->dst_mask;
5911 value = addend + gp - p + 4;
5912 overflowed_p = mips_elf_overflow_p (value, 16);
5916 case R_MIPS_LITERAL:
5917 /* Because we don't merge literal sections, we can handle this
5918 just like R_MIPS_GPREL16. In the long run, we should merge
5919 shared literals, and then we will need to additional work
5924 case R_MIPS_GPREL16:
5926 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
5928 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
5929 overflowed_p = mips_elf_overflow_p (value, 16);
5935 value = mips_elf_got16_entry (abfd, info, symbol + addend);
5936 if (value == (bfd_vma) -1)
5939 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5942 overflowed_p = mips_elf_overflow_p (value, 16);
5949 case R_MIPS_GOT_DISP:
5951 overflowed_p = mips_elf_overflow_p (value, 16);
5954 case R_MIPS_GPREL32:
5955 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
5959 value = mips_elf_sign_extend (addend, 16) + symbol - p;
5960 overflowed_p = mips_elf_overflow_p (value, 16);
5963 case R_MIPS_GOT_HI16:
5964 case R_MIPS_CALL_HI16:
5965 /* We're allowed to handle these two relocations identically.
5966 The dynamic linker is allowed to handle the CALL relocations
5967 differently by creating a lazy evaluation stub. */
5969 value = mips_elf_high (value);
5970 value &= howto->dst_mask;
5973 case R_MIPS_GOT_LO16:
5974 case R_MIPS_CALL_LO16:
5975 value = g & howto->dst_mask;
5979 value = (symbol + addend) & howto->dst_mask;
5982 case R_MIPS_GOT_PAGE:
5983 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
5984 if (value == (bfd_vma) -1)
5986 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5989 overflowed_p = mips_elf_overflow_p (value, 16);
5992 case R_MIPS_GOT_OFST:
5993 mips_elf_got_page (abfd, info, symbol + addend, &value);
5994 overflowed_p = mips_elf_overflow_p (value, 16);
5998 value = symbol - addend;
5999 value &= howto->dst_mask;
6003 value = mips_elf_higher (addend + symbol);
6004 value &= howto->dst_mask;
6007 case R_MIPS_HIGHEST:
6008 value = mips_elf_highest (addend + symbol);
6009 value &= howto->dst_mask;
6012 case R_MIPS_SCN_DISP:
6013 value = symbol + addend - sec->output_offset;
6014 value &= howto->dst_mask;
6019 /* Both of these may be ignored. R_MIPS_JALR is an optimization
6020 hint; we could improve performance by honoring that hint. */
6021 return bfd_reloc_continue;
6023 case R_MIPS_GNU_VTINHERIT:
6024 case R_MIPS_GNU_VTENTRY:
6025 /* We don't do anything with these at present. */
6026 return bfd_reloc_continue;
6029 case R_MIPS16_GPREL:
6030 /* These relocations, used for MIPS16, are not clearly
6031 documented anywhere. What do they do? */
6032 return bfd_reloc_notsupported;
6035 /* An unrecognized relocation type. */
6036 return bfd_reloc_notsupported;
6039 /* Store the VALUE for our caller. */
6041 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6044 /* Obtain the field relocated by RELOCATION. */
6047 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6048 reloc_howto_type *howto;
6049 const Elf_Internal_Rela *relocation;
6054 bfd_byte *location = contents + relocation->r_offset;
6056 switch (bfd_get_reloc_size (howto))
6063 x = bfd_get_8 (input_bfd, location);
6067 x = bfd_get_16 (input_bfd, location);
6071 x = bfd_get_32 (input_bfd, location);
6076 x = bfd_get_64 (input_bfd, location);
6090 /* It has been determined that the result of the RELOCATION is the
6091 VALUE. Use HOWTO to place VALUE into the output file at the
6092 appropriate position. The SECTION is the section to which the
6095 Returns false if anything goes wrong. */
6098 mips_elf_perform_relocation (howto, relocation, value, input_bfd, contents)
6099 reloc_howto_type *howto;
6100 const Elf_Internal_Rela *relocation;
6106 bfd_byte *location = contents + relocation->r_offset;
6108 /* Obtain the current value. */
6109 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6111 /* Clear the field we are setting. */
6112 x &= ~howto->dst_mask;
6114 /* Set the field. */
6115 x |= (value & howto->dst_mask);
6117 /* Put the value into the output. */
6118 switch (bfd_get_reloc_size (howto))
6125 bfd_put_8 (input_bfd, x, location);
6129 bfd_put_16 (input_bfd, x, location);
6133 bfd_put_32 (input_bfd, x, location);
6138 bfd_put_64 (input_bfd, x, location);
6150 /* Relocate a MIPS ELF section. */
6153 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6154 contents, relocs, local_syms, local_sections)
6156 struct bfd_link_info *info;
6158 asection *input_section;
6160 Elf_Internal_Rela *relocs;
6161 Elf_Internal_Sym *local_syms;
6162 asection **local_sections;
6164 const Elf_Internal_Rela *rel;
6165 const Elf_Internal_Rela *relend;
6167 bfd_vma last_hi16_addend;
6168 boolean use_saved_addend_p = false;
6169 boolean last_hi16_addend_valid_p = false;
6170 struct elf_backend_data *bed;
6172 bed = get_elf_backend_data (output_bfd);
6173 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6174 for (rel = relocs; rel < relend; ++rel)
6178 reloc_howto_type *howto;
6180 /* Find the relocation howto for this relocation. */
6181 howto = elf_mips_howto_table + ELF32_R_TYPE (rel->r_info);
6183 if (!use_saved_addend_p)
6185 Elf_Internal_Shdr *rel_hdr;
6187 /* If these relocations were originally of the REL variety,
6188 we must pull the addend out of the field that will be
6189 relocated. Otherwise, we simply use the contents of the
6190 RELA relocation. To determine which flavor or relocation
6191 this is, we depend on the fact that the INPUT_SECTION's
6192 REL_HDR is read before its REL_HDR2. */
6193 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6194 if ((size_t) (rel - relocs)
6195 >= (rel_hdr->sh_size / rel_hdr->sh_entsize
6196 * bed->s->int_rels_per_ext_rel))
6197 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6198 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6200 int r_type = ELF32_R_TYPE (rel->r_info);
6202 addend = mips_elf_obtain_contents (howto,
6206 addend &= howto->src_mask;
6208 /* For some kinds of relocations, the ADDEND is a
6209 combination of the addend stored in two different
6211 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16)
6213 /* Scan ahead to find a matching R_MIPS_LO16
6217 if (!mips_elf_next_lo16_addend (rel, relend, &l))
6220 /* Save the high-order bit for later. When we
6221 encounter the R_MIPS_LO16 relocation we will need
6224 last_hi16_addend = addend;
6225 last_hi16_addend_valid_p = true;
6227 /* Compute the combined addend. */
6230 else if (r_type == R_MIPS_LO16)
6232 /* Used the saved HI16 addend. */
6233 if (!last_hi16_addend_valid_p)
6235 addend |= last_hi16_addend;
6239 addend = rel->r_addend;
6242 /* In the N32 and 64-bit ABIs there may be multiple consecutive
6243 relocations for the same offset. In that case we are
6244 supposed to treat the output of each relocation as the addend
6246 if (rel + 1 < relend
6247 && rel->r_offset == rel[1].r_offset
6248 && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
6249 use_saved_addend_p = true;
6251 use_saved_addend_p = false;
6253 /* Figure out what value we are supposed to relocate. */
6254 switch (mips_elf_calculate_relocation (output_bfd,
6266 case bfd_reloc_continue:
6267 /* There's nothing to do. */
6270 case bfd_reloc_undefined:
6273 case bfd_reloc_notsupported:
6277 case bfd_reloc_overflow:
6278 if (use_saved_addend_p)
6279 /* Ignore overflow until we reach the last relocation for
6280 a given location. */
6283 || ! ((*info->callbacks->reloc_overflow)
6284 (info, name, howto->name, (bfd_vma) 0,
6285 input_bfd, input_section, rel->r_offset)))
6298 /* If we've got another relocation for the address, keep going
6299 until we reach the last one. */
6300 if (use_saved_addend_p)
6306 /* Actually perform the relocation. */
6307 mips_elf_perform_relocation (howto, rel, value, input_bfd,
6314 /* This hook function is called before the linker writes out a global
6315 symbol. We mark symbols as small common if appropriate. This is
6316 also where we undo the increment of the value for a mips16 symbol. */
6320 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6321 bfd *abfd ATTRIBUTE_UNUSED;
6322 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6323 const char *name ATTRIBUTE_UNUSED;
6324 Elf_Internal_Sym *sym;
6325 asection *input_sec;
6327 /* If we see a common symbol, which implies a relocatable link, then
6328 if a symbol was small common in an input file, mark it as small
6329 common in the output file. */
6330 if (sym->st_shndx == SHN_COMMON
6331 && strcmp (input_sec->name, ".scommon") == 0)
6332 sym->st_shndx = SHN_MIPS_SCOMMON;
6334 if (sym->st_other == STO_MIPS16
6335 && (sym->st_value & 1) != 0)
6341 /* Functions for the dynamic linker. */
6343 /* The name of the dynamic interpreter. This is put in the .interp
6346 #define ELF_DYNAMIC_INTERPRETER(abfd) \
6347 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
6348 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
6349 : "/usr/lib/libc.so.1")
6351 /* Create dynamic sections when linking against a dynamic object. */
6354 _bfd_mips_elf_create_dynamic_sections (abfd, info)
6356 struct bfd_link_info *info;
6358 struct elf_link_hash_entry *h;
6360 register asection *s;
6361 const char * const *namep;
6363 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6364 | SEC_LINKER_CREATED | SEC_READONLY);
6366 /* Mips ABI requests the .dynamic section to be read only. */
6367 s = bfd_get_section_by_name (abfd, ".dynamic");
6370 if (! bfd_set_section_flags (abfd, s, flags))
6374 /* We need to create .got section. */
6375 if (! mips_elf_create_got_section (abfd, info))
6378 /* Create the .msym section on IRIX6. It is used by the dynamic
6379 linker to speed up dynamic relocations, and to avoid computing
6380 the ELF hash for symbols. */
6381 if (IRIX_COMPAT (abfd) == ict_irix6
6382 && !mips_elf_create_msym_section (abfd))
6385 /* Create .stub section. */
6386 if (bfd_get_section_by_name (abfd,
6387 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6389 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6391 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6392 || ! bfd_set_section_alignment (abfd, s,
6393 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6397 if (IRIX_COMPAT (abfd) == ict_irix5
6399 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6401 s = bfd_make_section (abfd, ".rld_map");
6403 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6404 || ! bfd_set_section_alignment (abfd, s,
6405 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6409 /* On IRIX5, we adjust add some additional symbols and change the
6410 alignments of several sections. There is no ABI documentation
6411 indicating that this is necessary on IRIX6, nor any evidence that
6412 the linker takes such action. */
6413 if (IRIX_COMPAT (abfd) == ict_irix5)
6415 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6418 if (! (_bfd_generic_link_add_one_symbol
6419 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
6420 (bfd_vma) 0, (const char *) NULL, false,
6421 get_elf_backend_data (abfd)->collect,
6422 (struct bfd_link_hash_entry **) &h)))
6424 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6425 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6426 h->type = STT_SECTION;
6428 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6432 /* We need to create a .compact_rel section. */
6433 if (! mips_elf_create_compact_rel_section (abfd, info))
6436 /* Change aligments of some sections. */
6437 s = bfd_get_section_by_name (abfd, ".hash");
6439 bfd_set_section_alignment (abfd, s, 4);
6440 s = bfd_get_section_by_name (abfd, ".dynsym");
6442 bfd_set_section_alignment (abfd, s, 4);
6443 s = bfd_get_section_by_name (abfd, ".dynstr");
6445 bfd_set_section_alignment (abfd, s, 4);
6446 s = bfd_get_section_by_name (abfd, ".reginfo");
6448 bfd_set_section_alignment (abfd, s, 4);
6449 s = bfd_get_section_by_name (abfd, ".dynamic");
6451 bfd_set_section_alignment (abfd, s, 4);
6457 if (! (_bfd_generic_link_add_one_symbol
6458 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
6459 (bfd_vma) 0, (const char *) NULL, false,
6460 get_elf_backend_data (abfd)->collect,
6461 (struct bfd_link_hash_entry **) &h)))
6463 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6464 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6465 h->type = STT_SECTION;
6467 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6470 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6472 /* __rld_map is a four byte word located in the .data section
6473 and is filled in by the rtld to contain a pointer to
6474 the _r_debug structure. Its symbol value will be set in
6475 mips_elf_finish_dynamic_symbol. */
6476 s = bfd_get_section_by_name (abfd, ".rld_map");
6477 BFD_ASSERT (s != NULL);
6480 if (! (_bfd_generic_link_add_one_symbol
6481 (info, abfd, "__rld_map", BSF_GLOBAL, s,
6482 (bfd_vma) 0, (const char *) NULL, false,
6483 get_elf_backend_data (abfd)->collect,
6484 (struct bfd_link_hash_entry **) &h)))
6486 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6487 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6488 h->type = STT_OBJECT;
6490 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6498 /* Create the .compact_rel section. */
6501 mips_elf_create_compact_rel_section (abfd, info)
6503 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6506 register asection *s;
6508 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
6510 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
6513 s = bfd_make_section (abfd, ".compact_rel");
6515 || ! bfd_set_section_flags (abfd, s, flags)
6516 || ! bfd_set_section_alignment (abfd, s,
6517 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6520 s->_raw_size = sizeof (Elf32_External_compact_rel);
6526 /* Create the .got section to hold the global offset table. */
6529 mips_elf_create_got_section (abfd, info)
6531 struct bfd_link_info *info;
6534 register asection *s;
6535 struct elf_link_hash_entry *h;
6536 struct mips_got_info *g;
6538 /* This function may be called more than once. */
6539 if (mips_elf_got_section (abfd))
6542 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6543 | SEC_LINKER_CREATED);
6545 s = bfd_make_section (abfd, ".got");
6547 || ! bfd_set_section_flags (abfd, s, flags)
6548 || ! bfd_set_section_alignment (abfd, s, 4))
6551 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
6552 linker script because we don't want to define the symbol if we
6553 are not creating a global offset table. */
6555 if (! (_bfd_generic_link_add_one_symbol
6556 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
6557 (bfd_vma) 0, (const char *) NULL, false,
6558 get_elf_backend_data (abfd)->collect,
6559 (struct bfd_link_hash_entry **) &h)))
6561 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6562 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6563 h->type = STT_OBJECT;
6566 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
6569 /* The first several global offset table entries are reserved. */
6570 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
6572 g = (struct mips_got_info *) bfd_alloc (abfd,
6573 sizeof (struct mips_got_info));
6576 g->global_gotsym = NULL;
6577 g->local_gotno = MIPS_RESERVED_GOTNO;
6578 g->assigned_gotno = MIPS_RESERVED_GOTNO;
6579 if (elf_section_data (s) == NULL)
6582 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
6583 if (elf_section_data (s) == NULL)
6586 elf_section_data (s)->tdata = (PTR) g;
6587 elf_section_data (s)->this_hdr.sh_flags
6588 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6593 /* Returns the .msym section for ABFD, creating it if it does not
6594 already exist. Returns NULL to indicate error. */
6597 mips_elf_create_msym_section (abfd)
6602 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6605 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6607 || !bfd_set_section_flags (abfd, s,
6611 | SEC_LINKER_CREATED
6613 || !bfd_set_section_alignment (abfd, s,
6614 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6621 /* Add room for N relocations to the .rel.dyn section in ABFD. */
6624 mips_elf_allocate_dynamic_relocations (abfd, n)
6630 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
6631 BFD_ASSERT (s != NULL);
6633 if (s->_raw_size == 0)
6635 /* Make room for a null element. */
6636 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
6639 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
6642 /* Look through the relocs for a section during the first phase, and
6643 allocate space in the global offset table. */
6646 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
6648 struct bfd_link_info *info;
6650 const Elf_Internal_Rela *relocs;
6654 Elf_Internal_Shdr *symtab_hdr;
6655 struct elf_link_hash_entry **sym_hashes;
6656 struct mips_got_info *g;
6658 const Elf_Internal_Rela *rel;
6659 const Elf_Internal_Rela *rel_end;
6662 struct elf_backend_data *bed;
6664 if (info->relocateable)
6667 dynobj = elf_hash_table (info)->dynobj;
6668 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6669 sym_hashes = elf_sym_hashes (abfd);
6670 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6672 /* Check for the mips16 stub sections. */
6674 name = bfd_get_section_name (abfd, sec);
6675 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6677 unsigned long r_symndx;
6679 /* Look at the relocation information to figure out which symbol
6682 r_symndx = ELF32_R_SYM (relocs->r_info);
6684 if (r_symndx < extsymoff
6685 || sym_hashes[r_symndx - extsymoff] == NULL)
6689 /* This stub is for a local symbol. This stub will only be
6690 needed if there is some relocation in this BFD, other
6691 than a 16 bit function call, which refers to this symbol. */
6692 for (o = abfd->sections; o != NULL; o = o->next)
6694 Elf_Internal_Rela *sec_relocs;
6695 const Elf_Internal_Rela *r, *rend;
6697 /* We can ignore stub sections when looking for relocs. */
6698 if ((o->flags & SEC_RELOC) == 0
6699 || o->reloc_count == 0
6700 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6701 sizeof FN_STUB - 1) == 0
6702 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6703 sizeof CALL_STUB - 1) == 0
6704 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6705 sizeof CALL_FP_STUB - 1) == 0)
6708 sec_relocs = (_bfd_elf32_link_read_relocs
6709 (abfd, o, (PTR) NULL,
6710 (Elf_Internal_Rela *) NULL,
6711 info->keep_memory));
6712 if (sec_relocs == NULL)
6715 rend = sec_relocs + o->reloc_count;
6716 for (r = sec_relocs; r < rend; r++)
6717 if (ELF32_R_SYM (r->r_info) == r_symndx
6718 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6721 if (! info->keep_memory)
6730 /* There is no non-call reloc for this stub, so we do
6731 not need it. Since this function is called before
6732 the linker maps input sections to output sections, we
6733 can easily discard it by setting the SEC_EXCLUDE
6735 sec->flags |= SEC_EXCLUDE;
6739 /* Record this stub in an array of local symbol stubs for
6741 if (elf_tdata (abfd)->local_stubs == NULL)
6743 unsigned long symcount;
6746 if (elf_bad_symtab (abfd))
6747 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
6749 symcount = symtab_hdr->sh_info;
6750 n = (asection **) bfd_zalloc (abfd,
6751 symcount * sizeof (asection *));
6754 elf_tdata (abfd)->local_stubs = n;
6757 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6759 /* We don't need to set mips16_stubs_seen in this case.
6760 That flag is used to see whether we need to look through
6761 the global symbol table for stubs. We don't need to set
6762 it here, because we just have a local stub. */
6766 struct mips_elf_link_hash_entry *h;
6768 h = ((struct mips_elf_link_hash_entry *)
6769 sym_hashes[r_symndx - extsymoff]);
6771 /* H is the symbol this stub is for. */
6774 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6777 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6778 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6780 unsigned long r_symndx;
6781 struct mips_elf_link_hash_entry *h;
6784 /* Look at the relocation information to figure out which symbol
6787 r_symndx = ELF32_R_SYM (relocs->r_info);
6789 if (r_symndx < extsymoff
6790 || sym_hashes[r_symndx - extsymoff] == NULL)
6792 /* This stub was actually built for a static symbol defined
6793 in the same file. We assume that all static symbols in
6794 mips16 code are themselves mips16, so we can simply
6795 discard this stub. Since this function is called before
6796 the linker maps input sections to output sections, we can
6797 easily discard it by setting the SEC_EXCLUDE flag. */
6798 sec->flags |= SEC_EXCLUDE;
6802 h = ((struct mips_elf_link_hash_entry *)
6803 sym_hashes[r_symndx - extsymoff]);
6805 /* H is the symbol this stub is for. */
6807 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6808 loc = &h->call_fp_stub;
6810 loc = &h->call_stub;
6812 /* If we already have an appropriate stub for this function, we
6813 don't need another one, so we can discard this one. Since
6814 this function is called before the linker maps input sections
6815 to output sections, we can easily discard it by setting the
6816 SEC_EXCLUDE flag. We can also discard this section if we
6817 happen to already know that this is a mips16 function; it is
6818 not necessary to check this here, as it is checked later, but
6819 it is slightly faster to check now. */
6820 if (*loc != NULL || h->root.other == STO_MIPS16)
6822 sec->flags |= SEC_EXCLUDE;
6827 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6837 sgot = mips_elf_got_section (dynobj);
6842 BFD_ASSERT (elf_section_data (sgot) != NULL);
6843 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6844 BFD_ASSERT (g != NULL);
6849 bed = get_elf_backend_data (abfd);
6850 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6851 for (rel = relocs; rel < rel_end; ++rel)
6853 unsigned long r_symndx;
6855 struct elf_link_hash_entry *h;
6857 r_symndx = ELF32_R_SYM (rel->r_info);
6858 r_type = ELF32_R_TYPE (rel->r_info);
6860 if (r_symndx < extsymoff)
6864 h = sym_hashes[r_symndx - extsymoff];
6866 /* This may be an indirect symbol created because of a version. */
6869 while (h->root.type == bfd_link_hash_indirect)
6870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6874 /* Some relocs require a global offset table. */
6875 if (dynobj == NULL || sgot == NULL)
6881 case R_MIPS_CALL_HI16:
6882 case R_MIPS_CALL_LO16:
6883 case R_MIPS_GOT_HI16:
6884 case R_MIPS_GOT_LO16:
6886 elf_hash_table (info)->dynobj = dynobj = abfd;
6887 if (! mips_elf_create_got_section (dynobj, info))
6889 g = mips_elf_got_info (dynobj, &sgot);
6895 && (info->shared || h != NULL)
6896 && (sec->flags & SEC_ALLOC) != 0)
6897 elf_hash_table (info)->dynobj = dynobj = abfd;
6905 if (!h && (r_type == R_MIPS_CALL_LO16
6906 || r_type == R_MIPS_GOT_LO16
6907 || r_type == R_MIPS_GOT_DISP))
6909 /* We may need a local GOT entry for this relocation. We
6910 don't count R_MIPS_HI16 or R_MIPS_GOT16 relocations
6911 because they are always followed by a R_MIPS_LO16
6912 relocation for the value. We don't R_MIPS_GOT_PAGE
6913 because we can estimate the maximum number of pages
6914 needed by looking at the size of the segment.
6916 This estimation is very conservative since we can merge
6917 duplicate entries in the GOT. In order to be less
6918 conservative, we could actually build the GOT here,
6919 rather than in relocate_section. */
6921 sgot->_raw_size += 4;
6929 (*_bfd_error_handler)
6930 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6931 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6932 bfd_set_error (bfd_error_bad_value);
6937 case R_MIPS_CALL_HI16:
6938 case R_MIPS_CALL_LO16:
6939 /* This symbol requires a global offset table entry. */
6940 if (!mips_elf_record_global_got_symbol (h, info, g))
6943 /* We need a stub, not a plt entry for the undefined
6944 function. But we record it as if it needs plt. See
6945 elf_adjust_dynamic_symbol in elflink.h. */
6946 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6952 case R_MIPS_GOT_HI16:
6953 case R_MIPS_GOT_LO16:
6954 case R_MIPS_GOT_DISP:
6955 /* This symbol requires a global offset table entry. */
6956 if (h && !mips_elf_record_global_got_symbol (h, info, g))
6962 if ((info->shared || h != NULL)
6963 && (sec->flags & SEC_ALLOC) != 0)
6967 const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
6969 sreloc = bfd_get_section_by_name (dynobj, name);
6972 sreloc = bfd_make_section (dynobj, name);
6974 || ! bfd_set_section_flags (dynobj, sreloc,
6979 | SEC_LINKER_CREATED
6981 || ! bfd_set_section_alignment (dynobj, sreloc,
6987 /* When creating a shared object, we must copy these
6988 reloc types into the output file as R_MIPS_REL32
6989 relocs. We make room for this reloc in the
6990 .rel.dyn reloc section. */
6991 mips_elf_allocate_dynamic_relocations (dynobj, 1);
6994 struct mips_elf_link_hash_entry *hmips;
6996 /* We only need to copy this reloc if the symbol is
6997 defined in a dynamic object. */
6998 hmips = (struct mips_elf_link_hash_entry *) h;
6999 ++hmips->mips_32_relocs;
7002 /* Even though we don't directly need a GOT entry for
7003 this symbol, a symbol must have a dynamic symbol
7004 table index greater that DT_GOTSYM if there are
7005 dynamic relocations against it. */
7006 if (!mips_elf_record_global_got_symbol (h, info, g))
7010 if (SGI_COMPAT (dynobj))
7011 mips_elf_hash_table (info)->compact_rel_size +=
7012 sizeof (Elf32_External_crinfo);
7016 case R_MIPS_GPREL16:
7017 case R_MIPS_LITERAL:
7018 case R_MIPS_GPREL32:
7019 if (SGI_COMPAT (dynobj))
7020 mips_elf_hash_table (info)->compact_rel_size +=
7021 sizeof (Elf32_External_crinfo);
7024 /* This relocation describes the C++ object vtable hierarchy.
7025 Reconstruct it for later use during GC. */
7026 case R_MIPS_GNU_VTINHERIT:
7027 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7031 /* This relocation describes which C++ vtable entries are actually
7032 used. Record for later use during GC. */
7033 case R_MIPS_GNU_VTENTRY:
7034 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7042 /* If this reloc is not a 16 bit call, and it has a global
7043 symbol, then we will need the fn_stub if there is one.
7044 References from a stub section do not count. */
7046 && r_type != R_MIPS16_26
7047 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7048 sizeof FN_STUB - 1) != 0
7049 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7050 sizeof CALL_STUB - 1) != 0
7051 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7052 sizeof CALL_FP_STUB - 1) != 0)
7054 struct mips_elf_link_hash_entry *mh;
7056 mh = (struct mips_elf_link_hash_entry *) h;
7057 mh->need_fn_stub = true;
7064 /* Return the section that should be marked against GC for a given
7068 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7070 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7071 Elf_Internal_Rela *rel;
7072 struct elf_link_hash_entry *h;
7073 Elf_Internal_Sym *sym;
7075 /* ??? Do mips16 stub sections need to be handled special? */
7079 switch (ELF32_R_TYPE (rel->r_info))
7081 case R_MIPS_GNU_VTINHERIT:
7082 case R_MIPS_GNU_VTENTRY:
7086 switch (h->root.type)
7088 case bfd_link_hash_defined:
7089 case bfd_link_hash_defweak:
7090 return h->root.u.def.section;
7092 case bfd_link_hash_common:
7093 return h->root.u.c.p->section;
7102 if (!(elf_bad_symtab (abfd)
7103 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7104 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7105 && sym->st_shndx != SHN_COMMON))
7107 return bfd_section_from_elf_index (abfd, sym->st_shndx);
7114 /* Update the got entry reference counts for the section being removed. */
7117 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7118 bfd *abfd ATTRIBUTE_UNUSED;
7119 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7120 asection *sec ATTRIBUTE_UNUSED;
7121 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
7124 Elf_Internal_Shdr *symtab_hdr;
7125 struct elf_link_hash_entry **sym_hashes;
7126 bfd_signed_vma *local_got_refcounts;
7127 const Elf_Internal_Rela *rel, *relend;
7128 unsigned long r_symndx;
7129 struct elf_link_hash_entry *h;
7131 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7132 sym_hashes = elf_sym_hashes (abfd);
7133 local_got_refcounts = elf_local_got_refcounts (abfd);
7135 relend = relocs + sec->reloc_count;
7136 for (rel = relocs; rel < relend; rel++)
7137 switch (ELF32_R_TYPE (rel->r_info))
7141 case R_MIPS_CALL_HI16:
7142 case R_MIPS_CALL_LO16:
7143 case R_MIPS_GOT_HI16:
7144 case R_MIPS_GOT_LO16:
7145 /* ??? It would seem that the existing MIPS code does no sort
7146 of reference counting or whatnot on its GOT and PLT entries,
7147 so it is not possible to garbage collect them at this time. */
7159 /* Adjust a symbol defined by a dynamic object and referenced by a
7160 regular object. The current definition is in some section of the
7161 dynamic object, but we're not including those sections. We have to
7162 change the definition to something the rest of the link can
7166 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
7167 struct bfd_link_info *info;
7168 struct elf_link_hash_entry *h;
7171 struct mips_elf_link_hash_entry *hmips;
7174 dynobj = elf_hash_table (info)->dynobj;
7176 /* Make sure we know what is going on here. */
7177 BFD_ASSERT (dynobj != NULL
7178 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7179 || h->weakdef != NULL
7180 || ((h->elf_link_hash_flags
7181 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7182 && (h->elf_link_hash_flags
7183 & ELF_LINK_HASH_REF_REGULAR) != 0
7184 && (h->elf_link_hash_flags
7185 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7187 /* If this symbol is defined in a dynamic object, we need to copy
7188 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7190 hmips = (struct mips_elf_link_hash_entry *) h;
7191 if (! info->relocateable
7192 && hmips->mips_32_relocs != 0
7193 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7194 mips_elf_allocate_dynamic_relocations (dynobj, hmips->mips_32_relocs);
7196 /* For a function, create a stub, if needed. */
7197 if (h->type == STT_FUNC
7198 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7200 if (! elf_hash_table (info)->dynamic_sections_created)
7203 /* If this symbol is not defined in a regular file, then set
7204 the symbol to the stub location. This is required to make
7205 function pointers compare as equal between the normal
7206 executable and the shared library. */
7207 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7209 /* We need .stub section. */
7210 s = bfd_get_section_by_name (dynobj,
7211 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7212 BFD_ASSERT (s != NULL);
7214 h->root.u.def.section = s;
7215 h->root.u.def.value = s->_raw_size;
7217 /* XXX Write this stub address somewhere. */
7218 h->plt.offset = s->_raw_size;
7220 /* Make room for this stub code. */
7221 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7223 /* The last half word of the stub will be filled with the index
7224 of this symbol in .dynsym section. */
7229 /* If this is a weak symbol, and there is a real definition, the
7230 processor independent code will have arranged for us to see the
7231 real definition first, and we can just use the same value. */
7232 if (h->weakdef != NULL)
7234 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7235 || h->weakdef->root.type == bfd_link_hash_defweak);
7236 h->root.u.def.section = h->weakdef->root.u.def.section;
7237 h->root.u.def.value = h->weakdef->root.u.def.value;
7241 /* This is a reference to a symbol defined by a dynamic object which
7242 is not a function. */
7247 /* This function is called after all the input files have been read,
7248 and the input sections have been assigned to output sections. We
7249 check for any mips16 stub sections that we can discard. */
7251 static boolean mips_elf_check_mips16_stubs
7252 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7255 _bfd_mips_elf_always_size_sections (output_bfd, info)
7257 struct bfd_link_info *info;
7261 /* The .reginfo section has a fixed size. */
7262 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7264 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7266 if (info->relocateable
7267 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7270 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7271 mips_elf_check_mips16_stubs,
7277 /* Check the mips16 stubs for a particular symbol, and see if we can
7282 mips_elf_check_mips16_stubs (h, data)
7283 struct mips_elf_link_hash_entry *h;
7284 PTR data ATTRIBUTE_UNUSED;
7286 if (h->fn_stub != NULL
7287 && ! h->need_fn_stub)
7289 /* We don't need the fn_stub; the only references to this symbol
7290 are 16 bit calls. Clobber the size to 0 to prevent it from
7291 being included in the link. */
7292 h->fn_stub->_raw_size = 0;
7293 h->fn_stub->_cooked_size = 0;
7294 h->fn_stub->flags &= ~ SEC_RELOC;
7295 h->fn_stub->reloc_count = 0;
7296 h->fn_stub->flags |= SEC_EXCLUDE;
7299 if (h->call_stub != NULL
7300 && h->root.other == STO_MIPS16)
7302 /* We don't need the call_stub; this is a 16 bit function, so
7303 calls from other 16 bit functions are OK. Clobber the size
7304 to 0 to prevent it from being included in the link. */
7305 h->call_stub->_raw_size = 0;
7306 h->call_stub->_cooked_size = 0;
7307 h->call_stub->flags &= ~ SEC_RELOC;
7308 h->call_stub->reloc_count = 0;
7309 h->call_stub->flags |= SEC_EXCLUDE;
7312 if (h->call_fp_stub != NULL
7313 && h->root.other == STO_MIPS16)
7315 /* We don't need the call_stub; this is a 16 bit function, so
7316 calls from other 16 bit functions are OK. Clobber the size
7317 to 0 to prevent it from being included in the link. */
7318 h->call_fp_stub->_raw_size = 0;
7319 h->call_fp_stub->_cooked_size = 0;
7320 h->call_fp_stub->flags &= ~ SEC_RELOC;
7321 h->call_fp_stub->reloc_count = 0;
7322 h->call_fp_stub->flags |= SEC_EXCLUDE;
7328 /* Set the sizes of the dynamic sections. */
7331 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
7333 struct bfd_link_info *info;
7338 struct mips_got_info *g;
7340 dynobj = elf_hash_table (info)->dynobj;
7341 BFD_ASSERT (dynobj != NULL);
7343 if (elf_hash_table (info)->dynamic_sections_created)
7345 /* Set the contents of the .interp section to the interpreter. */
7348 s = bfd_get_section_by_name (dynobj, ".interp");
7349 BFD_ASSERT (s != NULL);
7351 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7353 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7357 /* The check_relocs and adjust_dynamic_symbol entry points have
7358 determined the sizes of the various dynamic sections. Allocate
7361 for (s = dynobj->sections; s != NULL; s = s->next)
7366 /* It's OK to base decisions on the section name, because none
7367 of the dynobj section names depend upon the input files. */
7368 name = bfd_get_section_name (dynobj, s);
7370 if ((s->flags & SEC_LINKER_CREATED) == 0)
7375 if (strncmp (name, ".rel", 4) == 0)
7377 if (s->_raw_size == 0)
7379 /* We only strip the section if the output section name
7380 has the same name. Otherwise, there might be several
7381 input sections for this output section. FIXME: This
7382 code is probably not needed these days anyhow, since
7383 the linker now does not create empty output sections. */
7384 if (s->output_section != NULL
7386 bfd_get_section_name (s->output_section->owner,
7387 s->output_section)) == 0)
7392 const char *outname;
7395 /* If this relocation section applies to a read only
7396 section, then we probably need a DT_TEXTREL entry.
7397 If the relocation section is .rel.dyn, we always
7398 assert a DT_TEXTREL entry rather than testing whether
7399 there exists a relocation to a read only section or
7401 outname = bfd_get_section_name (output_bfd,
7403 target = bfd_get_section_by_name (output_bfd, outname + 4);
7405 && (target->flags & SEC_READONLY) != 0
7406 && (target->flags & SEC_ALLOC) != 0)
7408 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
7411 /* We use the reloc_count field as a counter if we need
7412 to copy relocs into the output file. */
7414 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
7418 else if (strncmp (name, ".got", 4) == 0)
7421 bfd_size_type loadable_size = 0;
7422 bfd_size_type local_gotno;
7425 BFD_ASSERT (elf_section_data (s) != NULL);
7426 g = (struct mips_got_info *) elf_section_data (s)->tdata;
7427 BFD_ASSERT (g != NULL);
7429 /* Calculate the total loadable size of the output. That
7430 will give us the maximum number of GOT_PAGE entries
7432 for (sub = info->input_bfds; sub; sub = sub->link_next)
7434 asection *subsection;
7436 for (subsection = sub->sections;
7438 subsection = subsection->next)
7440 if ((subsection->flags & SEC_ALLOC) == 0)
7442 loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
7445 loadable_size += MIPS_FUNCTION_STUB_SIZE;
7447 /* Assume there are two loadable segments consisting of
7448 contiguous sections. Is 5 enough? */
7449 local_gotno = (loadable_size >> 16) + 5;
7450 g->local_gotno += local_gotno;
7451 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
7453 /* There has to be a global GOT entry for every symbol with
7454 a dynamic symbol table index of DT_MIPS_GOTSYM or
7455 higher. Therefore, it make sense to put those symbols
7456 that need GOT entries at the end of the symbol table. We
7458 if (!mips_elf_sort_hash_table (info))
7461 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7462 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
7464 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
7466 /* Irix rld assumes that the function stub isn't at the end
7467 of .text section. So put a dummy. XXX */
7468 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7470 else if (! info->shared
7471 && ! mips_elf_hash_table (info)->use_rld_obj_head
7472 && strncmp (name, ".rld_map", 8) == 0)
7474 /* We add a room for __rld_map. It will be filled in by the
7475 rtld to contain a pointer to the _r_debug structure. */
7478 else if (SGI_COMPAT (output_bfd)
7479 && strncmp (name, ".compact_rel", 12) == 0)
7480 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
7481 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
7483 s->_raw_size = (sizeof (Elf32_External_Msym)
7484 * (elf_hash_table (info)->dynsymcount
7485 + bfd_count_sections (output_bfd)));
7486 else if (strncmp (name, ".init", 5) != 0)
7488 /* It's not one of our sections, so don't allocate space. */
7494 _bfd_strip_section_from_output (s);
7498 /* Allocate memory for the section contents. */
7499 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
7500 if (s->contents == NULL && s->_raw_size != 0)
7502 bfd_set_error (bfd_error_no_memory);
7507 if (elf_hash_table (info)->dynamic_sections_created)
7509 /* Add some entries to the .dynamic section. We fill in the
7510 values later, in elf_mips_finish_dynamic_sections, but we
7511 must add the entries now so that we get the correct size for
7512 the .dynamic section. The DT_DEBUG entry is filled in by the
7513 dynamic linker and used by the debugger. */
7516 if (SGI_COMPAT (output_bfd))
7518 /* SGI object has the equivalence of DT_DEBUG in the
7519 DT_MIPS_RLD_MAP entry. */
7520 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
7524 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
7530 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
7534 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
7537 if (bfd_get_section_by_name (dynobj,
7538 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
7540 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
7543 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
7546 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
7550 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
7553 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
7556 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
7558 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
7561 s = bfd_get_section_by_name (dynobj, ".liblist");
7562 BFD_ASSERT (s != NULL);
7564 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
7568 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
7571 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
7575 /* Time stamps in executable files are a bad idea. */
7576 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
7581 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
7586 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
7590 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
7593 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
7596 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
7599 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
7602 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
7605 if (IRIX_COMPAT (dynobj) == ict_irix5
7606 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
7609 if (IRIX_COMPAT (dynobj) == ict_irix6
7610 && (bfd_get_section_by_name
7611 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
7612 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
7615 if (bfd_get_section_by_name (dynobj,
7616 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
7617 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
7621 /* If we use dynamic linking, we generate a section symbol for each
7622 output section. These are local symbols, which means that they
7623 must come first in the dynamic symbol table.
7624 That means we must increment the dynamic symbol index of every
7625 other dynamic symbol. */
7630 if (elf_hash_table (info)->dynamic_sections_created)
7632 c = bfd_count_sections (output_bfd);
7633 elf_link_hash_traverse (elf_hash_table (info),
7634 _bfd_elf_link_adjust_dynindx,
7636 elf_hash_table (info)->dynsymcount += c;
7638 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7640 elf_section_data (s)->dynindx = i;
7641 /* These symbols will have no names, so we don't need to
7642 fiddle with dynstr_index. */
7650 /* If NAME is one of the special IRIX6 symbols defined by the linker,
7651 adjust it appropriately now. */
7654 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
7655 bfd *abfd ATTRIBUTE_UNUSED;
7657 Elf_Internal_Sym *sym;
7659 /* The linker script takes care of providing names and values for
7660 these, but we must place them into the right sections. */
7661 static const char* const text_section_symbols[] = {
7664 "__dso_displacement",
7666 "__program_header_table",
7670 static const char* const data_section_symbols[] = {
7678 const char* const *p;
7681 for (i = 0; i < 2; ++i)
7682 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7685 if (strcmp (*p, name) == 0)
7687 /* All of these symbols are given type STT_SECTION by the
7689 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7691 /* The IRIX linker puts these symbols in special sections. */
7693 sym->st_shndx = SHN_MIPS_TEXT;
7695 sym->st_shndx = SHN_MIPS_DATA;
7701 /* Finish up dynamic symbol handling. We set the contents of various
7702 dynamic sections here. */
7705 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7707 struct bfd_link_info *info;
7708 struct elf_link_hash_entry *h;
7709 Elf_Internal_Sym *sym;
7715 struct mips_got_info *g;
7717 struct mips_elf_link_hash_entry *mh;
7719 dynobj = elf_hash_table (info)->dynobj;
7720 gval = sym->st_value;
7721 mh = (struct mips_elf_link_hash_entry *) h;
7723 if (h->plt.offset != (bfd_vma) -1)
7727 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7729 /* This symbol has a stub. Set it up. */
7731 BFD_ASSERT (h->dynindx != -1);
7733 s = bfd_get_section_by_name (dynobj,
7734 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7735 BFD_ASSERT (s != NULL);
7737 /* Fill the stub. */
7739 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7741 bfd_put_32 (output_bfd, STUB_MOVE, p);
7744 /* FIXME: Can h->dynindex be more than 64K? */
7745 if (h->dynindx & 0xffff0000)
7748 bfd_put_32 (output_bfd, STUB_JALR, p);
7750 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7752 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7753 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7755 /* Mark the symbol as undefined. plt.offset != -1 occurs
7756 only for the referenced symbol. */
7757 sym->st_shndx = SHN_UNDEF;
7759 /* The run-time linker uses the st_value field of the symbol
7760 to reset the global offset table entry for this external
7761 to its stub address when unlinking a shared object. */
7762 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7763 sym->st_value = gval;
7766 BFD_ASSERT (h->dynindx != -1);
7768 sgot = mips_elf_got_section (dynobj);
7769 BFD_ASSERT (sgot != NULL);
7770 BFD_ASSERT (elf_section_data (sgot) != NULL);
7771 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7772 BFD_ASSERT (g != NULL);
7774 /* Run through the global symbol table, creating GOT entries for all
7775 the symbols that need them. */
7776 if (h->dynindx >= g->global_gotsym->dynindx)
7782 value = sym->st_value;
7784 /* For an entity defined in a shared object, this will be
7785 NULL. (For functions in shared objects for
7786 which we have created stubs, ST_VALUE will be non-NULL.
7787 That's because such the functions are now no longer defined
7788 in a shared object.) */
7789 value = h->root.u.def.value;
7791 offset = mips_elf_global_got_index (dynobj, h);
7792 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
7795 /* Create a .msym entry, if appropriate. */
7796 smsym = bfd_get_section_by_name (dynobj,
7797 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
7800 Elf32_Internal_Msym msym;
7802 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
7803 /* It is undocumented what the `1' indicates, but IRIX6 uses
7805 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
7806 bfd_mips_elf_swap_msym_out
7808 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
7811 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7812 name = h->root.root.string;
7813 if (strcmp (name, "_DYNAMIC") == 0
7814 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7815 sym->st_shndx = SHN_ABS;
7816 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7818 sym->st_shndx = SHN_ABS;
7819 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7822 else if (SGI_COMPAT (output_bfd))
7824 if (strcmp (name, "_gp_disp") == 0)
7826 sym->st_shndx = SHN_ABS;
7827 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7828 sym->st_value = elf_gp (output_bfd);
7830 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7831 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7833 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7834 sym->st_other = STO_PROTECTED;
7836 sym->st_shndx = SHN_MIPS_DATA;
7838 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7840 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7841 sym->st_other = STO_PROTECTED;
7842 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7843 sym->st_shndx = SHN_ABS;
7845 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7847 if (h->type == STT_FUNC)
7848 sym->st_shndx = SHN_MIPS_TEXT;
7849 else if (h->type == STT_OBJECT)
7850 sym->st_shndx = SHN_MIPS_DATA;
7854 /* Handle the IRIX6-specific symbols. */
7855 if (IRIX_COMPAT (output_bfd) == ict_irix6)
7856 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
7858 if (SGI_COMPAT (output_bfd)
7861 if (! mips_elf_hash_table (info)->use_rld_obj_head
7862 && strcmp (name, "__rld_map") == 0)
7864 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7865 BFD_ASSERT (s != NULL);
7866 sym->st_value = s->output_section->vma + s->output_offset;
7867 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7868 if (mips_elf_hash_table (info)->rld_value == 0)
7869 mips_elf_hash_table (info)->rld_value = sym->st_value;
7871 else if (mips_elf_hash_table (info)->use_rld_obj_head
7872 && strcmp (name, "__rld_obj_head") == 0)
7874 /* IRIX6 does not use a .rld_map section. */
7875 if (IRIX_COMPAT (output_bfd) == ict_irix5)
7876 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
7878 mips_elf_hash_table (info)->rld_value = sym->st_value;
7882 /* If this is a mips16 symbol, force the value to be even. */
7883 if (sym->st_other == STO_MIPS16
7884 && (sym->st_value & 1) != 0)
7890 /* Finish up the dynamic sections. */
7893 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
7895 struct bfd_link_info *info;
7900 struct mips_got_info *g;
7902 dynobj = elf_hash_table (info)->dynobj;
7904 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7906 sgot = mips_elf_got_section (dynobj);
7911 BFD_ASSERT (elf_section_data (sgot) != NULL);
7912 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7913 BFD_ASSERT (g != NULL);
7916 if (elf_hash_table (info)->dynamic_sections_created)
7920 BFD_ASSERT (sdyn != NULL);
7921 BFD_ASSERT (g != NULL);
7923 for (b = sdyn->contents;
7924 b < sdyn->contents + sdyn->_raw_size;
7925 b += MIPS_ELF_DYN_SIZE (dynobj))
7927 Elf_Internal_Dyn dyn;
7933 /* Read in the current dynamic entry. */
7934 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
7936 /* Assume that we're going to modify it and write it out. */
7942 s = (bfd_get_section_by_name
7944 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
7945 BFD_ASSERT (s != NULL);
7946 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
7950 /* Rewrite DT_STRSZ. */
7952 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7958 case DT_MIPS_CONFLICT:
7961 case DT_MIPS_LIBLIST:
7964 s = bfd_get_section_by_name (output_bfd, name);
7965 BFD_ASSERT (s != NULL);
7966 dyn.d_un.d_ptr = s->vma;
7969 case DT_MIPS_RLD_VERSION:
7970 dyn.d_un.d_val = 1; /* XXX */
7974 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7977 case DT_MIPS_CONFLICTNO:
7979 elemsize = sizeof (Elf32_Conflict);
7982 case DT_MIPS_LIBLISTNO:
7984 elemsize = sizeof (Elf32_Lib);
7986 s = bfd_get_section_by_name (output_bfd, name);
7989 if (s->_cooked_size != 0)
7990 dyn.d_un.d_val = s->_cooked_size / elemsize;
7992 dyn.d_un.d_val = s->_raw_size / elemsize;
7998 case DT_MIPS_TIME_STAMP:
7999 time ((time_t *) &dyn.d_un.d_val);
8002 case DT_MIPS_ICHECKSUM:
8007 case DT_MIPS_IVERSION:
8012 case DT_MIPS_BASE_ADDRESS:
8013 s = output_bfd->sections;
8014 BFD_ASSERT (s != NULL);
8015 dyn.d_un.d_ptr = s->vma & ~(0xffff);
8018 case DT_MIPS_LOCAL_GOTNO:
8019 dyn.d_un.d_val = g->local_gotno;
8022 case DT_MIPS_SYMTABNO:
8024 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8025 s = bfd_get_section_by_name (output_bfd, name);
8026 BFD_ASSERT (s != NULL);
8028 if (s->_cooked_size != 0)
8029 dyn.d_un.d_val = s->_cooked_size / elemsize;
8031 dyn.d_un.d_val = s->_raw_size / elemsize;
8034 case DT_MIPS_UNREFEXTNO:
8035 /* The index into the dynamic symbol table which is the
8036 entry of the first external symbol that is not
8037 referenced within the same object. */
8038 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8041 case DT_MIPS_GOTSYM:
8042 dyn.d_un.d_val = g->global_gotsym->dynindx;
8045 case DT_MIPS_HIPAGENO:
8046 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8049 case DT_MIPS_RLD_MAP:
8050 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8053 case DT_MIPS_OPTIONS:
8054 s = (bfd_get_section_by_name
8055 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8056 dyn.d_un.d_ptr = s->vma;
8060 s = (bfd_get_section_by_name
8061 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8062 dyn.d_un.d_ptr = s->vma;
8071 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8076 /* The first entry of the global offset table will be filled at
8077 runtime. The second entry will be used by some runtime loaders.
8078 This isn't the case of Irix rld. */
8079 if (sgot != NULL && sgot->_raw_size > 0)
8081 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8082 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8083 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8087 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8088 = MIPS_ELF_GOT_SIZE (output_bfd);
8094 Elf_Internal_Sym sym;
8095 Elf32_compact_rel cpt;
8097 /* Set up the section symbols for the output sections. SGI sets
8098 the STT_NOTYPE attribute for these symbols. Should we do so? */
8100 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
8101 smsym = bfd_get_section_by_name (dynobj,
8102 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8103 if (sdynsym != NULL)
8105 Elf32_Internal_Msym msym;
8109 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8112 msym.ms_hash_value = 0;
8113 msym.ms_info = ELF32_MS_INFO (0, 1);
8115 for (s = output_bfd->sections; s != NULL; s = s->next)
8120 sym.st_value = s->vma;
8122 indx = elf_section_data (s)->this_idx;
8123 BFD_ASSERT (indx > 0);
8124 sym.st_shndx = indx;
8126 dynindx = elf_section_data (s)->dynindx;
8128 (*get_elf_backend_data (output_bfd)->s->swap_symbol_out)
8131 + (dynindx * MIPS_ELF_SYM_SIZE (output_bfd)));
8134 bfd_mips_elf_swap_msym_out
8136 (((Elf32_External_Msym *) smsym->contents)
8140 /* Set the sh_info field of the output .dynsym section to
8141 the index of the first global symbol. */
8142 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
8143 bfd_count_sections (output_bfd) + 1;
8146 if (SGI_COMPAT (output_bfd))
8148 /* Write .compact_rel section out. */
8149 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8153 cpt.num = s->reloc_count;
8155 cpt.offset = (s->output_section->filepos
8156 + sizeof (Elf32_External_compact_rel));
8159 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8160 ((Elf32_External_compact_rel *)
8163 /* Clean up a dummy stub function entry in .text. */
8164 s = bfd_get_section_by_name (dynobj,
8165 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8168 file_ptr dummy_offset;
8170 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8171 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8172 memset (s->contents + dummy_offset, 0,
8173 MIPS_FUNCTION_STUB_SIZE);
8178 /* Clean up a first relocation in .rel.dyn. */
8179 s = bfd_get_section_by_name (dynobj,
8180 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
8181 if (s != NULL && s->_raw_size > 0)
8182 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
8188 /* This is almost identical to bfd_generic_get_... except that some
8189 MIPS relocations need to be handled specially. Sigh. */
8192 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8193 relocateable, symbols)
8195 struct bfd_link_info *link_info;
8196 struct bfd_link_order *link_order;
8198 boolean relocateable;
8201 /* Get enough memory to hold the stuff */
8202 bfd *input_bfd = link_order->u.indirect.section->owner;
8203 asection *input_section = link_order->u.indirect.section;
8205 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8206 arelent **reloc_vector = NULL;
8212 reloc_vector = (arelent **) bfd_malloc (reloc_size);
8213 if (reloc_vector == NULL && reloc_size != 0)
8216 /* read in the section */
8217 if (!bfd_get_section_contents (input_bfd,
8221 input_section->_raw_size))
8224 /* We're not relaxing the section, so just copy the size info */
8225 input_section->_cooked_size = input_section->_raw_size;
8226 input_section->reloc_done = true;
8228 reloc_count = bfd_canonicalize_reloc (input_bfd,
8232 if (reloc_count < 0)
8235 if (reloc_count > 0)
8240 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8243 struct bfd_hash_entry *h;
8244 struct bfd_link_hash_entry *lh;
8245 /* Skip all this stuff if we aren't mixing formats. */
8246 if (abfd && input_bfd
8247 && abfd->xvec == input_bfd->xvec)
8251 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8252 lh = (struct bfd_link_hash_entry *) h;
8259 case bfd_link_hash_undefined:
8260 case bfd_link_hash_undefweak:
8261 case bfd_link_hash_common:
8264 case bfd_link_hash_defined:
8265 case bfd_link_hash_defweak:
8267 gp = lh->u.def.value;
8269 case bfd_link_hash_indirect:
8270 case bfd_link_hash_warning:
8272 /* @@FIXME ignoring warning for now */
8274 case bfd_link_hash_new:
8283 for (parent = reloc_vector; *parent != (arelent *) NULL;
8286 char *error_message = (char *) NULL;
8287 bfd_reloc_status_type r;
8289 /* Specific to MIPS: Deal with relocation types that require
8290 knowing the gp of the output bfd. */
8291 asymbol *sym = *(*parent)->sym_ptr_ptr;
8292 if (bfd_is_abs_section (sym->section) && abfd)
8294 /* The special_function wouldn't get called anyways. */
8298 /* The gp isn't there; let the special function code
8299 fall over on its own. */
8301 else if ((*parent)->howto->special_function
8302 == _bfd_mips_elf_gprel16_reloc)
8304 /* bypass special_function call */
8305 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8306 relocateable, (PTR) data, gp);
8307 goto skip_bfd_perform_relocation;
8309 /* end mips specific stuff */
8311 r = bfd_perform_relocation (input_bfd,
8315 relocateable ? abfd : (bfd *) NULL,
8317 skip_bfd_perform_relocation:
8321 asection *os = input_section->output_section;
8323 /* A partial link, so keep the relocs */
8324 os->orelocation[os->reloc_count] = *parent;
8328 if (r != bfd_reloc_ok)
8332 case bfd_reloc_undefined:
8333 if (!((*link_info->callbacks->undefined_symbol)
8334 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8335 input_bfd, input_section, (*parent)->address)))
8338 case bfd_reloc_dangerous:
8339 BFD_ASSERT (error_message != (char *) NULL);
8340 if (!((*link_info->callbacks->reloc_dangerous)
8341 (link_info, error_message, input_bfd, input_section,
8342 (*parent)->address)))
8345 case bfd_reloc_overflow:
8346 if (!((*link_info->callbacks->reloc_overflow)
8347 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8348 (*parent)->howto->name, (*parent)->addend,
8349 input_bfd, input_section, (*parent)->address)))
8352 case bfd_reloc_outofrange:
8361 if (reloc_vector != NULL)
8362 free (reloc_vector);
8366 if (reloc_vector != NULL)
8367 free (reloc_vector);
8370 #define bfd_elf32_bfd_get_relocated_section_contents \
8371 elf32_mips_get_relocated_section_contents
8373 /* ECOFF swapping routines. These are used when dealing with the
8374 .mdebug section, which is in the ECOFF debugging format. */
8375 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8377 /* Symbol table magic number. */
8379 /* Alignment of debugging information. E.g., 4. */
8381 /* Sizes of external symbolic information. */
8382 sizeof (struct hdr_ext),
8383 sizeof (struct dnr_ext),
8384 sizeof (struct pdr_ext),
8385 sizeof (struct sym_ext),
8386 sizeof (struct opt_ext),
8387 sizeof (struct fdr_ext),
8388 sizeof (struct rfd_ext),
8389 sizeof (struct ext_ext),
8390 /* Functions to swap in external symbolic data. */
8399 _bfd_ecoff_swap_tir_in,
8400 _bfd_ecoff_swap_rndx_in,
8401 /* Functions to swap out external symbolic data. */
8410 _bfd_ecoff_swap_tir_out,
8411 _bfd_ecoff_swap_rndx_out,
8412 /* Function to read in symbolic data. */
8413 _bfd_mips_elf_read_ecoff_info
8416 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8417 #define TARGET_LITTLE_NAME "elf32-littlemips"
8418 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8419 #define TARGET_BIG_NAME "elf32-bigmips"
8420 #define ELF_ARCH bfd_arch_mips
8421 #define ELF_MACHINE_CODE EM_MIPS
8423 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8424 a value of 0x1000, and we are compatible. */
8425 #define ELF_MAXPAGESIZE 0x1000
8427 #define elf_backend_collect true
8428 #define elf_backend_type_change_ok true
8429 #define elf_backend_can_gc_sections true
8430 #define elf_info_to_howto mips_info_to_howto_rela
8431 #define elf_info_to_howto_rel mips_info_to_howto_rel
8432 #define elf_backend_sym_is_global mips_elf_sym_is_global
8433 #define elf_backend_object_p _bfd_mips_elf_object_p
8434 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
8435 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8436 #define elf_backend_section_from_bfd_section \
8437 _bfd_mips_elf_section_from_bfd_section
8438 #define elf_backend_section_processing _bfd_mips_elf_section_processing
8439 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8440 #define elf_backend_additional_program_headers \
8441 _bfd_mips_elf_additional_program_headers
8442 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
8443 #define elf_backend_final_write_processing \
8444 _bfd_mips_elf_final_write_processing
8445 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8446 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
8447 #define elf_backend_create_dynamic_sections \
8448 _bfd_mips_elf_create_dynamic_sections
8449 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
8450 #define elf_backend_adjust_dynamic_symbol \
8451 _bfd_mips_elf_adjust_dynamic_symbol
8452 #define elf_backend_always_size_sections \
8453 _bfd_mips_elf_always_size_sections
8454 #define elf_backend_size_dynamic_sections \
8455 _bfd_mips_elf_size_dynamic_sections
8456 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
8457 #define elf_backend_link_output_symbol_hook \
8458 _bfd_mips_elf_link_output_symbol_hook
8459 #define elf_backend_finish_dynamic_symbol \
8460 _bfd_mips_elf_finish_dynamic_symbol
8461 #define elf_backend_finish_dynamic_sections \
8462 _bfd_mips_elf_finish_dynamic_sections
8463 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
8464 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8466 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8467 #define elf_backend_plt_header_size 0
8469 #define bfd_elf32_bfd_is_local_label_name \
8470 mips_elf_is_local_label_name
8471 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8472 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8473 #define bfd_elf32_bfd_link_hash_table_create \
8474 _bfd_mips_elf_link_hash_table_create
8475 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
8476 #define bfd_elf32_bfd_copy_private_bfd_data \
8477 _bfd_mips_elf_copy_private_bfd_data
8478 #define bfd_elf32_bfd_merge_private_bfd_data \
8479 _bfd_mips_elf_merge_private_bfd_data
8480 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
8481 #define bfd_elf32_bfd_print_private_bfd_data \
8482 _bfd_mips_elf_print_private_bfd_data
8483 #include "elf32-target.h"