1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
26 different MIPS ELF from other targets. This matters when linking.
27 This file supports both, switching at runtime. */
37 /* Get the ECOFF swapping routines. */
39 #include "coff/symconst.h"
40 #include "coff/internal.h"
41 #include "coff/ecoff.h"
42 #include "coff/mips.h"
44 #include "ecoffswap.h"
46 /* This structure is used to hold .got information when linking. It
47 is stored in the tdata field of the bfd_elf_section_data structure. */
51 /* The global symbol in the GOT with the lowest index in the dynamic
53 struct elf_link_hash_entry *global_gotsym;
54 /* The number of 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 void mips_info_to_howto_rel
102 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
103 static void mips_info_to_howto_rela
104 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
105 static void bfd_mips_elf32_swap_gptab_in
106 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
107 static void bfd_mips_elf32_swap_gptab_out
108 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
109 static void bfd_mips_elf_swap_msym_in
110 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
111 static void bfd_mips_elf_swap_msym_out
112 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
113 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
114 static boolean mips_elf_create_procedure_table
115 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
116 struct ecoff_debug_info *));
117 static INLINE int elf_mips_isa PARAMS ((flagword));
118 static INLINE int elf_mips_mach PARAMS ((flagword));
119 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
120 static boolean mips_elf_is_local_label_name
121 PARAMS ((bfd *, const char *));
122 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
123 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
124 static int gptab_compare PARAMS ((const void *, const void *));
125 static void mips_elf_relocate_hi16
126 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
128 static boolean mips_elf_relocate_got_local
129 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
130 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
131 static void mips_elf_relocate_global_got
132 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
133 static bfd_reloc_status_type mips16_jump_reloc
134 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
135 static bfd_reloc_status_type mips16_gprel_reloc
136 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
137 static boolean mips_elf_create_compact_rel_section
138 PARAMS ((bfd *, struct bfd_link_info *));
139 static boolean mips_elf_create_got_section
140 PARAMS ((bfd *, struct bfd_link_info *));
141 static bfd_reloc_status_type mips_elf_final_gp
142 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
143 static bfd_byte *elf32_mips_get_relocated_section_contents
144 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
145 bfd_byte *, boolean, asymbol **));
146 static asection *mips_elf_create_msym_section
148 static void mips_elf_irix6_finish_dynamic_symbol
149 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
150 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
151 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
152 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
153 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
154 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
155 static bfd_vma mips_elf_global_got_index
156 PARAMS ((bfd *, struct elf_link_hash_entry *));
157 static bfd_vma mips_elf_local_got_index
158 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
159 static bfd_vma mips_elf_got_offset_from_index
160 PARAMS ((bfd *, bfd *, bfd_vma));
161 static boolean mips_elf_record_global_got_symbol
162 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
163 struct mips_got_info *));
164 static bfd_vma mips_elf_got_page
165 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
166 static boolean mips_elf_next_lo16_addend
167 PARAMS ((const Elf_Internal_Rela *, const Elf_Internal_Rela *, bfd_vma *));
168 static bfd_reloc_status_type mips_elf_calculate_relocation
169 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
170 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
171 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
173 static bfd_vma mips_elf_obtain_contents
174 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
175 static boolean mips_elf_perform_relocation
176 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
177 const Elf_Internal_Rela *, bfd_vma,
178 bfd *, asection *, bfd_byte *, boolean));
179 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
180 static boolean mips_elf_sort_hash_table_f
181 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
182 static boolean mips_elf_sort_hash_table
183 PARAMS ((struct bfd_link_info *, unsigned long));
184 static asection * mips_elf_got_section PARAMS ((bfd *));
185 static struct mips_got_info *mips_elf_got_info
186 PARAMS ((bfd *, asection **));
187 static boolean mips_elf_local_relocation_p
188 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **));
189 static bfd_vma mips_elf_create_local_got_entry
190 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
191 static bfd_vma mips_elf_got16_entry
192 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
193 static unsigned int mips_elf_create_dynamic_relocation
194 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
195 long, bfd_vma, asection *));
196 static void mips_elf_allocate_dynamic_relocations
197 PARAMS ((bfd *, unsigned int));
198 static boolean mips_elf_stub_section_p
199 PARAMS ((bfd *, asection *));
201 /* The level of IRIX compatibility we're striving for. */
209 /* Nonzero if ABFD is using the N32 ABI. */
211 #define ABI_N32_P(abfd) \
212 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
214 /* Nonzero if ABFD is using the 64-bit ABI. FIXME: This is never
216 #define ABI_64_P(abfd) \
217 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
219 /* What version of Irix we are trying to be compatible with. FIXME:
220 At the moment, we never generate "normal" MIPS ELF ABI executables;
221 we always use some version of Irix. */
223 #define IRIX_COMPAT(abfd) \
224 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5)
226 /* Whether we are trying to be compatible with IRIX at all. */
228 #define SGI_COMPAT(abfd) \
229 (IRIX_COMPAT (abfd) != ict_none)
231 /* The name of the msym section. */
232 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
234 /* The name of the srdata section. */
235 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
237 /* The name of the options section. */
238 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
239 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
241 /* The name of the stub section. */
242 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
243 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
245 /* The name of the dynamic relocation section. */
246 #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
248 /* The size of an external REL relocation. */
249 #define MIPS_ELF_REL_SIZE(abfd) \
250 (get_elf_backend_data (abfd)->s->sizeof_rel)
252 /* The size of an external dynamic table entry. */
253 #define MIPS_ELF_DYN_SIZE(abfd) \
254 (get_elf_backend_data (abfd)->s->sizeof_dyn)
256 /* The size of a GOT entry. */
257 #define MIPS_ELF_GOT_SIZE(abfd) \
258 (get_elf_backend_data (abfd)->s->arch_size / 8)
260 /* The size of a symbol-table entry. */
261 #define MIPS_ELF_SYM_SIZE(abfd) \
262 (get_elf_backend_data (abfd)->s->sizeof_sym)
264 /* The default alignment for sections, as a power of two. */
265 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
266 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
268 /* Get word-sized data. */
269 #define MIPS_ELF_GET_WORD(abfd, ptr) \
270 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
272 /* Put out word-sized data. */
273 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
275 ? bfd_put_64 (abfd, val, ptr) \
276 : bfd_put_32 (abfd, val, ptr))
278 /* Add a dynamic symbol table-entry. */
280 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
281 (ABI_64_P (elf_hash_table (info)->dynobj) \
282 ? bfd_elf64_add_dynamic_entry (info, tag, val) \
283 : bfd_elf32_add_dynamic_entry (info, tag, val))
285 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
286 (ABI_64_P (elf_hash_table (info)->dynobj) \
287 ? (abort (), false) \
288 : bfd_elf32_add_dynamic_entry (info, tag, val))
291 /* The number of local .got entries we reserve. */
292 #define MIPS_RESERVED_GOTNO (2)
294 /* Instructions which appear in a stub. For some reason the stub is
295 slightly different on an SGI system. */
296 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
297 #define STUB_LW(abfd) \
300 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
301 : 0x8f998010) /* lw t9,0x8010(gp) */ \
302 : 0x8f998000) /* lw t9,0x8000(gp) */
303 #define STUB_MOVE 0x03e07825 /* move t7,ra */
304 #define STUB_JALR 0x0320f809 /* jal t9 */
305 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
306 #define MIPS_FUNCTION_STUB_SIZE (16)
309 /* We no longer try to identify particular sections for the .dynsym
310 section. When we do, we wind up crashing if there are other random
311 sections with relocations. */
313 /* Names of sections which appear in the .dynsym section in an Irix 5
316 static const char * const mips_elf_dynsym_sec_names[] =
329 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
330 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
332 /* The number of entries in mips_elf_dynsym_sec_names which go in the
335 #define MIPS_TEXT_DYNSYM_SECNO (3)
339 /* The names of the runtime procedure table symbols used on Irix 5. */
341 static const char * const mips_elf_dynsym_rtproc_names[] =
344 "_procedure_string_table",
345 "_procedure_table_size",
349 /* These structures are used to generate the .compact_rel section on
354 unsigned long id1; /* Always one? */
355 unsigned long num; /* Number of compact relocation entries. */
356 unsigned long id2; /* Always two? */
357 unsigned long offset; /* The file offset of the first relocation. */
358 unsigned long reserved0; /* Zero? */
359 unsigned long reserved1; /* Zero? */
368 bfd_byte reserved0[4];
369 bfd_byte reserved1[4];
370 } Elf32_External_compact_rel;
374 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
375 unsigned int rtype : 4; /* Relocation types. See below. */
376 unsigned int dist2to : 8;
377 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
378 unsigned long konst; /* KONST field. See below. */
379 unsigned long vaddr; /* VADDR to be relocated. */
384 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
385 unsigned int rtype : 4; /* Relocation types. See below. */
386 unsigned int dist2to : 8;
387 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
388 unsigned long konst; /* KONST field. See below. */
396 } Elf32_External_crinfo;
402 } Elf32_External_crinfo2;
404 /* These are the constants used to swap the bitfields in a crinfo. */
406 #define CRINFO_CTYPE (0x1)
407 #define CRINFO_CTYPE_SH (31)
408 #define CRINFO_RTYPE (0xf)
409 #define CRINFO_RTYPE_SH (27)
410 #define CRINFO_DIST2TO (0xff)
411 #define CRINFO_DIST2TO_SH (19)
412 #define CRINFO_RELVADDR (0x7ffff)
413 #define CRINFO_RELVADDR_SH (0)
415 /* A compact relocation info has long (3 words) or short (2 words)
416 formats. A short format doesn't have VADDR field and relvaddr
417 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
418 #define CRF_MIPS_LONG 1
419 #define CRF_MIPS_SHORT 0
421 /* There are 4 types of compact relocation at least. The value KONST
422 has different meaning for each type:
425 CT_MIPS_REL32 Address in data
426 CT_MIPS_WORD Address in word (XXX)
427 CT_MIPS_GPHI_LO GP - vaddr
428 CT_MIPS_JMPAD Address to jump
431 #define CRT_MIPS_REL32 0xa
432 #define CRT_MIPS_WORD 0xb
433 #define CRT_MIPS_GPHI_LO 0xc
434 #define CRT_MIPS_JMPAD 0xd
436 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
437 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
438 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
439 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
441 static void bfd_elf32_swap_compact_rel_out
442 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
443 static void bfd_elf32_swap_crinfo_out
444 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
446 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
448 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
449 from smaller values. Start with zero, widen, *then* decrement. */
450 #define MINUS_ONE (((bfd_vma)0) - 1)
452 static reloc_howto_type elf_mips_howto_table[] =
455 HOWTO (R_MIPS_NONE, /* type */
457 0, /* size (0 = byte, 1 = short, 2 = long) */
459 false, /* pc_relative */
461 complain_overflow_dont, /* complain_on_overflow */
462 bfd_elf_generic_reloc, /* special_function */
463 "R_MIPS_NONE", /* name */
464 false, /* partial_inplace */
467 false), /* pcrel_offset */
469 /* 16 bit relocation. */
470 HOWTO (R_MIPS_16, /* type */
472 1, /* size (0 = byte, 1 = short, 2 = long) */
474 false, /* pc_relative */
476 complain_overflow_bitfield, /* complain_on_overflow */
477 bfd_elf_generic_reloc, /* special_function */
478 "R_MIPS_16", /* name */
479 true, /* partial_inplace */
480 0xffff, /* src_mask */
481 0xffff, /* dst_mask */
482 false), /* pcrel_offset */
484 /* 32 bit relocation. */
485 HOWTO (R_MIPS_32, /* type */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
489 false, /* pc_relative */
491 complain_overflow_bitfield, /* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_MIPS_32", /* name */
494 true, /* partial_inplace */
495 0xffffffff, /* src_mask */
496 0xffffffff, /* dst_mask */
497 false), /* pcrel_offset */
499 /* 32 bit symbol relative relocation. */
500 HOWTO (R_MIPS_REL32, /* type */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
504 false, /* pc_relative */
506 complain_overflow_bitfield, /* complain_on_overflow */
507 bfd_elf_generic_reloc, /* special_function */
508 "R_MIPS_REL32", /* name */
509 true, /* partial_inplace */
510 0xffffffff, /* src_mask */
511 0xffffffff, /* dst_mask */
512 false), /* pcrel_offset */
514 /* 26 bit branch address. */
515 HOWTO (R_MIPS_26, /* type */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
519 false, /* pc_relative */
521 complain_overflow_dont, /* complain_on_overflow */
522 /* This needs complex overflow
523 detection, because the upper four
524 bits must match the PC. */
525 bfd_elf_generic_reloc, /* special_function */
526 "R_MIPS_26", /* name */
527 true, /* partial_inplace */
528 0x3ffffff, /* src_mask */
529 0x3ffffff, /* dst_mask */
530 false), /* pcrel_offset */
532 /* High 16 bits of symbol value. */
533 HOWTO (R_MIPS_HI16, /* type */
535 2, /* size (0 = byte, 1 = short, 2 = long) */
537 false, /* pc_relative */
539 complain_overflow_dont, /* complain_on_overflow */
540 _bfd_mips_elf_hi16_reloc, /* special_function */
541 "R_MIPS_HI16", /* name */
542 true, /* partial_inplace */
543 0xffff, /* src_mask */
544 0xffff, /* dst_mask */
545 false), /* pcrel_offset */
547 /* Low 16 bits of symbol value. */
548 HOWTO (R_MIPS_LO16, /* type */
550 2, /* size (0 = byte, 1 = short, 2 = long) */
552 false, /* pc_relative */
554 complain_overflow_dont, /* complain_on_overflow */
555 _bfd_mips_elf_lo16_reloc, /* special_function */
556 "R_MIPS_LO16", /* name */
557 true, /* partial_inplace */
558 0xffff, /* src_mask */
559 0xffff, /* dst_mask */
560 false), /* pcrel_offset */
562 /* GP relative reference. */
563 HOWTO (R_MIPS_GPREL16, /* type */
565 2, /* size (0 = byte, 1 = short, 2 = long) */
567 false, /* pc_relative */
569 complain_overflow_signed, /* complain_on_overflow */
570 _bfd_mips_elf_gprel16_reloc, /* special_function */
571 "R_MIPS_GPREL16", /* name */
572 true, /* partial_inplace */
573 0xffff, /* src_mask */
574 0xffff, /* dst_mask */
575 false), /* pcrel_offset */
577 /* Reference to literal section. */
578 HOWTO (R_MIPS_LITERAL, /* type */
580 2, /* size (0 = byte, 1 = short, 2 = long) */
582 false, /* pc_relative */
584 complain_overflow_signed, /* complain_on_overflow */
585 _bfd_mips_elf_gprel16_reloc, /* special_function */
586 "R_MIPS_LITERAL", /* name */
587 true, /* partial_inplace */
588 0xffff, /* src_mask */
589 0xffff, /* dst_mask */
590 false), /* pcrel_offset */
592 /* Reference to global offset table. */
593 HOWTO (R_MIPS_GOT16, /* type */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
597 false, /* pc_relative */
599 complain_overflow_signed, /* complain_on_overflow */
600 _bfd_mips_elf_got16_reloc, /* special_function */
601 "R_MIPS_GOT16", /* name */
602 false, /* partial_inplace */
604 0xffff, /* dst_mask */
605 false), /* pcrel_offset */
607 /* 16 bit PC relative reference. */
608 HOWTO (R_MIPS_PC16, /* type */
610 2, /* size (0 = byte, 1 = short, 2 = long) */
612 true, /* pc_relative */
614 complain_overflow_signed, /* complain_on_overflow */
615 bfd_elf_generic_reloc, /* special_function */
616 "R_MIPS_PC16", /* name */
617 true, /* partial_inplace */
618 0xffff, /* src_mask */
619 0xffff, /* dst_mask */
620 false), /* pcrel_offset */
622 /* 16 bit call through global offset table. */
623 HOWTO (R_MIPS_CALL16, /* type */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
627 false, /* pc_relative */
629 complain_overflow_signed, /* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_MIPS_CALL16", /* name */
632 false, /* partial_inplace */
634 0xffff, /* dst_mask */
635 false), /* pcrel_offset */
637 /* 32 bit GP relative reference. */
638 HOWTO (R_MIPS_GPREL32, /* type */
640 2, /* size (0 = byte, 1 = short, 2 = long) */
642 false, /* pc_relative */
644 complain_overflow_bitfield, /* complain_on_overflow */
645 _bfd_mips_elf_gprel32_reloc, /* special_function */
646 "R_MIPS_GPREL32", /* name */
647 true, /* partial_inplace */
648 0xffffffff, /* src_mask */
649 0xffffffff, /* dst_mask */
650 false), /* pcrel_offset */
652 /* The remaining relocs are defined on Irix 5, although they are
653 not defined by the ABI. */
658 /* A 5 bit shift field. */
659 HOWTO (R_MIPS_SHIFT5, /* type */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
663 false, /* pc_relative */
665 complain_overflow_bitfield, /* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_MIPS_SHIFT5", /* name */
668 true, /* partial_inplace */
669 0x000007c0, /* src_mask */
670 0x000007c0, /* dst_mask */
671 false), /* pcrel_offset */
673 /* A 6 bit shift field. */
674 /* FIXME: This is not handled correctly; a special function is
675 needed to put the most significant bit in the right place. */
676 HOWTO (R_MIPS_SHIFT6, /* type */
678 2, /* size (0 = byte, 1 = short, 2 = long) */
680 false, /* pc_relative */
682 complain_overflow_bitfield, /* complain_on_overflow */
683 bfd_elf_generic_reloc, /* special_function */
684 "R_MIPS_SHIFT6", /* name */
685 true, /* partial_inplace */
686 0x000007c4, /* src_mask */
687 0x000007c4, /* dst_mask */
688 false), /* pcrel_offset */
690 /* A 64 bit relocation. */
691 HOWTO (R_MIPS_64, /* type */
693 4, /* size (0 = byte, 1 = short, 2 = long) */
695 false, /* pc_relative */
697 complain_overflow_bitfield, /* complain_on_overflow */
698 mips32_64bit_reloc, /* special_function */
699 "R_MIPS_64", /* name */
700 true, /* partial_inplace */
701 MINUS_ONE, /* src_mask */
702 MINUS_ONE, /* dst_mask */
703 false), /* pcrel_offset */
705 /* Displacement in the global offset table. */
706 HOWTO (R_MIPS_GOT_DISP, /* type */
708 2, /* size (0 = byte, 1 = short, 2 = long) */
710 false, /* pc_relative */
712 complain_overflow_bitfield, /* complain_on_overflow */
713 bfd_elf_generic_reloc, /* special_function */
714 "R_MIPS_GOT_DISP", /* name */
715 true, /* partial_inplace */
716 0x0000ffff, /* src_mask */
717 0x0000ffff, /* dst_mask */
718 false), /* pcrel_offset */
720 /* Displacement to page pointer in the global offset table. */
721 HOWTO (R_MIPS_GOT_PAGE, /* type */
723 2, /* size (0 = byte, 1 = short, 2 = long) */
725 false, /* pc_relative */
727 complain_overflow_bitfield, /* complain_on_overflow */
728 bfd_elf_generic_reloc, /* special_function */
729 "R_MIPS_GOT_PAGE", /* name */
730 true, /* partial_inplace */
731 0x0000ffff, /* src_mask */
732 0x0000ffff, /* dst_mask */
733 false), /* pcrel_offset */
735 /* Offset from page pointer in the global offset table. */
736 HOWTO (R_MIPS_GOT_OFST, /* type */
738 2, /* size (0 = byte, 1 = short, 2 = long) */
740 false, /* pc_relative */
742 complain_overflow_bitfield, /* complain_on_overflow */
743 bfd_elf_generic_reloc, /* special_function */
744 "R_MIPS_GOT_OFST", /* name */
745 true, /* partial_inplace */
746 0x0000ffff, /* src_mask */
747 0x0000ffff, /* dst_mask */
748 false), /* pcrel_offset */
750 /* High 16 bits of displacement in global offset table. */
751 HOWTO (R_MIPS_GOT_HI16, /* type */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
755 false, /* pc_relative */
757 complain_overflow_dont, /* complain_on_overflow */
758 bfd_elf_generic_reloc, /* special_function */
759 "R_MIPS_GOT_HI16", /* name */
760 true, /* partial_inplace */
761 0x0000ffff, /* src_mask */
762 0x0000ffff, /* dst_mask */
763 false), /* pcrel_offset */
765 /* Low 16 bits of displacement in global offset table. */
766 HOWTO (R_MIPS_GOT_LO16, /* type */
768 2, /* size (0 = byte, 1 = short, 2 = long) */
770 false, /* pc_relative */
772 complain_overflow_dont, /* complain_on_overflow */
773 bfd_elf_generic_reloc, /* special_function */
774 "R_MIPS_GOT_LO16", /* name */
775 true, /* partial_inplace */
776 0x0000ffff, /* src_mask */
777 0x0000ffff, /* dst_mask */
778 false), /* pcrel_offset */
780 /* 64 bit subtraction. Used in the N32 ABI. */
781 HOWTO (R_MIPS_SUB, /* type */
783 4, /* size (0 = byte, 1 = short, 2 = long) */
785 false, /* pc_relative */
787 complain_overflow_bitfield, /* complain_on_overflow */
788 bfd_elf_generic_reloc, /* special_function */
789 "R_MIPS_SUB", /* name */
790 true, /* partial_inplace */
791 MINUS_ONE, /* src_mask */
792 MINUS_ONE, /* dst_mask */
793 false), /* pcrel_offset */
795 /* Used to cause the linker to insert and delete instructions? */
796 EMPTY_HOWTO (R_MIPS_INSERT_A),
797 EMPTY_HOWTO (R_MIPS_INSERT_B),
798 EMPTY_HOWTO (R_MIPS_DELETE),
800 /* Get the higher value of a 64 bit addend. */
801 HOWTO (R_MIPS_HIGHER, /* type */
803 2, /* size (0 = byte, 1 = short, 2 = long) */
805 false, /* pc_relative */
807 complain_overflow_dont, /* complain_on_overflow */
808 bfd_elf_generic_reloc, /* special_function */
809 "R_MIPS_HIGHER", /* name */
810 true, /* partial_inplace */
812 0xffff, /* dst_mask */
813 false), /* pcrel_offset */
815 /* Get the highest value of a 64 bit addend. */
816 HOWTO (R_MIPS_HIGHEST, /* type */
818 2, /* size (0 = byte, 1 = short, 2 = long) */
820 false, /* pc_relative */
822 complain_overflow_dont, /* complain_on_overflow */
823 bfd_elf_generic_reloc, /* special_function */
824 "R_MIPS_HIGHEST", /* name */
825 true, /* partial_inplace */
827 0xffff, /* dst_mask */
828 false), /* pcrel_offset */
830 /* High 16 bits of displacement in global offset table. */
831 HOWTO (R_MIPS_CALL_HI16, /* type */
833 2, /* size (0 = byte, 1 = short, 2 = long) */
835 false, /* pc_relative */
837 complain_overflow_dont, /* complain_on_overflow */
838 bfd_elf_generic_reloc, /* special_function */
839 "R_MIPS_CALL_HI16", /* name */
840 true, /* partial_inplace */
841 0x0000ffff, /* src_mask */
842 0x0000ffff, /* dst_mask */
843 false), /* pcrel_offset */
845 /* Low 16 bits of displacement in global offset table. */
846 HOWTO (R_MIPS_CALL_LO16, /* type */
848 2, /* size (0 = byte, 1 = short, 2 = long) */
850 false, /* pc_relative */
852 complain_overflow_dont, /* complain_on_overflow */
853 bfd_elf_generic_reloc, /* special_function */
854 "R_MIPS_CALL_LO16", /* name */
855 true, /* partial_inplace */
856 0x0000ffff, /* src_mask */
857 0x0000ffff, /* dst_mask */
858 false), /* pcrel_offset */
860 /* Section displacement. */
861 HOWTO (R_MIPS_SCN_DISP, /* type */
863 2, /* size (0 = byte, 1 = short, 2 = long) */
865 false, /* pc_relative */
867 complain_overflow_dont, /* complain_on_overflow */
868 bfd_elf_generic_reloc, /* special_function */
869 "R_MIPS_SCN_DISP", /* name */
870 false, /* partial_inplace */
871 0xffffffff, /* src_mask */
872 0xffffffff, /* dst_mask */
873 false), /* pcrel_offset */
875 EMPTY_HOWTO (R_MIPS_REL16),
876 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
877 EMPTY_HOWTO (R_MIPS_PJUMP),
878 EMPTY_HOWTO (R_MIPS_RELGOT),
880 /* Protected jump conversion. This is an optimization hint. No
881 relocation is required for correctness. */
882 HOWTO (R_MIPS_JALR, /* type */
884 0, /* size (0 = byte, 1 = short, 2 = long) */
886 false, /* pc_relative */
888 complain_overflow_dont, /* complain_on_overflow */
889 bfd_elf_generic_reloc, /* special_function */
890 "R_MIPS_JALR", /* name */
891 false, /* partial_inplace */
892 0x00000000, /* src_mask */
893 0x00000000, /* dst_mask */
894 false), /* pcrel_offset */
897 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
898 is a hack to make the linker think that we need 64 bit values. */
899 static reloc_howto_type elf_mips_ctor64_howto =
900 HOWTO (R_MIPS_64, /* type */
902 4, /* size (0 = byte, 1 = short, 2 = long) */
904 false, /* pc_relative */
906 complain_overflow_signed, /* complain_on_overflow */
907 mips32_64bit_reloc, /* special_function */
908 "R_MIPS_64", /* name */
909 true, /* partial_inplace */
910 0xffffffff, /* src_mask */
911 0xffffffff, /* dst_mask */
912 false); /* pcrel_offset */
914 /* The reloc used for the mips16 jump instruction. */
915 static reloc_howto_type elf_mips16_jump_howto =
916 HOWTO (R_MIPS16_26, /* type */
918 2, /* size (0 = byte, 1 = short, 2 = long) */
920 false, /* pc_relative */
922 complain_overflow_dont, /* complain_on_overflow */
923 /* This needs complex overflow
924 detection, because the upper four
925 bits must match the PC. */
926 mips16_jump_reloc, /* special_function */
927 "R_MIPS16_26", /* name */
928 true, /* partial_inplace */
929 0x3ffffff, /* src_mask */
930 0x3ffffff, /* dst_mask */
931 false); /* pcrel_offset */
933 /* The reloc used for the mips16 gprel instruction. */
934 static reloc_howto_type elf_mips16_gprel_howto =
935 HOWTO (R_MIPS16_GPREL, /* type */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
939 false, /* pc_relative */
941 complain_overflow_signed, /* complain_on_overflow */
942 mips16_gprel_reloc, /* special_function */
943 "R_MIPS16_GPREL", /* name */
944 true, /* partial_inplace */
945 0x07ff001f, /* src_mask */
946 0x07ff001f, /* dst_mask */
947 false); /* pcrel_offset */
950 /* GNU extension to record C++ vtable hierarchy */
951 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
952 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
954 2, /* size (0 = byte, 1 = short, 2 = long) */
956 false, /* pc_relative */
958 complain_overflow_dont, /* complain_on_overflow */
959 NULL, /* special_function */
960 "R_MIPS_GNU_VTINHERIT", /* name */
961 false, /* partial_inplace */
964 false); /* pcrel_offset */
966 /* GNU extension to record C++ vtable member usage */
967 static reloc_howto_type elf_mips_gnu_vtentry_howto =
968 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
970 2, /* size (0 = byte, 1 = short, 2 = long) */
972 false, /* pc_relative */
974 complain_overflow_dont, /* complain_on_overflow */
975 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
976 "R_MIPS_GNU_VTENTRY", /* name */
977 false, /* partial_inplace */
980 false); /* pcrel_offset */
982 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
983 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
984 the HI16. Here we just save the information we need; we do the
985 actual relocation when we see the LO16. MIPS ELF requires that the
986 LO16 immediately follow the HI16. As a GNU extension, we permit an
987 arbitrary number of HI16 relocs to be associated with a single LO16
988 reloc. This extension permits gcc to output the HI and LO relocs
993 struct mips_hi16 *next;
998 /* FIXME: This should not be a static variable. */
1000 static struct mips_hi16 *mips_hi16_list;
1002 bfd_reloc_status_type
1003 _bfd_mips_elf_hi16_reloc (abfd,
1010 bfd *abfd ATTRIBUTE_UNUSED;
1011 arelent *reloc_entry;
1014 asection *input_section;
1016 char **error_message;
1018 bfd_reloc_status_type ret;
1020 struct mips_hi16 *n;
1022 /* If we're relocating, and this an external symbol, we don't want
1023 to change anything. */
1024 if (output_bfd != (bfd *) NULL
1025 && (symbol->flags & BSF_SECTION_SYM) == 0
1026 && reloc_entry->addend == 0)
1028 reloc_entry->address += input_section->output_offset;
1029 return bfd_reloc_ok;
1034 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1036 boolean relocateable;
1039 if (ret == bfd_reloc_undefined)
1042 if (output_bfd != NULL)
1043 relocateable = true;
1046 relocateable = false;
1047 output_bfd = symbol->section->output_section->owner;
1050 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1051 error_message, &gp);
1052 if (ret != bfd_reloc_ok)
1055 relocation = gp - reloc_entry->address;
1059 if (bfd_is_und_section (symbol->section)
1060 && output_bfd == (bfd *) NULL)
1061 ret = bfd_reloc_undefined;
1063 if (bfd_is_com_section (symbol->section))
1066 relocation = symbol->value;
1069 relocation += symbol->section->output_section->vma;
1070 relocation += symbol->section->output_offset;
1071 relocation += reloc_entry->addend;
1073 if (reloc_entry->address > input_section->_cooked_size)
1074 return bfd_reloc_outofrange;
1076 /* Save the information, and let LO16 do the actual relocation. */
1077 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
1079 return bfd_reloc_outofrange;
1080 n->addr = (bfd_byte *) data + reloc_entry->address;
1081 n->addend = relocation;
1082 n->next = mips_hi16_list;
1085 if (output_bfd != (bfd *) NULL)
1086 reloc_entry->address += input_section->output_offset;
1091 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1092 inplace relocation; this function exists in order to do the
1093 R_MIPS_HI16 relocation described above. */
1095 bfd_reloc_status_type
1096 _bfd_mips_elf_lo16_reloc (abfd,
1104 arelent *reloc_entry;
1107 asection *input_section;
1109 char **error_message;
1111 arelent gp_disp_relent;
1113 if (mips_hi16_list != NULL)
1115 struct mips_hi16 *l;
1122 unsigned long vallo;
1123 struct mips_hi16 *next;
1125 /* Do the HI16 relocation. Note that we actually don't need
1126 to know anything about the LO16 itself, except where to
1127 find the low 16 bits of the addend needed by the LO16. */
1128 insn = bfd_get_32 (abfd, l->addr);
1129 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1131 val = ((insn & 0xffff) << 16) + vallo;
1134 /* The low order 16 bits are always treated as a signed
1135 value. Therefore, a negative value in the low order bits
1136 requires an adjustment in the high order bits. We need
1137 to make this adjustment in two ways: once for the bits we
1138 took from the data, and once for the bits we are putting
1139 back in to the data. */
1140 if ((vallo & 0x8000) != 0)
1142 if ((val & 0x8000) != 0)
1145 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1146 bfd_put_32 (abfd, insn, l->addr);
1148 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1150 gp_disp_relent = *reloc_entry;
1151 reloc_entry = &gp_disp_relent;
1152 reloc_entry->addend = l->addend;
1160 mips_hi16_list = NULL;
1162 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1164 bfd_reloc_status_type ret;
1165 bfd_vma gp, relocation;
1167 /* FIXME: Does this case ever occur? */
1169 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1170 if (ret != bfd_reloc_ok)
1173 relocation = gp - reloc_entry->address;
1174 relocation += symbol->section->output_section->vma;
1175 relocation += symbol->section->output_offset;
1176 relocation += reloc_entry->addend;
1178 if (reloc_entry->address > input_section->_cooked_size)
1179 return bfd_reloc_outofrange;
1181 gp_disp_relent = *reloc_entry;
1182 reloc_entry = &gp_disp_relent;
1183 reloc_entry->addend = relocation - 4;
1186 /* Now do the LO16 reloc in the usual way. */
1187 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1188 input_section, output_bfd, error_message);
1191 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1192 table used for PIC code. If the symbol is an external symbol, the
1193 instruction is modified to contain the offset of the appropriate
1194 entry in the global offset table. If the symbol is a section
1195 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1196 addends are combined to form the real addend against the section
1197 symbol; the GOT16 is modified to contain the offset of an entry in
1198 the global offset table, and the LO16 is modified to offset it
1199 appropriately. Thus an offset larger than 16 bits requires a
1200 modified value in the global offset table.
1202 This implementation suffices for the assembler, but the linker does
1203 not yet know how to create global offset tables. */
1205 bfd_reloc_status_type
1206 _bfd_mips_elf_got16_reloc (abfd,
1214 arelent *reloc_entry;
1217 asection *input_section;
1219 char **error_message;
1221 /* If we're relocating, and this an external symbol, we don't want
1222 to change anything. */
1223 if (output_bfd != (bfd *) NULL
1224 && (symbol->flags & BSF_SECTION_SYM) == 0
1225 && reloc_entry->addend == 0)
1227 reloc_entry->address += input_section->output_offset;
1228 return bfd_reloc_ok;
1231 /* If we're relocating, and this is a local symbol, we can handle it
1233 if (output_bfd != (bfd *) NULL
1234 && (symbol->flags & BSF_SECTION_SYM) != 0)
1235 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1236 input_section, output_bfd, error_message);
1241 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1242 dangerous relocation. */
1245 mips_elf_assign_gp (output_bfd, pgp)
1253 /* If we've already figured out what GP will be, just return it. */
1254 *pgp = _bfd_get_gp_value (output_bfd);
1258 count = bfd_get_symcount (output_bfd);
1259 sym = bfd_get_outsymbols (output_bfd);
1261 /* The linker script will have created a symbol named `_gp' with the
1262 appropriate value. */
1263 if (sym == (asymbol **) NULL)
1267 for (i = 0; i < count; i++, sym++)
1269 register CONST char *name;
1271 name = bfd_asymbol_name (*sym);
1272 if (*name == '_' && strcmp (name, "_gp") == 0)
1274 *pgp = bfd_asymbol_value (*sym);
1275 _bfd_set_gp_value (output_bfd, *pgp);
1283 /* Only get the error once. */
1285 _bfd_set_gp_value (output_bfd, *pgp);
1292 /* We have to figure out the gp value, so that we can adjust the
1293 symbol value correctly. We look up the symbol _gp in the output
1294 BFD. If we can't find it, we're stuck. We cache it in the ELF
1295 target data. We don't need to adjust the symbol value for an
1296 external symbol if we are producing relocateable output. */
1298 static bfd_reloc_status_type
1299 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1302 boolean relocateable;
1303 char **error_message;
1306 if (bfd_is_und_section (symbol->section)
1310 return bfd_reloc_undefined;
1313 *pgp = _bfd_get_gp_value (output_bfd);
1316 || (symbol->flags & BSF_SECTION_SYM) != 0))
1320 /* Make up a value. */
1321 *pgp = symbol->section->output_section->vma + 0x4000;
1322 _bfd_set_gp_value (output_bfd, *pgp);
1324 else if (!mips_elf_assign_gp (output_bfd, pgp))
1327 (char *) _("GP relative relocation when _gp not defined");
1328 return bfd_reloc_dangerous;
1332 return bfd_reloc_ok;
1335 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1336 become the offset from the gp register. This function also handles
1337 R_MIPS_LITERAL relocations, although those can be handled more
1338 cleverly because the entries in the .lit8 and .lit4 sections can be
1341 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1342 arelent *, asection *,
1343 boolean, PTR, bfd_vma));
1345 bfd_reloc_status_type
1346 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1347 output_bfd, error_message)
1349 arelent *reloc_entry;
1352 asection *input_section;
1354 char **error_message;
1356 boolean relocateable;
1357 bfd_reloc_status_type ret;
1360 /* If we're relocating, and this is an external symbol with no
1361 addend, we don't want to change anything. We will only have an
1362 addend if this is a newly created reloc, not read from an ELF
1364 if (output_bfd != (bfd *) NULL
1365 && (symbol->flags & BSF_SECTION_SYM) == 0
1366 && reloc_entry->addend == 0)
1368 reloc_entry->address += input_section->output_offset;
1369 return bfd_reloc_ok;
1372 if (output_bfd != (bfd *) NULL)
1373 relocateable = true;
1376 relocateable = false;
1377 output_bfd = symbol->section->output_section->owner;
1380 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1382 if (ret != bfd_reloc_ok)
1385 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1386 relocateable, data, gp);
1389 static bfd_reloc_status_type
1390 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1394 arelent *reloc_entry;
1395 asection *input_section;
1396 boolean relocateable;
1404 if (bfd_is_com_section (symbol->section))
1407 relocation = symbol->value;
1409 relocation += symbol->section->output_section->vma;
1410 relocation += symbol->section->output_offset;
1412 if (reloc_entry->address > input_section->_cooked_size)
1413 return bfd_reloc_outofrange;
1415 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1417 /* Set val to the offset into the section or symbol. */
1418 if (reloc_entry->howto->src_mask == 0)
1420 /* This case occurs with the 64-bit MIPS ELF ABI. */
1421 val = reloc_entry->addend;
1425 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1430 /* Adjust val for the final section location and GP value. If we
1431 are producing relocateable output, we don't want to do this for
1432 an external symbol. */
1434 || (symbol->flags & BSF_SECTION_SYM) != 0)
1435 val += relocation - gp;
1437 insn = (insn &~ 0xffff) | (val & 0xffff);
1438 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1441 reloc_entry->address += input_section->output_offset;
1443 /* Make sure it fit in 16 bits. */
1444 if (val >= 0x8000 && val < 0xffff8000)
1445 return bfd_reloc_overflow;
1447 return bfd_reloc_ok;
1450 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1451 from the gp register? XXX */
1453 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1454 arelent *, asection *,
1455 boolean, PTR, bfd_vma));
1457 bfd_reloc_status_type
1458 _bfd_mips_elf_gprel32_reloc (abfd,
1466 arelent *reloc_entry;
1469 asection *input_section;
1471 char **error_message;
1473 boolean relocateable;
1474 bfd_reloc_status_type ret;
1477 /* If we're relocating, and this is an external symbol with no
1478 addend, we don't want to change anything. We will only have an
1479 addend if this is a newly created reloc, not read from an ELF
1481 if (output_bfd != (bfd *) NULL
1482 && (symbol->flags & BSF_SECTION_SYM) == 0
1483 && reloc_entry->addend == 0)
1485 *error_message = (char *)
1486 _("32bits gp relative relocation occurs for an external symbol");
1487 return bfd_reloc_outofrange;
1490 if (output_bfd != (bfd *) NULL)
1492 relocateable = true;
1493 gp = _bfd_get_gp_value (output_bfd);
1497 relocateable = false;
1498 output_bfd = symbol->section->output_section->owner;
1500 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1501 error_message, &gp);
1502 if (ret != bfd_reloc_ok)
1506 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1507 relocateable, data, gp);
1510 static bfd_reloc_status_type
1511 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1515 arelent *reloc_entry;
1516 asection *input_section;
1517 boolean relocateable;
1524 if (bfd_is_com_section (symbol->section))
1527 relocation = symbol->value;
1529 relocation += symbol->section->output_section->vma;
1530 relocation += symbol->section->output_offset;
1532 if (reloc_entry->address > input_section->_cooked_size)
1533 return bfd_reloc_outofrange;
1535 if (reloc_entry->howto->src_mask == 0)
1537 /* This case arises with the 64-bit MIPS ELF ABI. */
1541 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1543 /* Set val to the offset into the section or symbol. */
1544 val += reloc_entry->addend;
1546 /* Adjust val for the final section location and GP value. If we
1547 are producing relocateable output, we don't want to do this for
1548 an external symbol. */
1550 || (symbol->flags & BSF_SECTION_SYM) != 0)
1551 val += relocation - gp;
1553 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1556 reloc_entry->address += input_section->output_offset;
1558 return bfd_reloc_ok;
1561 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1562 generated when addreses are 64 bits. The upper 32 bits are a simle
1565 static bfd_reloc_status_type
1566 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1567 output_bfd, error_message)
1569 arelent *reloc_entry;
1572 asection *input_section;
1574 char **error_message;
1576 bfd_reloc_status_type r;
1581 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1582 input_section, output_bfd, error_message);
1583 if (r != bfd_reloc_continue)
1586 /* Do a normal 32 bit relocation on the lower 32 bits. */
1587 reloc32 = *reloc_entry;
1588 if (bfd_big_endian (abfd))
1589 reloc32.address += 4;
1590 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1591 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1592 output_bfd, error_message);
1594 /* Sign extend into the upper 32 bits. */
1595 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1596 if ((val & 0x80000000) != 0)
1600 addr = reloc_entry->address;
1601 if (bfd_little_endian (abfd))
1603 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1608 /* Handle a mips16 jump. */
1610 static bfd_reloc_status_type
1611 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1612 output_bfd, error_message)
1613 bfd *abfd ATTRIBUTE_UNUSED;
1614 arelent *reloc_entry;
1616 PTR data ATTRIBUTE_UNUSED;
1617 asection *input_section;
1619 char **error_message ATTRIBUTE_UNUSED;
1621 if (output_bfd != (bfd *) NULL
1622 && (symbol->flags & BSF_SECTION_SYM) == 0
1623 && reloc_entry->addend == 0)
1625 reloc_entry->address += input_section->output_offset;
1626 return bfd_reloc_ok;
1631 static boolean warned;
1634 (*_bfd_error_handler)
1635 (_("Linking mips16 objects into %s format is not supported"),
1636 bfd_get_target (input_section->output_section->owner));
1640 return bfd_reloc_undefined;
1643 /* Handle a mips16 GP relative reloc. */
1645 static bfd_reloc_status_type
1646 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1647 output_bfd, error_message)
1649 arelent *reloc_entry;
1652 asection *input_section;
1654 char **error_message;
1656 boolean relocateable;
1657 bfd_reloc_status_type ret;
1659 unsigned short extend, insn;
1660 unsigned long final;
1662 /* If we're relocating, and this is an external symbol with no
1663 addend, we don't want to change anything. We will only have an
1664 addend if this is a newly created reloc, not read from an ELF
1666 if (output_bfd != NULL
1667 && (symbol->flags & BSF_SECTION_SYM) == 0
1668 && reloc_entry->addend == 0)
1670 reloc_entry->address += input_section->output_offset;
1671 return bfd_reloc_ok;
1674 if (output_bfd != NULL)
1675 relocateable = true;
1678 relocateable = false;
1679 output_bfd = symbol->section->output_section->owner;
1682 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1684 if (ret != bfd_reloc_ok)
1687 if (reloc_entry->address > input_section->_cooked_size)
1688 return bfd_reloc_outofrange;
1690 /* Pick up the mips16 extend instruction and the real instruction. */
1691 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1692 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1694 /* Stuff the current addend back as a 32 bit value, do the usual
1695 relocation, and then clean up. */
1697 (((extend & 0x1f) << 11)
1700 (bfd_byte *) data + reloc_entry->address);
1702 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1703 relocateable, data, gp);
1705 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1708 | ((final >> 11) & 0x1f)
1710 (bfd_byte *) data + reloc_entry->address);
1714 (bfd_byte *) data + reloc_entry->address + 2);
1719 /* Return the ISA for a MIPS e_flags value. */
1722 elf_mips_isa (flags)
1725 switch (flags & EF_MIPS_ARCH)
1739 /* Return the MACH for a MIPS e_flags value. */
1742 elf_mips_mach (flags)
1745 switch (flags & EF_MIPS_MACH)
1747 case E_MIPS_MACH_3900:
1748 return bfd_mach_mips3900;
1750 case E_MIPS_MACH_4010:
1751 return bfd_mach_mips4010;
1753 case E_MIPS_MACH_4100:
1754 return bfd_mach_mips4100;
1756 case E_MIPS_MACH_4111:
1757 return bfd_mach_mips4111;
1759 case E_MIPS_MACH_4650:
1760 return bfd_mach_mips4650;
1763 switch (flags & EF_MIPS_ARCH)
1767 return bfd_mach_mips3000;
1771 return bfd_mach_mips6000;
1775 return bfd_mach_mips4000;
1779 return bfd_mach_mips8000;
1787 /* Return printable name for ABI. */
1790 elf_mips_abi_name (abfd)
1795 if (ABI_N32_P (abfd))
1797 else if (ABI_64_P (abfd))
1800 flags = elf_elfheader (abfd)->e_flags;
1801 switch (flags & EF_MIPS_ABI)
1805 case E_MIPS_ABI_O32:
1807 case E_MIPS_ABI_O64:
1809 case E_MIPS_ABI_EABI32:
1811 case E_MIPS_ABI_EABI64:
1814 return "unknown abi";
1818 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1820 struct elf_reloc_map {
1821 bfd_reloc_code_real_type bfd_reloc_val;
1822 enum elf_mips_reloc_type elf_reloc_val;
1825 static CONST struct elf_reloc_map mips_reloc_map[] =
1827 { BFD_RELOC_NONE, R_MIPS_NONE, },
1828 { BFD_RELOC_16, R_MIPS_16 },
1829 { BFD_RELOC_32, R_MIPS_32 },
1830 { BFD_RELOC_64, R_MIPS_64 },
1831 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1832 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1833 { BFD_RELOC_LO16, R_MIPS_LO16 },
1834 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1835 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1836 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1837 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1838 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1839 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1840 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1841 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1842 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1843 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1844 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1845 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1846 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1847 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1850 /* Given a BFD reloc type, return a howto structure. */
1852 static reloc_howto_type *
1853 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1855 bfd_reloc_code_real_type code;
1859 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1861 if (mips_reloc_map[i].bfd_reloc_val == code)
1862 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1868 bfd_set_error (bfd_error_bad_value);
1871 case BFD_RELOC_CTOR:
1872 /* We need to handle BFD_RELOC_CTOR specially.
1873 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1874 size of addresses on this architecture. */
1875 if (bfd_arch_bits_per_address (abfd) == 32)
1876 return &elf_mips_howto_table[(int) R_MIPS_32];
1878 return &elf_mips_ctor64_howto;
1880 case BFD_RELOC_MIPS16_JMP:
1881 return &elf_mips16_jump_howto;
1882 case BFD_RELOC_MIPS16_GPREL:
1883 return &elf_mips16_gprel_howto;
1884 case BFD_RELOC_VTABLE_INHERIT:
1885 return &elf_mips_gnu_vtinherit_howto;
1886 case BFD_RELOC_VTABLE_ENTRY:
1887 return &elf_mips_gnu_vtentry_howto;
1891 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1894 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1897 Elf32_Internal_Rel *dst;
1899 unsigned int r_type;
1901 r_type = ELF32_R_TYPE (dst->r_info);
1905 cache_ptr->howto = &elf_mips16_jump_howto;
1907 case R_MIPS16_GPREL:
1908 cache_ptr->howto = &elf_mips16_gprel_howto;
1910 case R_MIPS_GNU_VTINHERIT:
1911 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1913 case R_MIPS_GNU_VTENTRY:
1914 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1918 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1919 cache_ptr->howto = &elf_mips_howto_table[r_type];
1923 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1924 value for the object file. We get the addend now, rather than
1925 when we do the relocation, because the symbol manipulations done
1926 by the linker may cause us to lose track of the input BFD. */
1927 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1928 && (r_type == (unsigned int) R_MIPS_GPREL16
1929 || r_type == (unsigned int) R_MIPS_LITERAL))
1930 cache_ptr->addend = elf_gp (abfd);
1933 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1936 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1939 Elf32_Internal_Rela *dst;
1941 /* Since an Elf32_Internal_Rel is an initial prefix of an
1942 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1944 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1946 /* If we ever need to do any extra processing with dst->r_addend
1947 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1950 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1951 routines swap this structure in and out. They are used outside of
1952 BFD, so they are globally visible. */
1955 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1957 const Elf32_External_RegInfo *ex;
1960 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1961 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1962 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1963 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1964 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1965 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1969 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1971 const Elf32_RegInfo *in;
1972 Elf32_External_RegInfo *ex;
1974 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1975 (bfd_byte *) ex->ri_gprmask);
1976 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1977 (bfd_byte *) ex->ri_cprmask[0]);
1978 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1979 (bfd_byte *) ex->ri_cprmask[1]);
1980 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1981 (bfd_byte *) ex->ri_cprmask[2]);
1982 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1983 (bfd_byte *) ex->ri_cprmask[3]);
1984 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1985 (bfd_byte *) ex->ri_gp_value);
1988 /* In the 64 bit ABI, the .MIPS.options section holds register
1989 information in an Elf64_Reginfo structure. These routines swap
1990 them in and out. They are globally visible because they are used
1991 outside of BFD. These routines are here so that gas can call them
1992 without worrying about whether the 64 bit ABI has been included. */
1995 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1997 const Elf64_External_RegInfo *ex;
1998 Elf64_Internal_RegInfo *in;
2000 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
2001 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
2002 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
2003 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
2004 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
2005 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
2006 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
2010 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
2012 const Elf64_Internal_RegInfo *in;
2013 Elf64_External_RegInfo *ex;
2015 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
2016 (bfd_byte *) ex->ri_gprmask);
2017 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
2018 (bfd_byte *) ex->ri_pad);
2019 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
2020 (bfd_byte *) ex->ri_cprmask[0]);
2021 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
2022 (bfd_byte *) ex->ri_cprmask[1]);
2023 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
2024 (bfd_byte *) ex->ri_cprmask[2]);
2025 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
2026 (bfd_byte *) ex->ri_cprmask[3]);
2027 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
2028 (bfd_byte *) ex->ri_gp_value);
2031 /* Swap an entry in a .gptab section. Note that these routines rely
2032 on the equivalence of the two elements of the union. */
2035 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
2037 const Elf32_External_gptab *ex;
2040 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
2041 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
2045 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
2047 const Elf32_gptab *in;
2048 Elf32_External_gptab *ex;
2050 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
2051 ex->gt_entry.gt_g_value);
2052 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
2053 ex->gt_entry.gt_bytes);
2057 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2059 const Elf32_compact_rel *in;
2060 Elf32_External_compact_rel *ex;
2062 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
2063 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
2064 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
2065 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
2066 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
2067 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
2071 bfd_elf32_swap_crinfo_out (abfd, in, ex)
2073 const Elf32_crinfo *in;
2074 Elf32_External_crinfo *ex;
2078 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2079 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2080 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2081 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2082 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
2083 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
2084 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
2087 /* Swap in an options header. */
2090 bfd_mips_elf_swap_options_in (abfd, ex, in)
2092 const Elf_External_Options *ex;
2093 Elf_Internal_Options *in;
2095 in->kind = bfd_h_get_8 (abfd, ex->kind);
2096 in->size = bfd_h_get_8 (abfd, ex->size);
2097 in->section = bfd_h_get_16 (abfd, ex->section);
2098 in->info = bfd_h_get_32 (abfd, ex->info);
2101 /* Swap out an options header. */
2104 bfd_mips_elf_swap_options_out (abfd, in, ex)
2106 const Elf_Internal_Options *in;
2107 Elf_External_Options *ex;
2109 bfd_h_put_8 (abfd, in->kind, ex->kind);
2110 bfd_h_put_8 (abfd, in->size, ex->size);
2111 bfd_h_put_16 (abfd, in->section, ex->section);
2112 bfd_h_put_32 (abfd, in->info, ex->info);
2115 /* Swap in an MSYM entry. */
2118 bfd_mips_elf_swap_msym_in (abfd, ex, in)
2120 const Elf32_External_Msym *ex;
2121 Elf32_Internal_Msym *in;
2123 in->ms_hash_value = bfd_h_get_32 (abfd, ex->ms_hash_value);
2124 in->ms_info = bfd_h_get_32 (abfd, ex->ms_info);
2127 /* Swap out an MSYM entry. */
2130 bfd_mips_elf_swap_msym_out (abfd, in, ex)
2132 const Elf32_Internal_Msym *in;
2133 Elf32_External_Msym *ex;
2135 bfd_h_put_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2136 bfd_h_put_32 (abfd, in->ms_info, ex->ms_info);
2140 /* Determine whether a symbol is global for the purposes of splitting
2141 the symbol table into global symbols and local symbols. At least
2142 on Irix 5, this split must be between section symbols and all other
2143 symbols. On most ELF targets the split is between static symbols
2144 and externally visible symbols. */
2148 mips_elf_sym_is_global (abfd, sym)
2149 bfd *abfd ATTRIBUTE_UNUSED;
2152 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
2155 /* Set the right machine number for a MIPS ELF file. This is used for
2156 both the 32-bit and the 64-bit ABI. */
2159 _bfd_mips_elf_object_p (abfd)
2162 /* Irix 5 and 6 is broken. Object file symbol tables are not always
2163 sorted correctly such that local symbols precede global symbols,
2164 and the sh_info field in the symbol table is not always right. */
2165 elf_bad_symtab (abfd) = true;
2167 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2168 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2172 /* The final processing done just before writing out a MIPS ELF object
2173 file. This gets the MIPS architecture right based on the machine
2174 number. This is used by both the 32-bit and the 64-bit ABI. */
2178 _bfd_mips_elf_final_write_processing (abfd, linker)
2180 boolean linker ATTRIBUTE_UNUSED;
2184 Elf_Internal_Shdr **hdrpp;
2188 switch (bfd_get_mach (abfd))
2191 case bfd_mach_mips3000:
2192 val = E_MIPS_ARCH_1;
2195 case bfd_mach_mips3900:
2196 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2199 case bfd_mach_mips6000:
2200 val = E_MIPS_ARCH_2;
2203 case bfd_mach_mips4000:
2204 case bfd_mach_mips4300:
2205 val = E_MIPS_ARCH_3;
2208 case bfd_mach_mips4010:
2209 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2212 case bfd_mach_mips4100:
2213 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2216 case bfd_mach_mips4111:
2217 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2220 case bfd_mach_mips4650:
2221 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2224 case bfd_mach_mips8000:
2225 val = E_MIPS_ARCH_4;
2229 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2230 elf_elfheader (abfd)->e_flags |= val;
2232 /* Set the sh_info field for .gptab sections and other appropriate
2233 info for each special section. */
2234 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2235 i < elf_elfheader (abfd)->e_shnum;
2238 switch ((*hdrpp)->sh_type)
2241 case SHT_MIPS_LIBLIST:
2242 sec = bfd_get_section_by_name (abfd, ".dynstr");
2244 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2247 case SHT_MIPS_GPTAB:
2248 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2249 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2250 BFD_ASSERT (name != NULL
2251 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2252 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2253 BFD_ASSERT (sec != NULL);
2254 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2257 case SHT_MIPS_CONTENT:
2258 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2259 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2260 BFD_ASSERT (name != NULL
2261 && strncmp (name, ".MIPS.content",
2262 sizeof ".MIPS.content" - 1) == 0);
2263 sec = bfd_get_section_by_name (abfd,
2264 name + sizeof ".MIPS.content" - 1);
2265 BFD_ASSERT (sec != NULL);
2266 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2269 case SHT_MIPS_SYMBOL_LIB:
2270 sec = bfd_get_section_by_name (abfd, ".dynsym");
2272 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2273 sec = bfd_get_section_by_name (abfd, ".liblist");
2275 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2278 case SHT_MIPS_EVENTS:
2279 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2280 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2281 BFD_ASSERT (name != NULL);
2282 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2283 sec = bfd_get_section_by_name (abfd,
2284 name + sizeof ".MIPS.events" - 1);
2287 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2288 sizeof ".MIPS.post_rel" - 1) == 0);
2289 sec = bfd_get_section_by_name (abfd,
2291 + sizeof ".MIPS.post_rel" - 1));
2293 BFD_ASSERT (sec != NULL);
2294 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2301 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2304 _bfd_mips_elf_set_private_flags (abfd, flags)
2308 BFD_ASSERT (!elf_flags_init (abfd)
2309 || elf_elfheader (abfd)->e_flags == flags);
2311 elf_elfheader (abfd)->e_flags = flags;
2312 elf_flags_init (abfd) = true;
2316 /* Copy backend specific data from one object module to another */
2319 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2323 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2324 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2327 BFD_ASSERT (!elf_flags_init (obfd)
2328 || (elf_elfheader (obfd)->e_flags
2329 == elf_elfheader (ibfd)->e_flags));
2331 elf_gp (obfd) = elf_gp (ibfd);
2332 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2333 elf_flags_init (obfd) = true;
2337 /* Merge backend specific data from an object file to the output
2338 object file when linking. */
2341 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2349 /* Check if we have the same endianess */
2350 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2351 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2355 if (bfd_big_endian (ibfd))
2356 msg = _("%s: compiled for a big endian system and target is little endian");
2358 msg = _("%s: compiled for a little endian system and target is big endian");
2360 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2362 bfd_set_error (bfd_error_wrong_format);
2366 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2367 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2370 new_flags = elf_elfheader (ibfd)->e_flags;
2371 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2372 old_flags = elf_elfheader (obfd)->e_flags;
2374 if (! elf_flags_init (obfd))
2376 elf_flags_init (obfd) = true;
2377 elf_elfheader (obfd)->e_flags = new_flags;
2378 elf_elfheader (obfd)->e_ident[EI_CLASS]
2379 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
2381 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2382 && bfd_get_arch_info (obfd)->the_default)
2384 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2385 bfd_get_mach (ibfd)))
2392 /* Check flag compatibility. */
2394 new_flags &= ~EF_MIPS_NOREORDER;
2395 old_flags &= ~EF_MIPS_NOREORDER;
2397 if (new_flags == old_flags)
2402 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2404 new_flags &= ~EF_MIPS_PIC;
2405 old_flags &= ~EF_MIPS_PIC;
2406 (*_bfd_error_handler)
2407 (_("%s: linking PIC files with non-PIC files"),
2408 bfd_get_filename (ibfd));
2412 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2414 new_flags &= ~EF_MIPS_CPIC;
2415 old_flags &= ~EF_MIPS_CPIC;
2416 (*_bfd_error_handler)
2417 (_("%s: linking abicalls files with non-abicalls files"),
2418 bfd_get_filename (ibfd));
2422 /* Compare the ISA's. */
2423 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2424 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2426 int new_mach = new_flags & EF_MIPS_MACH;
2427 int old_mach = old_flags & EF_MIPS_MACH;
2428 int new_isa = elf_mips_isa (new_flags);
2429 int old_isa = elf_mips_isa (old_flags);
2431 /* If either has no machine specified, just compare the general isa's.
2432 Some combinations of machines are ok, if the isa's match. */
2435 || new_mach == old_mach
2438 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2439 and -mips4 code. They will normally use the same data sizes and
2440 calling conventions. */
2442 if ((new_isa == 1 || new_isa == 2)
2443 ? (old_isa != 1 && old_isa != 2)
2444 : (old_isa == 1 || old_isa == 2))
2446 (*_bfd_error_handler)
2447 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2448 bfd_get_filename (ibfd), new_isa, old_isa);
2455 (*_bfd_error_handler)
2456 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2457 bfd_get_filename (ibfd),
2458 elf_mips_mach (new_flags),
2459 elf_mips_mach (old_flags));
2463 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2464 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2467 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
2468 does set EI_CLASS differently from any 32-bit ABI. */
2469 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
2470 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2471 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2473 /* Only error if both are set (to different values). */
2474 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
2475 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2476 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2478 (*_bfd_error_handler)
2479 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2480 bfd_get_filename (ibfd),
2481 elf_mips_abi_name (ibfd),
2482 elf_mips_abi_name (obfd));
2485 new_flags &= ~EF_MIPS_ABI;
2486 old_flags &= ~EF_MIPS_ABI;
2489 /* Warn about any other mismatches */
2490 if (new_flags != old_flags)
2492 (*_bfd_error_handler)
2493 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2494 bfd_get_filename (ibfd), (unsigned long) new_flags,
2495 (unsigned long) old_flags);
2501 bfd_set_error (bfd_error_bad_value);
2509 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2513 FILE *file = (FILE *) ptr;
2515 BFD_ASSERT (abfd != NULL && ptr != NULL);
2517 /* Print normal ELF private data. */
2518 _bfd_elf_print_private_bfd_data (abfd, ptr);
2520 /* xgettext:c-format */
2521 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2523 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2524 fprintf (file, _ (" [abi=O32]"));
2525 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2526 fprintf (file, _ (" [abi=O64]"));
2527 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2528 fprintf (file, _ (" [abi=EABI32]"));
2529 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2530 fprintf (file, _ (" [abi=EABI64]"));
2531 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2532 fprintf (file, _ (" [abi unknown]"));
2533 else if (ABI_N32_P (abfd))
2534 fprintf (file, _ (" [abi=N32]"));
2535 else if (ABI_64_P (abfd))
2536 fprintf (file, _ (" [abi=64]"));
2538 fprintf (file, _ (" [no abi set]"));
2540 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2541 fprintf (file, _ (" [mips1]"));
2542 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2543 fprintf (file, _ (" [mips2]"));
2544 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2545 fprintf (file, _ (" [mips3]"));
2546 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2547 fprintf (file, _ (" [mips4]"));
2549 fprintf (file, _ (" [unknown ISA]"));
2551 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2552 fprintf (file, _ (" [32bitmode]"));
2554 fprintf (file, _ (" [not 32bitmode]"));
2561 /* Handle a MIPS specific section when reading an object file. This
2562 is called when elfcode.h finds a section with an unknown type.
2563 This routine supports both the 32-bit and 64-bit ELF ABI.
2565 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2569 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2571 Elf_Internal_Shdr *hdr;
2576 /* There ought to be a place to keep ELF backend specific flags, but
2577 at the moment there isn't one. We just keep track of the
2578 sections by their name, instead. Fortunately, the ABI gives
2579 suggested names for all the MIPS specific sections, so we will
2580 probably get away with this. */
2581 switch (hdr->sh_type)
2583 case SHT_MIPS_LIBLIST:
2584 if (strcmp (name, ".liblist") != 0)
2588 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2591 case SHT_MIPS_CONFLICT:
2592 if (strcmp (name, ".conflict") != 0)
2595 case SHT_MIPS_GPTAB:
2596 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2599 case SHT_MIPS_UCODE:
2600 if (strcmp (name, ".ucode") != 0)
2603 case SHT_MIPS_DEBUG:
2604 if (strcmp (name, ".mdebug") != 0)
2606 flags = SEC_DEBUGGING;
2608 case SHT_MIPS_REGINFO:
2609 if (strcmp (name, ".reginfo") != 0
2610 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2612 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2614 case SHT_MIPS_IFACE:
2615 if (strcmp (name, ".MIPS.interfaces") != 0)
2618 case SHT_MIPS_CONTENT:
2619 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2622 case SHT_MIPS_OPTIONS:
2623 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2626 case SHT_MIPS_DWARF:
2627 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2630 case SHT_MIPS_SYMBOL_LIB:
2631 if (strcmp (name, ".MIPS.symlib") != 0)
2634 case SHT_MIPS_EVENTS:
2635 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2636 && strncmp (name, ".MIPS.post_rel",
2637 sizeof ".MIPS.post_rel" - 1) != 0)
2644 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2649 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2650 (bfd_get_section_flags (abfd,
2656 /* FIXME: We should record sh_info for a .gptab section. */
2658 /* For a .reginfo section, set the gp value in the tdata information
2659 from the contents of this section. We need the gp value while
2660 processing relocs, so we just get it now. The .reginfo section
2661 is not used in the 64-bit MIPS ELF ABI. */
2662 if (hdr->sh_type == SHT_MIPS_REGINFO)
2664 Elf32_External_RegInfo ext;
2667 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2668 (file_ptr) 0, sizeof ext))
2670 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2671 elf_gp (abfd) = s.ri_gp_value;
2674 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2675 set the gp value based on what we find. We may see both
2676 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2677 they should agree. */
2678 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2680 bfd_byte *contents, *l, *lend;
2682 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2683 if (contents == NULL)
2685 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2686 (file_ptr) 0, hdr->sh_size))
2692 lend = contents + hdr->sh_size;
2693 while (l + sizeof (Elf_External_Options) <= lend)
2695 Elf_Internal_Options intopt;
2697 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2699 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2701 Elf64_Internal_RegInfo intreg;
2703 bfd_mips_elf64_swap_reginfo_in
2705 ((Elf64_External_RegInfo *)
2706 (l + sizeof (Elf_External_Options))),
2708 elf_gp (abfd) = intreg.ri_gp_value;
2710 else if (intopt.kind == ODK_REGINFO)
2712 Elf32_RegInfo intreg;
2714 bfd_mips_elf32_swap_reginfo_in
2716 ((Elf32_External_RegInfo *)
2717 (l + sizeof (Elf_External_Options))),
2719 elf_gp (abfd) = intreg.ri_gp_value;
2729 /* Set the correct type for a MIPS ELF section. We do this by the
2730 section name, which is a hack, but ought to work. This routine is
2731 used by both the 32-bit and the 64-bit ABI. */
2734 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2736 Elf32_Internal_Shdr *hdr;
2739 register const char *name;
2741 name = bfd_get_section_name (abfd, sec);
2743 if (strcmp (name, ".liblist") == 0)
2745 hdr->sh_type = SHT_MIPS_LIBLIST;
2746 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2747 /* The sh_link field is set in final_write_processing. */
2749 else if (strcmp (name, ".conflict") == 0)
2750 hdr->sh_type = SHT_MIPS_CONFLICT;
2751 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2753 hdr->sh_type = SHT_MIPS_GPTAB;
2754 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2755 /* The sh_info field is set in final_write_processing. */
2757 else if (strcmp (name, ".ucode") == 0)
2758 hdr->sh_type = SHT_MIPS_UCODE;
2759 else if (strcmp (name, ".mdebug") == 0)
2761 hdr->sh_type = SHT_MIPS_DEBUG;
2762 /* In a shared object on Irix 5.3, the .mdebug section has an
2763 entsize of 0. FIXME: Does this matter? */
2764 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2765 hdr->sh_entsize = 0;
2767 hdr->sh_entsize = 1;
2769 else if (strcmp (name, ".reginfo") == 0)
2771 hdr->sh_type = SHT_MIPS_REGINFO;
2772 /* In a shared object on Irix 5.3, the .reginfo section has an
2773 entsize of 0x18. FIXME: Does this matter? */
2774 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2775 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2777 hdr->sh_entsize = 1;
2779 else if (SGI_COMPAT (abfd)
2780 && (strcmp (name, ".hash") == 0
2781 || strcmp (name, ".dynamic") == 0
2782 || strcmp (name, ".dynstr") == 0))
2784 hdr->sh_entsize = 0;
2786 /* This isn't how the Irix 6 linker behaves. */
2787 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2790 else if (strcmp (name, ".got") == 0
2791 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2792 || strcmp (name, ".sdata") == 0
2793 || strcmp (name, ".sbss") == 0
2794 || strcmp (name, ".lit4") == 0
2795 || strcmp (name, ".lit8") == 0)
2796 hdr->sh_flags |= SHF_MIPS_GPREL;
2797 else if (strcmp (name, ".MIPS.interfaces") == 0)
2799 hdr->sh_type = SHT_MIPS_IFACE;
2800 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2802 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2804 hdr->sh_type = SHT_MIPS_CONTENT;
2805 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2806 /* The sh_info field is set in final_write_processing. */
2808 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2810 hdr->sh_type = SHT_MIPS_OPTIONS;
2811 hdr->sh_entsize = 1;
2812 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2814 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2815 hdr->sh_type = SHT_MIPS_DWARF;
2816 else if (strcmp (name, ".MIPS.symlib") == 0)
2818 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2819 /* The sh_link and sh_info fields are set in
2820 final_write_processing. */
2822 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2823 || strncmp (name, ".MIPS.post_rel",
2824 sizeof ".MIPS.post_rel" - 1) == 0)
2826 hdr->sh_type = SHT_MIPS_EVENTS;
2827 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2828 /* The sh_link field is set in final_write_processing. */
2830 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2832 hdr->sh_type = SHT_MIPS_MSYM;
2833 hdr->sh_flags |= SHF_ALLOC;
2834 hdr->sh_entsize = 8;
2837 /* The generic elf_fake_sections will set up REL_HDR using the
2838 default kind of relocations. But, we may actually need both
2839 kinds of relocations, so we set up the second header here. */
2840 if ((sec->flags & SEC_RELOC) != 0)
2842 struct bfd_elf_section_data *esd;
2844 esd = elf_section_data (sec);
2845 BFD_ASSERT (esd->rel_hdr2 == NULL);
2847 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2850 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
2851 !elf_section_data (sec)->use_rela_p);
2857 /* Given a BFD section, try to locate the corresponding ELF section
2858 index. This is used by both the 32-bit and the 64-bit ABI.
2859 Actually, it's not clear to me that the 64-bit ABI supports these,
2860 but for non-PIC objects we will certainly want support for at least
2861 the .scommon section. */
2864 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2865 bfd *abfd ATTRIBUTE_UNUSED;
2866 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
2870 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2872 *retval = SHN_MIPS_SCOMMON;
2875 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2877 *retval = SHN_MIPS_ACOMMON;
2883 /* When are writing out the .options or .MIPS.options section,
2884 remember the bytes we are writing out, so that we can install the
2885 GP value in the section_processing routine. */
2888 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2893 bfd_size_type count;
2895 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2899 if (elf_section_data (section) == NULL)
2901 section->used_by_bfd =
2902 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2903 if (elf_section_data (section) == NULL)
2906 c = (bfd_byte *) elf_section_data (section)->tdata;
2911 if (section->_cooked_size != 0)
2912 size = section->_cooked_size;
2914 size = section->_raw_size;
2915 c = (bfd_byte *) bfd_zalloc (abfd, size);
2918 elf_section_data (section)->tdata = (PTR) c;
2921 memcpy (c + offset, location, count);
2924 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2928 /* Work over a section just before writing it out. This routine is
2929 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2930 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2934 _bfd_mips_elf_section_processing (abfd, hdr)
2936 Elf_Internal_Shdr *hdr;
2938 if (hdr->sh_type == SHT_MIPS_REGINFO
2939 && hdr->sh_size > 0)
2943 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2944 BFD_ASSERT (hdr->contents == NULL);
2947 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2950 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2951 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2955 if (hdr->sh_type == SHT_MIPS_OPTIONS
2956 && hdr->bfd_section != NULL
2957 && elf_section_data (hdr->bfd_section) != NULL
2958 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2960 bfd_byte *contents, *l, *lend;
2962 /* We stored the section contents in the elf_section_data tdata
2963 field in the set_section_contents routine. We save the
2964 section contents so that we don't have to read them again.
2965 At this point we know that elf_gp is set, so we can look
2966 through the section contents to see if there is an
2967 ODK_REGINFO structure. */
2969 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2971 lend = contents + hdr->sh_size;
2972 while (l + sizeof (Elf_External_Options) <= lend)
2974 Elf_Internal_Options intopt;
2976 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2978 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2985 + sizeof (Elf_External_Options)
2986 + (sizeof (Elf64_External_RegInfo) - 8)),
2989 bfd_h_put_64 (abfd, elf_gp (abfd), buf);
2990 if (bfd_write (buf, 1, 8, abfd) != 8)
2993 else if (intopt.kind == ODK_REGINFO)
3000 + sizeof (Elf_External_Options)
3001 + (sizeof (Elf32_External_RegInfo) - 4)),
3004 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
3005 if (bfd_write (buf, 1, 4, abfd) != 4)
3012 if (hdr->bfd_section != NULL)
3014 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3016 if (strcmp (name, ".sdata") == 0
3017 || strcmp (name, ".lit8") == 0
3018 || strcmp (name, ".lit4") == 0)
3020 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3021 hdr->sh_type = SHT_PROGBITS;
3023 else if (strcmp (name, ".sbss") == 0)
3025 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3026 hdr->sh_type = SHT_NOBITS;
3028 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3030 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3031 hdr->sh_type = SHT_PROGBITS;
3033 else if (strcmp (name, ".compact_rel") == 0)
3036 hdr->sh_type = SHT_PROGBITS;
3038 else if (strcmp (name, ".rtproc") == 0)
3040 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3042 unsigned int adjust;
3044 adjust = hdr->sh_size % hdr->sh_addralign;
3046 hdr->sh_size += hdr->sh_addralign - adjust;
3055 /* MIPS ELF uses two common sections. One is the usual one, and the
3056 other is for small objects. All the small objects are kept
3057 together, and then referenced via the gp pointer, which yields
3058 faster assembler code. This is what we use for the small common
3059 section. This approach is copied from ecoff.c. */
3060 static asection mips_elf_scom_section;
3061 static asymbol mips_elf_scom_symbol;
3062 static asymbol *mips_elf_scom_symbol_ptr;
3064 /* MIPS ELF also uses an acommon section, which represents an
3065 allocated common symbol which may be overridden by a
3066 definition in a shared library. */
3067 static asection mips_elf_acom_section;
3068 static asymbol mips_elf_acom_symbol;
3069 static asymbol *mips_elf_acom_symbol_ptr;
3071 /* The Irix 5 support uses two virtual sections, which represent
3072 text/data symbols defined in dynamic objects. */
3073 static asection mips_elf_text_section;
3074 static asection *mips_elf_text_section_ptr;
3075 static asymbol mips_elf_text_symbol;
3076 static asymbol *mips_elf_text_symbol_ptr;
3078 static asection mips_elf_data_section;
3079 static asection *mips_elf_data_section_ptr;
3080 static asymbol mips_elf_data_symbol;
3081 static asymbol *mips_elf_data_symbol_ptr;
3083 /* Handle the special MIPS section numbers that a symbol may use.
3084 This is used for both the 32-bit and the 64-bit ABI. */
3087 _bfd_mips_elf_symbol_processing (abfd, asym)
3091 elf_symbol_type *elfsym;
3093 elfsym = (elf_symbol_type *) asym;
3094 switch (elfsym->internal_elf_sym.st_shndx)
3096 case SHN_MIPS_ACOMMON:
3097 /* This section is used in a dynamically linked executable file.
3098 It is an allocated common section. The dynamic linker can
3099 either resolve these symbols to something in a shared
3100 library, or it can just leave them here. For our purposes,
3101 we can consider these symbols to be in a new section. */
3102 if (mips_elf_acom_section.name == NULL)
3104 /* Initialize the acommon section. */
3105 mips_elf_acom_section.name = ".acommon";
3106 mips_elf_acom_section.flags = SEC_ALLOC;
3107 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3108 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3109 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3110 mips_elf_acom_symbol.name = ".acommon";
3111 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3112 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3113 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3115 asym->section = &mips_elf_acom_section;
3119 /* Common symbols less than the GP size are automatically
3120 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3121 if (asym->value > elf_gp_size (abfd)
3122 || IRIX_COMPAT (abfd) == ict_irix6)
3125 case SHN_MIPS_SCOMMON:
3126 if (mips_elf_scom_section.name == NULL)
3128 /* Initialize the small common section. */
3129 mips_elf_scom_section.name = ".scommon";
3130 mips_elf_scom_section.flags = SEC_IS_COMMON;
3131 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3132 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3133 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3134 mips_elf_scom_symbol.name = ".scommon";
3135 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3136 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3137 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3139 asym->section = &mips_elf_scom_section;
3140 asym->value = elfsym->internal_elf_sym.st_size;
3143 case SHN_MIPS_SUNDEFINED:
3144 asym->section = bfd_und_section_ptr;
3147 #if 0 /* for SGI_COMPAT */
3149 asym->section = mips_elf_text_section_ptr;
3153 asym->section = mips_elf_data_section_ptr;
3159 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3163 _bfd_mips_elf_additional_program_headers (abfd)
3169 if (!SGI_COMPAT (abfd))
3172 /* See if we need a PT_MIPS_REGINFO segment. */
3173 s = bfd_get_section_by_name (abfd, ".reginfo");
3174 if (s && (s->flags & SEC_LOAD))
3177 /* See if we need a PT_MIPS_OPTIONS segment. */
3178 if (IRIX_COMPAT (abfd) == ict_irix6
3179 && bfd_get_section_by_name (abfd,
3180 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3183 /* See if we need a PT_MIPS_RTPROC segment. */
3184 if (IRIX_COMPAT (abfd) == ict_irix5
3185 && bfd_get_section_by_name (abfd, ".dynamic")
3186 && bfd_get_section_by_name (abfd, ".mdebug"))
3192 /* Modify the segment map for an Irix 5 executable. */
3195 _bfd_mips_elf_modify_segment_map (abfd)
3199 struct elf_segment_map *m, **pm;
3201 if (! SGI_COMPAT (abfd))
3204 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3206 s = bfd_get_section_by_name (abfd, ".reginfo");
3207 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3209 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3210 if (m->p_type == PT_MIPS_REGINFO)
3214 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3218 m->p_type = PT_MIPS_REGINFO;
3222 /* We want to put it after the PHDR and INTERP segments. */
3223 pm = &elf_tdata (abfd)->segment_map;
3225 && ((*pm)->p_type == PT_PHDR
3226 || (*pm)->p_type == PT_INTERP))
3234 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3235 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3236 PT_OPTIONS segement immediately following the program header
3238 if (IRIX_COMPAT (abfd) == ict_irix6)
3242 for (s = abfd->sections; s; s = s->next)
3243 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3248 struct elf_segment_map *options_segment;
3250 /* Usually, there's a program header table. But, sometimes
3251 there's not (like when running the `ld' testsuite). So,
3252 if there's no program header table, we just put the
3253 options segement at the end. */
3254 for (pm = &elf_tdata (abfd)->segment_map;
3257 if ((*pm)->p_type == PT_PHDR)
3260 options_segment = bfd_zalloc (abfd,
3261 sizeof (struct elf_segment_map));
3262 options_segment->next = *pm;
3263 options_segment->p_type = PT_MIPS_OPTIONS;
3264 options_segment->p_flags = PF_R;
3265 options_segment->p_flags_valid = true;
3266 options_segment->count = 1;
3267 options_segment->sections[0] = s;
3268 *pm = options_segment;
3273 /* If there are .dynamic and .mdebug sections, we make a room
3274 for the RTPROC header. FIXME: Rewrite without section names. */
3275 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3276 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3277 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3279 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3280 if (m->p_type == PT_MIPS_RTPROC)
3284 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3288 m->p_type = PT_MIPS_RTPROC;
3290 s = bfd_get_section_by_name (abfd, ".rtproc");
3295 m->p_flags_valid = 1;
3303 /* We want to put it after the DYNAMIC segment. */
3304 pm = &elf_tdata (abfd)->segment_map;
3305 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3315 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3316 .dynstr, .dynsym, and .hash sections, and everything in
3318 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3319 if ((*pm)->p_type == PT_DYNAMIC)
3324 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3326 static const char *sec_names[] =
3327 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3330 struct elf_segment_map *n;
3334 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3336 s = bfd_get_section_by_name (abfd, sec_names[i]);
3337 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3343 sz = s->_cooked_size;
3346 if (high < s->vma + sz)
3352 for (s = abfd->sections; s != NULL; s = s->next)
3353 if ((s->flags & SEC_LOAD) != 0
3356 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3360 n = ((struct elf_segment_map *)
3361 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3368 for (s = abfd->sections; s != NULL; s = s->next)
3370 if ((s->flags & SEC_LOAD) != 0
3373 + (s->_cooked_size != 0 ?
3374 s->_cooked_size : s->_raw_size))
3389 /* The structure of the runtime procedure descriptor created by the
3390 loader for use by the static exception system. */
3392 typedef struct runtime_pdr {
3393 bfd_vma adr; /* memory address of start of procedure */
3394 long regmask; /* save register mask */
3395 long regoffset; /* save register offset */
3396 long fregmask; /* save floating point register mask */
3397 long fregoffset; /* save floating point register offset */
3398 long frameoffset; /* frame size */
3399 short framereg; /* frame pointer register */
3400 short pcreg; /* offset or reg of return pc */
3401 long irpss; /* index into the runtime string table */
3403 struct exception_info *exception_info;/* pointer to exception array */
3405 #define cbRPDR sizeof(RPDR)
3406 #define rpdNil ((pRPDR) 0)
3408 /* Swap RPDR (runtime procedure table entry) for output. */
3410 static void ecoff_swap_rpdr_out
3411 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3414 ecoff_swap_rpdr_out (abfd, in, ex)
3417 struct rpdr_ext *ex;
3419 /* ecoff_put_off was defined in ecoffswap.h. */
3420 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3421 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3422 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3423 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3424 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3425 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3427 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3428 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3430 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3432 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3436 /* Read ECOFF debugging information from a .mdebug section into a
3437 ecoff_debug_info structure. */
3440 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3443 struct ecoff_debug_info *debug;
3446 const struct ecoff_debug_swap *swap;
3447 char *ext_hdr = NULL;
3449 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3450 memset (debug, 0, sizeof(*debug));
3452 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3453 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3456 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3457 swap->external_hdr_size)
3461 symhdr = &debug->symbolic_header;
3462 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3464 /* The symbolic header contains absolute file offsets and sizes to
3466 #define READ(ptr, offset, count, size, type) \
3467 if (symhdr->count == 0) \
3468 debug->ptr = NULL; \
3471 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3472 if (debug->ptr == NULL) \
3473 goto error_return; \
3474 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3475 || (bfd_read (debug->ptr, size, symhdr->count, \
3476 abfd) != size * symhdr->count)) \
3477 goto error_return; \
3480 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3481 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3482 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3483 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3484 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3485 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3487 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3488 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3489 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3490 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3491 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3495 debug->adjust = NULL;
3500 if (ext_hdr != NULL)
3502 if (debug->line != NULL)
3504 if (debug->external_dnr != NULL)
3505 free (debug->external_dnr);
3506 if (debug->external_pdr != NULL)
3507 free (debug->external_pdr);
3508 if (debug->external_sym != NULL)
3509 free (debug->external_sym);
3510 if (debug->external_opt != NULL)
3511 free (debug->external_opt);
3512 if (debug->external_aux != NULL)
3513 free (debug->external_aux);
3514 if (debug->ss != NULL)
3516 if (debug->ssext != NULL)
3517 free (debug->ssext);
3518 if (debug->external_fdr != NULL)
3519 free (debug->external_fdr);
3520 if (debug->external_rfd != NULL)
3521 free (debug->external_rfd);
3522 if (debug->external_ext != NULL)
3523 free (debug->external_ext);
3527 /* MIPS ELF local labels start with '$', not 'L'. */
3531 mips_elf_is_local_label_name (abfd, name)
3538 /* On Irix 6, the labels go back to starting with '.', so we accept
3539 the generic ELF local label syntax as well. */
3540 return _bfd_elf_is_local_label_name (abfd, name);
3543 /* MIPS ELF uses a special find_nearest_line routine in order the
3544 handle the ECOFF debugging information. */
3546 struct mips_elf_find_line
3548 struct ecoff_debug_info d;
3549 struct ecoff_find_line i;
3553 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3554 functionname_ptr, line_ptr)
3559 const char **filename_ptr;
3560 const char **functionname_ptr;
3561 unsigned int *line_ptr;
3565 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3566 filename_ptr, functionname_ptr,
3570 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3571 filename_ptr, functionname_ptr,
3573 ABI_64_P (abfd) ? 8 : 0))
3576 msec = bfd_get_section_by_name (abfd, ".mdebug");
3580 struct mips_elf_find_line *fi;
3581 const struct ecoff_debug_swap * const swap =
3582 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3584 /* If we are called during a link, mips_elf_final_link may have
3585 cleared the SEC_HAS_CONTENTS field. We force it back on here
3586 if appropriate (which it normally will be). */
3587 origflags = msec->flags;
3588 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3589 msec->flags |= SEC_HAS_CONTENTS;
3591 fi = elf_tdata (abfd)->find_line_info;
3594 bfd_size_type external_fdr_size;
3597 struct fdr *fdr_ptr;
3599 fi = ((struct mips_elf_find_line *)
3600 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3603 msec->flags = origflags;
3607 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3609 msec->flags = origflags;
3613 /* Swap in the FDR information. */
3614 fi->d.fdr = ((struct fdr *)
3616 (fi->d.symbolic_header.ifdMax *
3617 sizeof (struct fdr))));
3618 if (fi->d.fdr == NULL)
3620 msec->flags = origflags;
3623 external_fdr_size = swap->external_fdr_size;
3624 fdr_ptr = fi->d.fdr;
3625 fraw_src = (char *) fi->d.external_fdr;
3626 fraw_end = (fraw_src
3627 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3628 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3629 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3631 elf_tdata (abfd)->find_line_info = fi;
3633 /* Note that we don't bother to ever free this information.
3634 find_nearest_line is either called all the time, as in
3635 objdump -l, so the information should be saved, or it is
3636 rarely called, as in ld error messages, so the memory
3637 wasted is unimportant. Still, it would probably be a
3638 good idea for free_cached_info to throw it away. */
3641 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3642 &fi->i, filename_ptr, functionname_ptr,
3645 msec->flags = origflags;
3649 msec->flags = origflags;
3652 /* Fall back on the generic ELF find_nearest_line routine. */
3654 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3655 filename_ptr, functionname_ptr,
3659 /* The mips16 compiler uses a couple of special sections to handle
3660 floating point arguments.
3662 Section names that look like .mips16.fn.FNNAME contain stubs that
3663 copy floating point arguments from the fp regs to the gp regs and
3664 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3665 call should be redirected to the stub instead. If no 32 bit
3666 function calls FNNAME, the stub should be discarded. We need to
3667 consider any reference to the function, not just a call, because
3668 if the address of the function is taken we will need the stub,
3669 since the address might be passed to a 32 bit function.
3671 Section names that look like .mips16.call.FNNAME contain stubs
3672 that copy floating point arguments from the gp regs to the fp
3673 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3674 then any 16 bit function that calls FNNAME should be redirected
3675 to the stub instead. If FNNAME is not a 32 bit function, the
3676 stub should be discarded.
3678 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3679 which call FNNAME and then copy the return value from the fp regs
3680 to the gp regs. These stubs store the return value in $18 while
3681 calling FNNAME; any function which might call one of these stubs
3682 must arrange to save $18 around the call. (This case is not
3683 needed for 32 bit functions that call 16 bit functions, because
3684 16 bit functions always return floating point values in both
3687 Note that in all cases FNNAME might be defined statically.
3688 Therefore, FNNAME is not used literally. Instead, the relocation
3689 information will indicate which symbol the section is for.
3691 We record any stubs that we find in the symbol table. */
3693 #define FN_STUB ".mips16.fn."
3694 #define CALL_STUB ".mips16.call."
3695 #define CALL_FP_STUB ".mips16.call.fp."
3697 /* MIPS ELF linker hash table. */
3699 struct mips_elf_link_hash_table
3701 struct elf_link_hash_table root;
3703 /* We no longer use this. */
3704 /* String section indices for the dynamic section symbols. */
3705 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3707 /* The number of .rtproc entries. */
3708 bfd_size_type procedure_count;
3709 /* The size of the .compact_rel section (if SGI_COMPAT). */
3710 bfd_size_type compact_rel_size;
3711 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3712 entry is set to the address of __rld_obj_head as in Irix 5. */
3713 boolean use_rld_obj_head;
3714 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3716 /* This is set if we see any mips16 stub sections. */
3717 boolean mips16_stubs_seen;
3720 /* Look up an entry in a MIPS ELF linker hash table. */
3722 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3723 ((struct mips_elf_link_hash_entry *) \
3724 elf_link_hash_lookup (&(table)->root, (string), (create), \
3727 /* Traverse a MIPS ELF linker hash table. */
3729 #define mips_elf_link_hash_traverse(table, func, info) \
3730 (elf_link_hash_traverse \
3732 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3735 /* Get the MIPS ELF linker hash table from a link_info structure. */
3737 #define mips_elf_hash_table(p) \
3738 ((struct mips_elf_link_hash_table *) ((p)->hash))
3740 static boolean mips_elf_output_extsym
3741 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3743 /* Create an entry in a MIPS ELF linker hash table. */
3745 static struct bfd_hash_entry *
3746 mips_elf_link_hash_newfunc (entry, table, string)
3747 struct bfd_hash_entry *entry;
3748 struct bfd_hash_table *table;
3751 struct mips_elf_link_hash_entry *ret =
3752 (struct mips_elf_link_hash_entry *) entry;
3754 /* Allocate the structure if it has not already been allocated by a
3756 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3757 ret = ((struct mips_elf_link_hash_entry *)
3758 bfd_hash_allocate (table,
3759 sizeof (struct mips_elf_link_hash_entry)));
3760 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3761 return (struct bfd_hash_entry *) ret;
3763 /* Call the allocation method of the superclass. */
3764 ret = ((struct mips_elf_link_hash_entry *)
3765 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3767 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3769 /* Set local fields. */
3770 memset (&ret->esym, 0, sizeof (EXTR));
3771 /* We use -2 as a marker to indicate that the information has
3772 not been set. -1 means there is no associated ifd. */
3774 ret->possibly_dynamic_relocs = 0;
3775 ret->min_dyn_reloc_index = 0;
3776 ret->fn_stub = NULL;
3777 ret->need_fn_stub = false;
3778 ret->call_stub = NULL;
3779 ret->call_fp_stub = NULL;
3782 return (struct bfd_hash_entry *) ret;
3785 /* Create a MIPS ELF linker hash table. */
3787 struct bfd_link_hash_table *
3788 _bfd_mips_elf_link_hash_table_create (abfd)
3791 struct mips_elf_link_hash_table *ret;
3793 ret = ((struct mips_elf_link_hash_table *)
3794 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3795 if (ret == (struct mips_elf_link_hash_table *) NULL)
3798 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3799 mips_elf_link_hash_newfunc))
3801 bfd_release (abfd, ret);
3806 /* We no longer use this. */
3807 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3808 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3810 ret->procedure_count = 0;
3811 ret->compact_rel_size = 0;
3812 ret->use_rld_obj_head = false;
3814 ret->mips16_stubs_seen = false;
3816 return &ret->root.root;
3819 /* Hook called by the linker routine which adds symbols from an object
3820 file. We must handle the special MIPS section numbers here. */
3824 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3826 struct bfd_link_info *info;
3827 const Elf_Internal_Sym *sym;
3829 flagword *flagsp ATTRIBUTE_UNUSED;
3833 if (SGI_COMPAT (abfd)
3834 && (abfd->flags & DYNAMIC) != 0
3835 && strcmp (*namep, "_rld_new_interface") == 0)
3837 /* Skip Irix 5 rld entry name. */
3842 switch (sym->st_shndx)
3845 /* Common symbols less than the GP size are automatically
3846 treated as SHN_MIPS_SCOMMON symbols. */
3847 if (sym->st_size > elf_gp_size (abfd)
3848 || IRIX_COMPAT (abfd) == ict_irix6)
3851 case SHN_MIPS_SCOMMON:
3852 *secp = bfd_make_section_old_way (abfd, ".scommon");
3853 (*secp)->flags |= SEC_IS_COMMON;
3854 *valp = sym->st_size;
3858 /* This section is used in a shared object. */
3859 if (mips_elf_text_section_ptr == NULL)
3861 /* Initialize the section. */
3862 mips_elf_text_section.name = ".text";
3863 mips_elf_text_section.flags = SEC_NO_FLAGS;
3864 mips_elf_text_section.output_section = NULL;
3865 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3866 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3867 mips_elf_text_symbol.name = ".text";
3868 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3869 mips_elf_text_symbol.section = &mips_elf_text_section;
3870 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3871 mips_elf_text_section_ptr = &mips_elf_text_section;
3873 /* This code used to do *secp = bfd_und_section_ptr if
3874 info->shared. I don't know why, and that doesn't make sense,
3875 so I took it out. */
3876 *secp = mips_elf_text_section_ptr;
3879 case SHN_MIPS_ACOMMON:
3880 /* Fall through. XXX Can we treat this as allocated data? */
3882 /* This section is used in a shared object. */
3883 if (mips_elf_data_section_ptr == NULL)
3885 /* Initialize the section. */
3886 mips_elf_data_section.name = ".data";
3887 mips_elf_data_section.flags = SEC_NO_FLAGS;
3888 mips_elf_data_section.output_section = NULL;
3889 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3890 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3891 mips_elf_data_symbol.name = ".data";
3892 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3893 mips_elf_data_symbol.section = &mips_elf_data_section;
3894 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3895 mips_elf_data_section_ptr = &mips_elf_data_section;
3897 /* This code used to do *secp = bfd_und_section_ptr if
3898 info->shared. I don't know why, and that doesn't make sense,
3899 so I took it out. */
3900 *secp = mips_elf_data_section_ptr;
3903 case SHN_MIPS_SUNDEFINED:
3904 *secp = bfd_und_section_ptr;
3908 if (SGI_COMPAT (abfd)
3910 && info->hash->creator == abfd->xvec
3911 && strcmp (*namep, "__rld_obj_head") == 0)
3913 struct elf_link_hash_entry *h;
3915 /* Mark __rld_obj_head as dynamic. */
3917 if (! (_bfd_generic_link_add_one_symbol
3918 (info, abfd, *namep, BSF_GLOBAL, *secp,
3919 (bfd_vma) *valp, (const char *) NULL, false,
3920 get_elf_backend_data (abfd)->collect,
3921 (struct bfd_link_hash_entry **) &h)))
3923 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3924 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3925 h->type = STT_OBJECT;
3927 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3930 mips_elf_hash_table (info)->use_rld_obj_head = true;
3933 /* If this is a mips16 text symbol, add 1 to the value to make it
3934 odd. This will cause something like .word SYM to come up with
3935 the right value when it is loaded into the PC. */
3936 if (sym->st_other == STO_MIPS16)
3942 /* Structure used to pass information to mips_elf_output_extsym. */
3947 struct bfd_link_info *info;
3948 struct ecoff_debug_info *debug;
3949 const struct ecoff_debug_swap *swap;
3953 /* This routine is used to write out ECOFF debugging external symbol
3954 information. It is called via mips_elf_link_hash_traverse. The
3955 ECOFF external symbol information must match the ELF external
3956 symbol information. Unfortunately, at this point we don't know
3957 whether a symbol is required by reloc information, so the two
3958 tables may wind up being different. We must sort out the external
3959 symbol information before we can set the final size of the .mdebug
3960 section, and we must set the size of the .mdebug section before we
3961 can relocate any sections, and we can't know which symbols are
3962 required by relocation until we relocate the sections.
3963 Fortunately, it is relatively unlikely that any symbol will be
3964 stripped but required by a reloc. In particular, it can not happen
3965 when generating a final executable. */
3968 mips_elf_output_extsym (h, data)
3969 struct mips_elf_link_hash_entry *h;
3972 struct extsym_info *einfo = (struct extsym_info *) data;
3974 asection *sec, *output_section;
3976 if (h->root.indx == -2)
3978 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3979 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3980 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3981 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3983 else if (einfo->info->strip == strip_all
3984 || (einfo->info->strip == strip_some
3985 && bfd_hash_lookup (einfo->info->keep_hash,
3986 h->root.root.root.string,
3987 false, false) == NULL))
3995 if (h->esym.ifd == -2)
3998 h->esym.cobol_main = 0;
3999 h->esym.weakext = 0;
4000 h->esym.reserved = 0;
4001 h->esym.ifd = ifdNil;
4002 h->esym.asym.value = 0;
4003 h->esym.asym.st = stGlobal;
4005 if (SGI_COMPAT (einfo->abfd)
4006 && (h->root.root.type == bfd_link_hash_undefined
4007 || h->root.root.type == bfd_link_hash_undefweak))
4011 /* Use undefined class. Also, set class and type for some
4013 name = h->root.root.root.string;
4014 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4015 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4017 h->esym.asym.sc = scData;
4018 h->esym.asym.st = stLabel;
4019 h->esym.asym.value = 0;
4021 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4023 h->esym.asym.sc = scAbs;
4024 h->esym.asym.st = stLabel;
4025 h->esym.asym.value =
4026 mips_elf_hash_table (einfo->info)->procedure_count;
4028 else if (strcmp (name, "_gp_disp") == 0)
4030 h->esym.asym.sc = scAbs;
4031 h->esym.asym.st = stLabel;
4032 h->esym.asym.value = elf_gp (einfo->abfd);
4035 h->esym.asym.sc = scUndefined;
4037 else if (h->root.root.type != bfd_link_hash_defined
4038 && h->root.root.type != bfd_link_hash_defweak)
4039 h->esym.asym.sc = scAbs;
4044 sec = h->root.root.u.def.section;
4045 output_section = sec->output_section;
4047 /* When making a shared library and symbol h is the one from
4048 the another shared library, OUTPUT_SECTION may be null. */
4049 if (output_section == NULL)
4050 h->esym.asym.sc = scUndefined;
4053 name = bfd_section_name (output_section->owner, output_section);
4055 if (strcmp (name, ".text") == 0)
4056 h->esym.asym.sc = scText;
4057 else if (strcmp (name, ".data") == 0)
4058 h->esym.asym.sc = scData;
4059 else if (strcmp (name, ".sdata") == 0)
4060 h->esym.asym.sc = scSData;
4061 else if (strcmp (name, ".rodata") == 0
4062 || strcmp (name, ".rdata") == 0)
4063 h->esym.asym.sc = scRData;
4064 else if (strcmp (name, ".bss") == 0)
4065 h->esym.asym.sc = scBss;
4066 else if (strcmp (name, ".sbss") == 0)
4067 h->esym.asym.sc = scSBss;
4068 else if (strcmp (name, ".init") == 0)
4069 h->esym.asym.sc = scInit;
4070 else if (strcmp (name, ".fini") == 0)
4071 h->esym.asym.sc = scFini;
4073 h->esym.asym.sc = scAbs;
4077 h->esym.asym.reserved = 0;
4078 h->esym.asym.index = indexNil;
4081 if (h->root.root.type == bfd_link_hash_common)
4082 h->esym.asym.value = h->root.root.u.c.size;
4083 else if (h->root.root.type == bfd_link_hash_defined
4084 || h->root.root.type == bfd_link_hash_defweak)
4086 if (h->esym.asym.sc == scCommon)
4087 h->esym.asym.sc = scBss;
4088 else if (h->esym.asym.sc == scSCommon)
4089 h->esym.asym.sc = scSBss;
4091 sec = h->root.root.u.def.section;
4092 output_section = sec->output_section;
4093 if (output_section != NULL)
4094 h->esym.asym.value = (h->root.root.u.def.value
4095 + sec->output_offset
4096 + output_section->vma);
4098 h->esym.asym.value = 0;
4100 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4102 /* Set type and value for a symbol with a function stub. */
4103 h->esym.asym.st = stProc;
4104 sec = h->root.root.u.def.section;
4106 h->esym.asym.value = 0;
4109 output_section = sec->output_section;
4110 if (output_section != NULL)
4111 h->esym.asym.value = (h->root.plt.offset
4112 + sec->output_offset
4113 + output_section->vma);
4115 h->esym.asym.value = 0;
4122 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4123 h->root.root.root.string,
4126 einfo->failed = true;
4133 /* Create a runtime procedure table from the .mdebug section. */
4136 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4139 struct bfd_link_info *info;
4141 struct ecoff_debug_info *debug;
4143 const struct ecoff_debug_swap *swap;
4144 HDRR *hdr = &debug->symbolic_header;
4146 struct rpdr_ext *erp;
4148 struct pdr_ext *epdr;
4149 struct sym_ext *esym;
4152 unsigned long size, count;
4153 unsigned long sindex;
4157 const char *no_name_func = _("static procedure (no name)");
4165 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4167 sindex = strlen (no_name_func) + 1;
4168 count = hdr->ipdMax;
4171 size = swap->external_pdr_size;
4173 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4177 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4180 size = sizeof (RPDR);
4181 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4185 sv = (char **) bfd_malloc (sizeof (char *) * count);
4189 count = hdr->isymMax;
4190 size = swap->external_sym_size;
4191 esym = (struct sym_ext *) bfd_malloc (size * count);
4195 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4198 count = hdr->issMax;
4199 ss = (char *) bfd_malloc (count);
4202 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4205 count = hdr->ipdMax;
4206 for (i = 0; i < count; i++, rp++)
4208 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4209 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4210 rp->adr = sym.value;
4211 rp->regmask = pdr.regmask;
4212 rp->regoffset = pdr.regoffset;
4213 rp->fregmask = pdr.fregmask;
4214 rp->fregoffset = pdr.fregoffset;
4215 rp->frameoffset = pdr.frameoffset;
4216 rp->framereg = pdr.framereg;
4217 rp->pcreg = pdr.pcreg;
4219 sv[i] = ss + sym.iss;
4220 sindex += strlen (sv[i]) + 1;
4224 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4225 size = BFD_ALIGN (size, 16);
4226 rtproc = (PTR) bfd_alloc (abfd, size);
4229 mips_elf_hash_table (info)->procedure_count = 0;
4233 mips_elf_hash_table (info)->procedure_count = count + 2;
4235 erp = (struct rpdr_ext *) rtproc;
4236 memset (erp, 0, sizeof (struct rpdr_ext));
4238 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4239 strcpy (str, no_name_func);
4240 str += strlen (no_name_func) + 1;
4241 for (i = 0; i < count; i++)
4243 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4244 strcpy (str, sv[i]);
4245 str += strlen (sv[i]) + 1;
4247 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
4249 /* Set the size and contents of .rtproc section. */
4250 s->_raw_size = size;
4251 s->contents = (bfd_byte *) rtproc;
4253 /* Skip this section later on (I don't think this currently
4254 matters, but someday it might). */
4255 s->link_order_head = (struct bfd_link_order *) NULL;
4284 /* A comparison routine used to sort .gptab entries. */
4287 gptab_compare (p1, p2)
4291 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4292 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4294 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4297 /* We need to use a special link routine to handle the .reginfo and
4298 the .mdebug sections. We need to merge all instances of these
4299 sections together, not write them all out sequentially. */
4302 _bfd_mips_elf_final_link (abfd, info)
4304 struct bfd_link_info *info;
4308 struct bfd_link_order *p;
4309 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4310 asection *rtproc_sec;
4311 Elf32_RegInfo reginfo;
4312 struct ecoff_debug_info debug;
4313 const struct ecoff_debug_swap *swap
4314 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4315 HDRR *symhdr = &debug.symbolic_header;
4316 PTR mdebug_handle = NULL;
4318 /* If all the things we linked together were PIC, but we're
4319 producing an executable (rather than a shared object), then the
4320 resulting file is CPIC (i.e., it calls PIC code.) */
4322 && !info->relocateable
4323 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4325 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4326 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4329 /* We'd carefully arranged the dynamic symbol indices, and then the
4330 generic size_dynamic_sections renumbered them out from under us.
4331 Rather than trying somehow to prevent the renumbering, just do
4333 if (elf_hash_table (info)->dynobj)
4337 struct mips_got_info *g;
4339 /* When we resort, we must tell mips_elf_sort_hash_table what
4340 the lowest index it may use is. That's the number of section
4341 symbols we're going to add. The generic ELF linker only
4342 adds these symbols when building a shared object. Note that
4343 we count the sections after (possibly) removing the .options
4345 if (!mips_elf_sort_hash_table (info, (info->shared
4346 ? bfd_count_sections (abfd) + 1
4350 /* Make sure we didn't grow the global .got region. */
4351 dynobj = elf_hash_table (info)->dynobj;
4352 got = bfd_get_section_by_name (dynobj, ".got");
4353 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4355 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
4356 - g->global_gotsym->dynindx)
4357 <= g->global_gotno);
4360 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4361 include it, even though we don't process it quite right. (Some
4362 entries are supposed to be merged.) Empirically, we seem to be
4363 better off including it then not. */
4364 if (IRIX_COMPAT (abfd) == ict_irix5)
4365 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4367 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4369 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4370 if (p->type == bfd_indirect_link_order)
4371 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4372 (*secpp)->link_order_head = NULL;
4373 *secpp = (*secpp)->next;
4374 --abfd->section_count;
4380 /* Get a value for the GP register. */
4381 if (elf_gp (abfd) == 0)
4383 struct bfd_link_hash_entry *h;
4385 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4386 if (h != (struct bfd_link_hash_entry *) NULL
4387 && h->type == bfd_link_hash_defined)
4388 elf_gp (abfd) = (h->u.def.value
4389 + h->u.def.section->output_section->vma
4390 + h->u.def.section->output_offset);
4393 /* If the relocate_section function needs to do a reloc
4394 involving the GP value, it should make a reloc_dangerous
4395 callback to warn that GP is not defined. */
4399 /* Go through the sections and collect the .reginfo and .mdebug
4403 gptab_data_sec = NULL;
4404 gptab_bss_sec = NULL;
4405 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4407 if (strcmp (o->name, ".reginfo") == 0)
4409 memset (®info, 0, sizeof reginfo);
4411 /* We have found the .reginfo section in the output file.
4412 Look through all the link_orders comprising it and merge
4413 the information together. */
4414 for (p = o->link_order_head;
4415 p != (struct bfd_link_order *) NULL;
4418 asection *input_section;
4420 Elf32_External_RegInfo ext;
4423 if (p->type != bfd_indirect_link_order)
4425 if (p->type == bfd_fill_link_order)
4430 input_section = p->u.indirect.section;
4431 input_bfd = input_section->owner;
4433 /* The linker emulation code has probably clobbered the
4434 size to be zero bytes. */
4435 if (input_section->_raw_size == 0)
4436 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4438 if (! bfd_get_section_contents (input_bfd, input_section,
4444 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4446 reginfo.ri_gprmask |= sub.ri_gprmask;
4447 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4448 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4449 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4450 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4452 /* ri_gp_value is set by the function
4453 mips_elf32_section_processing when the section is
4454 finally written out. */
4456 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4457 elf_link_input_bfd ignores this section. */
4458 input_section->flags &=~ SEC_HAS_CONTENTS;
4461 /* Size has been set in mips_elf_always_size_sections */
4462 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4464 /* Skip this section later on (I don't think this currently
4465 matters, but someday it might). */
4466 o->link_order_head = (struct bfd_link_order *) NULL;
4471 if (strcmp (o->name, ".mdebug") == 0)
4473 struct extsym_info einfo;
4475 /* We have found the .mdebug section in the output file.
4476 Look through all the link_orders comprising it and merge
4477 the information together. */
4478 symhdr->magic = swap->sym_magic;
4479 /* FIXME: What should the version stamp be? */
4481 symhdr->ilineMax = 0;
4485 symhdr->isymMax = 0;
4486 symhdr->ioptMax = 0;
4487 symhdr->iauxMax = 0;
4489 symhdr->issExtMax = 0;
4492 symhdr->iextMax = 0;
4494 /* We accumulate the debugging information itself in the
4495 debug_info structure. */
4497 debug.external_dnr = NULL;
4498 debug.external_pdr = NULL;
4499 debug.external_sym = NULL;
4500 debug.external_opt = NULL;
4501 debug.external_aux = NULL;
4503 debug.ssext = debug.ssext_end = NULL;
4504 debug.external_fdr = NULL;
4505 debug.external_rfd = NULL;
4506 debug.external_ext = debug.external_ext_end = NULL;
4508 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4509 if (mdebug_handle == (PTR) NULL)
4512 if (SGI_COMPAT (abfd))
4518 static const char * const name[] =
4519 { ".text", ".init", ".fini", ".data",
4520 ".rodata", ".sdata", ".sbss", ".bss" };
4521 static const int sc[] = { scText, scInit, scFini, scData,
4522 scRData, scSData, scSBss, scBss };
4525 esym.cobol_main = 0;
4529 esym.asym.iss = issNil;
4530 esym.asym.st = stLocal;
4531 esym.asym.reserved = 0;
4532 esym.asym.index = indexNil;
4534 for (i = 0; i < 8; i++)
4536 esym.asym.sc = sc[i];
4537 s = bfd_get_section_by_name (abfd, name[i]);
4540 esym.asym.value = s->vma;
4541 last = s->vma + s->_raw_size;
4544 esym.asym.value = last;
4546 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4552 for (p = o->link_order_head;
4553 p != (struct bfd_link_order *) NULL;
4556 asection *input_section;
4558 const struct ecoff_debug_swap *input_swap;
4559 struct ecoff_debug_info input_debug;
4563 if (p->type != bfd_indirect_link_order)
4565 if (p->type == bfd_fill_link_order)
4570 input_section = p->u.indirect.section;
4571 input_bfd = input_section->owner;
4573 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4574 || (get_elf_backend_data (input_bfd)
4575 ->elf_backend_ecoff_debug_swap) == NULL)
4577 /* I don't know what a non MIPS ELF bfd would be
4578 doing with a .mdebug section, but I don't really
4579 want to deal with it. */
4583 input_swap = (get_elf_backend_data (input_bfd)
4584 ->elf_backend_ecoff_debug_swap);
4586 BFD_ASSERT (p->size == input_section->_raw_size);
4588 /* The ECOFF linking code expects that we have already
4589 read in the debugging information and set up an
4590 ecoff_debug_info structure, so we do that now. */
4591 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4595 if (! (bfd_ecoff_debug_accumulate
4596 (mdebug_handle, abfd, &debug, swap, input_bfd,
4597 &input_debug, input_swap, info)))
4600 /* Loop through the external symbols. For each one with
4601 interesting information, try to find the symbol in
4602 the linker global hash table and save the information
4603 for the output external symbols. */
4604 eraw_src = input_debug.external_ext;
4605 eraw_end = (eraw_src
4606 + (input_debug.symbolic_header.iextMax
4607 * input_swap->external_ext_size));
4609 eraw_src < eraw_end;
4610 eraw_src += input_swap->external_ext_size)
4614 struct mips_elf_link_hash_entry *h;
4616 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4617 if (ext.asym.sc == scNil
4618 || ext.asym.sc == scUndefined
4619 || ext.asym.sc == scSUndefined)
4622 name = input_debug.ssext + ext.asym.iss;
4623 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4624 name, false, false, true);
4625 if (h == NULL || h->esym.ifd != -2)
4631 < input_debug.symbolic_header.ifdMax);
4632 ext.ifd = input_debug.ifdmap[ext.ifd];
4638 /* Free up the information we just read. */
4639 free (input_debug.line);
4640 free (input_debug.external_dnr);
4641 free (input_debug.external_pdr);
4642 free (input_debug.external_sym);
4643 free (input_debug.external_opt);
4644 free (input_debug.external_aux);
4645 free (input_debug.ss);
4646 free (input_debug.ssext);
4647 free (input_debug.external_fdr);
4648 free (input_debug.external_rfd);
4649 free (input_debug.external_ext);
4651 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4652 elf_link_input_bfd ignores this section. */
4653 input_section->flags &=~ SEC_HAS_CONTENTS;
4656 if (SGI_COMPAT (abfd) && info->shared)
4658 /* Create .rtproc section. */
4659 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4660 if (rtproc_sec == NULL)
4662 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4663 | SEC_LINKER_CREATED | SEC_READONLY);
4665 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4666 if (rtproc_sec == NULL
4667 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4668 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4672 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4673 info, rtproc_sec, &debug))
4677 /* Build the external symbol information. */
4680 einfo.debug = &debug;
4682 einfo.failed = false;
4683 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4684 mips_elf_output_extsym,
4689 /* Set the size of the .mdebug section. */
4690 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4692 /* Skip this section later on (I don't think this currently
4693 matters, but someday it might). */
4694 o->link_order_head = (struct bfd_link_order *) NULL;
4699 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4701 const char *subname;
4704 Elf32_External_gptab *ext_tab;
4707 /* The .gptab.sdata and .gptab.sbss sections hold
4708 information describing how the small data area would
4709 change depending upon the -G switch. These sections
4710 not used in executables files. */
4711 if (! info->relocateable)
4715 for (p = o->link_order_head;
4716 p != (struct bfd_link_order *) NULL;
4719 asection *input_section;
4721 if (p->type != bfd_indirect_link_order)
4723 if (p->type == bfd_fill_link_order)
4728 input_section = p->u.indirect.section;
4730 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4731 elf_link_input_bfd ignores this section. */
4732 input_section->flags &=~ SEC_HAS_CONTENTS;
4735 /* Skip this section later on (I don't think this
4736 currently matters, but someday it might). */
4737 o->link_order_head = (struct bfd_link_order *) NULL;
4739 /* Really remove the section. */
4740 for (secpp = &abfd->sections;
4742 secpp = &(*secpp)->next)
4744 *secpp = (*secpp)->next;
4745 --abfd->section_count;
4750 /* There is one gptab for initialized data, and one for
4751 uninitialized data. */
4752 if (strcmp (o->name, ".gptab.sdata") == 0)
4754 else if (strcmp (o->name, ".gptab.sbss") == 0)
4758 (*_bfd_error_handler)
4759 (_("%s: illegal section name `%s'"),
4760 bfd_get_filename (abfd), o->name);
4761 bfd_set_error (bfd_error_nonrepresentable_section);
4765 /* The linker script always combines .gptab.data and
4766 .gptab.sdata into .gptab.sdata, and likewise for
4767 .gptab.bss and .gptab.sbss. It is possible that there is
4768 no .sdata or .sbss section in the output file, in which
4769 case we must change the name of the output section. */
4770 subname = o->name + sizeof ".gptab" - 1;
4771 if (bfd_get_section_by_name (abfd, subname) == NULL)
4773 if (o == gptab_data_sec)
4774 o->name = ".gptab.data";
4776 o->name = ".gptab.bss";
4777 subname = o->name + sizeof ".gptab" - 1;
4778 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4781 /* Set up the first entry. */
4783 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4786 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4787 tab[0].gt_header.gt_unused = 0;
4789 /* Combine the input sections. */
4790 for (p = o->link_order_head;
4791 p != (struct bfd_link_order *) NULL;
4794 asection *input_section;
4798 bfd_size_type gpentry;
4800 if (p->type != bfd_indirect_link_order)
4802 if (p->type == bfd_fill_link_order)
4807 input_section = p->u.indirect.section;
4808 input_bfd = input_section->owner;
4810 /* Combine the gptab entries for this input section one
4811 by one. We know that the input gptab entries are
4812 sorted by ascending -G value. */
4813 size = bfd_section_size (input_bfd, input_section);
4815 for (gpentry = sizeof (Elf32_External_gptab);
4817 gpentry += sizeof (Elf32_External_gptab))
4819 Elf32_External_gptab ext_gptab;
4820 Elf32_gptab int_gptab;
4826 if (! (bfd_get_section_contents
4827 (input_bfd, input_section, (PTR) &ext_gptab,
4828 gpentry, sizeof (Elf32_External_gptab))))
4834 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4836 val = int_gptab.gt_entry.gt_g_value;
4837 add = int_gptab.gt_entry.gt_bytes - last;
4840 for (look = 1; look < c; look++)
4842 if (tab[look].gt_entry.gt_g_value >= val)
4843 tab[look].gt_entry.gt_bytes += add;
4845 if (tab[look].gt_entry.gt_g_value == val)
4851 Elf32_gptab *new_tab;
4854 /* We need a new table entry. */
4855 new_tab = ((Elf32_gptab *)
4856 bfd_realloc ((PTR) tab,
4857 (c + 1) * sizeof (Elf32_gptab)));
4858 if (new_tab == NULL)
4864 tab[c].gt_entry.gt_g_value = val;
4865 tab[c].gt_entry.gt_bytes = add;
4867 /* Merge in the size for the next smallest -G
4868 value, since that will be implied by this new
4871 for (look = 1; look < c; look++)
4873 if (tab[look].gt_entry.gt_g_value < val
4875 || (tab[look].gt_entry.gt_g_value
4876 > tab[max].gt_entry.gt_g_value)))
4880 tab[c].gt_entry.gt_bytes +=
4881 tab[max].gt_entry.gt_bytes;
4886 last = int_gptab.gt_entry.gt_bytes;
4889 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4890 elf_link_input_bfd ignores this section. */
4891 input_section->flags &=~ SEC_HAS_CONTENTS;
4894 /* The table must be sorted by -G value. */
4896 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4898 /* Swap out the table. */
4899 ext_tab = ((Elf32_External_gptab *)
4900 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4901 if (ext_tab == NULL)
4907 for (i = 0; i < c; i++)
4908 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4911 o->_raw_size = c * sizeof (Elf32_External_gptab);
4912 o->contents = (bfd_byte *) ext_tab;
4914 /* Skip this section later on (I don't think this currently
4915 matters, but someday it might). */
4916 o->link_order_head = (struct bfd_link_order *) NULL;
4920 /* Invoke the regular ELF backend linker to do all the work. */
4921 if (ABI_64_P (abfd))
4924 if (!bfd_elf64_bfd_final_link (abfd, info))
4931 else if (!bfd_elf32_bfd_final_link (abfd, info))
4934 /* Now write out the computed sections. */
4936 if (reginfo_sec != (asection *) NULL)
4938 Elf32_External_RegInfo ext;
4940 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
4941 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4942 (file_ptr) 0, sizeof ext))
4946 if (mdebug_sec != (asection *) NULL)
4948 BFD_ASSERT (abfd->output_has_begun);
4949 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4951 mdebug_sec->filepos))
4954 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4957 if (gptab_data_sec != (asection *) NULL)
4959 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4960 gptab_data_sec->contents,
4962 gptab_data_sec->_raw_size))
4966 if (gptab_bss_sec != (asection *) NULL)
4968 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4969 gptab_bss_sec->contents,
4971 gptab_bss_sec->_raw_size))
4975 if (SGI_COMPAT (abfd))
4977 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4978 if (rtproc_sec != NULL)
4980 if (! bfd_set_section_contents (abfd, rtproc_sec,
4981 rtproc_sec->contents,
4983 rtproc_sec->_raw_size))
4991 /* Handle a MIPS ELF HI16 reloc. */
4994 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4996 Elf_Internal_Rela *relhi;
4997 Elf_Internal_Rela *rello;
5004 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5006 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5009 addend += ((insn & 0xffff) << 16) + addlo;
5011 if ((addlo & 0x8000) != 0)
5013 if ((addend & 0x8000) != 0)
5016 bfd_put_32 (input_bfd,
5017 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
5018 contents + relhi->r_offset);
5021 /* Handle a MIPS ELF local GOT16 reloc. */
5024 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
5029 Elf_Internal_Rela *relhi;
5030 Elf_Internal_Rela *rello;
5034 unsigned int assigned_gotno;
5040 bfd_byte *got_contents;
5041 struct mips_got_info *g;
5043 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5045 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5048 addend += ((insn & 0xffff) << 16) + addlo;
5050 if ((addlo & 0x8000) != 0)
5052 if ((addend & 0x8000) != 0)
5055 /* Get a got entry representing requested hipage. */
5056 BFD_ASSERT (elf_section_data (sgot) != NULL);
5057 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5058 BFD_ASSERT (g != NULL);
5060 assigned_gotno = g->assigned_gotno;
5061 got_contents = sgot->contents;
5062 hipage = addend & 0xffff0000;
5064 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
5066 address = bfd_get_32 (input_bfd, got_contents + i * 4);
5067 if (hipage == (address & 0xffff0000))
5071 if (i == assigned_gotno)
5073 if (assigned_gotno >= g->local_gotno)
5075 (*_bfd_error_handler)
5076 (_("more got entries are needed for hipage relocations"));
5077 bfd_set_error (bfd_error_bad_value);
5081 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
5082 ++g->assigned_gotno;
5085 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
5086 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
5087 contents + relhi->r_offset);
5092 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
5095 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
5097 Elf_Internal_Rela *rel;
5103 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5104 bfd_put_32 (input_bfd,
5105 (insn & 0xffff0000) | (offset & 0xffff),
5106 contents + rel->r_offset);
5109 /* Returns the GOT section for ABFD. */
5112 mips_elf_got_section (abfd)
5115 return bfd_get_section_by_name (abfd, ".got");
5118 /* Returns the GOT information associated with the link indicated by
5119 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5122 static struct mips_got_info *
5123 mips_elf_got_info (abfd, sgotp)
5128 struct mips_got_info *g;
5130 sgot = mips_elf_got_section (abfd);
5131 BFD_ASSERT (sgot != NULL);
5132 BFD_ASSERT (elf_section_data (sgot) != NULL);
5133 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5134 BFD_ASSERT (g != NULL);
5141 /* Return whether a relocation is against a local symbol. */
5144 mips_elf_local_relocation_p (input_bfd, relocation, local_sections)
5146 const Elf_Internal_Rela *relocation;
5147 asection **local_sections;
5149 unsigned long r_symndx;
5150 Elf_Internal_Shdr *symtab_hdr;
5152 r_symndx = ELF32_R_SYM (relocation->r_info);
5153 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5154 if (! elf_bad_symtab (input_bfd))
5155 return r_symndx < symtab_hdr->sh_info;
5158 /* The symbol table does not follow the rule that local symbols
5159 must come before globals. */
5160 return local_sections[r_symndx] != NULL;
5164 /* Sign-extend VALUE, which has the indicated number of BITS. */
5167 mips_elf_sign_extend (value, bits)
5171 if (value & (1 << (bits - 1)))
5172 /* VALUE is negative. */
5173 value |= ((bfd_vma) - 1) << bits;
5178 /* Return non-zero if the indicated VALUE has overflowed the maximum
5179 range expressable by a signed number with the indicated number of
5183 mips_elf_overflow_p (value, bits)
5187 bfd_signed_vma svalue = (bfd_signed_vma) value;
5189 if (svalue > (1 << (bits - 1)) - 1)
5190 /* The value is too big. */
5192 else if (svalue < -(1 << (bits - 1)))
5193 /* The value is too small. */
5200 /* Calculate the %high function. */
5203 mips_elf_high (value)
5206 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5209 /* Calculate the %higher function. */
5212 mips_elf_higher (value)
5213 bfd_vma value ATTRIBUTE_UNUSED;
5216 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5219 return (bfd_vma) -1;
5223 /* Calculate the %highest function. */
5226 mips_elf_highest (value)
5227 bfd_vma value ATTRIBUTE_UNUSED;
5230 return ((value + (bfd_vma) 0x800080008000) > 48) & 0xffff;
5233 return (bfd_vma) -1;
5237 /* Returns the GOT index for the global symbol indicated by H. */
5240 mips_elf_global_got_index (abfd, h)
5242 struct elf_link_hash_entry *h;
5246 struct mips_got_info *g;
5248 g = mips_elf_got_info (abfd, &sgot);
5250 /* Once we determine the global GOT entry with the lowest dynamic
5251 symbol table index, we must put all dynamic symbols with greater
5252 indices into the GOT. That makes it easy to calculate the GOT
5254 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5255 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5256 * MIPS_ELF_GOT_SIZE (abfd));
5257 BFD_ASSERT (index < sgot->_raw_size);
5262 /* Returns the offset for the entry at the INDEXth position
5266 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5274 sgot = mips_elf_got_section (dynobj);
5275 gp = _bfd_get_gp_value (output_bfd);
5276 return (sgot->output_section->vma + sgot->output_offset + index -
5280 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5281 symbol table index lower than any we've seen to date, record it for
5285 mips_elf_record_global_got_symbol (h, info, g)
5286 struct elf_link_hash_entry *h;
5287 struct bfd_link_info *info;
5288 struct mips_got_info *g ATTRIBUTE_UNUSED;
5290 /* A global symbol in the GOT must also be in the dynamic symbol
5292 if (h->dynindx == -1
5293 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5296 /* If we've already marked this entry as need GOT space, we don't
5297 need to do it again. */
5298 if (h->got.offset != (bfd_vma) - 1)
5301 /* By setting this to a value other than -1, we are indicating that
5302 there needs to be a GOT entry for H. */
5308 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5309 the dynamic symbols. */
5311 struct mips_elf_hash_sort_data
5313 /* The symbol in the global GOT with the lowest dynamic symbol table
5315 struct elf_link_hash_entry *low;
5316 /* The least dynamic symbol table index corresponding to a symbol
5317 with a GOT entry. */
5318 long min_got_dynindx;
5319 /* The greatest dynamic symbol table index not corresponding to a
5320 symbol without a GOT entry. */
5321 long max_non_got_dynindx;
5324 /* If H needs a GOT entry, assign it the highest available dynamic
5325 index. Otherwise, assign it the lowest available dynamic
5329 mips_elf_sort_hash_table_f (h, data)
5330 struct mips_elf_link_hash_entry *h;
5333 struct mips_elf_hash_sort_data *hsd
5334 = (struct mips_elf_hash_sort_data *) data;
5336 /* Symbols without dynamic symbol table entries aren't interesting
5338 if (h->root.dynindx == -1)
5341 if (h->root.got.offset != 0)
5342 h->root.dynindx = hsd->max_non_got_dynindx++;
5345 h->root.dynindx = --hsd->min_got_dynindx;
5346 hsd->low = (struct elf_link_hash_entry *) h;
5352 /* Sort the dynamic symbol table so that symbols that need GOT entries
5353 appear towards the end. This reduces the amount of GOT space
5354 required. MAX_LOCAL is used to set the number of local symbols
5355 known to be in the dynamic symbol table. During
5356 mips_elf_size_dynamic_sections, this value is 1. Afterward, the
5357 section symbols are added and the count is higher. */
5360 mips_elf_sort_hash_table (info, max_local)
5361 struct bfd_link_info *info;
5362 unsigned long max_local;
5364 struct mips_elf_hash_sort_data hsd;
5365 struct mips_got_info *g;
5368 dynobj = elf_hash_table (info)->dynobj;
5371 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5372 hsd.max_non_got_dynindx = max_local;
5373 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5374 elf_hash_table (info)),
5375 mips_elf_sort_hash_table_f,
5378 /* There shoud have been enough room in the symbol table to
5379 accomodate both the GOT and non-GOT symbols. */
5380 BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5382 /* Now we know which dynamic symbol has the lowest dynamic symbol
5383 table index in the GOT. */
5384 g = mips_elf_got_info (dynobj, NULL);
5385 g->global_gotsym = hsd.low;
5390 /* Create a local GOT entry for VALUE. Return the index of the entry,
5391 or -1 if it could not be created. */
5394 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5396 struct mips_got_info *g;
5400 if (g->assigned_gotno >= g->local_gotno)
5402 /* We didn't allocate enough space in the GOT. */
5403 (*_bfd_error_handler)
5404 (_("not enough GOT space for local GOT entries"));
5405 bfd_set_error (bfd_error_bad_value);
5406 return (bfd_vma) -1;
5409 MIPS_ELF_PUT_WORD (abfd, value,
5411 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5412 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5415 /* Returns the GOT offset at which the indicated address can be found.
5416 If there is not yet a GOT entry for this value, create one. Returns
5417 -1 if no satisfactory GOT offset can be found. */
5420 mips_elf_local_got_index (abfd, info, value)
5422 struct bfd_link_info *info;
5426 struct mips_got_info *g;
5429 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5431 /* Look to see if we already have an appropriate entry. */
5432 for (entry = (sgot->contents
5433 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5434 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5435 entry += MIPS_ELF_GOT_SIZE (abfd))
5437 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5438 if (address == value)
5439 return entry - sgot->contents;
5442 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5445 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5446 are supposed to be placed at small offsets in the GOT, i.e.,
5447 within 32KB of GP. Return the index into the GOT for this page,
5448 and store the offset from this entry to the desired address in
5449 OFFSETP, if it is non-NULL. */
5452 mips_elf_got_page (abfd, info, value, offsetp)
5454 struct bfd_link_info *info;
5459 struct mips_got_info *g;
5461 bfd_byte *last_entry;
5465 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5467 /* Look to see if we aleady have an appropriate entry. */
5468 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5469 for (entry = (sgot->contents
5470 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5471 entry != last_entry;
5472 entry += MIPS_ELF_GOT_SIZE (abfd))
5474 address = MIPS_ELF_GET_WORD (abfd, entry);
5476 if (!mips_elf_overflow_p (value - address, 16))
5478 /* This entry will serve as the page pointer. We can add a
5479 16-bit number to it to get the actual address. */
5480 index = entry - sgot->contents;
5485 /* If we didn't have an appropriate entry, we create one now. */
5486 if (entry == last_entry)
5487 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5491 address = MIPS_ELF_GET_WORD (abfd, entry);
5492 *offsetp = value - address;
5498 /* Find a GOT entry whose higher-order 16 bits are the same as those
5499 for value. Return the index into the GOT for this entry. */
5502 mips_elf_got16_entry (abfd, info, value)
5504 struct bfd_link_info *info;
5508 struct mips_got_info *g;
5510 bfd_byte *last_entry;
5514 value &= 0xffff0000;
5515 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5517 /* Look to see if we already have an appropriate entry. */
5518 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5519 for (entry = (sgot->contents
5520 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5521 entry != last_entry;
5522 entry += MIPS_ELF_GOT_SIZE (abfd))
5524 address = MIPS_ELF_GET_WORD (abfd, entry);
5525 if ((address & 0xffff0000) == value)
5527 /* This entry has the right high-order 16 bits. */
5528 index = MIPS_ELF_GOT_SIZE (abfd) * (entry - sgot->contents);
5533 /* If we didn't have an appropriate entry, we create one now. */
5534 if (entry == last_entry)
5535 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5540 /* Sets *ADDENDP to the addend for the first R_MIPS_LO16 relocation
5541 found, beginning with RELOCATION. RELEND is one-past-the-end of
5542 the relocation table. */
5545 mips_elf_next_lo16_addend (relocation, relend, addendp)
5546 const Elf_Internal_Rela *relocation;
5547 const Elf_Internal_Rela *relend;
5550 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5551 immediately following. However, for the IRIX6 ABI, the next
5552 relocation may be a composed relocation consisting of several
5553 relocations for the same address. In that case, the R_MIPS_LO16
5554 relocation may occur as one of these. We permit a similar
5555 extension in general, as that is useful for GCC. */
5556 while (relocation < relend)
5558 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS_LO16)
5560 *addendp = relocation->r_addend;
5567 /* We didn't find it. */
5568 bfd_set_error (bfd_error_bad_value);
5572 /* Create a rel.dyn relocation for the dynamic linker to resolve. The
5573 relocatin is against the symbol with the dynamic symbol table index
5574 DYNINDX. REL is the original relocation, which is now being made
5578 mips_elf_create_dynamic_relocation (output_bfd, info, rel, dynindx,
5579 addend, input_section)
5581 struct bfd_link_info *info;
5582 const Elf_Internal_Rela *rel;
5585 asection *input_section;
5587 Elf_Internal_Rel outrel;
5593 r_type = ELF32_R_TYPE (rel->r_info);
5594 dynobj = elf_hash_table (info)->dynobj;
5596 = bfd_get_section_by_name (dynobj,
5597 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5598 BFD_ASSERT (sreloc != NULL);
5602 /* The symbol for the relocation is the same as it was for the
5603 original relocation. */
5604 outrel.r_info = ELF32_R_INFO (dynindx, R_MIPS_REL32);
5606 /* The offset for the dynamic relocation is the same as for the
5607 original relocation, adjusted by the offset at which the original
5608 section is output. */
5609 if (elf_section_data (input_section)->stab_info == NULL)
5610 outrel.r_offset = rel->r_offset;
5615 off = (_bfd_stab_section_offset
5616 (output_bfd, &elf_hash_table (info)->stab_info,
5618 &elf_section_data (input_section)->stab_info,
5620 if (off == (bfd_vma) -1)
5622 outrel.r_offset = off;
5624 outrel.r_offset += (input_section->output_section->vma
5625 + input_section->output_offset);
5627 /* If we've decided to skip this relocation, just output an emtpy
5630 memset (&outrel, 0, sizeof (outrel));
5632 if (ABI_64_P (output_bfd))
5634 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5635 (output_bfd, &outrel,
5637 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5640 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5641 (((Elf32_External_Rel *)
5643 + sreloc->reloc_count));
5644 ++sreloc->reloc_count;
5646 /* Make sure the output section is writable. The dynamic linker
5647 will be writing to it. */
5648 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5651 /* On IRIX5, make an entry of compact relocation info. */
5652 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5654 asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5659 Elf32_crinfo cptrel;
5661 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5662 cptrel.vaddr = (rel->r_offset
5663 + input_section->output_section->vma
5664 + input_section->output_offset);
5665 if (r_type == R_MIPS_REL32)
5666 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5668 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5669 mips_elf_set_cr_dist2to (cptrel, 0);
5670 cptrel.konst = addend;
5672 cr = (scpt->contents
5673 + sizeof (Elf32_External_compact_rel));
5674 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5675 ((Elf32_External_crinfo *) cr
5676 + scpt->reloc_count));
5677 ++scpt->reloc_count;
5681 return sreloc->reloc_count - 1;
5684 /* Calculate the value produced by the RELOCATION (which comes from
5685 the INPUT_BFD). The ADDEND is the addend to use for this
5686 RELOCATION; RELOCATION->R_ADDEND is ignored.
5688 The result of the relocation calculation is stored in VALUEP.
5689 REQUIRE_JALXP indicates whether or not the opcode used with this
5690 relocation must be JALX.
5692 This function returns bfd_reloc_continue if the caller need take no
5693 further action regarding this relocation, bfd_reloc_notsupported if
5694 something goes dramatically wrong, bfd_reloc_overflow if an
5695 overflow occurs, and bfd_reloc_ok to indicate success. */
5697 static bfd_reloc_status_type
5698 mips_elf_calculate_relocation (abfd,
5712 asection *input_section;
5713 struct bfd_link_info *info;
5714 const Elf_Internal_Rela *relocation;
5716 reloc_howto_type *howto;
5717 Elf_Internal_Sym *local_syms;
5718 asection **local_sections;
5721 boolean *require_jalxp;
5723 /* The eventual value we will return. */
5725 /* The address of the symbol against which the relocation is
5728 /* The final GP value to be used for the relocatable, executable, or
5729 shared object file being produced. */
5730 bfd_vma gp = (bfd_vma) - 1;
5731 /* The place (section offset or address) of the storage unit being
5734 /* The value of GP used to create the relocatable object. */
5735 bfd_vma gp0 = (bfd_vma) - 1;
5736 /* The offset into the global offset table at which the address of
5737 the relocation entry symbol, adjusted by the addend, resides
5738 during execution. */
5739 bfd_vma g = (bfd_vma) - 1;
5740 /* The section in which the symbol referenced by the relocation is
5742 asection *sec = NULL;
5743 struct mips_elf_link_hash_entry* h = NULL;
5744 /* True if the symbol referred to by this relocation is a local
5747 /* True if the symbol referred to by this relocation is "_gp_disp". */
5748 boolean gp_disp_p = false;
5749 Elf_Internal_Shdr *symtab_hdr;
5751 unsigned long r_symndx;
5753 /* True if overflow occurred during the calculation of the
5754 relocation value. */
5755 boolean overflowed_p;
5756 /* True if this relocation refers to a MIPS16 function. */
5757 boolean target_is_16_bit_code_p = false;
5759 /* Parse the relocation. */
5760 r_symndx = ELF32_R_SYM (relocation->r_info);
5761 r_type = ELF32_R_TYPE (relocation->r_info);
5762 p = (input_section->output_section->vma
5763 + input_section->output_offset
5764 + relocation->r_offset);
5766 /* Assume that there will be no overflow. */
5767 overflowed_p = false;
5769 /* Figure out whether or not the symbol is local, and get the offset
5770 used in the array of hash table entries. */
5771 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5772 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5774 if (! elf_bad_symtab (input_bfd))
5775 extsymoff = symtab_hdr->sh_info;
5778 /* The symbol table does not follow the rule that local symbols
5779 must come before globals. */
5783 /* Figure out the value of the symbol. */
5786 Elf_Internal_Sym *sym;
5788 sym = local_syms + r_symndx;
5789 sec = local_sections[r_symndx];
5791 symbol = sec->output_section->vma + sec->output_offset;
5792 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5793 symbol += sym->st_value;
5795 /* MIPS16 text labels should be treated as odd. */
5796 if (sym->st_other == STO_MIPS16)
5799 /* Record the name of this symbol, for our caller. */
5800 *namep = bfd_elf_string_from_elf_section (input_bfd,
5801 symtab_hdr->sh_link,
5804 *namep = bfd_section_name (input_bfd, sec);
5806 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
5810 /* For global symbols we look up the symbol in the hash-table. */
5811 h = ((struct mips_elf_link_hash_entry *)
5812 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5813 /* Find the real hash-table entry for this symbol. */
5814 while (h->root.type == bfd_link_hash_indirect
5815 || h->root.type == bfd_link_hash_warning)
5816 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5818 /* Record the name of this symbol, for our caller. */
5819 *namep = h->root.root.root.string;
5821 /* See if this is the special _gp_disp symbol. Note that such a
5822 symbol must always be a global symbol. */
5823 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5825 /* Relocations against _gp_disp are permitted only with
5826 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5827 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5828 return bfd_reloc_notsupported;
5832 /* If this symbol is defined, calculate its address. Note that
5833 _gp_disp is a magic symbol, always implicitly defined by the
5834 linker, so it's inappropriate to check to see whether or not
5836 else if ((h->root.root.type == bfd_link_hash_defined
5837 || h->root.root.type == bfd_link_hash_defweak)
5838 && h->root.root.u.def.section)
5840 sec = h->root.root.u.def.section;
5841 if (sec->output_section)
5842 symbol = (h->root.root.u.def.value
5843 + sec->output_section->vma
5844 + sec->output_offset);
5846 symbol = h->root.root.u.def.value;
5850 (*info->callbacks->undefined_symbol)
5851 (info, h->root.root.root.string, input_bfd,
5852 input_section, relocation->r_offset);
5853 return bfd_reloc_undefined;
5856 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
5859 /* If this is a 32-bit call to a 16-bit function with a stub, we
5860 need to redirect the call to the stub, unless we're already *in*
5862 if (r_type != R_MIPS16_26 && !info->relocateable
5863 && ((h != NULL && h->fn_stub != NULL)
5864 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
5865 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5866 && !mips_elf_stub_section_p (input_bfd, input_section))
5868 /* This is a 32-bit call to a 16-bit function. We should
5869 have already noticed that we were going to need the
5872 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5875 BFD_ASSERT (h->need_fn_stub);
5879 symbol = sec->output_section->vma + sec->output_offset;
5881 /* If this is a 16-bit call to a 32-bit function with a stub, we
5882 need to redirect the call to the stub. */
5883 else if (r_type == R_MIPS16_26 && !info->relocateable
5885 && (h->call_stub != NULL || h->call_fp_stub != NULL)
5886 && !target_is_16_bit_code_p)
5888 /* If both call_stub and call_fp_stub are defined, we can figure
5889 out which one to use by seeing which one appears in the input
5891 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5896 for (o = input_bfd->sections; o != NULL; o = o->next)
5898 if (strncmp (bfd_get_section_name (input_bfd, o),
5899 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5901 sec = h->call_fp_stub;
5908 else if (h->call_stub != NULL)
5911 sec = h->call_fp_stub;
5913 BFD_ASSERT (sec->_raw_size > 0);
5914 symbol = sec->output_section->vma + sec->output_offset;
5917 /* Calls from 16-bit code to 32-bit code and vice versa require the
5918 special jalx instruction. */
5919 *require_jalxp = (!info->relocateable
5920 && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p));
5922 /* If we haven't already determined the GOT offset, or the GP value,
5923 and we're going to need it, get it now. */
5927 case R_MIPS_GOT_DISP:
5928 case R_MIPS_GOT_HI16:
5929 case R_MIPS_CALL_HI16:
5930 case R_MIPS_GOT_LO16:
5931 case R_MIPS_CALL_LO16:
5932 /* Find the index into the GOT where this value is located. */
5935 BFD_ASSERT (addend == 0);
5936 g = mips_elf_global_got_index
5937 (elf_hash_table (info)->dynobj,
5938 (struct elf_link_hash_entry*) h);
5942 g = mips_elf_local_got_index (abfd, info, symbol + addend);
5943 if (g == (bfd_vma) -1)
5947 /* Convert GOT indices to actual offsets. */
5948 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5954 case R_MIPS_GPREL16:
5955 case R_MIPS_GPREL32:
5956 gp0 = _bfd_get_gp_value (input_bfd);
5957 gp = _bfd_get_gp_value (abfd);
5964 /* Figure out what kind of relocation is being performed. */
5968 return bfd_reloc_continue;
5971 value = symbol + mips_elf_sign_extend (addend, 16);
5972 overflowed_p = mips_elf_overflow_p (value, 16);
5978 /* If we're creating a shared library, or this relocation is
5979 against a symbol in a shared library, then we can't know
5980 where the symbol will end up. So, we create a relocation
5981 record in the output, and leave the job up to the dynamic
5983 if (info->shared || !sec->output_section)
5985 unsigned int reloc_index;
5987 BFD_ASSERT (h != NULL);
5989 = mips_elf_create_dynamic_relocation (abfd,
5995 if (h->min_dyn_reloc_index == 0
5996 || reloc_index < h->min_dyn_reloc_index)
5997 h->min_dyn_reloc_index = reloc_index;
5998 value = symbol + addend;
6002 if (r_type != R_MIPS_REL32)
6003 value = symbol + addend;
6007 value &= howto->dst_mask;
6011 /* The calculation for R_MIPS_26 is just the same as for an
6012 R_MIPS_26. It's only the storage of the relocated field into
6013 the output file that's different. That's handled in
6014 mips_elf_perform_relocation. So, we just fall through to the
6015 R_MIPS_26 case here. */
6018 value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
6020 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
6021 value &= howto->dst_mask;
6027 value = mips_elf_high (addend + symbol);
6028 value &= howto->dst_mask;
6032 value = mips_elf_high (addend + gp - p);
6033 overflowed_p = mips_elf_overflow_p (value, 16);
6039 value = (symbol + addend) & howto->dst_mask;
6042 value = addend + gp - p + 4;
6043 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6044 for overflow. But, on, say, Irix 5, relocations against
6045 _gp_disp are normally generated from the .cpload
6046 pseudo-op. It generates code that normally looks like
6049 lui $gp,%hi(_gp_disp)
6050 addiu $gp,$gp,%lo(_gp_disp)
6053 Here $t9 holds the address of the function being called,
6054 as required by the MIPS ELF ABI. The R_MIPS_LO16
6055 relocation can easily overflow in this situation, but the
6056 R_MIPS_HI16 relocation will handle the overflow.
6057 Therefore, we consider this a bug in the MIPS ABI, and do
6058 not check for overflow here. */
6062 case R_MIPS_LITERAL:
6063 /* Because we don't merge literal sections, we can handle this
6064 just like R_MIPS_GPREL16. In the long run, we should merge
6065 shared literals, and then we will need to additional work
6070 case R_MIPS16_GPREL:
6071 /* The R_MIPS16_GPREL performs the same calculation as
6072 R_MIPS_GPREL16, but stores the relocated bits in a different
6073 order. We don't need to do anything special here; the
6074 differences are handled in mips_elf_perform_relocation. */
6075 case R_MIPS_GPREL16:
6077 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
6079 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
6080 overflowed_p = mips_elf_overflow_p (value, 16);
6086 value = mips_elf_got16_entry (abfd, info, symbol + addend);
6087 if (value == (bfd_vma) -1)
6090 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6093 overflowed_p = mips_elf_overflow_p (value, 16);
6100 case R_MIPS_GOT_DISP:
6102 overflowed_p = mips_elf_overflow_p (value, 16);
6105 case R_MIPS_GPREL32:
6106 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6110 value = mips_elf_sign_extend (addend, 16) + symbol - p;
6111 overflowed_p = mips_elf_overflow_p (value, 16);
6114 case R_MIPS_GOT_HI16:
6115 case R_MIPS_CALL_HI16:
6116 /* We're allowed to handle these two relocations identically.
6117 The dynamic linker is allowed to handle the CALL relocations
6118 differently by creating a lazy evaluation stub. */
6120 value = mips_elf_high (value);
6121 value &= howto->dst_mask;
6124 case R_MIPS_GOT_LO16:
6125 case R_MIPS_CALL_LO16:
6126 value = g & howto->dst_mask;
6129 case R_MIPS_GOT_PAGE:
6130 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
6131 if (value == (bfd_vma) -1)
6133 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6136 overflowed_p = mips_elf_overflow_p (value, 16);
6139 case R_MIPS_GOT_OFST:
6140 mips_elf_got_page (abfd, info, symbol + addend, &value);
6141 overflowed_p = mips_elf_overflow_p (value, 16);
6145 value = symbol - addend;
6146 value &= howto->dst_mask;
6150 value = mips_elf_higher (addend + symbol);
6151 value &= howto->dst_mask;
6154 case R_MIPS_HIGHEST:
6155 value = mips_elf_highest (addend + symbol);
6156 value &= howto->dst_mask;
6159 case R_MIPS_SCN_DISP:
6160 value = symbol + addend - sec->output_offset;
6161 value &= howto->dst_mask;
6166 /* Both of these may be ignored. R_MIPS_JALR is an optimization
6167 hint; we could improve performance by honoring that hint. */
6168 return bfd_reloc_continue;
6170 case R_MIPS_GNU_VTINHERIT:
6171 case R_MIPS_GNU_VTENTRY:
6172 /* We don't do anything with these at present. */
6173 return bfd_reloc_continue;
6176 /* An unrecognized relocation type. */
6177 return bfd_reloc_notsupported;
6180 /* Store the VALUE for our caller. */
6182 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6185 /* Obtain the field relocated by RELOCATION. */
6188 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6189 reloc_howto_type *howto;
6190 const Elf_Internal_Rela *relocation;
6195 bfd_byte *location = contents + relocation->r_offset;
6197 /* Obtain the bytes. */
6198 x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
6200 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
6201 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6202 && bfd_little_endian (input_bfd))
6203 /* The two 16-bit words will be reversed on a little-endian
6204 system. See mips_elf_perform_relocation for more details. */
6205 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6210 /* It has been determined that the result of the RELOCATION is the
6211 VALUE. Use HOWTO to place VALUE into the output file at the
6212 appropriate position. The SECTION is the section to which the
6213 relocation applies. If REQUIRE_JALX is true, then the opcode used
6214 for the relocation must be either JAL or JALX, and it is
6215 unconditionally converted to JALX.
6217 Returns false if anything goes wrong. */
6220 mips_elf_perform_relocation (info, howto, relocation, value,
6221 input_bfd, input_section,
6222 contents, require_jalx)
6223 struct bfd_link_info *info;
6224 reloc_howto_type *howto;
6225 const Elf_Internal_Rela *relocation;
6228 asection *input_section;
6230 boolean require_jalx;
6234 int r_type = ELF32_R_TYPE (relocation->r_info);
6236 /* Figure out where the relocation is occurring. */
6237 location = contents + relocation->r_offset;
6239 /* Obtain the current value. */
6240 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6242 /* Clear the field we are setting. */
6243 x &= ~howto->dst_mask;
6245 /* If this is the R_MIPS16_26 relocation, we must store the
6246 value in a funny way. */
6247 if (r_type == R_MIPS16_26)
6249 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
6250 Most mips16 instructions are 16 bits, but these instructions
6253 The format of these instructions is:
6255 +--------------+--------------------------------+
6256 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
6257 +--------------+--------------------------------+
6259 +-----------------------------------------------+
6261 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
6262 Note that the immediate value in the first word is swapped.
6264 When producing a relocateable object file, R_MIPS16_26 is
6265 handled mostly like R_MIPS_26. In particular, the addend is
6266 stored as a straight 26-bit value in a 32-bit instruction.
6267 (gas makes life simpler for itself by never adjusting a
6268 R_MIPS16_26 reloc to be against a section, so the addend is
6269 always zero). However, the 32 bit instruction is stored as 2
6270 16-bit values, rather than a single 32-bit value. In a
6271 big-endian file, the result is the same; in a little-endian
6272 file, the two 16-bit halves of the 32 bit value are swapped.
6273 This is so that a disassembler can recognize the jal
6276 When doing a final link, R_MIPS16_26 is treated as a 32 bit
6277 instruction stored as two 16-bit values. The addend A is the
6278 contents of the targ26 field. The calculation is the same as
6279 R_MIPS_26. When storing the calculated value, reorder the
6280 immediate value as shown above, and don't forget to store the
6281 value as two 16-bit values.
6283 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
6287 +--------+----------------------+
6291 +--------+----------------------+
6294 +----------+------+-------------+
6298 +----------+--------------------+
6299 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
6300 ((sub1 << 16) | sub2)).
6302 When producing a relocateable object file, the calculation is
6303 (((A < 2) | (P & 0xf0000000) + S) >> 2)
6304 When producing a fully linked file, the calculation is
6305 let R = (((A < 2) | (P & 0xf0000000) + S) >> 2)
6306 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
6308 if (!info->relocateable)
6309 /* Shuffle the bits according to the formula above. */
6310 value = (((value & 0x1f0000) << 5)
6311 | ((value & 0x3e00000) >> 5)
6312 | (value & 0xffff));
6315 else if (r_type == R_MIPS16_GPREL)
6317 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
6318 mode. A typical instruction will have a format like this:
6320 +--------------+--------------------------------+
6321 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
6322 +--------------+--------------------------------+
6323 ! Major ! rx ! ry ! Imm 4:0 !
6324 +--------------+--------------------------------+
6326 EXTEND is the five bit value 11110. Major is the instruction
6329 This is handled exactly like R_MIPS_GPREL16, except that the
6330 addend is retrieved and stored as shown in this diagram; that
6331 is, the Imm fields above replace the V-rel16 field.
6333 All we need to do here is shuffle the bits appropriately. As
6334 above, the two 16-bit halves must be swapped on a
6335 little-endian system. */
6336 value = (((value & 0x7e0) << 16)
6337 | ((value & 0xf800) << 5)
6341 /* Set the field. */
6342 x |= (value & howto->dst_mask);
6344 /* If required, turn JAL into JALX. */
6348 bfd_vma opcode = x >> 26;
6349 bfd_vma jalx_opcode;
6351 /* Check to see if the opcode is already JAL or JALX. */
6352 if (r_type == R_MIPS16_26)
6354 ok = ((opcode == 0x6) || (opcode == 0x7));
6359 ok = ((opcode == 0x3) || (opcode == 0x1d));
6363 /* If the opcode is not JAL or JALX, there's a problem. */
6366 (*_bfd_error_handler)
6367 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
6368 bfd_get_filename (input_bfd),
6369 input_section->name,
6370 (unsigned long) relocation->r_offset);
6371 bfd_set_error (bfd_error_bad_value);
6375 /* Make this the JALX opcode. */
6376 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6379 /* Swap the high- and low-order 16 bits on little-endian systems
6380 when doing a MIPS16 relocation. */
6381 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
6382 && bfd_little_endian (input_bfd))
6383 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6385 /* Put the value into the output. */
6386 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6390 /* Returns true if SECTION is a MIPS16 stub section. */
6393 mips_elf_stub_section_p (abfd, section)
6394 bfd *abfd ATTRIBUTE_UNUSED;
6397 const char *name = bfd_get_section_name (abfd, section);
6399 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
6400 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6401 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
6404 /* Relocate a MIPS ELF section. */
6407 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6408 contents, relocs, local_syms, local_sections)
6410 struct bfd_link_info *info;
6412 asection *input_section;
6414 Elf_Internal_Rela *relocs;
6415 Elf_Internal_Sym *local_syms;
6416 asection **local_sections;
6418 Elf_Internal_Rela *rel;
6419 const Elf_Internal_Rela *relend;
6421 bfd_vma last_hi16_addend;
6422 boolean use_saved_addend_p = false;
6423 boolean last_hi16_addend_valid_p = false;
6424 struct elf_backend_data *bed;
6426 bed = get_elf_backend_data (output_bfd);
6427 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6428 for (rel = relocs; rel < relend; ++rel)
6432 reloc_howto_type *howto;
6433 boolean require_jalx;
6434 /* True if the relocation is a RELA relocation, rather than a
6436 boolean rela_relocation_p = true;
6437 int r_type = ELF32_R_TYPE (rel->r_info);
6439 /* Find the relocation howto for this relocation. */
6440 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6441 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6442 64-bit code, but make sure all their addresses are in the
6443 lowermost or uppermost 32-bit section of the 64-bit address
6444 space. Thus, when they use an R_MIPS_64 they mean what is
6445 usually meant by R_MIPS_32, with the exception that the
6446 stored value is sign-extended to 64 bits. */
6447 howto = elf_mips_howto_table + R_MIPS_32;
6449 howto = elf_mips_howto_table + r_type;
6451 if (!use_saved_addend_p)
6453 Elf_Internal_Shdr *rel_hdr;
6455 /* If these relocations were originally of the REL variety,
6456 we must pull the addend out of the field that will be
6457 relocated. Otherwise, we simply use the contents of the
6458 RELA relocation. To determine which flavor or relocation
6459 this is, we depend on the fact that the INPUT_SECTION's
6460 REL_HDR is read before its REL_HDR2. */
6461 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6462 if ((size_t) (rel - relocs)
6463 >= (rel_hdr->sh_size / rel_hdr->sh_entsize
6464 * bed->s->int_rels_per_ext_rel))
6465 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6466 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6468 /* Note that this is a REL relocation. */
6469 rela_relocation_p = false;
6471 /* Get the addend, which is stored in the input file. */
6472 addend = mips_elf_obtain_contents (howto,
6476 addend &= howto->src_mask;
6478 /* For some kinds of relocations, the ADDEND is a
6479 combination of the addend stored in two different
6481 if (r_type == R_MIPS_HI16
6482 || (r_type == R_MIPS_GOT16
6483 && mips_elf_local_relocation_p (input_bfd, rel,
6486 /* Scan ahead to find a matching R_MIPS_LO16
6490 if (!mips_elf_next_lo16_addend (rel, relend, &l))
6493 /* Save the high-order bit for later. When we
6494 encounter the R_MIPS_LO16 relocation we will need
6497 last_hi16_addend = addend;
6498 last_hi16_addend_valid_p = true;
6500 /* Compute the combined addend. */
6503 else if (r_type == R_MIPS_LO16)
6505 /* Used the saved HI16 addend. */
6506 if (!last_hi16_addend_valid_p)
6508 bfd_set_error (bfd_error_bad_value);
6511 addend |= last_hi16_addend;
6513 else if (r_type == R_MIPS16_GPREL)
6515 /* The addend is scrambled in the object file. See
6516 mips_elf_perform_relocation for details on the
6518 addend = (((addend & 0x1f0000) >> 5)
6519 | ((addend & 0x7e00000) >> 16)
6524 addend = rel->r_addend;
6527 if (info->relocateable)
6529 Elf_Internal_Sym *sym;
6530 unsigned long r_symndx;
6532 /* Since we're just relocating, all we need to do is copy
6533 the relocations back out to the object file, unless they're
6534 against a section symbol, in which case we need to adjust
6535 by the section offset. */
6537 if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections))
6538 /* A non-local relocation is never against a section. */
6541 r_symndx = ELF32_R_SYM (rel->r_info);
6542 sym = local_syms + r_symndx;
6543 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6546 /* Adjust the addend appropriately. */
6547 addend += local_sections[r_symndx]->output_offset;
6549 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
6550 then we only want to write out the high-order 16 bits.
6551 The subsequent R_MIPS_LO16 will handle the low-order bits. */
6552 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16)
6555 if (rela_relocation_p)
6556 /* If this is a RELA relocation, just update the addend.
6557 We have to cast away constness for REL. */
6558 rel->r_addend = addend;
6561 /* Otherwise, we have to write the value back out. Note
6562 that we use the source mask, rather than the
6563 destination mask because the place to which we are
6564 writing will be source of the addend in the final
6566 addend &= howto->src_mask;
6567 if (!mips_elf_perform_relocation (info, howto, rel, addend,
6568 input_bfd, input_section,
6573 /* Go on to the next relocation. */
6577 /* In the N32 and 64-bit ABIs there may be multiple consecutive
6578 relocations for the same offset. In that case we are
6579 supposed to treat the output of each relocation as the addend
6581 if (rel + 1 < relend
6582 && rel->r_offset == rel[1].r_offset
6583 && r_type != R_MIPS_NONE)
6584 use_saved_addend_p = true;
6586 use_saved_addend_p = false;
6588 /* Figure out what value we are supposed to relocate. */
6589 switch (mips_elf_calculate_relocation (output_bfd,
6602 case bfd_reloc_continue:
6603 /* There's nothing to do. */
6606 case bfd_reloc_undefined:
6607 /* mips_elf_calculate_relocation already called the
6608 undefined_symbol callback. */
6611 case bfd_reloc_notsupported:
6615 case bfd_reloc_overflow:
6616 if (use_saved_addend_p)
6617 /* Ignore overflow until we reach the last relocation for
6618 a given location. */
6622 BFD_ASSERT (name != NULL);
6623 if (! ((*info->callbacks->reloc_overflow)
6624 (info, name, howto->name, (bfd_vma) 0,
6625 input_bfd, input_section, rel->r_offset)))
6638 /* If we've got another relocation for the address, keep going
6639 until we reach the last one. */
6640 if (use_saved_addend_p)
6646 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6647 /* See the comment above about using R_MIPS_64 in the 32-bit
6648 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
6649 that calculated the right value. Now, however, we
6650 sign-extend the 32-bit result to 64-bits, and store it as a
6651 64-bit value. We are especially generous here in that we
6652 go to extreme lengths to support this usage on systems with
6653 only a 32-bit VMA. */
6656 /* Just sign-extend the value, and then fall through to the
6657 normal case, using the R_MIPS_64 howto. That will store
6658 the 64-bit value into a 64-bit area. */
6659 value = mips_elf_sign_extend (value, 64);
6660 howto = elf_mips_howto_table + R_MIPS_64;
6662 /* In the 32-bit VMA case, we must handle sign-extension and
6663 endianness manually. */
6668 if (value & 0x80000000)
6669 sign_bits = 0xffffffff;
6673 /* If only a 32-bit VMA is available do two separate
6675 if (bfd_big_endian (input_bfd))
6677 /* Store the sign-bits (which are most significant)
6679 low_bits = sign_bits;
6685 high_bits = sign_bits;
6687 bfd_put_32 (input_bfd, low_bits,
6688 contents + rel->r_offset);
6689 bfd_put_32 (input_bfd, high_bits,
6690 contents + rel->r_offset + 4);
6695 /* Actually perform the relocation. */
6696 if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
6697 input_section, contents,
6705 /* This hook function is called before the linker writes out a global
6706 symbol. We mark symbols as small common if appropriate. This is
6707 also where we undo the increment of the value for a mips16 symbol. */
6711 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6712 bfd *abfd ATTRIBUTE_UNUSED;
6713 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6714 const char *name ATTRIBUTE_UNUSED;
6715 Elf_Internal_Sym *sym;
6716 asection *input_sec;
6718 /* If we see a common symbol, which implies a relocatable link, then
6719 if a symbol was small common in an input file, mark it as small
6720 common in the output file. */
6721 if (sym->st_shndx == SHN_COMMON
6722 && strcmp (input_sec->name, ".scommon") == 0)
6723 sym->st_shndx = SHN_MIPS_SCOMMON;
6725 if (sym->st_other == STO_MIPS16
6726 && (sym->st_value & 1) != 0)
6732 /* Functions for the dynamic linker. */
6734 /* The name of the dynamic interpreter. This is put in the .interp
6737 #define ELF_DYNAMIC_INTERPRETER(abfd) \
6738 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
6739 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
6740 : "/usr/lib/libc.so.1")
6742 /* Create dynamic sections when linking against a dynamic object. */
6745 _bfd_mips_elf_create_dynamic_sections (abfd, info)
6747 struct bfd_link_info *info;
6749 struct elf_link_hash_entry *h;
6751 register asection *s;
6752 const char * const *namep;
6754 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6755 | SEC_LINKER_CREATED | SEC_READONLY);
6757 /* Mips ABI requests the .dynamic section to be read only. */
6758 s = bfd_get_section_by_name (abfd, ".dynamic");
6761 if (! bfd_set_section_flags (abfd, s, flags))
6765 /* We need to create .got section. */
6766 if (! mips_elf_create_got_section (abfd, info))
6769 /* Create the .msym section on IRIX6. It is used by the dynamic
6770 linker to speed up dynamic relocations, and to avoid computing
6771 the ELF hash for symbols. */
6772 if (IRIX_COMPAT (abfd) == ict_irix6
6773 && !mips_elf_create_msym_section (abfd))
6776 /* Create .stub section. */
6777 if (bfd_get_section_by_name (abfd,
6778 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6780 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6782 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6783 || ! bfd_set_section_alignment (abfd, s,
6784 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6788 if (IRIX_COMPAT (abfd) == ict_irix5
6790 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6792 s = bfd_make_section (abfd, ".rld_map");
6794 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6795 || ! bfd_set_section_alignment (abfd, s,
6796 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6800 /* On IRIX5, we adjust add some additional symbols and change the
6801 alignments of several sections. There is no ABI documentation
6802 indicating that this is necessary on IRIX6, nor any evidence that
6803 the linker takes such action. */
6804 if (IRIX_COMPAT (abfd) == ict_irix5)
6806 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6809 if (! (_bfd_generic_link_add_one_symbol
6810 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
6811 (bfd_vma) 0, (const char *) NULL, false,
6812 get_elf_backend_data (abfd)->collect,
6813 (struct bfd_link_hash_entry **) &h)))
6815 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6816 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6817 h->type = STT_SECTION;
6819 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6823 /* We need to create a .compact_rel section. */
6824 if (! mips_elf_create_compact_rel_section (abfd, info))
6827 /* Change aligments of some sections. */
6828 s = bfd_get_section_by_name (abfd, ".hash");
6830 bfd_set_section_alignment (abfd, s, 4);
6831 s = bfd_get_section_by_name (abfd, ".dynsym");
6833 bfd_set_section_alignment (abfd, s, 4);
6834 s = bfd_get_section_by_name (abfd, ".dynstr");
6836 bfd_set_section_alignment (abfd, s, 4);
6837 s = bfd_get_section_by_name (abfd, ".reginfo");
6839 bfd_set_section_alignment (abfd, s, 4);
6840 s = bfd_get_section_by_name (abfd, ".dynamic");
6842 bfd_set_section_alignment (abfd, s, 4);
6848 if (! (_bfd_generic_link_add_one_symbol
6849 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
6850 (bfd_vma) 0, (const char *) NULL, false,
6851 get_elf_backend_data (abfd)->collect,
6852 (struct bfd_link_hash_entry **) &h)))
6854 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6855 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6856 h->type = STT_SECTION;
6858 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6861 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6863 /* __rld_map is a four byte word located in the .data section
6864 and is filled in by the rtld to contain a pointer to
6865 the _r_debug structure. Its symbol value will be set in
6866 mips_elf_finish_dynamic_symbol. */
6867 s = bfd_get_section_by_name (abfd, ".rld_map");
6868 BFD_ASSERT (s != NULL);
6871 if (! (_bfd_generic_link_add_one_symbol
6872 (info, abfd, "__rld_map", BSF_GLOBAL, s,
6873 (bfd_vma) 0, (const char *) NULL, false,
6874 get_elf_backend_data (abfd)->collect,
6875 (struct bfd_link_hash_entry **) &h)))
6877 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6878 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6879 h->type = STT_OBJECT;
6881 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6889 /* Create the .compact_rel section. */
6892 mips_elf_create_compact_rel_section (abfd, info)
6894 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6897 register asection *s;
6899 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
6901 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
6904 s = bfd_make_section (abfd, ".compact_rel");
6906 || ! bfd_set_section_flags (abfd, s, flags)
6907 || ! bfd_set_section_alignment (abfd, s,
6908 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6911 s->_raw_size = sizeof (Elf32_External_compact_rel);
6917 /* Create the .got section to hold the global offset table. */
6920 mips_elf_create_got_section (abfd, info)
6922 struct bfd_link_info *info;
6925 register asection *s;
6926 struct elf_link_hash_entry *h;
6927 struct mips_got_info *g;
6929 /* This function may be called more than once. */
6930 if (mips_elf_got_section (abfd))
6933 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6934 | SEC_LINKER_CREATED);
6936 s = bfd_make_section (abfd, ".got");
6938 || ! bfd_set_section_flags (abfd, s, flags)
6939 || ! bfd_set_section_alignment (abfd, s, 4))
6942 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
6943 linker script because we don't want to define the symbol if we
6944 are not creating a global offset table. */
6946 if (! (_bfd_generic_link_add_one_symbol
6947 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
6948 (bfd_vma) 0, (const char *) NULL, false,
6949 get_elf_backend_data (abfd)->collect,
6950 (struct bfd_link_hash_entry **) &h)))
6952 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6953 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6954 h->type = STT_OBJECT;
6957 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
6960 /* The first several global offset table entries are reserved. */
6961 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
6963 g = (struct mips_got_info *) bfd_alloc (abfd,
6964 sizeof (struct mips_got_info));
6967 g->global_gotsym = NULL;
6968 g->local_gotno = MIPS_RESERVED_GOTNO;
6969 g->assigned_gotno = MIPS_RESERVED_GOTNO;
6970 if (elf_section_data (s) == NULL)
6973 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
6974 if (elf_section_data (s) == NULL)
6977 elf_section_data (s)->tdata = (PTR) g;
6978 elf_section_data (s)->this_hdr.sh_flags
6979 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6984 /* Returns the .msym section for ABFD, creating it if it does not
6985 already exist. Returns NULL to indicate error. */
6988 mips_elf_create_msym_section (abfd)
6993 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6996 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6998 || !bfd_set_section_flags (abfd, s,
7002 | SEC_LINKER_CREATED
7004 || !bfd_set_section_alignment (abfd, s,
7005 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7012 /* Add room for N relocations to the .rel.dyn section in ABFD. */
7015 mips_elf_allocate_dynamic_relocations (abfd, n)
7021 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
7022 BFD_ASSERT (s != NULL);
7024 if (s->_raw_size == 0)
7026 /* Make room for a null element. */
7027 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
7030 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
7033 /* Look through the relocs for a section during the first phase, and
7034 allocate space in the global offset table. */
7037 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
7039 struct bfd_link_info *info;
7041 const Elf_Internal_Rela *relocs;
7045 Elf_Internal_Shdr *symtab_hdr;
7046 struct elf_link_hash_entry **sym_hashes;
7047 struct mips_got_info *g;
7049 const Elf_Internal_Rela *rel;
7050 const Elf_Internal_Rela *rel_end;
7053 struct elf_backend_data *bed;
7055 if (info->relocateable)
7058 dynobj = elf_hash_table (info)->dynobj;
7059 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7060 sym_hashes = elf_sym_hashes (abfd);
7061 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7063 /* Check for the mips16 stub sections. */
7065 name = bfd_get_section_name (abfd, sec);
7066 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
7068 unsigned long r_symndx;
7070 /* Look at the relocation information to figure out which symbol
7073 r_symndx = ELF32_R_SYM (relocs->r_info);
7075 if (r_symndx < extsymoff
7076 || sym_hashes[r_symndx - extsymoff] == NULL)
7080 /* This stub is for a local symbol. This stub will only be
7081 needed if there is some relocation in this BFD, other
7082 than a 16 bit function call, which refers to this symbol. */
7083 for (o = abfd->sections; o != NULL; o = o->next)
7085 Elf_Internal_Rela *sec_relocs;
7086 const Elf_Internal_Rela *r, *rend;
7088 /* We can ignore stub sections when looking for relocs. */
7089 if ((o->flags & SEC_RELOC) == 0
7090 || o->reloc_count == 0
7091 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
7092 sizeof FN_STUB - 1) == 0
7093 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
7094 sizeof CALL_STUB - 1) == 0
7095 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
7096 sizeof CALL_FP_STUB - 1) == 0)
7099 sec_relocs = (_bfd_elf32_link_read_relocs
7100 (abfd, o, (PTR) NULL,
7101 (Elf_Internal_Rela *) NULL,
7102 info->keep_memory));
7103 if (sec_relocs == NULL)
7106 rend = sec_relocs + o->reloc_count;
7107 for (r = sec_relocs; r < rend; r++)
7108 if (ELF32_R_SYM (r->r_info) == r_symndx
7109 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
7112 if (! info->keep_memory)
7121 /* There is no non-call reloc for this stub, so we do
7122 not need it. Since this function is called before
7123 the linker maps input sections to output sections, we
7124 can easily discard it by setting the SEC_EXCLUDE
7126 sec->flags |= SEC_EXCLUDE;
7130 /* Record this stub in an array of local symbol stubs for
7132 if (elf_tdata (abfd)->local_stubs == NULL)
7134 unsigned long symcount;
7137 if (elf_bad_symtab (abfd))
7138 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
7140 symcount = symtab_hdr->sh_info;
7141 n = (asection **) bfd_zalloc (abfd,
7142 symcount * sizeof (asection *));
7145 elf_tdata (abfd)->local_stubs = n;
7148 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7150 /* We don't need to set mips16_stubs_seen in this case.
7151 That flag is used to see whether we need to look through
7152 the global symbol table for stubs. We don't need to set
7153 it here, because we just have a local stub. */
7157 struct mips_elf_link_hash_entry *h;
7159 h = ((struct mips_elf_link_hash_entry *)
7160 sym_hashes[r_symndx - extsymoff]);
7162 /* H is the symbol this stub is for. */
7165 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7168 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
7169 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7171 unsigned long r_symndx;
7172 struct mips_elf_link_hash_entry *h;
7175 /* Look at the relocation information to figure out which symbol
7178 r_symndx = ELF32_R_SYM (relocs->r_info);
7180 if (r_symndx < extsymoff
7181 || sym_hashes[r_symndx - extsymoff] == NULL)
7183 /* This stub was actually built for a static symbol defined
7184 in the same file. We assume that all static symbols in
7185 mips16 code are themselves mips16, so we can simply
7186 discard this stub. Since this function is called before
7187 the linker maps input sections to output sections, we can
7188 easily discard it by setting the SEC_EXCLUDE flag. */
7189 sec->flags |= SEC_EXCLUDE;
7193 h = ((struct mips_elf_link_hash_entry *)
7194 sym_hashes[r_symndx - extsymoff]);
7196 /* H is the symbol this stub is for. */
7198 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7199 loc = &h->call_fp_stub;
7201 loc = &h->call_stub;
7203 /* If we already have an appropriate stub for this function, we
7204 don't need another one, so we can discard this one. Since
7205 this function is called before the linker maps input sections
7206 to output sections, we can easily discard it by setting the
7207 SEC_EXCLUDE flag. We can also discard this section if we
7208 happen to already know that this is a mips16 function; it is
7209 not necessary to check this here, as it is checked later, but
7210 it is slightly faster to check now. */
7211 if (*loc != NULL || h->root.other == STO_MIPS16)
7213 sec->flags |= SEC_EXCLUDE;
7218 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7228 sgot = mips_elf_got_section (dynobj);
7233 BFD_ASSERT (elf_section_data (sgot) != NULL);
7234 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7235 BFD_ASSERT (g != NULL);
7240 bed = get_elf_backend_data (abfd);
7241 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7242 for (rel = relocs; rel < rel_end; ++rel)
7244 unsigned long r_symndx;
7246 struct elf_link_hash_entry *h;
7248 r_symndx = ELF32_R_SYM (rel->r_info);
7249 r_type = ELF32_R_TYPE (rel->r_info);
7251 if (r_symndx < extsymoff)
7255 h = sym_hashes[r_symndx - extsymoff];
7257 /* This may be an indirect symbol created because of a version. */
7260 while (h->root.type == bfd_link_hash_indirect)
7261 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7265 /* Some relocs require a global offset table. */
7266 if (dynobj == NULL || sgot == NULL)
7272 case R_MIPS_CALL_HI16:
7273 case R_MIPS_CALL_LO16:
7274 case R_MIPS_GOT_HI16:
7275 case R_MIPS_GOT_LO16:
7276 case R_MIPS_GOT_PAGE:
7277 case R_MIPS_GOT_OFST:
7278 case R_MIPS_GOT_DISP:
7280 elf_hash_table (info)->dynobj = dynobj = abfd;
7281 if (! mips_elf_create_got_section (dynobj, info))
7283 g = mips_elf_got_info (dynobj, &sgot);
7290 && (info->shared || h != NULL)
7291 && (sec->flags & SEC_ALLOC) != 0)
7292 elf_hash_table (info)->dynobj = dynobj = abfd;
7300 if (!h && (r_type == R_MIPS_CALL_LO16
7301 || r_type == R_MIPS_GOT_LO16
7302 || r_type == R_MIPS_GOT_DISP))
7304 /* We may need a local GOT entry for this relocation. We
7305 don't count R_MIPS_HI16 or R_MIPS_GOT16 relocations
7306 because they are always followed by a R_MIPS_LO16
7307 relocation for the value. We don't R_MIPS_GOT_PAGE
7308 because we can estimate the maximum number of pages
7309 needed by looking at the size of the segment.
7311 This estimation is very conservative since we can merge
7312 duplicate entries in the GOT. In order to be less
7313 conservative, we could actually build the GOT here,
7314 rather than in relocate_section. */
7316 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7324 (*_bfd_error_handler)
7325 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
7326 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
7327 bfd_set_error (bfd_error_bad_value);
7332 case R_MIPS_CALL_HI16:
7333 case R_MIPS_CALL_LO16:
7334 /* This symbol requires a global offset table entry. */
7335 if (!mips_elf_record_global_got_symbol (h, info, g))
7338 /* We need a stub, not a plt entry for the undefined
7339 function. But we record it as if it needs plt. See
7340 elf_adjust_dynamic_symbol in elflink.h. */
7341 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
7347 case R_MIPS_GOT_HI16:
7348 case R_MIPS_GOT_LO16:
7349 case R_MIPS_GOT_DISP:
7350 /* This symbol requires a global offset table entry. */
7351 if (h && !mips_elf_record_global_got_symbol (h, info, g))
7358 if ((info->shared || h != NULL)
7359 && (sec->flags & SEC_ALLOC) != 0)
7363 const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
7365 sreloc = bfd_get_section_by_name (dynobj, name);
7368 sreloc = bfd_make_section (dynobj, name);
7370 || ! bfd_set_section_flags (dynobj, sreloc,
7375 | SEC_LINKER_CREATED
7377 || ! bfd_set_section_alignment (dynobj, sreloc,
7383 /* When creating a shared object, we must copy these
7384 reloc types into the output file as R_MIPS_REL32
7385 relocs. We make room for this reloc in the
7386 .rel.dyn reloc section. */
7387 mips_elf_allocate_dynamic_relocations (dynobj, 1);
7390 struct mips_elf_link_hash_entry *hmips;
7392 /* We only need to copy this reloc if the symbol is
7393 defined in a dynamic object. */
7394 hmips = (struct mips_elf_link_hash_entry *) h;
7395 ++hmips->possibly_dynamic_relocs;
7398 /* Even though we don't directly need a GOT entry for
7399 this symbol, a symbol must have a dynamic symbol
7400 table index greater that DT_GOTSYM if there are
7401 dynamic relocations against it. */
7402 if (!mips_elf_record_global_got_symbol (h, info, g))
7406 if (SGI_COMPAT (dynobj))
7407 mips_elf_hash_table (info)->compact_rel_size +=
7408 sizeof (Elf32_External_crinfo);
7412 case R_MIPS_GPREL16:
7413 case R_MIPS_LITERAL:
7414 case R_MIPS_GPREL32:
7415 if (SGI_COMPAT (dynobj))
7416 mips_elf_hash_table (info)->compact_rel_size +=
7417 sizeof (Elf32_External_crinfo);
7420 /* This relocation describes the C++ object vtable hierarchy.
7421 Reconstruct it for later use during GC. */
7422 case R_MIPS_GNU_VTINHERIT:
7423 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7427 /* This relocation describes which C++ vtable entries are actually
7428 used. Record for later use during GC. */
7429 case R_MIPS_GNU_VTENTRY:
7430 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7438 /* If this reloc is not a 16 bit call, and it has a global
7439 symbol, then we will need the fn_stub if there is one.
7440 References from a stub section do not count. */
7442 && r_type != R_MIPS16_26
7443 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7444 sizeof FN_STUB - 1) != 0
7445 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7446 sizeof CALL_STUB - 1) != 0
7447 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7448 sizeof CALL_FP_STUB - 1) != 0)
7450 struct mips_elf_link_hash_entry *mh;
7452 mh = (struct mips_elf_link_hash_entry *) h;
7453 mh->need_fn_stub = true;
7460 /* Return the section that should be marked against GC for a given
7464 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7466 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7467 Elf_Internal_Rela *rel;
7468 struct elf_link_hash_entry *h;
7469 Elf_Internal_Sym *sym;
7471 /* ??? Do mips16 stub sections need to be handled special? */
7475 switch (ELF32_R_TYPE (rel->r_info))
7477 case R_MIPS_GNU_VTINHERIT:
7478 case R_MIPS_GNU_VTENTRY:
7482 switch (h->root.type)
7484 case bfd_link_hash_defined:
7485 case bfd_link_hash_defweak:
7486 return h->root.u.def.section;
7488 case bfd_link_hash_common:
7489 return h->root.u.c.p->section;
7498 if (!(elf_bad_symtab (abfd)
7499 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7500 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7501 && sym->st_shndx != SHN_COMMON))
7503 return bfd_section_from_elf_index (abfd, sym->st_shndx);
7510 /* Update the got entry reference counts for the section being removed. */
7513 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7514 bfd *abfd ATTRIBUTE_UNUSED;
7515 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7516 asection *sec ATTRIBUTE_UNUSED;
7517 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
7520 Elf_Internal_Shdr *symtab_hdr;
7521 struct elf_link_hash_entry **sym_hashes;
7522 bfd_signed_vma *local_got_refcounts;
7523 const Elf_Internal_Rela *rel, *relend;
7524 unsigned long r_symndx;
7525 struct elf_link_hash_entry *h;
7527 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7528 sym_hashes = elf_sym_hashes (abfd);
7529 local_got_refcounts = elf_local_got_refcounts (abfd);
7531 relend = relocs + sec->reloc_count;
7532 for (rel = relocs; rel < relend; rel++)
7533 switch (ELF32_R_TYPE (rel->r_info))
7537 case R_MIPS_CALL_HI16:
7538 case R_MIPS_CALL_LO16:
7539 case R_MIPS_GOT_HI16:
7540 case R_MIPS_GOT_LO16:
7541 /* ??? It would seem that the existing MIPS code does no sort
7542 of reference counting or whatnot on its GOT and PLT entries,
7543 so it is not possible to garbage collect them at this time. */
7555 /* Adjust a symbol defined by a dynamic object and referenced by a
7556 regular object. The current definition is in some section of the
7557 dynamic object, but we're not including those sections. We have to
7558 change the definition to something the rest of the link can
7562 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
7563 struct bfd_link_info *info;
7564 struct elf_link_hash_entry *h;
7567 struct mips_elf_link_hash_entry *hmips;
7570 dynobj = elf_hash_table (info)->dynobj;
7572 /* Make sure we know what is going on here. */
7573 BFD_ASSERT (dynobj != NULL
7574 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7575 || h->weakdef != NULL
7576 || ((h->elf_link_hash_flags
7577 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7578 && (h->elf_link_hash_flags
7579 & ELF_LINK_HASH_REF_REGULAR) != 0
7580 && (h->elf_link_hash_flags
7581 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7583 /* If this symbol is defined in a dynamic object, we need to copy
7584 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7586 hmips = (struct mips_elf_link_hash_entry *) h;
7587 if (! info->relocateable
7588 && hmips->possibly_dynamic_relocs != 0
7589 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7590 mips_elf_allocate_dynamic_relocations (dynobj,
7591 hmips->possibly_dynamic_relocs);
7593 /* For a function, create a stub, if needed. */
7594 if (h->type == STT_FUNC
7595 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7597 if (! elf_hash_table (info)->dynamic_sections_created)
7600 /* If this symbol is not defined in a regular file, then set
7601 the symbol to the stub location. This is required to make
7602 function pointers compare as equal between the normal
7603 executable and the shared library. */
7604 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7606 /* We need .stub section. */
7607 s = bfd_get_section_by_name (dynobj,
7608 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7609 BFD_ASSERT (s != NULL);
7611 h->root.u.def.section = s;
7612 h->root.u.def.value = s->_raw_size;
7614 /* XXX Write this stub address somewhere. */
7615 h->plt.offset = s->_raw_size;
7617 /* Make room for this stub code. */
7618 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7620 /* The last half word of the stub will be filled with the index
7621 of this symbol in .dynsym section. */
7626 /* If this is a weak symbol, and there is a real definition, the
7627 processor independent code will have arranged for us to see the
7628 real definition first, and we can just use the same value. */
7629 if (h->weakdef != NULL)
7631 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7632 || h->weakdef->root.type == bfd_link_hash_defweak);
7633 h->root.u.def.section = h->weakdef->root.u.def.section;
7634 h->root.u.def.value = h->weakdef->root.u.def.value;
7638 /* This is a reference to a symbol defined by a dynamic object which
7639 is not a function. */
7644 /* This function is called after all the input files have been read,
7645 and the input sections have been assigned to output sections. We
7646 check for any mips16 stub sections that we can discard. */
7648 static boolean mips_elf_check_mips16_stubs
7649 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7652 _bfd_mips_elf_always_size_sections (output_bfd, info)
7654 struct bfd_link_info *info;
7658 /* The .reginfo section has a fixed size. */
7659 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7661 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7663 if (info->relocateable
7664 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7667 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7668 mips_elf_check_mips16_stubs,
7674 /* Check the mips16 stubs for a particular symbol, and see if we can
7679 mips_elf_check_mips16_stubs (h, data)
7680 struct mips_elf_link_hash_entry *h;
7681 PTR data ATTRIBUTE_UNUSED;
7683 if (h->fn_stub != NULL
7684 && ! h->need_fn_stub)
7686 /* We don't need the fn_stub; the only references to this symbol
7687 are 16 bit calls. Clobber the size to 0 to prevent it from
7688 being included in the link. */
7689 h->fn_stub->_raw_size = 0;
7690 h->fn_stub->_cooked_size = 0;
7691 h->fn_stub->flags &= ~ SEC_RELOC;
7692 h->fn_stub->reloc_count = 0;
7693 h->fn_stub->flags |= SEC_EXCLUDE;
7696 if (h->call_stub != NULL
7697 && h->root.other == STO_MIPS16)
7699 /* We don't need the call_stub; this is a 16 bit function, so
7700 calls from other 16 bit functions are OK. Clobber the size
7701 to 0 to prevent it from being included in the link. */
7702 h->call_stub->_raw_size = 0;
7703 h->call_stub->_cooked_size = 0;
7704 h->call_stub->flags &= ~ SEC_RELOC;
7705 h->call_stub->reloc_count = 0;
7706 h->call_stub->flags |= SEC_EXCLUDE;
7709 if (h->call_fp_stub != NULL
7710 && h->root.other == STO_MIPS16)
7712 /* We don't need the call_stub; this is a 16 bit function, so
7713 calls from other 16 bit functions are OK. Clobber the size
7714 to 0 to prevent it from being included in the link. */
7715 h->call_fp_stub->_raw_size = 0;
7716 h->call_fp_stub->_cooked_size = 0;
7717 h->call_fp_stub->flags &= ~ SEC_RELOC;
7718 h->call_fp_stub->reloc_count = 0;
7719 h->call_fp_stub->flags |= SEC_EXCLUDE;
7725 /* Set the sizes of the dynamic sections. */
7728 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
7730 struct bfd_link_info *info;
7735 struct mips_got_info *g;
7737 dynobj = elf_hash_table (info)->dynobj;
7738 BFD_ASSERT (dynobj != NULL);
7740 if (elf_hash_table (info)->dynamic_sections_created)
7742 /* Set the contents of the .interp section to the interpreter. */
7745 s = bfd_get_section_by_name (dynobj, ".interp");
7746 BFD_ASSERT (s != NULL);
7748 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7750 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7754 /* The check_relocs and adjust_dynamic_symbol entry points have
7755 determined the sizes of the various dynamic sections. Allocate
7758 for (s = dynobj->sections; s != NULL; s = s->next)
7763 /* It's OK to base decisions on the section name, because none
7764 of the dynobj section names depend upon the input files. */
7765 name = bfd_get_section_name (dynobj, s);
7767 if ((s->flags & SEC_LINKER_CREATED) == 0)
7772 if (strncmp (name, ".rel", 4) == 0)
7774 if (s->_raw_size == 0)
7776 /* We only strip the section if the output section name
7777 has the same name. Otherwise, there might be several
7778 input sections for this output section. FIXME: This
7779 code is probably not needed these days anyhow, since
7780 the linker now does not create empty output sections. */
7781 if (s->output_section != NULL
7783 bfd_get_section_name (s->output_section->owner,
7784 s->output_section)) == 0)
7789 const char *outname;
7792 /* If this relocation section applies to a read only
7793 section, then we probably need a DT_TEXTREL entry.
7794 If the relocation section is .rel.dyn, we always
7795 assert a DT_TEXTREL entry rather than testing whether
7796 there exists a relocation to a read only section or
7798 outname = bfd_get_section_name (output_bfd,
7800 target = bfd_get_section_by_name (output_bfd, outname + 4);
7802 && (target->flags & SEC_READONLY) != 0
7803 && (target->flags & SEC_ALLOC) != 0)
7805 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
7808 /* We use the reloc_count field as a counter if we need
7809 to copy relocs into the output file. */
7811 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
7815 else if (strncmp (name, ".got", 4) == 0)
7818 bfd_size_type loadable_size = 0;
7819 bfd_size_type local_gotno;
7822 BFD_ASSERT (elf_section_data (s) != NULL);
7823 g = (struct mips_got_info *) elf_section_data (s)->tdata;
7824 BFD_ASSERT (g != NULL);
7826 /* Calculate the total loadable size of the output. That
7827 will give us the maximum number of GOT_PAGE entries
7829 for (sub = info->input_bfds; sub; sub = sub->link_next)
7831 asection *subsection;
7833 for (subsection = sub->sections;
7835 subsection = subsection->next)
7837 if ((subsection->flags & SEC_ALLOC) == 0)
7839 loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
7842 loadable_size += MIPS_FUNCTION_STUB_SIZE;
7844 /* Assume there are two loadable segments consisting of
7845 contiguous sections. Is 5 enough? */
7846 local_gotno = (loadable_size >> 16) + 5;
7847 g->local_gotno += local_gotno;
7848 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
7850 /* There has to be a global GOT entry for every symbol with
7851 a dynamic symbol table index of DT_MIPS_GOTSYM or
7852 higher. Therefore, it make sense to put those symbols
7853 that need GOT entries at the end of the symbol table. We
7855 if (!mips_elf_sort_hash_table (info, 1))
7858 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7859 g->global_gotno = i;
7860 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
7862 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
7864 /* Irix rld assumes that the function stub isn't at the end
7865 of .text section. So put a dummy. XXX */
7866 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7868 else if (! info->shared
7869 && ! mips_elf_hash_table (info)->use_rld_obj_head
7870 && strncmp (name, ".rld_map", 8) == 0)
7872 /* We add a room for __rld_map. It will be filled in by the
7873 rtld to contain a pointer to the _r_debug structure. */
7876 else if (SGI_COMPAT (output_bfd)
7877 && strncmp (name, ".compact_rel", 12) == 0)
7878 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
7879 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
7881 s->_raw_size = (sizeof (Elf32_External_Msym)
7882 * (elf_hash_table (info)->dynsymcount
7883 + bfd_count_sections (output_bfd)));
7884 else if (strncmp (name, ".init", 5) != 0)
7886 /* It's not one of our sections, so don't allocate space. */
7892 _bfd_strip_section_from_output (s);
7896 /* Allocate memory for the section contents. */
7897 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
7898 if (s->contents == NULL && s->_raw_size != 0)
7900 bfd_set_error (bfd_error_no_memory);
7905 if (elf_hash_table (info)->dynamic_sections_created)
7907 /* Add some entries to the .dynamic section. We fill in the
7908 values later, in elf_mips_finish_dynamic_sections, but we
7909 must add the entries now so that we get the correct size for
7910 the .dynamic section. The DT_DEBUG entry is filled in by the
7911 dynamic linker and used by the debugger. */
7914 if (SGI_COMPAT (output_bfd))
7916 /* SGI object has the equivalence of DT_DEBUG in the
7917 DT_MIPS_RLD_MAP entry. */
7918 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
7922 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
7928 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
7932 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
7935 if (bfd_get_section_by_name (dynobj,
7936 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
7938 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
7941 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
7944 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
7948 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
7951 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
7954 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
7956 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
7959 s = bfd_get_section_by_name (dynobj, ".liblist");
7960 BFD_ASSERT (s != NULL);
7962 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
7966 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
7969 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
7973 /* Time stamps in executable files are a bad idea. */
7974 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
7979 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
7984 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
7988 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
7991 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
7994 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
7997 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8000 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8003 if (IRIX_COMPAT (dynobj) == ict_irix5
8004 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8007 if (IRIX_COMPAT (dynobj) == ict_irix6
8008 && (bfd_get_section_by_name
8009 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8010 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8013 if (bfd_get_section_by_name (dynobj,
8014 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
8015 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
8022 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8023 adjust it appropriately now. */
8026 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
8027 bfd *abfd ATTRIBUTE_UNUSED;
8029 Elf_Internal_Sym *sym;
8031 /* The linker script takes care of providing names and values for
8032 these, but we must place them into the right sections. */
8033 static const char* const text_section_symbols[] = {
8036 "__dso_displacement",
8038 "__program_header_table",
8042 static const char* const data_section_symbols[] = {
8050 const char* const *p;
8053 for (i = 0; i < 2; ++i)
8054 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8057 if (strcmp (*p, name) == 0)
8059 /* All of these symbols are given type STT_SECTION by the
8061 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8063 /* The IRIX linker puts these symbols in special sections. */
8065 sym->st_shndx = SHN_MIPS_TEXT;
8067 sym->st_shndx = SHN_MIPS_DATA;
8073 /* Finish up dynamic symbol handling. We set the contents of various
8074 dynamic sections here. */
8077 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8079 struct bfd_link_info *info;
8080 struct elf_link_hash_entry *h;
8081 Elf_Internal_Sym *sym;
8087 struct mips_got_info *g;
8089 struct mips_elf_link_hash_entry *mh;
8091 dynobj = elf_hash_table (info)->dynobj;
8092 gval = sym->st_value;
8093 mh = (struct mips_elf_link_hash_entry *) h;
8095 if (h->plt.offset != (bfd_vma) -1)
8099 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
8101 /* This symbol has a stub. Set it up. */
8103 BFD_ASSERT (h->dynindx != -1);
8105 s = bfd_get_section_by_name (dynobj,
8106 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8107 BFD_ASSERT (s != NULL);
8109 /* Fill the stub. */
8111 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
8113 bfd_put_32 (output_bfd, STUB_MOVE, p);
8116 /* FIXME: Can h->dynindex be more than 64K? */
8117 if (h->dynindx & 0xffff0000)
8120 bfd_put_32 (output_bfd, STUB_JALR, p);
8122 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
8124 BFD_ASSERT (h->plt.offset <= s->_raw_size);
8125 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
8127 /* Mark the symbol as undefined. plt.offset != -1 occurs
8128 only for the referenced symbol. */
8129 sym->st_shndx = SHN_UNDEF;
8131 /* The run-time linker uses the st_value field of the symbol
8132 to reset the global offset table entry for this external
8133 to its stub address when unlinking a shared object. */
8134 gval = s->output_section->vma + s->output_offset + h->plt.offset;
8135 sym->st_value = gval;
8138 BFD_ASSERT (h->dynindx != -1);
8140 sgot = mips_elf_got_section (dynobj);
8141 BFD_ASSERT (sgot != NULL);
8142 BFD_ASSERT (elf_section_data (sgot) != NULL);
8143 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8144 BFD_ASSERT (g != NULL);
8146 /* Run through the global symbol table, creating GOT entries for all
8147 the symbols that need them. */
8148 if (h->dynindx >= g->global_gotsym->dynindx)
8154 value = sym->st_value;
8156 /* For an entity defined in a shared object, this will be
8157 NULL. (For functions in shared objects for
8158 which we have created stubs, ST_VALUE will be non-NULL.
8159 That's because such the functions are now no longer defined
8160 in a shared object.) */
8161 value = h->root.u.def.value;
8163 offset = mips_elf_global_got_index (dynobj, h);
8164 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8167 /* Create a .msym entry, if appropriate. */
8168 smsym = bfd_get_section_by_name (dynobj,
8169 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8172 Elf32_Internal_Msym msym;
8174 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
8175 /* It is undocumented what the `1' indicates, but IRIX6 uses
8177 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
8178 bfd_mips_elf_swap_msym_out
8180 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
8183 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8184 name = h->root.root.string;
8185 if (strcmp (name, "_DYNAMIC") == 0
8186 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
8187 sym->st_shndx = SHN_ABS;
8188 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
8190 sym->st_shndx = SHN_ABS;
8191 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8194 else if (SGI_COMPAT (output_bfd))
8196 if (strcmp (name, "_gp_disp") == 0)
8198 sym->st_shndx = SHN_ABS;
8199 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8200 sym->st_value = elf_gp (output_bfd);
8202 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8203 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8205 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8206 sym->st_other = STO_PROTECTED;
8208 sym->st_shndx = SHN_MIPS_DATA;
8210 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8212 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8213 sym->st_other = STO_PROTECTED;
8214 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8215 sym->st_shndx = SHN_ABS;
8217 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8219 if (h->type == STT_FUNC)
8220 sym->st_shndx = SHN_MIPS_TEXT;
8221 else if (h->type == STT_OBJECT)
8222 sym->st_shndx = SHN_MIPS_DATA;
8226 /* Handle the IRIX6-specific symbols. */
8227 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8228 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8230 if (SGI_COMPAT (output_bfd)
8233 if (! mips_elf_hash_table (info)->use_rld_obj_head
8234 && strcmp (name, "__rld_map") == 0)
8236 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8237 BFD_ASSERT (s != NULL);
8238 sym->st_value = s->output_section->vma + s->output_offset;
8239 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
8240 if (mips_elf_hash_table (info)->rld_value == 0)
8241 mips_elf_hash_table (info)->rld_value = sym->st_value;
8243 else if (mips_elf_hash_table (info)->use_rld_obj_head
8244 && strcmp (name, "__rld_obj_head") == 0)
8246 /* IRIX6 does not use a .rld_map section. */
8247 if (IRIX_COMPAT (output_bfd) == ict_irix5)
8248 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8250 mips_elf_hash_table (info)->rld_value = sym->st_value;
8254 /* If this is a mips16 symbol, force the value to be even. */
8255 if (sym->st_other == STO_MIPS16
8256 && (sym->st_value & 1) != 0)
8262 /* Finish up the dynamic sections. */
8265 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
8267 struct bfd_link_info *info;
8272 struct mips_got_info *g;
8274 dynobj = elf_hash_table (info)->dynobj;
8276 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8278 sgot = mips_elf_got_section (dynobj);
8283 BFD_ASSERT (elf_section_data (sgot) != NULL);
8284 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8285 BFD_ASSERT (g != NULL);
8288 if (elf_hash_table (info)->dynamic_sections_created)
8292 BFD_ASSERT (sdyn != NULL);
8293 BFD_ASSERT (g != NULL);
8295 for (b = sdyn->contents;
8296 b < sdyn->contents + sdyn->_raw_size;
8297 b += MIPS_ELF_DYN_SIZE (dynobj))
8299 Elf_Internal_Dyn dyn;
8305 /* Read in the current dynamic entry. */
8306 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8308 /* Assume that we're going to modify it and write it out. */
8314 s = (bfd_get_section_by_name
8316 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
8317 BFD_ASSERT (s != NULL);
8318 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8322 /* Rewrite DT_STRSZ. */
8324 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
8330 case DT_MIPS_CONFLICT:
8333 case DT_MIPS_LIBLIST:
8336 s = bfd_get_section_by_name (output_bfd, name);
8337 BFD_ASSERT (s != NULL);
8338 dyn.d_un.d_ptr = s->vma;
8341 case DT_MIPS_RLD_VERSION:
8342 dyn.d_un.d_val = 1; /* XXX */
8346 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8349 case DT_MIPS_CONFLICTNO:
8351 elemsize = sizeof (Elf32_Conflict);
8354 case DT_MIPS_LIBLISTNO:
8356 elemsize = sizeof (Elf32_Lib);
8358 s = bfd_get_section_by_name (output_bfd, name);
8361 if (s->_cooked_size != 0)
8362 dyn.d_un.d_val = s->_cooked_size / elemsize;
8364 dyn.d_un.d_val = s->_raw_size / elemsize;
8370 case DT_MIPS_TIME_STAMP:
8371 time ((time_t *) &dyn.d_un.d_val);
8374 case DT_MIPS_ICHECKSUM:
8379 case DT_MIPS_IVERSION:
8384 case DT_MIPS_BASE_ADDRESS:
8385 s = output_bfd->sections;
8386 BFD_ASSERT (s != NULL);
8387 dyn.d_un.d_ptr = s->vma & ~(0xffff);
8390 case DT_MIPS_LOCAL_GOTNO:
8391 dyn.d_un.d_val = g->local_gotno;
8394 case DT_MIPS_SYMTABNO:
8396 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8397 s = bfd_get_section_by_name (output_bfd, name);
8398 BFD_ASSERT (s != NULL);
8400 if (s->_cooked_size != 0)
8401 dyn.d_un.d_val = s->_cooked_size / elemsize;
8403 dyn.d_un.d_val = s->_raw_size / elemsize;
8406 case DT_MIPS_UNREFEXTNO:
8407 /* The index into the dynamic symbol table which is the
8408 entry of the first external symbol that is not
8409 referenced within the same object. */
8410 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8413 case DT_MIPS_GOTSYM:
8414 dyn.d_un.d_val = g->global_gotsym->dynindx;
8417 case DT_MIPS_HIPAGENO:
8418 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8421 case DT_MIPS_RLD_MAP:
8422 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8425 case DT_MIPS_OPTIONS:
8426 s = (bfd_get_section_by_name
8427 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8428 dyn.d_un.d_ptr = s->vma;
8432 s = (bfd_get_section_by_name
8433 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8434 dyn.d_un.d_ptr = s->vma;
8443 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8448 /* The first entry of the global offset table will be filled at
8449 runtime. The second entry will be used by some runtime loaders.
8450 This isn't the case of Irix rld. */
8451 if (sgot != NULL && sgot->_raw_size > 0)
8453 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8454 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8455 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8459 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8460 = MIPS_ELF_GOT_SIZE (output_bfd);
8465 Elf32_compact_rel cpt;
8467 /* ??? The section symbols for the output sections were set up in
8468 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
8469 symbols. Should we do so? */
8471 smsym = bfd_get_section_by_name (dynobj,
8472 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8475 Elf32_Internal_Msym msym;
8477 msym.ms_hash_value = 0;
8478 msym.ms_info = ELF32_MS_INFO (0, 1);
8480 for (s = output_bfd->sections; s != NULL; s = s->next)
8482 long dynindx = elf_section_data (s)->dynindx;
8484 bfd_mips_elf_swap_msym_out
8486 (((Elf32_External_Msym *) smsym->contents)
8491 if (SGI_COMPAT (output_bfd))
8493 /* Write .compact_rel section out. */
8494 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8498 cpt.num = s->reloc_count;
8500 cpt.offset = (s->output_section->filepos
8501 + sizeof (Elf32_External_compact_rel));
8504 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8505 ((Elf32_External_compact_rel *)
8508 /* Clean up a dummy stub function entry in .text. */
8509 s = bfd_get_section_by_name (dynobj,
8510 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8513 file_ptr dummy_offset;
8515 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8516 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8517 memset (s->contents + dummy_offset, 0,
8518 MIPS_FUNCTION_STUB_SIZE);
8523 /* Clean up a first relocation in .rel.dyn. */
8524 s = bfd_get_section_by_name (dynobj,
8525 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
8526 if (s != NULL && s->_raw_size > 0)
8527 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
8533 /* This is almost identical to bfd_generic_get_... except that some
8534 MIPS relocations need to be handled specially. Sigh. */
8537 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8538 relocateable, symbols)
8540 struct bfd_link_info *link_info;
8541 struct bfd_link_order *link_order;
8543 boolean relocateable;
8546 /* Get enough memory to hold the stuff */
8547 bfd *input_bfd = link_order->u.indirect.section->owner;
8548 asection *input_section = link_order->u.indirect.section;
8550 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8551 arelent **reloc_vector = NULL;
8557 reloc_vector = (arelent **) bfd_malloc (reloc_size);
8558 if (reloc_vector == NULL && reloc_size != 0)
8561 /* read in the section */
8562 if (!bfd_get_section_contents (input_bfd,
8566 input_section->_raw_size))
8569 /* We're not relaxing the section, so just copy the size info */
8570 input_section->_cooked_size = input_section->_raw_size;
8571 input_section->reloc_done = true;
8573 reloc_count = bfd_canonicalize_reloc (input_bfd,
8577 if (reloc_count < 0)
8580 if (reloc_count > 0)
8585 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8588 struct bfd_hash_entry *h;
8589 struct bfd_link_hash_entry *lh;
8590 /* Skip all this stuff if we aren't mixing formats. */
8591 if (abfd && input_bfd
8592 && abfd->xvec == input_bfd->xvec)
8596 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8597 lh = (struct bfd_link_hash_entry *) h;
8604 case bfd_link_hash_undefined:
8605 case bfd_link_hash_undefweak:
8606 case bfd_link_hash_common:
8609 case bfd_link_hash_defined:
8610 case bfd_link_hash_defweak:
8612 gp = lh->u.def.value;
8614 case bfd_link_hash_indirect:
8615 case bfd_link_hash_warning:
8617 /* @@FIXME ignoring warning for now */
8619 case bfd_link_hash_new:
8628 for (parent = reloc_vector; *parent != (arelent *) NULL;
8631 char *error_message = (char *) NULL;
8632 bfd_reloc_status_type r;
8634 /* Specific to MIPS: Deal with relocation types that require
8635 knowing the gp of the output bfd. */
8636 asymbol *sym = *(*parent)->sym_ptr_ptr;
8637 if (bfd_is_abs_section (sym->section) && abfd)
8639 /* The special_function wouldn't get called anyways. */
8643 /* The gp isn't there; let the special function code
8644 fall over on its own. */
8646 else if ((*parent)->howto->special_function
8647 == _bfd_mips_elf_gprel16_reloc)
8649 /* bypass special_function call */
8650 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8651 relocateable, (PTR) data, gp);
8652 goto skip_bfd_perform_relocation;
8654 /* end mips specific stuff */
8656 r = bfd_perform_relocation (input_bfd,
8660 relocateable ? abfd : (bfd *) NULL,
8662 skip_bfd_perform_relocation:
8666 asection *os = input_section->output_section;
8668 /* A partial link, so keep the relocs */
8669 os->orelocation[os->reloc_count] = *parent;
8673 if (r != bfd_reloc_ok)
8677 case bfd_reloc_undefined:
8678 if (!((*link_info->callbacks->undefined_symbol)
8679 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8680 input_bfd, input_section, (*parent)->address)))
8683 case bfd_reloc_dangerous:
8684 BFD_ASSERT (error_message != (char *) NULL);
8685 if (!((*link_info->callbacks->reloc_dangerous)
8686 (link_info, error_message, input_bfd, input_section,
8687 (*parent)->address)))
8690 case bfd_reloc_overflow:
8691 if (!((*link_info->callbacks->reloc_overflow)
8692 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8693 (*parent)->howto->name, (*parent)->addend,
8694 input_bfd, input_section, (*parent)->address)))
8697 case bfd_reloc_outofrange:
8706 if (reloc_vector != NULL)
8707 free (reloc_vector);
8711 if (reloc_vector != NULL)
8712 free (reloc_vector);
8715 #define bfd_elf32_bfd_get_relocated_section_contents \
8716 elf32_mips_get_relocated_section_contents
8718 /* ECOFF swapping routines. These are used when dealing with the
8719 .mdebug section, which is in the ECOFF debugging format. */
8720 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8722 /* Symbol table magic number. */
8724 /* Alignment of debugging information. E.g., 4. */
8726 /* Sizes of external symbolic information. */
8727 sizeof (struct hdr_ext),
8728 sizeof (struct dnr_ext),
8729 sizeof (struct pdr_ext),
8730 sizeof (struct sym_ext),
8731 sizeof (struct opt_ext),
8732 sizeof (struct fdr_ext),
8733 sizeof (struct rfd_ext),
8734 sizeof (struct ext_ext),
8735 /* Functions to swap in external symbolic data. */
8744 _bfd_ecoff_swap_tir_in,
8745 _bfd_ecoff_swap_rndx_in,
8746 /* Functions to swap out external symbolic data. */
8755 _bfd_ecoff_swap_tir_out,
8756 _bfd_ecoff_swap_rndx_out,
8757 /* Function to read in symbolic data. */
8758 _bfd_mips_elf_read_ecoff_info
8761 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8762 #define TARGET_LITTLE_NAME "elf32-littlemips"
8763 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8764 #define TARGET_BIG_NAME "elf32-bigmips"
8765 #define ELF_ARCH bfd_arch_mips
8766 #define ELF_MACHINE_CODE EM_MIPS
8768 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8769 a value of 0x1000, and we are compatible. */
8770 #define ELF_MAXPAGESIZE 0x1000
8772 #define elf_backend_collect true
8773 #define elf_backend_type_change_ok true
8774 #define elf_backend_can_gc_sections true
8775 #define elf_info_to_howto mips_info_to_howto_rela
8776 #define elf_info_to_howto_rel mips_info_to_howto_rel
8777 #define elf_backend_sym_is_global mips_elf_sym_is_global
8778 #define elf_backend_object_p _bfd_mips_elf_object_p
8779 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
8780 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8781 #define elf_backend_section_from_bfd_section \
8782 _bfd_mips_elf_section_from_bfd_section
8783 #define elf_backend_section_processing _bfd_mips_elf_section_processing
8784 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8785 #define elf_backend_additional_program_headers \
8786 _bfd_mips_elf_additional_program_headers
8787 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
8788 #define elf_backend_final_write_processing \
8789 _bfd_mips_elf_final_write_processing
8790 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8791 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
8792 #define elf_backend_create_dynamic_sections \
8793 _bfd_mips_elf_create_dynamic_sections
8794 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
8795 #define elf_backend_adjust_dynamic_symbol \
8796 _bfd_mips_elf_adjust_dynamic_symbol
8797 #define elf_backend_always_size_sections \
8798 _bfd_mips_elf_always_size_sections
8799 #define elf_backend_size_dynamic_sections \
8800 _bfd_mips_elf_size_dynamic_sections
8801 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
8802 #define elf_backend_link_output_symbol_hook \
8803 _bfd_mips_elf_link_output_symbol_hook
8804 #define elf_backend_finish_dynamic_symbol \
8805 _bfd_mips_elf_finish_dynamic_symbol
8806 #define elf_backend_finish_dynamic_sections \
8807 _bfd_mips_elf_finish_dynamic_sections
8808 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
8809 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8811 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8812 #define elf_backend_plt_header_size 0
8814 #define bfd_elf32_bfd_is_local_label_name \
8815 mips_elf_is_local_label_name
8816 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8817 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8818 #define bfd_elf32_bfd_link_hash_table_create \
8819 _bfd_mips_elf_link_hash_table_create
8820 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
8821 #define bfd_elf32_bfd_copy_private_bfd_data \
8822 _bfd_mips_elf_copy_private_bfd_data
8823 #define bfd_elf32_bfd_merge_private_bfd_data \
8824 _bfd_mips_elf_merge_private_bfd_data
8825 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
8826 #define bfd_elf32_bfd_print_private_bfd_data \
8827 _bfd_mips_elf_print_private_bfd_data
8828 #include "elf32-target.h"