1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996 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 mips_elf_hi16_reloc
PARAMS ((bfd
*abfd
,
51 static bfd_reloc_status_type mips_elf_got16_reloc
PARAMS ((bfd
*abfd
,
58 static bfd_reloc_status_type mips_elf_lo16_reloc
PARAMS ((bfd
*abfd
,
65 static bfd_reloc_status_type mips_elf_gprel16_reloc
PARAMS ((bfd
*abfd
,
72 static bfd_reloc_status_type mips_elf_gprel32_reloc
PARAMS ((bfd
*abfd
,
79 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
80 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
81 static void mips_info_to_howto_rel
82 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
83 static void bfd_mips_elf32_swap_gptab_in
84 PARAMS ((bfd
*, const Elf32_External_gptab
*, Elf32_gptab
*));
85 static void bfd_mips_elf32_swap_gptab_out
86 PARAMS ((bfd
*, const Elf32_gptab
*, Elf32_External_gptab
*));
87 static boolean mips_elf_sym_is_global
PARAMS ((bfd
*, asymbol
*));
88 static boolean mips_elf_object_p
PARAMS ((bfd
*));
89 static boolean mips_elf_create_procedure_table
90 PARAMS ((PTR
, bfd
*, struct bfd_link_info
*, asection
*,
91 struct ecoff_debug_info
*));
92 static int mips_elf_additional_program_headers
PARAMS ((bfd
*));
93 static boolean mips_elf_modify_segment_map
PARAMS ((bfd
*));
94 static void mips_elf_final_write_processing
95 PARAMS ((bfd
*, boolean
));
96 static boolean mips_elf_set_private_flags
PARAMS ((bfd
*, flagword
));
97 static boolean mips_elf_copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
98 static boolean mips_elf_merge_private_bfd_data
PARAMS ((bfd
*, bfd
*));
99 static boolean mips_elf_section_from_shdr
100 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, char *));
101 static boolean mips_elf_fake_sections
102 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*));
103 static boolean mips_elf_section_from_bfd_section
104 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*, int *));
105 static boolean mips_elf_section_processing
106 PARAMS ((bfd
*, Elf32_Internal_Shdr
*));
107 static void mips_elf_symbol_processing
PARAMS ((bfd
*, asymbol
*));
108 static boolean mips_elf_read_ecoff_info
109 PARAMS ((bfd
*, asection
*, struct ecoff_debug_info
*));
110 static boolean mips_elf_is_local_label
111 PARAMS ((bfd
*, asymbol
*));
112 static boolean mips_elf_find_nearest_line
113 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_vma
, const char **,
114 const char **, unsigned int *));
115 static struct bfd_hash_entry
*mips_elf_link_hash_newfunc
116 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
117 static struct bfd_link_hash_table
*mips_elf_link_hash_table_create
119 static int gptab_compare
PARAMS ((const void *, const void *));
120 static boolean mips_elf_final_link
121 PARAMS ((bfd
*, struct bfd_link_info
*));
122 static void mips_elf_relocate_hi16
123 PARAMS ((bfd
*, Elf_Internal_Rela
*, Elf_Internal_Rela
*, bfd_byte
*,
125 static void mips_elf_relocate_got_local
126 PARAMS ((bfd
*, bfd
*, asection
*, Elf_Internal_Rela
*,
127 Elf_Internal_Rela
*, bfd_byte
*, bfd_vma
));
128 static void mips_elf_relocate_global_got
129 PARAMS ((bfd
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_vma
));
130 static boolean mips_elf_adjust_dynindx
131 PARAMS ((struct elf_link_hash_entry
*, PTR
));
132 static boolean mips_elf_relocate_section
133 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
134 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
135 static boolean mips_elf_create_dynamic_sections
136 PARAMS ((bfd
*, struct bfd_link_info
*));
137 static boolean mips_elf_create_compact_rel_section
138 PARAMS ((bfd
*, struct bfd_link_info
*));
139 static boolean mips_elf_create_got_section
140 PARAMS ((bfd
*, struct bfd_link_info
*));
141 static boolean mips_elf_check_relocs
142 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
143 const Elf_Internal_Rela
*));
144 static boolean mips_elf_adjust_dynamic_symbol
145 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
146 static boolean mips_elf_size_dynamic_sections
147 PARAMS ((bfd
*, struct bfd_link_info
*));
148 static boolean mips_elf_finish_dynamic_symbol
149 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
150 Elf_Internal_Sym
*));
151 static boolean mips_elf_finish_dynamic_sections
152 PARAMS ((bfd
*, struct bfd_link_info
*));
153 static boolean mips_elf_add_symbol_hook
154 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
155 const char **, flagword
*, asection
**, bfd_vma
*));
156 static bfd_reloc_status_type mips_elf_final_gp
157 PARAMS ((bfd
*, asymbol
*, boolean
, char **));
158 static bfd_byte
*elf32_mips_get_relocated_section_contents
159 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
160 bfd_byte
*, boolean
, asymbol
**));
162 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
163 executables. FIXME: At the moment, we default to always generating
164 Irix 5 executables. */
166 #define SGI_COMPAT(abfd) (1)
168 /* This structure is used to hold .got information when linking. It
169 is stored in the tdata field of the bfd_elf_section_data structure. */
173 /* The symbol index of the first global .got symbol. */
174 unsigned long global_gotsym
;
175 /* The number of local .got entries. */
176 unsigned int local_gotno
;
179 /* The number of local .got entries we reserve. */
180 #define MIPS_RESERVED_GOTNO (2)
182 /* Instructions which appear in a stub. For some reason the stub is
183 slightly different on an SGI system. */
184 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
185 #define STUB_LW(abfd) \
187 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
188 : 0x8f998000) /* lw t9,0x8000(gp) */
189 #define STUB_MOVE 0x03e07825 /* move t7,ra */
190 #define STUB_JALR 0x0320f809 /* jal t9 */
191 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
192 #define MIPS_FUNCTION_STUB_SIZE (16)
194 /* Names of sections which appear in the .dynsym section in an Irix 5
197 static const char * const mips_elf_dynsym_sec_names
[] =
210 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
211 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
213 /* The number of entries in mips_elf_dynsym_sec_names which go in the
216 #define MIPS_TEXT_DYNSYM_SECNO (3)
218 /* The names of the runtime procedure table symbols used on Irix 5. */
220 static const char * const mips_elf_dynsym_rtproc_names
[] =
223 "_procedure_string_table",
224 "_procedure_table_size",
228 /* These structures are used to generate the .compact_rel section on
233 unsigned long id1
; /* Always one? */
234 unsigned long num
; /* Number of compact relocation entries. */
235 unsigned long id2
; /* Always two? */
236 unsigned long offset
; /* The file offset of the first relocation. */
237 unsigned long reserved0
; /* Zero? */
238 unsigned long reserved1
; /* Zero? */
247 bfd_byte reserved0
[4];
248 bfd_byte reserved1
[4];
249 } Elf32_External_compact_rel
;
253 unsigned int ctype
: 1; /* 1: long 0: short format. See below. */
254 unsigned int rtype
: 4; /* Relocation types. See below. */
255 unsigned int dist2to
: 8;
256 unsigned int relvaddr
: 19; /* (VADDR - vaddr of the previous entry)/ 4 */
257 unsigned long konst
; /* KONST field. See below. */
258 unsigned long vaddr
; /* VADDR to be relocated. */
263 unsigned int ctype
: 1; /* 1: long 0: short format. See below. */
264 unsigned int rtype
: 4; /* Relocation types. See below. */
265 unsigned int dist2to
: 8;
266 unsigned int relvaddr
: 19; /* (VADDR - vaddr of the previous entry)/ 4 */
267 unsigned long konst
; /* KONST field. See below. */
275 } Elf32_External_crinfo
;
281 } Elf32_External_crinfo2
;
283 /* These are the constants used to swap the bitfields in a crinfo. */
285 #define CRINFO_CTYPE (0x1)
286 #define CRINFO_CTYPE_SH (31)
287 #define CRINFO_RTYPE (0xf)
288 #define CRINFO_RTYPE_SH (27)
289 #define CRINFO_DIST2TO (0xff)
290 #define CRINFO_DIST2TO_SH (19)
291 #define CRINFO_RELVADDR (0x7ffff)
292 #define CRINFO_RELVADDR_SH (0)
294 /* A compact relocation info has long (3 words) or short (2 words)
295 formats. A short format doesn't have VADDR field and relvaddr
296 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
297 #define CRF_MIPS_LONG 1
298 #define CRF_MIPS_SHORT 0
300 /* There are 4 types of compact relocation at least. The value KONST
301 has different meaning for each type:
304 CT_MIPS_REL32 Address in data
305 CT_MIPS_WORD Address in word (XXX)
306 CT_MIPS_GPHI_LO GP - vaddr
307 CT_MIPS_JMPAD Address to jump
310 #define CRT_MIPS_REL32 0xa
311 #define CRT_MIPS_WORD 0xb
312 #define CRT_MIPS_GPHI_LO 0xc
313 #define CRT_MIPS_JMPAD 0xd
315 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
316 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
317 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
318 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
320 static void bfd_elf32_swap_compact_rel_out
321 PARAMS ((bfd
*, const Elf32_compact_rel
*, Elf32_External_compact_rel
*));
322 static void bfd_elf32_swap_crinfo_out
323 PARAMS ((bfd
*, const Elf32_crinfo
*, Elf32_External_crinfo
*));
325 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
330 R_MIPS_16
, R_MIPS_32
,
331 R_MIPS_REL32
, R_MIPS_26
,
332 R_MIPS_HI16
, R_MIPS_LO16
,
333 R_MIPS_GPREL16
, R_MIPS_LITERAL
,
334 R_MIPS_GOT16
, R_MIPS_PC16
,
335 R_MIPS_CALL16
, R_MIPS_GPREL32
,
336 /* The remaining relocs are defined on Irix, although they are not
337 in the MIPS ELF ABI. */
338 R_MIPS_UNUSED1
, R_MIPS_UNUSED2
,
340 R_MIPS_SHIFT5
, R_MIPS_SHIFT6
,
341 R_MIPS_64
, R_MIPS_GOT_DISP
,
342 R_MIPS_GOT_PAGE
, R_MIPS_GOT_OFST
,
343 R_MIPS_GOT_HI16
, R_MIPS_GOT_LO16
,
344 R_MIPS_SUB
, R_MIPS_INSERT_A
,
345 R_MIPS_INSERT_B
, R_MIPS_DELETE
,
346 R_MIPS_HIGHER
, R_MIPS_HIGHEST
,
347 R_MIPS_CALL_HI16
, R_MIPS_CALL_LO16
,
351 static reloc_howto_type elf_mips_howto_table
[] =
354 HOWTO (R_MIPS_NONE
, /* type */
356 0, /* size (0 = byte, 1 = short, 2 = long) */
358 false, /* pc_relative */
360 complain_overflow_dont
, /* complain_on_overflow */
361 bfd_elf_generic_reloc
, /* special_function */
362 "R_MIPS_NONE", /* name */
363 false, /* partial_inplace */
366 false), /* pcrel_offset */
368 /* 16 bit relocation. */
369 HOWTO (R_MIPS_16
, /* type */
371 1, /* size (0 = byte, 1 = short, 2 = long) */
373 false, /* pc_relative */
375 complain_overflow_bitfield
, /* complain_on_overflow */
376 bfd_elf_generic_reloc
, /* special_function */
377 "R_MIPS_16", /* name */
378 true, /* partial_inplace */
379 0xffff, /* src_mask */
380 0xffff, /* dst_mask */
381 false), /* pcrel_offset */
383 /* 32 bit relocation. */
384 HOWTO (R_MIPS_32
, /* type */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
388 false, /* pc_relative */
390 complain_overflow_bitfield
, /* complain_on_overflow */
391 bfd_elf_generic_reloc
, /* special_function */
392 "R_MIPS_32", /* name */
393 true, /* partial_inplace */
394 0xffffffff, /* src_mask */
395 0xffffffff, /* dst_mask */
396 false), /* pcrel_offset */
398 /* 32 bit symbol relative relocation. */
399 HOWTO (R_MIPS_REL32
, /* type */
401 2, /* size (0 = byte, 1 = short, 2 = long) */
403 false, /* pc_relative */
405 complain_overflow_bitfield
, /* complain_on_overflow */
406 bfd_elf_generic_reloc
, /* special_function */
407 "R_MIPS_REL32", /* name */
408 true, /* partial_inplace */
409 0xffffffff, /* src_mask */
410 0xffffffff, /* dst_mask */
411 false), /* pcrel_offset */
413 /* 26 bit branch address. */
414 HOWTO (R_MIPS_26
, /* type */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
418 false, /* pc_relative */
420 complain_overflow_dont
, /* complain_on_overflow */
421 /* This needs complex overflow
422 detection, because the upper four
423 bits must match the PC. */
424 bfd_elf_generic_reloc
, /* special_function */
425 "R_MIPS_26", /* name */
426 true, /* partial_inplace */
427 0x3ffffff, /* src_mask */
428 0x3ffffff, /* dst_mask */
429 false), /* pcrel_offset */
431 /* High 16 bits of symbol value. */
432 HOWTO (R_MIPS_HI16
, /* type */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
436 false, /* pc_relative */
438 complain_overflow_dont
, /* complain_on_overflow */
439 mips_elf_hi16_reloc
, /* special_function */
440 "R_MIPS_HI16", /* name */
441 true, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 false), /* pcrel_offset */
446 /* Low 16 bits of symbol value. */
447 HOWTO (R_MIPS_LO16
, /* type */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
451 false, /* pc_relative */
453 complain_overflow_dont
, /* complain_on_overflow */
454 mips_elf_lo16_reloc
, /* special_function */
455 "R_MIPS_LO16", /* name */
456 true, /* partial_inplace */
457 0xffff, /* src_mask */
458 0xffff, /* dst_mask */
459 false), /* pcrel_offset */
461 /* GP relative reference. */
462 HOWTO (R_MIPS_GPREL16
, /* type */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
466 false, /* pc_relative */
468 complain_overflow_signed
, /* complain_on_overflow */
469 mips_elf_gprel16_reloc
, /* special_function */
470 "R_MIPS_GPREL16", /* name */
471 true, /* partial_inplace */
472 0xffff, /* src_mask */
473 0xffff, /* dst_mask */
474 false), /* pcrel_offset */
476 /* Reference to literal section. */
477 HOWTO (R_MIPS_LITERAL
, /* type */
479 2, /* size (0 = byte, 1 = short, 2 = long) */
481 false, /* pc_relative */
483 complain_overflow_signed
, /* complain_on_overflow */
484 mips_elf_gprel16_reloc
, /* special_function */
485 "R_MIPS_LITERAL", /* name */
486 true, /* partial_inplace */
487 0xffff, /* src_mask */
488 0xffff, /* dst_mask */
489 false), /* pcrel_offset */
491 /* Reference to global offset table. */
492 HOWTO (R_MIPS_GOT16
, /* type */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
496 false, /* pc_relative */
498 complain_overflow_signed
, /* complain_on_overflow */
499 mips_elf_got16_reloc
, /* special_function */
500 "R_MIPS_GOT16", /* name */
501 false, /* partial_inplace */
503 0xffff, /* dst_mask */
504 false), /* pcrel_offset */
506 /* 16 bit PC relative reference. */
507 HOWTO (R_MIPS_PC16
, /* type */
509 2, /* size (0 = byte, 1 = short, 2 = long) */
511 true, /* pc_relative */
513 complain_overflow_signed
, /* complain_on_overflow */
514 bfd_elf_generic_reloc
, /* special_function */
515 "R_MIPS_PC16", /* name */
516 true, /* partial_inplace */
517 0xffff, /* src_mask */
518 0xffff, /* dst_mask */
519 false), /* pcrel_offset */
521 /* 16 bit call through global offset table. */
522 /* FIXME: This is not handled correctly. */
523 HOWTO (R_MIPS_CALL16
, /* type */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
527 false, /* pc_relative */
529 complain_overflow_signed
, /* complain_on_overflow */
530 bfd_elf_generic_reloc
, /* special_function */
531 "R_MIPS_CALL16", /* name */
532 false, /* partial_inplace */
534 0xffff, /* dst_mask */
535 false), /* pcrel_offset */
537 /* 32 bit GP relative reference. */
538 HOWTO (R_MIPS_GPREL32
, /* type */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
542 false, /* pc_relative */
544 complain_overflow_bitfield
, /* complain_on_overflow */
545 mips_elf_gprel32_reloc
, /* special_function */
546 "R_MIPS_GPREL32", /* name */
547 true, /* partial_inplace */
548 0xffffffff, /* src_mask */
549 0xffffffff, /* dst_mask */
550 false), /* pcrel_offset */
552 /* The remaining relocs are defined on Irix 5, although they are
553 not defined by the ABI. */
558 /* A 5 bit shift field. */
559 HOWTO (R_MIPS_SHIFT5
, /* type */
561 2, /* size (0 = byte, 1 = short, 2 = long) */
563 false, /* pc_relative */
565 complain_overflow_bitfield
, /* complain_on_overflow */
566 bfd_elf_generic_reloc
, /* special_function */
567 "R_MIPS_SHIFT5", /* name */
568 true, /* partial_inplace */
569 0x000007c0, /* src_mask */
570 0x000007c0, /* dst_mask */
571 false), /* pcrel_offset */
573 /* A 6 bit shift field. */
574 /* FIXME: This is not handled correctly; a special function is
575 needed to put the most significant bit in the right place. */
576 HOWTO (R_MIPS_SHIFT6
, /* type */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
580 false, /* pc_relative */
582 complain_overflow_bitfield
, /* complain_on_overflow */
583 bfd_elf_generic_reloc
, /* special_function */
584 "R_MIPS_SHIFT6", /* name */
585 true, /* partial_inplace */
586 0x000007c4, /* src_mask */
587 0x000007c4, /* dst_mask */
588 false), /* pcrel_offset */
590 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
593 /* Displacement in the global offset table. */
594 /* FIXME: Not handled correctly. */
595 HOWTO (R_MIPS_GOT_DISP
, /* type */
597 2, /* size (0 = byte, 1 = short, 2 = long) */
599 false, /* pc_relative */
601 complain_overflow_bitfield
, /* complain_on_overflow */
602 bfd_elf_generic_reloc
, /* special_function */
603 "R_MIPS_GOT_DISP", /* name */
604 true, /* partial_inplace */
605 0x0000ffff, /* src_mask */
606 0x0000ffff, /* dst_mask */
607 false), /* pcrel_offset */
609 /* Displacement to page pointer in the global offset table. */
610 /* FIXME: Not handled correctly. */
611 HOWTO (R_MIPS_GOT_PAGE
, /* type */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
615 false, /* pc_relative */
617 complain_overflow_bitfield
, /* complain_on_overflow */
618 bfd_elf_generic_reloc
, /* special_function */
619 "R_MIPS_GOT_PAGE", /* name */
620 true, /* partial_inplace */
621 0x0000ffff, /* src_mask */
622 0x0000ffff, /* dst_mask */
623 false), /* pcrel_offset */
625 /* Offset from page pointer in the global offset table. */
626 /* FIXME: Not handled correctly. */
627 HOWTO (R_MIPS_GOT_OFST
, /* type */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
631 false, /* pc_relative */
633 complain_overflow_bitfield
, /* complain_on_overflow */
634 bfd_elf_generic_reloc
, /* special_function */
635 "R_MIPS_GOT_OFST", /* name */
636 true, /* partial_inplace */
637 0x0000ffff, /* src_mask */
638 0x0000ffff, /* dst_mask */
639 false), /* pcrel_offset */
641 /* High 16 bits of displacement in global offset table. */
642 /* FIXME: Not handled correctly. */
643 HOWTO (R_MIPS_GOT_HI16
, /* type */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
647 false, /* pc_relative */
649 complain_overflow_dont
, /* complain_on_overflow */
650 bfd_elf_generic_reloc
, /* special_function */
651 "R_MIPS_GOT_HI16", /* name */
652 true, /* partial_inplace */
653 0x0000ffff, /* src_mask */
654 0x0000ffff, /* dst_mask */
655 false), /* pcrel_offset */
657 /* Low 16 bits of displacement in global offset table. */
658 /* FIXME: Not handled correctly. */
659 HOWTO (R_MIPS_GOT_LO16
, /* type */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
663 false, /* pc_relative */
665 complain_overflow_dont
, /* complain_on_overflow */
666 bfd_elf_generic_reloc
, /* special_function */
667 "R_MIPS_GOT_LO16", /* name */
668 true, /* partial_inplace */
669 0x0000ffff, /* src_mask */
670 0x0000ffff, /* dst_mask */
671 false), /* pcrel_offset */
673 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
676 /* Used to cause the linker to insert and delete instructions? */
681 /* Get the higher values of a 64 bit addend. Presumably not used in
686 /* High 16 bits of displacement in global offset table. */
687 /* FIXME: Not handled correctly. */
688 HOWTO (R_MIPS_CALL_HI16
, /* type */
690 2, /* size (0 = byte, 1 = short, 2 = long) */
692 false, /* pc_relative */
694 complain_overflow_dont
, /* complain_on_overflow */
695 bfd_elf_generic_reloc
, /* special_function */
696 "R_MIPS_CALL_HI16", /* name */
697 true, /* partial_inplace */
698 0x0000ffff, /* src_mask */
699 0x0000ffff, /* dst_mask */
700 false), /* pcrel_offset */
702 /* Low 16 bits of displacement in global offset table. */
703 /* FIXME: Not handled correctly. */
704 HOWTO (R_MIPS_CALL_LO16
, /* type */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
708 false, /* pc_relative */
710 complain_overflow_dont
, /* complain_on_overflow */
711 bfd_elf_generic_reloc
, /* special_function */
712 "R_MIPS_CALL_LO16", /* name */
713 true, /* partial_inplace */
714 0x0000ffff, /* src_mask */
715 0x0000ffff, /* dst_mask */
716 false) /* pcrel_offset */
719 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
720 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
721 the HI16. Here we just save the information we need; we do the
722 actual relocation when we see the LO16. MIPS ELF requires that the
723 LO16 immediately follow the HI16, so this ought to work. */
725 static bfd_byte
*mips_hi16_addr
;
726 static bfd_vma mips_hi16_addend
;
728 static bfd_reloc_status_type
729 mips_elf_hi16_reloc (abfd
,
737 arelent
*reloc_entry
;
740 asection
*input_section
;
742 char **error_message
;
744 bfd_reloc_status_type ret
;
747 /* If we're relocating, and this an external symbol, we don't want
748 to change anything. */
749 if (output_bfd
!= (bfd
*) NULL
750 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
751 && reloc_entry
->addend
== 0)
753 reloc_entry
->address
+= input_section
->output_offset
;
759 if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
761 boolean relocateable
;
763 if (ret
== bfd_reloc_undefined
)
766 if (output_bfd
!= NULL
)
770 relocateable
= false;
771 output_bfd
= symbol
->section
->output_section
->owner
;
774 ret
= mips_elf_final_gp (output_bfd
, symbol
, relocateable
,
776 if (ret
!= bfd_reloc_ok
)
779 relocation
= elf_gp (output_bfd
) - reloc_entry
->address
;
783 if (bfd_is_und_section (symbol
->section
)
784 && output_bfd
== (bfd
*) NULL
)
785 ret
= bfd_reloc_undefined
;
787 if (bfd_is_com_section (symbol
->section
))
790 relocation
= symbol
->value
;
793 relocation
+= symbol
->section
->output_section
->vma
;
794 relocation
+= symbol
->section
->output_offset
;
795 relocation
+= reloc_entry
->addend
;
797 if (reloc_entry
->address
> input_section
->_cooked_size
)
798 return bfd_reloc_outofrange
;
800 /* Save the information, and let LO16 do the actual relocation. */
801 mips_hi16_addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
802 mips_hi16_addend
= relocation
;
804 if (output_bfd
!= (bfd
*) NULL
)
805 reloc_entry
->address
+= input_section
->output_offset
;
810 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
811 inplace relocation; this function exists in order to do the
812 R_MIPS_HI16 relocation described above. */
814 static bfd_reloc_status_type
815 mips_elf_lo16_reloc (abfd
,
823 arelent
*reloc_entry
;
826 asection
*input_section
;
828 char **error_message
;
830 arelent gp_disp_relent
;
832 if (mips_hi16_addr
!= (bfd_byte
*) NULL
)
838 /* Do the HI16 relocation. Note that we actually don't need to
839 know anything about the LO16 itself, except where to find the
840 low 16 bits of the addend needed by the LO16. */
841 insn
= bfd_get_32 (abfd
, mips_hi16_addr
);
842 vallo
= (bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
844 val
= ((insn
& 0xffff) << 16) + vallo
;
845 val
+= mips_hi16_addend
;
847 /* The low order 16 bits are always treated as a signed value.
848 Therefore, a negative value in the low order bits requires an
849 adjustment in the high order bits. We need to make this
850 adjustment in two ways: once for the bits we took from the
851 data, and once for the bits we are putting back in to the
853 if ((vallo
& 0x8000) != 0)
855 if ((val
& 0x8000) != 0)
858 insn
= (insn
&~ 0xffff) | ((val
>> 16) & 0xffff);
859 bfd_put_32 (abfd
, insn
, mips_hi16_addr
);
861 mips_hi16_addr
= (bfd_byte
*) NULL
;
863 if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
865 gp_disp_relent
= *reloc_entry
;
866 reloc_entry
= &gp_disp_relent
;
867 reloc_entry
->addend
= mips_hi16_addend
;
870 else if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
872 bfd_reloc_status_type ret
;
875 /* FIXME: Does this case ever occur? */
877 ret
= mips_elf_final_gp (output_bfd
, symbol
, true, error_message
);
878 if (ret
!= bfd_reloc_ok
)
881 relocation
= elf_gp (output_bfd
) - reloc_entry
->address
;
882 relocation
+= symbol
->section
->output_section
->vma
;
883 relocation
+= symbol
->section
->output_offset
;
884 relocation
+= reloc_entry
->addend
;
886 if (reloc_entry
->address
> input_section
->_cooked_size
)
887 return bfd_reloc_outofrange
;
889 gp_disp_relent
= *reloc_entry
;
890 reloc_entry
= &gp_disp_relent
;
891 reloc_entry
->addend
= relocation
- 4;
894 /* Now do the LO16 reloc in the usual way. */
895 return bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
896 input_section
, output_bfd
, error_message
);
899 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
900 table used for PIC code. If the symbol is an external symbol, the
901 instruction is modified to contain the offset of the appropriate
902 entry in the global offset table. If the symbol is a section
903 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
904 addends are combined to form the real addend against the section
905 symbol; the GOT16 is modified to contain the offset of an entry in
906 the global offset table, and the LO16 is modified to offset it
907 appropriately. Thus an offset larger than 16 bits requires a
908 modified value in the global offset table.
910 This implementation suffices for the assembler, but the linker does
911 not yet know how to create global offset tables. */
913 static bfd_reloc_status_type
914 mips_elf_got16_reloc (abfd
,
922 arelent
*reloc_entry
;
925 asection
*input_section
;
927 char **error_message
;
929 /* If we're relocating, and this an external symbol, we don't want
930 to change anything. */
931 if (output_bfd
!= (bfd
*) NULL
932 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
933 && reloc_entry
->addend
== 0)
935 reloc_entry
->address
+= input_section
->output_offset
;
939 /* If we're relocating, and this is a local symbol, we can handle it
941 if (output_bfd
!= (bfd
*) NULL
942 && (symbol
->flags
& BSF_SECTION_SYM
) != 0)
943 return mips_elf_hi16_reloc (abfd
, reloc_entry
, symbol
, data
,
944 input_section
, output_bfd
, error_message
);
949 /* We have to figure out the gp value, so that we can adjust the
950 symbol value correctly. We look up the symbol _gp in the output
951 BFD. If we can't find it, we're stuck. We cache it in the ELF
952 target data. We don't need to adjust the symbol value for an
953 external symbol if we are producing relocateable output. */
955 static bfd_reloc_status_type
956 mips_elf_final_gp (output_bfd
, symbol
, relocateable
, error_message
)
959 boolean relocateable
;
960 char **error_message
;
962 if (bfd_is_und_section (symbol
->section
)
964 return bfd_reloc_undefined
;
966 /* This doesn't work if the BFD is not ELF. */
967 if (output_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
970 if (elf_gp (output_bfd
) == 0
972 || (symbol
->flags
& BSF_SECTION_SYM
) != 0))
976 /* Make up a value. */
977 elf_gp (output_bfd
) =
978 symbol
->section
->output_section
->vma
+ 0x4000;
986 count
= bfd_get_symcount (output_bfd
);
987 sym
= bfd_get_outsymbols (output_bfd
);
989 if (sym
== (asymbol
**) NULL
)
993 for (i
= 0; i
< count
; i
++, sym
++)
995 register CONST
char *name
;
997 name
= bfd_asymbol_name (*sym
);
998 if (*name
== '_' && strcmp (name
, "_gp") == 0)
1000 elf_gp (output_bfd
) = bfd_asymbol_value (*sym
);
1008 /* Only get the error once. */
1009 elf_gp (output_bfd
) = 4;
1011 (char *) "GP relative relocation when _gp not defined";
1012 return bfd_reloc_dangerous
;
1017 return bfd_reloc_ok
;
1020 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1021 become the offset from the gp register. This function also handles
1022 R_MIPS_LITERAL relocations, although those can be handled more
1023 cleverly because the entries in the .lit8 and .lit4 sections can be
1026 static bfd_reloc_status_type gprel16_with_gp
PARAMS ((bfd
*, asymbol
*,
1027 arelent
*, asection
*,
1028 boolean
, PTR
, bfd_vma
));
1030 static bfd_reloc_status_type
1031 mips_elf_gprel16_reloc (abfd
,
1039 arelent
*reloc_entry
;
1042 asection
*input_section
;
1044 char **error_message
;
1046 boolean relocateable
;
1047 bfd_reloc_status_type ret
;
1049 /* If we're relocating, and this is an external symbol with no
1050 addend, we don't want to change anything. We will only have an
1051 addend if this is a newly created reloc, not read from an ELF
1053 if (output_bfd
!= (bfd
*) NULL
1054 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1055 && reloc_entry
->addend
== 0)
1057 reloc_entry
->address
+= input_section
->output_offset
;
1058 return bfd_reloc_ok
;
1061 if (output_bfd
!= (bfd
*) NULL
)
1062 relocateable
= true;
1065 relocateable
= false;
1066 output_bfd
= symbol
->section
->output_section
->owner
;
1069 ret
= mips_elf_final_gp (output_bfd
, symbol
, relocateable
, error_message
);
1070 if (ret
!= bfd_reloc_ok
)
1073 return gprel16_with_gp (abfd
, symbol
, reloc_entry
, input_section
,
1074 relocateable
, data
, elf_gp (output_bfd
));
1077 static bfd_reloc_status_type
1078 gprel16_with_gp (abfd
, symbol
, reloc_entry
, input_section
, relocateable
, data
,
1082 arelent
*reloc_entry
;
1083 asection
*input_section
;
1084 boolean relocateable
;
1092 if (bfd_is_com_section (symbol
->section
))
1095 relocation
= symbol
->value
;
1097 relocation
+= symbol
->section
->output_section
->vma
;
1098 relocation
+= symbol
->section
->output_offset
;
1100 if (reloc_entry
->address
> input_section
->_cooked_size
)
1101 return bfd_reloc_outofrange
;
1103 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
1105 /* Set val to the offset into the section or symbol. */
1106 val
= ((insn
& 0xffff) + reloc_entry
->addend
) & 0xffff;
1110 /* Adjust val for the final section location and GP value. If we
1111 are producing relocateable output, we don't want to do this for
1112 an external symbol. */
1114 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
1115 val
+= relocation
- gp
;
1117 insn
= (insn
&~ 0xffff) | (val
& 0xffff);
1118 bfd_put_32 (abfd
, insn
, (bfd_byte
*) data
+ reloc_entry
->address
);
1121 reloc_entry
->address
+= input_section
->output_offset
;
1123 /* Make sure it fit in 16 bits. */
1124 if (val
>= 0x8000 && val
< 0xffff8000)
1125 return bfd_reloc_overflow
;
1127 return bfd_reloc_ok
;
1130 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1131 from the gp register? XXX */
1133 static bfd_reloc_status_type gprel32_with_gp
PARAMS ((bfd
*, asymbol
*,
1134 arelent
*, asection
*,
1135 boolean
, PTR
, bfd_vma
));
1137 static bfd_reloc_status_type
1138 mips_elf_gprel32_reloc (abfd
,
1146 arelent
*reloc_entry
;
1149 asection
*input_section
;
1151 char **error_message
;
1153 boolean relocateable
;
1154 bfd_reloc_status_type ret
;
1156 /* If we're relocating, and this is an external symbol with no
1157 addend, we don't want to change anything. We will only have an
1158 addend if this is a newly created reloc, not read from an ELF
1160 if (output_bfd
!= (bfd
*) NULL
1161 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1162 && reloc_entry
->addend
== 0)
1164 *error_message
= (char *)
1165 "32bits gp relative relocation occurs for an external symbol";
1166 return bfd_reloc_outofrange
;
1169 if (output_bfd
!= (bfd
*) NULL
)
1170 relocateable
= true;
1173 relocateable
= false;
1174 output_bfd
= symbol
->section
->output_section
->owner
;
1176 ret
= mips_elf_final_gp (output_bfd
, symbol
, relocateable
,
1178 if (ret
!= bfd_reloc_ok
)
1182 return gprel32_with_gp (abfd
, symbol
, reloc_entry
, input_section
,
1183 relocateable
, data
, elf_gp (output_bfd
));
1186 static bfd_reloc_status_type
1187 gprel32_with_gp (abfd
, symbol
, reloc_entry
, input_section
, relocateable
, data
,
1191 arelent
*reloc_entry
;
1192 asection
*input_section
;
1193 boolean relocateable
;
1200 if (bfd_is_com_section (symbol
->section
))
1203 relocation
= symbol
->value
;
1205 relocation
+= symbol
->section
->output_section
->vma
;
1206 relocation
+= symbol
->section
->output_offset
;
1208 if (reloc_entry
->address
> input_section
->_cooked_size
)
1209 return bfd_reloc_outofrange
;
1211 val
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
1213 /* Set val to the offset into the section or symbol. */
1214 val
+= reloc_entry
->addend
;
1216 /* Adjust val for the final section location and GP value. If we
1217 are producing relocateable output, we don't want to do this for
1218 an external symbol. */
1220 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
1221 val
+= relocation
- gp
;
1223 bfd_put_32 (abfd
, val
, (bfd_byte
*) data
+ reloc_entry
->address
);
1226 reloc_entry
->address
+= input_section
->output_offset
;
1228 return bfd_reloc_ok
;
1231 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1233 struct elf_reloc_map
{
1234 bfd_reloc_code_real_type bfd_reloc_val
;
1235 enum reloc_type elf_reloc_val
;
1238 static CONST
struct elf_reloc_map mips_reloc_map
[] =
1240 { BFD_RELOC_NONE
, R_MIPS_NONE
, },
1241 { BFD_RELOC_16
, R_MIPS_16
},
1242 { BFD_RELOC_32
, R_MIPS_32
},
1243 { BFD_RELOC_CTOR
, R_MIPS_32
},
1244 { BFD_RELOC_32_PCREL
, R_MIPS_REL32
},
1245 { BFD_RELOC_MIPS_JMP
, R_MIPS_26
},
1246 { BFD_RELOC_HI16_S
, R_MIPS_HI16
},
1247 { BFD_RELOC_LO16
, R_MIPS_LO16
},
1248 { BFD_RELOC_MIPS_GPREL
, R_MIPS_GPREL16
},
1249 { BFD_RELOC_MIPS_LITERAL
, R_MIPS_LITERAL
},
1250 { BFD_RELOC_MIPS_GOT16
, R_MIPS_GOT16
},
1251 { BFD_RELOC_16_PCREL
, R_MIPS_PC16
},
1252 { BFD_RELOC_MIPS_CALL16
, R_MIPS_CALL16
},
1253 { BFD_RELOC_MIPS_GPREL32
, R_MIPS_GPREL32
}
1256 /* Given a BFD reloc type, return a howto structure. */
1258 static reloc_howto_type
*
1259 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
1261 bfd_reloc_code_real_type code
;
1265 for (i
= 0; i
< sizeof (mips_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
1267 if (mips_reloc_map
[i
].bfd_reloc_val
== code
)
1268 return &elf_mips_howto_table
[(int) mips_reloc_map
[i
].elf_reloc_val
];
1273 /* Given a MIPS reloc type, fill in an arelent structure. */
1276 mips_info_to_howto_rel (abfd
, cache_ptr
, dst
)
1279 Elf32_Internal_Rel
*dst
;
1281 unsigned int r_type
;
1283 r_type
= ELF32_R_TYPE (dst
->r_info
);
1284 BFD_ASSERT (r_type
< (unsigned int) R_MIPS_max
);
1285 cache_ptr
->howto
= &elf_mips_howto_table
[r_type
];
1287 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1288 value for the object file. We get the addend now, rather than
1289 when we do the relocation, because the symbol manipulations done
1290 by the linker may cause us to lose track of the input BFD. */
1291 if (((*cache_ptr
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) != 0
1292 && (r_type
== (unsigned int) R_MIPS_GPREL16
1293 || r_type
== (unsigned int) R_MIPS_LITERAL
))
1294 cache_ptr
->addend
= elf_gp (abfd
);
1297 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1298 routines swap this structure in and out. They are used outside of
1299 BFD, so they are globally visible. */
1302 bfd_mips_elf32_swap_reginfo_in (abfd
, ex
, in
)
1304 const Elf32_External_RegInfo
*ex
;
1307 in
->ri_gprmask
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gprmask
);
1308 in
->ri_cprmask
[0] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[0]);
1309 in
->ri_cprmask
[1] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[1]);
1310 in
->ri_cprmask
[2] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[2]);
1311 in
->ri_cprmask
[3] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[3]);
1312 in
->ri_gp_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gp_value
);
1316 bfd_mips_elf32_swap_reginfo_out (abfd
, in
, ex
)
1318 const Elf32_RegInfo
*in
;
1319 Elf32_External_RegInfo
*ex
;
1321 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gprmask
,
1322 (bfd_byte
*) ex
->ri_gprmask
);
1323 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[0],
1324 (bfd_byte
*) ex
->ri_cprmask
[0]);
1325 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[1],
1326 (bfd_byte
*) ex
->ri_cprmask
[1]);
1327 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[2],
1328 (bfd_byte
*) ex
->ri_cprmask
[2]);
1329 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[3],
1330 (bfd_byte
*) ex
->ri_cprmask
[3]);
1331 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gp_value
,
1332 (bfd_byte
*) ex
->ri_gp_value
);
1335 /* Swap an entry in a .gptab section. Note that these routines rely
1336 on the equivalence of the two elements of the union. */
1339 bfd_mips_elf32_swap_gptab_in (abfd
, ex
, in
)
1341 const Elf32_External_gptab
*ex
;
1344 in
->gt_entry
.gt_g_value
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_g_value
);
1345 in
->gt_entry
.gt_bytes
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_bytes
);
1349 bfd_mips_elf32_swap_gptab_out (abfd
, in
, ex
)
1351 const Elf32_gptab
*in
;
1352 Elf32_External_gptab
*ex
;
1354 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_g_value
,
1355 ex
->gt_entry
.gt_g_value
);
1356 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_bytes
,
1357 ex
->gt_entry
.gt_bytes
);
1361 bfd_elf32_swap_compact_rel_out (abfd
, in
, ex
)
1363 const Elf32_compact_rel
*in
;
1364 Elf32_External_compact_rel
*ex
;
1366 bfd_h_put_32 (abfd
, (bfd_vma
) in
->id1
, ex
->id1
);
1367 bfd_h_put_32 (abfd
, (bfd_vma
) in
->num
, ex
->num
);
1368 bfd_h_put_32 (abfd
, (bfd_vma
) in
->id2
, ex
->id2
);
1369 bfd_h_put_32 (abfd
, (bfd_vma
) in
->offset
, ex
->offset
);
1370 bfd_h_put_32 (abfd
, (bfd_vma
) in
->reserved0
, ex
->reserved0
);
1371 bfd_h_put_32 (abfd
, (bfd_vma
) in
->reserved1
, ex
->reserved1
);
1375 bfd_elf32_swap_crinfo_out (abfd
, in
, ex
)
1377 const Elf32_crinfo
*in
;
1378 Elf32_External_crinfo
*ex
;
1382 l
= (((in
->ctype
& CRINFO_CTYPE
) << CRINFO_CTYPE_SH
)
1383 | ((in
->rtype
& CRINFO_RTYPE
) << CRINFO_RTYPE_SH
)
1384 | ((in
->dist2to
& CRINFO_DIST2TO
) << CRINFO_DIST2TO_SH
)
1385 | ((in
->relvaddr
& CRINFO_RELVADDR
) << CRINFO_RELVADDR_SH
));
1386 bfd_h_put_32 (abfd
, (bfd_vma
) l
, ex
->info
);
1387 bfd_h_put_32 (abfd
, (bfd_vma
) in
->konst
, ex
->konst
);
1388 bfd_h_put_32 (abfd
, (bfd_vma
) in
->vaddr
, ex
->vaddr
);
1391 /* Determine whether a symbol is global for the purposes of splitting
1392 the symbol table into global symbols and local symbols. At least
1393 on Irix 5, this split must be between section symbols and all other
1394 symbols. On most ELF targets the split is between static symbols
1395 and externally visible symbols. */
1399 mips_elf_sym_is_global (abfd
, sym
)
1403 return (sym
->flags
& BSF_SECTION_SYM
) == 0 ? true : false;
1406 /* Set the right machine number for a MIPS ELF file. */
1409 mips_elf_object_p (abfd
)
1412 switch (elf_elfheader (abfd
)->e_flags
& EF_MIPS_ARCH
)
1416 /* Just use the default, which was set in elfcode.h. */
1420 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 6000);
1424 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 4000);
1428 /* Irix 5 is broken. Object file symbol tables are not always
1429 sorted correctly such that local symbols precede global symbols,
1430 and the sh_info field in the symbol table is not always right. */
1431 elf_bad_symtab (abfd
) = true;
1436 /* The final processing done just before writing out a MIPS ELF object
1437 file. This gets the MIPS architecture right based on the machine
1442 mips_elf_final_write_processing (abfd
, linker
)
1448 Elf_Internal_Shdr
**hdrpp
;
1450 switch (bfd_get_mach (abfd
))
1453 val
= E_MIPS_ARCH_1
;
1457 val
= E_MIPS_ARCH_2
;
1461 val
= E_MIPS_ARCH_3
;
1469 elf_elfheader (abfd
)->e_flags
&=~ EF_MIPS_ARCH
;
1470 elf_elfheader (abfd
)->e_flags
|= val
;
1472 /* Set the sh_info field for .gptab sections. */
1473 for (i
= 1, hdrpp
= elf_elfsections (abfd
) + 1;
1474 i
< elf_elfheader (abfd
)->e_shnum
;
1477 if ((*hdrpp
)->sh_type
== SHT_MIPS_GPTAB
)
1482 BFD_ASSERT ((*hdrpp
)->bfd_section
!= NULL
);
1483 name
= bfd_get_section_name (abfd
, (*hdrpp
)->bfd_section
);
1484 BFD_ASSERT (name
!= NULL
1485 && strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0);
1486 sec
= bfd_get_section_by_name (abfd
, name
+ sizeof ".gptab" - 1);
1487 BFD_ASSERT (sec
!= NULL
);
1488 (*hdrpp
)->sh_info
= elf_section_data (sec
)->this_idx
;
1493 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1496 mips_elf_set_private_flags (abfd
, flags
)
1500 BFD_ASSERT (!elf_flags_init (abfd
)
1501 || elf_elfheader (abfd
)->e_flags
== flags
);
1503 elf_elfheader (abfd
)->e_flags
= flags
;
1504 elf_flags_init (abfd
) = true;
1508 /* Copy backend specific data from one object module to another */
1511 mips_elf_copy_private_bfd_data (ibfd
, obfd
)
1515 /* This function is selected based on the input vector. We only
1516 want to copy information over if the output BFD also uses Elf
1518 if (bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1521 BFD_ASSERT (!elf_flags_init (obfd
)
1522 || (elf_elfheader (obfd
)->e_flags
1523 == elf_elfheader (ibfd
)->e_flags
));
1525 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1526 elf_flags_init (obfd
) = true;
1530 /* Merge backend specific data from an object file to the output
1531 object file when linking. */
1534 mips_elf_merge_private_bfd_data (ibfd
, obfd
)
1541 /* Check if we have the same endianess */
1542 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1543 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1545 (*_bfd_error_handler
)
1546 ("%s: compiled for a %s endian system and target is %s endian",
1547 bfd_get_filename (ibfd
),
1548 bfd_big_endian (ibfd
) ? "big" : "little",
1549 bfd_big_endian (obfd
) ? "big" : "little");
1551 bfd_set_error (bfd_error_wrong_format
);
1555 /* This function is selected based on the input vector. We only
1556 want to copy information over if the output BFD also uses Elf
1558 if (bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1561 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1562 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_MIPS_NOREORDER
;
1563 old_flags
= elf_elfheader (obfd
)->e_flags
;
1565 if (!elf_flags_init (obfd
)) /* First call, no flags set */
1567 elf_flags_init (obfd
) = true;
1568 elf_elfheader (obfd
)->e_flags
= new_flags
;
1570 else if (((new_flags
^ old_flags
) & ~EF_MIPS_NOREORDER
)
1571 == 0) /* Compatible flags are ok */
1573 else /* Incompatible flags */
1575 /* Warn about -fPIC mismatch */
1576 if ((new_flags
& EF_MIPS_PIC
) != (old_flags
& EF_MIPS_PIC
))
1578 new_flags
&= ~EF_MIPS_PIC
;
1579 (*_bfd_error_handler
)
1580 ("%s: needs all files compiled with -fPIC",
1581 bfd_get_filename (ibfd
));
1584 if ((new_flags
& EF_MIPS_CPIC
) != (old_flags
& EF_MIPS_CPIC
))
1586 new_flags
&= ~EF_MIPS_CPIC
;
1587 (*_bfd_error_handler
)
1588 ("%s: needs all files compiled with -mabicalls",
1589 bfd_get_filename (ibfd
));
1592 /* Warn about any other mismatches */
1593 if (new_flags
!= old_flags
)
1594 (*_bfd_error_handler
)
1595 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1596 bfd_get_filename (ibfd
), (unsigned long) new_flags
,
1597 (unsigned long) old_flags
);
1599 bfd_set_error (bfd_error_bad_value
);
1606 /* Handle a MIPS specific section when reading an object file. This
1607 is called when elfcode.h finds a section with an unknown type.
1608 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1612 mips_elf_section_from_shdr (abfd
, hdr
, name
)
1614 Elf32_Internal_Shdr
*hdr
;
1619 /* There ought to be a place to keep ELF backend specific flags, but
1620 at the moment there isn't one. We just keep track of the
1621 sections by their name, instead. Fortunately, the ABI gives
1622 suggested names for all the MIPS specific sections, so we will
1623 probably get away with this. */
1624 switch (hdr
->sh_type
)
1626 case SHT_MIPS_LIBLIST
:
1627 if (strcmp (name
, ".liblist") != 0)
1631 if (strcmp (name
, ".msym") != 0)
1634 case SHT_MIPS_CONFLICT
:
1635 if (strcmp (name
, ".conflict") != 0)
1638 case SHT_MIPS_GPTAB
:
1639 if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) != 0)
1642 case SHT_MIPS_UCODE
:
1643 if (strcmp (name
, ".ucode") != 0)
1646 case SHT_MIPS_DEBUG
:
1647 if (strcmp (name
, ".mdebug") != 0)
1650 case SHT_MIPS_REGINFO
:
1651 if (strcmp (name
, ".reginfo") != 0
1652 || hdr
->sh_size
!= sizeof (Elf32_External_RegInfo
))
1655 case SHT_MIPS_OPTIONS
:
1656 if (strcmp (name
, ".options") != 0)
1659 case SHT_MIPS_DWARF
:
1660 if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) != 0)
1663 case SHT_MIPS_EVENTS
:
1664 if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1671 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1673 newsect
= hdr
->bfd_section
;
1675 if (hdr
->sh_type
== SHT_MIPS_DEBUG
)
1677 if (! bfd_set_section_flags (abfd
, newsect
,
1678 (bfd_get_section_flags (abfd
, newsect
)
1683 /* FIXME: We should record sh_info for a .gptab section. */
1685 /* For a .reginfo section, set the gp value in the tdata information
1686 from the contents of this section. We need the gp value while
1687 processing relocs, so we just get it now. */
1688 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1690 Elf32_External_RegInfo ext
;
1693 if (! bfd_get_section_contents (abfd
, newsect
, (PTR
) &ext
,
1694 (file_ptr
) 0, sizeof ext
))
1696 bfd_mips_elf32_swap_reginfo_in (abfd
, &ext
, &s
);
1697 elf_gp (abfd
) = s
.ri_gp_value
;
1703 /* Set the correct type for a MIPS ELF section. We do this by the
1704 section name, which is a hack, but ought to work. */
1707 mips_elf_fake_sections (abfd
, hdr
, sec
)
1709 Elf32_Internal_Shdr
*hdr
;
1712 register const char *name
;
1714 name
= bfd_get_section_name (abfd
, sec
);
1716 if (strcmp (name
, ".liblist") == 0)
1718 hdr
->sh_type
= SHT_MIPS_LIBLIST
;
1719 hdr
->sh_info
= sec
->_raw_size
/ sizeof (Elf32_Lib
);
1720 /* FIXME: Set the sh_link field. */
1722 else if (strcmp (name
, ".msym") == 0)
1724 hdr
->sh_type
= SHT_MIPS_MSYM
;
1725 hdr
->sh_entsize
= 8;
1726 /* FIXME: Set the sh_info field. */
1728 else if (strcmp (name
, ".conflict") == 0)
1729 hdr
->sh_type
= SHT_MIPS_CONFLICT
;
1730 else if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0)
1732 hdr
->sh_type
= SHT_MIPS_GPTAB
;
1733 hdr
->sh_entsize
= sizeof (Elf32_External_gptab
);
1734 /* The sh_info field is set in mips_elf_final_write_processing. */
1736 else if (strcmp (name
, ".ucode") == 0)
1737 hdr
->sh_type
= SHT_MIPS_UCODE
;
1738 else if (strcmp (name
, ".mdebug") == 0)
1740 hdr
->sh_type
= SHT_MIPS_DEBUG
;
1741 /* In a shared object on Irix 5.3, the .mdebug section has an
1742 entsize of 0. FIXME: Does this matter? */
1743 if (SGI_COMPAT (abfd
) && (abfd
->flags
& DYNAMIC
) != 0)
1744 hdr
->sh_entsize
= 0;
1746 hdr
->sh_entsize
= 1;
1748 else if (strcmp (name
, ".reginfo") == 0)
1750 hdr
->sh_type
= SHT_MIPS_REGINFO
;
1751 /* In a shared object on Irix 5.3, the .reginfo section has an
1752 entsize of 0x18. FIXME: Does this matter? */
1753 if (SGI_COMPAT (abfd
) && (abfd
->flags
& DYNAMIC
) != 0)
1754 hdr
->sh_entsize
= sizeof (Elf32_External_RegInfo
);
1756 hdr
->sh_entsize
= 1;
1758 /* Force the section size to the correct value, even if the
1759 linker thinks it is larger. The link routine below will only
1760 write out this much data for .reginfo. */
1761 hdr
->sh_size
= sec
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1763 else if (SGI_COMPAT (abfd
)
1764 && (strcmp (name
, ".hash") == 0
1765 || strcmp (name
, ".dynamic") == 0
1766 || strcmp (name
, ".dynstr") == 0))
1768 hdr
->sh_entsize
= 0;
1769 hdr
->sh_info
= SIZEOF_MIPS_DYNSYM_SECNAMES
;
1771 else if (strcmp (name
, ".got") == 0
1772 || strcmp (name
, ".sdata") == 0
1773 || strcmp (name
, ".sbss") == 0
1774 || strcmp (name
, ".lit4") == 0
1775 || strcmp (name
, ".lit8") == 0)
1776 hdr
->sh_flags
|= SHF_MIPS_GPREL
;
1777 else if (strcmp (name
, ".options") == 0)
1779 hdr
->sh_type
= SHT_MIPS_OPTIONS
;
1780 hdr
->sh_entsize
= 1;
1782 else if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) == 0)
1783 hdr
->sh_type
= SHT_MIPS_DWARF
;
1784 else if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1785 hdr
->sh_type
= SHT_MIPS_EVENTS
;
1790 /* Given a BFD section, try to locate the corresponding ELF section
1794 mips_elf_section_from_bfd_section (abfd
, hdr
, sec
, retval
)
1796 Elf32_Internal_Shdr
*hdr
;
1800 if (strcmp (bfd_get_section_name (abfd
, sec
), ".scommon") == 0)
1802 *retval
= SHN_MIPS_SCOMMON
;
1805 if (strcmp (bfd_get_section_name (abfd
, sec
), ".acommon") == 0)
1807 *retval
= SHN_MIPS_ACOMMON
;
1813 /* Work over a section just before writing it out. We update the GP
1814 value in the .reginfo section based on the value we are using.
1815 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1816 name; there has to be a better way. */
1819 mips_elf_section_processing (abfd
, hdr
)
1821 Elf32_Internal_Shdr
*hdr
;
1823 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1827 BFD_ASSERT (hdr
->sh_size
== sizeof (Elf32_External_RegInfo
));
1828 BFD_ASSERT (hdr
->contents
== NULL
);
1831 hdr
->sh_offset
+ sizeof (Elf32_External_RegInfo
) - 4,
1834 bfd_h_put_32 (abfd
, (bfd_vma
) elf_gp (abfd
), buf
);
1835 if (bfd_write (buf
, (bfd_size_type
) 1, (bfd_size_type
) 4, abfd
) != 4)
1839 if (hdr
->bfd_section
!= NULL
)
1841 const char *name
= bfd_get_section_name (abfd
, hdr
->bfd_section
);
1843 if (strcmp (name
, ".sdata") == 0)
1845 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1846 hdr
->sh_type
= SHT_PROGBITS
;
1848 else if (strcmp (name
, ".sbss") == 0)
1850 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1851 hdr
->sh_type
= SHT_NOBITS
;
1853 else if (strcmp (name
, ".lit8") == 0
1854 || strcmp (name
, ".lit4") == 0)
1856 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1857 hdr
->sh_type
= SHT_PROGBITS
;
1859 else if (strcmp (name
, ".compact_rel") == 0)
1862 hdr
->sh_type
= SHT_PROGBITS
;
1864 else if (strcmp (name
, ".rtproc") == 0)
1866 if (hdr
->sh_addralign
!= 0 && hdr
->sh_entsize
== 0)
1868 unsigned int adjust
;
1870 adjust
= hdr
->sh_size
% hdr
->sh_addralign
;
1872 hdr
->sh_size
+= hdr
->sh_addralign
- adjust
;
1880 /* MIPS ELF uses two common sections. One is the usual one, and the
1881 other is for small objects. All the small objects are kept
1882 together, and then referenced via the gp pointer, which yields
1883 faster assembler code. This is what we use for the small common
1884 section. This approach is copied from ecoff.c. */
1885 static asection mips_elf_scom_section
;
1886 static asymbol mips_elf_scom_symbol
;
1887 static asymbol
*mips_elf_scom_symbol_ptr
;
1889 /* MIPS ELF also uses an acommon section, which represents an
1890 allocated common symbol which may be overridden by a
1891 definition in a shared library. */
1892 static asection mips_elf_acom_section
;
1893 static asymbol mips_elf_acom_symbol
;
1894 static asymbol
*mips_elf_acom_symbol_ptr
;
1896 /* The Irix 5 support uses two virtual sections, which represent
1897 text/data symbols defined in dynamic objects. */
1898 static asection mips_elf_text_section
;
1899 static asection
*mips_elf_text_section_ptr
;
1900 static asymbol mips_elf_text_symbol
;
1901 static asymbol
*mips_elf_text_symbol_ptr
;
1903 static asection mips_elf_data_section
;
1904 static asection
*mips_elf_data_section_ptr
;
1905 static asymbol mips_elf_data_symbol
;
1906 static asymbol
*mips_elf_data_symbol_ptr
;
1908 /* Handle the special MIPS section numbers that a symbol may use. */
1911 mips_elf_symbol_processing (abfd
, asym
)
1915 elf_symbol_type
*elfsym
;
1917 elfsym
= (elf_symbol_type
*) asym
;
1918 switch (elfsym
->internal_elf_sym
.st_shndx
)
1920 case SHN_MIPS_ACOMMON
:
1921 /* This section is used in a dynamically linked executable file.
1922 It is an allocated common section. The dynamic linker can
1923 either resolve these symbols to something in a shared
1924 library, or it can just leave them here. For our purposes,
1925 we can consider these symbols to be in a new section. */
1926 if (mips_elf_acom_section
.name
== NULL
)
1928 /* Initialize the acommon section. */
1929 mips_elf_acom_section
.name
= ".acommon";
1930 mips_elf_acom_section
.flags
= SEC_ALLOC
;
1931 mips_elf_acom_section
.output_section
= &mips_elf_acom_section
;
1932 mips_elf_acom_section
.symbol
= &mips_elf_acom_symbol
;
1933 mips_elf_acom_section
.symbol_ptr_ptr
= &mips_elf_acom_symbol_ptr
;
1934 mips_elf_acom_symbol
.name
= ".acommon";
1935 mips_elf_acom_symbol
.flags
= BSF_SECTION_SYM
;
1936 mips_elf_acom_symbol
.section
= &mips_elf_acom_section
;
1937 mips_elf_acom_symbol_ptr
= &mips_elf_acom_symbol
;
1939 asym
->section
= &mips_elf_acom_section
;
1943 /* Common symbols less than the GP size are automatically
1944 treated as SHN_MIPS_SCOMMON symbols. */
1945 if (asym
->value
> elf_gp_size (abfd
))
1948 case SHN_MIPS_SCOMMON
:
1949 if (mips_elf_scom_section
.name
== NULL
)
1951 /* Initialize the small common section. */
1952 mips_elf_scom_section
.name
= ".scommon";
1953 mips_elf_scom_section
.flags
= SEC_IS_COMMON
;
1954 mips_elf_scom_section
.output_section
= &mips_elf_scom_section
;
1955 mips_elf_scom_section
.symbol
= &mips_elf_scom_symbol
;
1956 mips_elf_scom_section
.symbol_ptr_ptr
= &mips_elf_scom_symbol_ptr
;
1957 mips_elf_scom_symbol
.name
= ".scommon";
1958 mips_elf_scom_symbol
.flags
= BSF_SECTION_SYM
;
1959 mips_elf_scom_symbol
.section
= &mips_elf_scom_section
;
1960 mips_elf_scom_symbol_ptr
= &mips_elf_scom_symbol
;
1962 asym
->section
= &mips_elf_scom_section
;
1963 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
1966 case SHN_MIPS_SUNDEFINED
:
1967 asym
->section
= bfd_und_section_ptr
;
1970 #if 0 /* for SGI_COMPAT */
1972 asym
->section
= mips_elf_text_section_ptr
;
1976 asym
->section
= mips_elf_data_section_ptr
;
1982 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
1986 mips_elf_additional_program_headers (abfd
)
1994 if (! SGI_COMPAT (abfd
))
1997 s
= bfd_get_section_by_name (abfd
, ".reginfo");
1998 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2000 /* We need a PT_MIPS_REGINFO segment. */
2004 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
2005 && bfd_get_section_by_name (abfd
, ".mdebug") != NULL
)
2007 /* We need a PT_MIPS_RTPROC segment. */
2014 /* Modify the segment map for an Irix 5 executable. */
2017 mips_elf_modify_segment_map (abfd
)
2021 struct elf_segment_map
*m
, **pm
;
2023 if (! SGI_COMPAT (abfd
))
2026 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2028 s
= bfd_get_section_by_name (abfd
, ".reginfo");
2029 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2031 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2032 if (m
->p_type
== PT_MIPS_REGINFO
)
2036 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, sizeof *m
);
2040 m
->p_type
= PT_MIPS_REGINFO
;
2044 /* We want to put it after the PHDR and INTERP segments. */
2045 pm
= &elf_tdata (abfd
)->segment_map
;
2047 && ((*pm
)->p_type
== PT_PHDR
2048 || (*pm
)->p_type
== PT_INTERP
))
2056 /* If there are .dynamic and .mdebug sections, we make a room for
2057 the RTPROC header. FIXME: Rewrite without section names. */
2058 if (bfd_get_section_by_name (abfd
, ".interp") == NULL
2059 && bfd_get_section_by_name (abfd
, ".dynamic") != NULL
2060 && bfd_get_section_by_name (abfd
, ".mdebug") != NULL
)
2062 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2063 if (m
->p_type
== PT_MIPS_RTPROC
)
2067 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, sizeof *m
);
2071 m
->p_type
= PT_MIPS_RTPROC
;
2073 s
= bfd_get_section_by_name (abfd
, ".rtproc");
2078 m
->p_flags_valid
= 1;
2086 /* We want to put it after the DYNAMIC segment. */
2087 pm
= &elf_tdata (abfd
)->segment_map
;
2088 while (*pm
!= NULL
&& (*pm
)->p_type
!= PT_DYNAMIC
)
2098 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2099 .dynsym, and .hash sections, and everything in between. */
2100 for (pm
= &elf_tdata (abfd
)->segment_map
; *pm
!= NULL
; pm
= &(*pm
)->next
)
2101 if ((*pm
)->p_type
== PT_DYNAMIC
)
2106 && strcmp (m
->sections
[0]->name
, ".dynamic") == 0)
2108 static const char *sec_names
[] =
2109 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2112 struct elf_segment_map
*n
;
2116 for (i
= 0; i
< sizeof sec_names
/ sizeof sec_names
[0]; i
++)
2118 s
= bfd_get_section_by_name (abfd
, sec_names
[i
]);
2119 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2125 sz
= s
->_cooked_size
;
2128 if (high
< s
->vma
+ sz
)
2134 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2135 if ((s
->flags
& SEC_LOAD
) != 0
2138 + (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
))
2142 n
= ((struct elf_segment_map
*)
2143 bfd_zalloc (abfd
, sizeof *n
+ (c
- 1) * sizeof (asection
*)));
2150 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2152 if ((s
->flags
& SEC_LOAD
) != 0
2155 + (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
))
2169 /* The structure of the runtime procedure descriptor created by the
2170 loader for use by the static exception system. */
2172 typedef struct runtime_pdr
{
2173 bfd_vma adr
; /* memory address of start of procedure */
2174 long regmask
; /* save register mask */
2175 long regoffset
; /* save register offset */
2176 long fregmask
; /* save floating point register mask */
2177 long fregoffset
; /* save floating point register offset */
2178 long frameoffset
; /* frame size */
2179 short framereg
; /* frame pointer register */
2180 short pcreg
; /* offset or reg of return pc */
2181 long irpss
; /* index into the runtime string table */
2183 struct exception_info
*exception_info
;/* pointer to exception array */
2185 #define cbRPDR sizeof(RPDR)
2186 #define rpdNil ((pRPDR) 0)
2188 /* Swap RPDR (runtime procedure table entry) for output. */
2190 static void ecoff_swap_rpdr_out
2191 PARAMS ((bfd
*, const RPDR
*, struct rpdr_ext
*));
2194 ecoff_swap_rpdr_out (abfd
, in
, ex
)
2197 struct rpdr_ext
*ex
;
2199 /* ecoff_put_off was defined in ecoffswap.h. */
2200 ecoff_put_off (abfd
, in
->adr
, (bfd_byte
*) ex
->p_adr
);
2201 bfd_h_put_32 (abfd
, in
->regmask
, (bfd_byte
*) ex
->p_regmask
);
2202 bfd_h_put_32 (abfd
, in
->regoffset
, (bfd_byte
*) ex
->p_regoffset
);
2203 bfd_h_put_32 (abfd
, in
->fregmask
, (bfd_byte
*) ex
->p_fregmask
);
2204 bfd_h_put_32 (abfd
, in
->fregoffset
, (bfd_byte
*) ex
->p_fregoffset
);
2205 bfd_h_put_32 (abfd
, in
->frameoffset
, (bfd_byte
*) ex
->p_frameoffset
);
2207 bfd_h_put_16 (abfd
, in
->framereg
, (bfd_byte
*) ex
->p_framereg
);
2208 bfd_h_put_16 (abfd
, in
->pcreg
, (bfd_byte
*) ex
->p_pcreg
);
2210 bfd_h_put_32 (abfd
, in
->irpss
, (bfd_byte
*) ex
->p_irpss
);
2212 ecoff_put_off (abfd
, in
->exception_info
, (bfd_byte
*) ex
->p_exception_info
);
2216 /* Read ECOFF debugging information from a .mdebug section into a
2217 ecoff_debug_info structure. */
2220 mips_elf_read_ecoff_info (abfd
, section
, debug
)
2223 struct ecoff_debug_info
*debug
;
2226 const struct ecoff_debug_swap
*swap
;
2227 char *ext_hdr
= NULL
;
2229 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2231 ext_hdr
= (char *) bfd_malloc ((size_t) swap
->external_hdr_size
);
2232 if (ext_hdr
== NULL
&& swap
->external_hdr_size
!= 0)
2235 if (bfd_get_section_contents (abfd
, section
, ext_hdr
, (file_ptr
) 0,
2236 swap
->external_hdr_size
)
2240 symhdr
= &debug
->symbolic_header
;
2241 (*swap
->swap_hdr_in
) (abfd
, ext_hdr
, symhdr
);
2243 /* The symbolic header contains absolute file offsets and sizes to
2245 #define READ(ptr, offset, count, size, type) \
2246 if (symhdr->count == 0) \
2247 debug->ptr = NULL; \
2250 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2251 if (debug->ptr == NULL) \
2252 goto error_return; \
2253 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2254 || (bfd_read (debug->ptr, size, symhdr->count, \
2255 abfd) != size * symhdr->count)) \
2256 goto error_return; \
2259 READ (line
, cbLineOffset
, cbLine
, sizeof (unsigned char), unsigned char *);
2260 READ (external_dnr
, cbDnOffset
, idnMax
, swap
->external_dnr_size
, PTR
);
2261 READ (external_pdr
, cbPdOffset
, ipdMax
, swap
->external_pdr_size
, PTR
);
2262 READ (external_sym
, cbSymOffset
, isymMax
, swap
->external_sym_size
, PTR
);
2263 READ (external_opt
, cbOptOffset
, ioptMax
, swap
->external_opt_size
, PTR
);
2264 READ (external_aux
, cbAuxOffset
, iauxMax
, sizeof (union aux_ext
),
2266 READ (ss
, cbSsOffset
, issMax
, sizeof (char), char *);
2267 READ (ssext
, cbSsExtOffset
, issExtMax
, sizeof (char), char *);
2268 READ (external_fdr
, cbFdOffset
, ifdMax
, swap
->external_fdr_size
, PTR
);
2269 READ (external_rfd
, cbRfdOffset
, crfd
, swap
->external_rfd_size
, PTR
);
2270 READ (external_ext
, cbExtOffset
, iextMax
, swap
->external_ext_size
, PTR
);
2274 debug
->adjust
= NULL
;
2279 if (ext_hdr
!= NULL
)
2281 if (debug
->line
!= NULL
)
2283 if (debug
->external_dnr
!= NULL
)
2284 free (debug
->external_dnr
);
2285 if (debug
->external_pdr
!= NULL
)
2286 free (debug
->external_pdr
);
2287 if (debug
->external_sym
!= NULL
)
2288 free (debug
->external_sym
);
2289 if (debug
->external_opt
!= NULL
)
2290 free (debug
->external_opt
);
2291 if (debug
->external_aux
!= NULL
)
2292 free (debug
->external_aux
);
2293 if (debug
->ss
!= NULL
)
2295 if (debug
->ssext
!= NULL
)
2296 free (debug
->ssext
);
2297 if (debug
->external_fdr
!= NULL
)
2298 free (debug
->external_fdr
);
2299 if (debug
->external_rfd
!= NULL
)
2300 free (debug
->external_rfd
);
2301 if (debug
->external_ext
!= NULL
)
2302 free (debug
->external_ext
);
2306 /* MIPS ELF local labels start with '$', not 'L'. */
2310 mips_elf_is_local_label (abfd
, symbol
)
2314 return symbol
->name
[0] == '$';
2317 /* MIPS ELF uses a special find_nearest_line routine in order the
2318 handle the ECOFF debugging information. */
2320 struct mips_elf_find_line
2322 struct ecoff_debug_info d
;
2323 struct ecoff_find_line i
;
2327 mips_elf_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2328 functionname_ptr
, line_ptr
)
2333 const char **filename_ptr
;
2334 const char **functionname_ptr
;
2335 unsigned int *line_ptr
;
2339 msec
= bfd_get_section_by_name (abfd
, ".mdebug");
2343 struct mips_elf_find_line
*fi
;
2344 const struct ecoff_debug_swap
* const swap
=
2345 get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2347 /* If we are called during a link, mips_elf_final_link may have
2348 cleared the SEC_HAS_CONTENTS field. We force it back on here
2349 if appropriate (which it normally will be). */
2350 origflags
= msec
->flags
;
2351 if (elf_section_data (msec
)->this_hdr
.sh_type
!= SHT_NOBITS
)
2352 msec
->flags
|= SEC_HAS_CONTENTS
;
2354 fi
= elf_tdata (abfd
)->find_line_info
;
2357 bfd_size_type external_fdr_size
;
2360 struct fdr
*fdr_ptr
;
2362 fi
= ((struct mips_elf_find_line
*)
2363 bfd_alloc (abfd
, sizeof (struct mips_elf_find_line
)));
2366 msec
->flags
= origflags
;
2370 memset (fi
, 0, sizeof (struct mips_elf_find_line
));
2372 if (! mips_elf_read_ecoff_info (abfd
, msec
, &fi
->d
))
2374 msec
->flags
= origflags
;
2378 /* Swap in the FDR information. */
2379 fi
->d
.fdr
= ((struct fdr
*)
2381 (fi
->d
.symbolic_header
.ifdMax
*
2382 sizeof (struct fdr
))));
2383 if (fi
->d
.fdr
== NULL
)
2385 msec
->flags
= origflags
;
2388 external_fdr_size
= swap
->external_fdr_size
;
2389 fdr_ptr
= fi
->d
.fdr
;
2390 fraw_src
= (char *) fi
->d
.external_fdr
;
2391 fraw_end
= (fraw_src
2392 + fi
->d
.symbolic_header
.ifdMax
* external_fdr_size
);
2393 for (; fraw_src
< fraw_end
; fraw_src
+= external_fdr_size
, fdr_ptr
++)
2394 (*swap
->swap_fdr_in
) (abfd
, (PTR
) fraw_src
, fdr_ptr
);
2396 elf_tdata (abfd
)->find_line_info
= fi
;
2398 /* Note that we don't bother to ever free this information.
2399 find_nearest_line is either called all the time, as in
2400 objdump -l, so the information should be saved, or it is
2401 rarely called, as in ld error messages, so the memory
2402 wasted is unimportant. Still, it would probably be a
2403 good idea for free_cached_info to throw it away. */
2406 if (_bfd_ecoff_locate_line (abfd
, section
, offset
, &fi
->d
, swap
,
2407 &fi
->i
, filename_ptr
, functionname_ptr
,
2410 msec
->flags
= origflags
;
2414 msec
->flags
= origflags
;
2417 /* Fall back on the generic ELF find_nearest_line routine. */
2419 return _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
2420 filename_ptr
, functionname_ptr
,
2424 /* The MIPS ELF linker needs additional information for each symbol in
2425 the global hash table. */
2427 struct mips_elf_link_hash_entry
2429 struct elf_link_hash_entry root
;
2431 /* External symbol information. */
2435 /* MIPS ELF linker hash table. */
2437 struct mips_elf_link_hash_table
2439 struct elf_link_hash_table root
;
2440 /* String section indices for the dynamic section symbols. */
2441 bfd_size_type dynsym_sec_strindex
[SIZEOF_MIPS_DYNSYM_SECNAMES
];
2442 /* The number of .rtproc entries. */
2443 bfd_size_type procedure_count
;
2444 /* The size of the .compact_rel section (if SGI_COMPAT). */
2445 bfd_size_type compact_rel_size
;
2448 /* Look up an entry in a MIPS ELF linker hash table. */
2450 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
2451 ((struct mips_elf_link_hash_entry *) \
2452 elf_link_hash_lookup (&(table)->root, (string), (create), \
2455 /* Traverse a MIPS ELF linker hash table. */
2457 #define mips_elf_link_hash_traverse(table, func, info) \
2458 (elf_link_hash_traverse \
2460 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2463 /* Get the MIPS ELF linker hash table from a link_info structure. */
2465 #define mips_elf_hash_table(p) \
2466 ((struct mips_elf_link_hash_table *) ((p)->hash))
2468 static boolean mips_elf_output_extsym
2469 PARAMS ((struct mips_elf_link_hash_entry
*, PTR
));
2471 /* Create an entry in a MIPS ELF linker hash table. */
2473 static struct bfd_hash_entry
*
2474 mips_elf_link_hash_newfunc (entry
, table
, string
)
2475 struct bfd_hash_entry
*entry
;
2476 struct bfd_hash_table
*table
;
2479 struct mips_elf_link_hash_entry
*ret
=
2480 (struct mips_elf_link_hash_entry
*) entry
;
2482 /* Allocate the structure if it has not already been allocated by a
2484 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
2485 ret
= ((struct mips_elf_link_hash_entry
*)
2486 bfd_hash_allocate (table
,
2487 sizeof (struct mips_elf_link_hash_entry
)));
2488 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
2489 return (struct bfd_hash_entry
*) ret
;
2491 /* Call the allocation method of the superclass. */
2492 ret
= ((struct mips_elf_link_hash_entry
*)
2493 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2495 if (ret
!= (struct mips_elf_link_hash_entry
*) NULL
)
2497 /* Set local fields. */
2498 memset (&ret
->esym
, 0, sizeof (EXTR
));
2499 /* We use -2 as a marker to indicate that the information has
2500 not been set. -1 means there is no associated ifd. */
2504 return (struct bfd_hash_entry
*) ret
;
2507 /* Create a MIPS ELF linker hash table. */
2509 static struct bfd_link_hash_table
*
2510 mips_elf_link_hash_table_create (abfd
)
2513 struct mips_elf_link_hash_table
*ret
;
2516 ret
= ((struct mips_elf_link_hash_table
*)
2517 bfd_alloc (abfd
, sizeof (struct mips_elf_link_hash_table
)));
2518 if (ret
== (struct mips_elf_link_hash_table
*) NULL
)
2521 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2522 mips_elf_link_hash_newfunc
))
2524 bfd_release (abfd
, ret
);
2528 for (i
= 0; i
< SIZEOF_MIPS_DYNSYM_SECNAMES
; i
++)
2529 ret
->dynsym_sec_strindex
[i
] = (bfd_size_type
) -1;
2530 ret
->procedure_count
= 0;
2531 ret
->compact_rel_size
= 0;
2533 return &ret
->root
.root
;
2536 /* Hook called by the linker routine which adds symbols from an object
2537 file. We must handle the special MIPS section numbers here. */
2541 mips_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
2543 struct bfd_link_info
*info
;
2544 const Elf_Internal_Sym
*sym
;
2550 if (SGI_COMPAT (abfd
)
2551 && (abfd
->flags
& DYNAMIC
) != 0
2552 && strcmp (*namep
, "_rld_new_interface") == 0)
2554 /* Skip Irix 5 rld entry name. */
2559 switch (sym
->st_shndx
)
2562 /* Common symbols less than the GP size are automatically
2563 treated as SHN_MIPS_SCOMMON symbols. */
2564 if (sym
->st_size
> elf_gp_size (abfd
))
2567 case SHN_MIPS_SCOMMON
:
2568 *secp
= bfd_make_section_old_way (abfd
, ".scommon");
2569 (*secp
)->flags
|= SEC_IS_COMMON
;
2570 *valp
= sym
->st_size
;
2574 /* This section is used in a shared object. */
2575 if (mips_elf_text_section_ptr
== NULL
)
2577 /* Initialize the section. */
2578 mips_elf_text_section
.name
= ".text";
2579 mips_elf_text_section
.flags
= SEC_NO_FLAGS
;
2580 mips_elf_text_section
.output_section
= NULL
;
2581 mips_elf_text_section
.owner
= abfd
;
2582 mips_elf_text_section
.symbol
= &mips_elf_text_symbol
;
2583 mips_elf_text_section
.symbol_ptr_ptr
= &mips_elf_text_symbol_ptr
;
2584 mips_elf_text_symbol
.name
= ".text";
2585 mips_elf_text_symbol
.flags
= BSF_SECTION_SYM
;
2586 mips_elf_text_symbol
.section
= &mips_elf_text_section
;
2587 mips_elf_text_symbol_ptr
= &mips_elf_text_symbol
;
2588 mips_elf_text_section_ptr
= &mips_elf_text_section
;
2591 *secp
= bfd_und_section_ptr
;
2593 *secp
= mips_elf_text_section_ptr
;
2596 case SHN_MIPS_ACOMMON
:
2597 /* Fall through. XXX Can we treat this as allocated data? */
2599 /* This section is used in a shared object. */
2600 if (mips_elf_data_section_ptr
== NULL
)
2602 /* Initialize the section. */
2603 mips_elf_data_section
.name
= ".data";
2604 mips_elf_data_section
.flags
= SEC_NO_FLAGS
;
2605 mips_elf_data_section
.output_section
= NULL
;
2606 mips_elf_data_section
.owner
= abfd
;
2607 mips_elf_data_section
.symbol
= &mips_elf_data_symbol
;
2608 mips_elf_data_section
.symbol_ptr_ptr
= &mips_elf_data_symbol_ptr
;
2609 mips_elf_data_symbol
.name
= ".data";
2610 mips_elf_data_symbol
.flags
= BSF_SECTION_SYM
;
2611 mips_elf_data_symbol
.section
= &mips_elf_data_section
;
2612 mips_elf_data_symbol_ptr
= &mips_elf_data_symbol
;
2613 mips_elf_data_section_ptr
= &mips_elf_data_section
;
2616 *secp
= bfd_und_section_ptr
;
2618 *secp
= mips_elf_data_section_ptr
;
2621 case SHN_MIPS_SUNDEFINED
:
2622 *secp
= bfd_und_section_ptr
;
2629 /* Structure used to pass information to mips_elf_output_extsym. */
2634 struct bfd_link_info
*info
;
2635 struct ecoff_debug_info
*debug
;
2636 const struct ecoff_debug_swap
*swap
;
2640 /* This routine is used to write out ECOFF debugging external symbol
2641 information. It is called via mips_elf_link_hash_traverse. The
2642 ECOFF external symbol information must match the ELF external
2643 symbol information. Unfortunately, at this point we don't know
2644 whether a symbol is required by reloc information, so the two
2645 tables may wind up being different. We must sort out the external
2646 symbol information before we can set the final size of the .mdebug
2647 section, and we must set the size of the .mdebug section before we
2648 can relocate any sections, and we can't know which symbols are
2649 required by relocation until we relocate the sections.
2650 Fortunately, it is relatively unlikely that any symbol will be
2651 stripped but required by a reloc. In particular, it can not happen
2652 when generating a final executable. */
2655 mips_elf_output_extsym (h
, data
)
2656 struct mips_elf_link_hash_entry
*h
;
2659 struct extsym_info
*einfo
= (struct extsym_info
*) data
;
2661 asection
*sec
, *output_section
;
2663 if (h
->root
.indx
== -2)
2665 else if (((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2666 || (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2667 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2668 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
2670 else if (einfo
->info
->strip
== strip_all
2671 || (einfo
->info
->strip
== strip_some
2672 && bfd_hash_lookup (einfo
->info
->keep_hash
,
2673 h
->root
.root
.root
.string
,
2674 false, false) == NULL
))
2682 if (h
->esym
.ifd
== -2)
2685 h
->esym
.cobol_main
= 0;
2686 h
->esym
.weakext
= 0;
2687 h
->esym
.reserved
= 0;
2688 h
->esym
.ifd
= ifdNil
;
2689 h
->esym
.asym
.value
= 0;
2690 h
->esym
.asym
.st
= stGlobal
;
2692 if (SGI_COMPAT (einfo
->abfd
)
2693 && (h
->root
.root
.type
== bfd_link_hash_undefined
2694 || h
->root
.root
.type
== bfd_link_hash_undefweak
))
2698 /* Use undefined class. Also, set class and type for some
2700 name
= h
->root
.root
.root
.string
;
2701 if (strcmp (name
, mips_elf_dynsym_rtproc_names
[0]) == 0
2702 || strcmp (name
, mips_elf_dynsym_rtproc_names
[1]) == 0)
2704 h
->esym
.asym
.sc
= scData
;
2705 h
->esym
.asym
.st
= stLabel
;
2706 h
->esym
.asym
.value
= 0;
2708 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[2]) == 0)
2710 h
->esym
.asym
.sc
= scAbs
;
2711 h
->esym
.asym
.st
= stLabel
;
2712 h
->esym
.asym
.value
=
2713 mips_elf_hash_table (einfo
->info
)->procedure_count
;
2715 else if (strcmp (name
, "_gp_disp") == 0)
2717 h
->esym
.asym
.sc
= scAbs
;
2718 h
->esym
.asym
.st
= stLabel
;
2719 h
->esym
.asym
.value
= elf_gp (einfo
->abfd
);
2722 h
->esym
.asym
.sc
= scUndefined
;
2724 else if (h
->root
.root
.type
!= bfd_link_hash_defined
2725 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
2726 h
->esym
.asym
.sc
= scAbs
;
2731 sec
= h
->root
.root
.u
.def
.section
;
2732 output_section
= sec
->output_section
;
2734 /* When making a shared library and symbol h is the one from
2735 the another shared library, OUTPUT_SECTION may be null. */
2736 if (output_section
== NULL
)
2737 h
->esym
.asym
.sc
= scUndefined
;
2740 name
= bfd_section_name (output_section
->owner
, output_section
);
2742 if (strcmp (name
, ".text") == 0)
2743 h
->esym
.asym
.sc
= scText
;
2744 else if (strcmp (name
, ".data") == 0)
2745 h
->esym
.asym
.sc
= scData
;
2746 else if (strcmp (name
, ".sdata") == 0)
2747 h
->esym
.asym
.sc
= scSData
;
2748 else if (strcmp (name
, ".rodata") == 0
2749 || strcmp (name
, ".rdata") == 0)
2750 h
->esym
.asym
.sc
= scRData
;
2751 else if (strcmp (name
, ".bss") == 0)
2752 h
->esym
.asym
.sc
= scBss
;
2753 else if (strcmp (name
, ".sbss") == 0)
2754 h
->esym
.asym
.sc
= scSBss
;
2755 else if (strcmp (name
, ".init") == 0)
2756 h
->esym
.asym
.sc
= scInit
;
2757 else if (strcmp (name
, ".fini") == 0)
2758 h
->esym
.asym
.sc
= scFini
;
2760 h
->esym
.asym
.sc
= scAbs
;
2764 h
->esym
.asym
.reserved
= 0;
2765 h
->esym
.asym
.index
= indexNil
;
2768 if (h
->root
.root
.type
== bfd_link_hash_common
)
2769 h
->esym
.asym
.value
= h
->root
.root
.u
.c
.size
;
2770 else if (h
->root
.root
.type
== bfd_link_hash_defined
2771 || h
->root
.root
.type
== bfd_link_hash_defweak
)
2773 if (h
->esym
.asym
.sc
== scCommon
)
2774 h
->esym
.asym
.sc
= scBss
;
2775 else if (h
->esym
.asym
.sc
== scSCommon
)
2776 h
->esym
.asym
.sc
= scSBss
;
2778 sec
= h
->root
.root
.u
.def
.section
;
2779 output_section
= sec
->output_section
;
2780 if (output_section
!= NULL
)
2781 h
->esym
.asym
.value
= (h
->root
.root
.u
.def
.value
2782 + sec
->output_offset
2783 + output_section
->vma
);
2785 h
->esym
.asym
.value
= 0;
2787 else if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
2789 /* Set type and value for a symbol with a function stub. */
2790 h
->esym
.asym
.st
= stProc
;
2791 sec
= h
->root
.root
.u
.def
.section
;
2793 h
->esym
.asym
.value
= 0;
2796 output_section
= sec
->output_section
;
2797 if (output_section
!= NULL
)
2798 h
->esym
.asym
.value
= (h
->root
.plt_offset
2799 + sec
->output_offset
2800 + output_section
->vma
);
2802 h
->esym
.asym
.value
= 0;
2809 if (! bfd_ecoff_debug_one_external (einfo
->abfd
, einfo
->debug
, einfo
->swap
,
2810 h
->root
.root
.root
.string
,
2813 einfo
->failed
= true;
2820 /* Create a runtime procedure table from the .mdebug section. */
2823 mips_elf_create_procedure_table (handle
, abfd
, info
, s
, debug
)
2826 struct bfd_link_info
*info
;
2828 struct ecoff_debug_info
*debug
;
2830 const struct ecoff_debug_swap
*swap
;
2831 HDRR
*hdr
= &debug
->symbolic_header
;
2833 struct rpdr_ext
*erp
;
2835 struct pdr_ext
*epdr
;
2836 struct sym_ext
*esym
;
2839 unsigned long size
, count
;
2840 unsigned long sindex
;
2844 const char *no_name_func
= "static procedure (no name)";
2851 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2853 sindex
= strlen (no_name_func
) + 1;
2854 count
= hdr
->ipdMax
;
2857 size
= swap
->external_pdr_size
;
2859 epdr
= (struct pdr_ext
*) bfd_malloc (size
* count
);
2863 if (! _bfd_ecoff_get_accumulated_pdr (handle
, (PTR
) epdr
))
2866 size
= sizeof (RPDR
);
2867 rp
= rpdr
= (RPDR
*) bfd_malloc (size
* count
);
2871 sv
= (char **) bfd_malloc (sizeof (char *) * count
);
2875 count
= hdr
->isymMax
;
2876 size
= swap
->external_sym_size
;
2877 esym
= (struct sym_ext
*) bfd_malloc (size
* count
);
2881 if (! _bfd_ecoff_get_accumulated_sym (handle
, (PTR
) esym
))
2884 count
= hdr
->issMax
;
2885 ss
= (char *) bfd_malloc (count
);
2888 if (! _bfd_ecoff_get_accumulated_ss (handle
, (PTR
) ss
))
2891 count
= hdr
->ipdMax
;
2892 for (i
= 0; i
< count
; i
++, rp
++, epdr
++)
2894 (*swap
->swap_pdr_in
) (abfd
, (PTR
) epdr
, &pdr
);
2895 (*swap
->swap_sym_in
) (abfd
, (PTR
) &esym
[pdr
.isym
], &sym
);
2896 rp
->adr
= sym
.value
;
2897 rp
->regmask
= pdr
.regmask
;
2898 rp
->regoffset
= pdr
.regoffset
;
2899 rp
->fregmask
= pdr
.fregmask
;
2900 rp
->fregoffset
= pdr
.fregoffset
;
2901 rp
->frameoffset
= pdr
.frameoffset
;
2902 rp
->framereg
= pdr
.framereg
;
2903 rp
->pcreg
= pdr
.pcreg
;
2905 sv
[i
] = ss
+ sym
.iss
;
2906 sindex
+= strlen (sv
[i
]) + 1;
2910 size
= sizeof (struct rpdr_ext
) * (count
+ 2) + sindex
;
2911 size
= BFD_ALIGN (size
, 16);
2912 rtproc
= (PTR
) bfd_alloc (abfd
, size
);
2915 mips_elf_hash_table (info
)->procedure_count
= 0;
2919 mips_elf_hash_table (info
)->procedure_count
= count
+ 2;
2921 erp
= (struct rpdr_ext
*) rtproc
;
2922 memset (rp
, 0, sizeof (struct rpdr_ext
));
2924 str
= (char *) rtproc
+ sizeof (struct rpdr_ext
) * (count
+ 2);
2925 strcpy (str
, no_name_func
);
2926 str
+= strlen (no_name_func
) + 1;
2927 for (i
= 0; i
< count
; i
++)
2929 ecoff_swap_rpdr_out (abfd
, rpdr
+ i
, erp
+ i
);
2930 strcpy (str
, sv
[i
]);
2931 str
+= strlen (sv
[i
]) + 1;
2933 ecoff_put_off (abfd
, (bfd_vma
) -1, (bfd_byte
*) (erp
+ count
)->p_adr
);
2935 /* Set the size and contents of .rtproc section. */
2936 s
->_raw_size
= size
;
2937 s
->contents
= rtproc
;
2939 /* Skip this section later on (I don't think this currently
2940 matters, but someday it might). */
2941 s
->link_order_head
= (struct bfd_link_order
*) NULL
;
2965 /* A comparison routine used to sort .gptab entries. */
2968 gptab_compare (p1
, p2
)
2972 const Elf32_gptab
*a1
= (const Elf32_gptab
*) p1
;
2973 const Elf32_gptab
*a2
= (const Elf32_gptab
*) p2
;
2975 return a1
->gt_entry
.gt_g_value
- a2
->gt_entry
.gt_g_value
;
2978 /* We need to use a special link routine to handle the .reginfo and
2979 the .mdebug sections. We need to merge all instances of these
2980 sections together, not write them all out sequentially. */
2983 mips_elf_final_link (abfd
, info
)
2985 struct bfd_link_info
*info
;
2989 struct bfd_link_order
*p
;
2990 asection
*reginfo_sec
, *mdebug_sec
, *gptab_data_sec
, *gptab_bss_sec
;
2991 asection
*rtproc_sec
;
2992 Elf32_RegInfo reginfo
;
2993 struct ecoff_debug_info debug
;
2994 const struct ecoff_debug_swap
*swap
2995 = get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2996 HDRR
*symhdr
= &debug
.symbolic_header
;
2997 PTR mdebug_handle
= NULL
;
2999 /* Drop the .options section, since it has special semantics which I
3000 haven't bothered to figure out. */
3001 for (secpp
= &abfd
->sections
; *secpp
!= NULL
; secpp
= &(*secpp
)->next
)
3003 if (strcmp ((*secpp
)->name
, ".options") == 0)
3005 for (p
= (*secpp
)->link_order_head
; p
!= NULL
; p
= p
->next
)
3006 if (p
->type
== bfd_indirect_link_order
)
3007 p
->u
.indirect
.section
->flags
&=~ SEC_HAS_CONTENTS
;
3008 (*secpp
)->link_order_head
= NULL
;
3009 *secpp
= (*secpp
)->next
;
3010 --abfd
->section_count
;
3015 /* Get a value for the GP register. */
3016 if (elf_gp (abfd
) == 0)
3018 struct bfd_link_hash_entry
*h
;
3020 h
= bfd_link_hash_lookup (info
->hash
, "_gp", false, false, true);
3021 if (h
!= (struct bfd_link_hash_entry
*) NULL
3022 && h
->type
== bfd_link_hash_defined
)
3023 elf_gp (abfd
) = (h
->u
.def
.value
3024 + h
->u
.def
.section
->output_section
->vma
3025 + h
->u
.def
.section
->output_offset
);
3026 else if (info
->relocateable
)
3030 /* Make up a value. */
3032 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3035 && (strcmp (o
->name
, ".sbss") == 0
3036 || strcmp (o
->name
, ".sdata") == 0
3037 || strcmp (o
->name
, ".lit4") == 0
3038 || strcmp (o
->name
, ".lit8") == 0))
3041 elf_gp (abfd
) = lo
+ ELF_MIPS_GP_OFFSET (abfd
);
3045 /* If the relocate_section function needs to do a reloc
3046 involving the GP value, it should make a reloc_dangerous
3047 callback to warn that GP is not defined. */
3051 /* Go through the sections and collect the .reginfo and .mdebug
3055 gptab_data_sec
= NULL
;
3056 gptab_bss_sec
= NULL
;
3057 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3059 if (strcmp (o
->name
, ".reginfo") == 0)
3061 memset (®info
, 0, sizeof reginfo
);
3063 /* We have found the .reginfo section in the output file.
3064 Look through all the link_orders comprising it and merge
3065 the information together. */
3066 for (p
= o
->link_order_head
;
3067 p
!= (struct bfd_link_order
*) NULL
;
3070 asection
*input_section
;
3072 Elf32_External_RegInfo ext
;
3075 if (p
->type
!= bfd_indirect_link_order
)
3077 if (p
->type
== bfd_fill_link_order
)
3082 input_section
= p
->u
.indirect
.section
;
3083 input_bfd
= input_section
->owner
;
3085 /* The linker emulation code has probably clobbered the
3086 size to be zero bytes. */
3087 if (input_section
->_raw_size
== 0)
3088 input_section
->_raw_size
= sizeof (Elf32_External_RegInfo
);
3090 if (! bfd_get_section_contents (input_bfd
, input_section
,
3096 bfd_mips_elf32_swap_reginfo_in (input_bfd
, &ext
, &sub
);
3098 reginfo
.ri_gprmask
|= sub
.ri_gprmask
;
3099 reginfo
.ri_cprmask
[0] |= sub
.ri_cprmask
[0];
3100 reginfo
.ri_cprmask
[1] |= sub
.ri_cprmask
[1];
3101 reginfo
.ri_cprmask
[2] |= sub
.ri_cprmask
[2];
3102 reginfo
.ri_cprmask
[3] |= sub
.ri_cprmask
[3];
3104 /* ri_gp_value is set by the function
3105 mips_elf_section_processing when the section is
3106 finally written out. */
3108 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3109 elf_link_input_bfd ignores this section. */
3110 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3113 /* Force the section size to the value we want. */
3114 o
->_raw_size
= sizeof (Elf32_External_RegInfo
);
3116 /* Skip this section later on (I don't think this currently
3117 matters, but someday it might). */
3118 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3123 if (strcmp (o
->name
, ".mdebug") == 0)
3125 struct extsym_info einfo
;
3127 /* We have found the .mdebug section in the output file.
3128 Look through all the link_orders comprising it and merge
3129 the information together. */
3130 symhdr
->magic
= swap
->sym_magic
;
3131 /* FIXME: What should the version stamp be? */
3133 symhdr
->ilineMax
= 0;
3137 symhdr
->isymMax
= 0;
3138 symhdr
->ioptMax
= 0;
3139 symhdr
->iauxMax
= 0;
3141 symhdr
->issExtMax
= 0;
3144 symhdr
->iextMax
= 0;
3146 /* We accumulate the debugging information itself in the
3147 debug_info structure. */
3149 debug
.external_dnr
= NULL
;
3150 debug
.external_pdr
= NULL
;
3151 debug
.external_sym
= NULL
;
3152 debug
.external_opt
= NULL
;
3153 debug
.external_aux
= NULL
;
3155 debug
.ssext
= debug
.ssext_end
= NULL
;
3156 debug
.external_fdr
= NULL
;
3157 debug
.external_rfd
= NULL
;
3158 debug
.external_ext
= debug
.external_ext_end
= NULL
;
3160 mdebug_handle
= bfd_ecoff_debug_init (abfd
, &debug
, swap
, info
);
3161 if (mdebug_handle
== (PTR
) NULL
)
3164 if (SGI_COMPAT (abfd
))
3170 static const char * const name
[] =
3171 { ".text", ".init", ".fini", ".data",
3172 ".rodata", ".sdata", ".sbss", ".bss" };
3173 static const int sc
[] = { scText
, scInit
, scFini
, scData
,
3174 scRData
, scSData
, scSBss
, scBss
};
3177 esym
.cobol_main
= 0;
3181 esym
.asym
.iss
= issNil
;
3182 esym
.asym
.st
= stLocal
;
3183 esym
.asym
.reserved
= 0;
3184 esym
.asym
.index
= indexNil
;
3185 for (i
= 0; i
< 8; i
++)
3187 esym
.asym
.sc
= sc
[i
];
3188 s
= bfd_get_section_by_name (abfd
, name
[i
]);
3191 esym
.asym
.value
= s
->vma
;
3192 last
= s
->vma
+ s
->_raw_size
;
3195 esym
.asym
.value
= last
;
3197 if (! bfd_ecoff_debug_one_external (abfd
, &debug
, swap
,
3203 for (p
= o
->link_order_head
;
3204 p
!= (struct bfd_link_order
*) NULL
;
3207 asection
*input_section
;
3209 const struct ecoff_debug_swap
*input_swap
;
3210 struct ecoff_debug_info input_debug
;
3214 if (p
->type
!= bfd_indirect_link_order
)
3216 if (p
->type
== bfd_fill_link_order
)
3221 input_section
= p
->u
.indirect
.section
;
3222 input_bfd
= input_section
->owner
;
3224 if (bfd_get_flavour (input_bfd
) != bfd_target_elf_flavour
3225 || (get_elf_backend_data (input_bfd
)
3226 ->elf_backend_ecoff_debug_swap
) == NULL
)
3228 /* I don't know what a non MIPS ELF bfd would be
3229 doing with a .mdebug section, but I don't really
3230 want to deal with it. */
3234 input_swap
= (get_elf_backend_data (input_bfd
)
3235 ->elf_backend_ecoff_debug_swap
);
3237 BFD_ASSERT (p
->size
== input_section
->_raw_size
);
3239 /* The ECOFF linking code expects that we have already
3240 read in the debugging information and set up an
3241 ecoff_debug_info structure, so we do that now. */
3242 if (! mips_elf_read_ecoff_info (input_bfd
, input_section
,
3246 if (! (bfd_ecoff_debug_accumulate
3247 (mdebug_handle
, abfd
, &debug
, swap
, input_bfd
,
3248 &input_debug
, input_swap
, info
)))
3251 /* Loop through the external symbols. For each one with
3252 interesting information, try to find the symbol in
3253 the linker global hash table and save the information
3254 for the output external symbols. */
3255 eraw_src
= input_debug
.external_ext
;
3256 eraw_end
= (eraw_src
3257 + (input_debug
.symbolic_header
.iextMax
3258 * input_swap
->external_ext_size
));
3260 eraw_src
< eraw_end
;
3261 eraw_src
+= input_swap
->external_ext_size
)
3265 struct mips_elf_link_hash_entry
*h
;
3267 (*input_swap
->swap_ext_in
) (input_bfd
, (PTR
) eraw_src
, &ext
);
3268 if (ext
.asym
.sc
== scNil
3269 || ext
.asym
.sc
== scUndefined
3270 || ext
.asym
.sc
== scSUndefined
)
3273 name
= input_debug
.ssext
+ ext
.asym
.iss
;
3274 h
= mips_elf_link_hash_lookup (mips_elf_hash_table (info
),
3275 name
, false, false, true);
3276 if (h
== NULL
|| h
->esym
.ifd
!= -2)
3282 < input_debug
.symbolic_header
.ifdMax
);
3283 ext
.ifd
= input_debug
.ifdmap
[ext
.ifd
];
3289 /* Free up the information we just read. */
3290 free (input_debug
.line
);
3291 free (input_debug
.external_dnr
);
3292 free (input_debug
.external_pdr
);
3293 free (input_debug
.external_sym
);
3294 free (input_debug
.external_opt
);
3295 free (input_debug
.external_aux
);
3296 free (input_debug
.ss
);
3297 free (input_debug
.ssext
);
3298 free (input_debug
.external_fdr
);
3299 free (input_debug
.external_rfd
);
3300 free (input_debug
.external_ext
);
3302 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3303 elf_link_input_bfd ignores this section. */
3304 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3307 if (SGI_COMPAT (abfd
) && info
->shared
)
3309 /* Create .rtproc section. */
3310 rtproc_sec
= bfd_get_section_by_name (abfd
, ".rtproc");
3311 if (rtproc_sec
== NULL
)
3313 flagword flags
= (SEC_HAS_CONTENTS
| SEC_IN_MEMORY
3316 rtproc_sec
= bfd_make_section (abfd
, ".rtproc");
3317 if (rtproc_sec
== NULL
3318 || ! bfd_set_section_flags (abfd
, rtproc_sec
, flags
)
3319 || ! bfd_set_section_alignment (abfd
, rtproc_sec
, 12))
3323 if (! mips_elf_create_procedure_table (mdebug_handle
, abfd
,
3324 info
, rtproc_sec
, &debug
))
3328 /* Build the external symbol information. */
3331 einfo
.debug
= &debug
;
3333 einfo
.failed
= false;
3334 mips_elf_link_hash_traverse (mips_elf_hash_table (info
),
3335 mips_elf_output_extsym
,
3340 /* Set the size of the .mdebug section. */
3341 o
->_raw_size
= bfd_ecoff_debug_size (abfd
, &debug
, swap
);
3343 /* Skip this section later on (I don't think this currently
3344 matters, but someday it might). */
3345 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3350 if (strncmp (o
->name
, ".gptab.", sizeof ".gptab." - 1) == 0)
3352 const char *subname
;
3355 Elf32_External_gptab
*ext_tab
;
3358 /* The .gptab.sdata and .gptab.sbss sections hold
3359 information describing how the small data area would
3360 change depending upon the -G switch. These sections
3361 not used in executables files. */
3362 if (! info
->relocateable
)
3366 for (p
= o
->link_order_head
;
3367 p
!= (struct bfd_link_order
*) NULL
;
3370 asection
*input_section
;
3372 if (p
->type
!= bfd_indirect_link_order
)
3374 if (p
->type
== bfd_fill_link_order
)
3379 input_section
= p
->u
.indirect
.section
;
3381 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3382 elf_link_input_bfd ignores this section. */
3383 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3386 /* Skip this section later on (I don't think this
3387 currently matters, but someday it might). */
3388 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3390 /* Really remove the section. */
3391 for (secpp
= &abfd
->sections
;
3393 secpp
= &(*secpp
)->next
)
3395 *secpp
= (*secpp
)->next
;
3396 --abfd
->section_count
;
3401 /* There is one gptab for initialized data, and one for
3402 uninitialized data. */
3403 if (strcmp (o
->name
, ".gptab.sdata") == 0)
3405 else if (strcmp (o
->name
, ".gptab.sbss") == 0)
3409 (*_bfd_error_handler
)
3410 ("%s: illegal section name `%s'",
3411 bfd_get_filename (abfd
), o
->name
);
3412 bfd_set_error (bfd_error_nonrepresentable_section
);
3416 /* The linker script always combines .gptab.data and
3417 .gptab.sdata into .gptab.sdata, and likewise for
3418 .gptab.bss and .gptab.sbss. It is possible that there is
3419 no .sdata or .sbss section in the output file, in which
3420 case we must change the name of the output section. */
3421 subname
= o
->name
+ sizeof ".gptab" - 1;
3422 if (bfd_get_section_by_name (abfd
, subname
) == NULL
)
3424 if (o
== gptab_data_sec
)
3425 o
->name
= ".gptab.data";
3427 o
->name
= ".gptab.bss";
3428 subname
= o
->name
+ sizeof ".gptab" - 1;
3429 BFD_ASSERT (bfd_get_section_by_name (abfd
, subname
) != NULL
);
3432 /* Set up the first entry. */
3434 tab
= (Elf32_gptab
*) bfd_malloc (c
* sizeof (Elf32_gptab
));
3437 tab
[0].gt_header
.gt_current_g_value
= elf_gp_size (abfd
);
3438 tab
[0].gt_header
.gt_unused
= 0;
3440 /* Combine the input sections. */
3441 for (p
= o
->link_order_head
;
3442 p
!= (struct bfd_link_order
*) NULL
;
3445 asection
*input_section
;
3449 bfd_size_type gpentry
;
3451 if (p
->type
!= bfd_indirect_link_order
)
3453 if (p
->type
== bfd_fill_link_order
)
3458 input_section
= p
->u
.indirect
.section
;
3459 input_bfd
= input_section
->owner
;
3461 /* Combine the gptab entries for this input section one
3462 by one. We know that the input gptab entries are
3463 sorted by ascending -G value. */
3464 size
= bfd_section_size (input_bfd
, input_section
);
3466 for (gpentry
= sizeof (Elf32_External_gptab
);
3468 gpentry
+= sizeof (Elf32_External_gptab
))
3470 Elf32_External_gptab ext_gptab
;
3471 Elf32_gptab int_gptab
;
3477 if (! (bfd_get_section_contents
3478 (input_bfd
, input_section
, (PTR
) &ext_gptab
,
3479 gpentry
, sizeof (Elf32_External_gptab
))))
3485 bfd_mips_elf32_swap_gptab_in (input_bfd
, &ext_gptab
,
3487 val
= int_gptab
.gt_entry
.gt_g_value
;
3488 add
= int_gptab
.gt_entry
.gt_bytes
- last
;
3491 for (look
= 1; look
< c
; look
++)
3493 if (tab
[look
].gt_entry
.gt_g_value
>= val
)
3494 tab
[look
].gt_entry
.gt_bytes
+= add
;
3496 if (tab
[look
].gt_entry
.gt_g_value
== val
)
3502 Elf32_gptab
*new_tab
;
3505 /* We need a new table entry. */
3506 new_tab
= ((Elf32_gptab
*)
3507 bfd_realloc ((PTR
) tab
,
3508 (c
+ 1) * sizeof (Elf32_gptab
)));
3509 if (new_tab
== NULL
)
3515 tab
[c
].gt_entry
.gt_g_value
= val
;
3516 tab
[c
].gt_entry
.gt_bytes
= add
;
3518 /* Merge in the size for the next smallest -G
3519 value, since that will be implied by this new
3522 for (look
= 1; look
< c
; look
++)
3524 if (tab
[look
].gt_entry
.gt_g_value
< val
3526 || (tab
[look
].gt_entry
.gt_g_value
3527 > tab
[max
].gt_entry
.gt_g_value
)))
3531 tab
[c
].gt_entry
.gt_bytes
+=
3532 tab
[max
].gt_entry
.gt_bytes
;
3537 last
= int_gptab
.gt_entry
.gt_bytes
;
3540 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3541 elf_link_input_bfd ignores this section. */
3542 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3545 /* The table must be sorted by -G value. */
3547 qsort (tab
+ 1, c
- 1, sizeof (tab
[0]), gptab_compare
);
3549 /* Swap out the table. */
3550 ext_tab
= ((Elf32_External_gptab
*)
3551 bfd_alloc (abfd
, c
* sizeof (Elf32_External_gptab
)));
3552 if (ext_tab
== NULL
)
3558 for (i
= 0; i
< c
; i
++)
3559 bfd_mips_elf32_swap_gptab_out (abfd
, tab
+ i
, ext_tab
+ i
);
3562 o
->_raw_size
= c
* sizeof (Elf32_External_gptab
);
3563 o
->contents
= (bfd_byte
*) ext_tab
;
3565 /* Skip this section later on (I don't think this currently
3566 matters, but someday it might). */
3567 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3571 /* Invoke the regular ELF backend linker to do all the work. */
3572 if (! bfd_elf32_bfd_final_link (abfd
, info
))
3575 /* Now write out the computed sections. */
3577 if (reginfo_sec
!= (asection
*) NULL
)
3579 Elf32_External_RegInfo ext
;
3581 bfd_mips_elf32_swap_reginfo_out (abfd
, ®info
, &ext
);
3582 if (! bfd_set_section_contents (abfd
, reginfo_sec
, (PTR
) &ext
,
3583 (file_ptr
) 0, sizeof ext
))
3587 if (mdebug_sec
!= (asection
*) NULL
)
3589 BFD_ASSERT (abfd
->output_has_begun
);
3590 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle
, abfd
, &debug
,
3592 mdebug_sec
->filepos
))
3595 bfd_ecoff_debug_free (mdebug_handle
, abfd
, &debug
, swap
, info
);
3598 if (gptab_data_sec
!= (asection
*) NULL
)
3600 if (! bfd_set_section_contents (abfd
, gptab_data_sec
,
3601 gptab_data_sec
->contents
,
3603 gptab_data_sec
->_raw_size
))
3607 if (gptab_bss_sec
!= (asection
*) NULL
)
3609 if (! bfd_set_section_contents (abfd
, gptab_bss_sec
,
3610 gptab_bss_sec
->contents
,
3612 gptab_bss_sec
->_raw_size
))
3616 if (SGI_COMPAT (abfd
))
3618 rtproc_sec
= bfd_get_section_by_name (abfd
, ".rtproc");
3619 if (rtproc_sec
!= NULL
)
3621 if (! bfd_set_section_contents (abfd
, rtproc_sec
,
3622 rtproc_sec
->contents
,
3624 rtproc_sec
->_raw_size
))
3632 /* Handle a MIPS ELF HI16 reloc. */
3635 mips_elf_relocate_hi16 (input_bfd
, relhi
, rello
, contents
, addend
)
3637 Elf_Internal_Rela
*relhi
;
3638 Elf_Internal_Rela
*rello
;
3645 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
3647 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
3650 addend
+= ((insn
& 0xffff) << 16) + addlo
;
3652 if ((addlo
& 0x8000) != 0)
3654 if ((addend
& 0x8000) != 0)
3657 bfd_put_32 (input_bfd
,
3658 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
3659 contents
+ relhi
->r_offset
);
3662 /* Handle a MIPS ELF local GOT16 reloc. */
3665 mips_elf_relocate_got_local (output_bfd
, input_bfd
, sgot
, relhi
, rello
,
3670 Elf_Internal_Rela
*relhi
;
3671 Elf_Internal_Rela
*rello
;
3681 bfd_byte
*got_contents
;
3682 struct mips_got_info
*g
;
3684 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
3686 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
3689 addend
+= ((insn
& 0xffff) << 16) + addlo
;
3691 if ((addlo
& 0x8000) != 0)
3693 if ((addend
& 0x8000) != 0)
3696 /* Get a got entry representing requested hipage. */
3697 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
3698 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
3699 BFD_ASSERT (g
!= NULL
);
3701 local_gotno
= g
->local_gotno
;
3702 got_contents
= sgot
->contents
;
3703 hipage
= addend
& 0xffff0000;
3705 for (i
= MIPS_RESERVED_GOTNO
; i
< local_gotno
; i
++)
3707 address
= bfd_get_32 (input_bfd
, got_contents
+ i
* 4);
3708 if (hipage
== (address
& 0xffff0000))
3710 if (address
== (bfd_vma
) 0)
3712 bfd_put_32 (input_bfd
, hipage
, got_contents
+ i
* 4);
3717 BFD_ASSERT (i
< local_gotno
);
3719 if (i
== local_gotno
)
3720 (*_bfd_error_handler
)
3721 ("ELF MIPS linker: more got entries are needed for hipage: %x",
3725 i
= - ELF_MIPS_GP_OFFSET (output_bfd
) + i
* 4;
3726 bfd_put_32 (input_bfd
, (insn
& 0xffff0000) | (i
& 0xffff),
3727 contents
+ relhi
->r_offset
);
3730 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
3733 mips_elf_relocate_global_got (input_bfd
, rel
, contents
, offset
)
3735 Elf_Internal_Rela
*rel
;
3741 insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3742 bfd_put_32 (input_bfd
,
3743 (insn
& 0xffff0000) | (offset
& 0xffff),
3744 contents
+ rel
->r_offset
);
3747 /* Relocate a MIPS ELF section. */
3750 mips_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
3751 contents
, relocs
, local_syms
, local_sections
)
3753 struct bfd_link_info
*info
;
3755 asection
*input_section
;
3757 Elf_Internal_Rela
*relocs
;
3758 Elf_Internal_Sym
*local_syms
;
3759 asection
**local_sections
;
3761 Elf_Internal_Shdr
*symtab_hdr
;
3764 asection
*sgot
, *sreloc
, *scpt
;
3766 Elf_Internal_Rela
*rel
;
3767 Elf_Internal_Rela
*relend
;
3768 struct mips_got_info
*g
;
3770 dynobj
= elf_hash_table (info
)->dynobj
;
3771 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3775 if (dynobj
== NULL
|| ! SGI_COMPAT (output_bfd
))
3778 scpt
= bfd_get_section_by_name (dynobj
, ".compact_rel");
3781 if (elf_bad_symtab (input_bfd
))
3783 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
3788 locsymcount
= symtab_hdr
->sh_info
;
3789 extsymoff
= symtab_hdr
->sh_info
;
3793 relend
= relocs
+ input_section
->reloc_count
;
3794 for (; rel
< relend
; rel
++)
3797 reloc_howto_type
*howto
;
3798 unsigned long r_symndx
;
3800 struct elf_link_hash_entry
*h
;
3802 Elf_Internal_Sym
*sym
;
3803 bfd_reloc_status_type r
;
3805 r_type
= ELF32_R_TYPE (rel
->r_info
);
3806 if (r_type
< 0 || r_type
>= (int) R_MIPS_max
)
3808 bfd_set_error (bfd_error_bad_value
);
3811 howto
= elf_mips_howto_table
+ r_type
;
3814 && (r_type
== R_MIPS_CALL16
3815 || r_type
== R_MIPS_GOT16
3816 || r_type
== R_MIPS_CALL_HI16
3817 || r_type
== R_MIPS_CALL_LO16
3818 || r_type
== R_MIPS_GOT_HI16
3819 || r_type
== R_MIPS_GOT_LO16
))
3821 /* We need the .got section. */
3824 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3825 BFD_ASSERT (sgot
!= NULL
);
3826 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
3827 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
3828 BFD_ASSERT (g
!= NULL
);
3832 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3834 /* Mix in the change in GP address for a GP relative reloc. */
3835 if (r_type
!= R_MIPS_GPREL16
3836 && r_type
!= R_MIPS_LITERAL
3837 && r_type
!= R_MIPS_GPREL32
)
3841 if (elf_gp (output_bfd
) == 0)
3843 if (! ((*info
->callbacks
->reloc_dangerous
)
3845 "GP relative relocation when GP not defined",
3846 input_bfd
, input_section
,
3849 /* Only give the error once per link. */
3850 elf_gp (output_bfd
) = 4;
3853 if (r_symndx
< extsymoff
3854 || (elf_bad_symtab (input_bfd
)
3855 && local_sections
[r_symndx
] != NULL
))
3857 /* This is a relocation against a section. The current
3858 addend in the instruction is the difference between
3859 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
3860 must change this to be the difference between the
3861 final definition (which will end up in RELOCATION)
3862 and the GP value of OUTPUT_BFD (which is in GP). */
3863 addend
= elf_gp (input_bfd
) - elf_gp (output_bfd
);
3865 else if (! info
->relocateable
)
3867 /* We are doing a final link. The current addend in the
3868 instruction is simply the desired offset into the
3869 symbol (normally zero). We want the instruction to
3870 hold the difference between the final definition of
3871 the symbol (which will end up in RELOCATION) and the
3872 GP value of OUTPUT_BFD (which is in GP). */
3873 addend
= - elf_gp (output_bfd
);
3877 /* We are generating relocateable output, and we aren't
3878 going to define this symbol, so we just leave the
3879 instruction alone. */
3887 if (info
->relocateable
)
3889 /* This is a relocateable link. We don't have to change
3890 anything, unless the reloc is against a section symbol,
3891 in which case we have to adjust according to where the
3892 section symbol winds up in the output section. */
3893 if (r_symndx
>= locsymcount
3894 || (elf_bad_symtab (input_bfd
)
3895 && local_sections
[r_symndx
] == NULL
))
3899 sym
= local_syms
+ r_symndx
;
3900 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3904 sec
= local_sections
[r_symndx
];
3906 /* It would be logical to add sym->st_value here,
3907 but Irix 5 sometimes generates a garbage symbol
3909 addend
+= sec
->output_offset
;
3911 /* If this is HI16 or GOT16 with an associated LO16,
3912 adjust the addend accordingly. Otherwise, just
3914 if ((r_type
!= R_MIPS_HI16
&& r_type
!= R_MIPS_GOT16
)
3915 || (rel
+ 1) >= relend
3916 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
3917 r
= _bfd_relocate_contents (howto
, input_bfd
,
3919 contents
+ rel
->r_offset
);
3922 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
3934 /* This is a final link. */
3936 if (r_symndx
< extsymoff
3937 || (elf_bad_symtab (input_bfd
)
3938 && local_sections
[r_symndx
] != NULL
))
3941 sym
= local_syms
+ r_symndx
;
3942 sec
= local_sections
[r_symndx
];
3943 relocation
= (sec
->output_section
->vma
3944 + sec
->output_offset
);
3946 /* It would be logical to always add sym->st_value here,
3947 but Irix 5 sometimes generates a garbage symbol
3949 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3950 relocation
+= sym
->st_value
;
3957 indx
= r_symndx
- extsymoff
;
3958 h
= elf_sym_hashes (input_bfd
)[indx
];
3959 if (strcmp (h
->root
.root
.string
, "_gp_disp") == 0)
3961 if (elf_gp (output_bfd
) == 0)
3963 if (! ((*info
->callbacks
->reloc_dangerous
)
3965 "_gp_disp used when GP not defined",
3966 input_bfd
, input_section
,
3969 /* Only give the error once per link. */
3970 elf_gp (output_bfd
) = 4;
3975 sec
= input_section
;
3976 if (sec
->output_section
!= NULL
)
3977 relocation
= (elf_gp (output_bfd
)
3979 + sec
->output_section
->vma
3980 + sec
->output_offset
));
3982 relocation
= elf_gp (output_bfd
) - rel
->r_offset
;
3983 if (r_type
== R_MIPS_LO16
)
3987 else if (h
->root
.type
== bfd_link_hash_defined
3988 || h
->root
.type
== bfd_link_hash_defweak
)
3990 sec
= h
->root
.u
.def
.section
;
3991 if (sec
->output_section
== NULL
)
3994 relocation
= (h
->root
.u
.def
.value
3995 + sec
->output_section
->vma
3996 + sec
->output_offset
);
3998 else if (h
->root
.type
== bfd_link_hash_undefweak
)
4000 else if (info
->shared
&& ! info
->symbolic
)
4002 else if (strcmp (h
->root
.root
.string
, "_DYNAMIC_LINK") == 0)
4004 /* If this is a dynamic link, we should have created
4005 a _DYNAMIC_LINK symbol in
4006 mips_elf_create_dynamic_sections. Otherwise, we
4007 should define the symbol with a value of 0.
4008 FIXME: It should probably get into the symbol
4009 table somehow as well. */
4010 BFD_ASSERT (! info
->shared
);
4011 BFD_ASSERT (bfd_get_section_by_name (output_bfd
,
4012 ".dynamic") == NULL
);
4017 if (! ((*info
->callbacks
->undefined_symbol
)
4018 (info
, h
->root
.root
.string
, input_bfd
,
4019 input_section
, rel
->r_offset
)))
4025 if (r_type
== R_MIPS_HI16
4026 && (rel
+ 1) < relend
4027 && ELF32_R_TYPE ((rel
+ 1)->r_info
) == R_MIPS_LO16
)
4029 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
4030 contents
, relocation
+ addend
);
4033 else if (r_type
== R_MIPS_GOT16
&& local
)
4035 /* GOT16 must be also with associated LO16 in the local
4036 case. In this case, the addend is extracted and the
4037 section in which the referenced object is determined.
4038 Then the final address of the object is computed and
4039 the GOT entry for the hipage (an aligned 64kb chunk)
4040 is added to .got section if needed. The offset field
4041 of the GOT16-relocated instruction is replaced by the
4042 index of this GOT entry for the hipage. */
4043 if ((rel
+ 1) < relend
4044 && ELF32_R_TYPE ((rel
+ 1)->r_info
) == R_MIPS_LO16
)
4046 mips_elf_relocate_got_local (output_bfd
, input_bfd
, sgot
,
4049 relocation
+ addend
);
4053 r
= bfd_reloc_outofrange
;
4055 else if (r_type
== R_MIPS_CALL16
4056 || r_type
== R_MIPS_GOT16
4057 || r_type
== R_MIPS_CALL_LO16
4058 || r_type
== R_MIPS_GOT_LO16
)
4062 /* This symbol must be registered as a global symbol
4063 having the corresponding got entry. */
4064 BFD_ASSERT (h
->got_offset
!= (bfd_vma
) -1);
4066 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
4067 BFD_ASSERT (g
->local_gotno
<= offset
4068 && offset
< sgot
->_raw_size
);
4069 bfd_put_32 (output_bfd
, relocation
+ addend
,
4070 sgot
->contents
+ offset
);
4071 offset
= (sgot
->output_section
->vma
+ sgot
->output_offset
4072 + offset
- elf_gp (output_bfd
));
4073 mips_elf_relocate_global_got (input_bfd
, rel
, contents
,
4077 else if (r_type
== R_MIPS_CALL_HI16
4078 || r_type
== R_MIPS_GOT_HI16
)
4082 /* This must be a global symbol with a got entry. The
4083 next reloc must be the corresponding LO16 reloc. */
4084 BFD_ASSERT (h
!= NULL
&& h
->got_offset
!= (bfd_vma
) -1);
4085 BFD_ASSERT ((rel
+ 1) < relend
);
4086 BFD_ASSERT (ELF32_R_TYPE ((rel
+ 1)->r_info
)
4087 == (r_type
== R_MIPS_CALL_HI16
4089 : R_MIPS_GOT_LO16
));
4091 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
4092 BFD_ASSERT (g
->local_gotno
<= offset
4093 && offset
< sgot
->_raw_size
);
4094 bfd_put_32 (output_bfd
, relocation
+ addend
,
4095 sgot
->contents
+ offset
);
4096 offset
= (sgot
->output_section
->vma
+ sgot
->output_offset
4097 + offset
- elf_gp (output_bfd
));
4098 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1, contents
,
4102 else if (r_type
== R_MIPS_REL32
4103 || r_type
== R_MIPS_32
)
4105 Elf_Internal_Rel outrel
;
4106 Elf32_crinfo cptrel
;
4110 && (input_section
->flags
& SEC_ALLOC
) != 0)
4112 /* When generating a shared object, these
4113 relocations are copied into the output file to be
4114 resolved at run time. */
4117 sreloc
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
4118 BFD_ASSERT (sreloc
!= NULL
);
4121 outrel
.r_offset
= (rel
->r_offset
4122 + input_section
->output_section
->vma
4123 + input_section
->output_offset
);
4125 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
4128 && (! info
->symbolic
4129 || (h
->elf_link_hash_flags
4130 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
4132 BFD_ASSERT (h
->dynindx
!= -1);
4133 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MIPS_REL32
);
4134 sec
= input_section
;
4141 sec
= local_sections
[r_symndx
];
4144 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4146 == bfd_link_hash_defweak
));
4147 sec
= h
->root
.u
.def
.section
;
4149 if (sec
!= NULL
&& bfd_is_abs_section (sec
))
4151 else if (sec
== NULL
|| sec
->owner
== NULL
)
4153 bfd_set_error (bfd_error_bad_value
);
4160 osec
= sec
->output_section
;
4161 indx
= elf_section_data (osec
)->dynindx
;
4166 outrel
.r_info
= ELF32_R_INFO (indx
, R_MIPS_REL32
);
4167 addend
+= relocation
;
4170 bfd_put_32 (output_bfd
, addend
, contents
+ rel
->r_offset
);
4171 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
4172 (((Elf32_External_Rel
*)
4174 + sreloc
->reloc_count
));
4175 ++sreloc
->reloc_count
;
4177 if (SGI_COMPAT (output_bfd
))
4182 /* Make an entry of compact relocation info. */
4183 mips_elf_set_cr_format (cptrel
, CRF_MIPS_LONG
);
4184 cptrel
.vaddr
= (rel
->r_offset
4185 + input_section
->output_section
->vma
4186 + input_section
->output_offset
);
4187 if (r_type
== R_MIPS_REL32
)
4188 mips_elf_set_cr_type (cptrel
, CRT_MIPS_REL32
);
4190 mips_elf_set_cr_type (cptrel
, CRT_MIPS_WORD
);
4191 mips_elf_set_cr_dist2to (cptrel
, 0);
4192 cptrel
.konst
= addend
;
4194 cr
= (scpt
->contents
4195 + sizeof (Elf32_External_compact_rel
));
4196 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4197 ((Elf32_External_crinfo
*) cr
4198 + scpt
->reloc_count
));
4199 ++scpt
->reloc_count
;
4202 /* This reloc will be computed at runtime, so
4203 there's no need to do anything now. */
4207 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4208 contents
, rel
->r_offset
,
4209 relocation
, addend
);
4212 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4213 contents
, rel
->r_offset
,
4214 relocation
, addend
);
4216 if (SGI_COMPAT (abfd
)
4218 && (input_section
->flags
& SEC_ALLOC
) != 0)
4220 Elf32_crinfo cptrel
;
4223 /* Make an entry of compact relocation info. */
4224 mips_elf_set_cr_format (cptrel
, CRF_MIPS_LONG
);
4225 cptrel
.vaddr
= (rel
->r_offset
4226 + input_section
->output_section
->vma
4227 + input_section
->output_offset
);
4232 mips_elf_set_cr_type (cptrel
, CRT_MIPS_JMPAD
);
4233 /* XXX How should we set dist2to in this case. */
4234 mips_elf_set_cr_dist2to (cptrel
, 8);
4235 cptrel
.konst
= addend
+ relocation
;
4236 cr
= scpt
->contents
+ sizeof (Elf32_External_compact_rel
);
4237 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4238 ((Elf32_External_crinfo
*) cr
4239 + scpt
->reloc_count
));
4240 ++scpt
->reloc_count
;
4243 case R_MIPS_GPREL16
:
4244 case R_MIPS_LITERAL
:
4245 case R_MIPS_GPREL32
:
4246 mips_elf_set_cr_type (cptrel
, CRT_MIPS_GPHI_LO
);
4247 cptrel
.konst
= elf_gp (output_bfd
) - cptrel
.vaddr
;
4248 mips_elf_set_cr_dist2to (cptrel
, 4);
4249 cr
= scpt
->contents
+ sizeof (Elf32_External_compact_rel
);
4250 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4251 ((Elf32_External_crinfo
*) cr
4252 + scpt
->reloc_count
));
4253 ++scpt
->reloc_count
;
4262 if (r
!= bfd_reloc_ok
)
4267 case bfd_reloc_outofrange
:
4269 case bfd_reloc_overflow
:
4274 name
= h
->root
.root
.string
;
4277 name
= bfd_elf_string_from_elf_section (input_bfd
,
4278 symtab_hdr
->sh_link
,
4283 name
= bfd_section_name (input_bfd
, sec
);
4285 if (! ((*info
->callbacks
->reloc_overflow
)
4286 (info
, name
, howto
->name
, (bfd_vma
) 0,
4287 input_bfd
, input_section
, rel
->r_offset
)))
4298 /* Functions for the dynamic linker. */
4300 /* The name of the dynamic interpreter. This is put in the .interp
4303 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
4305 /* Create dynamic sections when linking against a dynamic object. */
4308 mips_elf_create_dynamic_sections (abfd
, info
)
4310 struct bfd_link_info
*info
;
4312 struct elf_link_hash_entry
*h
;
4314 register asection
*s
;
4315 const char * const *namep
;
4317 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4320 /* Mips ABI requests the .dynamic section to be read only. */
4321 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4324 if (! bfd_set_section_flags (abfd
, s
, flags
))
4328 /* We need to create .got section. */
4329 if (! mips_elf_create_got_section (abfd
, info
))
4332 /* Create .stub section. */
4333 if (bfd_get_section_by_name (abfd
, ".stub") == NULL
)
4335 s
= bfd_make_section (abfd
, ".stub");
4337 || ! bfd_set_section_flags (abfd
, s
, flags
)
4338 || ! bfd_set_section_alignment (abfd
, s
, 2))
4342 if (SGI_COMPAT (abfd
))
4344 for (namep
= mips_elf_dynsym_rtproc_names
; *namep
!= NULL
; namep
++)
4347 if (! (_bfd_generic_link_add_one_symbol
4348 (info
, abfd
, *namep
, BSF_GLOBAL
, bfd_und_section_ptr
,
4349 (bfd_vma
) 0, (const char *) NULL
, false,
4350 get_elf_backend_data (abfd
)->collect
,
4351 (struct bfd_link_hash_entry
**) &h
)))
4353 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4354 h
->type
= STT_SECTION
;
4356 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4360 /* We need to create a .compact_rel section. */
4361 if (! mips_elf_create_compact_rel_section (abfd
, info
))
4364 /* Change aligments of some sections. */
4365 s
= bfd_get_section_by_name (abfd
, ".hash");
4367 bfd_set_section_alignment (abfd
, s
, 4);
4368 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4370 bfd_set_section_alignment (abfd
, s
, 4);
4371 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4373 bfd_set_section_alignment (abfd
, s
, 4);
4374 s
= bfd_get_section_by_name (abfd
, ".reginfo");
4376 bfd_set_section_alignment (abfd
, s
, 4);
4377 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4379 bfd_set_section_alignment (abfd
, s
, 4);
4385 if (! (_bfd_generic_link_add_one_symbol
4386 (info
, abfd
, "_DYNAMIC_LINK", BSF_GLOBAL
, bfd_abs_section_ptr
,
4387 (bfd_vma
) 0, (const char *) NULL
, false,
4388 get_elf_backend_data (abfd
)->collect
,
4389 (struct bfd_link_hash_entry
**) &h
)))
4391 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4392 h
->type
= STT_SECTION
;
4394 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4401 /* Create the .compact_rel section. */
4404 mips_elf_create_compact_rel_section (abfd
, info
)
4406 struct bfd_link_info
*info
;
4409 register asection
*s
;
4411 if (bfd_get_section_by_name (abfd
, ".compact_rel") == NULL
)
4413 flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
;
4415 s
= bfd_make_section (abfd
, ".compact_rel");
4417 || ! bfd_set_section_flags (abfd
, s
, flags
)
4418 || ! bfd_set_section_alignment (abfd
, s
, 2))
4421 s
->_raw_size
= sizeof (Elf32_External_compact_rel
);
4427 /* Create the .got section to hold the global offset table. */
4430 mips_elf_create_got_section (abfd
, info
)
4432 struct bfd_link_info
*info
;
4435 register asection
*s
;
4436 struct elf_link_hash_entry
*h
;
4437 struct mips_got_info
*g
;
4439 /* This function may be called more than once. */
4440 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
4443 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
4445 s
= bfd_make_section (abfd
, ".got");
4447 || ! bfd_set_section_flags (abfd
, s
, flags
)
4448 || ! bfd_set_section_alignment (abfd
, s
, 4))
4451 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4452 linker script because we don't want to define the symbol if we
4453 are not creating a global offset table. */
4455 if (! (_bfd_generic_link_add_one_symbol
4456 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
4457 (bfd_vma
) 0, (const char *) NULL
, false,
4458 get_elf_backend_data (abfd
)->collect
,
4459 (struct bfd_link_hash_entry
**) &h
)))
4461 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4462 h
->type
= STT_OBJECT
;
4465 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4468 /* The first several global offset table entries are reserved. */
4469 s
->_raw_size
= MIPS_RESERVED_GOTNO
* 4;
4471 g
= (struct mips_got_info
*) bfd_alloc (abfd
,
4472 sizeof (struct mips_got_info
));
4475 g
->global_gotsym
= 0;
4476 g
->local_gotno
= MIPS_RESERVED_GOTNO
;
4477 if (elf_section_data (s
) == NULL
)
4480 (PTR
) bfd_zalloc (abfd
, sizeof (struct bfd_elf_section_data
));
4481 if (elf_section_data (s
) == NULL
)
4484 elf_section_data (s
)->tdata
= (PTR
) g
;
4489 /* Look through the relocs for a section during the first phase, and
4490 allocate space in the global offset table. */
4493 mips_elf_check_relocs (abfd
, info
, sec
, relocs
)
4495 struct bfd_link_info
*info
;
4497 const Elf_Internal_Rela
*relocs
;
4500 Elf_Internal_Shdr
*symtab_hdr
;
4501 struct elf_link_hash_entry
**sym_hashes
;
4502 struct mips_got_info
*g
;
4504 const Elf_Internal_Rela
*rel
;
4505 const Elf_Internal_Rela
*rel_end
;
4509 if (info
->relocateable
)
4512 dynobj
= elf_hash_table (info
)->dynobj
;
4513 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4514 sym_hashes
= elf_sym_hashes (abfd
);
4515 extsymoff
= (elf_bad_symtab (abfd
)) ? 0 : symtab_hdr
->sh_info
;
4520 rel_end
= relocs
+ sec
->reloc_count
;
4521 for (rel
= relocs
; rel
< rel_end
; rel
++)
4523 unsigned long r_symndx
;
4524 struct elf_link_hash_entry
*h
;
4526 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4528 if (r_symndx
< extsymoff
)
4531 h
= sym_hashes
[r_symndx
- extsymoff
];
4533 /* Some relocs require a global offset table. */
4536 switch (ELF32_R_TYPE (rel
->r_info
))
4540 case R_MIPS_CALL_HI16
:
4541 case R_MIPS_CALL_LO16
:
4542 case R_MIPS_GOT_HI16
:
4543 case R_MIPS_GOT_LO16
:
4544 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
4545 if (! mips_elf_create_got_section (dynobj
, info
))
4554 switch (ELF32_R_TYPE (rel
->r_info
))
4557 case R_MIPS_CALL_HI16
:
4558 case R_MIPS_CALL_LO16
:
4559 /* This symbol requires a global offset table entry. */
4562 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4563 BFD_ASSERT (sgot
!= NULL
);
4564 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4565 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4566 BFD_ASSERT (g
!= NULL
);
4569 BFD_ASSERT (h
!= NULL
);
4571 /* Make sure this symbol is output as a dynamic symbol. */
4572 if (h
->dynindx
== -1)
4574 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4578 if (h
->got_offset
!= (bfd_vma
) -1)
4580 /* We have already allocated space in the .got. */
4584 /* Note the index of the first global got symbol in .dynsym. */
4585 if (g
->global_gotsym
== 0
4586 || g
->global_gotsym
> (unsigned long) h
->dynindx
)
4587 g
->global_gotsym
= h
->dynindx
;
4589 /* Make this symbol to have the corresponding got entry. */
4592 /* We need a stub, not a plt entry for the undefined
4593 function. But we record it as if it needs plt. See
4594 elf_adjust_dynamic_symbol in elflink.h. */
4595 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
4601 case R_MIPS_GOT_HI16
:
4602 case R_MIPS_GOT_LO16
:
4603 /* This symbol requires a global offset table entry. */
4606 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4607 BFD_ASSERT (sgot
!= NULL
);
4608 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4609 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4610 BFD_ASSERT (g
!= NULL
);
4615 /* Make sure this symbol is output as a dynamic symbol. */
4616 if (h
->dynindx
== -1)
4618 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4622 if (h
->got_offset
!= (bfd_vma
) -1)
4624 /* We have already allocated space in the .got. */
4627 /* Note the index of the first global got symbol in
4629 if (g
->global_gotsym
== 0
4630 || g
->global_gotsym
> (unsigned long) h
->dynindx
)
4631 g
->global_gotsym
= h
->dynindx
;
4633 /* Make this symbol to be the global got symbol. */
4642 && (sec
->flags
& SEC_ALLOC
) != 0)
4644 /* When creating a shared object, we must copy these
4645 reloc types into the output file as R_MIPS_REL32
4646 relocs. We create the .rel.dyn reloc section in
4647 dynobj and make room for this reloc. */
4650 const char *name
= ".rel.dyn";
4652 sreloc
= bfd_get_section_by_name (dynobj
, name
);
4655 sreloc
= bfd_make_section (dynobj
, name
);
4657 || ! bfd_set_section_flags (dynobj
, sreloc
,
4663 || ! bfd_set_section_alignment (dynobj
, sreloc
, 4))
4666 /* Add a null element. */
4667 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
4668 ++sreloc
->reloc_count
;
4672 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
4675 if (SGI_COMPAT (abfd
))
4676 mips_elf_hash_table (info
)->compact_rel_size
+=
4677 sizeof (Elf32_External_crinfo
);
4682 case R_MIPS_GPREL16
:
4683 case R_MIPS_LITERAL
:
4684 case R_MIPS_GPREL32
:
4685 if (SGI_COMPAT (abfd
))
4686 mips_elf_hash_table (info
)->compact_rel_size
+=
4687 sizeof (Elf32_External_crinfo
);
4698 /* Adjust a symbol defined by a dynamic object and referenced by a
4699 regular object. The current definition is in some section of the
4700 dynamic object, but we're not including those sections. We have to
4701 change the definition to something the rest of the link can
4705 mips_elf_adjust_dynamic_symbol (info
, h
)
4706 struct bfd_link_info
*info
;
4707 struct elf_link_hash_entry
*h
;
4712 dynobj
= elf_hash_table (info
)->dynobj
;
4714 /* Make sure we know what is going on here. */
4715 BFD_ASSERT (dynobj
!= NULL
4716 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
4717 || h
->weakdef
!= NULL
4718 || ((h
->elf_link_hash_flags
4719 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4720 && (h
->elf_link_hash_flags
4721 & ELF_LINK_HASH_REF_REGULAR
) != 0
4722 && (h
->elf_link_hash_flags
4723 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
4725 /* For a function, create a stub, if needed. */
4726 if (h
->type
== STT_FUNC
4727 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
4729 if (! elf_hash_table (info
)->dynamic_sections_created
)
4732 /* If this symbol is not defined in a regular file, then set
4733 the symbol to the stub location. This is required to make
4734 function pointers compare as equal between the normal
4735 executable and the shared library. */
4736 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4738 /* We need .stub section. */
4739 s
= bfd_get_section_by_name (dynobj
, ".stub");
4740 BFD_ASSERT (s
!= NULL
);
4742 h
->root
.u
.def
.section
= s
;
4743 h
->root
.u
.def
.value
= s
->_raw_size
;
4745 /* XXX Write this stub address somewhere. */
4746 h
->plt_offset
= s
->_raw_size
;
4748 /* Make room for this stub code. */
4749 s
->_raw_size
+= MIPS_FUNCTION_STUB_SIZE
;
4751 /* The last half word of the stub will be filled with the index
4752 of this symbol in .dynsym section. */
4757 /* If this is a weak symbol, and there is a real definition, the
4758 processor independent code will have arranged for us to see the
4759 real definition first, and we can just use the same value. */
4760 if (h
->weakdef
!= NULL
)
4762 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
4763 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
4764 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
4765 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
4769 /* This is a reference to a symbol defined by a dynamic object which
4770 is not a function. */
4775 /* Set the sizes of the dynamic sections. */
4778 mips_elf_size_dynamic_sections (output_bfd
, info
)
4780 struct bfd_link_info
*info
;
4786 struct mips_got_info
*g
;
4788 dynobj
= elf_hash_table (info
)->dynobj
;
4789 BFD_ASSERT (dynobj
!= NULL
);
4791 if (elf_hash_table (info
)->dynamic_sections_created
)
4793 /* Set the contents of the .interp section to the interpreter. */
4796 s
= bfd_get_section_by_name (dynobj
, ".interp");
4797 BFD_ASSERT (s
!= NULL
);
4798 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4799 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4803 /* Recompute the size of .got for local entires (reserved and
4804 hipages) if needed. To estimate it, get the upper bound of total
4805 size of loadable sections. */
4806 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4810 bfd_size_type loadable_size
= 0;
4811 bfd_size_type local_gotno
;
4814 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4815 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4816 BFD_ASSERT (g
!= NULL
);
4818 for (sub
= info
->input_bfds
; sub
; sub
= sub
->link_next
)
4819 for (s
= sub
->sections
; s
!= NULL
; s
= s
->next
)
4821 if ((s
->flags
& SEC_ALLOC
) == 0)
4823 loadable_size
+= (s
->_raw_size
+ 0xf) & ~0xf;
4826 loadable_size
+= MIPS_FUNCTION_STUB_SIZE
;
4828 /* Assume there are two loadable segments consisting of
4829 contiguous sections. Is 5 enough? */
4830 local_gotno
= (loadable_size
>> 16) + 5 + MIPS_RESERVED_GOTNO
;
4831 g
->local_gotno
= local_gotno
;
4832 sgot
->_raw_size
+= local_gotno
* 4;
4835 /* The check_relocs and adjust_dynamic_symbol entry points have
4836 determined the sizes of the various dynamic sections. Allocate
4839 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4844 /* It's OK to base decisions on the section name, because none
4845 of the dynobj section names depend upon the input files. */
4846 name
= bfd_get_section_name (dynobj
, s
);
4848 if ((s
->flags
& SEC_IN_MEMORY
) == 0)
4853 if (strncmp (name
, ".rel", 4) == 0)
4855 if (s
->_raw_size
== 0)
4861 /* If this relocation section applies to a read only
4862 section, then we probably need a DT_TEXTREL entry.
4863 If the relocation section is .rel.dyn, we always
4864 assert a DT_TEXTREL entry rather than testing whether
4865 there exists a relocation to a read only section or
4867 target
= bfd_get_section_by_name (output_bfd
, name
+ 4);
4868 if ((target
!= NULL
&& (target
->flags
& SEC_READONLY
) != 0)
4869 || strcmp (name
, ".rel.dyn") == 0)
4872 /* We use the reloc_count field as a counter if we need
4873 to copy relocs into the output file. */
4874 if (strcmp (name
, ".rel.dyn") != 0)
4878 else if (strncmp (name
, ".got", 4) == 0)
4882 BFD_ASSERT (elf_section_data (s
) != NULL
);
4883 g
= (struct mips_got_info
*) elf_section_data (s
)->tdata
;
4884 BFD_ASSERT (g
!= NULL
);
4886 /* Fix the size of .got section for the correspondence of
4887 global symbols and got entries. This adds some useless
4888 got entries. Is this required by ABI really? */
4889 i
= elf_hash_table (info
)->dynsymcount
- g
->global_gotsym
;
4890 s
->_raw_size
+= i
* 4;
4892 else if (strncmp (name
, ".stub", 5) == 0)
4894 /* Irix rld assumes that the function stub isn't at the end
4895 of .text section. So put a dummy. XXX */
4896 s
->_raw_size
+= MIPS_FUNCTION_STUB_SIZE
;
4898 else if (SGI_COMPAT (output_bfd
)
4899 && strncmp (name
, ".compact_rel", 12) == 0)
4900 s
->_raw_size
+= mips_elf_hash_table (info
)->compact_rel_size
;
4901 else if (strncmp (name
, ".init", 5) != 0)
4903 /* It's not one of our sections, so don't allocate space. */
4911 for (spp
= &s
->output_section
->owner
->sections
;
4912 *spp
!= s
->output_section
;
4913 spp
= &(*spp
)->next
)
4915 *spp
= s
->output_section
->next
;
4916 --s
->output_section
->owner
->section_count
;
4921 /* Allocate memory for the section contents. */
4922 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
4923 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
4925 bfd_set_error (bfd_error_no_memory
);
4928 memset (s
->contents
, 0, s
->_raw_size
);
4931 if (elf_hash_table (info
)->dynamic_sections_created
)
4933 /* Add some entries to the .dynamic section. We fill in the
4934 values later, in elf_mips_finish_dynamic_sections, but we
4935 must add the entries now so that we get the correct size for
4936 the .dynamic section. The DT_DEBUG entry is filled in by the
4937 dynamic linker and used by the debugger. */
4940 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
4946 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
4950 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0))
4953 if (bfd_get_section_by_name (dynobj
, ".rel.dyn"))
4955 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0))
4958 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0))
4961 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
, 0))
4965 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_CONFLICTNO
, 0))
4968 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LIBLISTNO
, 0))
4971 if (bfd_get_section_by_name (dynobj
, ".conflict") != NULL
)
4973 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_CONFLICT
, 0))
4976 s
= bfd_get_section_by_name (dynobj
, ".liblist");
4977 BFD_ASSERT (s
!= NULL
);
4979 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LIBLIST
, 0))
4983 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_RLD_VERSION
, 0))
4986 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_FLAGS
, 0))
4990 /* Time stamps in executable files are a bad idea. */
4991 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_TIME_STAMP
, 0))
4996 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_ICHECKSUM
, 0))
5001 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_IVERSION
, 0))
5005 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_BASE_ADDRESS
, 0))
5008 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LOCAL_GOTNO
, 0))
5011 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_SYMTABNO
, 0))
5014 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_UNREFEXTNO
, 0))
5017 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_GOTSYM
, 0))
5020 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_HIPAGENO
, 0))
5023 #if 0 /* (SGI_COMPAT) */
5024 if (! bfd_get_section_by_name (dynobj
, ".init"))
5025 if (! bfd_elf32_add_dynamic_entry (info
, DT_INIT
, 0))
5028 if (! bfd_get_section_by_name (dynobj
, ".fini"))
5029 if (! bfd_elf32_add_dynamic_entry (info
, DT_FINI
, 0))
5034 /* If we use dynamic linking, we generate a section symbol for each
5035 output section. These are local symbols, which means that they
5036 must come first in the dynamic symbol table.
5037 That means we must increment the dynamic symbol index of every
5038 other dynamic symbol. */
5040 const char * const *namep
;
5042 bfd_size_type strindex
;
5043 struct bfd_strtab_hash
*dynstr
;
5044 struct mips_got_info
*g
;
5046 if (elf_hash_table (info
)->dynamic_sections_created
)
5048 if (SGI_COMPAT (output_bfd
))
5050 c
= SIZEOF_MIPS_DYNSYM_SECNAMES
- 1;
5051 elf_link_hash_traverse (elf_hash_table (info
),
5052 mips_elf_adjust_dynindx
,
5054 elf_hash_table (info
)->dynsymcount
+= c
;
5056 dynstr
= elf_hash_table (info
)->dynstr
;
5057 BFD_ASSERT (dynstr
!= NULL
);
5059 for (i
= 1, namep
= mips_elf_dynsym_sec_names
;
5063 s
= bfd_get_section_by_name (output_bfd
, *namep
);
5065 elf_section_data (s
)->dynindx
= i
;
5067 strindex
= _bfd_stringtab_add (dynstr
, *namep
, true, false);
5068 if (strindex
== (bfd_size_type
) -1)
5071 mips_elf_hash_table (info
)->dynsym_sec_strindex
[i
] = strindex
;
5076 c
= bfd_count_sections (output_bfd
);
5077 elf_link_hash_traverse (elf_hash_table (info
),
5078 mips_elf_adjust_dynindx
,
5080 elf_hash_table (info
)->dynsymcount
+= c
;
5082 for (i
= 1, s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
, i
++)
5084 elf_section_data (s
)->dynindx
= i
;
5085 /* These symbols will have no names, so we don't need to
5086 fiddle with dynstr_index. */
5091 s
= bfd_get_section_by_name (dynobj
, ".got");
5092 BFD_ASSERT (s
!= NULL
);
5093 BFD_ASSERT (elf_section_data (s
) != NULL
);
5094 g
= (struct mips_got_info
*) elf_section_data (s
)->tdata
;
5095 BFD_ASSERT (g
!= NULL
);
5097 /* If there are no global got symbols, fake the last symbol so for
5099 if (g
->global_gotsym
)
5100 g
->global_gotsym
+= c
;
5102 g
->global_gotsym
= elf_hash_table (info
)->dynsymcount
- 1;
5108 /* Increment the index of a dynamic symbol by a given amount. Called
5109 via elf_link_hash_traverse. */
5112 mips_elf_adjust_dynindx (h
, cparg
)
5113 struct elf_link_hash_entry
*h
;
5116 unsigned int *cp
= (unsigned int *) cparg
;
5118 if (h
->dynindx
!= -1)
5123 /* Finish up dynamic symbol handling. We set the contents of various
5124 dynamic sections here. */
5127 mips_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
5129 struct bfd_link_info
*info
;
5130 struct elf_link_hash_entry
*h
;
5131 Elf_Internal_Sym
*sym
;
5136 struct mips_got_info
*g
;
5139 dynobj
= elf_hash_table (info
)->dynobj
;
5140 gval
= sym
->st_value
;
5142 if (h
->plt_offset
!= (bfd_vma
) -1)
5146 bfd_byte stub
[MIPS_FUNCTION_STUB_SIZE
];
5148 /* This symbol has a stub. Set it up. */
5150 BFD_ASSERT (h
->dynindx
!= -1);
5152 s
= bfd_get_section_by_name (dynobj
, ".stub");
5153 BFD_ASSERT (s
!= NULL
);
5155 /* Fill the stub. */
5157 bfd_put_32 (output_bfd
, STUB_LW(output_bfd
), p
);
5159 bfd_put_32 (output_bfd
, STUB_MOVE
, p
);
5162 /* FIXME: Can h->dynindex be more than 64K? */
5163 if (h
->dynindx
& 0xffff0000)
5166 bfd_put_32 (output_bfd
, STUB_JALR
, p
);
5168 bfd_put_32 (output_bfd
, STUB_LI16
+ h
->dynindx
, p
);
5170 BFD_ASSERT (h
->plt_offset
<= s
->_raw_size
);
5171 memcpy (s
->contents
+ h
->plt_offset
, stub
, MIPS_FUNCTION_STUB_SIZE
);
5173 /* Mark the symbol as undefined. plt_offset != -1 occurs
5174 only for the referenced symbol. */
5175 sym
->st_shndx
= SHN_UNDEF
;
5177 /* The run-time linker uses the st_value field of the symbol
5178 to reset the global offset table entry for this external
5179 to its stub address when unlinking a shared object. */
5180 gval
= s
->output_section
->vma
+ s
->output_offset
+ h
->plt_offset
;
5181 sym
->st_value
= gval
;
5184 BFD_ASSERT (h
->dynindx
!= -1);
5186 sgot
= bfd_get_section_by_name (dynobj
, ".got");
5187 BFD_ASSERT (sgot
!= NULL
);
5188 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
5189 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
5190 BFD_ASSERT (g
!= NULL
);
5192 if ((unsigned long) h
->dynindx
>= g
->global_gotsym
)
5194 bfd_size_type offset
;
5196 /* This symbol has an entry in the global offset table. Set its
5197 value to the corresponding got entry, if needed. */
5198 if (h
->got_offset
== (bfd_vma
) -1)
5200 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
5201 BFD_ASSERT (g
->local_gotno
* 4 <= offset
5202 && offset
< sgot
->_raw_size
);
5203 bfd_put_32 (output_bfd
, gval
, sgot
->contents
+ offset
);
5207 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5208 name
= h
->root
.root
.string
;
5209 if (strcmp (name
, "_DYNAMIC") == 0
5210 || strcmp (name
, "_GLOBAL_OFFSET_TABLE_") == 0)
5211 sym
->st_shndx
= SHN_ABS
;
5212 else if (strcmp (name
, "_DYNAMIC_LINK") == 0)
5214 sym
->st_shndx
= SHN_ABS
;
5215 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5218 else if (SGI_COMPAT (output_bfd
))
5220 if (strcmp (name
, "_gp_disp") == 0)
5222 sym
->st_shndx
= SHN_ABS
;
5223 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5224 sym
->st_value
= elf_gp (output_bfd
);
5226 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[0]) == 0
5227 || strcmp (name
, mips_elf_dynsym_rtproc_names
[1]) == 0)
5229 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5230 sym
->st_other
= STO_PROTECTED
;
5232 sym
->st_shndx
= SHN_MIPS_DATA
;
5234 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[2]) == 0)
5236 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5237 sym
->st_other
= STO_PROTECTED
;
5238 sym
->st_value
= mips_elf_hash_table (info
)->procedure_count
;
5239 sym
->st_shndx
= SHN_ABS
;
5241 else if (sym
->st_shndx
!= SHN_UNDEF
)
5243 if (h
->type
== STT_FUNC
)
5244 sym
->st_shndx
= SHN_MIPS_TEXT
;
5245 else if (h
->type
== STT_OBJECT
)
5246 sym
->st_shndx
= SHN_MIPS_DATA
;
5253 /* Finish up the dynamic sections. */
5256 mips_elf_finish_dynamic_sections (output_bfd
, info
)
5258 struct bfd_link_info
*info
;
5263 struct mips_got_info
*g
;
5265 dynobj
= elf_hash_table (info
)->dynobj
;
5267 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
5269 sgot
= bfd_get_section_by_name (dynobj
, ".got");
5270 BFD_ASSERT (sgot
!= NULL
);
5272 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
5273 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
5274 BFD_ASSERT (g
!= NULL
);
5276 if (elf_hash_table (info
)->dynamic_sections_created
)
5278 Elf32_External_Dyn
*dyncon
, *dynconend
;
5280 BFD_ASSERT (sdyn
!= NULL
);
5282 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5283 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
5284 for (; dyncon
< dynconend
; dyncon
++)
5286 Elf_Internal_Dyn dyn
;
5291 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5299 s
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
5300 BFD_ASSERT (s
!= NULL
);
5301 dyn
.d_un
.d_val
= sizeof (Elf32_External_Rel
);
5302 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5306 /* Rewrite DT_STRSZ. */
5308 _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
5309 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5315 case DT_MIPS_CONFLICT
:
5318 case DT_MIPS_LIBLIST
:
5321 s
= bfd_get_section_by_name (output_bfd
, name
);
5322 BFD_ASSERT (s
!= NULL
);
5323 dyn
.d_un
.d_ptr
= s
->vma
;
5324 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5327 case DT_MIPS_RLD_VERSION
:
5328 dyn
.d_un
.d_val
= 1; /* XXX */
5329 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5333 dyn
.d_un
.d_val
= RHF_NOTPOT
; /* XXX */
5334 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5337 case DT_MIPS_CONFLICTNO
:
5339 elemsize
= sizeof (Elf32_Conflict
);
5342 case DT_MIPS_LIBLISTNO
:
5344 elemsize
= sizeof (Elf32_Lib
);
5346 s
= bfd_get_section_by_name (output_bfd
, name
);
5349 if (s
->_cooked_size
!= 0)
5350 dyn
.d_un
.d_val
= s
->_cooked_size
/ elemsize
;
5352 dyn
.d_un
.d_val
= s
->_raw_size
/ elemsize
;
5357 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5360 case DT_MIPS_TIME_STAMP
:
5361 time ((time_t *) &dyn
.d_un
.d_val
);
5362 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5365 case DT_MIPS_ICHECKSUM
:
5369 case DT_MIPS_IVERSION
:
5373 case DT_MIPS_BASE_ADDRESS
:
5374 s
= output_bfd
->sections
;
5375 BFD_ASSERT (s
!= NULL
);
5376 dyn
.d_un
.d_ptr
= s
->vma
& ~(0xffff);
5377 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5380 case DT_MIPS_LOCAL_GOTNO
:
5381 dyn
.d_un
.d_val
= g
->local_gotno
;
5382 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5385 case DT_MIPS_SYMTABNO
:
5387 elemsize
= sizeof (Elf32_External_Sym
);
5388 s
= bfd_get_section_by_name (output_bfd
, name
);
5389 BFD_ASSERT (s
!= NULL
);
5391 if (s
->_cooked_size
!= 0)
5392 dyn
.d_un
.d_val
= s
->_cooked_size
/ elemsize
;
5394 dyn
.d_un
.d_val
= s
->_raw_size
/ elemsize
;
5395 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5398 case DT_MIPS_UNREFEXTNO
:
5400 dyn
.d_un
.d_val
= SIZEOF_MIPS_DYNSYM_SECNAMES
;
5401 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5404 case DT_MIPS_GOTSYM
:
5405 dyn
.d_un
.d_val
= g
->global_gotsym
;
5406 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5409 case DT_MIPS_HIPAGENO
:
5410 dyn
.d_un
.d_val
= g
->local_gotno
- MIPS_RESERVED_GOTNO
;
5411 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5418 /* The first entry of the global offset table will be filled at
5419 runtime. The second entry will be used by some runtime loaders.
5420 This isn't the case of Irix rld. */
5421 if (sgot
->_raw_size
> 0)
5423 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5424 bfd_put_32 (output_bfd
, (bfd_vma
) 0x80000000, sgot
->contents
+ 4);
5427 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5434 Elf_Internal_Sym sym
;
5437 const char * const * namep
= mips_elf_dynsym_sec_names
;
5438 Elf32_compact_rel cpt
;
5440 /* Set up the section symbols for the output sections. SGI sets
5441 the STT_NOTYPE attribute for these symbols. Should we do so? */
5443 sdynsym
= bfd_get_section_by_name (dynobj
, ".dynsym");
5444 if (sdynsym
!= NULL
)
5446 if (SGI_COMPAT (output_bfd
))
5450 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_NOTYPE
);
5454 while ((name
= *namep
++) != NULL
)
5456 s
= bfd_get_section_by_name (output_bfd
, name
);
5459 sym
.st_value
= s
->vma
;
5460 dindx
= elf_section_data (s
)->dynindx
;
5461 last
= s
->vma
+ s
->_raw_size
;
5465 sym
.st_value
= last
;
5469 sym
.st_shndx
= (i
< MIPS_TEXT_DYNSYM_SECNO
5474 mips_elf_hash_table (info
)->dynsym_sec_strindex
[dindx
];
5476 bfd_elf32_swap_symbol_out (output_bfd
, &sym
,
5477 (((Elf32_External_Sym
*)
5482 /* Set the sh_info field of the output .dynsym section to
5483 the index of the first global symbol. */
5484 elf_section_data (sdynsym
->output_section
)->this_hdr
.sh_info
=
5485 SIZEOF_MIPS_DYNSYM_SECNAMES
;
5491 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5494 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5498 sym
.st_value
= s
->vma
;
5500 indx
= elf_section_data (s
)->this_idx
;
5501 BFD_ASSERT (indx
> 0);
5502 sym
.st_shndx
= indx
;
5504 bfd_elf32_swap_symbol_out (output_bfd
, &sym
,
5505 (((Elf32_External_Sym
*)
5507 + elf_section_data (s
)->dynindx
));
5510 /* Set the sh_info field of the output .dynsym section to
5511 the index of the first global symbol. */
5512 elf_section_data (sdynsym
->output_section
)->this_hdr
.sh_info
=
5513 bfd_count_sections (output_bfd
) + 1;
5517 if (SGI_COMPAT (output_bfd
))
5519 /* Write .compact_rel section out. */
5520 s
= bfd_get_section_by_name (dynobj
, ".compact_rel");
5524 cpt
.num
= s
->reloc_count
;
5526 cpt
.offset
= (s
->output_section
->filepos
5527 + sizeof (Elf32_External_compact_rel
));
5530 bfd_elf32_swap_compact_rel_out (output_bfd
, &cpt
,
5531 ((Elf32_External_compact_rel
*)
5534 /* Clean up a dummy stub function entry in .text. */
5535 s
= bfd_get_section_by_name (dynobj
, ".stub");
5538 file_ptr dummy_offset
;
5540 BFD_ASSERT (s
->_raw_size
>= MIPS_FUNCTION_STUB_SIZE
);
5541 dummy_offset
= s
->_raw_size
- MIPS_FUNCTION_STUB_SIZE
;
5542 memset (s
->contents
+ dummy_offset
, 0,
5543 MIPS_FUNCTION_STUB_SIZE
);
5548 /* Clean up a first relocation in .rel.dyn. */
5549 s
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
5551 memset (s
->contents
, 0, sizeof (Elf32_External_Rel
));
5557 /* This is almost identical to bfd_generic_get_... except that some
5558 MIPS relocations need to be handled specially. Sigh. */
5561 elf32_mips_get_relocated_section_contents (abfd
, link_info
, link_order
, data
,
5562 relocateable
, symbols
)
5564 struct bfd_link_info
*link_info
;
5565 struct bfd_link_order
*link_order
;
5567 boolean relocateable
;
5570 /* Get enough memory to hold the stuff */
5571 bfd
*input_bfd
= link_order
->u
.indirect
.section
->owner
;
5572 asection
*input_section
= link_order
->u
.indirect
.section
;
5574 long reloc_size
= bfd_get_reloc_upper_bound (input_bfd
, input_section
);
5575 arelent
**reloc_vector
= NULL
;
5581 reloc_vector
= (arelent
**) bfd_malloc (reloc_size
);
5582 if (reloc_vector
== NULL
&& reloc_size
!= 0)
5585 /* read in the section */
5586 if (!bfd_get_section_contents (input_bfd
,
5590 input_section
->_raw_size
))
5593 /* We're not relaxing the section, so just copy the size info */
5594 input_section
->_cooked_size
= input_section
->_raw_size
;
5595 input_section
->reloc_done
= true;
5597 reloc_count
= bfd_canonicalize_reloc (input_bfd
,
5601 if (reloc_count
< 0)
5604 if (reloc_count
> 0)
5609 bfd_vma gp
= 0x12345678; /* initialize just to shut gcc up */
5612 struct bfd_hash_entry
*h
;
5613 struct bfd_link_hash_entry
*lh
;
5614 /* Skip all this stuff if we aren't mixing formats. */
5615 if (abfd
&& input_bfd
5616 && abfd
->xvec
== input_bfd
->xvec
)
5620 h
= bfd_hash_lookup (&link_info
->hash
->table
, "_gp", false, false);
5621 lh
= (struct bfd_link_hash_entry
*) h
;
5628 case bfd_link_hash_undefined
:
5629 case bfd_link_hash_undefweak
:
5630 case bfd_link_hash_common
:
5633 case bfd_link_hash_defined
:
5634 case bfd_link_hash_defweak
:
5636 gp
= lh
->u
.def
.value
;
5638 case bfd_link_hash_indirect
:
5639 case bfd_link_hash_warning
:
5641 /* @@FIXME ignoring warning for now */
5643 case bfd_link_hash_new
:
5652 for (parent
= reloc_vector
; *parent
!= (arelent
*) NULL
;
5655 char *error_message
= (char *) NULL
;
5656 bfd_reloc_status_type r
;
5658 /* Specific to MIPS: Deal with relocation types that require
5659 knowing the gp of the output bfd. */
5660 asymbol
*sym
= *(*parent
)->sym_ptr_ptr
;
5661 if (bfd_is_abs_section (sym
->section
) && abfd
)
5663 /* The special_function wouldn't get called anyways. */
5667 /* The gp isn't there; let the special function code
5668 fall over on its own. */
5670 else if ((*parent
)->howto
->special_function
5671 == mips_elf_gprel16_reloc
)
5673 /* bypass special_function call */
5674 r
= gprel16_with_gp (input_bfd
, sym
, *parent
, input_section
,
5675 relocateable
, (PTR
) data
, gp
);
5676 goto skip_bfd_perform_relocation
;
5678 /* end mips specific stuff */
5680 r
= bfd_perform_relocation (input_bfd
,
5684 relocateable
? abfd
: (bfd
*) NULL
,
5686 skip_bfd_perform_relocation
:
5690 asection
*os
= input_section
->output_section
;
5692 /* A partial link, so keep the relocs */
5693 os
->orelocation
[os
->reloc_count
] = *parent
;
5697 if (r
!= bfd_reloc_ok
)
5701 case bfd_reloc_undefined
:
5702 if (!((*link_info
->callbacks
->undefined_symbol
)
5703 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
5704 input_bfd
, input_section
, (*parent
)->address
)))
5707 case bfd_reloc_dangerous
:
5708 BFD_ASSERT (error_message
!= (char *) NULL
);
5709 if (!((*link_info
->callbacks
->reloc_dangerous
)
5710 (link_info
, error_message
, input_bfd
, input_section
,
5711 (*parent
)->address
)))
5714 case bfd_reloc_overflow
:
5715 if (!((*link_info
->callbacks
->reloc_overflow
)
5716 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
5717 (*parent
)->howto
->name
, (*parent
)->addend
,
5718 input_bfd
, input_section
, (*parent
)->address
)))
5721 case bfd_reloc_outofrange
:
5730 if (reloc_vector
!= NULL
)
5731 free (reloc_vector
);
5735 if (reloc_vector
!= NULL
)
5736 free (reloc_vector
);
5739 #define bfd_elf32_bfd_get_relocated_section_contents \
5740 elf32_mips_get_relocated_section_contents
5742 /* ECOFF swapping routines. These are used when dealing with the
5743 .mdebug section, which is in the ECOFF debugging format. */
5744 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap
=
5746 /* Symbol table magic number. */
5748 /* Alignment of debugging information. E.g., 4. */
5750 /* Sizes of external symbolic information. */
5751 sizeof (struct hdr_ext
),
5752 sizeof (struct dnr_ext
),
5753 sizeof (struct pdr_ext
),
5754 sizeof (struct sym_ext
),
5755 sizeof (struct opt_ext
),
5756 sizeof (struct fdr_ext
),
5757 sizeof (struct rfd_ext
),
5758 sizeof (struct ext_ext
),
5759 /* Functions to swap in external symbolic data. */
5768 _bfd_ecoff_swap_tir_in
,
5769 _bfd_ecoff_swap_rndx_in
,
5770 /* Functions to swap out external symbolic data. */
5779 _bfd_ecoff_swap_tir_out
,
5780 _bfd_ecoff_swap_rndx_out
,
5781 /* Function to read in symbolic data. */
5782 mips_elf_read_ecoff_info
5785 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
5786 #define TARGET_LITTLE_NAME "elf32-littlemips"
5787 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
5788 #define TARGET_BIG_NAME "elf32-bigmips"
5789 #define ELF_ARCH bfd_arch_mips
5790 #define ELF_MACHINE_CODE EM_MIPS
5791 #define ELF_MAXPAGESIZE 0x10000
5792 #define elf_backend_collect true
5793 #define elf_backend_type_change_ok true
5794 #define elf_info_to_howto 0
5795 #define elf_info_to_howto_rel mips_info_to_howto_rel
5796 #define elf_backend_sym_is_global mips_elf_sym_is_global
5797 #define elf_backend_object_p mips_elf_object_p
5798 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
5799 #define elf_backend_fake_sections mips_elf_fake_sections
5800 #define elf_backend_section_from_bfd_section \
5801 mips_elf_section_from_bfd_section
5802 #define elf_backend_section_processing mips_elf_section_processing
5803 #define elf_backend_symbol_processing mips_elf_symbol_processing
5804 #define elf_backend_additional_program_headers \
5805 mips_elf_additional_program_headers
5806 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
5807 #define elf_backend_final_write_processing \
5808 mips_elf_final_write_processing
5809 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
5811 #define bfd_elf32_bfd_is_local_label mips_elf_is_local_label
5812 #define bfd_elf32_find_nearest_line mips_elf_find_nearest_line
5814 #define bfd_elf32_bfd_link_hash_table_create \
5815 mips_elf_link_hash_table_create
5816 #define bfd_elf32_bfd_final_link mips_elf_final_link
5817 #define bfd_elf32_bfd_copy_private_bfd_data \
5818 mips_elf_copy_private_bfd_data
5819 #define bfd_elf32_bfd_merge_private_bfd_data \
5820 mips_elf_merge_private_bfd_data
5821 #define bfd_elf32_bfd_set_private_flags mips_elf_set_private_flags
5822 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
5823 #define elf_backend_create_dynamic_sections \
5824 mips_elf_create_dynamic_sections
5825 #define elf_backend_check_relocs mips_elf_check_relocs
5826 #define elf_backend_adjust_dynamic_symbol \
5827 mips_elf_adjust_dynamic_symbol
5828 #define elf_backend_size_dynamic_sections \
5829 mips_elf_size_dynamic_sections
5830 #define elf_backend_relocate_section mips_elf_relocate_section
5831 #define elf_backend_finish_dynamic_symbol \
5832 mips_elf_finish_dynamic_symbol
5833 #define elf_backend_finish_dynamic_sections \
5834 mips_elf_finish_dynamic_sections
5835 #define elf_backend_want_hdr_in_seg 1
5837 #include "elf32-target.h"