1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* Get the ECOFF swapping routines. */
33 #include "coff/symconst.h"
34 #include "coff/internal.h"
35 #include "coff/ecoff.h"
36 #include "coff/mips.h"
38 #include "ecoffswap.h"
40 static bfd_reloc_status_type mips_elf_hi16_reloc
PARAMS ((bfd
*abfd
,
47 static bfd_reloc_status_type mips_elf_got16_reloc
PARAMS ((bfd
*abfd
,
54 static bfd_reloc_status_type mips_elf_lo16_reloc
PARAMS ((bfd
*abfd
,
61 static bfd_reloc_status_type mips_elf_gprel16_reloc
PARAMS ((bfd
*abfd
,
68 static const struct reloc_howto_struct
*bfd_elf32_bfd_reloc_type_lookup
69 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
70 static void mips_info_to_howto_rel
71 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
72 static boolean mips_elf_sym_is_global
PARAMS ((bfd
*, asymbol
*));
73 static boolean mips_elf_object_p
PARAMS ((bfd
*));
74 static void mips_elf_final_write_processing
75 PARAMS ((bfd
*, boolean
));
76 static boolean mips_elf_section_from_shdr
77 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, char *));
78 static boolean mips_elf_fake_sections
79 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*));
80 static boolean mips_elf_section_from_bfd_section
81 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*, int *));
82 static boolean mips_elf_section_processing
83 PARAMS ((bfd
*, Elf32_Internal_Shdr
*));
84 static void mips_elf_symbol_processing
PARAMS ((bfd
*, asymbol
*));
85 static boolean mips_elf_read_ecoff_info
86 PARAMS ((bfd
*, asection
*, struct ecoff_debug_info
*));
87 static struct bfd_hash_entry
*mips_elf_link_hash_newfunc
88 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
89 static struct bfd_link_hash_table
*mips_elf_link_hash_table_create
91 static int gptab_compare
PARAMS ((const void *, const void *));
92 static boolean mips_elf_final_link
93 PARAMS ((bfd
*, struct bfd_link_info
*));
94 static void mips_elf_relocate_hi16
95 PARAMS ((bfd
*, Elf_Internal_Rela
*, Elf_Internal_Rela
*, bfd_byte
*,
97 static boolean mips_elf_relocate_section
98 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
99 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
100 static boolean mips_elf_add_symbol_hook
101 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
102 const char **, flagword
*, asection
**, bfd_vma
*));
104 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
109 R_MIPS_16
, R_MIPS_32
,
110 R_MIPS_REL32
, R_MIPS_26
,
111 R_MIPS_HI16
, R_MIPS_LO16
,
112 R_MIPS_GPREL16
, R_MIPS_LITERAL
,
113 R_MIPS_GOT16
, R_MIPS_PC16
,
114 R_MIPS_CALL16
, R_MIPS_GPREL32
,
115 /* The remaining relocs are defined on Irix, although they are not
116 in the MIPS ELF ABI. */
117 R_MIPS_UNUSED1
, R_MIPS_UNUSED2
,
119 R_MIPS_SHIFT5
, R_MIPS_SHIFT6
,
120 R_MIPS_64
, R_MIPS_GOT_DISP
,
121 R_MIPS_GOT_PAGE
, R_MIPS_GOT_OFST
,
122 R_MIPS_GOT_HI16
, R_MIPS_GOT_LO16
,
123 R_MIPS_SUB
, R_MIPS_INSERT_A
,
124 R_MIPS_INSERT_B
, R_MIPS_DELETE
,
125 R_MIPS_HIGHER
, R_MIPS_HIGHEST
,
126 R_MIPS_CALL_HI16
, R_MIPS_CALL_LO16
,
130 static reloc_howto_type elf_mips_howto_table
[] =
133 HOWTO (R_MIPS_NONE
, /* type */
135 0, /* size (0 = byte, 1 = short, 2 = long) */
137 false, /* pc_relative */
139 complain_overflow_dont
, /* complain_on_overflow */
140 bfd_elf_generic_reloc
, /* special_function */
141 "R_MIPS_NONE", /* name */
142 false, /* partial_inplace */
145 false), /* pcrel_offset */
147 /* 16 bit relocation. */
148 HOWTO (R_MIPS_16
, /* type */
150 1, /* size (0 = byte, 1 = short, 2 = long) */
152 false, /* pc_relative */
154 complain_overflow_bitfield
, /* complain_on_overflow */
155 bfd_elf_generic_reloc
, /* special_function */
156 "R_MIPS_16", /* name */
157 true, /* partial_inplace */
158 0xffff, /* src_mask */
159 0xffff, /* dst_mask */
160 false), /* pcrel_offset */
162 /* 32 bit relocation. */
163 HOWTO (R_MIPS_32
, /* type */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
167 false, /* pc_relative */
169 complain_overflow_bitfield
, /* complain_on_overflow */
170 bfd_elf_generic_reloc
, /* special_function */
171 "R_MIPS_32", /* name */
172 true, /* partial_inplace */
173 0xffffffff, /* src_mask */
174 0xffffffff, /* dst_mask */
175 false), /* pcrel_offset */
177 /* 32 bit symbol relative relocation. */
178 HOWTO (R_MIPS_REL32
, /* type */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
182 false, /* pc_relative */
184 complain_overflow_bitfield
, /* complain_on_overflow */
185 bfd_elf_generic_reloc
, /* special_function */
186 "R_MIPS_REL32", /* name */
187 true, /* partial_inplace */
188 0xffffffff, /* src_mask */
189 0xffffffff, /* dst_mask */
190 false), /* pcrel_offset */
192 /* 26 bit branch address. */
193 HOWTO (R_MIPS_26
, /* type */
195 2, /* size (0 = byte, 1 = short, 2 = long) */
197 false, /* pc_relative */
199 complain_overflow_dont
, /* complain_on_overflow */
200 /* This needs complex overflow
201 detection, because the upper four
202 bits must match the PC. */
203 bfd_elf_generic_reloc
, /* special_function */
204 "R_MIPS_26", /* name */
205 true, /* partial_inplace */
206 0x3ffffff, /* src_mask */
207 0x3ffffff, /* dst_mask */
208 false), /* pcrel_offset */
210 /* High 16 bits of symbol value. */
211 HOWTO (R_MIPS_HI16
, /* type */
213 2, /* size (0 = byte, 1 = short, 2 = long) */
215 false, /* pc_relative */
217 complain_overflow_dont
, /* complain_on_overflow */
218 mips_elf_hi16_reloc
, /* special_function */
219 "R_MIPS_HI16", /* name */
220 true, /* partial_inplace */
221 0xffff, /* src_mask */
222 0xffff, /* dst_mask */
223 false), /* pcrel_offset */
225 /* Low 16 bits of symbol value. */
226 HOWTO (R_MIPS_LO16
, /* type */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
230 false, /* pc_relative */
232 complain_overflow_dont
, /* complain_on_overflow */
233 mips_elf_lo16_reloc
, /* special_function */
234 "R_MIPS_LO16", /* name */
235 true, /* partial_inplace */
236 0xffff, /* src_mask */
237 0xffff, /* dst_mask */
238 false), /* pcrel_offset */
240 /* GP relative reference. */
241 HOWTO (R_MIPS_GPREL16
, /* type */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
245 false, /* pc_relative */
247 complain_overflow_signed
, /* complain_on_overflow */
248 mips_elf_gprel16_reloc
, /* special_function */
249 "R_MIPS_GPREL16", /* name */
250 true, /* partial_inplace */
251 0xffff, /* src_mask */
252 0xffff, /* dst_mask */
253 false), /* pcrel_offset */
255 /* Reference to literal section. */
256 HOWTO (R_MIPS_LITERAL
, /* type */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
260 false, /* pc_relative */
262 complain_overflow_signed
, /* complain_on_overflow */
263 mips_elf_gprel16_reloc
, /* special_function */
264 "R_MIPS_LITERAL", /* name */
265 true, /* partial_inplace */
266 0xffff, /* src_mask */
267 0xffff, /* dst_mask */
268 false), /* pcrel_offset */
270 /* Reference to global offset table. */
271 /* FIXME: This is not handled correctly. */
272 HOWTO (R_MIPS_GOT16
, /* type */
274 2, /* size (0 = byte, 1 = short, 2 = long) */
276 false, /* pc_relative */
278 complain_overflow_signed
, /* complain_on_overflow */
279 mips_elf_got16_reloc
, /* special_function */
280 "R_MIPS_GOT16", /* name */
281 false, /* partial_inplace */
283 0xffff, /* dst_mask */
284 false), /* pcrel_offset */
286 /* 16 bit PC relative reference. */
287 HOWTO (R_MIPS_PC16
, /* type */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
291 true, /* pc_relative */
293 complain_overflow_signed
, /* complain_on_overflow */
294 bfd_elf_generic_reloc
, /* special_function */
295 "R_MIPS_PC16", /* name */
296 true, /* partial_inplace */
297 0xffff, /* src_mask */
298 0xffff, /* dst_mask */
299 false), /* pcrel_offset */
301 /* 16 bit call through global offset table. */
302 /* FIXME: This is not handled correctly. */
303 HOWTO (R_MIPS_CALL16
, /* type */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
307 false, /* pc_relative */
309 complain_overflow_signed
, /* complain_on_overflow */
310 bfd_elf_generic_reloc
, /* special_function */
311 "R_MIPS_CALL16", /* name */
312 false, /* partial_inplace */
314 0xffff, /* dst_mask */
315 false), /* pcrel_offset */
317 /* 32 bit GP relative reference. */
318 /* FIXME: This is not handled correctly. */
319 HOWTO (R_MIPS_GPREL32
, /* type */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
323 false, /* pc_relative */
325 complain_overflow_bitfield
, /* complain_on_overflow */
326 bfd_elf_generic_reloc
, /* special_function */
327 "R_MIPS_GPREL32", /* name */
328 true, /* partial_inplace */
329 0xffffffff, /* src_mask */
330 0xffffffff, /* dst_mask */
331 false), /* pcrel_offset */
333 /* The remaining relocs are defined on Irix 5, although they are
334 not defined by the ABI. */
339 /* A 5 bit shift field. */
340 HOWTO (R_MIPS_SHIFT5
, /* type */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
344 false, /* pc_relative */
346 complain_overflow_bitfield
, /* complain_on_overflow */
347 bfd_elf_generic_reloc
, /* special_function */
348 "R_MIPS_SHIFT5", /* name */
349 true, /* partial_inplace */
350 0x000007c0, /* src_mask */
351 0x000007c0, /* dst_mask */
352 false), /* pcrel_offset */
354 /* A 6 bit shift field. */
355 /* FIXME: This is not handled correctly; a special function is
356 needed to put the most significant bit in the right place. */
357 HOWTO (R_MIPS_SHIFT6
, /* type */
359 2, /* size (0 = byte, 1 = short, 2 = long) */
361 false, /* pc_relative */
363 complain_overflow_bitfield
, /* complain_on_overflow */
364 bfd_elf_generic_reloc
, /* special_function */
365 "R_MIPS_SHIFT6", /* name */
366 true, /* partial_inplace */
367 0x000007c4, /* src_mask */
368 0x000007c4, /* dst_mask */
369 false), /* pcrel_offset */
371 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
374 /* Displacement in the global offset table. */
375 /* FIXME: Not handled correctly. */
376 HOWTO (R_MIPS_GOT_DISP
, /* type */
378 2, /* size (0 = byte, 1 = short, 2 = long) */
380 false, /* pc_relative */
382 complain_overflow_bitfield
, /* complain_on_overflow */
383 bfd_elf_generic_reloc
, /* special_function */
384 "R_MIPS_GOT_DISP", /* name */
385 true, /* partial_inplace */
386 0x0000ffff, /* src_mask */
387 0x0000ffff, /* dst_mask */
388 false), /* pcrel_offset */
390 /* Displacement to page pointer in the global offset table. */
391 /* FIXME: Not handled correctly. */
392 HOWTO (R_MIPS_GOT_PAGE
, /* type */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
396 false, /* pc_relative */
398 complain_overflow_bitfield
, /* complain_on_overflow */
399 bfd_elf_generic_reloc
, /* special_function */
400 "R_MIPS_GOT_PAGE", /* name */
401 true, /* partial_inplace */
402 0x0000ffff, /* src_mask */
403 0x0000ffff, /* dst_mask */
404 false), /* pcrel_offset */
406 /* Offset from page pointer in the global offset table. */
407 /* FIXME: Not handled correctly. */
408 HOWTO (R_MIPS_GOT_OFST
, /* type */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
412 false, /* pc_relative */
414 complain_overflow_bitfield
, /* complain_on_overflow */
415 bfd_elf_generic_reloc
, /* special_function */
416 "R_MIPS_GOT_OFST", /* name */
417 true, /* partial_inplace */
418 0x0000ffff, /* src_mask */
419 0x0000ffff, /* dst_mask */
420 false), /* pcrel_offset */
422 /* High 16 bits of displacement in global offset table. */
423 /* FIXME: Not handled correctly. */
424 HOWTO (R_MIPS_GOT_HI16
, /* type */
426 2, /* size (0 = byte, 1 = short, 2 = long) */
428 false, /* pc_relative */
430 complain_overflow_dont
, /* complain_on_overflow */
431 bfd_elf_generic_reloc
, /* special_function */
432 "R_MIPS_GOT_HI16", /* name */
433 true, /* partial_inplace */
434 0x0000ffff, /* src_mask */
435 0x0000ffff, /* dst_mask */
436 false), /* pcrel_offset */
438 /* Low 16 bits of displacement in global offset table. */
439 /* FIXME: Not handled correctly. */
440 HOWTO (R_MIPS_GOT_LO16
, /* type */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
444 false, /* pc_relative */
446 complain_overflow_dont
, /* complain_on_overflow */
447 bfd_elf_generic_reloc
, /* special_function */
448 "R_MIPS_GOT_LO16", /* name */
449 true, /* partial_inplace */
450 0x0000ffff, /* src_mask */
451 0x0000ffff, /* dst_mask */
452 false), /* pcrel_offset */
454 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
457 /* Used to cause the linker to insert and delete instructions? */
462 /* Get the higher values of a 64 bit addend. Presumably not used in
467 /* High 16 bits of displacement in global offset table. */
468 /* FIXME: Not handled correctly. */
469 HOWTO (R_MIPS_CALL_HI16
, /* type */
471 2, /* size (0 = byte, 1 = short, 2 = long) */
473 false, /* pc_relative */
475 complain_overflow_dont
, /* complain_on_overflow */
476 bfd_elf_generic_reloc
, /* special_function */
477 "R_MIPS_CALL_HI16", /* name */
478 true, /* partial_inplace */
479 0x0000ffff, /* src_mask */
480 0x0000ffff, /* dst_mask */
481 false), /* pcrel_offset */
483 /* Low 16 bits of displacement in global offset table. */
484 /* FIXME: Not handled correctly. */
485 HOWTO (R_MIPS_CALL_LO16
, /* type */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
489 false, /* pc_relative */
491 complain_overflow_dont
, /* complain_on_overflow */
492 bfd_elf_generic_reloc
, /* special_function */
493 "R_MIPS_CALL_LO16", /* name */
494 true, /* partial_inplace */
495 0x0000ffff, /* src_mask */
496 0x0000ffff, /* dst_mask */
497 false) /* pcrel_offset */
500 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
501 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
502 the HI16. Here we just save the information we need; we do the
503 actual relocation when we see the LO16. MIPS ELF requires that the
504 LO16 immediately follow the HI16, so this ought to work. */
506 static bfd_byte
*mips_hi16_addr
;
507 static bfd_vma mips_hi16_addend
;
509 static bfd_reloc_status_type
510 mips_elf_hi16_reloc (abfd
,
518 arelent
*reloc_entry
;
521 asection
*input_section
;
523 char **error_message
;
525 bfd_reloc_status_type ret
;
528 /* If we're relocating, and this an external symbol, we don't want
529 to change anything. */
530 if (output_bfd
!= (bfd
*) NULL
531 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
532 && reloc_entry
->addend
== 0)
534 reloc_entry
->address
+= input_section
->output_offset
;
538 /* FIXME: The symbol _gp_disp requires special handling, which we do
540 if (strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
544 if (bfd_is_und_section (symbol
->section
)
545 && output_bfd
== (bfd
*) NULL
)
546 ret
= bfd_reloc_undefined
;
548 if (bfd_is_com_section (symbol
->section
))
551 relocation
= symbol
->value
;
553 relocation
+= symbol
->section
->output_section
->vma
;
554 relocation
+= symbol
->section
->output_offset
;
555 relocation
+= reloc_entry
->addend
;
557 if (reloc_entry
->address
> input_section
->_cooked_size
)
558 return bfd_reloc_outofrange
;
560 /* Save the information, and let LO16 do the actual relocation. */
561 mips_hi16_addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
562 mips_hi16_addend
= relocation
;
564 if (output_bfd
!= (bfd
*) NULL
)
565 reloc_entry
->address
+= input_section
->output_offset
;
570 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
571 inplace relocation; this function exists in order to do the
572 R_MIPS_HI16 relocation described above. */
574 static bfd_reloc_status_type
575 mips_elf_lo16_reloc (abfd
,
583 arelent
*reloc_entry
;
586 asection
*input_section
;
588 char **error_message
;
590 /* FIXME: The symbol _gp_disp requires special handling, which we do
592 if (output_bfd
== (bfd
*) NULL
593 && strcmp (bfd_asymbol_name (symbol
), "_gp_disp") == 0)
596 if (mips_hi16_addr
!= (bfd_byte
*) NULL
)
602 /* Do the HI16 relocation. Note that we actually don't need to
603 know anything about the LO16 itself, except where to find the
604 low 16 bits of the addend needed by the LO16. */
605 insn
= bfd_get_32 (abfd
, mips_hi16_addr
);
606 vallo
= (bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
608 val
= ((insn
& 0xffff) << 16) + vallo
;
609 val
+= mips_hi16_addend
;
611 /* The low order 16 bits are always treated as a signed value.
612 Therefore, a negative value in the low order bits requires an
613 adjustment in the high order bits. We need to make this
614 adjustment in two ways: once for the bits we took from the
615 data, and once for the bits we are putting back in to the
617 if ((vallo
& 0x8000) != 0)
619 if ((val
& 0x8000) != 0)
622 insn
= (insn
&~ 0xffff) | ((val
>> 16) & 0xffff);
623 bfd_put_32 (abfd
, insn
, mips_hi16_addr
);
625 mips_hi16_addr
= (bfd_byte
*) NULL
;
628 /* Now do the LO16 reloc in the usual way. */
629 return bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
630 input_section
, output_bfd
, error_message
);
633 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
634 table used for PIC code. If the symbol is an external symbol, the
635 instruction is modified to contain the offset of the appropriate
636 entry in the global offset table. If the symbol is a section
637 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
638 addends are combined to form the real addend against the section
639 symbol; the GOT16 is modified to contain the offset of an entry in
640 the global offset table, and the LO16 is modified to offset it
641 appropriately. Thus an offset larger than 16 bits requires a
642 modified value in the global offset table.
644 This implementation suffices for the assembler, but the linker does
645 not yet know how to create global offset tables. */
647 static bfd_reloc_status_type
648 mips_elf_got16_reloc (abfd
,
656 arelent
*reloc_entry
;
659 asection
*input_section
;
661 char **error_message
;
663 /* If we're relocating, and this an external symbol, we don't want
664 to change anything. */
665 if (output_bfd
!= (bfd
*) NULL
666 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
667 && reloc_entry
->addend
== 0)
669 reloc_entry
->address
+= input_section
->output_offset
;
673 /* If we're relocating, and this is a local symbol, we can handle it
675 if (output_bfd
!= (bfd
*) NULL
676 && (symbol
->flags
& BSF_SECTION_SYM
) != 0)
677 return mips_elf_hi16_reloc (abfd
, reloc_entry
, symbol
, data
,
678 input_section
, output_bfd
, error_message
);
683 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
684 become the offset from the gp register. This function also handles
685 R_MIPS_LITERAL relocations, although those can be handled more
686 cleverly because the entries in the .lit8 and .lit4 sections can be
689 static bfd_reloc_status_type
690 mips_elf_gprel16_reloc (abfd
,
698 arelent
*reloc_entry
;
701 asection
*input_section
;
703 char **error_message
;
705 boolean relocateable
;
710 /* If we're relocating, and this is an external symbol with no
711 addend, we don't want to change anything. We will only have an
712 addend if this is a newly created reloc, not read from an ELF
714 if (output_bfd
!= (bfd
*) NULL
715 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
716 && reloc_entry
->addend
== 0)
718 reloc_entry
->address
+= input_section
->output_offset
;
722 if (output_bfd
!= (bfd
*) NULL
)
726 relocateable
= false;
727 output_bfd
= symbol
->section
->output_section
->owner
;
730 if (bfd_is_und_section (symbol
->section
)
731 && relocateable
== false)
732 return bfd_reloc_undefined
;
734 /* We have to figure out the gp value, so that we can adjust the
735 symbol value correctly. We look up the symbol _gp in the output
736 BFD. If we can't find it, we're stuck. We cache it in the ELF
737 target data. We don't need to adjust the symbol value for an
738 external symbol if we are producing relocateable output. */
739 if (elf_gp (output_bfd
) == 0
740 && (relocateable
== false
741 || (symbol
->flags
& BSF_SECTION_SYM
) != 0))
743 if (relocateable
!= false)
745 /* Make up a value. */
746 elf_gp (output_bfd
) =
747 symbol
->section
->output_section
->vma
+ 0x4000;
755 count
= bfd_get_symcount (output_bfd
);
756 sym
= bfd_get_outsymbols (output_bfd
);
758 if (sym
== (asymbol
**) NULL
)
762 for (i
= 0; i
< count
; i
++, sym
++)
764 register CONST
char *name
;
766 name
= bfd_asymbol_name (*sym
);
767 if (*name
== '_' && strcmp (name
, "_gp") == 0)
769 elf_gp (output_bfd
) = bfd_asymbol_value (*sym
);
777 /* Only get the error once. */
778 elf_gp (output_bfd
) = 4;
780 (char *) "GP relative relocation when _gp not defined";
781 return bfd_reloc_dangerous
;
786 if (bfd_is_com_section (symbol
->section
))
789 relocation
= symbol
->value
;
791 relocation
+= symbol
->section
->output_section
->vma
;
792 relocation
+= symbol
->section
->output_offset
;
794 if (reloc_entry
->address
> input_section
->_cooked_size
)
795 return bfd_reloc_outofrange
;
797 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
799 /* Set val to the offset into the section or symbol. */
800 val
= ((insn
& 0xffff) + reloc_entry
->addend
) & 0xffff;
804 /* Adjust val for the final section location and GP value. If we
805 are producing relocateable output, we don't want to do this for
806 an external symbol. */
807 if (relocateable
== false
808 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
809 val
+= relocation
- elf_gp (output_bfd
);
811 insn
= (insn
&~ 0xffff) | (val
& 0xffff);
812 bfd_put_32 (abfd
, insn
, (bfd_byte
*) data
+ reloc_entry
->address
);
814 if (relocateable
!= false)
815 reloc_entry
->address
+= input_section
->output_offset
;
817 /* Make sure it fit in 16 bits. */
818 if (val
>= 0x8000 && val
< 0xffff8000)
819 return bfd_reloc_overflow
;
824 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
826 struct elf_reloc_map
{
827 bfd_reloc_code_real_type bfd_reloc_val
;
828 enum reloc_type elf_reloc_val
;
831 static CONST
struct elf_reloc_map mips_reloc_map
[] =
833 { BFD_RELOC_NONE
, R_MIPS_NONE
, },
834 { BFD_RELOC_16
, R_MIPS_16
},
835 { BFD_RELOC_32
, R_MIPS_32
},
836 { BFD_RELOC_CTOR
, R_MIPS_32
},
837 { BFD_RELOC_32_PCREL
, R_MIPS_REL32
},
838 { BFD_RELOC_MIPS_JMP
, R_MIPS_26
},
839 { BFD_RELOC_HI16_S
, R_MIPS_HI16
},
840 { BFD_RELOC_LO16
, R_MIPS_LO16
},
841 { BFD_RELOC_MIPS_GPREL
, R_MIPS_GPREL16
},
842 { BFD_RELOC_MIPS_LITERAL
, R_MIPS_LITERAL
},
843 { BFD_RELOC_MIPS_GOT16
, R_MIPS_GOT16
},
844 { BFD_RELOC_16_PCREL
, R_MIPS_PC16
},
845 { BFD_RELOC_MIPS_CALL16
, R_MIPS_CALL16
},
846 { BFD_RELOC_MIPS_GPREL32
, R_MIPS_GPREL32
}
849 /* Given a BFD reloc type, return a howto structure. */
851 static const struct reloc_howto_struct
*
852 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
854 bfd_reloc_code_real_type code
;
858 for (i
= 0; i
< sizeof (mips_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
860 if (mips_reloc_map
[i
].bfd_reloc_val
== code
)
861 return &elf_mips_howto_table
[(int) mips_reloc_map
[i
].elf_reloc_val
];
866 /* Given a MIPS reloc type, fill in an arelent structure. */
869 mips_info_to_howto_rel (abfd
, cache_ptr
, dst
)
872 Elf32_Internal_Rel
*dst
;
876 r_type
= ELF32_R_TYPE (dst
->r_info
);
877 BFD_ASSERT (r_type
< (unsigned int) R_MIPS_max
);
878 cache_ptr
->howto
= &elf_mips_howto_table
[r_type
];
880 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
881 value for the object file. We get the addend now, rather than
882 when we do the relocation, because the symbol manipulations done
883 by the linker may cause us to lose track of the input BFD. */
884 if (((*cache_ptr
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) != 0
885 && (r_type
== (unsigned int) R_MIPS_GPREL16
886 || r_type
== (unsigned int) R_MIPS_LITERAL
))
887 cache_ptr
->addend
= elf_gp (abfd
);
890 /* A .reginfo section holds a single Elf32_RegInfo structure. These
891 routines swap this structure in and out. They are used outside of
892 BFD, so they are globally visible. */
895 bfd_mips_elf32_swap_reginfo_in (abfd
, ex
, in
)
897 const Elf32_External_RegInfo
*ex
;
900 in
->ri_gprmask
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gprmask
);
901 in
->ri_cprmask
[0] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[0]);
902 in
->ri_cprmask
[1] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[1]);
903 in
->ri_cprmask
[2] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[2]);
904 in
->ri_cprmask
[3] = bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_cprmask
[3]);
905 in
->ri_gp_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) ex
->ri_gp_value
);
909 bfd_mips_elf32_swap_reginfo_out (abfd
, in
, ex
)
911 const Elf32_RegInfo
*in
;
912 Elf32_External_RegInfo
*ex
;
914 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gprmask
,
915 (bfd_byte
*) ex
->ri_gprmask
);
916 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[0],
917 (bfd_byte
*) ex
->ri_cprmask
[0]);
918 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[1],
919 (bfd_byte
*) ex
->ri_cprmask
[1]);
920 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[2],
921 (bfd_byte
*) ex
->ri_cprmask
[2]);
922 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_cprmask
[3],
923 (bfd_byte
*) ex
->ri_cprmask
[3]);
924 bfd_h_put_32 (abfd
, (bfd_vma
) in
->ri_gp_value
,
925 (bfd_byte
*) ex
->ri_gp_value
);
928 /* Swap an entry in a .gptab section. Note that these routines rely
929 on the equivalence of the two elements of the union. */
932 bfd_mips_elf32_swap_gptab_in (abfd
, ex
, in
)
934 const Elf32_External_gptab
*ex
;
937 in
->gt_entry
.gt_g_value
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_g_value
);
938 in
->gt_entry
.gt_bytes
= bfd_h_get_32 (abfd
, ex
->gt_entry
.gt_bytes
);
942 bfd_mips_elf32_swap_gptab_out (abfd
, in
, ex
)
944 const Elf32_gptab
*in
;
945 Elf32_External_gptab
*ex
;
947 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_g_value
,
948 ex
->gt_entry
.gt_g_value
);
949 bfd_h_put_32 (abfd
, (bfd_vma
) in
->gt_entry
.gt_bytes
,
950 ex
->gt_entry
.gt_bytes
);
953 /* Determine whether a symbol is global for the purposes of splitting
954 the symbol table into global symbols and local symbols. At least
955 on Irix 5, this split must be between section symbols and all other
956 symbols. On most ELF targets the split is between static symbols
957 and externally visible symbols. */
961 mips_elf_sym_is_global (abfd
, sym
)
965 return (sym
->flags
& BSF_SECTION_SYM
) == 0 ? true : false;
968 /* Set the right machine number for a MIPS ELF file. */
971 mips_elf_object_p (abfd
)
974 switch (elf_elfheader (abfd
)->e_flags
& EF_MIPS_ARCH
)
978 /* Just use the default, which was set in elfcode.h. */
982 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 6000);
986 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 4000);
990 /* Irix 5 is broken. Object file symbol tables are not always
991 sorted correctly such that local symbols precede global symbols,
992 and the sh_info field in the symbol table is not always right.
993 We try to quickly check whether the symbol table is broken for
994 this BFD, and, if it is, we set elf_bad_symtab in tdata. */
995 if (elf_onesymtab (abfd
) != 0)
997 Elf_Internal_Shdr
*symtab_hdr
;
998 Elf32_External_Sym esym
;
1000 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1002 (symtab_hdr
->sh_offset
1003 + symtab_hdr
->sh_size
1004 - sizeof (Elf32_External_Sym
)),
1006 || (bfd_read ((PTR
) &esym
, 1, sizeof (Elf32_External_Sym
), abfd
)
1007 != sizeof (Elf32_External_Sym
)))
1009 if (ELF_ST_BIND (bfd_h_get_8 (abfd
, (bfd_byte
*) esym
.st_info
))
1011 elf_bad_symtab (abfd
) = true;
1017 /* The final processing done just before writing out a MIPS ELF object
1018 file. This gets the MIPS architecture right based on the machine
1023 mips_elf_final_write_processing (abfd
, linker
)
1029 Elf_Internal_Shdr
**hdrpp
;
1031 switch (bfd_get_mach (abfd
))
1034 val
= E_MIPS_ARCH_1
;
1038 val
= E_MIPS_ARCH_2
;
1042 val
= E_MIPS_ARCH_3
;
1049 elf_elfheader (abfd
)->e_flags
&=~ EF_MIPS_ARCH
;
1050 elf_elfheader (abfd
)->e_flags
|= val
;
1052 /* Set the sh_info field for .gptab sections. */
1053 for (i
= 1, hdrpp
= elf_elfsections (abfd
) + 1;
1054 i
< elf_elfheader (abfd
)->e_shnum
;
1057 if ((*hdrpp
)->sh_type
== SHT_MIPS_GPTAB
)
1062 BFD_ASSERT ((*hdrpp
)->bfd_section
!= NULL
);
1063 name
= bfd_get_section_name (abfd
, (*hdrpp
)->bfd_section
);
1064 BFD_ASSERT (name
!= NULL
1065 && strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0);
1066 sec
= bfd_get_section_by_name (abfd
, name
+ sizeof ".gptab" - 1);
1067 BFD_ASSERT (sec
!= NULL
);
1068 (*hdrpp
)->sh_info
= elf_section_data (sec
)->this_idx
;
1073 /* Handle a MIPS specific section when reading an object file. This
1074 is called when elfcode.h finds a section with an unknown type.
1075 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1079 mips_elf_section_from_shdr (abfd
, hdr
, name
)
1081 Elf32_Internal_Shdr
*hdr
;
1086 /* There ought to be a place to keep ELF backend specific flags, but
1087 at the moment there isn't one. We just keep track of the
1088 sections by their name, instead. Fortunately, the ABI gives
1089 suggested names for all the MIPS specific sections, so we will
1090 probably get away with this. */
1091 switch (hdr
->sh_type
)
1093 case SHT_MIPS_LIBLIST
:
1094 if (strcmp (name
, ".liblist") != 0)
1098 if (strcmp (name
, ".msym") != 0)
1101 case SHT_MIPS_CONFLICT
:
1102 if (strcmp (name
, ".conflict") != 0)
1105 case SHT_MIPS_GPTAB
:
1106 if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) != 0)
1109 case SHT_MIPS_UCODE
:
1110 if (strcmp (name
, ".ucode") != 0)
1113 case SHT_MIPS_DEBUG
:
1114 if (strcmp (name
, ".mdebug") != 0)
1117 case SHT_MIPS_REGINFO
:
1118 if (strcmp (name
, ".reginfo") != 0
1119 || hdr
->sh_size
!= sizeof (Elf32_External_RegInfo
))
1122 case SHT_MIPS_OPTIONS
:
1123 if (strcmp (name
, ".options") != 0)
1126 case SHT_MIPS_DWARF
:
1127 if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) != 0)
1130 case SHT_MIPS_EVENTS
:
1131 if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1138 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1140 newsect
= hdr
->bfd_section
;
1142 if (hdr
->sh_type
== SHT_MIPS_DEBUG
)
1144 if (! bfd_set_section_flags (abfd
, newsect
,
1145 (bfd_get_section_flags (abfd
, newsect
)
1150 /* FIXME: We should record sh_info for a .gptab section. */
1152 /* For a .reginfo section, set the gp value in the tdata information
1153 from the contents of this section. We need the gp value while
1154 processing relocs, so we just get it now. */
1155 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1157 Elf32_External_RegInfo ext
;
1160 if (! bfd_get_section_contents (abfd
, newsect
, (PTR
) &ext
,
1161 (file_ptr
) 0, sizeof ext
))
1163 bfd_mips_elf32_swap_reginfo_in (abfd
, &ext
, &s
);
1164 elf_gp (abfd
) = s
.ri_gp_value
;
1170 /* Set the correct type for a MIPS ELF section. We do this by the
1171 section name, which is a hack, but ought to work. */
1174 mips_elf_fake_sections (abfd
, hdr
, sec
)
1176 Elf32_Internal_Shdr
*hdr
;
1179 register const char *name
;
1181 name
= bfd_get_section_name (abfd
, sec
);
1183 if (strcmp (name
, ".liblist") == 0)
1185 hdr
->sh_type
= SHT_MIPS_LIBLIST
;
1186 hdr
->sh_info
= sec
->_raw_size
/ sizeof (Elf32_Lib
);
1187 /* FIXME: Set the sh_link field. */
1189 else if (strcmp (name
, ".msym") == 0)
1191 hdr
->sh_type
= SHT_MIPS_MSYM
;
1192 hdr
->sh_entsize
= 8;
1193 /* FIXME: Set the sh_info field. */
1195 else if (strcmp (name
, ".conflict") == 0)
1196 hdr
->sh_type
= SHT_MIPS_CONFLICT
;
1197 else if (strncmp (name
, ".gptab.", sizeof ".gptab." - 1) == 0)
1199 hdr
->sh_type
= SHT_MIPS_GPTAB
;
1200 hdr
->sh_entsize
= sizeof (Elf32_External_gptab
);
1201 /* The sh_info field is set in mips_elf_final_write_processing. */
1203 else if (strcmp (name
, ".ucode") == 0)
1204 hdr
->sh_type
= SHT_MIPS_UCODE
;
1205 else if (strcmp (name
, ".mdebug") == 0)
1207 hdr
->sh_type
= SHT_MIPS_DEBUG
;
1208 hdr
->sh_entsize
= 1;
1210 else if (strcmp (name
, ".reginfo") == 0)
1212 hdr
->sh_type
= SHT_MIPS_REGINFO
;
1213 hdr
->sh_entsize
= 1;
1215 /* Force the section size to the correct value, even if the
1216 linker thinks it is larger. The link routine below will only
1217 write out this much data for .reginfo. */
1218 hdr
->sh_size
= sec
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1220 else if (strcmp (name
, ".options") == 0)
1222 hdr
->sh_type
= SHT_MIPS_OPTIONS
;
1223 hdr
->sh_entsize
= 1;
1225 else if (strncmp (name
, ".debug_", sizeof ".debug_" - 1) == 0)
1226 hdr
->sh_type
= SHT_MIPS_DWARF
;
1227 else if (strncmp (name
, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1228 hdr
->sh_type
= SHT_MIPS_EVENTS
;
1233 /* Given a BFD section, try to locate the corresponding ELF section
1237 mips_elf_section_from_bfd_section (abfd
, hdr
, sec
, retval
)
1239 Elf32_Internal_Shdr
*hdr
;
1243 if (strcmp (bfd_get_section_name (abfd
, sec
), ".scommon") == 0)
1245 *retval
= SHN_MIPS_SCOMMON
;
1248 if (strcmp (bfd_get_section_name (abfd
, sec
), ".acommon") == 0)
1250 *retval
= SHN_MIPS_ACOMMON
;
1256 /* Work over a section just before writing it out. We update the GP
1257 value in the .reginfo section based on the value we are using.
1258 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1259 name; there has to be a better way. */
1262 mips_elf_section_processing (abfd
, hdr
)
1264 Elf32_Internal_Shdr
*hdr
;
1266 if (hdr
->sh_type
== SHT_MIPS_REGINFO
)
1270 BFD_ASSERT (hdr
->sh_size
== sizeof (Elf32_External_RegInfo
));
1271 BFD_ASSERT (hdr
->contents
== NULL
);
1274 hdr
->sh_offset
+ sizeof (Elf32_External_RegInfo
) - 4,
1277 bfd_h_put_32 (abfd
, (bfd_vma
) elf_gp (abfd
), buf
);
1278 if (bfd_write (buf
, (bfd_size_type
) 1, (bfd_size_type
) 4, abfd
) != 4)
1282 if (hdr
->bfd_section
!= NULL
)
1284 const char *name
= bfd_get_section_name (abfd
, hdr
->bfd_section
);
1286 if (strcmp (name
, ".sdata") == 0)
1288 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1289 hdr
->sh_type
= SHT_PROGBITS
;
1291 else if (strcmp (name
, ".sbss") == 0)
1293 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1294 hdr
->sh_type
= SHT_NOBITS
;
1296 else if (strcmp (name
, ".lit8") == 0
1297 || strcmp (name
, ".lit4") == 0)
1299 hdr
->sh_flags
|= SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
;
1300 hdr
->sh_type
= SHT_PROGBITS
;
1307 /* MIPS ELF uses two common sections. One is the usual one, and the
1308 other is for small objects. All the small objects are kept
1309 together, and then referenced via the gp pointer, which yields
1310 faster assembler code. This is what we use for the small common
1311 section. This approach is copied from ecoff.c. */
1312 static asection mips_elf_scom_section
;
1313 static asymbol mips_elf_scom_symbol
;
1314 static asymbol
*mips_elf_scom_symbol_ptr
;
1316 /* MIPS ELF also uses an acommon section, which represents an
1317 allocated common symbol which may be overridden by a
1318 definition in a shared library. */
1319 static asection mips_elf_acom_section
;
1320 static asymbol mips_elf_acom_symbol
;
1321 static asymbol
*mips_elf_acom_symbol_ptr
;
1323 /* Handle the special MIPS section numbers that a symbol may use. */
1326 mips_elf_symbol_processing (abfd
, asym
)
1330 elf_symbol_type
*elfsym
;
1332 elfsym
= (elf_symbol_type
*) asym
;
1333 switch (elfsym
->internal_elf_sym
.st_shndx
)
1335 case SHN_MIPS_ACOMMON
:
1336 /* This section is used in a dynamically linked executable file.
1337 It is an allocated common section. The dynamic linker can
1338 either resolve these symbols to something in a shared
1339 library, or it can just leave them here. For our purposes,
1340 we can consider these symbols to be in a new section. */
1341 if (mips_elf_acom_section
.name
== NULL
)
1343 /* Initialize the acommon section. */
1344 mips_elf_acom_section
.name
= ".acommon";
1345 mips_elf_acom_section
.flags
= SEC_NO_FLAGS
;
1346 mips_elf_acom_section
.output_section
= &mips_elf_acom_section
;
1347 mips_elf_acom_section
.symbol
= &mips_elf_acom_symbol
;
1348 mips_elf_acom_section
.symbol_ptr_ptr
= &mips_elf_acom_symbol_ptr
;
1349 mips_elf_acom_symbol
.name
= ".acommon";
1350 mips_elf_acom_symbol
.flags
= BSF_SECTION_SYM
;
1351 mips_elf_acom_symbol
.section
= &mips_elf_acom_section
;
1352 mips_elf_acom_symbol_ptr
= &mips_elf_acom_symbol
;
1354 asym
->section
= &mips_elf_acom_section
;
1358 /* Common symbols less than the GP size are automatically
1359 treated as SHN_MIPS_SCOMMON symbols. */
1360 if (asym
->value
> elf_gp_size (abfd
))
1363 case SHN_MIPS_SCOMMON
:
1364 if (mips_elf_scom_section
.name
== NULL
)
1366 /* Initialize the small common section. */
1367 mips_elf_scom_section
.name
= ".scommon";
1368 mips_elf_scom_section
.flags
= SEC_IS_COMMON
;
1369 mips_elf_scom_section
.output_section
= &mips_elf_scom_section
;
1370 mips_elf_scom_section
.symbol
= &mips_elf_scom_symbol
;
1371 mips_elf_scom_section
.symbol_ptr_ptr
= &mips_elf_scom_symbol_ptr
;
1372 mips_elf_scom_symbol
.name
= ".scommon";
1373 mips_elf_scom_symbol
.flags
= BSF_SECTION_SYM
;
1374 mips_elf_scom_symbol
.section
= &mips_elf_scom_section
;
1375 mips_elf_scom_symbol_ptr
= &mips_elf_scom_symbol
;
1377 asym
->section
= &mips_elf_scom_section
;
1378 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
1381 case SHN_MIPS_SUNDEFINED
:
1382 asym
->section
= bfd_und_section_ptr
;
1387 /* Read ECOFF debugging information from a .mdebug section into a
1388 ecoff_debug_info structure. */
1391 mips_elf_read_ecoff_info (abfd
, section
, debug
)
1394 struct ecoff_debug_info
*debug
;
1397 const struct ecoff_debug_swap
*swap
;
1398 char *ext_hdr
= NULL
;
1400 swap
= get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
1402 ext_hdr
= (char *) malloc (swap
->external_hdr_size
);
1403 if (ext_hdr
== NULL
&& swap
->external_hdr_size
!= 0)
1405 bfd_set_error (bfd_error_no_memory
);
1409 if (bfd_get_section_contents (abfd
, section
, ext_hdr
, (file_ptr
) 0,
1410 swap
->external_hdr_size
)
1414 symhdr
= &debug
->symbolic_header
;
1415 (*swap
->swap_hdr_in
) (abfd
, ext_hdr
, symhdr
);
1417 /* The symbolic header contains absolute file offsets and sizes to
1419 #define READ(ptr, offset, count, size, type) \
1420 if (symhdr->count == 0) \
1421 debug->ptr = NULL; \
1424 debug->ptr = (type) malloc (size * symhdr->count); \
1425 if (debug->ptr == NULL) \
1427 bfd_set_error (bfd_error_no_memory); \
1428 goto error_return; \
1430 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1431 || (bfd_read (debug->ptr, size, symhdr->count, \
1432 abfd) != size * symhdr->count)) \
1433 goto error_return; \
1436 READ (line
, cbLineOffset
, cbLine
, sizeof (unsigned char), unsigned char *);
1437 READ (external_dnr
, cbDnOffset
, idnMax
, swap
->external_dnr_size
, PTR
);
1438 READ (external_pdr
, cbPdOffset
, ipdMax
, swap
->external_pdr_size
, PTR
);
1439 READ (external_sym
, cbSymOffset
, isymMax
, swap
->external_sym_size
, PTR
);
1440 READ (external_opt
, cbOptOffset
, ioptMax
, swap
->external_opt_size
, PTR
);
1441 READ (external_aux
, cbAuxOffset
, iauxMax
, sizeof (union aux_ext
),
1443 READ (ss
, cbSsOffset
, issMax
, sizeof (char), char *);
1444 READ (ssext
, cbSsExtOffset
, issExtMax
, sizeof (char), char *);
1445 READ (external_fdr
, cbFdOffset
, ifdMax
, swap
->external_fdr_size
, PTR
);
1446 READ (external_rfd
, cbRfdOffset
, crfd
, swap
->external_rfd_size
, PTR
);
1447 READ (external_ext
, cbExtOffset
, iextMax
, swap
->external_ext_size
, PTR
);
1451 debug
->adjust
= NULL
;
1456 if (ext_hdr
!= NULL
)
1458 if (debug
->line
!= NULL
)
1460 if (debug
->external_dnr
!= NULL
)
1461 free (debug
->external_dnr
);
1462 if (debug
->external_pdr
!= NULL
)
1463 free (debug
->external_pdr
);
1464 if (debug
->external_sym
!= NULL
)
1465 free (debug
->external_sym
);
1466 if (debug
->external_opt
!= NULL
)
1467 free (debug
->external_opt
);
1468 if (debug
->external_aux
!= NULL
)
1469 free (debug
->external_aux
);
1470 if (debug
->ss
!= NULL
)
1472 if (debug
->ssext
!= NULL
)
1473 free (debug
->ssext
);
1474 if (debug
->external_fdr
!= NULL
)
1475 free (debug
->external_fdr
);
1476 if (debug
->external_rfd
!= NULL
)
1477 free (debug
->external_rfd
);
1478 if (debug
->external_ext
!= NULL
)
1479 free (debug
->external_ext
);
1483 /* The MIPS ELF linker needs additional information for each symbol in
1484 the global hash table. */
1486 struct mips_elf_link_hash_entry
1488 struct elf_link_hash_entry root
;
1490 /* External symbol information. */
1494 /* MIPS ELF linker hash table. */
1496 struct mips_elf_link_hash_table
1498 struct elf_link_hash_table root
;
1501 /* Look up an entry in a MIPS ELF linker hash table. */
1503 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1504 ((struct mips_elf_link_hash_entry *) \
1505 elf_link_hash_lookup (&(table)->root, (string), (create), \
1508 /* Traverse a MIPS ELF linker hash table. */
1510 #define mips_elf_link_hash_traverse(table, func, info) \
1511 (elf_link_hash_traverse \
1513 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1516 /* Get the MIPS ELF linker hash table from a link_info structure. */
1518 #define mips_elf_hash_table(p) \
1519 ((struct mips_elf_link_hash_table *) ((p)->hash))
1521 static boolean mips_elf_output_extsym
1522 PARAMS ((struct mips_elf_link_hash_entry
*, PTR
));
1524 /* Create an entry in a MIPS ELF linker hash table. */
1526 static struct bfd_hash_entry
*
1527 mips_elf_link_hash_newfunc (entry
, table
, string
)
1528 struct bfd_hash_entry
*entry
;
1529 struct bfd_hash_table
*table
;
1532 struct mips_elf_link_hash_entry
*ret
=
1533 (struct mips_elf_link_hash_entry
*) entry
;
1535 /* Allocate the structure if it has not already been allocated by a
1537 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
1538 ret
= ((struct mips_elf_link_hash_entry
*)
1539 bfd_hash_allocate (table
,
1540 sizeof (struct mips_elf_link_hash_entry
)));
1541 if (ret
== (struct mips_elf_link_hash_entry
*) NULL
)
1543 bfd_set_error (bfd_error_no_memory
);
1544 return (struct bfd_hash_entry
*) ret
;
1547 /* Call the allocation method of the superclass. */
1548 ret
= ((struct mips_elf_link_hash_entry
*)
1549 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
1551 if (ret
!= (struct mips_elf_link_hash_entry
*) NULL
)
1553 /* Set local fields. */
1554 memset (&ret
->esym
, 0, sizeof (EXTR
));
1555 /* We use -2 as a marker to indicate that the information has
1556 not been set. -1 means there is no associated ifd. */
1560 return (struct bfd_hash_entry
*) ret
;
1563 /* Create a MIPS ELF linker hash table. */
1565 static struct bfd_link_hash_table
*
1566 mips_elf_link_hash_table_create (abfd
)
1569 struct mips_elf_link_hash_table
*ret
;
1571 ret
= ((struct mips_elf_link_hash_table
*)
1572 bfd_alloc (abfd
, sizeof (struct mips_elf_link_hash_table
)));
1573 if (ret
== (struct mips_elf_link_hash_table
*) NULL
)
1575 bfd_set_error (bfd_error_no_memory
);
1579 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
1580 mips_elf_link_hash_newfunc
))
1582 bfd_release (abfd
, ret
);
1586 return &ret
->root
.root
;
1589 /* Hook called by the linker routine which adds symbols from an object
1590 file. We must handle the special MIPS section numbers here. */
1594 mips_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
1596 struct bfd_link_info
*info
;
1597 const Elf_Internal_Sym
*sym
;
1603 switch (sym
->st_shndx
)
1606 /* Common symbols less than the GP size are automatically
1607 treated as SHN_MIPS_SCOMMON symbols. */
1608 if (sym
->st_size
> elf_gp_size (abfd
))
1611 case SHN_MIPS_SCOMMON
:
1612 *secp
= bfd_make_section_old_way (abfd
, ".scommon");
1613 (*secp
)->flags
|= SEC_IS_COMMON
;
1614 *valp
= sym
->st_size
;
1617 case SHN_MIPS_SUNDEFINED
:
1618 *secp
= bfd_und_section_ptr
;
1625 /* Structure used to pass information to mips_elf_output_extsym. */
1630 struct bfd_link_info
*info
;
1631 struct ecoff_debug_info
*debug
;
1632 const struct ecoff_debug_swap
*swap
;
1636 /* This routine is used to write out ECOFF debugging external symbol
1637 information. It is called via mips_elf_link_hash_traverse. The
1638 ECOFF external symbol information must match the ELF external
1639 symbol information. Unfortunately, at this point we don't know
1640 whether a symbol is required by reloc information, so the two
1641 tables may wind up being different. We must sort out the external
1642 symbol information before we can set the final size of the .mdebug
1643 section, and we must set the size of the .mdebug section before we
1644 can relocate any sections, and we can't know which symbols are
1645 required by relocation until we relocate the sections.
1646 Fortunately, it is relatively unlikely that any symbol will be
1647 stripped but required by a reloc. In particular, it can not happen
1648 when generating a final executable. */
1651 mips_elf_output_extsym (h
, data
)
1652 struct mips_elf_link_hash_entry
*h
;
1655 struct extsym_info
*einfo
= (struct extsym_info
*) data
;
1658 if (h
->root
.indx
== -2)
1660 else if (((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1661 || (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
1662 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
1663 && (h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
1665 else if (einfo
->info
->strip
== strip_all
1666 || (einfo
->info
->strip
== strip_some
1667 && bfd_hash_lookup (einfo
->info
->keep_hash
,
1668 h
->root
.root
.root
.string
,
1669 false, false) == NULL
))
1677 if (h
->esym
.ifd
== -2)
1680 h
->esym
.cobol_main
= 0;
1681 h
->esym
.weakext
= 0;
1682 h
->esym
.reserved
= 0;
1683 h
->esym
.ifd
= ifdNil
;
1684 h
->esym
.asym
.value
= 0;
1685 h
->esym
.asym
.st
= stGlobal
;
1687 if (h
->root
.root
.type
!= bfd_link_hash_defined
)
1688 h
->esym
.asym
.sc
= scAbs
;
1691 asection
*output_section
;
1694 output_section
= h
->root
.root
.u
.def
.section
->output_section
;
1695 name
= bfd_section_name (output_section
->owner
, output_section
);
1697 if (strcmp (name
, ".text") == 0)
1698 h
->esym
.asym
.sc
= scText
;
1699 else if (strcmp (name
, ".data") == 0)
1700 h
->esym
.asym
.sc
= scData
;
1701 else if (strcmp (name
, ".sdata") == 0)
1702 h
->esym
.asym
.sc
= scSData
;
1703 else if (strcmp (name
, ".rodata") == 0
1704 || strcmp (name
, ".rdata") == 0)
1705 h
->esym
.asym
.sc
= scRData
;
1706 else if (strcmp (name
, ".bss") == 0)
1707 h
->esym
.asym
.sc
= scBss
;
1708 else if (strcmp (name
, ".sbss") == 0)
1709 h
->esym
.asym
.sc
= scSBss
;
1710 else if (strcmp (name
, ".init") == 0)
1711 h
->esym
.asym
.sc
= scInit
;
1712 else if (strcmp (name
, ".fini") == 0)
1713 h
->esym
.asym
.sc
= scFini
;
1715 h
->esym
.asym
.sc
= scAbs
;
1718 h
->esym
.asym
.reserved
= 0;
1719 h
->esym
.asym
.index
= indexNil
;
1723 if (! bfd_ecoff_debug_one_external (einfo
->abfd
, einfo
->debug
, einfo
->swap
,
1724 h
->root
.root
.root
.string
,
1727 einfo
->failed
= true;
1734 /* A comparison routine used to sort .gptab entries. */
1737 gptab_compare (p1
, p2
)
1741 const Elf32_gptab
*a1
= (const Elf32_gptab
*) p1
;
1742 const Elf32_gptab
*a2
= (const Elf32_gptab
*) p2
;
1744 return a1
->gt_entry
.gt_g_value
- a2
->gt_entry
.gt_g_value
;
1747 /* We need to use a special link routine to handle the .reginfo and
1748 the .mdebug sections. We need to merge all instances of these
1749 sections together, not write them all out sequentially. */
1752 mips_elf_final_link (abfd
, info
)
1754 struct bfd_link_info
*info
;
1758 struct bfd_link_order
*p
;
1759 asection
*reginfo_sec
, *mdebug_sec
, *gptab_data_sec
, *gptab_bss_sec
;
1760 Elf32_RegInfo reginfo
;
1761 struct ecoff_debug_info debug
;
1762 const struct ecoff_debug_swap
*swap
1763 = get_elf_backend_data (abfd
)->elf_backend_ecoff_debug_swap
;
1764 HDRR
*symhdr
= &debug
.symbolic_header
;
1765 PTR mdebug_handle
= NULL
;
1767 /* Drop the .options section, since it has special semantics which I
1768 haven't bothered to figure out. */
1769 for (secpp
= &abfd
->sections
; *secpp
!= NULL
; secpp
= &(*secpp
)->next
)
1771 if (strcmp ((*secpp
)->name
, ".options") == 0)
1773 for (p
= (*secpp
)->link_order_head
; p
!= NULL
; p
= p
->next
)
1774 if (p
->type
== bfd_indirect_link_order
)
1775 p
->u
.indirect
.section
->flags
&=~ SEC_HAS_CONTENTS
;
1776 (*secpp
)->link_order_head
= NULL
;
1777 *secpp
= (*secpp
)->next
;
1778 --abfd
->section_count
;
1783 /* Go through the sections and collect the .reginfo and .mdebug
1787 gptab_data_sec
= NULL
;
1788 gptab_bss_sec
= NULL
;
1789 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
1791 if (strcmp (o
->name
, ".reginfo") == 0)
1793 memset (®info
, 0, sizeof reginfo
);
1795 /* We have found the .reginfo section in the output file.
1796 Look through all the link_orders comprising it and merge
1797 the information together. */
1798 for (p
= o
->link_order_head
;
1799 p
!= (struct bfd_link_order
*) NULL
;
1802 asection
*input_section
;
1804 Elf32_External_RegInfo ext
;
1807 if (p
->type
!= bfd_indirect_link_order
)
1809 if (p
->type
== bfd_fill_link_order
)
1814 input_section
= p
->u
.indirect
.section
;
1815 input_bfd
= input_section
->owner
;
1816 BFD_ASSERT (input_section
->_raw_size
1817 == sizeof (Elf32_External_RegInfo
));
1818 if (! bfd_get_section_contents (input_bfd
, input_section
,
1824 bfd_mips_elf32_swap_reginfo_in (input_bfd
, &ext
, &sub
);
1826 reginfo
.ri_gprmask
|= sub
.ri_gprmask
;
1827 reginfo
.ri_cprmask
[0] |= sub
.ri_cprmask
[0];
1828 reginfo
.ri_cprmask
[1] |= sub
.ri_cprmask
[1];
1829 reginfo
.ri_cprmask
[2] |= sub
.ri_cprmask
[2];
1830 reginfo
.ri_cprmask
[3] |= sub
.ri_cprmask
[3];
1832 /* ri_gp_value is set by the function
1833 mips_elf_section_processing when the section is
1834 finally written out. */
1836 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1837 elf_link_input_bfd ignores this section. */
1838 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
1841 /* Force the section size to the value we want. */
1842 o
->_raw_size
= sizeof (Elf32_External_RegInfo
);
1844 /* Skip this section later on (I don't think this currently
1845 matters, but someday it might). */
1846 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
1851 if (strcmp (o
->name
, ".mdebug") == 0)
1853 struct extsym_info einfo
;
1855 /* We have found the .mdebug section in the output file.
1856 Look through all the link_orders comprising it and merge
1857 the information together. */
1858 symhdr
->magic
= swap
->sym_magic
;
1859 /* FIXME: What should the version stamp be? */
1861 symhdr
->ilineMax
= 0;
1865 symhdr
->isymMax
= 0;
1866 symhdr
->ioptMax
= 0;
1867 symhdr
->iauxMax
= 0;
1869 symhdr
->issExtMax
= 0;
1872 symhdr
->iextMax
= 0;
1874 /* We accumulate the debugging information itself in the
1875 debug_info structure. */
1877 debug
.external_dnr
= NULL
;
1878 debug
.external_pdr
= NULL
;
1879 debug
.external_sym
= NULL
;
1880 debug
.external_opt
= NULL
;
1881 debug
.external_aux
= NULL
;
1883 debug
.ssext
= debug
.ssext_end
= NULL
;
1884 debug
.external_fdr
= NULL
;
1885 debug
.external_rfd
= NULL
;
1886 debug
.external_ext
= debug
.external_ext_end
= NULL
;
1888 mdebug_handle
= bfd_ecoff_debug_init (abfd
, &debug
, swap
, info
);
1889 if (mdebug_handle
== (PTR
) NULL
)
1892 for (p
= o
->link_order_head
;
1893 p
!= (struct bfd_link_order
*) NULL
;
1896 asection
*input_section
;
1898 const struct ecoff_debug_swap
*input_swap
;
1899 struct ecoff_debug_info input_debug
;
1903 if (p
->type
!= bfd_indirect_link_order
)
1905 if (p
->type
== bfd_fill_link_order
)
1910 input_section
= p
->u
.indirect
.section
;
1911 input_bfd
= input_section
->owner
;
1913 if (bfd_get_flavour (input_bfd
) != bfd_target_elf_flavour
1914 || (get_elf_backend_data (input_bfd
)
1915 ->elf_backend_ecoff_debug_swap
) == NULL
)
1917 /* I don't know what a non MIPS ELF bfd would be
1918 doing with a .mdebug section, but I don't really
1919 want to deal with it. */
1923 input_swap
= (get_elf_backend_data (input_bfd
)
1924 ->elf_backend_ecoff_debug_swap
);
1926 BFD_ASSERT (p
->size
== input_section
->_raw_size
);
1928 /* The ECOFF linking code expects that we have already
1929 read in the debugging information and set up an
1930 ecoff_debug_info structure, so we do that now. */
1931 if (! mips_elf_read_ecoff_info (input_bfd
, input_section
,
1935 if (! (bfd_ecoff_debug_accumulate
1936 (mdebug_handle
, abfd
, &debug
, swap
, input_bfd
,
1937 &input_debug
, input_swap
, info
)))
1940 /* Loop through the external symbols. For each one with
1941 interesting information, try to find the symbol in
1942 the linker global hash table and save the information
1943 for the output external symbols. */
1944 eraw_src
= input_debug
.external_ext
;
1945 eraw_end
= (eraw_src
1946 + (input_debug
.symbolic_header
.iextMax
1947 * input_swap
->external_ext_size
));
1949 eraw_src
< eraw_end
;
1950 eraw_src
+= input_swap
->external_ext_size
)
1954 struct mips_elf_link_hash_entry
*h
;
1956 (*input_swap
->swap_ext_in
) (input_bfd
, (PTR
) eraw_src
, &ext
);
1957 if (ext
.asym
.sc
== scNil
1958 || ext
.asym
.sc
== scUndefined
1959 || ext
.asym
.sc
== scSUndefined
)
1962 name
= input_debug
.ssext
+ ext
.asym
.iss
;
1963 h
= mips_elf_link_hash_lookup (mips_elf_hash_table (info
),
1964 name
, false, false, true);
1965 if (h
== NULL
|| h
->esym
.ifd
!= -2)
1971 < input_debug
.symbolic_header
.ifdMax
);
1972 ext
.ifd
= input_debug
.ifdmap
[ext
.ifd
];
1978 /* Free up the information we just read. */
1979 free (input_debug
.line
);
1980 free (input_debug
.external_dnr
);
1981 free (input_debug
.external_pdr
);
1982 free (input_debug
.external_sym
);
1983 free (input_debug
.external_opt
);
1984 free (input_debug
.external_aux
);
1985 free (input_debug
.ss
);
1986 free (input_debug
.ssext
);
1987 free (input_debug
.external_fdr
);
1988 free (input_debug
.external_rfd
);
1989 free (input_debug
.external_ext
);
1991 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1992 elf_link_input_bfd ignores this section. */
1993 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
1996 /* Build the external symbol information. */
1999 einfo
.debug
= &debug
;
2001 einfo
.failed
= false;
2002 mips_elf_link_hash_traverse (mips_elf_hash_table (info
),
2003 mips_elf_output_extsym
,
2008 /* Set the size of the .mdebug section. */
2009 o
->_raw_size
= bfd_ecoff_debug_size (abfd
, &debug
, swap
);
2011 /* Skip this section later on (I don't think this currently
2012 matters, but someday it might). */
2013 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2018 if (strncmp (o
->name
, ".gptab.", sizeof ".gptab." - 1) == 0)
2020 const char *subname
;
2023 Elf32_External_gptab
*ext_tab
;
2026 /* The .gptab.sdata and .gptab.sbss sections hold
2027 information describing how the small data area would
2028 change depending upon the -G switch. These sections
2029 not used in executables files. */
2030 if (! info
->relocateable
)
2034 for (p
= o
->link_order_head
;
2035 p
!= (struct bfd_link_order
*) NULL
;
2038 asection
*input_section
;
2040 if (p
->type
!= bfd_indirect_link_order
)
2042 if (p
->type
== bfd_fill_link_order
)
2047 input_section
= p
->u
.indirect
.section
;
2049 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2050 elf_link_input_bfd ignores this section. */
2051 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2054 /* Skip this section later on (I don't think this
2055 currently matters, but someday it might). */
2056 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2058 /* Really remove the section. */
2059 for (secpp
= &abfd
->sections
;
2061 secpp
= &(*secpp
)->next
)
2063 *secpp
= (*secpp
)->next
;
2064 --abfd
->section_count
;
2069 /* There is one gptab for initialized data, and one for
2070 uninitialized data. */
2071 if (strcmp (o
->name
, ".gptab.sdata") == 0)
2073 else if (strcmp (o
->name
, ".gptab.sbss") == 0)
2077 bfd_set_error (bfd_error_nonrepresentable_section
);
2081 /* The linker script always combines .gptab.data and
2082 .gptab.sdata into .gptab.sdata, and likewise for
2083 .gptab.bss and .gptab.sbss. It is possible that there is
2084 no .sdata or .sbss section in the output file, in which
2085 case we must change the name of the output section. */
2086 subname
= o
->name
+ sizeof ".gptab" - 1;
2087 if (bfd_get_section_by_name (abfd
, subname
) == NULL
)
2089 if (o
== gptab_data_sec
)
2090 o
->name
= ".gptab.data";
2092 o
->name
= ".gptab.bss";
2093 subname
= o
->name
+ sizeof ".gptab" - 1;
2094 BFD_ASSERT (bfd_get_section_by_name (abfd
, subname
) != NULL
);
2097 /* Set up the first entry. */
2099 tab
= (Elf32_gptab
*) malloc (c
* sizeof (Elf32_gptab
));
2102 bfd_set_error (bfd_error_no_memory
);
2105 tab
[0].gt_header
.gt_current_g_value
= elf_gp_size (abfd
);
2106 tab
[0].gt_header
.gt_unused
= 0;
2108 /* Combine the input sections. */
2109 for (p
= o
->link_order_head
;
2110 p
!= (struct bfd_link_order
*) NULL
;
2113 asection
*input_section
;
2117 bfd_size_type gpentry
;
2119 if (p
->type
!= bfd_indirect_link_order
)
2121 if (p
->type
== bfd_fill_link_order
)
2126 input_section
= p
->u
.indirect
.section
;
2127 input_bfd
= input_section
->owner
;
2129 /* Combine the gptab entries for this input section one
2130 by one. We know that the input gptab entries are
2131 sorted by ascending -G value. */
2132 size
= bfd_section_size (input_bfd
, input_section
);
2134 for (gpentry
= sizeof (Elf32_External_gptab
);
2136 gpentry
+= sizeof (Elf32_External_gptab
))
2138 Elf32_External_gptab ext_gptab
;
2139 Elf32_gptab int_gptab
;
2145 if (! (bfd_get_section_contents
2146 (input_bfd
, input_section
, (PTR
) &ext_gptab
,
2147 gpentry
, sizeof (Elf32_External_gptab
))))
2153 bfd_mips_elf32_swap_gptab_in (input_bfd
, &ext_gptab
,
2155 val
= int_gptab
.gt_entry
.gt_g_value
;
2156 add
= int_gptab
.gt_entry
.gt_bytes
- last
;
2159 for (look
= 1; look
< c
; look
++)
2161 if (tab
[look
].gt_entry
.gt_g_value
>= val
)
2162 tab
[look
].gt_entry
.gt_bytes
+= add
;
2164 if (tab
[look
].gt_entry
.gt_g_value
== val
)
2170 Elf32_gptab
*new_tab
;
2173 /* We need a new table entry. */
2174 new_tab
= ((Elf32_gptab
*)
2176 (c
+ 1) * sizeof (Elf32_gptab
)));
2177 if (new_tab
== NULL
)
2179 bfd_set_error (bfd_error_no_memory
);
2184 tab
[c
].gt_entry
.gt_g_value
= val
;
2185 tab
[c
].gt_entry
.gt_bytes
= add
;
2187 /* Merge in the size for the next smallest -G
2188 value, since that will be implied by this new
2191 for (look
= 1; look
< c
; look
++)
2193 if (tab
[look
].gt_entry
.gt_g_value
< val
2195 || (tab
[look
].gt_entry
.gt_g_value
2196 > tab
[max
].gt_entry
.gt_g_value
)))
2200 tab
[c
].gt_entry
.gt_bytes
+=
2201 tab
[max
].gt_entry
.gt_bytes
;
2206 last
= int_gptab
.gt_entry
.gt_bytes
;
2209 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2210 elf_link_input_bfd ignores this section. */
2211 input_section
->flags
&=~ SEC_HAS_CONTENTS
;
2214 /* The table must be sorted by -G value. */
2216 qsort (tab
+ 1, c
- 1, sizeof (tab
[0]), gptab_compare
);
2218 /* Swap out the table. */
2219 ext_tab
= ((Elf32_External_gptab
*)
2220 bfd_alloc (abfd
, c
* sizeof (Elf32_External_gptab
)));
2221 if (ext_tab
== NULL
)
2223 bfd_set_error (bfd_error_no_memory
);
2228 for (i
= 0; i
< c
; i
++)
2229 bfd_mips_elf32_swap_gptab_out (abfd
, tab
+ i
, ext_tab
+ i
);
2232 o
->_raw_size
= c
* sizeof (Elf32_External_gptab
);
2233 o
->contents
= (bfd_byte
*) ext_tab
;
2235 /* Skip this section later on (I don't think this currently
2236 matters, but someday it might). */
2237 o
->link_order_head
= (struct bfd_link_order
*) NULL
;
2241 /* Get a value for the GP register. */
2242 if (elf_gp (abfd
) == 0)
2244 struct bfd_link_hash_entry
*h
;
2246 h
= bfd_link_hash_lookup (info
->hash
, "_gp", false, false, true);
2247 if (h
!= (struct bfd_link_hash_entry
*) NULL
2248 && h
->type
== bfd_link_hash_defined
)
2249 elf_gp (abfd
) = (h
->u
.def
.value
2250 + h
->u
.def
.section
->output_section
->vma
2251 + h
->u
.def
.section
->output_offset
);
2252 else if (info
->relocateable
)
2256 /* Make up a value. */
2258 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2261 && (strcmp (o
->name
, ".sbss") == 0
2262 || strcmp (o
->name
, ".sdata") == 0
2263 || strcmp (o
->name
, ".lit4") == 0
2264 || strcmp (o
->name
, ".lit8") == 0))
2267 elf_gp (abfd
) = lo
+ 0x8000;
2271 /* If the relocate_section function needs to do a reloc
2272 involving the GP value, it should make a reloc_dangerous
2273 callback to warn that GP is not defined. */
2277 /* Invoke the regular ELF backend linker to do all the work. */
2278 if (! bfd_elf32_bfd_final_link (abfd
, info
))
2281 /* Now write out the computed sections. */
2283 if (reginfo_sec
!= (asection
*) NULL
)
2285 Elf32_External_RegInfo ext
;
2287 bfd_mips_elf32_swap_reginfo_out (abfd
, ®info
, &ext
);
2288 if (! bfd_set_section_contents (abfd
, reginfo_sec
, (PTR
) &ext
,
2289 (file_ptr
) 0, sizeof ext
))
2293 if (mdebug_sec
!= (asection
*) NULL
)
2295 BFD_ASSERT (abfd
->output_has_begun
);
2296 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle
, abfd
, &debug
,
2298 mdebug_sec
->filepos
))
2301 bfd_ecoff_debug_free (mdebug_handle
, abfd
, &debug
, swap
, info
);
2304 if (gptab_data_sec
!= (asection
*) NULL
)
2306 if (! bfd_set_section_contents (abfd
, gptab_data_sec
,
2307 gptab_data_sec
->contents
,
2309 gptab_data_sec
->_raw_size
))
2313 if (gptab_bss_sec
!= (asection
*) NULL
)
2315 if (! bfd_set_section_contents (abfd
, gptab_bss_sec
,
2316 gptab_bss_sec
->contents
,
2318 gptab_bss_sec
->_raw_size
))
2325 /* Handle a MIPS ELF HI16 reloc. */
2328 mips_elf_relocate_hi16 (input_bfd
, relhi
, rello
, contents
, addend
)
2330 Elf_Internal_Rela
*relhi
;
2331 Elf_Internal_Rela
*rello
;
2338 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
2340 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
2343 addend
+= ((insn
& 0xffff) << 16) + addlo
;
2345 if ((addlo
& 0x8000) != 0)
2347 if ((addend
& 0x8000) != 0)
2350 bfd_put_32 (input_bfd
,
2351 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
2352 contents
+ relhi
->r_offset
);
2355 /* Relocate a MIPS ELF section. */
2358 mips_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
2359 contents
, relocs
, local_syms
, local_sections
)
2361 struct bfd_link_info
*info
;
2363 asection
*input_section
;
2365 Elf_Internal_Rela
*relocs
;
2366 Elf_Internal_Sym
*local_syms
;
2367 asection
**local_sections
;
2369 Elf_Internal_Shdr
*symtab_hdr
;
2372 Elf_Internal_Rela
*rel
;
2373 Elf_Internal_Rela
*relend
;
2375 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2377 if (elf_bad_symtab (input_bfd
))
2379 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
2384 locsymcount
= symtab_hdr
->sh_info
;
2385 extsymoff
= symtab_hdr
->sh_info
;
2389 relend
= relocs
+ input_section
->reloc_count
;
2390 for (; rel
< relend
; rel
++)
2393 reloc_howto_type
*howto
;
2396 struct elf_link_hash_entry
*h
;
2398 Elf_Internal_Sym
*sym
;
2399 bfd_reloc_status_type r
;
2401 r_type
= ELF32_R_TYPE (rel
->r_info
);
2402 if (r_type
< 0 || r_type
>= (int) R_MIPS_max
)
2404 bfd_set_error (bfd_error_bad_value
);
2407 howto
= elf_mips_howto_table
+ r_type
;
2409 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2411 /* Mix in the change in GP address for a GP relative reloc. */
2412 if (r_type
!= R_MIPS_GPREL16
2413 && r_type
!= R_MIPS_LITERAL
2414 && r_type
!= R_MIPS_GPREL32
)
2418 if (elf_gp (output_bfd
) == 0)
2420 if (! ((*info
->callbacks
->reloc_dangerous
)
2422 "GP relative relocation when GP not defined",
2423 input_bfd
, input_section
,
2426 /* Only give the error once per link. */
2427 elf_gp (output_bfd
) = 4;
2430 if (r_symndx
< extsymoff
2431 || (elf_bad_symtab (input_bfd
)
2432 && local_sections
[r_symndx
] != NULL
))
2434 /* This is a relocation against a section. The current
2435 addend in the instruction is the difference between
2436 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
2437 must change this to be the difference between the
2438 final definition (which will end up in RELOCATION)
2439 and the GP value of OUTPUT_BFD (which is in GP). */
2440 addend
= elf_gp (input_bfd
) - elf_gp (output_bfd
);
2442 else if (! info
->relocateable
)
2444 /* We are doing a final link. The current addend in the
2445 instruction is simply the desired offset into the
2446 symbol (normally zero). We want the instruction to
2447 hold the difference between the final definition of
2448 the symbol (which will end up in RELOCATION) and the
2449 GP value of OUTPUT_BFD (which is in GP). */
2450 addend
= - elf_gp (output_bfd
);
2454 /* We are generating relocateable output, and we aren't
2455 going to define this symbol, so we just leave the
2456 instruction alone. */
2464 if (info
->relocateable
)
2466 /* This is a relocateable link. We don't have to change
2467 anything, unless the reloc is against a section symbol,
2468 in which case we have to adjust according to where the
2469 section symbol winds up in the output section. */
2470 if (r_symndx
>= locsymcount
2471 || (elf_bad_symtab (input_bfd
)
2472 && local_sections
[r_symndx
] == NULL
))
2476 sym
= local_syms
+ r_symndx
;
2477 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2481 sec
= local_sections
[r_symndx
];
2483 /* It would be logical to add sym->st_value here,
2484 but Irix 5 sometimes generates a garbage symbol
2486 addend
+= sec
->output_offset
;
2488 /* If this is HI16 with an associated LO16, adjust
2489 the addend accordingly. Otherwise, just
2491 if (r_type
!= R_MIPS_HI16
2492 || (rel
+ 1) >= relend
2493 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
2494 r
= _bfd_relocate_contents (howto
, input_bfd
,
2496 contents
+ rel
->r_offset
);
2499 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
2510 /* This is a final link. */
2512 if (r_symndx
< extsymoff
2513 || (elf_bad_symtab (input_bfd
)
2514 && local_sections
[r_symndx
] != NULL
))
2516 sym
= local_syms
+ r_symndx
;
2517 sec
= local_sections
[r_symndx
];
2518 relocation
= (sec
->output_section
->vma
2519 + sec
->output_offset
);
2521 /* It would be logical to always add sym->st_value here,
2522 but Irix 5 sometimes generates a garbage symbol
2524 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2525 relocation
+= sym
->st_value
;
2531 indx
= r_symndx
- extsymoff
;
2532 h
= elf_sym_hashes (input_bfd
)[indx
];
2533 if (h
->root
.type
== bfd_link_hash_defined
)
2535 sec
= h
->root
.u
.def
.section
;
2536 relocation
= (h
->root
.u
.def
.value
2537 + sec
->output_section
->vma
2538 + sec
->output_offset
);
2540 else if (h
->root
.type
== bfd_link_hash_weak
)
2544 if (! ((*info
->callbacks
->undefined_symbol
)
2545 (info
, h
->root
.root
.string
, input_bfd
,
2546 input_section
, rel
->r_offset
)))
2552 if (r_type
!= R_MIPS_HI16
2553 || (rel
+ 1) >= relend
2554 || ELF32_R_TYPE ((rel
+ 1)->r_info
) != R_MIPS_LO16
)
2555 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2556 contents
, rel
->r_offset
,
2557 relocation
, addend
);
2560 mips_elf_relocate_hi16 (input_bfd
, rel
, rel
+ 1,
2561 contents
, relocation
+ addend
);
2566 if (r
!= bfd_reloc_ok
)
2571 case bfd_reloc_outofrange
:
2573 case bfd_reloc_overflow
:
2578 name
= h
->root
.root
.string
;
2581 name
= elf_string_from_elf_section (input_bfd
,
2582 symtab_hdr
->sh_link
,
2587 name
= bfd_section_name (input_bfd
, sec
);
2589 if (! ((*info
->callbacks
->reloc_overflow
)
2590 (info
, name
, howto
->name
, (bfd_vma
) 0,
2591 input_bfd
, input_section
, rel
->r_offset
)))
2602 /* ECOFF swapping routines. These are used when dealing with the
2603 .mdebug section, which is in the ECOFF debugging format. */
2604 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap
=
2606 /* Symbol table magic number. */
2608 /* Alignment of debugging information. E.g., 4. */
2610 /* Sizes of external symbolic information. */
2611 sizeof (struct hdr_ext
),
2612 sizeof (struct dnr_ext
),
2613 sizeof (struct pdr_ext
),
2614 sizeof (struct sym_ext
),
2615 sizeof (struct opt_ext
),
2616 sizeof (struct fdr_ext
),
2617 sizeof (struct rfd_ext
),
2618 sizeof (struct ext_ext
),
2619 /* Functions to swap in external symbolic data. */
2628 _bfd_ecoff_swap_tir_in
,
2629 _bfd_ecoff_swap_rndx_in
,
2630 /* Functions to swap out external symbolic data. */
2639 _bfd_ecoff_swap_tir_out
,
2640 _bfd_ecoff_swap_rndx_out
,
2641 /* Function to read in symbolic data. */
2642 mips_elf_read_ecoff_info
2645 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
2646 #define TARGET_LITTLE_NAME "elf32-littlemips"
2647 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
2648 #define TARGET_BIG_NAME "elf32-bigmips"
2649 #define ELF_ARCH bfd_arch_mips
2650 #define ELF_MACHINE_CODE EM_MIPS
2651 #define ELF_MAXPAGESIZE 0x10000
2652 #define elf_backend_collect true
2653 #define elf_info_to_howto 0
2654 #define elf_info_to_howto_rel mips_info_to_howto_rel
2655 #define elf_backend_sym_is_global mips_elf_sym_is_global
2656 #define elf_backend_object_p mips_elf_object_p
2657 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
2658 #define elf_backend_fake_sections mips_elf_fake_sections
2659 #define elf_backend_section_from_bfd_section \
2660 mips_elf_section_from_bfd_section
2661 #define elf_backend_section_processing mips_elf_section_processing
2662 #define elf_backend_symbol_processing mips_elf_symbol_processing
2663 #define elf_backend_final_write_processing \
2664 mips_elf_final_write_processing
2665 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
2667 #define bfd_elf32_bfd_link_hash_table_create \
2668 mips_elf_link_hash_table_create
2669 #define bfd_elf32_bfd_final_link mips_elf_final_link
2670 #define elf_backend_relocate_section mips_elf_relocate_section
2671 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
2673 #include "elf32-target.h"