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,
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
35 /* Get the ECOFF swapping routines. */
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
42 #include "ecoffswap.h"
44 /* This structure is used to hold .got information when linking. It
45 is stored in the tdata field of the bfd_elf_section_data structure. */
49 /* The global symbol in the GOT with the lowest index in the dynamic
51 struct elf_link_hash_entry *global_gotsym;
52 /* The number of local .got entries. */
53 unsigned int local_gotno;
54 /* The number of local .got entries we have used. */
55 unsigned int assigned_gotno;
58 /* The MIPS ELF linker needs additional information for each symbol in
59 the global hash table. */
61 struct mips_elf_link_hash_entry
63 struct elf_link_hash_entry root;
65 /* External symbol information. */
68 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
69 unsigned int mips_32_relocs;
71 /* The index of the first dynamic relocation (in the .rel.dyn
72 section) against this symbol. */
73 unsigned int min_dyn_reloc_index;
75 /* If there is a stub that 32 bit functions should use to call this
76 16 bit function, this points to the section containing the stub. */
79 /* Whether we need the fn_stub; this is set if this symbol appears
80 in any relocs other than a 16 bit call. */
83 /* If there is a stub that 16 bit functions should use to call this
84 32 bit function, this points to the section containing the stub. */
87 /* This is like the call_stub field, but it is used if the function
88 being called returns a floating point value. */
89 asection *call_fp_stub;
92 static bfd_reloc_status_type mips32_64bit_reloc
93 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
94 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
95 PARAMS ((bfd *, bfd_reloc_code_real_type));
96 static void mips_info_to_howto_rel
97 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
98 static void mips_info_to_howto_rela
99 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
100 static void bfd_mips_elf32_swap_gptab_in
101 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
102 static void bfd_mips_elf32_swap_gptab_out
103 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
104 static void bfd_mips_elf_swap_msym_in
105 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
106 static void bfd_mips_elf_swap_msym_out
107 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
108 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
109 static boolean mips_elf32_object_p PARAMS ((bfd *));
110 static boolean mips_elf_create_procedure_table
111 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
112 struct ecoff_debug_info *));
113 static int mips_elf_additional_program_headers PARAMS ((bfd *));
114 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
115 static INLINE int elf_mips_isa PARAMS ((flagword));
116 static INLINE int elf_mips_mach PARAMS ((flagword));
117 static INLINE char* elf_mips_abi_name PARAMS ((flagword));
118 static boolean mips_elf32_section_from_shdr
119 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
120 static boolean mips_elf32_section_processing
121 PARAMS ((bfd *, Elf32_Internal_Shdr *));
122 static boolean mips_elf_is_local_label_name
123 PARAMS ((bfd *, const char *));
124 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
125 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
126 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
128 static int gptab_compare PARAMS ((const void *, const void *));
129 static boolean mips_elf_final_link
130 PARAMS ((bfd *, struct bfd_link_info *));
131 static void mips_elf_relocate_hi16
132 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
134 static boolean mips_elf_relocate_got_local
135 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
136 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
137 static void mips_elf_relocate_global_got
138 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
139 static bfd_reloc_status_type mips16_jump_reloc
140 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
141 static bfd_reloc_status_type mips16_gprel_reloc
142 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
143 static boolean mips_elf_relocate_section
144 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
145 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
146 static boolean mips_elf_link_output_symbol_hook
147 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
149 static boolean mips_elf_create_dynamic_sections
150 PARAMS ((bfd *, struct bfd_link_info *));
151 static boolean mips_elf_create_compact_rel_section
152 PARAMS ((bfd *, struct bfd_link_info *));
153 static boolean mips_elf_create_got_section
154 PARAMS ((bfd *, struct bfd_link_info *));
155 static boolean mips_elf_check_relocs
156 PARAMS ((bfd *, struct bfd_link_info *, asection *,
157 const Elf_Internal_Rela *));
158 static boolean mips_elf_adjust_dynamic_symbol
159 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
160 static boolean mips_elf_always_size_sections
161 PARAMS ((bfd *, struct bfd_link_info *));
162 static boolean mips_elf_size_dynamic_sections
163 PARAMS ((bfd *, struct bfd_link_info *));
164 static boolean mips_elf_finish_dynamic_symbol
165 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
166 Elf_Internal_Sym *));
167 static boolean mips_elf_finish_dynamic_sections
168 PARAMS ((bfd *, struct bfd_link_info *));
169 static boolean mips_elf_add_symbol_hook
170 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
171 const char **, flagword *, asection **, bfd_vma *));
172 static bfd_reloc_status_type mips_elf_final_gp
173 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
174 static bfd_byte *elf32_mips_get_relocated_section_contents
175 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
176 bfd_byte *, boolean, asymbol **));
177 static asection *mips_elf_create_msym_section
179 static void mips_elf_irix6_finish_dynamic_symbol
180 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
181 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
182 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
183 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
184 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
185 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
186 static bfd_vma mips_elf_global_got_index
187 PARAMS ((bfd *, struct elf_link_hash_entry *));
188 static bfd_vma mips_elf_local_got_index
189 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
190 static bfd_vma mips_elf_got_offset_from_index
191 PARAMS ((bfd *, bfd *, bfd_vma));
192 static boolean mips_elf_record_global_got_symbol
193 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
194 struct mips_got_info *));
195 static bfd_vma mips_elf_got_page
196 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
197 static boolean mips_elf_next_lo16_addend
198 PARAMS ((Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_vma *));
199 static bfd_reloc_status_type mips_elf_calculate_relocation
200 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
201 Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
202 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **,
203 bfd_vma *, const char **));
204 static bfd_vma mips_elf_obtain_contents
205 PARAMS ((reloc_howto_type *, Elf_Internal_Rela *, bfd *, bfd_byte *));
206 static void mips_elf_perform_relocation
207 PARAMS ((reloc_howto_type *, Elf_Internal_Rela *, bfd_vma,
209 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
210 static boolean mips_elf_sort_hash_table_f
211 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
212 static boolean mips_elf_sort_hash_table
213 PARAMS ((struct bfd_link_info *));
214 static asection * mips_elf_got_section PARAMS ((bfd *));
215 static struct mips_got_info *mips_elf_got_info
216 PARAMS ((bfd *, asection **));
217 static bfd_vma mips_elf_create_local_got_entry
218 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
219 static bfd_vma mips_elf_got16_entry
220 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
221 static unsigned int mips_elf_create_dynamic_relocation
222 PARAMS ((bfd *, struct bfd_link_info *, Elf32_Internal_Rela *,
223 long, bfd_vma, asection *));
225 /* The level of IRIX compatibility we're striving for. */
233 /* Nonzero if ABFD is using the N32 ABI. */
235 #define ABI_N32_P(abfd) \
236 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
238 /* What version of Irix we are trying to be compatible with. FIXME:
239 At the moment, we never generate "normal" MIPS ELF ABI executables;
240 we always use some version of Irix. */
242 #define IRIX_COMPAT(abfd) \
243 (ABI_N32_P (abfd) ? ict_irix6 : ict_irix5)
245 /* Whether we are trying to be compatible with IRIX at all. */
247 #define SGI_COMPAT(abfd) \
248 (IRIX_COMPAT (abfd) != ict_none)
250 /* The name of the msym section. */
251 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
253 /* The name of the srdata section. */
254 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
256 /* The name of the options section. */
257 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
258 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
260 /* The name of the stub section. */
261 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
262 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
264 /* The number of local .got entries we reserve. */
265 #define MIPS_RESERVED_GOTNO (2)
267 /* Instructions which appear in a stub. For some reason the stub is
268 slightly different on an SGI system. */
269 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
270 #define STUB_LW(abfd) \
272 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
273 : 0x8f998000) /* lw t9,0x8000(gp) */
274 #define STUB_MOVE 0x03e07825 /* move t7,ra */
275 #define STUB_JALR 0x0320f809 /* jal t9 */
276 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
277 #define MIPS_FUNCTION_STUB_SIZE (16)
280 /* We no longer try to identify particular sections for the .dynsym
281 section. When we do, we wind up crashing if there are other random
282 sections with relocations. */
284 /* Names of sections which appear in the .dynsym section in an Irix 5
287 static const char * const mips_elf_dynsym_sec_names[] =
300 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
301 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
303 /* The number of entries in mips_elf_dynsym_sec_names which go in the
306 #define MIPS_TEXT_DYNSYM_SECNO (3)
310 /* The names of the runtime procedure table symbols used on Irix 5. */
312 static const char * const mips_elf_dynsym_rtproc_names[] =
315 "_procedure_string_table",
316 "_procedure_table_size",
320 /* These structures are used to generate the .compact_rel section on
325 unsigned long id1; /* Always one? */
326 unsigned long num; /* Number of compact relocation entries. */
327 unsigned long id2; /* Always two? */
328 unsigned long offset; /* The file offset of the first relocation. */
329 unsigned long reserved0; /* Zero? */
330 unsigned long reserved1; /* Zero? */
339 bfd_byte reserved0[4];
340 bfd_byte reserved1[4];
341 } Elf32_External_compact_rel;
345 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
346 unsigned int rtype : 4; /* Relocation types. See below. */
347 unsigned int dist2to : 8;
348 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
349 unsigned long konst; /* KONST field. See below. */
350 unsigned long vaddr; /* VADDR to be relocated. */
355 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
356 unsigned int rtype : 4; /* Relocation types. See below. */
357 unsigned int dist2to : 8;
358 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
359 unsigned long konst; /* KONST field. See below. */
367 } Elf32_External_crinfo;
373 } Elf32_External_crinfo2;
375 /* These are the constants used to swap the bitfields in a crinfo. */
377 #define CRINFO_CTYPE (0x1)
378 #define CRINFO_CTYPE_SH (31)
379 #define CRINFO_RTYPE (0xf)
380 #define CRINFO_RTYPE_SH (27)
381 #define CRINFO_DIST2TO (0xff)
382 #define CRINFO_DIST2TO_SH (19)
383 #define CRINFO_RELVADDR (0x7ffff)
384 #define CRINFO_RELVADDR_SH (0)
386 /* A compact relocation info has long (3 words) or short (2 words)
387 formats. A short format doesn't have VADDR field and relvaddr
388 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
389 #define CRF_MIPS_LONG 1
390 #define CRF_MIPS_SHORT 0
392 /* There are 4 types of compact relocation at least. The value KONST
393 has different meaning for each type:
396 CT_MIPS_REL32 Address in data
397 CT_MIPS_WORD Address in word (XXX)
398 CT_MIPS_GPHI_LO GP - vaddr
399 CT_MIPS_JMPAD Address to jump
402 #define CRT_MIPS_REL32 0xa
403 #define CRT_MIPS_WORD 0xb
404 #define CRT_MIPS_GPHI_LO 0xc
405 #define CRT_MIPS_JMPAD 0xd
407 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
408 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
409 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
410 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
412 static void bfd_elf32_swap_compact_rel_out
413 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
414 static void bfd_elf32_swap_crinfo_out
415 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
417 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
419 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
420 from smaller values. Start with zero, widen, *then* decrement. */
421 #define MINUS_ONE (((bfd_vma)0) - 1)
423 static reloc_howto_type elf_mips_howto_table[] =
426 HOWTO (R_MIPS_NONE, /* type */
428 0, /* size (0 = byte, 1 = short, 2 = long) */
430 false, /* pc_relative */
432 complain_overflow_dont, /* complain_on_overflow */
433 bfd_elf_generic_reloc, /* special_function */
434 "R_MIPS_NONE", /* name */
435 false, /* partial_inplace */
438 false), /* pcrel_offset */
440 /* 16 bit relocation. */
441 HOWTO (R_MIPS_16, /* type */
443 1, /* size (0 = byte, 1 = short, 2 = long) */
445 false, /* pc_relative */
447 complain_overflow_bitfield, /* complain_on_overflow */
448 bfd_elf_generic_reloc, /* special_function */
449 "R_MIPS_16", /* name */
450 true, /* partial_inplace */
451 0xffff, /* src_mask */
452 0xffff, /* dst_mask */
453 false), /* pcrel_offset */
455 /* 32 bit relocation. */
456 HOWTO (R_MIPS_32, /* type */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
460 false, /* pc_relative */
462 complain_overflow_bitfield, /* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_MIPS_32", /* name */
465 true, /* partial_inplace */
466 0xffffffff, /* src_mask */
467 0xffffffff, /* dst_mask */
468 false), /* pcrel_offset */
470 /* 32 bit symbol relative relocation. */
471 HOWTO (R_MIPS_REL32, /* type */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
475 false, /* pc_relative */
477 complain_overflow_bitfield, /* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_MIPS_REL32", /* name */
480 true, /* partial_inplace */
481 0xffffffff, /* src_mask */
482 0xffffffff, /* dst_mask */
483 false), /* pcrel_offset */
485 /* 26 bit branch address. */
486 HOWTO (R_MIPS_26, /* type */
488 2, /* size (0 = byte, 1 = short, 2 = long) */
490 false, /* pc_relative */
492 complain_overflow_dont, /* complain_on_overflow */
493 /* This needs complex overflow
494 detection, because the upper four
495 bits must match the PC. */
496 bfd_elf_generic_reloc, /* special_function */
497 "R_MIPS_26", /* name */
498 true, /* partial_inplace */
499 0x3ffffff, /* src_mask */
500 0x3ffffff, /* dst_mask */
501 false), /* pcrel_offset */
503 /* High 16 bits of symbol value. */
504 HOWTO (R_MIPS_HI16, /* type */
506 2, /* size (0 = byte, 1 = short, 2 = long) */
508 false, /* pc_relative */
510 complain_overflow_dont, /* complain_on_overflow */
511 _bfd_mips_elf_hi16_reloc, /* special_function */
512 "R_MIPS_HI16", /* name */
513 true, /* partial_inplace */
514 0xffff, /* src_mask */
515 0xffff, /* dst_mask */
516 false), /* pcrel_offset */
518 /* Low 16 bits of symbol value. */
519 HOWTO (R_MIPS_LO16, /* type */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
523 false, /* pc_relative */
525 complain_overflow_dont, /* complain_on_overflow */
526 _bfd_mips_elf_lo16_reloc, /* special_function */
527 "R_MIPS_LO16", /* name */
528 true, /* partial_inplace */
529 0xffff, /* src_mask */
530 0xffff, /* dst_mask */
531 false), /* pcrel_offset */
533 /* GP relative reference. */
534 HOWTO (R_MIPS_GPREL16, /* type */
536 2, /* size (0 = byte, 1 = short, 2 = long) */
538 false, /* pc_relative */
540 complain_overflow_signed, /* complain_on_overflow */
541 _bfd_mips_elf_gprel16_reloc, /* special_function */
542 "R_MIPS_GPREL16", /* name */
543 true, /* partial_inplace */
544 0xffff, /* src_mask */
545 0xffff, /* dst_mask */
546 false), /* pcrel_offset */
548 /* Reference to literal section. */
549 HOWTO (R_MIPS_LITERAL, /* type */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
553 false, /* pc_relative */
555 complain_overflow_signed, /* complain_on_overflow */
556 _bfd_mips_elf_gprel16_reloc, /* special_function */
557 "R_MIPS_LITERAL", /* name */
558 true, /* partial_inplace */
559 0xffff, /* src_mask */
560 0xffff, /* dst_mask */
561 false), /* pcrel_offset */
563 /* Reference to global offset table. */
564 HOWTO (R_MIPS_GOT16, /* type */
566 2, /* size (0 = byte, 1 = short, 2 = long) */
568 false, /* pc_relative */
570 complain_overflow_signed, /* complain_on_overflow */
571 _bfd_mips_elf_got16_reloc, /* special_function */
572 "R_MIPS_GOT16", /* name */
573 false, /* partial_inplace */
575 0xffff, /* dst_mask */
576 false), /* pcrel_offset */
578 /* 16 bit PC relative reference. */
579 HOWTO (R_MIPS_PC16, /* type */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
583 true, /* pc_relative */
585 complain_overflow_signed, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* special_function */
587 "R_MIPS_PC16", /* name */
588 true, /* partial_inplace */
589 0xffff, /* src_mask */
590 0xffff, /* dst_mask */
591 false), /* pcrel_offset */
593 /* 16 bit call through global offset table. */
594 HOWTO (R_MIPS_CALL16, /* type */
596 2, /* size (0 = byte, 1 = short, 2 = long) */
598 false, /* pc_relative */
600 complain_overflow_signed, /* complain_on_overflow */
601 bfd_elf_generic_reloc, /* special_function */
602 "R_MIPS_CALL16", /* name */
603 false, /* partial_inplace */
605 0xffff, /* dst_mask */
606 false), /* pcrel_offset */
608 /* 32 bit GP relative reference. */
609 HOWTO (R_MIPS_GPREL32, /* type */
611 2, /* size (0 = byte, 1 = short, 2 = long) */
613 false, /* pc_relative */
615 complain_overflow_bitfield, /* complain_on_overflow */
616 _bfd_mips_elf_gprel32_reloc, /* special_function */
617 "R_MIPS_GPREL32", /* name */
618 true, /* partial_inplace */
619 0xffffffff, /* src_mask */
620 0xffffffff, /* dst_mask */
621 false), /* pcrel_offset */
623 /* The remaining relocs are defined on Irix 5, although they are
624 not defined by the ABI. */
629 /* A 5 bit shift field. */
630 HOWTO (R_MIPS_SHIFT5, /* type */
632 2, /* size (0 = byte, 1 = short, 2 = long) */
634 false, /* pc_relative */
636 complain_overflow_bitfield, /* complain_on_overflow */
637 bfd_elf_generic_reloc, /* special_function */
638 "R_MIPS_SHIFT5", /* name */
639 true, /* partial_inplace */
640 0x000007c0, /* src_mask */
641 0x000007c0, /* dst_mask */
642 false), /* pcrel_offset */
644 /* A 6 bit shift field. */
645 /* FIXME: This is not handled correctly; a special function is
646 needed to put the most significant bit in the right place. */
647 HOWTO (R_MIPS_SHIFT6, /* type */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
651 false, /* pc_relative */
653 complain_overflow_bitfield, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_MIPS_SHIFT6", /* name */
656 true, /* partial_inplace */
657 0x000007c4, /* src_mask */
658 0x000007c4, /* dst_mask */
659 false), /* pcrel_offset */
661 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
662 are 64 bits long; the upper 32 bits are simply a sign extension.
663 The fields of the howto should be the same as for R_MIPS_32,
664 other than the type, name, and special_function. */
665 HOWTO (R_MIPS_64, /* type */
667 2, /* size (0 = byte, 1 = short, 2 = long) */
669 false, /* pc_relative */
671 complain_overflow_bitfield, /* complain_on_overflow */
672 mips32_64bit_reloc, /* special_function */
673 "R_MIPS_64", /* name */
674 true, /* partial_inplace */
675 0xffffffff, /* src_mask */
676 0xffffffff, /* dst_mask */
677 false), /* pcrel_offset */
679 /* Displacement in the global offset table. */
680 HOWTO (R_MIPS_GOT_DISP, /* type */
682 2, /* size (0 = byte, 1 = short, 2 = long) */
684 false, /* pc_relative */
686 complain_overflow_bitfield, /* complain_on_overflow */
687 bfd_elf_generic_reloc, /* special_function */
688 "R_MIPS_GOT_DISP", /* name */
689 true, /* partial_inplace */
690 0x0000ffff, /* src_mask */
691 0x0000ffff, /* dst_mask */
692 false), /* pcrel_offset */
694 /* Displacement to page pointer in the global offset table. */
695 HOWTO (R_MIPS_GOT_PAGE, /* type */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
699 false, /* pc_relative */
701 complain_overflow_bitfield, /* complain_on_overflow */
702 bfd_elf_generic_reloc, /* special_function */
703 "R_MIPS_GOT_PAGE", /* name */
704 true, /* partial_inplace */
705 0x0000ffff, /* src_mask */
706 0x0000ffff, /* dst_mask */
707 false), /* pcrel_offset */
709 /* Offset from page pointer in the global offset table. */
710 HOWTO (R_MIPS_GOT_OFST, /* type */
712 2, /* size (0 = byte, 1 = short, 2 = long) */
714 false, /* pc_relative */
716 complain_overflow_bitfield, /* complain_on_overflow */
717 bfd_elf_generic_reloc, /* special_function */
718 "R_MIPS_GOT_OFST", /* name */
719 true, /* partial_inplace */
720 0x0000ffff, /* src_mask */
721 0x0000ffff, /* dst_mask */
722 false), /* pcrel_offset */
724 /* High 16 bits of displacement in global offset table. */
725 HOWTO (R_MIPS_GOT_HI16, /* type */
727 2, /* size (0 = byte, 1 = short, 2 = long) */
729 false, /* pc_relative */
731 complain_overflow_dont, /* complain_on_overflow */
732 bfd_elf_generic_reloc, /* special_function */
733 "R_MIPS_GOT_HI16", /* name */
734 true, /* partial_inplace */
735 0x0000ffff, /* src_mask */
736 0x0000ffff, /* dst_mask */
737 false), /* pcrel_offset */
739 /* Low 16 bits of displacement in global offset table. */
740 HOWTO (R_MIPS_GOT_LO16, /* type */
742 2, /* size (0 = byte, 1 = short, 2 = long) */
744 false, /* pc_relative */
746 complain_overflow_dont, /* complain_on_overflow */
747 bfd_elf_generic_reloc, /* special_function */
748 "R_MIPS_GOT_LO16", /* name */
749 true, /* partial_inplace */
750 0x0000ffff, /* src_mask */
751 0x0000ffff, /* dst_mask */
752 false), /* pcrel_offset */
754 /* 64 bit subtraction. Used in the N32 ABI. */
755 HOWTO (R_MIPS_SUB, /* type */
757 4, /* size (0 = byte, 1 = short, 2 = long) */
759 false, /* pc_relative */
761 complain_overflow_bitfield, /* complain_on_overflow */
762 bfd_elf_generic_reloc, /* special_function */
763 "R_MIPS_SUB", /* name */
764 true, /* partial_inplace */
765 MINUS_ONE, /* src_mask */
766 MINUS_ONE, /* dst_mask */
767 false), /* pcrel_offset */
769 /* Used to cause the linker to insert and delete instructions? */
774 /* Get the higher values of a 64 bit addend. Presumably not used in
779 /* High 16 bits of displacement in global offset table. */
780 HOWTO (R_MIPS_CALL_HI16, /* type */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
784 false, /* pc_relative */
786 complain_overflow_dont, /* complain_on_overflow */
787 bfd_elf_generic_reloc, /* special_function */
788 "R_MIPS_CALL_HI16", /* name */
789 true, /* partial_inplace */
790 0x0000ffff, /* src_mask */
791 0x0000ffff, /* dst_mask */
792 false), /* pcrel_offset */
794 /* Low 16 bits of displacement in global offset table. */
795 HOWTO (R_MIPS_CALL_LO16, /* type */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
799 false, /* pc_relative */
801 complain_overflow_dont, /* complain_on_overflow */
802 bfd_elf_generic_reloc, /* special_function */
803 "R_MIPS_CALL_LO16", /* name */
804 true, /* partial_inplace */
805 0x0000ffff, /* src_mask */
806 0x0000ffff, /* dst_mask */
807 false), /* pcrel_offset */
809 /* Section displacement. */
810 HOWTO (R_MIPS_SCN_DISP, /* type */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
814 false, /* pc_relative */
816 complain_overflow_dont, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
818 "R_MIPS_SCN_DISP", /* name */
819 false, /* partial_inplace */
820 0xffffffff, /* src_mask */
821 0xffffffff, /* dst_mask */
822 false), /* pcrel_offset */
825 { R_MIPS_ADD_IMMEDIATE },
829 /* Protected jump conversion. This is an optimization hint. No
830 relocation is required for correctness. */
831 HOWTO (R_MIPS_JALR, /* type */
833 0, /* 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_JALR", /* name */
840 false, /* partial_inplace */
841 0x00000000, /* src_mask */
842 0x00000000, /* dst_mask */
843 false), /* pcrel_offset */
846 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
847 is a hack to make the linker think that we need 64 bit values. */
848 static reloc_howto_type elf_mips_ctor64_howto =
849 HOWTO (R_MIPS_64, /* type */
851 4, /* size (0 = byte, 1 = short, 2 = long) */
853 false, /* pc_relative */
855 complain_overflow_signed, /* complain_on_overflow */
856 mips32_64bit_reloc, /* special_function */
857 "R_MIPS_64", /* name */
858 true, /* partial_inplace */
859 0xffffffff, /* src_mask */
860 0xffffffff, /* dst_mask */
861 false); /* pcrel_offset */
863 /* The reloc used for the mips16 jump instruction. */
864 static reloc_howto_type elf_mips16_jump_howto =
865 HOWTO (R_MIPS16_26, /* type */
867 2, /* size (0 = byte, 1 = short, 2 = long) */
869 false, /* pc_relative */
871 complain_overflow_dont, /* complain_on_overflow */
872 /* This needs complex overflow
873 detection, because the upper four
874 bits must match the PC. */
875 mips16_jump_reloc, /* special_function */
876 "R_MIPS16_26", /* name */
877 true, /* partial_inplace */
878 0x3ffffff, /* src_mask */
879 0x3ffffff, /* dst_mask */
880 false); /* pcrel_offset */
882 /* The reloc used for the mips16 gprel instruction. The src_mask and
883 dsk_mask for this howto do not reflect the actual instruction, in
884 which the value is not contiguous; the masks are for the
885 convenience of the relocate_section routine. */
886 static reloc_howto_type elf_mips16_gprel_howto =
887 HOWTO (R_MIPS16_GPREL, /* type */
889 2, /* size (0 = byte, 1 = short, 2 = long) */
891 false, /* pc_relative */
893 complain_overflow_signed, /* complain_on_overflow */
894 mips16_gprel_reloc, /* special_function */
895 "R_MIPS16_GPREL", /* name */
896 true, /* partial_inplace */
897 0xffff, /* src_mask */
898 0xffff, /* dst_mask */
899 false); /* pcrel_offset */
902 /* GNU extension to record C++ vtable hierarchy */
903 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
904 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
908 false, /* pc_relative */
910 complain_overflow_dont, /* complain_on_overflow */
911 NULL, /* special_function */
912 "R_MIPS_GNU_VTINHERIT", /* name */
913 false, /* partial_inplace */
916 false); /* pcrel_offset */
918 /* GNU extension to record C++ vtable member usage */
919 static reloc_howto_type elf_mips_gnu_vtentry_howto =
920 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
922 2, /* size (0 = byte, 1 = short, 2 = long) */
924 false, /* pc_relative */
926 complain_overflow_dont, /* complain_on_overflow */
927 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
928 "R_MIPS_GNU_VTENTRY", /* name */
929 false, /* partial_inplace */
932 false); /* pcrel_offset */
934 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
935 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
936 the HI16. Here we just save the information we need; we do the
937 actual relocation when we see the LO16. MIPS ELF requires that the
938 LO16 immediately follow the HI16. As a GNU extension, we permit an
939 arbitrary number of HI16 relocs to be associated with a single LO16
940 reloc. This extension permits gcc to output the HI and LO relocs
945 struct mips_hi16 *next;
950 /* FIXME: This should not be a static variable. */
952 static struct mips_hi16 *mips_hi16_list;
954 bfd_reloc_status_type
955 _bfd_mips_elf_hi16_reloc (abfd,
963 arelent *reloc_entry;
966 asection *input_section;
968 char **error_message;
970 bfd_reloc_status_type ret;
974 /* If we're relocating, and this an external symbol, we don't want
975 to change anything. */
976 if (output_bfd != (bfd *) NULL
977 && (symbol->flags & BSF_SECTION_SYM) == 0
978 && reloc_entry->addend == 0)
980 reloc_entry->address += input_section->output_offset;
986 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
988 boolean relocateable;
991 if (ret == bfd_reloc_undefined)
994 if (output_bfd != NULL)
998 relocateable = false;
999 output_bfd = symbol->section->output_section->owner;
1002 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1003 error_message, &gp);
1004 if (ret != bfd_reloc_ok)
1007 relocation = gp - reloc_entry->address;
1011 if (bfd_is_und_section (symbol->section)
1012 && output_bfd == (bfd *) NULL)
1013 ret = bfd_reloc_undefined;
1015 if (bfd_is_com_section (symbol->section))
1018 relocation = symbol->value;
1021 relocation += symbol->section->output_section->vma;
1022 relocation += symbol->section->output_offset;
1023 relocation += reloc_entry->addend;
1025 if (reloc_entry->address > input_section->_cooked_size)
1026 return bfd_reloc_outofrange;
1028 /* Save the information, and let LO16 do the actual relocation. */
1029 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
1031 return bfd_reloc_outofrange;
1032 n->addr = (bfd_byte *) data + reloc_entry->address;
1033 n->addend = relocation;
1034 n->next = mips_hi16_list;
1037 if (output_bfd != (bfd *) NULL)
1038 reloc_entry->address += input_section->output_offset;
1043 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1044 inplace relocation; this function exists in order to do the
1045 R_MIPS_HI16 relocation described above. */
1047 bfd_reloc_status_type
1048 _bfd_mips_elf_lo16_reloc (abfd,
1056 arelent *reloc_entry;
1059 asection *input_section;
1061 char **error_message;
1063 arelent gp_disp_relent;
1065 if (mips_hi16_list != NULL)
1067 struct mips_hi16 *l;
1074 unsigned long vallo;
1075 struct mips_hi16 *next;
1077 /* Do the HI16 relocation. Note that we actually don't need
1078 to know anything about the LO16 itself, except where to
1079 find the low 16 bits of the addend needed by the LO16. */
1080 insn = bfd_get_32 (abfd, l->addr);
1081 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1083 val = ((insn & 0xffff) << 16) + vallo;
1086 /* The low order 16 bits are always treated as a signed
1087 value. Therefore, a negative value in the low order bits
1088 requires an adjustment in the high order bits. We need
1089 to make this adjustment in two ways: once for the bits we
1090 took from the data, and once for the bits we are putting
1091 back in to the data. */
1092 if ((vallo & 0x8000) != 0)
1094 if ((val & 0x8000) != 0)
1097 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1098 bfd_put_32 (abfd, insn, l->addr);
1100 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1102 gp_disp_relent = *reloc_entry;
1103 reloc_entry = &gp_disp_relent;
1104 reloc_entry->addend = l->addend;
1112 mips_hi16_list = NULL;
1114 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1116 bfd_reloc_status_type ret;
1117 bfd_vma gp, relocation;
1119 /* FIXME: Does this case ever occur? */
1121 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1122 if (ret != bfd_reloc_ok)
1125 relocation = gp - reloc_entry->address;
1126 relocation += symbol->section->output_section->vma;
1127 relocation += symbol->section->output_offset;
1128 relocation += reloc_entry->addend;
1130 if (reloc_entry->address > input_section->_cooked_size)
1131 return bfd_reloc_outofrange;
1133 gp_disp_relent = *reloc_entry;
1134 reloc_entry = &gp_disp_relent;
1135 reloc_entry->addend = relocation - 4;
1138 /* Now do the LO16 reloc in the usual way. */
1139 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1140 input_section, output_bfd, error_message);
1143 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1144 table used for PIC code. If the symbol is an external symbol, the
1145 instruction is modified to contain the offset of the appropriate
1146 entry in the global offset table. If the symbol is a section
1147 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1148 addends are combined to form the real addend against the section
1149 symbol; the GOT16 is modified to contain the offset of an entry in
1150 the global offset table, and the LO16 is modified to offset it
1151 appropriately. Thus an offset larger than 16 bits requires a
1152 modified value in the global offset table.
1154 This implementation suffices for the assembler, but the linker does
1155 not yet know how to create global offset tables. */
1157 bfd_reloc_status_type
1158 _bfd_mips_elf_got16_reloc (abfd,
1166 arelent *reloc_entry;
1169 asection *input_section;
1171 char **error_message;
1173 /* If we're relocating, and this an external symbol, we don't want
1174 to change anything. */
1175 if (output_bfd != (bfd *) NULL
1176 && (symbol->flags & BSF_SECTION_SYM) == 0
1177 && reloc_entry->addend == 0)
1179 reloc_entry->address += input_section->output_offset;
1180 return bfd_reloc_ok;
1183 /* If we're relocating, and this is a local symbol, we can handle it
1185 if (output_bfd != (bfd *) NULL
1186 && (symbol->flags & BSF_SECTION_SYM) != 0)
1187 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1188 input_section, output_bfd, error_message);
1193 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1194 dangerous relocation. */
1197 mips_elf_assign_gp (output_bfd, pgp)
1205 /* If we've already figured out what GP will be, just return it. */
1206 *pgp = _bfd_get_gp_value (output_bfd);
1210 count = bfd_get_symcount (output_bfd);
1211 sym = bfd_get_outsymbols (output_bfd);
1213 /* The linker script will have created a symbol named `_gp' with the
1214 appropriate value. */
1215 if (sym == (asymbol **) NULL)
1219 for (i = 0; i < count; i++, sym++)
1221 register CONST char *name;
1223 name = bfd_asymbol_name (*sym);
1224 if (*name == '_' && strcmp (name, "_gp") == 0)
1226 *pgp = bfd_asymbol_value (*sym);
1227 _bfd_set_gp_value (output_bfd, *pgp);
1235 /* Only get the error once. */
1237 _bfd_set_gp_value (output_bfd, *pgp);
1244 /* We have to figure out the gp value, so that we can adjust the
1245 symbol value correctly. We look up the symbol _gp in the output
1246 BFD. If we can't find it, we're stuck. We cache it in the ELF
1247 target data. We don't need to adjust the symbol value for an
1248 external symbol if we are producing relocateable output. */
1250 static bfd_reloc_status_type
1251 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1254 boolean relocateable;
1255 char **error_message;
1258 if (bfd_is_und_section (symbol->section)
1262 return bfd_reloc_undefined;
1265 *pgp = _bfd_get_gp_value (output_bfd);
1268 || (symbol->flags & BSF_SECTION_SYM) != 0))
1272 /* Make up a value. */
1273 *pgp = symbol->section->output_section->vma + 0x4000;
1274 _bfd_set_gp_value (output_bfd, *pgp);
1276 else if (!mips_elf_assign_gp (output_bfd, pgp))
1279 (char *) _("GP relative relocation when _gp not defined");
1280 return bfd_reloc_dangerous;
1284 return bfd_reloc_ok;
1287 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1288 become the offset from the gp register. This function also handles
1289 R_MIPS_LITERAL relocations, although those can be handled more
1290 cleverly because the entries in the .lit8 and .lit4 sections can be
1293 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1294 arelent *, asection *,
1295 boolean, PTR, bfd_vma));
1297 bfd_reloc_status_type
1298 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1299 output_bfd, error_message)
1301 arelent *reloc_entry;
1304 asection *input_section;
1306 char **error_message;
1308 boolean relocateable;
1309 bfd_reloc_status_type ret;
1312 /* If we're relocating, and this is an external symbol with no
1313 addend, we don't want to change anything. We will only have an
1314 addend if this is a newly created reloc, not read from an ELF
1316 if (output_bfd != (bfd *) NULL
1317 && (symbol->flags & BSF_SECTION_SYM) == 0
1318 && reloc_entry->addend == 0)
1320 reloc_entry->address += input_section->output_offset;
1321 return bfd_reloc_ok;
1324 if (output_bfd != (bfd *) NULL)
1325 relocateable = true;
1328 relocateable = false;
1329 output_bfd = symbol->section->output_section->owner;
1332 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1334 if (ret != bfd_reloc_ok)
1337 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1338 relocateable, data, gp);
1341 static bfd_reloc_status_type
1342 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1346 arelent *reloc_entry;
1347 asection *input_section;
1348 boolean relocateable;
1356 if (bfd_is_com_section (symbol->section))
1359 relocation = symbol->value;
1361 relocation += symbol->section->output_section->vma;
1362 relocation += symbol->section->output_offset;
1364 if (reloc_entry->address > input_section->_cooked_size)
1365 return bfd_reloc_outofrange;
1367 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1369 /* Set val to the offset into the section or symbol. */
1370 if (reloc_entry->howto->src_mask == 0)
1372 /* This case occurs with the 64-bit MIPS ELF ABI. */
1373 val = reloc_entry->addend;
1377 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1382 /* Adjust val for the final section location and GP value. If we
1383 are producing relocateable output, we don't want to do this for
1384 an external symbol. */
1386 || (symbol->flags & BSF_SECTION_SYM) != 0)
1387 val += relocation - gp;
1389 insn = (insn &~ 0xffff) | (val & 0xffff);
1390 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1393 reloc_entry->address += input_section->output_offset;
1395 /* Make sure it fit in 16 bits. */
1396 if (val >= 0x8000 && val < 0xffff8000)
1397 return bfd_reloc_overflow;
1399 return bfd_reloc_ok;
1402 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1403 from the gp register? XXX */
1405 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1406 arelent *, asection *,
1407 boolean, PTR, bfd_vma));
1409 bfd_reloc_status_type
1410 _bfd_mips_elf_gprel32_reloc (abfd,
1418 arelent *reloc_entry;
1421 asection *input_section;
1423 char **error_message;
1425 boolean relocateable;
1426 bfd_reloc_status_type ret;
1429 /* If we're relocating, and this is an external symbol with no
1430 addend, we don't want to change anything. We will only have an
1431 addend if this is a newly created reloc, not read from an ELF
1433 if (output_bfd != (bfd *) NULL
1434 && (symbol->flags & BSF_SECTION_SYM) == 0
1435 && reloc_entry->addend == 0)
1437 *error_message = (char *)
1438 _("32bits gp relative relocation occurs for an external symbol");
1439 return bfd_reloc_outofrange;
1442 if (output_bfd != (bfd *) NULL)
1444 relocateable = true;
1445 gp = _bfd_get_gp_value (output_bfd);
1449 relocateable = false;
1450 output_bfd = symbol->section->output_section->owner;
1452 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1453 error_message, &gp);
1454 if (ret != bfd_reloc_ok)
1458 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1459 relocateable, data, gp);
1462 static bfd_reloc_status_type
1463 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1467 arelent *reloc_entry;
1468 asection *input_section;
1469 boolean relocateable;
1476 if (bfd_is_com_section (symbol->section))
1479 relocation = symbol->value;
1481 relocation += symbol->section->output_section->vma;
1482 relocation += symbol->section->output_offset;
1484 if (reloc_entry->address > input_section->_cooked_size)
1485 return bfd_reloc_outofrange;
1487 if (reloc_entry->howto->src_mask == 0)
1489 /* This case arises with the 64-bit MIPS ELF ABI. */
1493 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1495 /* Set val to the offset into the section or symbol. */
1496 val += reloc_entry->addend;
1498 /* Adjust val for the final section location and GP value. If we
1499 are producing relocateable output, we don't want to do this for
1500 an external symbol. */
1502 || (symbol->flags & BSF_SECTION_SYM) != 0)
1503 val += relocation - gp;
1505 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1508 reloc_entry->address += input_section->output_offset;
1510 return bfd_reloc_ok;
1513 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1514 generated when addreses are 64 bits. The upper 32 bits are a simle
1517 static bfd_reloc_status_type
1518 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1519 output_bfd, error_message)
1521 arelent *reloc_entry;
1524 asection *input_section;
1526 char **error_message;
1528 bfd_reloc_status_type r;
1533 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1534 input_section, output_bfd, error_message);
1535 if (r != bfd_reloc_continue)
1538 /* Do a normal 32 bit relocation on the lower 32 bits. */
1539 reloc32 = *reloc_entry;
1540 if (bfd_big_endian (abfd))
1541 reloc32.address += 4;
1542 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1543 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1544 output_bfd, error_message);
1546 /* Sign extend into the upper 32 bits. */
1547 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1548 if ((val & 0x80000000) != 0)
1552 addr = reloc_entry->address;
1553 if (bfd_little_endian (abfd))
1555 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1560 /* Handle a mips16 jump. */
1562 static bfd_reloc_status_type
1563 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1564 output_bfd, error_message)
1566 arelent *reloc_entry;
1569 asection *input_section;
1571 char **error_message;
1573 if (output_bfd != (bfd *) NULL
1574 && (symbol->flags & BSF_SECTION_SYM) == 0
1575 && reloc_entry->addend == 0)
1577 reloc_entry->address += input_section->output_offset;
1578 return bfd_reloc_ok;
1583 static boolean warned;
1586 (*_bfd_error_handler)
1587 (_("Linking mips16 objects into %s format is not supported"),
1588 bfd_get_target (input_section->output_section->owner));
1592 return bfd_reloc_undefined;
1595 /* Handle a mips16 GP relative reloc. */
1597 static bfd_reloc_status_type
1598 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1599 output_bfd, error_message)
1601 arelent *reloc_entry;
1604 asection *input_section;
1606 char **error_message;
1608 boolean relocateable;
1609 bfd_reloc_status_type ret;
1611 unsigned short extend, insn;
1612 unsigned long final;
1614 /* If we're relocating, and this is an external symbol with no
1615 addend, we don't want to change anything. We will only have an
1616 addend if this is a newly created reloc, not read from an ELF
1618 if (output_bfd != NULL
1619 && (symbol->flags & BSF_SECTION_SYM) == 0
1620 && reloc_entry->addend == 0)
1622 reloc_entry->address += input_section->output_offset;
1623 return bfd_reloc_ok;
1626 if (output_bfd != NULL)
1627 relocateable = true;
1630 relocateable = false;
1631 output_bfd = symbol->section->output_section->owner;
1634 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1636 if (ret != bfd_reloc_ok)
1639 if (reloc_entry->address > input_section->_cooked_size)
1640 return bfd_reloc_outofrange;
1642 /* Pick up the mips16 extend instruction and the real instruction. */
1643 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1644 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1646 /* Stuff the current addend back as a 32 bit value, do the usual
1647 relocation, and then clean up. */
1649 (((extend & 0x1f) << 11)
1652 (bfd_byte *) data + reloc_entry->address);
1654 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1655 relocateable, data, gp);
1657 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1660 | ((final >> 11) & 0x1f)
1662 (bfd_byte *) data + reloc_entry->address);
1666 (bfd_byte *) data + reloc_entry->address + 2);
1671 /* Return the ISA for a MIPS e_flags value. */
1674 elf_mips_isa (flags)
1677 switch (flags & EF_MIPS_ARCH)
1691 /* Return the MACH for a MIPS e_flags value. */
1694 elf_mips_mach (flags)
1697 switch (flags & EF_MIPS_MACH)
1699 case E_MIPS_MACH_3900:
1700 return bfd_mach_mips3900;
1702 case E_MIPS_MACH_4010:
1703 return bfd_mach_mips4010;
1705 case E_MIPS_MACH_4100:
1706 return bfd_mach_mips4100;
1708 case E_MIPS_MACH_4111:
1709 return bfd_mach_mips4111;
1711 case E_MIPS_MACH_4650:
1712 return bfd_mach_mips4650;
1715 switch (flags & EF_MIPS_ARCH)
1719 return bfd_mach_mips3000;
1723 return bfd_mach_mips6000;
1727 return bfd_mach_mips4000;
1731 return bfd_mach_mips8000;
1739 /* Return printable name for ABI from flagword. */
1742 elf_mips_abi_name (flags)
1745 switch (flags & EF_MIPS_ABI)
1749 case E_MIPS_ABI_O32:
1751 case E_MIPS_ABI_O64:
1753 case E_MIPS_ABI_EABI32:
1755 case E_MIPS_ABI_EABI64:
1758 return "unknown abi";
1762 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1764 struct elf_reloc_map {
1765 bfd_reloc_code_real_type bfd_reloc_val;
1766 enum elf_mips_reloc_type elf_reloc_val;
1769 static CONST struct elf_reloc_map mips_reloc_map[] =
1771 { BFD_RELOC_NONE, R_MIPS_NONE, },
1772 { BFD_RELOC_16, R_MIPS_16 },
1773 { BFD_RELOC_32, R_MIPS_32 },
1774 { BFD_RELOC_64, R_MIPS_64 },
1775 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1776 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1777 { BFD_RELOC_LO16, R_MIPS_LO16 },
1778 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1779 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1780 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1781 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1782 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1783 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1784 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1785 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1786 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1787 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1788 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1789 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1790 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1791 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1794 /* Given a BFD reloc type, return a howto structure. */
1796 static reloc_howto_type *
1797 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1799 bfd_reloc_code_real_type code;
1803 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1805 if (mips_reloc_map[i].bfd_reloc_val == code)
1806 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1812 bfd_set_error (bfd_error_bad_value);
1815 case BFD_RELOC_CTOR:
1816 /* We need to handle BFD_RELOC_CTOR specially.
1817 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1818 size of addresses on this architecture. */
1819 if (bfd_arch_bits_per_address (abfd) == 32)
1820 return &elf_mips_howto_table[(int) R_MIPS_32];
1822 return &elf_mips_ctor64_howto;
1824 case BFD_RELOC_MIPS16_JMP:
1825 return &elf_mips16_jump_howto;
1826 case BFD_RELOC_MIPS16_GPREL:
1827 return &elf_mips16_gprel_howto;
1828 case BFD_RELOC_VTABLE_INHERIT:
1829 return &elf_mips_gnu_vtinherit_howto;
1830 case BFD_RELOC_VTABLE_ENTRY:
1831 return &elf_mips_gnu_vtentry_howto;
1835 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1838 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1841 Elf32_Internal_Rel *dst;
1843 unsigned int r_type;
1845 r_type = ELF32_R_TYPE (dst->r_info);
1849 cache_ptr->howto = &elf_mips16_jump_howto;
1851 case R_MIPS16_GPREL:
1852 cache_ptr->howto = &elf_mips16_gprel_howto;
1854 case R_MIPS_GNU_VTINHERIT:
1855 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1857 case R_MIPS_GNU_VTENTRY:
1858 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1862 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1863 cache_ptr->howto = &elf_mips_howto_table[r_type];
1867 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1868 value for the object file. We get the addend now, rather than
1869 when we do the relocation, because the symbol manipulations done
1870 by the linker may cause us to lose track of the input BFD. */
1871 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1872 && (r_type == (unsigned int) R_MIPS_GPREL16
1873 || r_type == (unsigned int) R_MIPS_LITERAL))
1874 cache_ptr->addend = elf_gp (abfd);
1877 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1880 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1883 Elf32_Internal_Rela *dst;
1885 /* Since an Elf32_Internal_Rel is an initial prefix of an
1886 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1888 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1890 /* If we ever need to do any extra processing with dst->r_addend
1891 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1894 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1895 routines swap this structure in and out. They are used outside of
1896 BFD, so they are globally visible. */
1899 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1901 const Elf32_External_RegInfo *ex;
1904 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1905 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1906 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1907 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1908 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1909 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1913 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1915 const Elf32_RegInfo *in;
1916 Elf32_External_RegInfo *ex;
1918 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1919 (bfd_byte *) ex->ri_gprmask);
1920 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1921 (bfd_byte *) ex->ri_cprmask[0]);
1922 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1923 (bfd_byte *) ex->ri_cprmask[1]);
1924 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1925 (bfd_byte *) ex->ri_cprmask[2]);
1926 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1927 (bfd_byte *) ex->ri_cprmask[3]);
1928 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1929 (bfd_byte *) ex->ri_gp_value);
1932 /* In the 64 bit ABI, the .MIPS.options section holds register
1933 information in an Elf64_Reginfo structure. These routines swap
1934 them in and out. They are globally visible because they are used
1935 outside of BFD. These routines are here so that gas can call them
1936 without worrying about whether the 64 bit ABI has been included. */
1939 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1941 const Elf64_External_RegInfo *ex;
1942 Elf64_Internal_RegInfo *in;
1944 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1945 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1946 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1947 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1948 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1949 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1950 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1954 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1956 const Elf64_Internal_RegInfo *in;
1957 Elf64_External_RegInfo *ex;
1959 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1960 (bfd_byte *) ex->ri_gprmask);
1961 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1962 (bfd_byte *) ex->ri_pad);
1963 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1964 (bfd_byte *) ex->ri_cprmask[0]);
1965 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1966 (bfd_byte *) ex->ri_cprmask[1]);
1967 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1968 (bfd_byte *) ex->ri_cprmask[2]);
1969 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1970 (bfd_byte *) ex->ri_cprmask[3]);
1971 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1972 (bfd_byte *) ex->ri_gp_value);
1975 /* Swap an entry in a .gptab section. Note that these routines rely
1976 on the equivalence of the two elements of the union. */
1979 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1981 const Elf32_External_gptab *ex;
1984 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1985 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1989 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1991 const Elf32_gptab *in;
1992 Elf32_External_gptab *ex;
1994 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1995 ex->gt_entry.gt_g_value);
1996 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1997 ex->gt_entry.gt_bytes);
2001 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2003 const Elf32_compact_rel *in;
2004 Elf32_External_compact_rel *ex;
2006 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
2007 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
2008 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
2009 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
2010 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
2011 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
2015 bfd_elf32_swap_crinfo_out (abfd, in, ex)
2017 const Elf32_crinfo *in;
2018 Elf32_External_crinfo *ex;
2022 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2023 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2024 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2025 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2026 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
2027 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
2028 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
2031 /* Swap in an options header. */
2034 bfd_mips_elf_swap_options_in (abfd, ex, in)
2036 const Elf_External_Options *ex;
2037 Elf_Internal_Options *in;
2039 in->kind = bfd_h_get_8 (abfd, ex->kind);
2040 in->size = bfd_h_get_8 (abfd, ex->size);
2041 in->section = bfd_h_get_16 (abfd, ex->section);
2042 in->info = bfd_h_get_32 (abfd, ex->info);
2045 /* Swap out an options header. */
2048 bfd_mips_elf_swap_options_out (abfd, in, ex)
2050 const Elf_Internal_Options *in;
2051 Elf_External_Options *ex;
2053 bfd_h_put_8 (abfd, in->kind, ex->kind);
2054 bfd_h_put_8 (abfd, in->size, ex->size);
2055 bfd_h_put_16 (abfd, in->section, ex->section);
2056 bfd_h_put_32 (abfd, in->info, ex->info);
2059 /* Swap in an MSYM entry. */
2062 bfd_mips_elf_swap_msym_in (abfd, ex, in)
2064 const Elf32_External_Msym *ex;
2065 Elf32_Internal_Msym *in;
2067 in->ms_hash_value = bfd_h_get_32 (abfd, ex->ms_hash_value);
2068 in->ms_info = bfd_h_get_32 (abfd, ex->ms_info);
2071 /* Swap out an MSYM entry. */
2074 bfd_mips_elf_swap_msym_out (abfd, in, ex)
2076 const Elf32_Internal_Msym *in;
2077 Elf32_External_Msym *ex;
2079 bfd_h_put_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2080 bfd_h_put_32 (abfd, in->ms_info, ex->ms_info);
2084 /* Determine whether a symbol is global for the purposes of splitting
2085 the symbol table into global symbols and local symbols. At least
2086 on Irix 5, this split must be between section symbols and all other
2087 symbols. On most ELF targets the split is between static symbols
2088 and externally visible symbols. */
2092 mips_elf_sym_is_global (abfd, sym)
2096 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
2099 /* Set the right machine number for a MIPS ELF file. This is used for
2100 both the 32-bit and the 64-bit ABI. */
2103 _bfd_mips_elf_object_p (abfd)
2106 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2107 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2111 /* Set the right machine number for a 32-bit MIPS ELF file. */
2114 mips_elf32_object_p (abfd)
2117 /* Irix 5 is broken. Object file symbol tables are not always
2118 sorted correctly such that local symbols precede global symbols,
2119 and the sh_info field in the symbol table is not always right. */
2120 elf_bad_symtab (abfd) = true;
2122 return _bfd_mips_elf_object_p (abfd);
2125 /* The final processing done just before writing out a MIPS ELF object
2126 file. This gets the MIPS architecture right based on the machine
2127 number. This is used by both the 32-bit and the 64-bit ABI. */
2131 _bfd_mips_elf_final_write_processing (abfd, linker)
2137 Elf_Internal_Shdr **hdrpp;
2141 switch (bfd_get_mach (abfd))
2144 case bfd_mach_mips3000:
2145 val = E_MIPS_ARCH_1;
2148 case bfd_mach_mips3900:
2149 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2152 case bfd_mach_mips6000:
2153 val = E_MIPS_ARCH_2;
2156 case bfd_mach_mips4000:
2157 case bfd_mach_mips4300:
2158 val = E_MIPS_ARCH_3;
2161 case bfd_mach_mips4010:
2162 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2165 case bfd_mach_mips4100:
2166 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2169 case bfd_mach_mips4111:
2170 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2173 case bfd_mach_mips4650:
2174 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2177 case bfd_mach_mips8000:
2178 val = E_MIPS_ARCH_4;
2182 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2183 elf_elfheader (abfd)->e_flags |= val;
2185 /* Set the sh_info field for .gptab sections and other appropriate
2186 info for each special section. */
2187 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2188 i < elf_elfheader (abfd)->e_shnum;
2191 switch ((*hdrpp)->sh_type)
2194 case SHT_MIPS_LIBLIST:
2195 sec = bfd_get_section_by_name (abfd, ".dynstr");
2197 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2200 case SHT_MIPS_GPTAB:
2201 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2202 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2203 BFD_ASSERT (name != NULL
2204 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2205 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2206 BFD_ASSERT (sec != NULL);
2207 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2210 case SHT_MIPS_CONTENT:
2211 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2212 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2213 BFD_ASSERT (name != NULL
2214 && strncmp (name, ".MIPS.content",
2215 sizeof ".MIPS.content" - 1) == 0);
2216 sec = bfd_get_section_by_name (abfd,
2217 name + sizeof ".MIPS.content" - 1);
2218 BFD_ASSERT (sec != NULL);
2219 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2222 case SHT_MIPS_SYMBOL_LIB:
2223 sec = bfd_get_section_by_name (abfd, ".dynsym");
2225 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2226 sec = bfd_get_section_by_name (abfd, ".liblist");
2228 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2231 case SHT_MIPS_EVENTS:
2232 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2233 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2234 BFD_ASSERT (name != NULL);
2235 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2236 sec = bfd_get_section_by_name (abfd,
2237 name + sizeof ".MIPS.events" - 1);
2240 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2241 sizeof ".MIPS.post_rel" - 1) == 0);
2242 sec = bfd_get_section_by_name (abfd,
2244 + sizeof ".MIPS.post_rel" - 1));
2246 BFD_ASSERT (sec != NULL);
2247 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2254 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2257 _bfd_mips_elf_set_private_flags (abfd, flags)
2261 BFD_ASSERT (!elf_flags_init (abfd)
2262 || elf_elfheader (abfd)->e_flags == flags);
2264 elf_elfheader (abfd)->e_flags = flags;
2265 elf_flags_init (abfd) = true;
2269 /* Copy backend specific data from one object module to another */
2272 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2276 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2277 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2280 BFD_ASSERT (!elf_flags_init (obfd)
2281 || (elf_elfheader (obfd)->e_flags
2282 == elf_elfheader (ibfd)->e_flags));
2284 elf_gp (obfd) = elf_gp (ibfd);
2285 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2286 elf_flags_init (obfd) = true;
2290 /* Merge backend specific data from an object file to the output
2291 object file when linking. */
2294 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2302 /* Check if we have the same endianess */
2303 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2304 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2308 if (bfd_big_endian (ibfd))
2309 msg = _("%s: compiled for a big endian system and target is little endian");
2311 msg = _("%s: compiled for a little endian system and target is big endian");
2313 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2315 bfd_set_error (bfd_error_wrong_format);
2319 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2320 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2323 new_flags = elf_elfheader (ibfd)->e_flags;
2324 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2325 old_flags = elf_elfheader (obfd)->e_flags;
2327 if (! elf_flags_init (obfd))
2329 elf_flags_init (obfd) = true;
2330 elf_elfheader (obfd)->e_flags = new_flags;
2332 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2333 && bfd_get_arch_info (obfd)->the_default)
2335 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2336 bfd_get_mach (ibfd)))
2343 /* Check flag compatibility. */
2345 new_flags &= ~EF_MIPS_NOREORDER;
2346 old_flags &= ~EF_MIPS_NOREORDER;
2348 if (new_flags == old_flags)
2353 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2355 new_flags &= ~EF_MIPS_PIC;
2356 old_flags &= ~EF_MIPS_PIC;
2357 (*_bfd_error_handler)
2358 (_("%s: linking PIC files with non-PIC files"),
2359 bfd_get_filename (ibfd));
2363 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2365 new_flags &= ~EF_MIPS_CPIC;
2366 old_flags &= ~EF_MIPS_CPIC;
2367 (*_bfd_error_handler)
2368 (_("%s: linking abicalls files with non-abicalls files"),
2369 bfd_get_filename (ibfd));
2373 /* Compare the ISA's. */
2374 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2375 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2377 int new_mach = new_flags & EF_MIPS_MACH;
2378 int old_mach = old_flags & EF_MIPS_MACH;
2379 int new_isa = elf_mips_isa (new_flags);
2380 int old_isa = elf_mips_isa (old_flags);
2382 /* If either has no machine specified, just compare the general isa's.
2383 Some combinations of machines are ok, if the isa's match. */
2386 || new_mach == old_mach
2389 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2390 and -mips4 code. They will normally use the same data sizes and
2391 calling conventions. */
2393 if ((new_isa == 1 || new_isa == 2)
2394 ? (old_isa != 1 && old_isa != 2)
2395 : (old_isa == 1 || old_isa == 2))
2397 (*_bfd_error_handler)
2398 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2399 bfd_get_filename (ibfd), new_isa, old_isa);
2406 (*_bfd_error_handler)
2407 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2408 bfd_get_filename (ibfd),
2409 elf_mips_mach (new_flags),
2410 elf_mips_mach (old_flags));
2414 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2415 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2419 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI))
2421 /* Only error if both are set (to different values). */
2422 if ((new_flags & EF_MIPS_ABI)
2423 && (old_flags & EF_MIPS_ABI))
2425 (*_bfd_error_handler)
2426 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2427 bfd_get_filename (ibfd),
2428 elf_mips_abi_name (new_flags),
2429 elf_mips_abi_name (old_flags));
2432 new_flags &= ~EF_MIPS_ABI;
2433 old_flags &= ~EF_MIPS_ABI;
2436 /* Warn about any other mismatches */
2437 if (new_flags != old_flags)
2439 (*_bfd_error_handler)
2440 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2441 bfd_get_filename (ibfd), (unsigned long) new_flags,
2442 (unsigned long) old_flags);
2448 bfd_set_error (bfd_error_bad_value);
2456 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2460 FILE *file = (FILE *) ptr;
2462 BFD_ASSERT (abfd != NULL && ptr != NULL);
2464 /* Print normal ELF private data. */
2465 _bfd_elf_print_private_bfd_data (abfd, ptr);
2467 /* xgettext:c-format */
2468 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2470 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2471 fprintf (file, _ (" [abi=O32]"));
2472 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2473 fprintf (file, _ (" [abi=O64]"));
2474 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2475 fprintf (file, _ (" [abi=EABI32]"));
2476 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2477 fprintf (file, _ (" [abi=EABI64]"));
2478 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2479 fprintf (file, _ (" [abi unknown]"));
2480 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2))
2481 fprintf (file, _ (" [abi=N32]"));
2483 fprintf (file, _ (" [no abi set]"));
2485 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2486 fprintf (file, _ (" [mips1]"));
2487 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2488 fprintf (file, _ (" [mips2]"));
2489 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2490 fprintf (file, _ (" [mips3]"));
2491 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2492 fprintf (file, _ (" [mips4]"));
2494 fprintf (file, _ (" [unknown ISA]"));
2496 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2497 fprintf (file, _ (" [32bitmode]"));
2499 fprintf (file, _ (" [not 32bitmode]"));
2506 /* Handle a MIPS specific section when reading an object file. This
2507 is called when elfcode.h finds a section with an unknown type.
2508 This routine supports both the 32-bit and 64-bit ELF ABI.
2510 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2514 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2516 Elf_Internal_Shdr *hdr;
2521 /* There ought to be a place to keep ELF backend specific flags, but
2522 at the moment there isn't one. We just keep track of the
2523 sections by their name, instead. Fortunately, the ABI gives
2524 suggested names for all the MIPS specific sections, so we will
2525 probably get away with this. */
2526 switch (hdr->sh_type)
2528 case SHT_MIPS_LIBLIST:
2529 if (strcmp (name, ".liblist") != 0)
2533 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2536 case SHT_MIPS_CONFLICT:
2537 if (strcmp (name, ".conflict") != 0)
2540 case SHT_MIPS_GPTAB:
2541 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2544 case SHT_MIPS_UCODE:
2545 if (strcmp (name, ".ucode") != 0)
2548 case SHT_MIPS_DEBUG:
2549 if (strcmp (name, ".mdebug") != 0)
2551 flags = SEC_DEBUGGING;
2553 case SHT_MIPS_REGINFO:
2554 if (strcmp (name, ".reginfo") != 0
2555 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2557 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2559 case SHT_MIPS_IFACE:
2560 if (strcmp (name, ".MIPS.interfaces") != 0)
2563 case SHT_MIPS_CONTENT:
2564 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2567 case SHT_MIPS_OPTIONS:
2568 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2571 case SHT_MIPS_DWARF:
2572 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2575 case SHT_MIPS_SYMBOL_LIB:
2576 if (strcmp (name, ".MIPS.symlib") != 0)
2579 case SHT_MIPS_EVENTS:
2580 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2581 && strncmp (name, ".MIPS.post_rel",
2582 sizeof ".MIPS.post_rel" - 1) != 0)
2589 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2594 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2595 (bfd_get_section_flags (abfd,
2604 /* Handle a 32-bit MIPS ELF specific section. */
2607 mips_elf32_section_from_shdr (abfd, hdr, name)
2609 Elf_Internal_Shdr *hdr;
2612 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2615 /* FIXME: We should record sh_info for a .gptab section. */
2617 /* For a .reginfo section, set the gp value in the tdata information
2618 from the contents of this section. We need the gp value while
2619 processing relocs, so we just get it now. The .reginfo section
2620 is not used in the 64-bit MIPS ELF ABI. */
2621 if (hdr->sh_type == SHT_MIPS_REGINFO)
2623 Elf32_External_RegInfo ext;
2626 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2627 (file_ptr) 0, sizeof ext))
2629 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2630 elf_gp (abfd) = s.ri_gp_value;
2633 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2634 set the gp value based on what we find. We may see both
2635 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2636 they should agree. */
2637 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2639 bfd_byte *contents, *l, *lend;
2641 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2642 if (contents == NULL)
2644 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2645 (file_ptr) 0, hdr->sh_size))
2651 lend = contents + hdr->sh_size;
2652 while (l + sizeof (Elf_External_Options) <= lend)
2654 Elf_Internal_Options intopt;
2656 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2658 if (intopt.kind == ODK_REGINFO)
2660 Elf32_RegInfo intreg;
2662 bfd_mips_elf32_swap_reginfo_in
2664 ((Elf32_External_RegInfo *)
2665 (l + sizeof (Elf_External_Options))),
2667 elf_gp (abfd) = intreg.ri_gp_value;
2677 /* Set the correct type for a MIPS ELF section. We do this by the
2678 section name, which is a hack, but ought to work. This routine is
2679 used by both the 32-bit and the 64-bit ABI. */
2682 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2684 Elf32_Internal_Shdr *hdr;
2687 register const char *name;
2689 name = bfd_get_section_name (abfd, sec);
2691 if (strcmp (name, ".liblist") == 0)
2693 hdr->sh_type = SHT_MIPS_LIBLIST;
2694 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2695 /* The sh_link field is set in final_write_processing. */
2697 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2699 hdr->sh_type = SHT_MIPS_MSYM;
2700 hdr->sh_entsize = 8;
2701 /* FIXME: Set the sh_info field. */
2703 else if (strcmp (name, ".conflict") == 0)
2704 hdr->sh_type = SHT_MIPS_CONFLICT;
2705 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2707 hdr->sh_type = SHT_MIPS_GPTAB;
2708 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2709 /* The sh_info field is set in final_write_processing. */
2711 else if (strcmp (name, ".ucode") == 0)
2712 hdr->sh_type = SHT_MIPS_UCODE;
2713 else if (strcmp (name, ".mdebug") == 0)
2715 hdr->sh_type = SHT_MIPS_DEBUG;
2716 /* In a shared object on Irix 5.3, the .mdebug section has an
2717 entsize of 0. FIXME: Does this matter? */
2718 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2719 hdr->sh_entsize = 0;
2721 hdr->sh_entsize = 1;
2723 else if (strcmp (name, ".reginfo") == 0)
2725 hdr->sh_type = SHT_MIPS_REGINFO;
2726 /* In a shared object on Irix 5.3, the .reginfo section has an
2727 entsize of 0x18. FIXME: Does this matter? */
2728 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2729 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2731 hdr->sh_entsize = 1;
2733 else if (SGI_COMPAT (abfd)
2734 && (strcmp (name, ".hash") == 0
2735 || strcmp (name, ".dynamic") == 0
2736 || strcmp (name, ".dynstr") == 0))
2738 hdr->sh_entsize = 0;
2740 /* This isn't how the Irix 6 linker behaves. */
2741 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2744 else if (strcmp (name, ".got") == 0
2745 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2746 || strcmp (name, ".sdata") == 0
2747 || strcmp (name, ".sbss") == 0
2748 || strcmp (name, ".lit4") == 0
2749 || strcmp (name, ".lit8") == 0)
2750 hdr->sh_flags |= SHF_MIPS_GPREL;
2751 else if (strcmp (name, ".MIPS.interfaces") == 0)
2753 hdr->sh_type = SHT_MIPS_IFACE;
2754 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2756 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2758 hdr->sh_type = SHT_MIPS_CONTENT;
2759 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2760 /* The sh_info field is set in final_write_processing. */
2762 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2764 hdr->sh_type = SHT_MIPS_OPTIONS;
2765 hdr->sh_entsize = 1;
2766 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2768 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2769 hdr->sh_type = SHT_MIPS_DWARF;
2770 else if (strcmp (name, ".MIPS.symlib") == 0)
2772 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2773 /* The sh_link and sh_info fields are set in
2774 final_write_processing. */
2776 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2777 || strncmp (name, ".MIPS.post_rel",
2778 sizeof ".MIPS.post_rel" - 1) == 0)
2780 hdr->sh_type = SHT_MIPS_EVENTS;
2781 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2782 /* The sh_link field is set in final_write_processing. */
2784 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2786 hdr->sh_type = SHT_MIPS_MSYM;
2787 hdr->sh_flags |= SHF_ALLOC;
2788 hdr->sh_entsize = 8;
2791 /* The generic elf_fake_sections will set up REL_HDR using the
2792 default kind of relocations. But, we may actually need both
2793 kinds of relocations, so we set up the second header here. */
2794 if ((sec->flags & SEC_RELOC) != 0)
2796 struct bfd_elf_section_data *esd;
2798 esd = elf_section_data (sec);
2799 BFD_ASSERT (esd->rel_hdr2 == NULL);
2801 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2804 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
2805 !elf_section_data (sec)->use_rela_p);
2811 /* Given a BFD section, try to locate the corresponding ELF section
2812 index. This is used by both the 32-bit and the 64-bit ABI.
2813 Actually, it's not clear to me that the 64-bit ABI supports these,
2814 but for non-PIC objects we will certainly want support for at least
2815 the .scommon section. */
2818 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2820 Elf32_Internal_Shdr *hdr;
2824 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2826 *retval = SHN_MIPS_SCOMMON;
2829 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2831 *retval = SHN_MIPS_ACOMMON;
2837 /* When are writing out the .options or .MIPS.options section,
2838 remember the bytes we are writing out, so that we can install the
2839 GP value in the section_processing routine. */
2842 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2847 bfd_size_type count;
2849 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2853 if (elf_section_data (section) == NULL)
2855 section->used_by_bfd =
2856 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2857 if (elf_section_data (section) == NULL)
2860 c = (bfd_byte *) elf_section_data (section)->tdata;
2865 if (section->_cooked_size != 0)
2866 size = section->_cooked_size;
2868 size = section->_raw_size;
2869 c = (bfd_byte *) bfd_zalloc (abfd, size);
2872 elf_section_data (section)->tdata = (PTR) c;
2875 memcpy (c + offset, location, count);
2878 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2882 /* Work over a section just before writing it out. This routine is
2883 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2884 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2888 _bfd_mips_elf_section_processing (abfd, hdr)
2890 Elf_Internal_Shdr *hdr;
2892 if (hdr->bfd_section != NULL)
2894 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2896 if (strcmp (name, ".sdata") == 0
2897 || strcmp (name, ".lit8") == 0
2898 || strcmp (name, ".lit4") == 0)
2900 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2901 hdr->sh_type = SHT_PROGBITS;
2903 else if (strcmp (name, ".sbss") == 0)
2905 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2906 hdr->sh_type = SHT_NOBITS;
2908 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
2910 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
2911 hdr->sh_type = SHT_PROGBITS;
2913 else if (strcmp (name, ".compact_rel") == 0)
2916 hdr->sh_type = SHT_PROGBITS;
2918 else if (strcmp (name, ".rtproc") == 0)
2920 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2922 unsigned int adjust;
2924 adjust = hdr->sh_size % hdr->sh_addralign;
2926 hdr->sh_size += hdr->sh_addralign - adjust;
2934 /* Work over a section just before writing it out. We update the GP
2935 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2936 on the value we are using. */
2939 mips_elf32_section_processing (abfd, hdr)
2941 Elf32_Internal_Shdr *hdr;
2943 if (hdr->sh_type == SHT_MIPS_REGINFO
2944 && hdr->sh_size > 0)
2948 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2949 BFD_ASSERT (hdr->contents == NULL);
2952 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2955 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2956 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2960 if (hdr->sh_type == SHT_MIPS_OPTIONS
2961 && hdr->bfd_section != NULL
2962 && elf_section_data (hdr->bfd_section) != NULL
2963 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2965 bfd_byte *contents, *l, *lend;
2967 /* We stored the section contents in the elf_section_data tdata
2968 field in the set_section_contents routine. We save the
2969 section contents so that we don't have to read them again.
2970 At this point we know that elf_gp is set, so we can look
2971 through the section contents to see if there is an
2972 ODK_REGINFO structure. */
2974 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2976 lend = contents + hdr->sh_size;
2977 while (l + sizeof (Elf_External_Options) <= lend)
2979 Elf_Internal_Options intopt;
2981 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2983 if (intopt.kind == ODK_REGINFO)
2990 + sizeof (Elf_External_Options)
2991 + (sizeof (Elf32_External_RegInfo) - 4)),
2994 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2995 if (bfd_write (buf, 1, 4, abfd) != 4)
3002 return _bfd_mips_elf_section_processing (abfd, hdr);
3005 /* MIPS ELF uses two common sections. One is the usual one, and the
3006 other is for small objects. All the small objects are kept
3007 together, and then referenced via the gp pointer, which yields
3008 faster assembler code. This is what we use for the small common
3009 section. This approach is copied from ecoff.c. */
3010 static asection mips_elf_scom_section;
3011 static asymbol mips_elf_scom_symbol;
3012 static asymbol *mips_elf_scom_symbol_ptr;
3014 /* MIPS ELF also uses an acommon section, which represents an
3015 allocated common symbol which may be overridden by a
3016 definition in a shared library. */
3017 static asection mips_elf_acom_section;
3018 static asymbol mips_elf_acom_symbol;
3019 static asymbol *mips_elf_acom_symbol_ptr;
3021 /* The Irix 5 support uses two virtual sections, which represent
3022 text/data symbols defined in dynamic objects. */
3023 static asection mips_elf_text_section;
3024 static asection *mips_elf_text_section_ptr;
3025 static asymbol mips_elf_text_symbol;
3026 static asymbol *mips_elf_text_symbol_ptr;
3028 static asection mips_elf_data_section;
3029 static asection *mips_elf_data_section_ptr;
3030 static asymbol mips_elf_data_symbol;
3031 static asymbol *mips_elf_data_symbol_ptr;
3033 /* Handle the special MIPS section numbers that a symbol may use.
3034 This is used for both the 32-bit and the 64-bit ABI. */
3037 _bfd_mips_elf_symbol_processing (abfd, asym)
3041 elf_symbol_type *elfsym;
3043 elfsym = (elf_symbol_type *) asym;
3044 switch (elfsym->internal_elf_sym.st_shndx)
3046 case SHN_MIPS_ACOMMON:
3047 /* This section is used in a dynamically linked executable file.
3048 It is an allocated common section. The dynamic linker can
3049 either resolve these symbols to something in a shared
3050 library, or it can just leave them here. For our purposes,
3051 we can consider these symbols to be in a new section. */
3052 if (mips_elf_acom_section.name == NULL)
3054 /* Initialize the acommon section. */
3055 mips_elf_acom_section.name = ".acommon";
3056 mips_elf_acom_section.flags = SEC_ALLOC;
3057 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3058 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3059 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3060 mips_elf_acom_symbol.name = ".acommon";
3061 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3062 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3063 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3065 asym->section = &mips_elf_acom_section;
3069 /* Common symbols less than the GP size are automatically
3070 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3071 if (asym->value > elf_gp_size (abfd)
3072 || IRIX_COMPAT (abfd) == ict_irix6)
3075 case SHN_MIPS_SCOMMON:
3076 if (mips_elf_scom_section.name == NULL)
3078 /* Initialize the small common section. */
3079 mips_elf_scom_section.name = ".scommon";
3080 mips_elf_scom_section.flags = SEC_IS_COMMON;
3081 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3082 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3083 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3084 mips_elf_scom_symbol.name = ".scommon";
3085 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3086 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3087 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3089 asym->section = &mips_elf_scom_section;
3090 asym->value = elfsym->internal_elf_sym.st_size;
3093 case SHN_MIPS_SUNDEFINED:
3094 asym->section = bfd_und_section_ptr;
3097 #if 0 /* for SGI_COMPAT */
3099 asym->section = mips_elf_text_section_ptr;
3103 asym->section = mips_elf_data_section_ptr;
3109 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3113 mips_elf_additional_program_headers (abfd)
3119 if (!SGI_COMPAT (abfd))
3122 /* See if we need a PT_MIPS_REGINFO segment. */
3123 s = bfd_get_section_by_name (abfd, ".reginfo");
3124 if (s && (s->flags & SEC_LOAD))
3127 /* See if we need a PT_MIPS_OPTIONS segment. */
3128 if (IRIX_COMPAT (abfd) == ict_irix6
3129 && bfd_get_section_by_name (abfd,
3130 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3133 /* See if we need a PT_MIPS_RTPROC segment. */
3134 if (IRIX_COMPAT (abfd) == ict_irix5
3135 && bfd_get_section_by_name (abfd, ".dynamic")
3136 && bfd_get_section_by_name (abfd, ".mdebug"))
3142 /* Modify the segment map for an Irix 5 executable. */
3145 mips_elf_modify_segment_map (abfd)
3149 struct elf_segment_map *m, **pm;
3151 if (! SGI_COMPAT (abfd))
3154 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3156 s = bfd_get_section_by_name (abfd, ".reginfo");
3157 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3159 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3160 if (m->p_type == PT_MIPS_REGINFO)
3164 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3168 m->p_type = PT_MIPS_REGINFO;
3172 /* We want to put it after the PHDR and INTERP segments. */
3173 pm = &elf_tdata (abfd)->segment_map;
3175 && ((*pm)->p_type == PT_PHDR
3176 || (*pm)->p_type == PT_INTERP))
3184 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3185 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3186 PT_OPTIONS segement immediately following the program header
3188 if (IRIX_COMPAT (abfd) == ict_irix6)
3192 for (s = abfd->sections; s; s = s->next)
3193 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3198 struct elf_segment_map *options_segment;
3200 for (m = elf_tdata (abfd)->segment_map; m; m = m->next)
3201 if (m->p_type == PT_PHDR)
3204 /* There should always be a program header table. */
3208 options_segment = bfd_zalloc (abfd,
3209 sizeof (struct elf_segment_map));
3210 options_segment->next = m->next;
3211 options_segment->p_type = PT_MIPS_OPTIONS;
3212 options_segment->p_flags = PF_R;
3213 options_segment->p_flags_valid = true;
3214 options_segment->count = 1;
3215 options_segment->sections[0] = s;
3216 m->next = options_segment;
3221 /* If there are .dynamic and .mdebug sections, we make a room
3222 for the RTPROC header. FIXME: Rewrite without section names. */
3223 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3224 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3225 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3227 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3228 if (m->p_type == PT_MIPS_RTPROC)
3232 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3236 m->p_type = PT_MIPS_RTPROC;
3238 s = bfd_get_section_by_name (abfd, ".rtproc");
3243 m->p_flags_valid = 1;
3251 /* We want to put it after the DYNAMIC segment. */
3252 pm = &elf_tdata (abfd)->segment_map;
3253 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3263 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3264 .dynstr, .dynsym, and .hash sections, and everything in
3266 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3267 if ((*pm)->p_type == PT_DYNAMIC)
3272 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3274 static const char *sec_names[] =
3275 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3278 struct elf_segment_map *n;
3282 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3284 s = bfd_get_section_by_name (abfd, sec_names[i]);
3285 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3291 sz = s->_cooked_size;
3294 if (high < s->vma + sz)
3300 for (s = abfd->sections; s != NULL; s = s->next)
3301 if ((s->flags & SEC_LOAD) != 0
3304 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3308 n = ((struct elf_segment_map *)
3309 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3316 for (s = abfd->sections; s != NULL; s = s->next)
3318 if ((s->flags & SEC_LOAD) != 0
3321 + (s->_cooked_size != 0 ?
3322 s->_cooked_size : s->_raw_size))
3337 /* The structure of the runtime procedure descriptor created by the
3338 loader for use by the static exception system. */
3340 typedef struct runtime_pdr {
3341 bfd_vma adr; /* memory address of start of procedure */
3342 long regmask; /* save register mask */
3343 long regoffset; /* save register offset */
3344 long fregmask; /* save floating point register mask */
3345 long fregoffset; /* save floating point register offset */
3346 long frameoffset; /* frame size */
3347 short framereg; /* frame pointer register */
3348 short pcreg; /* offset or reg of return pc */
3349 long irpss; /* index into the runtime string table */
3351 struct exception_info *exception_info;/* pointer to exception array */
3353 #define cbRPDR sizeof(RPDR)
3354 #define rpdNil ((pRPDR) 0)
3356 /* Swap RPDR (runtime procedure table entry) for output. */
3358 static void ecoff_swap_rpdr_out
3359 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3362 ecoff_swap_rpdr_out (abfd, in, ex)
3365 struct rpdr_ext *ex;
3367 /* ecoff_put_off was defined in ecoffswap.h. */
3368 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3369 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3370 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3371 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3372 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3373 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3375 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3376 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3378 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3380 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3384 /* Read ECOFF debugging information from a .mdebug section into a
3385 ecoff_debug_info structure. */
3388 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3391 struct ecoff_debug_info *debug;
3394 const struct ecoff_debug_swap *swap;
3395 char *ext_hdr = NULL;
3397 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3398 memset (debug, 0, sizeof(*debug));
3400 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3401 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3404 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3405 swap->external_hdr_size)
3409 symhdr = &debug->symbolic_header;
3410 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3412 /* The symbolic header contains absolute file offsets and sizes to
3414 #define READ(ptr, offset, count, size, type) \
3415 if (symhdr->count == 0) \
3416 debug->ptr = NULL; \
3419 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3420 if (debug->ptr == NULL) \
3421 goto error_return; \
3422 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3423 || (bfd_read (debug->ptr, size, symhdr->count, \
3424 abfd) != size * symhdr->count)) \
3425 goto error_return; \
3428 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3429 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3430 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3431 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3432 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3433 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3435 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3436 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3437 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3438 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3439 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3443 debug->adjust = NULL;
3448 if (ext_hdr != NULL)
3450 if (debug->line != NULL)
3452 if (debug->external_dnr != NULL)
3453 free (debug->external_dnr);
3454 if (debug->external_pdr != NULL)
3455 free (debug->external_pdr);
3456 if (debug->external_sym != NULL)
3457 free (debug->external_sym);
3458 if (debug->external_opt != NULL)
3459 free (debug->external_opt);
3460 if (debug->external_aux != NULL)
3461 free (debug->external_aux);
3462 if (debug->ss != NULL)
3464 if (debug->ssext != NULL)
3465 free (debug->ssext);
3466 if (debug->external_fdr != NULL)
3467 free (debug->external_fdr);
3468 if (debug->external_rfd != NULL)
3469 free (debug->external_rfd);
3470 if (debug->external_ext != NULL)
3471 free (debug->external_ext);
3475 /* MIPS ELF local labels start with '$', not 'L'. */
3479 mips_elf_is_local_label_name (abfd, name)
3486 /* On Irix 6, the labels go back to starting with '.', so we accept
3487 the generic ELF local label syntax as well. */
3488 return _bfd_elf_is_local_label_name (abfd, name);
3491 /* MIPS ELF uses a special find_nearest_line routine in order the
3492 handle the ECOFF debugging information. */
3494 struct mips_elf_find_line
3496 struct ecoff_debug_info d;
3497 struct ecoff_find_line i;
3501 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3502 functionname_ptr, line_ptr)
3507 const char **filename_ptr;
3508 const char **functionname_ptr;
3509 unsigned int *line_ptr;
3513 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3514 filename_ptr, functionname_ptr,
3518 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3519 filename_ptr, functionname_ptr,
3523 msec = bfd_get_section_by_name (abfd, ".mdebug");
3527 struct mips_elf_find_line *fi;
3528 const struct ecoff_debug_swap * const swap =
3529 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3531 /* If we are called during a link, mips_elf_final_link may have
3532 cleared the SEC_HAS_CONTENTS field. We force it back on here
3533 if appropriate (which it normally will be). */
3534 origflags = msec->flags;
3535 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3536 msec->flags |= SEC_HAS_CONTENTS;
3538 fi = elf_tdata (abfd)->find_line_info;
3541 bfd_size_type external_fdr_size;
3544 struct fdr *fdr_ptr;
3546 fi = ((struct mips_elf_find_line *)
3547 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3550 msec->flags = origflags;
3554 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3556 msec->flags = origflags;
3560 /* Swap in the FDR information. */
3561 fi->d.fdr = ((struct fdr *)
3563 (fi->d.symbolic_header.ifdMax *
3564 sizeof (struct fdr))));
3565 if (fi->d.fdr == NULL)
3567 msec->flags = origflags;
3570 external_fdr_size = swap->external_fdr_size;
3571 fdr_ptr = fi->d.fdr;
3572 fraw_src = (char *) fi->d.external_fdr;
3573 fraw_end = (fraw_src
3574 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3575 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3576 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3578 elf_tdata (abfd)->find_line_info = fi;
3580 /* Note that we don't bother to ever free this information.
3581 find_nearest_line is either called all the time, as in
3582 objdump -l, so the information should be saved, or it is
3583 rarely called, as in ld error messages, so the memory
3584 wasted is unimportant. Still, it would probably be a
3585 good idea for free_cached_info to throw it away. */
3588 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3589 &fi->i, filename_ptr, functionname_ptr,
3592 msec->flags = origflags;
3596 msec->flags = origflags;
3599 /* Fall back on the generic ELF find_nearest_line routine. */
3601 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3602 filename_ptr, functionname_ptr,
3606 /* The mips16 compiler uses a couple of special sections to handle
3607 floating point arguments.
3609 Section names that look like .mips16.fn.FNNAME contain stubs that
3610 copy floating point arguments from the fp regs to the gp regs and
3611 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3612 call should be redirected to the stub instead. If no 32 bit
3613 function calls FNNAME, the stub should be discarded. We need to
3614 consider any reference to the function, not just a call, because
3615 if the address of the function is taken we will need the stub,
3616 since the address might be passed to a 32 bit function.
3618 Section names that look like .mips16.call.FNNAME contain stubs
3619 that copy floating point arguments from the gp regs to the fp
3620 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3621 then any 16 bit function that calls FNNAME should be redirected
3622 to the stub instead. If FNNAME is not a 32 bit function, the
3623 stub should be discarded.
3625 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3626 which call FNNAME and then copy the return value from the fp regs
3627 to the gp regs. These stubs store the return value in $18 while
3628 calling FNNAME; any function which might call one of these stubs
3629 must arrange to save $18 around the call. (This case is not
3630 needed for 32 bit functions that call 16 bit functions, because
3631 16 bit functions always return floating point values in both
3634 Note that in all cases FNNAME might be defined statically.
3635 Therefore, FNNAME is not used literally. Instead, the relocation
3636 information will indicate which symbol the section is for.
3638 We record any stubs that we find in the symbol table. */
3640 #define FN_STUB ".mips16.fn."
3641 #define CALL_STUB ".mips16.call."
3642 #define CALL_FP_STUB ".mips16.call.fp."
3644 /* MIPS ELF linker hash table. */
3646 struct mips_elf_link_hash_table
3648 struct elf_link_hash_table root;
3650 /* We no longer use this. */
3651 /* String section indices for the dynamic section symbols. */
3652 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3654 /* The number of .rtproc entries. */
3655 bfd_size_type procedure_count;
3656 /* The size of the .compact_rel section (if SGI_COMPAT). */
3657 bfd_size_type compact_rel_size;
3658 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3659 entry is set to the address of __rld_obj_head as in Irix 5. */
3660 boolean use_rld_obj_head;
3661 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3663 /* This is set if we see any mips16 stub sections. */
3664 boolean mips16_stubs_seen;
3667 /* Look up an entry in a MIPS ELF linker hash table. */
3669 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3670 ((struct mips_elf_link_hash_entry *) \
3671 elf_link_hash_lookup (&(table)->root, (string), (create), \
3674 /* Traverse a MIPS ELF linker hash table. */
3676 #define mips_elf_link_hash_traverse(table, func, info) \
3677 (elf_link_hash_traverse \
3679 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3682 /* Get the MIPS ELF linker hash table from a link_info structure. */
3684 #define mips_elf_hash_table(p) \
3685 ((struct mips_elf_link_hash_table *) ((p)->hash))
3687 static boolean mips_elf_output_extsym
3688 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3690 /* Create an entry in a MIPS ELF linker hash table. */
3692 static struct bfd_hash_entry *
3693 mips_elf_link_hash_newfunc (entry, table, string)
3694 struct bfd_hash_entry *entry;
3695 struct bfd_hash_table *table;
3698 struct mips_elf_link_hash_entry *ret =
3699 (struct mips_elf_link_hash_entry *) entry;
3701 /* Allocate the structure if it has not already been allocated by a
3703 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3704 ret = ((struct mips_elf_link_hash_entry *)
3705 bfd_hash_allocate (table,
3706 sizeof (struct mips_elf_link_hash_entry)));
3707 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3708 return (struct bfd_hash_entry *) ret;
3710 /* Call the allocation method of the superclass. */
3711 ret = ((struct mips_elf_link_hash_entry *)
3712 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3714 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3716 /* Set local fields. */
3717 memset (&ret->esym, 0, sizeof (EXTR));
3718 /* We use -2 as a marker to indicate that the information has
3719 not been set. -1 means there is no associated ifd. */
3721 ret->mips_32_relocs = 0;
3722 ret->min_dyn_reloc_index = 0;
3723 ret->fn_stub = NULL;
3724 ret->need_fn_stub = false;
3725 ret->call_stub = NULL;
3726 ret->call_fp_stub = NULL;
3729 return (struct bfd_hash_entry *) ret;
3732 /* Create a MIPS ELF linker hash table. */
3734 static struct bfd_link_hash_table *
3735 mips_elf_link_hash_table_create (abfd)
3738 struct mips_elf_link_hash_table *ret;
3740 ret = ((struct mips_elf_link_hash_table *)
3741 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3742 if (ret == (struct mips_elf_link_hash_table *) NULL)
3745 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3746 mips_elf_link_hash_newfunc))
3748 bfd_release (abfd, ret);
3753 /* We no longer use this. */
3754 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3755 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3757 ret->procedure_count = 0;
3758 ret->compact_rel_size = 0;
3759 ret->use_rld_obj_head = false;
3761 ret->mips16_stubs_seen = false;
3763 return &ret->root.root;
3766 /* Hook called by the linker routine which adds symbols from an object
3767 file. We must handle the special MIPS section numbers here. */
3771 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3773 struct bfd_link_info *info;
3774 const Elf_Internal_Sym *sym;
3780 if (SGI_COMPAT (abfd)
3781 && (abfd->flags & DYNAMIC) != 0
3782 && strcmp (*namep, "_rld_new_interface") == 0)
3784 /* Skip Irix 5 rld entry name. */
3789 switch (sym->st_shndx)
3792 /* Common symbols less than the GP size are automatically
3793 treated as SHN_MIPS_SCOMMON symbols. */
3794 if (sym->st_size > elf_gp_size (abfd)
3795 || IRIX_COMPAT (abfd) == ict_irix6)
3798 case SHN_MIPS_SCOMMON:
3799 *secp = bfd_make_section_old_way (abfd, ".scommon");
3800 (*secp)->flags |= SEC_IS_COMMON;
3801 *valp = sym->st_size;
3805 /* This section is used in a shared object. */
3806 if (mips_elf_text_section_ptr == NULL)
3808 /* Initialize the section. */
3809 mips_elf_text_section.name = ".text";
3810 mips_elf_text_section.flags = SEC_NO_FLAGS;
3811 mips_elf_text_section.output_section = NULL;
3812 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3813 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3814 mips_elf_text_symbol.name = ".text";
3815 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3816 mips_elf_text_symbol.section = &mips_elf_text_section;
3817 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3818 mips_elf_text_section_ptr = &mips_elf_text_section;
3820 /* This code used to do *secp = bfd_und_section_ptr if
3821 info->shared. I don't know why, and that doesn't make sense,
3822 so I took it out. */
3823 *secp = mips_elf_text_section_ptr;
3826 case SHN_MIPS_ACOMMON:
3827 /* Fall through. XXX Can we treat this as allocated data? */
3829 /* This section is used in a shared object. */
3830 if (mips_elf_data_section_ptr == NULL)
3832 /* Initialize the section. */
3833 mips_elf_data_section.name = ".data";
3834 mips_elf_data_section.flags = SEC_NO_FLAGS;
3835 mips_elf_data_section.output_section = NULL;
3836 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3837 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3838 mips_elf_data_symbol.name = ".data";
3839 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3840 mips_elf_data_symbol.section = &mips_elf_data_section;
3841 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3842 mips_elf_data_section_ptr = &mips_elf_data_section;
3844 /* This code used to do *secp = bfd_und_section_ptr if
3845 info->shared. I don't know why, and that doesn't make sense,
3846 so I took it out. */
3847 *secp = mips_elf_data_section_ptr;
3850 case SHN_MIPS_SUNDEFINED:
3851 *secp = bfd_und_section_ptr;
3855 if (SGI_COMPAT (abfd)
3857 && info->hash->creator == abfd->xvec
3858 && strcmp (*namep, "__rld_obj_head") == 0)
3860 struct elf_link_hash_entry *h;
3862 /* Mark __rld_obj_head as dynamic. */
3864 if (! (_bfd_generic_link_add_one_symbol
3865 (info, abfd, *namep, BSF_GLOBAL, *secp,
3866 (bfd_vma) *valp, (const char *) NULL, false,
3867 get_elf_backend_data (abfd)->collect,
3868 (struct bfd_link_hash_entry **) &h)))
3870 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3871 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3872 h->type = STT_OBJECT;
3874 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3877 mips_elf_hash_table (info)->use_rld_obj_head = true;
3880 /* If this is a mips16 text symbol, add 1 to the value to make it
3881 odd. This will cause something like .word SYM to come up with
3882 the right value when it is loaded into the PC. */
3883 if (sym->st_other == STO_MIPS16)
3889 /* Structure used to pass information to mips_elf_output_extsym. */
3894 struct bfd_link_info *info;
3895 struct ecoff_debug_info *debug;
3896 const struct ecoff_debug_swap *swap;
3900 /* This routine is used to write out ECOFF debugging external symbol
3901 information. It is called via mips_elf_link_hash_traverse. The
3902 ECOFF external symbol information must match the ELF external
3903 symbol information. Unfortunately, at this point we don't know
3904 whether a symbol is required by reloc information, so the two
3905 tables may wind up being different. We must sort out the external
3906 symbol information before we can set the final size of the .mdebug
3907 section, and we must set the size of the .mdebug section before we
3908 can relocate any sections, and we can't know which symbols are
3909 required by relocation until we relocate the sections.
3910 Fortunately, it is relatively unlikely that any symbol will be
3911 stripped but required by a reloc. In particular, it can not happen
3912 when generating a final executable. */
3915 mips_elf_output_extsym (h, data)
3916 struct mips_elf_link_hash_entry *h;
3919 struct extsym_info *einfo = (struct extsym_info *) data;
3921 asection *sec, *output_section;
3923 if (h->root.indx == -2)
3925 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3926 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3927 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3928 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3930 else if (einfo->info->strip == strip_all
3931 || (einfo->info->strip == strip_some
3932 && bfd_hash_lookup (einfo->info->keep_hash,
3933 h->root.root.root.string,
3934 false, false) == NULL))
3942 if (h->esym.ifd == -2)
3945 h->esym.cobol_main = 0;
3946 h->esym.weakext = 0;
3947 h->esym.reserved = 0;
3948 h->esym.ifd = ifdNil;
3949 h->esym.asym.value = 0;
3950 h->esym.asym.st = stGlobal;
3952 if (SGI_COMPAT (einfo->abfd)
3953 && (h->root.root.type == bfd_link_hash_undefined
3954 || h->root.root.type == bfd_link_hash_undefweak))
3958 /* Use undefined class. Also, set class and type for some
3960 name = h->root.root.root.string;
3961 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3962 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3964 h->esym.asym.sc = scData;
3965 h->esym.asym.st = stLabel;
3966 h->esym.asym.value = 0;
3968 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3970 h->esym.asym.sc = scAbs;
3971 h->esym.asym.st = stLabel;
3972 h->esym.asym.value =
3973 mips_elf_hash_table (einfo->info)->procedure_count;
3975 else if (strcmp (name, "_gp_disp") == 0)
3977 h->esym.asym.sc = scAbs;
3978 h->esym.asym.st = stLabel;
3979 h->esym.asym.value = elf_gp (einfo->abfd);
3982 h->esym.asym.sc = scUndefined;
3984 else if (h->root.root.type != bfd_link_hash_defined
3985 && h->root.root.type != bfd_link_hash_defweak)
3986 h->esym.asym.sc = scAbs;
3991 sec = h->root.root.u.def.section;
3992 output_section = sec->output_section;
3994 /* When making a shared library and symbol h is the one from
3995 the another shared library, OUTPUT_SECTION may be null. */
3996 if (output_section == NULL)
3997 h->esym.asym.sc = scUndefined;
4000 name = bfd_section_name (output_section->owner, output_section);
4002 if (strcmp (name, ".text") == 0)
4003 h->esym.asym.sc = scText;
4004 else if (strcmp (name, ".data") == 0)
4005 h->esym.asym.sc = scData;
4006 else if (strcmp (name, ".sdata") == 0)
4007 h->esym.asym.sc = scSData;
4008 else if (strcmp (name, ".rodata") == 0
4009 || strcmp (name, ".rdata") == 0)
4010 h->esym.asym.sc = scRData;
4011 else if (strcmp (name, ".bss") == 0)
4012 h->esym.asym.sc = scBss;
4013 else if (strcmp (name, ".sbss") == 0)
4014 h->esym.asym.sc = scSBss;
4015 else if (strcmp (name, ".init") == 0)
4016 h->esym.asym.sc = scInit;
4017 else if (strcmp (name, ".fini") == 0)
4018 h->esym.asym.sc = scFini;
4020 h->esym.asym.sc = scAbs;
4024 h->esym.asym.reserved = 0;
4025 h->esym.asym.index = indexNil;
4028 if (h->root.root.type == bfd_link_hash_common)
4029 h->esym.asym.value = h->root.root.u.c.size;
4030 else if (h->root.root.type == bfd_link_hash_defined
4031 || h->root.root.type == bfd_link_hash_defweak)
4033 if (h->esym.asym.sc == scCommon)
4034 h->esym.asym.sc = scBss;
4035 else if (h->esym.asym.sc == scSCommon)
4036 h->esym.asym.sc = scSBss;
4038 sec = h->root.root.u.def.section;
4039 output_section = sec->output_section;
4040 if (output_section != NULL)
4041 h->esym.asym.value = (h->root.root.u.def.value
4042 + sec->output_offset
4043 + output_section->vma);
4045 h->esym.asym.value = 0;
4047 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4049 /* Set type and value for a symbol with a function stub. */
4050 h->esym.asym.st = stProc;
4051 sec = h->root.root.u.def.section;
4053 h->esym.asym.value = 0;
4056 output_section = sec->output_section;
4057 if (output_section != NULL)
4058 h->esym.asym.value = (h->root.plt.offset
4059 + sec->output_offset
4060 + output_section->vma);
4062 h->esym.asym.value = 0;
4069 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4070 h->root.root.root.string,
4073 einfo->failed = true;
4080 /* Create a runtime procedure table from the .mdebug section. */
4083 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4086 struct bfd_link_info *info;
4088 struct ecoff_debug_info *debug;
4090 const struct ecoff_debug_swap *swap;
4091 HDRR *hdr = &debug->symbolic_header;
4093 struct rpdr_ext *erp;
4095 struct pdr_ext *epdr;
4096 struct sym_ext *esym;
4099 unsigned long size, count;
4100 unsigned long sindex;
4104 const char *no_name_func = _("static procedure (no name)");
4112 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4114 sindex = strlen (no_name_func) + 1;
4115 count = hdr->ipdMax;
4118 size = swap->external_pdr_size;
4120 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4124 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4127 size = sizeof (RPDR);
4128 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4132 sv = (char **) bfd_malloc (sizeof (char *) * count);
4136 count = hdr->isymMax;
4137 size = swap->external_sym_size;
4138 esym = (struct sym_ext *) bfd_malloc (size * count);
4142 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4145 count = hdr->issMax;
4146 ss = (char *) bfd_malloc (count);
4149 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4152 count = hdr->ipdMax;
4153 for (i = 0; i < count; i++, rp++)
4155 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4156 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4157 rp->adr = sym.value;
4158 rp->regmask = pdr.regmask;
4159 rp->regoffset = pdr.regoffset;
4160 rp->fregmask = pdr.fregmask;
4161 rp->fregoffset = pdr.fregoffset;
4162 rp->frameoffset = pdr.frameoffset;
4163 rp->framereg = pdr.framereg;
4164 rp->pcreg = pdr.pcreg;
4166 sv[i] = ss + sym.iss;
4167 sindex += strlen (sv[i]) + 1;
4171 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4172 size = BFD_ALIGN (size, 16);
4173 rtproc = (PTR) bfd_alloc (abfd, size);
4176 mips_elf_hash_table (info)->procedure_count = 0;
4180 mips_elf_hash_table (info)->procedure_count = count + 2;
4182 erp = (struct rpdr_ext *) rtproc;
4183 memset (erp, 0, sizeof (struct rpdr_ext));
4185 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4186 strcpy (str, no_name_func);
4187 str += strlen (no_name_func) + 1;
4188 for (i = 0; i < count; i++)
4190 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4191 strcpy (str, sv[i]);
4192 str += strlen (sv[i]) + 1;
4194 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
4196 /* Set the size and contents of .rtproc section. */
4197 s->_raw_size = size;
4198 s->contents = (bfd_byte *) rtproc;
4200 /* Skip this section later on (I don't think this currently
4201 matters, but someday it might). */
4202 s->link_order_head = (struct bfd_link_order *) NULL;
4231 /* A comparison routine used to sort .gptab entries. */
4234 gptab_compare (p1, p2)
4238 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4239 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4241 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4244 /* We need to use a special link routine to handle the .reginfo and
4245 the .mdebug sections. We need to merge all instances of these
4246 sections together, not write them all out sequentially. */
4249 mips_elf_final_link (abfd, info)
4251 struct bfd_link_info *info;
4255 struct bfd_link_order *p;
4256 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4257 asection *rtproc_sec;
4258 Elf32_RegInfo reginfo;
4259 struct ecoff_debug_info debug;
4260 const struct ecoff_debug_swap *swap
4261 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4262 HDRR *symhdr = &debug.symbolic_header;
4263 PTR mdebug_handle = NULL;
4265 /* If all the things we linked together were PIC, but we're
4266 producing an executable (rather than a shared object), then the
4267 resulting file is CPIC (i.e., it calls PIC code.) */
4269 && !info->relocateable
4270 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4272 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4273 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4276 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4277 include it, even though we don't process it quite right. (Some
4278 entries are supposed to be merged.) Empirically, we seem to be
4279 better off including it then not. */
4280 if (IRIX_COMPAT (abfd) == ict_irix5)
4281 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4283 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4285 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4286 if (p->type == bfd_indirect_link_order)
4287 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4288 (*secpp)->link_order_head = NULL;
4289 *secpp = (*secpp)->next;
4290 --abfd->section_count;
4296 /* Get a value for the GP register. */
4297 if (elf_gp (abfd) == 0)
4299 struct bfd_link_hash_entry *h;
4301 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4302 if (h != (struct bfd_link_hash_entry *) NULL
4303 && h->type == bfd_link_hash_defined)
4304 elf_gp (abfd) = (h->u.def.value
4305 + h->u.def.section->output_section->vma
4306 + h->u.def.section->output_offset);
4307 else if (info->relocateable)
4311 /* Find the GP-relative section with the lowest offset. */
4313 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4315 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4318 /* And calculate GP relative to that. */
4319 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4323 /* If the relocate_section function needs to do a reloc
4324 involving the GP value, it should make a reloc_dangerous
4325 callback to warn that GP is not defined. */
4329 /* Go through the sections and collect the .reginfo and .mdebug
4333 gptab_data_sec = NULL;
4334 gptab_bss_sec = NULL;
4335 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4337 if (strcmp (o->name, ".reginfo") == 0)
4339 memset (®info, 0, sizeof reginfo);
4341 /* We have found the .reginfo section in the output file.
4342 Look through all the link_orders comprising it and merge
4343 the information together. */
4344 for (p = o->link_order_head;
4345 p != (struct bfd_link_order *) NULL;
4348 asection *input_section;
4350 Elf32_External_RegInfo ext;
4353 if (p->type != bfd_indirect_link_order)
4355 if (p->type == bfd_fill_link_order)
4360 input_section = p->u.indirect.section;
4361 input_bfd = input_section->owner;
4363 /* The linker emulation code has probably clobbered the
4364 size to be zero bytes. */
4365 if (input_section->_raw_size == 0)
4366 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4368 if (! bfd_get_section_contents (input_bfd, input_section,
4374 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4376 reginfo.ri_gprmask |= sub.ri_gprmask;
4377 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4378 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4379 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4380 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4382 /* ri_gp_value is set by the function
4383 mips_elf32_section_processing when the section is
4384 finally written out. */
4386 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4387 elf_link_input_bfd ignores this section. */
4388 input_section->flags &=~ SEC_HAS_CONTENTS;
4391 /* Size has been set in mips_elf_always_size_sections */
4392 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4394 /* Skip this section later on (I don't think this currently
4395 matters, but someday it might). */
4396 o->link_order_head = (struct bfd_link_order *) NULL;
4401 if (strcmp (o->name, ".mdebug") == 0)
4403 struct extsym_info einfo;
4405 /* We have found the .mdebug section in the output file.
4406 Look through all the link_orders comprising it and merge
4407 the information together. */
4408 symhdr->magic = swap->sym_magic;
4409 /* FIXME: What should the version stamp be? */
4411 symhdr->ilineMax = 0;
4415 symhdr->isymMax = 0;
4416 symhdr->ioptMax = 0;
4417 symhdr->iauxMax = 0;
4419 symhdr->issExtMax = 0;
4422 symhdr->iextMax = 0;
4424 /* We accumulate the debugging information itself in the
4425 debug_info structure. */
4427 debug.external_dnr = NULL;
4428 debug.external_pdr = NULL;
4429 debug.external_sym = NULL;
4430 debug.external_opt = NULL;
4431 debug.external_aux = NULL;
4433 debug.ssext = debug.ssext_end = NULL;
4434 debug.external_fdr = NULL;
4435 debug.external_rfd = NULL;
4436 debug.external_ext = debug.external_ext_end = NULL;
4438 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4439 if (mdebug_handle == (PTR) NULL)
4442 if (SGI_COMPAT (abfd))
4448 static const char * const name[] =
4449 { ".text", ".init", ".fini", ".data",
4450 ".rodata", ".sdata", ".sbss", ".bss" };
4451 static const int sc[] = { scText, scInit, scFini, scData,
4452 scRData, scSData, scSBss, scBss };
4455 esym.cobol_main = 0;
4459 esym.asym.iss = issNil;
4460 esym.asym.st = stLocal;
4461 esym.asym.reserved = 0;
4462 esym.asym.index = indexNil;
4464 for (i = 0; i < 8; i++)
4466 esym.asym.sc = sc[i];
4467 s = bfd_get_section_by_name (abfd, name[i]);
4470 esym.asym.value = s->vma;
4471 last = s->vma + s->_raw_size;
4474 esym.asym.value = last;
4476 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4482 for (p = o->link_order_head;
4483 p != (struct bfd_link_order *) NULL;
4486 asection *input_section;
4488 const struct ecoff_debug_swap *input_swap;
4489 struct ecoff_debug_info input_debug;
4493 if (p->type != bfd_indirect_link_order)
4495 if (p->type == bfd_fill_link_order)
4500 input_section = p->u.indirect.section;
4501 input_bfd = input_section->owner;
4503 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4504 || (get_elf_backend_data (input_bfd)
4505 ->elf_backend_ecoff_debug_swap) == NULL)
4507 /* I don't know what a non MIPS ELF bfd would be
4508 doing with a .mdebug section, but I don't really
4509 want to deal with it. */
4513 input_swap = (get_elf_backend_data (input_bfd)
4514 ->elf_backend_ecoff_debug_swap);
4516 BFD_ASSERT (p->size == input_section->_raw_size);
4518 /* The ECOFF linking code expects that we have already
4519 read in the debugging information and set up an
4520 ecoff_debug_info structure, so we do that now. */
4521 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4525 if (! (bfd_ecoff_debug_accumulate
4526 (mdebug_handle, abfd, &debug, swap, input_bfd,
4527 &input_debug, input_swap, info)))
4530 /* Loop through the external symbols. For each one with
4531 interesting information, try to find the symbol in
4532 the linker global hash table and save the information
4533 for the output external symbols. */
4534 eraw_src = input_debug.external_ext;
4535 eraw_end = (eraw_src
4536 + (input_debug.symbolic_header.iextMax
4537 * input_swap->external_ext_size));
4539 eraw_src < eraw_end;
4540 eraw_src += input_swap->external_ext_size)
4544 struct mips_elf_link_hash_entry *h;
4546 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4547 if (ext.asym.sc == scNil
4548 || ext.asym.sc == scUndefined
4549 || ext.asym.sc == scSUndefined)
4552 name = input_debug.ssext + ext.asym.iss;
4553 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4554 name, false, false, true);
4555 if (h == NULL || h->esym.ifd != -2)
4561 < input_debug.symbolic_header.ifdMax);
4562 ext.ifd = input_debug.ifdmap[ext.ifd];
4568 /* Free up the information we just read. */
4569 free (input_debug.line);
4570 free (input_debug.external_dnr);
4571 free (input_debug.external_pdr);
4572 free (input_debug.external_sym);
4573 free (input_debug.external_opt);
4574 free (input_debug.external_aux);
4575 free (input_debug.ss);
4576 free (input_debug.ssext);
4577 free (input_debug.external_fdr);
4578 free (input_debug.external_rfd);
4579 free (input_debug.external_ext);
4581 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4582 elf_link_input_bfd ignores this section. */
4583 input_section->flags &=~ SEC_HAS_CONTENTS;
4586 if (SGI_COMPAT (abfd) && info->shared)
4588 /* Create .rtproc section. */
4589 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4590 if (rtproc_sec == NULL)
4592 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4593 | SEC_LINKER_CREATED | SEC_READONLY);
4595 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4596 if (rtproc_sec == NULL
4597 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4598 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4602 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4603 info, rtproc_sec, &debug))
4607 /* Build the external symbol information. */
4610 einfo.debug = &debug;
4612 einfo.failed = false;
4613 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4614 mips_elf_output_extsym,
4619 /* Set the size of the .mdebug section. */
4620 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4622 /* Skip this section later on (I don't think this currently
4623 matters, but someday it might). */
4624 o->link_order_head = (struct bfd_link_order *) NULL;
4629 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4631 const char *subname;
4634 Elf32_External_gptab *ext_tab;
4637 /* The .gptab.sdata and .gptab.sbss sections hold
4638 information describing how the small data area would
4639 change depending upon the -G switch. These sections
4640 not used in executables files. */
4641 if (! info->relocateable)
4645 for (p = o->link_order_head;
4646 p != (struct bfd_link_order *) NULL;
4649 asection *input_section;
4651 if (p->type != bfd_indirect_link_order)
4653 if (p->type == bfd_fill_link_order)
4658 input_section = p->u.indirect.section;
4660 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4661 elf_link_input_bfd ignores this section. */
4662 input_section->flags &=~ SEC_HAS_CONTENTS;
4665 /* Skip this section later on (I don't think this
4666 currently matters, but someday it might). */
4667 o->link_order_head = (struct bfd_link_order *) NULL;
4669 /* Really remove the section. */
4670 for (secpp = &abfd->sections;
4672 secpp = &(*secpp)->next)
4674 *secpp = (*secpp)->next;
4675 --abfd->section_count;
4680 /* There is one gptab for initialized data, and one for
4681 uninitialized data. */
4682 if (strcmp (o->name, ".gptab.sdata") == 0)
4684 else if (strcmp (o->name, ".gptab.sbss") == 0)
4688 (*_bfd_error_handler)
4689 (_("%s: illegal section name `%s'"),
4690 bfd_get_filename (abfd), o->name);
4691 bfd_set_error (bfd_error_nonrepresentable_section);
4695 /* The linker script always combines .gptab.data and
4696 .gptab.sdata into .gptab.sdata, and likewise for
4697 .gptab.bss and .gptab.sbss. It is possible that there is
4698 no .sdata or .sbss section in the output file, in which
4699 case we must change the name of the output section. */
4700 subname = o->name + sizeof ".gptab" - 1;
4701 if (bfd_get_section_by_name (abfd, subname) == NULL)
4703 if (o == gptab_data_sec)
4704 o->name = ".gptab.data";
4706 o->name = ".gptab.bss";
4707 subname = o->name + sizeof ".gptab" - 1;
4708 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4711 /* Set up the first entry. */
4713 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4716 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4717 tab[0].gt_header.gt_unused = 0;
4719 /* Combine the input sections. */
4720 for (p = o->link_order_head;
4721 p != (struct bfd_link_order *) NULL;
4724 asection *input_section;
4728 bfd_size_type gpentry;
4730 if (p->type != bfd_indirect_link_order)
4732 if (p->type == bfd_fill_link_order)
4737 input_section = p->u.indirect.section;
4738 input_bfd = input_section->owner;
4740 /* Combine the gptab entries for this input section one
4741 by one. We know that the input gptab entries are
4742 sorted by ascending -G value. */
4743 size = bfd_section_size (input_bfd, input_section);
4745 for (gpentry = sizeof (Elf32_External_gptab);
4747 gpentry += sizeof (Elf32_External_gptab))
4749 Elf32_External_gptab ext_gptab;
4750 Elf32_gptab int_gptab;
4756 if (! (bfd_get_section_contents
4757 (input_bfd, input_section, (PTR) &ext_gptab,
4758 gpentry, sizeof (Elf32_External_gptab))))
4764 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4766 val = int_gptab.gt_entry.gt_g_value;
4767 add = int_gptab.gt_entry.gt_bytes - last;
4770 for (look = 1; look < c; look++)
4772 if (tab[look].gt_entry.gt_g_value >= val)
4773 tab[look].gt_entry.gt_bytes += add;
4775 if (tab[look].gt_entry.gt_g_value == val)
4781 Elf32_gptab *new_tab;
4784 /* We need a new table entry. */
4785 new_tab = ((Elf32_gptab *)
4786 bfd_realloc ((PTR) tab,
4787 (c + 1) * sizeof (Elf32_gptab)));
4788 if (new_tab == NULL)
4794 tab[c].gt_entry.gt_g_value = val;
4795 tab[c].gt_entry.gt_bytes = add;
4797 /* Merge in the size for the next smallest -G
4798 value, since that will be implied by this new
4801 for (look = 1; look < c; look++)
4803 if (tab[look].gt_entry.gt_g_value < val
4805 || (tab[look].gt_entry.gt_g_value
4806 > tab[max].gt_entry.gt_g_value)))
4810 tab[c].gt_entry.gt_bytes +=
4811 tab[max].gt_entry.gt_bytes;
4816 last = int_gptab.gt_entry.gt_bytes;
4819 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4820 elf_link_input_bfd ignores this section. */
4821 input_section->flags &=~ SEC_HAS_CONTENTS;
4824 /* The table must be sorted by -G value. */
4826 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4828 /* Swap out the table. */
4829 ext_tab = ((Elf32_External_gptab *)
4830 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4831 if (ext_tab == NULL)
4837 for (i = 0; i < c; i++)
4838 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4841 o->_raw_size = c * sizeof (Elf32_External_gptab);
4842 o->contents = (bfd_byte *) ext_tab;
4844 /* Skip this section later on (I don't think this currently
4845 matters, but someday it might). */
4846 o->link_order_head = (struct bfd_link_order *) NULL;
4850 /* Invoke the regular ELF backend linker to do all the work. */
4851 if (! bfd_elf32_bfd_final_link (abfd, info))
4854 /* Now write out the computed sections. */
4856 if (reginfo_sec != (asection *) NULL)
4858 Elf32_External_RegInfo ext;
4860 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
4861 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4862 (file_ptr) 0, sizeof ext))
4866 if (mdebug_sec != (asection *) NULL)
4868 BFD_ASSERT (abfd->output_has_begun);
4869 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4871 mdebug_sec->filepos))
4874 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4877 if (gptab_data_sec != (asection *) NULL)
4879 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4880 gptab_data_sec->contents,
4882 gptab_data_sec->_raw_size))
4886 if (gptab_bss_sec != (asection *) NULL)
4888 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4889 gptab_bss_sec->contents,
4891 gptab_bss_sec->_raw_size))
4895 if (SGI_COMPAT (abfd))
4897 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4898 if (rtproc_sec != NULL)
4900 if (! bfd_set_section_contents (abfd, rtproc_sec,
4901 rtproc_sec->contents,
4903 rtproc_sec->_raw_size))
4911 /* Handle a MIPS ELF HI16 reloc. */
4914 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4916 Elf_Internal_Rela *relhi;
4917 Elf_Internal_Rela *rello;
4924 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4926 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4929 addend += ((insn & 0xffff) << 16) + addlo;
4931 if ((addlo & 0x8000) != 0)
4933 if ((addend & 0x8000) != 0)
4936 bfd_put_32 (input_bfd,
4937 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4938 contents + relhi->r_offset);
4941 /* Handle a MIPS ELF local GOT16 reloc. */
4944 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4949 Elf_Internal_Rela *relhi;
4950 Elf_Internal_Rela *rello;
4954 unsigned int assigned_gotno;
4960 bfd_byte *got_contents;
4961 struct mips_got_info *g;
4963 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4965 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4968 addend += ((insn & 0xffff) << 16) + addlo;
4970 if ((addlo & 0x8000) != 0)
4972 if ((addend & 0x8000) != 0)
4975 /* Get a got entry representing requested hipage. */
4976 BFD_ASSERT (elf_section_data (sgot) != NULL);
4977 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4978 BFD_ASSERT (g != NULL);
4980 assigned_gotno = g->assigned_gotno;
4981 got_contents = sgot->contents;
4982 hipage = addend & 0xffff0000;
4984 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4986 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4987 if (hipage == (address & 0xffff0000))
4991 if (i == assigned_gotno)
4993 if (assigned_gotno >= g->local_gotno)
4995 (*_bfd_error_handler)
4996 (_("more got entries are needed for hipage relocations"));
4997 bfd_set_error (bfd_error_bad_value);
5001 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
5002 ++g->assigned_gotno;
5005 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
5006 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
5007 contents + relhi->r_offset);
5012 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
5015 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
5017 Elf_Internal_Rela *rel;
5023 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5024 bfd_put_32 (input_bfd,
5025 (insn & 0xffff0000) | (offset & 0xffff),
5026 contents + rel->r_offset);
5029 /* Returns the GOT section for ABFD. */
5032 mips_elf_got_section (abfd)
5035 return bfd_get_section_by_name (abfd, ".got");
5038 /* Returns the GOT information associated with the link indicated by
5039 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5042 static struct mips_got_info *
5043 mips_elf_got_info (abfd, sgotp)
5048 struct mips_got_info *g;
5050 sgot = mips_elf_got_section (abfd);
5051 BFD_ASSERT (sgot != NULL);
5052 BFD_ASSERT (elf_section_data (sgot) != NULL);
5053 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5054 BFD_ASSERT (g != NULL);
5061 /* Sign-extend VALUE, which has the indicated number of BITS. */
5064 mips_elf_sign_extend (value, bits)
5068 if (value & (1 << (bits - 1)))
5069 /* VALUE is negative. */
5070 value |= ((bfd_vma) - 1) << bits;
5075 /* Return non-zero if the indicated VALUE has overflowed the maximum
5076 range expressable by a signed number with the indicated number of
5080 mips_elf_overflow_p (value, bits)
5084 bfd_signed_vma svalue = (bfd_signed_vma) value;
5086 if (svalue > (1 << (bits - 1)) - 1)
5087 /* The value is too big. */
5089 else if (svalue < -(1 << (bits - 1)))
5090 /* The value is too small. */
5097 /* Calculate the %high function. */
5100 mips_elf_high (value)
5103 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5106 /* Calculate the %higher function. */
5109 mips_elf_higher (value)
5113 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5116 return (bfd_vma) -1;
5120 /* Calculate the %highest function. */
5123 mips_elf_highest (value)
5127 return ((value + (bfd_vma) 0x800080008000) > 48) & 0xffff;
5130 return (bfd_vma) -1;
5134 /* Returns the GOT index for the global symbol indicated by H. */
5137 mips_elf_global_got_index (abfd, h)
5139 struct elf_link_hash_entry *h;
5143 struct mips_got_info *g;
5145 g = mips_elf_got_info (abfd, &sgot);
5147 /* Once we determine the global GOT entry with the lowest dynamic
5148 symbol table index, we must put all dynamic symbols with greater
5149 indices into the GOT. That makes it easy to calculate the GOT
5151 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5152 index = (h->dynindx - g->global_gotsym->dynindx + g->local_gotno) * 4;
5153 BFD_ASSERT (index < sgot->_raw_size);
5158 /* Returns the offset for the entry at the INDEXth position
5162 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5169 char *error_message;
5171 sgot = bfd_get_section_by_name (dynobj, ".got");
5172 gp = _bfd_get_gp_value (output_bfd);
5173 return (sgot->output_section->vma + sgot->output_offset + index -
5177 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5178 symbol table index lower than any we've seen to date, record it for
5182 mips_elf_record_global_got_symbol (h, info, g)
5183 struct elf_link_hash_entry *h;
5184 struct bfd_link_info *info;
5185 struct mips_got_info *g;
5187 /* A global symbol in the GOT must also be in the dynamic symbol
5189 if (h->dynindx == -1
5190 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5193 /* If we've already marked this entry as need GOT space, we don't
5194 need to do it again. */
5195 if (h->got.offset != (bfd_vma) - 1)
5198 /* By setting this to a value other than -1, we are indicating that
5199 there needs to be a GOT entry for H. */
5205 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5206 the dynamic symbols. */
5208 struct mips_elf_hash_sort_data
5210 /* The symbol in the global GOT with the lowest dynamic symbol table
5212 struct elf_link_hash_entry *low;
5213 /* The least dynamic symbol table index corresponding to a symbol
5214 with a GOT entry. */
5215 long min_got_dynindx;
5216 /* The greatest dynamic symbol table index not corresponding to a
5217 symbol without a GOT entry. */
5218 long max_non_got_dynindx;
5221 /* If H needs a GOT entry, assign it the highest available dynamic
5222 index. Otherwise, assign it the lowest available dynamic
5226 mips_elf_sort_hash_table_f (h, data)
5227 struct mips_elf_link_hash_entry *h;
5230 struct mips_elf_hash_sort_data *hsd
5231 = (struct mips_elf_hash_sort_data *) data;
5233 /* Symbols without dynamic symbol table entries aren't interesting
5235 if (h->root.dynindx == -1)
5238 if (h->root.got.offset != 0)
5239 h->root.dynindx = hsd->max_non_got_dynindx++;
5242 h->root.dynindx = --hsd->min_got_dynindx;
5243 hsd->low = (struct elf_link_hash_entry *) h;
5249 /* Sort the dynamic symbol table so that symbols that need GOT entries
5250 appear towards the end. This reduces the amount of GOT space
5254 mips_elf_sort_hash_table (info)
5255 struct bfd_link_info *info;
5257 struct mips_elf_hash_sort_data hsd;
5258 struct mips_got_info *g;
5261 dynobj = elf_hash_table (info)->dynobj;
5264 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5265 hsd.max_non_got_dynindx = 1;
5266 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5267 elf_hash_table (info)),
5268 mips_elf_sort_hash_table_f,
5271 /* There shoud have been enough room in the symbol table to
5272 accomodate both the GOT and non-GOT symbols. */
5273 BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5275 /* Now we know which dynamic symbol has the lowest dynamic symbol
5276 table index in the GOT. */
5277 g = mips_elf_got_info (dynobj, NULL);
5278 g->global_gotsym = hsd.low;
5283 /* Create a local GOT entry for VALUE. Return the index of the entry,
5284 or -1 if it could not be created. */
5287 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5289 struct mips_got_info *g;
5293 if (g->assigned_gotno >= g->local_gotno)
5295 /* We didn't allocate enough space in the GOT. */
5296 (*_bfd_error_handler)
5297 (_("not enough GOT space for local GOT entries"));
5298 bfd_set_error (bfd_error_bad_value);
5299 return (bfd_vma) -1;
5302 bfd_put_32 (abfd, value, sgot->contents + 4 * g->assigned_gotno);
5303 return 4 * g->assigned_gotno++;
5306 /* Returns the GOT offset at which the indicated address can be found.
5307 If there is not yet a GOT entry for this value, create one. Returns
5308 -1 if no satisfactory GOT offset can be found. */
5311 mips_elf_local_got_index (abfd, info, value)
5313 struct bfd_link_info *info;
5317 struct mips_got_info *g;
5320 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5322 /* Look to see if we already have an appropriate entry. */
5323 for (entry = sgot->contents + 4 * MIPS_RESERVED_GOTNO;
5324 entry != sgot->contents + 4 * g->assigned_gotno;
5327 bfd_vma address = bfd_get_32 (abfd, entry);
5329 if (address == value)
5330 return entry - sgot->contents;
5333 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5336 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5337 are supposed to be placed at small offsets in the GOT, i.e.,
5338 within 32KB of GP. Return the index into the GOT for this page,
5339 and store the offset from this entry to the desired address in
5340 OFFSETP, if it is non-NULL. */
5343 mips_elf_got_page (abfd, info, value, offsetp)
5345 struct bfd_link_info *info;
5350 struct mips_got_info *g;
5352 bfd_byte *last_entry;
5356 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5358 /* Look to see if we aleady have an appropriate entry. */
5359 last_entry = sgot->contents + 4 * g->assigned_gotno;
5360 for (entry = sgot->contents + 4 * MIPS_RESERVED_GOTNO;
5361 entry != last_entry;
5364 address = bfd_get_32 (abfd, entry);
5365 if (!mips_elf_overflow_p (value - address, 16))
5367 /* This entry will serve as the page pointer. We can add a
5368 16-bit number to it to get the actual address. */
5369 index = entry - sgot->contents;
5374 /* If we didn't have an appropriate entry, we create one now. */
5375 if (entry == last_entry)
5376 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5380 address = bfd_get_32 (abfd, entry);
5381 *offsetp = value - address;
5387 /* Find a GOT entry whose higher-order 16 bits are the same as those
5388 for value. Return the index into the GOT for this entry. */
5391 mips_elf_got16_entry (abfd, info, value)
5393 struct bfd_link_info *info;
5397 struct mips_got_info *g;
5399 bfd_byte *last_entry;
5403 value &= 0xffff0000;
5404 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5406 /* Look to see if we already have an appropriate entry. */
5407 last_entry = sgot->contents + 4 * g->assigned_gotno;
5408 for (entry = sgot->contents + 4 * MIPS_RESERVED_GOTNO;
5409 entry != last_entry;
5412 address = bfd_get_32 (abfd, entry);
5413 if (address & 0xffff0000 == value)
5415 /* This entry has the right high-order 16 bits. */
5416 index = 4 * (entry - sgot->contents);
5421 /* If we didn't have an appropriate entry, we create one now. */
5422 if (entry == last_entry)
5423 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5428 /* Sets *ADDENDP to the addend for the first R_MIPS_LO16 relocation
5429 found, beginning with RELOCATION. RELEND is one-past-the-end of
5430 the relocation table. */
5433 mips_elf_next_lo16_addend (relocation, relend, addendp)
5434 Elf_Internal_Rela *relocation;
5435 Elf_Internal_Rela *relend;
5438 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5439 immediately following. However, for the IRIX6 ABI, the next
5440 relocation may be a composed relocation consisting of several
5441 relocations for the same address. In that case, the R_MIPS_LO16
5442 relo!scation may occur as one of these. We permit a similar
5443 extension in general, as that is useful for GCC. */
5444 while (relocation < relend)
5446 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS_LO16)
5448 *addendp = relocation->r_addend;
5455 /* We didn't find it. */
5459 /* Create a rel.dyn relocation for the dynamic linker to resolve. The
5460 relocatin is against the symbol with the dynamic symbol table index
5461 DYNINDX. REL is the original relocation, which is now being made
5465 mips_elf_create_dynamic_relocation (output_bfd, info, rel, dynindx,
5466 addend, input_section)
5468 struct bfd_link_info *info;
5469 Elf_Internal_Rela *rel;
5472 asection *input_section;
5474 Elf_Internal_Rel outrel;
5480 r_type = ELF32_R_TYPE (rel->r_info);
5481 dynobj = elf_hash_table (info)->dynobj;
5482 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5483 BFD_ASSERT (sreloc != NULL);
5487 /* The symbol for the relocation is the same as it was for the
5488 original relocation. */
5489 outrel.r_info = ELF32_R_INFO (dynindx, R_MIPS_REL32);
5491 /* The offset for the dynamic relocation is the same as for the
5492 original relocation, adjusted by the offset at which the original
5493 section is output. */
5494 if (elf_section_data (input_section)->stab_info == NULL)
5495 outrel.r_offset = rel->r_offset;
5500 off = (_bfd_stab_section_offset
5501 (output_bfd, &elf_hash_table (info)->stab_info,
5503 &elf_section_data (input_section)->stab_info,
5505 if (off == (bfd_vma) -1)
5507 outrel.r_offset = off;
5509 outrel.r_offset += (input_section->output_section->vma
5510 + input_section->output_offset);
5512 /* If we've decided to skip this relocation, just output an emtpy
5515 memset (&outrel, 0, sizeof (outrel));
5517 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5518 (((Elf32_External_Rel *)
5520 + sreloc->reloc_count));
5521 ++sreloc->reloc_count;
5523 /* Make sure the output section is writable. The dynamic linker
5524 will be writing to it. */
5525 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5528 /* On IRIX5, make an entry of compact relocation info. */
5529 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5531 asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5536 Elf32_crinfo cptrel;
5538 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5539 cptrel.vaddr = (rel->r_offset
5540 + input_section->output_section->vma
5541 + input_section->output_offset);
5542 if (r_type == R_MIPS_REL32)
5543 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5545 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5546 mips_elf_set_cr_dist2to (cptrel, 0);
5547 cptrel.konst = addend;
5549 cr = (scpt->contents
5550 + sizeof (Elf32_External_compact_rel));
5551 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5552 ((Elf32_External_crinfo *) cr
5553 + scpt->reloc_count));
5554 ++scpt->reloc_count;
5558 return sreloc->reloc_count - 1;
5561 /* Calculate the value produced by the RELOCATION (which comes from
5562 the INPUT_BFD). The ADDEND is the addend to use for this
5563 RELOCATION; RELOCATION->R_ADDEND is ignored.
5565 The result of the relocation calculation is stored in VALUEP.
5567 This function returns bfd_reloc_continue if the caller need take no
5568 further action regarding this relocation, bfd_reloc_notsupported if
5569 something goes dramatically wrong, bfd_reloc_overflow if an
5570 overflow occurs, and bfd_reloc_ok to indicate success. */
5572 static bfd_reloc_status_type
5573 mips_elf_calculate_relocation (abfd,
5587 asection *input_section;
5588 struct bfd_link_info *info;
5589 Elf_Internal_Rela *relocation;
5591 reloc_howto_type *howto;
5592 Elf_Internal_Rela *relend;
5593 Elf_Internal_Sym *local_syms;
5594 asection **local_sections;
5598 /* The eventual value we will return. */
5600 /* The address of the symbol against which the relocation is
5603 /* The final GP value to be used for the relocatable, executable, or
5604 shared object file being produced. */
5605 bfd_vma gp = (bfd_vma) - 1;
5606 /* The place (section offset or address) of the storage unit being
5609 /* The value of GP used to create the relocatable object. */
5610 bfd_vma gp0 = (bfd_vma) - 1;
5611 /* The offset into the global offset table at which the address of
5612 the relocation entry symbol, adjusted by the addend, resides
5613 during execution. */
5614 bfd_vma g = (bfd_vma) - 1;
5615 /* The section in which the symbol referenced by the relocation is
5617 asection *sec = NULL;
5618 struct mips_elf_link_hash_entry* h = NULL;
5619 unsigned long r_symndx;
5621 boolean gp_disp_p = false;
5622 Elf_Internal_Shdr *symtab_hdr;
5625 boolean overflowed_p;
5627 /* Parse the relocation. */
5628 r_symndx = ELF32_R_SYM (relocation->r_info);
5629 r_type = ELF32_R_TYPE (relocation->r_info);
5630 p = (input_section->output_section->vma
5631 + input_section->output_offset
5632 + relocation->r_offset);
5634 /* Assume that there will be no overflow. */
5635 overflowed_p = false;
5637 /* Figure out whether or not the symbol is local. */
5638 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5639 if (elf_bad_symtab (input_bfd))
5641 /* The symbol table does not follow the rule that local symbols
5642 must come before globals. */
5644 local_p = local_sections[r_symndx] != NULL;
5648 extsymoff = symtab_hdr->sh_info;
5649 local_p = r_symndx < extsymoff;
5652 /* Figure out the value of the symbol. */
5655 Elf_Internal_Sym *sym;
5657 sym = local_syms + r_symndx;
5658 sec = local_sections[r_symndx];
5660 symbol = sec->output_section->vma + sec->output_offset;
5661 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5662 symbol += sym->st_value;
5664 /* MIPS16 text labels should be treated as odd. */
5665 if (sym->st_other == STO_MIPS16)
5668 /* Record the name of this symbol, for our caller. */
5669 *namep = bfd_elf_string_from_elf_section (input_bfd,
5670 symtab_hdr->sh_link,
5673 *namep = bfd_section_name (input_bfd, sec);
5677 /* For global symbols we look up the symbol in the hash-table. */
5678 h = ((struct mips_elf_link_hash_entry *)
5679 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5680 /* Find the real hash-table entry for this symbol. */
5681 while (h->root.type == bfd_link_hash_indirect
5682 || h->root.type == bfd_link_hash_warning)
5683 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5685 /* Record the name of this symbol, for our caller. */
5686 *namep = h->root.root.root.string;
5688 /* See if this is the special _gp_disp symbol. Note that such a
5689 symbol must always be a global symbol. */
5690 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5692 /* Relocations against _gp_disp are permitted only with
5693 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5694 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5695 return bfd_reloc_notsupported;
5700 /* If this symbol is defined, calculate its address. */
5701 if ((h->root.root.type == bfd_link_hash_defined
5702 || h->root.root.type == bfd_link_hash_defweak)
5703 && h->root.root.u.def.section)
5705 sec = h->root.root.u.def.section;
5706 if (sec->output_section)
5707 symbol = (h->root.root.u.def.value
5708 + sec->output_section->vma
5709 + sec->output_offset);
5711 symbol = h->root.root.u.def.value;
5715 (*info->callbacks->undefined_symbol)
5716 (info, h->root.root.root.string, input_bfd,
5717 input_section, relocation->r_offset);
5718 return bfd_reloc_undefined;
5722 /* If we haven't already determined the GOT offset, or the GP value,
5723 and we're going to need it, get it now. */
5727 case R_MIPS_GOT_DISP:
5728 case R_MIPS_GOT_HI16:
5729 case R_MIPS_CALL_HI16:
5730 case R_MIPS_GOT_LO16:
5731 case R_MIPS_CALL_LO16:
5732 /* Find the index into the GOT where this value is located. */
5735 BFD_ASSERT (addend == 0);
5736 g = mips_elf_global_got_index
5737 (elf_hash_table (info)->dynobj,
5738 (struct elf_link_hash_entry*) h);
5742 g = mips_elf_local_got_index (abfd, info, symbol + addend);
5743 if (g == (bfd_vma) -1)
5747 /* Convert GOT indices to actual offsets. */
5748 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5754 case R_MIPS_GPREL16:
5755 case R_MIPS_GPREL32:
5756 gp0 = _bfd_get_gp_value (input_bfd);
5757 gp = _bfd_get_gp_value (abfd);
5764 /* Figure out what kind of relocation is being performed. */
5768 return bfd_reloc_continue;
5771 value = symbol + mips_elf_sign_extend (addend, 16);
5772 overflowed_p = mips_elf_overflow_p (value, 16);
5777 /* If we're creating a shared library, or this relocation is
5778 against a symbol in a shared library, then we can't know
5779 where the symbol will end up. So, we create a relocation
5780 record in the output, and leave the job up to the dynamic
5782 if (info->shared || !sec->output_section)
5784 unsigned int reloc_index;
5786 BFD_ASSERT (h != NULL);
5788 = mips_elf_create_dynamic_relocation (abfd,
5794 if (h->min_dyn_reloc_index == 0
5795 || reloc_index < h->min_dyn_reloc_index)
5796 h->min_dyn_reloc_index = reloc_index;
5797 value = symbol + addend;
5801 if (r_type == R_MIPS_32)
5802 value = symbol + addend;
5806 value &= howto->dst_mask;
5811 value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
5813 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
5814 value &= howto->dst_mask;
5820 value = mips_elf_high (addend + symbol);
5821 value &= howto->dst_mask;
5825 value = mips_elf_high (addend + gp - p);
5826 overflowed_p = mips_elf_overflow_p (value, 16);
5832 value = (symbol + addend) & howto->dst_mask;
5835 value = addend + gp - p + 4;
5836 overflowed_p = mips_elf_overflow_p (value, 16);
5840 case R_MIPS_LITERAL:
5841 /* Because we don't merge literal sections, we can handle this
5842 just like R_MIPS_GPREL16. In the long run, we should merge
5843 shared literals, and then we will need to additional work
5848 case R_MIPS_GPREL16:
5850 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
5852 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
5853 overflowed_p = mips_elf_overflow_p (value, 16);
5859 value = mips_elf_got16_entry (abfd, info, symbol + addend);
5860 if (value == (bfd_vma) -1)
5863 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5866 overflowed_p = mips_elf_overflow_p (value, 16);
5873 case R_MIPS_GOT_DISP:
5875 overflowed_p = mips_elf_overflow_p (value, 16);
5878 case R_MIPS_GPREL32:
5879 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
5883 value = mips_elf_sign_extend (addend, 16) + symbol - p;
5884 overflowed_p = mips_elf_overflow_p (value, 16);
5887 case R_MIPS_GOT_HI16:
5888 case R_MIPS_CALL_HI16:
5889 /* We're allowed to handle these two relocations identically.
5890 The dynamic linker is allowed to handle the CALL relocations
5891 differently by creating a lazy evaluation stub. */
5893 value = mips_elf_high (value);
5894 value &= howto->dst_mask;
5897 case R_MIPS_GOT_LO16:
5898 case R_MIPS_CALL_LO16:
5899 value = g & howto->dst_mask;
5903 value = (symbol + addend) & howto->dst_mask;
5906 case R_MIPS_GOT_PAGE:
5907 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
5908 if (value == (bfd_vma) -1)
5910 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
5913 overflowed_p = mips_elf_overflow_p (value, 16);
5916 case R_MIPS_GOT_OFST:
5917 mips_elf_got_page (abfd, info, symbol + addend, &value);
5918 overflowed_p = mips_elf_overflow_p (value, 16);
5922 value = symbol - addend;
5923 value &= howto->dst_mask;
5927 value = mips_elf_higher (addend + symbol);
5928 value &= howto->dst_mask;
5931 case R_MIPS_HIGHEST:
5932 value = mips_elf_highest (addend + symbol);
5933 value &= howto->dst_mask;
5936 case R_MIPS_SCN_DISP:
5937 value = symbol + addend - sec->output_offset;
5938 value &= howto->dst_mask;
5943 /* Both of these may be ignored. R_MIPS_JALR is an optimization
5944 hint; we could improve performance by honoring that hint. */
5945 return bfd_reloc_continue;
5947 case R_MIPS_GNU_VTINHERIT:
5948 case R_MIPS_GNU_VTENTRY:
5949 /* We don't do anything with these at present. */
5950 return bfd_reloc_continue;
5953 case R_MIPS16_GPREL:
5954 /* These relocations, used for MIPS16, are not clearly
5955 documented anywhere. What do they do? */
5956 return bfd_reloc_notsupported;
5959 /* An unrecognized relocation type. */
5960 return bfd_reloc_notsupported;
5963 /* Store the VALUE for our caller. */
5965 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5968 /* Obtain the field relocated by RELOCATION. */
5971 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
5972 reloc_howto_type *howto;
5973 Elf_Internal_Rela *relocation;
5978 bfd_byte *location = contents + relocation->r_offset;
5980 switch (bfd_get_reloc_size (howto))
5987 x = bfd_get_8 (input_bfd, location);
5991 x = bfd_get_16 (input_bfd, location);
5995 x = bfd_get_32 (input_bfd, location);
6000 x = bfd_get_64 (input_bfd, location);
6014 /* It has been determined that the result of the RELOCATION is the
6015 VALUE. Use HOWTO to place VALUE into the output file at the
6016 appropriate position. The SECTION is the section to which the
6019 Returns false if anything goes wrong. */
6022 mips_elf_perform_relocation (howto, relocation, value, input_bfd, contents)
6023 reloc_howto_type *howto;
6024 Elf_Internal_Rela *relocation;
6030 bfd_byte *location = contents + relocation->r_offset;
6032 /* Obtain the current value. */
6033 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6035 /* Clear the field we are setting. */
6036 x &= ~howto->dst_mask;
6038 /* Set the field. */
6039 x |= (value & howto->dst_mask);
6041 /* Put the value into the output. */
6042 switch (bfd_get_reloc_size (howto))
6049 bfd_put_8 (input_bfd, x, location);
6053 bfd_put_16 (input_bfd, x, location);
6057 bfd_put_32 (input_bfd, x, location);
6062 bfd_put_64 (input_bfd, x, location);
6074 /* Relocate a MIPS ELF section. */
6077 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6078 contents, relocs, local_syms, local_sections)
6080 struct bfd_link_info *info;
6082 asection *input_section;
6084 Elf_Internal_Rela *relocs;
6085 Elf_Internal_Sym *local_syms;
6086 asection **local_sections;
6088 Elf_Internal_Shdr *symtab_hdr;
6091 asection *sgot, *sreloc, *scpt;
6094 Elf_Internal_Rela *rel;
6095 Elf_Internal_Rela *relend;
6096 struct mips_got_info *g;
6098 bfd_vma last_hi16_addend;
6099 boolean next_relocation_for_same_address_p = false;
6100 boolean use_saved_addend_p = false;
6101 boolean last_hi16_addend_valid_p = false;
6103 dynobj = elf_hash_table (info)->dynobj;
6104 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6108 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
6111 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
6114 if (elf_bad_symtab (input_bfd))
6116 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6121 locsymcount = symtab_hdr->sh_info;
6122 extsymoff = symtab_hdr->sh_info;
6125 gp = _bfd_get_gp_value (output_bfd);
6126 relend = relocs + input_section->reloc_count;
6128 for (rel = relocs; rel < relend; ++rel)
6133 reloc_howto_type *howto;
6135 /* Find the relocation howto for this relocation. */
6136 howto = elf_mips_howto_table + ELF32_R_TYPE (rel->r_info);
6138 if (!use_saved_addend_p)
6140 Elf_Internal_Shdr *rel_hdr;
6142 /* If these relocations were originally of the REL variety,
6143 we must pull the addend out of the field that will be
6144 relocated. Otherwise, we simply use the contents of the
6145 RELA relocation. To determine which flavor or relocation
6146 this is, we depend on the fact that the INPUT_SECTION's
6147 REL_HDR is read before its REL_HDR2. */
6148 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6149 if (rel - relocs >= rel_hdr->sh_size / rel_hdr->sh_entsize)
6150 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6151 if (rel_hdr->sh_entsize == sizeof (Elf32_External_Rel))
6153 int r_type = ELF32_R_TYPE (rel->r_info);
6155 addend = mips_elf_obtain_contents (howto,
6159 addend &= howto->src_mask;
6161 /* For some kinds of relocations, the ADDEND is a
6162 combination of the addend stored in two different
6164 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16)
6166 /* Scan ahead to find a matching R_MIPS_LO16
6170 if (!mips_elf_next_lo16_addend (rel, relend, &l))
6173 /* Save the high-order bit for later. When we
6174 encounter the R_MIPS_LO16 relocation we will need
6177 last_hi16_addend = addend;
6178 last_hi16_addend_valid_p = true;
6180 /* Compute the combined addend. */
6183 else if (r_type == R_MIPS_LO16)
6185 /* Used the saved HI16 addend. */
6186 if (!last_hi16_addend_valid_p)
6188 addend |= last_hi16_addend;
6192 addend = rel->r_addend;
6195 /* In the N32 and 64-bit ABIs there may be multiple consecutive
6196 relocations for the same offset. In that case we are
6197 supposed to treat the output of each relocation as the addend
6199 if (rel + 1 < relend && rel->r_offset == (rel + 1)->r_offset)
6200 use_saved_addend_p = true;
6202 use_saved_addend_p = false;
6204 /* Figure out what value we are supposed to relocate. */
6205 switch (mips_elf_calculate_relocation (output_bfd,
6218 case bfd_reloc_continue:
6219 /* There's nothing to do. */
6222 case bfd_reloc_undefined:
6225 case bfd_reloc_notsupported:
6229 case bfd_reloc_overflow:
6230 if (use_saved_addend_p)
6231 /* Ignore overflow until we reach the last relocation for
6232 a given location. */
6235 || ! ((*info->callbacks->reloc_overflow)
6236 (info, name, howto->name, (bfd_vma) 0,
6237 input_bfd, input_section, rel->r_offset)))
6250 /* If we've got another relocation for the address, keep going
6251 until we reach the last one. */
6252 if (use_saved_addend_p)
6258 /* Actually perform the relocation. */
6259 mips_elf_perform_relocation (howto, rel, value, input_bfd,
6266 /* This hook function is called before the linker writes out a global
6267 symbol. We mark symbols as small common if appropriate. This is
6268 also where we undo the increment of the value for a mips16 symbol. */
6272 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6274 struct bfd_link_info *info;
6276 Elf_Internal_Sym *sym;
6277 asection *input_sec;
6279 /* If we see a common symbol, which implies a relocatable link, then
6280 if a symbol was small common in an input file, mark it as small
6281 common in the output file. */
6282 if (sym->st_shndx == SHN_COMMON
6283 && strcmp (input_sec->name, ".scommon") == 0)
6284 sym->st_shndx = SHN_MIPS_SCOMMON;
6286 if (sym->st_other == STO_MIPS16
6287 && (sym->st_value & 1) != 0)
6293 /* Functions for the dynamic linker. */
6295 /* The name of the dynamic interpreter. This is put in the .interp
6298 #define ELF_DYNAMIC_INTERPRETER(abfd) \
6299 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" : "/usr/lib/libc.so.1")
6301 /* Create dynamic sections when linking against a dynamic object. */
6304 mips_elf_create_dynamic_sections (abfd, info)
6306 struct bfd_link_info *info;
6308 struct elf_link_hash_entry *h;
6310 register asection *s;
6311 const char * const *namep;
6313 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6314 | SEC_LINKER_CREATED | SEC_READONLY);
6316 /* Mips ABI requests the .dynamic section to be read only. */
6317 s = bfd_get_section_by_name (abfd, ".dynamic");
6320 if (! bfd_set_section_flags (abfd, s, flags))
6324 /* We need to create .got section. */
6325 if (! mips_elf_create_got_section (abfd, info))
6328 /* Create the .msym section on IRIX6. It is used by the dynamic
6329 linker to speed up dynamic relocations, and to avoid computing
6330 the ELF hash for symbols. */
6331 if (IRIX_COMPAT (abfd) == ict_irix6
6332 && !mips_elf_create_msym_section (abfd))
6335 /* Create .stub section. */
6336 if (bfd_get_section_by_name (abfd,
6337 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6339 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6341 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6342 || ! bfd_set_section_alignment (abfd, s, 2))
6346 if (IRIX_COMPAT (abfd) == ict_irix5
6348 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6350 s = bfd_make_section (abfd, ".rld_map");
6352 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6353 || ! bfd_set_section_alignment (abfd, s, 2))
6357 /* On IRIX5, we adjust add some additional symbols and change the
6358 alignments of several sections. There is no ABI documentation
6359 indicating that this is necessary on IRIX6, nor any evidence that
6360 the linker takes such action. */
6361 if (IRIX_COMPAT (abfd) == ict_irix5)
6363 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6366 if (! (_bfd_generic_link_add_one_symbol
6367 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
6368 (bfd_vma) 0, (const char *) NULL, false,
6369 get_elf_backend_data (abfd)->collect,
6370 (struct bfd_link_hash_entry **) &h)))
6372 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6373 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6374 h->type = STT_SECTION;
6376 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6380 /* We need to create a .compact_rel section. */
6381 if (! mips_elf_create_compact_rel_section (abfd, info))
6384 /* Change aligments of some sections. */
6385 s = bfd_get_section_by_name (abfd, ".hash");
6387 bfd_set_section_alignment (abfd, s, 4);
6388 s = bfd_get_section_by_name (abfd, ".dynsym");
6390 bfd_set_section_alignment (abfd, s, 4);
6391 s = bfd_get_section_by_name (abfd, ".dynstr");
6393 bfd_set_section_alignment (abfd, s, 4);
6394 s = bfd_get_section_by_name (abfd, ".reginfo");
6396 bfd_set_section_alignment (abfd, s, 4);
6397 s = bfd_get_section_by_name (abfd, ".dynamic");
6399 bfd_set_section_alignment (abfd, s, 4);
6405 if (! (_bfd_generic_link_add_one_symbol
6406 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
6407 (bfd_vma) 0, (const char *) NULL, false,
6408 get_elf_backend_data (abfd)->collect,
6409 (struct bfd_link_hash_entry **) &h)))
6411 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6412 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6413 h->type = STT_SECTION;
6415 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6418 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6420 /* __rld_map is a four byte word located in the .data section
6421 and is filled in by the rtld to contain a pointer to
6422 the _r_debug structure. Its symbol value will be set in
6423 mips_elf_finish_dynamic_symbol. */
6424 s = bfd_get_section_by_name (abfd, ".rld_map");
6425 BFD_ASSERT (s != NULL);
6428 if (! (_bfd_generic_link_add_one_symbol
6429 (info, abfd, "__rld_map", BSF_GLOBAL, s,
6430 (bfd_vma) 0, (const char *) NULL, false,
6431 get_elf_backend_data (abfd)->collect,
6432 (struct bfd_link_hash_entry **) &h)))
6434 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6435 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6436 h->type = STT_OBJECT;
6438 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6446 /* Create the .compact_rel section. */
6449 mips_elf_create_compact_rel_section (abfd, info)
6451 struct bfd_link_info *info;
6454 register asection *s;
6456 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
6458 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
6461 s = bfd_make_section (abfd, ".compact_rel");
6463 || ! bfd_set_section_flags (abfd, s, flags)
6464 || ! bfd_set_section_alignment (abfd, s, 2))
6467 s->_raw_size = sizeof (Elf32_External_compact_rel);
6473 /* Create the .got section to hold the global offset table. */
6476 mips_elf_create_got_section (abfd, info)
6478 struct bfd_link_info *info;
6481 register asection *s;
6482 struct elf_link_hash_entry *h;
6483 struct mips_got_info *g;
6485 /* This function may be called more than once. */
6486 if (bfd_get_section_by_name (abfd, ".got") != NULL)
6489 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6490 | SEC_LINKER_CREATED);
6492 s = bfd_make_section (abfd, ".got");
6494 || ! bfd_set_section_flags (abfd, s, flags)
6495 || ! bfd_set_section_alignment (abfd, s, 4))
6498 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
6499 linker script because we don't want to define the symbol if we
6500 are not creating a global offset table. */
6502 if (! (_bfd_generic_link_add_one_symbol
6503 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
6504 (bfd_vma) 0, (const char *) NULL, false,
6505 get_elf_backend_data (abfd)->collect,
6506 (struct bfd_link_hash_entry **) &h)))
6508 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6509 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6510 h->type = STT_OBJECT;
6513 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
6516 /* The first several global offset table entries are reserved. */
6517 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
6519 g = (struct mips_got_info *) bfd_alloc (abfd,
6520 sizeof (struct mips_got_info));
6523 g->global_gotsym = NULL;
6524 g->local_gotno = MIPS_RESERVED_GOTNO;
6525 g->assigned_gotno = MIPS_RESERVED_GOTNO;
6526 if (elf_section_data (s) == NULL)
6529 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
6530 if (elf_section_data (s) == NULL)
6533 elf_section_data (s)->tdata = (PTR) g;
6534 elf_section_data (s)->this_hdr.sh_flags
6535 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6540 /* Returns the .msym section for ABFD, creating it if it does not
6541 already exist. Returns NULL to indicate error. */
6544 mips_elf_create_msym_section (abfd)
6549 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6552 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6554 || !bfd_set_section_flags (abfd, s,
6558 | SEC_LINKER_CREATED
6560 || !bfd_set_section_alignment (abfd, s, 2))
6567 /* Look through the relocs for a section during the first phase, and
6568 allocate space in the global offset table. */
6571 mips_elf_check_relocs (abfd, info, sec, relocs)
6573 struct bfd_link_info *info;
6575 const Elf_Internal_Rela *relocs;
6579 Elf_Internal_Shdr *symtab_hdr;
6580 struct elf_link_hash_entry **sym_hashes;
6581 struct mips_got_info *g;
6583 const Elf_Internal_Rela *rel;
6584 const Elf_Internal_Rela *rel_end;
6588 if (info->relocateable)
6591 dynobj = elf_hash_table (info)->dynobj;
6592 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6593 sym_hashes = elf_sym_hashes (abfd);
6594 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6596 /* Check for the mips16 stub sections. */
6598 name = bfd_get_section_name (abfd, sec);
6599 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6601 unsigned long r_symndx;
6603 /* Look at the relocation information to figure out which symbol
6606 r_symndx = ELF32_R_SYM (relocs->r_info);
6608 if (r_symndx < extsymoff
6609 || sym_hashes[r_symndx - extsymoff] == NULL)
6613 /* This stub is for a local symbol. This stub will only be
6614 needed if there is some relocation in this BFD, other
6615 than a 16 bit function call, which refers to this symbol. */
6616 for (o = abfd->sections; o != NULL; o = o->next)
6618 Elf_Internal_Rela *sec_relocs;
6619 const Elf_Internal_Rela *r, *rend;
6621 /* We can ignore stub sections when looking for relocs. */
6622 if ((o->flags & SEC_RELOC) == 0
6623 || o->reloc_count == 0
6624 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6625 sizeof FN_STUB - 1) == 0
6626 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6627 sizeof CALL_STUB - 1) == 0
6628 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6629 sizeof CALL_FP_STUB - 1) == 0)
6632 sec_relocs = (_bfd_elf32_link_read_relocs
6633 (abfd, o, (PTR) NULL,
6634 (Elf_Internal_Rela *) NULL,
6635 info->keep_memory));
6636 if (sec_relocs == NULL)
6639 rend = sec_relocs + o->reloc_count;
6640 for (r = sec_relocs; r < rend; r++)
6641 if (ELF32_R_SYM (r->r_info) == r_symndx
6642 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6645 if (! info->keep_memory)
6654 /* There is no non-call reloc for this stub, so we do
6655 not need it. Since this function is called before
6656 the linker maps input sections to output sections, we
6657 can easily discard it by setting the SEC_EXCLUDE
6659 sec->flags |= SEC_EXCLUDE;
6663 /* Record this stub in an array of local symbol stubs for
6665 if (elf_tdata (abfd)->local_stubs == NULL)
6667 unsigned long symcount;
6670 if (elf_bad_symtab (abfd))
6671 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6673 symcount = symtab_hdr->sh_info;
6674 n = (asection **) bfd_zalloc (abfd,
6675 symcount * sizeof (asection *));
6678 elf_tdata (abfd)->local_stubs = n;
6681 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6683 /* We don't need to set mips16_stubs_seen in this case.
6684 That flag is used to see whether we need to look through
6685 the global symbol table for stubs. We don't need to set
6686 it here, because we just have a local stub. */
6690 struct mips_elf_link_hash_entry *h;
6692 h = ((struct mips_elf_link_hash_entry *)
6693 sym_hashes[r_symndx - extsymoff]);
6695 /* H is the symbol this stub is for. */
6698 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6701 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6702 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6704 unsigned long r_symndx;
6705 struct mips_elf_link_hash_entry *h;
6708 /* Look at the relocation information to figure out which symbol
6711 r_symndx = ELF32_R_SYM (relocs->r_info);
6713 if (r_symndx < extsymoff
6714 || sym_hashes[r_symndx - extsymoff] == NULL)
6716 /* This stub was actually built for a static symbol defined
6717 in the same file. We assume that all static symbols in
6718 mips16 code are themselves mips16, so we can simply
6719 discard this stub. Since this function is called before
6720 the linker maps input sections to output sections, we can
6721 easily discard it by setting the SEC_EXCLUDE flag. */
6722 sec->flags |= SEC_EXCLUDE;
6726 h = ((struct mips_elf_link_hash_entry *)
6727 sym_hashes[r_symndx - extsymoff]);
6729 /* H is the symbol this stub is for. */
6731 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6732 loc = &h->call_fp_stub;
6734 loc = &h->call_stub;
6736 /* If we already have an appropriate stub for this function, we
6737 don't need another one, so we can discard this one. Since
6738 this function is called before the linker maps input sections
6739 to output sections, we can easily discard it by setting the
6740 SEC_EXCLUDE flag. We can also discard this section if we
6741 happen to already know that this is a mips16 function; it is
6742 not necessary to check this here, as it is checked later, but
6743 it is slightly faster to check now. */
6744 if (*loc != NULL || h->root.other == STO_MIPS16)
6746 sec->flags |= SEC_EXCLUDE;
6751 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6761 sgot = bfd_get_section_by_name (dynobj, ".got");
6766 BFD_ASSERT (elf_section_data (sgot) != NULL);
6767 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6768 BFD_ASSERT (g != NULL);
6774 rel_end = relocs + sec->reloc_count;
6775 for (rel = relocs; rel < rel_end; rel++)
6777 unsigned long r_symndx;
6779 struct elf_link_hash_entry *h;
6781 r_symndx = ELF32_R_SYM (rel->r_info);
6782 r_type = ELF32_R_TYPE (rel->r_info);
6784 if (r_symndx < extsymoff)
6788 h = sym_hashes[r_symndx - extsymoff];
6790 /* This may be an indirect symbol created because of a version. */
6793 while (h->root.type == bfd_link_hash_indirect)
6794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6798 /* Some relocs require a global offset table. */
6799 if (dynobj == NULL || sgot == NULL)
6805 case R_MIPS_CALL_HI16:
6806 case R_MIPS_CALL_LO16:
6807 case R_MIPS_GOT_HI16:
6808 case R_MIPS_GOT_LO16:
6810 elf_hash_table (info)->dynobj = dynobj = abfd;
6811 if (! mips_elf_create_got_section (dynobj, info))
6813 g = mips_elf_got_info (dynobj, &sgot);
6819 && (info->shared || h != NULL)
6820 && (sec->flags & SEC_ALLOC) != 0)
6821 elf_hash_table (info)->dynobj = dynobj = abfd;
6829 if (!h && (r_type == R_MIPS_CALL_LO16
6830 || r_type == R_MIPS_GOT_LO16
6831 || r_type == R_MIPS_GOT_DISP))
6833 /* We may need a local GOT entry for this relocation. We
6834 don't count R_MIPS_HI16 or R_MIPS_GOT16 relocations
6835 because they are always followed by a R_MIPS_LO16
6836 relocation for the value. We don't R_MIPS_GOT_PAGE
6837 because we can estimate the maximum number of pages
6838 needed by looking at the size of the segment.
6840 This estimation is very conservative since we can merge
6841 duplicate entries in the GOT. In order to be less
6842 conservative, we could actually build the GOT here,
6843 rather than in relocate_section. */
6845 sgot->_raw_size += 4;
6853 (*_bfd_error_handler)
6854 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6855 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6856 bfd_set_error (bfd_error_bad_value);
6861 case R_MIPS_CALL_HI16:
6862 case R_MIPS_CALL_LO16:
6863 /* This symbol requires a global offset table entry. */
6864 if (!mips_elf_record_global_got_symbol (h, info, g))
6867 /* We need a stub, not a plt entry for the undefined
6868 function. But we record it as if it needs plt. See
6869 elf_adjust_dynamic_symbol in elflink.h. */
6870 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6876 case R_MIPS_GOT_HI16:
6877 case R_MIPS_GOT_LO16:
6878 case R_MIPS_GOT_DISP:
6879 /* This symbol requires a global offset table entry. */
6880 if (h && !mips_elf_record_global_got_symbol (h, info, g))
6886 if ((info->shared || h != NULL)
6887 && (sec->flags & SEC_ALLOC) != 0)
6891 const char *name = ".rel.dyn";
6893 sreloc = bfd_get_section_by_name (dynobj, name);
6896 sreloc = bfd_make_section (dynobj, name);
6898 || ! bfd_set_section_flags (dynobj, sreloc,
6903 | SEC_LINKER_CREATED
6905 || ! bfd_set_section_alignment (dynobj, sreloc,
6912 /* When creating a shared object, we must copy these
6913 reloc types into the output file as R_MIPS_REL32
6914 relocs. We make room for this reloc in the
6915 .rel.dyn reloc section */
6916 if (sreloc->_raw_size == 0)
6918 /* Add a null element. */
6919 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6920 ++sreloc->reloc_count;
6922 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6926 struct mips_elf_link_hash_entry *hmips;
6928 /* We only need to copy this reloc if the symbol is
6929 defined in a dynamic object. */
6930 hmips = (struct mips_elf_link_hash_entry *) h;
6931 ++hmips->mips_32_relocs;
6934 /* Even though we don't directly need a GOT entry for
6935 this symbol, a symbol must have a dynamic symbol
6936 table index greater that DT_GOTSYM if there are
6937 dynamic relocations against it. */
6938 if (!mips_elf_record_global_got_symbol (h, info, g))
6942 if (SGI_COMPAT (abfd))
6943 mips_elf_hash_table (info)->compact_rel_size +=
6944 sizeof (Elf32_External_crinfo);
6949 case R_MIPS_GPREL16:
6950 case R_MIPS_LITERAL:
6951 case R_MIPS_GPREL32:
6952 if (SGI_COMPAT (abfd))
6953 mips_elf_hash_table (info)->compact_rel_size +=
6954 sizeof (Elf32_External_crinfo);
6957 /* This relocation describes the C++ object vtable hierarchy.
6958 Reconstruct it for later use during GC. */
6959 case R_MIPS_GNU_VTINHERIT:
6960 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6964 /* This relocation describes which C++ vtable entries are actually
6965 used. Record for later use during GC. */
6966 case R_MIPS_GNU_VTENTRY:
6967 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6975 /* If this reloc is not a 16 bit call, and it has a global
6976 symbol, then we will need the fn_stub if there is one.
6977 References from a stub section do not count. */
6979 && r_type != R_MIPS16_26
6980 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6981 sizeof FN_STUB - 1) != 0
6982 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6983 sizeof CALL_STUB - 1) != 0
6984 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6985 sizeof CALL_FP_STUB - 1) != 0)
6987 struct mips_elf_link_hash_entry *mh;
6989 mh = (struct mips_elf_link_hash_entry *) h;
6990 mh->need_fn_stub = true;
6997 /* Return the section that should be marked against GC for a given
7001 mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7003 struct bfd_link_info *info;
7004 Elf_Internal_Rela *rel;
7005 struct elf_link_hash_entry *h;
7006 Elf_Internal_Sym *sym;
7008 /* ??? Do mips16 stub sections need to be handled special? */
7012 switch (ELF32_R_TYPE (rel->r_info))
7014 case R_MIPS_GNU_VTINHERIT:
7015 case R_MIPS_GNU_VTENTRY:
7019 switch (h->root.type)
7021 case bfd_link_hash_defined:
7022 case bfd_link_hash_defweak:
7023 return h->root.u.def.section;
7025 case bfd_link_hash_common:
7026 return h->root.u.c.p->section;
7035 if (!(elf_bad_symtab (abfd)
7036 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7037 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7038 && sym->st_shndx != SHN_COMMON))
7040 return bfd_section_from_elf_index (abfd, sym->st_shndx);
7047 /* Update the got entry reference counts for the section being removed. */
7050 mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7052 struct bfd_link_info *info;
7054 const Elf_Internal_Rela *relocs;
7057 Elf_Internal_Shdr *symtab_hdr;
7058 struct elf_link_hash_entry **sym_hashes;
7059 bfd_signed_vma *local_got_refcounts;
7060 const Elf_Internal_Rela *rel, *relend;
7061 unsigned long r_symndx;
7062 struct elf_link_hash_entry *h;
7064 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7065 sym_hashes = elf_sym_hashes (abfd);
7066 local_got_refcounts = elf_local_got_refcounts (abfd);
7068 relend = relocs + sec->reloc_count;
7069 for (rel = relocs; rel < relend; rel++)
7070 switch (ELF32_R_TYPE (rel->r_info))
7074 case R_MIPS_CALL_HI16:
7075 case R_MIPS_CALL_LO16:
7076 case R_MIPS_GOT_HI16:
7077 case R_MIPS_GOT_LO16:
7078 /* ??? It would seem that the existing MIPS code does no sort
7079 of reference counting or whatnot on its GOT and PLT entries,
7080 so it is not possible to garbage collect them at this time. */
7092 /* Adjust a symbol defined by a dynamic object and referenced by a
7093 regular object. The current definition is in some section of the
7094 dynamic object, but we're not including those sections. We have to
7095 change the definition to something the rest of the link can
7099 mips_elf_adjust_dynamic_symbol (info, h)
7100 struct bfd_link_info *info;
7101 struct elf_link_hash_entry *h;
7104 struct mips_elf_link_hash_entry *hmips;
7107 dynobj = elf_hash_table (info)->dynobj;
7109 /* Make sure we know what is going on here. */
7110 BFD_ASSERT (dynobj != NULL
7111 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7112 || h->weakdef != NULL
7113 || ((h->elf_link_hash_flags
7114 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7115 && (h->elf_link_hash_flags
7116 & ELF_LINK_HASH_REF_REGULAR) != 0
7117 && (h->elf_link_hash_flags
7118 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7120 /* If this symbol is defined in a dynamic object, we need to copy
7121 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7123 hmips = (struct mips_elf_link_hash_entry *) h;
7124 if (! info->relocateable
7125 && hmips->mips_32_relocs != 0
7126 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7128 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7129 BFD_ASSERT (s != NULL);
7131 if (s->_raw_size == 0)
7133 /* Make room for a null element. */
7134 s->_raw_size += sizeof (Elf32_External_Rel);
7137 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
7140 /* For a function, create a stub, if needed. */
7141 if (h->type == STT_FUNC
7142 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7144 if (! elf_hash_table (info)->dynamic_sections_created)
7147 /* If this symbol is not defined in a regular file, then set
7148 the symbol to the stub location. This is required to make
7149 function pointers compare as equal between the normal
7150 executable and the shared library. */
7151 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7153 /* We need .stub section. */
7154 s = bfd_get_section_by_name (dynobj,
7155 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7156 BFD_ASSERT (s != NULL);
7158 h->root.u.def.section = s;
7159 h->root.u.def.value = s->_raw_size;
7161 /* XXX Write this stub address somewhere. */
7162 h->plt.offset = s->_raw_size;
7164 /* Make room for this stub code. */
7165 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7167 /* The last half word of the stub will be filled with the index
7168 of this symbol in .dynsym section. */
7173 /* If this is a weak symbol, and there is a real definition, the
7174 processor independent code will have arranged for us to see the
7175 real definition first, and we can just use the same value. */
7176 if (h->weakdef != NULL)
7178 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7179 || h->weakdef->root.type == bfd_link_hash_defweak);
7180 h->root.u.def.section = h->weakdef->root.u.def.section;
7181 h->root.u.def.value = h->weakdef->root.u.def.value;
7185 /* This is a reference to a symbol defined by a dynamic object which
7186 is not a function. */
7191 /* This function is called after all the input files have been read,
7192 and the input sections have been assigned to output sections. We
7193 check for any mips16 stub sections that we can discard. */
7195 static boolean mips_elf_check_mips16_stubs
7196 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7199 mips_elf_always_size_sections (output_bfd, info)
7201 struct bfd_link_info *info;
7205 /* The .reginfo section has a fixed size. */
7206 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7208 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7210 if (info->relocateable
7211 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7214 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7215 mips_elf_check_mips16_stubs,
7221 /* Check the mips16 stubs for a particular symbol, and see if we can
7226 mips_elf_check_mips16_stubs (h, data)
7227 struct mips_elf_link_hash_entry *h;
7230 if (h->fn_stub != NULL
7231 && ! h->need_fn_stub)
7233 /* We don't need the fn_stub; the only references to this symbol
7234 are 16 bit calls. Clobber the size to 0 to prevent it from
7235 being included in the link. */
7236 h->fn_stub->_raw_size = 0;
7237 h->fn_stub->_cooked_size = 0;
7238 h->fn_stub->flags &= ~ SEC_RELOC;
7239 h->fn_stub->reloc_count = 0;
7240 h->fn_stub->flags |= SEC_EXCLUDE;
7243 if (h->call_stub != NULL
7244 && h->root.other == STO_MIPS16)
7246 /* We don't need the call_stub; this is a 16 bit function, so
7247 calls from other 16 bit functions are OK. Clobber the size
7248 to 0 to prevent it from being included in the link. */
7249 h->call_stub->_raw_size = 0;
7250 h->call_stub->_cooked_size = 0;
7251 h->call_stub->flags &= ~ SEC_RELOC;
7252 h->call_stub->reloc_count = 0;
7253 h->call_stub->flags |= SEC_EXCLUDE;
7256 if (h->call_fp_stub != NULL
7257 && h->root.other == STO_MIPS16)
7259 /* We don't need the call_stub; this is a 16 bit function, so
7260 calls from other 16 bit functions are OK. Clobber the size
7261 to 0 to prevent it from being included in the link. */
7262 h->call_fp_stub->_raw_size = 0;
7263 h->call_fp_stub->_cooked_size = 0;
7264 h->call_fp_stub->flags &= ~ SEC_RELOC;
7265 h->call_fp_stub->reloc_count = 0;
7266 h->call_fp_stub->flags |= SEC_EXCLUDE;
7272 /* Set the sizes of the dynamic sections. */
7275 mips_elf_size_dynamic_sections (output_bfd, info)
7277 struct bfd_link_info *info;
7282 struct mips_got_info *g;
7284 dynobj = elf_hash_table (info)->dynobj;
7285 BFD_ASSERT (dynobj != NULL);
7287 if (elf_hash_table (info)->dynamic_sections_created)
7289 /* Set the contents of the .interp section to the interpreter. */
7292 s = bfd_get_section_by_name (dynobj, ".interp");
7293 BFD_ASSERT (s != NULL);
7295 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7297 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7301 /* The check_relocs and adjust_dynamic_symbol entry points have
7302 determined the sizes of the various dynamic sections. Allocate
7305 for (s = dynobj->sections; s != NULL; s = s->next)
7310 /* It's OK to base decisions on the section name, because none
7311 of the dynobj section names depend upon the input files. */
7312 name = bfd_get_section_name (dynobj, s);
7314 if ((s->flags & SEC_LINKER_CREATED) == 0)
7319 if (strncmp (name, ".rel", 4) == 0)
7321 if (s->_raw_size == 0)
7323 /* We only strip the section if the output section name
7324 has the same name. Otherwise, there might be several
7325 input sections for this output section. FIXME: This
7326 code is probably not needed these days anyhow, since
7327 the linker now does not create empty output sections. */
7328 if (s->output_section != NULL
7330 bfd_get_section_name (s->output_section->owner,
7331 s->output_section)) == 0)
7336 const char *outname;
7339 /* If this relocation section applies to a read only
7340 section, then we probably need a DT_TEXTREL entry.
7341 If the relocation section is .rel.dyn, we always
7342 assert a DT_TEXTREL entry rather than testing whether
7343 there exists a relocation to a read only section or
7345 outname = bfd_get_section_name (output_bfd,
7347 target = bfd_get_section_by_name (output_bfd, outname + 4);
7349 && (target->flags & SEC_READONLY) != 0
7350 && (target->flags & SEC_ALLOC) != 0)
7351 || strcmp (outname, ".rel.dyn") == 0)
7354 /* We use the reloc_count field as a counter if we need
7355 to copy relocs into the output file. */
7356 if (strcmp (name, ".rel.dyn") != 0)
7360 else if (strncmp (name, ".got", 4) == 0)
7363 bfd_size_type loadable_size = 0;
7364 bfd_size_type local_gotno;
7367 BFD_ASSERT (elf_section_data (s) != NULL);
7368 g = (struct mips_got_info *) elf_section_data (s)->tdata;
7369 BFD_ASSERT (g != NULL);
7371 /* Calculate the total loadable size of the output. That
7372 will give us the maximum number of GOT_PAGE entries
7374 for (sub = info->input_bfds; sub; sub = sub->link_next)
7376 asection *subsection;
7378 for (subsection = sub->sections;
7380 subsection = subsection->next)
7382 if ((subsection->flags & SEC_ALLOC) == 0)
7384 loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
7387 loadable_size += MIPS_FUNCTION_STUB_SIZE;
7389 /* Assume there are two loadable segments consisting of
7390 contiguous sections. Is 5 enough? */
7391 local_gotno = (loadable_size >> 16) + 5;
7392 g->local_gotno += local_gotno;
7393 s->_raw_size += local_gotno * 4;
7395 /* There has to be a global GOT entry for every symbol with
7396 a dynamic symbol table index of DT_MIPS_GOTSYM or
7397 higher. Therefore, it make sense to put those symbols
7398 that need GOT entries at the end of the symbol table. We
7400 if (!mips_elf_sort_hash_table (info))
7403 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7404 s->_raw_size += i * 4;
7406 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
7408 /* Irix rld assumes that the function stub isn't at the end
7409 of .text section. So put a dummy. XXX */
7410 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7412 else if (! info->shared
7413 && ! mips_elf_hash_table (info)->use_rld_obj_head
7414 && strncmp (name, ".rld_map", 8) == 0)
7416 /* We add a room for __rld_map. It will be filled in by the
7417 rtld to contain a pointer to the _r_debug structure. */
7420 else if (SGI_COMPAT (output_bfd)
7421 && strncmp (name, ".compact_rel", 12) == 0)
7422 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
7423 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
7425 s->_raw_size = (sizeof (Elf32_External_Msym)
7426 * (elf_hash_table (info)->dynsymcount
7427 + bfd_count_sections (output_bfd)));
7428 else if (strncmp (name, ".init", 5) != 0)
7430 /* It's not one of our sections, so don't allocate space. */
7436 _bfd_strip_section_from_output (s);
7440 /* Allocate memory for the section contents. */
7441 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
7442 if (s->contents == NULL && s->_raw_size != 0)
7444 bfd_set_error (bfd_error_no_memory);
7449 if (elf_hash_table (info)->dynamic_sections_created)
7451 /* Add some entries to the .dynamic section. We fill in the
7452 values later, in elf_mips_finish_dynamic_sections, but we
7453 must add the entries now so that we get the correct size for
7454 the .dynamic section. The DT_DEBUG entry is filled in by the
7455 dynamic linker and used by the debugger. */
7458 if (SGI_COMPAT (output_bfd))
7460 /* SGI object has the equivalence of DT_DEBUG in the
7461 DT_MIPS_RLD_MAP entry. */
7462 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
7466 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
7472 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
7476 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
7479 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
7481 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
7484 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
7487 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
7491 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
7494 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
7497 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
7499 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
7502 s = bfd_get_section_by_name (dynobj, ".liblist");
7503 BFD_ASSERT (s != NULL);
7505 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
7509 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
7512 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
7516 /* Time stamps in executable files are a bad idea. */
7517 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
7522 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
7527 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
7531 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
7534 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
7537 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
7540 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
7543 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
7546 if (IRIX_COMPAT (dynobj) == ict_irix5
7547 && ! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
7550 if (IRIX_COMPAT (dynobj) == ict_irix6
7551 && (bfd_get_section_by_name
7552 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
7553 && !bfd_elf32_add_dynamic_entry (info, DT_MIPS_OPTIONS, 0))
7556 if (bfd_get_section_by_name (dynobj,
7557 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
7558 && !bfd_elf32_add_dynamic_entry (info, DT_MIPS_MSYM, 0))
7562 /* If we use dynamic linking, we generate a section symbol for each
7563 output section. These are local symbols, which means that they
7564 must come first in the dynamic symbol table.
7565 That means we must increment the dynamic symbol index of every
7566 other dynamic symbol. */
7569 struct mips_got_info *g;
7572 if (elf_hash_table (info)->dynamic_sections_created)
7575 /* We no longer try to restrict the set of sections which get
7576 dynamic symbol table entries, since it fails if we have
7577 other random sections which need dynamic relocations. */
7578 const char * const *namep;
7579 bfd_size_type strindex;
7580 struct bfd_strtab_hash *dynstr;
7582 if (SGI_COMPAT (output_bfd))
7584 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
7585 elf_link_hash_traverse (elf_hash_table (info),
7586 mips_elf_adjust_dynindx,
7588 elf_hash_table (info)->dynsymcount += c;
7590 dynstr = elf_hash_table (info)->dynstr;
7591 BFD_ASSERT (dynstr != NULL);
7593 for (i = 1, namep = mips_elf_dynsym_sec_names;
7597 s = bfd_get_section_by_name (output_bfd, *namep);
7599 elf_section_data (s)->dynindx = i;
7601 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
7602 if (strindex == (bfd_size_type) -1)
7605 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
7611 c = bfd_count_sections (output_bfd);
7612 elf_link_hash_traverse (elf_hash_table (info),
7613 _bfd_elf_link_adjust_dynindx,
7615 elf_hash_table (info)->dynsymcount += c;
7617 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7619 elf_section_data (s)->dynindx = i;
7620 /* These symbols will have no names, so we don't need to
7621 fiddle with dynstr_index. */
7630 /* If NAME is one of the special IRIX6 symbols defined by the linker,
7631 adjust it appropriately now. */
7634 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
7637 Elf_Internal_Sym *sym;
7639 /* The linker script takes care of providing names and values for
7640 these, but we must place them into the right sections. */
7641 static const char* const text_section_symbols[] = {
7644 "__dso_displacement",
7646 "__program_header_table",
7650 static const char* const data_section_symbols[] = {
7658 const char* const *p;
7661 for (i = 0; i < 2; ++i)
7662 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7665 if (strcmp (*p, name) == 0)
7667 /* All of these symbols are given type STT_SECTION by the
7669 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7671 /* The IRIX linker puts these symbols in special sections. */
7673 sym->st_shndx = SHN_MIPS_TEXT;
7675 sym->st_shndx = SHN_MIPS_DATA;
7681 /* Finish up dynamic symbol handling. We set the contents of various
7682 dynamic sections here. */
7685 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7687 struct bfd_link_info *info;
7688 struct elf_link_hash_entry *h;
7689 Elf_Internal_Sym *sym;
7695 struct mips_got_info *g;
7697 struct mips_elf_link_hash_entry *mh;
7699 dynobj = elf_hash_table (info)->dynobj;
7700 gval = sym->st_value;
7701 mh = (struct mips_elf_link_hash_entry *) h;
7703 if (h->plt.offset != (bfd_vma) -1)
7707 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7709 /* This symbol has a stub. Set it up. */
7711 BFD_ASSERT (h->dynindx != -1);
7713 s = bfd_get_section_by_name (dynobj,
7714 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7715 BFD_ASSERT (s != NULL);
7717 /* Fill the stub. */
7719 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7721 bfd_put_32 (output_bfd, STUB_MOVE, p);
7724 /* FIXME: Can h->dynindex be more than 64K? */
7725 if (h->dynindx & 0xffff0000)
7728 bfd_put_32 (output_bfd, STUB_JALR, p);
7730 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7732 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7733 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7735 /* Mark the symbol as undefined. plt.offset != -1 occurs
7736 only for the referenced symbol. */
7737 sym->st_shndx = SHN_UNDEF;
7739 /* The run-time linker uses the st_value field of the symbol
7740 to reset the global offset table entry for this external
7741 to its stub address when unlinking a shared object. */
7742 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7743 sym->st_value = gval;
7746 BFD_ASSERT (h->dynindx != -1);
7748 sgot = bfd_get_section_by_name (dynobj, ".got");
7749 BFD_ASSERT (sgot != NULL);
7750 BFD_ASSERT (elf_section_data (sgot) != NULL);
7751 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7752 BFD_ASSERT (g != NULL);
7754 /* Run through the global symbol table, creating GOT entries for all
7755 the symbols that need them. */
7756 if (h->dynindx >= g->global_gotsym->dynindx)
7762 value = sym->st_value;
7764 /* For an entity defined in a shared object, this will be
7765 NULL. (For functions in shared objects for
7766 which we have created stubs, ST_VALUE will be non-NULL.
7767 That's because such the functions are now no longer defined
7768 in a shared object.) */
7769 value = h->root.u.def.value;
7771 offset = mips_elf_global_got_index (dynobj, h);
7772 bfd_put_32 (output_bfd, value, sgot->contents + offset);
7775 /* Create a .msym entry, if appropriate. */
7776 smsym = bfd_get_section_by_name (dynobj,
7777 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
7780 Elf32_Internal_Msym msym;
7782 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
7783 /* It is undocumented what the `1' indicates, but IRIX6 uses
7785 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
7786 bfd_mips_elf_swap_msym_out
7788 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
7791 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7792 name = h->root.root.string;
7793 if (strcmp (name, "_DYNAMIC") == 0
7794 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7795 sym->st_shndx = SHN_ABS;
7796 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7798 sym->st_shndx = SHN_ABS;
7799 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7802 else if (SGI_COMPAT (output_bfd))
7804 if (strcmp (name, "_gp_disp") == 0)
7806 sym->st_shndx = SHN_ABS;
7807 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7808 sym->st_value = elf_gp (output_bfd);
7810 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7811 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7813 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7814 sym->st_other = STO_PROTECTED;
7816 sym->st_shndx = SHN_MIPS_DATA;
7818 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7820 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7821 sym->st_other = STO_PROTECTED;
7822 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7823 sym->st_shndx = SHN_ABS;
7825 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7827 if (h->type == STT_FUNC)
7828 sym->st_shndx = SHN_MIPS_TEXT;
7829 else if (h->type == STT_OBJECT)
7830 sym->st_shndx = SHN_MIPS_DATA;
7834 /* Handle the IRIX6-specific symbols. */
7835 if (IRIX_COMPAT (output_bfd) == ict_irix6)
7836 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
7838 if (SGI_COMPAT (output_bfd)
7841 if (! mips_elf_hash_table (info)->use_rld_obj_head
7842 && strcmp (name, "__rld_map") == 0)
7844 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7845 BFD_ASSERT (s != NULL);
7846 sym->st_value = s->output_section->vma + s->output_offset;
7847 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7848 if (mips_elf_hash_table (info)->rld_value == 0)
7849 mips_elf_hash_table (info)->rld_value = sym->st_value;
7851 else if (mips_elf_hash_table (info)->use_rld_obj_head
7852 && strcmp (name, "__rld_obj_head") == 0)
7854 /* IRIX6 does not use a .rld_map section. */
7855 if (IRIX_COMPAT (output_bfd) == ict_irix5)
7856 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
7858 mips_elf_hash_table (info)->rld_value = sym->st_value;
7862 /* If this is a mips16 symbol, force the value to be even. */
7863 if (sym->st_other == STO_MIPS16
7864 && (sym->st_value & 1) != 0)
7870 /* Finish up the dynamic sections. */
7873 mips_elf_finish_dynamic_sections (output_bfd, info)
7875 struct bfd_link_info *info;
7880 struct mips_got_info *g;
7882 dynobj = elf_hash_table (info)->dynobj;
7884 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7886 sgot = bfd_get_section_by_name (dynobj, ".got");
7891 BFD_ASSERT (elf_section_data (sgot) != NULL);
7892 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7893 BFD_ASSERT (g != NULL);
7896 if (elf_hash_table (info)->dynamic_sections_created)
7898 Elf32_External_Dyn *dyncon, *dynconend;
7900 BFD_ASSERT (sdyn != NULL);
7901 BFD_ASSERT (g != NULL);
7903 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7904 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7905 for (; dyncon < dynconend; dyncon++)
7907 Elf_Internal_Dyn dyn;
7912 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7920 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7921 BFD_ASSERT (s != NULL);
7922 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7923 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7927 /* Rewrite DT_STRSZ. */
7929 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7930 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7936 case DT_MIPS_CONFLICT:
7939 case DT_MIPS_LIBLIST:
7942 s = bfd_get_section_by_name (output_bfd, name);
7943 BFD_ASSERT (s != NULL);
7944 dyn.d_un.d_ptr = s->vma;
7945 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7948 case DT_MIPS_RLD_VERSION:
7949 dyn.d_un.d_val = 1; /* XXX */
7950 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7954 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7955 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7958 case DT_MIPS_CONFLICTNO:
7960 elemsize = sizeof (Elf32_Conflict);
7963 case DT_MIPS_LIBLISTNO:
7965 elemsize = sizeof (Elf32_Lib);
7967 s = bfd_get_section_by_name (output_bfd, name);
7970 if (s->_cooked_size != 0)
7971 dyn.d_un.d_val = s->_cooked_size / elemsize;
7973 dyn.d_un.d_val = s->_raw_size / elemsize;
7978 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7981 case DT_MIPS_TIME_STAMP:
7982 time ((time_t *) &dyn.d_un.d_val);
7983 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7986 case DT_MIPS_ICHECKSUM:
7990 case DT_MIPS_IVERSION:
7994 case DT_MIPS_BASE_ADDRESS:
7995 s = output_bfd->sections;
7996 BFD_ASSERT (s != NULL);
7997 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7998 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8001 case DT_MIPS_LOCAL_GOTNO:
8002 dyn.d_un.d_val = g->local_gotno;
8003 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8006 case DT_MIPS_SYMTABNO:
8008 elemsize = sizeof (Elf32_External_Sym);
8009 s = bfd_get_section_by_name (output_bfd, name);
8010 BFD_ASSERT (s != NULL);
8012 if (s->_cooked_size != 0)
8013 dyn.d_un.d_val = s->_cooked_size / elemsize;
8015 dyn.d_un.d_val = s->_raw_size / elemsize;
8016 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8019 case DT_MIPS_UNREFEXTNO:
8020 /* The index into the dynamic symbol table which is the
8021 entry of the first external symbol that is not
8022 referenced within the same object. */
8023 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8024 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8027 case DT_MIPS_GOTSYM:
8028 dyn.d_un.d_val = g->global_gotsym->dynindx;
8029 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8032 case DT_MIPS_HIPAGENO:
8033 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8034 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8037 case DT_MIPS_RLD_MAP:
8038 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8039 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8042 case DT_MIPS_OPTIONS:
8043 s = (bfd_get_section_by_name
8044 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8045 dyn.d_un.d_ptr = s->vma;
8046 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8050 s = (bfd_get_section_by_name
8051 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8052 dyn.d_un.d_ptr = s->vma;
8053 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8059 /* The first entry of the global offset table will be filled at
8060 runtime. The second entry will be used by some runtime loaders.
8061 This isn't the case of Irix rld. */
8062 if (sgot != NULL && sgot->_raw_size > 0)
8064 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
8065 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
8069 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
8075 Elf_Internal_Sym sym;
8076 Elf32_compact_rel cpt;
8078 /* Set up the section symbols for the output sections. SGI sets
8079 the STT_NOTYPE attribute for these symbols. Should we do so? */
8081 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
8082 smsym = bfd_get_section_by_name (dynobj,
8083 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8084 if (sdynsym != NULL)
8088 const char * const * namep = mips_elf_dynsym_sec_names;
8093 /* We no longer try to restrict the set of sections which get
8094 dynamic symbol table entries, since it fails if we have
8095 other random sections which need dynamic relocations. */
8096 if (SGI_COMPAT (output_bfd))
8100 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
8106 while ((name = *namep++) != NULL)
8108 s = bfd_get_section_by_name (output_bfd, name);
8111 sym.st_value = s->vma;
8112 dindx = elf_section_data (s)->dynindx;
8113 last = s->vma + s->_raw_size;
8117 sym.st_value = last;
8121 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
8126 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
8128 bfd_elf32_swap_symbol_out (output_bfd, &sym,
8129 (((Elf32_External_Sym *)
8134 /* Set the sh_info field of the output .dynsym section to
8135 the index of the first global symbol. */
8136 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
8137 SIZEOF_MIPS_DYNSYM_SECNAMES;
8142 Elf32_Internal_Msym msym;
8146 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8149 msym.ms_hash_value = 0;
8150 msym.ms_info = ELF32_MS_INFO (0, 1);
8152 for (s = output_bfd->sections; s != NULL; s = s->next)
8157 sym.st_value = s->vma;
8159 indx = elf_section_data (s)->this_idx;
8160 BFD_ASSERT (indx > 0);
8161 sym.st_shndx = indx;
8163 dynindx = elf_section_data (s)->dynindx;
8165 bfd_elf32_swap_symbol_out
8167 (((Elf32_External_Sym *) sdynsym->contents)
8171 bfd_mips_elf_swap_msym_out
8173 (((Elf32_External_Msym *) smsym->contents)
8177 /* Set the sh_info field of the output .dynsym section to
8178 the index of the first global symbol. */
8179 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
8180 bfd_count_sections (output_bfd) + 1;
8184 if (SGI_COMPAT (output_bfd))
8186 /* Write .compact_rel section out. */
8187 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8191 cpt.num = s->reloc_count;
8193 cpt.offset = (s->output_section->filepos
8194 + sizeof (Elf32_External_compact_rel));
8197 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8198 ((Elf32_External_compact_rel *)
8201 /* Clean up a dummy stub function entry in .text. */
8202 s = bfd_get_section_by_name (dynobj,
8203 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8206 file_ptr dummy_offset;
8208 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8209 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8210 memset (s->contents + dummy_offset, 0,
8211 MIPS_FUNCTION_STUB_SIZE);
8216 /* Clean up a first relocation in .rel.dyn. */
8217 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
8218 if (s != NULL && s->_raw_size > 0)
8219 memset (s->contents, 0, sizeof (Elf32_External_Rel));
8225 /* This is almost identical to bfd_generic_get_... except that some
8226 MIPS relocations need to be handled specially. Sigh. */
8229 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8230 relocateable, symbols)
8232 struct bfd_link_info *link_info;
8233 struct bfd_link_order *link_order;
8235 boolean relocateable;
8238 /* Get enough memory to hold the stuff */
8239 bfd *input_bfd = link_order->u.indirect.section->owner;
8240 asection *input_section = link_order->u.indirect.section;
8242 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8243 arelent **reloc_vector = NULL;
8249 reloc_vector = (arelent **) bfd_malloc (reloc_size);
8250 if (reloc_vector == NULL && reloc_size != 0)
8253 /* read in the section */
8254 if (!bfd_get_section_contents (input_bfd,
8258 input_section->_raw_size))
8261 /* We're not relaxing the section, so just copy the size info */
8262 input_section->_cooked_size = input_section->_raw_size;
8263 input_section->reloc_done = true;
8265 reloc_count = bfd_canonicalize_reloc (input_bfd,
8269 if (reloc_count < 0)
8272 if (reloc_count > 0)
8277 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8280 struct bfd_hash_entry *h;
8281 struct bfd_link_hash_entry *lh;
8282 /* Skip all this stuff if we aren't mixing formats. */
8283 if (abfd && input_bfd
8284 && abfd->xvec == input_bfd->xvec)
8288 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8289 lh = (struct bfd_link_hash_entry *) h;
8296 case bfd_link_hash_undefined:
8297 case bfd_link_hash_undefweak:
8298 case bfd_link_hash_common:
8301 case bfd_link_hash_defined:
8302 case bfd_link_hash_defweak:
8304 gp = lh->u.def.value;
8306 case bfd_link_hash_indirect:
8307 case bfd_link_hash_warning:
8309 /* @@FIXME ignoring warning for now */
8311 case bfd_link_hash_new:
8320 for (parent = reloc_vector; *parent != (arelent *) NULL;
8323 char *error_message = (char *) NULL;
8324 bfd_reloc_status_type r;
8326 /* Specific to MIPS: Deal with relocation types that require
8327 knowing the gp of the output bfd. */
8328 asymbol *sym = *(*parent)->sym_ptr_ptr;
8329 if (bfd_is_abs_section (sym->section) && abfd)
8331 /* The special_function wouldn't get called anyways. */
8335 /* The gp isn't there; let the special function code
8336 fall over on its own. */
8338 else if ((*parent)->howto->special_function
8339 == _bfd_mips_elf_gprel16_reloc)
8341 /* bypass special_function call */
8342 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8343 relocateable, (PTR) data, gp);
8344 goto skip_bfd_perform_relocation;
8346 /* end mips specific stuff */
8348 r = bfd_perform_relocation (input_bfd,
8352 relocateable ? abfd : (bfd *) NULL,
8354 skip_bfd_perform_relocation:
8358 asection *os = input_section->output_section;
8360 /* A partial link, so keep the relocs */
8361 os->orelocation[os->reloc_count] = *parent;
8365 if (r != bfd_reloc_ok)
8369 case bfd_reloc_undefined:
8370 if (!((*link_info->callbacks->undefined_symbol)
8371 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8372 input_bfd, input_section, (*parent)->address)))
8375 case bfd_reloc_dangerous:
8376 BFD_ASSERT (error_message != (char *) NULL);
8377 if (!((*link_info->callbacks->reloc_dangerous)
8378 (link_info, error_message, input_bfd, input_section,
8379 (*parent)->address)))
8382 case bfd_reloc_overflow:
8383 if (!((*link_info->callbacks->reloc_overflow)
8384 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8385 (*parent)->howto->name, (*parent)->addend,
8386 input_bfd, input_section, (*parent)->address)))
8389 case bfd_reloc_outofrange:
8398 if (reloc_vector != NULL)
8399 free (reloc_vector);
8403 if (reloc_vector != NULL)
8404 free (reloc_vector);
8407 #define bfd_elf32_bfd_get_relocated_section_contents \
8408 elf32_mips_get_relocated_section_contents
8410 /* ECOFF swapping routines. These are used when dealing with the
8411 .mdebug section, which is in the ECOFF debugging format. */
8412 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8414 /* Symbol table magic number. */
8416 /* Alignment of debugging information. E.g., 4. */
8418 /* Sizes of external symbolic information. */
8419 sizeof (struct hdr_ext),
8420 sizeof (struct dnr_ext),
8421 sizeof (struct pdr_ext),
8422 sizeof (struct sym_ext),
8423 sizeof (struct opt_ext),
8424 sizeof (struct fdr_ext),
8425 sizeof (struct rfd_ext),
8426 sizeof (struct ext_ext),
8427 /* Functions to swap in external symbolic data. */
8436 _bfd_ecoff_swap_tir_in,
8437 _bfd_ecoff_swap_rndx_in,
8438 /* Functions to swap out external symbolic data. */
8447 _bfd_ecoff_swap_tir_out,
8448 _bfd_ecoff_swap_rndx_out,
8449 /* Function to read in symbolic data. */
8450 _bfd_mips_elf_read_ecoff_info
8453 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8454 #define TARGET_LITTLE_NAME "elf32-littlemips"
8455 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8456 #define TARGET_BIG_NAME "elf32-bigmips"
8457 #define ELF_ARCH bfd_arch_mips
8458 #define ELF_MACHINE_CODE EM_MIPS
8460 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8461 a value of 0x1000, and we are compatible. */
8462 #define ELF_MAXPAGESIZE 0x1000
8464 #define elf_backend_collect true
8465 #define elf_backend_type_change_ok true
8466 #define elf_backend_can_gc_sections true
8467 #define elf_info_to_howto mips_info_to_howto_rela
8468 #define elf_info_to_howto_rel mips_info_to_howto_rel
8469 #define elf_backend_sym_is_global mips_elf_sym_is_global
8470 #define elf_backend_object_p mips_elf32_object_p
8471 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
8472 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8473 #define elf_backend_section_from_bfd_section \
8474 _bfd_mips_elf_section_from_bfd_section
8475 #define elf_backend_section_processing mips_elf32_section_processing
8476 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8477 #define elf_backend_additional_program_headers \
8478 mips_elf_additional_program_headers
8479 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
8480 #define elf_backend_final_write_processing \
8481 _bfd_mips_elf_final_write_processing
8482 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8484 #define bfd_elf32_bfd_is_local_label_name \
8485 mips_elf_is_local_label_name
8486 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8487 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8488 #define bfd_elf32_bfd_link_hash_table_create \
8489 mips_elf_link_hash_table_create
8490 #define bfd_elf32_bfd_final_link mips_elf_final_link
8491 #define bfd_elf32_bfd_copy_private_bfd_data \
8492 _bfd_mips_elf_copy_private_bfd_data
8493 #define bfd_elf32_bfd_merge_private_bfd_data \
8494 _bfd_mips_elf_merge_private_bfd_data
8495 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
8496 #define bfd_elf32_bfd_print_private_bfd_data \
8497 _bfd_mips_elf_print_private_bfd_data
8498 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
8499 #define elf_backend_create_dynamic_sections \
8500 mips_elf_create_dynamic_sections
8501 #define elf_backend_check_relocs mips_elf_check_relocs
8502 #define elf_backend_adjust_dynamic_symbol \
8503 mips_elf_adjust_dynamic_symbol
8504 #define elf_backend_always_size_sections \
8505 mips_elf_always_size_sections
8506 #define elf_backend_size_dynamic_sections \
8507 mips_elf_size_dynamic_sections
8508 #define elf_backend_relocate_section mips_elf_relocate_section
8509 #define elf_backend_link_output_symbol_hook \
8510 mips_elf_link_output_symbol_hook
8511 #define elf_backend_finish_dynamic_symbol \
8512 mips_elf_finish_dynamic_symbol
8513 #define elf_backend_finish_dynamic_sections \
8514 mips_elf_finish_dynamic_sections
8515 #define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
8516 #define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
8518 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8519 #define elf_backend_plt_header_size 0
8521 #include "elf32-target.h"