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 static bfd_reloc_status_type mips32_64bit_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48 static void mips_info_to_howto_rel
49 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
50 static void mips_info_to_howto_rela
51 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
52 static void bfd_mips_elf32_swap_gptab_in
53 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
54 static void bfd_mips_elf32_swap_gptab_out
55 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
56 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
57 static boolean mips_elf32_object_p PARAMS ((bfd *));
58 static boolean mips_elf_create_procedure_table
59 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
60 struct ecoff_debug_info *));
61 static int mips_elf_additional_program_headers PARAMS ((bfd *));
62 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
63 static INLINE int elf_mips_isa PARAMS ((flagword));
64 static INLINE int elf_mips_mach PARAMS ((flagword));
65 static INLINE char* elf_mips_abi_name PARAMS ((flagword));
66 static boolean mips_elf32_section_from_shdr
67 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
68 static boolean mips_elf32_section_processing
69 PARAMS ((bfd *, Elf32_Internal_Shdr *));
70 static boolean mips_elf_is_local_label_name
71 PARAMS ((bfd *, const char *));
72 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
73 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
74 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
76 static int gptab_compare PARAMS ((const void *, const void *));
77 static boolean mips_elf_final_link
78 PARAMS ((bfd *, struct bfd_link_info *));
79 static void mips_elf_relocate_hi16
80 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
82 static boolean mips_elf_relocate_got_local
83 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
84 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
85 static void mips_elf_relocate_global_got
86 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
87 static bfd_reloc_status_type mips16_jump_reloc
88 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
89 static bfd_reloc_status_type mips16_gprel_reloc
90 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
91 static boolean mips_elf_adjust_dynindx
92 PARAMS ((struct elf_link_hash_entry *, PTR));
93 static boolean mips_elf_relocate_section
94 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
95 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
96 static boolean mips_elf_link_output_symbol_hook
97 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
99 static boolean mips_elf_create_dynamic_sections
100 PARAMS ((bfd *, struct bfd_link_info *));
101 static boolean mips_elf_create_compact_rel_section
102 PARAMS ((bfd *, struct bfd_link_info *));
103 static boolean mips_elf_create_got_section
104 PARAMS ((bfd *, struct bfd_link_info *));
105 static boolean mips_elf_check_relocs
106 PARAMS ((bfd *, struct bfd_link_info *, asection *,
107 const Elf_Internal_Rela *));
108 static boolean mips_elf_adjust_dynamic_symbol
109 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
110 static boolean mips_elf_always_size_sections
111 PARAMS ((bfd *, struct bfd_link_info *));
112 static boolean mips_elf_size_dynamic_sections
113 PARAMS ((bfd *, struct bfd_link_info *));
114 static boolean mips_elf_finish_dynamic_symbol
115 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
116 Elf_Internal_Sym *));
117 static boolean mips_elf_finish_dynamic_sections
118 PARAMS ((bfd *, struct bfd_link_info *));
119 static boolean mips_elf_add_symbol_hook
120 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
121 const char **, flagword *, asection **, bfd_vma *));
122 static bfd_reloc_status_type mips_elf_final_gp
123 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
124 static bfd_byte *elf32_mips_get_relocated_section_contents
125 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
126 bfd_byte *, boolean, asymbol **));
128 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
129 executables. FIXME: At the moment, we default to always generating
130 Irix 5 executables. */
132 #define SGI_COMPAT(abfd) (1)
134 /* This structure is used to hold .got information when linking. It
135 is stored in the tdata field of the bfd_elf_section_data structure. */
139 /* The symbol index of the first global .got symbol. */
140 unsigned long global_gotsym;
141 /* The number of local .got entries. */
142 unsigned int local_gotno;
143 /* The number of local .got entries we have used. */
144 unsigned int assigned_gotno;
147 /* The number of local .got entries we reserve. */
148 #define MIPS_RESERVED_GOTNO (2)
150 /* Instructions which appear in a stub. For some reason the stub is
151 slightly different on an SGI system. */
152 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
153 #define STUB_LW(abfd) \
155 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
156 : 0x8f998000) /* lw t9,0x8000(gp) */
157 #define STUB_MOVE 0x03e07825 /* move t7,ra */
158 #define STUB_JALR 0x0320f809 /* jal t9 */
159 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
160 #define MIPS_FUNCTION_STUB_SIZE (16)
163 /* We no longer try to identify particular sections for the .dynsym
164 section. When we do, we wind up crashing if there are other random
165 sections with relocations. */
167 /* Names of sections which appear in the .dynsym section in an Irix 5
170 static const char * const mips_elf_dynsym_sec_names[] =
183 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
184 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
186 /* The number of entries in mips_elf_dynsym_sec_names which go in the
189 #define MIPS_TEXT_DYNSYM_SECNO (3)
193 /* The names of the runtime procedure table symbols used on Irix 5. */
195 static const char * const mips_elf_dynsym_rtproc_names[] =
198 "_procedure_string_table",
199 "_procedure_table_size",
203 /* These structures are used to generate the .compact_rel section on
208 unsigned long id1; /* Always one? */
209 unsigned long num; /* Number of compact relocation entries. */
210 unsigned long id2; /* Always two? */
211 unsigned long offset; /* The file offset of the first relocation. */
212 unsigned long reserved0; /* Zero? */
213 unsigned long reserved1; /* Zero? */
222 bfd_byte reserved0[4];
223 bfd_byte reserved1[4];
224 } Elf32_External_compact_rel;
228 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
229 unsigned int rtype : 4; /* Relocation types. See below. */
230 unsigned int dist2to : 8;
231 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
232 unsigned long konst; /* KONST field. See below. */
233 unsigned long vaddr; /* VADDR to be relocated. */
238 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
239 unsigned int rtype : 4; /* Relocation types. See below. */
240 unsigned int dist2to : 8;
241 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
242 unsigned long konst; /* KONST field. See below. */
250 } Elf32_External_crinfo;
256 } Elf32_External_crinfo2;
258 /* These are the constants used to swap the bitfields in a crinfo. */
260 #define CRINFO_CTYPE (0x1)
261 #define CRINFO_CTYPE_SH (31)
262 #define CRINFO_RTYPE (0xf)
263 #define CRINFO_RTYPE_SH (27)
264 #define CRINFO_DIST2TO (0xff)
265 #define CRINFO_DIST2TO_SH (19)
266 #define CRINFO_RELVADDR (0x7ffff)
267 #define CRINFO_RELVADDR_SH (0)
269 /* A compact relocation info has long (3 words) or short (2 words)
270 formats. A short format doesn't have VADDR field and relvaddr
271 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
272 #define CRF_MIPS_LONG 1
273 #define CRF_MIPS_SHORT 0
275 /* There are 4 types of compact relocation at least. The value KONST
276 has different meaning for each type:
279 CT_MIPS_REL32 Address in data
280 CT_MIPS_WORD Address in word (XXX)
281 CT_MIPS_GPHI_LO GP - vaddr
282 CT_MIPS_JMPAD Address to jump
285 #define CRT_MIPS_REL32 0xa
286 #define CRT_MIPS_WORD 0xb
287 #define CRT_MIPS_GPHI_LO 0xc
288 #define CRT_MIPS_JMPAD 0xd
290 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
291 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
292 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
293 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
295 static void bfd_elf32_swap_compact_rel_out
296 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
297 static void bfd_elf32_swap_crinfo_out
298 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
300 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
302 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
303 from smaller values. Start with zero, widen, *then* decrement. */
304 #define MINUS_ONE (((bfd_vma)0) - 1)
306 static reloc_howto_type elf_mips_howto_table[] =
309 HOWTO (R_MIPS_NONE, /* type */
311 0, /* size (0 = byte, 1 = short, 2 = long) */
313 false, /* pc_relative */
315 complain_overflow_dont, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_MIPS_NONE", /* name */
318 false, /* partial_inplace */
321 false), /* pcrel_offset */
323 /* 16 bit relocation. */
324 HOWTO (R_MIPS_16, /* type */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
328 false, /* pc_relative */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_MIPS_16", /* name */
333 true, /* partial_inplace */
334 0xffff, /* src_mask */
335 0xffff, /* dst_mask */
336 false), /* pcrel_offset */
338 /* 32 bit relocation. */
339 HOWTO (R_MIPS_32, /* type */
341 2, /* size (0 = byte, 1 = short, 2 = long) */
343 false, /* pc_relative */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_MIPS_32", /* name */
348 true, /* partial_inplace */
349 0xffffffff, /* src_mask */
350 0xffffffff, /* dst_mask */
351 false), /* pcrel_offset */
353 /* 32 bit symbol relative relocation. */
354 HOWTO (R_MIPS_REL32, /* type */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
358 false, /* pc_relative */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_MIPS_REL32", /* name */
363 true, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 false), /* pcrel_offset */
368 /* 26 bit branch address. */
369 HOWTO (R_MIPS_26, /* type */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
373 false, /* pc_relative */
375 complain_overflow_dont, /* complain_on_overflow */
376 /* This needs complex overflow
377 detection, because the upper four
378 bits must match the PC. */
379 bfd_elf_generic_reloc, /* special_function */
380 "R_MIPS_26", /* name */
381 true, /* partial_inplace */
382 0x3ffffff, /* src_mask */
383 0x3ffffff, /* dst_mask */
384 false), /* pcrel_offset */
386 /* High 16 bits of symbol value. */
387 HOWTO (R_MIPS_HI16, /* type */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
391 false, /* pc_relative */
393 complain_overflow_dont, /* complain_on_overflow */
394 _bfd_mips_elf_hi16_reloc, /* special_function */
395 "R_MIPS_HI16", /* name */
396 true, /* partial_inplace */
397 0xffff, /* src_mask */
398 0xffff, /* dst_mask */
399 false), /* pcrel_offset */
401 /* Low 16 bits of symbol value. */
402 HOWTO (R_MIPS_LO16, /* type */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
406 false, /* pc_relative */
408 complain_overflow_dont, /* complain_on_overflow */
409 _bfd_mips_elf_lo16_reloc, /* special_function */
410 "R_MIPS_LO16", /* name */
411 true, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 false), /* pcrel_offset */
416 /* GP relative reference. */
417 HOWTO (R_MIPS_GPREL16, /* type */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
421 false, /* pc_relative */
423 complain_overflow_signed, /* complain_on_overflow */
424 _bfd_mips_elf_gprel16_reloc, /* special_function */
425 "R_MIPS_GPREL16", /* name */
426 true, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 false), /* pcrel_offset */
431 /* Reference to literal section. */
432 HOWTO (R_MIPS_LITERAL, /* type */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
436 false, /* pc_relative */
438 complain_overflow_signed, /* complain_on_overflow */
439 _bfd_mips_elf_gprel16_reloc, /* special_function */
440 "R_MIPS_LITERAL", /* name */
441 true, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 false), /* pcrel_offset */
446 /* Reference to global offset table. */
447 HOWTO (R_MIPS_GOT16, /* type */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
451 false, /* pc_relative */
453 complain_overflow_signed, /* complain_on_overflow */
454 _bfd_mips_elf_got16_reloc, /* special_function */
455 "R_MIPS_GOT16", /* name */
456 false, /* partial_inplace */
458 0xffff, /* dst_mask */
459 false), /* pcrel_offset */
461 /* 16 bit PC relative reference. */
462 HOWTO (R_MIPS_PC16, /* type */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
466 true, /* pc_relative */
468 complain_overflow_signed, /* complain_on_overflow */
469 bfd_elf_generic_reloc, /* special_function */
470 "R_MIPS_PC16", /* name */
471 true, /* partial_inplace */
472 0xffff, /* src_mask */
473 0xffff, /* dst_mask */
474 false), /* pcrel_offset */
476 /* 16 bit call through global offset table. */
477 /* FIXME: This is not handled correctly. */
478 HOWTO (R_MIPS_CALL16, /* type */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
482 false, /* pc_relative */
484 complain_overflow_signed, /* complain_on_overflow */
485 bfd_elf_generic_reloc, /* special_function */
486 "R_MIPS_CALL16", /* name */
487 false, /* partial_inplace */
489 0xffff, /* dst_mask */
490 false), /* pcrel_offset */
492 /* 32 bit GP relative reference. */
493 HOWTO (R_MIPS_GPREL32, /* type */
495 2, /* size (0 = byte, 1 = short, 2 = long) */
497 false, /* pc_relative */
499 complain_overflow_bitfield, /* complain_on_overflow */
500 _bfd_mips_elf_gprel32_reloc, /* special_function */
501 "R_MIPS_GPREL32", /* name */
502 true, /* partial_inplace */
503 0xffffffff, /* src_mask */
504 0xffffffff, /* dst_mask */
505 false), /* pcrel_offset */
507 /* The remaining relocs are defined on Irix 5, although they are
508 not defined by the ABI. */
513 /* A 5 bit shift field. */
514 HOWTO (R_MIPS_SHIFT5, /* type */
516 2, /* size (0 = byte, 1 = short, 2 = long) */
518 false, /* pc_relative */
520 complain_overflow_bitfield, /* complain_on_overflow */
521 bfd_elf_generic_reloc, /* special_function */
522 "R_MIPS_SHIFT5", /* name */
523 true, /* partial_inplace */
524 0x000007c0, /* src_mask */
525 0x000007c0, /* dst_mask */
526 false), /* pcrel_offset */
528 /* A 6 bit shift field. */
529 /* FIXME: This is not handled correctly; a special function is
530 needed to put the most significant bit in the right place. */
531 HOWTO (R_MIPS_SHIFT6, /* type */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
535 false, /* pc_relative */
537 complain_overflow_bitfield, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_MIPS_SHIFT6", /* name */
540 true, /* partial_inplace */
541 0x000007c4, /* src_mask */
542 0x000007c4, /* dst_mask */
543 false), /* pcrel_offset */
545 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
546 are 64 bits long; the upper 32 bits are simply a sign extension.
547 The fields of the howto should be the same as for R_MIPS_32,
548 other than the type, name, and special_function. */
549 HOWTO (R_MIPS_64, /* type */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
553 false, /* pc_relative */
555 complain_overflow_bitfield, /* complain_on_overflow */
556 mips32_64bit_reloc, /* special_function */
557 "R_MIPS_64", /* name */
558 true, /* partial_inplace */
559 0xffffffff, /* src_mask */
560 0xffffffff, /* dst_mask */
561 false), /* pcrel_offset */
563 /* Displacement in the global offset table. */
564 /* FIXME: Not handled correctly. */
565 HOWTO (R_MIPS_GOT_DISP, /* type */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
569 false, /* pc_relative */
571 complain_overflow_bitfield, /* complain_on_overflow */
572 bfd_elf_generic_reloc, /* special_function */
573 "R_MIPS_GOT_DISP", /* name */
574 true, /* partial_inplace */
575 0x0000ffff, /* src_mask */
576 0x0000ffff, /* dst_mask */
577 false), /* pcrel_offset */
579 /* Displacement to page pointer in the global offset table. */
580 /* FIXME: Not handled correctly. */
581 HOWTO (R_MIPS_GOT_PAGE, /* type */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
585 false, /* pc_relative */
587 complain_overflow_bitfield, /* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_MIPS_GOT_PAGE", /* name */
590 true, /* partial_inplace */
591 0x0000ffff, /* src_mask */
592 0x0000ffff, /* dst_mask */
593 false), /* pcrel_offset */
595 /* Offset from page pointer in the global offset table. */
596 /* FIXME: Not handled correctly. */
597 HOWTO (R_MIPS_GOT_OFST, /* type */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
601 false, /* pc_relative */
603 complain_overflow_bitfield, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_MIPS_GOT_OFST", /* name */
606 true, /* partial_inplace */
607 0x0000ffff, /* src_mask */
608 0x0000ffff, /* dst_mask */
609 false), /* pcrel_offset */
611 /* High 16 bits of displacement in global offset table. */
612 /* FIXME: Not handled correctly. */
613 HOWTO (R_MIPS_GOT_HI16, /* type */
615 2, /* size (0 = byte, 1 = short, 2 = long) */
617 false, /* pc_relative */
619 complain_overflow_dont, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_MIPS_GOT_HI16", /* name */
622 true, /* partial_inplace */
623 0x0000ffff, /* src_mask */
624 0x0000ffff, /* dst_mask */
625 false), /* pcrel_offset */
627 /* Low 16 bits of displacement in global offset table. */
628 /* FIXME: Not handled correctly. */
629 HOWTO (R_MIPS_GOT_LO16, /* type */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
633 false, /* pc_relative */
635 complain_overflow_dont, /* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_MIPS_GOT_LO16", /* name */
638 true, /* partial_inplace */
639 0x0000ffff, /* src_mask */
640 0x0000ffff, /* dst_mask */
641 false), /* pcrel_offset */
643 /* 64 bit subtraction. Used in the N32 ABI. */
644 /* FIXME: Not handled correctly. */
645 HOWTO (R_MIPS_SUB, /* type */
647 4, /* size (0 = byte, 1 = short, 2 = long) */
649 false, /* pc_relative */
651 complain_overflow_bitfield, /* complain_on_overflow */
652 bfd_elf_generic_reloc, /* special_function */
653 "R_MIPS_SUB", /* name */
654 true, /* partial_inplace */
655 MINUS_ONE, /* src_mask */
656 MINUS_ONE, /* dst_mask */
657 false), /* pcrel_offset */
659 /* Used to cause the linker to insert and delete instructions? */
664 /* Get the higher values of a 64 bit addend. Presumably not used in
669 /* High 16 bits of displacement in global offset table. */
670 /* FIXME: Not handled correctly. */
671 HOWTO (R_MIPS_CALL_HI16, /* type */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
675 false, /* pc_relative */
677 complain_overflow_dont, /* complain_on_overflow */
678 bfd_elf_generic_reloc, /* special_function */
679 "R_MIPS_CALL_HI16", /* name */
680 true, /* partial_inplace */
681 0x0000ffff, /* src_mask */
682 0x0000ffff, /* dst_mask */
683 false), /* pcrel_offset */
685 /* Low 16 bits of displacement in global offset table. */
686 /* FIXME: Not handled correctly. */
687 HOWTO (R_MIPS_CALL_LO16, /* type */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
691 false, /* pc_relative */
693 complain_overflow_dont, /* complain_on_overflow */
694 bfd_elf_generic_reloc, /* special_function */
695 "R_MIPS_CALL_LO16", /* name */
696 true, /* partial_inplace */
697 0x0000ffff, /* src_mask */
698 0x0000ffff, /* dst_mask */
699 false), /* pcrel_offset */
703 { R_MIPS_ADD_IMMEDIATE },
707 /* Protected jump conversion. This is an optimization hint. No
708 relocation is required for correctness. */
709 HOWTO (R_MIPS_JALR, /* type */
711 0, /* size (0 = byte, 1 = short, 2 = long) */
713 false, /* pc_relative */
715 complain_overflow_dont, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_MIPS_JALR", /* name */
718 false, /* partial_inplace */
719 0x00000000, /* src_mask */
720 0x00000000, /* dst_mask */
721 false), /* pcrel_offset */
724 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
725 is a hack to make the linker think that we need 64 bit values. */
726 static reloc_howto_type elf_mips_ctor64_howto =
727 HOWTO (R_MIPS_64, /* type */
729 4, /* size (0 = byte, 1 = short, 2 = long) */
731 false, /* pc_relative */
733 complain_overflow_signed, /* complain_on_overflow */
734 mips32_64bit_reloc, /* special_function */
735 "R_MIPS_64", /* name */
736 true, /* partial_inplace */
737 0xffffffff, /* src_mask */
738 0xffffffff, /* dst_mask */
739 false); /* pcrel_offset */
741 /* The reloc used for the mips16 jump instruction. */
742 static reloc_howto_type elf_mips16_jump_howto =
743 HOWTO (R_MIPS16_26, /* type */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
747 false, /* pc_relative */
749 complain_overflow_dont, /* complain_on_overflow */
750 /* This needs complex overflow
751 detection, because the upper four
752 bits must match the PC. */
753 mips16_jump_reloc, /* special_function */
754 "R_MIPS16_26", /* name */
755 true, /* partial_inplace */
756 0x3ffffff, /* src_mask */
757 0x3ffffff, /* dst_mask */
758 false); /* pcrel_offset */
760 /* The reloc used for the mips16 gprel instruction. The src_mask and
761 dsk_mask for this howto do not reflect the actual instruction, in
762 which the value is not contiguous; the masks are for the
763 convenience of the relocate_section routine. */
764 static reloc_howto_type elf_mips16_gprel_howto =
765 HOWTO (R_MIPS16_GPREL, /* type */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
769 false, /* pc_relative */
771 complain_overflow_signed, /* complain_on_overflow */
772 mips16_gprel_reloc, /* special_function */
773 "R_MIPS16_GPREL", /* name */
774 true, /* partial_inplace */
775 0xffff, /* src_mask */
776 0xffff, /* dst_mask */
777 false); /* pcrel_offset */
780 /* GNU extension to record C++ vtable hierarchy */
781 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
782 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
784 2, /* size (0 = byte, 1 = short, 2 = long) */
786 false, /* pc_relative */
788 complain_overflow_dont, /* complain_on_overflow */
789 NULL, /* special_function */
790 "R_MIPS_GNU_VTINHERIT", /* name */
791 false, /* partial_inplace */
794 false); /* pcrel_offset */
796 /* GNU extension to record C++ vtable member usage */
797 static reloc_howto_type elf_mips_gnu_vtentry_howto =
798 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
800 2, /* size (0 = byte, 1 = short, 2 = long) */
802 false, /* pc_relative */
804 complain_overflow_dont, /* complain_on_overflow */
805 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
806 "R_MIPS_GNU_VTENTRY", /* name */
807 false, /* partial_inplace */
810 false); /* pcrel_offset */
812 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
813 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
814 the HI16. Here we just save the information we need; we do the
815 actual relocation when we see the LO16. MIPS ELF requires that the
816 LO16 immediately follow the HI16. As a GNU extension, we permit an
817 arbitrary number of HI16 relocs to be associated with a single LO16
818 reloc. This extension permits gcc to output the HI and LO relocs
823 struct mips_hi16 *next;
828 /* FIXME: This should not be a static variable. */
830 static struct mips_hi16 *mips_hi16_list;
832 bfd_reloc_status_type
833 _bfd_mips_elf_hi16_reloc (abfd,
841 arelent *reloc_entry;
844 asection *input_section;
846 char **error_message;
848 bfd_reloc_status_type ret;
852 /* If we're relocating, and this an external symbol, we don't want
853 to change anything. */
854 if (output_bfd != (bfd *) NULL
855 && (symbol->flags & BSF_SECTION_SYM) == 0
856 && reloc_entry->addend == 0)
858 reloc_entry->address += input_section->output_offset;
864 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
866 boolean relocateable;
869 if (ret == bfd_reloc_undefined)
872 if (output_bfd != NULL)
876 relocateable = false;
877 output_bfd = symbol->section->output_section->owner;
880 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
882 if (ret != bfd_reloc_ok)
885 relocation = gp - reloc_entry->address;
889 if (bfd_is_und_section (symbol->section)
890 && output_bfd == (bfd *) NULL)
891 ret = bfd_reloc_undefined;
893 if (bfd_is_com_section (symbol->section))
896 relocation = symbol->value;
899 relocation += symbol->section->output_section->vma;
900 relocation += symbol->section->output_offset;
901 relocation += reloc_entry->addend;
903 if (reloc_entry->address > input_section->_cooked_size)
904 return bfd_reloc_outofrange;
906 /* Save the information, and let LO16 do the actual relocation. */
907 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
909 return bfd_reloc_outofrange;
910 n->addr = (bfd_byte *) data + reloc_entry->address;
911 n->addend = relocation;
912 n->next = mips_hi16_list;
915 if (output_bfd != (bfd *) NULL)
916 reloc_entry->address += input_section->output_offset;
921 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
922 inplace relocation; this function exists in order to do the
923 R_MIPS_HI16 relocation described above. */
925 bfd_reloc_status_type
926 _bfd_mips_elf_lo16_reloc (abfd,
934 arelent *reloc_entry;
937 asection *input_section;
939 char **error_message;
941 arelent gp_disp_relent;
943 if (mips_hi16_list != NULL)
953 struct mips_hi16 *next;
955 /* Do the HI16 relocation. Note that we actually don't need
956 to know anything about the LO16 itself, except where to
957 find the low 16 bits of the addend needed by the LO16. */
958 insn = bfd_get_32 (abfd, l->addr);
959 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
961 val = ((insn & 0xffff) << 16) + vallo;
964 /* The low order 16 bits are always treated as a signed
965 value. Therefore, a negative value in the low order bits
966 requires an adjustment in the high order bits. We need
967 to make this adjustment in two ways: once for the bits we
968 took from the data, and once for the bits we are putting
969 back in to the data. */
970 if ((vallo & 0x8000) != 0)
972 if ((val & 0x8000) != 0)
975 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
976 bfd_put_32 (abfd, insn, l->addr);
978 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
980 gp_disp_relent = *reloc_entry;
981 reloc_entry = &gp_disp_relent;
982 reloc_entry->addend = l->addend;
990 mips_hi16_list = NULL;
992 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
994 bfd_reloc_status_type ret;
995 bfd_vma gp, relocation;
997 /* FIXME: Does this case ever occur? */
999 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1000 if (ret != bfd_reloc_ok)
1003 relocation = gp - reloc_entry->address;
1004 relocation += symbol->section->output_section->vma;
1005 relocation += symbol->section->output_offset;
1006 relocation += reloc_entry->addend;
1008 if (reloc_entry->address > input_section->_cooked_size)
1009 return bfd_reloc_outofrange;
1011 gp_disp_relent = *reloc_entry;
1012 reloc_entry = &gp_disp_relent;
1013 reloc_entry->addend = relocation - 4;
1016 /* Now do the LO16 reloc in the usual way. */
1017 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1018 input_section, output_bfd, error_message);
1021 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1022 table used for PIC code. If the symbol is an external symbol, the
1023 instruction is modified to contain the offset of the appropriate
1024 entry in the global offset table. If the symbol is a section
1025 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1026 addends are combined to form the real addend against the section
1027 symbol; the GOT16 is modified to contain the offset of an entry in
1028 the global offset table, and the LO16 is modified to offset it
1029 appropriately. Thus an offset larger than 16 bits requires a
1030 modified value in the global offset table.
1032 This implementation suffices for the assembler, but the linker does
1033 not yet know how to create global offset tables. */
1035 bfd_reloc_status_type
1036 _bfd_mips_elf_got16_reloc (abfd,
1044 arelent *reloc_entry;
1047 asection *input_section;
1049 char **error_message;
1051 /* If we're relocating, and this an external symbol, we don't want
1052 to change anything. */
1053 if (output_bfd != (bfd *) NULL
1054 && (symbol->flags & BSF_SECTION_SYM) == 0
1055 && reloc_entry->addend == 0)
1057 reloc_entry->address += input_section->output_offset;
1058 return bfd_reloc_ok;
1061 /* If we're relocating, and this is a local symbol, we can handle it
1063 if (output_bfd != (bfd *) NULL
1064 && (symbol->flags & BSF_SECTION_SYM) != 0)
1065 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1066 input_section, output_bfd, error_message);
1071 /* We have to figure out the gp value, so that we can adjust the
1072 symbol value correctly. We look up the symbol _gp in the output
1073 BFD. If we can't find it, we're stuck. We cache it in the ELF
1074 target data. We don't need to adjust the symbol value for an
1075 external symbol if we are producing relocateable output. */
1077 static bfd_reloc_status_type
1078 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1081 boolean relocateable;
1082 char **error_message;
1085 if (bfd_is_und_section (symbol->section)
1089 return bfd_reloc_undefined;
1092 *pgp = _bfd_get_gp_value (output_bfd);
1095 || (symbol->flags & BSF_SECTION_SYM) != 0))
1099 /* Make up a value. */
1100 *pgp = symbol->section->output_section->vma + 0x4000;
1101 _bfd_set_gp_value (output_bfd, *pgp);
1109 count = bfd_get_symcount (output_bfd);
1110 sym = bfd_get_outsymbols (output_bfd);
1112 if (sym == (asymbol **) NULL)
1116 for (i = 0; i < count; i++, sym++)
1118 register CONST char *name;
1120 name = bfd_asymbol_name (*sym);
1121 if (*name == '_' && strcmp (name, "_gp") == 0)
1123 *pgp = bfd_asymbol_value (*sym);
1124 _bfd_set_gp_value (output_bfd, *pgp);
1132 /* Only get the error once. */
1134 _bfd_set_gp_value (output_bfd, *pgp);
1136 (char *) _("GP relative relocation when _gp not defined");
1137 return bfd_reloc_dangerous;
1142 return bfd_reloc_ok;
1145 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1146 become the offset from the gp register. This function also handles
1147 R_MIPS_LITERAL relocations, although those can be handled more
1148 cleverly because the entries in the .lit8 and .lit4 sections can be
1151 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1152 arelent *, asection *,
1153 boolean, PTR, bfd_vma));
1155 bfd_reloc_status_type
1156 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1157 output_bfd, error_message)
1159 arelent *reloc_entry;
1162 asection *input_section;
1164 char **error_message;
1166 boolean relocateable;
1167 bfd_reloc_status_type ret;
1170 /* If we're relocating, and this is an external symbol with no
1171 addend, we don't want to change anything. We will only have an
1172 addend if this is a newly created reloc, not read from an ELF
1174 if (output_bfd != (bfd *) NULL
1175 && (symbol->flags & BSF_SECTION_SYM) == 0
1176 && reloc_entry->addend == 0)
1178 reloc_entry->address += input_section->output_offset;
1179 return bfd_reloc_ok;
1182 if (output_bfd != (bfd *) NULL)
1183 relocateable = true;
1186 relocateable = false;
1187 output_bfd = symbol->section->output_section->owner;
1190 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1192 if (ret != bfd_reloc_ok)
1195 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1196 relocateable, data, gp);
1199 static bfd_reloc_status_type
1200 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1204 arelent *reloc_entry;
1205 asection *input_section;
1206 boolean relocateable;
1214 if (bfd_is_com_section (symbol->section))
1217 relocation = symbol->value;
1219 relocation += symbol->section->output_section->vma;
1220 relocation += symbol->section->output_offset;
1222 if (reloc_entry->address > input_section->_cooked_size)
1223 return bfd_reloc_outofrange;
1225 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1227 /* Set val to the offset into the section or symbol. */
1228 if (reloc_entry->howto->src_mask == 0)
1230 /* This case occurs with the 64-bit MIPS ELF ABI. */
1231 val = reloc_entry->addend;
1235 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1240 /* Adjust val for the final section location and GP value. If we
1241 are producing relocateable output, we don't want to do this for
1242 an external symbol. */
1244 || (symbol->flags & BSF_SECTION_SYM) != 0)
1245 val += relocation - gp;
1247 insn = (insn &~ 0xffff) | (val & 0xffff);
1248 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1251 reloc_entry->address += input_section->output_offset;
1253 /* Make sure it fit in 16 bits. */
1254 if (val >= 0x8000 && val < 0xffff8000)
1255 return bfd_reloc_overflow;
1257 return bfd_reloc_ok;
1260 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1261 from the gp register? XXX */
1263 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1264 arelent *, asection *,
1265 boolean, PTR, bfd_vma));
1267 bfd_reloc_status_type
1268 _bfd_mips_elf_gprel32_reloc (abfd,
1276 arelent *reloc_entry;
1279 asection *input_section;
1281 char **error_message;
1283 boolean relocateable;
1284 bfd_reloc_status_type ret;
1287 /* If we're relocating, and this is an external symbol with no
1288 addend, we don't want to change anything. We will only have an
1289 addend if this is a newly created reloc, not read from an ELF
1291 if (output_bfd != (bfd *) NULL
1292 && (symbol->flags & BSF_SECTION_SYM) == 0
1293 && reloc_entry->addend == 0)
1295 *error_message = (char *)
1296 _("32bits gp relative relocation occurs for an external symbol");
1297 return bfd_reloc_outofrange;
1300 if (output_bfd != (bfd *) NULL)
1302 relocateable = true;
1303 gp = _bfd_get_gp_value (output_bfd);
1307 relocateable = false;
1308 output_bfd = symbol->section->output_section->owner;
1310 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1311 error_message, &gp);
1312 if (ret != bfd_reloc_ok)
1316 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1317 relocateable, data, gp);
1320 static bfd_reloc_status_type
1321 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1325 arelent *reloc_entry;
1326 asection *input_section;
1327 boolean relocateable;
1334 if (bfd_is_com_section (symbol->section))
1337 relocation = symbol->value;
1339 relocation += symbol->section->output_section->vma;
1340 relocation += symbol->section->output_offset;
1342 if (reloc_entry->address > input_section->_cooked_size)
1343 return bfd_reloc_outofrange;
1345 if (reloc_entry->howto->src_mask == 0)
1347 /* This case arises with the 64-bit MIPS ELF ABI. */
1351 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1353 /* Set val to the offset into the section or symbol. */
1354 val += reloc_entry->addend;
1356 /* Adjust val for the final section location and GP value. If we
1357 are producing relocateable output, we don't want to do this for
1358 an external symbol. */
1360 || (symbol->flags & BSF_SECTION_SYM) != 0)
1361 val += relocation - gp;
1363 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1366 reloc_entry->address += input_section->output_offset;
1368 return bfd_reloc_ok;
1371 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1372 generated when addreses are 64 bits. The upper 32 bits are a simle
1375 static bfd_reloc_status_type
1376 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1377 output_bfd, error_message)
1379 arelent *reloc_entry;
1382 asection *input_section;
1384 char **error_message;
1386 bfd_reloc_status_type r;
1391 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1392 input_section, output_bfd, error_message);
1393 if (r != bfd_reloc_continue)
1396 /* Do a normal 32 bit relocation on the lower 32 bits. */
1397 reloc32 = *reloc_entry;
1398 if (bfd_big_endian (abfd))
1399 reloc32.address += 4;
1400 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1401 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1402 output_bfd, error_message);
1404 /* Sign extend into the upper 32 bits. */
1405 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1406 if ((val & 0x80000000) != 0)
1410 addr = reloc_entry->address;
1411 if (bfd_little_endian (abfd))
1413 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1418 /* Handle a mips16 jump. */
1420 static bfd_reloc_status_type
1421 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1422 output_bfd, error_message)
1424 arelent *reloc_entry;
1427 asection *input_section;
1429 char **error_message;
1431 if (output_bfd != (bfd *) NULL
1432 && (symbol->flags & BSF_SECTION_SYM) == 0
1433 && reloc_entry->addend == 0)
1435 reloc_entry->address += input_section->output_offset;
1436 return bfd_reloc_ok;
1441 static boolean warned;
1444 (*_bfd_error_handler)
1445 (_("Linking mips16 objects into %s format is not supported"),
1446 bfd_get_target (input_section->output_section->owner));
1450 return bfd_reloc_undefined;
1453 /* Handle a mips16 GP relative reloc. */
1455 static bfd_reloc_status_type
1456 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1457 output_bfd, error_message)
1459 arelent *reloc_entry;
1462 asection *input_section;
1464 char **error_message;
1466 boolean relocateable;
1467 bfd_reloc_status_type ret;
1469 unsigned short extend, insn;
1470 unsigned long final;
1472 /* If we're relocating, and this is an external symbol with no
1473 addend, we don't want to change anything. We will only have an
1474 addend if this is a newly created reloc, not read from an ELF
1476 if (output_bfd != NULL
1477 && (symbol->flags & BSF_SECTION_SYM) == 0
1478 && reloc_entry->addend == 0)
1480 reloc_entry->address += input_section->output_offset;
1481 return bfd_reloc_ok;
1484 if (output_bfd != NULL)
1485 relocateable = true;
1488 relocateable = false;
1489 output_bfd = symbol->section->output_section->owner;
1492 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1494 if (ret != bfd_reloc_ok)
1497 if (reloc_entry->address > input_section->_cooked_size)
1498 return bfd_reloc_outofrange;
1500 /* Pick up the mips16 extend instruction and the real instruction. */
1501 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1502 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1504 /* Stuff the current addend back as a 32 bit value, do the usual
1505 relocation, and then clean up. */
1507 (((extend & 0x1f) << 11)
1510 (bfd_byte *) data + reloc_entry->address);
1512 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1513 relocateable, data, gp);
1515 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1518 | ((final >> 11) & 0x1f)
1520 (bfd_byte *) data + reloc_entry->address);
1524 (bfd_byte *) data + reloc_entry->address + 2);
1529 /* Return the ISA for a MIPS e_flags value. */
1532 elf_mips_isa (flags)
1535 switch (flags & EF_MIPS_ARCH)
1549 /* Return the MACH for a MIPS e_flags value. */
1552 elf_mips_mach (flags)
1555 switch (flags & EF_MIPS_MACH)
1557 case E_MIPS_MACH_3900:
1558 return bfd_mach_mips3900;
1560 case E_MIPS_MACH_4010:
1561 return bfd_mach_mips4010;
1563 case E_MIPS_MACH_4100:
1564 return bfd_mach_mips4100;
1566 case E_MIPS_MACH_4111:
1567 return bfd_mach_mips4111;
1569 case E_MIPS_MACH_4650:
1570 return bfd_mach_mips4650;
1573 switch (flags & EF_MIPS_ARCH)
1577 return bfd_mach_mips3000;
1581 return bfd_mach_mips6000;
1585 return bfd_mach_mips4000;
1589 return bfd_mach_mips8000;
1597 /* Return printable name for ABI from flagword. */
1600 elf_mips_abi_name (flags)
1603 switch (flags & EF_MIPS_ABI)
1607 case E_MIPS_ABI_O32:
1609 case E_MIPS_ABI_O64:
1611 case E_MIPS_ABI_EABI32:
1613 case E_MIPS_ABI_EABI64:
1616 return "unknown abi";
1620 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1622 struct elf_reloc_map {
1623 bfd_reloc_code_real_type bfd_reloc_val;
1624 enum elf_mips_reloc_type elf_reloc_val;
1627 static CONST struct elf_reloc_map mips_reloc_map[] =
1629 { BFD_RELOC_NONE, R_MIPS_NONE, },
1630 { BFD_RELOC_16, R_MIPS_16 },
1631 { BFD_RELOC_32, R_MIPS_32 },
1632 { BFD_RELOC_64, R_MIPS_64 },
1633 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1634 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1635 { BFD_RELOC_LO16, R_MIPS_LO16 },
1636 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1637 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1638 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1639 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1640 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1641 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1642 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1643 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1644 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1645 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1646 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1647 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1648 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1649 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1652 /* Given a BFD reloc type, return a howto structure. */
1654 static reloc_howto_type *
1655 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1657 bfd_reloc_code_real_type code;
1661 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1663 if (mips_reloc_map[i].bfd_reloc_val == code)
1664 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1670 bfd_set_error (bfd_error_bad_value);
1673 case BFD_RELOC_CTOR:
1674 /* We need to handle BFD_RELOC_CTOR specially.
1675 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1676 size of addresses on this architecture. */
1677 if (bfd_arch_bits_per_address (abfd) == 32)
1678 return &elf_mips_howto_table[(int) R_MIPS_32];
1680 return &elf_mips_ctor64_howto;
1682 case BFD_RELOC_MIPS16_JMP:
1683 return &elf_mips16_jump_howto;
1684 case BFD_RELOC_MIPS16_GPREL:
1685 return &elf_mips16_gprel_howto;
1686 case BFD_RELOC_VTABLE_INHERIT:
1687 return &elf_mips_gnu_vtinherit_howto;
1688 case BFD_RELOC_VTABLE_ENTRY:
1689 return &elf_mips_gnu_vtentry_howto;
1693 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1696 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1699 Elf32_Internal_Rel *dst;
1701 unsigned int r_type;
1703 r_type = ELF32_R_TYPE (dst->r_info);
1707 cache_ptr->howto = &elf_mips16_jump_howto;
1709 case R_MIPS16_GPREL:
1710 cache_ptr->howto = &elf_mips16_gprel_howto;
1712 case R_MIPS_GNU_VTINHERIT:
1713 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1715 case R_MIPS_GNU_VTENTRY:
1716 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1720 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1721 cache_ptr->howto = &elf_mips_howto_table[r_type];
1725 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1726 value for the object file. We get the addend now, rather than
1727 when we do the relocation, because the symbol manipulations done
1728 by the linker may cause us to lose track of the input BFD. */
1729 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1730 && (r_type == (unsigned int) R_MIPS_GPREL16
1731 || r_type == (unsigned int) R_MIPS_LITERAL))
1732 cache_ptr->addend = elf_gp (abfd);
1735 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1738 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1741 Elf32_Internal_Rela *dst;
1743 /* Since an Elf32_Internal_Rel is an initial prefix of an
1744 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1746 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1748 /* If we ever need to do any extra processing with dst->r_addend
1749 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1752 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1753 routines swap this structure in and out. They are used outside of
1754 BFD, so they are globally visible. */
1757 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1759 const Elf32_External_RegInfo *ex;
1762 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1763 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1764 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1765 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1766 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1767 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1771 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1773 const Elf32_RegInfo *in;
1774 Elf32_External_RegInfo *ex;
1776 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1777 (bfd_byte *) ex->ri_gprmask);
1778 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1779 (bfd_byte *) ex->ri_cprmask[0]);
1780 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1781 (bfd_byte *) ex->ri_cprmask[1]);
1782 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1783 (bfd_byte *) ex->ri_cprmask[2]);
1784 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1785 (bfd_byte *) ex->ri_cprmask[3]);
1786 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1787 (bfd_byte *) ex->ri_gp_value);
1790 /* In the 64 bit ABI, the .MIPS.options section holds register
1791 information in an Elf64_Reginfo structure. These routines swap
1792 them in and out. They are globally visible because they are used
1793 outside of BFD. These routines are here so that gas can call them
1794 without worrying about whether the 64 bit ABI has been included. */
1797 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1799 const Elf64_External_RegInfo *ex;
1800 Elf64_Internal_RegInfo *in;
1802 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1803 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1804 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1805 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1806 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1807 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1808 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1812 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1814 const Elf64_Internal_RegInfo *in;
1815 Elf64_External_RegInfo *ex;
1817 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1818 (bfd_byte *) ex->ri_gprmask);
1819 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1820 (bfd_byte *) ex->ri_pad);
1821 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1822 (bfd_byte *) ex->ri_cprmask[0]);
1823 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1824 (bfd_byte *) ex->ri_cprmask[1]);
1825 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1826 (bfd_byte *) ex->ri_cprmask[2]);
1827 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1828 (bfd_byte *) ex->ri_cprmask[3]);
1829 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1830 (bfd_byte *) ex->ri_gp_value);
1833 /* Swap an entry in a .gptab section. Note that these routines rely
1834 on the equivalence of the two elements of the union. */
1837 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1839 const Elf32_External_gptab *ex;
1842 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1843 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1847 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1849 const Elf32_gptab *in;
1850 Elf32_External_gptab *ex;
1852 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1853 ex->gt_entry.gt_g_value);
1854 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1855 ex->gt_entry.gt_bytes);
1859 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1861 const Elf32_compact_rel *in;
1862 Elf32_External_compact_rel *ex;
1864 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1865 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1866 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1867 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1868 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1869 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1873 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1875 const Elf32_crinfo *in;
1876 Elf32_External_crinfo *ex;
1880 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1881 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1882 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1883 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1884 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1885 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1886 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1889 /* Swap in an options header. */
1892 bfd_mips_elf_swap_options_in (abfd, ex, in)
1894 const Elf_External_Options *ex;
1895 Elf_Internal_Options *in;
1897 in->kind = bfd_h_get_8 (abfd, ex->kind);
1898 in->size = bfd_h_get_8 (abfd, ex->size);
1899 in->section = bfd_h_get_16 (abfd, ex->section);
1900 in->info = bfd_h_get_32 (abfd, ex->info);
1903 /* Swap out an options header. */
1906 bfd_mips_elf_swap_options_out (abfd, in, ex)
1908 const Elf_Internal_Options *in;
1909 Elf_External_Options *ex;
1911 bfd_h_put_8 (abfd, in->kind, ex->kind);
1912 bfd_h_put_8 (abfd, in->size, ex->size);
1913 bfd_h_put_16 (abfd, in->section, ex->section);
1914 bfd_h_put_32 (abfd, in->info, ex->info);
1917 /* Determine whether a symbol is global for the purposes of splitting
1918 the symbol table into global symbols and local symbols. At least
1919 on Irix 5, this split must be between section symbols and all other
1920 symbols. On most ELF targets the split is between static symbols
1921 and externally visible symbols. */
1925 mips_elf_sym_is_global (abfd, sym)
1929 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1932 /* Set the right machine number for a MIPS ELF file. This is used for
1933 both the 32-bit and the 64-bit ABI. */
1936 _bfd_mips_elf_object_p (abfd)
1939 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
1940 elf_mips_mach (elf_elfheader (abfd)->e_flags));
1944 /* Set the right machine number for a 32-bit MIPS ELF file. */
1947 mips_elf32_object_p (abfd)
1950 /* Irix 5 is broken. Object file symbol tables are not always
1951 sorted correctly such that local symbols precede global symbols,
1952 and the sh_info field in the symbol table is not always right. */
1953 elf_bad_symtab (abfd) = true;
1955 return _bfd_mips_elf_object_p (abfd);
1958 /* The final processing done just before writing out a MIPS ELF object
1959 file. This gets the MIPS architecture right based on the machine
1960 number. This is used by both the 32-bit and the 64-bit ABI. */
1964 _bfd_mips_elf_final_write_processing (abfd, linker)
1970 Elf_Internal_Shdr **hdrpp;
1974 switch (bfd_get_mach (abfd))
1977 case bfd_mach_mips3000:
1978 val = E_MIPS_ARCH_1;
1981 case bfd_mach_mips3900:
1982 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
1985 case bfd_mach_mips6000:
1986 val = E_MIPS_ARCH_2;
1989 case bfd_mach_mips4000:
1990 case bfd_mach_mips4300:
1991 val = E_MIPS_ARCH_3;
1994 case bfd_mach_mips4010:
1995 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
1998 case bfd_mach_mips4100:
1999 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2002 case bfd_mach_mips4111:
2003 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2006 case bfd_mach_mips4650:
2007 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2010 case bfd_mach_mips8000:
2011 val = E_MIPS_ARCH_4;
2015 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2016 elf_elfheader (abfd)->e_flags |= val;
2018 /* Set the sh_info field for .gptab sections and other appropriate
2019 info for each special section. */
2020 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2021 i < elf_elfheader (abfd)->e_shnum;
2024 switch ((*hdrpp)->sh_type)
2026 case SHT_MIPS_LIBLIST:
2027 sec = bfd_get_section_by_name (abfd, ".dynstr");
2029 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2032 case SHT_MIPS_GPTAB:
2033 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2034 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2035 BFD_ASSERT (name != NULL
2036 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2037 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2038 BFD_ASSERT (sec != NULL);
2039 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2042 case SHT_MIPS_CONTENT:
2043 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2044 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2045 BFD_ASSERT (name != NULL
2046 && strncmp (name, ".MIPS.content",
2047 sizeof ".MIPS.content" - 1) == 0);
2048 sec = bfd_get_section_by_name (abfd,
2049 name + sizeof ".MIPS.content" - 1);
2050 BFD_ASSERT (sec != NULL);
2051 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2054 case SHT_MIPS_SYMBOL_LIB:
2055 sec = bfd_get_section_by_name (abfd, ".dynsym");
2057 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2058 sec = bfd_get_section_by_name (abfd, ".liblist");
2060 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2063 case SHT_MIPS_EVENTS:
2064 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2065 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2066 BFD_ASSERT (name != NULL);
2067 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2068 sec = bfd_get_section_by_name (abfd,
2069 name + sizeof ".MIPS.events" - 1);
2072 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2073 sizeof ".MIPS.post_rel" - 1) == 0);
2074 sec = bfd_get_section_by_name (abfd,
2076 + sizeof ".MIPS.post_rel" - 1));
2078 BFD_ASSERT (sec != NULL);
2079 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2086 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2089 _bfd_mips_elf_set_private_flags (abfd, flags)
2093 BFD_ASSERT (!elf_flags_init (abfd)
2094 || elf_elfheader (abfd)->e_flags == flags);
2096 elf_elfheader (abfd)->e_flags = flags;
2097 elf_flags_init (abfd) = true;
2101 /* Copy backend specific data from one object module to another */
2104 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2108 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2109 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2112 BFD_ASSERT (!elf_flags_init (obfd)
2113 || (elf_elfheader (obfd)->e_flags
2114 == elf_elfheader (ibfd)->e_flags));
2116 elf_gp (obfd) = elf_gp (ibfd);
2117 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2118 elf_flags_init (obfd) = true;
2122 /* Merge backend specific data from an object file to the output
2123 object file when linking. */
2126 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2134 /* Check if we have the same endianess */
2135 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2136 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2140 if (bfd_big_endian (ibfd))
2141 msg = _("%s: compiled for a big endian system and target is little endian");
2143 msg = _("%s: compiled for a little endian system and target is big endian");
2145 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2147 bfd_set_error (bfd_error_wrong_format);
2151 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2152 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2155 new_flags = elf_elfheader (ibfd)->e_flags;
2156 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2157 old_flags = elf_elfheader (obfd)->e_flags;
2159 if (! elf_flags_init (obfd))
2161 elf_flags_init (obfd) = true;
2162 elf_elfheader (obfd)->e_flags = new_flags;
2164 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2165 && bfd_get_arch_info (obfd)->the_default)
2167 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2168 bfd_get_mach (ibfd)))
2175 /* Check flag compatibility. */
2177 new_flags &= ~EF_MIPS_NOREORDER;
2178 old_flags &= ~EF_MIPS_NOREORDER;
2180 if (new_flags == old_flags)
2185 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2187 new_flags &= ~EF_MIPS_PIC;
2188 old_flags &= ~EF_MIPS_PIC;
2189 (*_bfd_error_handler)
2190 (_("%s: linking PIC files with non-PIC files"),
2191 bfd_get_filename (ibfd));
2195 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2197 new_flags &= ~EF_MIPS_CPIC;
2198 old_flags &= ~EF_MIPS_CPIC;
2199 (*_bfd_error_handler)
2200 (_("%s: linking abicalls files with non-abicalls files"),
2201 bfd_get_filename (ibfd));
2205 /* Compare the ISA's. */
2206 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2207 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2209 int new_mach = new_flags & EF_MIPS_MACH;
2210 int old_mach = old_flags & EF_MIPS_MACH;
2211 int new_isa = elf_mips_isa (new_flags);
2212 int old_isa = elf_mips_isa (old_flags);
2214 /* If either has no machine specified, just compare the general isa's.
2215 Some combinations of machines are ok, if the isa's match. */
2218 || new_mach == old_mach
2221 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2222 and -mips4 code. They will normally use the same data sizes and
2223 calling conventions. */
2225 if ((new_isa == 1 || new_isa == 2)
2226 ? (old_isa != 1 && old_isa != 2)
2227 : (old_isa == 1 || old_isa == 2))
2229 (*_bfd_error_handler)
2230 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2231 bfd_get_filename (ibfd), new_isa, old_isa);
2238 (*_bfd_error_handler)
2239 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2240 bfd_get_filename (ibfd),
2241 elf_mips_mach (new_flags),
2242 elf_mips_mach (old_flags));
2246 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2247 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2251 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI))
2253 /* Only error if both are set (to different values). */
2254 if ((new_flags & EF_MIPS_ABI)
2255 && (old_flags & EF_MIPS_ABI))
2257 (*_bfd_error_handler)
2258 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2259 bfd_get_filename (ibfd),
2260 elf_mips_abi_name (new_flags),
2261 elf_mips_abi_name (old_flags));
2264 new_flags &= ~EF_MIPS_ABI;
2265 old_flags &= ~EF_MIPS_ABI;
2268 /* Warn about any other mismatches */
2269 if (new_flags != old_flags)
2271 (*_bfd_error_handler)
2272 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2273 bfd_get_filename (ibfd), (unsigned long) new_flags,
2274 (unsigned long) old_flags);
2280 bfd_set_error (bfd_error_bad_value);
2288 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2292 FILE *file = (FILE *) ptr;
2294 BFD_ASSERT (abfd != NULL && ptr != NULL);
2296 /* Print normal ELF private data. */
2297 _bfd_elf_print_private_bfd_data (abfd, ptr);
2299 /* xgettext:c-format */
2300 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2302 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2303 fprintf (file, _ (" [abi=O32]"));
2304 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2305 fprintf (file, _ (" [abi=O64]"));
2306 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2307 fprintf (file, _ (" [abi=EABI32]"));
2308 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2309 fprintf (file, _ (" [abi=EABI64]"));
2310 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2311 fprintf (file, _ (" [abi unknown]"));
2313 fprintf (file, _ (" [no abi set]"));
2315 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2316 fprintf (file, _ (" [mips1]"));
2317 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2318 fprintf (file, _ (" [mips2]"));
2319 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2320 fprintf (file, _ (" [mips3]"));
2321 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2322 fprintf (file, _ (" [mips4]"));
2324 fprintf (file, _ (" [unknown ISA]"));
2326 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2327 fprintf (file, _ (" [32bitmode]"));
2329 fprintf (file, _ (" [not 32bitmode]"));
2336 /* Handle a MIPS specific section when reading an object file. This
2337 is called when elfcode.h finds a section with an unknown type.
2338 This routine supports both the 32-bit and 64-bit ELF ABI.
2340 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2344 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2346 Elf_Internal_Shdr *hdr;
2351 /* There ought to be a place to keep ELF backend specific flags, but
2352 at the moment there isn't one. We just keep track of the
2353 sections by their name, instead. Fortunately, the ABI gives
2354 suggested names for all the MIPS specific sections, so we will
2355 probably get away with this. */
2356 switch (hdr->sh_type)
2358 case SHT_MIPS_LIBLIST:
2359 if (strcmp (name, ".liblist") != 0)
2363 if (strcmp (name, ".msym") != 0)
2366 case SHT_MIPS_CONFLICT:
2367 if (strcmp (name, ".conflict") != 0)
2370 case SHT_MIPS_GPTAB:
2371 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2374 case SHT_MIPS_UCODE:
2375 if (strcmp (name, ".ucode") != 0)
2378 case SHT_MIPS_DEBUG:
2379 if (strcmp (name, ".mdebug") != 0)
2381 flags = SEC_DEBUGGING;
2383 case SHT_MIPS_REGINFO:
2384 if (strcmp (name, ".reginfo") != 0
2385 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2387 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2389 case SHT_MIPS_IFACE:
2390 if (strcmp (name, ".MIPS.interfaces") != 0)
2393 case SHT_MIPS_CONTENT:
2394 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2397 case SHT_MIPS_OPTIONS:
2398 if (strcmp (name, ".options") != 0
2399 && strcmp (name, ".MIPS.options") != 0)
2402 case SHT_MIPS_DWARF:
2403 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2406 case SHT_MIPS_SYMBOL_LIB:
2407 if (strcmp (name, ".MIPS.symlib") != 0)
2410 case SHT_MIPS_EVENTS:
2411 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2412 && strncmp (name, ".MIPS.post_rel",
2413 sizeof ".MIPS.post_rel" - 1) != 0)
2420 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2425 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2426 (bfd_get_section_flags (abfd,
2435 /* Handle a 32-bit MIPS ELF specific section. */
2438 mips_elf32_section_from_shdr (abfd, hdr, name)
2440 Elf_Internal_Shdr *hdr;
2443 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2446 /* FIXME: We should record sh_info for a .gptab section. */
2448 /* For a .reginfo section, set the gp value in the tdata information
2449 from the contents of this section. We need the gp value while
2450 processing relocs, so we just get it now. The .reginfo section
2451 is not used in the 64-bit MIPS ELF ABI. */
2452 if (hdr->sh_type == SHT_MIPS_REGINFO)
2454 Elf32_External_RegInfo ext;
2457 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2458 (file_ptr) 0, sizeof ext))
2460 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2461 elf_gp (abfd) = s.ri_gp_value;
2464 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2465 set the gp value based on what we find. We may see both
2466 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2467 they should agree. */
2468 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2470 bfd_byte *contents, *l, *lend;
2472 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2473 if (contents == NULL)
2475 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2476 (file_ptr) 0, hdr->sh_size))
2482 lend = contents + hdr->sh_size;
2483 while (l + sizeof (Elf_External_Options) <= lend)
2485 Elf_Internal_Options intopt;
2487 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2489 if (intopt.kind == ODK_REGINFO)
2491 Elf32_RegInfo intreg;
2493 bfd_mips_elf32_swap_reginfo_in
2495 ((Elf32_External_RegInfo *)
2496 (l + sizeof (Elf_External_Options))),
2498 elf_gp (abfd) = intreg.ri_gp_value;
2508 /* Set the correct type for a MIPS ELF section. We do this by the
2509 section name, which is a hack, but ought to work. This routine is
2510 used by both the 32-bit and the 64-bit ABI. */
2513 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2515 Elf32_Internal_Shdr *hdr;
2518 register const char *name;
2520 name = bfd_get_section_name (abfd, sec);
2522 if (strcmp (name, ".liblist") == 0)
2524 hdr->sh_type = SHT_MIPS_LIBLIST;
2525 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2526 /* The sh_link field is set in final_write_processing. */
2528 else if (strcmp (name, ".msym") == 0)
2530 hdr->sh_type = SHT_MIPS_MSYM;
2531 hdr->sh_entsize = 8;
2532 /* FIXME: Set the sh_info field. */
2534 else if (strcmp (name, ".conflict") == 0)
2535 hdr->sh_type = SHT_MIPS_CONFLICT;
2536 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2538 hdr->sh_type = SHT_MIPS_GPTAB;
2539 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2540 /* The sh_info field is set in final_write_processing. */
2542 else if (strcmp (name, ".ucode") == 0)
2543 hdr->sh_type = SHT_MIPS_UCODE;
2544 else if (strcmp (name, ".mdebug") == 0)
2546 hdr->sh_type = SHT_MIPS_DEBUG;
2547 /* In a shared object on Irix 5.3, the .mdebug section has an
2548 entsize of 0. FIXME: Does this matter? */
2549 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2550 hdr->sh_entsize = 0;
2552 hdr->sh_entsize = 1;
2554 else if (strcmp (name, ".reginfo") == 0)
2556 hdr->sh_type = SHT_MIPS_REGINFO;
2557 /* In a shared object on Irix 5.3, the .reginfo section has an
2558 entsize of 0x18. FIXME: Does this matter? */
2559 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2560 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2562 hdr->sh_entsize = 1;
2564 else if (SGI_COMPAT (abfd)
2565 && (strcmp (name, ".hash") == 0
2566 || strcmp (name, ".dynamic") == 0
2567 || strcmp (name, ".dynstr") == 0))
2569 hdr->sh_entsize = 0;
2571 /* This isn't how the Irix 6 linker behaves. */
2572 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2575 else if (strcmp (name, ".got") == 0
2576 || strcmp (name, ".sdata") == 0
2577 || strcmp (name, ".sbss") == 0
2578 || strcmp (name, ".lit4") == 0
2579 || strcmp (name, ".lit8") == 0)
2580 hdr->sh_flags |= SHF_MIPS_GPREL;
2581 else if (strcmp (name, ".MIPS.interfaces") == 0)
2583 hdr->sh_type = SHT_MIPS_IFACE;
2584 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2586 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2588 hdr->sh_type = SHT_MIPS_CONTENT;
2589 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2590 /* The sh_info field is set in final_write_processing. */
2592 else if (strcmp (name, ".options") == 0
2593 || strcmp (name, ".MIPS.options") == 0)
2595 hdr->sh_type = SHT_MIPS_OPTIONS;
2596 hdr->sh_entsize = 1;
2597 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2599 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2600 hdr->sh_type = SHT_MIPS_DWARF;
2601 else if (strcmp (name, ".MIPS.symlib") == 0)
2603 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2604 /* The sh_link and sh_info fields are set in
2605 final_write_processing. */
2607 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2608 || strncmp (name, ".MIPS.post_rel",
2609 sizeof ".MIPS.post_rel" - 1) == 0)
2611 hdr->sh_type = SHT_MIPS_EVENTS;
2612 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2613 /* The sh_link field is set in final_write_processing. */
2619 /* Given a BFD section, try to locate the corresponding ELF section
2620 index. This is used by both the 32-bit and the 64-bit ABI.
2621 Actually, it's not clear to me that the 64-bit ABI supports these,
2622 but for non-PIC objects we will certainly want support for at least
2623 the .scommon section. */
2626 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2628 Elf32_Internal_Shdr *hdr;
2632 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2634 *retval = SHN_MIPS_SCOMMON;
2637 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2639 *retval = SHN_MIPS_ACOMMON;
2645 /* When are writing out the .options or .MIPS.options section,
2646 remember the bytes we are writing out, so that we can install the
2647 GP value in the section_processing routine. */
2650 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2655 bfd_size_type count;
2657 if (strcmp (section->name, ".options") == 0
2658 || strcmp (section->name, ".MIPS.options") == 0)
2662 if (elf_section_data (section) == NULL)
2664 section->used_by_bfd =
2665 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2666 if (elf_section_data (section) == NULL)
2669 c = (bfd_byte *) elf_section_data (section)->tdata;
2674 if (section->_cooked_size != 0)
2675 size = section->_cooked_size;
2677 size = section->_raw_size;
2678 c = (bfd_byte *) bfd_zalloc (abfd, size);
2681 elf_section_data (section)->tdata = (PTR) c;
2684 memcpy (c + offset, location, count);
2687 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2691 /* Work over a section just before writing it out. This routine is
2692 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2693 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2697 _bfd_mips_elf_section_processing (abfd, hdr)
2699 Elf_Internal_Shdr *hdr;
2701 if (hdr->bfd_section != NULL)
2703 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2705 if (strcmp (name, ".sdata") == 0)
2707 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2708 hdr->sh_type = SHT_PROGBITS;
2710 else if (strcmp (name, ".sbss") == 0)
2712 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2713 hdr->sh_type = SHT_NOBITS;
2715 else if (strcmp (name, ".lit8") == 0
2716 || strcmp (name, ".lit4") == 0)
2718 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2719 hdr->sh_type = SHT_PROGBITS;
2721 else if (strcmp (name, ".compact_rel") == 0)
2724 hdr->sh_type = SHT_PROGBITS;
2726 else if (strcmp (name, ".rtproc") == 0)
2728 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2730 unsigned int adjust;
2732 adjust = hdr->sh_size % hdr->sh_addralign;
2734 hdr->sh_size += hdr->sh_addralign - adjust;
2742 /* Work over a section just before writing it out. We update the GP
2743 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2744 on the value we are using. */
2747 mips_elf32_section_processing (abfd, hdr)
2749 Elf32_Internal_Shdr *hdr;
2751 if (hdr->sh_type == SHT_MIPS_REGINFO)
2755 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2756 BFD_ASSERT (hdr->contents == NULL);
2759 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2762 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2763 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2767 if (hdr->sh_type == SHT_MIPS_OPTIONS
2768 && hdr->bfd_section != NULL
2769 && elf_section_data (hdr->bfd_section) != NULL
2770 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2772 bfd_byte *contents, *l, *lend;
2774 /* We stored the section contents in the elf_section_data tdata
2775 field in the set_section_contents routine. We save the
2776 section contents so that we don't have to read them again.
2777 At this point we know that elf_gp is set, so we can look
2778 through the section contents to see if there is an
2779 ODK_REGINFO structure. */
2781 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2783 lend = contents + hdr->sh_size;
2784 while (l + sizeof (Elf_External_Options) <= lend)
2786 Elf_Internal_Options intopt;
2788 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2790 if (intopt.kind == ODK_REGINFO)
2797 + sizeof (Elf_External_Options)
2798 + (sizeof (Elf32_External_RegInfo) - 4)),
2801 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2802 if (bfd_write (buf, 1, 4, abfd) != 4)
2809 return _bfd_mips_elf_section_processing (abfd, hdr);
2812 /* MIPS ELF uses two common sections. One is the usual one, and the
2813 other is for small objects. All the small objects are kept
2814 together, and then referenced via the gp pointer, which yields
2815 faster assembler code. This is what we use for the small common
2816 section. This approach is copied from ecoff.c. */
2817 static asection mips_elf_scom_section;
2818 static asymbol mips_elf_scom_symbol;
2819 static asymbol *mips_elf_scom_symbol_ptr;
2821 /* MIPS ELF also uses an acommon section, which represents an
2822 allocated common symbol which may be overridden by a
2823 definition in a shared library. */
2824 static asection mips_elf_acom_section;
2825 static asymbol mips_elf_acom_symbol;
2826 static asymbol *mips_elf_acom_symbol_ptr;
2828 /* The Irix 5 support uses two virtual sections, which represent
2829 text/data symbols defined in dynamic objects. */
2830 static asection mips_elf_text_section;
2831 static asection *mips_elf_text_section_ptr;
2832 static asymbol mips_elf_text_symbol;
2833 static asymbol *mips_elf_text_symbol_ptr;
2835 static asection mips_elf_data_section;
2836 static asection *mips_elf_data_section_ptr;
2837 static asymbol mips_elf_data_symbol;
2838 static asymbol *mips_elf_data_symbol_ptr;
2840 /* Handle the special MIPS section numbers that a symbol may use.
2841 This is used for both the 32-bit and the 64-bit ABI. */
2844 _bfd_mips_elf_symbol_processing (abfd, asym)
2848 elf_symbol_type *elfsym;
2850 elfsym = (elf_symbol_type *) asym;
2851 switch (elfsym->internal_elf_sym.st_shndx)
2853 case SHN_MIPS_ACOMMON:
2854 /* This section is used in a dynamically linked executable file.
2855 It is an allocated common section. The dynamic linker can
2856 either resolve these symbols to something in a shared
2857 library, or it can just leave them here. For our purposes,
2858 we can consider these symbols to be in a new section. */
2859 if (mips_elf_acom_section.name == NULL)
2861 /* Initialize the acommon section. */
2862 mips_elf_acom_section.name = ".acommon";
2863 mips_elf_acom_section.flags = SEC_ALLOC;
2864 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2865 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2866 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2867 mips_elf_acom_symbol.name = ".acommon";
2868 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2869 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2870 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2872 asym->section = &mips_elf_acom_section;
2876 /* Common symbols less than the GP size are automatically
2877 treated as SHN_MIPS_SCOMMON symbols. */
2878 if (asym->value > elf_gp_size (abfd))
2881 case SHN_MIPS_SCOMMON:
2882 if (mips_elf_scom_section.name == NULL)
2884 /* Initialize the small common section. */
2885 mips_elf_scom_section.name = ".scommon";
2886 mips_elf_scom_section.flags = SEC_IS_COMMON;
2887 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2888 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2889 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2890 mips_elf_scom_symbol.name = ".scommon";
2891 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2892 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2893 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2895 asym->section = &mips_elf_scom_section;
2896 asym->value = elfsym->internal_elf_sym.st_size;
2899 case SHN_MIPS_SUNDEFINED:
2900 asym->section = bfd_und_section_ptr;
2903 #if 0 /* for SGI_COMPAT */
2905 asym->section = mips_elf_text_section_ptr;
2909 asym->section = mips_elf_data_section_ptr;
2915 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2919 mips_elf_additional_program_headers (abfd)
2927 if (! SGI_COMPAT (abfd))
2930 s = bfd_get_section_by_name (abfd, ".reginfo");
2931 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2933 /* We need a PT_MIPS_REGINFO segment. */
2937 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2938 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2940 /* We need a PT_MIPS_RTPROC segment. */
2947 /* Modify the segment map for an Irix 5 executable. */
2950 mips_elf_modify_segment_map (abfd)
2954 struct elf_segment_map *m, **pm;
2956 if (! SGI_COMPAT (abfd))
2959 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2961 s = bfd_get_section_by_name (abfd, ".reginfo");
2962 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2964 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2965 if (m->p_type == PT_MIPS_REGINFO)
2969 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2973 m->p_type = PT_MIPS_REGINFO;
2977 /* We want to put it after the PHDR and INTERP segments. */
2978 pm = &elf_tdata (abfd)->segment_map;
2980 && ((*pm)->p_type == PT_PHDR
2981 || (*pm)->p_type == PT_INTERP))
2989 /* If there are .dynamic and .mdebug sections, we make a room for
2990 the RTPROC header. FIXME: Rewrite without section names. */
2991 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2992 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2993 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2995 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2996 if (m->p_type == PT_MIPS_RTPROC)
3000 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3004 m->p_type = PT_MIPS_RTPROC;
3006 s = bfd_get_section_by_name (abfd, ".rtproc");
3011 m->p_flags_valid = 1;
3019 /* We want to put it after the DYNAMIC segment. */
3020 pm = &elf_tdata (abfd)->segment_map;
3021 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3031 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
3032 .dynsym, and .hash sections, and everything in between. */
3033 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3034 if ((*pm)->p_type == PT_DYNAMIC)
3039 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3041 static const char *sec_names[] =
3042 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3045 struct elf_segment_map *n;
3049 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3051 s = bfd_get_section_by_name (abfd, sec_names[i]);
3052 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3058 sz = s->_cooked_size;
3061 if (high < s->vma + sz)
3067 for (s = abfd->sections; s != NULL; s = s->next)
3068 if ((s->flags & SEC_LOAD) != 0
3071 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3075 n = ((struct elf_segment_map *)
3076 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3083 for (s = abfd->sections; s != NULL; s = s->next)
3085 if ((s->flags & SEC_LOAD) != 0
3088 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3102 /* The structure of the runtime procedure descriptor created by the
3103 loader for use by the static exception system. */
3105 typedef struct runtime_pdr {
3106 bfd_vma adr; /* memory address of start of procedure */
3107 long regmask; /* save register mask */
3108 long regoffset; /* save register offset */
3109 long fregmask; /* save floating point register mask */
3110 long fregoffset; /* save floating point register offset */
3111 long frameoffset; /* frame size */
3112 short framereg; /* frame pointer register */
3113 short pcreg; /* offset or reg of return pc */
3114 long irpss; /* index into the runtime string table */
3116 struct exception_info *exception_info;/* pointer to exception array */
3118 #define cbRPDR sizeof(RPDR)
3119 #define rpdNil ((pRPDR) 0)
3121 /* Swap RPDR (runtime procedure table entry) for output. */
3123 static void ecoff_swap_rpdr_out
3124 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3127 ecoff_swap_rpdr_out (abfd, in, ex)
3130 struct rpdr_ext *ex;
3132 /* ecoff_put_off was defined in ecoffswap.h. */
3133 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3134 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3135 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3136 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3137 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3138 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3140 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3141 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3143 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3145 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3149 /* Read ECOFF debugging information from a .mdebug section into a
3150 ecoff_debug_info structure. */
3153 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3156 struct ecoff_debug_info *debug;
3159 const struct ecoff_debug_swap *swap;
3160 char *ext_hdr = NULL;
3162 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3163 memset (debug, 0, sizeof(*debug));
3165 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3166 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3169 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3170 swap->external_hdr_size)
3174 symhdr = &debug->symbolic_header;
3175 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3177 /* The symbolic header contains absolute file offsets and sizes to
3179 #define READ(ptr, offset, count, size, type) \
3180 if (symhdr->count == 0) \
3181 debug->ptr = NULL; \
3184 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3185 if (debug->ptr == NULL) \
3186 goto error_return; \
3187 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3188 || (bfd_read (debug->ptr, size, symhdr->count, \
3189 abfd) != size * symhdr->count)) \
3190 goto error_return; \
3193 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3194 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3195 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3196 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3197 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3198 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3200 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3201 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3202 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3203 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3204 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3208 debug->adjust = NULL;
3213 if (ext_hdr != NULL)
3215 if (debug->line != NULL)
3217 if (debug->external_dnr != NULL)
3218 free (debug->external_dnr);
3219 if (debug->external_pdr != NULL)
3220 free (debug->external_pdr);
3221 if (debug->external_sym != NULL)
3222 free (debug->external_sym);
3223 if (debug->external_opt != NULL)
3224 free (debug->external_opt);
3225 if (debug->external_aux != NULL)
3226 free (debug->external_aux);
3227 if (debug->ss != NULL)
3229 if (debug->ssext != NULL)
3230 free (debug->ssext);
3231 if (debug->external_fdr != NULL)
3232 free (debug->external_fdr);
3233 if (debug->external_rfd != NULL)
3234 free (debug->external_rfd);
3235 if (debug->external_ext != NULL)
3236 free (debug->external_ext);
3240 /* MIPS ELF local labels start with '$', not 'L'. */
3244 mips_elf_is_local_label_name (abfd, name)
3251 /* On Irix 6, the labels go back to starting with '.', so we accept
3252 the generic ELF local label syntax as well. */
3253 return _bfd_elf_is_local_label_name (abfd, name);
3256 /* MIPS ELF uses a special find_nearest_line routine in order the
3257 handle the ECOFF debugging information. */
3259 struct mips_elf_find_line
3261 struct ecoff_debug_info d;
3262 struct ecoff_find_line i;
3266 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3267 functionname_ptr, line_ptr)
3272 const char **filename_ptr;
3273 const char **functionname_ptr;
3274 unsigned int *line_ptr;
3278 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3279 filename_ptr, functionname_ptr,
3283 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3284 filename_ptr, functionname_ptr,
3288 msec = bfd_get_section_by_name (abfd, ".mdebug");
3292 struct mips_elf_find_line *fi;
3293 const struct ecoff_debug_swap * const swap =
3294 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3296 /* If we are called during a link, mips_elf_final_link may have
3297 cleared the SEC_HAS_CONTENTS field. We force it back on here
3298 if appropriate (which it normally will be). */
3299 origflags = msec->flags;
3300 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3301 msec->flags |= SEC_HAS_CONTENTS;
3303 fi = elf_tdata (abfd)->find_line_info;
3306 bfd_size_type external_fdr_size;
3309 struct fdr *fdr_ptr;
3311 fi = ((struct mips_elf_find_line *)
3312 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3315 msec->flags = origflags;
3319 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3321 msec->flags = origflags;
3325 /* Swap in the FDR information. */
3326 fi->d.fdr = ((struct fdr *)
3328 (fi->d.symbolic_header.ifdMax *
3329 sizeof (struct fdr))));
3330 if (fi->d.fdr == NULL)
3332 msec->flags = origflags;
3335 external_fdr_size = swap->external_fdr_size;
3336 fdr_ptr = fi->d.fdr;
3337 fraw_src = (char *) fi->d.external_fdr;
3338 fraw_end = (fraw_src
3339 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3340 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3341 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3343 elf_tdata (abfd)->find_line_info = fi;
3345 /* Note that we don't bother to ever free this information.
3346 find_nearest_line is either called all the time, as in
3347 objdump -l, so the information should be saved, or it is
3348 rarely called, as in ld error messages, so the memory
3349 wasted is unimportant. Still, it would probably be a
3350 good idea for free_cached_info to throw it away. */
3353 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3354 &fi->i, filename_ptr, functionname_ptr,
3357 msec->flags = origflags;
3361 msec->flags = origflags;
3364 /* Fall back on the generic ELF find_nearest_line routine. */
3366 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3367 filename_ptr, functionname_ptr,
3371 /* The mips16 compiler uses a couple of special sections to handle
3372 floating point arguments.
3374 Section names that look like .mips16.fn.FNNAME contain stubs that
3375 copy floating point arguments from the fp regs to the gp regs and
3376 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3377 call should be redirected to the stub instead. If no 32 bit
3378 function calls FNNAME, the stub should be discarded. We need to
3379 consider any reference to the function, not just a call, because
3380 if the address of the function is taken we will need the stub,
3381 since the address might be passed to a 32 bit function.
3383 Section names that look like .mips16.call.FNNAME contain stubs
3384 that copy floating point arguments from the gp regs to the fp
3385 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3386 then any 16 bit function that calls FNNAME should be redirected
3387 to the stub instead. If FNNAME is not a 32 bit function, the
3388 stub should be discarded.
3390 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3391 which call FNNAME and then copy the return value from the fp regs
3392 to the gp regs. These stubs store the return value in $18 while
3393 calling FNNAME; any function which might call one of these stubs
3394 must arrange to save $18 around the call. (This case is not
3395 needed for 32 bit functions that call 16 bit functions, because
3396 16 bit functions always return floating point values in both
3399 Note that in all cases FNNAME might be defined statically.
3400 Therefore, FNNAME is not used literally. Instead, the relocation
3401 information will indicate which symbol the section is for.
3403 We record any stubs that we find in the symbol table. */
3405 #define FN_STUB ".mips16.fn."
3406 #define CALL_STUB ".mips16.call."
3407 #define CALL_FP_STUB ".mips16.call.fp."
3409 /* The MIPS ELF linker needs additional information for each symbol in
3410 the global hash table. */
3412 struct mips_elf_link_hash_entry
3414 struct elf_link_hash_entry root;
3416 /* External symbol information. */
3419 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3420 unsigned int mips_32_relocs;
3422 /* If there is a stub that 32 bit functions should use to call this
3423 16 bit function, this points to the section containing the stub. */
3426 /* Whether we need the fn_stub; this is set if this symbol appears
3427 in any relocs other than a 16 bit call. */
3428 boolean need_fn_stub;
3430 /* If there is a stub that 16 bit functions should use to call this
3431 32 bit function, this points to the section containing the stub. */
3432 asection *call_stub;
3434 /* This is like the call_stub field, but it is used if the function
3435 being called returns a floating point value. */
3436 asection *call_fp_stub;
3439 /* MIPS ELF linker hash table. */
3441 struct mips_elf_link_hash_table
3443 struct elf_link_hash_table root;
3445 /* We no longer use this. */
3446 /* String section indices for the dynamic section symbols. */
3447 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3449 /* The number of .rtproc entries. */
3450 bfd_size_type procedure_count;
3451 /* The size of the .compact_rel section (if SGI_COMPAT). */
3452 bfd_size_type compact_rel_size;
3453 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3454 entry is set to the address of __rld_obj_head as in Irix 5. */
3455 boolean use_rld_obj_head;
3456 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3458 /* This is set if we see any mips16 stub sections. */
3459 boolean mips16_stubs_seen;
3462 /* Look up an entry in a MIPS ELF linker hash table. */
3464 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3465 ((struct mips_elf_link_hash_entry *) \
3466 elf_link_hash_lookup (&(table)->root, (string), (create), \
3469 /* Traverse a MIPS ELF linker hash table. */
3471 #define mips_elf_link_hash_traverse(table, func, info) \
3472 (elf_link_hash_traverse \
3474 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3477 /* Get the MIPS ELF linker hash table from a link_info structure. */
3479 #define mips_elf_hash_table(p) \
3480 ((struct mips_elf_link_hash_table *) ((p)->hash))
3482 static boolean mips_elf_output_extsym
3483 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3485 /* Create an entry in a MIPS ELF linker hash table. */
3487 static struct bfd_hash_entry *
3488 mips_elf_link_hash_newfunc (entry, table, string)
3489 struct bfd_hash_entry *entry;
3490 struct bfd_hash_table *table;
3493 struct mips_elf_link_hash_entry *ret =
3494 (struct mips_elf_link_hash_entry *) entry;
3496 /* Allocate the structure if it has not already been allocated by a
3498 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3499 ret = ((struct mips_elf_link_hash_entry *)
3500 bfd_hash_allocate (table,
3501 sizeof (struct mips_elf_link_hash_entry)));
3502 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3503 return (struct bfd_hash_entry *) ret;
3505 /* Call the allocation method of the superclass. */
3506 ret = ((struct mips_elf_link_hash_entry *)
3507 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3509 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3511 /* Set local fields. */
3512 memset (&ret->esym, 0, sizeof (EXTR));
3513 /* We use -2 as a marker to indicate that the information has
3514 not been set. -1 means there is no associated ifd. */
3516 ret->mips_32_relocs = 0;
3517 ret->fn_stub = NULL;
3518 ret->need_fn_stub = false;
3519 ret->call_stub = NULL;
3520 ret->call_fp_stub = NULL;
3523 return (struct bfd_hash_entry *) ret;
3526 /* Create a MIPS ELF linker hash table. */
3528 static struct bfd_link_hash_table *
3529 mips_elf_link_hash_table_create (abfd)
3532 struct mips_elf_link_hash_table *ret;
3534 ret = ((struct mips_elf_link_hash_table *)
3535 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3536 if (ret == (struct mips_elf_link_hash_table *) NULL)
3539 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3540 mips_elf_link_hash_newfunc))
3542 bfd_release (abfd, ret);
3547 /* We no longer use this. */
3548 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3549 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3551 ret->procedure_count = 0;
3552 ret->compact_rel_size = 0;
3553 ret->use_rld_obj_head = false;
3555 ret->mips16_stubs_seen = false;
3557 return &ret->root.root;
3560 /* Hook called by the linker routine which adds symbols from an object
3561 file. We must handle the special MIPS section numbers here. */
3565 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3567 struct bfd_link_info *info;
3568 const Elf_Internal_Sym *sym;
3574 if (SGI_COMPAT (abfd)
3575 && (abfd->flags & DYNAMIC) != 0
3576 && strcmp (*namep, "_rld_new_interface") == 0)
3578 /* Skip Irix 5 rld entry name. */
3583 switch (sym->st_shndx)
3586 /* Common symbols less than the GP size are automatically
3587 treated as SHN_MIPS_SCOMMON symbols. */
3588 if (sym->st_size > elf_gp_size (abfd))
3591 case SHN_MIPS_SCOMMON:
3592 *secp = bfd_make_section_old_way (abfd, ".scommon");
3593 (*secp)->flags |= SEC_IS_COMMON;
3594 *valp = sym->st_size;
3598 /* This section is used in a shared object. */
3599 if (mips_elf_text_section_ptr == NULL)
3601 /* Initialize the section. */
3602 mips_elf_text_section.name = ".text";
3603 mips_elf_text_section.flags = SEC_NO_FLAGS;
3604 mips_elf_text_section.output_section = NULL;
3605 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3606 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3607 mips_elf_text_symbol.name = ".text";
3608 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3609 mips_elf_text_symbol.section = &mips_elf_text_section;
3610 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3611 mips_elf_text_section_ptr = &mips_elf_text_section;
3613 /* This code used to do *secp = bfd_und_section_ptr if
3614 info->shared. I don't know why, and that doesn't make sense,
3615 so I took it out. */
3616 *secp = mips_elf_text_section_ptr;
3619 case SHN_MIPS_ACOMMON:
3620 /* Fall through. XXX Can we treat this as allocated data? */
3622 /* This section is used in a shared object. */
3623 if (mips_elf_data_section_ptr == NULL)
3625 /* Initialize the section. */
3626 mips_elf_data_section.name = ".data";
3627 mips_elf_data_section.flags = SEC_NO_FLAGS;
3628 mips_elf_data_section.output_section = NULL;
3629 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3630 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3631 mips_elf_data_symbol.name = ".data";
3632 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3633 mips_elf_data_symbol.section = &mips_elf_data_section;
3634 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3635 mips_elf_data_section_ptr = &mips_elf_data_section;
3637 /* This code used to do *secp = bfd_und_section_ptr if
3638 info->shared. I don't know why, and that doesn't make sense,
3639 so I took it out. */
3640 *secp = mips_elf_data_section_ptr;
3643 case SHN_MIPS_SUNDEFINED:
3644 *secp = bfd_und_section_ptr;
3648 if (SGI_COMPAT (abfd)
3650 && info->hash->creator == abfd->xvec
3651 && strcmp (*namep, "__rld_obj_head") == 0)
3653 struct elf_link_hash_entry *h;
3655 /* Mark __rld_obj_head as dynamic. */
3657 if (! (_bfd_generic_link_add_one_symbol
3658 (info, abfd, *namep, BSF_GLOBAL, *secp,
3659 (bfd_vma) *valp, (const char *) NULL, false,
3660 get_elf_backend_data (abfd)->collect,
3661 (struct bfd_link_hash_entry **) &h)))
3663 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3664 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3665 h->type = STT_OBJECT;
3667 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3670 mips_elf_hash_table (info)->use_rld_obj_head = true;
3673 /* If this is a mips16 text symbol, add 1 to the value to make it
3674 odd. This will cause something like .word SYM to come up with
3675 the right value when it is loaded into the PC. */
3676 if (sym->st_other == STO_MIPS16)
3682 /* Structure used to pass information to mips_elf_output_extsym. */
3687 struct bfd_link_info *info;
3688 struct ecoff_debug_info *debug;
3689 const struct ecoff_debug_swap *swap;
3693 /* This routine is used to write out ECOFF debugging external symbol
3694 information. It is called via mips_elf_link_hash_traverse. The
3695 ECOFF external symbol information must match the ELF external
3696 symbol information. Unfortunately, at this point we don't know
3697 whether a symbol is required by reloc information, so the two
3698 tables may wind up being different. We must sort out the external
3699 symbol information before we can set the final size of the .mdebug
3700 section, and we must set the size of the .mdebug section before we
3701 can relocate any sections, and we can't know which symbols are
3702 required by relocation until we relocate the sections.
3703 Fortunately, it is relatively unlikely that any symbol will be
3704 stripped but required by a reloc. In particular, it can not happen
3705 when generating a final executable. */
3708 mips_elf_output_extsym (h, data)
3709 struct mips_elf_link_hash_entry *h;
3712 struct extsym_info *einfo = (struct extsym_info *) data;
3714 asection *sec, *output_section;
3716 if (h->root.indx == -2)
3718 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3719 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3720 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3721 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3723 else if (einfo->info->strip == strip_all
3724 || (einfo->info->strip == strip_some
3725 && bfd_hash_lookup (einfo->info->keep_hash,
3726 h->root.root.root.string,
3727 false, false) == NULL))
3735 if (h->esym.ifd == -2)
3738 h->esym.cobol_main = 0;
3739 h->esym.weakext = 0;
3740 h->esym.reserved = 0;
3741 h->esym.ifd = ifdNil;
3742 h->esym.asym.value = 0;
3743 h->esym.asym.st = stGlobal;
3745 if (SGI_COMPAT (einfo->abfd)
3746 && (h->root.root.type == bfd_link_hash_undefined
3747 || h->root.root.type == bfd_link_hash_undefweak))
3751 /* Use undefined class. Also, set class and type for some
3753 name = h->root.root.root.string;
3754 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3755 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3757 h->esym.asym.sc = scData;
3758 h->esym.asym.st = stLabel;
3759 h->esym.asym.value = 0;
3761 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3763 h->esym.asym.sc = scAbs;
3764 h->esym.asym.st = stLabel;
3765 h->esym.asym.value =
3766 mips_elf_hash_table (einfo->info)->procedure_count;
3768 else if (strcmp (name, "_gp_disp") == 0)
3770 h->esym.asym.sc = scAbs;
3771 h->esym.asym.st = stLabel;
3772 h->esym.asym.value = elf_gp (einfo->abfd);
3775 h->esym.asym.sc = scUndefined;
3777 else if (h->root.root.type != bfd_link_hash_defined
3778 && h->root.root.type != bfd_link_hash_defweak)
3779 h->esym.asym.sc = scAbs;
3784 sec = h->root.root.u.def.section;
3785 output_section = sec->output_section;
3787 /* When making a shared library and symbol h is the one from
3788 the another shared library, OUTPUT_SECTION may be null. */
3789 if (output_section == NULL)
3790 h->esym.asym.sc = scUndefined;
3793 name = bfd_section_name (output_section->owner, output_section);
3795 if (strcmp (name, ".text") == 0)
3796 h->esym.asym.sc = scText;
3797 else if (strcmp (name, ".data") == 0)
3798 h->esym.asym.sc = scData;
3799 else if (strcmp (name, ".sdata") == 0)
3800 h->esym.asym.sc = scSData;
3801 else if (strcmp (name, ".rodata") == 0
3802 || strcmp (name, ".rdata") == 0)
3803 h->esym.asym.sc = scRData;
3804 else if (strcmp (name, ".bss") == 0)
3805 h->esym.asym.sc = scBss;
3806 else if (strcmp (name, ".sbss") == 0)
3807 h->esym.asym.sc = scSBss;
3808 else if (strcmp (name, ".init") == 0)
3809 h->esym.asym.sc = scInit;
3810 else if (strcmp (name, ".fini") == 0)
3811 h->esym.asym.sc = scFini;
3813 h->esym.asym.sc = scAbs;
3817 h->esym.asym.reserved = 0;
3818 h->esym.asym.index = indexNil;
3821 if (h->root.root.type == bfd_link_hash_common)
3822 h->esym.asym.value = h->root.root.u.c.size;
3823 else if (h->root.root.type == bfd_link_hash_defined
3824 || h->root.root.type == bfd_link_hash_defweak)
3826 if (h->esym.asym.sc == scCommon)
3827 h->esym.asym.sc = scBss;
3828 else if (h->esym.asym.sc == scSCommon)
3829 h->esym.asym.sc = scSBss;
3831 sec = h->root.root.u.def.section;
3832 output_section = sec->output_section;
3833 if (output_section != NULL)
3834 h->esym.asym.value = (h->root.root.u.def.value
3835 + sec->output_offset
3836 + output_section->vma);
3838 h->esym.asym.value = 0;
3840 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3842 /* Set type and value for a symbol with a function stub. */
3843 h->esym.asym.st = stProc;
3844 sec = h->root.root.u.def.section;
3846 h->esym.asym.value = 0;
3849 output_section = sec->output_section;
3850 if (output_section != NULL)
3851 h->esym.asym.value = (h->root.plt.offset
3852 + sec->output_offset
3853 + output_section->vma);
3855 h->esym.asym.value = 0;
3862 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3863 h->root.root.root.string,
3866 einfo->failed = true;
3873 /* Create a runtime procedure table from the .mdebug section. */
3876 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3879 struct bfd_link_info *info;
3881 struct ecoff_debug_info *debug;
3883 const struct ecoff_debug_swap *swap;
3884 HDRR *hdr = &debug->symbolic_header;
3886 struct rpdr_ext *erp;
3888 struct pdr_ext *epdr;
3889 struct sym_ext *esym;
3892 unsigned long size, count;
3893 unsigned long sindex;
3897 const char *no_name_func = _("static procedure (no name)");
3905 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3907 sindex = strlen (no_name_func) + 1;
3908 count = hdr->ipdMax;
3911 size = swap->external_pdr_size;
3913 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3917 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3920 size = sizeof (RPDR);
3921 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3925 sv = (char **) bfd_malloc (sizeof (char *) * count);
3929 count = hdr->isymMax;
3930 size = swap->external_sym_size;
3931 esym = (struct sym_ext *) bfd_malloc (size * count);
3935 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3938 count = hdr->issMax;
3939 ss = (char *) bfd_malloc (count);
3942 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3945 count = hdr->ipdMax;
3946 for (i = 0; i < count; i++, rp++)
3948 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3949 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3950 rp->adr = sym.value;
3951 rp->regmask = pdr.regmask;
3952 rp->regoffset = pdr.regoffset;
3953 rp->fregmask = pdr.fregmask;
3954 rp->fregoffset = pdr.fregoffset;
3955 rp->frameoffset = pdr.frameoffset;
3956 rp->framereg = pdr.framereg;
3957 rp->pcreg = pdr.pcreg;
3959 sv[i] = ss + sym.iss;
3960 sindex += strlen (sv[i]) + 1;
3964 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3965 size = BFD_ALIGN (size, 16);
3966 rtproc = (PTR) bfd_alloc (abfd, size);
3969 mips_elf_hash_table (info)->procedure_count = 0;
3973 mips_elf_hash_table (info)->procedure_count = count + 2;
3975 erp = (struct rpdr_ext *) rtproc;
3976 memset (erp, 0, sizeof (struct rpdr_ext));
3978 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3979 strcpy (str, no_name_func);
3980 str += strlen (no_name_func) + 1;
3981 for (i = 0; i < count; i++)
3983 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3984 strcpy (str, sv[i]);
3985 str += strlen (sv[i]) + 1;
3987 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3989 /* Set the size and contents of .rtproc section. */
3990 s->_raw_size = size;
3991 s->contents = (bfd_byte *) rtproc;
3993 /* Skip this section later on (I don't think this currently
3994 matters, but someday it might). */
3995 s->link_order_head = (struct bfd_link_order *) NULL;
4024 /* A comparison routine used to sort .gptab entries. */
4027 gptab_compare (p1, p2)
4031 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4032 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4034 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4037 /* We need to use a special link routine to handle the .reginfo and
4038 the .mdebug sections. We need to merge all instances of these
4039 sections together, not write them all out sequentially. */
4042 mips_elf_final_link (abfd, info)
4044 struct bfd_link_info *info;
4048 struct bfd_link_order *p;
4049 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4050 asection *rtproc_sec;
4051 Elf32_RegInfo reginfo;
4052 struct ecoff_debug_info debug;
4053 const struct ecoff_debug_swap *swap
4054 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4055 HDRR *symhdr = &debug.symbolic_header;
4056 PTR mdebug_handle = NULL;
4058 /* Drop the .options section, since it has special semantics which I
4059 haven't bothered to figure out. */
4060 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4062 if (strcmp ((*secpp)->name, ".options") == 0)
4064 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4065 if (p->type == bfd_indirect_link_order)
4066 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4067 (*secpp)->link_order_head = NULL;
4068 *secpp = (*secpp)->next;
4069 --abfd->section_count;
4074 /* Get a value for the GP register. */
4075 if (elf_gp (abfd) == 0)
4077 struct bfd_link_hash_entry *h;
4079 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4080 if (h != (struct bfd_link_hash_entry *) NULL
4081 && h->type == bfd_link_hash_defined)
4082 elf_gp (abfd) = (h->u.def.value
4083 + h->u.def.section->output_section->vma
4084 + h->u.def.section->output_offset);
4085 else if (info->relocateable)
4089 /* Make up a value. */
4091 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4094 && (strcmp (o->name, ".sbss") == 0
4095 || strcmp (o->name, ".sdata") == 0
4096 || strcmp (o->name, ".lit4") == 0
4097 || strcmp (o->name, ".lit8") == 0))
4100 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4104 /* If the relocate_section function needs to do a reloc
4105 involving the GP value, it should make a reloc_dangerous
4106 callback to warn that GP is not defined. */
4110 /* Go through the sections and collect the .reginfo and .mdebug
4114 gptab_data_sec = NULL;
4115 gptab_bss_sec = NULL;
4116 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4118 if (strcmp (o->name, ".reginfo") == 0)
4120 memset (®info, 0, sizeof reginfo);
4122 /* We have found the .reginfo section in the output file.
4123 Look through all the link_orders comprising it and merge
4124 the information together. */
4125 for (p = o->link_order_head;
4126 p != (struct bfd_link_order *) NULL;
4129 asection *input_section;
4131 Elf32_External_RegInfo ext;
4134 if (p->type != bfd_indirect_link_order)
4136 if (p->type == bfd_fill_link_order)
4141 input_section = p->u.indirect.section;
4142 input_bfd = input_section->owner;
4144 /* The linker emulation code has probably clobbered the
4145 size to be zero bytes. */
4146 if (input_section->_raw_size == 0)
4147 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4149 if (! bfd_get_section_contents (input_bfd, input_section,
4155 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4157 reginfo.ri_gprmask |= sub.ri_gprmask;
4158 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4159 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4160 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4161 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4163 /* ri_gp_value is set by the function
4164 mips_elf32_section_processing when the section is
4165 finally written out. */
4167 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4168 elf_link_input_bfd ignores this section. */
4169 input_section->flags &=~ SEC_HAS_CONTENTS;
4172 /* Size has been set in mips_elf_always_size_sections */
4173 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4175 /* Skip this section later on (I don't think this currently
4176 matters, but someday it might). */
4177 o->link_order_head = (struct bfd_link_order *) NULL;
4182 if (strcmp (o->name, ".mdebug") == 0)
4184 struct extsym_info einfo;
4186 /* We have found the .mdebug section in the output file.
4187 Look through all the link_orders comprising it and merge
4188 the information together. */
4189 symhdr->magic = swap->sym_magic;
4190 /* FIXME: What should the version stamp be? */
4192 symhdr->ilineMax = 0;
4196 symhdr->isymMax = 0;
4197 symhdr->ioptMax = 0;
4198 symhdr->iauxMax = 0;
4200 symhdr->issExtMax = 0;
4203 symhdr->iextMax = 0;
4205 /* We accumulate the debugging information itself in the
4206 debug_info structure. */
4208 debug.external_dnr = NULL;
4209 debug.external_pdr = NULL;
4210 debug.external_sym = NULL;
4211 debug.external_opt = NULL;
4212 debug.external_aux = NULL;
4214 debug.ssext = debug.ssext_end = NULL;
4215 debug.external_fdr = NULL;
4216 debug.external_rfd = NULL;
4217 debug.external_ext = debug.external_ext_end = NULL;
4219 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4220 if (mdebug_handle == (PTR) NULL)
4223 if (SGI_COMPAT (abfd))
4229 static const char * const name[] =
4230 { ".text", ".init", ".fini", ".data",
4231 ".rodata", ".sdata", ".sbss", ".bss" };
4232 static const int sc[] = { scText, scInit, scFini, scData,
4233 scRData, scSData, scSBss, scBss };
4236 esym.cobol_main = 0;
4240 esym.asym.iss = issNil;
4241 esym.asym.st = stLocal;
4242 esym.asym.reserved = 0;
4243 esym.asym.index = indexNil;
4245 for (i = 0; i < 8; i++)
4247 esym.asym.sc = sc[i];
4248 s = bfd_get_section_by_name (abfd, name[i]);
4251 esym.asym.value = s->vma;
4252 last = s->vma + s->_raw_size;
4255 esym.asym.value = last;
4257 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4263 for (p = o->link_order_head;
4264 p != (struct bfd_link_order *) NULL;
4267 asection *input_section;
4269 const struct ecoff_debug_swap *input_swap;
4270 struct ecoff_debug_info input_debug;
4274 if (p->type != bfd_indirect_link_order)
4276 if (p->type == bfd_fill_link_order)
4281 input_section = p->u.indirect.section;
4282 input_bfd = input_section->owner;
4284 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4285 || (get_elf_backend_data (input_bfd)
4286 ->elf_backend_ecoff_debug_swap) == NULL)
4288 /* I don't know what a non MIPS ELF bfd would be
4289 doing with a .mdebug section, but I don't really
4290 want to deal with it. */
4294 input_swap = (get_elf_backend_data (input_bfd)
4295 ->elf_backend_ecoff_debug_swap);
4297 BFD_ASSERT (p->size == input_section->_raw_size);
4299 /* The ECOFF linking code expects that we have already
4300 read in the debugging information and set up an
4301 ecoff_debug_info structure, so we do that now. */
4302 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4306 if (! (bfd_ecoff_debug_accumulate
4307 (mdebug_handle, abfd, &debug, swap, input_bfd,
4308 &input_debug, input_swap, info)))
4311 /* Loop through the external symbols. For each one with
4312 interesting information, try to find the symbol in
4313 the linker global hash table and save the information
4314 for the output external symbols. */
4315 eraw_src = input_debug.external_ext;
4316 eraw_end = (eraw_src
4317 + (input_debug.symbolic_header.iextMax
4318 * input_swap->external_ext_size));
4320 eraw_src < eraw_end;
4321 eraw_src += input_swap->external_ext_size)
4325 struct mips_elf_link_hash_entry *h;
4327 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4328 if (ext.asym.sc == scNil
4329 || ext.asym.sc == scUndefined
4330 || ext.asym.sc == scSUndefined)
4333 name = input_debug.ssext + ext.asym.iss;
4334 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4335 name, false, false, true);
4336 if (h == NULL || h->esym.ifd != -2)
4342 < input_debug.symbolic_header.ifdMax);
4343 ext.ifd = input_debug.ifdmap[ext.ifd];
4349 /* Free up the information we just read. */
4350 free (input_debug.line);
4351 free (input_debug.external_dnr);
4352 free (input_debug.external_pdr);
4353 free (input_debug.external_sym);
4354 free (input_debug.external_opt);
4355 free (input_debug.external_aux);
4356 free (input_debug.ss);
4357 free (input_debug.ssext);
4358 free (input_debug.external_fdr);
4359 free (input_debug.external_rfd);
4360 free (input_debug.external_ext);
4362 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4363 elf_link_input_bfd ignores this section. */
4364 input_section->flags &=~ SEC_HAS_CONTENTS;
4367 if (SGI_COMPAT (abfd) && info->shared)
4369 /* Create .rtproc section. */
4370 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4371 if (rtproc_sec == NULL)
4373 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4374 | SEC_LINKER_CREATED | SEC_READONLY);
4376 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4377 if (rtproc_sec == NULL
4378 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4379 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4383 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4384 info, rtproc_sec, &debug))
4388 /* Build the external symbol information. */
4391 einfo.debug = &debug;
4393 einfo.failed = false;
4394 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4395 mips_elf_output_extsym,
4400 /* Set the size of the .mdebug section. */
4401 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4403 /* Skip this section later on (I don't think this currently
4404 matters, but someday it might). */
4405 o->link_order_head = (struct bfd_link_order *) NULL;
4410 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4412 const char *subname;
4415 Elf32_External_gptab *ext_tab;
4418 /* The .gptab.sdata and .gptab.sbss sections hold
4419 information describing how the small data area would
4420 change depending upon the -G switch. These sections
4421 not used in executables files. */
4422 if (! info->relocateable)
4426 for (p = o->link_order_head;
4427 p != (struct bfd_link_order *) NULL;
4430 asection *input_section;
4432 if (p->type != bfd_indirect_link_order)
4434 if (p->type == bfd_fill_link_order)
4439 input_section = p->u.indirect.section;
4441 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4442 elf_link_input_bfd ignores this section. */
4443 input_section->flags &=~ SEC_HAS_CONTENTS;
4446 /* Skip this section later on (I don't think this
4447 currently matters, but someday it might). */
4448 o->link_order_head = (struct bfd_link_order *) NULL;
4450 /* Really remove the section. */
4451 for (secpp = &abfd->sections;
4453 secpp = &(*secpp)->next)
4455 *secpp = (*secpp)->next;
4456 --abfd->section_count;
4461 /* There is one gptab for initialized data, and one for
4462 uninitialized data. */
4463 if (strcmp (o->name, ".gptab.sdata") == 0)
4465 else if (strcmp (o->name, ".gptab.sbss") == 0)
4469 (*_bfd_error_handler)
4470 (_("%s: illegal section name `%s'"),
4471 bfd_get_filename (abfd), o->name);
4472 bfd_set_error (bfd_error_nonrepresentable_section);
4476 /* The linker script always combines .gptab.data and
4477 .gptab.sdata into .gptab.sdata, and likewise for
4478 .gptab.bss and .gptab.sbss. It is possible that there is
4479 no .sdata or .sbss section in the output file, in which
4480 case we must change the name of the output section. */
4481 subname = o->name + sizeof ".gptab" - 1;
4482 if (bfd_get_section_by_name (abfd, subname) == NULL)
4484 if (o == gptab_data_sec)
4485 o->name = ".gptab.data";
4487 o->name = ".gptab.bss";
4488 subname = o->name + sizeof ".gptab" - 1;
4489 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4492 /* Set up the first entry. */
4494 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4497 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4498 tab[0].gt_header.gt_unused = 0;
4500 /* Combine the input sections. */
4501 for (p = o->link_order_head;
4502 p != (struct bfd_link_order *) NULL;
4505 asection *input_section;
4509 bfd_size_type gpentry;
4511 if (p->type != bfd_indirect_link_order)
4513 if (p->type == bfd_fill_link_order)
4518 input_section = p->u.indirect.section;
4519 input_bfd = input_section->owner;
4521 /* Combine the gptab entries for this input section one
4522 by one. We know that the input gptab entries are
4523 sorted by ascending -G value. */
4524 size = bfd_section_size (input_bfd, input_section);
4526 for (gpentry = sizeof (Elf32_External_gptab);
4528 gpentry += sizeof (Elf32_External_gptab))
4530 Elf32_External_gptab ext_gptab;
4531 Elf32_gptab int_gptab;
4537 if (! (bfd_get_section_contents
4538 (input_bfd, input_section, (PTR) &ext_gptab,
4539 gpentry, sizeof (Elf32_External_gptab))))
4545 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4547 val = int_gptab.gt_entry.gt_g_value;
4548 add = int_gptab.gt_entry.gt_bytes - last;
4551 for (look = 1; look < c; look++)
4553 if (tab[look].gt_entry.gt_g_value >= val)
4554 tab[look].gt_entry.gt_bytes += add;
4556 if (tab[look].gt_entry.gt_g_value == val)
4562 Elf32_gptab *new_tab;
4565 /* We need a new table entry. */
4566 new_tab = ((Elf32_gptab *)
4567 bfd_realloc ((PTR) tab,
4568 (c + 1) * sizeof (Elf32_gptab)));
4569 if (new_tab == NULL)
4575 tab[c].gt_entry.gt_g_value = val;
4576 tab[c].gt_entry.gt_bytes = add;
4578 /* Merge in the size for the next smallest -G
4579 value, since that will be implied by this new
4582 for (look = 1; look < c; look++)
4584 if (tab[look].gt_entry.gt_g_value < val
4586 || (tab[look].gt_entry.gt_g_value
4587 > tab[max].gt_entry.gt_g_value)))
4591 tab[c].gt_entry.gt_bytes +=
4592 tab[max].gt_entry.gt_bytes;
4597 last = int_gptab.gt_entry.gt_bytes;
4600 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4601 elf_link_input_bfd ignores this section. */
4602 input_section->flags &=~ SEC_HAS_CONTENTS;
4605 /* The table must be sorted by -G value. */
4607 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4609 /* Swap out the table. */
4610 ext_tab = ((Elf32_External_gptab *)
4611 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4612 if (ext_tab == NULL)
4618 for (i = 0; i < c; i++)
4619 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4622 o->_raw_size = c * sizeof (Elf32_External_gptab);
4623 o->contents = (bfd_byte *) ext_tab;
4625 /* Skip this section later on (I don't think this currently
4626 matters, but someday it might). */
4627 o->link_order_head = (struct bfd_link_order *) NULL;
4631 /* Invoke the regular ELF backend linker to do all the work. */
4632 if (! bfd_elf32_bfd_final_link (abfd, info))
4635 /* Now write out the computed sections. */
4637 if (reginfo_sec != (asection *) NULL)
4639 Elf32_External_RegInfo ext;
4641 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
4642 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4643 (file_ptr) 0, sizeof ext))
4647 if (mdebug_sec != (asection *) NULL)
4649 BFD_ASSERT (abfd->output_has_begun);
4650 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4652 mdebug_sec->filepos))
4655 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4658 if (gptab_data_sec != (asection *) NULL)
4660 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4661 gptab_data_sec->contents,
4663 gptab_data_sec->_raw_size))
4667 if (gptab_bss_sec != (asection *) NULL)
4669 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4670 gptab_bss_sec->contents,
4672 gptab_bss_sec->_raw_size))
4676 if (SGI_COMPAT (abfd))
4678 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4679 if (rtproc_sec != NULL)
4681 if (! bfd_set_section_contents (abfd, rtproc_sec,
4682 rtproc_sec->contents,
4684 rtproc_sec->_raw_size))
4692 /* Handle a MIPS ELF HI16 reloc. */
4695 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4697 Elf_Internal_Rela *relhi;
4698 Elf_Internal_Rela *rello;
4705 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4707 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4710 addend += ((insn & 0xffff) << 16) + addlo;
4712 if ((addlo & 0x8000) != 0)
4714 if ((addend & 0x8000) != 0)
4717 bfd_put_32 (input_bfd,
4718 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4719 contents + relhi->r_offset);
4722 /* Handle a MIPS ELF local GOT16 reloc. */
4725 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4730 Elf_Internal_Rela *relhi;
4731 Elf_Internal_Rela *rello;
4735 unsigned int assigned_gotno;
4741 bfd_byte *got_contents;
4742 struct mips_got_info *g;
4744 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4746 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4749 addend += ((insn & 0xffff) << 16) + addlo;
4751 if ((addlo & 0x8000) != 0)
4753 if ((addend & 0x8000) != 0)
4756 /* Get a got entry representing requested hipage. */
4757 BFD_ASSERT (elf_section_data (sgot) != NULL);
4758 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4759 BFD_ASSERT (g != NULL);
4761 assigned_gotno = g->assigned_gotno;
4762 got_contents = sgot->contents;
4763 hipage = addend & 0xffff0000;
4765 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4767 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4768 if (hipage == (address & 0xffff0000))
4772 if (i == assigned_gotno)
4774 if (assigned_gotno >= g->local_gotno)
4776 (*_bfd_error_handler)
4777 (_("more got entries are needed for hipage relocations"));
4778 bfd_set_error (bfd_error_bad_value);
4782 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4783 ++g->assigned_gotno;
4786 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4787 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4788 contents + relhi->r_offset);
4793 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4796 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4798 Elf_Internal_Rela *rel;
4804 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4805 bfd_put_32 (input_bfd,
4806 (insn & 0xffff0000) | (offset & 0xffff),
4807 contents + rel->r_offset);
4810 /* Relocate a MIPS ELF section. */
4813 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4814 contents, relocs, local_syms, local_sections)
4816 struct bfd_link_info *info;
4818 asection *input_section;
4820 Elf_Internal_Rela *relocs;
4821 Elf_Internal_Sym *local_syms;
4822 asection **local_sections;
4824 Elf_Internal_Shdr *symtab_hdr;
4827 asection *sgot, *sreloc, *scpt;
4830 Elf_Internal_Rela *rel;
4831 Elf_Internal_Rela *relend;
4832 struct mips_got_info *g;
4834 dynobj = elf_hash_table (info)->dynobj;
4835 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4839 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4842 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4845 if (elf_bad_symtab (input_bfd))
4847 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4852 locsymcount = symtab_hdr->sh_info;
4853 extsymoff = symtab_hdr->sh_info;
4856 gp = _bfd_get_gp_value (output_bfd);
4859 relend = relocs + input_section->reloc_count;
4860 for (; rel < relend; rel++)
4863 reloc_howto_type *howto;
4864 unsigned long r_symndx;
4866 struct elf_link_hash_entry *h;
4868 Elf_Internal_Sym *sym;
4869 struct mips_elf_link_hash_entry *mh;
4871 bfd_reloc_status_type r;
4873 r_type = ELF32_R_TYPE (rel->r_info);
4874 if (r_type == R_MIPS_GNU_VTINHERIT
4875 || r_type == R_MIPS_GNU_VTENTRY)
4877 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4878 && r_type != R_MIPS16_26
4879 && r_type != R_MIPS16_GPREL)
4881 bfd_set_error (bfd_error_bad_value);
4884 if (r_type == R_MIPS16_26)
4885 howto = &elf_mips16_jump_howto;
4886 else if (r_type == R_MIPS16_GPREL)
4887 howto = &elf_mips16_gprel_howto;
4889 howto = elf_mips_howto_table + r_type;
4892 && (r_type == R_MIPS_CALL16
4893 || r_type == R_MIPS_GOT16
4894 || r_type == R_MIPS_CALL_HI16
4895 || r_type == R_MIPS_CALL_LO16
4896 || r_type == R_MIPS_GOT_HI16
4897 || r_type == R_MIPS_GOT_LO16))
4899 /* We need the .got section. */
4902 sgot = bfd_get_section_by_name (dynobj, ".got");
4903 BFD_ASSERT (sgot != NULL);
4904 BFD_ASSERT (elf_section_data (sgot) != NULL);
4905 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4906 BFD_ASSERT (g != NULL);
4910 r_symndx = ELF32_R_SYM (rel->r_info);
4912 /* Mix in the change in GP address for a GP relative reloc. */
4913 if (r_type != R_MIPS_GPREL16
4914 && r_type != R_MIPS_LITERAL
4915 && r_type != R_MIPS_GPREL32
4916 && r_type != R_MIPS16_GPREL)
4922 if (! ((*info->callbacks->reloc_dangerous)
4924 _("GP relative relocation when GP not defined"),
4925 input_bfd, input_section,
4928 /* Only give the error once per link. */
4930 _bfd_set_gp_value (output_bfd, gp);
4933 if (r_symndx < extsymoff
4934 || (elf_bad_symtab (input_bfd)
4935 && local_sections[r_symndx] != NULL))
4937 /* This is a relocation against a section. The current
4938 addend in the instruction is the difference between
4939 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4940 must change this to be the difference between the
4941 final definition (which will end up in RELOCATION)
4942 and the GP value of OUTPUT_BFD (which is in GP). */
4943 addend = elf_gp (input_bfd) - gp;
4945 else if (! info->relocateable)
4947 /* We are doing a final link. The current addend in the
4948 instruction is simply the desired offset into the
4949 symbol (normally zero). We want the instruction to
4950 hold the difference between the final definition of
4951 the symbol (which will end up in RELOCATION) and the
4952 GP value of OUTPUT_BFD (which is in GP). */
4957 /* We are generating relocateable output, and we aren't
4958 going to define this symbol, so we just leave the
4959 instruction alone. */
4967 if (info->relocateable)
4969 /* This is a relocateable link. We don't have to change
4970 anything, unless the reloc is against a section symbol,
4971 in which case we have to adjust according to where the
4972 section symbol winds up in the output section. */
4973 if (r_symndx >= locsymcount
4974 || (elf_bad_symtab (input_bfd)
4975 && local_sections[r_symndx] == NULL))
4979 sym = local_syms + r_symndx;
4980 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4984 sec = local_sections[r_symndx];
4986 /* It would be logical to add sym->st_value here,
4987 but Irix 5 sometimes generates a garbage symbol
4989 addend += sec->output_offset;
4991 /* If this is HI16 or GOT16 with an associated LO16,
4992 adjust the addend accordingly. Otherwise, just
4994 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4995 r = _bfd_relocate_contents (howto, input_bfd,
4997 contents + rel->r_offset + 4);
4998 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4999 r = _bfd_relocate_contents (howto, input_bfd,
5001 contents + rel->r_offset);
5004 Elf_Internal_Rela *lorel;
5006 /* As a GNU extension, permit an arbitrary
5007 number of R_MIPS_HI16 relocs before the
5008 R_MIPS_LO16 reloc. This permits gcc to emit
5009 the HI and LO relocs itself. */
5010 if (r_type == R_MIPS_GOT16)
5014 for (lorel = rel + 1;
5016 && (ELF32_R_TYPE (lorel->r_info)
5022 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5024 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5029 r = _bfd_relocate_contents (howto, input_bfd,
5031 contents + rel->r_offset);
5040 boolean undefined_error;
5042 /* This is a final link. */
5043 undefined_error = false;
5045 if (r_symndx < extsymoff
5046 || (elf_bad_symtab (input_bfd)
5047 && local_sections[r_symndx] != NULL))
5050 sym = local_syms + r_symndx;
5051 sec = local_sections[r_symndx];
5052 relocation = (sec->output_section->vma
5053 + sec->output_offset);
5055 /* It would be logical to always add sym->st_value here,
5056 but Irix 5 sometimes generates a garbage symbol
5058 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5059 relocation += sym->st_value;
5061 /* mips16 text labels should be treated as odd. */
5062 if (sym->st_other == STO_MIPS16)
5070 indx = r_symndx - extsymoff;
5071 h = elf_sym_hashes (input_bfd)[indx];
5072 while (h->root.type == bfd_link_hash_indirect
5073 || h->root.type == bfd_link_hash_warning)
5074 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5075 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5079 if (! ((*info->callbacks->reloc_dangerous)
5081 _("_gp_disp used when GP not defined"),
5082 input_bfd, input_section,
5085 /* Only give the error once per link. */
5087 _bfd_set_gp_value (output_bfd, gp);
5092 sec = input_section;
5093 if (sec->output_section != NULL)
5096 + sec->output_section->vma
5097 + sec->output_offset));
5099 relocation = gp - rel->r_offset;
5100 if (r_type == R_MIPS_LO16)
5104 else if (h->root.type == bfd_link_hash_defined
5105 || h->root.type == bfd_link_hash_defweak)
5107 sec = h->root.u.def.section;
5108 if (sec->output_section == NULL)
5111 relocation = (h->root.u.def.value
5112 + sec->output_section->vma
5113 + sec->output_offset);
5115 else if (h->root.type == bfd_link_hash_undefweak)
5117 else if (info->shared && !info->symbolic && !info->no_undefined)
5119 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5121 /* If this is a dynamic link, we should have created
5122 a _DYNAMIC_LINK symbol in
5123 mips_elf_create_dynamic_sections. Otherwise, we
5124 should define the symbol with a value of 0.
5125 FIXME: It should probably get into the symbol
5126 table somehow as well. */
5127 BFD_ASSERT (! info->shared);
5128 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5129 ".dynamic") == NULL);
5134 if (! ((*info->callbacks->undefined_symbol)
5135 (info, h->root.root.string, input_bfd,
5136 input_section, rel->r_offset)))
5138 undefined_error = true;
5143 mh = (struct mips_elf_link_hash_entry *) h;
5146 else if (sym != NULL)
5147 other = sym->st_other;
5151 /* If this function has an fn_stub, then it is a mips16
5152 function which needs a stub if it is called by a 32 bit
5153 function. If this reloc is anything other than a 16 bit
5154 call, redirect the reloc to the stub. We don't redirect
5155 relocs from other stub functions. */
5156 if (r_type != R_MIPS16_26
5158 && mh->fn_stub != NULL)
5160 && elf_tdata (input_bfd)->local_stubs != NULL
5161 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5162 && strncmp (bfd_get_section_name (input_bfd, input_section),
5163 FN_STUB, sizeof FN_STUB - 1) != 0
5164 && strncmp (bfd_get_section_name (input_bfd, input_section),
5165 CALL_STUB, sizeof CALL_STUB - 1) != 0
5166 && strncmp (bfd_get_section_name (input_bfd, input_section),
5167 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5171 BFD_ASSERT (mh->need_fn_stub);
5172 relocation = (mh->fn_stub->output_section->vma
5173 + mh->fn_stub->output_offset);
5179 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5180 relocation = (fn_stub->output_section->vma
5181 + fn_stub->output_offset);
5184 /* RELOCATION now points to 32 bit code. */
5188 /* If this function has a call_stub, then it is called by a
5189 mips16 function; the call needs to go through a stub if
5190 this function is a 32 bit function. If this reloc is a
5191 16 bit call, and the symbol is not a 16 bit function,
5192 then redirect the reloc to the stub. Note that we don't
5193 need to worry about calling the function through a
5194 function pointer; such calls are handled by routing
5195 through a special mips16 routine. We don't have to check
5196 whether this call is from a stub; it can't be, because a
5197 stub contains 32 bit code, and hence can not have a 16
5199 if (r_type == R_MIPS16_26
5201 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5202 && other != STO_MIPS16)
5206 /* If both call_stub and call_fp_stub are defined, we
5207 can figure out which one to use by seeing which one
5208 appears in the input file. */
5209 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5214 for (o = input_bfd->sections; o != NULL; o = o->next)
5216 if (strncmp (bfd_get_section_name (input_bfd, o),
5217 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5219 stub = mh->call_fp_stub;
5224 stub = mh->call_stub;
5226 else if (mh->call_stub != NULL)
5227 stub = mh->call_stub;
5229 stub = mh->call_fp_stub;
5231 BFD_ASSERT (stub->_raw_size > 0);
5232 relocation = stub->output_section->vma + stub->output_offset;
5235 if (r_type == R_MIPS_HI16)
5237 Elf_Internal_Rela *lorel;
5239 /* As a GNU extension, permit an arbitrary number of
5240 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5241 This permits gcc to emit the HI and LO relocs itself. */
5242 for (lorel = rel + 1;
5244 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5248 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5250 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5251 contents, relocation + addend);
5255 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5256 contents, rel->r_offset,
5257 relocation, addend);
5259 else if (r_type == R_MIPS_GOT16 && local)
5261 /* GOT16 must also have an associated LO16 in the local
5262 case. In this case, the addend is extracted and the
5263 section in which the referenced object is determined.
5264 Then the final address of the object is computed and
5265 the GOT entry for the hipage (an aligned 64kb chunk)
5266 is added to .got section if needed. The offset field
5267 of the GOT16-relocated instruction is replaced by the
5268 index of this GOT entry for the hipage. */
5269 if ((rel + 1) < relend
5270 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5272 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5275 relocation + addend))
5280 r = bfd_reloc_outofrange;
5282 else if (r_type == R_MIPS_CALL16
5283 || r_type == R_MIPS_GOT16
5284 || r_type == R_MIPS_CALL_LO16
5285 || r_type == R_MIPS_GOT_LO16)
5289 /* This symbol must be registered as a global symbol
5290 having the corresponding got entry. */
5291 BFD_ASSERT (h->got.offset != (bfd_vma) -1);
5293 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5294 BFD_ASSERT (g->local_gotno <= offset
5295 && offset < sgot->_raw_size);
5296 bfd_put_32 (output_bfd, relocation + addend,
5297 sgot->contents + offset);
5298 offset = (sgot->output_section->vma + sgot->output_offset
5300 mips_elf_relocate_global_got (input_bfd, rel, contents,
5304 else if (r_type == R_MIPS_CALL_HI16
5305 || r_type == R_MIPS_GOT_HI16)
5309 /* This must be a global symbol with a got entry. The
5310 next reloc must be the corresponding LO16 reloc. */
5311 BFD_ASSERT (h != NULL && h->got.offset != (bfd_vma) -1);
5312 BFD_ASSERT ((rel + 1) < relend);
5313 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5314 == (r_type == R_MIPS_CALL_HI16
5315 ? (int) R_MIPS_CALL_LO16
5316 : (int) R_MIPS_GOT_LO16));
5318 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5319 BFD_ASSERT (g->local_gotno <= offset
5320 && offset < sgot->_raw_size);
5321 bfd_put_32 (output_bfd, relocation + addend,
5322 sgot->contents + offset);
5323 offset = (sgot->output_section->vma + sgot->output_offset
5325 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5329 else if (r_type == R_MIPS_REL32
5330 || r_type == R_MIPS_32)
5332 Elf_Internal_Rel outrel;
5333 Elf32_crinfo cptrel;
5337 || (elf_hash_table (info)->dynamic_sections_created
5339 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5341 && (input_section->flags & SEC_ALLOC) != 0)
5345 /* When generating a shared object, these
5346 relocations are copied into the output file to be
5347 resolved at run time. */
5350 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5351 BFD_ASSERT (sreloc != NULL);
5356 if (elf_section_data (input_section)->stab_info == NULL)
5357 outrel.r_offset = rel->r_offset;
5362 off = (_bfd_stab_section_offset
5363 (output_bfd, &elf_hash_table (info)->stab_info,
5365 &elf_section_data (input_section)->stab_info,
5367 if (off == (bfd_vma) -1)
5369 outrel.r_offset = off;
5372 outrel.r_offset += (input_section->output_section->vma
5373 + input_section->output_offset);
5375 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5378 memset (&outrel, 0, sizeof outrel);
5380 && (! info->symbolic
5381 || (h->elf_link_hash_flags
5382 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5384 BFD_ASSERT (h->dynindx != -1);
5385 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5386 sec = input_section;
5393 sec = local_sections[r_symndx];
5396 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5398 == bfd_link_hash_defweak));
5399 sec = h->root.u.def.section;
5401 if (sec != NULL && bfd_is_abs_section (sec))
5403 else if (sec == NULL || sec->owner == NULL)
5405 bfd_set_error (bfd_error_bad_value);
5412 osec = sec->output_section;
5413 indx = elf_section_data (osec)->dynindx;
5418 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5419 addend += relocation;
5423 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5425 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5426 (((Elf32_External_Rel *)
5428 + sreloc->reloc_count));
5429 ++sreloc->reloc_count;
5431 if (! skip && SGI_COMPAT (output_bfd))
5436 /* Make an entry of compact relocation info. */
5437 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5438 cptrel.vaddr = (rel->r_offset
5439 + input_section->output_section->vma
5440 + input_section->output_offset);
5441 if (r_type == R_MIPS_REL32)
5442 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5444 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5445 mips_elf_set_cr_dist2to (cptrel, 0);
5446 cptrel.konst = addend;
5448 cr = (scpt->contents
5449 + sizeof (Elf32_External_compact_rel));
5450 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5451 ((Elf32_External_crinfo *) cr
5452 + scpt->reloc_count));
5453 ++scpt->reloc_count;
5456 /* This reloc will be computed at runtime, so
5457 there's no need to do anything now. */
5461 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5462 contents, rel->r_offset,
5463 relocation, addend);
5465 else if (r_type == R_MIPS_64)
5470 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5471 addr = rel->r_offset;
5472 if (bfd_big_endian (input_bfd))
5474 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5475 contents, addr, relocation,
5477 val = bfd_get_32 (input_bfd, contents + addr);
5478 if ((val & 0x80000000) != 0)
5482 addr = rel->r_offset;
5483 if (bfd_little_endian (input_bfd))
5485 bfd_put_32 (input_bfd, val, contents + addr);
5487 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5491 /* This is a jump to a mips16 routine from a mips32
5492 routine. We need to change jal into jalx. */
5493 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5494 if (((insn >> 26) & 0x3f) != 0x3
5495 && ((insn >> 26) & 0x3f) != 0x1d)
5497 (*_bfd_error_handler)
5498 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5499 bfd_get_filename (input_bfd),
5500 input_section->name,
5501 (unsigned long) rel->r_offset);
5502 bfd_set_error (bfd_error_bad_value);
5505 insn = (insn & 0x3ffffff) | (0x1d << 26);
5506 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5507 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5508 contents, rel->r_offset,
5509 relocation, addend);
5511 else if (r_type == R_MIPS16_26)
5513 /* It's easiest to do the normal relocation, and then
5514 dig out the instruction and swap the first word the
5515 way the mips16 expects it. If this is little endian,
5516 though, we need to swap the two words first, and then
5517 swap them back again later, so that the address looks
5520 if (bfd_little_endian (input_bfd))
5524 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5525 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5526 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5529 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5530 contents, rel->r_offset,
5531 relocation, addend);
5532 if (r == bfd_reloc_ok)
5536 if (bfd_little_endian (input_bfd))
5538 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5539 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5540 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5543 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5544 insn = ((insn & 0xfc00)
5545 | ((insn & 0x1f) << 5)
5546 | ((insn & 0x3e0) >> 5));
5547 /* If this is a jump to a 32 bit routine, then make
5549 if (other != STO_MIPS16)
5551 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5554 else if (r_type == R_MIPS16_GPREL)
5556 unsigned short extend, insn;
5558 unsigned long final;
5560 /* Extract the addend into buf, run the regular reloc,
5561 and stuff the resulting value back into the
5563 if (rel->r_offset > input_section->_raw_size)
5564 r = bfd_reloc_outofrange;
5567 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5568 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5569 bfd_put_32 (input_bfd,
5570 (((extend & 0x1f) << 11)
5574 r = _bfd_final_link_relocate (howto, input_bfd,
5576 (bfd_vma) 0, relocation,
5578 final = bfd_get_32 (input_bfd, buf);
5579 bfd_put_16 (input_bfd,
5581 | ((final >> 11) & 0x1f)
5583 contents + rel->r_offset);
5584 bfd_put_16 (input_bfd,
5587 contents + rel->r_offset + 2);
5591 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5592 contents, rel->r_offset,
5593 relocation, addend);
5595 /* The jal instruction can only jump to an address which is
5596 divisible by 4, and it can only jump to an address with
5597 the same upper 4 bits as the PC. */
5598 if (r == bfd_reloc_ok
5599 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5605 pc = ((input_section->output_section->vma
5606 + input_section->output_offset
5609 target = bfd_get_32 (input_bfd, contents + rel->r_offset)
5610 & (howto->dst_mask);
5611 addr = (target << 2) + pc;
5612 if (other == STO_MIPS16)
5613 addr &= ~ (bfd_vma) 1;
5615 || ((addr & 0xf0000000)
5617 r = bfd_reloc_overflow;
5620 /* Don't bother to report a relocation overflow for a call
5621 to a weak undefined symbol with a value of zero. This
5624 even if we aren't in range to call address zero. */
5625 if (r == bfd_reloc_overflow
5626 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5627 && relocation + addend == 0
5629 && h->root.type == bfd_link_hash_undefweak)
5632 /* If we've already issued an error for an undefined symbol,
5633 don't issue another useless error. */
5635 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5638 if (SGI_COMPAT (abfd)
5640 && (input_section->flags & SEC_ALLOC) != 0)
5642 Elf32_crinfo cptrel;
5645 /* Make an entry of compact relocation info. */
5646 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5647 cptrel.vaddr = (rel->r_offset
5648 + input_section->output_section->vma
5649 + input_section->output_offset);
5654 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5655 /* XXX How should we set dist2to in this case. */
5656 mips_elf_set_cr_dist2to (cptrel, 8);
5657 cptrel.konst = addend + relocation;
5658 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5659 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5660 ((Elf32_External_crinfo *) cr
5661 + scpt->reloc_count));
5662 ++scpt->reloc_count;
5665 case R_MIPS_GPREL16:
5666 case R_MIPS_LITERAL:
5667 case R_MIPS_GPREL32:
5668 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5669 cptrel.konst = gp - cptrel.vaddr;
5670 mips_elf_set_cr_dist2to (cptrel, 4);
5671 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5672 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5673 ((Elf32_External_crinfo *) cr
5674 + scpt->reloc_count));
5675 ++scpt->reloc_count;
5684 if (r != bfd_reloc_ok)
5689 case bfd_reloc_outofrange:
5691 case bfd_reloc_overflow:
5696 name = h->root.root.string;
5699 name = bfd_elf_string_from_elf_section (input_bfd,
5700 symtab_hdr->sh_link,
5705 name = bfd_section_name (input_bfd, sec);
5707 if (! ((*info->callbacks->reloc_overflow)
5708 (info, name, howto->name, (bfd_vma) 0,
5709 input_bfd, input_section, rel->r_offset)))
5720 /* This hook function is called before the linker writes out a global
5721 symbol. We mark symbols as small common if appropriate. This is
5722 also where we undo the increment of the value for a mips16 symbol. */
5726 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5728 struct bfd_link_info *info;
5730 Elf_Internal_Sym *sym;
5731 asection *input_sec;
5733 /* If we see a common symbol, which implies a relocatable link, then
5734 if a symbol was small common in an input file, mark it as small
5735 common in the output file. */
5736 if (sym->st_shndx == SHN_COMMON
5737 && strcmp (input_sec->name, ".scommon") == 0)
5738 sym->st_shndx = SHN_MIPS_SCOMMON;
5740 if (sym->st_other == STO_MIPS16
5741 && (sym->st_value & 1) != 0)
5747 /* Functions for the dynamic linker. */
5749 /* The name of the dynamic interpreter. This is put in the .interp
5752 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5754 /* Create dynamic sections when linking against a dynamic object. */
5757 mips_elf_create_dynamic_sections (abfd, info)
5759 struct bfd_link_info *info;
5761 struct elf_link_hash_entry *h;
5763 register asection *s;
5764 const char * const *namep;
5766 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5767 | SEC_LINKER_CREATED | SEC_READONLY);
5769 /* Mips ABI requests the .dynamic section to be read only. */
5770 s = bfd_get_section_by_name (abfd, ".dynamic");
5773 if (! bfd_set_section_flags (abfd, s, flags))
5777 /* We need to create .got section. */
5778 if (! mips_elf_create_got_section (abfd, info))
5781 /* Create .stub section. */
5782 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5784 s = bfd_make_section (abfd, ".stub");
5786 || ! bfd_set_section_flags (abfd, s, flags)
5787 || ! bfd_set_section_alignment (abfd, s, 2))
5791 if (SGI_COMPAT (abfd)
5793 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5795 s = bfd_make_section (abfd, ".rld_map");
5797 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5798 || ! bfd_set_section_alignment (abfd, s, 2))
5802 if (SGI_COMPAT (abfd))
5804 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5807 if (! (_bfd_generic_link_add_one_symbol
5808 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5809 (bfd_vma) 0, (const char *) NULL, false,
5810 get_elf_backend_data (abfd)->collect,
5811 (struct bfd_link_hash_entry **) &h)))
5813 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5814 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5815 h->type = STT_SECTION;
5817 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5821 /* We need to create a .compact_rel section. */
5822 if (! mips_elf_create_compact_rel_section (abfd, info))
5825 /* Change aligments of some sections. */
5826 s = bfd_get_section_by_name (abfd, ".hash");
5828 bfd_set_section_alignment (abfd, s, 4);
5829 s = bfd_get_section_by_name (abfd, ".dynsym");
5831 bfd_set_section_alignment (abfd, s, 4);
5832 s = bfd_get_section_by_name (abfd, ".dynstr");
5834 bfd_set_section_alignment (abfd, s, 4);
5835 s = bfd_get_section_by_name (abfd, ".reginfo");
5837 bfd_set_section_alignment (abfd, s, 4);
5838 s = bfd_get_section_by_name (abfd, ".dynamic");
5840 bfd_set_section_alignment (abfd, s, 4);
5846 if (! (_bfd_generic_link_add_one_symbol
5847 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5848 (bfd_vma) 0, (const char *) NULL, false,
5849 get_elf_backend_data (abfd)->collect,
5850 (struct bfd_link_hash_entry **) &h)))
5852 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5853 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5854 h->type = STT_SECTION;
5856 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5859 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5861 /* __rld_map is a four byte word located in the .data section
5862 and is filled in by the rtld to contain a pointer to
5863 the _r_debug structure. Its symbol value will be set in
5864 mips_elf_finish_dynamic_symbol. */
5865 s = bfd_get_section_by_name (abfd, ".rld_map");
5866 BFD_ASSERT (s != NULL);
5869 if (! (_bfd_generic_link_add_one_symbol
5870 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5871 (bfd_vma) 0, (const char *) NULL, false,
5872 get_elf_backend_data (abfd)->collect,
5873 (struct bfd_link_hash_entry **) &h)))
5875 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5876 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5877 h->type = STT_OBJECT;
5879 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5887 /* Create the .compact_rel section. */
5890 mips_elf_create_compact_rel_section (abfd, info)
5892 struct bfd_link_info *info;
5895 register asection *s;
5897 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5899 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5902 s = bfd_make_section (abfd, ".compact_rel");
5904 || ! bfd_set_section_flags (abfd, s, flags)
5905 || ! bfd_set_section_alignment (abfd, s, 2))
5908 s->_raw_size = sizeof (Elf32_External_compact_rel);
5914 /* Create the .got section to hold the global offset table. */
5917 mips_elf_create_got_section (abfd, info)
5919 struct bfd_link_info *info;
5922 register asection *s;
5923 struct elf_link_hash_entry *h;
5924 struct mips_got_info *g;
5926 /* This function may be called more than once. */
5927 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5930 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5931 | SEC_LINKER_CREATED);
5933 s = bfd_make_section (abfd, ".got");
5935 || ! bfd_set_section_flags (abfd, s, flags)
5936 || ! bfd_set_section_alignment (abfd, s, 4))
5939 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5940 linker script because we don't want to define the symbol if we
5941 are not creating a global offset table. */
5943 if (! (_bfd_generic_link_add_one_symbol
5944 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5945 (bfd_vma) 0, (const char *) NULL, false,
5946 get_elf_backend_data (abfd)->collect,
5947 (struct bfd_link_hash_entry **) &h)))
5949 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5950 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5951 h->type = STT_OBJECT;
5954 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5957 /* The first several global offset table entries are reserved. */
5958 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5960 g = (struct mips_got_info *) bfd_alloc (abfd,
5961 sizeof (struct mips_got_info));
5964 g->global_gotsym = 0;
5965 g->local_gotno = MIPS_RESERVED_GOTNO;
5966 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5967 if (elf_section_data (s) == NULL)
5970 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5971 if (elf_section_data (s) == NULL)
5974 elf_section_data (s)->tdata = (PTR) g;
5979 /* Look through the relocs for a section during the first phase, and
5980 allocate space in the global offset table. */
5983 mips_elf_check_relocs (abfd, info, sec, relocs)
5985 struct bfd_link_info *info;
5987 const Elf_Internal_Rela *relocs;
5991 Elf_Internal_Shdr *symtab_hdr;
5992 struct elf_link_hash_entry **sym_hashes;
5993 struct mips_got_info *g;
5995 const Elf_Internal_Rela *rel;
5996 const Elf_Internal_Rela *rel_end;
6000 if (info->relocateable)
6003 dynobj = elf_hash_table (info)->dynobj;
6004 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6005 sym_hashes = elf_sym_hashes (abfd);
6006 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6008 /* Check for the mips16 stub sections. */
6010 name = bfd_get_section_name (abfd, sec);
6011 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6013 unsigned long r_symndx;
6015 /* Look at the relocation information to figure out which symbol
6018 r_symndx = ELF32_R_SYM (relocs->r_info);
6020 if (r_symndx < extsymoff
6021 || sym_hashes[r_symndx - extsymoff] == NULL)
6025 /* This stub is for a local symbol. This stub will only be
6026 needed if there is some relocation in this BFD, other
6027 than a 16 bit function call, which refers to this symbol. */
6028 for (o = abfd->sections; o != NULL; o = o->next)
6030 Elf_Internal_Rela *sec_relocs;
6031 const Elf_Internal_Rela *r, *rend;
6033 /* We can ignore stub sections when looking for relocs. */
6034 if ((o->flags & SEC_RELOC) == 0
6035 || o->reloc_count == 0
6036 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6037 sizeof FN_STUB - 1) == 0
6038 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6039 sizeof CALL_STUB - 1) == 0
6040 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6041 sizeof CALL_FP_STUB - 1) == 0)
6044 sec_relocs = (_bfd_elf32_link_read_relocs
6045 (abfd, o, (PTR) NULL,
6046 (Elf_Internal_Rela *) NULL,
6047 info->keep_memory));
6048 if (sec_relocs == NULL)
6051 rend = sec_relocs + o->reloc_count;
6052 for (r = sec_relocs; r < rend; r++)
6053 if (ELF32_R_SYM (r->r_info) == r_symndx
6054 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6057 if (! info->keep_memory)
6066 /* There is no non-call reloc for this stub, so we do
6067 not need it. Since this function is called before
6068 the linker maps input sections to output sections, we
6069 can easily discard it by setting the SEC_EXCLUDE
6071 sec->flags |= SEC_EXCLUDE;
6075 /* Record this stub in an array of local symbol stubs for
6077 if (elf_tdata (abfd)->local_stubs == NULL)
6079 unsigned long symcount;
6082 if (elf_bad_symtab (abfd))
6083 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6085 symcount = symtab_hdr->sh_info;
6086 n = (asection **) bfd_zalloc (abfd,
6087 symcount * sizeof (asection *));
6090 elf_tdata (abfd)->local_stubs = n;
6093 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6095 /* We don't need to set mips16_stubs_seen in this case.
6096 That flag is used to see whether we need to look through
6097 the global symbol table for stubs. We don't need to set
6098 it here, because we just have a local stub. */
6102 struct mips_elf_link_hash_entry *h;
6104 h = ((struct mips_elf_link_hash_entry *)
6105 sym_hashes[r_symndx - extsymoff]);
6107 /* H is the symbol this stub is for. */
6110 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6113 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6114 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6116 unsigned long r_symndx;
6117 struct mips_elf_link_hash_entry *h;
6120 /* Look at the relocation information to figure out which symbol
6123 r_symndx = ELF32_R_SYM (relocs->r_info);
6125 if (r_symndx < extsymoff
6126 || sym_hashes[r_symndx - extsymoff] == NULL)
6128 /* This stub was actually built for a static symbol defined
6129 in the same file. We assume that all static symbols in
6130 mips16 code are themselves mips16, so we can simply
6131 discard this stub. Since this function is called before
6132 the linker maps input sections to output sections, we can
6133 easily discard it by setting the SEC_EXCLUDE flag. */
6134 sec->flags |= SEC_EXCLUDE;
6138 h = ((struct mips_elf_link_hash_entry *)
6139 sym_hashes[r_symndx - extsymoff]);
6141 /* H is the symbol this stub is for. */
6143 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6144 loc = &h->call_fp_stub;
6146 loc = &h->call_stub;
6148 /* If we already have an appropriate stub for this function, we
6149 don't need another one, so we can discard this one. Since
6150 this function is called before the linker maps input sections
6151 to output sections, we can easily discard it by setting the
6152 SEC_EXCLUDE flag. We can also discard this section if we
6153 happen to already know that this is a mips16 function; it is
6154 not necessary to check this here, as it is checked later, but
6155 it is slightly faster to check now. */
6156 if (*loc != NULL || h->root.other == STO_MIPS16)
6158 sec->flags |= SEC_EXCLUDE;
6163 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6173 sgot = bfd_get_section_by_name (dynobj, ".got");
6178 BFD_ASSERT (elf_section_data (sgot) != NULL);
6179 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6180 BFD_ASSERT (g != NULL);
6186 rel_end = relocs + sec->reloc_count;
6187 for (rel = relocs; rel < rel_end; rel++)
6189 unsigned long r_symndx;
6190 struct elf_link_hash_entry *h;
6192 r_symndx = ELF32_R_SYM (rel->r_info);
6194 if (r_symndx < extsymoff)
6198 h = sym_hashes[r_symndx - extsymoff];
6200 /* This may be an indirect symbol created because of a version. */
6203 while (h->root.type == bfd_link_hash_indirect)
6204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6208 /* Some relocs require a global offset table. */
6209 if (dynobj == NULL || sgot == NULL)
6211 switch (ELF32_R_TYPE (rel->r_info))
6215 case R_MIPS_CALL_HI16:
6216 case R_MIPS_CALL_LO16:
6217 case R_MIPS_GOT_HI16:
6218 case R_MIPS_GOT_LO16:
6220 elf_hash_table (info)->dynobj = dynobj = abfd;
6221 if (! mips_elf_create_got_section (dynobj, info))
6223 sgot = bfd_get_section_by_name (dynobj, ".got");
6224 BFD_ASSERT (sgot != NULL);
6225 BFD_ASSERT (elf_section_data (sgot) != NULL);
6226 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6227 BFD_ASSERT (g != NULL);
6233 && (info->shared || h != NULL)
6234 && (sec->flags & SEC_ALLOC) != 0)
6235 elf_hash_table (info)->dynobj = dynobj = abfd;
6243 switch (ELF32_R_TYPE (rel->r_info))
6246 case R_MIPS_CALL_HI16:
6247 case R_MIPS_CALL_LO16:
6248 /* This symbol requires a global offset table entry. */
6252 (*_bfd_error_handler)
6253 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6254 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6255 bfd_set_error (bfd_error_bad_value);
6259 /* Make sure this symbol is output as a dynamic symbol. */
6260 if (h->dynindx == -1)
6262 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6266 if (h->got.offset != (bfd_vma) -1)
6268 /* We have already allocated space in the .got. */
6272 /* Note the index of the first global got symbol in .dynsym. */
6273 if (g->global_gotsym == 0
6274 || g->global_gotsym > (unsigned long) h->dynindx)
6275 g->global_gotsym = h->dynindx;
6277 /* Make this symbol to have the corresponding got entry. */
6280 /* We need a stub, not a plt entry for the undefined
6281 function. But we record it as if it needs plt. See
6282 elf_adjust_dynamic_symbol in elflink.h. */
6283 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6289 case R_MIPS_GOT_HI16:
6290 case R_MIPS_GOT_LO16:
6291 /* This symbol requires a global offset table entry. */
6295 /* Make sure this symbol is output as a dynamic symbol. */
6296 if (h->dynindx == -1)
6298 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6302 if (h->got.offset != (bfd_vma) -1)
6304 /* We have already allocated space in the .got. */
6307 /* Note the index of the first global got symbol in
6309 if (g->global_gotsym == 0
6310 || g->global_gotsym > (unsigned long) h->dynindx)
6311 g->global_gotsym = h->dynindx;
6313 /* Make this symbol to be the global got symbol. */
6321 if ((info->shared || h != NULL)
6322 && (sec->flags & SEC_ALLOC) != 0)
6326 const char *name = ".rel.dyn";
6328 sreloc = bfd_get_section_by_name (dynobj, name);
6331 sreloc = bfd_make_section (dynobj, name);
6333 || ! bfd_set_section_flags (dynobj, sreloc,
6338 | SEC_LINKER_CREATED
6340 || ! bfd_set_section_alignment (dynobj, sreloc,
6347 /* When creating a shared object, we must copy these
6348 reloc types into the output file as R_MIPS_REL32
6349 relocs. We make room for this reloc in the
6350 .rel.dyn reloc section */
6351 if (sreloc->_raw_size == 0)
6353 /* Add a null element. */
6354 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6355 ++sreloc->reloc_count;
6357 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6361 struct mips_elf_link_hash_entry *hmips;
6363 /* We only need to copy this reloc if the symbol is
6364 defined in a dynamic object. */
6365 hmips = (struct mips_elf_link_hash_entry *) h;
6366 ++hmips->mips_32_relocs;
6370 if (SGI_COMPAT (abfd))
6371 mips_elf_hash_table (info)->compact_rel_size +=
6372 sizeof (Elf32_External_crinfo);
6377 case R_MIPS_GPREL16:
6378 case R_MIPS_LITERAL:
6379 case R_MIPS_GPREL32:
6380 if (SGI_COMPAT (abfd))
6381 mips_elf_hash_table (info)->compact_rel_size +=
6382 sizeof (Elf32_External_crinfo);
6385 /* This relocation describes the C++ object vtable hierarchy.
6386 Reconstruct it for later use during GC. */
6387 case R_MIPS_GNU_VTINHERIT:
6388 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6392 /* This relocation describes which C++ vtable entries are actually
6393 used. Record for later use during GC. */
6394 case R_MIPS_GNU_VTENTRY:
6395 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6403 /* If this reloc is not a 16 bit call, and it has a global
6404 symbol, then we will need the fn_stub if there is one.
6405 References from a stub section do not count. */
6407 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6408 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6409 sizeof FN_STUB - 1) != 0
6410 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6411 sizeof CALL_STUB - 1) != 0
6412 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6413 sizeof CALL_FP_STUB - 1) != 0)
6415 struct mips_elf_link_hash_entry *mh;
6417 mh = (struct mips_elf_link_hash_entry *) h;
6418 mh->need_fn_stub = true;
6425 /* Return the section that should be marked against GC for a given
6429 mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
6431 struct bfd_link_info *info;
6432 Elf_Internal_Rela *rel;
6433 struct elf_link_hash_entry *h;
6434 Elf_Internal_Sym *sym;
6436 /* ??? Do mips16 stub sections need to be handled special? */
6440 switch (ELF32_R_TYPE (rel->r_info))
6442 case R_MIPS_GNU_VTINHERIT:
6443 case R_MIPS_GNU_VTENTRY:
6447 switch (h->root.type)
6449 case bfd_link_hash_defined:
6450 case bfd_link_hash_defweak:
6451 return h->root.u.def.section;
6453 case bfd_link_hash_common:
6454 return h->root.u.c.p->section;
6463 if (!(elf_bad_symtab (abfd)
6464 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6465 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
6466 && sym->st_shndx != SHN_COMMON))
6468 return bfd_section_from_elf_index (abfd, sym->st_shndx);
6475 /* Update the got entry reference counts for the section being removed. */
6478 mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6480 struct bfd_link_info *info;
6482 const Elf_Internal_Rela *relocs;
6485 Elf_Internal_Shdr *symtab_hdr;
6486 struct elf_link_hash_entry **sym_hashes;
6487 bfd_signed_vma *local_got_refcounts;
6488 const Elf_Internal_Rela *rel, *relend;
6489 unsigned long r_symndx;
6490 struct elf_link_hash_entry *h;
6492 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6493 sym_hashes = elf_sym_hashes (abfd);
6494 local_got_refcounts = elf_local_got_refcounts (abfd);
6496 relend = relocs + sec->reloc_count;
6497 for (rel = relocs; rel < relend; rel++)
6498 switch (ELF32_R_TYPE (rel->r_info))
6502 case R_MIPS_CALL_HI16:
6503 case R_MIPS_CALL_LO16:
6504 case R_MIPS_GOT_HI16:
6505 case R_MIPS_GOT_LO16:
6506 /* ??? It would seem that the existing MIPS code does no sort
6507 of reference counting or whatnot on its GOT and PLT entries,
6508 so it is not possible to garbage collect them at this time. */
6520 /* Adjust a symbol defined by a dynamic object and referenced by a
6521 regular object. The current definition is in some section of the
6522 dynamic object, but we're not including those sections. We have to
6523 change the definition to something the rest of the link can
6527 mips_elf_adjust_dynamic_symbol (info, h)
6528 struct bfd_link_info *info;
6529 struct elf_link_hash_entry *h;
6532 struct mips_elf_link_hash_entry *hmips;
6535 dynobj = elf_hash_table (info)->dynobj;
6537 /* Make sure we know what is going on here. */
6538 BFD_ASSERT (dynobj != NULL
6539 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6540 || h->weakdef != NULL
6541 || ((h->elf_link_hash_flags
6542 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6543 && (h->elf_link_hash_flags
6544 & ELF_LINK_HASH_REF_REGULAR) != 0
6545 && (h->elf_link_hash_flags
6546 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6548 /* If this symbol is defined in a dynamic object, we need to copy
6549 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6551 hmips = (struct mips_elf_link_hash_entry *) h;
6552 if (! info->relocateable
6553 && hmips->mips_32_relocs != 0
6554 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6556 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6557 BFD_ASSERT (s != NULL);
6559 if (s->_raw_size == 0)
6561 /* Make room for a null element. */
6562 s->_raw_size += sizeof (Elf32_External_Rel);
6565 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6568 /* For a function, create a stub, if needed. */
6569 if (h->type == STT_FUNC
6570 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6572 if (! elf_hash_table (info)->dynamic_sections_created)
6575 /* If this symbol is not defined in a regular file, then set
6576 the symbol to the stub location. This is required to make
6577 function pointers compare as equal between the normal
6578 executable and the shared library. */
6579 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6581 /* We need .stub section. */
6582 s = bfd_get_section_by_name (dynobj, ".stub");
6583 BFD_ASSERT (s != NULL);
6585 h->root.u.def.section = s;
6586 h->root.u.def.value = s->_raw_size;
6588 /* XXX Write this stub address somewhere. */
6589 h->plt.offset = s->_raw_size;
6591 /* Make room for this stub code. */
6592 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6594 /* The last half word of the stub will be filled with the index
6595 of this symbol in .dynsym section. */
6600 /* If this is a weak symbol, and there is a real definition, the
6601 processor independent code will have arranged for us to see the
6602 real definition first, and we can just use the same value. */
6603 if (h->weakdef != NULL)
6605 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6606 || h->weakdef->root.type == bfd_link_hash_defweak);
6607 h->root.u.def.section = h->weakdef->root.u.def.section;
6608 h->root.u.def.value = h->weakdef->root.u.def.value;
6612 /* This is a reference to a symbol defined by a dynamic object which
6613 is not a function. */
6618 /* This function is called after all the input files have been read,
6619 and the input sections have been assigned to output sections. We
6620 check for any mips16 stub sections that we can discard. */
6622 static boolean mips_elf_check_mips16_stubs
6623 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6626 mips_elf_always_size_sections (output_bfd, info)
6628 struct bfd_link_info *info;
6632 /* The .reginfo section has a fixed size. */
6633 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6635 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6637 if (info->relocateable
6638 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6641 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6642 mips_elf_check_mips16_stubs,
6648 /* Check the mips16 stubs for a particular symbol, and see if we can
6653 mips_elf_check_mips16_stubs (h, data)
6654 struct mips_elf_link_hash_entry *h;
6657 if (h->fn_stub != NULL
6658 && ! h->need_fn_stub)
6660 /* We don't need the fn_stub; the only references to this symbol
6661 are 16 bit calls. Clobber the size to 0 to prevent it from
6662 being included in the link. */
6663 h->fn_stub->_raw_size = 0;
6664 h->fn_stub->_cooked_size = 0;
6665 h->fn_stub->flags &= ~ SEC_RELOC;
6666 h->fn_stub->reloc_count = 0;
6667 h->fn_stub->flags |= SEC_EXCLUDE;
6670 if (h->call_stub != NULL
6671 && h->root.other == STO_MIPS16)
6673 /* We don't need the call_stub; this is a 16 bit function, so
6674 calls from other 16 bit functions are OK. Clobber the size
6675 to 0 to prevent it from being included in the link. */
6676 h->call_stub->_raw_size = 0;
6677 h->call_stub->_cooked_size = 0;
6678 h->call_stub->flags &= ~ SEC_RELOC;
6679 h->call_stub->reloc_count = 0;
6680 h->call_stub->flags |= SEC_EXCLUDE;
6683 if (h->call_fp_stub != NULL
6684 && h->root.other == STO_MIPS16)
6686 /* We don't need the call_stub; this is a 16 bit function, so
6687 calls from other 16 bit functions are OK. Clobber the size
6688 to 0 to prevent it from being included in the link. */
6689 h->call_fp_stub->_raw_size = 0;
6690 h->call_fp_stub->_cooked_size = 0;
6691 h->call_fp_stub->flags &= ~ SEC_RELOC;
6692 h->call_fp_stub->reloc_count = 0;
6693 h->call_fp_stub->flags |= SEC_EXCLUDE;
6699 /* Set the sizes of the dynamic sections. */
6702 mips_elf_size_dynamic_sections (output_bfd, info)
6704 struct bfd_link_info *info;
6710 struct mips_got_info *g;
6712 dynobj = elf_hash_table (info)->dynobj;
6713 BFD_ASSERT (dynobj != NULL);
6715 if (elf_hash_table (info)->dynamic_sections_created)
6717 /* Set the contents of the .interp section to the interpreter. */
6720 s = bfd_get_section_by_name (dynobj, ".interp");
6721 BFD_ASSERT (s != NULL);
6722 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6723 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6727 /* Recompute the size of .got for local entires (reserved and
6728 hipages) if needed. To estimate it, get the upper bound of total
6729 size of loadable sections. */
6730 sgot = bfd_get_section_by_name (dynobj, ".got");
6734 bfd_size_type loadable_size = 0;
6735 bfd_size_type local_gotno;
6738 BFD_ASSERT (elf_section_data (sgot) != NULL);
6739 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6740 BFD_ASSERT (g != NULL);
6742 for (sub = info->input_bfds; sub; sub = sub->link_next)
6743 for (s = sub->sections; s != NULL; s = s->next)
6745 if ((s->flags & SEC_ALLOC) == 0)
6747 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6750 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6752 /* Assume there are two loadable segments consisting of
6753 contiguous sections. Is 5 enough? */
6754 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6755 g->local_gotno = local_gotno;
6756 sgot->_raw_size += local_gotno * 4;
6759 /* The check_relocs and adjust_dynamic_symbol entry points have
6760 determined the sizes of the various dynamic sections. Allocate
6763 for (s = dynobj->sections; s != NULL; s = s->next)
6768 /* It's OK to base decisions on the section name, because none
6769 of the dynobj section names depend upon the input files. */
6770 name = bfd_get_section_name (dynobj, s);
6772 if ((s->flags & SEC_LINKER_CREATED) == 0)
6777 if (strncmp (name, ".rel", 4) == 0)
6779 if (s->_raw_size == 0)
6781 /* We only strip the section if the output section name
6782 has the same name. Otherwise, there might be several
6783 input sections for this output section. FIXME: This
6784 code is probably not needed these days anyhow, since
6785 the linker now does not create empty output sections. */
6786 if (s->output_section != NULL
6788 bfd_get_section_name (s->output_section->owner,
6789 s->output_section)) == 0)
6794 const char *outname;
6797 /* If this relocation section applies to a read only
6798 section, then we probably need a DT_TEXTREL entry.
6799 If the relocation section is .rel.dyn, we always
6800 assert a DT_TEXTREL entry rather than testing whether
6801 there exists a relocation to a read only section or
6803 outname = bfd_get_section_name (output_bfd,
6805 target = bfd_get_section_by_name (output_bfd, outname + 4);
6807 && (target->flags & SEC_READONLY) != 0
6808 && (target->flags & SEC_ALLOC) != 0)
6809 || strcmp (outname, ".rel.dyn") == 0)
6812 /* We use the reloc_count field as a counter if we need
6813 to copy relocs into the output file. */
6814 if (strcmp (name, ".rel.dyn") != 0)
6818 else if (strncmp (name, ".got", 4) == 0)
6822 BFD_ASSERT (elf_section_data (s) != NULL);
6823 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6824 BFD_ASSERT (g != NULL);
6826 /* Fix the size of .got section for the correspondence of
6827 global symbols and got entries. This adds some useless
6828 got entries. Is this required by ABI really? */
6829 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6830 s->_raw_size += i * 4;
6832 else if (strncmp (name, ".stub", 5) == 0)
6834 /* Irix rld assumes that the function stub isn't at the end
6835 of .text section. So put a dummy. XXX */
6836 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6838 else if (! info->shared
6839 && ! mips_elf_hash_table (info)->use_rld_obj_head
6840 && strncmp (name, ".rld_map", 8) == 0)
6842 /* We add a room for __rld_map. It will be filled in by the
6843 rtld to contain a pointer to the _r_debug structure. */
6846 else if (SGI_COMPAT (output_bfd)
6847 && strncmp (name, ".compact_rel", 12) == 0)
6848 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6849 else if (strncmp (name, ".init", 5) != 0)
6851 /* It's not one of our sections, so don't allocate space. */
6857 _bfd_strip_section_from_output (s);
6861 /* Allocate memory for the section contents. */
6862 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6863 if (s->contents == NULL && s->_raw_size != 0)
6865 bfd_set_error (bfd_error_no_memory);
6868 memset (s->contents, 0, s->_raw_size);
6871 if (elf_hash_table (info)->dynamic_sections_created)
6873 /* Add some entries to the .dynamic section. We fill in the
6874 values later, in elf_mips_finish_dynamic_sections, but we
6875 must add the entries now so that we get the correct size for
6876 the .dynamic section. The DT_DEBUG entry is filled in by the
6877 dynamic linker and used by the debugger. */
6880 if (SGI_COMPAT (output_bfd))
6882 /* SGI object has the equivalence of DT_DEBUG in the
6883 DT_MIPS_RLD_MAP entry. */
6884 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6888 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6894 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6898 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6901 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6903 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6906 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6909 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6913 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6916 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6919 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6921 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6924 s = bfd_get_section_by_name (dynobj, ".liblist");
6925 BFD_ASSERT (s != NULL);
6927 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6931 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6934 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6938 /* Time stamps in executable files are a bad idea. */
6939 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6944 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6949 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6953 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6956 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6959 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6962 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6965 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6968 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6971 #if 0 /* (SGI_COMPAT) */
6972 if (! bfd_get_section_by_name (dynobj, ".init"))
6973 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6976 if (! bfd_get_section_by_name (dynobj, ".fini"))
6977 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6982 /* If we use dynamic linking, we generate a section symbol for each
6983 output section. These are local symbols, which means that they
6984 must come first in the dynamic symbol table.
6985 That means we must increment the dynamic symbol index of every
6986 other dynamic symbol. */
6989 struct mips_got_info *g;
6992 if (elf_hash_table (info)->dynamic_sections_created)
6995 /* We no longer try to restrict the set of sections which get
6996 dynamic symbol table entries, since it fails if we have
6997 other random sections which need dynamic relocations. */
6998 const char * const *namep;
6999 bfd_size_type strindex;
7000 struct bfd_strtab_hash *dynstr;
7002 if (SGI_COMPAT (output_bfd))
7004 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
7005 elf_link_hash_traverse (elf_hash_table (info),
7006 mips_elf_adjust_dynindx,
7008 elf_hash_table (info)->dynsymcount += c;
7010 dynstr = elf_hash_table (info)->dynstr;
7011 BFD_ASSERT (dynstr != NULL);
7013 for (i = 1, namep = mips_elf_dynsym_sec_names;
7017 s = bfd_get_section_by_name (output_bfd, *namep);
7019 elf_section_data (s)->dynindx = i;
7021 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
7022 if (strindex == (bfd_size_type) -1)
7025 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
7031 c = bfd_count_sections (output_bfd);
7032 elf_link_hash_traverse (elf_hash_table (info),
7033 mips_elf_adjust_dynindx,
7035 elf_hash_table (info)->dynsymcount += c;
7037 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7039 elf_section_data (s)->dynindx = i;
7040 /* These symbols will have no names, so we don't need to
7041 fiddle with dynstr_index. */
7048 BFD_ASSERT (elf_section_data (sgot) != NULL);
7049 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7050 BFD_ASSERT (g != NULL);
7052 /* If there are no global got symbols, fake the last symbol so
7054 if (g->global_gotsym)
7055 g->global_gotsym += c;
7057 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
7064 /* Increment the index of a dynamic symbol by a given amount. Called
7065 via elf_link_hash_traverse. */
7068 mips_elf_adjust_dynindx (h, cparg)
7069 struct elf_link_hash_entry *h;
7072 unsigned int *cp = (unsigned int *) cparg;
7074 if (h->dynindx != -1)
7079 /* Finish up dynamic symbol handling. We set the contents of various
7080 dynamic sections here. */
7083 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7085 struct bfd_link_info *info;
7086 struct elf_link_hash_entry *h;
7087 Elf_Internal_Sym *sym;
7092 struct mips_got_info *g;
7095 dynobj = elf_hash_table (info)->dynobj;
7096 gval = sym->st_value;
7098 if (h->plt.offset != (bfd_vma) -1)
7102 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7104 /* This symbol has a stub. Set it up. */
7106 BFD_ASSERT (h->dynindx != -1);
7108 s = bfd_get_section_by_name (dynobj, ".stub");
7109 BFD_ASSERT (s != NULL);
7111 /* Fill the stub. */
7113 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7115 bfd_put_32 (output_bfd, STUB_MOVE, p);
7118 /* FIXME: Can h->dynindex be more than 64K? */
7119 if (h->dynindx & 0xffff0000)
7122 bfd_put_32 (output_bfd, STUB_JALR, p);
7124 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7126 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7127 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7129 /* Mark the symbol as undefined. plt.offset != -1 occurs
7130 only for the referenced symbol. */
7131 sym->st_shndx = SHN_UNDEF;
7133 /* The run-time linker uses the st_value field of the symbol
7134 to reset the global offset table entry for this external
7135 to its stub address when unlinking a shared object. */
7136 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7137 sym->st_value = gval;
7140 BFD_ASSERT (h->dynindx != -1);
7142 sgot = bfd_get_section_by_name (dynobj, ".got");
7143 BFD_ASSERT (sgot != NULL);
7144 BFD_ASSERT (elf_section_data (sgot) != NULL);
7145 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7146 BFD_ASSERT (g != NULL);
7148 if ((unsigned long) h->dynindx >= g->global_gotsym)
7150 bfd_size_type offset;
7152 /* This symbol has an entry in the global offset table. Set its
7153 value to the corresponding got entry, if needed. */
7154 if (h->got.offset == (bfd_vma) -1)
7156 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
7157 BFD_ASSERT (g->local_gotno * 4 <= offset
7158 && offset < sgot->_raw_size);
7159 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
7163 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7164 name = h->root.root.string;
7165 if (strcmp (name, "_DYNAMIC") == 0
7166 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7167 sym->st_shndx = SHN_ABS;
7168 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7170 sym->st_shndx = SHN_ABS;
7171 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7174 else if (SGI_COMPAT (output_bfd))
7176 if (strcmp (name, "_gp_disp") == 0)
7178 sym->st_shndx = SHN_ABS;
7179 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7180 sym->st_value = elf_gp (output_bfd);
7182 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7183 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7185 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7186 sym->st_other = STO_PROTECTED;
7188 sym->st_shndx = SHN_MIPS_DATA;
7190 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7192 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7193 sym->st_other = STO_PROTECTED;
7194 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7195 sym->st_shndx = SHN_ABS;
7197 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7199 if (h->type == STT_FUNC)
7200 sym->st_shndx = SHN_MIPS_TEXT;
7201 else if (h->type == STT_OBJECT)
7202 sym->st_shndx = SHN_MIPS_DATA;
7206 if (SGI_COMPAT (output_bfd)
7209 if (! mips_elf_hash_table (info)->use_rld_obj_head
7210 && strcmp (name, "__rld_map") == 0)
7212 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7213 BFD_ASSERT (s != NULL);
7214 sym->st_value = s->output_section->vma + s->output_offset;
7215 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7216 if (mips_elf_hash_table (info)->rld_value == 0)
7217 mips_elf_hash_table (info)->rld_value = sym->st_value;
7219 else if (mips_elf_hash_table (info)->use_rld_obj_head
7220 && strcmp (name, "__rld_obj_head") == 0)
7222 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7223 BFD_ASSERT (s != NULL);
7224 mips_elf_hash_table (info)->rld_value = sym->st_value;
7228 /* If this is a mips16 symbol, force the value to be even. */
7229 if (sym->st_other == STO_MIPS16
7230 && (sym->st_value & 1) != 0)
7236 /* Finish up the dynamic sections. */
7239 mips_elf_finish_dynamic_sections (output_bfd, info)
7241 struct bfd_link_info *info;
7246 struct mips_got_info *g;
7248 dynobj = elf_hash_table (info)->dynobj;
7250 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7252 sgot = bfd_get_section_by_name (dynobj, ".got");
7257 BFD_ASSERT (elf_section_data (sgot) != NULL);
7258 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7259 BFD_ASSERT (g != NULL);
7262 if (elf_hash_table (info)->dynamic_sections_created)
7264 Elf32_External_Dyn *dyncon, *dynconend;
7266 BFD_ASSERT (sdyn != NULL);
7267 BFD_ASSERT (g != NULL);
7269 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7270 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7271 for (; dyncon < dynconend; dyncon++)
7273 Elf_Internal_Dyn dyn;
7278 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7286 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7287 BFD_ASSERT (s != NULL);
7288 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7289 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7293 /* Rewrite DT_STRSZ. */
7295 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7296 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7302 case DT_MIPS_CONFLICT:
7305 case DT_MIPS_LIBLIST:
7308 s = bfd_get_section_by_name (output_bfd, name);
7309 BFD_ASSERT (s != NULL);
7310 dyn.d_un.d_ptr = s->vma;
7311 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7314 case DT_MIPS_RLD_VERSION:
7315 dyn.d_un.d_val = 1; /* XXX */
7316 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7320 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7321 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7324 case DT_MIPS_CONFLICTNO:
7326 elemsize = sizeof (Elf32_Conflict);
7329 case DT_MIPS_LIBLISTNO:
7331 elemsize = sizeof (Elf32_Lib);
7333 s = bfd_get_section_by_name (output_bfd, name);
7336 if (s->_cooked_size != 0)
7337 dyn.d_un.d_val = s->_cooked_size / elemsize;
7339 dyn.d_un.d_val = s->_raw_size / elemsize;
7344 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7347 case DT_MIPS_TIME_STAMP:
7348 time ((time_t *) &dyn.d_un.d_val);
7349 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7352 case DT_MIPS_ICHECKSUM:
7356 case DT_MIPS_IVERSION:
7360 case DT_MIPS_BASE_ADDRESS:
7361 s = output_bfd->sections;
7362 BFD_ASSERT (s != NULL);
7363 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7364 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7367 case DT_MIPS_LOCAL_GOTNO:
7368 dyn.d_un.d_val = g->local_gotno;
7369 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7372 case DT_MIPS_SYMTABNO:
7374 elemsize = sizeof (Elf32_External_Sym);
7375 s = bfd_get_section_by_name (output_bfd, name);
7376 BFD_ASSERT (s != NULL);
7378 if (s->_cooked_size != 0)
7379 dyn.d_un.d_val = s->_cooked_size / elemsize;
7381 dyn.d_un.d_val = s->_raw_size / elemsize;
7382 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7385 case DT_MIPS_UNREFEXTNO:
7387 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7389 dyn.d_un.d_val = bfd_count_sections (output_bfd);
7391 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7394 case DT_MIPS_GOTSYM:
7395 dyn.d_un.d_val = g->global_gotsym;
7396 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7399 case DT_MIPS_HIPAGENO:
7400 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7401 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7404 case DT_MIPS_RLD_MAP:
7405 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7406 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7413 /* The first entry of the global offset table will be filled at
7414 runtime. The second entry will be used by some runtime loaders.
7415 This isn't the case of Irix rld. */
7416 if (sgot != NULL && sgot->_raw_size > 0)
7418 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7419 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7423 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7428 Elf_Internal_Sym sym;
7429 Elf32_compact_rel cpt;
7431 /* Set up the section symbols for the output sections. SGI sets
7432 the STT_NOTYPE attribute for these symbols. Should we do so? */
7434 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7435 if (sdynsym != NULL)
7439 const char * const * namep = mips_elf_dynsym_sec_names;
7444 /* We no longer try to restrict the set of sections which get
7445 dynamic symbol table entries, since it fails if we have
7446 other random sections which need dynamic relocations. */
7447 if (SGI_COMPAT (output_bfd))
7451 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7457 while ((name = *namep++) != NULL)
7459 s = bfd_get_section_by_name (output_bfd, name);
7462 sym.st_value = s->vma;
7463 dindx = elf_section_data (s)->dynindx;
7464 last = s->vma + s->_raw_size;
7468 sym.st_value = last;
7472 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7477 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7479 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7480 (((Elf32_External_Sym *)
7485 /* Set the sh_info field of the output .dynsym section to
7486 the index of the first global symbol. */
7487 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7488 SIZEOF_MIPS_DYNSYM_SECNAMES;
7495 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7498 for (s = output_bfd->sections; s != NULL; s = s->next)
7502 sym.st_value = s->vma;
7504 indx = elf_section_data (s)->this_idx;
7505 BFD_ASSERT (indx > 0);
7506 sym.st_shndx = indx;
7508 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7509 (((Elf32_External_Sym *)
7511 + elf_section_data (s)->dynindx));
7514 /* Set the sh_info field of the output .dynsym section to
7515 the index of the first global symbol. */
7516 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7517 bfd_count_sections (output_bfd) + 1;
7521 if (SGI_COMPAT (output_bfd))
7523 /* Write .compact_rel section out. */
7524 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7528 cpt.num = s->reloc_count;
7530 cpt.offset = (s->output_section->filepos
7531 + sizeof (Elf32_External_compact_rel));
7534 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7535 ((Elf32_External_compact_rel *)
7538 /* Clean up a dummy stub function entry in .text. */
7539 s = bfd_get_section_by_name (dynobj, ".stub");
7542 file_ptr dummy_offset;
7544 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7545 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7546 memset (s->contents + dummy_offset, 0,
7547 MIPS_FUNCTION_STUB_SIZE);
7552 /* Clean up a first relocation in .rel.dyn. */
7553 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7554 if (s != NULL && s->_raw_size > 0)
7555 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7561 /* This is almost identical to bfd_generic_get_... except that some
7562 MIPS relocations need to be handled specially. Sigh. */
7565 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7566 relocateable, symbols)
7568 struct bfd_link_info *link_info;
7569 struct bfd_link_order *link_order;
7571 boolean relocateable;
7574 /* Get enough memory to hold the stuff */
7575 bfd *input_bfd = link_order->u.indirect.section->owner;
7576 asection *input_section = link_order->u.indirect.section;
7578 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7579 arelent **reloc_vector = NULL;
7585 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7586 if (reloc_vector == NULL && reloc_size != 0)
7589 /* read in the section */
7590 if (!bfd_get_section_contents (input_bfd,
7594 input_section->_raw_size))
7597 /* We're not relaxing the section, so just copy the size info */
7598 input_section->_cooked_size = input_section->_raw_size;
7599 input_section->reloc_done = true;
7601 reloc_count = bfd_canonicalize_reloc (input_bfd,
7605 if (reloc_count < 0)
7608 if (reloc_count > 0)
7613 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7616 struct bfd_hash_entry *h;
7617 struct bfd_link_hash_entry *lh;
7618 /* Skip all this stuff if we aren't mixing formats. */
7619 if (abfd && input_bfd
7620 && abfd->xvec == input_bfd->xvec)
7624 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7625 lh = (struct bfd_link_hash_entry *) h;
7632 case bfd_link_hash_undefined:
7633 case bfd_link_hash_undefweak:
7634 case bfd_link_hash_common:
7637 case bfd_link_hash_defined:
7638 case bfd_link_hash_defweak:
7640 gp = lh->u.def.value;
7642 case bfd_link_hash_indirect:
7643 case bfd_link_hash_warning:
7645 /* @@FIXME ignoring warning for now */
7647 case bfd_link_hash_new:
7656 for (parent = reloc_vector; *parent != (arelent *) NULL;
7659 char *error_message = (char *) NULL;
7660 bfd_reloc_status_type r;
7662 /* Specific to MIPS: Deal with relocation types that require
7663 knowing the gp of the output bfd. */
7664 asymbol *sym = *(*parent)->sym_ptr_ptr;
7665 if (bfd_is_abs_section (sym->section) && abfd)
7667 /* The special_function wouldn't get called anyways. */
7671 /* The gp isn't there; let the special function code
7672 fall over on its own. */
7674 else if ((*parent)->howto->special_function
7675 == _bfd_mips_elf_gprel16_reloc)
7677 /* bypass special_function call */
7678 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7679 relocateable, (PTR) data, gp);
7680 goto skip_bfd_perform_relocation;
7682 /* end mips specific stuff */
7684 r = bfd_perform_relocation (input_bfd,
7688 relocateable ? abfd : (bfd *) NULL,
7690 skip_bfd_perform_relocation:
7694 asection *os = input_section->output_section;
7696 /* A partial link, so keep the relocs */
7697 os->orelocation[os->reloc_count] = *parent;
7701 if (r != bfd_reloc_ok)
7705 case bfd_reloc_undefined:
7706 if (!((*link_info->callbacks->undefined_symbol)
7707 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7708 input_bfd, input_section, (*parent)->address)))
7711 case bfd_reloc_dangerous:
7712 BFD_ASSERT (error_message != (char *) NULL);
7713 if (!((*link_info->callbacks->reloc_dangerous)
7714 (link_info, error_message, input_bfd, input_section,
7715 (*parent)->address)))
7718 case bfd_reloc_overflow:
7719 if (!((*link_info->callbacks->reloc_overflow)
7720 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7721 (*parent)->howto->name, (*parent)->addend,
7722 input_bfd, input_section, (*parent)->address)))
7725 case bfd_reloc_outofrange:
7734 if (reloc_vector != NULL)
7735 free (reloc_vector);
7739 if (reloc_vector != NULL)
7740 free (reloc_vector);
7743 #define bfd_elf32_bfd_get_relocated_section_contents \
7744 elf32_mips_get_relocated_section_contents
7746 /* ECOFF swapping routines. These are used when dealing with the
7747 .mdebug section, which is in the ECOFF debugging format. */
7748 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7750 /* Symbol table magic number. */
7752 /* Alignment of debugging information. E.g., 4. */
7754 /* Sizes of external symbolic information. */
7755 sizeof (struct hdr_ext),
7756 sizeof (struct dnr_ext),
7757 sizeof (struct pdr_ext),
7758 sizeof (struct sym_ext),
7759 sizeof (struct opt_ext),
7760 sizeof (struct fdr_ext),
7761 sizeof (struct rfd_ext),
7762 sizeof (struct ext_ext),
7763 /* Functions to swap in external symbolic data. */
7772 _bfd_ecoff_swap_tir_in,
7773 _bfd_ecoff_swap_rndx_in,
7774 /* Functions to swap out external symbolic data. */
7783 _bfd_ecoff_swap_tir_out,
7784 _bfd_ecoff_swap_rndx_out,
7785 /* Function to read in symbolic data. */
7786 _bfd_mips_elf_read_ecoff_info
7789 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7790 #define TARGET_LITTLE_NAME "elf32-littlemips"
7791 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7792 #define TARGET_BIG_NAME "elf32-bigmips"
7793 #define ELF_ARCH bfd_arch_mips
7794 #define ELF_MACHINE_CODE EM_MIPS
7796 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7797 a value of 0x1000, and we are compatible. */
7798 #define ELF_MAXPAGESIZE 0x1000
7800 #define elf_backend_collect true
7801 #define elf_backend_type_change_ok true
7802 #define elf_backend_can_gc_sections true
7803 #define elf_info_to_howto mips_info_to_howto_rela
7804 #define elf_info_to_howto_rel mips_info_to_howto_rel
7805 #define elf_backend_sym_is_global mips_elf_sym_is_global
7806 #define elf_backend_object_p mips_elf32_object_p
7807 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7808 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7809 #define elf_backend_section_from_bfd_section \
7810 _bfd_mips_elf_section_from_bfd_section
7811 #define elf_backend_section_processing mips_elf32_section_processing
7812 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7813 #define elf_backend_additional_program_headers \
7814 mips_elf_additional_program_headers
7815 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7816 #define elf_backend_final_write_processing \
7817 _bfd_mips_elf_final_write_processing
7818 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7820 #define bfd_elf32_bfd_is_local_label_name \
7821 mips_elf_is_local_label_name
7822 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7823 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7824 #define bfd_elf32_bfd_link_hash_table_create \
7825 mips_elf_link_hash_table_create
7826 #define bfd_elf32_bfd_final_link mips_elf_final_link
7827 #define bfd_elf32_bfd_copy_private_bfd_data \
7828 _bfd_mips_elf_copy_private_bfd_data
7829 #define bfd_elf32_bfd_merge_private_bfd_data \
7830 _bfd_mips_elf_merge_private_bfd_data
7831 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7832 #define bfd_elf32_bfd_print_private_bfd_data \
7833 _bfd_mips_elf_print_private_bfd_data
7834 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7835 #define elf_backend_create_dynamic_sections \
7836 mips_elf_create_dynamic_sections
7837 #define elf_backend_check_relocs mips_elf_check_relocs
7838 #define elf_backend_adjust_dynamic_symbol \
7839 mips_elf_adjust_dynamic_symbol
7840 #define elf_backend_always_size_sections \
7841 mips_elf_always_size_sections
7842 #define elf_backend_size_dynamic_sections \
7843 mips_elf_size_dynamic_sections
7844 #define elf_backend_relocate_section mips_elf_relocate_section
7845 #define elf_backend_link_output_symbol_hook \
7846 mips_elf_link_output_symbol_hook
7847 #define elf_backend_finish_dynamic_symbol \
7848 mips_elf_finish_dynamic_symbol
7849 #define elf_backend_finish_dynamic_sections \
7850 mips_elf_finish_dynamic_sections
7851 #define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
7852 #define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
7854 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
7855 #define elf_backend_plt_header_size 0
7857 #include "elf32-target.h"