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.
7 <mark@codesourcery.com>
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 global .got entries. */
55 unsigned int global_gotno;
56 /* The number of local .got entries. */
57 unsigned int local_gotno;
58 /* The number of local .got entries we have used. */
59 unsigned int assigned_gotno;
62 /* The MIPS ELF linker needs additional information for each symbol in
63 the global hash table. */
65 struct mips_elf_link_hash_entry
67 struct elf_link_hash_entry root;
69 /* External symbol information. */
72 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
74 unsigned int possibly_dynamic_relocs;
76 /* The index of the first dynamic relocation (in the .rel.dyn
77 section) against this symbol. */
78 unsigned int min_dyn_reloc_index;
80 /* If there is a stub that 32 bit functions should use to call this
81 16 bit function, this points to the section containing the stub. */
84 /* Whether we need the fn_stub; this is set if this symbol appears
85 in any relocs other than a 16 bit call. */
88 /* If there is a stub that 16 bit functions should use to call this
89 32 bit function, this points to the section containing the stub. */
92 /* This is like the call_stub field, but it is used if the function
93 being called returns a floating point value. */
94 asection *call_fp_stub;
97 static bfd_reloc_status_type mips32_64bit_reloc
98 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
99 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
100 PARAMS ((bfd *, bfd_reloc_code_real_type));
101 static reloc_howto_type *mips_rtype_to_howto
102 PARAMS ((unsigned int));
103 static void mips_info_to_howto_rel
104 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
105 static void mips_info_to_howto_rela
106 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
107 static void bfd_mips_elf32_swap_gptab_in
108 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
109 static void bfd_mips_elf32_swap_gptab_out
110 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
111 static void bfd_mips_elf_swap_msym_in
112 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
113 static void bfd_mips_elf_swap_msym_out
114 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
115 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
116 static boolean mips_elf_create_procedure_table
117 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
118 struct ecoff_debug_info *));
119 static INLINE int elf_mips_isa PARAMS ((flagword));
120 static INLINE int elf_mips_mach PARAMS ((flagword));
121 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
122 static boolean mips_elf_is_local_label_name
123 PARAMS ((bfd *, const char *));
124 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
125 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
126 static int gptab_compare PARAMS ((const void *, const void *));
127 static void mips_elf_relocate_hi16
128 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
130 static boolean mips_elf_relocate_got_local
131 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
132 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
133 static void mips_elf_relocate_global_got
134 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
135 static bfd_reloc_status_type mips16_jump_reloc
136 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
137 static bfd_reloc_status_type mips16_gprel_reloc
138 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
139 static boolean mips_elf_create_compact_rel_section
140 PARAMS ((bfd *, struct bfd_link_info *));
141 static boolean mips_elf_create_got_section
142 PARAMS ((bfd *, struct bfd_link_info *));
143 static bfd_reloc_status_type mips_elf_final_gp
144 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
145 static bfd_byte *elf32_mips_get_relocated_section_contents
146 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
147 bfd_byte *, boolean, asymbol **));
148 static asection *mips_elf_create_msym_section
150 static void mips_elf_irix6_finish_dynamic_symbol
151 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
152 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
153 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
154 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
155 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
156 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
157 static bfd_vma mips_elf_global_got_index
158 PARAMS ((bfd *, struct elf_link_hash_entry *));
159 static bfd_vma mips_elf_local_got_index
160 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
161 static bfd_vma mips_elf_got_offset_from_index
162 PARAMS ((bfd *, bfd *, bfd_vma));
163 static boolean mips_elf_record_global_got_symbol
164 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
165 struct mips_got_info *));
166 static bfd_vma mips_elf_got_page
167 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
168 static const Elf_Internal_Rela *mips_elf_next_lo16_relocation
169 PARAMS ((const Elf_Internal_Rela *, const Elf_Internal_Rela *));
170 static bfd_reloc_status_type mips_elf_calculate_relocation
171 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
172 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
173 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
175 static bfd_vma mips_elf_obtain_contents
176 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
177 static boolean mips_elf_perform_relocation
178 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
179 const Elf_Internal_Rela *, bfd_vma,
180 bfd *, asection *, bfd_byte *, boolean));
181 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
182 static boolean mips_elf_sort_hash_table_f
183 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
184 static boolean mips_elf_sort_hash_table
185 PARAMS ((struct bfd_link_info *, unsigned long));
186 static asection * mips_elf_got_section PARAMS ((bfd *));
187 static struct mips_got_info *mips_elf_got_info
188 PARAMS ((bfd *, asection **));
189 static boolean mips_elf_local_relocation_p
190 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **));
191 static bfd_vma mips_elf_create_local_got_entry
192 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
193 static bfd_vma mips_elf_got16_entry
194 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
195 static boolean mips_elf_create_dynamic_relocation
196 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
197 struct mips_elf_link_hash_entry *, asection *,
198 bfd_vma, bfd_vma *, asection *));
199 static void mips_elf_allocate_dynamic_relocations
200 PARAMS ((bfd *, unsigned int));
201 static boolean mips_elf_stub_section_p
202 PARAMS ((bfd *, asection *));
204 /* The level of IRIX compatibility we're striving for. */
212 /* Nonzero if ABFD is using the N32 ABI. */
214 #define ABI_N32_P(abfd) \
215 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
217 /* Nonzero if ABFD is using the 64-bit ABI. FIXME: This is never
219 #define ABI_64_P(abfd) \
220 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
222 /* What version of Irix we are trying to be compatible with. FIXME:
223 At the moment, we never generate "normal" MIPS ELF ABI executables;
224 we always use some version of Irix. */
226 #define IRIX_COMPAT(abfd) \
227 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5)
229 /* Whether we are trying to be compatible with IRIX at all. */
231 #define SGI_COMPAT(abfd) \
232 (IRIX_COMPAT (abfd) != ict_none)
234 /* The name of the msym section. */
235 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
237 /* The name of the srdata section. */
238 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
240 /* The name of the options section. */
241 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
242 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
244 /* The name of the stub section. */
245 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
246 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
248 /* The name of the dynamic relocation section. */
249 #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
251 /* The size of an external REL relocation. */
252 #define MIPS_ELF_REL_SIZE(abfd) \
253 (get_elf_backend_data (abfd)->s->sizeof_rel)
255 /* The size of an external dynamic table entry. */
256 #define MIPS_ELF_DYN_SIZE(abfd) \
257 (get_elf_backend_data (abfd)->s->sizeof_dyn)
259 /* The size of a GOT entry. */
260 #define MIPS_ELF_GOT_SIZE(abfd) \
261 (get_elf_backend_data (abfd)->s->arch_size / 8)
263 /* The size of a symbol-table entry. */
264 #define MIPS_ELF_SYM_SIZE(abfd) \
265 (get_elf_backend_data (abfd)->s->sizeof_sym)
267 /* The default alignment for sections, as a power of two. */
268 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
269 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
271 /* Get word-sized data. */
272 #define MIPS_ELF_GET_WORD(abfd, ptr) \
273 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
275 /* Put out word-sized data. */
276 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
278 ? bfd_put_64 (abfd, val, ptr) \
279 : bfd_put_32 (abfd, val, ptr))
281 /* Add a dynamic symbol table-entry. */
283 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
284 (ABI_64_P (elf_hash_table (info)->dynobj) \
285 ? bfd_elf64_add_dynamic_entry (info, tag, val) \
286 : bfd_elf32_add_dynamic_entry (info, tag, val))
288 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
289 (ABI_64_P (elf_hash_table (info)->dynobj) \
290 ? (abort (), false) \
291 : bfd_elf32_add_dynamic_entry (info, tag, val))
294 /* The number of local .got entries we reserve. */
295 #define MIPS_RESERVED_GOTNO (2)
297 /* Instructions which appear in a stub. For some reason the stub is
298 slightly different on an SGI system. */
299 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
300 #define STUB_LW(abfd) \
303 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
304 : 0x8f998010) /* lw t9,0x8010(gp) */ \
305 : 0x8f998000) /* lw t9,0x8000(gp) */
306 #define STUB_MOVE 0x03e07825 /* move t7,ra */
307 #define STUB_JALR 0x0320f809 /* jal t9 */
308 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
309 #define MIPS_FUNCTION_STUB_SIZE (16)
312 /* We no longer try to identify particular sections for the .dynsym
313 section. When we do, we wind up crashing if there are other random
314 sections with relocations. */
316 /* Names of sections which appear in the .dynsym section in an Irix 5
319 static const char * const mips_elf_dynsym_sec_names[] =
332 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
333 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
335 /* The number of entries in mips_elf_dynsym_sec_names which go in the
338 #define MIPS_TEXT_DYNSYM_SECNO (3)
342 /* The names of the runtime procedure table symbols used on Irix 5. */
344 static const char * const mips_elf_dynsym_rtproc_names[] =
347 "_procedure_string_table",
348 "_procedure_table_size",
352 /* These structures are used to generate the .compact_rel section on
357 unsigned long id1; /* Always one? */
358 unsigned long num; /* Number of compact relocation entries. */
359 unsigned long id2; /* Always two? */
360 unsigned long offset; /* The file offset of the first relocation. */
361 unsigned long reserved0; /* Zero? */
362 unsigned long reserved1; /* Zero? */
371 bfd_byte reserved0[4];
372 bfd_byte reserved1[4];
373 } Elf32_External_compact_rel;
377 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
378 unsigned int rtype : 4; /* Relocation types. See below. */
379 unsigned int dist2to : 8;
380 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
381 unsigned long konst; /* KONST field. See below. */
382 unsigned long vaddr; /* VADDR to be relocated. */
387 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
388 unsigned int rtype : 4; /* Relocation types. See below. */
389 unsigned int dist2to : 8;
390 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
391 unsigned long konst; /* KONST field. See below. */
399 } Elf32_External_crinfo;
405 } Elf32_External_crinfo2;
407 /* These are the constants used to swap the bitfields in a crinfo. */
409 #define CRINFO_CTYPE (0x1)
410 #define CRINFO_CTYPE_SH (31)
411 #define CRINFO_RTYPE (0xf)
412 #define CRINFO_RTYPE_SH (27)
413 #define CRINFO_DIST2TO (0xff)
414 #define CRINFO_DIST2TO_SH (19)
415 #define CRINFO_RELVADDR (0x7ffff)
416 #define CRINFO_RELVADDR_SH (0)
418 /* A compact relocation info has long (3 words) or short (2 words)
419 formats. A short format doesn't have VADDR field and relvaddr
420 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
421 #define CRF_MIPS_LONG 1
422 #define CRF_MIPS_SHORT 0
424 /* There are 4 types of compact relocation at least. The value KONST
425 has different meaning for each type:
428 CT_MIPS_REL32 Address in data
429 CT_MIPS_WORD Address in word (XXX)
430 CT_MIPS_GPHI_LO GP - vaddr
431 CT_MIPS_JMPAD Address to jump
434 #define CRT_MIPS_REL32 0xa
435 #define CRT_MIPS_WORD 0xb
436 #define CRT_MIPS_GPHI_LO 0xc
437 #define CRT_MIPS_JMPAD 0xd
439 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
440 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
441 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
442 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
444 static void bfd_elf32_swap_compact_rel_out
445 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
446 static void bfd_elf32_swap_crinfo_out
447 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
449 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
451 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
452 from smaller values. Start with zero, widen, *then* decrement. */
453 #define MINUS_ONE (((bfd_vma)0) - 1)
455 static reloc_howto_type elf_mips_howto_table[] =
458 HOWTO (R_MIPS_NONE, /* type */
460 0, /* size (0 = byte, 1 = short, 2 = long) */
462 false, /* pc_relative */
464 complain_overflow_dont, /* complain_on_overflow */
465 bfd_elf_generic_reloc, /* special_function */
466 "R_MIPS_NONE", /* name */
467 false, /* partial_inplace */
470 false), /* pcrel_offset */
472 /* 16 bit relocation. */
473 HOWTO (R_MIPS_16, /* type */
475 1, /* size (0 = byte, 1 = short, 2 = long) */
477 false, /* pc_relative */
479 complain_overflow_bitfield, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* special_function */
481 "R_MIPS_16", /* name */
482 true, /* partial_inplace */
483 0xffff, /* src_mask */
484 0xffff, /* dst_mask */
485 false), /* pcrel_offset */
487 /* 32 bit relocation. */
488 HOWTO (R_MIPS_32, /* type */
490 2, /* size (0 = byte, 1 = short, 2 = long) */
492 false, /* pc_relative */
494 complain_overflow_bitfield, /* complain_on_overflow */
495 bfd_elf_generic_reloc, /* special_function */
496 "R_MIPS_32", /* name */
497 true, /* partial_inplace */
498 0xffffffff, /* src_mask */
499 0xffffffff, /* dst_mask */
500 false), /* pcrel_offset */
502 /* 32 bit symbol relative relocation. */
503 HOWTO (R_MIPS_REL32, /* type */
505 2, /* size (0 = byte, 1 = short, 2 = long) */
507 false, /* pc_relative */
509 complain_overflow_bitfield, /* complain_on_overflow */
510 bfd_elf_generic_reloc, /* special_function */
511 "R_MIPS_REL32", /* name */
512 true, /* partial_inplace */
513 0xffffffff, /* src_mask */
514 0xffffffff, /* dst_mask */
515 false), /* pcrel_offset */
517 /* 26 bit branch address. */
518 HOWTO (R_MIPS_26, /* type */
520 2, /* size (0 = byte, 1 = short, 2 = long) */
522 false, /* pc_relative */
524 complain_overflow_dont, /* complain_on_overflow */
525 /* This needs complex overflow
526 detection, because the upper four
527 bits must match the PC. */
528 bfd_elf_generic_reloc, /* special_function */
529 "R_MIPS_26", /* name */
530 true, /* partial_inplace */
531 0x3ffffff, /* src_mask */
532 0x3ffffff, /* dst_mask */
533 false), /* pcrel_offset */
535 /* High 16 bits of symbol value. */
536 HOWTO (R_MIPS_HI16, /* type */
538 2, /* size (0 = byte, 1 = short, 2 = long) */
540 false, /* pc_relative */
542 complain_overflow_dont, /* complain_on_overflow */
543 _bfd_mips_elf_hi16_reloc, /* special_function */
544 "R_MIPS_HI16", /* name */
545 true, /* partial_inplace */
546 0xffff, /* src_mask */
547 0xffff, /* dst_mask */
548 false), /* pcrel_offset */
550 /* Low 16 bits of symbol value. */
551 HOWTO (R_MIPS_LO16, /* type */
553 2, /* size (0 = byte, 1 = short, 2 = long) */
555 false, /* pc_relative */
557 complain_overflow_dont, /* complain_on_overflow */
558 _bfd_mips_elf_lo16_reloc, /* special_function */
559 "R_MIPS_LO16", /* name */
560 true, /* partial_inplace */
561 0xffff, /* src_mask */
562 0xffff, /* dst_mask */
563 false), /* pcrel_offset */
565 /* GP relative reference. */
566 HOWTO (R_MIPS_GPREL16, /* type */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
570 false, /* pc_relative */
572 complain_overflow_signed, /* complain_on_overflow */
573 _bfd_mips_elf_gprel16_reloc, /* special_function */
574 "R_MIPS_GPREL16", /* name */
575 true, /* partial_inplace */
576 0xffff, /* src_mask */
577 0xffff, /* dst_mask */
578 false), /* pcrel_offset */
580 /* Reference to literal section. */
581 HOWTO (R_MIPS_LITERAL, /* type */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
585 false, /* pc_relative */
587 complain_overflow_signed, /* complain_on_overflow */
588 _bfd_mips_elf_gprel16_reloc, /* special_function */
589 "R_MIPS_LITERAL", /* name */
590 true, /* partial_inplace */
591 0xffff, /* src_mask */
592 0xffff, /* dst_mask */
593 false), /* pcrel_offset */
595 /* Reference to global offset table. */
596 HOWTO (R_MIPS_GOT16, /* type */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
600 false, /* pc_relative */
602 complain_overflow_signed, /* complain_on_overflow */
603 _bfd_mips_elf_got16_reloc, /* special_function */
604 "R_MIPS_GOT16", /* name */
605 false, /* partial_inplace */
606 0xffff, /* src_mask */
607 0xffff, /* dst_mask */
608 false), /* pcrel_offset */
610 /* 16 bit PC relative reference. */
611 HOWTO (R_MIPS_PC16, /* type */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
615 true, /* pc_relative */
617 complain_overflow_signed, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_MIPS_PC16", /* name */
620 true, /* partial_inplace */
621 0xffff, /* src_mask */
622 0xffff, /* dst_mask */
623 false), /* pcrel_offset */
625 /* 16 bit call through global offset table. */
626 HOWTO (R_MIPS_CALL16, /* type */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
630 false, /* pc_relative */
632 complain_overflow_signed, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_MIPS_CALL16", /* name */
635 false, /* partial_inplace */
636 0xffff, /* src_mask */
637 0xffff, /* dst_mask */
638 false), /* pcrel_offset */
640 /* 32 bit GP relative reference. */
641 HOWTO (R_MIPS_GPREL32, /* type */
643 2, /* size (0 = byte, 1 = short, 2 = long) */
645 false, /* pc_relative */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 _bfd_mips_elf_gprel32_reloc, /* special_function */
649 "R_MIPS_GPREL32", /* name */
650 true, /* partial_inplace */
651 0xffffffff, /* src_mask */
652 0xffffffff, /* dst_mask */
653 false), /* pcrel_offset */
655 /* The remaining relocs are defined on Irix 5, although they are
656 not defined by the ABI. */
661 /* A 5 bit shift field. */
662 HOWTO (R_MIPS_SHIFT5, /* type */
664 2, /* size (0 = byte, 1 = short, 2 = long) */
666 false, /* pc_relative */
668 complain_overflow_bitfield, /* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_MIPS_SHIFT5", /* name */
671 true, /* partial_inplace */
672 0x000007c0, /* src_mask */
673 0x000007c0, /* dst_mask */
674 false), /* pcrel_offset */
676 /* A 6 bit shift field. */
677 /* FIXME: This is not handled correctly; a special function is
678 needed to put the most significant bit in the right place. */
679 HOWTO (R_MIPS_SHIFT6, /* type */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
683 false, /* pc_relative */
685 complain_overflow_bitfield, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_MIPS_SHIFT6", /* name */
688 true, /* partial_inplace */
689 0x000007c4, /* src_mask */
690 0x000007c4, /* dst_mask */
691 false), /* pcrel_offset */
693 /* A 64 bit relocation. */
694 HOWTO (R_MIPS_64, /* type */
696 4, /* size (0 = byte, 1 = short, 2 = long) */
698 false, /* pc_relative */
700 complain_overflow_bitfield, /* complain_on_overflow */
701 mips32_64bit_reloc, /* special_function */
702 "R_MIPS_64", /* name */
703 true, /* partial_inplace */
704 MINUS_ONE, /* src_mask */
705 MINUS_ONE, /* dst_mask */
706 false), /* pcrel_offset */
708 /* Displacement in the global offset table. */
709 HOWTO (R_MIPS_GOT_DISP, /* type */
711 2, /* size (0 = byte, 1 = short, 2 = long) */
713 false, /* pc_relative */
715 complain_overflow_bitfield, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_MIPS_GOT_DISP", /* name */
718 true, /* partial_inplace */
719 0x0000ffff, /* src_mask */
720 0x0000ffff, /* dst_mask */
721 false), /* pcrel_offset */
723 /* Displacement to page pointer in the global offset table. */
724 HOWTO (R_MIPS_GOT_PAGE, /* type */
726 2, /* size (0 = byte, 1 = short, 2 = long) */
728 false, /* pc_relative */
730 complain_overflow_bitfield, /* complain_on_overflow */
731 bfd_elf_generic_reloc, /* special_function */
732 "R_MIPS_GOT_PAGE", /* name */
733 true, /* partial_inplace */
734 0x0000ffff, /* src_mask */
735 0x0000ffff, /* dst_mask */
736 false), /* pcrel_offset */
738 /* Offset from page pointer in the global offset table. */
739 HOWTO (R_MIPS_GOT_OFST, /* type */
741 2, /* size (0 = byte, 1 = short, 2 = long) */
743 false, /* pc_relative */
745 complain_overflow_bitfield, /* complain_on_overflow */
746 bfd_elf_generic_reloc, /* special_function */
747 "R_MIPS_GOT_OFST", /* name */
748 true, /* partial_inplace */
749 0x0000ffff, /* src_mask */
750 0x0000ffff, /* dst_mask */
751 false), /* pcrel_offset */
753 /* High 16 bits of displacement in global offset table. */
754 HOWTO (R_MIPS_GOT_HI16, /* type */
756 2, /* size (0 = byte, 1 = short, 2 = long) */
758 false, /* pc_relative */
760 complain_overflow_dont, /* complain_on_overflow */
761 bfd_elf_generic_reloc, /* special_function */
762 "R_MIPS_GOT_HI16", /* name */
763 true, /* partial_inplace */
764 0x0000ffff, /* src_mask */
765 0x0000ffff, /* dst_mask */
766 false), /* pcrel_offset */
768 /* Low 16 bits of displacement in global offset table. */
769 HOWTO (R_MIPS_GOT_LO16, /* type */
771 2, /* size (0 = byte, 1 = short, 2 = long) */
773 false, /* pc_relative */
775 complain_overflow_dont, /* complain_on_overflow */
776 bfd_elf_generic_reloc, /* special_function */
777 "R_MIPS_GOT_LO16", /* name */
778 true, /* partial_inplace */
779 0x0000ffff, /* src_mask */
780 0x0000ffff, /* dst_mask */
781 false), /* pcrel_offset */
783 /* 64 bit subtraction. Used in the N32 ABI. */
784 HOWTO (R_MIPS_SUB, /* type */
786 4, /* size (0 = byte, 1 = short, 2 = long) */
788 false, /* pc_relative */
790 complain_overflow_bitfield, /* complain_on_overflow */
791 bfd_elf_generic_reloc, /* special_function */
792 "R_MIPS_SUB", /* name */
793 true, /* partial_inplace */
794 MINUS_ONE, /* src_mask */
795 MINUS_ONE, /* dst_mask */
796 false), /* pcrel_offset */
798 /* Used to cause the linker to insert and delete instructions? */
799 EMPTY_HOWTO (R_MIPS_INSERT_A),
800 EMPTY_HOWTO (R_MIPS_INSERT_B),
801 EMPTY_HOWTO (R_MIPS_DELETE),
803 /* Get the higher value of a 64 bit addend. */
804 HOWTO (R_MIPS_HIGHER, /* type */
806 2, /* size (0 = byte, 1 = short, 2 = long) */
808 false, /* pc_relative */
810 complain_overflow_dont, /* complain_on_overflow */
811 bfd_elf_generic_reloc, /* special_function */
812 "R_MIPS_HIGHER", /* name */
813 true, /* partial_inplace */
815 0xffff, /* dst_mask */
816 false), /* pcrel_offset */
818 /* Get the highest value of a 64 bit addend. */
819 HOWTO (R_MIPS_HIGHEST, /* type */
821 2, /* size (0 = byte, 1 = short, 2 = long) */
823 false, /* pc_relative */
825 complain_overflow_dont, /* complain_on_overflow */
826 bfd_elf_generic_reloc, /* special_function */
827 "R_MIPS_HIGHEST", /* name */
828 true, /* partial_inplace */
830 0xffff, /* dst_mask */
831 false), /* pcrel_offset */
833 /* High 16 bits of displacement in global offset table. */
834 HOWTO (R_MIPS_CALL_HI16, /* type */
836 2, /* size (0 = byte, 1 = short, 2 = long) */
838 false, /* pc_relative */
840 complain_overflow_dont, /* complain_on_overflow */
841 bfd_elf_generic_reloc, /* special_function */
842 "R_MIPS_CALL_HI16", /* name */
843 true, /* partial_inplace */
844 0x0000ffff, /* src_mask */
845 0x0000ffff, /* dst_mask */
846 false), /* pcrel_offset */
848 /* Low 16 bits of displacement in global offset table. */
849 HOWTO (R_MIPS_CALL_LO16, /* type */
851 2, /* size (0 = byte, 1 = short, 2 = long) */
853 false, /* pc_relative */
855 complain_overflow_dont, /* complain_on_overflow */
856 bfd_elf_generic_reloc, /* special_function */
857 "R_MIPS_CALL_LO16", /* name */
858 true, /* partial_inplace */
859 0x0000ffff, /* src_mask */
860 0x0000ffff, /* dst_mask */
861 false), /* pcrel_offset */
863 /* Section displacement. */
864 HOWTO (R_MIPS_SCN_DISP, /* type */
866 2, /* size (0 = byte, 1 = short, 2 = long) */
868 false, /* pc_relative */
870 complain_overflow_dont, /* complain_on_overflow */
871 bfd_elf_generic_reloc, /* special_function */
872 "R_MIPS_SCN_DISP", /* name */
873 false, /* partial_inplace */
874 0xffffffff, /* src_mask */
875 0xffffffff, /* dst_mask */
876 false), /* pcrel_offset */
878 EMPTY_HOWTO (R_MIPS_REL16),
879 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
880 EMPTY_HOWTO (R_MIPS_PJUMP),
881 EMPTY_HOWTO (R_MIPS_RELGOT),
883 /* Protected jump conversion. This is an optimization hint. No
884 relocation is required for correctness. */
885 HOWTO (R_MIPS_JALR, /* type */
887 0, /* size (0 = byte, 1 = short, 2 = long) */
889 false, /* pc_relative */
891 complain_overflow_dont, /* complain_on_overflow */
892 bfd_elf_generic_reloc, /* special_function */
893 "R_MIPS_JALR", /* name */
894 false, /* partial_inplace */
895 0x00000000, /* src_mask */
896 0x00000000, /* dst_mask */
897 false), /* pcrel_offset */
900 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
901 is a hack to make the linker think that we need 64 bit values. */
902 static reloc_howto_type elf_mips_ctor64_howto =
903 HOWTO (R_MIPS_64, /* type */
905 4, /* size (0 = byte, 1 = short, 2 = long) */
907 false, /* pc_relative */
909 complain_overflow_signed, /* complain_on_overflow */
910 mips32_64bit_reloc, /* special_function */
911 "R_MIPS_64", /* name */
912 true, /* partial_inplace */
913 0xffffffff, /* src_mask */
914 0xffffffff, /* dst_mask */
915 false); /* pcrel_offset */
917 /* The reloc used for the mips16 jump instruction. */
918 static reloc_howto_type elf_mips16_jump_howto =
919 HOWTO (R_MIPS16_26, /* type */
921 2, /* size (0 = byte, 1 = short, 2 = long) */
923 false, /* pc_relative */
925 complain_overflow_dont, /* complain_on_overflow */
926 /* This needs complex overflow
927 detection, because the upper four
928 bits must match the PC. */
929 mips16_jump_reloc, /* special_function */
930 "R_MIPS16_26", /* name */
931 true, /* partial_inplace */
932 0x3ffffff, /* src_mask */
933 0x3ffffff, /* dst_mask */
934 false); /* pcrel_offset */
936 /* The reloc used for the mips16 gprel instruction. */
937 static reloc_howto_type elf_mips16_gprel_howto =
938 HOWTO (R_MIPS16_GPREL, /* type */
940 2, /* size (0 = byte, 1 = short, 2 = long) */
942 false, /* pc_relative */
944 complain_overflow_signed, /* complain_on_overflow */
945 mips16_gprel_reloc, /* special_function */
946 "R_MIPS16_GPREL", /* name */
947 true, /* partial_inplace */
948 0x07ff001f, /* src_mask */
949 0x07ff001f, /* dst_mask */
950 false); /* pcrel_offset */
953 /* GNU extension to record C++ vtable hierarchy */
954 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
955 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
957 2, /* size (0 = byte, 1 = short, 2 = long) */
959 false, /* pc_relative */
961 complain_overflow_dont, /* complain_on_overflow */
962 NULL, /* special_function */
963 "R_MIPS_GNU_VTINHERIT", /* name */
964 false, /* partial_inplace */
967 false); /* pcrel_offset */
969 /* GNU extension to record C++ vtable member usage */
970 static reloc_howto_type elf_mips_gnu_vtentry_howto =
971 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
973 2, /* size (0 = byte, 1 = short, 2 = long) */
975 false, /* pc_relative */
977 complain_overflow_dont, /* complain_on_overflow */
978 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
979 "R_MIPS_GNU_VTENTRY", /* name */
980 false, /* partial_inplace */
983 false); /* pcrel_offset */
985 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
986 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
987 the HI16. Here we just save the information we need; we do the
988 actual relocation when we see the LO16. MIPS ELF requires that the
989 LO16 immediately follow the HI16. As a GNU extension, we permit an
990 arbitrary number of HI16 relocs to be associated with a single LO16
991 reloc. This extension permits gcc to output the HI and LO relocs
996 struct mips_hi16 *next;
1001 /* FIXME: This should not be a static variable. */
1003 static struct mips_hi16 *mips_hi16_list;
1005 bfd_reloc_status_type
1006 _bfd_mips_elf_hi16_reloc (abfd,
1013 bfd *abfd ATTRIBUTE_UNUSED;
1014 arelent *reloc_entry;
1017 asection *input_section;
1019 char **error_message;
1021 bfd_reloc_status_type ret;
1023 struct mips_hi16 *n;
1025 /* If we're relocating, and this an external symbol, we don't want
1026 to change anything. */
1027 if (output_bfd != (bfd *) NULL
1028 && (symbol->flags & BSF_SECTION_SYM) == 0
1029 && reloc_entry->addend == 0)
1031 reloc_entry->address += input_section->output_offset;
1032 return bfd_reloc_ok;
1037 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1039 boolean relocateable;
1042 if (ret == bfd_reloc_undefined)
1045 if (output_bfd != NULL)
1046 relocateable = true;
1049 relocateable = false;
1050 output_bfd = symbol->section->output_section->owner;
1053 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1054 error_message, &gp);
1055 if (ret != bfd_reloc_ok)
1058 relocation = gp - reloc_entry->address;
1062 if (bfd_is_und_section (symbol->section)
1063 && output_bfd == (bfd *) NULL)
1064 ret = bfd_reloc_undefined;
1066 if (bfd_is_com_section (symbol->section))
1069 relocation = symbol->value;
1072 relocation += symbol->section->output_section->vma;
1073 relocation += symbol->section->output_offset;
1074 relocation += reloc_entry->addend;
1076 if (reloc_entry->address > input_section->_cooked_size)
1077 return bfd_reloc_outofrange;
1079 /* Save the information, and let LO16 do the actual relocation. */
1080 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
1082 return bfd_reloc_outofrange;
1083 n->addr = (bfd_byte *) data + reloc_entry->address;
1084 n->addend = relocation;
1085 n->next = mips_hi16_list;
1088 if (output_bfd != (bfd *) NULL)
1089 reloc_entry->address += input_section->output_offset;
1094 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1095 inplace relocation; this function exists in order to do the
1096 R_MIPS_HI16 relocation described above. */
1098 bfd_reloc_status_type
1099 _bfd_mips_elf_lo16_reloc (abfd,
1107 arelent *reloc_entry;
1110 asection *input_section;
1112 char **error_message;
1114 arelent gp_disp_relent;
1116 if (mips_hi16_list != NULL)
1118 struct mips_hi16 *l;
1125 unsigned long vallo;
1126 struct mips_hi16 *next;
1128 /* Do the HI16 relocation. Note that we actually don't need
1129 to know anything about the LO16 itself, except where to
1130 find the low 16 bits of the addend needed by the LO16. */
1131 insn = bfd_get_32 (abfd, l->addr);
1132 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1134 val = ((insn & 0xffff) << 16) + vallo;
1137 /* The low order 16 bits are always treated as a signed
1138 value. Therefore, a negative value in the low order bits
1139 requires an adjustment in the high order bits. We need
1140 to make this adjustment in two ways: once for the bits we
1141 took from the data, and once for the bits we are putting
1142 back in to the data. */
1143 if ((vallo & 0x8000) != 0)
1145 if ((val & 0x8000) != 0)
1148 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1149 bfd_put_32 (abfd, insn, l->addr);
1151 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1153 gp_disp_relent = *reloc_entry;
1154 reloc_entry = &gp_disp_relent;
1155 reloc_entry->addend = l->addend;
1163 mips_hi16_list = NULL;
1165 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1167 bfd_reloc_status_type ret;
1168 bfd_vma gp, relocation;
1170 /* FIXME: Does this case ever occur? */
1172 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1173 if (ret != bfd_reloc_ok)
1176 relocation = gp - reloc_entry->address;
1177 relocation += symbol->section->output_section->vma;
1178 relocation += symbol->section->output_offset;
1179 relocation += reloc_entry->addend;
1181 if (reloc_entry->address > input_section->_cooked_size)
1182 return bfd_reloc_outofrange;
1184 gp_disp_relent = *reloc_entry;
1185 reloc_entry = &gp_disp_relent;
1186 reloc_entry->addend = relocation - 4;
1189 /* Now do the LO16 reloc in the usual way. */
1190 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1191 input_section, output_bfd, error_message);
1194 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1195 table used for PIC code. If the symbol is an external symbol, the
1196 instruction is modified to contain the offset of the appropriate
1197 entry in the global offset table. If the symbol is a section
1198 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1199 addends are combined to form the real addend against the section
1200 symbol; the GOT16 is modified to contain the offset of an entry in
1201 the global offset table, and the LO16 is modified to offset it
1202 appropriately. Thus an offset larger than 16 bits requires a
1203 modified value in the global offset table.
1205 This implementation suffices for the assembler, but the linker does
1206 not yet know how to create global offset tables. */
1208 bfd_reloc_status_type
1209 _bfd_mips_elf_got16_reloc (abfd,
1217 arelent *reloc_entry;
1220 asection *input_section;
1222 char **error_message;
1224 /* If we're relocating, and this an external symbol, we don't want
1225 to change anything. */
1226 if (output_bfd != (bfd *) NULL
1227 && (symbol->flags & BSF_SECTION_SYM) == 0
1228 && reloc_entry->addend == 0)
1230 reloc_entry->address += input_section->output_offset;
1231 return bfd_reloc_ok;
1234 /* If we're relocating, and this is a local symbol, we can handle it
1236 if (output_bfd != (bfd *) NULL
1237 && (symbol->flags & BSF_SECTION_SYM) != 0)
1238 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1239 input_section, output_bfd, error_message);
1244 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1245 dangerous relocation. */
1248 mips_elf_assign_gp (output_bfd, pgp)
1256 /* If we've already figured out what GP will be, just return it. */
1257 *pgp = _bfd_get_gp_value (output_bfd);
1261 count = bfd_get_symcount (output_bfd);
1262 sym = bfd_get_outsymbols (output_bfd);
1264 /* The linker script will have created a symbol named `_gp' with the
1265 appropriate value. */
1266 if (sym == (asymbol **) NULL)
1270 for (i = 0; i < count; i++, sym++)
1272 register CONST char *name;
1274 name = bfd_asymbol_name (*sym);
1275 if (*name == '_' && strcmp (name, "_gp") == 0)
1277 *pgp = bfd_asymbol_value (*sym);
1278 _bfd_set_gp_value (output_bfd, *pgp);
1286 /* Only get the error once. */
1288 _bfd_set_gp_value (output_bfd, *pgp);
1295 /* We have to figure out the gp value, so that we can adjust the
1296 symbol value correctly. We look up the symbol _gp in the output
1297 BFD. If we can't find it, we're stuck. We cache it in the ELF
1298 target data. We don't need to adjust the symbol value for an
1299 external symbol if we are producing relocateable output. */
1301 static bfd_reloc_status_type
1302 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1305 boolean relocateable;
1306 char **error_message;
1309 if (bfd_is_und_section (symbol->section)
1313 return bfd_reloc_undefined;
1316 *pgp = _bfd_get_gp_value (output_bfd);
1319 || (symbol->flags & BSF_SECTION_SYM) != 0))
1323 /* Make up a value. */
1324 *pgp = symbol->section->output_section->vma + 0x4000;
1325 _bfd_set_gp_value (output_bfd, *pgp);
1327 else if (!mips_elf_assign_gp (output_bfd, pgp))
1330 (char *) _("GP relative relocation when _gp not defined");
1331 return bfd_reloc_dangerous;
1335 return bfd_reloc_ok;
1338 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1339 become the offset from the gp register. This function also handles
1340 R_MIPS_LITERAL relocations, although those can be handled more
1341 cleverly because the entries in the .lit8 and .lit4 sections can be
1344 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1345 arelent *, asection *,
1346 boolean, PTR, bfd_vma));
1348 bfd_reloc_status_type
1349 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1350 output_bfd, error_message)
1352 arelent *reloc_entry;
1355 asection *input_section;
1357 char **error_message;
1359 boolean relocateable;
1360 bfd_reloc_status_type ret;
1363 /* If we're relocating, and this is an external symbol with no
1364 addend, we don't want to change anything. We will only have an
1365 addend if this is a newly created reloc, not read from an ELF
1367 if (output_bfd != (bfd *) NULL
1368 && (symbol->flags & BSF_SECTION_SYM) == 0
1369 && reloc_entry->addend == 0)
1371 reloc_entry->address += input_section->output_offset;
1372 return bfd_reloc_ok;
1375 if (output_bfd != (bfd *) NULL)
1376 relocateable = true;
1379 relocateable = false;
1380 output_bfd = symbol->section->output_section->owner;
1383 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1385 if (ret != bfd_reloc_ok)
1388 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1389 relocateable, data, gp);
1392 static bfd_reloc_status_type
1393 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1397 arelent *reloc_entry;
1398 asection *input_section;
1399 boolean relocateable;
1407 if (bfd_is_com_section (symbol->section))
1410 relocation = symbol->value;
1412 relocation += symbol->section->output_section->vma;
1413 relocation += symbol->section->output_offset;
1415 if (reloc_entry->address > input_section->_cooked_size)
1416 return bfd_reloc_outofrange;
1418 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1420 /* Set val to the offset into the section or symbol. */
1421 if (reloc_entry->howto->src_mask == 0)
1423 /* This case occurs with the 64-bit MIPS ELF ABI. */
1424 val = reloc_entry->addend;
1428 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1433 /* Adjust val for the final section location and GP value. If we
1434 are producing relocateable output, we don't want to do this for
1435 an external symbol. */
1437 || (symbol->flags & BSF_SECTION_SYM) != 0)
1438 val += relocation - gp;
1440 insn = (insn &~ 0xffff) | (val & 0xffff);
1441 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1444 reloc_entry->address += input_section->output_offset;
1446 /* Make sure it fit in 16 bits. */
1447 if (val >= 0x8000 && val < 0xffff8000)
1448 return bfd_reloc_overflow;
1450 return bfd_reloc_ok;
1453 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1454 from the gp register? XXX */
1456 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1457 arelent *, asection *,
1458 boolean, PTR, bfd_vma));
1460 bfd_reloc_status_type
1461 _bfd_mips_elf_gprel32_reloc (abfd,
1469 arelent *reloc_entry;
1472 asection *input_section;
1474 char **error_message;
1476 boolean relocateable;
1477 bfd_reloc_status_type ret;
1480 /* If we're relocating, and this is an external symbol with no
1481 addend, we don't want to change anything. We will only have an
1482 addend if this is a newly created reloc, not read from an ELF
1484 if (output_bfd != (bfd *) NULL
1485 && (symbol->flags & BSF_SECTION_SYM) == 0
1486 && reloc_entry->addend == 0)
1488 *error_message = (char *)
1489 _("32bits gp relative relocation occurs for an external symbol");
1490 return bfd_reloc_outofrange;
1493 if (output_bfd != (bfd *) NULL)
1495 relocateable = true;
1496 gp = _bfd_get_gp_value (output_bfd);
1500 relocateable = false;
1501 output_bfd = symbol->section->output_section->owner;
1503 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1504 error_message, &gp);
1505 if (ret != bfd_reloc_ok)
1509 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1510 relocateable, data, gp);
1513 static bfd_reloc_status_type
1514 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1518 arelent *reloc_entry;
1519 asection *input_section;
1520 boolean relocateable;
1527 if (bfd_is_com_section (symbol->section))
1530 relocation = symbol->value;
1532 relocation += symbol->section->output_section->vma;
1533 relocation += symbol->section->output_offset;
1535 if (reloc_entry->address > input_section->_cooked_size)
1536 return bfd_reloc_outofrange;
1538 if (reloc_entry->howto->src_mask == 0)
1540 /* This case arises with the 64-bit MIPS ELF ABI. */
1544 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1546 /* Set val to the offset into the section or symbol. */
1547 val += reloc_entry->addend;
1549 /* Adjust val for the final section location and GP value. If we
1550 are producing relocateable output, we don't want to do this for
1551 an external symbol. */
1553 || (symbol->flags & BSF_SECTION_SYM) != 0)
1554 val += relocation - gp;
1556 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1559 reloc_entry->address += input_section->output_offset;
1561 return bfd_reloc_ok;
1564 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1565 generated when addreses are 64 bits. The upper 32 bits are a simle
1568 static bfd_reloc_status_type
1569 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1570 output_bfd, error_message)
1572 arelent *reloc_entry;
1575 asection *input_section;
1577 char **error_message;
1579 bfd_reloc_status_type r;
1584 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1585 input_section, output_bfd, error_message);
1586 if (r != bfd_reloc_continue)
1589 /* Do a normal 32 bit relocation on the lower 32 bits. */
1590 reloc32 = *reloc_entry;
1591 if (bfd_big_endian (abfd))
1592 reloc32.address += 4;
1593 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1594 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1595 output_bfd, error_message);
1597 /* Sign extend into the upper 32 bits. */
1598 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1599 if ((val & 0x80000000) != 0)
1603 addr = reloc_entry->address;
1604 if (bfd_little_endian (abfd))
1606 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1611 /* Handle a mips16 jump. */
1613 static bfd_reloc_status_type
1614 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1615 output_bfd, error_message)
1616 bfd *abfd ATTRIBUTE_UNUSED;
1617 arelent *reloc_entry;
1619 PTR data ATTRIBUTE_UNUSED;
1620 asection *input_section;
1622 char **error_message ATTRIBUTE_UNUSED;
1624 if (output_bfd != (bfd *) NULL
1625 && (symbol->flags & BSF_SECTION_SYM) == 0
1626 && reloc_entry->addend == 0)
1628 reloc_entry->address += input_section->output_offset;
1629 return bfd_reloc_ok;
1634 static boolean warned;
1637 (*_bfd_error_handler)
1638 (_("Linking mips16 objects into %s format is not supported"),
1639 bfd_get_target (input_section->output_section->owner));
1643 return bfd_reloc_undefined;
1646 /* Handle a mips16 GP relative reloc. */
1648 static bfd_reloc_status_type
1649 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1650 output_bfd, error_message)
1652 arelent *reloc_entry;
1655 asection *input_section;
1657 char **error_message;
1659 boolean relocateable;
1660 bfd_reloc_status_type ret;
1662 unsigned short extend, insn;
1663 unsigned long final;
1665 /* If we're relocating, and this is an external symbol with no
1666 addend, we don't want to change anything. We will only have an
1667 addend if this is a newly created reloc, not read from an ELF
1669 if (output_bfd != NULL
1670 && (symbol->flags & BSF_SECTION_SYM) == 0
1671 && reloc_entry->addend == 0)
1673 reloc_entry->address += input_section->output_offset;
1674 return bfd_reloc_ok;
1677 if (output_bfd != NULL)
1678 relocateable = true;
1681 relocateable = false;
1682 output_bfd = symbol->section->output_section->owner;
1685 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1687 if (ret != bfd_reloc_ok)
1690 if (reloc_entry->address > input_section->_cooked_size)
1691 return bfd_reloc_outofrange;
1693 /* Pick up the mips16 extend instruction and the real instruction. */
1694 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1695 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1697 /* Stuff the current addend back as a 32 bit value, do the usual
1698 relocation, and then clean up. */
1700 (((extend & 0x1f) << 11)
1703 (bfd_byte *) data + reloc_entry->address);
1705 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1706 relocateable, data, gp);
1708 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1711 | ((final >> 11) & 0x1f)
1713 (bfd_byte *) data + reloc_entry->address);
1717 (bfd_byte *) data + reloc_entry->address + 2);
1722 /* Return the ISA for a MIPS e_flags value. */
1725 elf_mips_isa (flags)
1728 switch (flags & EF_MIPS_ARCH)
1742 /* Return the MACH for a MIPS e_flags value. */
1745 elf_mips_mach (flags)
1748 switch (flags & EF_MIPS_MACH)
1750 case E_MIPS_MACH_3900:
1751 return bfd_mach_mips3900;
1753 case E_MIPS_MACH_4010:
1754 return bfd_mach_mips4010;
1756 case E_MIPS_MACH_4100:
1757 return bfd_mach_mips4100;
1759 case E_MIPS_MACH_4111:
1760 return bfd_mach_mips4111;
1762 case E_MIPS_MACH_4650:
1763 return bfd_mach_mips4650;
1766 switch (flags & EF_MIPS_ARCH)
1770 return bfd_mach_mips3000;
1774 return bfd_mach_mips6000;
1778 return bfd_mach_mips4000;
1782 return bfd_mach_mips8000;
1790 /* Return printable name for ABI. */
1793 elf_mips_abi_name (abfd)
1798 if (ABI_N32_P (abfd))
1800 else if (ABI_64_P (abfd))
1803 flags = elf_elfheader (abfd)->e_flags;
1804 switch (flags & EF_MIPS_ABI)
1808 case E_MIPS_ABI_O32:
1810 case E_MIPS_ABI_O64:
1812 case E_MIPS_ABI_EABI32:
1814 case E_MIPS_ABI_EABI64:
1817 return "unknown abi";
1821 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1823 struct elf_reloc_map {
1824 bfd_reloc_code_real_type bfd_reloc_val;
1825 enum elf_mips_reloc_type elf_reloc_val;
1828 static CONST struct elf_reloc_map mips_reloc_map[] =
1830 { BFD_RELOC_NONE, R_MIPS_NONE, },
1831 { BFD_RELOC_16, R_MIPS_16 },
1832 { BFD_RELOC_32, R_MIPS_32 },
1833 { BFD_RELOC_64, R_MIPS_64 },
1834 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1835 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1836 { BFD_RELOC_LO16, R_MIPS_LO16 },
1837 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1838 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1839 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1840 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1841 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1842 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1843 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1844 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1845 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1846 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1847 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1848 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1849 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1850 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1853 /* Given a BFD reloc type, return a howto structure. */
1855 static reloc_howto_type *
1856 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1858 bfd_reloc_code_real_type code;
1862 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1864 if (mips_reloc_map[i].bfd_reloc_val == code)
1865 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1871 bfd_set_error (bfd_error_bad_value);
1874 case BFD_RELOC_CTOR:
1875 /* We need to handle BFD_RELOC_CTOR specially.
1876 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1877 size of addresses on this architecture. */
1878 if (bfd_arch_bits_per_address (abfd) == 32)
1879 return &elf_mips_howto_table[(int) R_MIPS_32];
1881 return &elf_mips_ctor64_howto;
1883 case BFD_RELOC_MIPS16_JMP:
1884 return &elf_mips16_jump_howto;
1885 case BFD_RELOC_MIPS16_GPREL:
1886 return &elf_mips16_gprel_howto;
1887 case BFD_RELOC_VTABLE_INHERIT:
1888 return &elf_mips_gnu_vtinherit_howto;
1889 case BFD_RELOC_VTABLE_ENTRY:
1890 return &elf_mips_gnu_vtentry_howto;
1894 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1896 static reloc_howto_type *
1897 mips_rtype_to_howto (r_type)
1898 unsigned int r_type;
1903 return &elf_mips16_jump_howto;
1905 case R_MIPS16_GPREL:
1906 return &elf_mips16_gprel_howto;
1908 case R_MIPS_GNU_VTINHERIT:
1909 return &elf_mips_gnu_vtinherit_howto;
1911 case R_MIPS_GNU_VTENTRY:
1912 return &elf_mips_gnu_vtentry_howto;
1916 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1917 return &elf_mips_howto_table[r_type];
1922 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1925 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1928 Elf32_Internal_Rel *dst;
1930 unsigned int r_type;
1932 r_type = ELF32_R_TYPE (dst->r_info);
1933 cache_ptr->howto = mips_rtype_to_howto (r_type);
1935 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1936 value for the object file. We get the addend now, rather than
1937 when we do the relocation, because the symbol manipulations done
1938 by the linker may cause us to lose track of the input BFD. */
1939 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1940 && (r_type == (unsigned int) R_MIPS_GPREL16
1941 || r_type == (unsigned int) R_MIPS_LITERAL))
1942 cache_ptr->addend = elf_gp (abfd);
1945 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1948 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1951 Elf32_Internal_Rela *dst;
1953 /* Since an Elf32_Internal_Rel is an initial prefix of an
1954 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1956 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1958 /* If we ever need to do any extra processing with dst->r_addend
1959 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1962 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1963 routines swap this structure in and out. They are used outside of
1964 BFD, so they are globally visible. */
1967 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1969 const Elf32_External_RegInfo *ex;
1972 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1973 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1974 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1975 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1976 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1977 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1981 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1983 const Elf32_RegInfo *in;
1984 Elf32_External_RegInfo *ex;
1986 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1987 (bfd_byte *) ex->ri_gprmask);
1988 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1989 (bfd_byte *) ex->ri_cprmask[0]);
1990 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1991 (bfd_byte *) ex->ri_cprmask[1]);
1992 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1993 (bfd_byte *) ex->ri_cprmask[2]);
1994 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1995 (bfd_byte *) ex->ri_cprmask[3]);
1996 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1997 (bfd_byte *) ex->ri_gp_value);
2000 /* In the 64 bit ABI, the .MIPS.options section holds register
2001 information in an Elf64_Reginfo structure. These routines swap
2002 them in and out. They are globally visible because they are used
2003 outside of BFD. These routines are here so that gas can call them
2004 without worrying about whether the 64 bit ABI has been included. */
2007 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
2009 const Elf64_External_RegInfo *ex;
2010 Elf64_Internal_RegInfo *in;
2012 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
2013 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
2014 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
2015 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
2016 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
2017 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
2018 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
2022 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
2024 const Elf64_Internal_RegInfo *in;
2025 Elf64_External_RegInfo *ex;
2027 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
2028 (bfd_byte *) ex->ri_gprmask);
2029 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
2030 (bfd_byte *) ex->ri_pad);
2031 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
2032 (bfd_byte *) ex->ri_cprmask[0]);
2033 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
2034 (bfd_byte *) ex->ri_cprmask[1]);
2035 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
2036 (bfd_byte *) ex->ri_cprmask[2]);
2037 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
2038 (bfd_byte *) ex->ri_cprmask[3]);
2039 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
2040 (bfd_byte *) ex->ri_gp_value);
2043 /* Swap an entry in a .gptab section. Note that these routines rely
2044 on the equivalence of the two elements of the union. */
2047 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
2049 const Elf32_External_gptab *ex;
2052 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
2053 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
2057 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
2059 const Elf32_gptab *in;
2060 Elf32_External_gptab *ex;
2062 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
2063 ex->gt_entry.gt_g_value);
2064 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
2065 ex->gt_entry.gt_bytes);
2069 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2071 const Elf32_compact_rel *in;
2072 Elf32_External_compact_rel *ex;
2074 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
2075 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
2076 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
2077 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
2078 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
2079 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
2083 bfd_elf32_swap_crinfo_out (abfd, in, ex)
2085 const Elf32_crinfo *in;
2086 Elf32_External_crinfo *ex;
2090 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2091 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2092 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2093 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2094 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
2095 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
2096 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
2099 /* Swap in an options header. */
2102 bfd_mips_elf_swap_options_in (abfd, ex, in)
2104 const Elf_External_Options *ex;
2105 Elf_Internal_Options *in;
2107 in->kind = bfd_h_get_8 (abfd, ex->kind);
2108 in->size = bfd_h_get_8 (abfd, ex->size);
2109 in->section = bfd_h_get_16 (abfd, ex->section);
2110 in->info = bfd_h_get_32 (abfd, ex->info);
2113 /* Swap out an options header. */
2116 bfd_mips_elf_swap_options_out (abfd, in, ex)
2118 const Elf_Internal_Options *in;
2119 Elf_External_Options *ex;
2121 bfd_h_put_8 (abfd, in->kind, ex->kind);
2122 bfd_h_put_8 (abfd, in->size, ex->size);
2123 bfd_h_put_16 (abfd, in->section, ex->section);
2124 bfd_h_put_32 (abfd, in->info, ex->info);
2127 /* Swap in an MSYM entry. */
2130 bfd_mips_elf_swap_msym_in (abfd, ex, in)
2132 const Elf32_External_Msym *ex;
2133 Elf32_Internal_Msym *in;
2135 in->ms_hash_value = bfd_h_get_32 (abfd, ex->ms_hash_value);
2136 in->ms_info = bfd_h_get_32 (abfd, ex->ms_info);
2139 /* Swap out an MSYM entry. */
2142 bfd_mips_elf_swap_msym_out (abfd, in, ex)
2144 const Elf32_Internal_Msym *in;
2145 Elf32_External_Msym *ex;
2147 bfd_h_put_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2148 bfd_h_put_32 (abfd, in->ms_info, ex->ms_info);
2152 /* Determine whether a symbol is global for the purposes of splitting
2153 the symbol table into global symbols and local symbols. At least
2154 on Irix 5, this split must be between section symbols and all other
2155 symbols. On most ELF targets the split is between static symbols
2156 and externally visible symbols. */
2160 mips_elf_sym_is_global (abfd, sym)
2161 bfd *abfd ATTRIBUTE_UNUSED;
2164 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
2167 /* Set the right machine number for a MIPS ELF file. This is used for
2168 both the 32-bit and the 64-bit ABI. */
2171 _bfd_mips_elf_object_p (abfd)
2174 /* Irix 5 and 6 is broken. Object file symbol tables are not always
2175 sorted correctly such that local symbols precede global symbols,
2176 and the sh_info field in the symbol table is not always right. */
2177 elf_bad_symtab (abfd) = true;
2179 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2180 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2184 /* The final processing done just before writing out a MIPS ELF object
2185 file. This gets the MIPS architecture right based on the machine
2186 number. This is used by both the 32-bit and the 64-bit ABI. */
2190 _bfd_mips_elf_final_write_processing (abfd, linker)
2192 boolean linker ATTRIBUTE_UNUSED;
2196 Elf_Internal_Shdr **hdrpp;
2200 switch (bfd_get_mach (abfd))
2203 case bfd_mach_mips3000:
2204 val = E_MIPS_ARCH_1;
2207 case bfd_mach_mips3900:
2208 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2211 case bfd_mach_mips6000:
2212 val = E_MIPS_ARCH_2;
2215 case bfd_mach_mips4000:
2216 case bfd_mach_mips4300:
2217 val = E_MIPS_ARCH_3;
2220 case bfd_mach_mips4010:
2221 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2224 case bfd_mach_mips4100:
2225 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2228 case bfd_mach_mips4111:
2229 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2232 case bfd_mach_mips4650:
2233 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2236 case bfd_mach_mips8000:
2237 val = E_MIPS_ARCH_4;
2241 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2242 elf_elfheader (abfd)->e_flags |= val;
2244 /* Set the sh_info field for .gptab sections and other appropriate
2245 info for each special section. */
2246 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2247 i < elf_elfheader (abfd)->e_shnum;
2250 switch ((*hdrpp)->sh_type)
2253 case SHT_MIPS_LIBLIST:
2254 sec = bfd_get_section_by_name (abfd, ".dynstr");
2256 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2259 case SHT_MIPS_GPTAB:
2260 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2261 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2262 BFD_ASSERT (name != NULL
2263 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2264 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2265 BFD_ASSERT (sec != NULL);
2266 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2269 case SHT_MIPS_CONTENT:
2270 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2271 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2272 BFD_ASSERT (name != NULL
2273 && strncmp (name, ".MIPS.content",
2274 sizeof ".MIPS.content" - 1) == 0);
2275 sec = bfd_get_section_by_name (abfd,
2276 name + sizeof ".MIPS.content" - 1);
2277 BFD_ASSERT (sec != NULL);
2278 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2281 case SHT_MIPS_SYMBOL_LIB:
2282 sec = bfd_get_section_by_name (abfd, ".dynsym");
2284 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2285 sec = bfd_get_section_by_name (abfd, ".liblist");
2287 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2290 case SHT_MIPS_EVENTS:
2291 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2292 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2293 BFD_ASSERT (name != NULL);
2294 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2295 sec = bfd_get_section_by_name (abfd,
2296 name + sizeof ".MIPS.events" - 1);
2299 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2300 sizeof ".MIPS.post_rel" - 1) == 0);
2301 sec = bfd_get_section_by_name (abfd,
2303 + sizeof ".MIPS.post_rel" - 1));
2305 BFD_ASSERT (sec != NULL);
2306 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2313 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2316 _bfd_mips_elf_set_private_flags (abfd, flags)
2320 BFD_ASSERT (!elf_flags_init (abfd)
2321 || elf_elfheader (abfd)->e_flags == flags);
2323 elf_elfheader (abfd)->e_flags = flags;
2324 elf_flags_init (abfd) = true;
2328 /* Copy backend specific data from one object module to another */
2331 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2335 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2336 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2339 BFD_ASSERT (!elf_flags_init (obfd)
2340 || (elf_elfheader (obfd)->e_flags
2341 == elf_elfheader (ibfd)->e_flags));
2343 elf_gp (obfd) = elf_gp (ibfd);
2344 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2345 elf_flags_init (obfd) = true;
2349 /* Merge backend specific data from an object file to the output
2350 object file when linking. */
2353 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2361 /* Check if we have the same endianess */
2362 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2363 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2367 if (bfd_big_endian (ibfd))
2368 msg = _("%s: compiled for a big endian system and target is little endian");
2370 msg = _("%s: compiled for a little endian system and target is big endian");
2372 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2374 bfd_set_error (bfd_error_wrong_format);
2378 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2379 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2382 new_flags = elf_elfheader (ibfd)->e_flags;
2383 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2384 old_flags = elf_elfheader (obfd)->e_flags;
2386 if (! elf_flags_init (obfd))
2388 elf_flags_init (obfd) = true;
2389 elf_elfheader (obfd)->e_flags = new_flags;
2390 elf_elfheader (obfd)->e_ident[EI_CLASS]
2391 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
2393 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2394 && bfd_get_arch_info (obfd)->the_default)
2396 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2397 bfd_get_mach (ibfd)))
2404 /* Check flag compatibility. */
2406 new_flags &= ~EF_MIPS_NOREORDER;
2407 old_flags &= ~EF_MIPS_NOREORDER;
2409 if (new_flags == old_flags)
2414 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2416 new_flags &= ~EF_MIPS_PIC;
2417 old_flags &= ~EF_MIPS_PIC;
2418 (*_bfd_error_handler)
2419 (_("%s: linking PIC files with non-PIC files"),
2420 bfd_get_filename (ibfd));
2424 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2426 new_flags &= ~EF_MIPS_CPIC;
2427 old_flags &= ~EF_MIPS_CPIC;
2428 (*_bfd_error_handler)
2429 (_("%s: linking abicalls files with non-abicalls files"),
2430 bfd_get_filename (ibfd));
2434 /* Compare the ISA's. */
2435 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2436 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2438 int new_mach = new_flags & EF_MIPS_MACH;
2439 int old_mach = old_flags & EF_MIPS_MACH;
2440 int new_isa = elf_mips_isa (new_flags);
2441 int old_isa = elf_mips_isa (old_flags);
2443 /* If either has no machine specified, just compare the general isa's.
2444 Some combinations of machines are ok, if the isa's match. */
2447 || new_mach == old_mach
2450 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2451 and -mips4 code. They will normally use the same data sizes and
2452 calling conventions. */
2454 if ((new_isa == 1 || new_isa == 2)
2455 ? (old_isa != 1 && old_isa != 2)
2456 : (old_isa == 1 || old_isa == 2))
2458 (*_bfd_error_handler)
2459 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2460 bfd_get_filename (ibfd), new_isa, old_isa);
2467 (*_bfd_error_handler)
2468 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2469 bfd_get_filename (ibfd),
2470 elf_mips_mach (new_flags),
2471 elf_mips_mach (old_flags));
2475 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2476 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2479 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
2480 does set EI_CLASS differently from any 32-bit ABI. */
2481 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
2482 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2483 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2485 /* Only error if both are set (to different values). */
2486 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
2487 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2488 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2490 (*_bfd_error_handler)
2491 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2492 bfd_get_filename (ibfd),
2493 elf_mips_abi_name (ibfd),
2494 elf_mips_abi_name (obfd));
2497 new_flags &= ~EF_MIPS_ABI;
2498 old_flags &= ~EF_MIPS_ABI;
2501 /* Warn about any other mismatches */
2502 if (new_flags != old_flags)
2504 (*_bfd_error_handler)
2505 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2506 bfd_get_filename (ibfd), (unsigned long) new_flags,
2507 (unsigned long) old_flags);
2513 bfd_set_error (bfd_error_bad_value);
2521 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2525 FILE *file = (FILE *) ptr;
2527 BFD_ASSERT (abfd != NULL && ptr != NULL);
2529 /* Print normal ELF private data. */
2530 _bfd_elf_print_private_bfd_data (abfd, ptr);
2532 /* xgettext:c-format */
2533 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2535 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2536 fprintf (file, _ (" [abi=O32]"));
2537 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2538 fprintf (file, _ (" [abi=O64]"));
2539 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2540 fprintf (file, _ (" [abi=EABI32]"));
2541 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2542 fprintf (file, _ (" [abi=EABI64]"));
2543 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2544 fprintf (file, _ (" [abi unknown]"));
2545 else if (ABI_N32_P (abfd))
2546 fprintf (file, _ (" [abi=N32]"));
2547 else if (ABI_64_P (abfd))
2548 fprintf (file, _ (" [abi=64]"));
2550 fprintf (file, _ (" [no abi set]"));
2552 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2553 fprintf (file, _ (" [mips1]"));
2554 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2555 fprintf (file, _ (" [mips2]"));
2556 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2557 fprintf (file, _ (" [mips3]"));
2558 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2559 fprintf (file, _ (" [mips4]"));
2561 fprintf (file, _ (" [unknown ISA]"));
2563 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2564 fprintf (file, _ (" [32bitmode]"));
2566 fprintf (file, _ (" [not 32bitmode]"));
2573 /* Handle a MIPS specific section when reading an object file. This
2574 is called when elfcode.h finds a section with an unknown type.
2575 This routine supports both the 32-bit and 64-bit ELF ABI.
2577 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2581 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2583 Elf_Internal_Shdr *hdr;
2588 /* There ought to be a place to keep ELF backend specific flags, but
2589 at the moment there isn't one. We just keep track of the
2590 sections by their name, instead. Fortunately, the ABI gives
2591 suggested names for all the MIPS specific sections, so we will
2592 probably get away with this. */
2593 switch (hdr->sh_type)
2595 case SHT_MIPS_LIBLIST:
2596 if (strcmp (name, ".liblist") != 0)
2600 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2603 case SHT_MIPS_CONFLICT:
2604 if (strcmp (name, ".conflict") != 0)
2607 case SHT_MIPS_GPTAB:
2608 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2611 case SHT_MIPS_UCODE:
2612 if (strcmp (name, ".ucode") != 0)
2615 case SHT_MIPS_DEBUG:
2616 if (strcmp (name, ".mdebug") != 0)
2618 flags = SEC_DEBUGGING;
2620 case SHT_MIPS_REGINFO:
2621 if (strcmp (name, ".reginfo") != 0
2622 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2624 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2626 case SHT_MIPS_IFACE:
2627 if (strcmp (name, ".MIPS.interfaces") != 0)
2630 case SHT_MIPS_CONTENT:
2631 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2634 case SHT_MIPS_OPTIONS:
2635 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2638 case SHT_MIPS_DWARF:
2639 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2642 case SHT_MIPS_SYMBOL_LIB:
2643 if (strcmp (name, ".MIPS.symlib") != 0)
2646 case SHT_MIPS_EVENTS:
2647 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2648 && strncmp (name, ".MIPS.post_rel",
2649 sizeof ".MIPS.post_rel" - 1) != 0)
2656 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2661 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2662 (bfd_get_section_flags (abfd,
2668 /* FIXME: We should record sh_info for a .gptab section. */
2670 /* For a .reginfo section, set the gp value in the tdata information
2671 from the contents of this section. We need the gp value while
2672 processing relocs, so we just get it now. The .reginfo section
2673 is not used in the 64-bit MIPS ELF ABI. */
2674 if (hdr->sh_type == SHT_MIPS_REGINFO)
2676 Elf32_External_RegInfo ext;
2679 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2680 (file_ptr) 0, sizeof ext))
2682 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2683 elf_gp (abfd) = s.ri_gp_value;
2686 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2687 set the gp value based on what we find. We may see both
2688 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2689 they should agree. */
2690 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2692 bfd_byte *contents, *l, *lend;
2694 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2695 if (contents == NULL)
2697 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2698 (file_ptr) 0, hdr->sh_size))
2704 lend = contents + hdr->sh_size;
2705 while (l + sizeof (Elf_External_Options) <= lend)
2707 Elf_Internal_Options intopt;
2709 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2711 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2713 Elf64_Internal_RegInfo intreg;
2715 bfd_mips_elf64_swap_reginfo_in
2717 ((Elf64_External_RegInfo *)
2718 (l + sizeof (Elf_External_Options))),
2720 elf_gp (abfd) = intreg.ri_gp_value;
2722 else if (intopt.kind == ODK_REGINFO)
2724 Elf32_RegInfo intreg;
2726 bfd_mips_elf32_swap_reginfo_in
2728 ((Elf32_External_RegInfo *)
2729 (l + sizeof (Elf_External_Options))),
2731 elf_gp (abfd) = intreg.ri_gp_value;
2741 /* Set the correct type for a MIPS ELF section. We do this by the
2742 section name, which is a hack, but ought to work. This routine is
2743 used by both the 32-bit and the 64-bit ABI. */
2746 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2748 Elf32_Internal_Shdr *hdr;
2751 register const char *name;
2753 name = bfd_get_section_name (abfd, sec);
2755 if (strcmp (name, ".liblist") == 0)
2757 hdr->sh_type = SHT_MIPS_LIBLIST;
2758 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2759 /* The sh_link field is set in final_write_processing. */
2761 else if (strcmp (name, ".conflict") == 0)
2762 hdr->sh_type = SHT_MIPS_CONFLICT;
2763 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2765 hdr->sh_type = SHT_MIPS_GPTAB;
2766 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2767 /* The sh_info field is set in final_write_processing. */
2769 else if (strcmp (name, ".ucode") == 0)
2770 hdr->sh_type = SHT_MIPS_UCODE;
2771 else if (strcmp (name, ".mdebug") == 0)
2773 hdr->sh_type = SHT_MIPS_DEBUG;
2774 /* In a shared object on Irix 5.3, the .mdebug section has an
2775 entsize of 0. FIXME: Does this matter? */
2776 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2777 hdr->sh_entsize = 0;
2779 hdr->sh_entsize = 1;
2781 else if (strcmp (name, ".reginfo") == 0)
2783 hdr->sh_type = SHT_MIPS_REGINFO;
2784 /* In a shared object on Irix 5.3, the .reginfo section has an
2785 entsize of 0x18. FIXME: Does this matter? */
2786 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2787 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2789 hdr->sh_entsize = 1;
2791 else if (SGI_COMPAT (abfd)
2792 && (strcmp (name, ".hash") == 0
2793 || strcmp (name, ".dynamic") == 0
2794 || strcmp (name, ".dynstr") == 0))
2796 hdr->sh_entsize = 0;
2798 /* This isn't how the Irix 6 linker behaves. */
2799 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2802 else if (strcmp (name, ".got") == 0
2803 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2804 || strcmp (name, ".sdata") == 0
2805 || strcmp (name, ".sbss") == 0
2806 || strcmp (name, ".lit4") == 0
2807 || strcmp (name, ".lit8") == 0)
2808 hdr->sh_flags |= SHF_MIPS_GPREL;
2809 else if (strcmp (name, ".MIPS.interfaces") == 0)
2811 hdr->sh_type = SHT_MIPS_IFACE;
2812 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2814 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2816 hdr->sh_type = SHT_MIPS_CONTENT;
2817 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2818 /* The sh_info field is set in final_write_processing. */
2820 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2822 hdr->sh_type = SHT_MIPS_OPTIONS;
2823 hdr->sh_entsize = 1;
2824 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2826 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2827 hdr->sh_type = SHT_MIPS_DWARF;
2828 else if (strcmp (name, ".MIPS.symlib") == 0)
2830 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2831 /* The sh_link and sh_info fields are set in
2832 final_write_processing. */
2834 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2835 || strncmp (name, ".MIPS.post_rel",
2836 sizeof ".MIPS.post_rel" - 1) == 0)
2838 hdr->sh_type = SHT_MIPS_EVENTS;
2839 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2840 /* The sh_link field is set in final_write_processing. */
2842 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2844 hdr->sh_type = SHT_MIPS_MSYM;
2845 hdr->sh_flags |= SHF_ALLOC;
2846 hdr->sh_entsize = 8;
2849 /* The generic elf_fake_sections will set up REL_HDR using the
2850 default kind of relocations. But, we may actually need both
2851 kinds of relocations, so we set up the second header here. */
2852 if ((sec->flags & SEC_RELOC) != 0)
2854 struct bfd_elf_section_data *esd;
2856 esd = elf_section_data (sec);
2857 BFD_ASSERT (esd->rel_hdr2 == NULL);
2859 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2862 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
2863 !elf_section_data (sec)->use_rela_p);
2869 /* Given a BFD section, try to locate the corresponding ELF section
2870 index. This is used by both the 32-bit and the 64-bit ABI.
2871 Actually, it's not clear to me that the 64-bit ABI supports these,
2872 but for non-PIC objects we will certainly want support for at least
2873 the .scommon section. */
2876 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2877 bfd *abfd ATTRIBUTE_UNUSED;
2878 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
2882 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2884 *retval = SHN_MIPS_SCOMMON;
2887 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2889 *retval = SHN_MIPS_ACOMMON;
2895 /* When are writing out the .options or .MIPS.options section,
2896 remember the bytes we are writing out, so that we can install the
2897 GP value in the section_processing routine. */
2900 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2905 bfd_size_type count;
2907 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2911 if (elf_section_data (section) == NULL)
2913 section->used_by_bfd =
2914 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2915 if (elf_section_data (section) == NULL)
2918 c = (bfd_byte *) elf_section_data (section)->tdata;
2923 if (section->_cooked_size != 0)
2924 size = section->_cooked_size;
2926 size = section->_raw_size;
2927 c = (bfd_byte *) bfd_zalloc (abfd, size);
2930 elf_section_data (section)->tdata = (PTR) c;
2933 memcpy (c + offset, location, count);
2936 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2940 /* Work over a section just before writing it out. This routine is
2941 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2942 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2946 _bfd_mips_elf_section_processing (abfd, hdr)
2948 Elf_Internal_Shdr *hdr;
2950 if (hdr->sh_type == SHT_MIPS_REGINFO
2951 && hdr->sh_size > 0)
2955 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2956 BFD_ASSERT (hdr->contents == NULL);
2959 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2962 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2963 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2967 if (hdr->sh_type == SHT_MIPS_OPTIONS
2968 && hdr->bfd_section != NULL
2969 && elf_section_data (hdr->bfd_section) != NULL
2970 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2972 bfd_byte *contents, *l, *lend;
2974 /* We stored the section contents in the elf_section_data tdata
2975 field in the set_section_contents routine. We save the
2976 section contents so that we don't have to read them again.
2977 At this point we know that elf_gp is set, so we can look
2978 through the section contents to see if there is an
2979 ODK_REGINFO structure. */
2981 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2983 lend = contents + hdr->sh_size;
2984 while (l + sizeof (Elf_External_Options) <= lend)
2986 Elf_Internal_Options intopt;
2988 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2990 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2997 + sizeof (Elf_External_Options)
2998 + (sizeof (Elf64_External_RegInfo) - 8)),
3001 bfd_h_put_64 (abfd, elf_gp (abfd), buf);
3002 if (bfd_write (buf, 1, 8, abfd) != 8)
3005 else if (intopt.kind == ODK_REGINFO)
3012 + sizeof (Elf_External_Options)
3013 + (sizeof (Elf32_External_RegInfo) - 4)),
3016 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
3017 if (bfd_write (buf, 1, 4, abfd) != 4)
3024 if (hdr->bfd_section != NULL)
3026 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3028 if (strcmp (name, ".sdata") == 0
3029 || strcmp (name, ".lit8") == 0
3030 || strcmp (name, ".lit4") == 0)
3032 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3033 hdr->sh_type = SHT_PROGBITS;
3035 else if (strcmp (name, ".sbss") == 0)
3037 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3038 hdr->sh_type = SHT_NOBITS;
3040 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3042 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3043 hdr->sh_type = SHT_PROGBITS;
3045 else if (strcmp (name, ".compact_rel") == 0)
3048 hdr->sh_type = SHT_PROGBITS;
3050 else if (strcmp (name, ".rtproc") == 0)
3052 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3054 unsigned int adjust;
3056 adjust = hdr->sh_size % hdr->sh_addralign;
3058 hdr->sh_size += hdr->sh_addralign - adjust;
3067 /* MIPS ELF uses two common sections. One is the usual one, and the
3068 other is for small objects. All the small objects are kept
3069 together, and then referenced via the gp pointer, which yields
3070 faster assembler code. This is what we use for the small common
3071 section. This approach is copied from ecoff.c. */
3072 static asection mips_elf_scom_section;
3073 static asymbol mips_elf_scom_symbol;
3074 static asymbol *mips_elf_scom_symbol_ptr;
3076 /* MIPS ELF also uses an acommon section, which represents an
3077 allocated common symbol which may be overridden by a
3078 definition in a shared library. */
3079 static asection mips_elf_acom_section;
3080 static asymbol mips_elf_acom_symbol;
3081 static asymbol *mips_elf_acom_symbol_ptr;
3083 /* The Irix 5 support uses two virtual sections, which represent
3084 text/data symbols defined in dynamic objects. */
3085 static asection mips_elf_text_section;
3086 static asection *mips_elf_text_section_ptr;
3087 static asymbol mips_elf_text_symbol;
3088 static asymbol *mips_elf_text_symbol_ptr;
3090 static asection mips_elf_data_section;
3091 static asection *mips_elf_data_section_ptr;
3092 static asymbol mips_elf_data_symbol;
3093 static asymbol *mips_elf_data_symbol_ptr;
3095 /* Handle the special MIPS section numbers that a symbol may use.
3096 This is used for both the 32-bit and the 64-bit ABI. */
3099 _bfd_mips_elf_symbol_processing (abfd, asym)
3103 elf_symbol_type *elfsym;
3105 elfsym = (elf_symbol_type *) asym;
3106 switch (elfsym->internal_elf_sym.st_shndx)
3108 case SHN_MIPS_ACOMMON:
3109 /* This section is used in a dynamically linked executable file.
3110 It is an allocated common section. The dynamic linker can
3111 either resolve these symbols to something in a shared
3112 library, or it can just leave them here. For our purposes,
3113 we can consider these symbols to be in a new section. */
3114 if (mips_elf_acom_section.name == NULL)
3116 /* Initialize the acommon section. */
3117 mips_elf_acom_section.name = ".acommon";
3118 mips_elf_acom_section.flags = SEC_ALLOC;
3119 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3120 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3121 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3122 mips_elf_acom_symbol.name = ".acommon";
3123 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3124 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3125 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3127 asym->section = &mips_elf_acom_section;
3131 /* Common symbols less than the GP size are automatically
3132 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3133 if (asym->value > elf_gp_size (abfd)
3134 || IRIX_COMPAT (abfd) == ict_irix6)
3137 case SHN_MIPS_SCOMMON:
3138 if (mips_elf_scom_section.name == NULL)
3140 /* Initialize the small common section. */
3141 mips_elf_scom_section.name = ".scommon";
3142 mips_elf_scom_section.flags = SEC_IS_COMMON;
3143 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3144 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3145 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3146 mips_elf_scom_symbol.name = ".scommon";
3147 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3148 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3149 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3151 asym->section = &mips_elf_scom_section;
3152 asym->value = elfsym->internal_elf_sym.st_size;
3155 case SHN_MIPS_SUNDEFINED:
3156 asym->section = bfd_und_section_ptr;
3159 #if 0 /* for SGI_COMPAT */
3161 asym->section = mips_elf_text_section_ptr;
3165 asym->section = mips_elf_data_section_ptr;
3171 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3175 _bfd_mips_elf_additional_program_headers (abfd)
3181 if (!SGI_COMPAT (abfd))
3184 /* See if we need a PT_MIPS_REGINFO segment. */
3185 s = bfd_get_section_by_name (abfd, ".reginfo");
3186 if (s && (s->flags & SEC_LOAD))
3189 /* See if we need a PT_MIPS_OPTIONS segment. */
3190 if (IRIX_COMPAT (abfd) == ict_irix6
3191 && bfd_get_section_by_name (abfd,
3192 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3195 /* See if we need a PT_MIPS_RTPROC segment. */
3196 if (IRIX_COMPAT (abfd) == ict_irix5
3197 && bfd_get_section_by_name (abfd, ".dynamic")
3198 && bfd_get_section_by_name (abfd, ".mdebug"))
3204 /* Modify the segment map for an Irix 5 executable. */
3207 _bfd_mips_elf_modify_segment_map (abfd)
3211 struct elf_segment_map *m, **pm;
3213 if (! SGI_COMPAT (abfd))
3216 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3218 s = bfd_get_section_by_name (abfd, ".reginfo");
3219 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3221 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3222 if (m->p_type == PT_MIPS_REGINFO)
3226 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3230 m->p_type = PT_MIPS_REGINFO;
3234 /* We want to put it after the PHDR and INTERP segments. */
3235 pm = &elf_tdata (abfd)->segment_map;
3237 && ((*pm)->p_type == PT_PHDR
3238 || (*pm)->p_type == PT_INTERP))
3246 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3247 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3248 PT_OPTIONS segement immediately following the program header
3250 if (IRIX_COMPAT (abfd) == ict_irix6)
3254 for (s = abfd->sections; s; s = s->next)
3255 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3260 struct elf_segment_map *options_segment;
3262 /* Usually, there's a program header table. But, sometimes
3263 there's not (like when running the `ld' testsuite). So,
3264 if there's no program header table, we just put the
3265 options segement at the end. */
3266 for (pm = &elf_tdata (abfd)->segment_map;
3269 if ((*pm)->p_type == PT_PHDR)
3272 options_segment = bfd_zalloc (abfd,
3273 sizeof (struct elf_segment_map));
3274 options_segment->next = *pm;
3275 options_segment->p_type = PT_MIPS_OPTIONS;
3276 options_segment->p_flags = PF_R;
3277 options_segment->p_flags_valid = true;
3278 options_segment->count = 1;
3279 options_segment->sections[0] = s;
3280 *pm = options_segment;
3285 /* If there are .dynamic and .mdebug sections, we make a room
3286 for the RTPROC header. FIXME: Rewrite without section names. */
3287 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3288 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3289 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3291 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3292 if (m->p_type == PT_MIPS_RTPROC)
3296 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3300 m->p_type = PT_MIPS_RTPROC;
3302 s = bfd_get_section_by_name (abfd, ".rtproc");
3307 m->p_flags_valid = 1;
3315 /* We want to put it after the DYNAMIC segment. */
3316 pm = &elf_tdata (abfd)->segment_map;
3317 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3327 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3328 .dynstr, .dynsym, and .hash sections, and everything in
3330 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3331 if ((*pm)->p_type == PT_DYNAMIC)
3336 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3338 static const char *sec_names[] =
3339 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3342 struct elf_segment_map *n;
3346 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3348 s = bfd_get_section_by_name (abfd, sec_names[i]);
3349 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3355 sz = s->_cooked_size;
3358 if (high < s->vma + sz)
3364 for (s = abfd->sections; s != NULL; s = s->next)
3365 if ((s->flags & SEC_LOAD) != 0
3368 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3372 n = ((struct elf_segment_map *)
3373 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3380 for (s = abfd->sections; s != NULL; s = s->next)
3382 if ((s->flags & SEC_LOAD) != 0
3385 + (s->_cooked_size != 0 ?
3386 s->_cooked_size : s->_raw_size))
3401 /* The structure of the runtime procedure descriptor created by the
3402 loader for use by the static exception system. */
3404 typedef struct runtime_pdr {
3405 bfd_vma adr; /* memory address of start of procedure */
3406 long regmask; /* save register mask */
3407 long regoffset; /* save register offset */
3408 long fregmask; /* save floating point register mask */
3409 long fregoffset; /* save floating point register offset */
3410 long frameoffset; /* frame size */
3411 short framereg; /* frame pointer register */
3412 short pcreg; /* offset or reg of return pc */
3413 long irpss; /* index into the runtime string table */
3415 struct exception_info *exception_info;/* pointer to exception array */
3417 #define cbRPDR sizeof(RPDR)
3418 #define rpdNil ((pRPDR) 0)
3420 /* Swap RPDR (runtime procedure table entry) for output. */
3422 static void ecoff_swap_rpdr_out
3423 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3426 ecoff_swap_rpdr_out (abfd, in, ex)
3429 struct rpdr_ext *ex;
3431 /* ecoff_put_off was defined in ecoffswap.h. */
3432 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3433 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3434 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3435 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3436 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3437 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3439 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3440 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3442 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3444 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3448 /* Read ECOFF debugging information from a .mdebug section into a
3449 ecoff_debug_info structure. */
3452 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3455 struct ecoff_debug_info *debug;
3458 const struct ecoff_debug_swap *swap;
3459 char *ext_hdr = NULL;
3461 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3462 memset (debug, 0, sizeof(*debug));
3464 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3465 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3468 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3469 swap->external_hdr_size)
3473 symhdr = &debug->symbolic_header;
3474 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3476 /* The symbolic header contains absolute file offsets and sizes to
3478 #define READ(ptr, offset, count, size, type) \
3479 if (symhdr->count == 0) \
3480 debug->ptr = NULL; \
3483 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3484 if (debug->ptr == NULL) \
3485 goto error_return; \
3486 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3487 || (bfd_read (debug->ptr, size, symhdr->count, \
3488 abfd) != size * symhdr->count)) \
3489 goto error_return; \
3492 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3493 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3494 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3495 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3496 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3497 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3499 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3500 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3501 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3502 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3503 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3507 debug->adjust = NULL;
3512 if (ext_hdr != NULL)
3514 if (debug->line != NULL)
3516 if (debug->external_dnr != NULL)
3517 free (debug->external_dnr);
3518 if (debug->external_pdr != NULL)
3519 free (debug->external_pdr);
3520 if (debug->external_sym != NULL)
3521 free (debug->external_sym);
3522 if (debug->external_opt != NULL)
3523 free (debug->external_opt);
3524 if (debug->external_aux != NULL)
3525 free (debug->external_aux);
3526 if (debug->ss != NULL)
3528 if (debug->ssext != NULL)
3529 free (debug->ssext);
3530 if (debug->external_fdr != NULL)
3531 free (debug->external_fdr);
3532 if (debug->external_rfd != NULL)
3533 free (debug->external_rfd);
3534 if (debug->external_ext != NULL)
3535 free (debug->external_ext);
3539 /* MIPS ELF local labels start with '$', not 'L'. */
3543 mips_elf_is_local_label_name (abfd, name)
3550 /* On Irix 6, the labels go back to starting with '.', so we accept
3551 the generic ELF local label syntax as well. */
3552 return _bfd_elf_is_local_label_name (abfd, name);
3555 /* MIPS ELF uses a special find_nearest_line routine in order the
3556 handle the ECOFF debugging information. */
3558 struct mips_elf_find_line
3560 struct ecoff_debug_info d;
3561 struct ecoff_find_line i;
3565 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3566 functionname_ptr, line_ptr)
3571 const char **filename_ptr;
3572 const char **functionname_ptr;
3573 unsigned int *line_ptr;
3577 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3578 filename_ptr, functionname_ptr,
3582 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3583 filename_ptr, functionname_ptr,
3585 ABI_64_P (abfd) ? 8 : 0))
3588 msec = bfd_get_section_by_name (abfd, ".mdebug");
3592 struct mips_elf_find_line *fi;
3593 const struct ecoff_debug_swap * const swap =
3594 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3596 /* If we are called during a link, mips_elf_final_link may have
3597 cleared the SEC_HAS_CONTENTS field. We force it back on here
3598 if appropriate (which it normally will be). */
3599 origflags = msec->flags;
3600 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3601 msec->flags |= SEC_HAS_CONTENTS;
3603 fi = elf_tdata (abfd)->find_line_info;
3606 bfd_size_type external_fdr_size;
3609 struct fdr *fdr_ptr;
3611 fi = ((struct mips_elf_find_line *)
3612 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3615 msec->flags = origflags;
3619 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3621 msec->flags = origflags;
3625 /* Swap in the FDR information. */
3626 fi->d.fdr = ((struct fdr *)
3628 (fi->d.symbolic_header.ifdMax *
3629 sizeof (struct fdr))));
3630 if (fi->d.fdr == NULL)
3632 msec->flags = origflags;
3635 external_fdr_size = swap->external_fdr_size;
3636 fdr_ptr = fi->d.fdr;
3637 fraw_src = (char *) fi->d.external_fdr;
3638 fraw_end = (fraw_src
3639 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3640 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3641 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3643 elf_tdata (abfd)->find_line_info = fi;
3645 /* Note that we don't bother to ever free this information.
3646 find_nearest_line is either called all the time, as in
3647 objdump -l, so the information should be saved, or it is
3648 rarely called, as in ld error messages, so the memory
3649 wasted is unimportant. Still, it would probably be a
3650 good idea for free_cached_info to throw it away. */
3653 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3654 &fi->i, filename_ptr, functionname_ptr,
3657 msec->flags = origflags;
3661 msec->flags = origflags;
3664 /* Fall back on the generic ELF find_nearest_line routine. */
3666 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3667 filename_ptr, functionname_ptr,
3671 /* The mips16 compiler uses a couple of special sections to handle
3672 floating point arguments.
3674 Section names that look like .mips16.fn.FNNAME contain stubs that
3675 copy floating point arguments from the fp regs to the gp regs and
3676 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3677 call should be redirected to the stub instead. If no 32 bit
3678 function calls FNNAME, the stub should be discarded. We need to
3679 consider any reference to the function, not just a call, because
3680 if the address of the function is taken we will need the stub,
3681 since the address might be passed to a 32 bit function.
3683 Section names that look like .mips16.call.FNNAME contain stubs
3684 that copy floating point arguments from the gp regs to the fp
3685 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3686 then any 16 bit function that calls FNNAME should be redirected
3687 to the stub instead. If FNNAME is not a 32 bit function, the
3688 stub should be discarded.
3690 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3691 which call FNNAME and then copy the return value from the fp regs
3692 to the gp regs. These stubs store the return value in $18 while
3693 calling FNNAME; any function which might call one of these stubs
3694 must arrange to save $18 around the call. (This case is not
3695 needed for 32 bit functions that call 16 bit functions, because
3696 16 bit functions always return floating point values in both
3699 Note that in all cases FNNAME might be defined statically.
3700 Therefore, FNNAME is not used literally. Instead, the relocation
3701 information will indicate which symbol the section is for.
3703 We record any stubs that we find in the symbol table. */
3705 #define FN_STUB ".mips16.fn."
3706 #define CALL_STUB ".mips16.call."
3707 #define CALL_FP_STUB ".mips16.call.fp."
3709 /* MIPS ELF linker hash table. */
3711 struct mips_elf_link_hash_table
3713 struct elf_link_hash_table root;
3715 /* We no longer use this. */
3716 /* String section indices for the dynamic section symbols. */
3717 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3719 /* The number of .rtproc entries. */
3720 bfd_size_type procedure_count;
3721 /* The size of the .compact_rel section (if SGI_COMPAT). */
3722 bfd_size_type compact_rel_size;
3723 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3724 entry is set to the address of __rld_obj_head as in Irix 5. */
3725 boolean use_rld_obj_head;
3726 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3728 /* This is set if we see any mips16 stub sections. */
3729 boolean mips16_stubs_seen;
3732 /* Look up an entry in a MIPS ELF linker hash table. */
3734 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3735 ((struct mips_elf_link_hash_entry *) \
3736 elf_link_hash_lookup (&(table)->root, (string), (create), \
3739 /* Traverse a MIPS ELF linker hash table. */
3741 #define mips_elf_link_hash_traverse(table, func, info) \
3742 (elf_link_hash_traverse \
3744 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3747 /* Get the MIPS ELF linker hash table from a link_info structure. */
3749 #define mips_elf_hash_table(p) \
3750 ((struct mips_elf_link_hash_table *) ((p)->hash))
3752 static boolean mips_elf_output_extsym
3753 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3755 /* Create an entry in a MIPS ELF linker hash table. */
3757 static struct bfd_hash_entry *
3758 mips_elf_link_hash_newfunc (entry, table, string)
3759 struct bfd_hash_entry *entry;
3760 struct bfd_hash_table *table;
3763 struct mips_elf_link_hash_entry *ret =
3764 (struct mips_elf_link_hash_entry *) entry;
3766 /* Allocate the structure if it has not already been allocated by a
3768 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3769 ret = ((struct mips_elf_link_hash_entry *)
3770 bfd_hash_allocate (table,
3771 sizeof (struct mips_elf_link_hash_entry)));
3772 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3773 return (struct bfd_hash_entry *) ret;
3775 /* Call the allocation method of the superclass. */
3776 ret = ((struct mips_elf_link_hash_entry *)
3777 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3779 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3781 /* Set local fields. */
3782 memset (&ret->esym, 0, sizeof (EXTR));
3783 /* We use -2 as a marker to indicate that the information has
3784 not been set. -1 means there is no associated ifd. */
3786 ret->possibly_dynamic_relocs = 0;
3787 ret->min_dyn_reloc_index = 0;
3788 ret->fn_stub = NULL;
3789 ret->need_fn_stub = false;
3790 ret->call_stub = NULL;
3791 ret->call_fp_stub = NULL;
3794 return (struct bfd_hash_entry *) ret;
3797 /* Create a MIPS ELF linker hash table. */
3799 struct bfd_link_hash_table *
3800 _bfd_mips_elf_link_hash_table_create (abfd)
3803 struct mips_elf_link_hash_table *ret;
3805 ret = ((struct mips_elf_link_hash_table *)
3806 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3807 if (ret == (struct mips_elf_link_hash_table *) NULL)
3810 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3811 mips_elf_link_hash_newfunc))
3813 bfd_release (abfd, ret);
3818 /* We no longer use this. */
3819 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3820 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3822 ret->procedure_count = 0;
3823 ret->compact_rel_size = 0;
3824 ret->use_rld_obj_head = false;
3826 ret->mips16_stubs_seen = false;
3828 return &ret->root.root;
3831 /* Hook called by the linker routine which adds symbols from an object
3832 file. We must handle the special MIPS section numbers here. */
3836 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3838 struct bfd_link_info *info;
3839 const Elf_Internal_Sym *sym;
3841 flagword *flagsp ATTRIBUTE_UNUSED;
3845 if (SGI_COMPAT (abfd)
3846 && (abfd->flags & DYNAMIC) != 0
3847 && strcmp (*namep, "_rld_new_interface") == 0)
3849 /* Skip Irix 5 rld entry name. */
3854 switch (sym->st_shndx)
3857 /* Common symbols less than the GP size are automatically
3858 treated as SHN_MIPS_SCOMMON symbols. */
3859 if (sym->st_size > elf_gp_size (abfd)
3860 || IRIX_COMPAT (abfd) == ict_irix6)
3863 case SHN_MIPS_SCOMMON:
3864 *secp = bfd_make_section_old_way (abfd, ".scommon");
3865 (*secp)->flags |= SEC_IS_COMMON;
3866 *valp = sym->st_size;
3870 /* This section is used in a shared object. */
3871 if (mips_elf_text_section_ptr == NULL)
3873 /* Initialize the section. */
3874 mips_elf_text_section.name = ".text";
3875 mips_elf_text_section.flags = SEC_NO_FLAGS;
3876 mips_elf_text_section.output_section = NULL;
3877 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3878 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3879 mips_elf_text_symbol.name = ".text";
3880 mips_elf_text_symbol.flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3881 mips_elf_text_symbol.section = &mips_elf_text_section;
3882 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3883 mips_elf_text_section_ptr = &mips_elf_text_section;
3885 /* This code used to do *secp = bfd_und_section_ptr if
3886 info->shared. I don't know why, and that doesn't make sense,
3887 so I took it out. */
3888 *secp = mips_elf_text_section_ptr;
3891 case SHN_MIPS_ACOMMON:
3892 /* Fall through. XXX Can we treat this as allocated data? */
3894 /* This section is used in a shared object. */
3895 if (mips_elf_data_section_ptr == NULL)
3897 /* Initialize the section. */
3898 mips_elf_data_section.name = ".data";
3899 mips_elf_data_section.flags = SEC_NO_FLAGS;
3900 mips_elf_data_section.output_section = NULL;
3901 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3902 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3903 mips_elf_data_symbol.name = ".data";
3904 mips_elf_data_symbol.flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3905 mips_elf_data_symbol.section = &mips_elf_data_section;
3906 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3907 mips_elf_data_section_ptr = &mips_elf_data_section;
3909 /* This code used to do *secp = bfd_und_section_ptr if
3910 info->shared. I don't know why, and that doesn't make sense,
3911 so I took it out. */
3912 *secp = mips_elf_data_section_ptr;
3915 case SHN_MIPS_SUNDEFINED:
3916 *secp = bfd_und_section_ptr;
3920 if (SGI_COMPAT (abfd)
3922 && info->hash->creator == abfd->xvec
3923 && strcmp (*namep, "__rld_obj_head") == 0)
3925 struct elf_link_hash_entry *h;
3927 /* Mark __rld_obj_head as dynamic. */
3929 if (! (_bfd_generic_link_add_one_symbol
3930 (info, abfd, *namep, BSF_GLOBAL, *secp,
3931 (bfd_vma) *valp, (const char *) NULL, false,
3932 get_elf_backend_data (abfd)->collect,
3933 (struct bfd_link_hash_entry **) &h)))
3935 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3936 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3937 h->type = STT_OBJECT;
3939 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3942 mips_elf_hash_table (info)->use_rld_obj_head = true;
3945 /* If this is a mips16 text symbol, add 1 to the value to make it
3946 odd. This will cause something like .word SYM to come up with
3947 the right value when it is loaded into the PC. */
3948 if (sym->st_other == STO_MIPS16)
3954 /* Structure used to pass information to mips_elf_output_extsym. */
3959 struct bfd_link_info *info;
3960 struct ecoff_debug_info *debug;
3961 const struct ecoff_debug_swap *swap;
3965 /* This routine is used to write out ECOFF debugging external symbol
3966 information. It is called via mips_elf_link_hash_traverse. The
3967 ECOFF external symbol information must match the ELF external
3968 symbol information. Unfortunately, at this point we don't know
3969 whether a symbol is required by reloc information, so the two
3970 tables may wind up being different. We must sort out the external
3971 symbol information before we can set the final size of the .mdebug
3972 section, and we must set the size of the .mdebug section before we
3973 can relocate any sections, and we can't know which symbols are
3974 required by relocation until we relocate the sections.
3975 Fortunately, it is relatively unlikely that any symbol will be
3976 stripped but required by a reloc. In particular, it can not happen
3977 when generating a final executable. */
3980 mips_elf_output_extsym (h, data)
3981 struct mips_elf_link_hash_entry *h;
3984 struct extsym_info *einfo = (struct extsym_info *) data;
3986 asection *sec, *output_section;
3988 if (h->root.indx == -2)
3990 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3991 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3992 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3993 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3995 else if (einfo->info->strip == strip_all
3996 || (einfo->info->strip == strip_some
3997 && bfd_hash_lookup (einfo->info->keep_hash,
3998 h->root.root.root.string,
3999 false, false) == NULL))
4007 if (h->esym.ifd == -2)
4010 h->esym.cobol_main = 0;
4011 h->esym.weakext = 0;
4012 h->esym.reserved = 0;
4013 h->esym.ifd = ifdNil;
4014 h->esym.asym.value = 0;
4015 h->esym.asym.st = stGlobal;
4017 if (SGI_COMPAT (einfo->abfd)
4018 && (h->root.root.type == bfd_link_hash_undefined
4019 || h->root.root.type == bfd_link_hash_undefweak))
4023 /* Use undefined class. Also, set class and type for some
4025 name = h->root.root.root.string;
4026 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4027 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4029 h->esym.asym.sc = scData;
4030 h->esym.asym.st = stLabel;
4031 h->esym.asym.value = 0;
4033 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4035 h->esym.asym.sc = scAbs;
4036 h->esym.asym.st = stLabel;
4037 h->esym.asym.value =
4038 mips_elf_hash_table (einfo->info)->procedure_count;
4040 else if (strcmp (name, "_gp_disp") == 0)
4042 h->esym.asym.sc = scAbs;
4043 h->esym.asym.st = stLabel;
4044 h->esym.asym.value = elf_gp (einfo->abfd);
4047 h->esym.asym.sc = scUndefined;
4049 else if (h->root.root.type != bfd_link_hash_defined
4050 && h->root.root.type != bfd_link_hash_defweak)
4051 h->esym.asym.sc = scAbs;
4056 sec = h->root.root.u.def.section;
4057 output_section = sec->output_section;
4059 /* When making a shared library and symbol h is the one from
4060 the another shared library, OUTPUT_SECTION may be null. */
4061 if (output_section == NULL)
4062 h->esym.asym.sc = scUndefined;
4065 name = bfd_section_name (output_section->owner, output_section);
4067 if (strcmp (name, ".text") == 0)
4068 h->esym.asym.sc = scText;
4069 else if (strcmp (name, ".data") == 0)
4070 h->esym.asym.sc = scData;
4071 else if (strcmp (name, ".sdata") == 0)
4072 h->esym.asym.sc = scSData;
4073 else if (strcmp (name, ".rodata") == 0
4074 || strcmp (name, ".rdata") == 0)
4075 h->esym.asym.sc = scRData;
4076 else if (strcmp (name, ".bss") == 0)
4077 h->esym.asym.sc = scBss;
4078 else if (strcmp (name, ".sbss") == 0)
4079 h->esym.asym.sc = scSBss;
4080 else if (strcmp (name, ".init") == 0)
4081 h->esym.asym.sc = scInit;
4082 else if (strcmp (name, ".fini") == 0)
4083 h->esym.asym.sc = scFini;
4085 h->esym.asym.sc = scAbs;
4089 h->esym.asym.reserved = 0;
4090 h->esym.asym.index = indexNil;
4093 if (h->root.root.type == bfd_link_hash_common)
4094 h->esym.asym.value = h->root.root.u.c.size;
4095 else if (h->root.root.type == bfd_link_hash_defined
4096 || h->root.root.type == bfd_link_hash_defweak)
4098 if (h->esym.asym.sc == scCommon)
4099 h->esym.asym.sc = scBss;
4100 else if (h->esym.asym.sc == scSCommon)
4101 h->esym.asym.sc = scSBss;
4103 sec = h->root.root.u.def.section;
4104 output_section = sec->output_section;
4105 if (output_section != NULL)
4106 h->esym.asym.value = (h->root.root.u.def.value
4107 + sec->output_offset
4108 + output_section->vma);
4110 h->esym.asym.value = 0;
4112 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4114 /* Set type and value for a symbol with a function stub. */
4115 h->esym.asym.st = stProc;
4116 sec = h->root.root.u.def.section;
4118 h->esym.asym.value = 0;
4121 output_section = sec->output_section;
4122 if (output_section != NULL)
4123 h->esym.asym.value = (h->root.plt.offset
4124 + sec->output_offset
4125 + output_section->vma);
4127 h->esym.asym.value = 0;
4134 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4135 h->root.root.root.string,
4138 einfo->failed = true;
4145 /* Create a runtime procedure table from the .mdebug section. */
4148 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4151 struct bfd_link_info *info;
4153 struct ecoff_debug_info *debug;
4155 const struct ecoff_debug_swap *swap;
4156 HDRR *hdr = &debug->symbolic_header;
4158 struct rpdr_ext *erp;
4160 struct pdr_ext *epdr;
4161 struct sym_ext *esym;
4164 unsigned long size, count;
4165 unsigned long sindex;
4169 const char *no_name_func = _("static procedure (no name)");
4177 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4179 sindex = strlen (no_name_func) + 1;
4180 count = hdr->ipdMax;
4183 size = swap->external_pdr_size;
4185 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4189 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4192 size = sizeof (RPDR);
4193 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4197 sv = (char **) bfd_malloc (sizeof (char *) * count);
4201 count = hdr->isymMax;
4202 size = swap->external_sym_size;
4203 esym = (struct sym_ext *) bfd_malloc (size * count);
4207 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4210 count = hdr->issMax;
4211 ss = (char *) bfd_malloc (count);
4214 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4217 count = hdr->ipdMax;
4218 for (i = 0; i < count; i++, rp++)
4220 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4221 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4222 rp->adr = sym.value;
4223 rp->regmask = pdr.regmask;
4224 rp->regoffset = pdr.regoffset;
4225 rp->fregmask = pdr.fregmask;
4226 rp->fregoffset = pdr.fregoffset;
4227 rp->frameoffset = pdr.frameoffset;
4228 rp->framereg = pdr.framereg;
4229 rp->pcreg = pdr.pcreg;
4231 sv[i] = ss + sym.iss;
4232 sindex += strlen (sv[i]) + 1;
4236 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4237 size = BFD_ALIGN (size, 16);
4238 rtproc = (PTR) bfd_alloc (abfd, size);
4241 mips_elf_hash_table (info)->procedure_count = 0;
4245 mips_elf_hash_table (info)->procedure_count = count + 2;
4247 erp = (struct rpdr_ext *) rtproc;
4248 memset (erp, 0, sizeof (struct rpdr_ext));
4250 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4251 strcpy (str, no_name_func);
4252 str += strlen (no_name_func) + 1;
4253 for (i = 0; i < count; i++)
4255 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4256 strcpy (str, sv[i]);
4257 str += strlen (sv[i]) + 1;
4259 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
4261 /* Set the size and contents of .rtproc section. */
4262 s->_raw_size = size;
4263 s->contents = (bfd_byte *) rtproc;
4265 /* Skip this section later on (I don't think this currently
4266 matters, but someday it might). */
4267 s->link_order_head = (struct bfd_link_order *) NULL;
4296 /* A comparison routine used to sort .gptab entries. */
4299 gptab_compare (p1, p2)
4303 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4304 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4306 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4309 /* We need to use a special link routine to handle the .reginfo and
4310 the .mdebug sections. We need to merge all instances of these
4311 sections together, not write them all out sequentially. */
4314 _bfd_mips_elf_final_link (abfd, info)
4316 struct bfd_link_info *info;
4320 struct bfd_link_order *p;
4321 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4322 asection *rtproc_sec;
4323 Elf32_RegInfo reginfo;
4324 struct ecoff_debug_info debug;
4325 const struct ecoff_debug_swap *swap
4326 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4327 HDRR *symhdr = &debug.symbolic_header;
4328 PTR mdebug_handle = NULL;
4330 /* If all the things we linked together were PIC, but we're
4331 producing an executable (rather than a shared object), then the
4332 resulting file is CPIC (i.e., it calls PIC code.) */
4334 && !info->relocateable
4335 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4337 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4338 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4341 /* We'd carefully arranged the dynamic symbol indices, and then the
4342 generic size_dynamic_sections renumbered them out from under us.
4343 Rather than trying somehow to prevent the renumbering, just do
4345 if (elf_hash_table (info)->dynamic_sections_created)
4349 struct mips_got_info *g;
4351 /* When we resort, we must tell mips_elf_sort_hash_table what
4352 the lowest index it may use is. That's the number of section
4353 symbols we're going to add. The generic ELF linker only
4354 adds these symbols when building a shared object. Note that
4355 we count the sections after (possibly) removing the .options
4357 if (!mips_elf_sort_hash_table (info, (info->shared
4358 ? bfd_count_sections (abfd) + 1
4362 /* Make sure we didn't grow the global .got region. */
4363 dynobj = elf_hash_table (info)->dynobj;
4364 got = bfd_get_section_by_name (dynobj, ".got");
4365 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4367 if (g->global_gotsym != NULL)
4368 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
4369 - g->global_gotsym->dynindx)
4370 <= g->global_gotno);
4373 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4374 include it, even though we don't process it quite right. (Some
4375 entries are supposed to be merged.) Empirically, we seem to be
4376 better off including it then not. */
4377 if (IRIX_COMPAT (abfd) == ict_irix5)
4378 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4380 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4382 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4383 if (p->type == bfd_indirect_link_order)
4384 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4385 (*secpp)->link_order_head = NULL;
4386 *secpp = (*secpp)->next;
4387 --abfd->section_count;
4393 /* Get a value for the GP register. */
4394 if (elf_gp (abfd) == 0)
4396 struct bfd_link_hash_entry *h;
4398 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4399 if (h != (struct bfd_link_hash_entry *) NULL
4400 && h->type == bfd_link_hash_defined)
4401 elf_gp (abfd) = (h->u.def.value
4402 + h->u.def.section->output_section->vma
4403 + h->u.def.section->output_offset);
4404 else if (info->relocateable)
4408 /* Find the GP-relative section with the lowest offset. */
4410 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4412 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4415 /* And calculate GP relative to that. */
4416 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4420 /* If the relocate_section function needs to do a reloc
4421 involving the GP value, it should make a reloc_dangerous
4422 callback to warn that GP is not defined. */
4426 /* Go through the sections and collect the .reginfo and .mdebug
4430 gptab_data_sec = NULL;
4431 gptab_bss_sec = NULL;
4432 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4434 if (strcmp (o->name, ".reginfo") == 0)
4436 memset (®info, 0, sizeof reginfo);
4438 /* We have found the .reginfo section in the output file.
4439 Look through all the link_orders comprising it and merge
4440 the information together. */
4441 for (p = o->link_order_head;
4442 p != (struct bfd_link_order *) NULL;
4445 asection *input_section;
4447 Elf32_External_RegInfo ext;
4450 if (p->type != bfd_indirect_link_order)
4452 if (p->type == bfd_fill_link_order)
4457 input_section = p->u.indirect.section;
4458 input_bfd = input_section->owner;
4460 /* The linker emulation code has probably clobbered the
4461 size to be zero bytes. */
4462 if (input_section->_raw_size == 0)
4463 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4465 if (! bfd_get_section_contents (input_bfd, input_section,
4471 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4473 reginfo.ri_gprmask |= sub.ri_gprmask;
4474 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4475 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4476 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4477 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4479 /* ri_gp_value is set by the function
4480 mips_elf32_section_processing when the section is
4481 finally written out. */
4483 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4484 elf_link_input_bfd ignores this section. */
4485 input_section->flags &=~ SEC_HAS_CONTENTS;
4488 /* Size has been set in mips_elf_always_size_sections */
4489 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4491 /* Skip this section later on (I don't think this currently
4492 matters, but someday it might). */
4493 o->link_order_head = (struct bfd_link_order *) NULL;
4498 if (strcmp (o->name, ".mdebug") == 0)
4500 struct extsym_info einfo;
4502 /* We have found the .mdebug section in the output file.
4503 Look through all the link_orders comprising it and merge
4504 the information together. */
4505 symhdr->magic = swap->sym_magic;
4506 /* FIXME: What should the version stamp be? */
4508 symhdr->ilineMax = 0;
4512 symhdr->isymMax = 0;
4513 symhdr->ioptMax = 0;
4514 symhdr->iauxMax = 0;
4516 symhdr->issExtMax = 0;
4519 symhdr->iextMax = 0;
4521 /* We accumulate the debugging information itself in the
4522 debug_info structure. */
4524 debug.external_dnr = NULL;
4525 debug.external_pdr = NULL;
4526 debug.external_sym = NULL;
4527 debug.external_opt = NULL;
4528 debug.external_aux = NULL;
4530 debug.ssext = debug.ssext_end = NULL;
4531 debug.external_fdr = NULL;
4532 debug.external_rfd = NULL;
4533 debug.external_ext = debug.external_ext_end = NULL;
4535 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4536 if (mdebug_handle == (PTR) NULL)
4539 if (SGI_COMPAT (abfd))
4545 static const char * const name[] =
4546 { ".text", ".init", ".fini", ".data",
4547 ".rodata", ".sdata", ".sbss", ".bss" };
4548 static const int sc[] = { scText, scInit, scFini, scData,
4549 scRData, scSData, scSBss, scBss };
4552 esym.cobol_main = 0;
4556 esym.asym.iss = issNil;
4557 esym.asym.st = stLocal;
4558 esym.asym.reserved = 0;
4559 esym.asym.index = indexNil;
4561 for (i = 0; i < 8; i++)
4563 esym.asym.sc = sc[i];
4564 s = bfd_get_section_by_name (abfd, name[i]);
4567 esym.asym.value = s->vma;
4568 last = s->vma + s->_raw_size;
4571 esym.asym.value = last;
4573 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4579 for (p = o->link_order_head;
4580 p != (struct bfd_link_order *) NULL;
4583 asection *input_section;
4585 const struct ecoff_debug_swap *input_swap;
4586 struct ecoff_debug_info input_debug;
4590 if (p->type != bfd_indirect_link_order)
4592 if (p->type == bfd_fill_link_order)
4597 input_section = p->u.indirect.section;
4598 input_bfd = input_section->owner;
4600 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4601 || (get_elf_backend_data (input_bfd)
4602 ->elf_backend_ecoff_debug_swap) == NULL)
4604 /* I don't know what a non MIPS ELF bfd would be
4605 doing with a .mdebug section, but I don't really
4606 want to deal with it. */
4610 input_swap = (get_elf_backend_data (input_bfd)
4611 ->elf_backend_ecoff_debug_swap);
4613 BFD_ASSERT (p->size == input_section->_raw_size);
4615 /* The ECOFF linking code expects that we have already
4616 read in the debugging information and set up an
4617 ecoff_debug_info structure, so we do that now. */
4618 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4622 if (! (bfd_ecoff_debug_accumulate
4623 (mdebug_handle, abfd, &debug, swap, input_bfd,
4624 &input_debug, input_swap, info)))
4627 /* Loop through the external symbols. For each one with
4628 interesting information, try to find the symbol in
4629 the linker global hash table and save the information
4630 for the output external symbols. */
4631 eraw_src = input_debug.external_ext;
4632 eraw_end = (eraw_src
4633 + (input_debug.symbolic_header.iextMax
4634 * input_swap->external_ext_size));
4636 eraw_src < eraw_end;
4637 eraw_src += input_swap->external_ext_size)
4641 struct mips_elf_link_hash_entry *h;
4643 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4644 if (ext.asym.sc == scNil
4645 || ext.asym.sc == scUndefined
4646 || ext.asym.sc == scSUndefined)
4649 name = input_debug.ssext + ext.asym.iss;
4650 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4651 name, false, false, true);
4652 if (h == NULL || h->esym.ifd != -2)
4658 < input_debug.symbolic_header.ifdMax);
4659 ext.ifd = input_debug.ifdmap[ext.ifd];
4665 /* Free up the information we just read. */
4666 free (input_debug.line);
4667 free (input_debug.external_dnr);
4668 free (input_debug.external_pdr);
4669 free (input_debug.external_sym);
4670 free (input_debug.external_opt);
4671 free (input_debug.external_aux);
4672 free (input_debug.ss);
4673 free (input_debug.ssext);
4674 free (input_debug.external_fdr);
4675 free (input_debug.external_rfd);
4676 free (input_debug.external_ext);
4678 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4679 elf_link_input_bfd ignores this section. */
4680 input_section->flags &=~ SEC_HAS_CONTENTS;
4683 if (SGI_COMPAT (abfd) && info->shared)
4685 /* Create .rtproc section. */
4686 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4687 if (rtproc_sec == NULL)
4689 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4690 | SEC_LINKER_CREATED | SEC_READONLY);
4692 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4693 if (rtproc_sec == NULL
4694 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4695 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4699 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4700 info, rtproc_sec, &debug))
4704 /* Build the external symbol information. */
4707 einfo.debug = &debug;
4709 einfo.failed = false;
4710 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4711 mips_elf_output_extsym,
4716 /* Set the size of the .mdebug section. */
4717 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4719 /* Skip this section later on (I don't think this currently
4720 matters, but someday it might). */
4721 o->link_order_head = (struct bfd_link_order *) NULL;
4726 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4728 const char *subname;
4731 Elf32_External_gptab *ext_tab;
4734 /* The .gptab.sdata and .gptab.sbss sections hold
4735 information describing how the small data area would
4736 change depending upon the -G switch. These sections
4737 not used in executables files. */
4738 if (! info->relocateable)
4742 for (p = o->link_order_head;
4743 p != (struct bfd_link_order *) NULL;
4746 asection *input_section;
4748 if (p->type != bfd_indirect_link_order)
4750 if (p->type == bfd_fill_link_order)
4755 input_section = p->u.indirect.section;
4757 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4758 elf_link_input_bfd ignores this section. */
4759 input_section->flags &=~ SEC_HAS_CONTENTS;
4762 /* Skip this section later on (I don't think this
4763 currently matters, but someday it might). */
4764 o->link_order_head = (struct bfd_link_order *) NULL;
4766 /* Really remove the section. */
4767 for (secpp = &abfd->sections;
4769 secpp = &(*secpp)->next)
4771 *secpp = (*secpp)->next;
4772 --abfd->section_count;
4777 /* There is one gptab for initialized data, and one for
4778 uninitialized data. */
4779 if (strcmp (o->name, ".gptab.sdata") == 0)
4781 else if (strcmp (o->name, ".gptab.sbss") == 0)
4785 (*_bfd_error_handler)
4786 (_("%s: illegal section name `%s'"),
4787 bfd_get_filename (abfd), o->name);
4788 bfd_set_error (bfd_error_nonrepresentable_section);
4792 /* The linker script always combines .gptab.data and
4793 .gptab.sdata into .gptab.sdata, and likewise for
4794 .gptab.bss and .gptab.sbss. It is possible that there is
4795 no .sdata or .sbss section in the output file, in which
4796 case we must change the name of the output section. */
4797 subname = o->name + sizeof ".gptab" - 1;
4798 if (bfd_get_section_by_name (abfd, subname) == NULL)
4800 if (o == gptab_data_sec)
4801 o->name = ".gptab.data";
4803 o->name = ".gptab.bss";
4804 subname = o->name + sizeof ".gptab" - 1;
4805 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4808 /* Set up the first entry. */
4810 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4813 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4814 tab[0].gt_header.gt_unused = 0;
4816 /* Combine the input sections. */
4817 for (p = o->link_order_head;
4818 p != (struct bfd_link_order *) NULL;
4821 asection *input_section;
4825 bfd_size_type gpentry;
4827 if (p->type != bfd_indirect_link_order)
4829 if (p->type == bfd_fill_link_order)
4834 input_section = p->u.indirect.section;
4835 input_bfd = input_section->owner;
4837 /* Combine the gptab entries for this input section one
4838 by one. We know that the input gptab entries are
4839 sorted by ascending -G value. */
4840 size = bfd_section_size (input_bfd, input_section);
4842 for (gpentry = sizeof (Elf32_External_gptab);
4844 gpentry += sizeof (Elf32_External_gptab))
4846 Elf32_External_gptab ext_gptab;
4847 Elf32_gptab int_gptab;
4853 if (! (bfd_get_section_contents
4854 (input_bfd, input_section, (PTR) &ext_gptab,
4855 gpentry, sizeof (Elf32_External_gptab))))
4861 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4863 val = int_gptab.gt_entry.gt_g_value;
4864 add = int_gptab.gt_entry.gt_bytes - last;
4867 for (look = 1; look < c; look++)
4869 if (tab[look].gt_entry.gt_g_value >= val)
4870 tab[look].gt_entry.gt_bytes += add;
4872 if (tab[look].gt_entry.gt_g_value == val)
4878 Elf32_gptab *new_tab;
4881 /* We need a new table entry. */
4882 new_tab = ((Elf32_gptab *)
4883 bfd_realloc ((PTR) tab,
4884 (c + 1) * sizeof (Elf32_gptab)));
4885 if (new_tab == NULL)
4891 tab[c].gt_entry.gt_g_value = val;
4892 tab[c].gt_entry.gt_bytes = add;
4894 /* Merge in the size for the next smallest -G
4895 value, since that will be implied by this new
4898 for (look = 1; look < c; look++)
4900 if (tab[look].gt_entry.gt_g_value < val
4902 || (tab[look].gt_entry.gt_g_value
4903 > tab[max].gt_entry.gt_g_value)))
4907 tab[c].gt_entry.gt_bytes +=
4908 tab[max].gt_entry.gt_bytes;
4913 last = int_gptab.gt_entry.gt_bytes;
4916 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4917 elf_link_input_bfd ignores this section. */
4918 input_section->flags &=~ SEC_HAS_CONTENTS;
4921 /* The table must be sorted by -G value. */
4923 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4925 /* Swap out the table. */
4926 ext_tab = ((Elf32_External_gptab *)
4927 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4928 if (ext_tab == NULL)
4934 for (i = 0; i < c; i++)
4935 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4938 o->_raw_size = c * sizeof (Elf32_External_gptab);
4939 o->contents = (bfd_byte *) ext_tab;
4941 /* Skip this section later on (I don't think this currently
4942 matters, but someday it might). */
4943 o->link_order_head = (struct bfd_link_order *) NULL;
4947 /* Invoke the regular ELF backend linker to do all the work. */
4948 if (ABI_64_P (abfd))
4951 if (!bfd_elf64_bfd_final_link (abfd, info))
4958 else if (!bfd_elf32_bfd_final_link (abfd, info))
4961 /* Now write out the computed sections. */
4963 if (reginfo_sec != (asection *) NULL)
4965 Elf32_External_RegInfo ext;
4967 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
4968 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4969 (file_ptr) 0, sizeof ext))
4973 if (mdebug_sec != (asection *) NULL)
4975 BFD_ASSERT (abfd->output_has_begun);
4976 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4978 mdebug_sec->filepos))
4981 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4984 if (gptab_data_sec != (asection *) NULL)
4986 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4987 gptab_data_sec->contents,
4989 gptab_data_sec->_raw_size))
4993 if (gptab_bss_sec != (asection *) NULL)
4995 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4996 gptab_bss_sec->contents,
4998 gptab_bss_sec->_raw_size))
5002 if (SGI_COMPAT (abfd))
5004 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
5005 if (rtproc_sec != NULL)
5007 if (! bfd_set_section_contents (abfd, rtproc_sec,
5008 rtproc_sec->contents,
5010 rtproc_sec->_raw_size))
5018 /* Handle a MIPS ELF HI16 reloc. */
5021 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
5023 Elf_Internal_Rela *relhi;
5024 Elf_Internal_Rela *rello;
5031 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5033 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5036 addend += ((insn & 0xffff) << 16) + addlo;
5038 if ((addlo & 0x8000) != 0)
5040 if ((addend & 0x8000) != 0)
5043 bfd_put_32 (input_bfd,
5044 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
5045 contents + relhi->r_offset);
5048 /* Handle a MIPS ELF local GOT16 reloc. */
5051 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
5056 Elf_Internal_Rela *relhi;
5057 Elf_Internal_Rela *rello;
5061 unsigned int assigned_gotno;
5067 bfd_byte *got_contents;
5068 struct mips_got_info *g;
5070 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5072 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5075 addend += ((insn & 0xffff) << 16) + addlo;
5077 if ((addlo & 0x8000) != 0)
5079 if ((addend & 0x8000) != 0)
5082 /* Get a got entry representing requested hipage. */
5083 BFD_ASSERT (elf_section_data (sgot) != NULL);
5084 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5085 BFD_ASSERT (g != NULL);
5087 assigned_gotno = g->assigned_gotno;
5088 got_contents = sgot->contents;
5089 hipage = addend & 0xffff0000;
5091 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
5093 address = bfd_get_32 (input_bfd, got_contents + i * 4);
5094 if (hipage == (address & 0xffff0000))
5098 if (i == assigned_gotno)
5100 if (assigned_gotno >= g->local_gotno)
5102 (*_bfd_error_handler)
5103 (_("more got entries are needed for hipage relocations"));
5104 bfd_set_error (bfd_error_bad_value);
5108 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
5109 ++g->assigned_gotno;
5112 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
5113 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
5114 contents + relhi->r_offset);
5119 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
5122 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
5124 Elf_Internal_Rela *rel;
5130 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5131 bfd_put_32 (input_bfd,
5132 (insn & 0xffff0000) | (offset & 0xffff),
5133 contents + rel->r_offset);
5136 /* Returns the GOT section for ABFD. */
5139 mips_elf_got_section (abfd)
5142 return bfd_get_section_by_name (abfd, ".got");
5145 /* Returns the GOT information associated with the link indicated by
5146 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5149 static struct mips_got_info *
5150 mips_elf_got_info (abfd, sgotp)
5155 struct mips_got_info *g;
5157 sgot = mips_elf_got_section (abfd);
5158 BFD_ASSERT (sgot != NULL);
5159 BFD_ASSERT (elf_section_data (sgot) != NULL);
5160 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5161 BFD_ASSERT (g != NULL);
5168 /* Return whether a relocation is against a local symbol. */
5171 mips_elf_local_relocation_p (input_bfd, relocation, local_sections)
5173 const Elf_Internal_Rela *relocation;
5174 asection **local_sections;
5176 unsigned long r_symndx;
5177 Elf_Internal_Shdr *symtab_hdr;
5179 r_symndx = ELF32_R_SYM (relocation->r_info);
5180 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5181 if (! elf_bad_symtab (input_bfd))
5182 return r_symndx < symtab_hdr->sh_info;
5185 /* The symbol table does not follow the rule that local symbols
5186 must come before globals. */
5187 return local_sections[r_symndx] != NULL;
5191 /* Sign-extend VALUE, which has the indicated number of BITS. */
5194 mips_elf_sign_extend (value, bits)
5198 if (value & (1 << (bits - 1)))
5199 /* VALUE is negative. */
5200 value |= ((bfd_vma) - 1) << bits;
5205 /* Return non-zero if the indicated VALUE has overflowed the maximum
5206 range expressable by a signed number with the indicated number of
5210 mips_elf_overflow_p (value, bits)
5214 bfd_signed_vma svalue = (bfd_signed_vma) value;
5216 if (svalue > (1 << (bits - 1)) - 1)
5217 /* The value is too big. */
5219 else if (svalue < -(1 << (bits - 1)))
5220 /* The value is too small. */
5227 /* Calculate the %high function. */
5230 mips_elf_high (value)
5233 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5236 /* Calculate the %higher function. */
5239 mips_elf_higher (value)
5240 bfd_vma value ATTRIBUTE_UNUSED;
5243 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5246 return (bfd_vma) -1;
5250 /* Calculate the %highest function. */
5253 mips_elf_highest (value)
5254 bfd_vma value ATTRIBUTE_UNUSED;
5257 return ((value + (bfd_vma) 0x800080008000) > 48) & 0xffff;
5260 return (bfd_vma) -1;
5264 /* Returns the GOT index for the global symbol indicated by H. */
5267 mips_elf_global_got_index (abfd, h)
5269 struct elf_link_hash_entry *h;
5273 struct mips_got_info *g;
5275 g = mips_elf_got_info (abfd, &sgot);
5277 /* Once we determine the global GOT entry with the lowest dynamic
5278 symbol table index, we must put all dynamic symbols with greater
5279 indices into the GOT. That makes it easy to calculate the GOT
5281 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5282 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5283 * MIPS_ELF_GOT_SIZE (abfd));
5284 BFD_ASSERT (index < sgot->_raw_size);
5289 /* Returns the offset for the entry at the INDEXth position
5293 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5301 sgot = mips_elf_got_section (dynobj);
5302 gp = _bfd_get_gp_value (output_bfd);
5303 return (sgot->output_section->vma + sgot->output_offset + index -
5307 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5308 symbol table index lower than any we've seen to date, record it for
5312 mips_elf_record_global_got_symbol (h, info, g)
5313 struct elf_link_hash_entry *h;
5314 struct bfd_link_info *info;
5315 struct mips_got_info *g ATTRIBUTE_UNUSED;
5317 /* A global symbol in the GOT must also be in the dynamic symbol
5319 if (h->dynindx == -1
5320 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5323 /* If we've already marked this entry as need GOT space, we don't
5324 need to do it again. */
5325 if (h->got.offset != (bfd_vma) - 1)
5328 /* By setting this to a value other than -1, we are indicating that
5329 there needs to be a GOT entry for H. */
5335 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5336 the dynamic symbols. */
5338 struct mips_elf_hash_sort_data
5340 /* The symbol in the global GOT with the lowest dynamic symbol table
5342 struct elf_link_hash_entry *low;
5343 /* The least dynamic symbol table index corresponding to a symbol
5344 with a GOT entry. */
5345 long min_got_dynindx;
5346 /* The greatest dynamic symbol table index not corresponding to a
5347 symbol without a GOT entry. */
5348 long max_non_got_dynindx;
5351 /* If H needs a GOT entry, assign it the highest available dynamic
5352 index. Otherwise, assign it the lowest available dynamic
5356 mips_elf_sort_hash_table_f (h, data)
5357 struct mips_elf_link_hash_entry *h;
5360 struct mips_elf_hash_sort_data *hsd
5361 = (struct mips_elf_hash_sort_data *) data;
5363 /* Symbols without dynamic symbol table entries aren't interesting
5365 if (h->root.dynindx == -1)
5368 if (h->root.got.offset != 0)
5369 h->root.dynindx = hsd->max_non_got_dynindx++;
5372 h->root.dynindx = --hsd->min_got_dynindx;
5373 hsd->low = (struct elf_link_hash_entry *) h;
5379 /* Sort the dynamic symbol table so that symbols that need GOT entries
5380 appear towards the end. This reduces the amount of GOT space
5381 required. MAX_LOCAL is used to set the number of local symbols
5382 known to be in the dynamic symbol table. During
5383 mips_elf_size_dynamic_sections, this value is 1. Afterward, the
5384 section symbols are added and the count is higher. */
5387 mips_elf_sort_hash_table (info, max_local)
5388 struct bfd_link_info *info;
5389 unsigned long max_local;
5391 struct mips_elf_hash_sort_data hsd;
5392 struct mips_got_info *g;
5395 dynobj = elf_hash_table (info)->dynobj;
5398 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5399 hsd.max_non_got_dynindx = max_local;
5400 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5401 elf_hash_table (info)),
5402 mips_elf_sort_hash_table_f,
5405 /* There shoud have been enough room in the symbol table to
5406 accomodate both the GOT and non-GOT symbols. */
5407 BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5409 /* Now we know which dynamic symbol has the lowest dynamic symbol
5410 table index in the GOT. */
5411 g = mips_elf_got_info (dynobj, NULL);
5412 g->global_gotsym = hsd.low;
5417 /* Create a local GOT entry for VALUE. Return the index of the entry,
5418 or -1 if it could not be created. */
5421 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5423 struct mips_got_info *g;
5427 if (g->assigned_gotno >= g->local_gotno)
5429 /* We didn't allocate enough space in the GOT. */
5430 (*_bfd_error_handler)
5431 (_("not enough GOT space for local GOT entries"));
5432 bfd_set_error (bfd_error_bad_value);
5433 return (bfd_vma) -1;
5436 MIPS_ELF_PUT_WORD (abfd, value,
5438 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5439 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5442 /* Returns the GOT offset at which the indicated address can be found.
5443 If there is not yet a GOT entry for this value, create one. Returns
5444 -1 if no satisfactory GOT offset can be found. */
5447 mips_elf_local_got_index (abfd, info, value)
5449 struct bfd_link_info *info;
5453 struct mips_got_info *g;
5456 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5458 /* Look to see if we already have an appropriate entry. */
5459 for (entry = (sgot->contents
5460 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5461 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5462 entry += MIPS_ELF_GOT_SIZE (abfd))
5464 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5465 if (address == value)
5466 return entry - sgot->contents;
5469 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5472 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5473 are supposed to be placed at small offsets in the GOT, i.e.,
5474 within 32KB of GP. Return the index into the GOT for this page,
5475 and store the offset from this entry to the desired address in
5476 OFFSETP, if it is non-NULL. */
5479 mips_elf_got_page (abfd, info, value, offsetp)
5481 struct bfd_link_info *info;
5486 struct mips_got_info *g;
5488 bfd_byte *last_entry;
5492 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5494 /* Look to see if we aleady have an appropriate entry. */
5495 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5496 for (entry = (sgot->contents
5497 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5498 entry != last_entry;
5499 entry += MIPS_ELF_GOT_SIZE (abfd))
5501 address = MIPS_ELF_GET_WORD (abfd, entry);
5503 if (!mips_elf_overflow_p (value - address, 16))
5505 /* This entry will serve as the page pointer. We can add a
5506 16-bit number to it to get the actual address. */
5507 index = entry - sgot->contents;
5512 /* If we didn't have an appropriate entry, we create one now. */
5513 if (entry == last_entry)
5514 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5518 address = MIPS_ELF_GET_WORD (abfd, entry);
5519 *offsetp = value - address;
5525 /* Find a GOT entry whose higher-order 16 bits are the same as those
5526 for value. Return the index into the GOT for this entry. */
5529 mips_elf_got16_entry (abfd, info, value)
5531 struct bfd_link_info *info;
5535 struct mips_got_info *g;
5537 bfd_byte *last_entry;
5541 /* Although the ABI says that it is "the high-order 16 bits" that we
5542 want, it is really the %high value. The complete value is
5543 calculated with a `addiu' of a LO16 relocation, just as with a
5545 value = mips_elf_high (value) << 16;
5546 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5548 /* Look to see if we already have an appropriate entry. */
5549 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5550 for (entry = (sgot->contents
5551 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5552 entry != last_entry;
5553 entry += MIPS_ELF_GOT_SIZE (abfd))
5555 address = MIPS_ELF_GET_WORD (abfd, entry);
5556 if ((address & 0xffff0000) == value)
5558 /* This entry has the right high-order 16 bits. */
5559 index = entry - sgot->contents;
5564 /* If we didn't have an appropriate entry, we create one now. */
5565 if (entry == last_entry)
5566 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5571 /* Returns the first R_MIPS_LO16 relocation found, beginning with
5572 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5574 static const Elf_Internal_Rela *
5575 mips_elf_next_lo16_relocation (relocation, relend)
5576 const Elf_Internal_Rela *relocation;
5577 const Elf_Internal_Rela *relend;
5579 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5580 immediately following. However, for the IRIX6 ABI, the next
5581 relocation may be a composed relocation consisting of several
5582 relocations for the same address. In that case, the R_MIPS_LO16
5583 relocation may occur as one of these. We permit a similar
5584 extension in general, as that is useful for GCC. */
5585 while (relocation < relend)
5587 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS_LO16)
5593 /* We didn't find it. */
5594 bfd_set_error (bfd_error_bad_value);
5598 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5599 is the original relocation, which is now being transformed into a
5600 dyanmic relocation. The ADDENDP is adjusted if necessary; the
5601 caller should store the result in place of the original addend. */
5604 mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
5605 symbol, addendp, input_section)
5607 struct bfd_link_info *info;
5608 const Elf_Internal_Rela *rel;
5609 struct mips_elf_link_hash_entry *h;
5613 asection *input_section;
5615 Elf_Internal_Rel outrel;
5621 r_type = ELF32_R_TYPE (rel->r_info);
5622 dynobj = elf_hash_table (info)->dynobj;
5624 = bfd_get_section_by_name (dynobj,
5625 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5626 BFD_ASSERT (sreloc != NULL);
5630 /* We begin by assuming that the offset for the dynamic relocation
5631 is the same as for the original relocation. We'll adjust this
5632 later to reflect the correct output offsets. */
5633 if (elf_section_data (input_section)->stab_info == NULL)
5634 outrel.r_offset = rel->r_offset;
5637 /* Except that in a stab section things are more complex.
5638 Because we compress stab information, the offset given in the
5639 relocation may not be the one we want; we must let the stabs
5640 machinery tell us the offset. */
5642 = (_bfd_stab_section_offset
5643 (output_bfd, &elf_hash_table (info)->stab_info,
5645 &elf_section_data (input_section)->stab_info,
5647 /* If we didn't need the relocation at all, this value will be
5649 if (outrel.r_offset == (bfd_vma) -1)
5653 /* If we've decided to skip this relocation, just output an emtpy
5654 record. Note that R_MIPS_NONE == 0, so that this call to memset
5655 is a way of setting R_TYPE to R_MIPS_NONE. */
5657 memset (&outrel, 0, sizeof (outrel));
5661 bfd_vma section_offset;
5663 /* We must now calculate the dynamic symbol table index to use
5664 in the relocation. */
5666 && (! info->symbolic || (h->root.elf_link_hash_flags
5667 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5669 indx = h->root.dynindx;
5670 BFD_ASSERT (indx != -1);
5674 if (sec != NULL && bfd_is_abs_section (sec))
5676 else if (sec == NULL || sec->owner == NULL)
5678 bfd_set_error (bfd_error_bad_value);
5683 indx = elf_section_data (sec->output_section)->dynindx;
5688 /* Figure out how far the target of the relocation is from
5689 the beginning of its section. */
5690 section_offset = symbol - sec->output_section->vma;
5691 /* The relocation we're building is section-relative.
5692 Therefore, the original addend must be adjusted by the
5694 *addendp += symbol - sec->output_section->vma;
5695 /* Now, the relocation is just against the section. */
5696 symbol = sec->output_section->vma;
5699 /* If the relocation was previously an absolute relocation, we
5700 must adjust it by the value we give it in the dynamic symbol
5702 if (r_type != R_MIPS_REL32)
5705 /* The relocation is always an REL32 relocation because we don't
5706 know where the shared library will wind up at load-time. */
5707 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5709 /* Adjust the output offset of the relocation to reference the
5710 correct location in the output file. */
5711 outrel.r_offset += (input_section->output_section->vma
5712 + input_section->output_offset);
5715 /* Put the relocation back out. We have to use the special
5716 relocation outputter in the 64-bit case since the 64-bit
5717 relocation format is non-standard. */
5718 if (ABI_64_P (output_bfd))
5720 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5721 (output_bfd, &outrel,
5723 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5726 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5727 (((Elf32_External_Rel *)
5729 + sreloc->reloc_count));
5731 /* Record the index of the first relocation referencing H. This
5732 information is later emitted in the .msym section. */
5734 && (h->min_dyn_reloc_index == 0
5735 || sreloc->reloc_count < h->min_dyn_reloc_index))
5736 h->min_dyn_reloc_index = sreloc->reloc_count;
5738 /* We've now added another relocation. */
5739 ++sreloc->reloc_count;
5741 /* Make sure the output section is writable. The dynamic linker
5742 will be writing to it. */
5743 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5746 /* On IRIX5, make an entry of compact relocation info. */
5747 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5749 asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5754 Elf32_crinfo cptrel;
5756 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5757 cptrel.vaddr = (rel->r_offset
5758 + input_section->output_section->vma
5759 + input_section->output_offset);
5760 if (r_type == R_MIPS_REL32)
5761 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5763 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5764 mips_elf_set_cr_dist2to (cptrel, 0);
5765 cptrel.konst = *addendp;
5767 cr = (scpt->contents
5768 + sizeof (Elf32_External_compact_rel));
5769 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5770 ((Elf32_External_crinfo *) cr
5771 + scpt->reloc_count));
5772 ++scpt->reloc_count;
5779 /* Calculate the value produced by the RELOCATION (which comes from
5780 the INPUT_BFD). The ADDEND is the addend to use for this
5781 RELOCATION; RELOCATION->R_ADDEND is ignored.
5783 The result of the relocation calculation is stored in VALUEP.
5784 REQUIRE_JALXP indicates whether or not the opcode used with this
5785 relocation must be JALX.
5787 This function returns bfd_reloc_continue if the caller need take no
5788 further action regarding this relocation, bfd_reloc_notsupported if
5789 something goes dramatically wrong, bfd_reloc_overflow if an
5790 overflow occurs, and bfd_reloc_ok to indicate success. */
5792 static bfd_reloc_status_type
5793 mips_elf_calculate_relocation (abfd,
5807 asection *input_section;
5808 struct bfd_link_info *info;
5809 const Elf_Internal_Rela *relocation;
5811 reloc_howto_type *howto;
5812 Elf_Internal_Sym *local_syms;
5813 asection **local_sections;
5816 boolean *require_jalxp;
5818 /* The eventual value we will return. */
5820 /* The address of the symbol against which the relocation is
5823 /* The final GP value to be used for the relocatable, executable, or
5824 shared object file being produced. */
5825 bfd_vma gp = (bfd_vma) - 1;
5826 /* The place (section offset or address) of the storage unit being
5829 /* The value of GP used to create the relocatable object. */
5830 bfd_vma gp0 = (bfd_vma) - 1;
5831 /* The offset into the global offset table at which the address of
5832 the relocation entry symbol, adjusted by the addend, resides
5833 during execution. */
5834 bfd_vma g = (bfd_vma) - 1;
5835 /* The section in which the symbol referenced by the relocation is
5837 asection *sec = NULL;
5838 struct mips_elf_link_hash_entry* h = NULL;
5839 /* True if the symbol referred to by this relocation is a local
5842 /* True if the symbol referred to by this relocation is "_gp_disp". */
5843 boolean gp_disp_p = false;
5844 Elf_Internal_Shdr *symtab_hdr;
5846 unsigned long r_symndx;
5848 /* True if overflow occurred during the calculation of the
5849 relocation value. */
5850 boolean overflowed_p;
5851 /* True if this relocation refers to a MIPS16 function. */
5852 boolean target_is_16_bit_code_p = false;
5854 /* Parse the relocation. */
5855 r_symndx = ELF32_R_SYM (relocation->r_info);
5856 r_type = ELF32_R_TYPE (relocation->r_info);
5857 p = (input_section->output_section->vma
5858 + input_section->output_offset
5859 + relocation->r_offset);
5861 /* Assume that there will be no overflow. */
5862 overflowed_p = false;
5864 /* Figure out whether or not the symbol is local, and get the offset
5865 used in the array of hash table entries. */
5866 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5867 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5869 if (! elf_bad_symtab (input_bfd))
5870 extsymoff = symtab_hdr->sh_info;
5873 /* The symbol table does not follow the rule that local symbols
5874 must come before globals. */
5878 /* Figure out the value of the symbol. */
5881 Elf_Internal_Sym *sym;
5883 sym = local_syms + r_symndx;
5884 sec = local_sections[r_symndx];
5886 symbol = sec->output_section->vma + sec->output_offset;
5887 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5888 symbol += sym->st_value;
5890 /* MIPS16 text labels should be treated as odd. */
5891 if (sym->st_other == STO_MIPS16)
5894 /* Record the name of this symbol, for our caller. */
5895 *namep = bfd_elf_string_from_elf_section (input_bfd,
5896 symtab_hdr->sh_link,
5899 *namep = bfd_section_name (input_bfd, sec);
5901 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
5905 /* For global symbols we look up the symbol in the hash-table. */
5906 h = ((struct mips_elf_link_hash_entry *)
5907 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5908 /* Find the real hash-table entry for this symbol. */
5909 while (h->root.type == bfd_link_hash_indirect
5910 || h->root.type == bfd_link_hash_warning)
5911 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5913 /* Record the name of this symbol, for our caller. */
5914 *namep = h->root.root.root.string;
5916 /* See if this is the special _gp_disp symbol. Note that such a
5917 symbol must always be a global symbol. */
5918 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5920 /* Relocations against _gp_disp are permitted only with
5921 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5922 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5923 return bfd_reloc_notsupported;
5927 /* If this symbol is defined, calculate its address. Note that
5928 _gp_disp is a magic symbol, always implicitly defined by the
5929 linker, so it's inappropriate to check to see whether or not
5931 else if ((h->root.root.type == bfd_link_hash_defined
5932 || h->root.root.type == bfd_link_hash_defweak)
5933 && h->root.root.u.def.section)
5935 sec = h->root.root.u.def.section;
5936 if (sec->output_section)
5937 symbol = (h->root.root.u.def.value
5938 + sec->output_section->vma
5939 + sec->output_offset);
5941 symbol = h->root.root.u.def.value;
5943 else if (h->root.root.type == bfd_link_hash_undefweak)
5944 /* We allow relocations against undefined weak symbols, giving
5945 it the value zero, so that you can undefined weak functions
5946 and check to see if they exist by looking at their
5949 else if (info->shared && !info->symbolic && !info->no_undefined)
5951 else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0)
5953 /* If this is a dynamic link, we should have created a
5954 _DYNAMIC_LINK symbol in mips_elf_create_dynamic_sections.
5955 Otherwise, we should define the symbol with a value of 0.
5956 FIXME: It should probably get into the symbol table
5958 BFD_ASSERT (! info->shared);
5959 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5964 (*info->callbacks->undefined_symbol)
5965 (info, h->root.root.root.string, input_bfd,
5966 input_section, relocation->r_offset);
5967 return bfd_reloc_undefined;
5970 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
5973 /* If this is a 32-bit call to a 16-bit function with a stub, we
5974 need to redirect the call to the stub, unless we're already *in*
5976 if (r_type != R_MIPS16_26 && !info->relocateable
5977 && ((h != NULL && h->fn_stub != NULL)
5978 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
5979 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5980 && !mips_elf_stub_section_p (input_bfd, input_section))
5982 /* This is a 32-bit call to a 16-bit function. We should
5983 have already noticed that we were going to need the
5986 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5989 BFD_ASSERT (h->need_fn_stub);
5993 symbol = sec->output_section->vma + sec->output_offset;
5995 /* If this is a 16-bit call to a 32-bit function with a stub, we
5996 need to redirect the call to the stub. */
5997 else if (r_type == R_MIPS16_26 && !info->relocateable
5999 && (h->call_stub != NULL || h->call_fp_stub != NULL)
6000 && !target_is_16_bit_code_p)
6002 /* If both call_stub and call_fp_stub are defined, we can figure
6003 out which one to use by seeing which one appears in the input
6005 if (h->call_stub != NULL && h->call_fp_stub != NULL)
6010 for (o = input_bfd->sections; o != NULL; o = o->next)
6012 if (strncmp (bfd_get_section_name (input_bfd, o),
6013 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6015 sec = h->call_fp_stub;
6022 else if (h->call_stub != NULL)
6025 sec = h->call_fp_stub;
6027 BFD_ASSERT (sec->_raw_size > 0);
6028 symbol = sec->output_section->vma + sec->output_offset;
6031 /* Calls from 16-bit code to 32-bit code and vice versa require the
6032 special jalx instruction. */
6033 *require_jalxp = (!info->relocateable
6034 && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p));
6036 /* If we haven't already determined the GOT offset, or the GP value,
6037 and we're going to need it, get it now. */
6042 case R_MIPS_GOT_DISP:
6043 case R_MIPS_GOT_HI16:
6044 case R_MIPS_CALL_HI16:
6045 case R_MIPS_GOT_LO16:
6046 case R_MIPS_CALL_LO16:
6047 /* Find the index into the GOT where this value is located. */
6050 BFD_ASSERT (addend == 0);
6051 g = mips_elf_global_got_index
6052 (elf_hash_table (info)->dynobj,
6053 (struct elf_link_hash_entry*) h);
6055 else if (r_type == R_MIPS_GOT16)
6056 /* There's no need to create a local GOT entry here; the
6057 calculation for a local GOT16 entry does not involve G. */
6061 g = mips_elf_local_got_index (abfd, info, symbol + addend);
6062 if (g == (bfd_vma) -1)
6066 /* Convert GOT indices to actual offsets. */
6067 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6073 case R_MIPS_GPREL16:
6074 case R_MIPS_GPREL32:
6075 gp0 = _bfd_get_gp_value (input_bfd);
6076 gp = _bfd_get_gp_value (abfd);
6083 /* Figure out what kind of relocation is being performed. */
6087 return bfd_reloc_continue;
6090 value = symbol + mips_elf_sign_extend (addend, 16);
6091 overflowed_p = mips_elf_overflow_p (value, 16);
6098 || (elf_hash_table (info)->dynamic_sections_created
6100 && ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
6102 && (input_section->flags & SEC_ALLOC) != 0)
6104 /* If we're creating a shared library, or this relocation is
6105 against a symbol in a shared library, then we can't know
6106 where the symbol will end up. So, we create a relocation
6107 record in the output, and leave the job up to the dynamic
6110 if (!mips_elf_create_dynamic_relocation (abfd,
6122 if (r_type != R_MIPS_REL32)
6123 value = symbol + addend;
6127 value &= howto->dst_mask;
6131 /* The calculation for R_MIPS_26 is just the same as for an
6132 R_MIPS_26. It's only the storage of the relocated field into
6133 the output file that's different. That's handled in
6134 mips_elf_perform_relocation. So, we just fall through to the
6135 R_MIPS_26 case here. */
6138 value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
6140 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
6141 value &= howto->dst_mask;
6147 value = mips_elf_high (addend + symbol);
6148 value &= howto->dst_mask;
6152 value = mips_elf_high (addend + gp - p);
6153 overflowed_p = mips_elf_overflow_p (value, 16);
6159 value = (symbol + addend) & howto->dst_mask;
6162 value = addend + gp - p + 4;
6163 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6164 for overflow. But, on, say, Irix 5, relocations against
6165 _gp_disp are normally generated from the .cpload
6166 pseudo-op. It generates code that normally looks like
6169 lui $gp,%hi(_gp_disp)
6170 addiu $gp,$gp,%lo(_gp_disp)
6173 Here $t9 holds the address of the function being called,
6174 as required by the MIPS ELF ABI. The R_MIPS_LO16
6175 relocation can easily overflow in this situation, but the
6176 R_MIPS_HI16 relocation will handle the overflow.
6177 Therefore, we consider this a bug in the MIPS ABI, and do
6178 not check for overflow here. */
6182 case R_MIPS_LITERAL:
6183 /* Because we don't merge literal sections, we can handle this
6184 just like R_MIPS_GPREL16. In the long run, we should merge
6185 shared literals, and then we will need to additional work
6190 case R_MIPS16_GPREL:
6191 /* The R_MIPS16_GPREL performs the same calculation as
6192 R_MIPS_GPREL16, but stores the relocated bits in a different
6193 order. We don't need to do anything special here; the
6194 differences are handled in mips_elf_perform_relocation. */
6195 case R_MIPS_GPREL16:
6197 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
6199 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
6200 overflowed_p = mips_elf_overflow_p (value, 16);
6206 value = mips_elf_got16_entry (abfd, info, symbol + addend);
6207 if (value == (bfd_vma) -1)
6210 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6213 overflowed_p = mips_elf_overflow_p (value, 16);
6220 case R_MIPS_GOT_DISP:
6222 overflowed_p = mips_elf_overflow_p (value, 16);
6225 case R_MIPS_GPREL32:
6226 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6230 value = mips_elf_sign_extend (addend, 16) + symbol - p;
6231 overflowed_p = mips_elf_overflow_p (value, 16);
6234 case R_MIPS_GOT_HI16:
6235 case R_MIPS_CALL_HI16:
6236 /* We're allowed to handle these two relocations identically.
6237 The dynamic linker is allowed to handle the CALL relocations
6238 differently by creating a lazy evaluation stub. */
6240 value = mips_elf_high (value);
6241 value &= howto->dst_mask;
6244 case R_MIPS_GOT_LO16:
6245 case R_MIPS_CALL_LO16:
6246 value = g & howto->dst_mask;
6249 case R_MIPS_GOT_PAGE:
6250 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
6251 if (value == (bfd_vma) -1)
6253 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6256 overflowed_p = mips_elf_overflow_p (value, 16);
6259 case R_MIPS_GOT_OFST:
6260 mips_elf_got_page (abfd, info, symbol + addend, &value);
6261 overflowed_p = mips_elf_overflow_p (value, 16);
6265 value = symbol - addend;
6266 value &= howto->dst_mask;
6270 value = mips_elf_higher (addend + symbol);
6271 value &= howto->dst_mask;
6274 case R_MIPS_HIGHEST:
6275 value = mips_elf_highest (addend + symbol);
6276 value &= howto->dst_mask;
6279 case R_MIPS_SCN_DISP:
6280 value = symbol + addend - sec->output_offset;
6281 value &= howto->dst_mask;
6286 /* Both of these may be ignored. R_MIPS_JALR is an optimization
6287 hint; we could improve performance by honoring that hint. */
6288 return bfd_reloc_continue;
6290 case R_MIPS_GNU_VTINHERIT:
6291 case R_MIPS_GNU_VTENTRY:
6292 /* We don't do anything with these at present. */
6293 return bfd_reloc_continue;
6296 /* An unrecognized relocation type. */
6297 return bfd_reloc_notsupported;
6300 /* Store the VALUE for our caller. */
6302 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6305 /* Obtain the field relocated by RELOCATION. */
6308 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6309 reloc_howto_type *howto;
6310 const Elf_Internal_Rela *relocation;
6315 bfd_byte *location = contents + relocation->r_offset;
6317 /* Obtain the bytes. */
6318 x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
6320 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
6321 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6322 && bfd_little_endian (input_bfd))
6323 /* The two 16-bit words will be reversed on a little-endian
6324 system. See mips_elf_perform_relocation for more details. */
6325 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6330 /* It has been determined that the result of the RELOCATION is the
6331 VALUE. Use HOWTO to place VALUE into the output file at the
6332 appropriate position. The SECTION is the section to which the
6333 relocation applies. If REQUIRE_JALX is true, then the opcode used
6334 for the relocation must be either JAL or JALX, and it is
6335 unconditionally converted to JALX.
6337 Returns false if anything goes wrong. */
6340 mips_elf_perform_relocation (info, howto, relocation, value,
6341 input_bfd, input_section,
6342 contents, require_jalx)
6343 struct bfd_link_info *info;
6344 reloc_howto_type *howto;
6345 const Elf_Internal_Rela *relocation;
6348 asection *input_section;
6350 boolean require_jalx;
6354 int r_type = ELF32_R_TYPE (relocation->r_info);
6356 /* Figure out where the relocation is occurring. */
6357 location = contents + relocation->r_offset;
6359 /* Obtain the current value. */
6360 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6362 /* Clear the field we are setting. */
6363 x &= ~howto->dst_mask;
6365 /* If this is the R_MIPS16_26 relocation, we must store the
6366 value in a funny way. */
6367 if (r_type == R_MIPS16_26)
6369 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
6370 Most mips16 instructions are 16 bits, but these instructions
6373 The format of these instructions is:
6375 +--------------+--------------------------------+
6376 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
6377 +--------------+--------------------------------+
6379 +-----------------------------------------------+
6381 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
6382 Note that the immediate value in the first word is swapped.
6384 When producing a relocateable object file, R_MIPS16_26 is
6385 handled mostly like R_MIPS_26. In particular, the addend is
6386 stored as a straight 26-bit value in a 32-bit instruction.
6387 (gas makes life simpler for itself by never adjusting a
6388 R_MIPS16_26 reloc to be against a section, so the addend is
6389 always zero). However, the 32 bit instruction is stored as 2
6390 16-bit values, rather than a single 32-bit value. In a
6391 big-endian file, the result is the same; in a little-endian
6392 file, the two 16-bit halves of the 32 bit value are swapped.
6393 This is so that a disassembler can recognize the jal
6396 When doing a final link, R_MIPS16_26 is treated as a 32 bit
6397 instruction stored as two 16-bit values. The addend A is the
6398 contents of the targ26 field. The calculation is the same as
6399 R_MIPS_26. When storing the calculated value, reorder the
6400 immediate value as shown above, and don't forget to store the
6401 value as two 16-bit values.
6403 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
6407 +--------+----------------------+
6411 +--------+----------------------+
6414 +----------+------+-------------+
6418 +----------+--------------------+
6419 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
6420 ((sub1 << 16) | sub2)).
6422 When producing a relocateable object file, the calculation is
6423 (((A < 2) | (P & 0xf0000000) + S) >> 2)
6424 When producing a fully linked file, the calculation is
6425 let R = (((A < 2) | (P & 0xf0000000) + S) >> 2)
6426 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
6428 if (!info->relocateable)
6429 /* Shuffle the bits according to the formula above. */
6430 value = (((value & 0x1f0000) << 5)
6431 | ((value & 0x3e00000) >> 5)
6432 | (value & 0xffff));
6435 else if (r_type == R_MIPS16_GPREL)
6437 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
6438 mode. A typical instruction will have a format like this:
6440 +--------------+--------------------------------+
6441 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
6442 +--------------+--------------------------------+
6443 ! Major ! rx ! ry ! Imm 4:0 !
6444 +--------------+--------------------------------+
6446 EXTEND is the five bit value 11110. Major is the instruction
6449 This is handled exactly like R_MIPS_GPREL16, except that the
6450 addend is retrieved and stored as shown in this diagram; that
6451 is, the Imm fields above replace the V-rel16 field.
6453 All we need to do here is shuffle the bits appropriately. As
6454 above, the two 16-bit halves must be swapped on a
6455 little-endian system. */
6456 value = (((value & 0x7e0) << 16)
6457 | ((value & 0xf800) << 5)
6461 /* Set the field. */
6462 x |= (value & howto->dst_mask);
6464 /* If required, turn JAL into JALX. */
6468 bfd_vma opcode = x >> 26;
6469 bfd_vma jalx_opcode;
6471 /* Check to see if the opcode is already JAL or JALX. */
6472 if (r_type == R_MIPS16_26)
6474 ok = ((opcode == 0x6) || (opcode == 0x7));
6479 ok = ((opcode == 0x3) || (opcode == 0x1d));
6483 /* If the opcode is not JAL or JALX, there's a problem. */
6486 (*_bfd_error_handler)
6487 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
6488 bfd_get_filename (input_bfd),
6489 input_section->name,
6490 (unsigned long) relocation->r_offset);
6491 bfd_set_error (bfd_error_bad_value);
6495 /* Make this the JALX opcode. */
6496 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6499 /* Swap the high- and low-order 16 bits on little-endian systems
6500 when doing a MIPS16 relocation. */
6501 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
6502 && bfd_little_endian (input_bfd))
6503 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6505 /* Put the value into the output. */
6506 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6510 /* Returns true if SECTION is a MIPS16 stub section. */
6513 mips_elf_stub_section_p (abfd, section)
6514 bfd *abfd ATTRIBUTE_UNUSED;
6517 const char *name = bfd_get_section_name (abfd, section);
6519 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
6520 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6521 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
6524 /* Relocate a MIPS ELF section. */
6527 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6528 contents, relocs, local_syms, local_sections)
6530 struct bfd_link_info *info;
6532 asection *input_section;
6534 Elf_Internal_Rela *relocs;
6535 Elf_Internal_Sym *local_syms;
6536 asection **local_sections;
6538 Elf_Internal_Rela *rel;
6539 const Elf_Internal_Rela *relend;
6541 bfd_vma last_hi16_addend;
6542 boolean use_saved_addend_p = false;
6543 boolean last_hi16_addend_valid_p = false;
6544 struct elf_backend_data *bed;
6546 bed = get_elf_backend_data (output_bfd);
6547 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6548 for (rel = relocs; rel < relend; ++rel)
6552 reloc_howto_type *howto;
6553 boolean require_jalx;
6554 /* True if the relocation is a RELA relocation, rather than a
6556 boolean rela_relocation_p = true;
6557 int r_type = ELF32_R_TYPE (rel->r_info);
6559 /* Find the relocation howto for this relocation. */
6560 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6561 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6562 64-bit code, but make sure all their addresses are in the
6563 lowermost or uppermost 32-bit section of the 64-bit address
6564 space. Thus, when they use an R_MIPS_64 they mean what is
6565 usually meant by R_MIPS_32, with the exception that the
6566 stored value is sign-extended to 64 bits. */
6567 howto = elf_mips_howto_table + R_MIPS_32;
6569 howto = mips_rtype_to_howto (r_type);
6571 if (!use_saved_addend_p)
6573 Elf_Internal_Shdr *rel_hdr;
6575 /* If these relocations were originally of the REL variety,
6576 we must pull the addend out of the field that will be
6577 relocated. Otherwise, we simply use the contents of the
6578 RELA relocation. To determine which flavor or relocation
6579 this is, we depend on the fact that the INPUT_SECTION's
6580 REL_HDR is read before its REL_HDR2. */
6581 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6582 if ((size_t) (rel - relocs)
6583 >= (rel_hdr->sh_size / rel_hdr->sh_entsize
6584 * bed->s->int_rels_per_ext_rel))
6585 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6586 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6588 /* Note that this is a REL relocation. */
6589 rela_relocation_p = false;
6591 /* Get the addend, which is stored in the input file. */
6592 addend = mips_elf_obtain_contents (howto,
6596 addend &= howto->src_mask;
6598 /* For some kinds of relocations, the ADDEND is a
6599 combination of the addend stored in two different
6601 if (r_type == R_MIPS_HI16
6602 || (r_type == R_MIPS_GOT16
6603 && mips_elf_local_relocation_p (input_bfd, rel,
6607 const Elf_Internal_Rela *lo16_relocation;
6608 reloc_howto_type *lo16_howto;
6610 /* The combined value is the sum of the HI16 addend,
6611 left-shifted by sixteen bits, and the LO16
6612 addend, sign extended. (Usually, the code does
6613 a `lui' of the HI16 value, and then an `addiu' of
6616 Scan ahead to find a matching R_MIPS_LO16
6619 = mips_elf_next_lo16_relocation (rel, relend);
6620 if (lo16_relocation == NULL)
6623 /* Obtain the addend kept there. */
6624 lo16_howto = mips_rtype_to_howto (R_MIPS_LO16);
6625 l = mips_elf_obtain_contents (lo16_howto,
6627 input_bfd, contents);
6628 l &= lo16_howto->src_mask;
6629 l = mips_elf_sign_extend (l, 16);
6631 /* Save the high-order bit for later. When we
6632 encounter the R_MIPS_LO16 relocation we will need
6635 last_hi16_addend = addend;
6636 last_hi16_addend_valid_p = true;
6638 /* Compute the combined addend. */
6641 else if (r_type == R_MIPS_LO16)
6643 /* Used the saved HI16 addend. */
6644 if (!last_hi16_addend_valid_p)
6646 bfd_set_error (bfd_error_bad_value);
6649 addend |= last_hi16_addend;
6651 else if (r_type == R_MIPS16_GPREL)
6653 /* The addend is scrambled in the object file. See
6654 mips_elf_perform_relocation for details on the
6656 addend = (((addend & 0x1f0000) >> 5)
6657 | ((addend & 0x7e00000) >> 16)
6662 addend = rel->r_addend;
6665 if (info->relocateable)
6667 Elf_Internal_Sym *sym;
6668 unsigned long r_symndx;
6670 /* Since we're just relocating, all we need to do is copy
6671 the relocations back out to the object file, unless
6672 they're against a section symbol, in which case we need
6673 to adjust by the section offset, or unless they're GP
6674 relative in which case we need to adjust by the amount
6675 that we're adjusting GP in this relocateable object. */
6677 if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections))
6678 /* There's nothing to do for non-local relocations. */
6681 if (r_type == R_MIPS16_GPREL
6682 || r_type == R_MIPS_GPREL16
6683 || r_type == R_MIPS_GPREL32)
6684 addend -= (_bfd_get_gp_value (output_bfd)
6685 - _bfd_get_gp_value (input_bfd));
6686 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26)
6687 /* The addend is stored without its two least
6688 significant bits (which are always zero.) In a
6689 non-relocateable link, calculate_relocation will do
6690 this shift; here, we must do it ourselves. */
6693 r_symndx = ELF32_R_SYM (rel->r_info);
6694 sym = local_syms + r_symndx;
6695 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6696 /* Adjust the addend appropriately. */
6697 addend += local_sections[r_symndx]->output_offset;
6699 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
6700 then we only want to write out the high-order 16 bits.
6701 The subsequent R_MIPS_LO16 will handle the low-order bits. */
6702 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16)
6703 addend = mips_elf_high (addend);
6704 /* If the relocation is for an R_MIPS_26 relocation, then
6705 the two low-order bits are not stored in the object file;
6706 they are implicitly zero. */
6707 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26)
6710 if (rela_relocation_p)
6711 /* If this is a RELA relocation, just update the addend.
6712 We have to cast away constness for REL. */
6713 rel->r_addend = addend;
6716 /* Otherwise, we have to write the value back out. Note
6717 that we use the source mask, rather than the
6718 destination mask because the place to which we are
6719 writing will be source of the addend in the final
6721 addend &= howto->src_mask;
6722 if (!mips_elf_perform_relocation (info, howto, rel, addend,
6723 input_bfd, input_section,
6728 /* Go on to the next relocation. */
6732 /* In the N32 and 64-bit ABIs there may be multiple consecutive
6733 relocations for the same offset. In that case we are
6734 supposed to treat the output of each relocation as the addend
6736 if (rel + 1 < relend
6737 && rel->r_offset == rel[1].r_offset
6738 && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
6739 use_saved_addend_p = true;
6741 use_saved_addend_p = false;
6743 /* Figure out what value we are supposed to relocate. */
6744 switch (mips_elf_calculate_relocation (output_bfd,
6757 case bfd_reloc_continue:
6758 /* There's nothing to do. */
6761 case bfd_reloc_undefined:
6762 /* mips_elf_calculate_relocation already called the
6763 undefined_symbol callback. There's no real point in
6764 trying to perform the relocation at this point, so we
6765 just skip ahead to the next relocation. */
6768 case bfd_reloc_notsupported:
6772 case bfd_reloc_overflow:
6773 if (use_saved_addend_p)
6774 /* Ignore overflow until we reach the last relocation for
6775 a given location. */
6779 BFD_ASSERT (name != NULL);
6780 if (! ((*info->callbacks->reloc_overflow)
6781 (info, name, howto->name, (bfd_vma) 0,
6782 input_bfd, input_section, rel->r_offset)))
6795 /* If we've got another relocation for the address, keep going
6796 until we reach the last one. */
6797 if (use_saved_addend_p)
6803 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6804 /* See the comment above about using R_MIPS_64 in the 32-bit
6805 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
6806 that calculated the right value. Now, however, we
6807 sign-extend the 32-bit result to 64-bits, and store it as a
6808 64-bit value. We are especially generous here in that we
6809 go to extreme lengths to support this usage on systems with
6810 only a 32-bit VMA. */
6813 /* Just sign-extend the value, and then fall through to the
6814 normal case, using the R_MIPS_64 howto. That will store
6815 the 64-bit value into a 64-bit area. */
6816 value = mips_elf_sign_extend (value, 64);
6817 howto = elf_mips_howto_table + R_MIPS_64;
6819 /* In the 32-bit VMA case, we must handle sign-extension and
6820 endianness manually. */
6825 if (value & 0x80000000)
6826 sign_bits = 0xffffffff;
6830 /* If only a 32-bit VMA is available do two separate
6832 if (bfd_big_endian (input_bfd))
6834 /* Store the sign-bits (which are most significant)
6836 low_bits = sign_bits;
6842 high_bits = sign_bits;
6844 bfd_put_32 (input_bfd, low_bits,
6845 contents + rel->r_offset);
6846 bfd_put_32 (input_bfd, high_bits,
6847 contents + rel->r_offset + 4);
6852 /* Actually perform the relocation. */
6853 if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
6854 input_section, contents,
6862 /* This hook function is called before the linker writes out a global
6863 symbol. We mark symbols as small common if appropriate. This is
6864 also where we undo the increment of the value for a mips16 symbol. */
6868 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6869 bfd *abfd ATTRIBUTE_UNUSED;
6870 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6871 const char *name ATTRIBUTE_UNUSED;
6872 Elf_Internal_Sym *sym;
6873 asection *input_sec;
6875 /* If we see a common symbol, which implies a relocatable link, then
6876 if a symbol was small common in an input file, mark it as small
6877 common in the output file. */
6878 if (sym->st_shndx == SHN_COMMON
6879 && strcmp (input_sec->name, ".scommon") == 0)
6880 sym->st_shndx = SHN_MIPS_SCOMMON;
6882 if (sym->st_other == STO_MIPS16
6883 && (sym->st_value & 1) != 0)
6889 /* Functions for the dynamic linker. */
6891 /* The name of the dynamic interpreter. This is put in the .interp
6894 #define ELF_DYNAMIC_INTERPRETER(abfd) \
6895 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
6896 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
6897 : "/usr/lib/libc.so.1")
6899 /* Create dynamic sections when linking against a dynamic object. */
6902 _bfd_mips_elf_create_dynamic_sections (abfd, info)
6904 struct bfd_link_info *info;
6906 struct elf_link_hash_entry *h;
6908 register asection *s;
6909 const char * const *namep;
6911 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6912 | SEC_LINKER_CREATED | SEC_READONLY);
6914 /* Mips ABI requests the .dynamic section to be read only. */
6915 s = bfd_get_section_by_name (abfd, ".dynamic");
6918 if (! bfd_set_section_flags (abfd, s, flags))
6922 /* We need to create .got section. */
6923 if (! mips_elf_create_got_section (abfd, info))
6926 /* Create the .msym section on IRIX6. It is used by the dynamic
6927 linker to speed up dynamic relocations, and to avoid computing
6928 the ELF hash for symbols. */
6929 if (IRIX_COMPAT (abfd) == ict_irix6
6930 && !mips_elf_create_msym_section (abfd))
6933 /* Create .stub section. */
6934 if (bfd_get_section_by_name (abfd,
6935 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6937 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6939 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6940 || ! bfd_set_section_alignment (abfd, s,
6941 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6945 if (IRIX_COMPAT (abfd) == ict_irix5
6947 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6949 s = bfd_make_section (abfd, ".rld_map");
6951 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6952 || ! bfd_set_section_alignment (abfd, s,
6953 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6957 /* On IRIX5, we adjust add some additional symbols and change the
6958 alignments of several sections. There is no ABI documentation
6959 indicating that this is necessary on IRIX6, nor any evidence that
6960 the linker takes such action. */
6961 if (IRIX_COMPAT (abfd) == ict_irix5)
6963 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6966 if (! (_bfd_generic_link_add_one_symbol
6967 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
6968 (bfd_vma) 0, (const char *) NULL, false,
6969 get_elf_backend_data (abfd)->collect,
6970 (struct bfd_link_hash_entry **) &h)))
6972 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6973 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6974 h->type = STT_SECTION;
6976 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6980 /* We need to create a .compact_rel section. */
6981 if (! mips_elf_create_compact_rel_section (abfd, info))
6984 /* Change aligments of some sections. */
6985 s = bfd_get_section_by_name (abfd, ".hash");
6987 bfd_set_section_alignment (abfd, s, 4);
6988 s = bfd_get_section_by_name (abfd, ".dynsym");
6990 bfd_set_section_alignment (abfd, s, 4);
6991 s = bfd_get_section_by_name (abfd, ".dynstr");
6993 bfd_set_section_alignment (abfd, s, 4);
6994 s = bfd_get_section_by_name (abfd, ".reginfo");
6996 bfd_set_section_alignment (abfd, s, 4);
6997 s = bfd_get_section_by_name (abfd, ".dynamic");
6999 bfd_set_section_alignment (abfd, s, 4);
7005 if (! (_bfd_generic_link_add_one_symbol
7006 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
7007 (bfd_vma) 0, (const char *) NULL, false,
7008 get_elf_backend_data (abfd)->collect,
7009 (struct bfd_link_hash_entry **) &h)))
7011 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7012 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7013 h->type = STT_SECTION;
7015 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7018 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7020 /* __rld_map is a four byte word located in the .data section
7021 and is filled in by the rtld to contain a pointer to
7022 the _r_debug structure. Its symbol value will be set in
7023 mips_elf_finish_dynamic_symbol. */
7024 s = bfd_get_section_by_name (abfd, ".rld_map");
7025 BFD_ASSERT (s != NULL);
7028 if (! (_bfd_generic_link_add_one_symbol
7029 (info, abfd, "__rld_map", BSF_GLOBAL, s,
7030 (bfd_vma) 0, (const char *) NULL, false,
7031 get_elf_backend_data (abfd)->collect,
7032 (struct bfd_link_hash_entry **) &h)))
7034 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7035 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7036 h->type = STT_OBJECT;
7038 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7046 /* Create the .compact_rel section. */
7049 mips_elf_create_compact_rel_section (abfd, info)
7051 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7054 register asection *s;
7056 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
7058 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
7061 s = bfd_make_section (abfd, ".compact_rel");
7063 || ! bfd_set_section_flags (abfd, s, flags)
7064 || ! bfd_set_section_alignment (abfd, s,
7065 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7068 s->_raw_size = sizeof (Elf32_External_compact_rel);
7074 /* Create the .got section to hold the global offset table. */
7077 mips_elf_create_got_section (abfd, info)
7079 struct bfd_link_info *info;
7082 register asection *s;
7083 struct elf_link_hash_entry *h;
7084 struct mips_got_info *g;
7086 /* This function may be called more than once. */
7087 if (mips_elf_got_section (abfd))
7090 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7091 | SEC_LINKER_CREATED);
7093 s = bfd_make_section (abfd, ".got");
7095 || ! bfd_set_section_flags (abfd, s, flags)
7096 || ! bfd_set_section_alignment (abfd, s, 4))
7099 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
7100 linker script because we don't want to define the symbol if we
7101 are not creating a global offset table. */
7103 if (! (_bfd_generic_link_add_one_symbol
7104 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
7105 (bfd_vma) 0, (const char *) NULL, false,
7106 get_elf_backend_data (abfd)->collect,
7107 (struct bfd_link_hash_entry **) &h)))
7109 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7110 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7111 h->type = STT_OBJECT;
7114 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
7117 /* The first several global offset table entries are reserved. */
7118 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
7120 g = (struct mips_got_info *) bfd_alloc (abfd,
7121 sizeof (struct mips_got_info));
7124 g->global_gotsym = NULL;
7125 g->local_gotno = MIPS_RESERVED_GOTNO;
7126 g->assigned_gotno = MIPS_RESERVED_GOTNO;
7127 if (elf_section_data (s) == NULL)
7130 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
7131 if (elf_section_data (s) == NULL)
7134 elf_section_data (s)->tdata = (PTR) g;
7135 elf_section_data (s)->this_hdr.sh_flags
7136 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7141 /* Returns the .msym section for ABFD, creating it if it does not
7142 already exist. Returns NULL to indicate error. */
7145 mips_elf_create_msym_section (abfd)
7150 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7153 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7155 || !bfd_set_section_flags (abfd, s,
7159 | SEC_LINKER_CREATED
7161 || !bfd_set_section_alignment (abfd, s,
7162 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7169 /* Add room for N relocations to the .rel.dyn section in ABFD. */
7172 mips_elf_allocate_dynamic_relocations (abfd, n)
7178 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
7179 BFD_ASSERT (s != NULL);
7181 if (s->_raw_size == 0)
7183 /* Make room for a null element. */
7184 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
7187 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
7190 /* Look through the relocs for a section during the first phase, and
7191 allocate space in the global offset table. */
7194 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
7196 struct bfd_link_info *info;
7198 const Elf_Internal_Rela *relocs;
7202 Elf_Internal_Shdr *symtab_hdr;
7203 struct elf_link_hash_entry **sym_hashes;
7204 struct mips_got_info *g;
7206 const Elf_Internal_Rela *rel;
7207 const Elf_Internal_Rela *rel_end;
7210 struct elf_backend_data *bed;
7212 if (info->relocateable)
7215 dynobj = elf_hash_table (info)->dynobj;
7216 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7217 sym_hashes = elf_sym_hashes (abfd);
7218 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7220 /* Check for the mips16 stub sections. */
7222 name = bfd_get_section_name (abfd, sec);
7223 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
7225 unsigned long r_symndx;
7227 /* Look at the relocation information to figure out which symbol
7230 r_symndx = ELF32_R_SYM (relocs->r_info);
7232 if (r_symndx < extsymoff
7233 || sym_hashes[r_symndx - extsymoff] == NULL)
7237 /* This stub is for a local symbol. This stub will only be
7238 needed if there is some relocation in this BFD, other
7239 than a 16 bit function call, which refers to this symbol. */
7240 for (o = abfd->sections; o != NULL; o = o->next)
7242 Elf_Internal_Rela *sec_relocs;
7243 const Elf_Internal_Rela *r, *rend;
7245 /* We can ignore stub sections when looking for relocs. */
7246 if ((o->flags & SEC_RELOC) == 0
7247 || o->reloc_count == 0
7248 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
7249 sizeof FN_STUB - 1) == 0
7250 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
7251 sizeof CALL_STUB - 1) == 0
7252 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
7253 sizeof CALL_FP_STUB - 1) == 0)
7256 sec_relocs = (_bfd_elf32_link_read_relocs
7257 (abfd, o, (PTR) NULL,
7258 (Elf_Internal_Rela *) NULL,
7259 info->keep_memory));
7260 if (sec_relocs == NULL)
7263 rend = sec_relocs + o->reloc_count;
7264 for (r = sec_relocs; r < rend; r++)
7265 if (ELF32_R_SYM (r->r_info) == r_symndx
7266 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
7269 if (! info->keep_memory)
7278 /* There is no non-call reloc for this stub, so we do
7279 not need it. Since this function is called before
7280 the linker maps input sections to output sections, we
7281 can easily discard it by setting the SEC_EXCLUDE
7283 sec->flags |= SEC_EXCLUDE;
7287 /* Record this stub in an array of local symbol stubs for
7289 if (elf_tdata (abfd)->local_stubs == NULL)
7291 unsigned long symcount;
7294 if (elf_bad_symtab (abfd))
7295 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
7297 symcount = symtab_hdr->sh_info;
7298 n = (asection **) bfd_zalloc (abfd,
7299 symcount * sizeof (asection *));
7302 elf_tdata (abfd)->local_stubs = n;
7305 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7307 /* We don't need to set mips16_stubs_seen in this case.
7308 That flag is used to see whether we need to look through
7309 the global symbol table for stubs. We don't need to set
7310 it here, because we just have a local stub. */
7314 struct mips_elf_link_hash_entry *h;
7316 h = ((struct mips_elf_link_hash_entry *)
7317 sym_hashes[r_symndx - extsymoff]);
7319 /* H is the symbol this stub is for. */
7322 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7325 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
7326 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7328 unsigned long r_symndx;
7329 struct mips_elf_link_hash_entry *h;
7332 /* Look at the relocation information to figure out which symbol
7335 r_symndx = ELF32_R_SYM (relocs->r_info);
7337 if (r_symndx < extsymoff
7338 || sym_hashes[r_symndx - extsymoff] == NULL)
7340 /* This stub was actually built for a static symbol defined
7341 in the same file. We assume that all static symbols in
7342 mips16 code are themselves mips16, so we can simply
7343 discard this stub. Since this function is called before
7344 the linker maps input sections to output sections, we can
7345 easily discard it by setting the SEC_EXCLUDE flag. */
7346 sec->flags |= SEC_EXCLUDE;
7350 h = ((struct mips_elf_link_hash_entry *)
7351 sym_hashes[r_symndx - extsymoff]);
7353 /* H is the symbol this stub is for. */
7355 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7356 loc = &h->call_fp_stub;
7358 loc = &h->call_stub;
7360 /* If we already have an appropriate stub for this function, we
7361 don't need another one, so we can discard this one. Since
7362 this function is called before the linker maps input sections
7363 to output sections, we can easily discard it by setting the
7364 SEC_EXCLUDE flag. We can also discard this section if we
7365 happen to already know that this is a mips16 function; it is
7366 not necessary to check this here, as it is checked later, but
7367 it is slightly faster to check now. */
7368 if (*loc != NULL || h->root.other == STO_MIPS16)
7370 sec->flags |= SEC_EXCLUDE;
7375 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7385 sgot = mips_elf_got_section (dynobj);
7390 BFD_ASSERT (elf_section_data (sgot) != NULL);
7391 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7392 BFD_ASSERT (g != NULL);
7397 bed = get_elf_backend_data (abfd);
7398 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7399 for (rel = relocs; rel < rel_end; ++rel)
7401 unsigned long r_symndx;
7403 struct elf_link_hash_entry *h;
7405 r_symndx = ELF32_R_SYM (rel->r_info);
7406 r_type = ELF32_R_TYPE (rel->r_info);
7408 if (r_symndx < extsymoff)
7412 h = sym_hashes[r_symndx - extsymoff];
7414 /* This may be an indirect symbol created because of a version. */
7417 while (h->root.type == bfd_link_hash_indirect)
7418 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7422 /* Some relocs require a global offset table. */
7423 if (dynobj == NULL || sgot == NULL)
7429 case R_MIPS_CALL_HI16:
7430 case R_MIPS_CALL_LO16:
7431 case R_MIPS_GOT_HI16:
7432 case R_MIPS_GOT_LO16:
7433 case R_MIPS_GOT_PAGE:
7434 case R_MIPS_GOT_OFST:
7435 case R_MIPS_GOT_DISP:
7437 elf_hash_table (info)->dynobj = dynobj = abfd;
7438 if (! mips_elf_create_got_section (dynobj, info))
7440 g = mips_elf_got_info (dynobj, &sgot);
7447 && (info->shared || h != NULL)
7448 && (sec->flags & SEC_ALLOC) != 0)
7449 elf_hash_table (info)->dynobj = dynobj = abfd;
7457 if (!h && (r_type == R_MIPS_CALL_LO16
7458 || r_type == R_MIPS_GOT_LO16
7459 || r_type == R_MIPS_GOT_DISP))
7461 /* We may need a local GOT entry for this relocation. We
7462 don't count R_MIPS_GOT_PAGE because we can estimate the
7463 maximum number of pages needed by looking at the size of
7464 the segment. Similar comments apply to R_MIPS_GOT16. We
7465 don't count R_MIPS_GOT_HI16, or R_MIPS_CALL_HI16 because
7466 these are always followed by an R_MIPS_GOT_LO16 or
7469 This estimation is very conservative since we can merge
7470 duplicate entries in the GOT. In order to be less
7471 conservative, we could actually build the GOT here,
7472 rather than in relocate_section. */
7474 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7482 (*_bfd_error_handler)
7483 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
7484 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
7485 bfd_set_error (bfd_error_bad_value);
7490 case R_MIPS_CALL_HI16:
7491 case R_MIPS_CALL_LO16:
7494 /* This symbol requires a global offset table entry. */
7495 if (!mips_elf_record_global_got_symbol (h, info, g))
7498 /* We need a stub, not a plt entry for the undefined
7499 function. But we record it as if it needs plt. See
7500 elf_adjust_dynamic_symbol in elflink.h. */
7501 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
7507 case R_MIPS_GOT_HI16:
7508 case R_MIPS_GOT_LO16:
7509 case R_MIPS_GOT_DISP:
7510 /* This symbol requires a global offset table entry. */
7511 if (h && !mips_elf_record_global_got_symbol (h, info, g))
7518 if ((info->shared || h != NULL)
7519 && (sec->flags & SEC_ALLOC) != 0)
7523 const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
7525 sreloc = bfd_get_section_by_name (dynobj, name);
7528 sreloc = bfd_make_section (dynobj, name);
7530 || ! bfd_set_section_flags (dynobj, sreloc,
7535 | SEC_LINKER_CREATED
7537 || ! bfd_set_section_alignment (dynobj, sreloc,
7543 /* When creating a shared object, we must copy these
7544 reloc types into the output file as R_MIPS_REL32
7545 relocs. We make room for this reloc in the
7546 .rel.dyn reloc section. */
7547 mips_elf_allocate_dynamic_relocations (dynobj, 1);
7550 struct mips_elf_link_hash_entry *hmips;
7552 /* We only need to copy this reloc if the symbol is
7553 defined in a dynamic object. */
7554 hmips = (struct mips_elf_link_hash_entry *) h;
7555 ++hmips->possibly_dynamic_relocs;
7558 /* Even though we don't directly need a GOT entry for
7559 this symbol, a symbol must have a dynamic symbol
7560 table index greater that DT_MIPS_GOTSYM if there are
7561 dynamic relocations against it. */
7563 && !mips_elf_record_global_got_symbol (h, info, g))
7567 if (SGI_COMPAT (dynobj))
7568 mips_elf_hash_table (info)->compact_rel_size +=
7569 sizeof (Elf32_External_crinfo);
7573 case R_MIPS_GPREL16:
7574 case R_MIPS_LITERAL:
7575 case R_MIPS_GPREL32:
7576 if (SGI_COMPAT (dynobj))
7577 mips_elf_hash_table (info)->compact_rel_size +=
7578 sizeof (Elf32_External_crinfo);
7581 /* This relocation describes the C++ object vtable hierarchy.
7582 Reconstruct it for later use during GC. */
7583 case R_MIPS_GNU_VTINHERIT:
7584 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7588 /* This relocation describes which C++ vtable entries are actually
7589 used. Record for later use during GC. */
7590 case R_MIPS_GNU_VTENTRY:
7591 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7599 /* If this reloc is not a 16 bit call, and it has a global
7600 symbol, then we will need the fn_stub if there is one.
7601 References from a stub section do not count. */
7603 && r_type != R_MIPS16_26
7604 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7605 sizeof FN_STUB - 1) != 0
7606 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7607 sizeof CALL_STUB - 1) != 0
7608 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7609 sizeof CALL_FP_STUB - 1) != 0)
7611 struct mips_elf_link_hash_entry *mh;
7613 mh = (struct mips_elf_link_hash_entry *) h;
7614 mh->need_fn_stub = true;
7621 /* Return the section that should be marked against GC for a given
7625 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7627 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7628 Elf_Internal_Rela *rel;
7629 struct elf_link_hash_entry *h;
7630 Elf_Internal_Sym *sym;
7632 /* ??? Do mips16 stub sections need to be handled special? */
7636 switch (ELF32_R_TYPE (rel->r_info))
7638 case R_MIPS_GNU_VTINHERIT:
7639 case R_MIPS_GNU_VTENTRY:
7643 switch (h->root.type)
7645 case bfd_link_hash_defined:
7646 case bfd_link_hash_defweak:
7647 return h->root.u.def.section;
7649 case bfd_link_hash_common:
7650 return h->root.u.c.p->section;
7659 if (!(elf_bad_symtab (abfd)
7660 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7661 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7662 && sym->st_shndx != SHN_COMMON))
7664 return bfd_section_from_elf_index (abfd, sym->st_shndx);
7671 /* Update the got entry reference counts for the section being removed. */
7674 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7675 bfd *abfd ATTRIBUTE_UNUSED;
7676 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7677 asection *sec ATTRIBUTE_UNUSED;
7678 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
7681 Elf_Internal_Shdr *symtab_hdr;
7682 struct elf_link_hash_entry **sym_hashes;
7683 bfd_signed_vma *local_got_refcounts;
7684 const Elf_Internal_Rela *rel, *relend;
7685 unsigned long r_symndx;
7686 struct elf_link_hash_entry *h;
7688 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7689 sym_hashes = elf_sym_hashes (abfd);
7690 local_got_refcounts = elf_local_got_refcounts (abfd);
7692 relend = relocs + sec->reloc_count;
7693 for (rel = relocs; rel < relend; rel++)
7694 switch (ELF32_R_TYPE (rel->r_info))
7698 case R_MIPS_CALL_HI16:
7699 case R_MIPS_CALL_LO16:
7700 case R_MIPS_GOT_HI16:
7701 case R_MIPS_GOT_LO16:
7702 /* ??? It would seem that the existing MIPS code does no sort
7703 of reference counting or whatnot on its GOT and PLT entries,
7704 so it is not possible to garbage collect them at this time. */
7716 /* Adjust a symbol defined by a dynamic object and referenced by a
7717 regular object. The current definition is in some section of the
7718 dynamic object, but we're not including those sections. We have to
7719 change the definition to something the rest of the link can
7723 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
7724 struct bfd_link_info *info;
7725 struct elf_link_hash_entry *h;
7728 struct mips_elf_link_hash_entry *hmips;
7731 dynobj = elf_hash_table (info)->dynobj;
7733 /* Make sure we know what is going on here. */
7734 BFD_ASSERT (dynobj != NULL
7735 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7736 || h->weakdef != NULL
7737 || ((h->elf_link_hash_flags
7738 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7739 && (h->elf_link_hash_flags
7740 & ELF_LINK_HASH_REF_REGULAR) != 0
7741 && (h->elf_link_hash_flags
7742 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7744 /* If this symbol is defined in a dynamic object, we need to copy
7745 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7747 hmips = (struct mips_elf_link_hash_entry *) h;
7748 if (! info->relocateable
7749 && hmips->possibly_dynamic_relocs != 0
7750 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7751 mips_elf_allocate_dynamic_relocations (dynobj,
7752 hmips->possibly_dynamic_relocs);
7754 /* For a function, create a stub, if needed. */
7755 if (h->type == STT_FUNC
7756 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7758 if (! elf_hash_table (info)->dynamic_sections_created)
7761 /* If this symbol is not defined in a regular file, then set
7762 the symbol to the stub location. This is required to make
7763 function pointers compare as equal between the normal
7764 executable and the shared library. */
7765 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7767 /* We need .stub section. */
7768 s = bfd_get_section_by_name (dynobj,
7769 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7770 BFD_ASSERT (s != NULL);
7772 h->root.u.def.section = s;
7773 h->root.u.def.value = s->_raw_size;
7775 /* XXX Write this stub address somewhere. */
7776 h->plt.offset = s->_raw_size;
7778 /* Make room for this stub code. */
7779 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7781 /* The last half word of the stub will be filled with the index
7782 of this symbol in .dynsym section. */
7787 /* If this is a weak symbol, and there is a real definition, the
7788 processor independent code will have arranged for us to see the
7789 real definition first, and we can just use the same value. */
7790 if (h->weakdef != NULL)
7792 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7793 || h->weakdef->root.type == bfd_link_hash_defweak);
7794 h->root.u.def.section = h->weakdef->root.u.def.section;
7795 h->root.u.def.value = h->weakdef->root.u.def.value;
7799 /* This is a reference to a symbol defined by a dynamic object which
7800 is not a function. */
7805 /* This function is called after all the input files have been read,
7806 and the input sections have been assigned to output sections. We
7807 check for any mips16 stub sections that we can discard. */
7809 static boolean mips_elf_check_mips16_stubs
7810 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7813 _bfd_mips_elf_always_size_sections (output_bfd, info)
7815 struct bfd_link_info *info;
7819 /* The .reginfo section has a fixed size. */
7820 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7822 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7824 if (info->relocateable
7825 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7828 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7829 mips_elf_check_mips16_stubs,
7835 /* Check the mips16 stubs for a particular symbol, and see if we can
7840 mips_elf_check_mips16_stubs (h, data)
7841 struct mips_elf_link_hash_entry *h;
7842 PTR data ATTRIBUTE_UNUSED;
7844 if (h->fn_stub != NULL
7845 && ! h->need_fn_stub)
7847 /* We don't need the fn_stub; the only references to this symbol
7848 are 16 bit calls. Clobber the size to 0 to prevent it from
7849 being included in the link. */
7850 h->fn_stub->_raw_size = 0;
7851 h->fn_stub->_cooked_size = 0;
7852 h->fn_stub->flags &= ~ SEC_RELOC;
7853 h->fn_stub->reloc_count = 0;
7854 h->fn_stub->flags |= SEC_EXCLUDE;
7857 if (h->call_stub != NULL
7858 && h->root.other == STO_MIPS16)
7860 /* We don't need the call_stub; this is a 16 bit function, so
7861 calls from other 16 bit functions are OK. Clobber the size
7862 to 0 to prevent it from being included in the link. */
7863 h->call_stub->_raw_size = 0;
7864 h->call_stub->_cooked_size = 0;
7865 h->call_stub->flags &= ~ SEC_RELOC;
7866 h->call_stub->reloc_count = 0;
7867 h->call_stub->flags |= SEC_EXCLUDE;
7870 if (h->call_fp_stub != NULL
7871 && h->root.other == STO_MIPS16)
7873 /* We don't need the call_stub; this is a 16 bit function, so
7874 calls from other 16 bit functions are OK. Clobber the size
7875 to 0 to prevent it from being included in the link. */
7876 h->call_fp_stub->_raw_size = 0;
7877 h->call_fp_stub->_cooked_size = 0;
7878 h->call_fp_stub->flags &= ~ SEC_RELOC;
7879 h->call_fp_stub->reloc_count = 0;
7880 h->call_fp_stub->flags |= SEC_EXCLUDE;
7886 /* Set the sizes of the dynamic sections. */
7889 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
7891 struct bfd_link_info *info;
7896 struct mips_got_info *g = NULL;
7898 dynobj = elf_hash_table (info)->dynobj;
7899 BFD_ASSERT (dynobj != NULL);
7901 if (elf_hash_table (info)->dynamic_sections_created)
7903 /* Set the contents of the .interp section to the interpreter. */
7906 s = bfd_get_section_by_name (dynobj, ".interp");
7907 BFD_ASSERT (s != NULL);
7909 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7911 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7915 /* The check_relocs and adjust_dynamic_symbol entry points have
7916 determined the sizes of the various dynamic sections. Allocate
7919 for (s = dynobj->sections; s != NULL; s = s->next)
7924 /* It's OK to base decisions on the section name, because none
7925 of the dynobj section names depend upon the input files. */
7926 name = bfd_get_section_name (dynobj, s);
7928 if ((s->flags & SEC_LINKER_CREATED) == 0)
7933 if (strncmp (name, ".rel", 4) == 0)
7935 if (s->_raw_size == 0)
7937 /* We only strip the section if the output section name
7938 has the same name. Otherwise, there might be several
7939 input sections for this output section. FIXME: This
7940 code is probably not needed these days anyhow, since
7941 the linker now does not create empty output sections. */
7942 if (s->output_section != NULL
7944 bfd_get_section_name (s->output_section->owner,
7945 s->output_section)) == 0)
7950 const char *outname;
7953 /* If this relocation section applies to a read only
7954 section, then we probably need a DT_TEXTREL entry.
7955 If the relocation section is .rel.dyn, we always
7956 assert a DT_TEXTREL entry rather than testing whether
7957 there exists a relocation to a read only section or
7959 outname = bfd_get_section_name (output_bfd,
7961 target = bfd_get_section_by_name (output_bfd, outname + 4);
7963 && (target->flags & SEC_READONLY) != 0
7964 && (target->flags & SEC_ALLOC) != 0)
7966 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
7969 /* We use the reloc_count field as a counter if we need
7970 to copy relocs into the output file. */
7972 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
7976 else if (strncmp (name, ".got", 4) == 0)
7979 bfd_size_type loadable_size = 0;
7980 bfd_size_type local_gotno;
7983 BFD_ASSERT (elf_section_data (s) != NULL);
7984 g = (struct mips_got_info *) elf_section_data (s)->tdata;
7985 BFD_ASSERT (g != NULL);
7987 /* Calculate the total loadable size of the output. That
7988 will give us the maximum number of GOT_PAGE entries
7990 for (sub = info->input_bfds; sub; sub = sub->link_next)
7992 asection *subsection;
7994 for (subsection = sub->sections;
7996 subsection = subsection->next)
7998 if ((subsection->flags & SEC_ALLOC) == 0)
8000 loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
8003 loadable_size += MIPS_FUNCTION_STUB_SIZE;
8005 /* Assume there are two loadable segments consisting of
8006 contiguous sections. Is 5 enough? */
8007 local_gotno = (loadable_size >> 16) + 5;
8008 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8009 /* It's possible we will need GOT_PAGE entries as well as
8010 GOT16 entries. Often, these will be able to share GOT
8011 entries, but not always. */
8014 g->local_gotno += local_gotno;
8015 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
8017 /* There has to be a global GOT entry for every symbol with
8018 a dynamic symbol table index of DT_MIPS_GOTSYM or
8019 higher. Therefore, it make sense to put those symbols
8020 that need GOT entries at the end of the symbol table. We
8022 if (!mips_elf_sort_hash_table (info, 1))
8025 if (g->global_gotsym != NULL)
8026 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
8028 /* If there are no global symbols, or none requiring
8029 relocations, then GLOBAL_GOTSYM will be NULL. */
8031 g->global_gotno = i;
8032 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
8034 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
8036 /* Irix rld assumes that the function stub isn't at the end
8037 of .text section. So put a dummy. XXX */
8038 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8040 else if (! info->shared
8041 && ! mips_elf_hash_table (info)->use_rld_obj_head
8042 && strncmp (name, ".rld_map", 8) == 0)
8044 /* We add a room for __rld_map. It will be filled in by the
8045 rtld to contain a pointer to the _r_debug structure. */
8048 else if (SGI_COMPAT (output_bfd)
8049 && strncmp (name, ".compact_rel", 12) == 0)
8050 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
8051 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
8053 s->_raw_size = (sizeof (Elf32_External_Msym)
8054 * (elf_hash_table (info)->dynsymcount
8055 + bfd_count_sections (output_bfd)));
8056 else if (strncmp (name, ".init", 5) != 0)
8058 /* It's not one of our sections, so don't allocate space. */
8064 _bfd_strip_section_from_output (s);
8068 /* Allocate memory for the section contents. */
8069 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
8070 if (s->contents == NULL && s->_raw_size != 0)
8072 bfd_set_error (bfd_error_no_memory);
8077 if (elf_hash_table (info)->dynamic_sections_created)
8079 /* Add some entries to the .dynamic section. We fill in the
8080 values later, in elf_mips_finish_dynamic_sections, but we
8081 must add the entries now so that we get the correct size for
8082 the .dynamic section. The DT_DEBUG entry is filled in by the
8083 dynamic linker and used by the debugger. */
8086 if (SGI_COMPAT (output_bfd))
8088 /* SGI object has the equivalence of DT_DEBUG in the
8089 DT_MIPS_RLD_MAP entry. */
8090 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8094 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8100 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8104 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8107 if (bfd_get_section_by_name (dynobj,
8108 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
8110 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8113 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8116 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8120 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
8123 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
8126 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
8128 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
8131 s = bfd_get_section_by_name (dynobj, ".liblist");
8132 BFD_ASSERT (s != NULL);
8134 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
8138 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8141 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8145 /* Time stamps in executable files are a bad idea. */
8146 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
8151 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
8156 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
8160 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8163 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8166 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8169 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8172 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8175 if (IRIX_COMPAT (dynobj) == ict_irix5
8176 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8179 if (IRIX_COMPAT (dynobj) == ict_irix6
8180 && (bfd_get_section_by_name
8181 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8182 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8185 if (bfd_get_section_by_name (dynobj,
8186 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
8187 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
8194 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8195 adjust it appropriately now. */
8198 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
8199 bfd *abfd ATTRIBUTE_UNUSED;
8201 Elf_Internal_Sym *sym;
8203 /* The linker script takes care of providing names and values for
8204 these, but we must place them into the right sections. */
8205 static const char* const text_section_symbols[] = {
8208 "__dso_displacement",
8210 "__program_header_table",
8214 static const char* const data_section_symbols[] = {
8222 const char* const *p;
8225 for (i = 0; i < 2; ++i)
8226 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8229 if (strcmp (*p, name) == 0)
8231 /* All of these symbols are given type STT_SECTION by the
8233 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8235 /* The IRIX linker puts these symbols in special sections. */
8237 sym->st_shndx = SHN_MIPS_TEXT;
8239 sym->st_shndx = SHN_MIPS_DATA;
8245 /* Finish up dynamic symbol handling. We set the contents of various
8246 dynamic sections here. */
8249 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8251 struct bfd_link_info *info;
8252 struct elf_link_hash_entry *h;
8253 Elf_Internal_Sym *sym;
8259 struct mips_got_info *g;
8261 struct mips_elf_link_hash_entry *mh;
8263 dynobj = elf_hash_table (info)->dynobj;
8264 gval = sym->st_value;
8265 mh = (struct mips_elf_link_hash_entry *) h;
8267 if (h->plt.offset != (bfd_vma) -1)
8271 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
8273 /* This symbol has a stub. Set it up. */
8275 BFD_ASSERT (h->dynindx != -1);
8277 s = bfd_get_section_by_name (dynobj,
8278 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8279 BFD_ASSERT (s != NULL);
8281 /* Fill the stub. */
8283 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
8285 bfd_put_32 (output_bfd, STUB_MOVE, p);
8288 /* FIXME: Can h->dynindex be more than 64K? */
8289 if (h->dynindx & 0xffff0000)
8292 bfd_put_32 (output_bfd, STUB_JALR, p);
8294 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
8296 BFD_ASSERT (h->plt.offset <= s->_raw_size);
8297 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
8299 /* Mark the symbol as undefined. plt.offset != -1 occurs
8300 only for the referenced symbol. */
8301 sym->st_shndx = SHN_UNDEF;
8303 /* The run-time linker uses the st_value field of the symbol
8304 to reset the global offset table entry for this external
8305 to its stub address when unlinking a shared object. */
8306 gval = s->output_section->vma + s->output_offset + h->plt.offset;
8307 sym->st_value = gval;
8310 BFD_ASSERT (h->dynindx != -1);
8312 sgot = mips_elf_got_section (dynobj);
8313 BFD_ASSERT (sgot != NULL);
8314 BFD_ASSERT (elf_section_data (sgot) != NULL);
8315 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8316 BFD_ASSERT (g != NULL);
8318 /* Run through the global symbol table, creating GOT entries for all
8319 the symbols that need them. */
8320 if (g->global_gotsym != NULL
8321 && h->dynindx >= g->global_gotsym->dynindx)
8327 value = sym->st_value;
8329 /* For an entity defined in a shared object, this will be
8330 NULL. (For functions in shared objects for
8331 which we have created stubs, ST_VALUE will be non-NULL.
8332 That's because such the functions are now no longer defined
8333 in a shared object.) */
8334 value = h->root.u.def.value;
8336 offset = mips_elf_global_got_index (dynobj, h);
8337 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8340 /* Create a .msym entry, if appropriate. */
8341 smsym = bfd_get_section_by_name (dynobj,
8342 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8345 Elf32_Internal_Msym msym;
8347 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
8348 /* It is undocumented what the `1' indicates, but IRIX6 uses
8350 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
8351 bfd_mips_elf_swap_msym_out
8353 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
8356 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8357 name = h->root.root.string;
8358 if (strcmp (name, "_DYNAMIC") == 0
8359 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
8360 sym->st_shndx = SHN_ABS;
8361 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
8363 sym->st_shndx = SHN_ABS;
8364 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8367 else if (SGI_COMPAT (output_bfd))
8369 if (strcmp (name, "_gp_disp") == 0)
8371 sym->st_shndx = SHN_ABS;
8372 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8373 sym->st_value = elf_gp (output_bfd);
8375 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8376 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8378 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8379 sym->st_other = STO_PROTECTED;
8381 sym->st_shndx = SHN_MIPS_DATA;
8383 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8385 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8386 sym->st_other = STO_PROTECTED;
8387 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8388 sym->st_shndx = SHN_ABS;
8390 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8392 if (h->type == STT_FUNC)
8393 sym->st_shndx = SHN_MIPS_TEXT;
8394 else if (h->type == STT_OBJECT)
8395 sym->st_shndx = SHN_MIPS_DATA;
8399 /* Handle the IRIX6-specific symbols. */
8400 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8401 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8403 if (SGI_COMPAT (output_bfd)
8406 if (! mips_elf_hash_table (info)->use_rld_obj_head
8407 && strcmp (name, "__rld_map") == 0)
8409 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8410 BFD_ASSERT (s != NULL);
8411 sym->st_value = s->output_section->vma + s->output_offset;
8412 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
8413 if (mips_elf_hash_table (info)->rld_value == 0)
8414 mips_elf_hash_table (info)->rld_value = sym->st_value;
8416 else if (mips_elf_hash_table (info)->use_rld_obj_head
8417 && strcmp (name, "__rld_obj_head") == 0)
8419 /* IRIX6 does not use a .rld_map section. */
8420 if (IRIX_COMPAT (output_bfd) == ict_irix5)
8421 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8423 mips_elf_hash_table (info)->rld_value = sym->st_value;
8427 /* If this is a mips16 symbol, force the value to be even. */
8428 if (sym->st_other == STO_MIPS16
8429 && (sym->st_value & 1) != 0)
8435 /* Finish up the dynamic sections. */
8438 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
8440 struct bfd_link_info *info;
8445 struct mips_got_info *g;
8447 dynobj = elf_hash_table (info)->dynobj;
8449 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8451 sgot = mips_elf_got_section (dynobj);
8456 BFD_ASSERT (elf_section_data (sgot) != NULL);
8457 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8458 BFD_ASSERT (g != NULL);
8461 if (elf_hash_table (info)->dynamic_sections_created)
8465 BFD_ASSERT (sdyn != NULL);
8466 BFD_ASSERT (g != NULL);
8468 for (b = sdyn->contents;
8469 b < sdyn->contents + sdyn->_raw_size;
8470 b += MIPS_ELF_DYN_SIZE (dynobj))
8472 Elf_Internal_Dyn dyn;
8478 /* Read in the current dynamic entry. */
8479 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8481 /* Assume that we're going to modify it and write it out. */
8487 s = (bfd_get_section_by_name
8489 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
8490 BFD_ASSERT (s != NULL);
8491 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8495 /* Rewrite DT_STRSZ. */
8497 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
8503 case DT_MIPS_CONFLICT:
8506 case DT_MIPS_LIBLIST:
8509 s = bfd_get_section_by_name (output_bfd, name);
8510 BFD_ASSERT (s != NULL);
8511 dyn.d_un.d_ptr = s->vma;
8514 case DT_MIPS_RLD_VERSION:
8515 dyn.d_un.d_val = 1; /* XXX */
8519 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8522 case DT_MIPS_CONFLICTNO:
8524 elemsize = sizeof (Elf32_Conflict);
8527 case DT_MIPS_LIBLISTNO:
8529 elemsize = sizeof (Elf32_Lib);
8531 s = bfd_get_section_by_name (output_bfd, name);
8534 if (s->_cooked_size != 0)
8535 dyn.d_un.d_val = s->_cooked_size / elemsize;
8537 dyn.d_un.d_val = s->_raw_size / elemsize;
8543 case DT_MIPS_TIME_STAMP:
8544 time ((time_t *) &dyn.d_un.d_val);
8547 case DT_MIPS_ICHECKSUM:
8552 case DT_MIPS_IVERSION:
8557 case DT_MIPS_BASE_ADDRESS:
8558 s = output_bfd->sections;
8559 BFD_ASSERT (s != NULL);
8560 dyn.d_un.d_ptr = s->vma & ~(0xffff);
8563 case DT_MIPS_LOCAL_GOTNO:
8564 dyn.d_un.d_val = g->local_gotno;
8567 case DT_MIPS_UNREFEXTNO:
8568 /* The index into the dynamic symbol table which is the
8569 entry of the first external symbol that is not
8570 referenced within the same object. */
8571 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8574 case DT_MIPS_GOTSYM:
8575 if (g->global_gotsym)
8577 dyn.d_un.d_val = g->global_gotsym->dynindx;
8580 /* In case if we don't have global got symbols we default
8581 to setting DT_MIPS_GOTSYM to the same value as
8582 DT_MIPS_SYMTABNO, so we just fall through. */
8584 case DT_MIPS_SYMTABNO:
8586 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8587 s = bfd_get_section_by_name (output_bfd, name);
8588 BFD_ASSERT (s != NULL);
8590 if (s->_cooked_size != 0)
8591 dyn.d_un.d_val = s->_cooked_size / elemsize;
8593 dyn.d_un.d_val = s->_raw_size / elemsize;
8596 case DT_MIPS_HIPAGENO:
8597 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8600 case DT_MIPS_RLD_MAP:
8601 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8604 case DT_MIPS_OPTIONS:
8605 s = (bfd_get_section_by_name
8606 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8607 dyn.d_un.d_ptr = s->vma;
8611 s = (bfd_get_section_by_name
8612 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8613 dyn.d_un.d_ptr = s->vma;
8622 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8627 /* The first entry of the global offset table will be filled at
8628 runtime. The second entry will be used by some runtime loaders.
8629 This isn't the case of Irix rld. */
8630 if (sgot != NULL && sgot->_raw_size > 0)
8632 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8633 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8634 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8638 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8639 = MIPS_ELF_GOT_SIZE (output_bfd);
8644 Elf32_compact_rel cpt;
8646 /* ??? The section symbols for the output sections were set up in
8647 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
8648 symbols. Should we do so? */
8650 smsym = bfd_get_section_by_name (dynobj,
8651 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8654 Elf32_Internal_Msym msym;
8656 msym.ms_hash_value = 0;
8657 msym.ms_info = ELF32_MS_INFO (0, 1);
8659 for (s = output_bfd->sections; s != NULL; s = s->next)
8661 long dynindx = elf_section_data (s)->dynindx;
8663 bfd_mips_elf_swap_msym_out
8665 (((Elf32_External_Msym *) smsym->contents)
8670 if (SGI_COMPAT (output_bfd))
8672 /* Write .compact_rel section out. */
8673 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8677 cpt.num = s->reloc_count;
8679 cpt.offset = (s->output_section->filepos
8680 + sizeof (Elf32_External_compact_rel));
8683 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8684 ((Elf32_External_compact_rel *)
8687 /* Clean up a dummy stub function entry in .text. */
8688 s = bfd_get_section_by_name (dynobj,
8689 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8692 file_ptr dummy_offset;
8694 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8695 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8696 memset (s->contents + dummy_offset, 0,
8697 MIPS_FUNCTION_STUB_SIZE);
8702 /* Clean up a first relocation in .rel.dyn. */
8703 s = bfd_get_section_by_name (dynobj,
8704 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
8705 if (s != NULL && s->_raw_size > 0)
8706 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
8712 /* This is almost identical to bfd_generic_get_... except that some
8713 MIPS relocations need to be handled specially. Sigh. */
8716 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8717 relocateable, symbols)
8719 struct bfd_link_info *link_info;
8720 struct bfd_link_order *link_order;
8722 boolean relocateable;
8725 /* Get enough memory to hold the stuff */
8726 bfd *input_bfd = link_order->u.indirect.section->owner;
8727 asection *input_section = link_order->u.indirect.section;
8729 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8730 arelent **reloc_vector = NULL;
8736 reloc_vector = (arelent **) bfd_malloc (reloc_size);
8737 if (reloc_vector == NULL && reloc_size != 0)
8740 /* read in the section */
8741 if (!bfd_get_section_contents (input_bfd,
8745 input_section->_raw_size))
8748 /* We're not relaxing the section, so just copy the size info */
8749 input_section->_cooked_size = input_section->_raw_size;
8750 input_section->reloc_done = true;
8752 reloc_count = bfd_canonicalize_reloc (input_bfd,
8756 if (reloc_count < 0)
8759 if (reloc_count > 0)
8764 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8767 struct bfd_hash_entry *h;
8768 struct bfd_link_hash_entry *lh;
8769 /* Skip all this stuff if we aren't mixing formats. */
8770 if (abfd && input_bfd
8771 && abfd->xvec == input_bfd->xvec)
8775 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8776 lh = (struct bfd_link_hash_entry *) h;
8783 case bfd_link_hash_undefined:
8784 case bfd_link_hash_undefweak:
8785 case bfd_link_hash_common:
8788 case bfd_link_hash_defined:
8789 case bfd_link_hash_defweak:
8791 gp = lh->u.def.value;
8793 case bfd_link_hash_indirect:
8794 case bfd_link_hash_warning:
8796 /* @@FIXME ignoring warning for now */
8798 case bfd_link_hash_new:
8807 for (parent = reloc_vector; *parent != (arelent *) NULL;
8810 char *error_message = (char *) NULL;
8811 bfd_reloc_status_type r;
8813 /* Specific to MIPS: Deal with relocation types that require
8814 knowing the gp of the output bfd. */
8815 asymbol *sym = *(*parent)->sym_ptr_ptr;
8816 if (bfd_is_abs_section (sym->section) && abfd)
8818 /* The special_function wouldn't get called anyways. */
8822 /* The gp isn't there; let the special function code
8823 fall over on its own. */
8825 else if ((*parent)->howto->special_function
8826 == _bfd_mips_elf_gprel16_reloc)
8828 /* bypass special_function call */
8829 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8830 relocateable, (PTR) data, gp);
8831 goto skip_bfd_perform_relocation;
8833 /* end mips specific stuff */
8835 r = bfd_perform_relocation (input_bfd,
8839 relocateable ? abfd : (bfd *) NULL,
8841 skip_bfd_perform_relocation:
8845 asection *os = input_section->output_section;
8847 /* A partial link, so keep the relocs */
8848 os->orelocation[os->reloc_count] = *parent;
8852 if (r != bfd_reloc_ok)
8856 case bfd_reloc_undefined:
8857 if (!((*link_info->callbacks->undefined_symbol)
8858 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8859 input_bfd, input_section, (*parent)->address)))
8862 case bfd_reloc_dangerous:
8863 BFD_ASSERT (error_message != (char *) NULL);
8864 if (!((*link_info->callbacks->reloc_dangerous)
8865 (link_info, error_message, input_bfd, input_section,
8866 (*parent)->address)))
8869 case bfd_reloc_overflow:
8870 if (!((*link_info->callbacks->reloc_overflow)
8871 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8872 (*parent)->howto->name, (*parent)->addend,
8873 input_bfd, input_section, (*parent)->address)))
8876 case bfd_reloc_outofrange:
8885 if (reloc_vector != NULL)
8886 free (reloc_vector);
8890 if (reloc_vector != NULL)
8891 free (reloc_vector);
8894 #define bfd_elf32_bfd_get_relocated_section_contents \
8895 elf32_mips_get_relocated_section_contents
8897 /* ECOFF swapping routines. These are used when dealing with the
8898 .mdebug section, which is in the ECOFF debugging format. */
8899 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8901 /* Symbol table magic number. */
8903 /* Alignment of debugging information. E.g., 4. */
8905 /* Sizes of external symbolic information. */
8906 sizeof (struct hdr_ext),
8907 sizeof (struct dnr_ext),
8908 sizeof (struct pdr_ext),
8909 sizeof (struct sym_ext),
8910 sizeof (struct opt_ext),
8911 sizeof (struct fdr_ext),
8912 sizeof (struct rfd_ext),
8913 sizeof (struct ext_ext),
8914 /* Functions to swap in external symbolic data. */
8923 _bfd_ecoff_swap_tir_in,
8924 _bfd_ecoff_swap_rndx_in,
8925 /* Functions to swap out external symbolic data. */
8934 _bfd_ecoff_swap_tir_out,
8935 _bfd_ecoff_swap_rndx_out,
8936 /* Function to read in symbolic data. */
8937 _bfd_mips_elf_read_ecoff_info
8940 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8941 #define TARGET_LITTLE_NAME "elf32-littlemips"
8942 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8943 #define TARGET_BIG_NAME "elf32-bigmips"
8944 #define ELF_ARCH bfd_arch_mips
8945 #define ELF_MACHINE_CODE EM_MIPS
8947 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8948 a value of 0x1000, and we are compatible. */
8949 #define ELF_MAXPAGESIZE 0x1000
8951 #define elf_backend_collect true
8952 #define elf_backend_type_change_ok true
8953 #define elf_backend_can_gc_sections true
8954 #define elf_info_to_howto mips_info_to_howto_rela
8955 #define elf_info_to_howto_rel mips_info_to_howto_rel
8956 #define elf_backend_sym_is_global mips_elf_sym_is_global
8957 #define elf_backend_object_p _bfd_mips_elf_object_p
8958 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
8959 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8960 #define elf_backend_section_from_bfd_section \
8961 _bfd_mips_elf_section_from_bfd_section
8962 #define elf_backend_section_processing _bfd_mips_elf_section_processing
8963 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8964 #define elf_backend_additional_program_headers \
8965 _bfd_mips_elf_additional_program_headers
8966 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
8967 #define elf_backend_final_write_processing \
8968 _bfd_mips_elf_final_write_processing
8969 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8970 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
8971 #define elf_backend_create_dynamic_sections \
8972 _bfd_mips_elf_create_dynamic_sections
8973 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
8974 #define elf_backend_adjust_dynamic_symbol \
8975 _bfd_mips_elf_adjust_dynamic_symbol
8976 #define elf_backend_always_size_sections \
8977 _bfd_mips_elf_always_size_sections
8978 #define elf_backend_size_dynamic_sections \
8979 _bfd_mips_elf_size_dynamic_sections
8980 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
8981 #define elf_backend_link_output_symbol_hook \
8982 _bfd_mips_elf_link_output_symbol_hook
8983 #define elf_backend_finish_dynamic_symbol \
8984 _bfd_mips_elf_finish_dynamic_symbol
8985 #define elf_backend_finish_dynamic_sections \
8986 _bfd_mips_elf_finish_dynamic_sections
8987 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
8988 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8990 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8991 #define elf_backend_plt_header_size 0
8993 #define bfd_elf32_bfd_is_local_label_name \
8994 mips_elf_is_local_label_name
8995 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8996 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8997 #define bfd_elf32_bfd_link_hash_table_create \
8998 _bfd_mips_elf_link_hash_table_create
8999 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
9000 #define bfd_elf32_bfd_copy_private_bfd_data \
9001 _bfd_mips_elf_copy_private_bfd_data
9002 #define bfd_elf32_bfd_merge_private_bfd_data \
9003 _bfd_mips_elf_merge_private_bfd_data
9004 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
9005 #define bfd_elf32_bfd_print_private_bfd_data \
9006 _bfd_mips_elf_print_private_bfd_data
9007 #include "elf32-target.h"