1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997 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 bfd_mips_elf32_swap_gptab_in
51 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
52 static void bfd_mips_elf32_swap_gptab_out
53 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
54 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
55 static boolean mips_elf32_object_p PARAMS ((bfd *));
56 static boolean mips_elf_create_procedure_table
57 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
58 struct ecoff_debug_info *));
59 static int mips_elf_additional_program_headers PARAMS ((bfd *));
60 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
61 static INLINE int elf_mips_isa PARAMS ((flagword));
62 static boolean mips_elf32_section_from_shdr
63 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
64 static boolean mips_elf32_section_processing
65 PARAMS ((bfd *, Elf32_Internal_Shdr *));
66 static boolean mips_elf_is_local_label_name
67 PARAMS ((bfd *, const char *));
68 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
69 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
70 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
72 static int gptab_compare PARAMS ((const void *, const void *));
73 static boolean mips_elf_final_link
74 PARAMS ((bfd *, struct bfd_link_info *));
75 static void mips_elf_relocate_hi16
76 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
78 static boolean mips_elf_relocate_got_local
79 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
80 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
81 static void mips_elf_relocate_global_got
82 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
83 static bfd_reloc_status_type mips16_jump_reloc
84 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
85 static bfd_reloc_status_type mips16_gprel_reloc
86 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
87 static boolean mips_elf_adjust_dynindx
88 PARAMS ((struct elf_link_hash_entry *, PTR));
89 static boolean mips_elf_relocate_section
90 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
91 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
92 static boolean mips_elf_link_output_symbol_hook
93 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
95 static boolean mips_elf_create_dynamic_sections
96 PARAMS ((bfd *, struct bfd_link_info *));
97 static boolean mips_elf_create_compact_rel_section
98 PARAMS ((bfd *, struct bfd_link_info *));
99 static boolean mips_elf_create_got_section
100 PARAMS ((bfd *, struct bfd_link_info *));
101 static boolean mips_elf_check_relocs
102 PARAMS ((bfd *, struct bfd_link_info *, asection *,
103 const Elf_Internal_Rela *));
104 static boolean mips_elf_adjust_dynamic_symbol
105 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
106 static boolean mips_elf_always_size_sections
107 PARAMS ((bfd *, struct bfd_link_info *));
108 static boolean mips_elf_size_dynamic_sections
109 PARAMS ((bfd *, struct bfd_link_info *));
110 static boolean mips_elf_finish_dynamic_symbol
111 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
112 Elf_Internal_Sym *));
113 static boolean mips_elf_finish_dynamic_sections
114 PARAMS ((bfd *, struct bfd_link_info *));
115 static boolean mips_elf_add_symbol_hook
116 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
117 const char **, flagword *, asection **, bfd_vma *));
118 static bfd_reloc_status_type mips_elf_final_gp
119 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
120 static bfd_byte *elf32_mips_get_relocated_section_contents
121 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
122 bfd_byte *, boolean, asymbol **));
124 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
125 executables. FIXME: At the moment, we default to always generating
126 Irix 5 executables. */
128 #define SGI_COMPAT(abfd) (1)
130 /* This structure is used to hold .got information when linking. It
131 is stored in the tdata field of the bfd_elf_section_data structure. */
135 /* The symbol index of the first global .got symbol. */
136 unsigned long global_gotsym;
137 /* The number of local .got entries. */
138 unsigned int local_gotno;
139 /* The number of local .got entries we have used. */
140 unsigned int assigned_gotno;
143 /* The number of local .got entries we reserve. */
144 #define MIPS_RESERVED_GOTNO (2)
146 /* Instructions which appear in a stub. For some reason the stub is
147 slightly different on an SGI system. */
148 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
149 #define STUB_LW(abfd) \
151 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
152 : 0x8f998000) /* lw t9,0x8000(gp) */
153 #define STUB_MOVE 0x03e07825 /* move t7,ra */
154 #define STUB_JALR 0x0320f809 /* jal t9 */
155 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
156 #define MIPS_FUNCTION_STUB_SIZE (16)
158 /* Names of sections which appear in the .dynsym section in an Irix 5
161 static const char * const mips_elf_dynsym_sec_names[] =
174 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
175 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
177 /* The number of entries in mips_elf_dynsym_sec_names which go in the
180 #define MIPS_TEXT_DYNSYM_SECNO (3)
182 /* The names of the runtime procedure table symbols used on Irix 5. */
184 static const char * const mips_elf_dynsym_rtproc_names[] =
187 "_procedure_string_table",
188 "_procedure_table_size",
192 /* These structures are used to generate the .compact_rel section on
197 unsigned long id1; /* Always one? */
198 unsigned long num; /* Number of compact relocation entries. */
199 unsigned long id2; /* Always two? */
200 unsigned long offset; /* The file offset of the first relocation. */
201 unsigned long reserved0; /* Zero? */
202 unsigned long reserved1; /* Zero? */
211 bfd_byte reserved0[4];
212 bfd_byte reserved1[4];
213 } Elf32_External_compact_rel;
217 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
218 unsigned int rtype : 4; /* Relocation types. See below. */
219 unsigned int dist2to : 8;
220 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
221 unsigned long konst; /* KONST field. See below. */
222 unsigned long vaddr; /* VADDR to be relocated. */
227 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
228 unsigned int rtype : 4; /* Relocation types. See below. */
229 unsigned int dist2to : 8;
230 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
231 unsigned long konst; /* KONST field. See below. */
239 } Elf32_External_crinfo;
245 } Elf32_External_crinfo2;
247 /* These are the constants used to swap the bitfields in a crinfo. */
249 #define CRINFO_CTYPE (0x1)
250 #define CRINFO_CTYPE_SH (31)
251 #define CRINFO_RTYPE (0xf)
252 #define CRINFO_RTYPE_SH (27)
253 #define CRINFO_DIST2TO (0xff)
254 #define CRINFO_DIST2TO_SH (19)
255 #define CRINFO_RELVADDR (0x7ffff)
256 #define CRINFO_RELVADDR_SH (0)
258 /* A compact relocation info has long (3 words) or short (2 words)
259 formats. A short format doesn't have VADDR field and relvaddr
260 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
261 #define CRF_MIPS_LONG 1
262 #define CRF_MIPS_SHORT 0
264 /* There are 4 types of compact relocation at least. The value KONST
265 has different meaning for each type:
268 CT_MIPS_REL32 Address in data
269 CT_MIPS_WORD Address in word (XXX)
270 CT_MIPS_GPHI_LO GP - vaddr
271 CT_MIPS_JMPAD Address to jump
274 #define CRT_MIPS_REL32 0xa
275 #define CRT_MIPS_WORD 0xb
276 #define CRT_MIPS_GPHI_LO 0xc
277 #define CRT_MIPS_JMPAD 0xd
279 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
280 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
281 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
282 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
284 static void bfd_elf32_swap_compact_rel_out
285 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
286 static void bfd_elf32_swap_crinfo_out
287 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
289 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
294 R_MIPS_16, R_MIPS_32,
295 R_MIPS_REL32, R_MIPS_26,
296 R_MIPS_HI16, R_MIPS_LO16,
297 R_MIPS_GPREL16, R_MIPS_LITERAL,
298 R_MIPS_GOT16, R_MIPS_PC16,
299 R_MIPS_CALL16, R_MIPS_GPREL32,
300 /* The remaining relocs are defined on Irix, although they are not
301 in the MIPS ELF ABI. */
302 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
304 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
305 R_MIPS_64, R_MIPS_GOT_DISP,
306 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
307 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
308 R_MIPS_SUB, R_MIPS_INSERT_A,
309 R_MIPS_INSERT_B, R_MIPS_DELETE,
310 R_MIPS_HIGHER, R_MIPS_HIGHEST,
311 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
313 /* These relocs are used for the mips16. */
318 static reloc_howto_type elf_mips_howto_table[] =
321 HOWTO (R_MIPS_NONE, /* type */
323 0, /* size (0 = byte, 1 = short, 2 = long) */
325 false, /* pc_relative */
327 complain_overflow_dont, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_MIPS_NONE", /* name */
330 false, /* partial_inplace */
333 false), /* pcrel_offset */
335 /* 16 bit relocation. */
336 HOWTO (R_MIPS_16, /* type */
338 1, /* size (0 = byte, 1 = short, 2 = long) */
340 false, /* pc_relative */
342 complain_overflow_bitfield, /* complain_on_overflow */
343 bfd_elf_generic_reloc, /* special_function */
344 "R_MIPS_16", /* name */
345 true, /* partial_inplace */
346 0xffff, /* src_mask */
347 0xffff, /* dst_mask */
348 false), /* pcrel_offset */
350 /* 32 bit relocation. */
351 HOWTO (R_MIPS_32, /* type */
353 2, /* size (0 = byte, 1 = short, 2 = long) */
355 false, /* pc_relative */
357 complain_overflow_bitfield, /* complain_on_overflow */
358 bfd_elf_generic_reloc, /* special_function */
359 "R_MIPS_32", /* name */
360 true, /* partial_inplace */
361 0xffffffff, /* src_mask */
362 0xffffffff, /* dst_mask */
363 false), /* pcrel_offset */
365 /* 32 bit symbol relative relocation. */
366 HOWTO (R_MIPS_REL32, /* type */
368 2, /* size (0 = byte, 1 = short, 2 = long) */
370 false, /* pc_relative */
372 complain_overflow_bitfield, /* complain_on_overflow */
373 bfd_elf_generic_reloc, /* special_function */
374 "R_MIPS_REL32", /* name */
375 true, /* partial_inplace */
376 0xffffffff, /* src_mask */
377 0xffffffff, /* dst_mask */
378 false), /* pcrel_offset */
380 /* 26 bit branch address. */
381 HOWTO (R_MIPS_26, /* type */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
385 false, /* pc_relative */
387 complain_overflow_dont, /* complain_on_overflow */
388 /* This needs complex overflow
389 detection, because the upper four
390 bits must match the PC. */
391 bfd_elf_generic_reloc, /* special_function */
392 "R_MIPS_26", /* name */
393 true, /* partial_inplace */
394 0x3ffffff, /* src_mask */
395 0x3ffffff, /* dst_mask */
396 false), /* pcrel_offset */
398 /* High 16 bits of symbol value. */
399 HOWTO (R_MIPS_HI16, /* type */
401 2, /* size (0 = byte, 1 = short, 2 = long) */
403 false, /* pc_relative */
405 complain_overflow_dont, /* complain_on_overflow */
406 _bfd_mips_elf_hi16_reloc, /* special_function */
407 "R_MIPS_HI16", /* name */
408 true, /* partial_inplace */
409 0xffff, /* src_mask */
410 0xffff, /* dst_mask */
411 false), /* pcrel_offset */
413 /* Low 16 bits of symbol value. */
414 HOWTO (R_MIPS_LO16, /* type */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
418 false, /* pc_relative */
420 complain_overflow_dont, /* complain_on_overflow */
421 _bfd_mips_elf_lo16_reloc, /* special_function */
422 "R_MIPS_LO16", /* name */
423 true, /* partial_inplace */
424 0xffff, /* src_mask */
425 0xffff, /* dst_mask */
426 false), /* pcrel_offset */
428 /* GP relative reference. */
429 HOWTO (R_MIPS_GPREL16, /* type */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
433 false, /* pc_relative */
435 complain_overflow_signed, /* complain_on_overflow */
436 _bfd_mips_elf_gprel16_reloc, /* special_function */
437 "R_MIPS_GPREL16", /* name */
438 true, /* partial_inplace */
439 0xffff, /* src_mask */
440 0xffff, /* dst_mask */
441 false), /* pcrel_offset */
443 /* Reference to literal section. */
444 HOWTO (R_MIPS_LITERAL, /* type */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
448 false, /* pc_relative */
450 complain_overflow_signed, /* complain_on_overflow */
451 _bfd_mips_elf_gprel16_reloc, /* special_function */
452 "R_MIPS_LITERAL", /* name */
453 true, /* partial_inplace */
454 0xffff, /* src_mask */
455 0xffff, /* dst_mask */
456 false), /* pcrel_offset */
458 /* Reference to global offset table. */
459 HOWTO (R_MIPS_GOT16, /* type */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
463 false, /* pc_relative */
465 complain_overflow_signed, /* complain_on_overflow */
466 _bfd_mips_elf_got16_reloc, /* special_function */
467 "R_MIPS_GOT16", /* name */
468 false, /* partial_inplace */
470 0xffff, /* dst_mask */
471 false), /* pcrel_offset */
473 /* 16 bit PC relative reference. */
474 HOWTO (R_MIPS_PC16, /* type */
476 2, /* size (0 = byte, 1 = short, 2 = long) */
478 true, /* pc_relative */
480 complain_overflow_signed, /* complain_on_overflow */
481 bfd_elf_generic_reloc, /* special_function */
482 "R_MIPS_PC16", /* name */
483 true, /* partial_inplace */
484 0xffff, /* src_mask */
485 0xffff, /* dst_mask */
486 false), /* pcrel_offset */
488 /* 16 bit call through global offset table. */
489 /* FIXME: This is not handled correctly. */
490 HOWTO (R_MIPS_CALL16, /* type */
492 2, /* size (0 = byte, 1 = short, 2 = long) */
494 false, /* pc_relative */
496 complain_overflow_signed, /* complain_on_overflow */
497 bfd_elf_generic_reloc, /* special_function */
498 "R_MIPS_CALL16", /* name */
499 false, /* partial_inplace */
501 0xffff, /* dst_mask */
502 false), /* pcrel_offset */
504 /* 32 bit GP relative reference. */
505 HOWTO (R_MIPS_GPREL32, /* type */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
509 false, /* pc_relative */
511 complain_overflow_bitfield, /* complain_on_overflow */
512 _bfd_mips_elf_gprel32_reloc, /* special_function */
513 "R_MIPS_GPREL32", /* name */
514 true, /* partial_inplace */
515 0xffffffff, /* src_mask */
516 0xffffffff, /* dst_mask */
517 false), /* pcrel_offset */
519 /* The remaining relocs are defined on Irix 5, although they are
520 not defined by the ABI. */
525 /* A 5 bit shift field. */
526 HOWTO (R_MIPS_SHIFT5, /* type */
528 2, /* size (0 = byte, 1 = short, 2 = long) */
530 false, /* pc_relative */
532 complain_overflow_bitfield, /* complain_on_overflow */
533 bfd_elf_generic_reloc, /* special_function */
534 "R_MIPS_SHIFT5", /* name */
535 true, /* partial_inplace */
536 0x000007c0, /* src_mask */
537 0x000007c0, /* dst_mask */
538 false), /* pcrel_offset */
540 /* A 6 bit shift field. */
541 /* FIXME: This is not handled correctly; a special function is
542 needed to put the most significant bit in the right place. */
543 HOWTO (R_MIPS_SHIFT6, /* type */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
547 false, /* pc_relative */
549 complain_overflow_bitfield, /* complain_on_overflow */
550 bfd_elf_generic_reloc, /* special_function */
551 "R_MIPS_SHIFT6", /* name */
552 true, /* partial_inplace */
553 0x000007c4, /* src_mask */
554 0x000007c4, /* dst_mask */
555 false), /* pcrel_offset */
557 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
558 are 64 bits long; the upper 32 bits are simply a sign extension.
559 The fields of the howto should be the same as for R_MIPS_32,
560 other than the type, name, and special_function. */
561 HOWTO (R_MIPS_64, /* type */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
565 false, /* pc_relative */
567 complain_overflow_bitfield, /* complain_on_overflow */
568 mips32_64bit_reloc, /* special_function */
569 "R_MIPS_64", /* name */
570 true, /* partial_inplace */
571 0xffffffff, /* src_mask */
572 0xffffffff, /* dst_mask */
573 false), /* pcrel_offset */
575 /* Displacement in the global offset table. */
576 /* FIXME: Not handled correctly. */
577 HOWTO (R_MIPS_GOT_DISP, /* type */
579 2, /* size (0 = byte, 1 = short, 2 = long) */
581 false, /* pc_relative */
583 complain_overflow_bitfield, /* complain_on_overflow */
584 bfd_elf_generic_reloc, /* special_function */
585 "R_MIPS_GOT_DISP", /* name */
586 true, /* partial_inplace */
587 0x0000ffff, /* src_mask */
588 0x0000ffff, /* dst_mask */
589 false), /* pcrel_offset */
591 /* Displacement to page pointer in the global offset table. */
592 /* FIXME: Not handled correctly. */
593 HOWTO (R_MIPS_GOT_PAGE, /* type */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
597 false, /* pc_relative */
599 complain_overflow_bitfield, /* complain_on_overflow */
600 bfd_elf_generic_reloc, /* special_function */
601 "R_MIPS_GOT_PAGE", /* name */
602 true, /* partial_inplace */
603 0x0000ffff, /* src_mask */
604 0x0000ffff, /* dst_mask */
605 false), /* pcrel_offset */
607 /* Offset from page pointer in the global offset table. */
608 /* FIXME: Not handled correctly. */
609 HOWTO (R_MIPS_GOT_OFST, /* type */
611 2, /* size (0 = byte, 1 = short, 2 = long) */
613 false, /* pc_relative */
615 complain_overflow_bitfield, /* complain_on_overflow */
616 bfd_elf_generic_reloc, /* special_function */
617 "R_MIPS_GOT_OFST", /* name */
618 true, /* partial_inplace */
619 0x0000ffff, /* src_mask */
620 0x0000ffff, /* dst_mask */
621 false), /* pcrel_offset */
623 /* High 16 bits of displacement in global offset table. */
624 /* FIXME: Not handled correctly. */
625 HOWTO (R_MIPS_GOT_HI16, /* type */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
629 false, /* pc_relative */
631 complain_overflow_dont, /* complain_on_overflow */
632 bfd_elf_generic_reloc, /* special_function */
633 "R_MIPS_GOT_HI16", /* name */
634 true, /* partial_inplace */
635 0x0000ffff, /* src_mask */
636 0x0000ffff, /* dst_mask */
637 false), /* pcrel_offset */
639 /* Low 16 bits of displacement in global offset table. */
640 /* FIXME: Not handled correctly. */
641 HOWTO (R_MIPS_GOT_LO16, /* type */
643 2, /* size (0 = byte, 1 = short, 2 = long) */
645 false, /* pc_relative */
647 complain_overflow_dont, /* complain_on_overflow */
648 bfd_elf_generic_reloc, /* special_function */
649 "R_MIPS_GOT_LO16", /* name */
650 true, /* partial_inplace */
651 0x0000ffff, /* src_mask */
652 0x0000ffff, /* dst_mask */
653 false), /* pcrel_offset */
655 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
658 /* Used to cause the linker to insert and delete instructions? */
663 /* Get the higher values of a 64 bit addend. Presumably not used in
668 /* High 16 bits of displacement in global offset table. */
669 /* FIXME: Not handled correctly. */
670 HOWTO (R_MIPS_CALL_HI16, /* type */
672 2, /* size (0 = byte, 1 = short, 2 = long) */
674 false, /* pc_relative */
676 complain_overflow_dont, /* complain_on_overflow */
677 bfd_elf_generic_reloc, /* special_function */
678 "R_MIPS_CALL_HI16", /* name */
679 true, /* partial_inplace */
680 0x0000ffff, /* src_mask */
681 0x0000ffff, /* dst_mask */
682 false), /* pcrel_offset */
684 /* Low 16 bits of displacement in global offset table. */
685 /* FIXME: Not handled correctly. */
686 HOWTO (R_MIPS_CALL_LO16, /* type */
688 2, /* size (0 = byte, 1 = short, 2 = long) */
690 false, /* pc_relative */
692 complain_overflow_dont, /* complain_on_overflow */
693 bfd_elf_generic_reloc, /* special_function */
694 "R_MIPS_CALL_LO16", /* name */
695 true, /* partial_inplace */
696 0x0000ffff, /* src_mask */
697 0x0000ffff, /* dst_mask */
698 false) /* pcrel_offset */
701 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
702 is a hack to make the linker think that we need 64 bit values. */
703 static reloc_howto_type elf_mips_ctor64_howto =
704 HOWTO (R_MIPS_64, /* type */
706 4, /* size (0 = byte, 1 = short, 2 = long) */
708 false, /* pc_relative */
710 complain_overflow_signed, /* complain_on_overflow */
711 mips32_64bit_reloc, /* special_function */
712 "R_MIPS_64", /* name */
713 true, /* partial_inplace */
714 0xffffffff, /* src_mask */
715 0xffffffff, /* dst_mask */
716 false); /* pcrel_offset */
718 /* The reloc used for the mips16 jump instruction. */
719 static reloc_howto_type elf_mips16_jump_howto =
720 HOWTO (R_MIPS16_26, /* type */
722 2, /* size (0 = byte, 1 = short, 2 = long) */
724 false, /* pc_relative */
726 complain_overflow_dont, /* complain_on_overflow */
727 /* This needs complex overflow
728 detection, because the upper four
729 bits must match the PC. */
730 mips16_jump_reloc, /* special_function */
731 "R_MIPS16_26", /* name */
732 true, /* partial_inplace */
733 0x3ffffff, /* src_mask */
734 0x3ffffff, /* dst_mask */
735 false); /* pcrel_offset */
737 /* The reloc used for the mips16 gprel instruction. The src_mask and
738 dsk_mask for this howto do not reflect the actual instruction, in
739 which the value is not contiguous; the masks are for the
740 convenience of the relocate_section routine. */
741 static reloc_howto_type elf_mips16_gprel_howto =
742 HOWTO (R_MIPS16_GPREL, /* type */
744 2, /* size (0 = byte, 1 = short, 2 = long) */
746 false, /* pc_relative */
748 complain_overflow_signed, /* complain_on_overflow */
749 mips16_gprel_reloc, /* special_function */
750 "R_MIPS16_GPREL", /* name */
751 true, /* partial_inplace */
752 0xffff, /* src_mask */
753 0xffff, /* dst_mask */
754 false); /* pcrel_offset */
756 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
757 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
758 the HI16. Here we just save the information we need; we do the
759 actual relocation when we see the LO16. MIPS ELF requires that the
760 LO16 immediately follow the HI16. As a GNU extension, we permit an
761 arbitrary number of HI16 relocs to be associated with a single LO16
762 reloc. This extension permits gcc to output the HI and LO relocs
767 struct mips_hi16 *next;
772 /* FIXME: This should not be a static variable. */
774 static struct mips_hi16 *mips_hi16_list;
776 bfd_reloc_status_type
777 _bfd_mips_elf_hi16_reloc (abfd,
785 arelent *reloc_entry;
788 asection *input_section;
790 char **error_message;
792 bfd_reloc_status_type ret;
796 /* If we're relocating, and this an external symbol, we don't want
797 to change anything. */
798 if (output_bfd != (bfd *) NULL
799 && (symbol->flags & BSF_SECTION_SYM) == 0
800 && reloc_entry->addend == 0)
802 reloc_entry->address += input_section->output_offset;
808 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
810 boolean relocateable;
813 if (ret == bfd_reloc_undefined)
816 if (output_bfd != NULL)
820 relocateable = false;
821 output_bfd = symbol->section->output_section->owner;
824 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
826 if (ret != bfd_reloc_ok)
829 relocation = gp - reloc_entry->address;
833 if (bfd_is_und_section (symbol->section)
834 && output_bfd == (bfd *) NULL)
835 ret = bfd_reloc_undefined;
837 if (bfd_is_com_section (symbol->section))
840 relocation = symbol->value;
843 relocation += symbol->section->output_section->vma;
844 relocation += symbol->section->output_offset;
845 relocation += reloc_entry->addend;
847 if (reloc_entry->address > input_section->_cooked_size)
848 return bfd_reloc_outofrange;
850 /* Save the information, and let LO16 do the actual relocation. */
851 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
853 return bfd_reloc_outofrange;
854 n->addr = (bfd_byte *) data + reloc_entry->address;
855 n->addend = relocation;
856 n->next = mips_hi16_list;
859 if (output_bfd != (bfd *) NULL)
860 reloc_entry->address += input_section->output_offset;
865 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
866 inplace relocation; this function exists in order to do the
867 R_MIPS_HI16 relocation described above. */
869 bfd_reloc_status_type
870 _bfd_mips_elf_lo16_reloc (abfd,
878 arelent *reloc_entry;
881 asection *input_section;
883 char **error_message;
885 arelent gp_disp_relent;
887 if (mips_hi16_list != NULL)
897 struct mips_hi16 *next;
899 /* Do the HI16 relocation. Note that we actually don't need
900 to know anything about the LO16 itself, except where to
901 find the low 16 bits of the addend needed by the LO16. */
902 insn = bfd_get_32 (abfd, l->addr);
903 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
905 val = ((insn & 0xffff) << 16) + vallo;
908 /* The low order 16 bits are always treated as a signed
909 value. Therefore, a negative value in the low order bits
910 requires an adjustment in the high order bits. We need
911 to make this adjustment in two ways: once for the bits we
912 took from the data, and once for the bits we are putting
913 back in to the data. */
914 if ((vallo & 0x8000) != 0)
916 if ((val & 0x8000) != 0)
919 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
920 bfd_put_32 (abfd, insn, l->addr);
922 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
924 gp_disp_relent = *reloc_entry;
925 reloc_entry = &gp_disp_relent;
926 reloc_entry->addend = l->addend;
934 mips_hi16_list = NULL;
936 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
938 bfd_reloc_status_type ret;
939 bfd_vma gp, relocation;
941 /* FIXME: Does this case ever occur? */
943 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
944 if (ret != bfd_reloc_ok)
947 relocation = gp - reloc_entry->address;
948 relocation += symbol->section->output_section->vma;
949 relocation += symbol->section->output_offset;
950 relocation += reloc_entry->addend;
952 if (reloc_entry->address > input_section->_cooked_size)
953 return bfd_reloc_outofrange;
955 gp_disp_relent = *reloc_entry;
956 reloc_entry = &gp_disp_relent;
957 reloc_entry->addend = relocation - 4;
960 /* Now do the LO16 reloc in the usual way. */
961 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
962 input_section, output_bfd, error_message);
965 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
966 table used for PIC code. If the symbol is an external symbol, the
967 instruction is modified to contain the offset of the appropriate
968 entry in the global offset table. If the symbol is a section
969 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
970 addends are combined to form the real addend against the section
971 symbol; the GOT16 is modified to contain the offset of an entry in
972 the global offset table, and the LO16 is modified to offset it
973 appropriately. Thus an offset larger than 16 bits requires a
974 modified value in the global offset table.
976 This implementation suffices for the assembler, but the linker does
977 not yet know how to create global offset tables. */
979 bfd_reloc_status_type
980 _bfd_mips_elf_got16_reloc (abfd,
988 arelent *reloc_entry;
991 asection *input_section;
993 char **error_message;
995 /* If we're relocating, and this an external symbol, we don't want
996 to change anything. */
997 if (output_bfd != (bfd *) NULL
998 && (symbol->flags & BSF_SECTION_SYM) == 0
999 && reloc_entry->addend == 0)
1001 reloc_entry->address += input_section->output_offset;
1002 return bfd_reloc_ok;
1005 /* If we're relocating, and this is a local symbol, we can handle it
1007 if (output_bfd != (bfd *) NULL
1008 && (symbol->flags & BSF_SECTION_SYM) != 0)
1009 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1010 input_section, output_bfd, error_message);
1015 /* We have to figure out the gp value, so that we can adjust the
1016 symbol value correctly. We look up the symbol _gp in the output
1017 BFD. If we can't find it, we're stuck. We cache it in the ELF
1018 target data. We don't need to adjust the symbol value for an
1019 external symbol if we are producing relocateable output. */
1021 static bfd_reloc_status_type
1022 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1025 boolean relocateable;
1026 char **error_message;
1029 if (bfd_is_und_section (symbol->section)
1033 return bfd_reloc_undefined;
1036 *pgp = _bfd_get_gp_value (output_bfd);
1039 || (symbol->flags & BSF_SECTION_SYM) != 0))
1043 /* Make up a value. */
1044 *pgp = symbol->section->output_section->vma + 0x4000;
1045 _bfd_set_gp_value (output_bfd, *pgp);
1053 count = bfd_get_symcount (output_bfd);
1054 sym = bfd_get_outsymbols (output_bfd);
1056 if (sym == (asymbol **) NULL)
1060 for (i = 0; i < count; i++, sym++)
1062 register CONST char *name;
1064 name = bfd_asymbol_name (*sym);
1065 if (*name == '_' && strcmp (name, "_gp") == 0)
1067 *pgp = bfd_asymbol_value (*sym);
1068 _bfd_set_gp_value (output_bfd, *pgp);
1076 /* Only get the error once. */
1078 _bfd_set_gp_value (output_bfd, *pgp);
1080 (char *) "GP relative relocation when _gp not defined";
1081 return bfd_reloc_dangerous;
1086 return bfd_reloc_ok;
1089 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1090 become the offset from the gp register. This function also handles
1091 R_MIPS_LITERAL relocations, although those can be handled more
1092 cleverly because the entries in the .lit8 and .lit4 sections can be
1095 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1096 arelent *, asection *,
1097 boolean, PTR, bfd_vma));
1099 bfd_reloc_status_type
1100 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1101 output_bfd, error_message)
1103 arelent *reloc_entry;
1106 asection *input_section;
1108 char **error_message;
1110 boolean relocateable;
1111 bfd_reloc_status_type ret;
1114 /* If we're relocating, and this is an external symbol with no
1115 addend, we don't want to change anything. We will only have an
1116 addend if this is a newly created reloc, not read from an ELF
1118 if (output_bfd != (bfd *) NULL
1119 && (symbol->flags & BSF_SECTION_SYM) == 0
1120 && reloc_entry->addend == 0)
1122 reloc_entry->address += input_section->output_offset;
1123 return bfd_reloc_ok;
1126 if (output_bfd != (bfd *) NULL)
1127 relocateable = true;
1130 relocateable = false;
1131 output_bfd = symbol->section->output_section->owner;
1134 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1136 if (ret != bfd_reloc_ok)
1139 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1140 relocateable, data, gp);
1143 static bfd_reloc_status_type
1144 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1148 arelent *reloc_entry;
1149 asection *input_section;
1150 boolean relocateable;
1158 if (bfd_is_com_section (symbol->section))
1161 relocation = symbol->value;
1163 relocation += symbol->section->output_section->vma;
1164 relocation += symbol->section->output_offset;
1166 if (reloc_entry->address > input_section->_cooked_size)
1167 return bfd_reloc_outofrange;
1169 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1171 /* Set val to the offset into the section or symbol. */
1172 if (reloc_entry->howto->src_mask == 0)
1174 /* This case occurs with the 64-bit MIPS ELF ABI. */
1175 val = reloc_entry->addend;
1179 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1184 /* Adjust val for the final section location and GP value. If we
1185 are producing relocateable output, we don't want to do this for
1186 an external symbol. */
1188 || (symbol->flags & BSF_SECTION_SYM) != 0)
1189 val += relocation - gp;
1191 insn = (insn &~ 0xffff) | (val & 0xffff);
1192 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1195 reloc_entry->address += input_section->output_offset;
1197 /* Make sure it fit in 16 bits. */
1198 if (val >= 0x8000 && val < 0xffff8000)
1199 return bfd_reloc_overflow;
1201 return bfd_reloc_ok;
1204 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1205 from the gp register? XXX */
1207 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1208 arelent *, asection *,
1209 boolean, PTR, bfd_vma));
1211 bfd_reloc_status_type
1212 _bfd_mips_elf_gprel32_reloc (abfd,
1220 arelent *reloc_entry;
1223 asection *input_section;
1225 char **error_message;
1227 boolean relocateable;
1228 bfd_reloc_status_type ret;
1231 /* If we're relocating, and this is an external symbol with no
1232 addend, we don't want to change anything. We will only have an
1233 addend if this is a newly created reloc, not read from an ELF
1235 if (output_bfd != (bfd *) NULL
1236 && (symbol->flags & BSF_SECTION_SYM) == 0
1237 && reloc_entry->addend == 0)
1239 *error_message = (char *)
1240 "32bits gp relative relocation occurs for an external symbol";
1241 return bfd_reloc_outofrange;
1244 if (output_bfd != (bfd *) NULL)
1246 relocateable = true;
1247 gp = _bfd_get_gp_value (output_bfd);
1251 relocateable = false;
1252 output_bfd = symbol->section->output_section->owner;
1254 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1255 error_message, &gp);
1256 if (ret != bfd_reloc_ok)
1260 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1261 relocateable, data, gp);
1264 static bfd_reloc_status_type
1265 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1269 arelent *reloc_entry;
1270 asection *input_section;
1271 boolean relocateable;
1278 if (bfd_is_com_section (symbol->section))
1281 relocation = symbol->value;
1283 relocation += symbol->section->output_section->vma;
1284 relocation += symbol->section->output_offset;
1286 if (reloc_entry->address > input_section->_cooked_size)
1287 return bfd_reloc_outofrange;
1289 if (reloc_entry->howto->src_mask == 0)
1291 /* This case arises with the 64-bit MIPS ELF ABI. */
1295 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1297 /* Set val to the offset into the section or symbol. */
1298 val += reloc_entry->addend;
1300 /* Adjust val for the final section location and GP value. If we
1301 are producing relocateable output, we don't want to do this for
1302 an external symbol. */
1304 || (symbol->flags & BSF_SECTION_SYM) != 0)
1305 val += relocation - gp;
1307 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1310 reloc_entry->address += input_section->output_offset;
1312 return bfd_reloc_ok;
1315 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1316 generated when addreses are 64 bits. The upper 32 bits are a simle
1319 static bfd_reloc_status_type
1320 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1321 output_bfd, error_message)
1323 arelent *reloc_entry;
1326 asection *input_section;
1328 char **error_message;
1330 bfd_reloc_status_type r;
1335 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1336 input_section, output_bfd, error_message);
1337 if (r != bfd_reloc_continue)
1340 /* Do a normal 32 bit relocation on the lower 32 bits. */
1341 reloc32 = *reloc_entry;
1342 if (bfd_big_endian (abfd))
1343 reloc32.address += 4;
1344 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1345 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1346 output_bfd, error_message);
1348 /* Sign extend into the upper 32 bits. */
1349 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1350 if ((val & 0x80000000) != 0)
1354 addr = reloc_entry->address;
1355 if (bfd_little_endian (abfd))
1357 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1362 /* Handle a mips16 jump. */
1364 static bfd_reloc_status_type
1365 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1366 output_bfd, error_message)
1368 arelent *reloc_entry;
1371 asection *input_section;
1373 char **error_message;
1375 if (output_bfd != (bfd *) NULL
1376 && (symbol->flags & BSF_SECTION_SYM) == 0
1377 && reloc_entry->addend == 0)
1379 reloc_entry->address += input_section->output_offset;
1380 return bfd_reloc_ok;
1385 static boolean warned;
1388 (*_bfd_error_handler)
1389 ("Linking mips16 objects into %s format is not supported",
1390 bfd_get_target (input_section->output_section->owner));
1394 return bfd_reloc_undefined;
1397 /* Handle a mips16 GP relative reloc. */
1399 static bfd_reloc_status_type
1400 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1401 output_bfd, error_message)
1403 arelent *reloc_entry;
1406 asection *input_section;
1408 char **error_message;
1410 boolean relocateable;
1411 bfd_reloc_status_type ret;
1413 unsigned short extend, insn;
1414 unsigned long final;
1416 /* If we're relocating, and this is an external symbol with no
1417 addend, we don't want to change anything. We will only have an
1418 addend if this is a newly created reloc, not read from an ELF
1420 if (output_bfd != NULL
1421 && (symbol->flags & BSF_SECTION_SYM) == 0
1422 && reloc_entry->addend == 0)
1424 reloc_entry->address += input_section->output_offset;
1425 return bfd_reloc_ok;
1428 if (output_bfd != NULL)
1429 relocateable = true;
1432 relocateable = false;
1433 output_bfd = symbol->section->output_section->owner;
1436 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1438 if (ret != bfd_reloc_ok)
1441 if (reloc_entry->address > input_section->_cooked_size)
1442 return bfd_reloc_outofrange;
1444 /* Pick up the mips16 extend instruction and the real instruction. */
1445 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1446 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1448 /* Stuff the current addend back as a 32 bit value, do the usual
1449 relocation, and then clean up. */
1451 (((extend & 0x1f) << 11)
1454 (bfd_byte *) data + reloc_entry->address);
1456 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1457 relocateable, data, gp);
1459 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1462 | ((final >> 11) & 0x1f)
1464 (bfd_byte *) data + reloc_entry->address);
1468 (bfd_byte *) data + reloc_entry->address + 2);
1473 /* Return the ISA for a MIPS e_flags value. */
1476 elf_mips_isa (flags)
1479 switch (flags & EF_MIPS_ARCH)
1493 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1495 struct elf_reloc_map {
1496 bfd_reloc_code_real_type bfd_reloc_val;
1497 enum reloc_type elf_reloc_val;
1500 static CONST struct elf_reloc_map mips_reloc_map[] =
1502 { BFD_RELOC_NONE, R_MIPS_NONE, },
1503 { BFD_RELOC_16, R_MIPS_16 },
1504 { BFD_RELOC_32, R_MIPS_32 },
1505 { BFD_RELOC_64, R_MIPS_64 },
1506 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1507 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1508 { BFD_RELOC_LO16, R_MIPS_LO16 },
1509 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1510 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1511 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1512 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1513 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1514 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1515 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1516 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1517 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1518 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }
1521 /* Given a BFD reloc type, return a howto structure. */
1523 static reloc_howto_type *
1524 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1526 bfd_reloc_code_real_type code;
1530 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1532 if (mips_reloc_map[i].bfd_reloc_val == code)
1533 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1536 /* We need to handle BFD_RELOC_CTOR specially. If this is a mips3
1537 file, then we assume that we are using 64 bit addresses, and use
1538 R_MIPS_64. Otherwise, we use R_MIPS_32. */
1539 if (code == BFD_RELOC_CTOR)
1541 if (elf_mips_isa (elf_elfheader (abfd)->e_flags) < 3)
1542 return &elf_mips_howto_table[(int) R_MIPS_32];
1544 return &elf_mips_ctor64_howto;
1547 /* Special handling for the MIPS16 relocs, since they are made up
1548 reloc types with a large value. */
1549 if (code == BFD_RELOC_MIPS16_JMP)
1550 return &elf_mips16_jump_howto;
1551 else if (code == BFD_RELOC_MIPS16_GPREL)
1552 return &elf_mips16_gprel_howto;
1557 /* Given a MIPS reloc type, fill in an arelent structure. */
1560 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1563 Elf32_Internal_Rel *dst;
1565 unsigned int r_type;
1567 r_type = ELF32_R_TYPE (dst->r_info);
1568 if (r_type == R_MIPS16_26)
1569 cache_ptr->howto = &elf_mips16_jump_howto;
1570 else if (r_type == R_MIPS16_GPREL)
1571 cache_ptr->howto = &elf_mips16_gprel_howto;
1574 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1575 cache_ptr->howto = &elf_mips_howto_table[r_type];
1578 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1579 value for the object file. We get the addend now, rather than
1580 when we do the relocation, because the symbol manipulations done
1581 by the linker may cause us to lose track of the input BFD. */
1582 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1583 && (r_type == (unsigned int) R_MIPS_GPREL16
1584 || r_type == (unsigned int) R_MIPS_LITERAL))
1585 cache_ptr->addend = elf_gp (abfd);
1588 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1589 routines swap this structure in and out. They are used outside of
1590 BFD, so they are globally visible. */
1593 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1595 const Elf32_External_RegInfo *ex;
1598 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1599 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1600 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1601 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1602 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1603 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1607 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1609 const Elf32_RegInfo *in;
1610 Elf32_External_RegInfo *ex;
1612 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1613 (bfd_byte *) ex->ri_gprmask);
1614 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1615 (bfd_byte *) ex->ri_cprmask[0]);
1616 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1617 (bfd_byte *) ex->ri_cprmask[1]);
1618 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1619 (bfd_byte *) ex->ri_cprmask[2]);
1620 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1621 (bfd_byte *) ex->ri_cprmask[3]);
1622 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1623 (bfd_byte *) ex->ri_gp_value);
1626 /* In the 64 bit ABI, the .MIPS.options section holds register
1627 information in an Elf64_Reginfo structure. These routines swap
1628 them in and out. They are globally visible because they are used
1629 outside of BFD. These routines are here so that gas can call them
1630 without worrying about whether the 64 bit ABI has been included. */
1633 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1635 const Elf64_External_RegInfo *ex;
1636 Elf64_Internal_RegInfo *in;
1638 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1639 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1640 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1641 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1642 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1643 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1644 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1648 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1650 const Elf64_Internal_RegInfo *in;
1651 Elf64_External_RegInfo *ex;
1653 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1654 (bfd_byte *) ex->ri_gprmask);
1655 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1656 (bfd_byte *) ex->ri_pad);
1657 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1658 (bfd_byte *) ex->ri_cprmask[0]);
1659 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1660 (bfd_byte *) ex->ri_cprmask[1]);
1661 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1662 (bfd_byte *) ex->ri_cprmask[2]);
1663 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1664 (bfd_byte *) ex->ri_cprmask[3]);
1665 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1666 (bfd_byte *) ex->ri_gp_value);
1669 /* Swap an entry in a .gptab section. Note that these routines rely
1670 on the equivalence of the two elements of the union. */
1673 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1675 const Elf32_External_gptab *ex;
1678 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1679 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1683 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1685 const Elf32_gptab *in;
1686 Elf32_External_gptab *ex;
1688 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1689 ex->gt_entry.gt_g_value);
1690 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1691 ex->gt_entry.gt_bytes);
1695 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1697 const Elf32_compact_rel *in;
1698 Elf32_External_compact_rel *ex;
1700 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1701 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1702 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1703 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1704 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1705 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1709 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1711 const Elf32_crinfo *in;
1712 Elf32_External_crinfo *ex;
1716 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1717 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1718 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1719 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1720 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1721 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1722 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1725 /* Swap in an options header. */
1728 bfd_mips_elf_swap_options_in (abfd, ex, in)
1730 const Elf_External_Options *ex;
1731 Elf_Internal_Options *in;
1733 in->kind = bfd_h_get_8 (abfd, ex->kind);
1734 in->size = bfd_h_get_8 (abfd, ex->size);
1735 in->section = bfd_h_get_16 (abfd, ex->section);
1736 in->info = bfd_h_get_32 (abfd, ex->info);
1739 /* Swap out an options header. */
1742 bfd_mips_elf_swap_options_out (abfd, in, ex)
1744 const Elf_Internal_Options *in;
1745 Elf_External_Options *ex;
1747 bfd_h_put_8 (abfd, in->kind, ex->kind);
1748 bfd_h_put_8 (abfd, in->size, ex->size);
1749 bfd_h_put_16 (abfd, in->section, ex->section);
1750 bfd_h_put_32 (abfd, in->info, ex->info);
1753 /* Determine whether a symbol is global for the purposes of splitting
1754 the symbol table into global symbols and local symbols. At least
1755 on Irix 5, this split must be between section symbols and all other
1756 symbols. On most ELF targets the split is between static symbols
1757 and externally visible symbols. */
1761 mips_elf_sym_is_global (abfd, sym)
1765 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1768 /* Set the right machine number for a MIPS ELF file. This is used for
1769 both the 32-bit and the 64-bit ABI. */
1772 _bfd_mips_elf_object_p (abfd)
1775 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
1779 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 3000);
1783 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
1787 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
1791 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 8000);
1798 /* Set the right machine number for a 32-bit MIPS ELF file. */
1801 mips_elf32_object_p (abfd)
1804 /* Irix 5 is broken. Object file symbol tables are not always
1805 sorted correctly such that local symbols precede global symbols,
1806 and the sh_info field in the symbol table is not always right. */
1807 elf_bad_symtab (abfd) = true;
1809 return _bfd_mips_elf_object_p (abfd);
1812 /* The final processing done just before writing out a MIPS ELF object
1813 file. This gets the MIPS architecture right based on the machine
1814 number. This is used by both the 32-bit and the 64-bit ABI. */
1818 _bfd_mips_elf_final_write_processing (abfd, linker)
1824 Elf_Internal_Shdr **hdrpp;
1828 switch (bfd_get_mach (abfd))
1831 val = E_MIPS_ARCH_1;
1835 val = E_MIPS_ARCH_2;
1839 val = E_MIPS_ARCH_3;
1843 val = E_MIPS_ARCH_4;
1851 elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1852 elf_elfheader (abfd)->e_flags |= val;
1854 /* Set the sh_info field for .gptab sections. */
1855 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1856 i < elf_elfheader (abfd)->e_shnum;
1859 switch ((*hdrpp)->sh_type)
1861 case SHT_MIPS_LIBLIST:
1862 sec = bfd_get_section_by_name (abfd, ".dynstr");
1864 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1867 case SHT_MIPS_GPTAB:
1868 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1869 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1870 BFD_ASSERT (name != NULL
1871 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1872 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1873 BFD_ASSERT (sec != NULL);
1874 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1877 case SHT_MIPS_CONTENT:
1878 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1879 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1880 BFD_ASSERT (name != NULL
1881 && strncmp (name, ".MIPS.content",
1882 sizeof ".MIPS.content" - 1) == 0);
1883 sec = bfd_get_section_by_name (abfd,
1884 name + sizeof ".MIPS.content" - 1);
1885 BFD_ASSERT (sec != NULL);
1886 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1889 case SHT_MIPS_SYMBOL_LIB:
1890 sec = bfd_get_section_by_name (abfd, ".dynsym");
1892 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1893 sec = bfd_get_section_by_name (abfd, ".liblist");
1895 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1898 case SHT_MIPS_EVENTS:
1899 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1900 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1901 BFD_ASSERT (name != NULL);
1902 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
1903 sec = bfd_get_section_by_name (abfd,
1904 name + sizeof ".MIPS.events" - 1);
1907 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
1908 sizeof ".MIPS.post_rel" - 1) == 0);
1909 sec = bfd_get_section_by_name (abfd,
1911 + sizeof ".MIPS.post_rel" - 1));
1913 BFD_ASSERT (sec != NULL);
1914 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1920 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1923 _bfd_mips_elf_set_private_flags (abfd, flags)
1927 BFD_ASSERT (!elf_flags_init (abfd)
1928 || elf_elfheader (abfd)->e_flags == flags);
1930 elf_elfheader (abfd)->e_flags = flags;
1931 elf_flags_init (abfd) = true;
1935 /* Copy backend specific data from one object module to another */
1938 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
1942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1943 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1946 BFD_ASSERT (!elf_flags_init (obfd)
1947 || (elf_elfheader (obfd)->e_flags
1948 == elf_elfheader (ibfd)->e_flags));
1950 elf_gp (obfd) = elf_gp (ibfd);
1951 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1952 elf_flags_init (obfd) = true;
1956 /* Merge backend specific data from an object file to the output
1957 object file when linking. */
1960 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
1968 /* Check if we have the same endianess */
1969 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1970 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1972 (*_bfd_error_handler)
1973 ("%s: compiled for a %s endian system and target is %s endian",
1974 bfd_get_filename (ibfd),
1975 bfd_big_endian (ibfd) ? "big" : "little",
1976 bfd_big_endian (obfd) ? "big" : "little");
1978 bfd_set_error (bfd_error_wrong_format);
1982 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1983 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1986 new_flags = elf_elfheader (ibfd)->e_flags;
1987 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
1988 old_flags = elf_elfheader (obfd)->e_flags;
1990 if (! elf_flags_init (obfd))
1992 elf_flags_init (obfd) = true;
1993 elf_elfheader (obfd)->e_flags = new_flags;
1995 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1996 && bfd_get_arch_info (obfd)->the_default)
1998 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1999 bfd_get_mach (ibfd)))
2006 /* Check flag compatibility. */
2008 new_flags &= ~EF_MIPS_NOREORDER;
2009 old_flags &= ~EF_MIPS_NOREORDER;
2011 if (new_flags == old_flags)
2016 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2018 new_flags &= ~EF_MIPS_PIC;
2019 old_flags &= ~EF_MIPS_PIC;
2020 (*_bfd_error_handler)
2021 ("%s: linking PIC files with non-PIC files",
2022 bfd_get_filename (ibfd));
2026 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2028 new_flags &= ~EF_MIPS_CPIC;
2029 old_flags &= ~EF_MIPS_CPIC;
2030 (*_bfd_error_handler)
2031 ("%s: linking abicalls files with non-abicalls files",
2032 bfd_get_filename (ibfd));
2036 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2037 and -mips4 code. They will normally use the same data sizes and
2038 calling conventions. */
2039 if ((new_flags & EF_MIPS_ARCH) != (old_flags & EF_MIPS_ARCH))
2041 int new_isa, old_isa;
2043 new_isa = elf_mips_isa (new_flags);
2044 old_isa = elf_mips_isa (old_flags);
2045 if ((new_isa == 1 || new_isa == 2)
2046 ? (old_isa != 1 && old_isa != 2)
2047 : (old_isa == 1 || old_isa == 2))
2049 (*_bfd_error_handler)
2050 ("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)",
2051 bfd_get_filename (ibfd), new_isa, old_isa);
2055 new_flags &= ~ EF_MIPS_ARCH;
2056 old_flags &= ~ EF_MIPS_ARCH;
2059 /* Warn about any other mismatches */
2060 if (new_flags != old_flags)
2062 (*_bfd_error_handler)
2063 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
2064 bfd_get_filename (ibfd), (unsigned long) new_flags,
2065 (unsigned long) old_flags);
2071 bfd_set_error (bfd_error_bad_value);
2078 /* Handle a MIPS specific section when reading an object file. This
2079 is called when elfcode.h finds a section with an unknown type.
2080 This routine supports both the 32-bit and 64-bit ELF ABI.
2082 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2086 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2088 Elf_Internal_Shdr *hdr;
2091 /* There ought to be a place to keep ELF backend specific flags, but
2092 at the moment there isn't one. We just keep track of the
2093 sections by their name, instead. Fortunately, the ABI gives
2094 suggested names for all the MIPS specific sections, so we will
2095 probably get away with this. */
2096 switch (hdr->sh_type)
2098 case SHT_MIPS_LIBLIST:
2099 if (strcmp (name, ".liblist") != 0)
2103 if (strcmp (name, ".msym") != 0)
2106 case SHT_MIPS_CONFLICT:
2107 if (strcmp (name, ".conflict") != 0)
2110 case SHT_MIPS_GPTAB:
2111 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2114 case SHT_MIPS_UCODE:
2115 if (strcmp (name, ".ucode") != 0)
2118 case SHT_MIPS_DEBUG:
2119 if (strcmp (name, ".mdebug") != 0)
2122 case SHT_MIPS_REGINFO:
2123 if (strcmp (name, ".reginfo") != 0
2124 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2127 case SHT_MIPS_IFACE:
2128 if (strcmp (name, ".MIPS.interfaces") != 0)
2131 case SHT_MIPS_CONTENT:
2132 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2135 case SHT_MIPS_OPTIONS:
2136 if (strcmp (name, ".options") != 0
2137 && strcmp (name, ".MIPS.options") != 0)
2140 case SHT_MIPS_DWARF:
2141 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2144 case SHT_MIPS_SYMBOL_LIB:
2145 if (strcmp (name, ".MIPS.symlib") != 0)
2148 case SHT_MIPS_EVENTS:
2149 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2150 && strncmp (name, ".MIPS.post_rel",
2151 sizeof ".MIPS.post_rel" - 1) != 0)
2158 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2161 if (hdr->sh_type == SHT_MIPS_DEBUG)
2163 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2164 (bfd_get_section_flags (abfd,
2173 /* Handle a 32-bit MIPS ELF specific section. */
2176 mips_elf32_section_from_shdr (abfd, hdr, name)
2178 Elf_Internal_Shdr *hdr;
2181 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2184 /* FIXME: We should record sh_info for a .gptab section. */
2186 /* For a .reginfo section, set the gp value in the tdata information
2187 from the contents of this section. We need the gp value while
2188 processing relocs, so we just get it now. The .reginfo section
2189 is not used in the 64-bit MIPS ELF ABI. */
2190 if (hdr->sh_type == SHT_MIPS_REGINFO)
2192 Elf32_External_RegInfo ext;
2195 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2196 (file_ptr) 0, sizeof ext))
2198 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2199 elf_gp (abfd) = s.ri_gp_value;
2202 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2203 set the gp value based on what we find. We may see both
2204 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2205 they should agree. */
2206 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2208 bfd_byte *contents, *l, *lend;
2210 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2211 if (contents == NULL)
2213 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2214 (file_ptr) 0, hdr->sh_size))
2220 lend = contents + hdr->sh_size;
2221 while (l + sizeof (Elf_External_Options) <= lend)
2223 Elf_Internal_Options intopt;
2225 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2227 if (intopt.kind == ODK_REGINFO)
2229 Elf32_RegInfo intreg;
2231 bfd_mips_elf32_swap_reginfo_in
2233 ((Elf32_External_RegInfo *)
2234 (l + sizeof (Elf_External_Options))),
2236 elf_gp (abfd) = intreg.ri_gp_value;
2246 /* Set the correct type for a MIPS ELF section. We do this by the
2247 section name, which is a hack, but ought to work. This routine is
2248 used by both the 32-bit and the 64-bit ABI. */
2251 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2253 Elf32_Internal_Shdr *hdr;
2256 register const char *name;
2258 name = bfd_get_section_name (abfd, sec);
2260 if (strcmp (name, ".liblist") == 0)
2262 hdr->sh_type = SHT_MIPS_LIBLIST;
2263 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2264 /* The sh_link field is set in final_write_processing. */
2266 else if (strcmp (name, ".msym") == 0)
2268 hdr->sh_type = SHT_MIPS_MSYM;
2269 hdr->sh_entsize = 8;
2270 /* FIXME: Set the sh_info field. */
2272 else if (strcmp (name, ".conflict") == 0)
2273 hdr->sh_type = SHT_MIPS_CONFLICT;
2274 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2276 hdr->sh_type = SHT_MIPS_GPTAB;
2277 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2278 /* The sh_info field is set in final_write_processing. */
2280 else if (strcmp (name, ".ucode") == 0)
2281 hdr->sh_type = SHT_MIPS_UCODE;
2282 else if (strcmp (name, ".mdebug") == 0)
2284 hdr->sh_type = SHT_MIPS_DEBUG;
2285 /* In a shared object on Irix 5.3, the .mdebug section has an
2286 entsize of 0. FIXME: Does this matter? */
2287 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2288 hdr->sh_entsize = 0;
2290 hdr->sh_entsize = 1;
2292 else if (strcmp (name, ".reginfo") == 0)
2294 hdr->sh_type = SHT_MIPS_REGINFO;
2295 /* In a shared object on Irix 5.3, the .reginfo section has an
2296 entsize of 0x18. FIXME: Does this matter? */
2297 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2298 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2300 hdr->sh_entsize = 1;
2302 /* Force the section size to the correct value, even if the
2303 linker thinks it is larger. The link routine below will only
2304 write out this much data for .reginfo. */
2305 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
2307 else if (SGI_COMPAT (abfd)
2308 && (strcmp (name, ".hash") == 0
2309 || strcmp (name, ".dynamic") == 0
2310 || strcmp (name, ".dynstr") == 0))
2312 hdr->sh_entsize = 0;
2313 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2315 else if (strcmp (name, ".got") == 0
2316 || strcmp (name, ".sdata") == 0
2317 || strcmp (name, ".sbss") == 0
2318 || strcmp (name, ".lit4") == 0
2319 || strcmp (name, ".lit8") == 0)
2320 hdr->sh_flags |= SHF_MIPS_GPREL;
2321 else if (strcmp (name, ".MIPS.interfaces") == 0)
2323 hdr->sh_type = SHT_MIPS_IFACE;
2324 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2326 else if (strcmp (name, ".MIPS.content") == 0)
2328 hdr->sh_type = SHT_MIPS_CONTENT;
2329 /* The sh_info field is set in final_write_processing. */
2331 else if (strcmp (name, ".options") == 0
2332 || strcmp (name, ".MIPS.options") == 0)
2334 hdr->sh_type = SHT_MIPS_OPTIONS;
2335 hdr->sh_entsize = 1;
2336 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2338 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2339 hdr->sh_type = SHT_MIPS_DWARF;
2340 else if (strcmp (name, ".MIPS.symlib") == 0)
2342 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2343 /* The sh_link and sh_info fields are set in
2344 final_write_processing. */
2346 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2347 || strncmp (name, ".MIPS.post_rel",
2348 sizeof ".MIPS.post_rel" - 1) == 0)
2350 hdr->sh_type = SHT_MIPS_EVENTS;
2351 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2352 /* The sh_link field is set in final_write_processing. */
2358 /* Given a BFD section, try to locate the corresponding ELF section
2359 index. This is used by both the 32-bit and the 64-bit ABI.
2360 Actually, it's not clear to me that the 64-bit ABI supports these,
2361 but for non-PIC objects we will certainly want support for at least
2362 the .scommon section. */
2365 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2367 Elf32_Internal_Shdr *hdr;
2371 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2373 *retval = SHN_MIPS_SCOMMON;
2376 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2378 *retval = SHN_MIPS_ACOMMON;
2384 /* When are writing out the .options or .MIPS.options section,
2385 remember the bytes we are writing out, so that we can install the
2386 GP value in the section_processing routine. */
2389 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2394 bfd_size_type count;
2396 if (strcmp (section->name, ".options") == 0
2397 || strcmp (section->name, ".MIPS.options") == 0)
2401 if (elf_section_data (section) == NULL)
2403 section->used_by_bfd =
2404 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2405 if (elf_section_data (section) == NULL)
2408 c = (bfd_byte *) elf_section_data (section)->tdata;
2413 if (section->_cooked_size != 0)
2414 size = section->_cooked_size;
2416 size = section->_raw_size;
2417 c = (PTR) bfd_zalloc (abfd, size);
2420 elf_section_data (section)->tdata = (PTR) c;
2423 memcpy (c + offset, location, count);
2426 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2430 /* Work over a section just before writing it out. This routine is
2431 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2432 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2436 _bfd_mips_elf_section_processing (abfd, hdr)
2438 Elf_Internal_Shdr *hdr;
2440 if (hdr->bfd_section != NULL)
2442 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2444 if (strcmp (name, ".sdata") == 0)
2446 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2447 hdr->sh_type = SHT_PROGBITS;
2449 else if (strcmp (name, ".sbss") == 0)
2451 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2452 hdr->sh_type = SHT_NOBITS;
2454 else if (strcmp (name, ".lit8") == 0
2455 || strcmp (name, ".lit4") == 0)
2457 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2458 hdr->sh_type = SHT_PROGBITS;
2460 else if (strcmp (name, ".compact_rel") == 0)
2463 hdr->sh_type = SHT_PROGBITS;
2465 else if (strcmp (name, ".rtproc") == 0)
2467 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2469 unsigned int adjust;
2471 adjust = hdr->sh_size % hdr->sh_addralign;
2473 hdr->sh_size += hdr->sh_addralign - adjust;
2481 /* Work over a section just before writing it out. We update the GP
2482 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2483 on the value we are using. */
2486 mips_elf32_section_processing (abfd, hdr)
2488 Elf32_Internal_Shdr *hdr;
2490 if (hdr->sh_type == SHT_MIPS_REGINFO)
2494 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2495 BFD_ASSERT (hdr->contents == NULL);
2498 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2501 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2502 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2506 if (hdr->sh_type == SHT_MIPS_OPTIONS
2507 && hdr->bfd_section != NULL
2508 && elf_section_data (hdr->bfd_section) != NULL
2509 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2511 bfd_byte *contents, *l, *lend;
2513 /* We stored the section contents in the elf_section_data tdata
2514 field in the set_section_contents routine. We save the
2515 section contents so that we don't have to read them again.
2516 At this point we know that elf_gp is set, so we can look
2517 through the section contents to see if there is an
2518 ODK_REGINFO structure. */
2520 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2522 lend = contents + hdr->sh_size;
2523 while (l + sizeof (Elf_External_Options) <= lend)
2525 Elf_Internal_Options intopt;
2527 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2529 if (intopt.kind == ODK_REGINFO)
2536 + sizeof (Elf_External_Options)
2537 + (sizeof (Elf32_External_RegInfo) - 4)),
2540 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2541 if (bfd_write (buf, 1, 4, abfd) != 4)
2548 return _bfd_mips_elf_section_processing (abfd, hdr);
2551 /* MIPS ELF uses two common sections. One is the usual one, and the
2552 other is for small objects. All the small objects are kept
2553 together, and then referenced via the gp pointer, which yields
2554 faster assembler code. This is what we use for the small common
2555 section. This approach is copied from ecoff.c. */
2556 static asection mips_elf_scom_section;
2557 static asymbol mips_elf_scom_symbol;
2558 static asymbol *mips_elf_scom_symbol_ptr;
2560 /* MIPS ELF also uses an acommon section, which represents an
2561 allocated common symbol which may be overridden by a
2562 definition in a shared library. */
2563 static asection mips_elf_acom_section;
2564 static asymbol mips_elf_acom_symbol;
2565 static asymbol *mips_elf_acom_symbol_ptr;
2567 /* The Irix 5 support uses two virtual sections, which represent
2568 text/data symbols defined in dynamic objects. */
2569 static asection mips_elf_text_section;
2570 static asection *mips_elf_text_section_ptr;
2571 static asymbol mips_elf_text_symbol;
2572 static asymbol *mips_elf_text_symbol_ptr;
2574 static asection mips_elf_data_section;
2575 static asection *mips_elf_data_section_ptr;
2576 static asymbol mips_elf_data_symbol;
2577 static asymbol *mips_elf_data_symbol_ptr;
2579 /* Handle the special MIPS section numbers that a symbol may use.
2580 This is used for both the 32-bit and the 64-bit ABI. */
2583 _bfd_mips_elf_symbol_processing (abfd, asym)
2587 elf_symbol_type *elfsym;
2589 elfsym = (elf_symbol_type *) asym;
2590 switch (elfsym->internal_elf_sym.st_shndx)
2592 case SHN_MIPS_ACOMMON:
2593 /* This section is used in a dynamically linked executable file.
2594 It is an allocated common section. The dynamic linker can
2595 either resolve these symbols to something in a shared
2596 library, or it can just leave them here. For our purposes,
2597 we can consider these symbols to be in a new section. */
2598 if (mips_elf_acom_section.name == NULL)
2600 /* Initialize the acommon section. */
2601 mips_elf_acom_section.name = ".acommon";
2602 mips_elf_acom_section.flags = SEC_ALLOC;
2603 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2604 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2605 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2606 mips_elf_acom_symbol.name = ".acommon";
2607 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2608 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2609 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2611 asym->section = &mips_elf_acom_section;
2615 /* Common symbols less than the GP size are automatically
2616 treated as SHN_MIPS_SCOMMON symbols. */
2617 if (asym->value > elf_gp_size (abfd))
2620 case SHN_MIPS_SCOMMON:
2621 if (mips_elf_scom_section.name == NULL)
2623 /* Initialize the small common section. */
2624 mips_elf_scom_section.name = ".scommon";
2625 mips_elf_scom_section.flags = SEC_IS_COMMON;
2626 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2627 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2628 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2629 mips_elf_scom_symbol.name = ".scommon";
2630 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2631 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2632 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2634 asym->section = &mips_elf_scom_section;
2635 asym->value = elfsym->internal_elf_sym.st_size;
2638 case SHN_MIPS_SUNDEFINED:
2639 asym->section = bfd_und_section_ptr;
2642 #if 0 /* for SGI_COMPAT */
2644 asym->section = mips_elf_text_section_ptr;
2648 asym->section = mips_elf_data_section_ptr;
2654 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2658 mips_elf_additional_program_headers (abfd)
2666 if (! SGI_COMPAT (abfd))
2669 s = bfd_get_section_by_name (abfd, ".reginfo");
2670 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2672 /* We need a PT_MIPS_REGINFO segment. */
2676 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2677 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2679 /* We need a PT_MIPS_RTPROC segment. */
2686 /* Modify the segment map for an Irix 5 executable. */
2689 mips_elf_modify_segment_map (abfd)
2693 struct elf_segment_map *m, **pm;
2695 if (! SGI_COMPAT (abfd))
2698 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2700 s = bfd_get_section_by_name (abfd, ".reginfo");
2701 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2703 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2704 if (m->p_type == PT_MIPS_REGINFO)
2708 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2712 m->p_type = PT_MIPS_REGINFO;
2716 /* We want to put it after the PHDR and INTERP segments. */
2717 pm = &elf_tdata (abfd)->segment_map;
2719 && ((*pm)->p_type == PT_PHDR
2720 || (*pm)->p_type == PT_INTERP))
2728 /* If there are .dynamic and .mdebug sections, we make a room for
2729 the RTPROC header. FIXME: Rewrite without section names. */
2730 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2731 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2732 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2734 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2735 if (m->p_type == PT_MIPS_RTPROC)
2739 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2743 m->p_type = PT_MIPS_RTPROC;
2745 s = bfd_get_section_by_name (abfd, ".rtproc");
2750 m->p_flags_valid = 1;
2758 /* We want to put it after the DYNAMIC segment. */
2759 pm = &elf_tdata (abfd)->segment_map;
2760 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2770 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2771 .dynsym, and .hash sections, and everything in between. */
2772 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2773 if ((*pm)->p_type == PT_DYNAMIC)
2778 && strcmp (m->sections[0]->name, ".dynamic") == 0)
2780 static const char *sec_names[] =
2781 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2784 struct elf_segment_map *n;
2788 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
2790 s = bfd_get_section_by_name (abfd, sec_names[i]);
2791 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2797 sz = s->_cooked_size;
2800 if (high < s->vma + sz)
2806 for (s = abfd->sections; s != NULL; s = s->next)
2807 if ((s->flags & SEC_LOAD) != 0
2810 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2814 n = ((struct elf_segment_map *)
2815 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
2822 for (s = abfd->sections; s != NULL; s = s->next)
2824 if ((s->flags & SEC_LOAD) != 0
2827 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2841 /* The structure of the runtime procedure descriptor created by the
2842 loader for use by the static exception system. */
2844 typedef struct runtime_pdr {
2845 bfd_vma adr; /* memory address of start of procedure */
2846 long regmask; /* save register mask */
2847 long regoffset; /* save register offset */
2848 long fregmask; /* save floating point register mask */
2849 long fregoffset; /* save floating point register offset */
2850 long frameoffset; /* frame size */
2851 short framereg; /* frame pointer register */
2852 short pcreg; /* offset or reg of return pc */
2853 long irpss; /* index into the runtime string table */
2855 struct exception_info *exception_info;/* pointer to exception array */
2857 #define cbRPDR sizeof(RPDR)
2858 #define rpdNil ((pRPDR) 0)
2860 /* Swap RPDR (runtime procedure table entry) for output. */
2862 static void ecoff_swap_rpdr_out
2863 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
2866 ecoff_swap_rpdr_out (abfd, in, ex)
2869 struct rpdr_ext *ex;
2871 /* ecoff_put_off was defined in ecoffswap.h. */
2872 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
2873 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
2874 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
2875 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
2876 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
2877 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
2879 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
2880 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
2882 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
2884 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
2888 /* Read ECOFF debugging information from a .mdebug section into a
2889 ecoff_debug_info structure. */
2892 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
2895 struct ecoff_debug_info *debug;
2898 const struct ecoff_debug_swap *swap;
2899 char *ext_hdr = NULL;
2901 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2903 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
2904 if (ext_hdr == NULL && swap->external_hdr_size != 0)
2907 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2908 swap->external_hdr_size)
2912 symhdr = &debug->symbolic_header;
2913 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2915 /* The symbolic header contains absolute file offsets and sizes to
2917 #define READ(ptr, offset, count, size, type) \
2918 if (symhdr->count == 0) \
2919 debug->ptr = NULL; \
2922 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2923 if (debug->ptr == NULL) \
2924 goto error_return; \
2925 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2926 || (bfd_read (debug->ptr, size, symhdr->count, \
2927 abfd) != size * symhdr->count)) \
2928 goto error_return; \
2931 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2932 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2933 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2934 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2935 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2936 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2938 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2939 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2940 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2941 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2942 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2946 debug->adjust = NULL;
2951 if (ext_hdr != NULL)
2953 if (debug->line != NULL)
2955 if (debug->external_dnr != NULL)
2956 free (debug->external_dnr);
2957 if (debug->external_pdr != NULL)
2958 free (debug->external_pdr);
2959 if (debug->external_sym != NULL)
2960 free (debug->external_sym);
2961 if (debug->external_opt != NULL)
2962 free (debug->external_opt);
2963 if (debug->external_aux != NULL)
2964 free (debug->external_aux);
2965 if (debug->ss != NULL)
2967 if (debug->ssext != NULL)
2968 free (debug->ssext);
2969 if (debug->external_fdr != NULL)
2970 free (debug->external_fdr);
2971 if (debug->external_rfd != NULL)
2972 free (debug->external_rfd);
2973 if (debug->external_ext != NULL)
2974 free (debug->external_ext);
2978 /* MIPS ELF local labels start with '$', not 'L'. */
2982 mips_elf_is_local_label_name (abfd, name)
2989 /* On Irix 6, the labels go back to starting with '.', so we accept
2990 the generic ELF local label syntax as well. */
2991 return _bfd_elf_is_local_label_name (abfd, name);
2994 /* MIPS ELF uses a special find_nearest_line routine in order the
2995 handle the ECOFF debugging information. */
2997 struct mips_elf_find_line
2999 struct ecoff_debug_info d;
3000 struct ecoff_find_line i;
3004 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3005 functionname_ptr, line_ptr)
3010 const char **filename_ptr;
3011 const char **functionname_ptr;
3012 unsigned int *line_ptr;
3016 msec = bfd_get_section_by_name (abfd, ".mdebug");
3020 struct mips_elf_find_line *fi;
3021 const struct ecoff_debug_swap * const swap =
3022 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3024 /* If we are called during a link, mips_elf_final_link may have
3025 cleared the SEC_HAS_CONTENTS field. We force it back on here
3026 if appropriate (which it normally will be). */
3027 origflags = msec->flags;
3028 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3029 msec->flags |= SEC_HAS_CONTENTS;
3031 fi = elf_tdata (abfd)->find_line_info;
3034 bfd_size_type external_fdr_size;
3037 struct fdr *fdr_ptr;
3039 fi = ((struct mips_elf_find_line *)
3040 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3043 msec->flags = origflags;
3047 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3049 msec->flags = origflags;
3053 /* Swap in the FDR information. */
3054 fi->d.fdr = ((struct fdr *)
3056 (fi->d.symbolic_header.ifdMax *
3057 sizeof (struct fdr))));
3058 if (fi->d.fdr == NULL)
3060 msec->flags = origflags;
3063 external_fdr_size = swap->external_fdr_size;
3064 fdr_ptr = fi->d.fdr;
3065 fraw_src = (char *) fi->d.external_fdr;
3066 fraw_end = (fraw_src
3067 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3068 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3069 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3071 elf_tdata (abfd)->find_line_info = fi;
3073 /* Note that we don't bother to ever free this information.
3074 find_nearest_line is either called all the time, as in
3075 objdump -l, so the information should be saved, or it is
3076 rarely called, as in ld error messages, so the memory
3077 wasted is unimportant. Still, it would probably be a
3078 good idea for free_cached_info to throw it away. */
3081 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3082 &fi->i, filename_ptr, functionname_ptr,
3085 msec->flags = origflags;
3089 msec->flags = origflags;
3092 /* Fall back on the generic ELF find_nearest_line routine. */
3094 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3095 filename_ptr, functionname_ptr,
3099 /* The mips16 compiler uses a couple of special sections to handle
3100 floating point arguments.
3102 Section names that look like .mips16.fn.FNNAME contain stubs that
3103 copy floating point arguments from the fp regs to the gp regs and
3104 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3105 call should be redirected to the stub instead. If no 32 bit
3106 function calls FNNAME, the stub should be discarded. We need to
3107 consider any reference to the function, not just a call, because
3108 if the address of the function is taken we will need the stub,
3109 since the address might be passed to a 32 bit function.
3111 Section names that look like .mips16.call.FNNAME contain stubs
3112 that copy floating point arguments from the gp regs to the fp
3113 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3114 then any 16 bit function that calls FNNAME should be redirected
3115 to the stub instead. If FNNAME is not a 32 bit function, the
3116 stub should be discarded.
3118 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3119 which call FNNAME and then copy the return value from the fp regs
3120 to the gp regs. These stubs store the return value in $18 while
3121 calling FNNAME; any function which might call one of these stubs
3122 must arrange to save $18 around the call. (This case is not
3123 needed for 32 bit functions that call 16 bit functions, because
3124 16 bit functions always return floating point values in both
3127 Note that in all cases FNNAME might be defined statically.
3128 Therefore, FNNAME is not used literally. Instead, the relocation
3129 information will indicate which symbol the section is for.
3131 We record any stubs that we find in the symbol table. */
3133 #define FN_STUB ".mips16.fn."
3134 #define CALL_STUB ".mips16.call."
3135 #define CALL_FP_STUB ".mips16.call.fp."
3137 /* The MIPS ELF linker needs additional information for each symbol in
3138 the global hash table. */
3140 struct mips_elf_link_hash_entry
3142 struct elf_link_hash_entry root;
3144 /* External symbol information. */
3147 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3148 unsigned int mips_32_relocs;
3150 /* If there is a stub that 32 bit functions should use to call this
3151 16 bit function, this points to the section containing the stub. */
3154 /* Whether we need the fn_stub; this is set if this symbol appears
3155 in any relocs other than a 16 bit call. */
3156 boolean need_fn_stub;
3158 /* If there is a stub that 16 bit functions should use to call this
3159 32 bit function, this points to the section containing the stub. */
3160 asection *call_stub;
3162 /* This is like the call_stub field, but it is used if the function
3163 being called returns a floating point value. */
3164 asection *call_fp_stub;
3167 /* MIPS ELF linker hash table. */
3169 struct mips_elf_link_hash_table
3171 struct elf_link_hash_table root;
3172 /* String section indices for the dynamic section symbols. */
3173 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3174 /* The number of .rtproc entries. */
3175 bfd_size_type procedure_count;
3176 /* The size of the .compact_rel section (if SGI_COMPAT). */
3177 bfd_size_type compact_rel_size;
3178 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3179 entry is set to the address of __rld_obj_head as in Irix 5. */
3180 boolean use_rld_obj_head;
3181 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3183 /* This is set if we see any mips16 stub sections. */
3184 boolean mips16_stubs_seen;
3187 /* Look up an entry in a MIPS ELF linker hash table. */
3189 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3190 ((struct mips_elf_link_hash_entry *) \
3191 elf_link_hash_lookup (&(table)->root, (string), (create), \
3194 /* Traverse a MIPS ELF linker hash table. */
3196 #define mips_elf_link_hash_traverse(table, func, info) \
3197 (elf_link_hash_traverse \
3199 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3202 /* Get the MIPS ELF linker hash table from a link_info structure. */
3204 #define mips_elf_hash_table(p) \
3205 ((struct mips_elf_link_hash_table *) ((p)->hash))
3207 static boolean mips_elf_output_extsym
3208 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3210 /* Create an entry in a MIPS ELF linker hash table. */
3212 static struct bfd_hash_entry *
3213 mips_elf_link_hash_newfunc (entry, table, string)
3214 struct bfd_hash_entry *entry;
3215 struct bfd_hash_table *table;
3218 struct mips_elf_link_hash_entry *ret =
3219 (struct mips_elf_link_hash_entry *) entry;
3221 /* Allocate the structure if it has not already been allocated by a
3223 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3224 ret = ((struct mips_elf_link_hash_entry *)
3225 bfd_hash_allocate (table,
3226 sizeof (struct mips_elf_link_hash_entry)));
3227 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3228 return (struct bfd_hash_entry *) ret;
3230 /* Call the allocation method of the superclass. */
3231 ret = ((struct mips_elf_link_hash_entry *)
3232 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3234 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3236 /* Set local fields. */
3237 memset (&ret->esym, 0, sizeof (EXTR));
3238 /* We use -2 as a marker to indicate that the information has
3239 not been set. -1 means there is no associated ifd. */
3241 ret->mips_32_relocs = 0;
3242 ret->fn_stub = NULL;
3243 ret->need_fn_stub = false;
3244 ret->call_stub = NULL;
3245 ret->call_fp_stub = NULL;
3248 return (struct bfd_hash_entry *) ret;
3251 /* Create a MIPS ELF linker hash table. */
3253 static struct bfd_link_hash_table *
3254 mips_elf_link_hash_table_create (abfd)
3257 struct mips_elf_link_hash_table *ret;
3260 ret = ((struct mips_elf_link_hash_table *)
3261 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3262 if (ret == (struct mips_elf_link_hash_table *) NULL)
3265 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3266 mips_elf_link_hash_newfunc))
3268 bfd_release (abfd, ret);
3272 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3273 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3274 ret->procedure_count = 0;
3275 ret->compact_rel_size = 0;
3276 ret->use_rld_obj_head = false;
3278 ret->mips16_stubs_seen = false;
3280 return &ret->root.root;
3283 /* Hook called by the linker routine which adds symbols from an object
3284 file. We must handle the special MIPS section numbers here. */
3288 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3290 struct bfd_link_info *info;
3291 const Elf_Internal_Sym *sym;
3297 if (SGI_COMPAT (abfd)
3298 && (abfd->flags & DYNAMIC) != 0
3299 && strcmp (*namep, "_rld_new_interface") == 0)
3301 /* Skip Irix 5 rld entry name. */
3306 switch (sym->st_shndx)
3309 /* Common symbols less than the GP size are automatically
3310 treated as SHN_MIPS_SCOMMON symbols. */
3311 if (sym->st_size > elf_gp_size (abfd))
3314 case SHN_MIPS_SCOMMON:
3315 *secp = bfd_make_section_old_way (abfd, ".scommon");
3316 (*secp)->flags |= SEC_IS_COMMON;
3317 *valp = sym->st_size;
3321 /* This section is used in a shared object. */
3322 if (mips_elf_text_section_ptr == NULL)
3324 /* Initialize the section. */
3325 mips_elf_text_section.name = ".text";
3326 mips_elf_text_section.flags = SEC_NO_FLAGS;
3327 mips_elf_text_section.output_section = NULL;
3328 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3329 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3330 mips_elf_text_symbol.name = ".text";
3331 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3332 mips_elf_text_symbol.section = &mips_elf_text_section;
3333 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3334 mips_elf_text_section_ptr = &mips_elf_text_section;
3336 /* This code used to do *secp = bfd_und_section_ptr if
3337 info->shared. I don't know why, and that doesn't make sense,
3338 so I took it out. */
3339 *secp = mips_elf_text_section_ptr;
3342 case SHN_MIPS_ACOMMON:
3343 /* Fall through. XXX Can we treat this as allocated data? */
3345 /* This section is used in a shared object. */
3346 if (mips_elf_data_section_ptr == NULL)
3348 /* Initialize the section. */
3349 mips_elf_data_section.name = ".data";
3350 mips_elf_data_section.flags = SEC_NO_FLAGS;
3351 mips_elf_data_section.output_section = NULL;
3352 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3353 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3354 mips_elf_data_symbol.name = ".data";
3355 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3356 mips_elf_data_symbol.section = &mips_elf_data_section;
3357 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3358 mips_elf_data_section_ptr = &mips_elf_data_section;
3360 /* This code used to do *secp = bfd_und_section_ptr if
3361 info->shared. I don't know why, and that doesn't make sense,
3362 so I took it out. */
3363 *secp = mips_elf_data_section_ptr;
3366 case SHN_MIPS_SUNDEFINED:
3367 *secp = bfd_und_section_ptr;
3371 if (SGI_COMPAT (abfd)
3373 && info->hash->creator == abfd->xvec
3374 && strcmp (*namep, "__rld_obj_head") == 0)
3376 struct elf_link_hash_entry *h;
3378 /* Mark __rld_obj_head as dynamic. */
3380 if (! (_bfd_generic_link_add_one_symbol
3381 (info, abfd, *namep, BSF_GLOBAL, *secp,
3382 (bfd_vma) *valp, (const char *) NULL, false,
3383 get_elf_backend_data (abfd)->collect,
3384 (struct bfd_link_hash_entry **) &h)))
3386 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3387 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3388 h->type = STT_OBJECT;
3390 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3393 mips_elf_hash_table (info)->use_rld_obj_head = true;
3396 /* If this is a mips16 text symbol, add 1 to the value to make it
3397 odd. This will cause something like .word SYM to come up with
3398 the right value when it is loaded into the PC. */
3399 if (sym->st_other == STO_MIPS16)
3405 /* Structure used to pass information to mips_elf_output_extsym. */
3410 struct bfd_link_info *info;
3411 struct ecoff_debug_info *debug;
3412 const struct ecoff_debug_swap *swap;
3416 /* This routine is used to write out ECOFF debugging external symbol
3417 information. It is called via mips_elf_link_hash_traverse. The
3418 ECOFF external symbol information must match the ELF external
3419 symbol information. Unfortunately, at this point we don't know
3420 whether a symbol is required by reloc information, so the two
3421 tables may wind up being different. We must sort out the external
3422 symbol information before we can set the final size of the .mdebug
3423 section, and we must set the size of the .mdebug section before we
3424 can relocate any sections, and we can't know which symbols are
3425 required by relocation until we relocate the sections.
3426 Fortunately, it is relatively unlikely that any symbol will be
3427 stripped but required by a reloc. In particular, it can not happen
3428 when generating a final executable. */
3431 mips_elf_output_extsym (h, data)
3432 struct mips_elf_link_hash_entry *h;
3435 struct extsym_info *einfo = (struct extsym_info *) data;
3437 asection *sec, *output_section;
3439 if (h->root.indx == -2)
3441 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3442 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3443 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3444 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3446 else if (einfo->info->strip == strip_all
3447 || (einfo->info->strip == strip_some
3448 && bfd_hash_lookup (einfo->info->keep_hash,
3449 h->root.root.root.string,
3450 false, false) == NULL))
3458 if (h->esym.ifd == -2)
3461 h->esym.cobol_main = 0;
3462 h->esym.weakext = 0;
3463 h->esym.reserved = 0;
3464 h->esym.ifd = ifdNil;
3465 h->esym.asym.value = 0;
3466 h->esym.asym.st = stGlobal;
3468 if (SGI_COMPAT (einfo->abfd)
3469 && (h->root.root.type == bfd_link_hash_undefined
3470 || h->root.root.type == bfd_link_hash_undefweak))
3474 /* Use undefined class. Also, set class and type for some
3476 name = h->root.root.root.string;
3477 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3478 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3480 h->esym.asym.sc = scData;
3481 h->esym.asym.st = stLabel;
3482 h->esym.asym.value = 0;
3484 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3486 h->esym.asym.sc = scAbs;
3487 h->esym.asym.st = stLabel;
3488 h->esym.asym.value =
3489 mips_elf_hash_table (einfo->info)->procedure_count;
3491 else if (strcmp (name, "_gp_disp") == 0)
3493 h->esym.asym.sc = scAbs;
3494 h->esym.asym.st = stLabel;
3495 h->esym.asym.value = elf_gp (einfo->abfd);
3498 h->esym.asym.sc = scUndefined;
3500 else if (h->root.root.type != bfd_link_hash_defined
3501 && h->root.root.type != bfd_link_hash_defweak)
3502 h->esym.asym.sc = scAbs;
3507 sec = h->root.root.u.def.section;
3508 output_section = sec->output_section;
3510 /* When making a shared library and symbol h is the one from
3511 the another shared library, OUTPUT_SECTION may be null. */
3512 if (output_section == NULL)
3513 h->esym.asym.sc = scUndefined;
3516 name = bfd_section_name (output_section->owner, output_section);
3518 if (strcmp (name, ".text") == 0)
3519 h->esym.asym.sc = scText;
3520 else if (strcmp (name, ".data") == 0)
3521 h->esym.asym.sc = scData;
3522 else if (strcmp (name, ".sdata") == 0)
3523 h->esym.asym.sc = scSData;
3524 else if (strcmp (name, ".rodata") == 0
3525 || strcmp (name, ".rdata") == 0)
3526 h->esym.asym.sc = scRData;
3527 else if (strcmp (name, ".bss") == 0)
3528 h->esym.asym.sc = scBss;
3529 else if (strcmp (name, ".sbss") == 0)
3530 h->esym.asym.sc = scSBss;
3531 else if (strcmp (name, ".init") == 0)
3532 h->esym.asym.sc = scInit;
3533 else if (strcmp (name, ".fini") == 0)
3534 h->esym.asym.sc = scFini;
3536 h->esym.asym.sc = scAbs;
3540 h->esym.asym.reserved = 0;
3541 h->esym.asym.index = indexNil;
3544 if (h->root.root.type == bfd_link_hash_common)
3545 h->esym.asym.value = h->root.root.u.c.size;
3546 else if (h->root.root.type == bfd_link_hash_defined
3547 || h->root.root.type == bfd_link_hash_defweak)
3549 if (h->esym.asym.sc == scCommon)
3550 h->esym.asym.sc = scBss;
3551 else if (h->esym.asym.sc == scSCommon)
3552 h->esym.asym.sc = scSBss;
3554 sec = h->root.root.u.def.section;
3555 output_section = sec->output_section;
3556 if (output_section != NULL)
3557 h->esym.asym.value = (h->root.root.u.def.value
3558 + sec->output_offset
3559 + output_section->vma);
3561 h->esym.asym.value = 0;
3563 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3565 /* Set type and value for a symbol with a function stub. */
3566 h->esym.asym.st = stProc;
3567 sec = h->root.root.u.def.section;
3569 h->esym.asym.value = 0;
3572 output_section = sec->output_section;
3573 if (output_section != NULL)
3574 h->esym.asym.value = (h->root.plt_offset
3575 + sec->output_offset
3576 + output_section->vma);
3578 h->esym.asym.value = 0;
3585 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3586 h->root.root.root.string,
3589 einfo->failed = true;
3596 /* Create a runtime procedure table from the .mdebug section. */
3599 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3602 struct bfd_link_info *info;
3604 struct ecoff_debug_info *debug;
3606 const struct ecoff_debug_swap *swap;
3607 HDRR *hdr = &debug->symbolic_header;
3609 struct rpdr_ext *erp;
3611 struct pdr_ext *epdr;
3612 struct sym_ext *esym;
3615 unsigned long size, count;
3616 unsigned long sindex;
3620 const char *no_name_func = "static procedure (no name)";
3628 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3630 sindex = strlen (no_name_func) + 1;
3631 count = hdr->ipdMax;
3634 size = swap->external_pdr_size;
3636 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3640 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3643 size = sizeof (RPDR);
3644 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3648 sv = (char **) bfd_malloc (sizeof (char *) * count);
3652 count = hdr->isymMax;
3653 size = swap->external_sym_size;
3654 esym = (struct sym_ext *) bfd_malloc (size * count);
3658 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3661 count = hdr->issMax;
3662 ss = (char *) bfd_malloc (count);
3665 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3668 count = hdr->ipdMax;
3669 for (i = 0; i < count; i++, rp++)
3671 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3672 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3673 rp->adr = sym.value;
3674 rp->regmask = pdr.regmask;
3675 rp->regoffset = pdr.regoffset;
3676 rp->fregmask = pdr.fregmask;
3677 rp->fregoffset = pdr.fregoffset;
3678 rp->frameoffset = pdr.frameoffset;
3679 rp->framereg = pdr.framereg;
3680 rp->pcreg = pdr.pcreg;
3682 sv[i] = ss + sym.iss;
3683 sindex += strlen (sv[i]) + 1;
3687 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3688 size = BFD_ALIGN (size, 16);
3689 rtproc = (PTR) bfd_alloc (abfd, size);
3692 mips_elf_hash_table (info)->procedure_count = 0;
3696 mips_elf_hash_table (info)->procedure_count = count + 2;
3698 erp = (struct rpdr_ext *) rtproc;
3699 memset (erp, 0, sizeof (struct rpdr_ext));
3701 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3702 strcpy (str, no_name_func);
3703 str += strlen (no_name_func) + 1;
3704 for (i = 0; i < count; i++)
3706 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3707 strcpy (str, sv[i]);
3708 str += strlen (sv[i]) + 1;
3710 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3712 /* Set the size and contents of .rtproc section. */
3713 s->_raw_size = size;
3714 s->contents = rtproc;
3716 /* Skip this section later on (I don't think this currently
3717 matters, but someday it might). */
3718 s->link_order_head = (struct bfd_link_order *) NULL;
3747 /* A comparison routine used to sort .gptab entries. */
3750 gptab_compare (p1, p2)
3754 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
3755 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
3757 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3760 /* We need to use a special link routine to handle the .reginfo and
3761 the .mdebug sections. We need to merge all instances of these
3762 sections together, not write them all out sequentially. */
3765 mips_elf_final_link (abfd, info)
3767 struct bfd_link_info *info;
3771 struct bfd_link_order *p;
3772 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3773 asection *rtproc_sec;
3774 Elf32_RegInfo reginfo;
3775 struct ecoff_debug_info debug;
3776 const struct ecoff_debug_swap *swap
3777 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3778 HDRR *symhdr = &debug.symbolic_header;
3779 PTR mdebug_handle = NULL;
3781 /* Drop the .options section, since it has special semantics which I
3782 haven't bothered to figure out. */
3783 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
3785 if (strcmp ((*secpp)->name, ".options") == 0)
3787 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
3788 if (p->type == bfd_indirect_link_order)
3789 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
3790 (*secpp)->link_order_head = NULL;
3791 *secpp = (*secpp)->next;
3792 --abfd->section_count;
3797 /* Get a value for the GP register. */
3798 if (elf_gp (abfd) == 0)
3800 struct bfd_link_hash_entry *h;
3802 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
3803 if (h != (struct bfd_link_hash_entry *) NULL
3804 && h->type == bfd_link_hash_defined)
3805 elf_gp (abfd) = (h->u.def.value
3806 + h->u.def.section->output_section->vma
3807 + h->u.def.section->output_offset);
3808 else if (info->relocateable)
3812 /* Make up a value. */
3814 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3817 && (strcmp (o->name, ".sbss") == 0
3818 || strcmp (o->name, ".sdata") == 0
3819 || strcmp (o->name, ".lit4") == 0
3820 || strcmp (o->name, ".lit8") == 0))
3823 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
3827 /* If the relocate_section function needs to do a reloc
3828 involving the GP value, it should make a reloc_dangerous
3829 callback to warn that GP is not defined. */
3833 /* Go through the sections and collect the .reginfo and .mdebug
3837 gptab_data_sec = NULL;
3838 gptab_bss_sec = NULL;
3839 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3841 if (strcmp (o->name, ".reginfo") == 0)
3843 memset (®info, 0, sizeof reginfo);
3845 /* We have found the .reginfo section in the output file.
3846 Look through all the link_orders comprising it and merge
3847 the information together. */
3848 for (p = o->link_order_head;
3849 p != (struct bfd_link_order *) NULL;
3852 asection *input_section;
3854 Elf32_External_RegInfo ext;
3857 if (p->type != bfd_indirect_link_order)
3859 if (p->type == bfd_fill_link_order)
3864 input_section = p->u.indirect.section;
3865 input_bfd = input_section->owner;
3867 /* The linker emulation code has probably clobbered the
3868 size to be zero bytes. */
3869 if (input_section->_raw_size == 0)
3870 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
3872 if (! bfd_get_section_contents (input_bfd, input_section,
3878 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
3880 reginfo.ri_gprmask |= sub.ri_gprmask;
3881 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3882 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3883 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3884 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3886 /* ri_gp_value is set by the function
3887 mips_elf32_section_processing when the section is
3888 finally written out. */
3890 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3891 elf_link_input_bfd ignores this section. */
3892 input_section->flags &=~ SEC_HAS_CONTENTS;
3895 /* Force the section size to the value we want. */
3896 o->_raw_size = sizeof (Elf32_External_RegInfo);
3898 /* Skip this section later on (I don't think this currently
3899 matters, but someday it might). */
3900 o->link_order_head = (struct bfd_link_order *) NULL;
3905 if (strcmp (o->name, ".mdebug") == 0)
3907 struct extsym_info einfo;
3909 /* We have found the .mdebug section in the output file.
3910 Look through all the link_orders comprising it and merge
3911 the information together. */
3912 symhdr->magic = swap->sym_magic;
3913 /* FIXME: What should the version stamp be? */
3915 symhdr->ilineMax = 0;
3919 symhdr->isymMax = 0;
3920 symhdr->ioptMax = 0;
3921 symhdr->iauxMax = 0;
3923 symhdr->issExtMax = 0;
3926 symhdr->iextMax = 0;
3928 /* We accumulate the debugging information itself in the
3929 debug_info structure. */
3931 debug.external_dnr = NULL;
3932 debug.external_pdr = NULL;
3933 debug.external_sym = NULL;
3934 debug.external_opt = NULL;
3935 debug.external_aux = NULL;
3937 debug.ssext = debug.ssext_end = NULL;
3938 debug.external_fdr = NULL;
3939 debug.external_rfd = NULL;
3940 debug.external_ext = debug.external_ext_end = NULL;
3942 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3943 if (mdebug_handle == (PTR) NULL)
3946 if (SGI_COMPAT (abfd))
3952 static const char * const name[] =
3953 { ".text", ".init", ".fini", ".data",
3954 ".rodata", ".sdata", ".sbss", ".bss" };
3955 static const int sc[] = { scText, scInit, scFini, scData,
3956 scRData, scSData, scSBss, scBss };
3959 esym.cobol_main = 0;
3963 esym.asym.iss = issNil;
3964 esym.asym.st = stLocal;
3965 esym.asym.reserved = 0;
3966 esym.asym.index = indexNil;
3967 for (i = 0; i < 8; i++)
3969 esym.asym.sc = sc[i];
3970 s = bfd_get_section_by_name (abfd, name[i]);
3973 esym.asym.value = s->vma;
3974 last = s->vma + s->_raw_size;
3977 esym.asym.value = last;
3979 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3985 for (p = o->link_order_head;
3986 p != (struct bfd_link_order *) NULL;
3989 asection *input_section;
3991 const struct ecoff_debug_swap *input_swap;
3992 struct ecoff_debug_info input_debug;
3996 if (p->type != bfd_indirect_link_order)
3998 if (p->type == bfd_fill_link_order)
4003 input_section = p->u.indirect.section;
4004 input_bfd = input_section->owner;
4006 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4007 || (get_elf_backend_data (input_bfd)
4008 ->elf_backend_ecoff_debug_swap) == NULL)
4010 /* I don't know what a non MIPS ELF bfd would be
4011 doing with a .mdebug section, but I don't really
4012 want to deal with it. */
4016 input_swap = (get_elf_backend_data (input_bfd)
4017 ->elf_backend_ecoff_debug_swap);
4019 BFD_ASSERT (p->size == input_section->_raw_size);
4021 /* The ECOFF linking code expects that we have already
4022 read in the debugging information and set up an
4023 ecoff_debug_info structure, so we do that now. */
4024 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4028 if (! (bfd_ecoff_debug_accumulate
4029 (mdebug_handle, abfd, &debug, swap, input_bfd,
4030 &input_debug, input_swap, info)))
4033 /* Loop through the external symbols. For each one with
4034 interesting information, try to find the symbol in
4035 the linker global hash table and save the information
4036 for the output external symbols. */
4037 eraw_src = input_debug.external_ext;
4038 eraw_end = (eraw_src
4039 + (input_debug.symbolic_header.iextMax
4040 * input_swap->external_ext_size));
4042 eraw_src < eraw_end;
4043 eraw_src += input_swap->external_ext_size)
4047 struct mips_elf_link_hash_entry *h;
4049 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4050 if (ext.asym.sc == scNil
4051 || ext.asym.sc == scUndefined
4052 || ext.asym.sc == scSUndefined)
4055 name = input_debug.ssext + ext.asym.iss;
4056 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4057 name, false, false, true);
4058 if (h == NULL || h->esym.ifd != -2)
4064 < input_debug.symbolic_header.ifdMax);
4065 ext.ifd = input_debug.ifdmap[ext.ifd];
4071 /* Free up the information we just read. */
4072 free (input_debug.line);
4073 free (input_debug.external_dnr);
4074 free (input_debug.external_pdr);
4075 free (input_debug.external_sym);
4076 free (input_debug.external_opt);
4077 free (input_debug.external_aux);
4078 free (input_debug.ss);
4079 free (input_debug.ssext);
4080 free (input_debug.external_fdr);
4081 free (input_debug.external_rfd);
4082 free (input_debug.external_ext);
4084 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4085 elf_link_input_bfd ignores this section. */
4086 input_section->flags &=~ SEC_HAS_CONTENTS;
4089 if (SGI_COMPAT (abfd) && info->shared)
4091 /* Create .rtproc section. */
4092 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4093 if (rtproc_sec == NULL)
4095 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4096 | SEC_LINKER_CREATED | SEC_READONLY);
4098 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4099 if (rtproc_sec == NULL
4100 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4101 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4105 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4106 info, rtproc_sec, &debug))
4110 /* Build the external symbol information. */
4113 einfo.debug = &debug;
4115 einfo.failed = false;
4116 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4117 mips_elf_output_extsym,
4122 /* Set the size of the .mdebug section. */
4123 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4125 /* Skip this section later on (I don't think this currently
4126 matters, but someday it might). */
4127 o->link_order_head = (struct bfd_link_order *) NULL;
4132 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4134 const char *subname;
4137 Elf32_External_gptab *ext_tab;
4140 /* The .gptab.sdata and .gptab.sbss sections hold
4141 information describing how the small data area would
4142 change depending upon the -G switch. These sections
4143 not used in executables files. */
4144 if (! info->relocateable)
4148 for (p = o->link_order_head;
4149 p != (struct bfd_link_order *) NULL;
4152 asection *input_section;
4154 if (p->type != bfd_indirect_link_order)
4156 if (p->type == bfd_fill_link_order)
4161 input_section = p->u.indirect.section;
4163 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4164 elf_link_input_bfd ignores this section. */
4165 input_section->flags &=~ SEC_HAS_CONTENTS;
4168 /* Skip this section later on (I don't think this
4169 currently matters, but someday it might). */
4170 o->link_order_head = (struct bfd_link_order *) NULL;
4172 /* Really remove the section. */
4173 for (secpp = &abfd->sections;
4175 secpp = &(*secpp)->next)
4177 *secpp = (*secpp)->next;
4178 --abfd->section_count;
4183 /* There is one gptab for initialized data, and one for
4184 uninitialized data. */
4185 if (strcmp (o->name, ".gptab.sdata") == 0)
4187 else if (strcmp (o->name, ".gptab.sbss") == 0)
4191 (*_bfd_error_handler)
4192 ("%s: illegal section name `%s'",
4193 bfd_get_filename (abfd), o->name);
4194 bfd_set_error (bfd_error_nonrepresentable_section);
4198 /* The linker script always combines .gptab.data and
4199 .gptab.sdata into .gptab.sdata, and likewise for
4200 .gptab.bss and .gptab.sbss. It is possible that there is
4201 no .sdata or .sbss section in the output file, in which
4202 case we must change the name of the output section. */
4203 subname = o->name + sizeof ".gptab" - 1;
4204 if (bfd_get_section_by_name (abfd, subname) == NULL)
4206 if (o == gptab_data_sec)
4207 o->name = ".gptab.data";
4209 o->name = ".gptab.bss";
4210 subname = o->name + sizeof ".gptab" - 1;
4211 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4214 /* Set up the first entry. */
4216 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4219 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4220 tab[0].gt_header.gt_unused = 0;
4222 /* Combine the input sections. */
4223 for (p = o->link_order_head;
4224 p != (struct bfd_link_order *) NULL;
4227 asection *input_section;
4231 bfd_size_type gpentry;
4233 if (p->type != bfd_indirect_link_order)
4235 if (p->type == bfd_fill_link_order)
4240 input_section = p->u.indirect.section;
4241 input_bfd = input_section->owner;
4243 /* Combine the gptab entries for this input section one
4244 by one. We know that the input gptab entries are
4245 sorted by ascending -G value. */
4246 size = bfd_section_size (input_bfd, input_section);
4248 for (gpentry = sizeof (Elf32_External_gptab);
4250 gpentry += sizeof (Elf32_External_gptab))
4252 Elf32_External_gptab ext_gptab;
4253 Elf32_gptab int_gptab;
4259 if (! (bfd_get_section_contents
4260 (input_bfd, input_section, (PTR) &ext_gptab,
4261 gpentry, sizeof (Elf32_External_gptab))))
4267 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4269 val = int_gptab.gt_entry.gt_g_value;
4270 add = int_gptab.gt_entry.gt_bytes - last;
4273 for (look = 1; look < c; look++)
4275 if (tab[look].gt_entry.gt_g_value >= val)
4276 tab[look].gt_entry.gt_bytes += add;
4278 if (tab[look].gt_entry.gt_g_value == val)
4284 Elf32_gptab *new_tab;
4287 /* We need a new table entry. */
4288 new_tab = ((Elf32_gptab *)
4289 bfd_realloc ((PTR) tab,
4290 (c + 1) * sizeof (Elf32_gptab)));
4291 if (new_tab == NULL)
4297 tab[c].gt_entry.gt_g_value = val;
4298 tab[c].gt_entry.gt_bytes = add;
4300 /* Merge in the size for the next smallest -G
4301 value, since that will be implied by this new
4304 for (look = 1; look < c; look++)
4306 if (tab[look].gt_entry.gt_g_value < val
4308 || (tab[look].gt_entry.gt_g_value
4309 > tab[max].gt_entry.gt_g_value)))
4313 tab[c].gt_entry.gt_bytes +=
4314 tab[max].gt_entry.gt_bytes;
4319 last = int_gptab.gt_entry.gt_bytes;
4322 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4323 elf_link_input_bfd ignores this section. */
4324 input_section->flags &=~ SEC_HAS_CONTENTS;
4327 /* The table must be sorted by -G value. */
4329 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4331 /* Swap out the table. */
4332 ext_tab = ((Elf32_External_gptab *)
4333 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4334 if (ext_tab == NULL)
4340 for (i = 0; i < c; i++)
4341 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4344 o->_raw_size = c * sizeof (Elf32_External_gptab);
4345 o->contents = (bfd_byte *) ext_tab;
4347 /* Skip this section later on (I don't think this currently
4348 matters, but someday it might). */
4349 o->link_order_head = (struct bfd_link_order *) NULL;
4353 /* Invoke the regular ELF backend linker to do all the work. */
4354 if (! bfd_elf32_bfd_final_link (abfd, info))
4357 /* Now write out the computed sections. */
4359 if (reginfo_sec != (asection *) NULL)
4361 Elf32_External_RegInfo ext;
4363 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
4364 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4365 (file_ptr) 0, sizeof ext))
4369 if (mdebug_sec != (asection *) NULL)
4371 BFD_ASSERT (abfd->output_has_begun);
4372 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4374 mdebug_sec->filepos))
4377 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4380 if (gptab_data_sec != (asection *) NULL)
4382 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4383 gptab_data_sec->contents,
4385 gptab_data_sec->_raw_size))
4389 if (gptab_bss_sec != (asection *) NULL)
4391 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4392 gptab_bss_sec->contents,
4394 gptab_bss_sec->_raw_size))
4398 if (SGI_COMPAT (abfd))
4400 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4401 if (rtproc_sec != NULL)
4403 if (! bfd_set_section_contents (abfd, rtproc_sec,
4404 rtproc_sec->contents,
4406 rtproc_sec->_raw_size))
4414 /* Handle a MIPS ELF HI16 reloc. */
4417 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4419 Elf_Internal_Rela *relhi;
4420 Elf_Internal_Rela *rello;
4427 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4429 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4432 addend += ((insn & 0xffff) << 16) + addlo;
4434 if ((addlo & 0x8000) != 0)
4436 if ((addend & 0x8000) != 0)
4439 bfd_put_32 (input_bfd,
4440 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4441 contents + relhi->r_offset);
4444 /* Handle a MIPS ELF local GOT16 reloc. */
4447 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4452 Elf_Internal_Rela *relhi;
4453 Elf_Internal_Rela *rello;
4457 unsigned int assigned_gotno;
4463 bfd_byte *got_contents;
4464 struct mips_got_info *g;
4466 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4468 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4471 addend += ((insn & 0xffff) << 16) + addlo;
4473 if ((addlo & 0x8000) != 0)
4475 if ((addend & 0x8000) != 0)
4478 /* Get a got entry representing requested hipage. */
4479 BFD_ASSERT (elf_section_data (sgot) != NULL);
4480 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4481 BFD_ASSERT (g != NULL);
4483 assigned_gotno = g->assigned_gotno;
4484 got_contents = sgot->contents;
4485 hipage = addend & 0xffff0000;
4487 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4489 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4490 if (hipage == (address & 0xffff0000))
4494 if (i == assigned_gotno)
4496 if (assigned_gotno >= g->local_gotno)
4498 (*_bfd_error_handler)
4499 ("more got entries are needed for hipage relocations");
4500 bfd_set_error (bfd_error_bad_value);
4504 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4505 ++g->assigned_gotno;
4508 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4509 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4510 contents + relhi->r_offset);
4515 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4518 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4520 Elf_Internal_Rela *rel;
4526 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4527 bfd_put_32 (input_bfd,
4528 (insn & 0xffff0000) | (offset & 0xffff),
4529 contents + rel->r_offset);
4532 /* Relocate a MIPS ELF section. */
4535 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4536 contents, relocs, local_syms, local_sections)
4538 struct bfd_link_info *info;
4540 asection *input_section;
4542 Elf_Internal_Rela *relocs;
4543 Elf_Internal_Sym *local_syms;
4544 asection **local_sections;
4546 Elf_Internal_Shdr *symtab_hdr;
4549 asection *sgot, *sreloc, *scpt;
4552 Elf_Internal_Rela *rel;
4553 Elf_Internal_Rela *relend;
4554 struct mips_got_info *g;
4556 dynobj = elf_hash_table (info)->dynobj;
4557 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4561 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4564 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4567 if (elf_bad_symtab (input_bfd))
4569 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4574 locsymcount = symtab_hdr->sh_info;
4575 extsymoff = symtab_hdr->sh_info;
4578 gp = _bfd_get_gp_value (output_bfd);
4581 relend = relocs + input_section->reloc_count;
4582 for (; rel < relend; rel++)
4585 reloc_howto_type *howto;
4586 unsigned long r_symndx;
4588 struct elf_link_hash_entry *h;
4590 Elf_Internal_Sym *sym;
4591 struct mips_elf_link_hash_entry *mh;
4593 bfd_reloc_status_type r;
4595 r_type = ELF32_R_TYPE (rel->r_info);
4596 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4597 && r_type != R_MIPS16_26
4598 && r_type != R_MIPS16_GPREL)
4600 bfd_set_error (bfd_error_bad_value);
4603 if (r_type == R_MIPS16_26)
4604 howto = &elf_mips16_jump_howto;
4605 else if (r_type == R_MIPS16_GPREL)
4606 howto = &elf_mips16_gprel_howto;
4608 howto = elf_mips_howto_table + r_type;
4611 && (r_type == R_MIPS_CALL16
4612 || r_type == R_MIPS_GOT16
4613 || r_type == R_MIPS_CALL_HI16
4614 || r_type == R_MIPS_CALL_LO16
4615 || r_type == R_MIPS_GOT_HI16
4616 || r_type == R_MIPS_GOT_LO16))
4618 /* We need the .got section. */
4621 sgot = bfd_get_section_by_name (dynobj, ".got");
4622 BFD_ASSERT (sgot != NULL);
4623 BFD_ASSERT (elf_section_data (sgot) != NULL);
4624 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4625 BFD_ASSERT (g != NULL);
4629 r_symndx = ELF32_R_SYM (rel->r_info);
4631 /* Mix in the change in GP address for a GP relative reloc. */
4632 if (r_type != R_MIPS_GPREL16
4633 && r_type != R_MIPS_LITERAL
4634 && r_type != R_MIPS_GPREL32
4635 && r_type != R_MIPS16_GPREL)
4641 if (! ((*info->callbacks->reloc_dangerous)
4643 "GP relative relocation when GP not defined",
4644 input_bfd, input_section,
4647 /* Only give the error once per link. */
4649 _bfd_set_gp_value (output_bfd, gp);
4652 if (r_symndx < extsymoff
4653 || (elf_bad_symtab (input_bfd)
4654 && local_sections[r_symndx] != NULL))
4656 /* This is a relocation against a section. The current
4657 addend in the instruction is the difference between
4658 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4659 must change this to be the difference between the
4660 final definition (which will end up in RELOCATION)
4661 and the GP value of OUTPUT_BFD (which is in GP). */
4662 addend = elf_gp (input_bfd) - gp;
4664 else if (! info->relocateable)
4666 /* We are doing a final link. The current addend in the
4667 instruction is simply the desired offset into the
4668 symbol (normally zero). We want the instruction to
4669 hold the difference between the final definition of
4670 the symbol (which will end up in RELOCATION) and the
4671 GP value of OUTPUT_BFD (which is in GP). */
4676 /* We are generating relocateable output, and we aren't
4677 going to define this symbol, so we just leave the
4678 instruction alone. */
4686 if (info->relocateable)
4688 /* This is a relocateable link. We don't have to change
4689 anything, unless the reloc is against a section symbol,
4690 in which case we have to adjust according to where the
4691 section symbol winds up in the output section. */
4692 if (r_symndx >= locsymcount
4693 || (elf_bad_symtab (input_bfd)
4694 && local_sections[r_symndx] == NULL))
4698 sym = local_syms + r_symndx;
4699 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4703 sec = local_sections[r_symndx];
4705 /* It would be logical to add sym->st_value here,
4706 but Irix 5 sometimes generates a garbage symbol
4708 addend += sec->output_offset;
4710 /* If this is HI16 or GOT16 with an associated LO16,
4711 adjust the addend accordingly. Otherwise, just
4713 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4714 r = _bfd_relocate_contents (howto, input_bfd,
4716 contents + rel->r_offset + 4);
4717 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4718 r = _bfd_relocate_contents (howto, input_bfd,
4720 contents + rel->r_offset);
4723 Elf_Internal_Rela *lorel;
4725 /* As a GNU extension, permit an arbitrary
4726 number of R_MIPS_HI16 relocs before the
4727 R_MIPS_LO16 reloc. This permits gcc to emit
4728 the HI and LO relocs itself. */
4729 if (r_type == R_MIPS_GOT16)
4733 for (lorel = rel + 1;
4735 && (ELF32_R_TYPE (lorel->r_info)
4741 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4743 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4748 r = _bfd_relocate_contents (howto, input_bfd,
4750 contents + rel->r_offset);
4760 /* This is a final link. */
4762 if (r_symndx < extsymoff
4763 || (elf_bad_symtab (input_bfd)
4764 && local_sections[r_symndx] != NULL))
4767 sym = local_syms + r_symndx;
4768 sec = local_sections[r_symndx];
4769 relocation = (sec->output_section->vma
4770 + sec->output_offset);
4772 /* It would be logical to always add sym->st_value here,
4773 but Irix 5 sometimes generates a garbage symbol
4775 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4776 relocation += sym->st_value;
4778 /* mips16 text labels should be treated as odd. */
4779 if (sym->st_other == STO_MIPS16)
4787 indx = r_symndx - extsymoff;
4788 h = elf_sym_hashes (input_bfd)[indx];
4789 while (h->root.type == bfd_link_hash_indirect
4790 || h->root.type == bfd_link_hash_warning)
4791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4792 if (strcmp (h->root.root.string, "_gp_disp") == 0)
4796 if (! ((*info->callbacks->reloc_dangerous)
4798 "_gp_disp used when GP not defined",
4799 input_bfd, input_section,
4802 /* Only give the error once per link. */
4804 _bfd_set_gp_value (output_bfd, gp);
4809 sec = input_section;
4810 if (sec->output_section != NULL)
4813 + sec->output_section->vma
4814 + sec->output_offset));
4816 relocation = gp - rel->r_offset;
4817 if (r_type == R_MIPS_LO16)
4821 else if (h->root.type == bfd_link_hash_defined
4822 || h->root.type == bfd_link_hash_defweak)
4824 sec = h->root.u.def.section;
4825 if (sec->output_section == NULL)
4828 relocation = (h->root.u.def.value
4829 + sec->output_section->vma
4830 + sec->output_offset);
4832 else if (h->root.type == bfd_link_hash_undefweak)
4834 else if (info->shared && ! info->symbolic)
4836 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
4838 /* If this is a dynamic link, we should have created
4839 a _DYNAMIC_LINK symbol in
4840 mips_elf_create_dynamic_sections. Otherwise, we
4841 should define the symbol with a value of 0.
4842 FIXME: It should probably get into the symbol
4843 table somehow as well. */
4844 BFD_ASSERT (! info->shared);
4845 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
4846 ".dynamic") == NULL);
4851 if (! ((*info->callbacks->undefined_symbol)
4852 (info, h->root.root.string, input_bfd,
4853 input_section, rel->r_offset)))
4859 mh = (struct mips_elf_link_hash_entry *) h;
4862 else if (sym != NULL)
4863 other = sym->st_other;
4867 /* If this function has an fn_stub, then it is a mips16
4868 function which needs a stub if it is called by a 32 bit
4869 function. If this reloc is anything other than a 16 bit
4870 call, redirect the reloc to the stub. We don't redirect
4871 relocs from other stub functions. */
4872 if (r_type != R_MIPS16_26
4874 && mh->fn_stub != NULL)
4876 && elf_tdata (input_bfd)->local_stubs != NULL
4877 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4878 && strncmp (bfd_get_section_name (input_bfd, input_section),
4879 FN_STUB, sizeof FN_STUB - 1) != 0
4880 && strncmp (bfd_get_section_name (input_bfd, input_section),
4881 CALL_STUB, sizeof CALL_STUB - 1) != 0
4882 && strncmp (bfd_get_section_name (input_bfd, input_section),
4883 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
4887 BFD_ASSERT (mh->need_fn_stub);
4888 relocation = (mh->fn_stub->output_section->vma
4889 + mh->fn_stub->output_offset);
4895 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
4896 relocation = (fn_stub->output_section->vma
4897 + fn_stub->output_offset);
4900 /* RELOCATION now points to 32 bit code. */
4904 /* If this function has a call_stub, then it is called by a
4905 mips16 function; the call needs to go through a stub if
4906 this function is a 32 bit function. If this reloc is a
4907 16 bit call, and the symbol is not a 16 bit function,
4908 then redirect the reloc to the stub. Note that we don't
4909 need to worry about calling the function through a
4910 function pointer; such calls are handled by routing
4911 through a special mips16 routine. We don't have to check
4912 whether this call is from a stub; it can't be, because a
4913 stub contains 32 bit code, and hence can not have a 16
4915 if (r_type == R_MIPS16_26
4917 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
4918 && other != STO_MIPS16)
4922 /* If both call_stub and call_fp_stub are defined, we
4923 can figure out which one to use by seeing which one
4924 appears in the input file. */
4925 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
4930 for (o = input_bfd->sections; o != NULL; o = o->next)
4932 if (strncmp (bfd_get_section_name (input_bfd, o),
4933 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4935 stub = mh->call_fp_stub;
4940 stub = mh->call_stub;
4942 else if (mh->call_stub != NULL)
4943 stub = mh->call_stub;
4945 stub = mh->call_fp_stub;
4947 BFD_ASSERT (stub->_raw_size > 0);
4948 relocation = stub->output_section->vma + stub->output_offset;
4951 if (r_type == R_MIPS_HI16)
4953 Elf_Internal_Rela *lorel;
4955 /* As a GNU extension, permit an arbitrary number of
4956 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
4957 This permits gcc to emit the HI and LO relocs itself. */
4958 for (lorel = rel + 1;
4960 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
4964 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4966 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4967 contents, relocation + addend);
4971 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4972 contents, rel->r_offset,
4973 relocation, addend);
4975 else if (r_type == R_MIPS_GOT16 && local)
4977 /* GOT16 must also have an associated LO16 in the local
4978 case. In this case, the addend is extracted and the
4979 section in which the referenced object is determined.
4980 Then the final address of the object is computed and
4981 the GOT entry for the hipage (an aligned 64kb chunk)
4982 is added to .got section if needed. The offset field
4983 of the GOT16-relocated instruction is replaced by the
4984 index of this GOT entry for the hipage. */
4985 if ((rel + 1) < relend
4986 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
4988 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
4991 relocation + addend))
4996 r = bfd_reloc_outofrange;
4998 else if (r_type == R_MIPS_CALL16
4999 || r_type == R_MIPS_GOT16
5000 || r_type == R_MIPS_CALL_LO16
5001 || r_type == R_MIPS_GOT_LO16)
5005 /* This symbol must be registered as a global symbol
5006 having the corresponding got entry. */
5007 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
5009 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5010 BFD_ASSERT (g->local_gotno <= offset
5011 && offset < sgot->_raw_size);
5012 bfd_put_32 (output_bfd, relocation + addend,
5013 sgot->contents + offset);
5014 offset = (sgot->output_section->vma + sgot->output_offset
5016 mips_elf_relocate_global_got (input_bfd, rel, contents,
5020 else if (r_type == R_MIPS_CALL_HI16
5021 || r_type == R_MIPS_GOT_HI16)
5025 /* This must be a global symbol with a got entry. The
5026 next reloc must be the corresponding LO16 reloc. */
5027 BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
5028 BFD_ASSERT ((rel + 1) < relend);
5029 BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
5030 == (r_type == R_MIPS_CALL_HI16
5032 : R_MIPS_GOT_LO16));
5034 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5035 BFD_ASSERT (g->local_gotno <= offset
5036 && offset < sgot->_raw_size);
5037 bfd_put_32 (output_bfd, relocation + addend,
5038 sgot->contents + offset);
5039 offset = (sgot->output_section->vma + sgot->output_offset
5041 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5045 else if (r_type == R_MIPS_REL32
5046 || r_type == R_MIPS_32)
5048 Elf_Internal_Rel outrel;
5049 Elf32_crinfo cptrel;
5053 || (elf_hash_table (info)->dynamic_sections_created
5055 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5057 && (input_section->flags & SEC_ALLOC) != 0)
5061 /* When generating a shared object, these
5062 relocations are copied into the output file to be
5063 resolved at run time. */
5066 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5067 BFD_ASSERT (sreloc != NULL);
5072 if (elf_section_data (input_section)->stab_info == NULL)
5073 outrel.r_offset = rel->r_offset;
5078 off = (_bfd_stab_section_offset
5079 (output_bfd, &elf_hash_table (info)->stab_info,
5081 &elf_section_data (input_section)->stab_info,
5083 if (off == (bfd_vma) -1)
5085 outrel.r_offset = off;
5088 outrel.r_offset += (input_section->output_section->vma
5089 + input_section->output_offset);
5091 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5094 memset (&outrel, 0, sizeof outrel);
5096 && (! info->symbolic
5097 || (h->elf_link_hash_flags
5098 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5100 BFD_ASSERT (h->dynindx != -1);
5101 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5102 sec = input_section;
5109 sec = local_sections[r_symndx];
5112 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5114 == bfd_link_hash_defweak));
5115 sec = h->root.u.def.section;
5117 if (sec != NULL && bfd_is_abs_section (sec))
5119 else if (sec == NULL || sec->owner == NULL)
5121 bfd_set_error (bfd_error_bad_value);
5128 osec = sec->output_section;
5129 indx = elf_section_data (osec)->dynindx;
5134 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5135 addend += relocation;
5139 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5141 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5142 (((Elf32_External_Rel *)
5144 + sreloc->reloc_count));
5145 ++sreloc->reloc_count;
5147 if (! skip && SGI_COMPAT (output_bfd))
5152 /* Make an entry of compact relocation info. */
5153 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5154 cptrel.vaddr = (rel->r_offset
5155 + input_section->output_section->vma
5156 + input_section->output_offset);
5157 if (r_type == R_MIPS_REL32)
5158 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5160 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5161 mips_elf_set_cr_dist2to (cptrel, 0);
5162 cptrel.konst = addend;
5164 cr = (scpt->contents
5165 + sizeof (Elf32_External_compact_rel));
5166 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5167 ((Elf32_External_crinfo *) cr
5168 + scpt->reloc_count));
5169 ++scpt->reloc_count;
5172 /* This reloc will be computed at runtime, so
5173 there's no need to do anything now. */
5177 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5178 contents, rel->r_offset,
5179 relocation, addend);
5181 else if (r_type == R_MIPS_64)
5186 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5187 addr = rel->r_offset;
5188 if (bfd_big_endian (input_bfd))
5190 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5191 contents, addr, relocation,
5193 val = bfd_get_32 (input_bfd, contents + addr);
5194 if ((val & 0x80000000) != 0)
5198 addr = rel->r_offset;
5199 if (bfd_little_endian (input_bfd))
5201 bfd_put_32 (input_bfd, val, contents + addr);
5203 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5207 /* This is a jump to a mips16 routine from a mips32
5208 routine. We need to change jal into jalx. */
5209 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5210 if (((insn >> 26) & 0x3f) != 0x3
5211 && ((insn >> 26) & 0x3f) != 0x1d)
5213 (*_bfd_error_handler)
5214 ("%s: %s+0x%lx: jump to mips16 routine which is not jal",
5215 bfd_get_filename (input_bfd),
5216 input_section->name,
5217 (unsigned long) rel->r_offset);
5218 bfd_set_error (bfd_error_bad_value);
5221 insn = (insn & 0x3ffffff) | (0x1d << 26);
5222 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5223 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5224 contents, rel->r_offset,
5225 relocation, addend);
5227 else if (r_type == R_MIPS16_26)
5229 /* It's easiest to do the normal relocation, and then
5230 dig out the instruction and swap the first word the
5231 way the mips16 expects it. If this is little endian,
5232 though, we need to swap the two words first, and then
5233 swap them back again later, so that the address looks
5236 if (bfd_little_endian (input_bfd))
5240 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5241 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5242 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5245 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5246 contents, rel->r_offset,
5247 relocation, addend);
5248 if (r == bfd_reloc_ok)
5252 if (bfd_little_endian (input_bfd))
5254 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5255 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5256 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5259 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5260 insn = ((insn & 0xfc00)
5261 | ((insn & 0x1f) << 5)
5262 | ((insn & 0x3e0) >> 5));
5263 /* If this is a jump to a 32 bit routine, then make
5265 if (other != STO_MIPS16)
5267 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5270 else if (r_type == R_MIPS16_GPREL)
5272 unsigned short extend, insn;
5274 unsigned long final;
5276 /* Extract the addend into buf, run the regular reloc,
5277 and stuff the resulting value back into the
5279 if (rel->r_offset > input_section->_raw_size)
5280 r = bfd_reloc_outofrange;
5283 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5284 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5285 bfd_put_32 (input_bfd,
5286 (((extend & 0x1f) << 11)
5290 r = _bfd_final_link_relocate (howto, input_bfd,
5292 (bfd_vma) 0, relocation,
5294 final = bfd_get_32 (input_bfd, buf);
5295 bfd_put_16 (input_bfd,
5297 | ((final >> 11) & 0x1f)
5299 contents + rel->r_offset);
5300 bfd_put_16 (input_bfd,
5303 contents + rel->r_offset + 2);
5307 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5308 contents, rel->r_offset,
5309 relocation, addend);
5311 /* The jal instruction can only jump to an address which is
5312 divisible by 4, and it can only jump to an address with
5313 the same upper 4 bits as the PC. */
5314 if (r == bfd_reloc_ok
5315 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5320 if (other == STO_MIPS16)
5321 addr &= ~ (bfd_vma) 1;
5324 || ((addr & 0xf0000000)
5325 != ((input_section->output_section->vma
5326 + input_section->output_offset
5329 r = bfd_reloc_overflow;
5332 if (SGI_COMPAT (abfd)
5334 && (input_section->flags & SEC_ALLOC) != 0)
5336 Elf32_crinfo cptrel;
5339 /* Make an entry of compact relocation info. */
5340 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5341 cptrel.vaddr = (rel->r_offset
5342 + input_section->output_section->vma
5343 + input_section->output_offset);
5348 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5349 /* XXX How should we set dist2to in this case. */
5350 mips_elf_set_cr_dist2to (cptrel, 8);
5351 cptrel.konst = addend + relocation;
5352 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5353 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5354 ((Elf32_External_crinfo *) cr
5355 + scpt->reloc_count));
5356 ++scpt->reloc_count;
5359 case R_MIPS_GPREL16:
5360 case R_MIPS_LITERAL:
5361 case R_MIPS_GPREL32:
5362 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5363 cptrel.konst = gp - cptrel.vaddr;
5364 mips_elf_set_cr_dist2to (cptrel, 4);
5365 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5366 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5367 ((Elf32_External_crinfo *) cr
5368 + scpt->reloc_count));
5369 ++scpt->reloc_count;
5378 if (r != bfd_reloc_ok)
5383 case bfd_reloc_outofrange:
5385 case bfd_reloc_overflow:
5390 name = h->root.root.string;
5393 name = bfd_elf_string_from_elf_section (input_bfd,
5394 symtab_hdr->sh_link,
5399 name = bfd_section_name (input_bfd, sec);
5401 if (! ((*info->callbacks->reloc_overflow)
5402 (info, name, howto->name, (bfd_vma) 0,
5403 input_bfd, input_section, rel->r_offset)))
5414 /* This hook function is called before the linker writes out a global
5415 symbol. This is where we undo the increment of the value for a
5420 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5422 struct bfd_link_info *info;
5424 Elf_Internal_Sym *sym;
5425 asection *input_sec;
5427 if (sym->st_other == STO_MIPS16
5428 && (sym->st_value & 1) != 0)
5433 /* Functions for the dynamic linker. */
5435 /* The name of the dynamic interpreter. This is put in the .interp
5438 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5440 /* Create dynamic sections when linking against a dynamic object. */
5443 mips_elf_create_dynamic_sections (abfd, info)
5445 struct bfd_link_info *info;
5447 struct elf_link_hash_entry *h;
5449 register asection *s;
5450 const char * const *namep;
5452 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5453 | SEC_LINKER_CREATED | SEC_READONLY);
5455 /* Mips ABI requests the .dynamic section to be read only. */
5456 s = bfd_get_section_by_name (abfd, ".dynamic");
5459 if (! bfd_set_section_flags (abfd, s, flags))
5463 /* We need to create .got section. */
5464 if (! mips_elf_create_got_section (abfd, info))
5467 /* Create .stub section. */
5468 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5470 s = bfd_make_section (abfd, ".stub");
5472 || ! bfd_set_section_flags (abfd, s, flags)
5473 || ! bfd_set_section_alignment (abfd, s, 2))
5477 if (SGI_COMPAT (abfd)
5479 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5481 s = bfd_make_section (abfd, ".rld_map");
5483 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5484 || ! bfd_set_section_alignment (abfd, s, 2))
5488 if (SGI_COMPAT (abfd))
5490 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5493 if (! (_bfd_generic_link_add_one_symbol
5494 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5495 (bfd_vma) 0, (const char *) NULL, false,
5496 get_elf_backend_data (abfd)->collect,
5497 (struct bfd_link_hash_entry **) &h)))
5499 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5500 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5501 h->type = STT_SECTION;
5503 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5507 /* We need to create a .compact_rel section. */
5508 if (! mips_elf_create_compact_rel_section (abfd, info))
5511 /* Change aligments of some sections. */
5512 s = bfd_get_section_by_name (abfd, ".hash");
5514 bfd_set_section_alignment (abfd, s, 4);
5515 s = bfd_get_section_by_name (abfd, ".dynsym");
5517 bfd_set_section_alignment (abfd, s, 4);
5518 s = bfd_get_section_by_name (abfd, ".dynstr");
5520 bfd_set_section_alignment (abfd, s, 4);
5521 s = bfd_get_section_by_name (abfd, ".reginfo");
5523 bfd_set_section_alignment (abfd, s, 4);
5524 s = bfd_get_section_by_name (abfd, ".dynamic");
5526 bfd_set_section_alignment (abfd, s, 4);
5532 if (! (_bfd_generic_link_add_one_symbol
5533 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5534 (bfd_vma) 0, (const char *) NULL, false,
5535 get_elf_backend_data (abfd)->collect,
5536 (struct bfd_link_hash_entry **) &h)))
5538 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5539 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5540 h->type = STT_SECTION;
5542 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5545 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5547 /* __rld_map is a four byte word located in the .data section
5548 and is filled in by the rtld to contain a pointer to
5549 the _r_debug structure. Its symbol value will be set in
5550 mips_elf_finish_dynamic_symbol. */
5551 s = bfd_get_section_by_name (abfd, ".rld_map");
5552 BFD_ASSERT (s != NULL);
5555 if (! (_bfd_generic_link_add_one_symbol
5556 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5557 (bfd_vma) 0, (const char *) NULL, false,
5558 get_elf_backend_data (abfd)->collect,
5559 (struct bfd_link_hash_entry **) &h)))
5561 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5562 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5563 h->type = STT_OBJECT;
5565 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5573 /* Create the .compact_rel section. */
5576 mips_elf_create_compact_rel_section (abfd, info)
5578 struct bfd_link_info *info;
5581 register asection *s;
5583 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5585 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5588 s = bfd_make_section (abfd, ".compact_rel");
5590 || ! bfd_set_section_flags (abfd, s, flags)
5591 || ! bfd_set_section_alignment (abfd, s, 2))
5594 s->_raw_size = sizeof (Elf32_External_compact_rel);
5600 /* Create the .got section to hold the global offset table. */
5603 mips_elf_create_got_section (abfd, info)
5605 struct bfd_link_info *info;
5608 register asection *s;
5609 struct elf_link_hash_entry *h;
5610 struct mips_got_info *g;
5612 /* This function may be called more than once. */
5613 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5616 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5617 | SEC_LINKER_CREATED);
5619 s = bfd_make_section (abfd, ".got");
5621 || ! bfd_set_section_flags (abfd, s, flags)
5622 || ! bfd_set_section_alignment (abfd, s, 4))
5625 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5626 linker script because we don't want to define the symbol if we
5627 are not creating a global offset table. */
5629 if (! (_bfd_generic_link_add_one_symbol
5630 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5631 (bfd_vma) 0, (const char *) NULL, false,
5632 get_elf_backend_data (abfd)->collect,
5633 (struct bfd_link_hash_entry **) &h)))
5635 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5636 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5637 h->type = STT_OBJECT;
5640 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5643 /* The first several global offset table entries are reserved. */
5644 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5646 g = (struct mips_got_info *) bfd_alloc (abfd,
5647 sizeof (struct mips_got_info));
5650 g->global_gotsym = 0;
5651 g->local_gotno = MIPS_RESERVED_GOTNO;
5652 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5653 if (elf_section_data (s) == NULL)
5656 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5657 if (elf_section_data (s) == NULL)
5660 elf_section_data (s)->tdata = (PTR) g;
5665 /* Look through the relocs for a section during the first phase, and
5666 allocate space in the global offset table. */
5669 mips_elf_check_relocs (abfd, info, sec, relocs)
5671 struct bfd_link_info *info;
5673 const Elf_Internal_Rela *relocs;
5677 Elf_Internal_Shdr *symtab_hdr;
5678 struct elf_link_hash_entry **sym_hashes;
5679 struct mips_got_info *g;
5681 const Elf_Internal_Rela *rel;
5682 const Elf_Internal_Rela *rel_end;
5686 if (info->relocateable)
5689 dynobj = elf_hash_table (info)->dynobj;
5690 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5691 sym_hashes = elf_sym_hashes (abfd);
5692 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5694 /* Check for the mips16 stub sections. */
5696 name = bfd_get_section_name (abfd, sec);
5697 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5699 unsigned long r_symndx;
5701 /* Look at the relocation information to figure out which symbol
5704 r_symndx = ELF32_R_SYM (relocs->r_info);
5706 if (r_symndx < extsymoff
5707 || sym_hashes[r_symndx - extsymoff] == NULL)
5711 /* This stub is for a local symbol. This stub will only be
5712 needed if there is some relocation in this BFD, other
5713 than a 16 bit function call, which refers to this symbol. */
5714 for (o = abfd->sections; o != NULL; o = o->next)
5716 Elf_Internal_Rela *sec_relocs;
5717 const Elf_Internal_Rela *r, *rend;
5719 /* We can ignore stub sections when looking for relocs. */
5720 if ((o->flags & SEC_RELOC) == 0
5721 || o->reloc_count == 0
5722 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5723 sizeof FN_STUB - 1) == 0
5724 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5725 sizeof CALL_STUB - 1) == 0
5726 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5727 sizeof CALL_FP_STUB - 1) == 0)
5730 sec_relocs = (_bfd_elf32_link_read_relocs
5731 (abfd, o, (PTR) NULL,
5732 (Elf_Internal_Rela *) NULL,
5733 info->keep_memory));
5734 if (sec_relocs == NULL)
5737 rend = sec_relocs + o->reloc_count;
5738 for (r = sec_relocs; r < rend; r++)
5739 if (ELF32_R_SYM (r->r_info) == r_symndx
5740 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
5743 if (! info->keep_memory)
5752 /* There is no non-call reloc for this stub, so we do
5753 not need it. Since this function is called before
5754 the linker maps input sections to output sections, we
5755 can easily discard it by setting the SEC_EXCLUDE
5757 sec->flags |= SEC_EXCLUDE;
5761 /* Record this stub in an array of local symbol stubs for
5763 if (elf_tdata (abfd)->local_stubs == NULL)
5765 unsigned long symcount;
5768 if (elf_bad_symtab (abfd))
5769 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5771 symcount = symtab_hdr->sh_info;
5772 n = (asection **) bfd_zalloc (abfd,
5773 symcount * sizeof (asection *));
5776 elf_tdata (abfd)->local_stubs = n;
5779 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5781 /* We don't need to set mips16_stubs_seen in this case.
5782 That flag is used to see whether we need to look through
5783 the global symbol table for stubs. We don't need to set
5784 it here, because we just have a local stub. */
5788 struct mips_elf_link_hash_entry *h;
5790 h = ((struct mips_elf_link_hash_entry *)
5791 sym_hashes[r_symndx - extsymoff]);
5793 /* H is the symbol this stub is for. */
5796 mips_elf_hash_table (info)->mips16_stubs_seen = true;
5799 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5800 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5802 unsigned long r_symndx;
5803 struct mips_elf_link_hash_entry *h;
5806 /* Look at the relocation information to figure out which symbol
5809 r_symndx = ELF32_R_SYM (relocs->r_info);
5811 if (r_symndx < extsymoff
5812 || sym_hashes[r_symndx - extsymoff] == NULL)
5814 /* This stub was actually built for a static symbol defined
5815 in the same file. We assume that all static symbols in
5816 mips16 code are themselves mips16, so we can simply
5817 discard this stub. Since this function is called before
5818 the linker maps input sections to output sections, we can
5819 easily discard it by setting the SEC_EXCLUDE flag. */
5820 sec->flags |= SEC_EXCLUDE;
5824 h = ((struct mips_elf_link_hash_entry *)
5825 sym_hashes[r_symndx - extsymoff]);
5827 /* H is the symbol this stub is for. */
5829 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5830 loc = &h->call_fp_stub;
5832 loc = &h->call_stub;
5834 /* If we already have an appropriate stub for this function, we
5835 don't need another one, so we can discard this one. Since
5836 this function is called before the linker maps input sections
5837 to output sections, we can easily discard it by setting the
5838 SEC_EXCLUDE flag. We can also discard this section if we
5839 happen to already know that this is a mips16 function; it is
5840 not necessary to check this here, as it is checked later, but
5841 it is slightly faster to check now. */
5842 if (*loc != NULL || h->root.other == STO_MIPS16)
5844 sec->flags |= SEC_EXCLUDE;
5849 mips_elf_hash_table (info)->mips16_stubs_seen = true;
5859 sgot = bfd_get_section_by_name (dynobj, ".got");
5864 BFD_ASSERT (elf_section_data (sgot) != NULL);
5865 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5866 BFD_ASSERT (g != NULL);
5872 rel_end = relocs + sec->reloc_count;
5873 for (rel = relocs; rel < rel_end; rel++)
5875 unsigned long r_symndx;
5876 struct elf_link_hash_entry *h;
5878 r_symndx = ELF32_R_SYM (rel->r_info);
5880 if (r_symndx < extsymoff)
5884 h = sym_hashes[r_symndx - extsymoff];
5886 /* This may be an indirect symbol created because of a version. */
5889 while (h->root.type == bfd_link_hash_indirect)
5890 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5894 /* Some relocs require a global offset table. */
5895 if (dynobj == NULL || sgot == NULL)
5897 switch (ELF32_R_TYPE (rel->r_info))
5901 case R_MIPS_CALL_HI16:
5902 case R_MIPS_CALL_LO16:
5903 case R_MIPS_GOT_HI16:
5904 case R_MIPS_GOT_LO16:
5906 elf_hash_table (info)->dynobj = dynobj = abfd;
5907 if (! mips_elf_create_got_section (dynobj, info))
5909 sgot = bfd_get_section_by_name (dynobj, ".got");
5910 BFD_ASSERT (sgot != NULL);
5911 BFD_ASSERT (elf_section_data (sgot) != NULL);
5912 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5913 BFD_ASSERT (g != NULL);
5919 && (info->shared || h != NULL)
5920 && (sec->flags & SEC_ALLOC) != 0)
5921 elf_hash_table (info)->dynobj = dynobj = abfd;
5929 switch (ELF32_R_TYPE (rel->r_info))
5932 case R_MIPS_CALL_HI16:
5933 case R_MIPS_CALL_LO16:
5934 /* This symbol requires a global offset table entry. */
5938 (*_bfd_error_handler)
5939 ("%s: CALL16 reloc at 0x%lx not against global symbol",
5940 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
5941 bfd_set_error (bfd_error_bad_value);
5945 /* Make sure this symbol is output as a dynamic symbol. */
5946 if (h->dynindx == -1)
5948 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5952 if (h->got_offset != (bfd_vma) -1)
5954 /* We have already allocated space in the .got. */
5958 /* Note the index of the first global got symbol in .dynsym. */
5959 if (g->global_gotsym == 0
5960 || g->global_gotsym > (unsigned long) h->dynindx)
5961 g->global_gotsym = h->dynindx;
5963 /* Make this symbol to have the corresponding got entry. */
5966 /* We need a stub, not a plt entry for the undefined
5967 function. But we record it as if it needs plt. See
5968 elf_adjust_dynamic_symbol in elflink.h. */
5969 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
5975 case R_MIPS_GOT_HI16:
5976 case R_MIPS_GOT_LO16:
5977 /* This symbol requires a global offset table entry. */
5981 /* Make sure this symbol is output as a dynamic symbol. */
5982 if (h->dynindx == -1)
5984 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5988 if (h->got_offset != (bfd_vma) -1)
5990 /* We have already allocated space in the .got. */
5993 /* Note the index of the first global got symbol in
5995 if (g->global_gotsym == 0
5996 || g->global_gotsym > (unsigned long) h->dynindx)
5997 g->global_gotsym = h->dynindx;
5999 /* Make this symbol to be the global got symbol. */
6007 if ((info->shared || h != NULL)
6008 && (sec->flags & SEC_ALLOC) != 0)
6012 const char *name = ".rel.dyn";
6014 sreloc = bfd_get_section_by_name (dynobj, name);
6017 sreloc = bfd_make_section (dynobj, name);
6019 || ! bfd_set_section_flags (dynobj, sreloc,
6024 | SEC_LINKER_CREATED
6026 || ! bfd_set_section_alignment (dynobj, sreloc,
6033 /* When creating a shared object, we must copy these
6034 reloc types into the output file as R_MIPS_REL32
6035 relocs. We make room for this reloc in the
6036 .rel.dyn reloc section */
6037 if (sreloc->_raw_size == 0)
6039 /* Add a null element. */
6040 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6041 ++sreloc->reloc_count;
6043 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6047 struct mips_elf_link_hash_entry *hmips;
6049 /* We only need to copy this reloc if the symbol is
6050 defined in a dynamic object. */
6051 hmips = (struct mips_elf_link_hash_entry *) h;
6052 ++hmips->mips_32_relocs;
6056 if (SGI_COMPAT (abfd))
6057 mips_elf_hash_table (info)->compact_rel_size +=
6058 sizeof (Elf32_External_crinfo);
6063 case R_MIPS_GPREL16:
6064 case R_MIPS_LITERAL:
6065 case R_MIPS_GPREL32:
6066 if (SGI_COMPAT (abfd))
6067 mips_elf_hash_table (info)->compact_rel_size +=
6068 sizeof (Elf32_External_crinfo);
6075 /* If this reloc is not a 16 bit call, and it has a global
6076 symbol, then we will need the fn_stub if there is one.
6077 References from a stub section do not count. */
6079 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6080 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6081 sizeof FN_STUB - 1) != 0
6082 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6083 sizeof CALL_STUB - 1) != 0
6084 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6085 sizeof CALL_FP_STUB - 1) != 0)
6087 struct mips_elf_link_hash_entry *mh;
6089 mh = (struct mips_elf_link_hash_entry *) h;
6090 mh->need_fn_stub = true;
6097 /* Adjust a symbol defined by a dynamic object and referenced by a
6098 regular object. The current definition is in some section of the
6099 dynamic object, but we're not including those sections. We have to
6100 change the definition to something the rest of the link can
6104 mips_elf_adjust_dynamic_symbol (info, h)
6105 struct bfd_link_info *info;
6106 struct elf_link_hash_entry *h;
6109 struct mips_elf_link_hash_entry *hmips;
6112 dynobj = elf_hash_table (info)->dynobj;
6114 /* Make sure we know what is going on here. */
6115 BFD_ASSERT (dynobj != NULL
6116 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6117 || h->weakdef != NULL
6118 || ((h->elf_link_hash_flags
6119 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6120 && (h->elf_link_hash_flags
6121 & ELF_LINK_HASH_REF_REGULAR) != 0
6122 && (h->elf_link_hash_flags
6123 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6125 /* If this symbol is defined in a dynamic object, we need to copy
6126 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6128 hmips = (struct mips_elf_link_hash_entry *) h;
6129 if (! info->relocateable
6130 && hmips->mips_32_relocs != 0
6131 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6133 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6134 BFD_ASSERT (s != NULL);
6136 if (s->_raw_size == 0)
6138 /* Make room for a null element. */
6139 s->_raw_size += sizeof (Elf32_External_Rel);
6142 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6145 /* For a function, create a stub, if needed. */
6146 if (h->type == STT_FUNC
6147 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6149 if (! elf_hash_table (info)->dynamic_sections_created)
6152 /* If this symbol is not defined in a regular file, then set
6153 the symbol to the stub location. This is required to make
6154 function pointers compare as equal between the normal
6155 executable and the shared library. */
6156 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6158 /* We need .stub section. */
6159 s = bfd_get_section_by_name (dynobj, ".stub");
6160 BFD_ASSERT (s != NULL);
6162 h->root.u.def.section = s;
6163 h->root.u.def.value = s->_raw_size;
6165 /* XXX Write this stub address somewhere. */
6166 h->plt_offset = s->_raw_size;
6168 /* Make room for this stub code. */
6169 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6171 /* The last half word of the stub will be filled with the index
6172 of this symbol in .dynsym section. */
6177 /* If this is a weak symbol, and there is a real definition, the
6178 processor independent code will have arranged for us to see the
6179 real definition first, and we can just use the same value. */
6180 if (h->weakdef != NULL)
6182 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6183 || h->weakdef->root.type == bfd_link_hash_defweak);
6184 h->root.u.def.section = h->weakdef->root.u.def.section;
6185 h->root.u.def.value = h->weakdef->root.u.def.value;
6189 /* This is a reference to a symbol defined by a dynamic object which
6190 is not a function. */
6195 /* This function is called after all the input files have been read,
6196 and the input sections have been assigned to output sections. We
6197 check for any mips16 stub sections that we can discard. */
6199 static boolean mips_elf_check_mips16_stubs
6200 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6203 mips_elf_always_size_sections (output_bfd, info)
6205 struct bfd_link_info *info;
6207 if (info->relocateable
6208 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6211 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6212 mips_elf_check_mips16_stubs,
6218 /* Check the mips16 stubs for a particular symbol, and see if we can
6223 mips_elf_check_mips16_stubs (h, data)
6224 struct mips_elf_link_hash_entry *h;
6227 if (h->fn_stub != NULL
6228 && ! h->need_fn_stub)
6230 /* We don't need the fn_stub; the only references to this symbol
6231 are 16 bit calls. Clobber the size to 0 to prevent it from
6232 being included in the link. */
6233 h->fn_stub->_raw_size = 0;
6234 h->fn_stub->_cooked_size = 0;
6235 h->fn_stub->flags &= ~ SEC_RELOC;
6236 h->fn_stub->reloc_count = 0;
6237 h->fn_stub->flags |= SEC_EXCLUDE;
6240 if (h->call_stub != NULL
6241 && h->root.other == STO_MIPS16)
6243 /* We don't need the call_stub; this is a 16 bit function, so
6244 calls from other 16 bit functions are OK. Clobber the size
6245 to 0 to prevent it from being included in the link. */
6246 h->call_stub->_raw_size = 0;
6247 h->call_stub->_cooked_size = 0;
6248 h->call_stub->flags &= ~ SEC_RELOC;
6249 h->call_stub->reloc_count = 0;
6250 h->call_stub->flags |= SEC_EXCLUDE;
6253 if (h->call_fp_stub != NULL
6254 && h->root.other == STO_MIPS16)
6256 /* We don't need the call_stub; this is a 16 bit function, so
6257 calls from other 16 bit functions are OK. Clobber the size
6258 to 0 to prevent it from being included in the link. */
6259 h->call_fp_stub->_raw_size = 0;
6260 h->call_fp_stub->_cooked_size = 0;
6261 h->call_fp_stub->flags &= ~ SEC_RELOC;
6262 h->call_fp_stub->reloc_count = 0;
6263 h->call_fp_stub->flags |= SEC_EXCLUDE;
6269 /* Set the sizes of the dynamic sections. */
6272 mips_elf_size_dynamic_sections (output_bfd, info)
6274 struct bfd_link_info *info;
6280 struct mips_got_info *g;
6282 dynobj = elf_hash_table (info)->dynobj;
6283 BFD_ASSERT (dynobj != NULL);
6285 if (elf_hash_table (info)->dynamic_sections_created)
6287 /* Set the contents of the .interp section to the interpreter. */
6290 s = bfd_get_section_by_name (dynobj, ".interp");
6291 BFD_ASSERT (s != NULL);
6292 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6293 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6297 /* Recompute the size of .got for local entires (reserved and
6298 hipages) if needed. To estimate it, get the upper bound of total
6299 size of loadable sections. */
6300 sgot = bfd_get_section_by_name (dynobj, ".got");
6304 bfd_size_type loadable_size = 0;
6305 bfd_size_type local_gotno;
6308 BFD_ASSERT (elf_section_data (sgot) != NULL);
6309 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6310 BFD_ASSERT (g != NULL);
6312 for (sub = info->input_bfds; sub; sub = sub->link_next)
6313 for (s = sub->sections; s != NULL; s = s->next)
6315 if ((s->flags & SEC_ALLOC) == 0)
6317 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6320 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6322 /* Assume there are two loadable segments consisting of
6323 contiguous sections. Is 5 enough? */
6324 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6325 g->local_gotno = local_gotno;
6326 sgot->_raw_size += local_gotno * 4;
6329 /* The check_relocs and adjust_dynamic_symbol entry points have
6330 determined the sizes of the various dynamic sections. Allocate
6333 for (s = dynobj->sections; s != NULL; s = s->next)
6338 /* It's OK to base decisions on the section name, because none
6339 of the dynobj section names depend upon the input files. */
6340 name = bfd_get_section_name (dynobj, s);
6342 if ((s->flags & SEC_LINKER_CREATED) == 0)
6347 if (strncmp (name, ".rel", 4) == 0)
6349 if (s->_raw_size == 0)
6353 const char *outname;
6356 /* If this relocation section applies to a read only
6357 section, then we probably need a DT_TEXTREL entry.
6358 If the relocation section is .rel.dyn, we always
6359 assert a DT_TEXTREL entry rather than testing whether
6360 there exists a relocation to a read only section or
6362 outname = bfd_get_section_name (output_bfd,
6364 target = bfd_get_section_by_name (output_bfd, outname + 4);
6365 if ((target != NULL && (target->flags & SEC_READONLY) != 0)
6366 || strcmp (outname, ".rel.dyn") == 0)
6369 /* We use the reloc_count field as a counter if we need
6370 to copy relocs into the output file. */
6371 if (strcmp (name, ".rel.dyn") != 0)
6375 else if (strncmp (name, ".got", 4) == 0)
6379 BFD_ASSERT (elf_section_data (s) != NULL);
6380 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6381 BFD_ASSERT (g != NULL);
6383 /* Fix the size of .got section for the correspondence of
6384 global symbols and got entries. This adds some useless
6385 got entries. Is this required by ABI really? */
6386 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6387 s->_raw_size += i * 4;
6389 else if (strncmp (name, ".stub", 5) == 0)
6391 /* Irix rld assumes that the function stub isn't at the end
6392 of .text section. So put a dummy. XXX */
6393 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6395 else if (! info->shared
6396 && ! mips_elf_hash_table (info)->use_rld_obj_head
6397 && strncmp (name, ".rld_map", 8) == 0)
6399 /* We add a room for __rld_map. It will be filled in by the
6400 rtld to contain a pointer to the _r_debug structure. */
6403 else if (SGI_COMPAT (output_bfd)
6404 && strncmp (name, ".compact_rel", 12) == 0)
6405 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6406 else if (strncmp (name, ".init", 5) != 0)
6408 /* It's not one of our sections, so don't allocate space. */
6416 for (spp = &s->output_section->owner->sections;
6417 *spp != s->output_section;
6418 spp = &(*spp)->next)
6420 *spp = s->output_section->next;
6421 --s->output_section->owner->section_count;
6426 /* Allocate memory for the section contents. */
6427 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6428 if (s->contents == NULL && s->_raw_size != 0)
6430 bfd_set_error (bfd_error_no_memory);
6433 memset (s->contents, 0, s->_raw_size);
6436 if (elf_hash_table (info)->dynamic_sections_created)
6438 /* Add some entries to the .dynamic section. We fill in the
6439 values later, in elf_mips_finish_dynamic_sections, but we
6440 must add the entries now so that we get the correct size for
6441 the .dynamic section. The DT_DEBUG entry is filled in by the
6442 dynamic linker and used by the debugger. */
6445 if (SGI_COMPAT (output_bfd))
6447 /* SGI object has the equivalence of DT_DEBUG in the
6448 DT_MIPS_RLD_MAP entry. */
6449 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6453 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6459 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6463 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6466 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6468 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6471 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6474 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6478 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6481 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6484 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6486 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6489 s = bfd_get_section_by_name (dynobj, ".liblist");
6490 BFD_ASSERT (s != NULL);
6492 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6496 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6499 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6503 /* Time stamps in executable files are a bad idea. */
6504 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6509 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6514 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6518 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6521 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6524 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6527 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6530 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6533 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6536 #if 0 /* (SGI_COMPAT) */
6537 if (! bfd_get_section_by_name (dynobj, ".init"))
6538 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6541 if (! bfd_get_section_by_name (dynobj, ".fini"))
6542 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6547 /* If we use dynamic linking, we generate a section symbol for each
6548 output section. These are local symbols, which means that they
6549 must come first in the dynamic symbol table.
6550 That means we must increment the dynamic symbol index of every
6551 other dynamic symbol. */
6553 const char * const *namep;
6555 bfd_size_type strindex;
6556 struct bfd_strtab_hash *dynstr;
6557 struct mips_got_info *g;
6560 if (elf_hash_table (info)->dynamic_sections_created)
6562 if (SGI_COMPAT (output_bfd))
6564 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6565 elf_link_hash_traverse (elf_hash_table (info),
6566 mips_elf_adjust_dynindx,
6568 elf_hash_table (info)->dynsymcount += c;
6570 dynstr = elf_hash_table (info)->dynstr;
6571 BFD_ASSERT (dynstr != NULL);
6573 for (i = 1, namep = mips_elf_dynsym_sec_names;
6577 s = bfd_get_section_by_name (output_bfd, *namep);
6579 elf_section_data (s)->dynindx = i;
6581 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
6582 if (strindex == (bfd_size_type) -1)
6585 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
6590 c = bfd_count_sections (output_bfd);
6591 elf_link_hash_traverse (elf_hash_table (info),
6592 mips_elf_adjust_dynindx,
6594 elf_hash_table (info)->dynsymcount += c;
6596 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
6598 elf_section_data (s)->dynindx = i;
6599 /* These symbols will have no names, so we don't need to
6600 fiddle with dynstr_index. */
6607 BFD_ASSERT (elf_section_data (sgot) != NULL);
6608 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6609 BFD_ASSERT (g != NULL);
6611 /* If there are no global got symbols, fake the last symbol so
6613 if (g->global_gotsym)
6614 g->global_gotsym += c;
6616 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
6623 /* Increment the index of a dynamic symbol by a given amount. Called
6624 via elf_link_hash_traverse. */
6627 mips_elf_adjust_dynindx (h, cparg)
6628 struct elf_link_hash_entry *h;
6631 unsigned int *cp = (unsigned int *) cparg;
6633 if (h->dynindx != -1)
6638 /* Finish up dynamic symbol handling. We set the contents of various
6639 dynamic sections here. */
6642 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6644 struct bfd_link_info *info;
6645 struct elf_link_hash_entry *h;
6646 Elf_Internal_Sym *sym;
6651 struct mips_got_info *g;
6654 dynobj = elf_hash_table (info)->dynobj;
6655 gval = sym->st_value;
6657 if (h->plt_offset != (bfd_vma) -1)
6661 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
6663 /* This symbol has a stub. Set it up. */
6665 BFD_ASSERT (h->dynindx != -1);
6667 s = bfd_get_section_by_name (dynobj, ".stub");
6668 BFD_ASSERT (s != NULL);
6670 /* Fill the stub. */
6672 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
6674 bfd_put_32 (output_bfd, STUB_MOVE, p);
6677 /* FIXME: Can h->dynindex be more than 64K? */
6678 if (h->dynindx & 0xffff0000)
6681 bfd_put_32 (output_bfd, STUB_JALR, p);
6683 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
6685 BFD_ASSERT (h->plt_offset <= s->_raw_size);
6686 memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
6688 /* Mark the symbol as undefined. plt_offset != -1 occurs
6689 only for the referenced symbol. */
6690 sym->st_shndx = SHN_UNDEF;
6692 /* The run-time linker uses the st_value field of the symbol
6693 to reset the global offset table entry for this external
6694 to its stub address when unlinking a shared object. */
6695 gval = s->output_section->vma + s->output_offset + h->plt_offset;
6696 sym->st_value = gval;
6699 BFD_ASSERT (h->dynindx != -1);
6701 sgot = bfd_get_section_by_name (dynobj, ".got");
6702 BFD_ASSERT (sgot != NULL);
6703 BFD_ASSERT (elf_section_data (sgot) != NULL);
6704 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6705 BFD_ASSERT (g != NULL);
6707 if ((unsigned long) h->dynindx >= g->global_gotsym)
6709 bfd_size_type offset;
6711 /* This symbol has an entry in the global offset table. Set its
6712 value to the corresponding got entry, if needed. */
6713 if (h->got_offset == (bfd_vma) -1)
6715 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
6716 BFD_ASSERT (g->local_gotno * 4 <= offset
6717 && offset < sgot->_raw_size);
6718 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
6722 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
6723 name = h->root.root.string;
6724 if (strcmp (name, "_DYNAMIC") == 0
6725 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
6726 sym->st_shndx = SHN_ABS;
6727 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
6729 sym->st_shndx = SHN_ABS;
6730 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6733 else if (SGI_COMPAT (output_bfd))
6735 if (strcmp (name, "_gp_disp") == 0)
6737 sym->st_shndx = SHN_ABS;
6738 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6739 sym->st_value = elf_gp (output_bfd);
6741 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
6742 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
6744 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6745 sym->st_other = STO_PROTECTED;
6747 sym->st_shndx = SHN_MIPS_DATA;
6749 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
6751 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6752 sym->st_other = STO_PROTECTED;
6753 sym->st_value = mips_elf_hash_table (info)->procedure_count;
6754 sym->st_shndx = SHN_ABS;
6756 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
6758 if (h->type == STT_FUNC)
6759 sym->st_shndx = SHN_MIPS_TEXT;
6760 else if (h->type == STT_OBJECT)
6761 sym->st_shndx = SHN_MIPS_DATA;
6765 if (SGI_COMPAT (output_bfd)
6768 if (! mips_elf_hash_table (info)->use_rld_obj_head
6769 && strcmp (name, "__rld_map") == 0)
6771 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6772 BFD_ASSERT (s != NULL);
6773 sym->st_value = s->output_section->vma + s->output_offset;
6774 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
6775 if (mips_elf_hash_table (info)->rld_value == 0)
6776 mips_elf_hash_table (info)->rld_value = sym->st_value;
6778 else if (mips_elf_hash_table (info)->use_rld_obj_head
6779 && strcmp (name, "__rld_obj_head") == 0)
6781 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6782 BFD_ASSERT (s != NULL);
6783 mips_elf_hash_table (info)->rld_value = sym->st_value;
6787 /* If this is a mips16 symbol, force the value to be even. */
6788 if (sym->st_other == STO_MIPS16
6789 && (sym->st_value & 1) != 0)
6795 /* Finish up the dynamic sections. */
6798 mips_elf_finish_dynamic_sections (output_bfd, info)
6800 struct bfd_link_info *info;
6805 struct mips_got_info *g;
6807 dynobj = elf_hash_table (info)->dynobj;
6809 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6811 sgot = bfd_get_section_by_name (dynobj, ".got");
6816 BFD_ASSERT (elf_section_data (sgot) != NULL);
6817 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6818 BFD_ASSERT (g != NULL);
6821 if (elf_hash_table (info)->dynamic_sections_created)
6823 Elf32_External_Dyn *dyncon, *dynconend;
6825 BFD_ASSERT (sdyn != NULL);
6826 BFD_ASSERT (g != NULL);
6828 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6829 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6830 for (; dyncon < dynconend; dyncon++)
6832 Elf_Internal_Dyn dyn;
6837 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6845 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6846 BFD_ASSERT (s != NULL);
6847 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
6848 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6852 /* Rewrite DT_STRSZ. */
6854 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
6855 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6861 case DT_MIPS_CONFLICT:
6864 case DT_MIPS_LIBLIST:
6867 s = bfd_get_section_by_name (output_bfd, name);
6868 BFD_ASSERT (s != NULL);
6869 dyn.d_un.d_ptr = s->vma;
6870 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6873 case DT_MIPS_RLD_VERSION:
6874 dyn.d_un.d_val = 1; /* XXX */
6875 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6879 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
6880 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6883 case DT_MIPS_CONFLICTNO:
6885 elemsize = sizeof (Elf32_Conflict);
6888 case DT_MIPS_LIBLISTNO:
6890 elemsize = sizeof (Elf32_Lib);
6892 s = bfd_get_section_by_name (output_bfd, name);
6895 if (s->_cooked_size != 0)
6896 dyn.d_un.d_val = s->_cooked_size / elemsize;
6898 dyn.d_un.d_val = s->_raw_size / elemsize;
6903 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6906 case DT_MIPS_TIME_STAMP:
6907 time ((time_t *) &dyn.d_un.d_val);
6908 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6911 case DT_MIPS_ICHECKSUM:
6915 case DT_MIPS_IVERSION:
6919 case DT_MIPS_BASE_ADDRESS:
6920 s = output_bfd->sections;
6921 BFD_ASSERT (s != NULL);
6922 dyn.d_un.d_ptr = s->vma & ~(0xffff);
6923 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6926 case DT_MIPS_LOCAL_GOTNO:
6927 dyn.d_un.d_val = g->local_gotno;
6928 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6931 case DT_MIPS_SYMTABNO:
6933 elemsize = sizeof (Elf32_External_Sym);
6934 s = bfd_get_section_by_name (output_bfd, name);
6935 BFD_ASSERT (s != NULL);
6937 if (s->_cooked_size != 0)
6938 dyn.d_un.d_val = s->_cooked_size / elemsize;
6940 dyn.d_un.d_val = s->_raw_size / elemsize;
6941 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6944 case DT_MIPS_UNREFEXTNO:
6946 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
6947 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6950 case DT_MIPS_GOTSYM:
6951 dyn.d_un.d_val = g->global_gotsym;
6952 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6955 case DT_MIPS_HIPAGENO:
6956 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
6957 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6960 case DT_MIPS_RLD_MAP:
6961 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
6962 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6969 /* The first entry of the global offset table will be filled at
6970 runtime. The second entry will be used by some runtime loaders.
6971 This isn't the case of Irix rld. */
6972 if (sgot != NULL && sgot->_raw_size > 0)
6974 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
6975 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
6979 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
6986 Elf_Internal_Sym sym;
6989 const char * const * namep = mips_elf_dynsym_sec_names;
6990 Elf32_compact_rel cpt;
6992 /* Set up the section symbols for the output sections. SGI sets
6993 the STT_NOTYPE attribute for these symbols. Should we do so? */
6995 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
6996 if (sdynsym != NULL)
6998 if (SGI_COMPAT (output_bfd))
7002 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7006 while ((name = *namep++) != NULL)
7008 s = bfd_get_section_by_name (output_bfd, name);
7011 sym.st_value = s->vma;
7012 dindx = elf_section_data (s)->dynindx;
7013 last = s->vma + s->_raw_size;
7017 sym.st_value = last;
7021 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7026 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7028 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7029 (((Elf32_External_Sym *)
7034 /* Set the sh_info field of the output .dynsym section to
7035 the index of the first global symbol. */
7036 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7037 SIZEOF_MIPS_DYNSYM_SECNAMES;
7043 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7046 for (s = output_bfd->sections; s != NULL; s = s->next)
7050 sym.st_value = s->vma;
7052 indx = elf_section_data (s)->this_idx;
7053 BFD_ASSERT (indx > 0);
7054 sym.st_shndx = indx;
7056 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7057 (((Elf32_External_Sym *)
7059 + elf_section_data (s)->dynindx));
7062 /* Set the sh_info field of the output .dynsym section to
7063 the index of the first global symbol. */
7064 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7065 bfd_count_sections (output_bfd) + 1;
7069 if (SGI_COMPAT (output_bfd))
7071 /* Write .compact_rel section out. */
7072 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7076 cpt.num = s->reloc_count;
7078 cpt.offset = (s->output_section->filepos
7079 + sizeof (Elf32_External_compact_rel));
7082 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7083 ((Elf32_External_compact_rel *)
7086 /* Clean up a dummy stub function entry in .text. */
7087 s = bfd_get_section_by_name (dynobj, ".stub");
7090 file_ptr dummy_offset;
7092 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7093 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7094 memset (s->contents + dummy_offset, 0,
7095 MIPS_FUNCTION_STUB_SIZE);
7100 /* Clean up a first relocation in .rel.dyn. */
7101 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7102 if (s != NULL && s->_raw_size > 0)
7103 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7109 /* This is almost identical to bfd_generic_get_... except that some
7110 MIPS relocations need to be handled specially. Sigh. */
7113 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7114 relocateable, symbols)
7116 struct bfd_link_info *link_info;
7117 struct bfd_link_order *link_order;
7119 boolean relocateable;
7122 /* Get enough memory to hold the stuff */
7123 bfd *input_bfd = link_order->u.indirect.section->owner;
7124 asection *input_section = link_order->u.indirect.section;
7126 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7127 arelent **reloc_vector = NULL;
7133 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7134 if (reloc_vector == NULL && reloc_size != 0)
7137 /* read in the section */
7138 if (!bfd_get_section_contents (input_bfd,
7142 input_section->_raw_size))
7145 /* We're not relaxing the section, so just copy the size info */
7146 input_section->_cooked_size = input_section->_raw_size;
7147 input_section->reloc_done = true;
7149 reloc_count = bfd_canonicalize_reloc (input_bfd,
7153 if (reloc_count < 0)
7156 if (reloc_count > 0)
7161 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7164 struct bfd_hash_entry *h;
7165 struct bfd_link_hash_entry *lh;
7166 /* Skip all this stuff if we aren't mixing formats. */
7167 if (abfd && input_bfd
7168 && abfd->xvec == input_bfd->xvec)
7172 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7173 lh = (struct bfd_link_hash_entry *) h;
7180 case bfd_link_hash_undefined:
7181 case bfd_link_hash_undefweak:
7182 case bfd_link_hash_common:
7185 case bfd_link_hash_defined:
7186 case bfd_link_hash_defweak:
7188 gp = lh->u.def.value;
7190 case bfd_link_hash_indirect:
7191 case bfd_link_hash_warning:
7193 /* @@FIXME ignoring warning for now */
7195 case bfd_link_hash_new:
7204 for (parent = reloc_vector; *parent != (arelent *) NULL;
7207 char *error_message = (char *) NULL;
7208 bfd_reloc_status_type r;
7210 /* Specific to MIPS: Deal with relocation types that require
7211 knowing the gp of the output bfd. */
7212 asymbol *sym = *(*parent)->sym_ptr_ptr;
7213 if (bfd_is_abs_section (sym->section) && abfd)
7215 /* The special_function wouldn't get called anyways. */
7219 /* The gp isn't there; let the special function code
7220 fall over on its own. */
7222 else if ((*parent)->howto->special_function
7223 == _bfd_mips_elf_gprel16_reloc)
7225 /* bypass special_function call */
7226 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7227 relocateable, (PTR) data, gp);
7228 goto skip_bfd_perform_relocation;
7230 /* end mips specific stuff */
7232 r = bfd_perform_relocation (input_bfd,
7236 relocateable ? abfd : (bfd *) NULL,
7238 skip_bfd_perform_relocation:
7242 asection *os = input_section->output_section;
7244 /* A partial link, so keep the relocs */
7245 os->orelocation[os->reloc_count] = *parent;
7249 if (r != bfd_reloc_ok)
7253 case bfd_reloc_undefined:
7254 if (!((*link_info->callbacks->undefined_symbol)
7255 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7256 input_bfd, input_section, (*parent)->address)))
7259 case bfd_reloc_dangerous:
7260 BFD_ASSERT (error_message != (char *) NULL);
7261 if (!((*link_info->callbacks->reloc_dangerous)
7262 (link_info, error_message, input_bfd, input_section,
7263 (*parent)->address)))
7266 case bfd_reloc_overflow:
7267 if (!((*link_info->callbacks->reloc_overflow)
7268 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7269 (*parent)->howto->name, (*parent)->addend,
7270 input_bfd, input_section, (*parent)->address)))
7273 case bfd_reloc_outofrange:
7282 if (reloc_vector != NULL)
7283 free (reloc_vector);
7287 if (reloc_vector != NULL)
7288 free (reloc_vector);
7291 #define bfd_elf32_bfd_get_relocated_section_contents \
7292 elf32_mips_get_relocated_section_contents
7294 /* ECOFF swapping routines. These are used when dealing with the
7295 .mdebug section, which is in the ECOFF debugging format. */
7296 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7298 /* Symbol table magic number. */
7300 /* Alignment of debugging information. E.g., 4. */
7302 /* Sizes of external symbolic information. */
7303 sizeof (struct hdr_ext),
7304 sizeof (struct dnr_ext),
7305 sizeof (struct pdr_ext),
7306 sizeof (struct sym_ext),
7307 sizeof (struct opt_ext),
7308 sizeof (struct fdr_ext),
7309 sizeof (struct rfd_ext),
7310 sizeof (struct ext_ext),
7311 /* Functions to swap in external symbolic data. */
7320 _bfd_ecoff_swap_tir_in,
7321 _bfd_ecoff_swap_rndx_in,
7322 /* Functions to swap out external symbolic data. */
7331 _bfd_ecoff_swap_tir_out,
7332 _bfd_ecoff_swap_rndx_out,
7333 /* Function to read in symbolic data. */
7334 _bfd_mips_elf_read_ecoff_info
7337 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7338 #define TARGET_LITTLE_NAME "elf32-littlemips"
7339 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7340 #define TARGET_BIG_NAME "elf32-bigmips"
7341 #define ELF_ARCH bfd_arch_mips
7342 #define ELF_MACHINE_CODE EM_MIPS
7344 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7345 a value of 0x1000, and we are compatible. */
7346 #define ELF_MAXPAGESIZE 0x1000
7348 #define elf_backend_collect true
7349 #define elf_backend_type_change_ok true
7350 #define elf_info_to_howto 0
7351 #define elf_info_to_howto_rel mips_info_to_howto_rel
7352 #define elf_backend_sym_is_global mips_elf_sym_is_global
7353 #define elf_backend_object_p mips_elf32_object_p
7354 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7355 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7356 #define elf_backend_section_from_bfd_section \
7357 _bfd_mips_elf_section_from_bfd_section
7358 #define elf_backend_section_processing mips_elf32_section_processing
7359 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7360 #define elf_backend_additional_program_headers \
7361 mips_elf_additional_program_headers
7362 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7363 #define elf_backend_final_write_processing \
7364 _bfd_mips_elf_final_write_processing
7365 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7367 #define bfd_elf32_bfd_is_local_label_name \
7368 mips_elf_is_local_label_name
7369 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7370 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7371 #define bfd_elf32_bfd_link_hash_table_create \
7372 mips_elf_link_hash_table_create
7373 #define bfd_elf32_bfd_final_link mips_elf_final_link
7374 #define bfd_elf32_bfd_copy_private_bfd_data \
7375 _bfd_mips_elf_copy_private_bfd_data
7376 #define bfd_elf32_bfd_merge_private_bfd_data \
7377 _bfd_mips_elf_merge_private_bfd_data
7378 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7379 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7380 #define elf_backend_create_dynamic_sections \
7381 mips_elf_create_dynamic_sections
7382 #define elf_backend_check_relocs mips_elf_check_relocs
7383 #define elf_backend_adjust_dynamic_symbol \
7384 mips_elf_adjust_dynamic_symbol
7385 #define elf_backend_always_size_sections \
7386 mips_elf_always_size_sections
7387 #define elf_backend_size_dynamic_sections \
7388 mips_elf_size_dynamic_sections
7389 #define elf_backend_relocate_section mips_elf_relocate_section
7390 #define elf_backend_link_output_symbol_hook \
7391 mips_elf_link_output_symbol_hook
7392 #define elf_backend_finish_dynamic_symbol \
7393 mips_elf_finish_dynamic_symbol
7394 #define elf_backend_finish_dynamic_sections \
7395 mips_elf_finish_dynamic_sections
7397 #include "elf32-target.h"