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
},
1254 { BFD_RELOC_MIPS_GOT_HI16
, R_MIPS_GOT_HI16
},
1255 { BFD_RELOC_MIPS_GOT_LO16
, R_MIPS_GOT_LO16
},
1256 { BFD_RELOC_MIPS_CALL_HI16
, R_MIPS_CALL_HI16
},
1257 { BFD_RELOC_MIPS_CALL_LO16
, R_MIPS_CALL_LO16
}
1260 /* Given a BFD reloc type, return a howto structure. */
1262 static reloc_howto_type
*
1263 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
1265 bfd_reloc_code_real_type code
;
1269 for (i
= 0; i
< sizeof (mips_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
1271 if (mips_reloc_map
[i
].bfd_reloc_val
== code
)
1272 return &elf_mips_howto_table
[(int) mips_reloc_map
[i
].elf_reloc_val
];
1277 /* Given a MIPS reloc type, fill in an arelent structure. */
1280 mips_info_to_howto_rel (abfd
, cache_ptr
, dst
)
1283 Elf32_Internal_Rel
*dst
;
1285 unsigned int r_type
;
1287 r_type
= ELF32_R_TYPE (dst
->r_info
);
1288 BFD_ASSERT (r_type
< (unsigned int) R_MIPS_max
);
1289 cache_ptr
->howto
= &elf_mips_howto_table
[r_type
];
1291 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1292 value for the object file. We get the addend now, rather than
1293 when we do the relocation, because the symbol manipulations done
1294 by the linker may cause us to lose track of the input BFD. */
1295 if (((*cache_ptr
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) != 0
1296 && (r_type
== (unsigned int) R_MIPS_GPREL16
1297 || r_type
== (unsigned int) R_MIPS_LITERAL
))
1298 cache_ptr
->addend
= elf_gp (abfd
);
1301 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1302 routines swap this structure in and out. They are used outside of
1303 BFD, so they are globally visible. */
1306 bfd_mips_elf32_swap_reginfo_in (abfd
, ex
, in
)
1308 const Elf32_External_RegInfo
*ex
;
1311 in
->ri_gprmask
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gprmask
);
1312 in
->ri_cprmask
[0] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[0]);
1313 in
->ri_cprmask
[1] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[1]);
1314 in
->ri_cprmask
[2] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[2]);
1315 in
->ri_cprmask
[3] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[3]);
1316 in
->ri_gp_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gp_value
);
1320 bfd_mips_elf32_swap_reginfo_out (abfd
, in
, ex
)
1322 const Elf32_RegInfo
*in
;
1323 Elf32_External_RegInfo
*ex
;
1325 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gprmask
,
1326 (bfd_byte
*) ex
->ri_gprmask
);
1327 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[0],
1328 (bfd_byte
*) ex
->ri_cprmask
[0]);
1329 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[1],
1330 (bfd_byte
*) ex
->ri_cprmask
[1]);
1331 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[2],
1332 (bfd_byte
*) ex
->ri_cprmask
[2]);
1333 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[3],
1334 (bfd_byte
*) ex
->ri_cprmask
[3]);
1335 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gp_value
,
1336 (bfd_byte
*) ex
->ri_gp_value
);
1339 /* Swap an entry in a .gptab section. Note that these routines rely
1340 on the equivalence of the two elements of the union. */
1343 bfd_mips_elf32_swap_gptab_in (abfd
, ex
, in
)
1345 const Elf32_External_gptab
*ex
;
1348 in
->gt_entry
.gt_g_value
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_g_value
);
1349 in
->gt_entry
.gt_bytes
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_bytes
);
1353 bfd_mips_elf32_swap_gptab_out (abfd
, in
, ex
)
1355 const Elf32_gptab
*in
;
1356 Elf32_External_gptab
*ex
;
1358 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_g_value
,
1359 ex
->gt_entry
.gt_g_value
);
1360 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_bytes
,
1361 ex
->gt_entry
.gt_bytes
);
1365 bfd_elf32_swap_compact_rel_out (abfd
, in
, ex
)
1367 const Elf32_compact_rel
*in
;
1368 Elf32_External_compact_rel
*ex
;
1370 bfd_h_put_32 (abfd
, (bfd_vma
) in
->id1
, ex
->id1
);
1371 bfd_h_put_32 (abfd
, (bfd_vma
) in
->num
, ex
->num
);
1372 bfd_h_put_32 (abfd
, (bfd_vma
) in
->id2
, ex
->id2
);
1373 bfd_h_put_32 (abfd
, (bfd_vma
) in
->offset
, ex
->offset
);
1374 bfd_h_put_32 (abfd
, (bfd_vma
) in
->reserved0
, ex
->reserved0
);
1375 bfd_h_put_32 (abfd
, (bfd_vma
) in
->reserved1
, ex
->reserved1
);
1379 bfd_elf32_swap_crinfo_out (abfd
, in
, ex
)
1381 const Elf32_crinfo
*in
;
1382 Elf32_External_crinfo
*ex
;
1386 l
= (((in
->ctype
& CRINFO_CTYPE
) << CRINFO_CTYPE_SH
)
1387 | ((in
->rtype
& CRINFO_RTYPE
) << CRINFO_RTYPE_SH
)
1388 | ((in
->dist2to
& CRINFO_DIST2TO
) << CRINFO_DIST2TO_SH
)
1389 | ((in
->relvaddr
& CRINFO_RELVADDR
) << CRINFO_RELVADDR_SH
));
1390 bfd_h_put_32 (abfd
, (bfd_vma
) l
, ex
->info
);
1391 bfd_h_put_32 (abfd
, (bfd_vma
) in
->konst
, ex
->konst
);
1392 bfd_h_put_32 (abfd
, (bfd_vma
) in
->vaddr
, ex
->vaddr
);
1395 /* Determine whether a symbol is global for the purposes of splitting
1396 the symbol table into global symbols and local symbols. At least
1397 on Irix 5, this split must be between section symbols and all other
1398 symbols. On most ELF targets the split is between static symbols
1399 and externally visible symbols. */
1403 mips_elf_sym_is_global (abfd
, sym
)
1407 return (sym
->flags
& BSF_SECTION_SYM
) == 0 ? true : false;
1410 /* Set the right machine number for a MIPS ELF file. */
1413 mips_elf_object_p (abfd
)
1416 switch (elf_elfheader (abfd
)->e_flags
& EF_MIPS_ARCH
)
1420 /* Just use the default, which was set in elfcode.h. */
1424 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 6000);
1428 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 4000);
1432 /* Irix 5 is broken. Object file symbol tables are not always
1433 sorted correctly such that local symbols precede global symbols,
1434 and the sh_info field in the symbol table is not always right. */
1435 elf_bad_symtab (abfd
) = true;
1440 /* The final processing done just before writing out a MIPS ELF object
1441 file. This gets the MIPS architecture right based on the machine
1446 mips_elf_final_write_processing (abfd
, linker
)
1452 Elf_Internal_Shdr
**hdrpp
;
1454 switch (bfd_get_mach (abfd
))
1457 val
= E_MIPS_ARCH_1
;
1461 val
= E_MIPS_ARCH_2
;
1465 val
= E_MIPS_ARCH_3
;
1473 elf_elfheader (abfd
)->e_flags
&=~ EF_MIPS_ARCH
;
1474 elf_elfheader (abfd
)->e_flags
|= val
;
1476 /* Set the sh_info field for .gptab sections. */
1477 for (i
= 1, hdrpp
= elf_elfsections (abfd
) + 1;
1478 i
< elf_elfheader (abfd
)->e_shnum
;
1481 if ((*hdrpp
)->sh_type
== SHT_MIPS_GPTAB
)
1486 BFD_ASSERT ((*hdrpp
)->bfd_section
!= NULL
);
1487 name
= bfd_get_section_name (abfd
, (*hdrpp
)->bfd_section
);
1488 BFD_ASSERT (name
!= NULL
1489 && strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0);
1490 sec
= bfd_get_section_by_name (abfd
, name
+ sizeof ".gptab" - 1);
1491 BFD_ASSERT (sec
!= NULL
);
1492 (*hdrpp
)->sh_info
= elf_section_data (sec
)->this_idx
;
1497 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1500 mips_elf_set_private_flags (abfd
, flags
)
1504 BFD_ASSERT (!elf_flags_init (abfd
)
1505 || elf_elfheader (abfd
)->e_flags
== flags
);
1507 elf_elfheader (abfd
)->e_flags
= flags
;
1508 elf_flags_init (abfd
) = true;
1512 /* Copy backend specific data from one object module to another */
1515 mips_elf_copy_private_bfd_data (ibfd
, obfd
)
1519 /* This function is selected based on the input vector. We only
1520 want to copy information over if the output BFD also uses Elf
1522 if (bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1525 BFD_ASSERT (!elf_flags_init (obfd
)
1526 || (elf_elfheader (obfd
)->e_flags
1527 == elf_elfheader (ibfd
)->e_flags
));
1529 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1530 elf_flags_init (obfd
) = true;
1534 /* Merge backend specific data from an object file to the output
1535 object file when linking. */
1538 mips_elf_merge_private_bfd_data (ibfd
, obfd
)
1545 /* Check if we have the same endianess */
1546 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1547 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1549 (*_bfd_error_handler
)
1550 ("%s: compiled for a %s endian system and target is %s endian",
1551 bfd_get_filename (ibfd
),
1552 bfd_big_endian (ibfd
) ? "big" : "little",
1553 bfd_big_endian (obfd
) ? "big" : "little");
1555 bfd_set_error (bfd_error_wrong_format
);
1559 /* This function is selected based on the input vector. We only
1560 want to copy information over if the output BFD also uses Elf
1562 if (bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1565 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1566 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_MIPS_NOREORDER
;
1567 old_flags
= elf_elfheader (obfd
)->e_flags
;
1569 if (!elf_flags_init (obfd
)) /* First call, no flags set */
1571 elf_flags_init (obfd
) = true;
1572 elf_elfheader (obfd
)->e_flags
= new_flags
;
1574 else if (((new_flags
^ old_flags
) & ~EF_MIPS_NOREORDER
)
1575 == 0) /* Compatible flags are ok */
1577 else /* Incompatible flags */
1579 /* Warn about -fPIC mismatch */
1580 if ((new_flags
& EF_MIPS_PIC
) != (old_flags
& EF_MIPS_PIC
))
1582 new_flags
&= ~EF_MIPS_PIC
;
1583 (*_bfd_error_handler
)
1584 ("%s: needs all files compiled with -fPIC",
1585 bfd_get_filename (ibfd
));
1588 if ((new_flags
& EF_MIPS_CPIC
) != (old_flags
& EF_MIPS_CPIC
))
1590 new_flags
&= ~EF_MIPS_CPIC
;
1591 (*_bfd_error_handler
)
1592 ("%s: needs all files compiled with -mabicalls",
1593 bfd_get_filename (ibfd
));
1596 /* Warn about any other mismatches */
1597 if (new_flags
!= old_flags
)
1598 (*_bfd_error_handler
)
1599 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1600 bfd_get_filename (ibfd
), (unsigned long) new_flags
,
1601 (unsigned long) old_flags
);
1603 bfd_set_error (bfd_error_bad_value
);
1610 /* Handle a MIPS specific section when reading an object file. This
1611 is called when elfcode.h finds a section with an unknown type.
1612 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1616 mips_elf_section_from_shdr (abfd
, hdr
, name
)
1618 Elf32_Internal_Shdr
*hdr
;
1623 /* There ought to be a place to keep ELF backend specific flags, but
1624 at the moment there isn't one. We just keep track of the
1625 sections by their name, instead. Fortunately, the ABI gives
1626 suggested names for all the MIPS specific sections, so we will
1627 probably get away with this. */
1628 switch (hdr
->sh_type
)
1630 case SHT_MIPS_LIBLIST
:
1631 if (strcmp (name
, ".liblist") != 0)
1635 if (strcmp (name
, ".msym") != 0)
1638 case SHT_MIPS_CONFLICT
:
1639 if (strcmp (name
, ".conflict") != 0)
1642 case SHT_MIPS_GPTAB
:
1643 if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) != 0)
1646 case SHT_MIPS_UCODE
:
1647 if (strcmp (name
, ".ucode") != 0)
1650 case SHT_MIPS_DEBUG
:
1651 if (strcmp (name
, ".mdebug") != 0)
1654 case SHT_MIPS_REGINFO
:
1655 if (strcmp (name
, ".reginfo") != 0
1656 || hdr
->sh_size
!= sizeof (Elf32_External_RegInfo
))
1659 case SHT_MIPS_OPTIONS
:
1660 if (strcmp (name
, ".options") != 0)
1663 case SHT_MIPS_DWARF
:
1664 if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) != 0)
1667 case SHT_MIPS_EVENTS
:
1668 if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1675 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1677 newsect
= hdr
->bfd_section
;
1679 if (hdr
->sh_type
== SHT_MIPS_DEBUG
)
1681 if (! bfd_set_section_flags (abfd
, newsect
,
1682 (bfd_get_section_flags (abfd
, newsect
)
1687 /* FIXME: We should record sh_info for a .gptab section. */
1689 /* For a .reginfo section, set the gp value in the tdata information
1690 from the contents of this section. We need the gp value while
1691 processing relocs, so we just get it now. */
1692 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1694 Elf32_External_RegInfo ext
;
1697 if (! bfd_get_section_contents (abfd
, newsect
, (PTR
) &ext
,
1698 (file_ptr
) 0, sizeof ext
))
1700 bfd_mips_elf32_swap_reginfo_in (abfd
, &ext
, &s
);
1701 elf_gp (abfd
) = s
.ri_gp_value
;
1707 /* Set the correct type for a MIPS ELF section. We do this by the
1708 section name, which is a hack, but ought to work. */
1711 mips_elf_fake_sections (abfd
, hdr
, sec
)
1713 Elf32_Internal_Shdr
*hdr
;
1716 register const char *name
;
1718 name
= bfd_get_section_name (abfd
, sec
);
1720 if (strcmp (name
, ".liblist") == 0)
1722 hdr
->sh_type
= SHT_MIPS_LIBLIST
;
1723 hdr
->sh_info
= sec
->_raw_size
/ sizeof (Elf32_Lib
);
1724 /* FIXME: Set the sh_link field. */
1726 else if (strcmp (name
, ".msym") == 0)
1728 hdr
->sh_type
= SHT_MIPS_MSYM
;
1729 hdr
->sh_entsize
= 8;
1730 /* FIXME: Set the sh_info field. */
1732 else if (strcmp (name
, ".conflict") == 0)
1733 hdr
->sh_type
= SHT_MIPS_CONFLICT
;
1734 else if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0)
1736 hdr
->sh_type
= SHT_MIPS_GPTAB
;
1737 hdr
->sh_entsize
= sizeof (Elf32_External_gptab
);
1738 /* The sh_info field is set in mips_elf_final_write_processing. */
1740 else if (strcmp (name
, ".ucode") == 0)
1741 hdr
->sh_type
= SHT_MIPS_UCODE
;
1742 else if (strcmp (name
, ".mdebug") == 0)
1744 hdr
->sh_type
= SHT_MIPS_DEBUG
;
1745 /* In a shared object on Irix 5.3, the .mdebug section has an
1746 entsize of 0. FIXME: Does this matter? */
1747 if (SGI_COMPAT (abfd
) && (abfd
->flags
& DYNAMIC
) != 0)
1748 hdr
->sh_entsize
= 0;
1750 hdr
->sh_entsize
= 1;
1752 else if (strcmp (name
, ".reginfo") == 0)
1754 hdr
->sh_type
= SHT_MIPS_REGINFO
;
1755 /* In a shared object on Irix 5.3, the .reginfo section has an
1756 entsize of 0x18. FIXME: Does this matter? */
1757 if (SGI_COMPAT (abfd
) && (abfd
->flags
& DYNAMIC
) != 0)
1758 hdr
->sh_entsize
= sizeof (Elf32_External_RegInfo
);
1760 hdr
->sh_entsize
= 1;
1762 /* Force the section size to the correct value, even if the
1763 linker thinks it is larger. The link routine below will only
1764 write out this much data for .reginfo. */
1765 hdr
->sh_size
= sec
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1767 else if (SGI_COMPAT (abfd
)
1768 && (strcmp (name
, ".hash") == 0
1769 || strcmp (name
, ".dynamic") == 0
1770 || strcmp (name
, ".dynstr") == 0))
1772 hdr
->sh_entsize
= 0;
1773 hdr
->sh_info
= SIZEOF_MIPS_DYNSYM_SECNAMES
;
1775 else if (strcmp (name
, ".got") == 0
1776 || strcmp (name
, ".sdata") == 0
1777 || strcmp (name
, ".sbss") == 0
1778 || strcmp (name
, ".lit4") == 0
1779 || strcmp (name
, ".lit8") == 0)
1780 hdr
->sh_flags
|= SHF_MIPS_GPREL
;
1781 else if (strcmp (name
, ".options") == 0)
1783 hdr
->sh_type
= SHT_MIPS_OPTIONS
;
1784 hdr
->sh_entsize
= 1;
1786 else if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) == 0)
1787 hdr
->sh_type
= SHT_MIPS_DWARF
;
1788 else if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1789 hdr
->sh_type
= SHT_MIPS_EVENTS
;
1794 /* Given a BFD section, try to locate the corresponding ELF section
1798 mips_elf_section_from_bfd_section (abfd
, hdr
, sec
, retval
)
1800 Elf32_Internal_Shdr
*hdr
;
1804 if (strcmp (bfd_get_section_name (abfd
, sec
), ".scommon") == 0)
1806 *retval
= SHN_MIPS_SCOMMON
;
1809 if (strcmp (bfd_get_section_name (abfd
, sec
), ".acommon") == 0)
1811 *retval
= SHN_MIPS_ACOMMON
;
1817 /* Work over a section just before writing it out. We update the GP
1818 value in the .reginfo section based on the value we are using.
1819 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1820 name; there has to be a better way. */
1823 mips_elf_section_processing (abfd
, hdr
)
1825 Elf32_Internal_Shdr
*hdr
;
1827 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1831 BFD_ASSERT (hdr
->sh_size
== sizeof (Elf32_External_RegInfo
));
1832 BFD_ASSERT (hdr
->contents
== NULL
);
1835 hdr
->sh_offset
+ sizeof (Elf32_External_RegInfo
) - 4,
1838 bfd_h_put_32 (abfd
, (bfd_vma
) elf_gp (abfd
), buf
);
1839 if (bfd_write (buf
, (bfd_size_type
) 1, (bfd_size_type
) 4, abfd
) != 4)
1843 if (hdr
->bfd_section
!= NULL
)
1845 const char *name
= bfd_get_section_name (abfd
, hdr
->bfd_section
);
1847 if (strcmp (name
, ".sdata") == 0)
1849 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1850 hdr
->sh_type
= SHT_PROGBITS
;
1852 else if (strcmp (name
, ".sbss") == 0)
1854 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1855 hdr
->sh_type
= SHT_NOBITS
;
1857 else if (strcmp (name
, ".lit8") == 0
1858 || strcmp (name
, ".lit4") == 0)
1860 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1861 hdr
->sh_type
= SHT_PROGBITS
;
1863 else if (strcmp (name
, ".compact_rel") == 0)
1866 hdr
->sh_type
= SHT_PROGBITS
;
1868 else if (strcmp (name
, ".rtproc") == 0)
1870 if (hdr
->sh_addralign
!= 0 && hdr
->sh_entsize
== 0)
1872 unsigned int adjust
;
1874 adjust
= hdr
->sh_size
% hdr
->sh_addralign
;
1876 hdr
->sh_size
+= hdr
->sh_addralign
- adjust
;
1884 /* MIPS ELF uses two common sections. One is the usual one, and the
1885 other is for small objects. All the small objects are kept
1886 together, and then referenced via the gp pointer, which yields
1887 faster assembler code. This is what we use for the small common
1888 section. This approach is copied from ecoff.c. */
1889 static asection mips_elf_scom_section
;
1890 static asymbol mips_elf_scom_symbol
;
1891 static asymbol
*mips_elf_scom_symbol_ptr
;
1893 /* MIPS ELF also uses an acommon section, which represents an
1894 allocated common symbol which may be overridden by a
1895 definition in a shared library. */
1896 static asection mips_elf_acom_section
;
1897 static asymbol mips_elf_acom_symbol
;
1898 static asymbol
*mips_elf_acom_symbol_ptr
;
1900 /* The Irix 5 support uses two virtual sections, which represent
1901 text/data symbols defined in dynamic objects. */
1902 static asection mips_elf_text_section
;
1903 static asection
*mips_elf_text_section_ptr
;
1904 static asymbol mips_elf_text_symbol
;
1905 static asymbol
*mips_elf_text_symbol_ptr
;
1907 static asection mips_elf_data_section
;
1908 static asection
*mips_elf_data_section_ptr
;
1909 static asymbol mips_elf_data_symbol
;
1910 static asymbol
*mips_elf_data_symbol_ptr
;
1912 /* Handle the special MIPS section numbers that a symbol may use. */
1915 mips_elf_symbol_processing (abfd
, asym
)
1919 elf_symbol_type
*elfsym
;
1921 elfsym
= (elf_symbol_type
*) asym
;
1922 switch (elfsym
->internal_elf_sym
.st_shndx
)
1924 case SHN_MIPS_ACOMMON
:
1925 /* This section is used in a dynamically linked executable file.
1926 It is an allocated common section. The dynamic linker can
1927 either resolve these symbols to something in a shared
1928 library, or it can just leave them here. For our purposes,
1929 we can consider these symbols to be in a new section. */
1930 if (mips_elf_acom_section
.name
== NULL
)
1932 /* Initialize the acommon section. */
1933 mips_elf_acom_section
.name
= ".acommon";
1934 mips_elf_acom_section
.flags
= SEC_ALLOC
;
1935 mips_elf_acom_section
.output_section
= &mips_elf_acom_section
;
1936 mips_elf_acom_section
.symbol
= &mips_elf_acom_symbol
;
1937 mips_elf_acom_section
.symbol_ptr_ptr
= &mips_elf_acom_symbol_ptr
;
1938 mips_elf_acom_symbol
.name
= ".acommon";
1939 mips_elf_acom_symbol
.flags
= BSF_SECTION_SYM
;
1940 mips_elf_acom_symbol
.section
= &mips_elf_acom_section
;
1941 mips_elf_acom_symbol_ptr
= &mips_elf_acom_symbol
;
1943 asym
->section
= &mips_elf_acom_section
;
1947 /* Common symbols less than the GP size are automatically
1948 treated as SHN_MIPS_SCOMMON symbols. */
1949 if (asym
->value
> elf_gp_size (abfd
))
1952 case SHN_MIPS_SCOMMON
:
1953 if (mips_elf_scom_section
.name
== NULL
)
1955 /* Initialize the small common section. */
1956 mips_elf_scom_section
.name
= ".scommon";
1957 mips_elf_scom_section
.flags
= SEC_IS_COMMON
;
1958 mips_elf_scom_section
.output_section
= &mips_elf_scom_section
;
1959 mips_elf_scom_section
.symbol
= &mips_elf_scom_symbol
;
1960 mips_elf_scom_section
.symbol_ptr_ptr
= &mips_elf_scom_symbol_ptr
;
1961 mips_elf_scom_symbol
.name
= ".scommon";
1962 mips_elf_scom_symbol
.flags
= BSF_SECTION_SYM
;
1963 mips_elf_scom_symbol
.section
= &mips_elf_scom_section
;
1964 mips_elf_scom_symbol_ptr
= &mips_elf_scom_symbol
;
1966 asym
->section
= &mips_elf_scom_section
;
1967 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
1970 case SHN_MIPS_SUNDEFINED
:
1971 asym
->section
= bfd_und_section_ptr
;
1974 #if 0 /* for SGI_COMPAT */
1976 asym
->section
= mips_elf_text_section_ptr
;
1980 asym
->section
= mips_elf_data_section_ptr
;
1986 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
1990 mips_elf_additional_program_headers (abfd
)
1998 if (! SGI_COMPAT (abfd
))
2001 s
= bfd_get_section_by_name (abfd
, ".reginfo");
2002 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2004 /* We need a PT_MIPS_REGINFO segment. */
2008 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
2009 && bfd_get_section_by_name (abfd
, ".mdebug") != NULL
)
2011 /* We need a PT_MIPS_RTPROC segment. */
2018 /* Modify the segment map for an Irix 5 executable. */
2021 mips_elf_modify_segment_map (abfd
)
2025 struct elf_segment_map
*m
, **pm
;
2027 if (! SGI_COMPAT (abfd
))
2030 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2032 s
= bfd_get_section_by_name (abfd
, ".reginfo");
2033 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2035 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2036 if (m
->p_type
== PT_MIPS_REGINFO
)
2040 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, sizeof *m
);
2044 m
->p_type
= PT_MIPS_REGINFO
;
2048 /* We want to put it after the PHDR and INTERP segments. */
2049 pm
= &elf_tdata (abfd
)->segment_map
;
2051 && ((*pm
)->p_type
== PT_PHDR
2052 || (*pm
)->p_type
== PT_INTERP
))
2060 /* If there are .dynamic and .mdebug sections, we make a room for
2061 the RTPROC header. FIXME: Rewrite without section names. */
2062 if (bfd_get_section_by_name (abfd
, ".interp") == NULL
2063 && bfd_get_section_by_name (abfd
, ".dynamic") != NULL
2064 && bfd_get_section_by_name (abfd
, ".mdebug") != NULL
)
2066 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2067 if (m
->p_type
== PT_MIPS_RTPROC
)
2071 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, sizeof *m
);
2075 m
->p_type
= PT_MIPS_RTPROC
;
2077 s
= bfd_get_section_by_name (abfd
, ".rtproc");
2082 m
->p_flags_valid
= 1;
2090 /* We want to put it after the DYNAMIC segment. */
2091 pm
= &elf_tdata (abfd
)->segment_map
;
2092 while (*pm
!= NULL
&& (*pm
)->p_type
!= PT_DYNAMIC
)
2102 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2103 .dynsym, and .hash sections, and everything in between. */
2104 for (pm
= &elf_tdata (abfd
)->segment_map
; *pm
!= NULL
; pm
= &(*pm
)->next
)
2105 if ((*pm
)->p_type
== PT_DYNAMIC
)
2110 && strcmp (m
->sections
[0]->name
, ".dynamic") == 0)
2112 static const char *sec_names
[] =
2113 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2116 struct elf_segment_map
*n
;
2120 for (i
= 0; i
< sizeof sec_names
/ sizeof sec_names
[0]; i
++)
2122 s
= bfd_get_section_by_name (abfd
, sec_names
[i
]);
2123 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2129 sz
= s
->_cooked_size
;
2132 if (high
< s
->vma
+ sz
)
2138 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2139 if ((s
->flags
& SEC_LOAD
) != 0
2142 + (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
))
2146 n
= ((struct elf_segment_map
*)
2147 bfd_zalloc (abfd
, sizeof *n
+ (c
- 1) * sizeof (asection
*)));
2154 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2156 if ((s
->flags
& SEC_LOAD
) != 0
2159 + (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
))
2173 /* The structure of the runtime procedure descriptor created by the
2174 loader for use by the static exception system. */
2176 typedef struct runtime_pdr
{
2177 bfd_vma adr
; /* memory address of start of procedure */
2178 long regmask
; /* save register mask */
2179 long regoffset
; /* save register offset */
2180 long fregmask
; /* save floating point register mask */
2181 long fregoffset
; /* save floating point register offset */
2182 long frameoffset
; /* frame size */
2183 short framereg
; /* frame pointer register */
2184 short pcreg
; /* offset or reg of return pc */
2185 long irpss
; /* index into the runtime string table */
2187 struct exception_info
*exception_info
;/* pointer to exception array */
2189 #define cbRPDR sizeof(RPDR)
2190 #define rpdNil ((pRPDR) 0)
2192 /* Swap RPDR (runtime procedure table entry) for output. */
2194 static void ecoff_swap_rpdr_out
2195 PARAMS ((bfd
*, const RPDR
*, struct rpdr_ext
*));
2198 ecoff_swap_rpdr_out (abfd
, in
, ex
)
2201 struct rpdr_ext
*ex
;
2203 /* ecoff_put_off was defined in ecoffswap.h. */
2204 ecoff_put_off (abfd
, in
->adr
, (bfd_byte
*) ex
->p_adr
);
2205 bfd_h_put_32 (abfd
, in
->regmask
, (bfd_byte
*) ex
->p_regmask
);
2206 bfd_h_put_32 (abfd
, in
->regoffset
, (bfd_byte
*) ex
->p_regoffset
);
2207 bfd_h_put_32 (abfd
, in
->fregmask
, (bfd_byte
*) ex
->p_fregmask
);
2208 bfd_h_put_32 (abfd
, in
->fregoffset
, (bfd_byte
*) ex
->p_fregoffset
);
2209 bfd_h_put_32 (abfd
, in
->frameoffset
, (bfd_byte
*) ex
->p_frameoffset
);
2211 bfd_h_put_16 (abfd
, in
->framereg
, (bfd_byte
*) ex
->p_framereg
);
2212 bfd_h_put_16 (abfd
, in
->pcreg
, (bfd_byte
*) ex
->p_pcreg
);
2214 bfd_h_put_32 (abfd
, in
->irpss
, (bfd_byte
*) ex
->p_irpss
);
2216 ecoff_put_off (abfd
, in
->exception_info
, (bfd_byte
*) ex
->p_exception_info
);
2220 /* Read ECOFF debugging information from a .mdebug section into a
2221 ecoff_debug_info structure. */
2224 mips_elf_read_ecoff_info (abfd
, section
, debug
)
2227 struct ecoff_debug_info
*debug
;
2230 const struct ecoff_debug_swap
*swap
;
2231 char *ext_hdr
= NULL
;
2233 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2235 ext_hdr
= (char *) bfd_malloc ((size_t) swap
->external_hdr_size
);
2236 if (ext_hdr
== NULL
&& swap
->external_hdr_size
!= 0)
2239 if (bfd_get_section_contents (abfd
, section
, ext_hdr
, (file_ptr
) 0,
2240 swap
->external_hdr_size
)
2244 symhdr
= &debug
->symbolic_header
;
2245 (*swap
->swap_hdr_in
) (abfd
, ext_hdr
, symhdr
);
2247 /* The symbolic header contains absolute file offsets and sizes to
2249 #define READ(ptr, offset, count, size, type) \
2250 if (symhdr->count == 0) \
2251 debug->ptr = NULL; \
2254 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2255 if (debug->ptr == NULL) \
2256 goto error_return; \
2257 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2258 || (bfd_read (debug->ptr, size, symhdr->count, \
2259 abfd) != size * symhdr->count)) \
2260 goto error_return; \
2263 READ (line
, cbLineOffset
, cbLine
, sizeof (unsigned char), unsigned char *);
2264 READ (external_dnr
, cbDnOffset
, idnMax
, swap
->external_dnr_size
, PTR
);
2265 READ (external_pdr
, cbPdOffset
, ipdMax
, swap
->external_pdr_size
, PTR
);
2266 READ (external_sym
, cbSymOffset
, isymMax
, swap
->external_sym_size
, PTR
);
2267 READ (external_opt
, cbOptOffset
, ioptMax
, swap
->external_opt_size
, PTR
);
2268 READ (external_aux
, cbAuxOffset
, iauxMax
, sizeof (union aux_ext
),
2270 READ (ss
, cbSsOffset
, issMax
, sizeof (char), char *);
2271 READ (ssext
, cbSsExtOffset
, issExtMax
, sizeof (char), char *);
2272 READ (external_fdr
, cbFdOffset
, ifdMax
, swap
->external_fdr_size
, PTR
);
2273 READ (external_rfd
, cbRfdOffset
, crfd
, swap
->external_rfd_size
, PTR
);
2274 READ (external_ext
, cbExtOffset
, iextMax
, swap
->external_ext_size
, PTR
);
2278 debug
->adjust
= NULL
;
2283 if (ext_hdr
!= NULL
)
2285 if (debug
->line
!= NULL
)
2287 if (debug
->external_dnr
!= NULL
)
2288 free (debug
->external_dnr
);
2289 if (debug
->external_pdr
!= NULL
)
2290 free (debug
->external_pdr
);
2291 if (debug
->external_sym
!= NULL
)
2292 free (debug
->external_sym
);
2293 if (debug
->external_opt
!= NULL
)
2294 free (debug
->external_opt
);
2295 if (debug
->external_aux
!= NULL
)
2296 free (debug
->external_aux
);
2297 if (debug
->ss
!= NULL
)
2299 if (debug
->ssext
!= NULL
)
2300 free (debug
->ssext
);
2301 if (debug
->external_fdr
!= NULL
)
2302 free (debug
->external_fdr
);
2303 if (debug
->external_rfd
!= NULL
)
2304 free (debug
->external_rfd
);
2305 if (debug
->external_ext
!= NULL
)
2306 free (debug
->external_ext
);
2310 /* MIPS ELF local labels start with '$', not 'L'. */
2314 mips_elf_is_local_label (abfd
, symbol
)
2318 return symbol
->name
[0] == '$';
2321 /* MIPS ELF uses a special find_nearest_line routine in order the
2322 handle the ECOFF debugging information. */
2324 struct mips_elf_find_line
2326 struct ecoff_debug_info d
;
2327 struct ecoff_find_line i
;
2331 mips_elf_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2332 functionname_ptr
, line_ptr
)
2337 const char **filename_ptr
;
2338 const char **functionname_ptr
;
2339 unsigned int *line_ptr
;
2343 msec
= bfd_get_section_by_name (abfd
, ".mdebug");
2347 struct mips_elf_find_line
*fi
;
2348 const struct ecoff_debug_swap
* const swap
=
2349 get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2351 /* If we are called during a link, mips_elf_final_link may have
2352 cleared the SEC_HAS_CONTENTS field. We force it back on here
2353 if appropriate (which it normally will be). */
2354 origflags
= msec
->flags
;
2355 if (elf_section_data (msec
)->this_hdr
.sh_type
!= SHT_NOBITS
)
2356 msec
->flags
|= SEC_HAS_CONTENTS
;
2358 fi
= elf_tdata (abfd
)->find_line_info
;
2361 bfd_size_type external_fdr_size
;
2364 struct fdr
*fdr_ptr
;
2366 fi
= ((struct mips_elf_find_line
*)
2367 bfd_alloc (abfd
, sizeof (struct mips_elf_find_line
)));
2370 msec
->flags
= origflags
;
2374 memset (fi
, 0, sizeof (struct mips_elf_find_line
));
2376 if (! mips_elf_read_ecoff_info (abfd
, msec
, &fi
->d
))
2378 msec
->flags
= origflags
;
2382 /* Swap in the FDR information. */
2383 fi
->d
.fdr
= ((struct fdr
*)
2385 (fi
->d
.symbolic_header
.ifdMax
*
2386 sizeof (struct fdr
))));
2387 if (fi
->d
.fdr
== NULL
)
2389 msec
->flags
= origflags
;
2392 external_fdr_size
= swap
->external_fdr_size
;
2393 fdr_ptr
= fi
->d
.fdr
;
2394 fraw_src
= (char *) fi
->d
.external_fdr
;
2395 fraw_end
= (fraw_src
2396 + fi
->d
.symbolic_header
.ifdMax
* external_fdr_size
);
2397 for (; fraw_src
< fraw_end
; fraw_src
+= external_fdr_size
, fdr_ptr
++)
2398 (*swap
->swap_fdr_in
) (abfd
, (PTR
) fraw_src
, fdr_ptr
);
2400 elf_tdata (abfd
)->find_line_info
= fi
;
2402 /* Note that we don't bother to ever free this information.
2403 find_nearest_line is either called all the time, as in
2404 objdump -l, so the information should be saved, or it is
2405 rarely called, as in ld error messages, so the memory
2406 wasted is unimportant. Still, it would probably be a
2407 good idea for free_cached_info to throw it away. */
2410 if (_bfd_ecoff_locate_line (abfd
, section
, offset
, &fi
->d
, swap
,
2411 &fi
->i
, filename_ptr
, functionname_ptr
,
2414 msec
->flags
= origflags
;
2418 msec
->flags
= origflags
;
2421 /* Fall back on the generic ELF find_nearest_line routine. */
2423 return _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
2424 filename_ptr
, functionname_ptr
,
2428 /* The MIPS ELF linker needs additional information for each symbol in
2429 the global hash table. */
2431 struct mips_elf_link_hash_entry
2433 struct elf_link_hash_entry root
;
2435 /* External symbol information. */
2439 /* MIPS ELF linker hash table. */
2441 struct mips_elf_link_hash_table
2443 struct elf_link_hash_table root
;
2444 /* String section indices for the dynamic section symbols. */
2445 bfd_size_type dynsym_sec_strindex
[SIZEOF_MIPS_DYNSYM_SECNAMES
];
2446 /* The number of .rtproc entries. */
2447 bfd_size_type procedure_count
;
2448 /* The size of the .compact_rel section (if SGI_COMPAT). */
2449 bfd_size_type compact_rel_size
;
2452 /* Look up an entry in a MIPS ELF linker hash table. */
2454 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
2455 ((struct mips_elf_link_hash_entry *) \
2456 elf_link_hash_lookup (&(table)->root, (string), (create), \
2459 /* Traverse a MIPS ELF linker hash table. */
2461 #define mips_elf_link_hash_traverse(table, func, info) \
2462 (elf_link_hash_traverse \
2464 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2467 /* Get the MIPS ELF linker hash table from a link_info structure. */
2469 #define mips_elf_hash_table(p) \
2470 ((struct mips_elf_link_hash_table *) ((p)->hash))
2472 static boolean mips_elf_output_extsym
2473 PARAMS ((struct mips_elf_link_hash_entry
*, PTR
));
2475 /* Create an entry in a MIPS ELF linker hash table. */
2477 static struct bfd_hash_entry
*
2478 mips_elf_link_hash_newfunc (entry
, table
, string
)
2479 struct bfd_hash_entry
*entry
;
2480 struct bfd_hash_table
*table
;
2483 struct mips_elf_link_hash_entry
*ret
=
2484 (struct mips_elf_link_hash_entry
*) entry
;
2486 /* Allocate the structure if it has not already been allocated by a
2488 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
2489 ret
= ((struct mips_elf_link_hash_entry
*)
2490 bfd_hash_allocate (table
,
2491 sizeof (struct mips_elf_link_hash_entry
)));
2492 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
2493 return (struct bfd_hash_entry
*) ret
;
2495 /* Call the allocation method of the superclass. */
2496 ret
= ((struct mips_elf_link_hash_entry
*)
2497 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2499 if (ret
!= (struct mips_elf_link_hash_entry
*) NULL
)
2501 /* Set local fields. */
2502 memset (&ret
->esym
, 0, sizeof (EXTR
));
2503 /* We use -2 as a marker to indicate that the information has
2504 not been set. -1 means there is no associated ifd. */
2508 return (struct bfd_hash_entry
*) ret
;
2511 /* Create a MIPS ELF linker hash table. */
2513 static struct bfd_link_hash_table
*
2514 mips_elf_link_hash_table_create (abfd
)
2517 struct mips_elf_link_hash_table
*ret
;
2520 ret
= ((struct mips_elf_link_hash_table
*)
2521 bfd_alloc (abfd
, sizeof (struct mips_elf_link_hash_table
)));
2522 if (ret
== (struct mips_elf_link_hash_table
*) NULL
)
2525 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2526 mips_elf_link_hash_newfunc
))
2528 bfd_release (abfd
, ret
);
2532 for (i
= 0; i
< SIZEOF_MIPS_DYNSYM_SECNAMES
; i
++)
2533 ret
->dynsym_sec_strindex
[i
] = (bfd_size_type
) -1;
2534 ret
->procedure_count
= 0;
2535 ret
->compact_rel_size
= 0;
2537 return &ret
->root
.root
;
2540 /* Hook called by the linker routine which adds symbols from an object
2541 file. We must handle the special MIPS section numbers here. */
2545 mips_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
2547 struct bfd_link_info
*info
;
2548 const Elf_Internal_Sym
*sym
;
2554 if (SGI_COMPAT (abfd
)
2555 && (abfd
->flags
& DYNAMIC
) != 0
2556 && strcmp (*namep
, "_rld_new_interface") == 0)
2558 /* Skip Irix 5 rld entry name. */
2563 switch (sym
->st_shndx
)
2566 /* Common symbols less than the GP size are automatically
2567 treated as SHN_MIPS_SCOMMON symbols. */
2568 if (sym
->st_size
> elf_gp_size (abfd
))
2571 case SHN_MIPS_SCOMMON
:
2572 *secp
= bfd_make_section_old_way (abfd
, ".scommon");
2573 (*secp
)->flags
|= SEC_IS_COMMON
;
2574 *valp
= sym
->st_size
;
2578 /* This section is used in a shared object. */
2579 if (mips_elf_text_section_ptr
== NULL
)
2581 /* Initialize the section. */
2582 mips_elf_text_section
.name
= ".text";
2583 mips_elf_text_section
.flags
= SEC_NO_FLAGS
;
2584 mips_elf_text_section
.output_section
= NULL
;
2585 mips_elf_text_section
.owner
= abfd
;
2586 mips_elf_text_section
.symbol
= &mips_elf_text_symbol
;
2587 mips_elf_text_section
.symbol_ptr_ptr
= &mips_elf_text_symbol_ptr
;
2588 mips_elf_text_symbol
.name
= ".text";
2589 mips_elf_text_symbol
.flags
= BSF_SECTION_SYM
;
2590 mips_elf_text_symbol
.section
= &mips_elf_text_section
;
2591 mips_elf_text_symbol_ptr
= &mips_elf_text_symbol
;
2592 mips_elf_text_section_ptr
= &mips_elf_text_section
;
2595 *secp
= bfd_und_section_ptr
;
2597 *secp
= mips_elf_text_section_ptr
;
2600 case SHN_MIPS_ACOMMON
:
2601 /* Fall through. XXX Can we treat this as allocated data? */
2603 /* This section is used in a shared object. */
2604 if (mips_elf_data_section_ptr
== NULL
)
2606 /* Initialize the section. */
2607 mips_elf_data_section
.name
= ".data";
2608 mips_elf_data_section
.flags
= SEC_NO_FLAGS
;
2609 mips_elf_data_section
.output_section
= NULL
;
2610 mips_elf_data_section
.owner
= abfd
;
2611 mips_elf_data_section
.symbol
= &mips_elf_data_symbol
;
2612 mips_elf_data_section
.symbol_ptr_ptr
= &mips_elf_data_symbol_ptr
;
2613 mips_elf_data_symbol
.name
= ".data";
2614 mips_elf_data_symbol
.flags
= BSF_SECTION_SYM
;
2615 mips_elf_data_symbol
.section
= &mips_elf_data_section
;
2616 mips_elf_data_symbol_ptr
= &mips_elf_data_symbol
;
2617 mips_elf_data_section_ptr
= &mips_elf_data_section
;
2620 *secp
= bfd_und_section_ptr
;
2622 *secp
= mips_elf_data_section_ptr
;
2625 case SHN_MIPS_SUNDEFINED
:
2626 *secp
= bfd_und_section_ptr
;
2633 /* Structure used to pass information to mips_elf_output_extsym. */
2638 struct bfd_link_info
*info
;
2639 struct ecoff_debug_info
*debug
;
2640 const struct ecoff_debug_swap
*swap
;
2644 /* This routine is used to write out ECOFF debugging external symbol
2645 information. It is called via mips_elf_link_hash_traverse. The
2646 ECOFF external symbol information must match the ELF external
2647 symbol information. Unfortunately, at this point we don't know
2648 whether a symbol is required by reloc information, so the two
2649 tables may wind up being different. We must sort out the external
2650 symbol information before we can set the final size of the .mdebug
2651 section, and we must set the size of the .mdebug section before we
2652 can relocate any sections, and we can't know which symbols are
2653 required by relocation until we relocate the sections.
2654 Fortunately, it is relatively unlikely that any symbol will be
2655 stripped but required by a reloc. In particular, it can not happen
2656 when generating a final executable. */
2659 mips_elf_output_extsym (h
, data
)
2660 struct mips_elf_link_hash_entry
*h
;
2663 struct extsym_info
*einfo
= (struct extsym_info
*) data
;
2665 asection
*sec
, *output_section
;
2667 if (h
->root
.indx
== -2)
2669 else if (((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2670 || (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2671 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2672 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
2674 else if (einfo
->info
->strip
== strip_all
2675 || (einfo
->info
->strip
== strip_some
2676 && bfd_hash_lookup (einfo
->info
->keep_hash
,
2677 h
->root
.root
.root
.string
,
2678 false, false) == NULL
))
2686 if (h
->esym
.ifd
== -2)
2689 h
->esym
.cobol_main
= 0;
2690 h
->esym
.weakext
= 0;
2691 h
->esym
.reserved
= 0;
2692 h
->esym
.ifd
= ifdNil
;
2693 h
->esym
.asym
.value
= 0;
2694 h
->esym
.asym
.st
= stGlobal
;
2696 if (SGI_COMPAT (einfo
->abfd
)
2697 && (h
->root
.root
.type
== bfd_link_hash_undefined
2698 || h
->root
.root
.type
== bfd_link_hash_undefweak
))
2702 /* Use undefined class. Also, set class and type for some
2704 name
= h
->root
.root
.root
.string
;
2705 if (strcmp (name
, mips_elf_dynsym_rtproc_names
[0]) == 0
2706 || strcmp (name
, mips_elf_dynsym_rtproc_names
[1]) == 0)
2708 h
->esym
.asym
.sc
= scData
;
2709 h
->esym
.asym
.st
= stLabel
;
2710 h
->esym
.asym
.value
= 0;
2712 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[2]) == 0)
2714 h
->esym
.asym
.sc
= scAbs
;
2715 h
->esym
.asym
.st
= stLabel
;
2716 h
->esym
.asym
.value
=
2717 mips_elf_hash_table (einfo
->info
)->procedure_count
;
2719 else if (strcmp (name
, "_gp_disp") == 0)
2721 h
->esym
.asym
.sc
= scAbs
;
2722 h
->esym
.asym
.st
= stLabel
;
2723 h
->esym
.asym
.value
= elf_gp (einfo
->abfd
);
2726 h
->esym
.asym
.sc
= scUndefined
;
2728 else if (h
->root
.root
.type
!= bfd_link_hash_defined
2729 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
2730 h
->esym
.asym
.sc
= scAbs
;
2735 sec
= h
->root
.root
.u
.def
.section
;
2736 output_section
= sec
->output_section
;
2738 /* When making a shared library and symbol h is the one from
2739 the another shared library, OUTPUT_SECTION may be null. */
2740 if (output_section
== NULL
)
2741 h
->esym
.asym
.sc
= scUndefined
;
2744 name
= bfd_section_name (output_section
->owner
, output_section
);
2746 if (strcmp (name
, ".text") == 0)
2747 h
->esym
.asym
.sc
= scText
;
2748 else if (strcmp (name
, ".data") == 0)
2749 h
->esym
.asym
.sc
= scData
;
2750 else if (strcmp (name
, ".sdata") == 0)
2751 h
->esym
.asym
.sc
= scSData
;
2752 else if (strcmp (name
, ".rodata") == 0
2753 || strcmp (name
, ".rdata") == 0)
2754 h
->esym
.asym
.sc
= scRData
;
2755 else if (strcmp (name
, ".bss") == 0)
2756 h
->esym
.asym
.sc
= scBss
;
2757 else if (strcmp (name
, ".sbss") == 0)
2758 h
->esym
.asym
.sc
= scSBss
;
2759 else if (strcmp (name
, ".init") == 0)
2760 h
->esym
.asym
.sc
= scInit
;
2761 else if (strcmp (name
, ".fini") == 0)
2762 h
->esym
.asym
.sc
= scFini
;
2764 h
->esym
.asym
.sc
= scAbs
;
2768 h
->esym
.asym
.reserved
= 0;
2769 h
->esym
.asym
.index
= indexNil
;
2772 if (h
->root
.root
.type
== bfd_link_hash_common
)
2773 h
->esym
.asym
.value
= h
->root
.root
.u
.c
.size
;
2774 else if (h
->root
.root
.type
== bfd_link_hash_defined
2775 || h
->root
.root
.type
== bfd_link_hash_defweak
)
2777 if (h
->esym
.asym
.sc
== scCommon
)
2778 h
->esym
.asym
.sc
= scBss
;
2779 else if (h
->esym
.asym
.sc
== scSCommon
)
2780 h
->esym
.asym
.sc
= scSBss
;
2782 sec
= h
->root
.root
.u
.def
.section
;
2783 output_section
= sec
->output_section
;
2784 if (output_section
!= NULL
)
2785 h
->esym
.asym
.value
= (h
->root
.root
.u
.def
.value
2786 + sec
->output_offset
2787 + output_section
->vma
);
2789 h
->esym
.asym
.value
= 0;
2791 else if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
2793 /* Set type and value for a symbol with a function stub. */
2794 h
->esym
.asym
.st
= stProc
;
2795 sec
= h
->root
.root
.u
.def
.section
;
2797 h
->esym
.asym
.value
= 0;
2800 output_section
= sec
->output_section
;
2801 if (output_section
!= NULL
)
2802 h
->esym
.asym
.value
= (h
->root
.plt_offset
2803 + sec
->output_offset
2804 + output_section
->vma
);
2806 h
->esym
.asym
.value
= 0;
2813 if (! bfd_ecoff_debug_one_external (einfo
->abfd
, einfo
->debug
, einfo
->swap
,
2814 h
->root
.root
.root
.string
,
2817 einfo
->failed
= true;
2824 /* Create a runtime procedure table from the .mdebug section. */
2827 mips_elf_create_procedure_table (handle
, abfd
, info
, s
, debug
)
2830 struct bfd_link_info
*info
;
2832 struct ecoff_debug_info
*debug
;
2834 const struct ecoff_debug_swap
*swap
;
2835 HDRR
*hdr
= &debug
->symbolic_header
;
2837 struct rpdr_ext
*erp
;
2839 struct pdr_ext
*epdr
;
2840 struct sym_ext
*esym
;
2843 unsigned long size
, count
;
2844 unsigned long sindex
;
2848 const char *no_name_func
= "static procedure (no name)";
2855 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
2857 sindex
= strlen (no_name_func
) + 1;
2858 count
= hdr
->ipdMax
;
2861 size
= swap
->external_pdr_size
;
2863 epdr
= (struct pdr_ext
*) bfd_malloc (size
* count
);
2867 if (! _bfd_ecoff_get_accumulated_pdr (handle
, (PTR
) epdr
))
2870 size
= sizeof (RPDR
);
2871 rp
= rpdr
= (RPDR
*) bfd_malloc (size
* count
);
2875 sv
= (char **) bfd_malloc (sizeof (char *) * count
);
2879 count
= hdr
->isymMax
;
2880 size
= swap
->external_sym_size
;
2881 esym
= (struct sym_ext
*) bfd_malloc (size
* count
);
2885 if (! _bfd_ecoff_get_accumulated_sym (handle
, (PTR
) esym
))
2888 count
= hdr
->issMax
;
2889 ss
= (char *) bfd_malloc (count
);
2892 if (! _bfd_ecoff_get_accumulated_ss (handle
, (PTR
) ss
))
2895 count
= hdr
->ipdMax
;
2896 for (i
= 0; i
< count
; i
++, rp
++, epdr
++)
2898 (*swap
->swap_pdr_in
) (abfd
, (PTR
) epdr
, &pdr
);
2899 (*swap
->swap_sym_in
) (abfd
, (PTR
) &esym
[pdr
.isym
], &sym
);
2900 rp
->adr
= sym
.value
;
2901 rp
->regmask
= pdr
.regmask
;
2902 rp
->regoffset
= pdr
.regoffset
;
2903 rp
->fregmask
= pdr
.fregmask
;
2904 rp
->fregoffset
= pdr
.fregoffset
;
2905 rp
->frameoffset
= pdr
.frameoffset
;
2906 rp
->framereg
= pdr
.framereg
;
2907 rp
->pcreg
= pdr
.pcreg
;
2909 sv
[i
] = ss
+ sym
.iss
;
2910 sindex
+= strlen (sv
[i
]) + 1;
2914 size
= sizeof (struct rpdr_ext
) * (count
+ 2) + sindex
;
2915 size
= BFD_ALIGN (size
, 16);
2916 rtproc
= (PTR
) bfd_alloc (abfd
, size
);
2919 mips_elf_hash_table (info
)->procedure_count
= 0;
2923 mips_elf_hash_table (info
)->procedure_count
= count
+ 2;
2925 erp
= (struct rpdr_ext
*) rtproc
;
2926 memset (rp
, 0, sizeof (struct rpdr_ext
));
2928 str
= (char *) rtproc
+ sizeof (struct rpdr_ext
) * (count
+ 2);
2929 strcpy (str
, no_name_func
);
2930 str
+= strlen (no_name_func
) + 1;
2931 for (i
= 0; i
< count
; i
++)
2933 ecoff_swap_rpdr_out (abfd
, rpdr
+ i
, erp
+ i
);
2934 strcpy (str
, sv
[i
]);
2935 str
+= strlen (sv
[i
]) + 1;
2937 ecoff_put_off (abfd
, (bfd_vma
) -1, (bfd_byte
*) (erp
+ count
)->p_adr
);
2939 /* Set the size and contents of .rtproc section. */
2940 s
->_raw_size
= size
;
2941 s
->contents
= rtproc
;
2943 /* Skip this section later on (I don't think this currently
2944 matters, but someday it might). */
2945 s
->link_order_head
= (struct bfd_link_order
*) NULL
;
2969 /* A comparison routine used to sort .gptab entries. */
2972 gptab_compare (p1
, p2
)
2976 const Elf32_gptab
*a1
= (const Elf32_gptab
*) p1
;
2977 const Elf32_gptab
*a2
= (const Elf32_gptab
*) p2
;
2979 return a1
->gt_entry
.gt_g_value
- a2
->gt_entry
.gt_g_value
;
2982 /* We need to use a special link routine to handle the .reginfo and
2983 the .mdebug sections. We need to merge all instances of these
2984 sections together, not write them all out sequentially. */
2987 mips_elf_final_link (abfd
, info
)
2989 struct bfd_link_info
*info
;
2993 struct bfd_link_order
*p
;
2994 asection
*reginfo_sec
, *mdebug_sec
, *gptab_data_sec
, *gptab_bss_sec
;
2995 asection
*rtproc_sec
;
2996 Elf32_RegInfo reginfo
;
2997 struct ecoff_debug_info debug
;
2998 const struct ecoff_debug_swap
*swap
2999 = get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
3000 HDRR
*symhdr
= &debug
.symbolic_header
;
3001 PTR mdebug_handle
= NULL
;
3003 /* Drop the .options section, since it has special semantics which I
3004 haven't bothered to figure out. */
3005 for (secpp
= &abfd
->sections
; *secpp
!= NULL
; secpp
= &(*secpp
)->next
)
3007 if (strcmp ((*secpp
)->name
, ".options") == 0)
3009 for (p
= (*secpp
)->link_order_head
; p
!= NULL
; p
= p
->next
)
3010 if (p
->type
== bfd_indirect_link_order
)
3011 p
->u
.indirect
.section
->flags
&=~ SEC_HAS_CONTENTS
;
3012 (*secpp
)->link_order_head
= NULL
;
3013 *secpp
= (*secpp
)->next
;
3014 --abfd
->section_count
;
3019 /* Get a value for the GP register. */
3020 if (elf_gp (abfd
) == 0)
3022 struct bfd_link_hash_entry
*h
;
3024 h
= bfd_link_hash_lookup (info
->hash
, "_gp", false, false, true);
3025 if (h
!= (struct bfd_link_hash_entry
*) NULL
3026 && h
->type
== bfd_link_hash_defined
)
3027 elf_gp (abfd
) = (h
->u
.def
.value
3028 + h
->u
.def
.section
->output_section
->vma
3029 + h
->u
.def
.section
->output_offset
);
3030 else if (info
->relocateable
)
3034 /* Make up a value. */
3036 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3039 && (strcmp (o
->name
, ".sbss") == 0
3040 || strcmp (o
->name
, ".sdata") == 0
3041 || strcmp (o
->name
, ".lit4") == 0
3042 || strcmp (o
->name
, ".lit8") == 0))
3045 elf_gp (abfd
) = lo
+ ELF_MIPS_GP_OFFSET (abfd
);
3049 /* If the relocate_section function needs to do a reloc
3050 involving the GP value, it should make a reloc_dangerous
3051 callback to warn that GP is not defined. */
3055 /* Go through the sections and collect the .reginfo and .mdebug
3059 gptab_data_sec
= NULL
;
3060 gptab_bss_sec
= NULL
;
3061 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3063 if (strcmp (o
->name
, ".reginfo") == 0)
3065 memset (®info
, 0, sizeof reginfo
);
3067 /* We have found the .reginfo section in the output file.
3068 Look through all the link_orders comprising it and merge
3069 the information together. */
3070 for (p
= o
->link_order_head
;
3071 p
!= (struct bfd_link_order
*) NULL
;
3074 asection
*input_section
;
3076 Elf32_External_RegInfo ext
;
3079 if (p
->type
!= bfd_indirect_link_order
)
3081 if (p
->type
== bfd_fill_link_order
)
3086 input_section
= p
->u
.indirect
.section
;
3087 input_bfd
= input_section
->owner
;
3089 /* The linker emulation code has probably clobbered the
3090 size to be zero bytes. */
3091 if (input_section
->_raw_size
== 0)
3092 input_section
->_raw_size
= sizeof (Elf32_External_RegInfo
);
3094 if (! bfd_get_section_contents (input_bfd
, input_section
,
3100 bfd_mips_elf32_swap_reginfo_in (input_bfd
, &ext
, &sub
);
3102 reginfo
.ri_gprmask
|= sub
.ri_gprmask
;
3103 reginfo
.ri_cprmask
[0] |= sub
.ri_cprmask
[0];
3104 reginfo
.ri_cprmask
[1] |= sub
.ri_cprmask
[1];
3105 reginfo
.ri_cprmask
[2] |= sub
.ri_cprmask
[2];
3106 reginfo
.ri_cprmask
[3] |= sub
.ri_cprmask
[3];
3108 /* ri_gp_value is set by the function
3109 mips_elf_section_processing when the section is
3110 finally written out. */
3112 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3113 elf_link_input_bfd ignores this section. */
3114 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3117 /* Force the section size to the value we want. */
3118 o
->_raw_size
= sizeof (Elf32_External_RegInfo
);
3120 /* Skip this section later on (I don't think this currently
3121 matters, but someday it might). */
3122 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3127 if (strcmp (o
->name
, ".mdebug") == 0)
3129 struct extsym_info einfo
;
3131 /* We have found the .mdebug section in the output file.
3132 Look through all the link_orders comprising it and merge
3133 the information together. */
3134 symhdr
->magic
= swap
->sym_magic
;
3135 /* FIXME: What should the version stamp be? */
3137 symhdr
->ilineMax
= 0;
3141 symhdr
->isymMax
= 0;
3142 symhdr
->ioptMax
= 0;
3143 symhdr
->iauxMax
= 0;
3145 symhdr
->issExtMax
= 0;
3148 symhdr
->iextMax
= 0;
3150 /* We accumulate the debugging information itself in the
3151 debug_info structure. */
3153 debug
.external_dnr
= NULL
;
3154 debug
.external_pdr
= NULL
;
3155 debug
.external_sym
= NULL
;
3156 debug
.external_opt
= NULL
;
3157 debug
.external_aux
= NULL
;
3159 debug
.ssext
= debug
.ssext_end
= NULL
;
3160 debug
.external_fdr
= NULL
;
3161 debug
.external_rfd
= NULL
;
3162 debug
.external_ext
= debug
.external_ext_end
= NULL
;
3164 mdebug_handle
= bfd_ecoff_debug_init (abfd
, &debug
, swap
, info
);
3165 if (mdebug_handle
== (PTR
) NULL
)
3168 if (SGI_COMPAT (abfd
))
3174 static const char * const name
[] =
3175 { ".text", ".init", ".fini", ".data",
3176 ".rodata", ".sdata", ".sbss", ".bss" };
3177 static const int sc
[] = { scText
, scInit
, scFini
, scData
,
3178 scRData
, scSData
, scSBss
, scBss
};
3181 esym
.cobol_main
= 0;
3185 esym
.asym
.iss
= issNil
;
3186 esym
.asym
.st
= stLocal
;
3187 esym
.asym
.reserved
= 0;
3188 esym
.asym
.index
= indexNil
;
3189 for (i
= 0; i
< 8; i
++)
3191 esym
.asym
.sc
= sc
[i
];
3192 s
= bfd_get_section_by_name (abfd
, name
[i
]);
3195 esym
.asym
.value
= s
->vma
;
3196 last
= s
->vma
+ s
->_raw_size
;
3199 esym
.asym
.value
= last
;
3201 if (! bfd_ecoff_debug_one_external (abfd
, &debug
, swap
,
3207 for (p
= o
->link_order_head
;
3208 p
!= (struct bfd_link_order
*) NULL
;
3211 asection
*input_section
;
3213 const struct ecoff_debug_swap
*input_swap
;
3214 struct ecoff_debug_info input_debug
;
3218 if (p
->type
!= bfd_indirect_link_order
)
3220 if (p
->type
== bfd_fill_link_order
)
3225 input_section
= p
->u
.indirect
.section
;
3226 input_bfd
= input_section
->owner
;
3228 if (bfd_get_flavour (input_bfd
) != bfd_target_elf_flavour
3229 || (get_elf_backend_data (input_bfd
)
3230 ->elf_backend_ecoff_debug_swap
) == NULL
)
3232 /* I don't know what a non MIPS ELF bfd would be
3233 doing with a .mdebug section, but I don't really
3234 want to deal with it. */
3238 input_swap
= (get_elf_backend_data (input_bfd
)
3239 ->elf_backend_ecoff_debug_swap
);
3241 BFD_ASSERT (p
->size
== input_section
->_raw_size
);
3243 /* The ECOFF linking code expects that we have already
3244 read in the debugging information and set up an
3245 ecoff_debug_info structure, so we do that now. */
3246 if (! mips_elf_read_ecoff_info (input_bfd
, input_section
,
3250 if (! (bfd_ecoff_debug_accumulate
3251 (mdebug_handle
, abfd
, &debug
, swap
, input_bfd
,
3252 &input_debug
, input_swap
, info
)))
3255 /* Loop through the external symbols. For each one with
3256 interesting information, try to find the symbol in
3257 the linker global hash table and save the information
3258 for the output external symbols. */
3259 eraw_src
= input_debug
.external_ext
;
3260 eraw_end
= (eraw_src
3261 + (input_debug
.symbolic_header
.iextMax
3262 * input_swap
->external_ext_size
));
3264 eraw_src
< eraw_end
;
3265 eraw_src
+= input_swap
->external_ext_size
)
3269 struct mips_elf_link_hash_entry
*h
;
3271 (*input_swap
->swap_ext_in
) (input_bfd
, (PTR
) eraw_src
, &ext
);
3272 if (ext
.asym
.sc
== scNil
3273 || ext
.asym
.sc
== scUndefined
3274 || ext
.asym
.sc
== scSUndefined
)
3277 name
= input_debug
.ssext
+ ext
.asym
.iss
;
3278 h
= mips_elf_link_hash_lookup (mips_elf_hash_table (info
),
3279 name
, false, false, true);
3280 if (h
== NULL
|| h
->esym
.ifd
!= -2)
3286 < input_debug
.symbolic_header
.ifdMax
);
3287 ext
.ifd
= input_debug
.ifdmap
[ext
.ifd
];
3293 /* Free up the information we just read. */
3294 free (input_debug
.line
);
3295 free (input_debug
.external_dnr
);
3296 free (input_debug
.external_pdr
);
3297 free (input_debug
.external_sym
);
3298 free (input_debug
.external_opt
);
3299 free (input_debug
.external_aux
);
3300 free (input_debug
.ss
);
3301 free (input_debug
.ssext
);
3302 free (input_debug
.external_fdr
);
3303 free (input_debug
.external_rfd
);
3304 free (input_debug
.external_ext
);
3306 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3307 elf_link_input_bfd ignores this section. */
3308 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3311 if (SGI_COMPAT (abfd
) && info
->shared
)
3313 /* Create .rtproc section. */
3314 rtproc_sec
= bfd_get_section_by_name (abfd
, ".rtproc");
3315 if (rtproc_sec
== NULL
)
3317 flagword flags
= (SEC_HAS_CONTENTS
| SEC_IN_MEMORY
3320 rtproc_sec
= bfd_make_section (abfd
, ".rtproc");
3321 if (rtproc_sec
== NULL
3322 || ! bfd_set_section_flags (abfd
, rtproc_sec
, flags
)
3323 || ! bfd_set_section_alignment (abfd
, rtproc_sec
, 12))
3327 if (! mips_elf_create_procedure_table (mdebug_handle
, abfd
,
3328 info
, rtproc_sec
, &debug
))
3332 /* Build the external symbol information. */
3335 einfo
.debug
= &debug
;
3337 einfo
.failed
= false;
3338 mips_elf_link_hash_traverse (mips_elf_hash_table (info
),
3339 mips_elf_output_extsym
,
3344 /* Set the size of the .mdebug section. */
3345 o
->_raw_size
= bfd_ecoff_debug_size (abfd
, &debug
, swap
);
3347 /* Skip this section later on (I don't think this currently
3348 matters, but someday it might). */
3349 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3354 if (strncmp (o
->name
, ".gptab.", sizeof ".gptab." - 1) == 0)
3356 const char *subname
;
3359 Elf32_External_gptab
*ext_tab
;
3362 /* The .gptab.sdata and .gptab.sbss sections hold
3363 information describing how the small data area would
3364 change depending upon the -G switch. These sections
3365 not used in executables files. */
3366 if (! info
->relocateable
)
3370 for (p
= o
->link_order_head
;
3371 p
!= (struct bfd_link_order
*) NULL
;
3374 asection
*input_section
;
3376 if (p
->type
!= bfd_indirect_link_order
)
3378 if (p
->type
== bfd_fill_link_order
)
3383 input_section
= p
->u
.indirect
.section
;
3385 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3386 elf_link_input_bfd ignores this section. */
3387 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3390 /* Skip this section later on (I don't think this
3391 currently matters, but someday it might). */
3392 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3394 /* Really remove the section. */
3395 for (secpp
= &abfd
->sections
;
3397 secpp
= &(*secpp
)->next
)
3399 *secpp
= (*secpp
)->next
;
3400 --abfd
->section_count
;
3405 /* There is one gptab for initialized data, and one for
3406 uninitialized data. */
3407 if (strcmp (o
->name
, ".gptab.sdata") == 0)
3409 else if (strcmp (o
->name
, ".gptab.sbss") == 0)
3413 (*_bfd_error_handler
)
3414 ("%s: illegal section name `%s'",
3415 bfd_get_filename (abfd
), o
->name
);
3416 bfd_set_error (bfd_error_nonrepresentable_section
);
3420 /* The linker script always combines .gptab.data and
3421 .gptab.sdata into .gptab.sdata, and likewise for
3422 .gptab.bss and .gptab.sbss. It is possible that there is
3423 no .sdata or .sbss section in the output file, in which
3424 case we must change the name of the output section. */
3425 subname
= o
->name
+ sizeof ".gptab" - 1;
3426 if (bfd_get_section_by_name (abfd
, subname
) == NULL
)
3428 if (o
== gptab_data_sec
)
3429 o
->name
= ".gptab.data";
3431 o
->name
= ".gptab.bss";
3432 subname
= o
->name
+ sizeof ".gptab" - 1;
3433 BFD_ASSERT (bfd_get_section_by_name (abfd
, subname
) != NULL
);
3436 /* Set up the first entry. */
3438 tab
= (Elf32_gptab
*) bfd_malloc (c
* sizeof (Elf32_gptab
));
3441 tab
[0].gt_header
.gt_current_g_value
= elf_gp_size (abfd
);
3442 tab
[0].gt_header
.gt_unused
= 0;
3444 /* Combine the input sections. */
3445 for (p
= o
->link_order_head
;
3446 p
!= (struct bfd_link_order
*) NULL
;
3449 asection
*input_section
;
3453 bfd_size_type gpentry
;
3455 if (p
->type
!= bfd_indirect_link_order
)
3457 if (p
->type
== bfd_fill_link_order
)
3462 input_section
= p
->u
.indirect
.section
;
3463 input_bfd
= input_section
->owner
;
3465 /* Combine the gptab entries for this input section one
3466 by one. We know that the input gptab entries are
3467 sorted by ascending -G value. */
3468 size
= bfd_section_size (input_bfd
, input_section
);
3470 for (gpentry
= sizeof (Elf32_External_gptab
);
3472 gpentry
+= sizeof (Elf32_External_gptab
))
3474 Elf32_External_gptab ext_gptab
;
3475 Elf32_gptab int_gptab
;
3481 if (! (bfd_get_section_contents
3482 (input_bfd
, input_section
, (PTR
) &ext_gptab
,
3483 gpentry
, sizeof (Elf32_External_gptab
))))
3489 bfd_mips_elf32_swap_gptab_in (input_bfd
, &ext_gptab
,
3491 val
= int_gptab
.gt_entry
.gt_g_value
;
3492 add
= int_gptab
.gt_entry
.gt_bytes
- last
;
3495 for (look
= 1; look
< c
; look
++)
3497 if (tab
[look
].gt_entry
.gt_g_value
>= val
)
3498 tab
[look
].gt_entry
.gt_bytes
+= add
;
3500 if (tab
[look
].gt_entry
.gt_g_value
== val
)
3506 Elf32_gptab
*new_tab
;
3509 /* We need a new table entry. */
3510 new_tab
= ((Elf32_gptab
*)
3511 bfd_realloc ((PTR
) tab
,
3512 (c
+ 1) * sizeof (Elf32_gptab
)));
3513 if (new_tab
== NULL
)
3519 tab
[c
].gt_entry
.gt_g_value
= val
;
3520 tab
[c
].gt_entry
.gt_bytes
= add
;
3522 /* Merge in the size for the next smallest -G
3523 value, since that will be implied by this new
3526 for (look
= 1; look
< c
; look
++)
3528 if (tab
[look
].gt_entry
.gt_g_value
< val
3530 || (tab
[look
].gt_entry
.gt_g_value
3531 > tab
[max
].gt_entry
.gt_g_value
)))
3535 tab
[c
].gt_entry
.gt_bytes
+=
3536 tab
[max
].gt_entry
.gt_bytes
;
3541 last
= int_gptab
.gt_entry
.gt_bytes
;
3544 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3545 elf_link_input_bfd ignores this section. */
3546 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
3549 /* The table must be sorted by -G value. */
3551 qsort (tab
+ 1, c
- 1, sizeof (tab
[0]), gptab_compare
);
3553 /* Swap out the table. */
3554 ext_tab
= ((Elf32_External_gptab
*)
3555 bfd_alloc (abfd
, c
* sizeof (Elf32_External_gptab
)));
3556 if (ext_tab
== NULL
)
3562 for (i
= 0; i
< c
; i
++)
3563 bfd_mips_elf32_swap_gptab_out (abfd
, tab
+ i
, ext_tab
+ i
);
3566 o
->_raw_size
= c
* sizeof (Elf32_External_gptab
);
3567 o
->contents
= (bfd_byte
*) ext_tab
;
3569 /* Skip this section later on (I don't think this currently
3570 matters, but someday it might). */
3571 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
3575 /* Invoke the regular ELF backend linker to do all the work. */
3576 if (! bfd_elf32_bfd_final_link (abfd
, info
))
3579 /* Now write out the computed sections. */
3581 if (reginfo_sec
!= (asection
*) NULL
)
3583 Elf32_External_RegInfo ext
;
3585 bfd_mips_elf32_swap_reginfo_out (abfd
, ®info
, &ext
);
3586 if (! bfd_set_section_contents (abfd
, reginfo_sec
, (PTR
) &ext
,
3587 (file_ptr
) 0, sizeof ext
))
3591 if (mdebug_sec
!= (asection
*) NULL
)
3593 BFD_ASSERT (abfd
->output_has_begun
);
3594 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle
, abfd
, &debug
,
3596 mdebug_sec
->filepos
))
3599 bfd_ecoff_debug_free (mdebug_handle
, abfd
, &debug
, swap
, info
);
3602 if (gptab_data_sec
!= (asection
*) NULL
)
3604 if (! bfd_set_section_contents (abfd
, gptab_data_sec
,
3605 gptab_data_sec
->contents
,
3607 gptab_data_sec
->_raw_size
))
3611 if (gptab_bss_sec
!= (asection
*) NULL
)
3613 if (! bfd_set_section_contents (abfd
, gptab_bss_sec
,
3614 gptab_bss_sec
->contents
,
3616 gptab_bss_sec
->_raw_size
))
3620 if (SGI_COMPAT (abfd
))
3622 rtproc_sec
= bfd_get_section_by_name (abfd
, ".rtproc");
3623 if (rtproc_sec
!= NULL
)
3625 if (! bfd_set_section_contents (abfd
, rtproc_sec
,
3626 rtproc_sec
->contents
,
3628 rtproc_sec
->_raw_size
))
3636 /* Handle a MIPS ELF HI16 reloc. */
3639 mips_elf_relocate_hi16 (input_bfd
, relhi
, rello
, contents
, addend
)
3641 Elf_Internal_Rela
*relhi
;
3642 Elf_Internal_Rela
*rello
;
3649 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
3651 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
3654 addend
+= ((insn
& 0xffff) << 16) + addlo
;
3656 if ((addlo
& 0x8000) != 0)
3658 if ((addend
& 0x8000) != 0)
3661 bfd_put_32 (input_bfd
,
3662 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
3663 contents
+ relhi
->r_offset
);
3666 /* Handle a MIPS ELF local GOT16 reloc. */
3669 mips_elf_relocate_got_local (output_bfd
, input_bfd
, sgot
, relhi
, rello
,
3674 Elf_Internal_Rela
*relhi
;
3675 Elf_Internal_Rela
*rello
;
3685 bfd_byte
*got_contents
;
3686 struct mips_got_info
*g
;
3688 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
3690 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
3693 addend
+= ((insn
& 0xffff) << 16) + addlo
;
3695 if ((addlo
& 0x8000) != 0)
3697 if ((addend
& 0x8000) != 0)
3700 /* Get a got entry representing requested hipage. */
3701 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
3702 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
3703 BFD_ASSERT (g
!= NULL
);
3705 local_gotno
= g
->local_gotno
;
3706 got_contents
= sgot
->contents
;
3707 hipage
= addend
& 0xffff0000;
3709 for (i
= MIPS_RESERVED_GOTNO
; i
< local_gotno
; i
++)
3711 address
= bfd_get_32 (input_bfd
, got_contents
+ i
* 4);
3712 if (hipage
== (address
& 0xffff0000))
3714 if (address
== (bfd_vma
) 0)
3716 bfd_put_32 (input_bfd
, hipage
, got_contents
+ i
* 4);
3721 BFD_ASSERT (i
< local_gotno
);
3723 if (i
== local_gotno
)
3724 (*_bfd_error_handler
)
3725 ("ELF MIPS linker: more got entries are needed for hipage: %x",
3729 i
= - ELF_MIPS_GP_OFFSET (output_bfd
) + i
* 4;
3730 bfd_put_32 (input_bfd
, (insn
& 0xffff0000) | (i
& 0xffff),
3731 contents
+ relhi
->r_offset
);
3734 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
3737 mips_elf_relocate_global_got (input_bfd
, rel
, contents
, offset
)
3739 Elf_Internal_Rela
*rel
;
3745 insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3746 bfd_put_32 (input_bfd
,
3747 (insn
& 0xffff0000) | (offset
& 0xffff),
3748 contents
+ rel
->r_offset
);
3751 /* Relocate a MIPS ELF section. */
3754 mips_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
3755 contents
, relocs
, local_syms
, local_sections
)
3757 struct bfd_link_info
*info
;
3759 asection
*input_section
;
3761 Elf_Internal_Rela
*relocs
;
3762 Elf_Internal_Sym
*local_syms
;
3763 asection
**local_sections
;
3765 Elf_Internal_Shdr
*symtab_hdr
;
3768 asection
*sgot
, *sreloc
, *scpt
;
3770 Elf_Internal_Rela
*rel
;
3771 Elf_Internal_Rela
*relend
;
3772 struct mips_got_info
*g
;
3774 dynobj
= elf_hash_table (info
)->dynobj
;
3775 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3779 if (dynobj
== NULL
|| ! SGI_COMPAT (output_bfd
))
3782 scpt
= bfd_get_section_by_name (dynobj
, ".compact_rel");
3785 if (elf_bad_symtab (input_bfd
))
3787 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
3792 locsymcount
= symtab_hdr
->sh_info
;
3793 extsymoff
= symtab_hdr
->sh_info
;
3797 relend
= relocs
+ input_section
->reloc_count
;
3798 for (; rel
< relend
; rel
++)
3801 reloc_howto_type
*howto
;
3802 unsigned long r_symndx
;
3804 struct elf_link_hash_entry
*h
;
3806 Elf_Internal_Sym
*sym
;
3807 bfd_reloc_status_type r
;
3809 r_type
= ELF32_R_TYPE (rel
->r_info
);
3810 if (r_type
< 0 || r_type
>= (int) R_MIPS_max
)
3812 bfd_set_error (bfd_error_bad_value
);
3815 howto
= elf_mips_howto_table
+ r_type
;
3818 && (r_type
== R_MIPS_CALL16
3819 || r_type
== R_MIPS_GOT16
3820 || r_type
== R_MIPS_CALL_HI16
3821 || r_type
== R_MIPS_CALL_LO16
3822 || r_type
== R_MIPS_GOT_HI16
3823 || r_type
== R_MIPS_GOT_LO16
))
3825 /* We need the .got section. */
3828 sgot
= bfd_get_section_by_name (dynobj
, ".got");
3829 BFD_ASSERT (sgot
!= NULL
);
3830 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
3831 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
3832 BFD_ASSERT (g
!= NULL
);
3836 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3838 /* Mix in the change in GP address for a GP relative reloc. */
3839 if (r_type
!= R_MIPS_GPREL16
3840 && r_type
!= R_MIPS_LITERAL
3841 && r_type
!= R_MIPS_GPREL32
)
3845 if (elf_gp (output_bfd
) == 0)
3847 if (! ((*info
->callbacks
->reloc_dangerous
)
3849 "GP relative relocation when GP not defined",
3850 input_bfd
, input_section
,
3853 /* Only give the error once per link. */
3854 elf_gp (output_bfd
) = 4;
3857 if (r_symndx
< extsymoff
3858 || (elf_bad_symtab (input_bfd
)
3859 && local_sections
[r_symndx
] != NULL
))
3861 /* This is a relocation against a section. The current
3862 addend in the instruction is the difference between
3863 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
3864 must change this to be the difference between the
3865 final definition (which will end up in RELOCATION)
3866 and the GP value of OUTPUT_BFD (which is in GP). */
3867 addend
= elf_gp (input_bfd
) - elf_gp (output_bfd
);
3869 else if (! info
->relocateable
)
3871 /* We are doing a final link. The current addend in the
3872 instruction is simply the desired offset into the
3873 symbol (normally zero). We want the instruction to
3874 hold the difference between the final definition of
3875 the symbol (which will end up in RELOCATION) and the
3876 GP value of OUTPUT_BFD (which is in GP). */
3877 addend
= - elf_gp (output_bfd
);
3881 /* We are generating relocateable output, and we aren't
3882 going to define this symbol, so we just leave the
3883 instruction alone. */
3891 if (info
->relocateable
)
3893 /* This is a relocateable link. We don't have to change
3894 anything, unless the reloc is against a section symbol,
3895 in which case we have to adjust according to where the
3896 section symbol winds up in the output section. */
3897 if (r_symndx
>= locsymcount
3898 || (elf_bad_symtab (input_bfd
)
3899 && local_sections
[r_symndx
] == NULL
))
3903 sym
= local_syms
+ r_symndx
;
3904 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3908 sec
= local_sections
[r_symndx
];
3910 /* It would be logical to add sym->st_value here,
3911 but Irix 5 sometimes generates a garbage symbol
3913 addend
+= sec
->output_offset
;
3915 /* If this is HI16 or GOT16 with an associated LO16,
3916 adjust the addend accordingly. Otherwise, just
3918 if ((r_type
!= R_MIPS_HI16
&& r_type
!= R_MIPS_GOT16
)
3919 || (rel
+ 1) >= relend
3920 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
3921 r
= _bfd_relocate_contents (howto
, input_bfd
,
3923 contents
+ rel
->r_offset
);
3926 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
3938 /* This is a final link. */
3940 if (r_symndx
< extsymoff
3941 || (elf_bad_symtab (input_bfd
)
3942 && local_sections
[r_symndx
] != NULL
))
3945 sym
= local_syms
+ r_symndx
;
3946 sec
= local_sections
[r_symndx
];
3947 relocation
= (sec
->output_section
->vma
3948 + sec
->output_offset
);
3950 /* It would be logical to always add sym->st_value here,
3951 but Irix 5 sometimes generates a garbage symbol
3953 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3954 relocation
+= sym
->st_value
;
3961 indx
= r_symndx
- extsymoff
;
3962 h
= elf_sym_hashes (input_bfd
)[indx
];
3963 while (h
->root
.type
== bfd_link_hash_indirect
3964 || h
->root
.type
== bfd_link_hash_warning
)
3965 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3966 if (strcmp (h
->root
.root
.string
, "_gp_disp") == 0)
3968 if (elf_gp (output_bfd
) == 0)
3970 if (! ((*info
->callbacks
->reloc_dangerous
)
3972 "_gp_disp used when GP not defined",
3973 input_bfd
, input_section
,
3976 /* Only give the error once per link. */
3977 elf_gp (output_bfd
) = 4;
3982 sec
= input_section
;
3983 if (sec
->output_section
!= NULL
)
3984 relocation
= (elf_gp (output_bfd
)
3986 + sec
->output_section
->vma
3987 + sec
->output_offset
));
3989 relocation
= elf_gp (output_bfd
) - rel
->r_offset
;
3990 if (r_type
== R_MIPS_LO16
)
3994 else if (h
->root
.type
== bfd_link_hash_defined
3995 || h
->root
.type
== bfd_link_hash_defweak
)
3997 sec
= h
->root
.u
.def
.section
;
3998 if (sec
->output_section
== NULL
)
4001 relocation
= (h
->root
.u
.def
.value
4002 + sec
->output_section
->vma
4003 + sec
->output_offset
);
4005 else if (h
->root
.type
== bfd_link_hash_undefweak
)
4007 else if (info
->shared
&& ! info
->symbolic
)
4009 else if (strcmp (h
->root
.root
.string
, "_DYNAMIC_LINK") == 0)
4011 /* If this is a dynamic link, we should have created
4012 a _DYNAMIC_LINK symbol in
4013 mips_elf_create_dynamic_sections. Otherwise, we
4014 should define the symbol with a value of 0.
4015 FIXME: It should probably get into the symbol
4016 table somehow as well. */
4017 BFD_ASSERT (! info
->shared
);
4018 BFD_ASSERT (bfd_get_section_by_name (output_bfd
,
4019 ".dynamic") == NULL
);
4024 if (! ((*info
->callbacks
->undefined_symbol
)
4025 (info
, h
->root
.root
.string
, input_bfd
,
4026 input_section
, rel
->r_offset
)))
4032 if (r_type
== R_MIPS_HI16
4033 && (rel
+ 1) < relend
4034 && ELF32_R_TYPE ((rel
+ 1)->r_info
) == R_MIPS_LO16
)
4036 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
4037 contents
, relocation
+ addend
);
4040 else if (r_type
== R_MIPS_GOT16
&& local
)
4042 /* GOT16 must be also with associated LO16 in the local
4043 case. In this case, the addend is extracted and the
4044 section in which the referenced object is determined.
4045 Then the final address of the object is computed and
4046 the GOT entry for the hipage (an aligned 64kb chunk)
4047 is added to .got section if needed. The offset field
4048 of the GOT16-relocated instruction is replaced by the
4049 index of this GOT entry for the hipage. */
4050 if ((rel
+ 1) < relend
4051 && ELF32_R_TYPE ((rel
+ 1)->r_info
) == R_MIPS_LO16
)
4053 mips_elf_relocate_got_local (output_bfd
, input_bfd
, sgot
,
4056 relocation
+ addend
);
4060 r
= bfd_reloc_outofrange
;
4062 else if (r_type
== R_MIPS_CALL16
4063 || r_type
== R_MIPS_GOT16
4064 || r_type
== R_MIPS_CALL_LO16
4065 || r_type
== R_MIPS_GOT_LO16
)
4069 /* This symbol must be registered as a global symbol
4070 having the corresponding got entry. */
4071 BFD_ASSERT (h
->got_offset
!= (bfd_vma
) -1);
4073 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
4074 BFD_ASSERT (g
->local_gotno
<= offset
4075 && offset
< sgot
->_raw_size
);
4076 bfd_put_32 (output_bfd
, relocation
+ addend
,
4077 sgot
->contents
+ offset
);
4078 offset
= (sgot
->output_section
->vma
+ sgot
->output_offset
4079 + offset
- elf_gp (output_bfd
));
4080 mips_elf_relocate_global_got (input_bfd
, rel
, contents
,
4084 else if (r_type
== R_MIPS_CALL_HI16
4085 || r_type
== R_MIPS_GOT_HI16
)
4089 /* This must be a global symbol with a got entry. The
4090 next reloc must be the corresponding LO16 reloc. */
4091 BFD_ASSERT (h
!= NULL
&& h
->got_offset
!= (bfd_vma
) -1);
4092 BFD_ASSERT ((rel
+ 1) < relend
);
4093 BFD_ASSERT (ELF32_R_TYPE ((rel
+ 1)->r_info
)
4094 == (r_type
== R_MIPS_CALL_HI16
4096 : R_MIPS_GOT_LO16
));
4098 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
4099 BFD_ASSERT (g
->local_gotno
<= offset
4100 && offset
< sgot
->_raw_size
);
4101 bfd_put_32 (output_bfd
, relocation
+ addend
,
4102 sgot
->contents
+ offset
);
4103 offset
= (sgot
->output_section
->vma
+ sgot
->output_offset
4104 + offset
- elf_gp (output_bfd
));
4105 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1, contents
,
4109 else if (r_type
== R_MIPS_REL32
4110 || r_type
== R_MIPS_32
)
4112 Elf_Internal_Rel outrel
;
4113 Elf32_crinfo cptrel
;
4117 && (input_section
->flags
& SEC_ALLOC
) != 0)
4119 /* When generating a shared object, these
4120 relocations are copied into the output file to be
4121 resolved at run time. */
4124 sreloc
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
4125 BFD_ASSERT (sreloc
!= NULL
);
4128 outrel
.r_offset
= (rel
->r_offset
4129 + input_section
->output_section
->vma
4130 + input_section
->output_offset
);
4132 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
4135 && (! info
->symbolic
4136 || (h
->elf_link_hash_flags
4137 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
4139 BFD_ASSERT (h
->dynindx
!= -1);
4140 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MIPS_REL32
);
4141 sec
= input_section
;
4148 sec
= local_sections
[r_symndx
];
4151 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4153 == bfd_link_hash_defweak
));
4154 sec
= h
->root
.u
.def
.section
;
4156 if (sec
!= NULL
&& bfd_is_abs_section (sec
))
4158 else if (sec
== NULL
|| sec
->owner
== NULL
)
4160 bfd_set_error (bfd_error_bad_value
);
4167 osec
= sec
->output_section
;
4168 indx
= elf_section_data (osec
)->dynindx
;
4173 outrel
.r_info
= ELF32_R_INFO (indx
, R_MIPS_REL32
);
4174 addend
+= relocation
;
4177 bfd_put_32 (output_bfd
, addend
, contents
+ rel
->r_offset
);
4178 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
4179 (((Elf32_External_Rel
*)
4181 + sreloc
->reloc_count
));
4182 ++sreloc
->reloc_count
;
4184 if (SGI_COMPAT (output_bfd
))
4189 /* Make an entry of compact relocation info. */
4190 mips_elf_set_cr_format (cptrel
, CRF_MIPS_LONG
);
4191 cptrel
.vaddr
= (rel
->r_offset
4192 + input_section
->output_section
->vma
4193 + input_section
->output_offset
);
4194 if (r_type
== R_MIPS_REL32
)
4195 mips_elf_set_cr_type (cptrel
, CRT_MIPS_REL32
);
4197 mips_elf_set_cr_type (cptrel
, CRT_MIPS_WORD
);
4198 mips_elf_set_cr_dist2to (cptrel
, 0);
4199 cptrel
.konst
= addend
;
4201 cr
= (scpt
->contents
4202 + sizeof (Elf32_External_compact_rel
));
4203 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4204 ((Elf32_External_crinfo
*) cr
4205 + scpt
->reloc_count
));
4206 ++scpt
->reloc_count
;
4209 /* This reloc will be computed at runtime, so
4210 there's no need to do anything now. */
4214 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4215 contents
, rel
->r_offset
,
4216 relocation
, addend
);
4219 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4220 contents
, rel
->r_offset
,
4221 relocation
, addend
);
4223 if (SGI_COMPAT (abfd
)
4225 && (input_section
->flags
& SEC_ALLOC
) != 0)
4227 Elf32_crinfo cptrel
;
4230 /* Make an entry of compact relocation info. */
4231 mips_elf_set_cr_format (cptrel
, CRF_MIPS_LONG
);
4232 cptrel
.vaddr
= (rel
->r_offset
4233 + input_section
->output_section
->vma
4234 + input_section
->output_offset
);
4239 mips_elf_set_cr_type (cptrel
, CRT_MIPS_JMPAD
);
4240 /* XXX How should we set dist2to in this case. */
4241 mips_elf_set_cr_dist2to (cptrel
, 8);
4242 cptrel
.konst
= addend
+ relocation
;
4243 cr
= scpt
->contents
+ sizeof (Elf32_External_compact_rel
);
4244 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4245 ((Elf32_External_crinfo
*) cr
4246 + scpt
->reloc_count
));
4247 ++scpt
->reloc_count
;
4250 case R_MIPS_GPREL16
:
4251 case R_MIPS_LITERAL
:
4252 case R_MIPS_GPREL32
:
4253 mips_elf_set_cr_type (cptrel
, CRT_MIPS_GPHI_LO
);
4254 cptrel
.konst
= elf_gp (output_bfd
) - cptrel
.vaddr
;
4255 mips_elf_set_cr_dist2to (cptrel
, 4);
4256 cr
= scpt
->contents
+ sizeof (Elf32_External_compact_rel
);
4257 bfd_elf32_swap_crinfo_out (output_bfd
, &cptrel
,
4258 ((Elf32_External_crinfo
*) cr
4259 + scpt
->reloc_count
));
4260 ++scpt
->reloc_count
;
4269 if (r
!= bfd_reloc_ok
)
4274 case bfd_reloc_outofrange
:
4276 case bfd_reloc_overflow
:
4281 name
= h
->root
.root
.string
;
4284 name
= bfd_elf_string_from_elf_section (input_bfd
,
4285 symtab_hdr
->sh_link
,
4290 name
= bfd_section_name (input_bfd
, sec
);
4292 if (! ((*info
->callbacks
->reloc_overflow
)
4293 (info
, name
, howto
->name
, (bfd_vma
) 0,
4294 input_bfd
, input_section
, rel
->r_offset
)))
4305 /* Functions for the dynamic linker. */
4307 /* The name of the dynamic interpreter. This is put in the .interp
4310 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
4312 /* Create dynamic sections when linking against a dynamic object. */
4315 mips_elf_create_dynamic_sections (abfd
, info
)
4317 struct bfd_link_info
*info
;
4319 struct elf_link_hash_entry
*h
;
4321 register asection
*s
;
4322 const char * const *namep
;
4324 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4327 /* Mips ABI requests the .dynamic section to be read only. */
4328 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4331 if (! bfd_set_section_flags (abfd
, s
, flags
))
4335 /* We need to create .got section. */
4336 if (! mips_elf_create_got_section (abfd
, info
))
4339 /* Create .stub section. */
4340 if (bfd_get_section_by_name (abfd
, ".stub") == NULL
)
4342 s
= bfd_make_section (abfd
, ".stub");
4344 || ! bfd_set_section_flags (abfd
, s
, flags
)
4345 || ! bfd_set_section_alignment (abfd
, s
, 2))
4349 if (SGI_COMPAT (abfd
))
4351 for (namep
= mips_elf_dynsym_rtproc_names
; *namep
!= NULL
; namep
++)
4354 if (! (_bfd_generic_link_add_one_symbol
4355 (info
, abfd
, *namep
, BSF_GLOBAL
, bfd_und_section_ptr
,
4356 (bfd_vma
) 0, (const char *) NULL
, false,
4357 get_elf_backend_data (abfd
)->collect
,
4358 (struct bfd_link_hash_entry
**) &h
)))
4360 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
4361 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4362 h
->type
= STT_SECTION
;
4364 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4368 /* We need to create a .compact_rel section. */
4369 if (! mips_elf_create_compact_rel_section (abfd
, info
))
4372 /* Change aligments of some sections. */
4373 s
= bfd_get_section_by_name (abfd
, ".hash");
4375 bfd_set_section_alignment (abfd
, s
, 4);
4376 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4378 bfd_set_section_alignment (abfd
, s
, 4);
4379 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4381 bfd_set_section_alignment (abfd
, s
, 4);
4382 s
= bfd_get_section_by_name (abfd
, ".reginfo");
4384 bfd_set_section_alignment (abfd
, s
, 4);
4385 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4387 bfd_set_section_alignment (abfd
, s
, 4);
4393 if (! (_bfd_generic_link_add_one_symbol
4394 (info
, abfd
, "_DYNAMIC_LINK", BSF_GLOBAL
, bfd_abs_section_ptr
,
4395 (bfd_vma
) 0, (const char *) NULL
, false,
4396 get_elf_backend_data (abfd
)->collect
,
4397 (struct bfd_link_hash_entry
**) &h
)))
4399 h
->elf_link_hash_flags
^=~ ELF_LINK_NON_ELF
;
4400 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4401 h
->type
= STT_SECTION
;
4403 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4410 /* Create the .compact_rel section. */
4413 mips_elf_create_compact_rel_section (abfd
, info
)
4415 struct bfd_link_info
*info
;
4418 register asection
*s
;
4420 if (bfd_get_section_by_name (abfd
, ".compact_rel") == NULL
)
4422 flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
;
4424 s
= bfd_make_section (abfd
, ".compact_rel");
4426 || ! bfd_set_section_flags (abfd
, s
, flags
)
4427 || ! bfd_set_section_alignment (abfd
, s
, 2))
4430 s
->_raw_size
= sizeof (Elf32_External_compact_rel
);
4436 /* Create the .got section to hold the global offset table. */
4439 mips_elf_create_got_section (abfd
, info
)
4441 struct bfd_link_info
*info
;
4444 register asection
*s
;
4445 struct elf_link_hash_entry
*h
;
4446 struct mips_got_info
*g
;
4448 /* This function may be called more than once. */
4449 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
4452 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
4454 s
= bfd_make_section (abfd
, ".got");
4456 || ! bfd_set_section_flags (abfd
, s
, flags
)
4457 || ! bfd_set_section_alignment (abfd
, s
, 4))
4460 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4461 linker script because we don't want to define the symbol if we
4462 are not creating a global offset table. */
4464 if (! (_bfd_generic_link_add_one_symbol
4465 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
4466 (bfd_vma
) 0, (const char *) NULL
, false,
4467 get_elf_backend_data (abfd
)->collect
,
4468 (struct bfd_link_hash_entry
**) &h
)))
4470 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
4471 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4472 h
->type
= STT_OBJECT
;
4475 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4478 /* The first several global offset table entries are reserved. */
4479 s
->_raw_size
= MIPS_RESERVED_GOTNO
* 4;
4481 g
= (struct mips_got_info
*) bfd_alloc (abfd
,
4482 sizeof (struct mips_got_info
));
4485 g
->global_gotsym
= 0;
4486 g
->local_gotno
= MIPS_RESERVED_GOTNO
;
4487 if (elf_section_data (s
) == NULL
)
4490 (PTR
) bfd_zalloc (abfd
, sizeof (struct bfd_elf_section_data
));
4491 if (elf_section_data (s
) == NULL
)
4494 elf_section_data (s
)->tdata
= (PTR
) g
;
4499 /* Look through the relocs for a section during the first phase, and
4500 allocate space in the global offset table. */
4503 mips_elf_check_relocs (abfd
, info
, sec
, relocs
)
4505 struct bfd_link_info
*info
;
4507 const Elf_Internal_Rela
*relocs
;
4510 Elf_Internal_Shdr
*symtab_hdr
;
4511 struct elf_link_hash_entry
**sym_hashes
;
4512 struct mips_got_info
*g
;
4514 const Elf_Internal_Rela
*rel
;
4515 const Elf_Internal_Rela
*rel_end
;
4519 if (info
->relocateable
)
4522 dynobj
= elf_hash_table (info
)->dynobj
;
4523 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4524 sym_hashes
= elf_sym_hashes (abfd
);
4525 extsymoff
= (elf_bad_symtab (abfd
)) ? 0 : symtab_hdr
->sh_info
;
4530 rel_end
= relocs
+ sec
->reloc_count
;
4531 for (rel
= relocs
; rel
< rel_end
; rel
++)
4533 unsigned long r_symndx
;
4534 struct elf_link_hash_entry
*h
;
4536 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4538 if (r_symndx
< extsymoff
)
4541 h
= sym_hashes
[r_symndx
- extsymoff
];
4543 /* Some relocs require a global offset table. */
4546 switch (ELF32_R_TYPE (rel
->r_info
))
4550 case R_MIPS_CALL_HI16
:
4551 case R_MIPS_CALL_LO16
:
4552 case R_MIPS_GOT_HI16
:
4553 case R_MIPS_GOT_LO16
:
4554 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
4555 if (! mips_elf_create_got_section (dynobj
, info
))
4564 switch (ELF32_R_TYPE (rel
->r_info
))
4567 case R_MIPS_CALL_HI16
:
4568 case R_MIPS_CALL_LO16
:
4569 /* This symbol requires a global offset table entry. */
4572 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4573 BFD_ASSERT (sgot
!= NULL
);
4574 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4575 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4576 BFD_ASSERT (g
!= NULL
);
4579 BFD_ASSERT (h
!= NULL
);
4581 /* Make sure this symbol is output as a dynamic symbol. */
4582 if (h
->dynindx
== -1)
4584 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4588 if (h
->got_offset
!= (bfd_vma
) -1)
4590 /* We have already allocated space in the .got. */
4594 /* Note the index of the first global got symbol in .dynsym. */
4595 if (g
->global_gotsym
== 0
4596 || g
->global_gotsym
> (unsigned long) h
->dynindx
)
4597 g
->global_gotsym
= h
->dynindx
;
4599 /* Make this symbol to have the corresponding got entry. */
4602 /* We need a stub, not a plt entry for the undefined
4603 function. But we record it as if it needs plt. See
4604 elf_adjust_dynamic_symbol in elflink.h. */
4605 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
4611 case R_MIPS_GOT_HI16
:
4612 case R_MIPS_GOT_LO16
:
4613 /* This symbol requires a global offset table entry. */
4616 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4617 BFD_ASSERT (sgot
!= NULL
);
4618 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4619 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4620 BFD_ASSERT (g
!= NULL
);
4625 /* Make sure this symbol is output as a dynamic symbol. */
4626 if (h
->dynindx
== -1)
4628 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4632 if (h
->got_offset
!= (bfd_vma
) -1)
4634 /* We have already allocated space in the .got. */
4637 /* Note the index of the first global got symbol in
4639 if (g
->global_gotsym
== 0
4640 || g
->global_gotsym
> (unsigned long) h
->dynindx
)
4641 g
->global_gotsym
= h
->dynindx
;
4643 /* Make this symbol to be the global got symbol. */
4652 && (sec
->flags
& SEC_ALLOC
) != 0)
4654 /* When creating a shared object, we must copy these
4655 reloc types into the output file as R_MIPS_REL32
4656 relocs. We create the .rel.dyn reloc section in
4657 dynobj and make room for this reloc. */
4660 const char *name
= ".rel.dyn";
4662 sreloc
= bfd_get_section_by_name (dynobj
, name
);
4665 sreloc
= bfd_make_section (dynobj
, name
);
4667 || ! bfd_set_section_flags (dynobj
, sreloc
,
4673 || ! bfd_set_section_alignment (dynobj
, sreloc
, 4))
4676 /* Add a null element. */
4677 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
4678 ++sreloc
->reloc_count
;
4682 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
4685 if (SGI_COMPAT (abfd
))
4686 mips_elf_hash_table (info
)->compact_rel_size
+=
4687 sizeof (Elf32_External_crinfo
);
4692 case R_MIPS_GPREL16
:
4693 case R_MIPS_LITERAL
:
4694 case R_MIPS_GPREL32
:
4695 if (SGI_COMPAT (abfd
))
4696 mips_elf_hash_table (info
)->compact_rel_size
+=
4697 sizeof (Elf32_External_crinfo
);
4708 /* Adjust a symbol defined by a dynamic object and referenced by a
4709 regular object. The current definition is in some section of the
4710 dynamic object, but we're not including those sections. We have to
4711 change the definition to something the rest of the link can
4715 mips_elf_adjust_dynamic_symbol (info
, h
)
4716 struct bfd_link_info
*info
;
4717 struct elf_link_hash_entry
*h
;
4722 dynobj
= elf_hash_table (info
)->dynobj
;
4724 /* Make sure we know what is going on here. */
4725 BFD_ASSERT (dynobj
!= NULL
4726 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
4727 || h
->weakdef
!= NULL
4728 || ((h
->elf_link_hash_flags
4729 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4730 && (h
->elf_link_hash_flags
4731 & ELF_LINK_HASH_REF_REGULAR
) != 0
4732 && (h
->elf_link_hash_flags
4733 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
4735 /* For a function, create a stub, if needed. */
4736 if (h
->type
== STT_FUNC
4737 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
4739 if (! elf_hash_table (info
)->dynamic_sections_created
)
4742 /* If this symbol is not defined in a regular file, then set
4743 the symbol to the stub location. This is required to make
4744 function pointers compare as equal between the normal
4745 executable and the shared library. */
4746 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4748 /* We need .stub section. */
4749 s
= bfd_get_section_by_name (dynobj
, ".stub");
4750 BFD_ASSERT (s
!= NULL
);
4752 h
->root
.u
.def
.section
= s
;
4753 h
->root
.u
.def
.value
= s
->_raw_size
;
4755 /* XXX Write this stub address somewhere. */
4756 h
->plt_offset
= s
->_raw_size
;
4758 /* Make room for this stub code. */
4759 s
->_raw_size
+= MIPS_FUNCTION_STUB_SIZE
;
4761 /* The last half word of the stub will be filled with the index
4762 of this symbol in .dynsym section. */
4767 /* If this is a weak symbol, and there is a real definition, the
4768 processor independent code will have arranged for us to see the
4769 real definition first, and we can just use the same value. */
4770 if (h
->weakdef
!= NULL
)
4772 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
4773 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
4774 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
4775 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
4779 /* This is a reference to a symbol defined by a dynamic object which
4780 is not a function. */
4785 /* Set the sizes of the dynamic sections. */
4788 mips_elf_size_dynamic_sections (output_bfd
, info
)
4790 struct bfd_link_info
*info
;
4796 struct mips_got_info
*g
;
4798 dynobj
= elf_hash_table (info
)->dynobj
;
4799 BFD_ASSERT (dynobj
!= NULL
);
4801 if (elf_hash_table (info
)->dynamic_sections_created
)
4803 /* Set the contents of the .interp section to the interpreter. */
4806 s
= bfd_get_section_by_name (dynobj
, ".interp");
4807 BFD_ASSERT (s
!= NULL
);
4808 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4809 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4813 /* Recompute the size of .got for local entires (reserved and
4814 hipages) if needed. To estimate it, get the upper bound of total
4815 size of loadable sections. */
4816 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4820 bfd_size_type loadable_size
= 0;
4821 bfd_size_type local_gotno
;
4824 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
4825 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
4826 BFD_ASSERT (g
!= NULL
);
4828 for (sub
= info
->input_bfds
; sub
; sub
= sub
->link_next
)
4829 for (s
= sub
->sections
; s
!= NULL
; s
= s
->next
)
4831 if ((s
->flags
& SEC_ALLOC
) == 0)
4833 loadable_size
+= (s
->_raw_size
+ 0xf) & ~0xf;
4836 loadable_size
+= MIPS_FUNCTION_STUB_SIZE
;
4838 /* Assume there are two loadable segments consisting of
4839 contiguous sections. Is 5 enough? */
4840 local_gotno
= (loadable_size
>> 16) + 5 + MIPS_RESERVED_GOTNO
;
4841 g
->local_gotno
= local_gotno
;
4842 sgot
->_raw_size
+= local_gotno
* 4;
4845 /* The check_relocs and adjust_dynamic_symbol entry points have
4846 determined the sizes of the various dynamic sections. Allocate
4849 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4854 /* It's OK to base decisions on the section name, because none
4855 of the dynobj section names depend upon the input files. */
4856 name
= bfd_get_section_name (dynobj
, s
);
4858 if ((s
->flags
& SEC_IN_MEMORY
) == 0)
4863 if (strncmp (name
, ".rel", 4) == 0)
4865 if (s
->_raw_size
== 0)
4871 /* If this relocation section applies to a read only
4872 section, then we probably need a DT_TEXTREL entry.
4873 If the relocation section is .rel.dyn, we always
4874 assert a DT_TEXTREL entry rather than testing whether
4875 there exists a relocation to a read only section or
4877 target
= bfd_get_section_by_name (output_bfd
, name
+ 4);
4878 if ((target
!= NULL
&& (target
->flags
& SEC_READONLY
) != 0)
4879 || strcmp (name
, ".rel.dyn") == 0)
4882 /* We use the reloc_count field as a counter if we need
4883 to copy relocs into the output file. */
4884 if (strcmp (name
, ".rel.dyn") != 0)
4888 else if (strncmp (name
, ".got", 4) == 0)
4892 BFD_ASSERT (elf_section_data (s
) != NULL
);
4893 g
= (struct mips_got_info
*) elf_section_data (s
)->tdata
;
4894 BFD_ASSERT (g
!= NULL
);
4896 /* Fix the size of .got section for the correspondence of
4897 global symbols and got entries. This adds some useless
4898 got entries. Is this required by ABI really? */
4899 i
= elf_hash_table (info
)->dynsymcount
- g
->global_gotsym
;
4900 s
->_raw_size
+= i
* 4;
4902 else if (strncmp (name
, ".stub", 5) == 0)
4904 /* Irix rld assumes that the function stub isn't at the end
4905 of .text section. So put a dummy. XXX */
4906 s
->_raw_size
+= MIPS_FUNCTION_STUB_SIZE
;
4908 else if (SGI_COMPAT (output_bfd
)
4909 && strncmp (name
, ".compact_rel", 12) == 0)
4910 s
->_raw_size
+= mips_elf_hash_table (info
)->compact_rel_size
;
4911 else if (strncmp (name
, ".init", 5) != 0)
4913 /* It's not one of our sections, so don't allocate space. */
4921 for (spp
= &s
->output_section
->owner
->sections
;
4922 *spp
!= s
->output_section
;
4923 spp
= &(*spp
)->next
)
4925 *spp
= s
->output_section
->next
;
4926 --s
->output_section
->owner
->section_count
;
4931 /* Allocate memory for the section contents. */
4932 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
4933 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
4935 bfd_set_error (bfd_error_no_memory
);
4938 memset (s
->contents
, 0, s
->_raw_size
);
4941 if (elf_hash_table (info
)->dynamic_sections_created
)
4943 /* Add some entries to the .dynamic section. We fill in the
4944 values later, in elf_mips_finish_dynamic_sections, but we
4945 must add the entries now so that we get the correct size for
4946 the .dynamic section. The DT_DEBUG entry is filled in by the
4947 dynamic linker and used by the debugger. */
4950 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
4956 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
4960 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0))
4963 if (bfd_get_section_by_name (dynobj
, ".rel.dyn"))
4965 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0))
4968 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0))
4971 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
, 0))
4975 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_CONFLICTNO
, 0))
4978 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LIBLISTNO
, 0))
4981 if (bfd_get_section_by_name (dynobj
, ".conflict") != NULL
)
4983 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_CONFLICT
, 0))
4986 s
= bfd_get_section_by_name (dynobj
, ".liblist");
4987 BFD_ASSERT (s
!= NULL
);
4989 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LIBLIST
, 0))
4993 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_RLD_VERSION
, 0))
4996 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_FLAGS
, 0))
5000 /* Time stamps in executable files are a bad idea. */
5001 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_TIME_STAMP
, 0))
5006 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_ICHECKSUM
, 0))
5011 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_IVERSION
, 0))
5015 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_BASE_ADDRESS
, 0))
5018 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_LOCAL_GOTNO
, 0))
5021 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_SYMTABNO
, 0))
5024 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_UNREFEXTNO
, 0))
5027 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_GOTSYM
, 0))
5030 if (! bfd_elf32_add_dynamic_entry (info
, DT_MIPS_HIPAGENO
, 0))
5033 #if 0 /* (SGI_COMPAT) */
5034 if (! bfd_get_section_by_name (dynobj
, ".init"))
5035 if (! bfd_elf32_add_dynamic_entry (info
, DT_INIT
, 0))
5038 if (! bfd_get_section_by_name (dynobj
, ".fini"))
5039 if (! bfd_elf32_add_dynamic_entry (info
, DT_FINI
, 0))
5044 /* If we use dynamic linking, we generate a section symbol for each
5045 output section. These are local symbols, which means that they
5046 must come first in the dynamic symbol table.
5047 That means we must increment the dynamic symbol index of every
5048 other dynamic symbol. */
5050 const char * const *namep
;
5052 bfd_size_type strindex
;
5053 struct bfd_strtab_hash
*dynstr
;
5054 struct mips_got_info
*g
;
5056 if (elf_hash_table (info
)->dynamic_sections_created
)
5058 if (SGI_COMPAT (output_bfd
))
5060 c
= SIZEOF_MIPS_DYNSYM_SECNAMES
- 1;
5061 elf_link_hash_traverse (elf_hash_table (info
),
5062 mips_elf_adjust_dynindx
,
5064 elf_hash_table (info
)->dynsymcount
+= c
;
5066 dynstr
= elf_hash_table (info
)->dynstr
;
5067 BFD_ASSERT (dynstr
!= NULL
);
5069 for (i
= 1, namep
= mips_elf_dynsym_sec_names
;
5073 s
= bfd_get_section_by_name (output_bfd
, *namep
);
5075 elf_section_data (s
)->dynindx
= i
;
5077 strindex
= _bfd_stringtab_add (dynstr
, *namep
, true, false);
5078 if (strindex
== (bfd_size_type
) -1)
5081 mips_elf_hash_table (info
)->dynsym_sec_strindex
[i
] = strindex
;
5086 c
= bfd_count_sections (output_bfd
);
5087 elf_link_hash_traverse (elf_hash_table (info
),
5088 mips_elf_adjust_dynindx
,
5090 elf_hash_table (info
)->dynsymcount
+= c
;
5092 for (i
= 1, s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
, i
++)
5094 elf_section_data (s
)->dynindx
= i
;
5095 /* These symbols will have no names, so we don't need to
5096 fiddle with dynstr_index. */
5101 s
= bfd_get_section_by_name (dynobj
, ".got");
5102 BFD_ASSERT (s
!= NULL
);
5103 BFD_ASSERT (elf_section_data (s
) != NULL
);
5104 g
= (struct mips_got_info
*) elf_section_data (s
)->tdata
;
5105 BFD_ASSERT (g
!= NULL
);
5107 /* If there are no global got symbols, fake the last symbol so for
5109 if (g
->global_gotsym
)
5110 g
->global_gotsym
+= c
;
5112 g
->global_gotsym
= elf_hash_table (info
)->dynsymcount
- 1;
5118 /* Increment the index of a dynamic symbol by a given amount. Called
5119 via elf_link_hash_traverse. */
5122 mips_elf_adjust_dynindx (h
, cparg
)
5123 struct elf_link_hash_entry
*h
;
5126 unsigned int *cp
= (unsigned int *) cparg
;
5128 if (h
->dynindx
!= -1)
5133 /* Finish up dynamic symbol handling. We set the contents of various
5134 dynamic sections here. */
5137 mips_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
5139 struct bfd_link_info
*info
;
5140 struct elf_link_hash_entry
*h
;
5141 Elf_Internal_Sym
*sym
;
5146 struct mips_got_info
*g
;
5149 dynobj
= elf_hash_table (info
)->dynobj
;
5150 gval
= sym
->st_value
;
5152 if (h
->plt_offset
!= (bfd_vma
) -1)
5156 bfd_byte stub
[MIPS_FUNCTION_STUB_SIZE
];
5158 /* This symbol has a stub. Set it up. */
5160 BFD_ASSERT (h
->dynindx
!= -1);
5162 s
= bfd_get_section_by_name (dynobj
, ".stub");
5163 BFD_ASSERT (s
!= NULL
);
5165 /* Fill the stub. */
5167 bfd_put_32 (output_bfd
, STUB_LW(output_bfd
), p
);
5169 bfd_put_32 (output_bfd
, STUB_MOVE
, p
);
5172 /* FIXME: Can h->dynindex be more than 64K? */
5173 if (h
->dynindx
& 0xffff0000)
5176 bfd_put_32 (output_bfd
, STUB_JALR
, p
);
5178 bfd_put_32 (output_bfd
, STUB_LI16
+ h
->dynindx
, p
);
5180 BFD_ASSERT (h
->plt_offset
<= s
->_raw_size
);
5181 memcpy (s
->contents
+ h
->plt_offset
, stub
, MIPS_FUNCTION_STUB_SIZE
);
5183 /* Mark the symbol as undefined. plt_offset != -1 occurs
5184 only for the referenced symbol. */
5185 sym
->st_shndx
= SHN_UNDEF
;
5187 /* The run-time linker uses the st_value field of the symbol
5188 to reset the global offset table entry for this external
5189 to its stub address when unlinking a shared object. */
5190 gval
= s
->output_section
->vma
+ s
->output_offset
+ h
->plt_offset
;
5191 sym
->st_value
= gval
;
5194 BFD_ASSERT (h
->dynindx
!= -1);
5196 sgot
= bfd_get_section_by_name (dynobj
, ".got");
5197 BFD_ASSERT (sgot
!= NULL
);
5198 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
5199 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
5200 BFD_ASSERT (g
!= NULL
);
5202 if ((unsigned long) h
->dynindx
>= g
->global_gotsym
)
5204 bfd_size_type offset
;
5206 /* This symbol has an entry in the global offset table. Set its
5207 value to the corresponding got entry, if needed. */
5208 if (h
->got_offset
== (bfd_vma
) -1)
5210 offset
= (h
->dynindx
- g
->global_gotsym
+ g
->local_gotno
) * 4;
5211 BFD_ASSERT (g
->local_gotno
* 4 <= offset
5212 && offset
< sgot
->_raw_size
);
5213 bfd_put_32 (output_bfd
, gval
, sgot
->contents
+ offset
);
5217 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5218 name
= h
->root
.root
.string
;
5219 if (strcmp (name
, "_DYNAMIC") == 0
5220 || strcmp (name
, "_GLOBAL_OFFSET_TABLE_") == 0)
5221 sym
->st_shndx
= SHN_ABS
;
5222 else if (strcmp (name
, "_DYNAMIC_LINK") == 0)
5224 sym
->st_shndx
= SHN_ABS
;
5225 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5228 else if (SGI_COMPAT (output_bfd
))
5230 if (strcmp (name
, "_gp_disp") == 0)
5232 sym
->st_shndx
= SHN_ABS
;
5233 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5234 sym
->st_value
= elf_gp (output_bfd
);
5236 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[0]) == 0
5237 || strcmp (name
, mips_elf_dynsym_rtproc_names
[1]) == 0)
5239 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5240 sym
->st_other
= STO_PROTECTED
;
5242 sym
->st_shndx
= SHN_MIPS_DATA
;
5244 else if (strcmp (name
, mips_elf_dynsym_rtproc_names
[2]) == 0)
5246 sym
->st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5247 sym
->st_other
= STO_PROTECTED
;
5248 sym
->st_value
= mips_elf_hash_table (info
)->procedure_count
;
5249 sym
->st_shndx
= SHN_ABS
;
5251 else if (sym
->st_shndx
!= SHN_UNDEF
)
5253 if (h
->type
== STT_FUNC
)
5254 sym
->st_shndx
= SHN_MIPS_TEXT
;
5255 else if (h
->type
== STT_OBJECT
)
5256 sym
->st_shndx
= SHN_MIPS_DATA
;
5263 /* Finish up the dynamic sections. */
5266 mips_elf_finish_dynamic_sections (output_bfd
, info
)
5268 struct bfd_link_info
*info
;
5273 struct mips_got_info
*g
;
5275 dynobj
= elf_hash_table (info
)->dynobj
;
5277 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
5279 sgot
= bfd_get_section_by_name (dynobj
, ".got");
5280 BFD_ASSERT (sgot
!= NULL
);
5282 BFD_ASSERT (elf_section_data (sgot
) != NULL
);
5283 g
= (struct mips_got_info
*) elf_section_data (sgot
)->tdata
;
5284 BFD_ASSERT (g
!= NULL
);
5286 if (elf_hash_table (info
)->dynamic_sections_created
)
5288 Elf32_External_Dyn
*dyncon
, *dynconend
;
5290 BFD_ASSERT (sdyn
!= NULL
);
5292 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5293 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
5294 for (; dyncon
< dynconend
; dyncon
++)
5296 Elf_Internal_Dyn dyn
;
5301 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5309 s
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
5310 BFD_ASSERT (s
!= NULL
);
5311 dyn
.d_un
.d_val
= sizeof (Elf32_External_Rel
);
5312 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5316 /* Rewrite DT_STRSZ. */
5318 _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
5319 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5325 case DT_MIPS_CONFLICT
:
5328 case DT_MIPS_LIBLIST
:
5331 s
= bfd_get_section_by_name (output_bfd
, name
);
5332 BFD_ASSERT (s
!= NULL
);
5333 dyn
.d_un
.d_ptr
= s
->vma
;
5334 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5337 case DT_MIPS_RLD_VERSION
:
5338 dyn
.d_un
.d_val
= 1; /* XXX */
5339 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5343 dyn
.d_un
.d_val
= RHF_NOTPOT
; /* XXX */
5344 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5347 case DT_MIPS_CONFLICTNO
:
5349 elemsize
= sizeof (Elf32_Conflict
);
5352 case DT_MIPS_LIBLISTNO
:
5354 elemsize
= sizeof (Elf32_Lib
);
5356 s
= bfd_get_section_by_name (output_bfd
, name
);
5359 if (s
->_cooked_size
!= 0)
5360 dyn
.d_un
.d_val
= s
->_cooked_size
/ elemsize
;
5362 dyn
.d_un
.d_val
= s
->_raw_size
/ elemsize
;
5367 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5370 case DT_MIPS_TIME_STAMP
:
5371 time ((time_t *) &dyn
.d_un
.d_val
);
5372 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5375 case DT_MIPS_ICHECKSUM
:
5379 case DT_MIPS_IVERSION
:
5383 case DT_MIPS_BASE_ADDRESS
:
5384 s
= output_bfd
->sections
;
5385 BFD_ASSERT (s
!= NULL
);
5386 dyn
.d_un
.d_ptr
= s
->vma
& ~(0xffff);
5387 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5390 case DT_MIPS_LOCAL_GOTNO
:
5391 dyn
.d_un
.d_val
= g
->local_gotno
;
5392 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5395 case DT_MIPS_SYMTABNO
:
5397 elemsize
= sizeof (Elf32_External_Sym
);
5398 s
= bfd_get_section_by_name (output_bfd
, name
);
5399 BFD_ASSERT (s
!= NULL
);
5401 if (s
->_cooked_size
!= 0)
5402 dyn
.d_un
.d_val
= s
->_cooked_size
/ elemsize
;
5404 dyn
.d_un
.d_val
= s
->_raw_size
/ elemsize
;
5405 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5408 case DT_MIPS_UNREFEXTNO
:
5410 dyn
.d_un
.d_val
= SIZEOF_MIPS_DYNSYM_SECNAMES
;
5411 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5414 case DT_MIPS_GOTSYM
:
5415 dyn
.d_un
.d_val
= g
->global_gotsym
;
5416 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5419 case DT_MIPS_HIPAGENO
:
5420 dyn
.d_un
.d_val
= g
->local_gotno
- MIPS_RESERVED_GOTNO
;
5421 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5428 /* The first entry of the global offset table will be filled at
5429 runtime. The second entry will be used by some runtime loaders.
5430 This isn't the case of Irix rld. */
5431 if (sgot
->_raw_size
> 0)
5433 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5434 bfd_put_32 (output_bfd
, (bfd_vma
) 0x80000000, sgot
->contents
+ 4);
5437 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5444 Elf_Internal_Sym sym
;
5447 const char * const * namep
= mips_elf_dynsym_sec_names
;
5448 Elf32_compact_rel cpt
;
5450 /* Set up the section symbols for the output sections. SGI sets
5451 the STT_NOTYPE attribute for these symbols. Should we do so? */
5453 sdynsym
= bfd_get_section_by_name (dynobj
, ".dynsym");
5454 if (sdynsym
!= NULL
)
5456 if (SGI_COMPAT (output_bfd
))
5460 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_NOTYPE
);
5464 while ((name
= *namep
++) != NULL
)
5466 s
= bfd_get_section_by_name (output_bfd
, name
);
5469 sym
.st_value
= s
->vma
;
5470 dindx
= elf_section_data (s
)->dynindx
;
5471 last
= s
->vma
+ s
->_raw_size
;
5475 sym
.st_value
= last
;
5479 sym
.st_shndx
= (i
< MIPS_TEXT_DYNSYM_SECNO
5484 mips_elf_hash_table (info
)->dynsym_sec_strindex
[dindx
];
5486 bfd_elf32_swap_symbol_out (output_bfd
, &sym
,
5487 (((Elf32_External_Sym
*)
5492 /* Set the sh_info field of the output .dynsym section to
5493 the index of the first global symbol. */
5494 elf_section_data (sdynsym
->output_section
)->this_hdr
.sh_info
=
5495 SIZEOF_MIPS_DYNSYM_SECNAMES
;
5501 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5504 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5508 sym
.st_value
= s
->vma
;
5510 indx
= elf_section_data (s
)->this_idx
;
5511 BFD_ASSERT (indx
> 0);
5512 sym
.st_shndx
= indx
;
5514 bfd_elf32_swap_symbol_out (output_bfd
, &sym
,
5515 (((Elf32_External_Sym
*)
5517 + elf_section_data (s
)->dynindx
));
5520 /* Set the sh_info field of the output .dynsym section to
5521 the index of the first global symbol. */
5522 elf_section_data (sdynsym
->output_section
)->this_hdr
.sh_info
=
5523 bfd_count_sections (output_bfd
) + 1;
5527 if (SGI_COMPAT (output_bfd
))
5529 /* Write .compact_rel section out. */
5530 s
= bfd_get_section_by_name (dynobj
, ".compact_rel");
5534 cpt
.num
= s
->reloc_count
;
5536 cpt
.offset
= (s
->output_section
->filepos
5537 + sizeof (Elf32_External_compact_rel
));
5540 bfd_elf32_swap_compact_rel_out (output_bfd
, &cpt
,
5541 ((Elf32_External_compact_rel
*)
5544 /* Clean up a dummy stub function entry in .text. */
5545 s
= bfd_get_section_by_name (dynobj
, ".stub");
5548 file_ptr dummy_offset
;
5550 BFD_ASSERT (s
->_raw_size
>= MIPS_FUNCTION_STUB_SIZE
);
5551 dummy_offset
= s
->_raw_size
- MIPS_FUNCTION_STUB_SIZE
;
5552 memset (s
->contents
+ dummy_offset
, 0,
5553 MIPS_FUNCTION_STUB_SIZE
);
5558 /* Clean up a first relocation in .rel.dyn. */
5559 s
= bfd_get_section_by_name (dynobj
, ".rel.dyn");
5561 memset (s
->contents
, 0, sizeof (Elf32_External_Rel
));
5567 /* This is almost identical to bfd_generic_get_... except that some
5568 MIPS relocations need to be handled specially. Sigh. */
5571 elf32_mips_get_relocated_section_contents (abfd
, link_info
, link_order
, data
,
5572 relocateable
, symbols
)
5574 struct bfd_link_info
*link_info
;
5575 struct bfd_link_order
*link_order
;
5577 boolean relocateable
;
5580 /* Get enough memory to hold the stuff */
5581 bfd
*input_bfd
= link_order
->u
.indirect
.section
->owner
;
5582 asection
*input_section
= link_order
->u
.indirect
.section
;
5584 long reloc_size
= bfd_get_reloc_upper_bound (input_bfd
, input_section
);
5585 arelent
**reloc_vector
= NULL
;
5591 reloc_vector
= (arelent
**) bfd_malloc (reloc_size
);
5592 if (reloc_vector
== NULL
&& reloc_size
!= 0)
5595 /* read in the section */
5596 if (!bfd_get_section_contents (input_bfd
,
5600 input_section
->_raw_size
))
5603 /* We're not relaxing the section, so just copy the size info */
5604 input_section
->_cooked_size
= input_section
->_raw_size
;
5605 input_section
->reloc_done
= true;
5607 reloc_count
= bfd_canonicalize_reloc (input_bfd
,
5611 if (reloc_count
< 0)
5614 if (reloc_count
> 0)
5619 bfd_vma gp
= 0x12345678; /* initialize just to shut gcc up */
5622 struct bfd_hash_entry
*h
;
5623 struct bfd_link_hash_entry
*lh
;
5624 /* Skip all this stuff if we aren't mixing formats. */
5625 if (abfd
&& input_bfd
5626 && abfd
->xvec
== input_bfd
->xvec
)
5630 h
= bfd_hash_lookup (&link_info
->hash
->table
, "_gp", false, false);
5631 lh
= (struct bfd_link_hash_entry
*) h
;
5638 case bfd_link_hash_undefined
:
5639 case bfd_link_hash_undefweak
:
5640 case bfd_link_hash_common
:
5643 case bfd_link_hash_defined
:
5644 case bfd_link_hash_defweak
:
5646 gp
= lh
->u
.def
.value
;
5648 case bfd_link_hash_indirect
:
5649 case bfd_link_hash_warning
:
5651 /* @@FIXME ignoring warning for now */
5653 case bfd_link_hash_new
:
5662 for (parent
= reloc_vector
; *parent
!= (arelent
*) NULL
;
5665 char *error_message
= (char *) NULL
;
5666 bfd_reloc_status_type r
;
5668 /* Specific to MIPS: Deal with relocation types that require
5669 knowing the gp of the output bfd. */
5670 asymbol
*sym
= *(*parent
)->sym_ptr_ptr
;
5671 if (bfd_is_abs_section (sym
->section
) && abfd
)
5673 /* The special_function wouldn't get called anyways. */
5677 /* The gp isn't there; let the special function code
5678 fall over on its own. */
5680 else if ((*parent
)->howto
->special_function
5681 == mips_elf_gprel16_reloc
)
5683 /* bypass special_function call */
5684 r
= gprel16_with_gp (input_bfd
, sym
, *parent
, input_section
,
5685 relocateable
, (PTR
) data
, gp
);
5686 goto skip_bfd_perform_relocation
;
5688 /* end mips specific stuff */
5690 r
= bfd_perform_relocation (input_bfd
,
5694 relocateable
? abfd
: (bfd
*) NULL
,
5696 skip_bfd_perform_relocation
:
5700 asection
*os
= input_section
->output_section
;
5702 /* A partial link, so keep the relocs */
5703 os
->orelocation
[os
->reloc_count
] = *parent
;
5707 if (r
!= bfd_reloc_ok
)
5711 case bfd_reloc_undefined
:
5712 if (!((*link_info
->callbacks
->undefined_symbol
)
5713 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
5714 input_bfd
, input_section
, (*parent
)->address
)))
5717 case bfd_reloc_dangerous
:
5718 BFD_ASSERT (error_message
!= (char *) NULL
);
5719 if (!((*link_info
->callbacks
->reloc_dangerous
)
5720 (link_info
, error_message
, input_bfd
, input_section
,
5721 (*parent
)->address
)))
5724 case bfd_reloc_overflow
:
5725 if (!((*link_info
->callbacks
->reloc_overflow
)
5726 (link_info
, bfd_asymbol_name (*(*parent
)->sym_ptr_ptr
),
5727 (*parent
)->howto
->name
, (*parent
)->addend
,
5728 input_bfd
, input_section
, (*parent
)->address
)))
5731 case bfd_reloc_outofrange
:
5740 if (reloc_vector
!= NULL
)
5741 free (reloc_vector
);
5745 if (reloc_vector
!= NULL
)
5746 free (reloc_vector
);
5749 #define bfd_elf32_bfd_get_relocated_section_contents \
5750 elf32_mips_get_relocated_section_contents
5752 /* ECOFF swapping routines. These are used when dealing with the
5753 .mdebug section, which is in the ECOFF debugging format. */
5754 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap
=
5756 /* Symbol table magic number. */
5758 /* Alignment of debugging information. E.g., 4. */
5760 /* Sizes of external symbolic information. */
5761 sizeof (struct hdr_ext
),
5762 sizeof (struct dnr_ext
),
5763 sizeof (struct pdr_ext
),
5764 sizeof (struct sym_ext
),
5765 sizeof (struct opt_ext
),
5766 sizeof (struct fdr_ext
),
5767 sizeof (struct rfd_ext
),
5768 sizeof (struct ext_ext
),
5769 /* Functions to swap in external symbolic data. */
5778 _bfd_ecoff_swap_tir_in
,
5779 _bfd_ecoff_swap_rndx_in
,
5780 /* Functions to swap out external symbolic data. */
5789 _bfd_ecoff_swap_tir_out
,
5790 _bfd_ecoff_swap_rndx_out
,
5791 /* Function to read in symbolic data. */
5792 mips_elf_read_ecoff_info
5795 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
5796 #define TARGET_LITTLE_NAME "elf32-littlemips"
5797 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
5798 #define TARGET_BIG_NAME "elf32-bigmips"
5799 #define ELF_ARCH bfd_arch_mips
5800 #define ELF_MACHINE_CODE EM_MIPS
5801 #define ELF_MAXPAGESIZE 0x10000
5802 #define elf_backend_collect true
5803 #define elf_backend_type_change_ok true
5804 #define elf_info_to_howto 0
5805 #define elf_info_to_howto_rel mips_info_to_howto_rel
5806 #define elf_backend_sym_is_global mips_elf_sym_is_global
5807 #define elf_backend_object_p mips_elf_object_p
5808 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
5809 #define elf_backend_fake_sections mips_elf_fake_sections
5810 #define elf_backend_section_from_bfd_section \
5811 mips_elf_section_from_bfd_section
5812 #define elf_backend_section_processing mips_elf_section_processing
5813 #define elf_backend_symbol_processing mips_elf_symbol_processing
5814 #define elf_backend_additional_program_headers \
5815 mips_elf_additional_program_headers
5816 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
5817 #define elf_backend_final_write_processing \
5818 mips_elf_final_write_processing
5819 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
5821 #define bfd_elf32_bfd_is_local_label mips_elf_is_local_label
5822 #define bfd_elf32_find_nearest_line mips_elf_find_nearest_line
5824 #define bfd_elf32_bfd_link_hash_table_create \
5825 mips_elf_link_hash_table_create
5826 #define bfd_elf32_bfd_final_link mips_elf_final_link
5827 #define bfd_elf32_bfd_copy_private_bfd_data \
5828 mips_elf_copy_private_bfd_data
5829 #define bfd_elf32_bfd_merge_private_bfd_data \
5830 mips_elf_merge_private_bfd_data
5831 #define bfd_elf32_bfd_set_private_flags mips_elf_set_private_flags
5832 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
5833 #define elf_backend_create_dynamic_sections \
5834 mips_elf_create_dynamic_sections
5835 #define elf_backend_check_relocs mips_elf_check_relocs
5836 #define elf_backend_adjust_dynamic_symbol \
5837 mips_elf_adjust_dynamic_symbol
5838 #define elf_backend_size_dynamic_sections \
5839 mips_elf_size_dynamic_sections
5840 #define elf_backend_relocate_section mips_elf_relocate_section
5841 #define elf_backend_finish_dynamic_symbol \
5842 mips_elf_finish_dynamic_symbol
5843 #define elf_backend_finish_dynamic_sections \
5844 mips_elf_finish_dynamic_sections
5846 #include "elf32-target.h"